57 lines
2.0 KiB
C#
57 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using PkmnLibSharp.FFI;
|
|
using PkmnLibSharp.Utils;
|
|
using Interface = PkmnLibSharp.FFI.StaticData.Libraries.NatureLibrary;
|
|
|
|
namespace PkmnLibSharp.StaticData.Libraries
|
|
{
|
|
public class NatureLibrary : HandleType
|
|
{
|
|
private Dictionary<string, Nature> _natures = new();
|
|
|
|
protected NatureLibrary(FFIHandle handle) : base(handle)
|
|
{
|
|
}
|
|
|
|
public static NatureLibrary Create(ulong capacity)
|
|
{
|
|
var handle = Interface.nature_library_new(capacity);
|
|
var lib = Resolver.Instance.ResolveNatureLibrary(handle.Resolve());
|
|
if (capacity > 0)
|
|
lib._natures = new Dictionary<string, Nature>((int)capacity, StringComparer.InvariantCultureIgnoreCase);
|
|
return lib;
|
|
}
|
|
|
|
public bool TryGetNature(string name, [NotNullWhen(true)] out Nature? nature)
|
|
{
|
|
if (_natures.TryGetValue(name, out nature))
|
|
return true;
|
|
var naturePtr = Interface.nature_library_get_nature(Handle, name.ToPtr());
|
|
if (naturePtr.Handle == 0)
|
|
return false;
|
|
nature = Resolver.Instance.ResolveNature(naturePtr.Resolve());
|
|
_natures.Add(name, nature);
|
|
return true;
|
|
}
|
|
|
|
public Nature GetRandomNature(ulong seed)
|
|
{
|
|
return Resolver.Instance.ResolveNature(Interface.nature_library_get_random_nature(Handle, seed).Result().Resolve());
|
|
}
|
|
|
|
public string GetNatureName(Nature nature)
|
|
{
|
|
var fd = _natures.FirstOrDefault(x => x.Value == nature);
|
|
return fd.Key ?? Interface.nature_library_get_nature_name(Handle, nature.Handle).PtrString()!;
|
|
}
|
|
|
|
public void LoadNature(string name, Nature nature)
|
|
{
|
|
Interface.nature_library_load_nature(Handle, name.ToPtr(), nature.Handle);
|
|
_natures.Add(name, nature);
|
|
}
|
|
}
|
|
} |