GrowthRate wrappers.

This commit is contained in:
Deukhoofd 2020-05-05 22:40:17 +02:00
parent bf158802d7
commit 32d07be361
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
8 changed files with 116 additions and 3 deletions

View File

@ -13,5 +13,10 @@ namespace Creatureliblibrary.Generated
[DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_ExternGrowthRate_Construct")] [DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_ExternGrowthRate_Construct")]
internal static extern byte Construct(ref IntPtr @out, IntPtr calcLevel, IntPtr calcExperience); internal static extern byte Construct(ref IntPtr @out, IntPtr calcLevel, IntPtr calcExperience);
/// <param name="p">const ExternGrowthRate *</param>
/// <returns>void</returns>
[DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_ExternGrowthRate_Destruct")]
internal static extern void Destruct(IntPtr p);
} }
} }

View File

@ -12,5 +12,10 @@ namespace Creatureliblibrary.Generated
[DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_LookupGrowthRate_Construct")] [DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_LookupGrowthRate_Construct")]
internal static extern IntPtr Construct(IntPtr experiencePerLevel, ulong count); internal static extern IntPtr Construct(IntPtr experiencePerLevel, ulong count);
/// <param name="p">const LookupGrowthRate *</param>
/// <returns>void</returns>
[DllImport("CreatureLibLibrary", CallingConvention = CallingConvention.Cdecl, EntryPoint= "CreatureLib_LookupGrowthRate_Destruct")]
internal static extern void Destruct(IntPtr p);
} }
} }

View File

@ -0,0 +1,32 @@
using System;
using System.Runtime.InteropServices;
namespace PkmnLibSharp.Library.GrowthRates
{
public class ExternGrowthRate : GrowthRate
{
public delegate uint CalculateExperienceDelegate(byte level);
public delegate byte CalculateLevelDelegate(uint experience);
private ExternGrowthRate(IntPtr ptr) : base(ptr)
{
}
public static ExternGrowthRate Create(CalculateLevelDelegate levelFunc,
CalculateExperienceDelegate experienceFunc)
{
var ptr = IntPtr.Zero;
Creatureliblibrary.Generated.ExternGrowthRate.Construct(ref ptr,
Marshal.GetFunctionPointerForDelegate(levelFunc),
Marshal.GetFunctionPointerForDelegate(experienceFunc)
);
return new ExternGrowthRate(ptr);
}
protected override void DeletePtr()
{
Creatureliblibrary.Generated.ExternGrowthRate.Destruct(Ptr);
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library.GrowthRates
{
public abstract class GrowthRate : PointerWrapper
{
internal GrowthRate(IntPtr ptr) : base(ptr)
{
}
public byte CalculateLevel(uint experience)
{
byte b = 0;
Creatureliblibrary.Generated.GrowthRate.CalculateLevel(ref b, Ptr, experience).Assert();
return b;
}
public uint CalculateExperience(byte level)
{
uint i = 0;
Creatureliblibrary.Generated.GrowthRate.CalculateExperience(ref i, Ptr, level).Assert();
return i;
}
}
}

View File

@ -0,0 +1,18 @@
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library.GrowthRates
{
public class LookupGrowthRate : GrowthRate
{
public LookupGrowthRate(uint[] experiencePerLevel) : base(
Creatureliblibrary.Generated.LookupGrowthRate.Construct(experiencePerLevel.ArrayPtr(),
(ulong) experiencePerLevel.Length))
{
}
protected override void DeletePtr()
{
Creatureliblibrary.Generated.LookupGrowthRate.Destruct(Ptr);
}
}
}

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,27 @@
using NUnit.Framework;
using PkmnLibSharp.Library.GrowthRates;
namespace PkmnLibSharpTests.Library
{
public class GrowthRateTests
{
[Test]
public void LookupGrowthRate()
{
var gr = new LookupGrowthRate(new uint[] {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000});
Assert.AreEqual(400, gr.CalculateExperience(5));
Assert.AreEqual(8, gr.CalculateLevel(750));
gr.Dispose();
}
[Test]
public void ExternGrowthRate()
{
var gr = PkmnLibSharp.Library.GrowthRates.ExternGrowthRate.Create(experience => (byte) (experience / 100),
level => (uint) (level * 100));
Assert.AreEqual(500, gr.CalculateExperience(5));
Assert.AreEqual(7, gr.CalculateLevel(750));
gr.Dispose();
}
}
}