Refactor entire library in line with new PkmnLib
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2023-06-25 16:30:12 +02:00
parent 2448f3bfb5
commit a8a6b775b3
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
93 changed files with 1294 additions and 2190 deletions

View File

@ -1,70 +1,56 @@
using PkmnLibSharp.FFI;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.DynamicData.LearnedMove;
namespace PkmnLibSharp.DynamicData
{
public class LearnedMove : ExternPointer<LearnedMove.CacheData>
public class LearnedMove : HandleType
{
public class CacheData
private LearnedMove(FFIHandle handle) : base(handle){}
public static LearnedMove Create(MoveData moveData, MoveLearnMethod learnMethod)
{
public MoveData? MoveData { get; internal set; }
public MoveLearnMethod? LearnMethod { get; internal set; }
}
internal LearnedMove(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
{
}
public LearnedMove(MoveData moveData, MoveLearnMethod learnMethod)
{
InitializePointer(Interface.learned_move_new(moveData.Ptr, learnMethod), true);
var handle = Interface.learned_move_new(moveData.Handle, learnMethod);
return Resolver.Instance.ResolveLearnedMove(handle.Resolve());
}
private MoveData? _moveData;
/// <summary>
/// The immutable move information of the move.
/// </summary>
public MoveData MoveData => Cache.MoveData ??= new MoveData(Interface.learned_move_move_data(Ptr), true);
public MoveData MoveData => _moveData ??= Resolver.Instance.ResolveMoveData(Interface.learned_move_move_data(Handle).Resolve());
/// <summary>
/// The maximal power points for this move.
/// </summary>
public byte MaxPP => Interface.learned_move_max_pp(Ptr);
public byte MaxPP => Interface.learned_move_max_pp(Handle);
/// <summary>
/// The amount of remaining power points. If this is 0, we can not use the move anymore.
/// </summary>
public byte RemainingPP => Interface.learned_move_remaining_pp(Ptr);
public byte RemainingPP => Interface.learned_move_remaining_pp(Handle);
private MoveLearnMethod? _learnMethod;
/// <summary>
/// The way the move was learned.
/// </summary>
public MoveLearnMethod LearnMethod => Cache.LearnMethod ??= Interface.learned_move_learn_method(Ptr);
public MoveLearnMethod LearnMethod => _learnMethod ??= Interface.learned_move_learn_method(Handle);
/// <summary>
/// Try and reduce the PP by a certain amount. If the amount is higher than the current remaining uses,
/// return false. Otherwise, reduce the PP, and return true.
/// </summary>
public bool TryUse(byte amount) => Interface.learned_move_try_use(Ptr, amount) == 1;
public bool TryUse(byte amount) => Interface.learned_move_try_use(Handle, amount) == 1;
/// <summary>
/// Set the remaining PP to the max amount of PP.
/// </summary>
public void RestoreAllUses() => Interface.learned_move_restore_all_uses(Ptr);
public void RestoreAllUses() => Interface.learned_move_restore_all_uses(Handle);
/// <summary>
/// Restore the remaining PP by a certain amount. Will prevent it from going above max PP.
/// </summary>
public void RestoreUses(byte amount) => Interface.learned_move_restore_uses(Ptr, amount);
protected override CacheData CreateCache() => new();
protected override void Destructor() => Interface.learned_move_drop(Ptr);
~LearnedMove()
{
Dispose();
}
public void RestoreUses(byte amount) => Interface.learned_move_restore_uses(Handle, amount);
}
public enum MoveLearnMethod : byte

View File

@ -4,19 +4,19 @@ using Interface = PkmnLibSharp.FFI.DynamicData.Libraries.BattleStatCalculator;
namespace PkmnLibSharp.DynamicData.Libraries
{
public abstract class BattleStatCalculator : ExternPointer<object>
public abstract class BattleStatCalculator : HandleType
{
protected BattleStatCalculator(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner){}
protected override object CreateCache() => new();
protected override void Destructor() => Interface.battle_stat_calculator_drop(Ptr);
protected BattleStatCalculator(FFIHandle handle) : base(handle){}
}
public class Gen7BattleStatCalculator : BattleStatCalculator
{
public Gen7BattleStatCalculator() : base(Interface.gen_7_battle_stat_calculator_new(), true)
protected Gen7BattleStatCalculator(FFIHandle handle) : base(handle){}
public static Gen7BattleStatCalculator Create()
{
var handle = Interface.gen_7_battle_stat_calculator_new();
return Resolver.Instance.ResolveGen7BattleStatCalculator(handle.Resolve());
}
}
}

View File

@ -4,22 +4,23 @@ using Interface = PkmnLibSharp.FFI.DynamicData.Libraries.DamageLibrary;
namespace PkmnLibSharp.DynamicData.Libraries
{
public abstract class DamageLibrary : ExternPointer<object>
public abstract class DamageLibrary : HandleType
{
protected DamageLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
protected DamageLibrary(FFIHandle ptr) : base(ptr)
{
}
protected override object CreateCache() => new();
protected override void Destructor() => Interface.damage_library_drop(Ptr);
}
public class Gen7DamageLibrary : DamageLibrary
{
public Gen7DamageLibrary(bool hasRandomness) : base(
Interface.gen_7_damage_library_new((byte)(hasRandomness ? 1 : 0)), true)
public Gen7DamageLibrary(FFIHandle ptr) : base(ptr)
{
}
public static Gen7DamageLibrary Create(bool hasRandomness)
{
var handle = Interface.gen_7_damage_library_new((byte)(hasRandomness ? 1 : 0));
return Resolver.Instance.ResolveGen7DamageLibrary(handle.Resolve());
}
}
}

View File

@ -4,31 +4,23 @@ using Interface = PkmnLibSharp.FFI.DynamicData.Libraries.DynamicLibrary;
namespace PkmnLibSharp.DynamicData.Libraries
{
public class DynamicLibrary : ExternPointer<DynamicLibrary.CacheData>
public class DynamicLibrary : HandleType
{
public class CacheData
{
public StaticData.Libraries.StaticData? StaticData { get; internal set; }
}
internal DynamicLibrary(IdentifiablePointer ptr) : base(ptr, false)
protected DynamicLibrary(FFIHandle handle) : base(handle)
{
}
public DynamicLibrary(StaticData.Libraries.StaticData staticData, BattleStatCalculator statCalculator,
DamageLibrary damageLibrary, MiscLibrary miscLibrary, ScriptResolver scriptResolver) : base(
Interface.dynamic_library_new(staticData.TakeOwnershipAndInvalidate(),
statCalculator.TakeOwnershipAndInvalidate(), damageLibrary.TakeOwnershipAndInvalidate(),
miscLibrary.TakeOwnershipAndInvalidate(), scriptResolver.TakeOwnershipAndInvalidate()), true)
public static DynamicLibrary Create(StaticData.Libraries.StaticData staticData,
BattleStatCalculator statCalculator, DamageLibrary damageLibrary, MiscLibrary miscLibrary,
ScriptResolver scriptResolver)
{
var handle = Interface.dynamic_library_new(staticData.Handle, statCalculator.Handle, damageLibrary.Handle,
miscLibrary.Handle, scriptResolver.Handle);
var lib = Resolver.Instance.ResolveDynamicLibrary(handle.Resolve());
lib.StaticData = staticData;
return lib;
}
public StaticData.Libraries.StaticData StaticData =>
Cache.StaticData ??=
new StaticData.Libraries.StaticData(Interface.dynamic_library_get_static_data(Ptr), false);
protected override CacheData CreateCache() => new();
protected override void Destructor() => Interface.dynamic_library_drop(Ptr);
public StaticData.Libraries.StaticData StaticData { get; private set; } = null!;
}
}

View File

@ -4,21 +4,23 @@ using Interface = PkmnLibSharp.FFI.DynamicData.Libraries.MiscLibrary;
namespace PkmnLibSharp.DynamicData.Libraries
{
public abstract class MiscLibrary : ExternPointer<object>
public abstract class MiscLibrary : HandleType
{
protected MiscLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
protected MiscLibrary(FFIHandle handle) : base(handle)
{
}
protected override object CreateCache() => new();
protected override void Destructor() => Interface.misc_library_drop(Ptr);
}
public class Gen7MiscLibrary : MiscLibrary
{
public Gen7MiscLibrary() : base(Interface.gen_7_misc_library_new(), true)
protected Gen7MiscLibrary(FFIHandle handle) : base(handle)
{
}
public static Gen7MiscLibrary Create()
{
var handle = Interface.gen_7_misc_library_new();
return Resolver.Instance.ResolveGen7MiscLibrary(handle.Resolve());
}
}
}

View File

@ -5,19 +5,9 @@ using Interface = PkmnLibSharp.FFI.DynamicData.Libraries.ScriptResolver;
namespace PkmnLibSharp.DynamicData.Libraries
{
public abstract class ScriptResolver : ExternPointer<ScriptResolver.CacheData>
public abstract class ScriptResolver : HandleType
{
protected ScriptResolver(IdentifiablePointer ptr) : base(ptr, true){}
protected ScriptResolver(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner){}
public class CacheData{}
protected override CacheData CreateCache() => new();
protected override void Destructor()
{
Interface.script_resolver_drop(Ptr);
}
protected ScriptResolver(FFIHandle handle) : base(handle){}
}
/// <summary>
@ -25,6 +15,12 @@ namespace PkmnLibSharp.DynamicData.Libraries
/// </summary>
public class EmptyScriptResolver : ScriptResolver
{
public EmptyScriptResolver() : base(Interface.empty_script_resolver_new()){}
protected EmptyScriptResolver(FFIHandle handle) : base(handle){}
public static EmptyScriptResolver Create()
{
var handle = Interface.empty_script_resolver_new();
return Resolver.Instance.ResolveEmptyScriptResolver(handle.Resolve());
}
}
}

View File

@ -6,7 +6,15 @@ namespace PkmnLibSharp.DynamicData.Libraries
{
public class WasmScriptResolver : ScriptResolver
{
public WasmScriptResolver() : base(Interface.webassembly_script_resolver_new(), true){}
protected WasmScriptResolver(FFIHandle handle) : base(handle)
{
}
public static WasmScriptResolver Create()
{
var handle = Interface.webassembly_script_resolver_new();
return Resolver.Instance.ResolveWasmScriptResolver(handle.Resolve());
}
/// <summary>
/// Loads a compiled WASM module into the script resolver.
@ -14,7 +22,8 @@ namespace PkmnLibSharp.DynamicData.Libraries
/// <param name="data">The bytes of a compiled WASM module</param>
public void LoadBytes(byte[] data)
{
Interface.webassembly_script_resolver_load_wasm_from_bytes(Ptr, data.ArrayPtr(), (ulong)data.LongLength);
Interface.webassembly_script_resolver_load_wasm_from_bytes(Handle, data.ArrayPtr(), (ulong)data.LongLength)
.Result();
}
/// <summary>
@ -23,7 +32,7 @@ namespace PkmnLibSharp.DynamicData.Libraries
/// </summary>
public void FinalizeResolver()
{
Interface.webassembly_script_resolver_finalize(Ptr);
Interface.webassembly_script_resolver_finalize(Handle).Result();
}
}
}

View File

@ -8,144 +8,148 @@ using Species = PkmnLibSharp.StaticData.Species;
namespace PkmnLibSharp.DynamicData
{
public class Pokemon : ExternPointer<Pokemon.CacheData>
public class Pokemon : HandleType
{
public Pokemon(DynamicLibrary dynamicLibrary, Species species, Form form, bool hiddenAbility, byte abilityIndex,
LevelInt level, uint uid, Gender gender, byte coloring, string nature) : base(
Interface.pokemon_new(dynamicLibrary.Ptr, species.Ptr, form.Ptr, hiddenAbility.ForeignBool(), abilityIndex,
level, uid, gender, coloring, nature.ToPtr()), true)
protected Pokemon(FFIHandle handle) : base(handle){}
public static Pokemon Create(DynamicLibrary dynamicLibrary, Species species, Form form, bool hiddenAbility, byte abilityIndex,
LevelInt level, uint uid, Gender gender, byte coloring, string nature)
{
var handle = Interface.pokemon_new(dynamicLibrary.Handle, species.Handle, form.Handle, hiddenAbility.ForeignBool(),
abilityIndex, level, uid, gender, coloring, nature.ToPtr())
.Result();
return Resolver.Instance.ResolvePokemon(handle.Resolve());
}
private DynamicLibrary? _library;
/// <summary>
/// The library data the Pokemon uses.
/// </summary>
public DynamicLibrary Library => Cache.DynamicLibrary ??= new DynamicLibrary(Interface.pokemon_library(Ptr));
public DynamicLibrary Library =>
_library ??= Resolver.Instance.ResolveDynamicLibrary(Interface.pokemon_library(Handle).Resolve());
/// <summary>
/// The species of the Pokemon.
/// </summary>
public Species Species => new(Interface.pokemon_species(Ptr));
public Species Species => Resolver.Instance.ResolveSpecies(Interface.pokemon_species(Handle).Resolve());
/// <summary>
/// The form of the Pokemon.
/// </summary>
public Form Form => new(Interface.pokemon_form(Ptr));
public Form Form => Resolver.Instance.ResolveForm(Interface.pokemon_form(Handle).Resolve());
/// <summary>
/// The species that should be displayed to the user. This handles stuff like the Illusion ability.
/// </summary>
public Species DisplaySpecies => new(Interface.pokemon_display_species(Ptr));
public Species DisplaySpecies =>
Resolver.Instance.ResolveSpecies(Interface.pokemon_display_species(Handle).Resolve());
/// <summary>
/// The form that should be displayed to the user. This handles stuff like the Illusion ability.
/// </summary>
public Form DisplayForm => new(Interface.pokemon_display_form(Ptr));
public Form DisplayForm => Resolver.Instance.ResolveForm(Interface.pokemon_display_form(Handle).Resolve());
public LevelInt Level => Interface.pokemon_level(Ptr);
public uint Experience => Interface.pokemon_experience(Ptr);
public uint UniqueIdentifier => Interface.pokemon_unique_identifier(Ptr);
public Gender Gender => Interface.pokemon_gender(Ptr);
public byte Coloring => Interface.pokemon_coloring(Ptr);
public LevelInt Level => Interface.pokemon_level(Handle);
public uint Experience => Interface.pokemon_experience(Handle);
public uint UniqueIdentifier => Interface.pokemon_unique_identifier(Handle);
public Gender Gender => Interface.pokemon_gender(Handle);
public byte Coloring => Interface.pokemon_coloring(Handle);
public bool IsShiny => Coloring == 1;
public Item? HeldItem
{
get
{
var ptr = Interface.pokemon_held_item(Ptr);
return ptr.IsNull ? null : new Item(ptr);
var ptr = Interface.pokemon_held_item(Handle);
return ptr.IsNull ? null : Resolver.Instance.ResolveItem(ptr.Resolve());
}
}
public bool HasHeldItem(string name) => Interface.pokemon_has_held_item(Ptr, name.ToPtr()) == 1;
public bool HasHeldItem(string name) => Interface.pokemon_has_held_item(Handle, name.ToPtr()) == 1;
public void SetHeldItem(Item? item)
{
if (item == null)
RemoveHeldItem();
else
Interface.pokemon_set_held_item(Ptr, item.Ptr);
Interface.pokemon_set_held_item(Handle, item.Handle);
}
public void RemoveHeldItem() => Interface.pokemon_remove_held_item(Ptr);
public void RemoveHeldItem() => Interface.pokemon_remove_held_item(Handle);
public bool ConsumeHeldItem() => Interface.pokemon_consume_held_item(Ptr) == 1;
public bool ConsumeHeldItem() => Interface.pokemon_consume_held_item(Handle).Result() == 1;
public uint CurrentHealth => Interface.pokemon_current_health(Ptr);
public uint MaxHealth => Interface.pokemon_max_health(Ptr);
public float Weight => Interface.pokemon_weight(Ptr);
public float Height => Interface.pokemon_height(Ptr);
public uint CurrentHealth => Interface.pokemon_current_health(Handle);
public uint MaxHealth => Interface.pokemon_max_health(Handle);
public float Weight => Interface.pokemon_weight(Handle);
public float Height => Interface.pokemon_height(Handle);
public string? Nickname => Interface.pokemon_nickname(Ptr).PtrString();
public bool HasHiddenAbility => Interface.pokemon_real_ability_is_hidden(Ptr) == 1;
public byte AbilityIndex => Interface.pokemon_real_ability_index(Ptr);
public string? Nickname => Interface.pokemon_nickname(Handle).Result().PtrString();
public bool HasHiddenAbility => Interface.pokemon_real_ability_is_hidden(Handle) == 1;
public byte AbilityIndex => Interface.pokemon_real_ability_index(Handle);
private ExternValueArray<TypeIdentifier>? _types;
public ExternValueArray<TypeIdentifier> Types =>
Cache.Types ??= new ExternValueArray<TypeIdentifier>(() => Interface.pokemon_types_length(Ptr),
arg => Interface.pokemon_types_get(Ptr, arg));
_types ??= new ExternValueArray<TypeIdentifier>(() => Interface.pokemon_types_length(Handle),
arg => Interface.pokemon_types_get(Handle, arg).Result());
public LearnedMove? LearnedMove(ulong index)
{
var ptr = Interface.pokemon_learned_move_get(Ptr, index);
return ptr.IsNull ? null : new LearnedMove(ptr, false);
var ptr = Interface.pokemon_learned_move_get(Handle, index);
return ptr.IsNull ? null : Resolver.Instance.ResolveLearnedMove(ptr.Resolve());
}
private StatisticSet<uint>? _flatStats;
public StatisticSet<uint> FlatStats =>
Cache.FlatStats ??= new StatisticSet<uint>(Interface.pokemon_flat_stats(Ptr), false);
_flatStats ??=
Resolver.Instance.ResolveStatisticSet<uint>(Interface.pokemon_flat_stats(Handle).Resolve());
private StatisticSet<uint>? _boostedStats;
public StatisticSet<uint> BoostedStats =>
Cache.BoostedStats ??= new StatisticSet<uint>(Interface.pokemon_boosted_stats(Ptr), false);
_boostedStats ??=
Resolver.Instance.ResolveStatisticSet<uint>(Interface.pokemon_boosted_stats(Handle).Resolve());
public sbyte GetStatBoost(Statistic statistic) => Interface.pokemon_get_stat_boost(Ptr, statistic);
public byte GetIndividualValue(Statistic statistic) => Interface.pokemon_get_individual_value(Ptr, statistic);
public byte GetEffortValue(Statistic statistic) => Interface.pokemon_get_effort_value(Ptr, statistic);
public sbyte GetStatBoost(Statistic statistic) => Interface.pokemon_get_stat_boost(Handle, statistic);
public byte GetIndividualValue(Statistic statistic) => Interface.pokemon_get_individual_value(Handle, statistic);
public byte GetEffortValue(Statistic statistic) => Interface.pokemon_get_effort_value(Handle, statistic);
public void SetIndividualValue(Statistic statistic, byte value) =>
Interface.pokemon_set_individual_value(Ptr, statistic, value);
Interface.pokemon_set_individual_value(Handle, statistic, value).Result();
public void SetEffortValue(Statistic statistic, byte value) =>
Interface.pokemon_set_effort_value(Ptr, statistic, value);
Interface.pokemon_set_effort_value(Handle, statistic, value).Result();
// TODO: Battle getter
public byte BattleSideIndex => Interface.pokemon_get_battle_side_index(Ptr);
public byte BattleIndex => Interface.pokemon_get_battle_index(Ptr);
public bool IsAbilityOverriden => Interface.pokemon_is_ability_overriden(Ptr) == 1;
public Ability ActiveAbility => new(Interface.pokemon_active_ability(Ptr));
public bool AllowedExperienceGain => Interface.pokemon_allowed_experience_gain(Ptr) == 1;
public Nature Nature => new(Interface.pokemon_nature(Ptr));
public byte BattleSideIndex => Interface.pokemon_get_battle_side_index(Handle);
public byte BattleIndex => Interface.pokemon_get_battle_index(Handle);
public bool IsAbilityOverriden => Interface.pokemon_is_ability_overriden(Handle) == 1;
public void RecalculateFlatStats() => Interface.pokemon_recalculate_flat_stats(Ptr);
public void RecalculateBoostedStats() => Interface.pokemon_recalculate_boosted_stats(Ptr);
public Ability ActiveAbility =>
Resolver.Instance.ResolveAbility(Interface.pokemon_active_ability(Handle).Result().Resolve());
public bool AllowedExperienceGain => Interface.pokemon_allowed_experience_gain(Handle) == 1;
public Nature Nature => Resolver.Instance.ResolveNature(Interface.pokemon_nature(Handle).Resolve());
public void RecalculateFlatStats() => Interface.pokemon_recalculate_flat_stats(Handle).Result();
public void RecalculateBoostedStats() => Interface.pokemon_recalculate_boosted_stats(Handle).Result();
public void ChangeSpecies(Species species, Form form) =>
Interface.pokemon_change_species(Ptr, species.Ptr, form.Ptr);
Interface.pokemon_change_species(Handle, species.Handle, form.Handle).Result();
public void ChangeForm(Form form) => Interface.pokemon_change_form(Ptr, form.Ptr);
public void ChangeForm(Form form) => Interface.pokemon_change_form(Handle, form.Handle).Result();
public bool IsUsable => Interface.pokemon_is_usable(Ptr) == 1;
public bool IsFainted => Interface.pokemon_is_fainted(Ptr) == 1;
public bool IsOnBattleField => Interface.pokemon_is_on_battlefield(Ptr) == 1;
public bool IsUsable => Interface.pokemon_is_usable(Handle) == 1;
public bool IsFainted => Interface.pokemon_is_fainted(Handle) == 1;
public bool IsOnBattleField => Interface.pokemon_is_on_battlefield(Handle) == 1;
public void Damage(uint amount, DamageSource source) => Interface.pokemon_damage(Ptr, amount, source);
public void Damage(uint amount, DamageSource source) => Interface.pokemon_damage(Handle, amount, source).Result();
public bool Heal(uint amount, bool allowRevive) =>
Interface.pokemon_heal(Ptr, amount, allowRevive.ForeignBool()) == 1;
Interface.pokemon_heal(Handle, amount, allowRevive.ForeignBool()) == 1;
public void LearnMove(string moveName, MoveLearnMethod learnMethod) =>
Interface.pokemon_learn_move(Ptr, moveName.ToPtr(), learnMethod);
Interface.pokemon_learn_move(Handle, moveName.ToPtr(), learnMethod).Result();
public void ClearStatus() => Interface.pokemon_clear_status(Ptr);
public class CacheData
{
public DynamicLibrary? DynamicLibrary { get; set; }
public ExternValueArray<TypeIdentifier>? Types { get; set; }
public StatisticSet<uint>? FlatStats { get; set; }
public StatisticSet<uint>? BoostedStats { get; set; }
}
protected override CacheData CreateCache() => new();
protected override void Destructor() => Interface.pokemon_drop(Ptr);
public void ClearStatus() => Interface.pokemon_clear_status(Handle);
}
}

View File

@ -88,7 +88,7 @@ namespace PkmnLibSharp.DynamicData
protected virtual Pokemon Finalize(Species species, Form form, Item? heldItem)
{
var pokemon = new Pokemon(_library, species, form, _hiddenAbility, _abilityIndex, _level,
var pokemon = Pokemon.Create(_library, species, form, _hiddenAbility, _abilityIndex, _level,
_identifier ?? (uint)_random.Next(), _gender!.Value, _coloring, _natureName!);
if (heldItem != null)
pokemon.SetHeldItem(heldItem);
@ -126,7 +126,7 @@ namespace PkmnLibSharp.DynamicData
if (string.IsNullOrEmpty(_natureName))
{
using var nature = _library.StaticData.NatureLibrary.GetRandomNature((ulong)_randomSeed);
var nature = _library.StaticData.NatureLibrary.GetRandomNature((ulong)_randomSeed);
_natureName = _library.StaticData.NatureLibrary.GetNatureName(nature);
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.DynamicData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.DynamicData
{
@ -10,55 +11,49 @@ namespace PkmnLibSharp.FFI.DynamicData
/// Instantiate a new learned move.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer learned_move_new(IntPtr move, MoveLearnMethod learnMethod);
/// <summary>
/// Drops a learned move.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void learned_move_drop(IntPtr value);
internal static extern FFIHandleValue learned_move_new(FFIHandleValue move, MoveLearnMethod learnMethod);
/// <summary>
/// The immutable move information of the move.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer learned_move_move_data(IntPtr value);
internal static extern FFIHandleValue learned_move_move_data(FFIHandleValue value);
/// <summary>
/// The maximal power points for this move.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte learned_move_max_pp(IntPtr value);
internal static extern byte learned_move_max_pp(FFIHandleValue value);
/// <summary>
/// The amount of remaining power points. If this is 0, we can not use the move anymore.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte learned_move_remaining_pp(IntPtr value);
internal static extern byte learned_move_remaining_pp(FFIHandleValue value);
/// <summary>
/// The way the move was learned.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern MoveLearnMethod learned_move_learn_method(IntPtr value);
internal static extern MoveLearnMethod learned_move_learn_method(FFIHandleValue value);
/// <summary>
/// Try and reduce the PP by a certain amount. If the amount is higher than the current uses,
/// return 0. Otherwise, reduce the PP, and return 1.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte learned_move_try_use(IntPtr value, byte amount);
internal static extern byte learned_move_try_use(FFIHandleValue value, byte amount);
/// <summary>
/// Set the remaining PP to the max amount of PP.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void learned_move_restore_all_uses(IntPtr value);
internal static extern void learned_move_restore_all_uses(FFIHandleValue value);
/// <summary>
/// Restore the remaining PP by a certain amount. Will prevent it from going above max PP.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void learned_move_restore_uses(IntPtr value, byte amount);
internal static extern void learned_move_restore_uses(FFIHandleValue value, byte amount);
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.DynamicData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.DynamicData.Libraries
{
@ -10,13 +11,6 @@ namespace PkmnLibSharp.FFI.DynamicData.Libraries
/// Creates a new Gen 7 battle stat calculator
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer gen_7_battle_stat_calculator_new();
/// <summary>
/// Creates a new Gen 7 battle stat calculator
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void battle_stat_calculator_drop(IntPtr ptr);
internal static extern FFIHandleValue gen_7_battle_stat_calculator_new();
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.DynamicData.Libraries
{
@ -11,12 +12,6 @@ namespace PkmnLibSharp.FFI.DynamicData.Libraries
/// <param name="hasRandomness">whether or not a random damage modifier (0.85x - 1.00x) is applied to the
/// calculated damage. 0 for not, 1 for true</param>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer gen_7_damage_library_new(byte hasRandomness);
/// <summary>
/// Drops a DamageLibrary.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void damage_library_drop(IntPtr ptr);
internal static extern FFIHandleValue gen_7_damage_library_new(byte hasRandomness);
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.DynamicData.Libraries
{
@ -9,20 +10,14 @@ namespace PkmnLibSharp.FFI.DynamicData.Libraries
/// Instantiates a new DynamicLibrary with given parameters.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer dynamic_library_new(IntPtr staticData, IntPtr statCalculator,
IntPtr damageLibrary, IntPtr miscLibrary, IntPtr scriptResolver);
/// <summary>
/// Drops a dynamic library.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void dynamic_library_drop(IntPtr dynamicLibrary);
internal static extern FFIHandleValue dynamic_library_new(FFIHandleValue staticData, FFIHandleValue statCalculator,
FFIHandleValue damageLibrary, FFIHandleValue miscLibrary, FFIHandleValue scriptResolver);
/// <summary>
/// The static data is the immutable storage data for this library.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer dynamic_library_get_static_data(IntPtr dynamicLibrary);
internal static extern FFIHandleValue dynamic_library_get_static_data(FFIHandleValue dynamicLibrary);
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.DynamicData.Libraries
{
@ -9,12 +10,6 @@ namespace PkmnLibSharp.FFI.DynamicData.Libraries
/// Instantiates a new MiscLibrary.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer gen_7_misc_library_new();
/// <summary>
/// Drops a MiscLibrary.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void misc_library_drop(IntPtr ptr);
internal static extern FFIHandleValue gen_7_misc_library_new();
}
}

View File

@ -1,5 +1,7 @@
using System;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.DynamicData.Libraries
{
@ -9,13 +11,7 @@ namespace PkmnLibSharp.FFI.DynamicData.Libraries
/// Instantiates a basic empty script resolver, that always returns None.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer empty_script_resolver_new();
/// <summary>
/// Drops a script resolver.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void script_resolver_drop(IntPtr ptr);
internal static extern FFIHandleValue empty_script_resolver_new();
// WASM is a feature flag in the crate, and can be disabled. If disabled, these entry points do not exist. In
// this case it's recommended to remove the WASM compile flag from the csproj as well.
@ -24,20 +20,22 @@ namespace PkmnLibSharp.FFI.DynamicData.Libraries
/// Instantiates a new WebAssemblyScriptResolver.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer webassembly_script_resolver_new();
internal static extern FFIHandleValue webassembly_script_resolver_new();
/// <summary>
/// Load a compiled WASM module.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void webassembly_script_resolver_load_wasm_from_bytes(IntPtr resolver, IntPtr byteArray,
[MustUseReturnValue]
internal static extern NativeResult webassembly_script_resolver_load_wasm_from_bytes(FFIHandleValue resolver, IntPtr byteArray,
ulong arrayLength);
/// <summary>
/// Tells the script resolver we're done loading wasm modules, and to finalize the resolver.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void webassembly_script_resolver_finalize(IntPtr resolver);
[MustUseReturnValue]
internal static extern NativeResult webassembly_script_resolver_finalize(FFIHandleValue resolver);
#endif
}
}

View File

@ -1,8 +1,9 @@
using System;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using PkmnLibSharp.DynamicData;
using PkmnLibSharp.FFI.StaticData;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.DynamicData
{
@ -12,227 +13,223 @@ namespace PkmnLibSharp.FFI.DynamicData
/// Instantiates a new Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_new(IntPtr dynamicLibrary, IntPtr species, IntPtr form,
byte hiddenAbility, byte abilityIndex, LevelInt level, uint uniqueIdentifier, Gender gender, byte coloring,
IntPtr natureName);
/// <summary>
/// Drops an Arc reference held by the FFI.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_drop(IntPtr pokemon);
internal static extern NativeResult<FFIHandleValue> pokemon_new(FFIHandleValue dynamicLibrary,
FFIHandleValue species, FFIHandleValue form, byte hiddenAbility, byte abilityIndex, LevelInt level,
uint uniqueIdentifier, Gender gender, byte coloring, IntPtr natureName);
/// <summary>
/// The library data of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_library(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_library(FFIHandleValue pokemon);
/// <summary>
/// The species of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_species(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_species(FFIHandleValue pokemon);
/// <summary>
/// The form of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_form(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_form(FFIHandleValue pokemon);
/// <summary>
/// The species that should be displayed to the user. This handles stuff like the Illusion ability.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_display_species(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_display_species(FFIHandleValue pokemon);
/// <summary>
/// The form that should be displayed to the user. This handles stuff like the Illusion ability.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_display_form(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_display_form(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern LevelInt pokemon_level(IntPtr pokemon);
internal static extern LevelInt pokemon_level(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint pokemon_experience(IntPtr pokemon);
internal static extern uint pokemon_experience(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint pokemon_unique_identifier(IntPtr pokemon);
internal static extern uint pokemon_unique_identifier(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern Gender pokemon_gender(IntPtr pokemon);
internal static extern Gender pokemon_gender(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_coloring(IntPtr pokemon);
internal static extern byte pokemon_coloring(FFIHandleValue pokemon);
/// <summary>
/// Gets the held item of a Pokemon
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_held_item(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_held_item(FFIHandleValue pokemon);
/// <summary>
/// Checks whether the Pokemon is holding a specific item.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_has_held_item(IntPtr pokemon, IntPtr itemName);
internal static extern byte pokemon_has_held_item(FFIHandleValue pokemon, IntPtr itemName);
/// <summary>
/// Changes the held item of the Pokemon. Returns the previously held item.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_set_held_item(IntPtr pokemon, IntPtr item);
internal static extern IdentifiablePointer pokemon_set_held_item(FFIHandleValue pokemon, FFIHandle item);
/// <summary>
/// Removes the held item from the Pokemon. Returns the previously held item.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_remove_held_item(IntPtr pokemon);
internal static extern IdentifiablePointer pokemon_remove_held_item(FFIHandleValue pokemon);
/// <summary>
/// Makes the Pokemon uses its held item.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_consume_held_item(IntPtr pokemon);
internal static extern NativeResult<byte> pokemon_consume_held_item(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint pokemon_current_health(IntPtr pokemon);
internal static extern uint pokemon_current_health(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint pokemon_max_health(IntPtr pokemon);
internal static extern uint pokemon_max_health(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern float pokemon_weight(IntPtr pokemon);
internal static extern float pokemon_weight(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern float pokemon_height(IntPtr pokemon);
internal static extern float pokemon_height(FFIHandleValue pokemon);
/// <summary>
/// An optional nickname of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr pokemon_nickname(IntPtr pokemon);
internal static extern NativeResult<IntPtr> pokemon_nickname(FFIHandleValue pokemon);
/// <summary>
/// Whether the actual ability on the form is a hidden ability.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_real_ability_is_hidden(IntPtr pokemon);
internal static extern byte pokemon_real_ability_is_hidden(FFIHandleValue pokemon);
/// <summary>
/// The index of the actual ability on the form.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_real_ability_index(IntPtr pokemon);
internal static extern byte pokemon_real_ability_index(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern ulong pokemon_types_length(IntPtr pokemon);
internal static extern ulong pokemon_types_length(FFIHandleValue pokemon);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern TypeIdentifier pokemon_types_get(IntPtr pokemon, ulong index);
internal static extern NativeResult<TypeIdentifier> pokemon_types_get(FFIHandleValue pokemon, ulong index);
/// <summary>
/// Gets a learned move of the Pokemon. Index should generally be below [`MAX_MOVES`], you will get
/// a null pointer otherwise.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_learned_move_get(IntPtr pokemon, ulong index);
internal static extern FFIHandleValue pokemon_learned_move_get(FFIHandleValue pokemon, ulong index);
/// <summary>
/// The stats of the Pokemon when disregarding any stat boosts.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_flat_stats(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_flat_stats(FFIHandleValue pokemon);
/// <summary>
/// The stats of the Pokemon including the stat boosts.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_boosted_stats(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_boosted_stats(FFIHandleValue pokemon);
/// <summary>
/// Get the stat boosts for a specific stat.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern sbyte pokemon_get_stat_boost(IntPtr pokemon, Statistic statistic);
internal static extern sbyte pokemon_get_stat_boost(FFIHandleValue pokemon, Statistic statistic);
/// <summary>
/// Change a boosted stat by a certain amount.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern sbyte pokemon_change_stat_boost(IntPtr pokemon, Statistic statistic, sbyte diffAmount,
internal static extern NativeResult<sbyte> pokemon_change_stat_boost(FFIHandleValue pokemon, Statistic statistic, sbyte diffAmount,
byte selfInflicted);
/// <summary>
/// Gets a <a href="https://bulbapedia.bulbagarden.net/wiki/Individual_values">individual value</a> of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_get_individual_value(IntPtr pokemon, Statistic statistic);
internal static extern byte pokemon_get_individual_value(FFIHandleValue pokemon, Statistic statistic);
/// <summary>
/// Modifies a <a href="https://bulbapedia.bulbagarden.net/wiki/Individual_values">individual value</a> of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_set_individual_value(IntPtr pokemon, Statistic statistic, byte value);
[MustUseReturnValue]
internal static extern NativeResult pokemon_set_individual_value(FFIHandleValue pokemon, Statistic statistic, byte value);
/// <summary>
/// Gets a <a href="https://bulbapedia.bulbagarden.net/wiki/Effort_values">effort value</a> of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_get_effort_value(IntPtr pokemon, Statistic statistic);
internal static extern byte pokemon_get_effort_value(FFIHandleValue pokemon, Statistic statistic);
/// <summary>
/// Modifies a <a href="https://bulbapedia.bulbagarden.net/wiki/Effort_values">effort value</a> of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_set_effort_value(IntPtr pokemon, Statistic statistic, byte value);
[MustUseReturnValue]
internal static extern NativeResult pokemon_set_effort_value(FFIHandleValue pokemon, Statistic statistic, byte value);
/// <summary>
/// Gets the data for the battle the Pokemon is currently in. If the Pokemon is not in a battle, this
/// returns null.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_get_battle(IntPtr pokemon);
internal static extern IdentifiablePointer pokemon_get_battle(FFIHandleValue pokemon);
/// <summary>
/// Get the index of the side of the battle the Pokemon is in. If the Pokemon
/// is not on the battlefield, this always returns 0.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_get_battle_side_index(IntPtr pokemon);
internal static extern byte pokemon_get_battle_side_index(FFIHandleValue pokemon);
/// <summary>
/// Get the index of the slot on the side of the battle the Pokemon is in. If the Pokemon
/// is not on the battlefield, this always returns 0.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_get_battle_index(IntPtr pokemon);
internal static extern byte pokemon_get_battle_index(FFIHandleValue pokemon);
/// <summary>
/// Returns whether something overrides the ability.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_is_ability_overriden(IntPtr pokemon);
internal static extern byte pokemon_is_ability_overriden(FFIHandleValue pokemon);
/// <summary>
/// Returns the currently active ability.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_active_ability(IntPtr pokemon);
internal static extern NativeResult<FFIHandleValue> pokemon_active_ability(FFIHandleValue pokemon);
/// <summary>
/// Whether or not the Pokemon is allowed to gain experience.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_allowed_experience_gain(IntPtr pokemon);
internal static extern byte pokemon_allowed_experience_gain(FFIHandleValue pokemon);
/// <summary>
/// The <a href="https://bulbapedia.bulbagarden.net/wiki/Nature">nature</a> of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer pokemon_nature(IntPtr pokemon);
internal static extern FFIHandleValue pokemon_nature(FFIHandleValue pokemon);
/// <summary>
/// Calculates the flat stats on the Pokemon. This should be called when for example the base
@ -240,67 +237,73 @@ namespace PkmnLibSharp.FFI.DynamicData
/// stats, as those depend on the flat stats.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_recalculate_flat_stats(IntPtr pokemon);
[MustUseReturnValue]
internal static extern NativeResult pokemon_recalculate_flat_stats(FFIHandleValue pokemon);
/// <summary>
/// Calculates the boosted stats on the Pokemon. This should be called when a stat boost changes.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_recalculate_boosted_stats(IntPtr pokemon);
[MustUseReturnValue]
internal static extern NativeResult pokemon_recalculate_boosted_stats(FFIHandleValue pokemon);
/// <summary>
/// Change the species of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_change_species(IntPtr pokemon, IntPtr species, IntPtr form);
[MustUseReturnValue]
internal static extern NativeResult pokemon_change_species(FFIHandleValue pokemon, FFIHandleValue species, FFIHandleValue form);
/// <summary>
/// Change the form of the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_change_form(IntPtr pokemon, IntPtr form);
[MustUseReturnValue]
internal static extern NativeResult pokemon_change_form(FFIHandleValue pokemon, FFIHandleValue form);
/// <summary>
/// Whether or not the Pokemon is usable in a battle.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_is_usable(IntPtr pokemon);
internal static extern byte pokemon_is_usable(FFIHandleValue pokemon);
/// <summary>
/// Returns whether the Pokemon is fainted.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_is_fainted(IntPtr pokemon);
internal static extern byte pokemon_is_fainted(FFIHandleValue pokemon);
/// <summary>
/// Whether or not the Pokemon is on the battlefield.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_is_on_battlefield(IntPtr pokemon);
internal static extern byte pokemon_is_on_battlefield(FFIHandleValue pokemon);
/// <summary>
/// Damages the Pokemon by a certain amount of damage, from a damage source.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_damage(IntPtr pokemon, uint damage, DamageSource damageSource);
[MustUseReturnValue]
internal static extern NativeResult pokemon_damage(FFIHandleValue pokemon, uint damage, DamageSource damageSource);
/// <summary>
/// Heals the Pokemon by a specific amount. Unless allowRevive is set to 1, this will not
/// heal if the Pokemon has 0 health. If the amount healed is 0, this will return false.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte pokemon_heal(IntPtr pokemon, uint damage, byte allowRevive);
internal static extern byte pokemon_heal(FFIHandleValue pokemon, uint damage, byte allowRevive);
/// <summary>
/// Learn a move.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_learn_move(IntPtr pokemon, IntPtr moveName, MoveLearnMethod learnMethod);
[MustUseReturnValue]
internal static extern NativeResult pokemon_learn_move(FFIHandleValue pokemon, IntPtr moveName, MoveLearnMethod learnMethod);
/// <summary>
/// Removes the current non-volatile status from the Pokemon.
/// </summary>
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void pokemon_clear_status(IntPtr pokemon);
internal static extern void pokemon_clear_status(FFIHandleValue pokemon);
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI
{
internal struct NativeResult
{
private readonly IntPtr Error = IntPtr.Zero;
internal NativeResult(IntPtr error)
{
Error = error;
}
public void Result()
{
if (Error != IntPtr.Zero)
{
throw new PkmnLibException(Error.PtrString()!);
}
}
}
internal struct NativeResult<T> where T : new()
{
private readonly IntPtr Error = IntPtr.Zero;
private readonly T Data = new();
internal NativeResult(IntPtr error, T data)
{
Error = error;
Data = data;
}
public T Result()
{
if (Error != IntPtr.Zero)
{
throw new PkmnLibException(Error.PtrString()!);
}
return Data;
}
}
public class PkmnLibException : Exception
{
internal PkmnLibException(string message) : base(message){}
}
}

View File

@ -1,26 +1,24 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class Ability
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer ability_new(IntPtr name, IntPtr effect, IntPtr parameters, ulong size);
internal static extern NativeResult<FFIHandleValue> ability_new(IntPtr name, IntPtr effect, IntPtr parameters, ulong size);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void ability_drop(IntPtr value);
internal static extern NativeResult<IntPtr> ability_name(FFIHandleValue handle);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern string ability_name(IntPtr value);
internal static extern NativeResult<IntPtr> ability_effect(FFIHandleValue handle);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern string ability_effect(IntPtr value);
internal static extern ulong ability_parameter_length(FFIHandleValue handle);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern ulong ability_parameter_length(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer ability_parameter_get(IntPtr value, ulong index);
internal static extern FFIHandleValue ability_parameter_get(FFIHandleValue handle, ulong index);
}
}

View File

@ -1,39 +1,37 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class EffectParameter
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer effect_parameter_new_bool(byte value);
internal static extern FFIHandleValue effect_parameter_new_bool(byte value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer effect_parameter_new_int(long value);
internal static extern FFIHandleValue effect_parameter_new_int(long value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer effect_parameter_new_float(float value);
internal static extern FFIHandleValue effect_parameter_new_float(float value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer effect_parameter_new_string(IntPtr value);
internal static extern NativeResult<FFIHandleValue> effect_parameter_new_string(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void effect_parameter_drop(IntPtr value);
internal static extern byte effect_parameter_get_type(FFIHandleValue value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte effect_parameter_get_type(IntPtr value);
internal static extern NativeResult<byte> effect_parameter_get_as_bool(FFIHandleValue value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte effect_parameter_get_as_bool(IntPtr value);
internal static extern NativeResult<long> effect_parameter_get_as_int(FFIHandleValue value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern long effect_parameter_get_as_int(IntPtr value);
internal static extern NativeResult<float> effect_parameter_get_as_float(FFIHandleValue value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern float effect_parameter_get_as_float(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr effect_parameter_get_as_string(IntPtr value);
internal static extern NativeResult<IntPtr> effect_parameter_get_as_string(FFIHandleValue value);
}
}

View File

@ -1,57 +1,55 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class Form
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer form_new(IntPtr name, float height, float weight,
uint baseExperience, IntPtr types, ulong typesLength, IntPtr baseStats, IntPtr abilities,
ulong abilitiesLength, IntPtr hiddenAbilities, ulong hiddenAbilitiesLength, IntPtr learnableMoves,
internal static extern NativeResult<FFIHandleValue> form_new(IntPtr name, float height, float weight,
uint baseExperience, IntPtr types, ulong typesLength, FFIHandleValue baseStats, IntPtr abilities,
ulong abilitiesLength, IntPtr hiddenAbilities, ulong hiddenAbilitiesLength, FFIHandleValue learnableMoves,
IntPtr flags, ulong flagsLength);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void form_drop(IntPtr ptr);
internal static extern NativeResult<IntPtr> form_name(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr form_name(IntPtr ptr);
internal static extern float form_height(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern float form_height(IntPtr ptr);
internal static extern float form_weight(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern float form_weight(IntPtr ptr);
internal static extern uint form_base_experience(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern uint form_base_experience(IntPtr ptr);
internal static extern ulong form_types_length(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern ulong form_types_length(IntPtr ptr);
internal static extern TypeIdentifier form_types_get(FFIHandleValue ptr, ulong index);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern TypeIdentifier form_types_get(IntPtr ptr, ulong index);
internal static extern FFIHandleValue form_base_stats(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer form_base_stats(IntPtr ptr);
internal static extern ulong form_abilities_length(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern ulong form_abilities_length(IntPtr ptr);
internal static extern IntPtr form_abilities_get(FFIHandleValue ptr, ulong index);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr form_abilities_get(IntPtr ptr, ulong index);
internal static extern ulong form_hidden_abilities_length(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern ulong form_hidden_abilities_length(IntPtr ptr);
internal static extern IntPtr form_hidden_abilities_get(FFIHandleValue ptr, ulong index);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr form_hidden_abilities_get(IntPtr ptr, ulong index);
internal static extern FFIHandleValue form_moves(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr form_moves(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte form_has_flag(IntPtr ptr, IntPtr flag);
internal static extern byte form_has_flag(FFIHandleValue ptr, IntPtr flag);
}
}

View File

@ -1,21 +1,19 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class GrowthRate
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr growth_rate_lookup_new(IntPtr array, ulong length);
internal static extern FFIHandleValue growth_rate_lookup_new(IntPtr array, ulong length);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void growth_rate_lookup_drop(IntPtr p);
internal static extern LevelInt growth_rate_calculate_level(FFIHandleValue p, uint experience);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern LevelInt growth_rate_calculate_level(IntPtr p, uint experience);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint growth_rate_calculate_experience(IntPtr p, LevelInt level);
internal static extern NativeResult<uint> growth_rate_calculate_experience(FFIHandleValue p, LevelInt level);
}
}

View File

@ -1,32 +1,30 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class Item
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer item_new(IntPtr name, ItemCategory category, BattleItemCategory battleCategory,
internal static extern NativeResult<FFIHandleValue> item_new(IntPtr name, ItemCategory category, BattleItemCategory battleCategory,
int price, IntPtr flags, ulong flagsLength);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void item_drop(IntPtr ptr);
internal static extern NativeResult<IntPtr> item_name(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr item_name(IntPtr ptr);
internal static extern ItemCategory item_category(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern ItemCategory item_category(IntPtr ptr);
internal static extern BattleItemCategory item_battle_category(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern BattleItemCategory item_battle_category(IntPtr ptr);
internal static extern int item_price(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int item_price(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte item_has_flag(IntPtr p, IntPtr flag);
internal static extern byte item_has_flag(FFIHandleValue p, IntPtr flag);
}
}

View File

@ -1,18 +1,16 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class LearnableMoves
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr learnable_moves_new();
internal static extern FFIHandleValue learnable_moves_new();
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void learnable_moves_drop(IntPtr p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void learnable_moves_add_level_move(IntPtr p, LevelInt level, IntPtr moveName);
internal static extern void learnable_moves_add_level_move(FFIHandleValue p, LevelInt level, IntPtr moveName);
}
}

View File

@ -1,26 +1,25 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class AbilityLibrary
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer ability_library_new(ulong capacity);
internal static extern FFIHandleValue ability_library_new(ulong capacity);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void ability_library_drop(IntPtr ptr);
internal static extern void ability_library_add(FFIHandleValue ptr, IntPtr key, FFIHandleValue value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer ability_library_get(IntPtr ptr, IntPtr key);
internal static extern FFIHandleValue ability_library_get(FFIHandleValue ptr, IntPtr key);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr ability_library_get_key_by_index(IntPtr ptr, ulong index);
internal static extern IntPtr ability_library_get_key_by_index(FFIHandleValue ptr, ulong index);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void ability_library_add(IntPtr ptr, IntPtr key, IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern ulong ability_library_len(IntPtr ptr);
internal static extern ulong ability_library_len(FFIHandleValue ptr);
}
}

View File

@ -1,24 +1,22 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class GrowthRateLibrary
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer growth_rate_library_new(ulong capacity);
internal static extern FFIHandleValue growth_rate_library_new(ulong capacity);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void growth_rate_library_drop(IntPtr ptr);
internal static extern NativeResult<LevelInt> growth_rate_library_calculate_level(FFIHandleValue ptr, IntPtr growthRate, uint experience);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern LevelInt growth_rate_library_calculate_level(IntPtr ptr, IntPtr growthRate, uint experience);
internal static extern NativeResult<uint> growth_rate_library_calculate_experience(FFIHandleValue ptr, IntPtr growthRate, LevelInt level);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint growth_rate_library_calculate_experience(IntPtr ptr, IntPtr growthRate, LevelInt level);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint growth_rate_library_add_growth_rate(IntPtr ptr, IntPtr name, IntPtr growthRate);
internal static extern void growth_rate_library_add_growth_rate(FFIHandleValue ptr, IntPtr name, FFIHandleValue growthRate);
}
}

View File

@ -1,26 +1,24 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class ItemLibrary
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer item_library_new(ulong capacity);
internal static extern FFIHandleValue item_library_new(ulong capacity);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void item_library_drop(IntPtr ptr);
internal static extern FFIHandleValue item_library_get(FFIHandleValue ptr, IntPtr key);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer item_library_get(IntPtr ptr, IntPtr key);
internal static extern IntPtr item_library_get_key_by_index(FFIHandleValue ptr, ulong index);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr item_library_get_key_by_index(IntPtr ptr, ulong index);
internal static extern void item_library_add(FFIHandleValue ptr, IntPtr key, FFIHandleValue value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void item_library_add(IntPtr ptr, IntPtr key, IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern ulong item_library_len(IntPtr ptr);
internal static extern ulong item_library_len(FFIHandleValue ptr);
}
}

View File

@ -1,22 +1,23 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class LibrarySettings
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer library_settings_new(LevelInt maxLevel, uint shinyRate);
internal static extern NativeResult<FFIHandleValue> library_settings_new(LevelInt maxLevel, uint shinyRate);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void library_settings_drop(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern LevelInt library_settings_maximum_level(IntPtr ptr);
internal static extern LevelInt library_settings_maximum_level(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint library_settings_shiny_rate(IntPtr ptr);
internal static extern uint library_settings_shiny_rate(FFIHandleValue ptr);
}
}

View File

@ -1,26 +1,24 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class MoveLibrary
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer move_library_new(ulong capacity);
internal static extern FFIHandleValue move_library_new(ulong capacity);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void move_library_drop(IntPtr ptr);
internal static extern IdentifiablePointer move_library_get(FFIHandleValue ptr, IntPtr key);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer move_library_get(IntPtr ptr, IntPtr key);
internal static extern IntPtr move_library_get_key_by_index(FFIHandleValue ptr, ulong index);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr move_library_get_key_by_index(IntPtr ptr, ulong index);
internal static extern void move_library_add(FFIHandleValue ptr, IntPtr key, FFIHandleValue value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void move_library_add(IntPtr ptr, IntPtr key, IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern ulong move_library_len(IntPtr ptr);
internal static extern ulong move_library_len(FFIHandleValue ptr);
}
}

View File

@ -1,26 +1,24 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class NatureLibrary
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer nature_library_new(ulong capacity);
internal static extern FFIHandleValue nature_library_new(ulong capacity);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void nature_library_drop(IntPtr ptr);
internal static extern void nature_library_load_nature(FFIHandleValue ptr, IntPtr name, FFIHandleValue nature);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void nature_library_load_nature(IntPtr ptr, IntPtr name, IntPtr nature);
internal static extern FFIHandleValue nature_library_get_nature(FFIHandleValue ptr, IntPtr name);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer nature_library_get_nature(IntPtr ptr, IntPtr name);
internal static extern NativeResult<FFIHandleValue> nature_library_get_random_nature(FFIHandleValue ptr, ulong seed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer nature_library_get_random_nature(IntPtr ptr, ulong seed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr nature_library_get_nature_name(IntPtr ptr, IntPtr nature);
internal static extern IntPtr nature_library_get_nature_name(FFIHandleValue ptr, FFIHandleValue nature);
}
}

View File

@ -1,26 +1,24 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class SpeciesLibrary
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer species_library_new(ulong capacity);
internal static extern FFIHandleValue species_library_new(ulong capacity);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void species_library_drop(IntPtr ptr);
internal static extern FFIHandleValue species_library_get(FFIHandleValue ptr, IntPtr key);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer species_library_get(IntPtr ptr, IntPtr key);
internal static extern IntPtr species_library_get_key_by_index(FFIHandleValue ptr, ulong index);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr species_library_get_key_by_index(IntPtr ptr, ulong index);
internal static extern void species_library_add(FFIHandleValue ptr, IntPtr key, FFIHandleValue value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void species_library_add(IntPtr ptr, IntPtr key, IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern ulong species_library_len(IntPtr ptr);
internal static extern ulong species_library_len(FFIHandleValue ptr);
}
}

View File

@ -1,40 +1,41 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class StaticData
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_new(IntPtr settings, IntPtr speciesLibrary,
IntPtr moveLibrary, IntPtr itemLibrary, IntPtr growthRateLibrary, IntPtr typeLibrary, IntPtr natureLibrary,
IntPtr abilityLibrary);
internal static extern FFIHandleValue static_data_new(FFIHandleValue settings, FFIHandleValue speciesLibrary,
FFIHandleValue moveLibrary, FFIHandleValue itemLibrary, FFIHandleValue growthRateLibrary, FFIHandleValue typeLibrary,
FFIHandleValue natureLibrary, FFIHandleValue abilityLibrary);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void static_data_drop(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_settings(IntPtr ptr);
internal static extern FFIHandleValue static_data_settings(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_species(IntPtr ptr);
internal static extern FFIHandleValue static_data_species(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_moves(IntPtr ptr);
internal static extern FFIHandleValue static_data_moves(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_items(IntPtr ptr);
internal static extern FFIHandleValue static_data_items(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_growth_rates(IntPtr ptr);
internal static extern FFIHandleValue static_data_growth_rates(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_types(IntPtr ptr);
internal static extern FFIHandleValue static_data_types(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_natures(IntPtr ptr);
internal static extern FFIHandleValue static_data_natures(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_data_abilities(IntPtr ptr);
internal static extern FFIHandleValue static_data_abilities(FFIHandleValue ptr);
}
}

View File

@ -1,37 +1,35 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData.Libraries
{
internal static class TypeLibrary
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer type_library_new(ulong capacity);
internal static extern FFIHandleValue type_library_new(ulong capacity);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void type_library_drop(IntPtr ptr);
internal static extern TypeIdentifier type_library_get_type_id(FFIHandleValue ptr, IntPtr name, IntPtr success);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern TypeIdentifier type_library_get_type_id(IntPtr ptr, IntPtr name, IntPtr success);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IntPtr type_library_get_type_name(IntPtr ptr, TypeIdentifier typeIdentifier,
internal static extern IntPtr type_library_get_type_name(FFIHandleValue ptr, TypeIdentifier typeIdentifier,
IntPtr success);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern float type_library_get_single_effectiveness(IntPtr ptr, TypeIdentifier attacking,
internal static extern float type_library_get_single_effectiveness(FFIHandleValue ptr, TypeIdentifier attacking,
TypeIdentifier defending);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern float type_library_get_effectiveness(IntPtr ptr, TypeIdentifier attacking,
internal static extern float type_library_get_effectiveness(FFIHandleValue ptr, TypeIdentifier attacking,
IntPtr defending, ulong defendingLength);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern TypeIdentifier type_library_register_type(IntPtr ptr, IntPtr name);
internal static extern TypeIdentifier type_library_register_type(FFIHandleValue ptr, IntPtr name);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void type_library_set_effectiveness(IntPtr ptr, TypeIdentifier attacking,
internal static extern void type_library_set_effectiveness(FFIHandleValue ptr, TypeIdentifier attacking,
TypeIdentifier defending, float effectiveness);
}
}

View File

@ -1,67 +1,62 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class MoveData
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer move_data_new(IntPtr name, TypeIdentifier moveType, MoveCategory category,
byte basePower, byte accuracy, byte baseUsages, MoveTarget target, sbyte priority, IntPtr secondaryEffect,
internal static extern NativeResult<FFIHandleValue> move_data_new(IntPtr name, TypeIdentifier moveType, MoveCategory category,
byte basePower, byte accuracy, byte baseUsages, MoveTarget target, sbyte priority, FFIHandleValue secondaryEffect,
IntPtr flags, ulong flagsLength);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void move_data_drop(IntPtr p);
internal static extern NativeResult<IntPtr> move_data_name(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr move_data_name(IntPtr p);
internal static extern TypeIdentifier move_data_move_type(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern TypeIdentifier move_data_move_type(IntPtr p);
internal static extern MoveCategory move_data_category(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern MoveCategory move_data_category(IntPtr p);
internal static extern byte move_data_base_power(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte move_data_base_power(IntPtr p);
internal static extern byte move_data_accuracy(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte move_data_accuracy(IntPtr p);
internal static extern byte move_data_base_usages(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte move_data_base_usages(IntPtr p);
internal static extern MoveTarget move_data_target(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern MoveTarget move_data_target(IntPtr p);
internal static extern sbyte move_data_priority(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern sbyte move_data_priority(IntPtr p);
internal static extern FFIHandleValue move_data_secondary_effect(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer move_data_secondary_effect(IntPtr p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte move_data_has_flag(IntPtr p, IntPtr flag);
internal static extern byte move_data_has_flag(FFIHandleValue p, IntPtr flag);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer secondary_effect_new(float chance, IntPtr effectName, IntPtr parameters,
internal static extern FFIHandleValue secondary_effect_new(float chance, IntPtr effectName, IntPtr parameters,
ulong parametersLength);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void secondary_effect_drop(IntPtr p);
internal static extern float secondary_effect_chance(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern float secondary_effect_chance(IntPtr p);
internal static extern NativeResult<IntPtr> secondary_effect_effect_name(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr secondary_effect_effect_name(IntPtr p);
internal static extern ulong secondary_effect_parameter_length(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern ulong secondary_effect_parameter_length(IntPtr p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer secondary_effect_parameter_get(IntPtr p, ulong index);
internal static extern FFIHandleValue secondary_effect_parameter_get(FFIHandleValue p, ulong index);
}
}

View File

@ -1,25 +1,23 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class Nature
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer nature_new(Statistic increaseStat, Statistic decreaseStat, float increaseModifier,
internal static extern FFIHandleValue nature_new(Statistic increaseStat, Statistic decreaseStat, float increaseModifier,
float decreaseModifier);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void nature_drop(IntPtr p);
internal static extern Statistic nature_increased_stat(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern Statistic nature_increased_stat(IntPtr p);
internal static extern Statistic nature_decreased_stat(FFIHandleValue p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern Statistic nature_decreased_stat(IntPtr p);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern float nature_get_stat_modifier(IntPtr p, Statistic statistic);
internal static extern float nature_get_stat_modifier(FFIHandleValue p, Statistic statistic);
}
}

View File

@ -1,41 +1,41 @@
using System;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class Species
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer species_new(ushort id, IntPtr name, float genderRate, IntPtr growthRate,
byte captureRate, IntPtr defaultForm, IntPtr flags, ulong flagsLength);
internal static extern NativeResult<FFIHandleValue> species_new(ushort id, IntPtr name, float genderRate, IntPtr growthRate,
byte captureRate, FFIHandleValue defaultForm, IntPtr flags, ulong flagsLength);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void species_drop(IntPtr ptr);
internal static extern ushort species_id(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern ushort species_id(IntPtr ptr);
internal static extern IntPtr species_name(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr species_name(IntPtr ptr);
internal static extern float species_gender_rate(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern float species_gender_rate(IntPtr ptr);
internal static extern IntPtr species_growth_rate(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr species_growth_rate(IntPtr ptr);
internal static extern byte species_capture_rate(FFIHandleValue ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte species_capture_rate(IntPtr ptr);
[MustUseReturnValue]
internal static extern NativeResult species_add_form(FFIHandleValue ptr, IntPtr name, FFIHandleValue form);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr species_add_form(IntPtr ptr, IntPtr name, IntPtr form);
internal static extern FFIHandleValue species_get_form(FFIHandleValue ptr, IntPtr name);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IdentifiablePointer species_get_form(IntPtr ptr, IntPtr name);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern Gender species_get_random_gender(IntPtr ptr, ulong seed);
internal static extern Gender species_get_random_gender(FFIHandleValue ptr, ulong seed);
}
}

View File

@ -1,69 +1,17 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class StaticStatisticSet
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_statistic_set_u8_new(byte hp, byte attack, byte defense,
byte specialAttack, byte specialDefense, byte speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_statistic_set_u16_new(ushort hp, ushort attack, ushort defense,
internal static extern FFIHandleValue static_statistic_set_u16_new(ushort hp, ushort attack, ushort defense,
ushort specialAttack, ushort specialDefense, ushort speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_statistic_set_u32_new(uint hp, uint attack, uint defense,
uint specialAttack, uint specialDefense, uint speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_statistic_set_i8_new(sbyte hp, sbyte attack, sbyte defense,
sbyte specialAttack, sbyte specialDefense, sbyte speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_statistic_set_i16_new(short hp, short attack, short defense,
short specialAttack, short specialDefense, short speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer static_statistic_set_i32_new(int hp, int attack, int defense, int specialAttack,
int specialDefense, int speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void static_statistic_set_u8_drop(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void static_statistic_set_u16_drop(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void static_statistic_set_u32_drop(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void static_statistic_set_i8_drop(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void static_statistic_set_i16_drop(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void static_statistic_set_i32_drop(IntPtr value);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte static_statistic_set_u8_get_stat(IntPtr value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern ushort static_statistic_set_u16_get_stat(IntPtr value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint static_statistic_set_u32_get_stat(IntPtr value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern sbyte static_statistic_set_i8_get_stat(IntPtr value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern short static_statistic_set_i16_get_stat(IntPtr value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern int static_statistic_set_i32_get_stat(IntPtr value, Statistic statistic);
internal static extern ushort static_statistic_set_u16_get_stat(FFIHandleValue value, Statistic statistic);
}
}

View File

@ -1,125 +1,58 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class StatisticSet
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer statistic_set_u8_new(byte hp, byte attack, byte defense, byte specialAttack,
internal static extern FFIHandleValue statistic_set_u8_new(byte hp, byte attack, byte defense, byte specialAttack,
byte specialDefense, byte speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer statistic_set_u16_new(ushort hp, ushort attack, ushort defense,
ushort specialAttack, ushort specialDefense, ushort speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer statistic_set_u32_new(uint hp, uint attack, uint defense, uint specialAttack,
internal static extern FFIHandleValue statistic_set_u32_new(uint hp, uint attack, uint defense, uint specialAttack,
uint specialDefense, uint speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer statistic_set_i8_new(sbyte hp, sbyte attack, sbyte defense, sbyte specialAttack,
internal static extern FFIHandleValue statistic_set_i8_new(sbyte hp, sbyte attack, sbyte defense, sbyte specialAttack,
sbyte specialDefense, sbyte speed);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer statistic_set_i16_new(short hp, short attack, short defense, short specialAttack,
short specialDefense, short speed);
internal static extern byte statistic_set_u8_get_stat(FFIHandleValue value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern IdentifiablePointer statistic_set_i32_new(int hp, int attack, int defense, int specialAttack,
int specialDefense, int speed);
internal static extern uint statistic_set_u32_get_stat(FFIHandleValue value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u8_drop(IntPtr value);
internal static extern sbyte statistic_set_i8_get_stat(FFIHandleValue value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u16_drop(IntPtr value);
internal static extern void statistic_set_u8_set_stat(FFIHandleValue value, Statistic statistic, byte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u32_drop(IntPtr value);
internal static extern void statistic_set_u32_set_stat(FFIHandleValue value, Statistic statistic, uint v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i8_drop(IntPtr value);
internal static extern void statistic_set_i8_set_stat(FFIHandleValue value, Statistic statistic, sbyte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i16_drop(IntPtr value);
internal static extern void statistic_set_u8_increase_stat(FFIHandleValue value, Statistic statistic, byte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i32_drop(IntPtr value);
internal static extern void statistic_set_u32_increase_stat(FFIHandleValue value, Statistic statistic, uint v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern byte statistic_set_u8_get_stat(IntPtr value, Statistic statistic);
internal static extern void statistic_set_i8_increase_stat(FFIHandleValue value, Statistic statistic, sbyte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern ushort statistic_set_u16_get_stat(IntPtr value, Statistic statistic);
internal static extern void statistic_set_u8_decrease_stat(FFIHandleValue value, Statistic statistic, byte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern uint statistic_set_u32_get_stat(IntPtr value, Statistic statistic);
internal static extern void statistic_set_u32_decrease_stat(FFIHandleValue value, Statistic statistic, uint v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern sbyte statistic_set_i8_get_stat(IntPtr value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern short statistic_set_i16_get_stat(IntPtr value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern int statistic_set_i32_get_stat(IntPtr value, Statistic statistic);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u8_set_stat(IntPtr value, Statistic statistic, byte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u16_set_stat(IntPtr value, Statistic statistic, ushort v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u32_set_stat(IntPtr value, Statistic statistic, uint v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i8_set_stat(IntPtr value, Statistic statistic, sbyte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i16_set_stat(IntPtr value, Statistic statistic, short v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i32_set_stat(IntPtr value, Statistic statistic, int v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u8_increase_stat(IntPtr value, Statistic statistic, byte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u16_increase_stat(IntPtr value, Statistic statistic, ushort v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u32_increase_stat(IntPtr value, Statistic statistic, uint v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i8_increase_stat(IntPtr value, Statistic statistic, sbyte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i16_increase_stat(IntPtr value, Statistic statistic, short v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i32_increase_stat(IntPtr value, Statistic statistic, int v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u8_decrease_stat(IntPtr value, Statistic statistic, byte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u16_decrease_stat(IntPtr value, Statistic statistic, ushort v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_u32_decrease_stat(IntPtr value, Statistic statistic, uint v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i8_decrease_stat(IntPtr value, Statistic statistic, sbyte v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i16_decrease_stat(IntPtr value, Statistic statistic, short v);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void statistic_set_i32_decrease_stat(IntPtr value, Statistic statistic, int v);
internal static extern void statistic_set_i8_decrease_stat(FFIHandleValue value, Statistic statistic, sbyte v);
}
}

View File

@ -1,71 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Ability;
namespace PkmnLibSharp.StaticData
{
public class Ability : ExternPointer<Ability.CacheData>
public class Ability : HandleType
{
[UsedImplicitly]
public class CacheData
private string? _name;
public string Name => _name ??= Interface.ability_name(Handle).Result().PtrString()!;
private string? _effect;
public string Effect => _effect ??= Interface.ability_effect(Handle).Result().PtrString()!;
public IReadOnlyList<EffectParameter> Parameters { get; private set; } = null!;
protected Ability(FFIHandle handle) : base(handle)
{
public string? Name { get; internal set; }
public string? Effect { get; internal set; }
public DisposableCachedExternArray<EffectParameter>? Parameters { get; internal set; }
}
public Ability(string name, string effect, IReadOnlyCollection<EffectParameter> parameters)
public static Ability Create(string name, string effect, IReadOnlyList<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 parameterArray = parameters.Select(x => (FFIHandleValue)x.Handle).ToArray();
var arrayPtr = parameterArray.ArrayPtr();
InitializePointer(Interface.ability_new(name.ToPtr(), effect.ToPtr(), arrayPtr, (ulong)parameters.Count),
true);
}
internal Ability(IdentifiablePointer ptr) : base(ptr, true)
{
}
public string Name => Cache.Name ?? (Cache.Name = Interface.ability_name(Ptr));
public string Effect => Cache.Effect ?? (Cache.Effect = Interface.ability_effect(Ptr));
public IReadOnlyList<EffectParameter> Parameters =>
Cache.Parameters ??= new DisposableCachedExternArray<EffectParameter>(
Interface.ability_parameter_length(Ptr),
#pragma warning disable IDISP012
arg => new EffectParameter(Interface.ability_parameter_get(Ptr, arg), false));
#pragma warning restore IDISP012
protected override CacheData CreateCache()
{
return new CacheData();
}
protected override void Destructor()
{
Interface.ability_drop(Ptr);
}
public override void InvalidateChildren()
{
if (Cache.Parameters == null) return;
for (var index = 0; index < Cache.Parameters.Count; index++)
{
Cache.Parameters.GetCachedValue(index)?.Invalidate();
}
Cache.Parameters.Dispose();
}
~Ability()
{
Dispose();
var handle = Interface.ability_new(name.ToPtr(), effect.ToPtr(), arrayPtr, (ulong)parameterArray.LongLength)
.Result();
var ability = Resolver.Instance.ResolveAbility(handle.Resolve());
ability.Parameters = parameters;
return ability;
}
}
}

View File

@ -1,88 +1,59 @@
using System;
using JetBrains.Annotations;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.EffectParameter;
namespace PkmnLibSharp.StaticData
{
public class EffectParameter : ExternPointer<EffectParameter.CacheData>
public class EffectParameter : HandleType
{
[UsedImplicitly]
public class CacheData
private ParameterType? _type;
public ParameterType? Type => _type ??= (ParameterType)Interface.effect_parameter_get_type(Handle);
private object? _data;
public object Data
{
public ParameterType? Type { get; internal set; }
public object? Data { get; internal set; }
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!;
}
}
internal EffectParameter(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
protected EffectParameter(FFIHandle handle) : base(handle)
{
}
public EffectParameter(bool b) : base(Interface.effect_parameter_new_bool(b.ForeignBool()), true)
{
}
public static EffectParameter FromBool(bool b) =>
Resolver.Instance.ResolveEffectParameter(Interface.effect_parameter_new_bool(b.ForeignBool()).Resolve());
public EffectParameter(long l) : base(Interface.effect_parameter_new_int(l), true)
{
}
public static EffectParameter FromLong(long l) =>
Resolver.Instance.ResolveEffectParameter(Interface.effect_parameter_new_int(l).Resolve());
public EffectParameter(float f) : base(Interface.effect_parameter_new_float(f), true)
{
}
public static EffectParameter FromFloat(float f) =>
Resolver.Instance.ResolveEffectParameter(Interface.effect_parameter_new_float(f).Resolve());
public EffectParameter(string s) : base(Interface.effect_parameter_new_string(s.ToPtr()), true)
{
}
public static EffectParameter FromString(string s) =>
Resolver.Instance.ResolveEffectParameter(Interface.effect_parameter_new_string(s.ToPtr()).Result().Resolve());
public static implicit operator EffectParameter(bool b) => FromBool(b);
public static implicit operator EffectParameter(long l) => FromLong(l);
public static implicit operator EffectParameter(float f) => FromFloat(f);
public static implicit operator EffectParameter(string s) => FromString(s);
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);
String = 3,
}
public override string ToString()
@ -90,10 +61,5 @@ namespace PkmnLibSharp.StaticData
var data = Data;
return data is string ? $"{Type}(\"{data}\")" : $"{Type}({data})";
}
~EffectParameter()
{
Dispose();
}
}
}

View File

@ -1,34 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Form;
namespace PkmnLibSharp.StaticData
{
public class Form : ExternPointer<Form.CacheData>
public class Form : HandleType
{
public class CacheData
{
public string? Name { get; internal set; }
public float? Height { get; internal set; }
public float? Weight { get; internal set; }
public uint? BaseExperience { get; internal set; }
public ulong? TypesLength { get; internal set; }
public CachedExternValueArray<TypeIdentifier>? Types { get; internal set; }
public StaticStatisticSet<ushort>? BaseStats { get; internal set; }
public CachedExternArray<string>? Abilities { get; internal set; }
public CachedExternArray<string>? HiddenAbilities { get; internal set; }
public LearnableMoves? LearnableMoves { get; internal set; }
}
internal Form(IdentifiablePointer formPtr) : base(formPtr, true)
protected Form(FFIHandle handle) : base(handle)
{
}
public Form(string name, float height, float weight, uint baseExperience, TypeIdentifier[] types,
StaticStatisticSet<short> baseStats, IReadOnlyCollection<string> abilities,
public static Form Create(string name, float height, float weight, uint baseExperience, TypeIdentifier[] types,
StaticStatisticSet<ushort> baseStats, IReadOnlyCollection<string> abilities,
IReadOnlyCollection<string> hiddenAbilities, LearnableMoves learnableMoves,
IReadOnlyCollection<string> flags)
{
@ -38,50 +22,45 @@ namespace PkmnLibSharp.StaticData
var hiddenAbilitiesPtrArray = hiddenAbilities.Select(x => x.ToPtr()).ToArray();
var flagsPtrArray = flags.Select(x => x.ToPtr()).ToArray();
var ptr = Interface.form_new(name.ToPtr(), height, weight, baseExperience, typesArr, (ulong)types.Length,
baseStats.TakeOwnershipAndInvalidate(), abilitiesPtrArray.ArrayPtr(), (ulong)abilities.Count,
var handle = Interface.form_new(name.ToPtr(), height, weight, baseExperience, typesArr, (ulong)types.Length,
baseStats.Handle, abilitiesPtrArray.ArrayPtr(), (ulong)abilities.Count,
hiddenAbilitiesPtrArray.ArrayPtr(), (ulong)hiddenAbilities.Count,
learnableMoves.TakeOwnershipAndInvalidate(), flagsPtrArray.ArrayPtr(), (ulong)flags.Count);
InitializePointer(ptr, true);
learnableMoves.Handle, flagsPtrArray.ArrayPtr(), (ulong)flags.Count);
var form = Resolver.Instance.ResolveForm(handle.Result().Resolve());
return form;
}
public string Name => Cache.Name ??= Interface.form_name(Ptr).PtrString()!;
public float Height => Cache.Height ??= Interface.form_height(Ptr);
public float Weight => Cache.Weight ??= Interface.form_weight(Ptr);
public uint BaseExperience => Cache.BaseExperience ??= Interface.form_base_experience(Ptr);
private string? _name;
public string Name => _name ??= Interface.form_name(Handle).Result().PtrString()!;
private float? _height;
public float Height => _height ??= Interface.form_height(Handle);
private float? _weight;
public float Weight => _weight ??= Interface.form_weight(Handle);
private uint? _baseExperience;
public uint BaseExperience => _baseExperience ??= Interface.form_base_experience(Handle);
private IReadOnlyList<TypeIdentifier>? _types;
public IReadOnlyList<TypeIdentifier> Types =>
Cache.Types ??= new CachedExternValueArray<TypeIdentifier>(Interface.form_types_length(Ptr),
arg => Interface.form_types_get(Ptr, arg));
_types ??= new CachedExternValueArray<TypeIdentifier>(Interface.form_types_length(Handle),
arg => Interface.form_types_get(Handle, arg));
private StaticStatisticSet<ushort>? _baseStats;
public StaticStatisticSet<ushort> BaseStats =>
Cache.BaseStats ??= new StaticStatisticSet<ushort>(Interface.form_base_stats(Ptr), false);
_baseStats ??= Resolver.Instance.ResolveStaticStatisticSet<ushort>(Interface.form_base_stats(Handle).Resolve());
private IReadOnlyList<string>? _abilities;
public IReadOnlyList<string> Abilities =>
Cache.Abilities ??= new CachedExternArray<string>(Interface.form_abilities_length(Ptr),
arg => Interface.form_abilities_get(Ptr, arg).PtrString()!);
_abilities ??= new CachedExternArray<string>(Interface.form_abilities_length(Handle),
arg => Interface.form_abilities_get(Handle, arg).PtrString()!);
private IReadOnlyList<string>? _hiddenAbilities;
public IReadOnlyList<string> HiddenAbilities =>
Cache.HiddenAbilities ??= new CachedExternArray<string>(Interface.form_hidden_abilities_length(Ptr),
arg => Interface.form_hidden_abilities_get(Ptr, arg).PtrString()!);
_hiddenAbilities ??= new CachedExternArray<string>(Interface.form_hidden_abilities_length(Handle),
arg => Interface.form_hidden_abilities_get(Handle, arg).PtrString()!);
private LearnableMoves? _learnableMoves;
public LearnableMoves LearnableMoves =>
Cache.LearnableMoves ??= new LearnableMoves(Interface.form_moves(Ptr), false);
protected override CacheData CreateCache() => new CacheData();
protected override void Destructor() => Interface.form_drop(Ptr);
public override void InvalidateChildren()
{
Cache.LearnableMoves?.Invalidate();
}
~Form()
{
Dispose();
}
_learnableMoves ??= Resolver.Instance.ResolveLearnableMoves(Interface.form_moves(Handle).Resolve());
}
}

View File

@ -1,56 +1,37 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.GrowthRate;
namespace PkmnLibSharp.StaticData
{
public abstract class GrowthRate : ExternPointer<object>
public abstract class GrowthRate : HandleType
{
protected internal GrowthRate()
{
}
protected internal GrowthRate(IntPtr ptr, bool isOwner) : base(ptr, isOwner)
protected internal GrowthRate(FFIHandle ptr) : base(ptr)
{
}
public LevelInt CalculateLevel(uint experience)
{
return Interface.growth_rate_calculate_level(Ptr, experience);
return Interface.growth_rate_calculate_level(Handle, experience);
}
public uint CalculateExperience(LevelInt level)
{
Debug.Assert(level >= 1);
return Interface.growth_rate_calculate_experience(Ptr, level);
}
protected override void Destructor()
{
Interface.growth_rate_lookup_drop(Ptr);
return Interface.growth_rate_calculate_experience(Handle, level).Result();
}
}
public class LookupGrowthRate : GrowthRate
{
public LookupGrowthRate(uint[] experienceArray)
protected LookupGrowthRate(FFIHandle handle) : base(handle){}
public static LookupGrowthRate Create(uint[] experienceArray)
{
var arrayPtr = experienceArray.ArrayPtr();
var ptr = Interface.growth_rate_lookup_new(arrayPtr, (ulong)experienceArray.Length);
InitializePointer(ptr, true);
}
protected override object CreateCache()
{
return new object();
}
~LookupGrowthRate()
{
Dispose();
return Resolver.Instance.ResolveLookupGrowthRate(ptr.Resolve());
}
}
@ -80,7 +61,7 @@ namespace PkmnLibSharp.StaticData
arr[i - 1] = 4 * Power3(i) / 5;
}
return new LookupGrowthRate(arr);
return LookupGrowthRate.Create(arr);
}
public static LookupGrowthRate MediumFast(LevelInt maxLevel)
@ -92,7 +73,7 @@ namespace PkmnLibSharp.StaticData
arr[i - 1] = Power3(i);
}
return new LookupGrowthRate(arr);
return LookupGrowthRate.Create(arr);
}
public static LookupGrowthRate MediumSlow(LevelInt maxLevel)
@ -104,7 +85,7 @@ namespace PkmnLibSharp.StaticData
arr[i - 1] = 6 * Power3(i) / 5 - 15 * Power2(i) + 100 * i - 140;
}
return new LookupGrowthRate(arr);
return LookupGrowthRate.Create(arr);
}
public static LookupGrowthRate Slow(LevelInt maxLevel)
@ -115,7 +96,7 @@ namespace PkmnLibSharp.StaticData
{
arr[i - 1] = 5 * Power3(i) / 4;
}
return new LookupGrowthRate(arr);
return LookupGrowthRate.Create(arr);
}
public static LookupGrowthRate Erratic(LevelInt maxLevel)
@ -143,7 +124,7 @@ namespace PkmnLibSharp.StaticData
arr[i - 1] = Power3(i) * (160 - i) / 100;
}
return new LookupGrowthRate(arr);
return LookupGrowthRate.Create(arr);
}
public static LookupGrowthRate Fluctuating(LevelInt maxLevel)
@ -166,7 +147,7 @@ namespace PkmnLibSharp.StaticData
arr[i - 1] = Power3(i) * (i / 2 + 32) / 50;
}
return new LookupGrowthRate(arr);
return LookupGrowthRate.Create(arr);
}
}
}

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Item;
@ -84,43 +82,29 @@ namespace PkmnLibSharp.StaticData
MiscBattleItem,
}
public class Item : ExternPointer<Item.CacheData>
public class Item : HandleType
{
public class CacheData
{
public string? Name { get; internal set; }
public ItemCategory? Category { get; internal set; }
public BattleItemCategory? BattleCategory { get; internal set; }
public int? Price { get; internal set; }
}
protected Item(FFIHandle handle) : base(handle){}
public Item(string name, ItemCategory category, BattleItemCategory battleItemCategory, int price,
public static Item Create(string name, ItemCategory category, BattleItemCategory battleItemCategory, int price,
IEnumerable<string> flags)
{
var ptrArray = flags.Select(x => x.ToPtr()).ToArray();
var ptrToPtrArray = ptrArray.ArrayPtr();
var ptr = Interface.item_new(name.ToPtr(), category, battleItemCategory, price, ptrToPtrArray,
var handle = Interface.item_new(name.ToPtr(), category, battleItemCategory, price, ptrToPtrArray,
(ulong)ptrArray.Length);
InitializePointer(ptr, true);
return Resolver.Instance.ResolveItem(handle.Result().Resolve());
}
internal Item(IdentifiablePointer ptr) : base(ptr, true)
{
}
private string? _name;
public string Name => _name ??= Interface.item_name(Handle).Result().PtrString()!;
private ItemCategory? _category;
public ItemCategory Category => _category ??= Interface.item_category(Handle);
private BattleItemCategory? _battleCategory;
public BattleItemCategory BattleCategory => _battleCategory ??= Interface.item_battle_category(Handle);
private int? _price;
public int Price => _price ??= Interface.item_price(Handle);
public string Name => Cache.Name ??= Interface.item_name(Ptr).PtrString()!;
public ItemCategory Category => Cache.Category ??= Interface.item_category(Ptr);
public BattleItemCategory BattleCategory => Cache.BattleCategory ??= Interface.item_battle_category(Ptr);
public int Price => Cache.Price ??= Interface.item_price(Ptr);
public bool HasFlag(string flag) => Interface.item_has_flag(Ptr, flag.ToPtr()) == 1;
protected override CacheData CreateCache() => new CacheData();
protected override void Destructor() => Interface.item_drop(Ptr);
~Item()
{
Dispose();
}
public bool HasFlag(string flag) => Interface.item_has_flag(Handle, flag.ToPtr()) == 1;
}
}

View File

@ -1,36 +1,28 @@
using System;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.LearnableMoves;
namespace PkmnLibSharp.StaticData
{
public class LearnableMoves : ExternPointer<object>
public class LearnableMoves : HandleType
{
internal LearnableMoves(IntPtr ptr, bool isOwner) : base(ptr, isOwner)
protected LearnableMoves(FFIHandle handle) : base(handle)
{
}
public LearnableMoves() : base(Interface.learnable_moves_new(), true)
public static LearnableMoves Create()
{
var handle = Interface.learnable_moves_new();
return Resolver.Instance.ResolveLearnableMoves(handle.Resolve());
}
public void AddLevelMove(LevelInt level, string moveName)
{
Interface.learnable_moves_add_level_move(Ptr, level, moveName.ToPtr());
Interface.learnable_moves_add_level_move(Handle, level, moveName.ToPtr());
}
public void AddLevelMove(LevelInt level, MoveData move)
{
Interface.learnable_moves_add_level_move(Ptr, level, move.Name.ToPtr());
}
protected override object CreateCache() => new object();
protected override void Destructor() => Interface.learnable_moves_drop(Ptr);
~LearnableMoves()
{
Dispose();
Interface.learnable_moves_add_level_move(Handle, level, move.Name.ToPtr());
}
}
}

View File

@ -2,6 +2,7 @@ using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BackingLevelInt = System.Byte;
// ReSharper disable BuiltInTypeReferenceStyle
namespace PkmnLibSharp.StaticData
{

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Libraries.AbilityLibrary;
@ -7,41 +8,19 @@ namespace PkmnLibSharp.StaticData.Libraries
{
public class AbilityLibrary : DataLibrary<Ability>
{
public AbilityLibrary(ulong capacity) : base(Interface.ability_library_new(capacity), true)
protected AbilityLibrary(FFIHandle handle) : base(handle)
{
}
internal AbilityLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
public static AbilityLibrary Create(ulong capacity)
{
var handle = Interface.ability_library_new(capacity).Resolve();
var self = Resolver.Instance.ResolveAbilityLibrary(handle);
self.ReserveCapacity(capacity);
return self;
}
protected override void Destructor() => Interface.ability_library_drop(Ptr);
public override void Add(string key, Ability value) =>
Interface.ability_library_add(Ptr, key.ToPtr(), value.TakeOwnershipAndInvalidate());
public override int Count => (int)Interface.ability_library_len(Ptr);
protected override Ability? GetValueByKey(string key)
{
var ptr = Interface.ability_library_get(Ptr, key.ToPtr());
return ptr.Ptr == IntPtr.Zero ? null : new Ability(ptr);
}
public override string? GetKeyByIndex(ulong index) =>
Interface.ability_library_get_key_by_index(Ptr, index).PtrString();
public override void InvalidateChildren()
{
foreach (var value in Cache.ValueCache.Values)
{
value.Invalidate();
}
}
~AbilityLibrary()
{
Dispose();
}
protected override void AddNative(string key, Ability value) =>
Interface.ability_library_add(Handle, key.ToPtr(), value.Handle);
}
}

View File

@ -1,91 +1,26 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
namespace PkmnLibSharp.StaticData.Libraries
{
public abstract class DataLibrary<T> : ExternPointer<DataLibrary<T>.CacheData>, IReadOnlyDictionary<string, T>
public abstract class DataLibrary<T> : HandleType, IReadOnlyDictionary<string, T> where T : HandleType
{
protected DataLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
private Dictionary<string, T> _backingDictionary = new(StringComparer.InvariantCultureIgnoreCase);
protected DataLibrary(FFIHandle handle) : base(handle)
{
}
public class CacheData
protected void ReserveCapacity(ulong capacity)
{
public List<string>? KeyCache { get; internal set; }
public Dictionary<string, T> ValueCache { get; } = new();
_backingDictionary = new Dictionary<string, T>((int)capacity, StringComparer.InvariantCultureIgnoreCase);
}
public abstract void Add(string key, T value);
public abstract int Count { get; }
protected abstract T? GetValueByKey(string key);
public bool ContainsKey(string key)
{
return GetValueByKey(key) != null;
}
public bool TryGetValue(string key, [NotNullWhen(true)] out T value)
{
if (Cache.ValueCache.TryGetValue(key, out value) && value != null)
return true;
var v = GetValueByKey(key);
if (v != null)
{
Cache.ValueCache.Add(key, v);
value = v;
}
return v != null;
}
public T this[string key]
{
get
{
if (!TryGetValue(key, out var value))
throw new KeyNotFoundException($"Value with key `{key}` was not found");
return value!;
}
}
public abstract string? GetKeyByIndex(ulong index);
public IEnumerable<string> Keys
{
get
{
if (Cache.KeyCache == null)
{
Cache.KeyCache = new List<string>(Count);
for (ulong i = 0; i < (ulong)Count; i++)
{
var key = GetKeyByIndex(i);
if (key == null)
break;
Cache.KeyCache.Add(key);
}
}
return Cache.KeyCache;
}
}
public IEnumerable<T> Values
{
get { return Keys.Select(key => this[key]); }
}
protected override CacheData CreateCache() => new();
public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
{
return Keys.Select(key => new KeyValuePair<string, T>(key, this[key])).GetEnumerator();
return _backingDictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
@ -93,9 +28,22 @@ namespace PkmnLibSharp.StaticData.Libraries
return GetEnumerator();
}
~DataLibrary()
protected abstract void AddNative(string key, T value);
public void Add(string key, T value)
{
Dispose();
AddNative(key, value);
_backingDictionary.Add(key, value);
}
public int Count => _backingDictionary.Count;
public bool ContainsKey(string key) => _backingDictionary.ContainsKey(key);
public bool TryGetValue(string key, out T value) => _backingDictionary.TryGetValue(key, out value);
public T this[string key] => _backingDictionary[key];
public IEnumerable<string> Keys => _backingDictionary.Keys;
public IEnumerable<T> Values => _backingDictionary.Values;
}
}

View File

@ -1,43 +1,43 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
using JetBrains.Annotations;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Libraries.GrowthRateLibrary;
namespace PkmnLibSharp.StaticData.Libraries
{
public class GrowthRateLibrary : ExternPointer<object>
public class GrowthRateLibrary : HandleType
{
public GrowthRateLibrary(ulong capacity) : base(Interface.growth_rate_library_new(capacity), true)
// ReSharper disable once CollectionNeverQueried.Local
private Dictionary<string, GrowthRate> _growthRates = new();
protected GrowthRateLibrary(FFIHandle handle) : base(handle)
{
}
internal GrowthRateLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
public static GrowthRateLibrary Create(ulong capacity)
{
var handle = Interface.growth_rate_library_new(capacity);
var lib = Resolver.Instance.ResolveGrowthRateLibrary(handle.Resolve());
if (capacity > 0)
lib._growthRates = new Dictionary<string, GrowthRate>((int)capacity, StringComparer.InvariantCultureIgnoreCase);
return lib;
}
[MustUseReturnValue]
public LevelInt CalculateLevel(string growthRate, uint experience) =>
Interface.growth_rate_library_calculate_level(Ptr, growthRate.ToPtr(), experience);
public LevelInt CalculateLevel(string name, uint experience) =>
Interface.growth_rate_library_calculate_level(Handle, name.ToPtr(), experience).Result();
[MustUseReturnValue]
public uint CalculateExperience(string growthRate, LevelInt level)
public uint CalculateExperience(string name, LevelInt level)
{
Debug.Assert(level >= 1);
return Interface.growth_rate_library_calculate_experience(Ptr, growthRate.ToPtr(), level);
return Interface.growth_rate_library_calculate_experience(Handle, name.ToPtr(), level).Result();
}
public void AddGrowthRate(string name, GrowthRate growthRate) =>
Interface.growth_rate_library_add_growth_rate(Ptr, name.ToPtr(), growthRate.TakeOwnershipAndInvalidate());
protected override object CreateCache() => new();
protected override void Destructor() => Interface.growth_rate_library_drop(Ptr);
~GrowthRateLibrary()
public void AddGrowthRate(string name, GrowthRate growthRate)
{
Dispose();
Interface.growth_rate_library_add_growth_rate(Handle, name.ToPtr(), growthRate.Handle);
_growthRates.Add(name, growthRate);
}
}
}

View File

@ -7,41 +7,19 @@ namespace PkmnLibSharp.StaticData.Libraries
{
public class ItemLibrary : DataLibrary<Item>
{
public ItemLibrary(ulong capacity) : base(Interface.item_library_new(capacity), true)
protected ItemLibrary(FFIHandle handle) : base(handle){}
public static ItemLibrary Create(ulong capacity)
{
var handle = Interface.item_library_new(capacity).Resolve();
var self = Resolver.Instance.ResolveItemLibrary(handle);
self.ReserveCapacity(capacity);
return self;
}
internal ItemLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
protected override void AddNative(string key, Item value)
{
}
protected override void Destructor() => Interface.item_library_drop(Ptr);
public override void Add(string key, Item value) =>
Interface.item_library_add(Ptr, key.ToPtr(), value.TakeOwnershipAndInvalidate());
public override int Count => (int)Interface.item_library_len(Ptr);
protected override Item? GetValueByKey(string key)
{
var ptr = Interface.item_library_get(Ptr, key.ToPtr());
return ptr.Ptr == IntPtr.Zero ? null : new Item(ptr);
}
public override string? GetKeyByIndex(ulong index) =>
Interface.item_library_get_key_by_index(Ptr, index).PtrString();
public override void InvalidateChildren()
{
foreach (var value in Cache.ValueCache.Values)
{
value.Invalidate();
}
}
~ItemLibrary()
{
Dispose();
Interface.item_library_add(Handle, key.ToPtr(), value.Handle);
}
}
}

View File

@ -8,13 +8,9 @@ namespace PkmnLibSharp.StaticData.Libraries
/// <summary>
/// This library holds several misc settings for the library.
/// </summary>
public class LibrarySettings : ExternPointer<LibrarySettings.CacheData>
public class LibrarySettings : HandleType
{
public class CacheData
{
public LevelInt? MaxLevel { get; internal set; }
public uint? ShinyRate { get; internal set; }
}
protected LibrarySettings(FFIHandle handle) : base(handle) {}
/// <inheritdoc cref="LibrarySettings"/>
/// <param name="maxLevel">The highest level a Pokemon can be.</param>
@ -22,35 +18,23 @@ namespace PkmnLibSharp.StaticData.Libraries
/// The chance of a Pokemon being shiny, as the denominator of a fraction, where the nominator
/// is 1. For example, if this is 1000, then the chance of a Pokemon being shiny is 1/1000.
/// </param>
public LibrarySettings(LevelInt maxLevel, uint shinyRate)
{
Debug.Assert(maxLevel >= 1);
Debug.Assert(shinyRate >= 1);
InitializePointer(Interface.library_settings_new(maxLevel, shinyRate), true);
}
internal LibrarySettings(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
public static LibrarySettings Create(LevelInt maxLevel, uint shinyRate)
{
var handle = Interface.library_settings_new(maxLevel, shinyRate).Result();
return Resolver.Instance.ResolveLibrarySettings(handle.Resolve());
}
private LevelInt? _maxLevel;
/// <summary>
/// The highest level a Pokemon can be.
/// </summary>
public LevelInt MaxLevel => Cache.MaxLevel ??= Interface.library_settings_maximum_level(Ptr);
public LevelInt MaxLevel => _maxLevel ??= Interface.library_settings_maximum_level(Handle);
private uint? _shinyRate;
/// <summary>
/// The chance of a Pokemon being shiny, as the denominator of a fraction, where the nominator
/// is 1. For example, if this is 1000, then the chance of a Pokemon being shiny is 1/1000.
/// </summary>
public uint ShinyRate => Cache.ShinyRate ??= Interface.library_settings_shiny_rate(Ptr);
protected override CacheData CreateCache() => new();
protected override void Destructor() => Interface.library_settings_drop(Ptr);
~LibrarySettings()
{
Dispose();
}
public uint ShinyRate => _shinyRate ??= Interface.library_settings_shiny_rate(Handle);
}
}

View File

@ -8,44 +8,19 @@ namespace PkmnLibSharp.StaticData.Libraries
{
public class MoveLibrary : DataLibrary<MoveData>
{
public MoveLibrary(ulong capacity) : base(Interface.move_library_new(capacity), true)
protected MoveLibrary(FFIHandle handle) : base(handle)
{
}
internal MoveLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
public static MoveLibrary Create(ulong capacity)
{
var handle = Interface.move_library_new(capacity);
return Resolver.Instance.ResolveMoveLibrary(handle.Resolve());
}
protected override void Destructor()
protected override void AddNative(string key, MoveData value)
{
Interface.move_library_drop(Ptr);
}
public override void Add(string key, MoveData value) =>
Interface.move_library_add(Ptr, key.ToPtr(), value.TakeOwnershipAndInvalidate());
public override int Count => (int)Interface.move_library_len(Ptr);
protected override MoveData? GetValueByKey(string key)
{
var ptr = Interface.move_library_get(Ptr, key.ToPtr());
return ptr.Ptr == IntPtr.Zero ? null : new MoveData(ptr, false);
}
public override string? GetKeyByIndex(ulong index) =>
Interface.move_library_get_key_by_index(Ptr, index).PtrString();
public override void InvalidateChildren()
{
foreach (var value in Cache.ValueCache.Values)
{
value.Invalidate();
}
}
~MoveLibrary()
{
Dispose();
Interface.move_library_add(Handle, key.ToPtr(), value.Handle);
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
@ -7,65 +8,50 @@ using Interface = PkmnLibSharp.FFI.StaticData.Libraries.NatureLibrary;
namespace PkmnLibSharp.StaticData.Libraries
{
public class NatureLibrary : ExternPointer<NatureLibrary.CacheData>
public class NatureLibrary : HandleType
{
public class CacheData
{
public Dictionary<string, Nature> Natures { get; } = new(StringComparer.InvariantCultureIgnoreCase);
}
private Dictionary<string, Nature> _natures = new();
public NatureLibrary(ulong capacity) : base(Interface.nature_library_new(capacity), true)
protected NatureLibrary(FFIHandle handle) : base(handle)
{
}
internal NatureLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
public static NatureLibrary Create(ulong capacity)
{
var handle = Interface.nature_library_new(capacity);
var lib = Resolver.Instance.ResolveNatureLibrary(handle.Resolve());
if (capacity > 0)
lib._natures = new Dictionary<string, Nature>((int)capacity, StringComparer.InvariantCultureIgnoreCase);
return lib;
}
public Optional<Nature> TryGetNature(string name)
public bool TryGetNature(string name, [NotNullWhen(true)] out Nature? nature)
{
if (Cache.Natures.TryGetValue(name, out var nature))
return nature;
var naturePtr = Interface.nature_library_get_nature(Ptr, name.ToPtr());
if (naturePtr.Ptr == IntPtr.Zero)
return Optional<Nature>.None();
nature = new Nature(naturePtr);
Cache.Natures.Add(name, nature);
return nature;
if (_natures.TryGetValue(name, out nature))
return true;
var naturePtr = Interface.nature_library_get_nature(Handle, name.ToPtr());
if (naturePtr.Handle == 0)
return false;
nature = Resolver.Instance.ResolveNature(naturePtr.Resolve());
_natures.Add(name, nature);
return true;
}
public Nature GetRandomNature(ulong seed)
{
return new Nature(Interface.nature_library_get_random_nature(Ptr, seed));
return Resolver.Instance.ResolveNature(Interface.nature_library_get_random_nature(Handle, seed).Result().Resolve());
}
public string GetNatureName(Nature nature)
{
var fd = Cache.Natures.FirstOrDefault(x => x.Value == nature);
return fd.Key ?? Interface.nature_library_get_nature_name(Ptr, nature.Ptr).PtrString()!;
var fd = _natures.FirstOrDefault(x => x.Value == nature);
return fd.Key ?? Interface.nature_library_get_nature_name(Handle, nature.Handle).PtrString()!;
}
public void LoadNature(string name, Nature nature) =>
Interface.nature_library_load_nature(Ptr, name.ToPtr(), nature.TakeOwnershipAndInvalidate());
protected override CacheData CreateCache() => new();
protected override void Destructor() => Interface.nature_library_drop(Ptr);
public override void InvalidateChildren()
public void LoadNature(string name, Nature nature)
{
base.InvalidateChildren();
foreach (var nature in Cache.Natures.Values)
{
nature.Invalidate();
}
}
~NatureLibrary()
{
Dispose();
Interface.nature_library_load_nature(Handle, name.ToPtr(), nature.Handle);
_natures.Add(name, nature);
}
}
}

View File

@ -1,5 +1,3 @@
using System;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Libraries.SpeciesLibrary;
@ -7,41 +5,22 @@ namespace PkmnLibSharp.StaticData.Libraries
{
public class SpeciesLibrary : DataLibrary<Species>
{
public SpeciesLibrary(ulong capacity) : base(Interface.species_library_new(capacity), true)
protected SpeciesLibrary(FFIHandle handle) : base(handle)
{
}
internal SpeciesLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
public static SpeciesLibrary Create(ulong capacity)
{
var handle = Interface.species_library_new(capacity);
var lib = Resolver.Instance.ResolveSpeciesLibrary(handle.Resolve());
if (capacity > 0)
lib.ReserveCapacity(capacity);
return lib;
}
protected override void Destructor() => Interface.species_library_drop(Ptr);
public override void Add(string key, Species value) =>
Interface.species_library_add(Ptr, key.ToPtr(), value.TakeOwnershipAndInvalidate());
public override int Count => (int)Interface.species_library_len(Ptr);
protected override Species? GetValueByKey(string key)
protected override void AddNative(string key, Species value)
{
var ptr = Interface.species_library_get(Ptr, key.ToPtr());
return ptr.Ptr == IntPtr.Zero ? null : new Species(ptr);
}
public override string? GetKeyByIndex(ulong index) =>
Interface.species_library_get_key_by_index(Ptr, index).PtrString();
public override void InvalidateChildren()
{
foreach (var value in Cache.ValueCache.Values)
{
value.Invalidate();
}
}
~SpeciesLibrary()
{
Dispose();
Interface.species_library_add(Handle, key.ToPtr(), value.Handle);
}
}
}

View File

@ -1,82 +1,40 @@
using JetBrains.Annotations;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Libraries.StaticData;
namespace PkmnLibSharp.StaticData.Libraries
{
public class StaticData : ExternPointer<StaticData.CacheData>
public class StaticData : HandleType
{
public class CacheData
{
public LibrarySettings? Settings { get; internal set; }
public SpeciesLibrary? SpeciesLibrary { get; internal set; }
public MoveLibrary? MoveLibrary { get; internal set; }
public ItemLibrary? ItemLibrary { get; internal set; }
public GrowthRateLibrary? GrowthRateLibrary { get; internal set; }
public TypeLibrary? TypeLibrary { get; internal set; }
public NatureLibrary? NatureLibrary { get; internal set; }
public AbilityLibrary? AbilityLibrary { get; internal set; }
}
internal StaticData(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner){}
public StaticData(LibrarySettings settings, SpeciesLibrary speciesLibrary, MoveLibrary moveLibrary,
ItemLibrary itemLibrary, GrowthRateLibrary growthRateLibrary, TypeLibrary typeLibrary,
NatureLibrary natureLibrary, AbilityLibrary abilityLibrary) : base(
Interface.static_data_new(settings.TakeOwnershipAndInvalidate(),
speciesLibrary.TakeOwnershipAndInvalidate(), moveLibrary.TakeOwnershipAndInvalidate(),
itemLibrary.TakeOwnershipAndInvalidate(), growthRateLibrary.TakeOwnershipAndInvalidate(),
typeLibrary.TakeOwnershipAndInvalidate(), natureLibrary.TakeOwnershipAndInvalidate(),
abilityLibrary.TakeOwnershipAndInvalidate()), true)
protected StaticData(FFIHandle handle) : base(handle)
{
}
public LibrarySettings LibrarySettings =>
Cache.Settings ??= new LibrarySettings(Interface.static_data_settings(Ptr), false);
public SpeciesLibrary SpeciesLibrary =>
Cache.SpeciesLibrary ??= new SpeciesLibrary(Interface.static_data_species(Ptr), false);
public MoveLibrary MoveLibrary =>
Cache.MoveLibrary ??= new MoveLibrary(Interface.static_data_moves(Ptr), false);
public ItemLibrary ItemLibrary =>
Cache.ItemLibrary ??= new ItemLibrary(Interface.static_data_items(Ptr), false);
public GrowthRateLibrary GrowthRateLibrary =>
Cache.GrowthRateLibrary ??= new GrowthRateLibrary(Interface.static_data_growth_rates(Ptr), false);
public TypeLibrary TypeLibrary =>
Cache.TypeLibrary ??= new TypeLibrary(Interface.static_data_types(Ptr), false);
public NatureLibrary NatureLibrary =>
Cache.NatureLibrary ??= new NatureLibrary(Interface.static_data_natures(Ptr), false);
public AbilityLibrary AbilityLibrary =>
Cache.AbilityLibrary ??= new AbilityLibrary(Interface.static_data_abilities(Ptr), false);
protected override CacheData CreateCache() => new();
protected override void Destructor() => Interface.static_data_drop(Ptr);
public override void InvalidateChildren()
public static StaticData Create(LibrarySettings settings, SpeciesLibrary speciesLibrary,
MoveLibrary moveLibrary, ItemLibrary itemLibrary, GrowthRateLibrary growthRateLibrary,
TypeLibrary typeLibrary, NatureLibrary natureLibrary, AbilityLibrary abilityLibrary)
{
if (!HasCache)
return;
Cache.Settings?.Invalidate();
Cache.SpeciesLibrary?.Invalidate();
Cache.MoveLibrary?.Invalidate();
Cache.ItemLibrary?.Invalidate();
Cache.GrowthRateLibrary?.Invalidate();
Cache.TypeLibrary?.Invalidate();
Cache.NatureLibrary?.Invalidate();
Cache.AbilityLibrary?.Invalidate();
var handle = Interface.static_data_new(settings.Handle, speciesLibrary.Handle, moveLibrary.Handle,
itemLibrary.Handle, growthRateLibrary.Handle, typeLibrary.Handle, natureLibrary.Handle,
abilityLibrary.Handle);
var data = Resolver.Instance.ResolveStaticData(handle.Resolve());
data.LibrarySettings = settings;
data.SpeciesLibrary = speciesLibrary;
data.MoveLibrary = moveLibrary;
data.ItemLibrary = itemLibrary;
data.GrowthRateLibrary = growthRateLibrary;
data.TypeLibrary = typeLibrary;
data.NatureLibrary = natureLibrary;
data.AbilityLibrary = abilityLibrary;
return data;
}
~StaticData()
{
Dispose();
}
public LibrarySettings LibrarySettings { get; private set; } = null!;
public SpeciesLibrary SpeciesLibrary { get; private set; } = null!;
public MoveLibrary MoveLibrary { get; private set; } = null!;
public ItemLibrary ItemLibrary { get; private set; } = null!;
public GrowthRateLibrary GrowthRateLibrary { get; private set; } = null!;
public TypeLibrary TypeLibrary { get; private set; } = null!;
public NatureLibrary NatureLibrary { get; private set; } = null!;
public AbilityLibrary AbilityLibrary { get; private set; } = null!;
}
}

View File

@ -7,32 +7,31 @@ using Interface = PkmnLibSharp.FFI.StaticData.Libraries.TypeLibrary;
namespace PkmnLibSharp.StaticData.Libraries
{
public class TypeLibrary : ExternPointer<TypeLibrary.CacheData>
public class TypeLibrary : HandleType
{
public class CacheData
{
public Dictionary<string, TypeIdentifier> TypeCache { get; } =
new(StringComparer.InvariantCultureIgnoreCase);
}
public Dictionary<string, TypeIdentifier> TypeCache { get; private set; } =
new(StringComparer.InvariantCultureIgnoreCase);
public TypeLibrary(ulong capacity) : base(Interface.type_library_new(capacity), true)
{
}
protected TypeLibrary(FFIHandle handle) : base(handle){}
internal TypeLibrary(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
public static TypeLibrary Create(ulong capacity)
{
var handle = Interface.type_library_new(capacity);
var lib = Resolver.Instance.ResolveTypeLibrary(handle.Resolve());
lib.TypeCache = new Dictionary<string, TypeIdentifier>((int)capacity, StringComparer.InvariantCultureIgnoreCase);
return lib;
}
public TypeIdentifier GetTypeId(string name)
{
if (Cache.TypeCache.TryGetValue(name, out var typeIdentifier))
if (TypeCache.TryGetValue(name, out var typeIdentifier))
return typeIdentifier;
throw new KeyNotFoundException($"No type found with name `{name}`");
}
public string GetTypeName(TypeIdentifier typeIdentifier)
{
var fd = Cache.TypeCache.FirstOrDefault(x => x.Value == typeIdentifier);
var fd = TypeCache.FirstOrDefault(x => x.Value == typeIdentifier);
if (fd.Key != null)
return fd.Key;
throw new KeyNotFoundException($"No type found for given identifier");
@ -40,32 +39,22 @@ namespace PkmnLibSharp.StaticData.Libraries
public float GetSingleEffectiveness(TypeIdentifier attacking, TypeIdentifier defending) =>
Interface.type_library_get_single_effectiveness(Ptr, attacking, defending);
Interface.type_library_get_single_effectiveness(Handle, attacking, defending);
public float GetEffectiveness(TypeIdentifier attacking, TypeIdentifier[] defending)
{
var arrayPtr = defending.ArrayPtr();
return Interface.type_library_get_effectiveness(Ptr, attacking, arrayPtr, (ulong)defending.Length);
return Interface.type_library_get_effectiveness(Handle, attacking, arrayPtr, (ulong)defending.Length);
}
public TypeIdentifier RegisterType(string name)
{
var typeId = Interface.type_library_register_type(Ptr, name.ToPtr());
Cache.TypeCache.Add(name, typeId);
var typeId = Interface.type_library_register_type(Handle, name.ToPtr());
TypeCache.Add(name, typeId);
return typeId;
}
public void SetEffectiveness(TypeIdentifier attacking, TypeIdentifier defending, float effectiveness) =>
Interface.type_library_set_effectiveness(Ptr, attacking, defending, effectiveness);
protected override CacheData CreateCache() => new();
protected override void Destructor() => Interface.type_library_drop(Ptr);
~TypeLibrary()
{
Dispose();
}
Interface.type_library_set_effectiveness(Handle, attacking, defending, effectiveness);
}
}

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.MoveData;
@ -62,84 +60,57 @@ namespace PkmnLibSharp.StaticData
SelfUse,
}
public class MoveData : ExternPointer<MoveData.CacheData>
public class MoveData : HandleType
{
public class CacheData
{
public string? Name { get; internal set; }
public TypeIdentifier? Type { get; internal set; }
public MoveCategory? Category { get; internal set; }
public byte? BasePower { get; internal set; }
public byte? Accuracy { get; internal set; }
public byte? BaseUsages { get; internal set; }
public MoveTarget? Target { get; internal set; }
public sbyte? Priority { get; internal set; }
public SecondaryEffect? SecondaryEffect { get; internal set; }
}
protected MoveData(FFIHandle handle) : base(handle) {}
internal MoveData(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
{
}
public MoveData(string name, TypeIdentifier moveType, MoveCategory category, byte basePower, byte accuracy,
public static MoveData Create(string name, TypeIdentifier moveType, MoveCategory category, byte basePower, byte accuracy,
byte baseUsages, MoveTarget target, sbyte priority, SecondaryEffect? secondaryEffect,
IEnumerable<string> flags)
{
var ptrArray = flags.Select(x => x.ToPtr()).ToArray();
var ptrToPtrArray = ptrArray.ArrayPtr();
var ptr = Interface.move_data_new(name.ToPtr(), moveType, category, basePower, accuracy, baseUsages, target,
priority, secondaryEffect?.TakeOwnershipAndInvalidate() ?? IntPtr.Zero, ptrToPtrArray,
var handle = Interface.move_data_new(name.ToPtr(), moveType, category, basePower, accuracy, baseUsages, target,
priority, secondaryEffect?.Handle ?? FFIHandle.Zero, ptrToPtrArray,
(ulong)ptrArray.Length);
InitializePointer(ptr, true);
return Resolver.Instance.ResolveMoveData(handle.Result().Resolve());
}
public string Name => Cache.Name ??= Interface.move_data_name(Ptr).PtrString()!;
public TypeIdentifier Type => Cache.Type ??= Interface.move_data_move_type(Ptr);
public byte BasePower => Cache.BasePower ??= Interface.move_data_base_power(Ptr);
public MoveCategory Category => Cache.Category ??= Interface.move_data_category(Ptr);
public byte Accuracy => Cache.Accuracy ??= Interface.move_data_accuracy(Ptr);
public byte BaseUsages => Cache.BaseUsages ??= Interface.move_data_base_usages(Ptr);
public MoveTarget Target => Cache.Target ??= Interface.move_data_target(Ptr);
public sbyte Priority => Cache.Priority ??= Interface.move_data_priority(Ptr);
private string? _name;
public string Name => _name ??= Interface.move_data_name(Handle).Result().PtrString()!;
private TypeIdentifier? _type;
public TypeIdentifier Type => _type ??= Interface.move_data_move_type(Handle);
private byte? _basePower;
public byte BasePower => _basePower ??= Interface.move_data_base_power(Handle);
private MoveCategory? _category;
public MoveCategory Category => _category ??= Interface.move_data_category(Handle);
private byte? _accuracy;
public byte Accuracy => _accuracy ??= Interface.move_data_accuracy(Handle);
private byte? _baseUsages;
public byte BaseUsages => _baseUsages ??= Interface.move_data_base_usages(Handle);
private MoveTarget? _target;
public MoveTarget Target => _target ??= Interface.move_data_target(Handle);
private sbyte? _priority;
public sbyte Priority => _priority ??= Interface.move_data_priority(Handle);
private SecondaryEffect? _secondaryEffect;
public SecondaryEffect? SecondaryEffect
{
get
{
if (Cache.SecondaryEffect != null)
return Cache.SecondaryEffect;
var effect = Interface.move_data_secondary_effect(Ptr);
if (effect.Ptr == IntPtr.Zero)
if (_secondaryEffect != null)
return _secondaryEffect;
var effect = Interface.move_data_secondary_effect(Handle);
if (effect.Handle == 0)
return null;
Cache.SecondaryEffect = new SecondaryEffect(effect, false);
return Cache.SecondaryEffect;
_secondaryEffect = Resolver.Instance.ResolveSecondaryEffect(effect.Resolve());
return _secondaryEffect;
}
}
public bool HasFlag(string flag)
{
return Interface.move_data_has_flag(Ptr, flag.ToPtr()) == 1;
}
protected override CacheData CreateCache()
{
return new CacheData();
}
protected override void Destructor()
{
Interface.move_data_drop(Ptr);
}
public override void InvalidateChildren()
{
base.InvalidateChildren();
Cache.SecondaryEffect?.Invalidate();
}
~MoveData()
{
Dispose();
return Interface.move_data_has_flag(Handle, flag.ToPtr()) == 1;
}
}
}

View File

@ -1,62 +1,41 @@
using System;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Nature;
namespace PkmnLibSharp.StaticData
{
public class Nature : ExternPointer<Nature.CacheData>
public class Nature : HandleType
{
public class CacheData
{
public Statistic? IncreasedStat { get; internal set; }
public Statistic? DecreasedStat { get; internal set; }
}
public Nature(Statistic increasedStat, Statistic decreasedStat, float increaseModifier = 1.1f,
float decreaseModifier = 0.9f) : base(
Interface.nature_new(increasedStat, decreasedStat, increaseModifier, decreaseModifier), true)
protected Nature(FFIHandle handle) : base(handle)
{
}
internal Nature(IdentifiablePointer ptr) : base(ptr, true)
public static Nature Create(Statistic increasedStat, Statistic decreasedStat, float increaseModifier = 1.1f,
float decreaseModifier = 0.9f)
{
var handle = Interface.nature_new(increasedStat, decreasedStat, increaseModifier, decreaseModifier);
return Resolver.Instance.ResolveNature(handle.Resolve());
}
public static Nature NeutralNature()
{
return new Nature(Statistic.HP, Statistic.HP, 1f, 1f);
return Create(Statistic.HP, Statistic.HP, 1f, 1f);
}
private Statistic? _increasedStat;
public Statistic IncreasedStat
{
get { return Cache.IncreasedStat ??= Interface.nature_increased_stat(Ptr); }
get { return _increasedStat ??= Interface.nature_increased_stat(Handle); }
}
private Statistic? _decreasedStat;
public Statistic DecreasedStat
{
get { return Cache.DecreasedStat ??= Interface.nature_decreased_stat(Ptr); }
get { return _decreasedStat ??= Interface.nature_decreased_stat(Handle); }
}
public float GetStatModifier(Statistic statistic)
{
return Interface.nature_get_stat_modifier(Ptr, statistic);
}
protected override CacheData CreateCache()
{
return new CacheData();
}
protected override void Destructor()
{
Interface.nature_drop(Ptr);
}
~Nature()
{
Dispose();
return Interface.nature_get_stat_modifier(Handle, statistic);
}
}
}

View File

@ -1,70 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.MoveData;
namespace PkmnLibSharp.StaticData
{
public class SecondaryEffect : ExternPointer<SecondaryEffect.CacheData>
public class SecondaryEffect : HandleType
{
public class CacheData
{
public float? Chance { get; internal set; }
public string? Name { get; internal set; }
public DisposableCachedExternArray<EffectParameter>? Parameters { get; internal set; }
}
internal SecondaryEffect(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
protected SecondaryEffect(FFIHandle handle) : base(handle)
{
}
public SecondaryEffect(float chance, string effectName, IReadOnlyCollection<EffectParameter> parameters)
public static SecondaryEffect Create(float chance, string effectName, IReadOnlyList<EffectParameter> parameters)
{
var parameterPtrs = parameters.Select(x => x.TakeOwnershipAndInvalidate()).ToArray();
var parameterPtrs = parameters.Select(x => (FFIHandleValue)x.Handle).ToArray();
var parameterArrayPtr = parameterPtrs.ArrayPtr();
InitializePointer(
Interface.secondary_effect_new(chance, effectName.ToPtr(), parameterArrayPtr, (ulong)parameters.Count),
true);
var handle = Interface.secondary_effect_new(chance, effectName.ToPtr(), parameterArrayPtr,
(ulong)parameters.Count);
var effect = Resolver.Instance.ResolveSecondaryEffect(handle.Resolve());
effect.Parameters = parameters;
return effect;
}
public float Chance => Cache.Chance ??= Interface.secondary_effect_chance(Ptr);
public string Name => Cache.Name ?? (Cache.Name = Interface.secondary_effect_effect_name(Ptr).PtrString()!);
private float? _chance;
public float Chance => _chance ??= Interface.secondary_effect_chance(Handle);
public IReadOnlyList<EffectParameter> GetParameters()
{
return Cache.Parameters ??= new DisposableCachedExternArray<EffectParameter>(
Interface.secondary_effect_parameter_length(Ptr),
arg => new EffectParameter(Interface.secondary_effect_parameter_get(Ptr, arg), false));
}
private string? _name;
public string Name => _name ??= Interface.secondary_effect_effect_name(Handle).Result().PtrString()!;
protected override CacheData CreateCache()
{
return new CacheData();
}
protected override void Destructor()
{
Interface.secondary_effect_drop(Ptr);
}
public override void InvalidateChildren()
{
base.InvalidateChildren();
if (Cache.Parameters != null)
{
for (var index = 0; index < Cache.Parameters.Count; index++)
{
Cache.Parameters.GetCachedValue(index)?.Invalidate();
}
Cache.Parameters.Dispose();
}
}
~SecondaryEffect()
{
Dispose();
}
public IReadOnlyList<EffectParameter> Parameters { get; private set; } = null!;
}
}

View File

@ -2,42 +2,38 @@ using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using PkmnLibSharp.FFI;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Species;
namespace PkmnLibSharp.StaticData
{
public class Species : ExternPointer<Species.CacheData>
public class Species : HandleType
{
public class CacheData
{
public ushort? Id { get; internal set; }
public string? Name { get; internal set; }
public float? GenderRate { get; internal set; }
public string? GrowthRate { get; internal set; }
public byte? CaptureRate { get; internal set; }
public Dictionary<string, Form> Forms { get; } = new();
}
internal Species(IdentifiablePointer ptr) : base(ptr, true)
protected Species(FFIHandle handle) : base(handle)
{
}
public Species(ushort id, string name, float genderRate, string growthRate, byte captureRate, Form defaultForm,
public static Species Create(ushort id, string name, float genderRate, string growthRate, byte captureRate, Form defaultForm,
IReadOnlyCollection<string> flags)
{
var flagsPtrArray = flags.Select(x => x.ToPtr()).ToArray();
var ptr = Interface.species_new(id, name.ToPtr(), genderRate, growthRate.ToPtr(), captureRate,
defaultForm.Ptr, flagsPtrArray.ArrayPtr(), (ulong)flags.Count);
InitializePointer(ptr, true);
var handle = Interface.species_new(id, name.ToPtr(), genderRate, growthRate.ToPtr(), captureRate,
defaultForm.Handle, flagsPtrArray.ArrayPtr(), (ulong)flags.Count);
var species = Resolver.Instance.ResolveSpecies(handle.Result().Resolve());
species._forms.Add("default", defaultForm);
return species;
}
public ushort Id => Cache.Id ??= Interface.species_id(Ptr);
public string Name => Cache.Name ??= Interface.species_name(Ptr).PtrString()!;
public float GenderRate => Cache.GenderRate ??= Interface.species_gender_rate(Ptr);
public string GrowthRate => Cache.GrowthRate ??= Interface.species_growth_rate(Ptr).PtrString()!;
public byte CaptureRate => Cache.CaptureRate ??= Interface.species_capture_rate(Ptr);
private ushort? _id;
public ushort Id => _id ??= Interface.species_id(Handle);
private string? _name;
public string Name => _name ??= Interface.species_name(Handle).PtrString()!;
private float? _genderRate;
public float GenderRate => _genderRate ??= Interface.species_gender_rate(Handle);
private string? _growthRate;
public string GrowthRate => _growthRate ??= Interface.species_growth_rate(Handle).PtrString()!;
private byte? _captureRate;
public byte CaptureRate => _captureRate ??= Interface.species_capture_rate(Handle);
public Form DefaultForm
{
@ -48,42 +44,29 @@ namespace PkmnLibSharp.StaticData
}
}
private Dictionary<string, Form> _forms = new(StringComparer.InvariantCultureIgnoreCase);
public bool TryGetForm(string formName, [NotNullWhen(true)] out Form? form)
{
if (Cache.Forms.TryGetValue(formName, out form))
if (_forms.TryGetValue(formName, out form))
return true;
var formPtr = Interface.species_get_form(Ptr, formName.ToPtr());
if (formPtr.Ptr == IntPtr.Zero)
var formPtr = Interface.species_get_form(Handle, formName.ToPtr());
if (formPtr.Handle == 0)
{
form = null;
return false;
}
form = new Form(formPtr);
Cache.Forms.Add(formName, form);
form = Resolver.Instance.ResolveForm(formPtr.Resolve());
_forms.Add(formName, form);
return true;
}
public void AddForm(Form form) =>
Interface.species_add_form(Ptr, form.Name.ToPtr(), form.TakeOwnershipAndInvalidate());
public Gender GetRandomGender(ulong seed) => Interface.species_get_random_gender(Ptr, seed);
protected override CacheData CreateCache() => new CacheData();
protected override void Destructor() => Interface.species_drop(Ptr);
public override void InvalidateChildren()
public void AddForm(Form form)
{
base.InvalidateChildren();
foreach (var form in Cache.Forms)
{
form.Value.Invalidate();
}
Interface.species_add_form(Handle, form.Name.ToPtr(), form.Handle).Result();
_forms.Add(form.Name, form);
}
~Species()
{
Dispose();
}
public Gender GetRandomGender(ulong seed) => Interface.species_get_random_gender(Handle, seed);
}
}

View File

@ -1,52 +1,26 @@
using System;
using JetBrains.Annotations;
using PkmnLibSharp.FFI;
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 StaticStatisticSet<T> : HandleType 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(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
protected StaticStatisticSet(FFIHandle handle) : base(handle)
{
}
public StaticStatisticSet(T hp, T attack, T defense, T specialAttack, T specialDefense, T speed)
public static StaticStatisticSet<T> Create(T hp, T attack, T defense, T specialAttack, T specialDefense, T speed)
{
var p = typeof(T) switch
var handle = 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);
return Resolver.Instance.ResolveStaticStatisticSet<T>(handle.Resolve());
}
[PublicAPI]
@ -54,86 +28,67 @@ namespace PkmnLibSharp.StaticData
{
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;
case Statistic.HP when _hp.HasValue:
return _hp.Value;
case Statistic.Attack when _attack.HasValue:
return _attack.Value;
case Statistic.Defense when _defense.HasValue:
return _defense.Value;
case Statistic.SpecialAttack when _specialAttack.HasValue:
return _specialAttack.Value;
case Statistic.SpecialDefense when _specialDefense.HasValue:
return _specialDefense.Value;
case Statistic.Speed when _speed.HasValue:
return _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),
{ } t when t == typeof(ushort) => Interface.static_statistic_set_u16_get_stat(Handle, statistic),
_ => throw new ArgumentOutOfRangeException()
};
switch (statistic)
{
case Statistic.HP:
Cache.HP = (T)p;
_hp = (T)p;
break;
case Statistic.Attack:
Cache.Attack = (T)p;
_attack = (T)p;
break;
case Statistic.Defense:
Cache.Defense = (T)p;
_defense = (T)p;
break;
case Statistic.SpecialAttack:
Cache.SpecialAttack = (T)p;
_specialAttack = (T)p;
break;
case Statistic.SpecialDefense:
Cache.SpecialDefense = (T)p;
_specialDefense = (T)p;
break;
case Statistic.Speed:
Cache.Speed = (T)p;
_speed = (T)p;
break;
}
return (T)p;
}
private T? _hp;
public T HP => GetStatistic(Statistic.HP);
private T? _attack;
public T Attack => GetStatistic(Statistic.Attack);
private T? _defense;
public T Defense => GetStatistic(Statistic.Defense);
private T? _specialAttack;
public T SpecialAttack => GetStatistic(Statistic.SpecialAttack);
private T? _specialDefense;
public T SpecialDefense => GetStatistic(Statistic.SpecialDefense);
private T? _speed;
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);
}
~StaticStatisticSet()
{
Dispose();
}
}
}

View File

@ -7,6 +7,6 @@ namespace PkmnLibSharp.StaticData
Defense = 2,
SpecialAttack = 3,
SpecialDefense = 4,
Speed = 5
Speed = 5,
}
}

View File

@ -1,42 +1,32 @@
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
public class StatisticSet<T> : HandleType where T : struct, IConvertible
{
internal StatisticSet(IdentifiablePointer ptr, bool isOwner) : base(ptr, isOwner)
protected StatisticSet(FFIHandle handle) : base(handle)
{
}
public StatisticSet(T hp, T attack, T defense, T specialAttack, T specialDefense, T speed)
public static StatisticSet<T> Create(T hp, T attack, T defense, T specialAttack, T specialDefense, T speed)
{
var p = typeof(T) switch
var handle = 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);
return Resolver.Instance.ResolveStatisticSet<T>(handle.Resolve());
}
[PublicAPI]
@ -44,12 +34,9 @@ namespace PkmnLibSharp.StaticData
{
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),
{ } t when t == typeof(byte) => Interface.statistic_set_u8_get_stat(Handle, statistic),
{ } t when t == typeof(uint) => Interface.statistic_set_u32_get_stat(Handle, statistic),
{ } t when t == typeof(sbyte) => Interface.statistic_set_i8_get_stat(Handle, statistic),
_ => throw new ArgumentOutOfRangeException()
};
return (T)p;
@ -58,17 +45,12 @@ namespace PkmnLibSharp.StaticData
[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));
if (typeof(T) == typeof(byte)) Interface.statistic_set_u8_set_stat(Handle, statistic, Convert.ToByte(value));
else if (typeof(T) == typeof(uint))
Interface.statistic_set_u32_set_stat(Ptr, statistic, Convert.ToUInt32(value));
Interface.statistic_set_u32_set_stat(Handle, 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));
Interface.statistic_set_i8_set_stat(Handle, statistic, Convert.ToSByte(value));
else throw new ArgumentOutOfRangeException();
}
public T HP
@ -106,23 +88,5 @@ namespace PkmnLibSharp.StaticData
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();
}
}
}

View File

@ -1,70 +0,0 @@
using System;
using PkmnLibSharp.FFI;
namespace PkmnLibSharp.Utils
{
public abstract class BasePointer<TCache> : IDisposable
where TCache : class
{
private IntPtr _ptr;
private ulong _identifier;
private TCache? _cache;
protected BasePointer()
{
}
protected BasePointer(IntPtr ptr, ulong identifier = 0)
{
_ptr = ptr;
_identifier = identifier;
}
protected BasePointer(IdentifiablePointer ptr)
{
_ptr = ptr.Ptr;
_identifier = ptr.Identifier;
}
protected internal void InitializePointer(IntPtr ptr, ulong identifier = 0)
{
_ptr = ptr;
_identifier = identifier;
}
protected internal void InitializePointer(IdentifiablePointer ptr)
{
_ptr = ptr.Ptr;
_identifier = ptr.Identifier;
}
protected internal ulong Identifier => _identifier;
internal virtual IntPtr Ptr => _ptr;
protected abstract TCache CreateCache();
internal TCache Cache
{
get
{
if (_cache == null && _identifier != 0)
{
_cache = CacheHandler.GetCache<TCache>(_identifier, CreateCache);
}
return _cache!;
}
}
protected bool HasCache => _cache != null;
protected abstract void Destructor();
public virtual void Dispose()
{
}
}
}

View File

@ -1,31 +0,0 @@
using System;
using System.Collections.Concurrent;
namespace PkmnLibSharp.Utils
{
internal static class CacheHandler
{
private static readonly ConcurrentDictionary<ulong, object> Caches = new();
internal static T GetCache<T>(ulong id, Func<object> ctor)
{
if (!Caches.TryGetValue(id, out var cache))
{
cache = ctor();
Caches.TryAdd(id, cache);
}
if (cache.GetType() != typeof(T))
{
throw new InvalidCastException(
$"Can't cast from cache data `{cache.GetType().FullName}` to cache data `{typeof(T).FullName}`");
}
return (T)cache;
}
internal static void RemoveCache(ulong id)
{
Caches.TryRemove(id, out _);
}
}
}

View File

@ -5,58 +5,6 @@ using System.Runtime.CompilerServices;
namespace PkmnLibSharp.Utils
{
public sealed class DisposableCachedExternArray<T> : IReadOnlyList<T>, IDisposable
where T: class, IDisposable
{
private readonly T?[] _array;
private readonly Func<ulong, T> _getItem;
public DisposableCachedExternArray(ulong size, Func<ulong, T> getItem)
{
_array = new T?[(int)size];
_getItem = getItem;
}
public IEnumerator<T> GetEnumerator()
{
for (var i = 0; i < Count; i++)
{
yield return this[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => _array.Length;
public T this[int index]
{
get
{
if (index >= _array.Length)
throw new ArgumentOutOfRangeException(
$"Index {index} was outside of the bounds of the external array with length {Count}");
return _array[index] ??= _getItem((ulong)index);
}
}
internal T? GetCachedValue(int i)
{
return _array[i];
}
public void Dispose()
{
foreach (var item in _array)
{
item?.Dispose();
}
}
}
public sealed class CachedExternArray<T> : IReadOnlyList<T>
where T: class
{

View File

@ -1,133 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using PkmnLibSharp.FFI;
[assembly: InternalsVisibleTo("PkmnLibRSharp")]
namespace PkmnLibSharp.Utils
{
public abstract class ExternPointer<TCache> : BasePointer<TCache>
where TCache : class
{
private bool _isOwner;
private bool _isInvalidated;
private bool _isDisposed;
protected ExternPointer()
{
}
protected ExternPointer(IntPtr ptr, bool isOwner)
{
InitializePointer(ptr, isOwner);
}
protected ExternPointer(IdentifiablePointer ptr, bool isOwner) : base(ptr)
{
_isOwner = isOwner;
}
protected void InitializePointer(IntPtr ptr, bool isOwner)
{
InitializePointer(ptr);
_isOwner = isOwner;
}
protected void InitializePointer(IdentifiablePointer ptr, bool isOwner)
{
InitializePointer(ptr);
_isOwner = isOwner;
}
internal override IntPtr Ptr
{
get
{
if (_isInvalidated)
{
throw new Exception("Pointer was used after invalidate");
}
return base.Ptr;
}
}
private IntPtr TakeOwnership()
{
if (!_isOwner)
{
throw new Exception("Tried to take ownership of a non-owned object");
}
_isOwner = false;
return Ptr;
}
internal IntPtr TakeOwnershipAndInvalidate()
{
var ptr = TakeOwnership();
Invalidate();
return ptr;
}
internal void Invalidate()
{
_isInvalidated = true;
CacheHandler.RemoveCache(Identifier);
InvalidateChildren();
}
public virtual void InvalidateChildren(){}
public override void Dispose()
{
if (_isDisposed)
return;
if (_isOwner)
{
InvalidateChildren();
if (!_isInvalidated)
Destructor();
_isOwner = false;
CacheHandler.RemoveCache(Identifier);
}
_isDisposed = true;
_isInvalidated = true;
GC.SuppressFinalize(this);
}
public override bool Equals(object obj)
{
if (obj is ExternPointer<TCache> other)
{
return Identifier == other.Identifier;
}
return false;
}
protected bool Equals(ExternPointer<TCache> other)
{
if (Identifier != 0)
{
return Identifier == other.Identifier;
}
return Ptr == other.Ptr;
}
public override int GetHashCode()
{
if (Identifier != 0)
return (int)Identifier;
return (int)Ptr;
}
public static bool operator ==(ExternPointer<TCache>? left, ExternPointer<TCache>? right)
{
return Equals(left, right);
}
public static bool operator !=(ExternPointer<TCache>? left, ExternPointer<TCache>? right)
{
return !Equals(left, right);
}
}
}

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using PkmnLibSharp.FFI;
namespace PkmnLibSharp.Utils
{
internal static class FFIHandleHandler
{
private static Dictionary<ulong, WeakReference> _handleReferences = new();
internal static void ReleaseHandle(ulong handle)
{
ffi_release_handle(handle);
_handleReferences.Remove(handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FFIHandle.FFIHandleReference GetHandleReference(ulong handle)
{
if (_handleReferences.TryGetValue(handle, out var weakReference))
{
if (weakReference.IsAlive)
return (FFIHandle.FFIHandleReference)weakReference.Target;
}
var reference = new FFIHandle.FFIHandleReference(handle);
_handleReferences[handle] = new WeakReference(reference);
return reference;
}
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern void ffi_release_handle(ulong handle);
}
internal struct FFIHandleValue
{
public static implicit operator FFIHandleValue(FFIHandle handle)
{
return new FFIHandleValue(handle.Handle);
}
public FFIHandleValue()
{
}
private FFIHandleValue(ulong handle)
{
Handle = handle;
}
public ulong Handle { get; }
public bool IsNull => Handle == 0;
public FFIHandle Resolve()
{
if (Handle == 0)
throw new NullReferenceException("Handle is null");
return new FFIHandle(Handle, FFIHandleHandler.GetHandleReference(Handle));
}
}
public struct FFIHandle
{
internal FFIHandle(ulong handle, FFIHandleReference handleReference)
{
Handle = handle;
_handleReference = handleReference;
}
public ulong Handle { get; }
public static FFIHandle Zero => new(0, null!);
/// <summary>
/// This is only here for reference counting purposes. As soon as every handle is released, the FFIHandleReference
/// will be garbage collected, and the finalizer will be called, which will release the handle, signifying to
/// PkmnLib that we no longer need the handle.
/// </summary>
// ReSharper disable once NotAccessedField.Local
private readonly FFIHandleReference _handleReference;
internal class FFIHandleReference
{
private readonly ulong _handle;
public FFIHandleReference(ulong handle)
{
_handle = handle;
}
~FFIHandleReference()
{
FFIHandleHandler.ReleaseHandle(this._handle);
}
}
}
}

View File

@ -0,0 +1,12 @@
namespace PkmnLibSharp.Utils
{
public abstract class HandleType
{
protected internal HandleType(FFIHandle handle)
{
Handle = handle;
}
protected internal FFIHandle Handle { get; }
}
}

View File

@ -1,39 +0,0 @@
using System;
namespace PkmnLibSharp.Utils
{
public struct Optional<T> : IDisposable
where T: IDisposable
{
public T? Value;
public bool HasValue;
private Optional(T? value, bool hasValue)
{
Value = value;
HasValue = hasValue;
}
public static implicit operator Optional<T>(T value)
{
return new Optional<T>(value, true);
}
public static implicit operator T(Optional<T> value)
{
return value.Value!;
}
public static Optional<T> None()
{
return new Optional<T>(default, false);
}
public void Dispose()
{
#pragma warning disable IDISP007
Value?.Dispose();
#pragma warning restore IDISP007
}
}
}

View File

@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using PkmnLibSharp.DynamicData;
using PkmnLibSharp.DynamicData.Libraries;
using PkmnLibSharp.FFI;
using PkmnLibSharp.StaticData;
using PkmnLibSharp.StaticData.Libraries;
namespace PkmnLibSharp.Utils
{
public class Resolver
{
public static Resolver Instance { get; set; } = new();
private static readonly Dictionary<FFIHandle, object> Instances = new();
protected static T GetOrCreateInstance<T>(FFIHandle handle)
{
if (!Instances.TryGetValue(handle, out var instance))
{
instance = typeof(T)
.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null,
new[] { typeof(FFIHandle) }, null)
?.Invoke(new object[] { handle });
Instances.Add(handle, instance!);
}
return (T)instance!;
}
public virtual EffectParameter ResolveEffectParameter(FFIHandle handle) =>
GetOrCreateInstance<EffectParameter>(handle);
public virtual Ability ResolveAbility(FFIHandle handle) => GetOrCreateInstance<Ability>(handle);
public virtual Form ResolveForm(FFIHandle handle) => GetOrCreateInstance<Form>(handle);
public virtual StaticStatisticSet<T> ResolveStaticStatisticSet<T>(FFIHandle handle)
where T : struct, IConvertible =>
GetOrCreateInstance<StaticStatisticSet<T>>(handle);
public virtual StatisticSet<T> ResolveStatisticSet<T>(FFIHandle handle) where T : struct, IConvertible =>
GetOrCreateInstance<StatisticSet<T>>(handle);
public virtual LearnableMoves ResolveLearnableMoves(FFIHandle handle) =>
GetOrCreateInstance<LearnableMoves>(handle);
public virtual Item ResolveItem(FFIHandle handle) => GetOrCreateInstance<Item>(handle);
public virtual SecondaryEffect ResolveSecondaryEffect(FFIHandle handle) =>
GetOrCreateInstance<SecondaryEffect>(handle);
public virtual MoveData ResolveMoveData(FFIHandle handle) => GetOrCreateInstance<MoveData>(handle);
public virtual Nature ResolveNature(FFIHandle handle) => GetOrCreateInstance<Nature>(handle);
public virtual Species ResolveSpecies(FFIHandle handle) => GetOrCreateInstance<Species>(handle);
public virtual AbilityLibrary ResolveAbilityLibrary(FFIHandle handle) =>
GetOrCreateInstance<AbilityLibrary>(handle);
public virtual ItemLibrary ResolveItemLibrary(FFIHandle handle) => GetOrCreateInstance<ItemLibrary>(handle);
public virtual LookupGrowthRate ResolveLookupGrowthRate(FFIHandle ptr) =>
GetOrCreateInstance<LookupGrowthRate>(ptr);
public virtual GrowthRateLibrary ResolveGrowthRateLibrary(FFIHandle handle) =>
GetOrCreateInstance<GrowthRateLibrary>(handle);
public virtual MoveLibrary ResolveMoveLibrary(FFIHandle handle) => GetOrCreateInstance<MoveLibrary>(handle);
public virtual NatureLibrary ResolveNatureLibrary(FFIHandle handle) =>
GetOrCreateInstance<NatureLibrary>(handle);
public virtual SpeciesLibrary ResolveSpeciesLibrary(FFIHandle handle) =>
GetOrCreateInstance<SpeciesLibrary>(handle);
public virtual LibrarySettings ResolveLibrarySettings(FFIHandle handle) =>
GetOrCreateInstance<LibrarySettings>(handle);
public virtual TypeLibrary ResolveTypeLibrary(FFIHandle handle) => GetOrCreateInstance<TypeLibrary>(handle);
public virtual StaticData.Libraries.StaticData ResolveStaticData(FFIHandle handle) =>
GetOrCreateInstance<StaticData.Libraries.StaticData>(handle);
public virtual LearnedMove ResolveLearnedMove(FFIHandle resolve) => GetOrCreateInstance<LearnedMove>(resolve);
public virtual Gen7BattleStatCalculator ResolveGen7BattleStatCalculator(FFIHandle handle) =>
GetOrCreateInstance<Gen7BattleStatCalculator>(handle);
public virtual Gen7DamageLibrary ResolveGen7DamageLibrary(FFIHandle handle) =>
GetOrCreateInstance<Gen7DamageLibrary>(handle);
public virtual Gen7MiscLibrary ResolveGen7MiscLibrary(FFIHandle handle) =>
GetOrCreateInstance<Gen7MiscLibrary>(handle);
public virtual EmptyScriptResolver ResolveEmptyScriptResolver(FFIHandle handle) =>
GetOrCreateInstance<EmptyScriptResolver>(handle);
#if WASM
public virtual WasmScriptResolver ResolveWasmScriptResolver(FFIHandle handle) =>
GetOrCreateInstance<WasmScriptResolver>(handle);
#endif
public DynamicLibrary ResolveDynamicLibrary(FFIHandle resolve) => GetOrCreateInstance<DynamicLibrary>(resolve);
public Pokemon ResolvePokemon(FFIHandle resolve) => GetOrCreateInstance<Pokemon>(resolve);
}
}

BIN
PkmnLibRSharp/libpkmn_lib.so (Stored with Git LFS)

Binary file not shown.

View File

@ -10,27 +10,27 @@ namespace PkmnLibRSharpTests.DynamicData
[Test]
public void LearnedMoveMoveData()
{
using var moveData = new MoveData("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
var moveData = MoveData.Create("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
MoveTarget.All, 0, null, Array.Empty<string>());
using var learnedMove = new LearnedMove(moveData, MoveLearnMethod.Level);
var learnedMove = LearnedMove.Create(moveData, MoveLearnMethod.Level);
Assert.AreEqual("foo", learnedMove.MoveData.Name);
}
[Test]
public void LearnedMoveMaxPP()
{
using var moveData = new MoveData("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
var moveData = MoveData.Create("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
MoveTarget.All, 0, null, Array.Empty<string>());
using var learnedMove = new LearnedMove(moveData, MoveLearnMethod.Level);
var learnedMove = LearnedMove.Create(moveData, MoveLearnMethod.Level);
Assert.AreEqual(30, learnedMove.MaxPP);
}
[Test]
public void LearnedMoveRemainingPP()
{
using var moveData = new MoveData("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
var moveData = MoveData.Create("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
MoveTarget.All, 0, null, Array.Empty<string>());
using var learnedMove = new LearnedMove(moveData, MoveLearnMethod.Level);
var learnedMove = LearnedMove.Create(moveData, MoveLearnMethod.Level);
Assert.IsTrue(learnedMove.TryUse(1));
Assert.AreEqual(29, learnedMove.RemainingPP);
}
@ -38,18 +38,18 @@ namespace PkmnLibRSharpTests.DynamicData
[Test]
public void LearnedMoveLearnMethod()
{
using var moveData = new MoveData("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
var moveData = MoveData.Create("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
MoveTarget.All, 0, null, Array.Empty<string>());
using var learnedMove = new LearnedMove(moveData, MoveLearnMethod.Level);
var learnedMove = LearnedMove.Create(moveData, MoveLearnMethod.Level);
Assert.AreEqual(MoveLearnMethod.Level, learnedMove.LearnMethod);
}
[Test]
public void LearnedMoveRestoreAllUses()
{
using var moveData = new MoveData("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
var moveData = MoveData.Create("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
MoveTarget.All, 0, null, Array.Empty<string>());
using var learnedMove = new LearnedMove(moveData, MoveLearnMethod.Level);
var learnedMove = LearnedMove.Create(moveData, MoveLearnMethod.Level);
Assert.IsTrue(learnedMove.TryUse(15));
Assert.AreEqual(15, learnedMove.RemainingPP);
learnedMove.RestoreAllUses();
@ -59,9 +59,9 @@ namespace PkmnLibRSharpTests.DynamicData
[Test]
public void LearnedMoveRestoreUses()
{
using var moveData = new MoveData("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
var moveData = MoveData.Create("foo", new TypeIdentifier(0), MoveCategory.Physical, 100, 20, 30,
MoveTarget.All, 0, null, Array.Empty<string>());
using var learnedMove = new LearnedMove(moveData, MoveLearnMethod.Level);
var learnedMove = LearnedMove.Create(moveData, MoveLearnMethod.Level);
Assert.IsTrue(learnedMove.TryUse(15));
Assert.AreEqual(15, learnedMove.RemainingPP);
learnedMove.RestoreUses(5);

View File

@ -12,24 +12,24 @@ namespace PkmnLibRSharpTests.DynamicData
[Test]
public void Pokemon_GetLibrary()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
Assert.AreEqual(library, pokemon.Library);
}
[Test]
public void Pokemon_GetSpecies()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
Assert.AreEqual(library.StaticData.SpeciesLibrary["testSpecies"], pokemon.Species);
}
[Test]
public void Pokemon_GetForm()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
Assert.AreEqual(library.StaticData.SpeciesLibrary["testSpecies"].DefaultForm, pokemon.Form);
}
@ -37,32 +37,32 @@ namespace PkmnLibRSharpTests.DynamicData
[Test]
public void Pokemon_GetDisplaySpecies()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
Assert.AreEqual(library.StaticData.SpeciesLibrary["testSpecies"], pokemon.Species);
}
[Test]
public void Pokemon_GetDisplayForm()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
Assert.AreEqual(library.StaticData.SpeciesLibrary["testSpecies"].DefaultForm, pokemon.Form);
}
[Test]
public void Pokemon_GetLevel()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
Assert.AreEqual(100, pokemon.Level);
}
[Test]
public void Pokemon_GetExperience()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).Build();
Assert.AreEqual(library.StaticData.GrowthRateLibrary.CalculateExperience("testGrowthrate", 100),
pokemon.Experience);
}
@ -70,60 +70,59 @@ namespace PkmnLibRSharpTests.DynamicData
[Test]
public void Pokemon_GetUniqueIdentifier()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).WithIdentifier(1000).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).WithIdentifier(1000).Build();
Assert.AreEqual(1000, pokemon.UniqueIdentifier);
}
[Test]
public void Pokemon_GetGender()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).WithGender(Gender.Male).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).WithGender(Gender.Male).Build();
Assert.AreEqual(Gender.Male, pokemon.Gender);
}
[Test]
public void Pokemon_GetColoring()
{
using var library = GetLibrary();
using var pokemon = new PokemonBuilder(library, "testSpecies", 100).ForceShiny(true).Build();
var library = GetLibrary();
var pokemon = new PokemonBuilder(library, "testSpecies", 100).ForceShiny(true).Build();
Assert.AreEqual(1, pokemon.Coloring);
}
private static DynamicLibrary GetLibrary()
{
using var settings = new LibrarySettings(100, 4096);
using var speciesLibrary = new SpeciesLibrary(0);
var settings = LibrarySettings.Create(100, 4096);
var speciesLibrary = SpeciesLibrary.Create(0);
FillSpeciesLibrary(speciesLibrary);
using var moves = new MoveLibrary(0);
using var items = new ItemLibrary(0);
using var growthRates = new GrowthRateLibrary(0);
using var gr = CommonGrowthRates.Erratic(100);
var moves = MoveLibrary.Create(0);
var items = ItemLibrary.Create(0);
var growthRates = GrowthRateLibrary.Create(0);
var gr = CommonGrowthRates.Erratic(100);
growthRates.AddGrowthRate("testGrowthrate", gr);
using var types = new TypeLibrary(0);
using var natures = new NatureLibrary(0);
var types = TypeLibrary.Create(0);
var natures = NatureLibrary.Create(0);
natures.LoadNature("testNature", Nature.NeutralNature());
using var abilities = new AbilityLibrary(0);
using var library = new PkmnLibSharp.StaticData.Libraries.StaticData(settings, speciesLibrary, moves, items,
var abilities = AbilityLibrary.Create(0);
var library = PkmnLibSharp.StaticData.Libraries.StaticData.Create(settings, speciesLibrary, moves, items,
growthRates, types, natures, abilities);
using var statCalc = new Gen7BattleStatCalculator();
using var damageLib = new Gen7DamageLibrary(false);
using var miscLib = new Gen7MiscLibrary();
using var scriptResolver = new WasmScriptResolver();
var statCalc = Gen7BattleStatCalculator.Create();
var damageLib = Gen7DamageLibrary.Create(false);
var miscLib = Gen7MiscLibrary.Create();
var scriptResolver = WasmScriptResolver.Create();
return new DynamicLibrary(library, statCalc, damageLib, miscLib, scriptResolver);
return DynamicLibrary.Create(library, statCalc, damageLib, miscLib, scriptResolver);
}
private static void FillSpeciesLibrary(SpeciesLibrary speciesLibrary)
{
using var stats = new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0);
using var moves = new LearnableMoves();
using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
var stats = StaticStatisticSet<ushort>.Create(5, 10, 30, 20, 2, 0);
var moves = LearnableMoves.Create();
var form = Form.Create("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
new[] { "foo", "bar" }, new[] { "set" }, moves, Array.Empty<string>());
using var species =
new Species(10, "testSpecies", 0.2f, "testGrowthrate", 120, form, Array.Empty<string>());
var species = Species.Create(10, "testSpecies", 0.2f, "testGrowthrate", 120, form, Array.Empty<string>());
speciesLibrary.Add(species.Name, species);
}
}

View File

@ -9,22 +9,22 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void AbilityGetName([Values("foo", "bar", "", "zet", "xy")] string name)
{
using var ability = new Ability(name, "", Array.Empty<EffectParameter>());
var ability = Ability.Create(name, "", Array.Empty<EffectParameter>());
Assert.AreEqual(name, ability.Name);
}
[Test]
public void AbilityGetEffect([Values("foo", "bar", "", "zet", "xy")] string effect)
{
using var ability = new Ability("", effect, Array.Empty<EffectParameter>());
var ability = Ability.Create("", effect, Array.Empty<EffectParameter>());
Assert.AreEqual(effect, ability.Effect);
}
[Test]
public void AbilityGetParameters()
{
using var ability = new Ability("", "",
new EffectParameter[] { new(100), new(false), new("foobar"), new(true) });
var ability = Ability.Create("", "",
new EffectParameter[] { 100, false, "foobar", true });
Assert.AreEqual(4, ability.Parameters.Count);
var p1 = ability.Parameters[0];
Assert.AreEqual(100, p1.Data);

View File

@ -8,7 +8,7 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void BoolParameter([Values(true, false)] bool value)
{
using var parameter = new EffectParameter(value);
var parameter = EffectParameter.FromBool(value);
Assert.AreEqual(EffectParameter.ParameterType.Bool, parameter.Type);
Assert.AreEqual(value, parameter.Data);
}
@ -16,7 +16,7 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void IntParameter([Random(long.MinValue, long.MaxValue, 10)] long value)
{
using var parameter = new EffectParameter(value);
var parameter = EffectParameter.FromLong(value);
Assert.AreEqual(EffectParameter.ParameterType.Int, parameter.Type);
Assert.AreEqual(value, parameter.Data);
}
@ -24,7 +24,7 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void FloatParameter([Random(float.MinValue, float.MaxValue, 10)] float value)
{
using var parameter = new EffectParameter(value);
var parameter = EffectParameter.FromFloat(value);
Assert.AreEqual(EffectParameter.ParameterType.Float, parameter.Type);
Assert.AreEqual(value, parameter.Data);
}
@ -32,7 +32,7 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void StringParameter([Values("foo", "bar", "zet", "x", "")] string value)
{
using var parameter = new EffectParameter(value);
var parameter = EffectParameter.FromString(value);
Assert.AreEqual(EffectParameter.ParameterType.String, parameter.Type);
Assert.AreEqual(value, parameter.Data);
}

View File

@ -9,9 +9,9 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void BasicTests()
{
using var stats = new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0);
using var moves = new LearnableMoves();
using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
var stats = StaticStatisticSet<ushort>.Create(5, 10, 30, 20, 2, 0);
var moves = LearnableMoves.Create();
var form = Form.Create("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
new[] { "foo", "bar" }, new[] { "set" }, moves, Array.Empty<string>());
Assert.AreEqual("foobar", form.Name);
Assert.AreEqual(0.2f, form.Height, 0.00001f);

View File

@ -1,4 +1,5 @@
using NUnit.Framework;
using PkmnLibSharp.FFI;
using PkmnLibSharp.StaticData;
namespace PkmnLibRSharpTests.StaticData
@ -8,13 +9,13 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void CreateAndDropLookupGrowthRate()
{
using var growthRate = new LookupGrowthRate(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
var growthRate = LookupGrowthRate.Create(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
}
[Test]
public void LookupGrowthRateCalculateLevel()
{
using var growthRate = new LookupGrowthRate(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
var growthRate = LookupGrowthRate.Create(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
Assert.AreEqual(1, growthRate.CalculateLevel(0));
Assert.AreEqual(2, growthRate.CalculateLevel(1));
Assert.AreEqual(2, growthRate.CalculateLevel(2));
@ -24,11 +25,15 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void LookupGrowthRateCalculateExperience()
{
using var growthRate = new LookupGrowthRate(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
var growthRate = LookupGrowthRate.Create(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
Assert.AreEqual(0, growthRate.CalculateExperience(1));
Assert.AreEqual(1, growthRate.CalculateExperience(2));
Assert.AreEqual(5, growthRate.CalculateExperience(3));
Assert.AreEqual(10, growthRate.CalculateExperience(4));
Assert.Throws<PkmnLibException>(() =>
{
growthRate.CalculateExperience(0);
}, "Level must be greater than 0, but was 0");
}
}

View File

@ -9,7 +9,7 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void BasicTests()
{
using var item = new Item("foobar", ItemCategory.Mail, BattleItemCategory.StatusHealing, 500,
var item = Item.Create("foobar", ItemCategory.Mail, BattleItemCategory.StatusHealing, 500,
Array.Empty<string>());
Assert.AreEqual("foobar", item.Name);
Assert.AreEqual(ItemCategory.Mail, item.Category);
@ -20,7 +20,7 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void FlagTests()
{
using var item = new Item("foobar", ItemCategory.Mail, BattleItemCategory.StatusHealing, 500,
var item = Item.Create("foobar", ItemCategory.Mail, BattleItemCategory.StatusHealing, 500,
new []{"foo", "zet"});
Assert.That(item.HasFlag("foo"));
Assert.That(item.HasFlag("zet"));

View File

@ -10,16 +10,16 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void Create()
{
using var lib = new AbilityLibrary(0);
var lib = AbilityLibrary.Create(0);
Assert.AreEqual(0, lib.Count);
}
[Test]
public void CreateAndAdd()
{
using var lib = new AbilityLibrary(1);
var lib = AbilityLibrary.Create(0);
Assert.AreEqual(0, lib.Count);
using var ability = new Ability("testAbility", "effect", Array.Empty<EffectParameter>());
var ability = Ability.Create("testAbility", "effect", Array.Empty<EffectParameter>());
lib.Add("foobar", ability);
Assert.AreEqual(1, lib.Count);
Assert.AreEqual("testAbility", lib["foobar"].Name);
@ -28,11 +28,11 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CreateAddIterate()
{
using var lib = new AbilityLibrary(10);
var lib = AbilityLibrary.Create(0);
Assert.AreEqual(0, lib.Count);
for (ushort i = 0; i < 10; i++)
{
using var item = new Ability(i.ToString(), "effect", Array.Empty<EffectParameter>());
var item = Ability.Create(i.ToString(), "effect", Array.Empty<EffectParameter>());
lib.Add(i + "_key", item);
}

View File

@ -17,7 +17,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[TestCase(100, 600_000)]
public void ErraticExperience(byte level, int experience)
{
using var growthRate = CommonGrowthRates.Erratic(100);
var growthRate = CommonGrowthRates.Erratic(100);
Assert.AreEqual(experience, growthRate.CalculateExperience(level));
}
@ -31,7 +31,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[TestCase(100, 800_000)]
public void FastExperience(byte level, int experience)
{
using var growthRate = CommonGrowthRates.Fast(100);
var growthRate = CommonGrowthRates.Fast(100);
Assert.AreEqual(experience, growthRate.CalculateExperience(level));
}
@ -45,7 +45,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[TestCase(100, 1_000_000)]
public void MediumFastExperience(byte level, int experience)
{
using var growthRate = CommonGrowthRates.MediumFast(100);
var growthRate = CommonGrowthRates.MediumFast(100);
Assert.AreEqual(experience, growthRate.CalculateExperience(level));
}
@ -59,7 +59,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[TestCase(100, 1_059_860)]
public void MediumSlowExperience(byte level, int experience)
{
using var growthRate = CommonGrowthRates.MediumSlow(100);
var growthRate = CommonGrowthRates.MediumSlow(100);
Assert.AreEqual(experience, growthRate.CalculateExperience(level));
}
@ -73,7 +73,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[TestCase(100, 1_250_000)]
public void SlowExperience(byte level, int experience)
{
using var growthRate = CommonGrowthRates.Slow(100);
var growthRate = CommonGrowthRates.Slow(100);
Assert.AreEqual(experience, growthRate.CalculateExperience(level));
}
@ -87,7 +87,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[TestCase(100, 1_640_000)]
public void FluctuatingExperience(byte level, int experience)
{
using var growthRate = CommonGrowthRates.Fluctuating(100);
var growthRate = CommonGrowthRates.Fluctuating(100);
Assert.AreEqual(experience, growthRate.CalculateExperience(level));
}
}

View File

@ -9,22 +9,22 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CreateGrowthRateLibrary()
{
using var library = new GrowthRateLibrary(0);
var library = GrowthRateLibrary.Create(0);
}
[Test]
public void AddGrowthRateToLibrary()
{
using var library = new GrowthRateLibrary(0);
using var growthRate = new LookupGrowthRate(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
var library = GrowthRateLibrary.Create(0);
var growthRate = LookupGrowthRate.Create(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
library.AddGrowthRate("foobar", growthRate);
}
[Test]
public void CalculateLevel()
{
using var library = new GrowthRateLibrary(0);
using var growthRate = new LookupGrowthRate(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
var library = GrowthRateLibrary.Create(0);
var growthRate = LookupGrowthRate.Create(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
library.AddGrowthRate("foobar", growthRate);
var level = library.CalculateLevel("foobar", 20);
Assert.AreEqual(5, level);
@ -33,8 +33,8 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CalculateExperience()
{
using var library = new GrowthRateLibrary(0);
using var growthRate = new LookupGrowthRate(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
var library = GrowthRateLibrary.Create(0);
var growthRate = LookupGrowthRate.Create(new uint[] { 0, 1, 5, 10, 20, 100, 200, 500 });
library.AddGrowthRate("foobar", growthRate);
var experience = library.CalculateExperience("foobar", 5);
Assert.AreEqual(20, experience);

View File

@ -10,16 +10,16 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void Create()
{
using var lib = new ItemLibrary(0);
var lib = ItemLibrary.Create(0);
Assert.AreEqual(0, lib.Count);
}
[Test]
public void CreateAndAdd()
{
using var lib = new ItemLibrary(1);
var lib = ItemLibrary.Create(1);
Assert.AreEqual(0, lib.Count);
using var item = new Item("testItem", ItemCategory.Mail, BattleItemCategory.None, 100, Array.Empty<string>());
var item = Item.Create("testItem", ItemCategory.Mail, BattleItemCategory.None, 100, Array.Empty<string>());
lib.Add("foobar", item);
Assert.AreEqual(1, lib.Count);
Assert.AreEqual("testItem", lib["foobar"].Name);
@ -28,11 +28,11 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CreateAddIterate()
{
using var lib = new ItemLibrary(10);
var lib = ItemLibrary.Create(10);
Assert.AreEqual(0, lib.Count);
for (ushort i = 0; i < 10; i++)
{
using var item = new Item(i.ToString(), ItemCategory.Mail, BattleItemCategory.None, 100,
var item = Item.Create(i.ToString(), ItemCategory.Mail, BattleItemCategory.None, 100,
Array.Empty<string>());
lib.Add(i + "_key", item);
}

View File

@ -8,14 +8,21 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CreateLibrarySettings()
{
using var settings = new LibrarySettings(100, 4096);
var _ = LibrarySettings.Create(100, 4096);
}
[Test]
public void GetMaxLevel()
{
using var settings = new LibrarySettings(100, 4096);
var settings = LibrarySettings.Create(100, 4096);
Assert.AreEqual(100, settings.MaxLevel);
}
[Test]
public void GetShinyRate()
{
var settings = LibrarySettings.Create(100, 4096);
Assert.AreEqual(4096, settings.ShinyRate);
}
}
}

View File

@ -10,16 +10,16 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void Create()
{
using var lib = new MoveLibrary(0);
var lib = MoveLibrary.Create(0);
Assert.AreEqual(0, lib.Count);
}
[Test]
public void CreateAndAdd()
{
using var lib = new MoveLibrary(1);
var lib = MoveLibrary.Create(1);
Assert.AreEqual(0, lib.Count);
using var move = new MoveData("testMove", new TypeIdentifier(0), MoveCategory.Physical, 100, 100, 1,
var move = MoveData.Create("testMove", new TypeIdentifier(0), MoveCategory.Physical, 100, 100, 1,
MoveTarget.All, 0, null, Array.Empty<string>());
lib.Add("foobar", move);
Assert.AreEqual(1, lib.Count);
@ -29,11 +29,11 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CreateAddIterate()
{
using var lib = new MoveLibrary(10);
var lib = MoveLibrary.Create(10);
Assert.AreEqual(0, lib.Count);
for (ushort i = 0; i < 10; i++)
{
using var move = new MoveData(i.ToString(), new TypeIdentifier(0), MoveCategory.Physical, 100, 100, 1,
var move = MoveData.Create(i.ToString(), new TypeIdentifier(0), MoveCategory.Physical, 100, 100, 1,
MoveTarget.All, 0, null, Array.Empty<string>());
lib.Add(i + "_key", move);
}

View File

@ -9,15 +9,15 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CreateNatureLibrary()
{
using var library = new NatureLibrary(0);
var _ = NatureLibrary.Create(0);
}
[Test]
public void LoadNatures()
{
using var library = new NatureLibrary(0);
using var nature1 = Nature.NeutralNature();
using var nature2 = new Nature(Statistic.Attack, Statistic.Defense);
var library = NatureLibrary.Create(0);
var nature1 = Nature.NeutralNature();
var nature2 = Nature.Create(Statistic.Attack, Statistic.Defense);
library.LoadNature("foo", nature1);
library.LoadNature("bar", nature2);
}
@ -25,56 +25,41 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void LoadAndGetNature()
{
using var library = new NatureLibrary(0);
using var nature1 = new Nature(Statistic.Attack, Statistic.Defense);
var library = NatureLibrary.Create(0);
var nature1 = Nature.Create(Statistic.Attack, Statistic.Defense);
library.LoadNature("foo", nature1);
using (var n = library.TryGetNature("foo"))
{
Assert.AreEqual(Statistic.Attack, n.Value!.IncreasedStat);
}
Assert.That(library.TryGetNature("foo", out var n));
Assert.AreEqual(Statistic.Attack, n!.IncreasedStat);
}
[Test]
public void LoadAndGetNatureTwice()
{
using var library = new NatureLibrary(0);
using var nature1 = new Nature(Statistic.Attack, Statistic.Defense);
var library = NatureLibrary.Create(0);
var nature1 = Nature.Create(Statistic.Attack, Statistic.Defense);
library.LoadNature("foo", nature1);
using (var n = library.TryGetNature("foo"))
{
Assert.That(n.HasValue);
Assert.AreEqual(Statistic.Attack, n.Value!.IncreasedStat);
}
using (var n = library.TryGetNature("foo"))
{
Assert.That(n.HasValue);
Assert.AreEqual(Statistic.Attack, n.Value!.IncreasedStat);
}
Assert.That(library.TryGetNature("foo", out var n));
Assert.AreEqual(Statistic.Attack, n!.IncreasedStat);
Assert.That(library.TryGetNature("foo", out var n2));
Assert.AreEqual(Statistic.Attack, n2!.IncreasedStat);
}
[Test]
public void GetUnknownNature()
{
using var library = new NatureLibrary(0);
using var nature1 = new Nature(Statistic.Attack, Statistic.Defense);
using var n = library.TryGetNature("foo");
Assert.False(n.HasValue);
var library = NatureLibrary.Create(0);
Assert.IsFalse(library.TryGetNature("foo", out _));
}
[Test]
public void GetNatureName()
{
using var library = new NatureLibrary(0);
using var nature1 = new Nature(Statistic.Attack, Statistic.Defense);
var library = NatureLibrary.Create(0);
var nature1 = Nature.Create(Statistic.Attack, Statistic.Defense);
library.LoadNature("foo", nature1);
using (var n = library.TryGetNature("foo"))
{
Assert.AreEqual("foo", library.GetNatureName(n));
}
Assert.That(library.TryGetNature("foo", out var n));
Assert.AreEqual("foo", library.GetNatureName(n!));
}
}

View File

@ -10,20 +10,20 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void Create()
{
using var lib = new SpeciesLibrary(0);
var lib = SpeciesLibrary.Create(0);
Assert.AreEqual(0, lib.Count);
}
[Test]
public void CreateAndAdd()
{
using var lib = new SpeciesLibrary(1);
var lib = SpeciesLibrary.Create(1);
Assert.AreEqual(0, lib.Count);
using var stats = new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0);
using var moves = new LearnableMoves();
using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
var stats = StaticStatisticSet<ushort>.Create(5, 10, 30, 20, 2, 0);
var moves = LearnableMoves.Create();
var form = Form.Create("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
new[] { "foo", "bar" }, new[] { "set" }, moves, Array.Empty<string>());
using var species = new Species(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>());
var species = Species.Create(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>());
lib.Add("foobar", species);
Assert.AreEqual(1, lib.Count);
Assert.AreEqual("testSpecies", lib["foobar"].Name);
@ -32,15 +32,15 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CreateAddIterate()
{
using var lib = new SpeciesLibrary(1);
var lib = SpeciesLibrary.Create(1);
Assert.AreEqual(0, lib.Count);
for (ushort i = 0; i < 10; i++)
{
using var stats = new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0);
using var moves = new LearnableMoves();
using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
var stats = StaticStatisticSet<ushort>.Create(5, 10, 30, 20, 2, 0);
var moves = LearnableMoves.Create();
var form = Form.Create("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
new[] { "foo", "bar" }, new[] { "set" }, moves, Array.Empty<string>());
using var species = new Species(i, i.ToString(), 0.2f, "growth", 120, form, Array.Empty<string>());
var species = Species.Create(i, i.ToString(), 0.2f, "growth", 120, form, Array.Empty<string>());
lib.Add(i + "_key", species);
}
Assert.AreEqual(10, lib.Count);

View File

@ -5,79 +5,79 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
{
public class StaticDataTests
{
private PkmnLibSharp.StaticData.Libraries.StaticData Build()
private static PkmnLibSharp.StaticData.Libraries.StaticData Build()
{
using var settings = new LibrarySettings(100, 4096);
using var species = new SpeciesLibrary(0);
using var moves = new MoveLibrary(0);
using var items = new ItemLibrary(0);
using var growthRates = new GrowthRateLibrary(0);
using var types = new TypeLibrary(0);
using var natures = new NatureLibrary(0);
using var abilities = new AbilityLibrary(0);
return new PkmnLibSharp.StaticData.Libraries.StaticData(settings, species, moves, items,
var settings = LibrarySettings.Create(100, 4096);
var species = SpeciesLibrary.Create(0);
var moves = MoveLibrary.Create(0);
var items = ItemLibrary.Create(0);
var growthRates = GrowthRateLibrary.Create(0);
var types = TypeLibrary.Create(0);
var natures = NatureLibrary.Create(0);
var abilities = AbilityLibrary.Create(0);
return PkmnLibSharp.StaticData.Libraries.StaticData.Create(settings, species, moves, items,
growthRates, types, natures, abilities);
}
[Test]
public void CreateNewStaticData()
{
using var _ = Build();
var _ = Build();
}
[Test]
public void GetSettings()
{
using var library = Build();
var library = Build();
var _ = library.LibrarySettings;
}
[Test]
public void GetSpecies()
{
using var library = Build();
var library = Build();
var _ = library.SpeciesLibrary;
}
[Test]
public void GetMoves()
{
using var library = Build();
var library = Build();
var _ = library.MoveLibrary;
}
[Test]
public void GetItems()
{
using var library = Build();
var library = Build();
var _ = library.ItemLibrary;
}
[Test]
public void GetGrowthRates()
{
using var library = Build();
var library = Build();
var _ = library.GrowthRateLibrary;
}
[Test]
public void GetTypeLibrary()
{
using var library = Build();
var library = Build();
var _ = library.TypeLibrary;
}
[Test]
public void GetNatureLibrary()
{
using var library = Build();
var library = Build();
var _ = library.NatureLibrary;
}
[Test]
public void GetAbilityLibrary()
{
using var library = Build();
var library = Build();
var _ = library.AbilityLibrary;
}
}

View File

@ -10,20 +10,20 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void CreateTypeLibrary()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
}
[Test]
public void RegisterType()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
typeLibrary.RegisterType("foobar");
}
[Test]
public void RegisterTypeRetrieve()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
var typeIdentifier1 = typeLibrary.RegisterType("foo");
Assert.AreEqual(typeIdentifier1, new TypeIdentifier(1));
var typeIdentifier2 = typeLibrary.RegisterType("bar");
@ -35,7 +35,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void GetUnknownTypeErrors()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
typeLibrary.RegisterType("foo");
Assert.Throws<KeyNotFoundException>(() =>
{
@ -46,7 +46,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void GetTypeName()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
var typeIdentifier1 = typeLibrary.RegisterType("foo");
Assert.AreEqual("foo", typeLibrary.GetTypeName(typeIdentifier1));
}
@ -54,7 +54,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void GetUnknownTypeNameErrors()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
typeLibrary.RegisterType("foo");
Assert.Throws<KeyNotFoundException>(() =>
{
@ -65,7 +65,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void SetEffectiveness()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
var typeIdentifier1 = typeLibrary.RegisterType("foo");
var typeIdentifier2 = typeLibrary.RegisterType("bar");
typeLibrary.SetEffectiveness(typeIdentifier1, typeIdentifier2, 0.5f);
@ -74,7 +74,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void GetEffectiveness()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
var typeIdentifier1 = typeLibrary.RegisterType("foo");
var typeIdentifier2 = typeLibrary.RegisterType("bar");
typeLibrary.SetEffectiveness(typeIdentifier1, typeIdentifier2, 0.5f);
@ -84,7 +84,7 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
[Test]
public void GetMultipleEffectiveness()
{
using var typeLibrary = new TypeLibrary(0);
var typeLibrary = TypeLibrary.Create(0);
var typeIdentifier1 = typeLibrary.RegisterType("foo");
var typeIdentifier2 = typeLibrary.RegisterType("bar");
typeLibrary.SetEffectiveness(typeIdentifier1, typeIdentifier2, 0.5f);

View File

@ -9,7 +9,7 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void BasicTests()
{
using var move = new MoveData("foobar", new TypeIdentifier(), MoveCategory.Physical, 68, 23, 56,
var move = MoveData.Create("foobar", new TypeIdentifier(), MoveCategory.Physical, 68, 23, 56,
MoveTarget.Any, -3, null, Array.Empty<string>());
Assert.AreEqual("foobar", move.Name);
Assert.AreEqual(MoveCategory.Physical, move.Category);
@ -24,8 +24,8 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void TestMoveWithSecondaryEffect()
{
using var effect = new SecondaryEffect(0.25f, "foobar", Array.Empty<EffectParameter>());
using var move = new MoveData("foobar", new TypeIdentifier(), MoveCategory.Physical, 68, 23, 56,
var effect = SecondaryEffect.Create(0.25f, "foobar", Array.Empty<EffectParameter>());
var move = MoveData.Create("foobar", new TypeIdentifier(), MoveCategory.Physical, 68, 23, 56,
MoveTarget.Any, -3, effect, Array.Empty<string>());
Assert.That(move.SecondaryEffect != null);
Assert.AreEqual("foobar", move.SecondaryEffect!.Name);

View File

@ -11,7 +11,7 @@ namespace PkmnLibRSharpTests.StaticData
[Values] Statistic decreasedStat
)
{
using var nature = new Nature(increasedStat, decreasedStat);
var nature = Nature.Create(increasedStat, decreasedStat);
Assert.AreEqual(increasedStat, nature.IncreasedStat);
Assert.AreEqual(decreasedStat, nature.DecreasedStat);
if (increasedStat == decreasedStat)

View File

@ -12,7 +12,7 @@ namespace PkmnLibRSharpTests.StaticData
[Values("foo", "bar", "", "ssdsdsdsdsd")] string effectName
)
{
using var effect = new SecondaryEffect(chance, effectName, Array.Empty<EffectParameter>());
var effect = SecondaryEffect.Create(chance, effectName, Array.Empty<EffectParameter>());
Assert.AreEqual(effectName, effect.Name);
Assert.AreEqual(chance, effect.Chance);
}

View File

@ -9,11 +9,11 @@ namespace PkmnLibRSharpTests.StaticData
[Test]
public void BasicTests()
{
using var stats = new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0);
using var moves = new LearnableMoves();
using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
var stats = StaticStatisticSet<ushort>.Create(5, 10, 30, 20, 2, 0);
var moves = LearnableMoves.Create();
var form = Form.Create("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
new[] { "foo", "bar" }, new[] { "set" }, moves, Array.Empty<string>());
using var species = new Species(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>());
var species = Species.Create(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>());
Assert.AreEqual(10, species.Id);
Assert.AreEqual("testSpecies", species.Name);
Assert.AreEqual(0.2f, species.GenderRate, 0.00001f);

View File

@ -5,76 +5,11 @@ namespace PkmnLibRSharpTests.StaticData
{
public class StaticStaticStatisticSetTests
{
[Test]
public void ByteStaticStatisticSet([Random(1)] byte hp, [Random(1)] byte attack, [Random(1)] byte defense,
[Random(1)] byte specialAttack, [Random(1)] byte specialDefense, [Random(1)] byte speed)
{
using var set = new StaticStatisticSet<byte>(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
Assert.AreEqual(set.SpecialAttack, specialAttack);
Assert.AreEqual(set.SpecialDefense, specialDefense);
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void UShortStaticStatisticSet([Random(1)] ushort hp, [Random(1)] ushort attack, [Random(1)] ushort defense,
[Random(1)] ushort specialAttack, [Random(1)] ushort specialDefense, [Random(1)] ushort speed)
{
using var set = new StaticStatisticSet<ushort>(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
Assert.AreEqual(set.SpecialAttack, specialAttack);
Assert.AreEqual(set.SpecialDefense, specialDefense);
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void UintStaticStatisticSet([Random(1)] uint hp, [Random(1)] uint attack, [Random(1)] uint defense,
[Random(1)] uint specialAttack, [Random(1)] uint specialDefense, [Random(1)] uint speed)
{
using var set = new StaticStatisticSet<uint>(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
Assert.AreEqual(set.SpecialAttack, specialAttack);
Assert.AreEqual(set.SpecialDefense, specialDefense);
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void SbyteStaticStatisticSet([Random(1)] sbyte hp, [Random(1)] sbyte attack, [Random(1)] sbyte defense,
[Random(1)] sbyte specialAttack, [Random(1)] sbyte specialDefense, [Random(1)] sbyte speed)
{
using var set = new StaticStatisticSet<sbyte>(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
Assert.AreEqual(set.SpecialAttack, specialAttack);
Assert.AreEqual(set.SpecialDefense, specialDefense);
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void ShortStaticStatisticSet([Random(1)] short hp, [Random(1)] short attack, [Random(1)] short defense,
[Random(1)] short specialAttack, [Random(1)] short specialDefense, [Random(1)] short speed)
{
using var set = new StaticStatisticSet<short>(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
Assert.AreEqual(set.SpecialAttack, specialAttack);
Assert.AreEqual(set.SpecialDefense, specialDefense);
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void IntStaticStatisticSet([Random(1)] int hp, [Random(1)] int attack, [Random(1)] int defense,
[Random(1)] int specialAttack, [Random(1)] int specialDefense, [Random(1)] int speed)
{
using var set = new StaticStatisticSet<int>(hp, attack, defense, specialAttack, specialDefense, speed);
var set = StaticStatisticSet<ushort>.Create(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);

View File

@ -9,7 +9,7 @@ namespace PkmnLibRSharpTests.StaticData
public void ByteStatisticSet([Random(1)] byte hp, [Random(1)] byte attack, [Random(1)] byte defense,
[Random(1)] byte specialAttack, [Random(1)] byte specialDefense, [Random(1)] byte speed)
{
using var set = new StatisticSet<byte>(hp, attack, defense, specialAttack, specialDefense, speed);
var set = StatisticSet<byte>.Create(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
@ -22,7 +22,7 @@ namespace PkmnLibRSharpTests.StaticData
public void ByteStatisticSetModifying([Random(1)] byte hp, [Random(1)] byte attack, [Random(1)] byte defense,
[Random(1)] byte specialAttack, [Random(1)] byte specialDefense, [Random(1)] byte speed)
{
using var set = new StatisticSet<byte>(0,0,0,0,0,0);
var set = StatisticSet<byte>.Create(0,0,0,0,0,0);
set.HP = hp;
Assert.AreEqual(set.HP, hp);
set.Attack = attack;
@ -37,24 +37,11 @@ namespace PkmnLibRSharpTests.StaticData
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void UShortStatisticSet([Random(1)] ushort hp, [Random(1)] ushort attack, [Random(1)] ushort defense,
[Random(1)] ushort specialAttack, [Random(1)] ushort specialDefense, [Random(1)] ushort speed)
{
using var set = new StatisticSet<ushort>(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
Assert.AreEqual(set.SpecialAttack, specialAttack);
Assert.AreEqual(set.SpecialDefense, specialDefense);
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void UintStatisticSet([Random(1)] uint hp, [Random(1)] uint attack, [Random(1)] uint defense,
[Random(1)] uint specialAttack, [Random(1)] uint specialDefense, [Random(1)] uint speed)
{
using var set = new StatisticSet<uint>(hp, attack, defense, specialAttack, specialDefense, speed);
var set = StatisticSet<uint>.Create(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
@ -67,33 +54,7 @@ namespace PkmnLibRSharpTests.StaticData
public void SbyteStatisticSet([Random(1)] sbyte hp, [Random(1)] sbyte attack, [Random(1)] sbyte defense,
[Random(1)] sbyte specialAttack, [Random(1)] sbyte specialDefense, [Random(1)] sbyte speed)
{
using var set = new StatisticSet<sbyte>(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
Assert.AreEqual(set.SpecialAttack, specialAttack);
Assert.AreEqual(set.SpecialDefense, specialDefense);
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void ShortStatisticSet([Random(1)] short hp, [Random(1)] short attack, [Random(1)] short defense,
[Random(1)] short specialAttack, [Random(1)] short specialDefense, [Random(1)] short speed)
{
using var set = new StatisticSet<short>(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);
Assert.AreEqual(set.SpecialAttack, specialAttack);
Assert.AreEqual(set.SpecialDefense, specialDefense);
Assert.AreEqual(set.Speed, speed);
}
[Test]
public void IntStatisticSet([Random(1)] int hp, [Random(1)] int attack, [Random(1)] int defense,
[Random(1)] int specialAttack, [Random(1)] int specialDefense, [Random(1)] int speed)
{
using var set = new StatisticSet<int>(hp, attack, defense, specialAttack, specialDefense, speed);
var set = StatisticSet<sbyte>.Create(hp, attack, defense, specialAttack, specialDefense, speed);
Assert.AreEqual(set.HP, hp);
Assert.AreEqual(set.Attack, attack);
Assert.AreEqual(set.Defense, defense);