60 lines
2.3 KiB
C#
60 lines
2.3 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 : HandleType
|
|
{
|
|
public Dictionary<string, TypeIdentifier> TypeCache { get; private set; } =
|
|
new(StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
protected TypeLibrary(FFIHandle handle) : base(handle){}
|
|
|
|
public static TypeLibrary Create(ulong capacity)
|
|
{
|
|
var handle = Interface.type_library_new(capacity);
|
|
var lib = Resolver.Instance.ResolveTypeLibrary(handle.Resolve());
|
|
lib.TypeCache = new Dictionary<string, TypeIdentifier>((int)capacity, StringComparer.InvariantCultureIgnoreCase);
|
|
return lib;
|
|
}
|
|
|
|
public TypeIdentifier GetTypeId(string name)
|
|
{
|
|
if (TypeCache.TryGetValue(name, out var typeIdentifier))
|
|
return typeIdentifier;
|
|
throw new KeyNotFoundException($"No type found with name `{name}`");
|
|
}
|
|
|
|
public string GetTypeName(TypeIdentifier typeIdentifier)
|
|
{
|
|
var fd = 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(Handle, attacking, defending);
|
|
|
|
public float GetEffectiveness(TypeIdentifier attacking, TypeIdentifier[] defending)
|
|
{
|
|
var arrayPtr = defending.ArrayPtr();
|
|
return Interface.type_library_get_effectiveness(Handle, attacking, arrayPtr, (ulong)defending.Length);
|
|
}
|
|
|
|
public TypeIdentifier RegisterType(string name)
|
|
{
|
|
var typeId = Interface.type_library_register_type(Handle, name.ToPtr());
|
|
TypeCache.Add(name, typeId);
|
|
return typeId;
|
|
}
|
|
|
|
public void SetEffectiveness(TypeIdentifier attacking, TypeIdentifier defending, float effectiveness) =>
|
|
Interface.type_library_set_effectiveness(Handle, attacking, defending, effectiveness);
|
|
}
|
|
} |