-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptLoadContext.cs
53 lines (45 loc) · 2 KB
/
ScriptLoadContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Reflection;
using System.Runtime.Loader;
using RenderCore.Interop;
using ScriptingInterface;
using SharpGen.Runtime;
namespace ScriptingCore;
public class ScriptLoadContext : AssemblyLoadContext
{
private readonly AssemblyDependencyResolver _resolver;
public ScriptLoadContext(string path) : base(isCollectible: true)
{
_resolver = new AssemblyDependencyResolver(path);
}
protected override Assembly? Load(AssemblyName assemblyName)
{
// Crucial!
// This prevents the script assembly from reloading interface DLL into it's own ALC,
// ensures the interface DLL has only one instance.
if (assemblyName.FullName == typeof(IScript).Assembly.FullName)
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var interfaceAsm = loadedAssemblies.First(assembly => assembly.FullName == assemblyName.FullName);
return interfaceAsm;
}
if (assemblyName.FullName == typeof(IDeviceContext).Assembly.FullName)
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var interfaceAsm = loadedAssemblies.First(assembly => assembly.FullName == assemblyName.FullName);
return interfaceAsm;
}
if (assemblyName.FullName == typeof(CppObject).Assembly.FullName)
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var interfaceAsm = loadedAssemblies.First(assembly => assembly.FullName == assemblyName.FullName);
return interfaceAsm;
}
var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
return assemblyPath != null ? LoadFromAssemblyPath(assemblyPath) : null;
}
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
var libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
return libraryPath != null ? LoadUnmanagedDllFromPath(libraryPath) : IntPtr.Zero;
}
}