Implements Formes iterator.

This commit is contained in:
Deukhoofd 2020-08-09 11:01:43 +02:00
parent a19c3bdeb4
commit cae763fdc0
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
11 changed files with 93 additions and 57 deletions

View File

@ -100,5 +100,15 @@ namespace Creaturelib.Generated
[DllImport("CreatureLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_CreatureSpecies_GetRandomGender")]
internal static extern Gender GetRandomGender(IntPtr p, IntPtr random);
/// <param name="p">CreatureSpecies *</param>
/// <returns>long unsigned int</returns>
[DllImport("CreatureLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_CreatureSpecies_GetVariantsCount")]
internal static extern ulong GetVariantsCount(IntPtr p);
/// <param name="p">CreatureSpecies *</param>
/// <returns>const const SpeciesVariant * *</returns>
[DllImport("CreatureLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_CreatureSpecies_GetVariants")]
internal static extern IntPtr GetVariants(IntPtr p);
}
}

View File

@ -14,9 +14,11 @@ namespace Pkmnlib.Generated
/// <param name="growthRate">const char *</param>
/// <param name="captureRate">unsigned char</param>
/// <param name="baseHappiness">unsigned char</param>
/// <param name="eggGroupsRaw">const const char * *</param>
/// <param name="eggGroupsLength">long unsigned int</param>
/// <returns>unsigned char</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_PokemonSpecies_Construct")]
internal static extern byte Construct(ref IntPtr @out, ushort id, IntPtr name, IntPtr defaultForme, float genderRatio, IntPtr growthRate, byte captureRate, byte baseHappiness);
internal static extern byte Construct(ref IntPtr @out, ushort id, IntPtr name, IntPtr defaultForme, float genderRatio, IntPtr growthRate, byte captureRate, byte baseHappiness, IntPtr eggGroupsRaw, ulong eggGroupsLength);
/// <param name="p">const PokemonSpecies *</param>
/// <returns>void</returns>
@ -52,5 +54,16 @@ namespace Pkmnlib.Generated
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_PokemonSpecies_GetEvolutions")]
internal static extern byte GetEvolutions(IntPtr p, ref IntPtr @out);
/// <param name="p">const PokemonSpecies *</param>
/// <returns>long unsigned int</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_PokemonSpecies_GetEggGroupCount")]
internal static extern ulong GetEggGroupCount(IntPtr p);
/// <param name="p">const PokemonSpecies *</param>
/// <param name="index">long unsigned int</param>
/// <returns>const char *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_PokemonSpecies_GetEggGroup")]
internal static extern IntPtr GetEggGroup(IntPtr p, ulong index);
}
}

View File

