79 lines
2.1 KiB
C#
79 lines
2.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace PkmnLibSharp.Utilities
|
|
{
|
|
public class ReadOnlyNativePtrArray<T> : IReadOnlyList<T> where T : PointerWrapper, new()
|
|
{
|
|
private readonly IntPtr _ptr;
|
|
|
|
internal ReadOnlyNativePtrArray(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 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 int Count { get; }
|
|
|
|
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 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 to the object we want.)
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |