Cache created wrappers for pointers, so we can resolve them later.
This commit is contained in:
parent
1bf2f0ad09
commit
42ec208425
|
@ -54,8 +54,7 @@ namespace PkmnLibSharp.Library
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected override void DeletePtr()
|
||||||
internal override void DeletePtr()
|
|
||||||
{
|
{
|
||||||
Creatureliblibrary.Generated.EffectParameter.Destruct(Ptr);
|
Creatureliblibrary.Generated.EffectParameter.Destruct(Ptr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,6 @@ namespace PkmnLibSharp.Library
|
||||||
return _abilities;
|
return _abilities;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImmutableArray<string> HiddenAbilities
|
public ImmutableArray<string> HiddenAbilities
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -89,13 +88,25 @@ namespace PkmnLibSharp.Library
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LearnableMoves Moves
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_moves != null) return _moves;
|
||||||
|
var movesPtr = SpeciesVariant.GetLearnableAttacks(Ptr);
|
||||||
|
if (!TryResolvePointer(movesPtr, out _moves))
|
||||||
|
{
|
||||||
|
_moves = new LearnableMoves(movesPtr);
|
||||||
|
}
|
||||||
|
return _moves;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int GetPkmnType(int index)
|
public int GetPkmnType(int index)
|
||||||
{
|
{
|
||||||
return Types[index];
|
return Types[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static unsafe Forme Create(string name, float height, float weight, uint baseExperience, byte[] types,
|
public static unsafe Forme Create(string name, float height, float weight, uint baseExperience, byte[] types,
|
||||||
ushort baseHealth, ushort baseAttack, ushort baseDefense, ushort baseSpecialAttack,
|
ushort baseHealth, ushort baseAttack, ushort baseDefense, ushort baseSpecialAttack,
|
||||||
ushort baseSpecialDefense, ushort baseSpeed, string[] abilities, string[] hiddenAbilities,
|
ushort baseSpecialDefense, ushort baseSpeed, string[] abilities, string[] hiddenAbilities,
|
||||||
|
@ -125,7 +136,7 @@ namespace PkmnLibSharp.Library
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void DeletePtr()
|
protected override void DeletePtr()
|
||||||
{
|
{
|
||||||
SpeciesVariant.Destruct(Ptr);
|
SpeciesVariant.Destruct(Ptr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ namespace PkmnLibSharp.Library
|
||||||
{
|
{
|
||||||
public class LearnableMoves : PointerWrapper
|
public class LearnableMoves : PointerWrapper
|
||||||
{
|
{
|
||||||
private LearnableMoves(IntPtr ptr) : base(ptr)
|
internal LearnableMoves(IntPtr ptr) : base(ptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ namespace PkmnLibSharp.Library
|
||||||
LearnableAttacks.AddLevelAttack(Ptr, level, move.Ptr);
|
LearnableAttacks.AddLevelAttack(Ptr, level, move.Ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void DeletePtr()
|
protected override void DeletePtr()
|
||||||
{
|
{
|
||||||
LearnableAttacks.Destruct(Ptr);
|
LearnableAttacks.Destruct(Ptr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ namespace PkmnLibSharp.Library
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void DeletePtr()
|
protected override void DeletePtr()
|
||||||
{
|
{
|
||||||
AttackData.Destruct(Ptr);
|
AttackData.Destruct(Ptr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace PkmnLibSharp.Library
|
||||||
return new Species(ptr);
|
return new Species(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void DeletePtr()
|
protected override void DeletePtr()
|
||||||
{
|
{
|
||||||
PokemonSpecies.Destruct(Ptr);
|
PokemonSpecies.Destruct(Ptr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
namespace PkmnLibSharp
|
namespace PkmnLibSharp
|
||||||
{
|
{
|
||||||
|
@ -7,16 +8,36 @@ namespace PkmnLibSharp
|
||||||
internal readonly IntPtr Ptr;
|
internal readonly IntPtr Ptr;
|
||||||
private bool _isDeleted = false;
|
private bool _isDeleted = false;
|
||||||
|
|
||||||
|
private static readonly ConcurrentDictionary<IntPtr, WeakReference<object>> Cached =
|
||||||
|
new ConcurrentDictionary<IntPtr, WeakReference<object>>();
|
||||||
|
|
||||||
protected PointerWrapper(IntPtr ptr)
|
protected PointerWrapper(IntPtr ptr)
|
||||||
{
|
{
|
||||||
Ptr = ptr;
|
Ptr = ptr;
|
||||||
|
var weakRef = new WeakReference<object>(this);
|
||||||
|
Cached.TryAdd(ptr, weakRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal abstract void DeletePtr();
|
internal static bool TryResolvePointer<T>(IntPtr p, out T result) where T : PointerWrapper
|
||||||
|
{
|
||||||
|
if (Cached.TryGetValue(p, out var val))
|
||||||
|
{
|
||||||
|
if (val.TryGetTarget(out var target))
|
||||||
|
{
|
||||||
|
result = (T) target;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void DeletePtr();
|
||||||
public virtual void Dispose()
|
public virtual void Dispose()
|
||||||
{
|
{
|
||||||
if (_isDeleted)
|
if (_isDeleted)
|
||||||
return;
|
return;
|
||||||
|
Cached.TryRemove(Ptr, out _);
|
||||||
DeletePtr();
|
DeletePtr();
|
||||||
_isDeleted = true;
|
_isDeleted = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue