Adds SpeciesLibrary

This commit is contained in:
Deukhoofd 2022-09-23 18:45:56 +02:00
parent b500a085d1
commit 2b6b3a40ed
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
6 changed files with 134 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.StaticData.Libraries
{
public abstract class DataLibrary<T> : ExternPointer<DataLibrary<T>.CacheData>
{
protected DataLibrary(IntPtr ptr, bool isOwner) : base(ptr, isOwner)
{
}
public class CacheData
{
public Dictionary<string, T> 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();
}
}

View File

@ -0,0 +1,26 @@
using System;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Libraries.SpeciesLibrary;
namespace PkmnLibSharp.StaticData.Libraries
{
public class SpeciesLibrary : DataLibrary<Species>
{
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);
}
}
}

View File

@ -18,6 +18,10 @@ namespace PkmnLibSharp.StaticData
public Dictionary<string, Form> 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<string> flags)
{

BIN
PkmnLibRSharp/libpkmn_lib.so (Stored with Git LFS)

Binary file not shown.

View File

@ -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<short>(5, 10, 30, 20, 2, 0), new[] { "foo", "bar" }, new[] { "set" },
new LearnableMoves(), Array.Empty<string>());
using var species = new Species(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>());
lib.Add("foobar", species);
Assert.AreEqual(1, lib.Length);
Assert.AreEqual("testSpecies", lib["foobar"].Name);
}
}
}