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

45 lines
1.6 KiB
C#

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace UnrealSharp.Core;
/// <summary>
/// Represents a UObject in Unreal Engine. Don't inherit from this class directly, use a CoreUObject.Object instead.
/// </summary>
public class UnrealSharpObject : IDisposable
{
internal static unsafe IntPtr Create(Type typeToCreate, IntPtr nativeObjectPtr)
{
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
ConstructorInfo? foundDefaultCtor = typeToCreate.GetConstructor(bindingFlags, Type.EmptyTypes);
if (foundDefaultCtor == null)
{
LogUnrealSharpCore.LogError("Failed to find default constructor for type: " + typeToCreate.FullName);
return IntPtr.Zero;
}
delegate*<object, void> foundConstructor = (delegate*<object, void>) foundDefaultCtor.MethodHandle.GetFunctionPointer();
UnrealSharpObject createdObject = (UnrealSharpObject) RuntimeHelpers.GetUninitializedObject(typeToCreate);
createdObject.NativeObject = nativeObjectPtr;
foundConstructor(createdObject);
return GCHandle.ToIntPtr(GCHandleUtilities.AllocateStrongPointer(createdObject, typeToCreate.Assembly));
}
/// <summary>
/// The pointer to the UObject that this C# object represents.
/// </summary>
public IntPtr NativeObject { get; private set; }
/// <inheritdoc />
public virtual void Dispose()
{
NativeObject = IntPtr.Zero;
GC.SuppressFinalize(this);
}
}