Adds Species interface
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-09-21 20:19:45 +02:00
parent c8be93f83f
commit b500a085d1
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 127 additions and 2 deletions

View File

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

View File

@ -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<short> baseStats, IReadOnlyCollection<string> abilities,
IReadOnlyCollection<string> 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);

View File

@ -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<Species.CacheData>
{
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<string, Form> Forms { get; } = new();
}
public Species(ushort id, string name, float genderRate, string growthRate, byte captureRate, Form defaultForm,
IReadOnlyCollection<string> 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);
}
}

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

Binary file not shown.

View File

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