Adds new NativePtrArray class to deal with native arrays easier.
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
||||
@@ -187,15 +187,20 @@ namespace PkmnLibSharp.Battling
|
||||
{
|
||||
return Creaturelibbattling.Generated.Creature.HasVolatileScript(Ptr, scriptName.ToPtr()) == 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public NativePtrArray<LearnedMove> GetMoves()
|
||||
{
|
||||
if (_moves != null) return _moves;
|
||||
var movesLength = Creaturelibbattling.Generated.Creature.GetAttacksCount(Ptr);
|
||||
var movesPtr = Creaturelibbattling.Generated.Creature.GetAttacks(Ptr);
|
||||
_moves = new NativePtrArray<LearnedMove>(movesPtr, (int) movesLength);
|
||||
return _moves;
|
||||
}
|
||||
|
||||
private Species _species;
|
||||
private Forme _forme;
|
||||
private string _nickname;
|
||||
|
||||
private NativePtrArray<LearnedMove> _moves;
|
||||
|
||||
protected override void DeletePtr()
|
||||
{
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Configurations>Release;Debug</Configurations>
|
||||
<Platforms>x86_64</Platforms>
|
||||
<Configurations>Debug</Configurations>
|
||||
<Platforms>AnyCPU</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Native/*.so.*" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" Link="%(Filename)%(Extension)"></Content>
|
||||
|
||||
111
PkmnLibSharp/Utilities/NativePtrArray.cs
Normal file
111
PkmnLibSharp/Utilities/NativePtrArray.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PkmnLibSharp.Utilities
|
||||
{
|
||||
public class NativePtrArray<T> : IList<T> where T : PointerWrapper, new()
|
||||
{
|
||||
private readonly IntPtr _ptr;
|
||||
|
||||
internal NativePtrArray(IntPtr ptr, int length)
|
||||
{
|
||||
_ptr = ptr;
|
||||
Count = length;
|
||||
}
|
||||
|
||||
public IEnumerator<T> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<object>(this);
|
||||
|
||||
Reference in New Issue
Block a user