93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
using System;
|
|
using JetBrains.Annotations;
|
|
using PkmnLibSharp.Utils;
|
|
using Interface = PkmnLibSharp.FFI.StaticData.EffectParameter;
|
|
|
|
namespace PkmnLibSharp.StaticData
|
|
{
|
|
public class EffectParameter : ExternPointer<EffectParameter.CacheData>
|
|
{
|
|
[UsedImplicitly]
|
|
public class CacheData
|
|
{
|
|
public ParameterType? Type { get; internal set; }
|
|
public object? Data { get; internal set; }
|
|
}
|
|
|
|
internal EffectParameter(IntPtr ptr, bool isOwner) : base(ptr, isOwner)
|
|
{
|
|
}
|
|
|
|
public EffectParameter(bool b) : base(Interface.effect_parameter_new_bool(b.ForeignBool()), true)
|
|
{
|
|
}
|
|
|
|
public EffectParameter(long l) : base(Interface.effect_parameter_new_int(l), true)
|
|
{
|
|
}
|
|
|
|
public EffectParameter(float f) : base(Interface.effect_parameter_new_float(f), true)
|
|
{
|
|
}
|
|
|
|
public EffectParameter(string s) : base(Interface.effect_parameter_new_string(s.ToPtr()), true)
|
|
{
|
|
}
|
|
|
|
public enum ParameterType : byte
|
|
{
|
|
Bool = 0,
|
|
Int = 1,
|
|
Float = 2,
|
|
String = 3
|
|
}
|
|
|
|
public ParameterType Type
|
|
{
|
|
get
|
|
{
|
|
var cache = Cache.Type;
|
|
if (cache.HasValue)
|
|
return cache.Value;
|
|
Cache.Type = (ParameterType)Interface.effect_parameter_get_type(Ptr);
|
|
return Cache.Type.Value;
|
|
}
|
|
}
|
|
|
|
public object Data
|
|
{
|
|
get
|
|
{
|
|
var cache = Cache.Data;
|
|
if (cache != null)
|
|
return cache;
|
|
var type = Type;
|
|
Cache.Data = type switch
|
|
{
|
|
ParameterType.Bool => Interface.effect_parameter_get_as_bool(Ptr) == 1,
|
|
ParameterType.Int => Interface.effect_parameter_get_as_int(Ptr),
|
|
ParameterType.Float => Interface.effect_parameter_get_as_float(Ptr),
|
|
ParameterType.String => Interface.effect_parameter_get_as_string(Ptr).PtrString(),
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
};
|
|
return Cache.Data!;
|
|
}
|
|
}
|
|
|
|
protected override CacheData CreateCache()
|
|
{
|
|
return new CacheData();
|
|
}
|
|
|
|
protected override void Destructor()
|
|
{
|
|
Interface.effect_parameter_drop(Ptr);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
var data = Data;
|
|
return data is string ? $"{Type}(\"{data}\")" : $"{Type}({data})";
|
|
}
|
|
}
|
|
} |