Wrapper for Species and SpeciesLibrary

This commit is contained in:
Deukhoofd 2020-05-04 21:16:42 +02:00
parent f37888554f
commit a8709f2b05
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
10 changed files with 320 additions and 10 deletions

View File

@ -87,5 +87,12 @@ namespace Creatureliblibrary.Generated
[DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_CreatureSpecies_GetVariantWithHash")] [DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_CreatureSpecies_GetVariantWithHash")]
internal static extern byte GetVariantWithHash(IntPtr @out, IntPtr p, uint hash); internal static extern byte GetVariantWithHash(IntPtr @out, IntPtr p, uint hash);
/// <param name="p">CreatureSpecies *</param>
/// <param name="name">const char *</param>
/// <param name="variant">SpeciesVariant *</param>
/// <returns>unsigned char</returns>
[DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_CreatureSpecies_SetVariant")]
internal static extern byte SetVariant(IntPtr p, IntPtr name, IntPtr variant);
} }
} }

View File

@ -132,7 +132,7 @@ namespace PkmnLibSharp.Library
} }
} }
private Forme(IntPtr parent) : base(parent) internal Forme(IntPtr parent) : base(parent)
{ {
} }

View File

@ -1,4 +1,3 @@
using System;
using PkmnLibSharp.Utilities; using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library namespace PkmnLibSharp.Library

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using Creatureliblibrary.Generated;
using Pkmnlib.Generated; using Pkmnlib.Generated;
using PkmnLibSharp.Utilities; using PkmnLibSharp.Utilities;
@ -6,8 +8,69 @@ namespace PkmnLibSharp.Library
{ {
public class Species : PointerWrapper public class Species : PointerWrapper
{ {
// ReSharper disable once SuggestBaseTypeForParameter private string _name;
private Species(IntPtr ptr) : base(ptr) private string _growthRate;
private readonly Dictionary<string, Forme> _formes =
new Dictionary<string, Forme>(StringComparer.InvariantCultureIgnoreCase);
public ushort Id => CreatureSpecies.GetId(Ptr);
public float GenderRate => CreatureSpecies.GetGenderRate(Ptr);
public byte CaptureRate => CreatureSpecies.GetCaptureRate(Ptr);
public string Name => _name ??= CreatureSpecies.GetName(Ptr).PtrString();
public string GrowthRate => _growthRate ??= CreatureSpecies.GetGrowthRate(Ptr).PtrString();
public bool HasForme(string s)
{
return CreatureSpecies.HasVariant(Ptr, s.ToPtr()) == MarshalHelper.True;
}
public bool TryGetForme(string s, out Forme forme)
{
if (_formes.TryGetValue(s, out forme))
return true;
var ptr = IntPtr.Zero;
if (CreatureSpecies.TryGetVariant(Ptr, s.ToPtr(), ref ptr) == MarshalHelper.True)
{
if (TryResolvePointer(ptr, out forme))
{
_formes.Add(s, forme);
return true;
}
forme = new Forme(ptr);
_formes.Add(s, forme);
return true;
}
return false;
}
public Forme GetForme(string s)
{
if (_formes.TryGetValue(s, out var forme))
return forme;
var ptr = IntPtr.Zero;
CreatureSpecies.GetVariant(ref ptr, Ptr, s.ToPtr()).Assert();
if (TryResolvePointer(ptr, out forme))
{
_formes.Add(s, forme);
return forme;
}
forme = new Forme(ptr);
_formes.Add(s, forme);
return forme;
}
public void SetForme(string s, Forme forme)
{
_formes.Add(s, forme);
CreatureSpecies.SetVariant(Ptr, s.ToPtr(), forme.Ptr).Assert();
}
internal Species(IntPtr ptr) : base(ptr)
{ {
} }
@ -17,7 +80,9 @@ namespace PkmnLibSharp.Library
var ptr = IntPtr.Zero; var ptr = IntPtr.Zero;
PokemonSpecies.Construct(ref ptr, id, name.ToPtr(), defaultForme.Ptr, genderRatio, PokemonSpecies.Construct(ref ptr, id, name.ToPtr(), defaultForme.Ptr, genderRatio,
growthRate.ToPtr(), captureRate, baseHappiness).Assert(); growthRate.ToPtr(), captureRate, baseHappiness).Assert();
return new Species(ptr); var s = new Species(ptr);
s._formes.Add("default", defaultForme);
return s;
} }
protected override void DeletePtr() protected override void DeletePtr()

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library
{
public class SpeciesLibrary : PointerWrapper
{
private readonly Dictionary<string, Species> _cache = new Dictionary<string, Species>(StringComparer.InvariantCultureIgnoreCase);
public ulong Count => Creatureliblibrary.Generated.SpeciesLibrary.GetCount(Ptr);
public void Insert(string key, Species species)
{
Creatureliblibrary.Generated.SpeciesLibrary.Insert(Ptr, key.ToPtr(), species.Ptr).Assert();
_cache.Add(key, species);
}
public void Delete(string key)
{
Creatureliblibrary.Generated.SpeciesLibrary.Delete(Ptr, key.ToPtr()).Assert();
_cache.Remove(key);
}
public bool TryGet(string key, out Species species)
{
if (_cache.TryGetValue(key, out species))
return true;
var ptr = IntPtr.Zero;
if (Creatureliblibrary.Generated.SpeciesLibrary.TryGet(Ptr, key.ToPtr(), ref ptr) != MarshalHelper.True)
return false;
if (TryResolvePointer(ptr, out species))
{
_cache.Add(key, species);
return true;
}
species = new Species(ptr);
_cache.Add(key, species);
return true;
}
public Species Get(string key)
{
if (_cache.TryGetValue(key, out var species))
return species;
var ptr = IntPtr.Zero;
Creatureliblibrary.Generated.SpeciesLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert();
if (TryResolvePointer(ptr, out species))
{
_cache.Add(key, species);
return species;
}
species = new Species(ptr);
_cache.Add(key, species);
return species;
}
public SpeciesLibrary(ulong initialCapacity) : base(
Creatureliblibrary.Generated.SpeciesLibrary.Construct(initialCapacity))
{
}
protected override void DeletePtr()
{
Creatureliblibrary.Generated.SpeciesLibrary.Destruct(Ptr);
}
}
}

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,76 @@
using NUnit.Framework;
using PkmnLibSharp.Library;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharpTests.Library
{
public class SpeciesLibraryTests
{
[Test]
public void ConstructDestruct()
{
var item = new SpeciesLibrary(100);
item.Dispose();
}
[Test]
public void Insert()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var library = new SpeciesLibrary(100);
library.Insert("foobar", species);
Assert.AreEqual(1, library.Count);
library.Dispose();
}
[Test]
public void Delete()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var library = new SpeciesLibrary(100);
library.Insert("foobar", species);
Assert.AreEqual(1, library.Count);
library.Delete("foobar");
Assert.AreEqual(0, library.Count);
library.Dispose();
}
[Test]
public void Get()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var library = new SpeciesLibrary(100);
library.Insert("foobar", species);
Assert.AreEqual(1, library.Count);
var m = library.Get("foobar");
Assert.AreEqual(m.Name, "testSpecies");
Assert.Throws<NativeException>(() =>
{
library.Get("barfoo");
});
library.Dispose();
}
[Test]
public void TryGet()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var library = new SpeciesLibrary(100);
library.Insert("foobar", species);
Assert.AreEqual(1, library.Count);
Assert.True(library.TryGet("foobar", out species));
Assert.AreEqual(species.Name, "testSpecies");
Assert.False(library.TryGet("barfoo", out species));
library.Dispose();
}
}
}

