Update PkmnLib to new functionality with capture mechanics
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2022-03-26 16:46:07 +01:00
parent cb3d3c74a1
commit 36b39ba3c4
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
28 changed files with 220 additions and 39 deletions

View File

@ -0,0 +1,47 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Battling.Events
{
public class CaptureAttemptEvent : EventData
{
private Pokemon? _pokemon;
private CaptureLibrary.CaptureResult? _result;
internal CaptureAttemptEvent(EventDataKind kind, IntPtr ptr) : base(kind, ptr)
{
}
public Pokemon Pokemon
{
get
{
if (_pokemon != null) return _pokemon;
var ptr = Pkmnlib.Generated.CaptureAttemptEvent.GetPokemon(Ptr);
if (TryResolvePointer(ptr, out _pokemon))
return _pokemon!;
_pokemon = Constructor.Active.ConstructPokemon(ptr)!;
return _pokemon;
}
}
public CaptureLibrary.CaptureResult Result
{
get
{
if (_result != null) return _result.Value;
var p = Pkmnlib.Generated.CaptureAttemptEvent.GetResult(Ptr);
_result = Marshal.PtrToStructure<CaptureLibrary.CaptureResult>(p);
return _result.Value;
}
}
protected override void DeletePtr()
{
Pkmnlib.Generated.CaptureAttemptEvent.Destruct(Ptr);
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Battling.Events
{
public class WeatherChangeEvent : EventData
{
internal WeatherChangeEvent(EventDataKind kind, IntPtr ptr) : base(kind, ptr)
{
}
public string? WeatherName => Pkmnlib.Generated.WeatherChangeEvent.GetWeatherName(Ptr).PtrString();
protected override void DeletePtr()
{
Pkmnlib.Generated.WeatherChangeEvent.Destruct(Ptr);
}
}
}

View File

@ -77,7 +77,7 @@ namespace PkmnLibSharp.Battling
return _experienceLibrary; return _experienceLibrary;
} }
} }
public ScriptResolver ScriptResolver public ScriptResolver ScriptResolver
{ {
get get
@ -92,18 +92,19 @@ namespace PkmnLibSharp.Battling
} }
} }
internal BattleLibrary(IntPtr ptr) : base(ptr) internal BattleLibrary(IntPtr ptr) : base(ptr)
{ {
} }
public BattleLibrary(PokemonLibrary staticLibrary, StatCalculator statCalculator, public BattleLibrary(PokemonLibrary staticLibrary, StatCalculator statCalculator, DamageLibrary damageLibrary,
DamageLibrary damageLibrary, ExperienceLibrary experienceLibrary, ScriptResolver scriptResolver, ExperienceLibrary experienceLibrary, ScriptResolver scriptResolver, MiscLibrary miscLibrary,
MiscLibrary miscLibrary) CaptureLibrary captureLibrary)
{ {
var ptr = IntPtr.Zero; var ptr = IntPtr.Zero;
Pkmnlib.Generated.BattleLibrary.Construct(ref ptr, staticLibrary.Ptr, statCalculator.Ptr, damageLibrary.Ptr, Pkmnlib.Generated.BattleLibrary.Construct(ref ptr, staticLibrary.Ptr, statCalculator.Ptr, damageLibrary.Ptr,
experienceLibrary.Ptr, scriptResolver.Ptr, miscLibrary.Ptr).Assert(); experienceLibrary.Ptr, scriptResolver.Ptr, miscLibrary.Ptr, captureLibrary.Ptr)
.Assert();
Initialize(ptr); Initialize(ptr);
_static = staticLibrary; _static = staticLibrary;
_statCalculator = statCalculator; _statCalculator = statCalculator;
@ -112,7 +113,7 @@ namespace PkmnLibSharp.Battling
_scriptResolver = scriptResolver; _scriptResolver = scriptResolver;
_miscLibrary = miscLibrary; _miscLibrary = miscLibrary;
} }
protected override void DeletePtr() protected override void DeletePtr()
{ {
Pkmnlib.Generated.BattleLibrary.Destruct(Ptr); Pkmnlib.Generated.BattleLibrary.Destruct(Ptr);

View File

@ -0,0 +1,37 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Battling
{
public class CaptureLibrary : PointerWrapper
{
[StructLayout(LayoutKind.Explicit)]
public struct CaptureResult
{
[FieldOffset(0)] private readonly byte _wasCaught;
[FieldOffset(1)] public readonly byte Shakes;
[FieldOffset(2)] private readonly byte _wasCritical;
public bool WasCaught => _wasCaught == 1;
public bool WasCritical => _wasCritical == 1;
}
internal CaptureLibrary(IntPtr ptr) : base(ptr)
{
}
public CaptureLibrary()
{
var ptr = this.Ptr;
Pkmnlib.Generated.CaptureLibrary.Construct(ref ptr);
Initialize(ptr);
}
protected override void DeletePtr()
{
Pkmnlib.Generated.CaptureLibrary.Destruct(Ptr);
}
}
}

View File

@ -348,6 +348,8 @@ namespace PkmnLibSharp.Battling
public string? StatusName => Creaturelib.Generated.Creature.GetStatusName(Ptr).PtrString(); public string? StatusName => Creaturelib.Generated.Creature.GetStatusName(Ptr).PtrString();
public bool WasCaught => Pkmnlib.Generated.Pokemon.WasCaught(Ptr) == 1;
public void Initialize() public void Initialize()
{ {
Creaturelib.Generated.Creature.Initialize(Ptr).Assert(); Creaturelib.Generated.Creature.Initialize(Ptr).Assert();
@ -531,6 +533,12 @@ namespace PkmnLibSharp.Battling
_forme = forme; _forme = forme;
} }
public void AttemptCapture(Item catchItem)
{
Pkmnlib.Generated.Pokemon.AttemptCapture(Ptr, catchItem.Ptr).Assert();
}
private Species? _displaySpecies; private Species? _displaySpecies;
private Forme? _displayForme; private Forme? _displayForme;
private Species? _species; private Species? _species;

View File

@ -4,7 +4,7 @@ using System.Diagnostics.CodeAnalysis;
namespace Creaturelib namespace Creaturelib
{ {
[SuppressMessage("ReSharper", "InconsistentNaming")] [SuppressMessage("ReSharper", "InconsistentNaming")]
internal enum AttackLearnMethod : int internal enum AttackLearnMethod : byte
{ {
Unknown = 0, Unknown = 0,
Level = 1, Level = 1,

View File

@ -25,9 +25,9 @@ namespace Creaturelib.Generated
/// <param name="chance">float</param> /// <param name="chance">float</param>
/// <param name="attack">ExecutingAttack *</param> /// <param name="attack">ExecutingAttack *</param>
/// <param name="target">Creature *</param> /// <param name="target">Creature *</param>
/// <returns>unsigned char</returns> /// <returns>signed char</returns>
[DllImport("libCreatureLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_BattleRandom_EffectChance")] [DllImport("libCreatureLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_BattleRandom_EffectChance")]
internal static extern byte EffectChance(ref byte @out, IntPtr p, float chance, IntPtr attack, IntPtr target); internal static extern sbyte EffectChance(ref byte @out, IntPtr p, float chance, IntPtr attack, IntPtr target);
/// <param name="p">BattleRandom *</param> /// <param name="p">BattleRandom *</param>
/// <returns>int</returns> /// <returns>int</returns>

View File

@ -4,7 +4,7 @@ using System.Diagnostics.CodeAnalysis;
namespace Pkmnlib namespace Pkmnlib
{ {
[SuppressMessage("ReSharper", "InconsistentNaming")] [SuppressMessage("ReSharper", "InconsistentNaming")]
internal enum AttackLearnMethod : int internal enum AttackLearnMethod : byte
{ {
Unknown = 0, Unknown = 0,
Level = 1, Level = 1,

View File

@ -13,9 +13,10 @@ namespace Pkmnlib.Generated
/// <param name="experienceLibrary">ExperienceLibrary *</param> /// <param name="experienceLibrary">ExperienceLibrary *</param>
/// <param name="scriptResolver">ScriptResolver *</param> /// <param name="scriptResolver">ScriptResolver *</param>
/// <param name="miscLibrary">MiscLibrary *</param> /// <param name="miscLibrary">MiscLibrary *</param>
/// <param name="captureLibrary">CaptureLibrary *</param>
/// <returns>unsigned char</returns> /// <returns>unsigned char</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_BattleLibrary_Construct")] [DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_BattleLibrary_Construct")]
internal static extern byte Construct(ref IntPtr @out, IntPtr staticLib, IntPtr statCalculator, IntPtr damageLibrary, IntPtr experienceLibrary, IntPtr scriptResolver, IntPtr miscLibrary); internal static extern byte Construct(ref IntPtr @out, IntPtr staticLib, IntPtr statCalculator, IntPtr damageLibrary, IntPtr experienceLibrary, IntPtr scriptResolver, IntPtr miscLibrary, IntPtr captureLibrary);
/// <param name="p">BattleLibrary *</param> /// <param name="p">BattleLibrary *</param>
/// <returns>void</returns> /// <returns>void</returns>

View File

@ -0,0 +1,25 @@
// AUTOMATICALLY GENERATED, DO NOT EDIT
using System;
using System.Runtime.InteropServices;
namespace Pkmnlib.Generated
{
internal static class CaptureAttemptEvent
{
/// <param name="p">CaptureAttemptEvent *</param>
/// <returns>void</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_CaptureAttemptEvent_Destruct")]
internal static extern void Destruct(IntPtr p);
/// <param name="p">CaptureAttemptEvent *</param>
/// <returns>const Pokemon *</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_CaptureAttemptEvent_GetPokemon")]
internal static extern IntPtr GetPokemon(IntPtr p);
/// <param name="p">CaptureAttemptEvent *</param>
/// <returns>const CaptureResult</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_CaptureAttemptEvent_GetResult")]
internal static extern IntPtr GetResult(IntPtr p);
}
}

View File

@ -0,0 +1,20 @@
// AUTOMATICALLY GENERATED, DO NOT EDIT
using System;
using System.Runtime.InteropServices;
namespace Pkmnlib.Generated
{
internal static class CaptureLibrary
{
/// <param name="out">CaptureLibrary * &</param>
/// <returns>unsigned char</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_CaptureLibrary_Construct")]
internal static extern byte Construct(ref IntPtr @out);
/// <param name="p">CaptureLibrary *</param>
/// <returns>void</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_CaptureLibrary_Destruct")]
internal static extern void Destruct(IntPtr p);
}
}

View File

@ -7,5 +7,6 @@ namespace Pkmnlib
internal enum PkmnEventDataKind : byte internal enum PkmnEventDataKind : byte
{ {
WeatherChange = 128, WeatherChange = 128,
CaptureAttempt = 129,
} }
} }

View File

@ -50,6 +50,11 @@ namespace Pkmnlib.Generated
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_IsShiny")] [DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_IsShiny")]
internal static extern byte IsShiny(IntPtr p); internal static extern byte IsShiny(IntPtr p);
/// <param name="p">const Pokemon *</param>
/// <returns>bool</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_WasCaught")]
internal static extern byte WasCaught(IntPtr p);
/// <param name="p">const Pokemon *</param> /// <param name="p">const Pokemon *</param>
/// <returns>const Nature *</returns> /// <returns>const Nature *</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_GetNature")] [DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_GetNature")]
@ -116,5 +121,11 @@ namespace Pkmnlib.Generated
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_Evolve")] [DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_Evolve")]
internal static extern byte Evolve(IntPtr p, IntPtr species, IntPtr forme); internal static extern byte Evolve(IntPtr p, IntPtr species, IntPtr forme);
/// <param name="p">Pokemon *</param>
/// <param name="catchItem">Item *</param>
/// <returns>unsigned char</returns>
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_AttemptCapture")]
internal static extern byte AttemptCapture(IntPtr p, IntPtr catchItem);
} }
} }

BIN
PkmnLibSharp/Native/Linux/libArbutils.so (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

BIN
PkmnLibSharp/Native/Linux/libpkmnLib.so (Stored with Git LFS)

Binary file not shown.

BIN
PkmnLibSharp/Native/Linux/libpkmnlib_ai.so (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
PkmnLibSharp/Native/Windows/libpkmnLib.dll (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

View File

@ -5,7 +5,7 @@
<Platforms>x64</Platforms> <Platforms>x64</Platforms>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8601;CS8602;CS8603;CS8604;CS8618</WarningsAsErrors> <WarningsAsErrors>CS8600;CS8601;CS8602;CS8603;CS8604;CS8618</WarningsAsErrors>
<LangVersion>9</LangVersion> <LangVersion>10</LangVersion>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks> <TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup> </PropertyGroup>

File diff suppressed because one or more lines are too long

View File

@ -104,7 +104,7 @@ def parse_type(type, enumSet):
if (type in enumSet): if (type in enumSet):
return type return type
print("Unhandled type '{}'".format(type)) print("Unhandled type '{}'".format(type))
return "void" return "IntPtr"
def clean_name(name): def clean_name(name):
if (name == "out"): if (name == "out"):

File diff suppressed because one or more lines are too long

View File

@ -24,7 +24,7 @@ namespace PkmnLibSharpTests.Battling
TestContext.WriteLine("Building battle library"); TestContext.WriteLine("Building battle library");
var scriptLibrary = new AngelScriptResolver(); var scriptLibrary = new AngelScriptResolver();
_cache = new BattleLibrary(BuildStatic(), new StatCalculator(), new DamageLibrary(), _cache = new BattleLibrary(BuildStatic(), new StatCalculator(), new DamageLibrary(),
new ExperienceLibrary(), scriptLibrary, new MiscLibrary(GetTime)); new ExperienceLibrary(), scriptLibrary, new MiscLibrary(GetTime), new CaptureLibrary());
scriptLibrary.Initialize(_cache); scriptLibrary.Initialize(_cache);
return _cache; return _cache;
} }

View File

@ -1,6 +1,9 @@
using System; using System;
using System.Diagnostics;
using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Loader;
using NUnit.Framework; using NUnit.Framework;
using PkmnLibSharp.Battling; using PkmnLibSharp.Battling;
using PkmnLibSharp.Utilities; using PkmnLibSharp.Utilities;
@ -14,10 +17,10 @@ namespace PkmnLibSharpTests
[OneTimeSetUp] [OneTimeSetUp]
public void Init() public void Init()
{ {
NativeLibrary.Load("Arbutils", Assembly.GetCallingAssembly(), DllImportSearchPath.AssemblyDirectory); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
NativeLibrary.Load("CreatureLib", Assembly.GetCallingAssembly(), DllImportSearchPath.AssemblyDirectory); {
NativeLibrary.Load("libangelscript.so.2.35.0", Assembly.GetCallingAssembly(), DllImportSearchPath.AssemblyDirectory); NativeLibrary.SetDllImportResolver(assembly, ImportResolver);
NativeLibrary.Load("pkmnLib", Assembly.GetCallingAssembly(), DllImportSearchPath.AssemblyDirectory); }
LogHandler.RegisterListener((level, s) => LogHandler.RegisterListener((level, s) =>
{ {
@ -26,6 +29,14 @@ namespace PkmnLibSharpTests
SignalHandler.SetSignalListener(s => throw new Exception("Encountered a catastrophic signal. \n" + s)); SignalHandler.SetSignalListener(s => throw new Exception("Encountered a catastrophic signal. \n" + s));
} }
private IntPtr ImportResolver(string libraryname, Assembly assembly, DllImportSearchPath? searchpath)
{
var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var assemblyFile = Environment.OSVersion.Platform == PlatformID.Unix
? Path.Join(directory, libraryname + ".so")
: Path.Join(directory, libraryname + ".dll");
return NativeLibrary.Load(assemblyFile);
}
} }
} }