Move net platform specific functionality to dedicated class

This commit is contained in:
UnknownShadow200 2024-07-03 22:36:15 +10:00
parent 9c0d858434
commit f427e1db09
4 changed files with 86 additions and 28 deletions

View File

@ -666,6 +666,7 @@
<Compile Include="Server\Maintenance\ZipWriter.cs" />
<Compile Include="util\Formatting\Wildcard.cs" />
<Compile Include="util\ImageUtils.cs" />
<Compile Include="util\NetBackend.cs" />
<Compile Include="util\NumberUtils.cs" />
<Compile Include="util\OperatingSystem.cs" />
<Compile Include="Server\Server.cs" />

View File

@ -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;
}

View File

@ -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
}

View File

@ -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
}