2021-09-12 20:23:05 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Reflection;
|
2022-05-28 04:56:46 +02:00
|
|
|
using System.Runtime.CompilerServices;
|
2021-09-12 20:23:05 +02:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Runtime.Loader;
|
2021-12-28 23:25:16 +01:00
|
|
|
using Godot.Bridge;
|
2021-09-12 20:23:05 +02:00
|
|
|
using Godot.NativeInterop;
|
|
|
|
|
|
|
|
namespace GodotPlugins
|
|
|
|
{
|
|
|
|
public static class Main
|
|
|
|
{
|
2022-05-28 04:56:46 +02:00
|
|
|
// IMPORTANT:
|
|
|
|
// Keeping strong references to the AssemblyLoadContext (our PluginLoadContext) prevents
|
|
|
|
// it from being unloaded. To avoid issues, we wrap the reference in this class, and mark
|
|
|
|
// all the methods that access it as non-inlineable. This way we prevent local references
|
|
|
|
// (either real or introduced by the JIT) to escape the scope of these methods due to
|
|
|
|
// inlining, which could keep the AssemblyLoadContext alive while trying to unload.
|
|
|
|
private sealed class PluginLoadContextWrapper
|
|
|
|
{
|
|
|
|
private PluginLoadContext? _pluginLoadContext;
|
|
|
|
|
|
|
|
public string? AssemblyLoadedPath
|
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
|
|
get => _pluginLoadContext?.AssemblyLoadedPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
|
|
public static (Assembly, PluginLoadContextWrapper) CreateAndLoadFromAssemblyName(
|
|
|
|
AssemblyName assemblyName,
|
|
|
|
string pluginPath,
|
|
|
|
ICollection<string> sharedAssemblies,
|
|
|
|
AssemblyLoadContext mainLoadContext
|
|
|
|
)
|
|
|
|
{
|
|
|
|
var wrapper = new PluginLoadContextWrapper();
|
|
|
|
wrapper._pluginLoadContext = new PluginLoadContext(
|
|
|
|
pluginPath, sharedAssemblies, mainLoadContext);
|
|
|
|
var assembly = wrapper._pluginLoadContext.LoadFromAssemblyName(assemblyName);
|
|
|
|
return (assembly, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
|
|
public WeakReference CreateWeakReference()
|
|
|
|
{
|
|
|
|
return new WeakReference(_pluginLoadContext, trackResurrection: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
|
|
internal void Unload()
|
|
|
|
{
|
|
|
|
_pluginLoadContext?.Unload();
|
|
|
|
_pluginLoadContext = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
private static readonly List<AssemblyName> SharedAssemblies = new();
|
|
|
|
private static readonly Assembly CoreApiAssembly = typeof(Godot.Object).Assembly;
|
|
|
|
private static Assembly? _editorApiAssembly;
|
2022-05-28 04:56:46 +02:00
|
|
|
private static PluginLoadContextWrapper? _projectLoadContext;
|
2021-09-12 20:23:05 +02:00
|
|
|
|
|
|
|
private static readonly AssemblyLoadContext MainLoadContext =
|
|
|
|
AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly()) ??
|
|
|
|
AssemblyLoadContext.Default;
|
|
|
|
|
2022-02-27 21:57:52 +01:00
|
|
|
private static DllImportResolver? _dllImportResolver;
|
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
// Right now we do it this way for simplicity as hot-reload is disabled. It will need to be changed later.
|
|
|
|
[UnmanagedCallersOnly]
|
2021-12-28 23:25:16 +01:00
|
|
|
// ReSharper disable once UnusedMember.Local
|
2022-02-27 21:57:52 +01:00
|
|
|
private static unsafe godot_bool InitializeFromEngine(IntPtr godotDllHandle, godot_bool editorHint,
|
2022-08-05 03:32:59 +02:00
|
|
|
PluginsCallbacks* pluginsCallbacks, ManagedCallbacks* managedCallbacks,
|
|
|
|
IntPtr unmanagedCallbacks, int unmanagedCallbacksSize)
|
2021-09-12 20:23:05 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-02-27 21:57:52 +01:00
|
|
|
_dllImportResolver = new GodotDllImportResolver(godotDllHandle).OnResolveDllImport;
|
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
SharedAssemblies.Add(CoreApiAssembly.GetName());
|
2022-02-27 21:57:52 +01:00
|
|
|
NativeLibrary.SetDllImportResolver(CoreApiAssembly, _dllImportResolver);
|
2021-09-12 20:23:05 +02:00
|
|
|
|
2022-05-28 04:56:46 +02:00
|
|
|
AlcReloadCfg.Configure(alcReloadEnabled: editorHint.ToBool());
|
2022-08-05 03:32:59 +02:00
|
|
|
NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize);
|
2022-05-28 04:56:46 +02:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
if (editorHint.ToBool())
|
|
|
|
{
|
|
|
|
_editorApiAssembly = Assembly.Load("GodotSharpEditor");
|
|
|
|
SharedAssemblies.Add(_editorApiAssembly.GetName());
|
2022-02-27 21:57:52 +01:00
|
|
|
NativeLibrary.SetDllImportResolver(_editorApiAssembly, _dllImportResolver);
|
2021-09-12 20:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*pluginsCallbacks = new()
|
|
|
|
{
|
|
|
|
LoadProjectAssemblyCallback = &LoadProjectAssembly,
|
|
|
|
LoadToolsAssemblyCallback = &LoadToolsAssembly,
|
2022-05-28 04:56:46 +02:00
|
|
|
UnloadProjectPluginCallback = &UnloadProjectPlugin,
|
2021-09-12 20:23:05 +02:00
|
|
|
};
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
*managedCallbacks = ManagedCallbacks.Create();
|
2021-09-12 20:23:05 +02:00
|
|
|
|
|
|
|
return godot_bool.True;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.Error.WriteLine(e);
|
2022-05-28 04:56:46 +02:00
|
|
|
return godot_bool.False;
|
2021-09-12 20:23:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
2021-12-28 23:25:16 +01:00
|
|
|
private struct PluginsCallbacks
|
2021-09-12 20:23:05 +02:00
|
|
|
{
|
2022-05-28 04:56:46 +02:00
|
|
|
public unsafe delegate* unmanaged<char*, godot_string*, godot_bool> LoadProjectAssemblyCallback;
|
2022-08-05 03:32:59 +02:00
|
|
|
public unsafe delegate* unmanaged<char*, IntPtr, int, IntPtr> LoadToolsAssemblyCallback;
|
2022-05-28 04:56:46 +02:00
|
|
|
public unsafe delegate* unmanaged<godot_bool> UnloadProjectPluginCallback;
|
2021-09-12 20:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[UnmanagedCallersOnly]
|
2022-05-28 04:56:46 +02:00
|
|
|
private static unsafe godot_bool LoadProjectAssembly(char* nAssemblyPath, godot_string* outLoadedAssemblyPath)
|
2021-09-12 20:23:05 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-05-28 04:56:46 +02:00
|
|
|
if (_projectLoadContext != null)
|
2021-12-28 23:25:16 +01:00
|
|
|
return godot_bool.True; // Already loaded
|
2021-09-12 20:23:05 +02:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
string assemblyPath = new(nAssemblyPath);
|
2021-09-12 20:23:05 +02:00
|
|
|
|
2022-05-28 04:56:46 +02:00
|
|
|
(var projectAssembly, _projectLoadContext) = LoadPlugin(assemblyPath);
|
|
|
|
|
|
|
|
string loadedAssemblyPath = _projectLoadContext.AssemblyLoadedPath ?? assemblyPath;
|
|
|
|
*outLoadedAssemblyPath = Marshaling.ConvertStringToNative(loadedAssemblyPath);
|
2021-09-12 20:23:05 +02:00
|
|
|
|
2022-05-28 04:56:46 +02:00
|
|
|
ScriptManagerBridge.LookupScriptsInAssembly(projectAssembly);
|
2021-09-12 20:23:05 +02:00
|
|
|
|
|
|
|
return godot_bool.True;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.Error.WriteLine(e);
|
2022-05-28 04:56:46 +02:00
|
|
|
return godot_bool.False;
|
2021-09-12 20:23:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[UnmanagedCallersOnly]
|
2022-08-05 03:32:59 +02:00
|
|
|
private static unsafe IntPtr LoadToolsAssembly(char* nAssemblyPath,
|
|
|
|
IntPtr unmanagedCallbacks, int unmanagedCallbacksSize)
|
2021-09-12 20:23:05 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string assemblyPath = new(nAssemblyPath);
|
|
|
|
|
|
|
|
if (_editorApiAssembly == null)
|
|
|
|
throw new InvalidOperationException("The Godot editor API assembly is not loaded");
|
|
|
|
|
2022-05-28 04:56:46 +02:00
|
|
|
var (assembly, _) = LoadPlugin(assemblyPath);
|
2021-09-12 20:23:05 +02:00
|
|
|
|
2022-02-27 21:57:52 +01:00
|
|
|
NativeLibrary.SetDllImportResolver(assembly, _dllImportResolver!);
|
2021-09-12 20:23:05 +02:00
|
|
|
|
|
|
|
var method = assembly.GetType("GodotTools.GodotSharpEditor")?
|
|
|
|
.GetMethod("InternalCreateInstance",
|
|
|
|
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
|
|
|
|
|
|
|
if (method == null)
|
|
|
|
{
|
|
|
|
throw new MissingMethodException("GodotTools.GodotSharpEditor",
|
|
|
|
"InternalCreateInstance");
|
|
|
|
}
|
|
|
|
|
2022-08-05 03:32:59 +02:00
|
|
|
return (IntPtr?)method
|
|
|
|
.Invoke(null, new object[] { unmanagedCallbacks, unmanagedCallbacksSize })
|
|
|
|
?? IntPtr.Zero;
|
2021-09-12 20:23:05 +02:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.Error.WriteLine(e);
|
|
|
|
return IntPtr.Zero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 04:56:46 +02:00
|
|
|
private static (Assembly, PluginLoadContextWrapper) LoadPlugin(string assemblyPath)
|
2021-09-12 20:23:05 +02:00
|
|
|
{
|
|
|
|
string assemblyName = Path.GetFileNameWithoutExtension(assemblyPath);
|
|
|
|
|
|
|
|
var sharedAssemblies = new List<string>();
|
|
|
|
|
|
|
|
foreach (var sharedAssembly in SharedAssemblies)
|
|
|
|
{
|
|
|
|
string? sharedAssemblyName = sharedAssembly.Name;
|
|
|
|
if (sharedAssemblyName != null)
|
|
|
|
sharedAssemblies.Add(sharedAssemblyName);
|
|
|
|
}
|
|
|
|
|
2022-05-28 04:56:46 +02:00
|
|
|
return PluginLoadContextWrapper.CreateAndLoadFromAssemblyName(
|
|
|
|
new AssemblyName(assemblyName), assemblyPath, sharedAssemblies, MainLoadContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
[UnmanagedCallersOnly]
|
|
|
|
private static godot_bool UnloadProjectPlugin()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return UnloadPlugin(ref _projectLoadContext).ToGodotBool();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.Error.WriteLine(e);
|
|
|
|
return godot_bool.False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool UnloadPlugin(ref PluginLoadContextWrapper? pluginLoadContext)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (pluginLoadContext == null)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Console.WriteLine("Unloading assembly load context...");
|
|
|
|
|
|
|
|
var alcWeakReference = pluginLoadContext.CreateWeakReference();
|
|
|
|
|
|
|
|
pluginLoadContext.Unload();
|
|
|
|
pluginLoadContext = null;
|
|
|
|
|
|
|
|
int startTimeMs = Environment.TickCount;
|
|
|
|
bool takingTooLong = false;
|
|
|
|
|
|
|
|
while (alcWeakReference.IsAlive)
|
|
|
|
{
|
|
|
|
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
|
|
|
|
GC.WaitForPendingFinalizers();
|
|
|
|
|
|
|
|
if (!alcWeakReference.IsAlive)
|
|
|
|
break;
|
|
|
|
|
|
|
|
int elapsedTimeMs = Environment.TickCount - startTimeMs;
|
|
|
|
|
|
|
|
if (!takingTooLong && elapsedTimeMs >= 2000)
|
|
|
|
{
|
|
|
|
takingTooLong = true;
|
|
|
|
|
|
|
|
// TODO: How to log from GodotPlugins? (delegate pointer?)
|
|
|
|
Console.Error.WriteLine("Assembly unloading is taking longer than expected...");
|
|
|
|
}
|
|
|
|
else if (elapsedTimeMs >= 5000)
|
|
|
|
{
|
|
|
|
// TODO: How to log from GodotPlugins? (delegate pointer?)
|
|
|
|
Console.Error.WriteLine(
|
|
|
|
"Failed to unload assemblies. Possible causes: Strong GC handles, running threads, etc.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.WriteLine("Assembly load context unloaded successfully.");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
// TODO: How to log exceptions from GodotPlugins? (delegate pointer?)
|
|
|
|
Console.Error.WriteLine(e);
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-12 20:23:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|