| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.IO; |
| 4 |
using System.Reflection; |
| 5 |
using System.Runtime.CompilerServices; |
| 6 |
using System.Runtime.InteropServices; |
| 7 |
using System.Runtime.Loader; |
| 8 |
using Godot.Bridge; |
| 9 |
using Godot.NativeInterop; |
| 10 |
|
| 11 |
namespace GodotPlugins |
| 12 |
{ |
| 13 |
public static class Main |
| 14 |
{ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private sealed class PluginLoadContextWrapper |
| 22 |
{ |
| 23 |
private PluginLoadContext? _pluginLoadContext; |
| 24 |
private readonly WeakReference _weakReference; |
| 25 |
|
| 26 |
private PluginLoadContextWrapper(PluginLoadContext pluginLoadContext, WeakReference weakReference) |
| 27 |
{ |
| 28 |
_pluginLoadContext = pluginLoadContext; |
| 29 |
_weakReference = weakReference; |
| 30 |
} |
| 31 |
|
| 32 |
public string? AssemblyLoadedPath |
| 33 |
{ |
| 34 |
[MethodImpl(MethodImplOptions.NoInlining)] |
| 35 |
get => _pluginLoadContext?.AssemblyLoadedPath; |
| 36 |
} |
| 37 |
|
| 38 |
public bool IsCollectible |
| 39 |
{ |
| 40 |
[MethodImpl(MethodImplOptions.NoInlining)] |
| 41 |
|
| 42 |
get => _pluginLoadContext?.IsCollectible ?? true; |
| 43 |
} |
| 44 |
|
| 45 |
public bool IsAlive |
| 46 |
{ |
| 47 |
[MethodImpl(MethodImplOptions.NoInlining)] |
| 48 |
get => _weakReference.IsAlive; |
| 49 |
} |
| 50 |
|
| 51 |
[MethodImpl(MethodImplOptions.NoInlining)] |
| 52 |
public static (Assembly, PluginLoadContextWrapper) CreateAndLoadFromAssemblyName( |
| 53 |
AssemblyName assemblyName, |
| 54 |
string pluginPath, |
| 55 |
ICollection<string> sharedAssemblies, |
| 56 |
AssemblyLoadContext mainLoadContext, |
| 57 |
bool isCollectible |
| 58 |
) |
| 59 |
{ |
| 60 |
var context = new PluginLoadContext(pluginPath, sharedAssemblies, mainLoadContext, isCollectible); |
| 61 |
var reference = new WeakReference(context, trackResurrection: true); |
| 62 |
var wrapper = new PluginLoadContextWrapper(context, reference); |
| 63 |
var assembly = context.LoadFromAssemblyName(assemblyName); |
| 64 |
return (assembly, wrapper); |
| 65 |
} |
| 66 |
|
| 67 |
[MethodImpl(MethodImplOptions.NoInlining)] |
| 68 |
internal void Unload() |
| 69 |
{ |
| 70 |
_pluginLoadContext?.Unload(); |
| 71 |
_pluginLoadContext = null; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
private static readonly List<AssemblyName> SharedAssemblies = new(); |
| 76 |
private static readonly Assembly CoreApiAssembly = typeof(global::Godot.GodotObject).Assembly; |
| 77 |
private static Assembly? _editorApiAssembly; |
| 78 |
private static PluginLoadContextWrapper? _projectLoadContext; |
| 79 |
private static bool _editorHint = false; |
| 80 |
|
| 81 |
private static readonly AssemblyLoadContext MainLoadContext = |
| 82 |
AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly()) ?? |
| 83 |
AssemblyLoadContext.Default; |
| 84 |
|
| 85 |
private static DllImportResolver? _dllImportResolver; |
| 86 |
|
| 87 |
|
| 88 |
[UnmanagedCallersOnly] |
| 89 |
|
| 90 |
private static unsafe godot_bool InitializeFromEngine(IntPtr godotDllHandle, godot_bool editorHint, |
| 91 |
PluginsCallbacks* pluginsCallbacks, ManagedCallbacks* managedCallbacks, |
| 92 |
IntPtr unmanagedCallbacks, int unmanagedCallbacksSize) |
| 93 |
{ |
| 94 |
try |
| 95 |
{ |
| 96 |
_editorHint = editorHint.ToBool(); |
| 97 |
|
| 98 |
_dllImportResolver = new GodotDllImportResolver(godotDllHandle).OnResolveDllImport; |
| 99 |
|
| 100 |
SharedAssemblies.Add(CoreApiAssembly.GetName()); |
| 101 |
NativeLibrary.SetDllImportResolver(CoreApiAssembly, _dllImportResolver); |
| 102 |
|
| 103 |
AlcReloadCfg.Configure(alcReloadEnabled: _editorHint); |
| 104 |
NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize); |
| 105 |
|
| 106 |
if (_editorHint) |
| 107 |
{ |
| 108 |
_editorApiAssembly = Assembly.Load("GodotSharpEditor"); |
| 109 |
SharedAssemblies.Add(_editorApiAssembly.GetName()); |
| 110 |
NativeLibrary.SetDllImportResolver(_editorApiAssembly, _dllImportResolver); |
| 111 |
} |
| 112 |
|
| 113 |
*pluginsCallbacks = new() |
| 114 |
{ |
| 115 |
LoadProjectAssemblyCallback = &LoadProjectAssembly, |
| 116 |
LoadToolsAssemblyCallback = &LoadToolsAssembly, |
| 117 |
UnloadProjectPluginCallback = &UnloadProjectPlugin, |
| 118 |
}; |
| 119 |
|
| 120 |
*managedCallbacks = ManagedCallbacks.Create(); |
| 121 |
|
| 122 |
return godot_bool.True; |
| 123 |
} |
| 124 |
catch (Exception e) |
| 125 |
{ |
| 126 |
Console.Error.WriteLine(e); |
| 127 |
return godot_bool.False; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
[StructLayout(LayoutKind.Sequential)] |
| 132 |
private struct PluginsCallbacks |
| 133 |
{ |
| 134 |
public unsafe delegate* unmanaged<char*, godot_string*, godot_bool> LoadProjectAssemblyCallback; |
| 135 |
public unsafe delegate* unmanaged<char*, IntPtr, int, IntPtr> LoadToolsAssemblyCallback; |
| 136 |
public unsafe delegate* unmanaged<godot_bool> UnloadProjectPluginCallback; |
| 137 |
} |
| 138 |
|
| 139 |
[UnmanagedCallersOnly] |
| 140 |
private static unsafe godot_bool LoadProjectAssembly(char* nAssemblyPath, godot_string* outLoadedAssemblyPath) |
| 141 |
{ |
| 142 |
try |
| 143 |
{ |
| 144 |
if (_projectLoadContext != null) |
| 145 |
return godot_bool.True; |
| 146 |
|
| 147 |
string assemblyPath = new(nAssemblyPath); |
| 148 |
|
| 149 |
(var projectAssembly, _projectLoadContext) = LoadPlugin(assemblyPath, isCollectible: _editorHint); |
| 150 |
|
| 151 |
string loadedAssemblyPath = _projectLoadContext.AssemblyLoadedPath ?? assemblyPath; |
| 152 |
*outLoadedAssemblyPath = Marshaling.ConvertStringToNative(loadedAssemblyPath); |
| 153 |
|
| 154 |
ScriptManagerBridge.LookupScriptsInAssembly(projectAssembly); |
| 155 |
|
| 156 |
return godot_bool.True; |
| 157 |
} |
| 158 |
catch (Exception e) |
| 159 |
{ |
| 160 |
Console.Error.WriteLine(e); |
| 161 |
return godot_bool.False; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
[UnmanagedCallersOnly] |
| 166 |
private static unsafe IntPtr LoadToolsAssembly(char* nAssemblyPath, |
| 167 |
IntPtr unmanagedCallbacks, int unmanagedCallbacksSize) |
| 168 |
{ |
| 169 |
try |
| 170 |
{ |
| 171 |
string assemblyPath = new(nAssemblyPath); |
| 172 |
|
| 173 |
if (_editorApiAssembly == null) |
| 174 |
throw new InvalidOperationException("The Godot editor API assembly is not loaded."); |
| 175 |
|
| 176 |
var (assembly, _) = LoadPlugin(assemblyPath, isCollectible: false); |
| 177 |
|
| 178 |
NativeLibrary.SetDllImportResolver(assembly, _dllImportResolver!); |
| 179 |
|
| 180 |
var method = assembly.GetType("GodotTools.GodotSharpEditor")? |
| 181 |
.GetMethod("InternalCreateInstance", |
| 182 |
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); |
| 183 |
|
| 184 |
if (method == null) |
| 185 |
{ |
| 186 |
throw new MissingMethodException("GodotTools.GodotSharpEditor", |
| 187 |
"InternalCreateInstance"); |
| 188 |
} |
| 189 |
|
| 190 |
return (IntPtr?)method |
| 191 |
.Invoke(null, new object[] { unmanagedCallbacks, unmanagedCallbacksSize }) |
| 192 |
?? IntPtr.Zero; |
| 193 |
} |
| 194 |
catch (Exception e) |
| 195 |
{ |
| 196 |
Console.Error.WriteLine(e); |
| 197 |
return IntPtr.Zero; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
private static (Assembly, PluginLoadContextWrapper) LoadPlugin(string assemblyPath, bool isCollectible) |
| 202 |
{ |
| 203 |
string assemblyName = Path.GetFileNameWithoutExtension(assemblyPath); |
| 204 |
|
| 205 |
var sharedAssemblies = new List<string>(); |
| 206 |
|
| 207 |
foreach (var sharedAssembly in SharedAssemblies) |
| 208 |
{ |
| 209 |
string? sharedAssemblyName = sharedAssembly.Name; |
| 210 |
if (sharedAssemblyName != null) |
| 211 |
sharedAssemblies.Add(sharedAssemblyName); |
| 212 |
} |
| 213 |
|
| 214 |
return PluginLoadContextWrapper.CreateAndLoadFromAssemblyName( |
| 215 |
new AssemblyName(assemblyName), assemblyPath, sharedAssemblies, MainLoadContext, isCollectible); |
| 216 |
} |
| 217 |
|
| 218 |
[UnmanagedCallersOnly] |
| 219 |
private static godot_bool UnloadProjectPlugin() |
| 220 |
{ |
| 221 |
try |
| 222 |
{ |
| 223 |
return UnloadPlugin(ref _projectLoadContext).ToGodotBool(); |
| 224 |
} |
| 225 |
catch (Exception e) |
| 226 |
{ |
| 227 |
Console.Error.WriteLine(e); |
| 228 |
return godot_bool.False; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
private static bool UnloadPlugin(ref PluginLoadContextWrapper? pluginLoadContext) |
| 233 |
{ |
| 234 |
try |
| 235 |
{ |
| 236 |
if (pluginLoadContext == null) |
| 237 |
return true; |
| 238 |
|
| 239 |
if (!pluginLoadContext.IsCollectible) |
| 240 |
{ |
| 241 |
Console.Error.WriteLine("Cannot unload a non-collectible assembly load context."); |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
Console.WriteLine("Unloading assembly load context..."); |
| 246 |
|
| 247 |
pluginLoadContext.Unload(); |
| 248 |
|
| 249 |
int startTimeMs = Environment.TickCount; |
| 250 |
bool takingTooLong = false; |
| 251 |
|
| 252 |
while (pluginLoadContext.IsAlive) |
| 253 |
{ |
| 254 |
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); |
| 255 |
GC.WaitForPendingFinalizers(); |
| 256 |
|
| 257 |
if (!pluginLoadContext.IsAlive) |
| 258 |
break; |
| 259 |
|
| 260 |
int elapsedTimeMs = Environment.TickCount - startTimeMs; |
| 261 |
|
| 262 |
if (!takingTooLong && elapsedTimeMs >= 200) |
| 263 |
{ |
| 264 |
takingTooLong = true; |
| 265 |
|
| 266 |
|
| 267 |
Console.Error.WriteLine("Assembly unloading is taking longer than expected..."); |
| 268 |
} |
| 269 |
else if (elapsedTimeMs >= 1000) |
| 270 |
{ |
| 271 |
|
| 272 |
Console.Error.WriteLine( |
| 273 |
"Failed to unload assemblies. Possible causes: Strong GC handles, running threads, etc."); |
| 274 |
|
| 275 |
return false; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
Console.WriteLine("Assembly load context unloaded successfully."); |
| 280 |
|
| 281 |
pluginLoadContext = null; |
| 282 |
return true; |
| 283 |
} |
| 284 |
catch (Exception e) |
| 285 |
{ |
| 286 |
|
| 287 |
Console.Error.WriteLine(e); |
| 288 |
return false; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |