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

@ -97,13 +97,14 @@ namespace PkmnLibSharp.Battling
{
}
public BattleLibrary(PokemonLibrary staticLibrary, StatCalculator statCalculator,
DamageLibrary damageLibrary, ExperienceLibrary experienceLibrary, ScriptResolver scriptResolver,
MiscLibrary miscLibrary)
public BattleLibrary(PokemonLibrary staticLibrary, StatCalculator statCalculator, DamageLibrary damageLibrary,
ExperienceLibrary experienceLibrary, ScriptResolver scriptResolver, MiscLibrary miscLibrary,
CaptureLibrary captureLibrary)
{
var ptr = IntPtr.Zero;
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);
_static = staticLibrary;
_statCalculator = statCalculator;

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 bool WasCaught => Pkmnlib.Generated.Pokemon.WasCaught(Ptr) == 1;
public void Initialize()
{
Creaturelib.Generated.Creature.Initialize(Ptr).Assert();
@ -531,6 +533,12 @@ namespace PkmnLibSharp.Battling
_forme = forme;
}
public void AttemptCapture(Item catchItem)
{
Pkmnlib.Generated.Pokemon.AttemptCapture(Ptr, catchItem.Ptr).Assert();
}
private Species? _displaySpecies;
private Forme? _displayForme;
private Species? _species;

View File

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

View File

@ -25,9 +25,9 @@ namespace Creaturelib.Generated
/// <param name="chance">float</param>
/// <param name="attack">ExecutingAttack *</param>
/// <param name="target">Creature *</param>
/// <returns>unsigned char</returns>
/// <returns>signed char</returns>
[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>
/// <returns>int</returns>

View File

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

View File

@ -13,9 +13,10 @@ namespace Pkmnlib.Generated
/// <param name="experienceLibrary">ExperienceLibrary *</param>
/// <param name="scriptResolver">ScriptResolver *</param>
/// <param name="miscLibrary">MiscLibrary *</param>
/// <param name="captureLibrary">CaptureLibrary *</param>
/// <returns>unsigned char</returns>
[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>
/// <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
{
WeatherChange = 128,
CaptureAttempt = 129,
}
}

View File

@ -50,6 +50,11 @@ namespace Pkmnlib.Generated
[DllImport("libpkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_IsShiny")]
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>
/// <returns>const Nature *</returns>
[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")]
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>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8601;CS8602;CS8603;CS8604;CS8618</WarningsAsErrors>
<LangVersion>9</LangVersion>
<LangVersion>10</LangVersion>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
</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):
return type
print("Unhandled type '{}'".format(type))
return "void"
return "IntPtr"
def clean_name(name):
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");
var scriptLibrary = new AngelScriptResolver();
_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);
return _cache;
}

View File

@ -1,6 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using NUnit.Framework;
using PkmnLibSharp.Battling;
using PkmnLibSharp.Utilities;
@ -14,10 +17,10 @@ namespace PkmnLibSharpTests
[OneTimeSetUp]
public void Init()
{
NativeLibrary.Load("Arbutils", Assembly.GetCallingAssembly(), DllImportSearchPath.AssemblyDirectory);
NativeLibrary.Load("CreatureLib", Assembly.GetCallingAssembly(), DllImportSearchPath.AssemblyDirectory);
NativeLibrary.Load("libangelscript.so.2.35.0", Assembly.GetCallingAssembly(), DllImportSearchPath.AssemblyDirectory);
NativeLibrary.Load("pkmnLib", Assembly.GetCallingAssembly(), DllImportSearchPath.AssemblyDirectory);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
NativeLibrary.SetDllImportResolver(assembly, ImportResolver);
}
LogHandler.RegisterListener((level, s) =>
{
@ -27,5 +30,13 @@ namespace PkmnLibSharpTests
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);
}
}
}