Fix resolving managed scripts references in Editor

This commit is contained in:
2023-05-11 22:10:26 +03:00
parent 4e78b49cac
commit cbe07b842f
2 changed files with 26 additions and 12 deletions

View File

@@ -908,12 +908,7 @@ namespace FlaxEngine.Interop
while (unloading)
System.Threading.Thread.Sleep(1);
#if FLAX_EDITOR
var isCollectible = true;
#else
var isCollectible = false;
#endif
scriptingAssemblyLoadContext = new AssemblyLoadContext("Flax", isCollectible);
InitScriptingAssemblyLoadContext();
DelegateHelpers.InitMethods();
}

View File

@@ -78,6 +78,19 @@ namespace FlaxEngine.Interop
return nativeLibrary;
}
private static void InitScriptingAssemblyLoadContext()
{
#if FLAX_EDITOR
var isCollectible = true;
#else
var isCollectible = false;
#endif
scriptingAssemblyLoadContext = new AssemblyLoadContext("Flax", isCollectible);
#if FLAX_EDITOR
scriptingAssemblyLoadContext.Resolving += ScriptingAssemblyLoadContext_Resolving;
#endif
}
[UnmanagedCallersOnly]
internal static unsafe void Init()
{
@@ -89,15 +102,21 @@ namespace FlaxEngine.Interop
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
#if FLAX_EDITOR
var isCollectible = true;
#else
var isCollectible = false;
#endif
scriptingAssemblyLoadContext = new AssemblyLoadContext("Flax", isCollectible);
InitScriptingAssemblyLoadContext();
DelegateHelpers.InitMethods();
}
private static Assembly? ScriptingAssemblyLoadContext_Resolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
{
// FIXME: There should be a better way to resolve the path to EditorTargetPath where the dependencies are stored
string editorTargetPath = Path.GetDirectoryName(nativeLibraryPaths.Keys.First(x => x != "FlaxEngine"));
var assemblyPath = Path.Combine(editorTargetPath, assemblyName.Name + ".dll");
if (File.Exists(assemblyPath))
return assemblyLoadContext.LoadFromAssemblyPath(assemblyPath);
return null;
}
[UnmanagedCallersOnly]
internal static unsafe void Exit()
{