Files
BusyRabbit/Plugins/UnrealSharp/Managed/UnrealSharp/UnrealSharp.Core/UnmanagedArray.cs
wyatt 648386cd73 Lua向C#逻辑迁移 一期 #13
将整个插件代码上传
2025-10-26 21:48:39 +08:00

65 lines
1.4 KiB
C#

using System.Runtime.InteropServices;
using UnrealSharp.Core.Interop;
using UnrealSharp.Core.Marshallers;
namespace UnrealSharp.Core;
[StructLayout(LayoutKind.Sequential)]
public struct UnmanagedArray
{
public IntPtr Data;
public int ArrayNum;
public int ArrayMax;
public void Destroy()
{
unsafe
{
fixed (UnmanagedArray* ptr = &this)
{
FScriptArrayExporter.CallDestroy(ptr);
}
Data = IntPtr.Zero;
ArrayNum = 0;
ArrayMax = 0;
}
}
public List<T> ToBlittableList<T>() where T : unmanaged
{
List<T> list = new List<T>(ArrayNum);
unsafe
{
T* data = (T*) Data.ToPointer();
for (int i = 0; i < ArrayNum; i++)
{
list.Add(data[i]);
}
}
return list;
}
public List<T> ToListWithMarshaller<T>(Func<IntPtr, int, T> resolver)
{
List<T> list = new List<T>(ArrayNum);
for (int i = 0; i < ArrayNum; i++)
{
list.Add(resolver(Data, i));
}
return list;
}
public void ForEachWithMarshaller<T>(Func<IntPtr, int, T> resolver, Action<T> action)
{
for (int i = 0; i < ArrayNum; i++)
{
T item = resolver(Data, i);
action(item);
}
}
}