Allow ReadOnlyNativePtrArray to contain nulls when IntPtr is zero.

This commit is contained in:
Deukhoofd 2020-09-01 12:27:16 +02:00
parent 2ba7dbf6a3
commit 471c082527
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 8 additions and 7 deletions

View File

@ -166,14 +166,15 @@ namespace PkmnLibSharp.Battling
return _nickname;
}
}
public ReadOnlyNativePtrArray<LearnedMove> Moves
public ReadOnlyNativePtrArray<LearnedMove?> Moves
{
get
{
if (_moves != null) return _moves;
var movesLength = Creaturelib.Generated.Creature.GetAttacksCount(Ptr);
var movesPtr = Creaturelib.Generated.Creature.GetAttacks(Ptr);
_moves = new ReadOnlyNativePtrArray<LearnedMove>(movesPtr, (int) movesLength, Constructor.GenericType.LearnedMove);
_moves = new ReadOnlyNativePtrArray<LearnedMove?>(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<LearnedMove>? _moves;
private ReadOnlyNativePtrArray<LearnedMove?>? _moves;
private Nature? _nature;
private Battle? _battle;
private BattleSide? _battleSide;

View File

@ -1,16 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace PkmnLibSharp.Utilities
{
public class ReadOnlyNativePtrArray<T> : IReadOnlyList<T> where T : PointerWrapper
public class ReadOnlyNativePtrArray<T> : IReadOnlyList<T?> 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))