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; return _nickname;
} }
} }
public ReadOnlyNativePtrArray<LearnedMove> Moves public ReadOnlyNativePtrArray<LearnedMove?> Moves
{ {
get get
{ {
if (_moves != null) return _moves; if (_moves != null) return _moves;
var movesLength = Creaturelib.Generated.Creature.GetAttacksCount(Ptr); var movesLength = Creaturelib.Generated.Creature.GetAttacksCount(Ptr);
var movesPtr = Creaturelib.Generated.Creature.GetAttacks(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; return _moves;
} }
} }
@ -439,7 +440,7 @@ namespace PkmnLibSharp.Battling
private Species? _species; private Species? _species;
private Forme? _forme; private Forme? _forme;
private string? _nickname; private string? _nickname;
private ReadOnlyNativePtrArray<LearnedMove>? _moves; private ReadOnlyNativePtrArray<LearnedMove?>? _moves;
private Nature? _nature; private Nature? _nature;
private Battle? _battle; private Battle? _battle;
private BattleSide? _battleSide; private BattleSide? _battleSide;

View File

@ -1,16 +1,14 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Serialization;
namespace PkmnLibSharp.Utilities 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 IntPtr _ptr;
private readonly T?[] _cache; private readonly T?[] _cache;
private readonly Type _type = typeof(T); private readonly Constructor.GenericType _constructorType;
private Constructor.GenericType _constructorType;
internal ReadOnlyNativePtrArray(Constructor.GenericType type) internal ReadOnlyNativePtrArray(Constructor.GenericType type)
{ {
@ -84,6 +82,8 @@ namespace PkmnLibSharp.Utilities
// Where's your god now? // 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.) // (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()); var p = new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer());
if (p == IntPtr.Zero)
return null;
if (_cache[index]?.Ptr == p) if (_cache[index]?.Ptr == p)
return _cache[index]!; return _cache[index]!;
if (PointerWrapper.TryResolvePointer(p, out T? t)) if (PointerWrapper.TryResolvePointer(p, out T? t))