PkmnLibRSharp/PkmnLibRSharp/StaticData/StatisticSet.cs

128 lines
5.7 KiB
C#

using System;
using JetBrains.Annotations;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.StatisticSet;
namespace PkmnLibSharp.StaticData
{
public class StatisticSet<T> : ExternPointer<object> where T : struct, IConvertible
{
internal StatisticSet(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
{
}
public StatisticSet(T hp, T attack, T defense, T specialAttack, T specialDefense, T speed)
{
var p = typeof(T) switch
{
{ } t when t == typeof(byte) => Interface.statistic_set_u8_new(Convert.ToByte(hp),
Convert.ToByte(attack), Convert.ToByte(defense), Convert.ToByte(specialAttack),
Convert.ToByte(specialDefense), Convert.ToByte(speed)),
{ } t when t == typeof(ushort) => Interface.statistic_set_u16_new(Convert.ToUInt16(hp),
Convert.ToUInt16(attack), Convert.ToUInt16(defense), Convert.ToUInt16(specialAttack),
Convert.ToUInt16(specialDefense), Convert.ToUInt16(speed)),
{ } t when t == typeof(uint) => Interface.statistic_set_u32_new(Convert.ToUInt32(hp),
Convert.ToUInt32(attack), Convert.ToUInt32(defense), Convert.ToUInt32(specialAttack),
Convert.ToUInt32(specialDefense), Convert.ToUInt32(speed)),
{ } t when t == typeof(sbyte) => Interface.statistic_set_i8_new(Convert.ToSByte(hp),
Convert.ToSByte(attack), Convert.ToSByte(defense), Convert.ToSByte(specialAttack),
Convert.ToSByte(specialDefense), Convert.ToSByte(speed)),
{ } t when t == typeof(short) => Interface.statistic_set_i16_new(Convert.ToInt16(hp),
Convert.ToInt16(attack), Convert.ToInt16(defense), Convert.ToInt16(specialAttack),
Convert.ToInt16(specialDefense), Convert.ToInt16(speed)),
{ } t when t == typeof(int) => Interface.statistic_set_i32_new(Convert.ToInt32(hp),
Convert.ToInt32(attack), Convert.ToInt32(defense), Convert.ToInt32(specialAttack),
Convert.ToInt32(specialDefense), Convert.ToInt32(speed)),
_ => throw new ArgumentOutOfRangeException()
};
InitializePointer(p, true);
}
[PublicAPI]
public T GetStatistic(Statistic statistic)
{
object p = typeof(T) switch
{
{ } t when t == typeof(byte) => Interface.statistic_set_u8_get_stat(Ptr, statistic),
{ } t when t == typeof(ushort) => Interface.statistic_set_u16_get_stat(Ptr, statistic),
{ } t when t == typeof(uint) => Interface.statistic_set_u32_get_stat(Ptr, statistic),
{ } t when t == typeof(sbyte) => Interface.statistic_set_i8_get_stat(Ptr, statistic),
{ } t when t == typeof(short) => Interface.statistic_set_i16_get_stat(Ptr, statistic),
{ } t when t == typeof(int) => Interface.statistic_set_i32_get_stat(Ptr, statistic),
_ => throw new ArgumentOutOfRangeException()
};
return (T)p;
}
[PublicAPI]
public void SetStatistic(Statistic statistic, T value)
{
if (typeof(T) == typeof(byte)) Interface.statistic_set_u8_set_stat(Ptr, statistic, Convert.ToByte(value));
else if (typeof(T) == typeof(ushort))
Interface.statistic_set_u16_set_stat(Ptr, statistic, Convert.ToUInt16(value));
else if (typeof(T) == typeof(uint))
Interface.statistic_set_u32_set_stat(Ptr, statistic, Convert.ToUInt32(value));
else if (typeof(T) == typeof(sbyte))
Interface.statistic_set_i8_set_stat(Ptr, statistic, Convert.ToSByte(value));
else if (typeof(T) == typeof(short))
Interface.statistic_set_i16_set_stat(Ptr, statistic, Convert.ToInt16(value));
else if (typeof(T) == typeof(int))
Interface.statistic_set_i32_set_stat(Ptr, statistic, Convert.ToInt32(value));
}
public T HP
{
get => GetStatistic(Statistic.HP);
set => SetStatistic(Statistic.HP, value);
}
public T Attack
{
get => GetStatistic(Statistic.Attack);
set => SetStatistic(Statistic.Attack, value);
}
public T Defense
{
get => GetStatistic(Statistic.Defense);
set => SetStatistic(Statistic.Defense, value);
}
public T SpecialAttack
{
get => GetStatistic(Statistic.SpecialAttack);
set => SetStatistic(Statistic.SpecialAttack, value);
}
public T SpecialDefense
{
get => GetStatistic(Statistic.SpecialDefense);
set => SetStatistic(Statistic.SpecialDefense, value);
}
public T Speed
{
get => GetStatistic(Statistic.Speed);
set => SetStatistic(Statistic.Speed, value);
}
protected override object CreateCache() => new();
protected override void Destructor()
{
if (typeof(T) == typeof(byte)) Interface.statistic_set_u8_drop(Ptr);
else if (typeof(T) == typeof(ushort)) Interface.statistic_set_u16_drop(Ptr);
else if (typeof(T) == typeof(uint)) Interface.statistic_set_u32_drop(Ptr);
else if (typeof(T) == typeof(sbyte)) Interface.statistic_set_i8_drop(Ptr);
else if (typeof(T) == typeof(short)) Interface.statistic_set_i16_drop(Ptr);
else if (typeof(T) == typeof(int)) Interface.statistic_set_i32_drop(Ptr);
}
~StatisticSet()
{
Dispose();
}
}
}