1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-18 14:21:41 +00:00

C#: Replace P/Invoke with delegate pointers

- Moves interop functions to UnmanagedCallbacks struct that
  contains the function pointers and is passed to C#.

- Implements UnmanagedCallbacksGenerator, a C# source generator that
  generates the UnmanagedCallbacks struct in C# and the body for the
  NativeFuncs methods (their implementation just calls the function
  pointer in the UnmanagedCallbacks). The generated methods are needed
  because .NET pins byref parameters of native calls, even if they are
  'ref struct's, which don't need pinning. The generated methods use
  `Unsafe.AsPointer` so that we can benefit from byref parameters
  without suffering overhead of pinning.

Co-authored-by: Raul Santos <raulsntos@gmail.com>
This commit is contained in:
Ignacio Roldán Etcheverry
2022-08-05 03:32:59 +02:00
parent 186d7f6239
commit 2c180f62d9
21 changed files with 1314 additions and 740 deletions

View File

@@ -72,7 +72,8 @@ namespace GodotPlugins
[UnmanagedCallersOnly]
// ReSharper disable once UnusedMember.Local
private static unsafe godot_bool InitializeFromEngine(IntPtr godotDllHandle, godot_bool editorHint,
PluginsCallbacks* pluginsCallbacks, ManagedCallbacks* managedCallbacks)
PluginsCallbacks* pluginsCallbacks, ManagedCallbacks* managedCallbacks,
IntPtr unmanagedCallbacks, int unmanagedCallbacksSize)
{
try
{
@@ -82,6 +83,7 @@ namespace GodotPlugins
NativeLibrary.SetDllImportResolver(CoreApiAssembly, _dllImportResolver);
AlcReloadCfg.Configure(alcReloadEnabled: editorHint.ToBool());
NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize);
if (editorHint.ToBool())
{
@@ -112,7 +114,7 @@ namespace GodotPlugins
private struct PluginsCallbacks
{
public unsafe delegate* unmanaged<char*, godot_string*, godot_bool> LoadProjectAssemblyCallback;
public unsafe delegate* unmanaged<char*, IntPtr> LoadToolsAssemblyCallback;
public unsafe delegate* unmanaged<char*, IntPtr, int, IntPtr> LoadToolsAssemblyCallback;
public unsafe delegate* unmanaged<godot_bool> UnloadProjectPluginCallback;
}
@@ -143,7 +145,8 @@ namespace GodotPlugins
}
[UnmanagedCallersOnly]
private static unsafe IntPtr LoadToolsAssembly(char* nAssemblyPath)
private static unsafe IntPtr LoadToolsAssembly(char* nAssemblyPath,
IntPtr unmanagedCallbacks, int unmanagedCallbacksSize)
{
try
{
@@ -166,7 +169,9 @@ namespace GodotPlugins
"InternalCreateInstance");
}
return (IntPtr?)method.Invoke(null, null) ?? IntPtr.Zero;
return (IntPtr?)method
.Invoke(null, new object[] { unmanagedCallbacks, unmanagedCallbacksSize })
?? IntPtr.Zero;
}
catch (Exception e)
{