Skip to content

Commit

Permalink
Move net platform specific functionality to dedicated class
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jul 3, 2024
1 parent 9c0d858 commit f427e1d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 28 deletions.
1 change: 1 addition & 0 deletions MCGalaxy/MCGalaxy_.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
26 changes: 8 additions & 18 deletions MCGalaxy/Scripting/Scripting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using MCGalaxy.Platform;

namespace MCGalaxy.Scripting
{
Expand Down Expand Up @@ -50,13 +51,11 @@ public static class IScripting
}

// 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;

Expand All @@ -68,21 +67,12 @@ public static class IScripting
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;
}

Expand Down
11 changes: 1 addition & 10 deletions MCGalaxy/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,8 @@ public sealed partial class Server
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
}

Expand Down
76 changes: 76 additions & 0 deletions MCGalaxy/util/NetBackend.cs
Original file line number Diff line number Diff line change
@@ -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);

Check failure on line 43 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet6

The name 'NativeLibrary' does not exist in the current context

Check failure on line 43 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet6

The name 'NativeLibrary' does not exist in the current context

Check failure on line 43 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet8

The name 'NativeLibrary' does not exist in the current context

Check failure on line 43 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet8

The name 'NativeLibrary' does not exist in the current context
}

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);

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet6

A local or parameter named 'name' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet6

Argument 1: cannot convert from 'System.Reflection.AssemblyName' to 'string'

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet6

Use of unassigned local variable 'name'

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet6

A local or parameter named 'name' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet6

Argument 1: cannot convert from 'System.Reflection.AssemblyName' to 'string'

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet6

Use of unassigned local variable 'name'

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet8

A local or parameter named 'name' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet8

Argument 1: cannot convert from 'System.Reflection.AssemblyName' to 'string'

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet8

Use of unassigned local variable 'name'

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet8

A local or parameter named 'name' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet8

Argument 1: cannot convert from 'System.Reflection.AssemblyName' to 'string'

Check failure on line 66 in MCGalaxy/util/NetBackend.cs

View workflow job for this annotation

GitHub Actions / build-dotnet8

Use of unassigned local variable '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
}

0 comments on commit f427e1d

Please sign in to comment.