-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathModAssemblyLoadContext.cs
More file actions
54 lines (46 loc) · 1.96 KB
/
ModAssemblyLoadContext.cs
File metadata and controls
54 lines (46 loc) · 1.96 KB
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
54
using KSA;
using System.Reflection;
using System.Runtime.Loader;
namespace StarMap.Core.ModRepository
{
internal class ModAssemblyLoadContext : AssemblyLoadContext
{
private readonly AssemblyLoadContext _coreAssemblyLoadContext;
private readonly AssemblyDependencyResolver _modDependencyResolver;
public ModAssemblyLoadContext(string modId, string modDirectory, AssemblyLoadContext coreAssemblyContext)
: base(isCollectible: true)
{
_coreAssemblyLoadContext = coreAssemblyContext;
_modDependencyResolver = new AssemblyDependencyResolver(
Path.GetFullPath(Path.Combine(modDirectory, modId + ".dll"))
);
}
protected override Assembly? Load(AssemblyName assemblyName)
{
var existingInDefault = AssemblyLoadContext.Default.Assemblies
.FirstOrDefault(a => string.Equals(a.GetName().Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase));
if (existingInDefault != null)
return existingInDefault;
var existingInGameContext = _coreAssemblyLoadContext?.Assemblies
.FirstOrDefault(a => string.Equals(a.GetName().Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase));
if (existingInGameContext != null)
return existingInGameContext;
if (_coreAssemblyLoadContext != null)
{
try
{
var asm = _coreAssemblyLoadContext.LoadFromAssemblyName(assemblyName);
if (asm != null)
return asm;
}
catch (FileNotFoundException)
{
}
}
var foundPath = _modDependencyResolver.ResolveAssemblyToPath(assemblyName);
if (foundPath is null)
return null;
return LoadFromAssemblyPath(Path.GetFullPath(foundPath));
}
}
}