diff --git a/PkmnLibSharp/Battling/Pokemon.cs b/PkmnLibSharp/Battling/Pokemon.cs index c96be4e..835626e 100644 --- a/PkmnLibSharp/Battling/Pokemon.cs +++ b/PkmnLibSharp/Battling/Pokemon.cs @@ -166,14 +166,15 @@ namespace PkmnLibSharp.Battling return _nickname; } } - public ReadOnlyNativePtrArray Moves + public ReadOnlyNativePtrArray Moves { get { if (_moves != null) return _moves; var movesLength = Creaturelib.Generated.Creature.GetAttacksCount(Ptr); var movesPtr = Creaturelib.Generated.Creature.GetAttacks(Ptr); - _moves = new ReadOnlyNativePtrArray(movesPtr, (int) movesLength, Constructor.GenericType.LearnedMove); + _moves = new ReadOnlyNativePtrArray(movesPtr, (int) movesLength, + Constructor.GenericType.LearnedMove); return _moves; } } @@ -439,7 +440,7 @@ namespace PkmnLibSharp.Battling private Species? _species; private Forme? _forme; private string? _nickname; - private ReadOnlyNativePtrArray? _moves; + private ReadOnlyNativePtrArray? _moves; private Nature? _nature; private Battle? _battle; private BattleSide? _battleSide; diff --git a/PkmnLibSharp/Utilities/ReadOnlyNativePtrArray.cs b/PkmnLibSharp/Utilities/ReadOnlyNativePtrArray.cs index e0ac112..8a65932 100644 --- a/PkmnLibSharp/Utilities/ReadOnlyNativePtrArray.cs +++ b/PkmnLibSharp/Utilities/ReadOnlyNativePtrArray.cs @@ -1,16 +1,14 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Runtime.Serialization; namespace PkmnLibSharp.Utilities { - public class ReadOnlyNativePtrArray : IReadOnlyList where T : PointerWrapper + public class ReadOnlyNativePtrArray : IReadOnlyList where T : PointerWrapper { private readonly IntPtr _ptr; private readonly T?[] _cache; - private readonly Type _type = typeof(T); - private Constructor.GenericType _constructorType; + private readonly Constructor.GenericType _constructorType; internal ReadOnlyNativePtrArray(Constructor.GenericType type) { @@ -84,6 +82,8 @@ namespace PkmnLibSharp.Utilities // 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 to the object we want.) var p = new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer()); + if (p == IntPtr.Zero) + return null; if (_cache[index]?.Ptr == p) return _cache[index]!; if (PointerWrapper.TryResolvePointer(p, out T? t))