View File

@ -1,5 +1,6 @@
using NUnit.Framework; using NUnit.Framework;
using PkmnLibSharp.Library; using PkmnLibSharp.Library;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharpTests.Library namespace PkmnLibSharpTests.Library
{ {
@ -13,5 +14,100 @@ namespace PkmnLibSharpTests.Library
var species = Species.Create(0, "testSpecies", forme, 0.5f, "exponential", 100, 80); var species = Species.Create(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
species.Dispose(); species.Dispose();
} }
[Test]
public void GetId()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
Assert.AreEqual(10, species.Id);
species.Dispose();
}
[Test]
public void GetGenderRate()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
Assert.AreEqual(0.5f, species.GenderRate);
species.Dispose();
}
[Test]
public void GetCaptureRate()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
Assert.AreEqual(100, species.CaptureRate);
species.Dispose();
}
[Test]
public void GetName()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
Assert.AreEqual("testSpecies", species.Name);
species.Dispose();
}
[Test]
public void GetGrowthRate()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
Assert.AreEqual("exponential", species.GrowthRate);
species.Dispose();
}
[Test]
public void HasForme()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
Assert.True(species.HasForme("default"));
species.Dispose();
}
[Test]
public void SetForme()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
Assert.True(species.HasForme("default"));
forme = Forme.Create("bar", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
species.SetForme("bar", forme);
Assert.True(species.HasForme("bar"));
species.Dispose();
}
[Test]
public void GetForme()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var f = species.GetForme("default");
Assert.AreEqual(forme, f);
Assert.Throws<NativeException>(() =>
{
species.GetForme("non-existing");
});
species.Dispose();
}
[Test]
public void TrGetForme()
{
var forme = Forme.Create("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, LearnableMoves.Create(100));
var species = Species.Create(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
Assert.True(species.TryGetForme("default", out var f));
Assert.AreEqual(forme, f);
Assert.False(species.TryGetForme("non-existing", out f));
species.Dispose();
}
} }
} }