Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qubka committed Jun 29, 2024
1 parent 84dcd15 commit 67f3d0a
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions managed/Plugify/NativeInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object[], object> ExternalInvoke(nint funcAddress, MethodInfo methodInfo)
{
ManagedType returnType = new ManagedType(methodInfo.ReturnType, methodInfo.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(MarshalAsAttribute), false));
Expand Down Expand Up @@ -903,7 +905,7 @@ private static Func<object[], object> ExternalInvoke(nint funcAddress, MethodInf
vm.ArgPointer(Pin<Matrix4x4>(paramValue, pins));
break;
case ValueType.Function:
Delegate d = GetWrapperDelegateForDelegate((Delegate)paramValue);
Delegate d = CreateConvertDelegate((Delegate)paramValue);
Pin<Delegate>(d, pins);
vm.ArgPointer(Pin<nint>(Marshal.GetFunctionPointerForDelegate(d), pins));
break;
Expand Down Expand Up @@ -1102,7 +1104,7 @@ private static Func<object[], object> ExternalInvoke(nint funcAddress, MethodInf
vm.ArgPointer(Pin<Matrix4x4>(paramValue, pins));
break;
case ValueType.Function:
Delegate d = GetWrapperDelegateForDelegate((Delegate)paramValue);
Delegate d = CreateConvertDelegate((Delegate)paramValue);
Pin<Delegate>(d, pins);
vm.ArgPointer(Pin<nint>(Marshal.GetFunctionPointerForDelegate(d), pins));
break;
Expand Down Expand Up @@ -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<Type> types = [];

Type returnType = methodInfo.ReturnType;
Expand All @@ -1824,6 +1830,8 @@ public static Delegate GetWrapperDelegateForDelegate(Delegate d)
return d;
}

#region External Call from C++ to C#

private static Func<object[], object> InternalInvoke(Delegate d)
{
MethodInfo methodInfo = d.Method;
Expand Down Expand Up @@ -2206,6 +2214,25 @@ private static Func<object[], object> 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)
Expand All @@ -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);

Expand Down

0 comments on commit 67f3d0a

Please sign in to comment.