From 67f3d0a5cac1e24ac2b766664960f6effa463132 Mon Sep 17 00:00:00 2001 From: Nikita Ushakov Date: Sat, 29 Jun 2024 16:19:30 +0100 Subject: [PATCH] Small refactor --- managed/Plugify/NativeInterop.cs | 52 +++++++++++++++++++------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/managed/Plugify/NativeInterop.cs b/managed/Plugify/NativeInterop.cs index 06a5203..c6cc151 100644 --- a/managed/Plugify/NativeInterop.cs +++ b/managed/Plugify/NativeInterop.cs @@ -693,7 +693,9 @@ public static Delegate GetDelegateForFunctionPointer(nint funcAddress, Type dele return Marshal.GetDelegateForFunctionPointer(funcAddress, delegateType); } - + + #region External Call from C# to C++ + private static Func ExternalInvoke(nint funcAddress, MethodInfo methodInfo) { ManagedType returnType = new ManagedType(methodInfo.ReturnType, methodInfo.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(MarshalAsAttribute), false)); @@ -903,7 +905,7 @@ private static Func ExternalInvoke(nint funcAddress, MethodInf vm.ArgPointer(Pin(paramValue, pins)); break; case ValueType.Function: - Delegate d = GetWrapperDelegateForDelegate((Delegate)paramValue); + Delegate d = CreateConvertDelegate((Delegate)paramValue); Pin(d, pins); vm.ArgPointer(Pin(Marshal.GetFunctionPointerForDelegate(d), pins)); break; @@ -1102,7 +1104,7 @@ private static Func ExternalInvoke(nint funcAddress, MethodInf vm.ArgPointer(Pin(paramValue, pins)); break; case ValueType.Function: - Delegate d = GetWrapperDelegateForDelegate((Delegate)paramValue); + Delegate d = CreateConvertDelegate((Delegate)paramValue); Pin(d, pins); vm.ArgPointer(Pin(Marshal.GetFunctionPointerForDelegate(d), pins)); break; @@ -1793,11 +1795,15 @@ private static void DeleteParams(List<(nint, ValueType)> handlers, bool hasRet) } } - public static Delegate GetWrapperDelegateForDelegate(Delegate d) + #endregion + + public static Delegate CreateConvertDelegate(Delegate d) { MethodInfo methodInfo = d.Method; if (NeedMarshal(methodInfo.ReturnType) || methodInfo.GetParameters().Any(p => NeedMarshal(p.ParameterType))) { + // Convert all string and array types into nint, if string and array return, append 1st hidden parameter + List types = []; Type returnType = methodInfo.ReturnType; @@ -1824,6 +1830,8 @@ public static Delegate GetWrapperDelegateForDelegate(Delegate d) return d; } + #region External Call from C++ to C# + private static Func InternalInvoke(Delegate d) { MethodInfo methodInfo = d.Method; @@ -2206,6 +2214,25 @@ private static Func InternalInvoke(Delegate d) }; } + #endregion + + public static void FreeObject(ManagedObject obj) + { + if (!ManagedObjectCache.Instance.RemoveObject(obj.guid)) + { + throw new Exception("Failed to remove object from cache: " + obj.guid); + } + } + + private static void OutError(nint outErrorStringPtr, string message) + { + if (outErrorStringPtr == nint.Zero) + return; + + byte[] bytes = Encoding.ASCII.GetBytes(message); + Marshal.Copy(bytes, 0, outErrorStringPtr, bytes.Length); + } + internal static bool NeedMarshal(Type paramType) { if (paramType.IsByRef) @@ -2224,23 +2251,6 @@ internal static bool NeedMarshal(Type paramType) return valueType is >= ValueType.String and <= ValueType.ArrayString; } - public static void FreeObject(ManagedObject obj) - { - if (!ManagedObjectCache.Instance.RemoveObject(obj.guid)) - { - throw new Exception("Failed to remove object from cache: " + obj.guid); - } - } - - private static void OutError(nint outErrorStringPtr, string message) - { - if (outErrorStringPtr == nint.Zero) - return; - - byte[] bytes = Encoding.ASCII.GetBytes(message); - Marshal.Copy(bytes, 0, outErrorStringPtr, bytes.Length); - } - [DllImport(NativeMethods.DllName)] private static extern void ManagedClass_Create(ref Guid assemblyGuid, nint classHolderPtr, int typeHash, nint typeNamePtr, bool isPlugin, [Out] out ManagedClass result);