Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Il2CppStructArray<T>.AsSpan() #129

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading