From b500a085d177f02df47f2baf5822ab98bb2f1755 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 21 Sep 2022 20:19:45 +0200 Subject: [PATCH] Adds Species interface --- PkmnLibRSharp/FFI/StaticData/Species.cs | 37 ++++++++++++ PkmnLibRSharp/StaticData/Form.cs | 6 ++ PkmnLibRSharp/StaticData/Species.cs | 57 +++++++++++++++++++ PkmnLibRSharp/libpkmn_lib.so | 4 +- PkmnLibRSharpTests/StaticData/SpeciesTests.cs | 25 ++++++++ 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 PkmnLibRSharp/FFI/StaticData/Species.cs create mode 100644 PkmnLibRSharp/StaticData/Species.cs create mode 100644 PkmnLibRSharpTests/StaticData/SpeciesTests.cs diff --git a/PkmnLibRSharp/FFI/StaticData/Species.cs b/PkmnLibRSharp/FFI/StaticData/Species.cs new file mode 100644 index 0000000..59dbd01 --- /dev/null +++ b/PkmnLibRSharp/FFI/StaticData/Species.cs @@ -0,0 +1,37 @@ +using System; +using System.Runtime.InteropServices; + +namespace PkmnLibSharp.FFI.StaticData +{ + internal static class Species + { + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr species_new(ushort id, IntPtr name, float genderRate, IntPtr growthRate, + byte captureRate, IntPtr defaultForm, IntPtr flags, ulong flagsLength); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern void species_drop(IntPtr ptr); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern ushort species_id(IntPtr ptr); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr species_name(IntPtr ptr); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern float species_gender_rate(IntPtr ptr); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr species_growth_rate(IntPtr ptr); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern byte species_capture_rate(IntPtr ptr); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr species_add_form(IntPtr ptr, IntPtr name, IntPtr form); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr species_get_form(IntPtr ptr, IntPtr name); + + } +} \ No newline at end of file diff --git a/PkmnLibRSharp/StaticData/Form.cs b/PkmnLibRSharp/StaticData/Form.cs index 2f58b94..23bd549 100644 --- a/PkmnLibRSharp/StaticData/Form.cs +++ b/PkmnLibRSharp/StaticData/Form.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using PkmnLibSharp.Utils; @@ -21,6 +22,10 @@ namespace PkmnLibSharp.StaticData public LearnableMoves? LearnableMoves { get; internal set; } } + internal Form(IntPtr formPtr, bool isOwner) : base(formPtr, isOwner) + { + } + public Form(string name, float height, float weight, uint baseExperience, TypeIdentifier[] types, StaticStatisticSet baseStats, IReadOnlyCollection abilities, IReadOnlyCollection hiddenAbilities, LearnableMoves learnableMoves, @@ -39,6 +44,7 @@ namespace PkmnLibSharp.StaticData InitializePointer(ptr, true); } + public string Name => Cache.Name ??= Interface.form_name(Ptr).PtrString()!; public float Height => Cache.Height ??= Interface.form_height(Ptr); public float Weight => Cache.Weight ??= Interface.form_weight(Ptr); diff --git a/PkmnLibRSharp/StaticData/Species.cs b/PkmnLibRSharp/StaticData/Species.cs new file mode 100644 index 0000000..bb16f5a --- /dev/null +++ b/PkmnLibRSharp/StaticData/Species.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using PkmnLibSharp.Utils; +using Interface = PkmnLibSharp.FFI.StaticData.Species; + +namespace PkmnLibSharp.StaticData +{ + public class Species : ExternPointer + { + public class CacheData + { + public ushort? Id { get; internal set; } + public string? Name { get; internal set; } + public float? GenderRate { get; internal set; } + public string? GrowthRate { get; internal set; } + public byte? CaptureRate { get; internal set; } + public Dictionary Forms { get; } = new(); + } + + public Species(ushort id, string name, float genderRate, string growthRate, byte captureRate, Form defaultForm, + IReadOnlyCollection flags) + { + var flagsPtrArray = flags.Select(x => x.ToPtr()).ToArray(); + var ptr = Interface.species_new(id, name.ToPtr(), genderRate, growthRate.ToPtr(), captureRate, + defaultForm.TakeOwnershipAndInvalidate(), flagsPtrArray.ArrayPtr(), (ulong)flags.Count); + InitializePointer(ptr, true); + } + + public ushort Id => Cache.Id ??= Interface.species_id(Ptr); + public string Name => Cache.Name ??= Interface.species_name(Ptr).PtrString()!; + public float GenderRate => Cache.GenderRate ??= Interface.species_gender_rate(Ptr); + public string GrowthRate => Cache.GrowthRate ??= Interface.species_growth_rate(Ptr).PtrString()!; + public byte CaptureRate => Cache.CaptureRate ??= Interface.species_capture_rate(Ptr); + + public bool TryGetForm(string formName, out Form? form) + { + if (Cache.Forms.TryGetValue(formName, out form)) + return true; + var formPtr = Interface.species_get_form(Ptr, formName.ToPtr()); + if (formPtr == IntPtr.Zero) + { + form = null; + return false; + } + form = new Form(formPtr, false); + Cache.Forms.Add(formName, form); + return true; + } + + public void AddForm(Form form) => + Interface.species_add_form(Ptr, form.Name.ToPtr(), form.TakeOwnershipAndInvalidate()); + + protected override CacheData CreateCache() => new CacheData(); + protected override void Destructor() => Interface.species_drop(Ptr); + } +} \ No newline at end of file diff --git a/PkmnLibRSharp/libpkmn_lib.so b/PkmnLibRSharp/libpkmn_lib.so index 5717c3e..a641b10 100755 --- a/PkmnLibRSharp/libpkmn_lib.so +++ b/PkmnLibRSharp/libpkmn_lib.so @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:780800f432a4d36d270db0b0fe412a0d95eefd3fbedb0e00ce49d4487b98e8d0 -size 155516656 +oid sha256:bd3d61f6e4ba8c9986b761d789a28203bc96381ab6fb0260b57788b846162638 +size 155516832 diff --git a/PkmnLibRSharpTests/StaticData/SpeciesTests.cs b/PkmnLibRSharpTests/StaticData/SpeciesTests.cs new file mode 100644 index 0000000..75f783a --- /dev/null +++ b/PkmnLibRSharpTests/StaticData/SpeciesTests.cs @@ -0,0 +1,25 @@ +using System; +using NUnit.Framework; +using PkmnLibSharp.StaticData; + +namespace PkmnLibRSharpTests.StaticData +{ + public class SpeciesTests + { + [Test] + public void BasicTests() + { + using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, + new StaticStatisticSet(5, 10, 30, 20, 2, 0), new[] { "foo", "bar" }, new[] { "set" }, + new LearnableMoves(), Array.Empty()); + using var species = new Species(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty()); + Assert.AreEqual(10, species.Id); + Assert.AreEqual("testSpecies", species.Name); + Assert.AreEqual(0.2f, species.GenderRate, 0.00001f); + Assert.AreEqual("growth", species.GrowthRate); + Assert.AreEqual(120, species.CaptureRate); + Assert.That(species.TryGetForm("default", out var defaultForm)); + Assert.AreEqual("foobar", defaultForm!.Name); + } + } +} \ No newline at end of file