From e48f4127e55276125136f17cffb70dc5ffb38fa3 Mon Sep 17 00:00:00 2001 From: Jeremy Pritts <49847914+ds5678@users.noreply.github.com> Date: Sat, 29 Jun 2024 11:53:51 -0400 Subject: [PATCH] `Il2CppStructArray.AsSpan()` (#129) --- .../Il2CppInterop.Runtime.csproj | 2 + .../InteropTypes/Arrays/Il2CppStructArray.cs | 47 +++++++++++-------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj b/Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj index 0766a12b..0815e6b0 100644 --- a/Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj +++ b/Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj @@ -10,6 +10,8 @@ Debug;Release AnyCPU false + Latest + enable diff --git a/Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs b/Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs index 7553eaee..d1e5b07f 100644 --- a/Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs +++ b/Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace Il2CppInterop.Runtime.InteropTypes.Arrays; @@ -9,6 +10,11 @@ static Il2CppStructArray() StaticCtorBody(typeof(Il2CppStructArray)); } + /// + /// The pointer to the first element in the array. + /// + private IntPtr ArrayStartPointer => IntPtr.Add(Pointer, 4 * IntPtr.Size); + public Il2CppStructArray(IntPtr nativeObject) : base(nativeObject) { } @@ -19,37 +25,38 @@ public Il2CppStructArray(long size) : base(AllocateArray(size)) public Il2CppStructArray(T[] arr) : base(AllocateArray(arr.Length)) { - for (var i = 0; i < arr.Length; i++) - this[i] = arr[i]; + arr.CopyTo(this); } - public override unsafe T this[int index] + public override T this[int index] { - get - { - if (index < 0 || index >= Length) - throw new ArgumentOutOfRangeException(nameof(index), - "Array index may not be negative or above length of the array"); - var arrayStartPointer = IntPtr.Add(Pointer, 4 * IntPtr.Size); - return ((T*)arrayStartPointer.ToPointer())[index]; - } - set - { - if (index < 0 || index >= Length) - throw new ArgumentOutOfRangeException(nameof(index), - "Array index may not be negative or above length of the array"); - var arrayStartPointer = IntPtr.Add(Pointer, 4 * IntPtr.Size); - ((T*)arrayStartPointer.ToPointer())[index] = value; - } + get => AsSpan()[index]; + set => AsSpan()[index] = value; } - public static implicit operator Il2CppStructArray(T[] arr) + public unsafe Span AsSpan() + { + return new Span(ArrayStartPointer.ToPointer(), Length); + } + + [return: NotNullIfNotNull(nameof(arr))] + public static implicit operator Il2CppStructArray?(T[]? arr) { if (arr == null) return null; return new Il2CppStructArray(arr); } + public static implicit operator Span(Il2CppStructArray? il2CppArray) + { + return il2CppArray is not null ? il2CppArray.AsSpan() : default; + } + + public static implicit operator ReadOnlySpan(Il2CppStructArray? il2CppArray) + { + return il2CppArray is not null ? il2CppArray.AsSpan() : default; + } + private static IntPtr AllocateArray(long size) { if (size < 0)