diff --git a/MCGalaxy/MCGalaxy_.csproj b/MCGalaxy/MCGalaxy_.csproj index 1932b4a08..c22715fb3 100644 --- a/MCGalaxy/MCGalaxy_.csproj +++ b/MCGalaxy/MCGalaxy_.csproj @@ -666,6 +666,7 @@ + diff --git a/MCGalaxy/Scripting/Scripting.cs b/MCGalaxy/Scripting/Scripting.cs index 2d2727011..f5f3be350 100644 --- a/MCGalaxy/Scripting/Scripting.cs +++ b/MCGalaxy/Scripting/Scripting.cs @@ -21,6 +21,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Reflection; +using MCGalaxy.Platform; namespace MCGalaxy.Scripting { @@ -50,13 +51,11 @@ namespace MCGalaxy.Scripting } // only used for resolving plugin DLLs depending on other plugin DLLs - static Assembly ResolvePluginAssembly(object sender, ResolveEventArgs args) { - Assembly requestingAssembly = null; - // This property only exists in .NET framework 4.0 and later + static Assembly ResolvePluginAssembly(object sender, ResolveEventArgs args) { #if !NET_20 - requestingAssembly = args.RequestingAssembly; -#endif - + // This property only exists in .NET framework 4.0 and later + Assembly requestingAssembly = args.RequestingAssembly; + if (requestingAssembly == null) return null; if (!IsPluginDLL(requestingAssembly)) return null; @@ -68,21 +67,12 @@ namespace MCGalaxy.Scripting 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 + Assembly coreRef = DotNetBackend.ResolvePluginReference(args.Name); + if (coreRef != null) return coreRef; Logger.Log(LogType.Warning, "Custom command/plugin [{0}] tried to load [{1}], but it could not be found", requestingAssembly.FullName, args.Name); +#endif return null; } diff --git a/MCGalaxy/Server/Server.cs b/MCGalaxy/Server/Server.cs index 419ee6c35..516b4175b 100644 --- a/MCGalaxy/Server/Server.cs +++ b/MCGalaxy/Server/Server.cs @@ -270,17 +270,8 @@ namespace MCGalaxy public static string GetRestartPath() { #if MCG_STANDALONE return GetRuntimeProcessExePath(); -#elif !NETSTANDARD - return RestartPath; #else - // NET core/5/6 executables tend to use the following structure: - // MCGalaxyCLI_core --> MCGalaxyCLI_core.dll - // in this case, 'RestartPath' will include '.dll' since this file - // is actually the managed assembly, but we need to remove '.dll' - // as the actual executable which must be started is the non .dll file - string path = RestartPath; - if (path.CaselessEnds(".dll")) path = path.Substring(0, path.Length - 4); - return path; + return DotNetBackend.GetExePath(RestartPath); #endif } diff --git a/MCGalaxy/util/NetBackend.cs b/MCGalaxy/util/NetBackend.cs new file mode 100644 index 000000000..61fbb4465 --- /dev/null +++ b/MCGalaxy/util/NetBackend.cs @@ -0,0 +1,76 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + https://opensource.org/license/ecl-2-0/ + https://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; + +namespace MCGalaxy.Platform +{ +#if !NETSTANDARD + public static class DotNetBackend + { + public static void Init() { } + + public static string GetExePath(string path) { + return path; + } + + public static Assembly ResolvePluginReference(string name) { + return null; + } + } +#else + public static class DotNetBackend + { + public static void Init() { + NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), ImportResolver); + } + + static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { + return IntPtr.Zero; + } + + + public static string GetExePath(string path) { + // NET core/5/6 executables tend to use the following structure: + // MCGalaxyCLI_core --> MCGalaxyCLI_core.dll + // in this case, 'RestartPath' will include '.dll' since this file + // is actually the managed assembly, but we need to remove '.dll' + // as the actual executable which must be started is the non .dll file + if (path.CaselessEnds(".dll")) path = path.Substring(0, path.Length - 4); + return path; + } + + public static Assembly ResolvePluginReference(string name) { + // 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(name); + string path = name.Name + ".dll"; + if (File.Exists(path)) return Assembly.LoadFrom(path); + } catch (Exception ex) { + Logger.LogError("Resolving plugin DLL reference", ex); + } + return null; + } + } +#endif +} \ No newline at end of file