@ -16,6 +16,27 @@ namespace PkmnLibSharp.Library
private string? _name;
private ReadOnlyArray<byte>? _types;
public Forme(string name, float height, float weight, uint baseExperience, byte[] types,
ushort baseHealth, ushort baseAttack, ushort baseDefense, ushort baseSpecialAttack,
ushort baseSpecialDefense, ushort baseSpeed, IReadOnlyCollection<string> abilities,
IReadOnlyCollection<string> hiddenAbilities,
LearnableMoves moves)
{
var abilitiesConverted = abilities.Select(x => x.ToPtr()).ToArray();
var hiddenAbilitiesConverted = hiddenAbilities.Select(x => x.ToPtr()).ToArray();
var ab = abilitiesConverted.ArrayPtr();
var hab = hiddenAbilitiesConverted.ArrayPtr();
var ptr = SpeciesVariant.Construct(name.ToPtr(), height, weight, baseExperience, types.ArrayPtr(),
(ulong) types.Length, baseHealth, baseAttack, baseDefense, baseSpecialAttack,
baseSpecialDefense, baseSpeed, ab, (ulong) abilities.Count, hab,
(ulong) hiddenAbilities.Count, moves.Ptr);
foreach (var intPtr in abilitiesConverted)
Marshal.FreeHGlobal(intPtr);
foreach (var intPtr in hiddenAbilitiesConverted)
Marshal.FreeHGlobal(intPtr);
Initialize(ptr);
}
internal Forme(IntPtr parent) : base(parent)
{
}
@ -113,28 +134,6 @@ namespace PkmnLibSharp.Library
return Types[index];
}
public Forme(string name, float height, float weight, uint baseExperience, byte[] types,
ushort baseHealth, ushort baseAttack, ushort baseDefense, ushort baseSpecialAttack,
ushort baseSpecialDefense, ushort baseSpeed, IReadOnlyCollection<string> abilities,
IReadOnlyCollection<string> hiddenAbilities,
LearnableMoves moves)
{
var abilitiesConverted = abilities.Select(x => x.ToPtr()).ToArray();
var hiddenAbilitiesConverted = hiddenAbilities.Select(x => x.ToPtr()).ToArray();
var ab = abilitiesConverted.ArrayPtr();
var hab = hiddenAbilitiesConverted.ArrayPtr();
var ptr = SpeciesVariant.Construct(name.ToPtr(), height, weight, baseExperience, types.ArrayPtr(),
(ulong) types.Length, baseHealth, baseAttack, baseDefense, baseSpecialAttack,
baseSpecialDefense, baseSpeed, ab, (ulong) abilities.Count, hab,
(ulong) hiddenAbilities.Count, moves.Ptr);
var f = new Forme(ptr);
foreach (var intPtr in abilitiesConverted)
Marshal.FreeHGlobal(intPtr);
foreach (var intPtr in hiddenAbilitiesConverted)
Marshal.FreeHGlobal(intPtr);
Initialize(ptr);
}
public byte GetRandomAbility(Random rand)
{
return SpeciesVariant.GetRandomTalent(Ptr, rand.Ptr);

View File

@ -15,12 +15,13 @@ namespace PkmnLibSharp.Library
}
public Species(ushort id, string name, Forme defaultForme, float genderRatio, string growthRate,
byte captureRate, byte baseHappiness)
byte captureRate, byte baseHappiness, string[] eggGroups)
{
var ptr = IntPtr.Zero;
var eggGroupsPtr = eggGroups.ArrayPtr();
PokemonSpecies.Construct(ref ptr, id, name.ToPtr(), defaultForme.Ptr, genderRatio,
growthRate.ToPtr(), captureRate, baseHappiness).Assert();
_formes.Add("default", defaultForme);
growthRate.ToPtr(), captureRate, baseHappiness, eggGroupsPtr, (ulong) eggGroups.Length).Assert();
_formeCache.Add("default", defaultForme);
Initialize(ptr);
}
@ -30,6 +31,18 @@ namespace PkmnLibSharp.Library
public string Name => _name ??= CreatureSpecies.GetName(Ptr).PtrString()!;
public string GrowthRate => _growthRate ??= CreatureSpecies.GetGrowthRate(Ptr).PtrString()!;
public ReadOnlyNativePtrArray<Forme> Formes
{
get
{
if (_formes != null) return _formes;
var startPtr = CreatureSpecies.GetVariants(Ptr);
var size = CreatureSpecies.GetVariantsCount(Ptr);
_formes = new ReadOnlyNativePtrArray<Forme>(startPtr, (int) size);
return _formes;
}
}
public ReadOnlyNativePtrArray<EvolutionData> Evolutions
{
get
@ -50,19 +63,19 @@ namespace PkmnLibSharp.Library
public bool TryGetForme(string s, out Forme? forme)
{
if (_formes.TryGetValue(s, out forme))
if (_formeCache.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!);
_formeCache.Add(s, forme!);
return true;
}
forme = new Forme(ptr);
_formes.Add(s, forme);
_formeCache.Add(s, forme);
return true;
}
@ -71,23 +84,23 @@ namespace PkmnLibSharp.Library
public Forme GetForme(string s)
{
if (_formes.TryGetValue(s, out var forme))
if (_formeCache.TryGetValue(s, out var forme))
return forme;
var ptr = IntPtr.Zero;
CreatureSpecies.GetVariant(ref ptr, Ptr, s.ToPtr()).Assert();
if (TryResolvePointer(ptr, out Forme? f))
{
_formes.Add(s, f!);
_formeCache.Add(s, f!);
return f!;
}
forme = new Forme(ptr);
_formes.Add(s, forme);
_formeCache.Add(s, forme);
return forme;
}
public void SetForme(string s, Forme forme)
{
_formes.Add(s, forme);
_formeCache.Add(s, forme);
CreatureSpecies.SetVariant(Ptr, s.ToPtr(), forme.Ptr).Assert();
}
@ -104,15 +117,16 @@ namespace PkmnLibSharp.Library
private string? _name;
private string? _growthRate;
private readonly Dictionary<string, Forme> _formes =
private readonly Dictionary<string, Forme> _formeCache =
new Dictionary<string, Forme>(StringComparer.InvariantCultureIgnoreCase);
private ReadOnlyNativePtrArray<EvolutionData>? _evolutions;
private ReadOnlyNativePtrArray<Forme>? _formes;
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
foreach (var forme in _formes)
foreach (var forme in _formeCache)
{
forme.Value.MarkAsDeleted();
}

