59 lines
2.1 KiB
C#
59 lines
2.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using JetBrains.Annotations;
|
|
using PkmnLibSharp.Utils;
|
|
using Interface = PkmnLibSharp.FFI.StaticData.Ability;
|
|
|
|
namespace PkmnLibSharp.StaticData
|
|
{
|
|
public class Ability : ExternPointer<Ability.CacheData>
|
|
{
|
|
[UsedImplicitly]
|
|
public class CacheData
|
|
{
|
|
public string? Name { get; internal set; }
|
|
public string? Effect { get; internal set; }
|
|
public ulong? ParameterLength { get; internal set; }
|
|
public EffectParameter?[]? Parameters { get; internal set; }
|
|
}
|
|
|
|
public Ability(string name, string effect, IReadOnlyCollection<EffectParameter> parameters)
|
|
{
|
|
// Passing effect parameters to Rust gives Rust full control over them, and means it can move it. As such
|
|
// we remove ownership and invalidate the passed parameters.
|
|
var parameterArray = parameters.Select(x => x.TakeOwnershipAndInvalidate()).ToArray();
|
|
var arrayPtr = parameterArray.ArrayPtr();
|
|
InitializePointer(Interface.ability_new(name.ToPtr(), effect.ToPtr(), arrayPtr, (ulong)parameters.Count),
|
|
true);
|
|
}
|
|
|
|
public string Name => Cache.Name ?? (Cache.Name = Interface.ability_name(Ptr));
|
|
public string Effect => Cache.Effect ?? (Cache.Effect = Interface.ability_effect(Ptr));
|
|
|
|
public ulong ParameterLength =>
|
|
Cache.ParameterLength ?? (Cache.ParameterLength = Interface.ability_parameter_length(Ptr)).Value;
|
|
|
|
public EffectParameter GetParameter(int index)
|
|
{
|
|
Cache.Parameters ??= new EffectParameter[ParameterLength];
|
|
if (Cache.Parameters[index] == null)
|
|
{
|
|
var ptr = Interface.ability_parameter_get(Ptr, (ulong)index);
|
|
Cache.Parameters[index] = new EffectParameter(ptr, false);
|
|
}
|
|
|
|
return Cache.Parameters[index]!;
|
|
}
|
|
|
|
|
|
protected override CacheData CreateCache()
|
|
{
|
|
return new CacheData();
|
|
}
|
|
|
|
protected override void Destructor()
|
|
{
|
|
Interface.ability_drop(Ptr);
|
|
}
|
|
}
|
|
} |