From c8b174efdefee39cbf35aa0a6b388f9c8a67ce4b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 16 Dec 2023 10:43:17 +1100 Subject: [PATCH] dotnet build: Support resolving assembly references from server folder for plugins --- MCGalaxy/Scripting/Scripting.cs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/MCGalaxy/Scripting/Scripting.cs b/MCGalaxy/Scripting/Scripting.cs index 4022dfe57..2d2727011 100644 --- a/MCGalaxy/Scripting/Scripting.cs +++ b/MCGalaxy/Scripting/Scripting.cs @@ -51,9 +51,14 @@ namespace MCGalaxy.Scripting // only used for resolving plugin DLLs depending on other plugin DLLs static Assembly ResolvePluginAssembly(object sender, ResolveEventArgs args) { - #if !NET_20 - if (args.RequestingAssembly == null) return null; - if (!IsPluginDLL(args.RequestingAssembly)) return null; + Assembly requestingAssembly = null; + // This property only exists in .NET framework 4.0 and later +#if !NET_20 + requestingAssembly = args.RequestingAssembly; +#endif + + if (requestingAssembly == null) return null; + if (!IsPluginDLL(requestingAssembly)) return null; Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assem in assemblies) @@ -61,11 +66,23 @@ namespace MCGalaxy.Scripting if (!IsPluginDLL(assem)) continue; if (args.Name == assem.FullName) return assem; - } - + } + +#if NETSTANDARD + // When there is a .deps.json, dotnet won't automatically always try looking in application's directory to resolve references + // https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/default-probing?source=recommendations#how-are-the-properties-populated + + try { + AssemblyName name = new AssemblyName(args.Name); + string path = name.Name + ".dll"; + if (File.Exists(path)) return Assembly.LoadFrom(path); + } catch (Exception ex) { + Logger.LogError("Resolving plugin DLL reference", ex); + } +#endif + Logger.Log(LogType.Warning, "Custom command/plugin [{0}] tried to load [{1}], but it could not be found", - args.RequestingAssembly.FullName, args.Name); - #endif + requestingAssembly.FullName, args.Name); return null; }