diff --git a/PkmnLibRSharp/FFI/StaticData/Libraries/SpeciesLibrary.cs b/PkmnLibRSharp/FFI/StaticData/Libraries/SpeciesLibrary.cs new file mode 100644 index 0000000..1813ea8 --- /dev/null +++ b/PkmnLibRSharp/FFI/StaticData/Libraries/SpeciesLibrary.cs @@ -0,0 +1,23 @@ +using System; +using System.Runtime.InteropServices; + +namespace PkmnLibSharp.FFI.StaticData.Libraries +{ + internal static class SpeciesLibrary + { + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + internal static extern IntPtr species_library_new(ulong capacity); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + internal static extern void species_library_drop(IntPtr ptr); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + internal static extern IntPtr species_library_get(IntPtr ptr, IntPtr key); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + internal static extern void species_library_add(IntPtr ptr, IntPtr key, IntPtr value); + + [DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + internal static extern ulong species_library_len(IntPtr ptr); + } +} \ No newline at end of file diff --git a/PkmnLibRSharp/StaticData/Libraries/DataLibrary.cs b/PkmnLibRSharp/StaticData/Libraries/DataLibrary.cs new file mode 100644 index 0000000..22fe855 --- /dev/null +++ b/PkmnLibRSharp/StaticData/Libraries/DataLibrary.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using PkmnLibSharp.Utils; + +namespace PkmnLibSharp.StaticData.Libraries +{ + public abstract class DataLibrary : ExternPointer.CacheData> + { + protected DataLibrary(IntPtr ptr, bool isOwner) : base(ptr, isOwner) + { + } + + public class CacheData + { + public Dictionary ValueCache { get; } = new(); + } + + public abstract void Add(string key, T value); + public abstract ulong Length { get; } + + protected abstract T? GetValueByKey(string key); + + public bool TryGetValue(string key, out T? value) + { + if (Cache.ValueCache.TryGetValue(key, out value) && value != null) + return true; + value = GetValueByKey(key); + if (value != null) + { + Cache.ValueCache.Add(key, value); + } + + return value != null; + } + + public T this[string key] + { + get + { + if (!TryGetValue(key, out var value)) + throw new KeyNotFoundException($"Value with key `{key}` was not found"); + return value!; + } + } + + protected override CacheData CreateCache() => new(); + } +} \ No newline at end of file diff --git a/PkmnLibRSharp/StaticData/Libraries/SpeciesLibrary.cs b/PkmnLibRSharp/StaticData/Libraries/SpeciesLibrary.cs new file mode 100644 index 0000000..b02b328 --- /dev/null +++ b/PkmnLibRSharp/StaticData/Libraries/SpeciesLibrary.cs @@ -0,0 +1,26 @@ +using System; +using PkmnLibSharp.Utils; +using Interface = PkmnLibSharp.FFI.StaticData.Libraries.SpeciesLibrary; + +namespace PkmnLibSharp.StaticData.Libraries +{ + public class SpeciesLibrary : DataLibrary + { + public SpeciesLibrary(ulong capacity) : base(Interface.species_library_new(capacity), true) + { + } + + protected override void Destructor() => Interface.species_library_drop(Ptr); + + public override void Add(string key, Species value) => + Interface.species_library_add(Ptr, key.ToPtr(), value.TakeOwnershipAndInvalidate()); + + public override ulong Length => Interface.species_library_len(Ptr); + + protected override Species? GetValueByKey(string key) + { + var ptr = Interface.species_library_get(Ptr, key.ToPtr()); + return ptr == IntPtr.Zero ? null : new Species(ptr, false); + } + } +} \ No newline at end of file diff --git a/PkmnLibRSharp/StaticData/Species.cs b/PkmnLibRSharp/StaticData/Species.cs index bb16f5a..05b1552 100644 --- a/PkmnLibRSharp/StaticData/Species.cs +++ b/PkmnLibRSharp/StaticData/Species.cs @@ -18,6 +18,10 @@ namespace PkmnLibSharp.StaticData public Dictionary Forms { get; } = new(); } + internal Species(IntPtr ptr, bool isOwner) : base(ptr, isOwner) + { + } + public Species(ushort id, string name, float genderRate, string growthRate, byte captureRate, Form defaultForm, IReadOnlyCollection flags) { diff --git a/PkmnLibRSharp/libpkmn_lib.so b/PkmnLibRSharp/libpkmn_lib.so index a641b10..5909c29 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:bd3d61f6e4ba8c9986b761d789a28203bc96381ab6fb0260b57788b846162638 -size 155516832 +oid sha256:2c4b99a230268b91e0384e18367eaba6720d0c708aa4fbbac195115ab0e34e39 +size 156040640 diff --git a/PkmnLibRSharpTests/StaticData/Libraries/SpeciesLibraryTests.cs b/PkmnLibRSharpTests/StaticData/Libraries/SpeciesLibraryTests.cs new file mode 100644 index 0000000..0076df6 --- /dev/null +++ b/PkmnLibRSharpTests/StaticData/Libraries/SpeciesLibraryTests.cs @@ -0,0 +1,31 @@ +using System; +using NUnit.Framework; +using PkmnLibSharp.StaticData; +using PkmnLibSharp.StaticData.Libraries; + +namespace PkmnLibRSharpTests.StaticData.Libraries +{ + public class SpeciesLibraryTests + { + [Test] + public void Create() + { + using var lib = new SpeciesLibrary(0); + Assert.AreEqual(0, lib.Length); + } + + [Test] + public void CreateAndAdd() + { + using var lib = new SpeciesLibrary(0); + Assert.AreEqual(0, lib.Length); + 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()); + lib.Add("foobar", species); + Assert.AreEqual(1, lib.Length); + Assert.AreEqual("testSpecies", lib["foobar"].Name); + } + } +} \ No newline at end of file