PkmnLibRSharp/PkmnLibRSharp/StaticData/EffectParameter.cs

119 lines
4.1 KiB
C#

using System;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.EffectParameter;
namespace PkmnLibSharp.StaticData
{
/// <summary>
/// A parameter for an effect. This is basically a simple way to dynamically store multiple different
/// primitives on data.
/// </summary>
public class EffectParameter : HandleType
{
private ParameterType? _type;
/// <summary>
/// The underlying type of the parameter.
/// </summary>
public ParameterType? Type => _type ??= (ParameterType)Interface.effect_parameter_get_type(Handle);
private object? _data;
/// <summary>
/// The data stored in the parameter.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when the parameter type is not recognized. This should never happen.
/// </exception>
public object Data
{
get
{
_data ??= Type switch
{
ParameterType.Bool => Interface.effect_parameter_get_as_bool(Handle).Result() == 1,
ParameterType.Int => Interface.effect_parameter_get_as_int(Handle).Result(),
ParameterType.Float => Interface.effect_parameter_get_as_float(Handle).Result(),
ParameterType.String => Interface.effect_parameter_get_as_string(Handle).Result().PtrString()!,
_ => throw new ArgumentOutOfRangeException()
};
return _data!;
}
}
/// <inheritdoc cref="EffectParameter"/>
protected EffectParameter(FFIHandle handle) : base(handle)
{
}
/// <summary>
/// Creates a new parameter from a boolean.
/// </summary>
public static EffectParameter FromBool(bool b) =>
Resolver.Instance.ResolveEffectParameter(Interface.effect_parameter_new_bool(b.ForeignBool()).Resolve());
/// <summary>
/// Creates a new parameter from a 64 bit integer.
/// </summary>
public static EffectParameter FromLong(long l) =>
Resolver.Instance.ResolveEffectParameter(Interface.effect_parameter_new_int(l).Resolve());
/// <summary>
/// Creates a new parameter from a float.
/// </summary>
public static EffectParameter FromFloat(float f) =>
Resolver.Instance.ResolveEffectParameter(Interface.effect_parameter_new_float(f).Resolve());
/// <summary>
/// Creates a new parameter from a string.
/// </summary>
public static EffectParameter FromString(string s) =>
Resolver.Instance.ResolveEffectParameter(
Interface.effect_parameter_new_string(s.ToPtr()).Result().Resolve());
/// <inheritdoc cref="FromBool"/>
public static implicit operator EffectParameter(bool b) => FromBool(b);
/// <inheritdoc cref="FromLong"/>
public static implicit operator EffectParameter(long l) => FromLong(l);
/// <inheritdoc cref="FromFloat"/>
public static implicit operator EffectParameter(float f) => FromFloat(f);
/// <inheritdoc cref="FromString"/>
public static implicit operator EffectParameter(string s) => FromString(s);
/// <summary>
/// The different types of parameters.
/// </summary>
public enum ParameterType : byte
{
/// <summary>
/// A boolean parameter.
/// </summary>
Bool = 0,
/// <summary>
/// A 64 bit integer parameter.
/// </summary>
Int = 1,
/// <summary>
/// A 32 bit floating point parameter.
/// </summary>
Float = 2,
/// <summary>
/// A string parameter.
/// </summary>
String = 3,
}
/// <inheritdoc />
public override string ToString()
{
var data = Data;
return data is string ? $"{Type}(\"{data}\")" : $"{Type}({data})";
}
}
}