Type Library wrapper and tests
This commit is contained in:
parent
cc66729529
commit
8845f33d04
|
@ -0,0 +1,60 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using PkmnLibSharp.Utilities;
|
||||||
|
|
||||||
|
namespace PkmnLibSharp.Library
|
||||||
|
{
|
||||||
|
public class TypeLibrary : PointerWrapper
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, byte> _cache =
|
||||||
|
new Dictionary<string, byte>(StringComparer.InvariantCultureIgnoreCase);
|
||||||
|
|
||||||
|
public TypeLibrary(ulong initialCapacity) : base(
|
||||||
|
Creatureliblibrary.Generated.TypeLibrary.Construct(initialCapacity))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte GetTypeId(string typeName)
|
||||||
|
{
|
||||||
|
if (_cache.TryGetValue(typeName, out var b))
|
||||||
|
return b;
|
||||||
|
Creatureliblibrary.Generated.TypeLibrary.GetTypeId(ref b, Ptr, typeName.ToPtr()).Assert();
|
||||||
|
_cache.Add(typeName, b);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte RegisterType(string typeName)
|
||||||
|
{
|
||||||
|
byte b = 0;
|
||||||
|
Creatureliblibrary.Generated.TypeLibrary.RegisterType(ref b, Ptr, typeName.ToPtr()).Assert();
|
||||||
|
_cache.Add(typeName, b);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetEffectiveness(byte attackingId, byte defensiveId, float effectiveness)
|
||||||
|
{
|
||||||
|
Creatureliblibrary.Generated.TypeLibrary.SetEffectiveness(Ptr, attackingId, defensiveId, effectiveness);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float GetSingleEffectiveness(byte attackingId, byte defensiveId)
|
||||||
|
{
|
||||||
|
float f = 0;
|
||||||
|
Creatureliblibrary.Generated.TypeLibrary.GetSingleEffectiveness(ref f, Ptr, attackingId, defensiveId)
|
||||||
|
.Assert();
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float GetEffectiveness(byte attackingId, byte[] defensiveIds)
|
||||||
|
{
|
||||||
|
float f = 0;
|
||||||
|
Creatureliblibrary.Generated.TypeLibrary.GetEffectiveness(ref f, Ptr, attackingId, defensiveIds.ArrayPtr(),
|
||||||
|
(ulong) defensiveIds.Length).Assert();
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void DeletePtr()
|
||||||
|
{
|
||||||
|
Creatureliblibrary.Generated.TypeLibrary.Destruct(Ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
PkmnLibSharp/Native/libCreatureLibLibrary.so (Stored with Git LFS)
BIN
PkmnLibSharp/Native/libCreatureLibLibrary.so (Stored with Git LFS)
Binary file not shown.
|
@ -0,0 +1,70 @@
|
||||||
|
using System;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using PkmnLibSharp.Library;
|
||||||
|
|
||||||
|
namespace PkmnLibSharpTests.Library
|
||||||
|
{
|
||||||
|
public class TypeLibraryTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void ConstructDestruct()
|
||||||
|
{
|
||||||
|
var tl = new TypeLibrary(0);
|
||||||
|
tl.Dispose();
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void RegisterType()
|
||||||
|
{
|
||||||
|
var tl = new TypeLibrary(0);
|
||||||
|
tl.RegisterType("testType1");
|
||||||
|
tl.Dispose();
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void GetTypeId()
|
||||||
|
{
|
||||||
|
var tl = new TypeLibrary(0);
|
||||||
|
tl.RegisterType("testType1");
|
||||||
|
var id = tl.GetTypeId("testType1");
|
||||||
|
Assert.AreEqual(0, id);
|
||||||
|
tl.RegisterType("testType2");
|
||||||
|
id = tl.GetTypeId("testType2");
|
||||||
|
Assert.AreEqual(1, id);
|
||||||
|
tl.Dispose();
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void SetEffectiveness()
|
||||||
|
{
|
||||||
|
var tl = new TypeLibrary(0);
|
||||||
|
tl.RegisterType("testType1");
|
||||||
|
tl.RegisterType("testType2");
|
||||||
|
tl.SetEffectiveness(0, 1, 0.5f);
|
||||||
|
tl.SetEffectiveness(1, 0, 2f);
|
||||||
|
tl.Dispose();
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void GetSingleEffectiveness()
|
||||||
|
{
|
||||||
|
var tl = new TypeLibrary(0);
|
||||||
|
tl.RegisterType("testType1");
|
||||||
|
tl.RegisterType("testType2");
|
||||||
|
tl.SetEffectiveness(0, 1, 0.5f);
|
||||||
|
tl.SetEffectiveness(1, 0, 2f);
|
||||||
|
Assert.AreEqual(0.5f, tl.GetSingleEffectiveness(0, 1));
|
||||||
|
Assert.AreEqual(2f, tl.GetSingleEffectiveness(1, 0));
|
||||||
|
tl.Dispose();
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void GetEffectiveness()
|
||||||
|
{
|
||||||
|
var tl = new TypeLibrary(0);
|
||||||
|
tl.RegisterType("testType1");
|
||||||
|
tl.RegisterType("testType2");
|
||||||
|
tl.SetEffectiveness(0, 1, 0.5f);
|
||||||
|
tl.SetEffectiveness(0, 0, 0.5f);
|
||||||
|
tl.SetEffectiveness(1, 0, 2f);
|
||||||
|
Assert.AreEqual(0.25f, tl.GetEffectiveness(0, new byte[] {0, 1}));
|
||||||
|
tl.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue