71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using PkmnLibSharp.FFI;
|
|
using PkmnLibSharp.Utils;
|
|
using Interface = PkmnLibSharp.FFI.StaticData.Libraries.TypeLibrary;
|
|
|
|
namespace PkmnLibSharp.StaticData.Libraries
|
|
{
|
|
public class TypeLibrary : ExternPointer<TypeLibrary.CacheData>
|
|
{
|
|
public class CacheData
|
|
{
|
|
public Dictionary<string, TypeIdentifier> TypeCache { get; } =
|
|
new(StringComparer.InvariantCultureIgnoreCase);
|
|
}
|
|
|
|
public TypeLibrary(ulong capacity) : base(Interface.type_library_new(capacity), true)
|
|
{
|
|
}
|
|
|
|
internal TypeLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
|
|
{
|
|
}
|
|
|
|
public TypeIdentifier GetTypeId(string name)
|
|
{
|
|
if (Cache.TypeCache.TryGetValue(name, out var typeIdentifier))
|
|
return typeIdentifier;
|
|
throw new KeyNotFoundException($"No type found with name `{name}`");
|
|
}
|
|
|
|
public string GetTypeName(TypeIdentifier typeIdentifier)
|
|
{
|
|
var fd = Cache.TypeCache.FirstOrDefault(x => x.Value == typeIdentifier);
|
|
if (fd.Key != null)
|
|
return fd.Key;
|
|
throw new KeyNotFoundException($"No type found for given identifier");
|
|
}
|
|
|
|
|
|
public float GetSingleEffectiveness(TypeIdentifier attacking, TypeIdentifier defending) =>
|
|
Interface.type_library_get_single_effectiveness(Ptr, attacking, defending);
|
|
|
|
public float GetEffectiveness(TypeIdentifier attacking, TypeIdentifier[] defending)
|
|
{
|
|
var arrayPtr = defending.ArrayPtr();
|
|
return Interface.type_library_get_effectiveness(Ptr, attacking, arrayPtr, (ulong)defending.Length);
|
|
}
|
|
|
|
public TypeIdentifier RegisterType(string name)
|
|
{
|
|
var typeId = Interface.type_library_register_type(Ptr, name.ToPtr());
|
|
Cache.TypeCache.Add(name, typeId);
|
|
return typeId;
|
|
}
|
|
|
|
public void SetEffectiveness(TypeIdentifier attacking, TypeIdentifier defending, float effectiveness) =>
|
|
Interface.type_library_set_effectiveness(Ptr, attacking, defending, effectiveness);
|
|
|
|
|
|
protected override CacheData CreateCache() => new();
|
|
|
|
protected override void Destructor() => Interface.type_library_drop(Ptr);
|
|
|
|
~TypeLibrary()
|
|
{
|
|
Dispose();
|
|
}
|
|
}
|
|
} |