PkmnLibRSharp/PkmnLibRSharp/StaticData/StaticStatisticSet.cs

133 lines
6.2 KiB
C#

using System;
using JetBrains.Annotations;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.StaticStatisticSet;
namespace PkmnLibSharp.StaticData
{
public class StaticStatisticSet<T> : ExternPointer<StaticStatisticSet<T>.CacheData> where T : struct, IConvertible
{
public class CacheData
{
public T? HP { get; internal set; }
public T? Attack { get; internal set; }
public T? Defense { get; internal set; }
public T? SpecialAttack { get; internal set; }
public T? SpecialDefense { get; internal set; }
public T? Speed { get; internal set; }
}
internal StaticStatisticSet(IntPtr ptr, bool isOwner) : base(ptr, isOwner)
{
}
public StaticStatisticSet(T hp, T attack, T defense, T specialAttack, T specialDefense, T speed)
{
var p = typeof(T) switch
{
{ } t when t == typeof(byte) => Interface.static_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.static_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.static_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.static_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.static_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.static_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)
{
switch (statistic)
{
case Statistic.HP when Cache.HP.HasValue:
return Cache.HP.Value;
case Statistic.Attack when Cache.Attack.HasValue:
return Cache.Attack.Value;
case Statistic.Defense when Cache.Defense.HasValue:
return Cache.Defense.Value;
case Statistic.SpecialAttack when Cache.SpecialAttack.HasValue:
return Cache.SpecialAttack.Value;
case Statistic.SpecialDefense when Cache.SpecialDefense.HasValue:
return Cache.SpecialDefense.Value;
case Statistic.Speed when Cache.Speed.HasValue:
return Cache.Speed.Value;
}
object p = typeof(T) switch
{
{ } t when t == typeof(byte) => Interface.static_statistic_set_u8_get_stat(Ptr, statistic),
{ } t when t == typeof(ushort) => Interface.static_statistic_set_u16_get_stat(Ptr, statistic),
{ } t when t == typeof(uint) => Interface.static_statistic_set_u32_get_stat(Ptr, statistic),
{ } t when t == typeof(sbyte) => Interface.static_statistic_set_i8_get_stat(Ptr, statistic),
{ } t when t == typeof(short) => Interface.static_statistic_set_i16_get_stat(Ptr, statistic),
{ } t when t == typeof(int) => Interface.static_statistic_set_i32_get_stat(Ptr, statistic),
_ => throw new ArgumentOutOfRangeException()
};
switch (statistic)
{
case Statistic.HP:
Cache.HP = (T)p;
break;
case Statistic.Attack:
Cache.Attack = (T)p;
break;
case Statistic.Defense:
Cache.Defense = (T)p;
break;
case Statistic.SpecialAttack:
Cache.SpecialAttack = (T)p;
break;
case Statistic.SpecialDefense:
Cache.SpecialDefense = (T)p;
break;
case Statistic.Speed:
Cache.Speed = (T)p;
break;
}
return (T)p;
}
public T HP => GetStatistic(Statistic.HP);
public T Attack => GetStatistic(Statistic.Attack);
public T Defense => GetStatistic(Statistic.Defense);
public T SpecialAttack => GetStatistic(Statistic.SpecialAttack);
public T SpecialDefense => GetStatistic(Statistic.SpecialDefense);
public T Speed => GetStatistic(Statistic.Speed);
protected override CacheData CreateCache()
{
return new CacheData();
}
protected override void Destructor()
{
if (typeof(T) == typeof(byte)) Interface.static_statistic_set_u8_drop(Ptr);
else if (typeof(T) == typeof(ushort)) Interface.static_statistic_set_u16_drop(Ptr);
else if (typeof(T) == typeof(uint)) Interface.static_statistic_set_u32_drop(Ptr);
else if (typeof(T) == typeof(sbyte)) Interface.static_statistic_set_i8_drop(Ptr);
else if (typeof(T) == typeof(short)) Interface.static_statistic_set_i16_drop(Ptr);
else if (typeof(T) == typeof(int)) Interface.static_statistic_set_i32_drop(Ptr);
}
}
}