diff --git a/PkmnLibSharp.sln b/PkmnLibSharp.sln index 3895118..443d8e1 100644 --- a/PkmnLibSharp.sln +++ b/PkmnLibSharp.sln @@ -10,9 +10,9 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4CB6DA3C-017B-4AE0-B889-3DFE6B969CD0}.Debug|Any CPU.ActiveCfg = Release|x86_64 - {4CB6DA3C-017B-4AE0-B889-3DFE6B969CD0}.Debug|Any CPU.Build.0 = Release|x86_64 - {0D15FD33-1AEA-44F4-8211-AA8AF97EA534}.Debug|Any CPU.ActiveCfg = Debug|x86_64 - {0D15FD33-1AEA-44F4-8211-AA8AF97EA534}.Debug|Any CPU.Build.0 = Debug|x86_64 + {4CB6DA3C-017B-4AE0-B889-3DFE6B969CD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4CB6DA3C-017B-4AE0-B889-3DFE6B969CD0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0D15FD33-1AEA-44F4-8211-AA8AF97EA534}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0D15FD33-1AEA-44F4-8211-AA8AF97EA534}.Debug|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection EndGlobal diff --git a/PkmnLibSharp/Battling/LearnedMove.cs b/PkmnLibSharp/Battling/LearnedMove.cs index 7ee2d66..dc00408 100644 --- a/PkmnLibSharp/Battling/LearnedMove.cs +++ b/PkmnLibSharp/Battling/LearnedMove.cs @@ -8,6 +8,7 @@ namespace PkmnLibSharp.Battling { public class LearnedMove : PointerWrapper { + public LearnedMove(){} internal LearnedMove(IntPtr ptr) : base(ptr){} public static LearnedMove Create(MoveData move, byte maxUses, MoveLearnMethod learnMethod) @@ -16,6 +17,26 @@ namespace PkmnLibSharp.Battling LearnedAttack.Construct(ref ptr, move.Ptr, maxUses, (AttackLearnMethod) learnMethod).Assert(); return new LearnedMove(ptr); } + + public MoveData Move + { + get + { + if (_move != null) return _move; + var ptr = LearnedAttack.GetAttack(Ptr); + if (TryResolvePointer(ptr, out _move)) + return _move; + _move = new MoveData(ptr); + return _move; + } + } + + public byte MaxUses => Creaturelibbattling.Generated.LearnedAttack.GetMaxUses(Ptr); + public byte RemainingUses => Creaturelibbattling.Generated.LearnedAttack.GetRemainingUses(Ptr); + public MoveLearnMethod LearnMethod => + (MoveLearnMethod) Creaturelibbattling.Generated.LearnedAttack.GetLearnMethod(Ptr); + + private MoveData _move; protected override void DeletePtr() { diff --git a/PkmnLibSharp/Battling/Pokemon.cs b/PkmnLibSharp/Battling/Pokemon.cs index 6a8eaa8..24257b6 100644 --- a/PkmnLibSharp/Battling/Pokemon.cs +++ b/PkmnLibSharp/Battling/Pokemon.cs @@ -187,15 +187,20 @@ namespace PkmnLibSharp.Battling { return Creaturelibbattling.Generated.Creature.HasVolatileScript(Ptr, scriptName.ToPtr()) == 1; } - - - + public NativePtrArray GetMoves() + { + if (_moves != null) return _moves; + var movesLength = Creaturelibbattling.Generated.Creature.GetAttacksCount(Ptr); + var movesPtr = Creaturelibbattling.Generated.Creature.GetAttacks(Ptr); + _moves = new NativePtrArray(movesPtr, (int) movesLength); + return _moves; + } private Species _species; private Forme _forme; private string _nickname; - + private NativePtrArray _moves; protected override void DeletePtr() { diff --git a/PkmnLibSharp/PkmnLibSharp.csproj b/PkmnLibSharp/PkmnLibSharp.csproj index ebb7a69..d02f9fe 100644 --- a/PkmnLibSharp/PkmnLibSharp.csproj +++ b/PkmnLibSharp/PkmnLibSharp.csproj @@ -2,8 +2,12 @@ netstandard2.1 - Release;Debug - x86_64 + Debug + AnyCPU + + + + true diff --git a/PkmnLibSharp/Utilities/NativePtrArray.cs b/PkmnLibSharp/Utilities/NativePtrArray.cs new file mode 100644 index 0000000..eadf6d5 --- /dev/null +++ b/PkmnLibSharp/Utilities/NativePtrArray.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace PkmnLibSharp.Utilities +{ + public class NativePtrArray : IList where T : PointerWrapper, new() + { + private readonly IntPtr _ptr; + + internal NativePtrArray(IntPtr ptr, int length) + { + _ptr = ptr; + Count = length; + } + + public IEnumerator GetEnumerator() + { + for (var i = 0; i < Count; i++) + { + yield return this[i]; + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void Add(T item) + { + throw new NotImplementedException(); + } + + public void Clear() + { + throw new NotImplementedException(); + } + + public bool Contains(T item) + { + foreach (var v in this) + { + if (item == null) + { + if (v.Ptr == IntPtr.Zero) + return true; + continue; + } + if (item.Ptr == v.Ptr) + return true; + } + return false; + } + + public void CopyTo(T[] array, int arrayIndex) + { + throw new NotImplementedException(); + } + + public bool Remove(T item) + { + throw new NotImplementedException(); + } + + public int Count { get; } + + public bool IsReadOnly => true; + public int IndexOf(T item) + { + for (var i = 0; i < Count; i++) + { + var p = _ptr + (i * IntPtr.Size); + if (item == null) + return i; + if (p == item.Ptr) + return i; + } + return -1; + } + + public void Insert(int index, T item) + { + throw new NotImplementedException(); + } + + public void RemoveAt(int index) + { + throw new NotImplementedException(); + } + + public T this[int index] + { + get + { + unsafe + { + // Where's your god now? + // (We add the offset of the index to the pointer, then dereference the pointer pointer to get the actual pointer.) + var p = new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer()); + if (PointerWrapper.TryResolvePointer(p, out T t)) + return t; + t = new T(); + t.Initialize(p); + return t; + } + } + set => throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/PkmnLibSharp/Utilities/PointerWrapper.cs b/PkmnLibSharp/Utilities/PointerWrapper.cs index fec9331..b9fb3f6 100644 --- a/PkmnLibSharp/Utilities/PointerWrapper.cs +++ b/PkmnLibSharp/Utilities/PointerWrapper.cs @@ -5,7 +5,7 @@ namespace PkmnLibSharp.Utilities { public abstract class PointerWrapper : IDisposable { - private readonly IntPtr _ptr; + private IntPtr _ptr; internal IntPtr Ptr { @@ -31,6 +31,11 @@ namespace PkmnLibSharp.Utilities } protected PointerWrapper(IntPtr ptr) + { + Initialize(ptr); + } + + protected internal void Initialize(IntPtr ptr) { _ptr = ptr; var weakRef = new WeakReference(this); diff --git a/PkmnLibSharpTests/Battling/BattleLibraryHelper.cs b/PkmnLibSharpTests/Battling/BattleLibraryHelper.cs index 1863538..1a914d6 100644 --- a/PkmnLibSharpTests/Battling/BattleLibraryHelper.cs +++ b/PkmnLibSharpTests/Battling/BattleLibraryHelper.cs @@ -32,6 +32,13 @@ namespace PkmnLibSharpTests.Battling 20, 100)); var moves = MoveLibrary.Create(10); + moves.Insert("testMove", MoveData.Create("testMove", 0, MoveCategory.Physical, 100, + 100, 20, MoveTarget.Any, 0, 0f, "", + new EffectParameter[0], new string[0])); + moves.Insert("testMove2", MoveData.Create("testMove2", 0, MoveCategory.Physical, 100, + 100, 20, MoveTarget.Any, 0, 0f, "", + new EffectParameter[0], new string[0])); + var items = new ItemLibrary(10); items.Insert("testItem", Item.Create("testItem", ItemCategory.MiscItem, BattleItemCategory.None, 500, new string[] { }, 20)); diff --git a/PkmnLibSharpTests/Battling/PokemonBuilderTests.cs b/PkmnLibSharpTests/Battling/PokemonBuilderTests.cs index 854a4c6..a00ca54 100644 --- a/PkmnLibSharpTests/Battling/PokemonBuilderTests.cs +++ b/PkmnLibSharpTests/Battling/PokemonBuilderTests.cs @@ -36,5 +36,17 @@ namespace PkmnLibSharpTests.Battling Assert.AreEqual(Gender.Female, pokemon.Gender); } + [Test] + public void BuildPokemonWithMoves() + { + var lib = BattleLibraryHelper.GetLibrary(); + var pokemon = new PokemonBuilder(lib, "testSpecies", 50) + .LearnMove("testMove", MoveLearnMethod.Unknown) + .LearnMove("testMove2", MoveLearnMethod.Level) + .Build(); + Assert.AreEqual("testMove", pokemon.GetMoves()[0].Move.Name); + Assert.AreEqual("testMove2", pokemon.GetMoves()[1].Move.Name); + } + } } \ No newline at end of file diff --git a/PkmnLibSharpTests/PkmnLibSharpTests.csproj b/PkmnLibSharpTests/PkmnLibSharpTests.csproj index 5b6e3ea..e934894 100644 --- a/PkmnLibSharpTests/PkmnLibSharpTests.csproj +++ b/PkmnLibSharpTests/PkmnLibSharpTests.csproj @@ -5,9 +5,9 @@ false - Debug;Release + Debug - x86_64 + AnyCPU