Skip to content

Commit

Permalink
Il2CppStructArray<T>.AsSpan() (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Jun 29, 2024
1 parent 45e22c2 commit e48f412
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU</Platforms>
<ImplicitUsings>false</ImplicitUsings>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
47 changes: 27 additions & 20 deletions Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace Il2CppInterop.Runtime.InteropTypes.Arrays;

Expand All @@ -9,6 +10,11 @@ static Il2CppStructArray()
StaticCtorBody(typeof(Il2CppStructArray<T>));
}

/// <summary>
/// The pointer to the first element in the array.
/// </summary>
private IntPtr ArrayStartPointer => IntPtr.Add(Pointer, 4 * IntPtr.Size);

public Il2CppStructArray(IntPtr nativeObject) : base(nativeObject)
{
}
Expand All @@ -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>(T[] arr)
public unsafe Span<T> AsSpan()
{
return new Span<T>(ArrayStartPointer.ToPointer(), Length);
}

[return: NotNullIfNotNull(nameof(arr))]
public static implicit operator Il2CppStructArray<T>?(T[]? arr)
{
if (arr == null) return null;

return new Il2CppStructArray<T>(arr);
}

public static implicit operator Span<T>(Il2CppStructArray<T>? il2CppArray)
{
return il2CppArray is not null ? il2CppArray.AsSpan() : default;
}

public static implicit operator ReadOnlySpan<T>(Il2CppStructArray<T>? il2CppArray)
{
return il2CppArray is not null ? il2CppArray.AsSpan() : default;
}

private static IntPtr AllocateArray(long size)
{
if (size < 0)
Expand Down

0 comments on commit e48f412

Please sign in to comment.