From fd938e8284177f1c6f28006bc1aee69f5adaa4fe Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 28 Nov 2023 11:22:14 +0100 Subject: [PATCH] Fix incorrect pointer marshalling from `Variant` to managed runtime #1992 --- Source/Engine/Engine/NativeInterop.Unmanaged.cs | 3 +++ Source/Engine/Engine/NativeInterop.cs | 8 +++----- Source/Engine/Scripting/ManagedCLR/MUtils.cpp | 4 ++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Engine/NativeInterop.Unmanaged.cs b/Source/Engine/Engine/NativeInterop.Unmanaged.cs index 574b74580..e99276a4e 100644 --- a/Source/Engine/Engine/NativeInterop.Unmanaged.cs +++ b/Source/Engine/Engine/NativeInterop.Unmanaged.cs @@ -1271,6 +1271,9 @@ namespace FlaxEngine.Interop case Type _ when type == typeof(IntPtr): monoType = MTypes.Ptr; break; + case Type _ when type.IsPointer: + monoType = MTypes.Ptr; + break; case Type _ when type.IsEnum: monoType = MTypes.Enum; break; diff --git a/Source/Engine/Engine/NativeInterop.cs b/Source/Engine/Engine/NativeInterop.cs index 30ce4d39f..9548c3f59 100644 --- a/Source/Engine/Engine/NativeInterop.cs +++ b/Source/Engine/Engine/NativeInterop.cs @@ -1112,11 +1112,9 @@ namespace FlaxEngine.Interop internal static void ToManagedPointer(ref IntPtr managedValue, IntPtr nativePtr, bool byRef) { - Type type = typeof(T); - byRef |= type.IsByRef; // Is this needed? - if (type.IsByRef) - Assert.IsTrue(type.GetElementType().IsValueType); - managedValue = byRef ? nativePtr : Unsafe.Read(nativePtr.ToPointer()); + if (byRef) + nativePtr = Unsafe.Read(nativePtr.ToPointer()); + managedValue = nativePtr; } internal static void ToManagedHandle(ref ManagedHandle managedValue, IntPtr nativePtr, bool byRef) diff --git a/Source/Engine/Scripting/ManagedCLR/MUtils.cpp b/Source/Engine/Scripting/ManagedCLR/MUtils.cpp index be8e10fab..52d049403 100644 --- a/Source/Engine/Scripting/ManagedCLR/MUtils.cpp +++ b/Source/Engine/Scripting/ManagedCLR/MUtils.cpp @@ -1208,6 +1208,10 @@ void* MUtils::VariantToManagedArgPtr(Variant& value, MType* type, bool& failed) object = nullptr; return object; } + case MTypes::Ptr: + if (value.Type.Type == VariantType::Null) + return nullptr; + return (void*)value; default: break; }