PkmnLibRSharp/PkmnLibRSharp/StaticData/Species.cs

61 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Species;
namespace PkmnLibSharp.StaticData
{
public class Species : ExternPointer<Species.CacheData>
{
public class CacheData
{
public ushort? Id { get; internal set; }
public string? Name { get; internal set; }
public float? GenderRate { get; internal set; }
public string? GrowthRate { get; internal set; }
public byte? CaptureRate { get; internal set; }
public Dictionary<string, Form> Forms { get; } = new();
}
internal Species(IntPtr ptr, bool isOwner) : base(ptr, isOwner)
{
}
public Species(ushort id, string name, float genderRate, string growthRate, byte captureRate, Form defaultForm,
IReadOnlyCollection<string> flags)
{
var flagsPtrArray = flags.Select(x => x.ToPtr()).ToArray();
var ptr = Interface.species_new(id, name.ToPtr(), genderRate, growthRate.ToPtr(), captureRate,
defaultForm.TakeOwnershipAndInvalidate(), flagsPtrArray.ArrayPtr(), (ulong)flags.Count);
InitializePointer(ptr, true);
}
public ushort Id => Cache.Id ??= Interface.species_id(Ptr);
public string Name => Cache.Name ??= Interface.species_name(Ptr).PtrString()!;
public float GenderRate => Cache.GenderRate ??= Interface.species_gender_rate(Ptr);
public string GrowthRate => Cache.GrowthRate ??= Interface.species_growth_rate(Ptr).PtrString()!;
public byte CaptureRate => Cache.CaptureRate ??= Interface.species_capture_rate(Ptr);
public bool TryGetForm(string formName, out Form? form)
{
if (Cache.Forms.TryGetValue(formName, out form))
return true;
var formPtr = Interface.species_get_form(Ptr, formName.ToPtr());
if (formPtr == IntPtr.Zero)
{
form = null;
return false;
}
form = new Form(formPtr, false);
Cache.Forms.Add(formName, form);
return true;
}
public void AddForm(Form form) =>
Interface.species_add_form(Ptr, form.Name.ToPtr(), form.TakeOwnershipAndInvalidate());
protected override CacheData CreateCache() => new CacheData();
protected override void Destructor() => Interface.species_drop(Ptr);
}
}