BIN
PkmnLibSharp/Native/libCreatureLib.so (Stored with Git LFS)

Binary file not shown.

BIN
PkmnLibSharp/Native/libpkmnLib.so (Stored with Git LFS)

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -29,7 +29,7 @@ namespace PkmnLibSharpTests.Battling
new Forme("default", 10f, 10f, 100, new byte[] {0, 1}, 100,
100, 100, 100, 100, 100, new[] {"testAbility", "testAbility2"},
new[] {"testHiddenAbility"}, new LearnableMoves(100)), 0.5f, "growthRate",
20, 100));
20, 100, new[]{"testEggGroup"}));
var moves = new MoveLibrary(10);
moves.Insert("testMove", new MoveData("testMove", 0, MoveCategory.Physical, 100,

View File

@ -18,7 +18,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
var library = new SpeciesLibrary(100);
library.Insert("foobar", species);
Assert.AreEqual(1, library.Count);
@ -30,7 +30,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
var library = new SpeciesLibrary(100);
library.Insert("foobar", species);
Assert.AreEqual(1, library.Count);
@ -44,7 +44,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
var library = new SpeciesLibrary(100);
library.Insert("foobar", species);
Assert.AreEqual(1, library.Count);
@ -62,7 +62,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
var library = new SpeciesLibrary(100);
library.Insert("foobar", species);
Assert.AreEqual(1, library.Count);

View File

@ -11,7 +11,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(0, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
species.Dispose();
}
@ -20,7 +20,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
Assert.AreEqual(10, species.Id);
species.Dispose();
}
@ -29,7 +29,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
Assert.AreEqual(0.5f, species.GenderRate);
species.Dispose();
}
@ -38,7 +38,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
Assert.AreEqual(100, species.CaptureRate);
species.Dispose();
}
@ -47,7 +47,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
Assert.AreEqual("testSpecies", species.Name);
species.Dispose();
}
@ -56,7 +56,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
Assert.AreEqual("exponential", species.GrowthRate);
species.Dispose();
}
@ -65,7 +65,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
Assert.True(species.HasForme("default"));
species.Dispose();
}
@ -74,7 +74,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
Assert.True(species.HasForme("default"));
forme = new Forme("bar", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
@ -87,7 +87,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
var f = species.GetForme("default");
Assert.AreEqual(forme, f);
Assert.Throws<NativeException>(() =>
@ -101,7 +101,7 @@ namespace PkmnLibSharpTests.Library
{
var forme = new Forme("foo", 1, 2, 100, new byte[] {0}, 10, 10, 10, 10, 10, 10, new[] {"foo"},
new[] {"bar"}, new LearnableMoves(100));
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80);
var species = new Species(10, "testSpecies", forme, 0.5f, "exponential", 100, 80, new[]{"testEggGroup"});
Assert.True(species.TryGetForme("default", out var f));
Assert.AreEqual(forme, f);
Assert.False(species.TryGetForme("non-existing", out f));