Fix externally referenced DLLs in scripts

External DLL references weren't being used, this commit fixes them so they are used.
This commit is contained in:
breadbyte 2022-10-21 00:29:46 +08:00
parent 78f9c35800
commit e006943535
No known key found for this signature in database
GPG key ID: 81897669486FFAFC
3 changed files with 60 additions and 9 deletions

View file

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace MinecraftClient.Scripting;
public static class AssemblyResolver {
private static Dictionary<string, string> ScriptAssemblies = new();
static AssemblyResolver() {
// Manually resolve assemblies that .NET can't resolve automatically.
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
var asmReqName = new AssemblyName(args.Name);
// Check the script-referenced assemblies if we have the DLL that is required.
foreach (var dll in ScriptAssemblies)
{
// If we have the assembly, load it.
if (asmReqName.FullName == dll.Key)
{
return Assembly.LoadFile(dll.Value);
}
}
ConsoleIO.WriteLogLine($"[Script Error] Failed to resolve assembly {args.Name} (are you missing a DLL file?)");
return null;
};
}
internal static void AddAssembly(string AssemblyFullName, string AssemblyPath)
{
if (ScriptAssemblies.ContainsKey(AssemblyFullName))
return;
ScriptAssemblies.Add(AssemblyFullName, AssemblyPath);
}
}