Browse Source

Check for SRE automatically

ghorsington 4 years ago
parent
commit
b56c832cfc
2 changed files with 57 additions and 5 deletions
  1. 38 5
      BepInEx.Preloader/Preloader.cs
  2. 19 0
      BepInEx/Utility.cs

+ 38 - 5
BepInEx.Preloader/Preloader.cs

@@ -40,17 +40,36 @@ namespace BepInEx.Preloader
 			);
 		}
 
+		private static void TryDo(Action action, out Exception exception)
+		{
+			exception = null;
+			try
+			{
+				action();
+			}
+			catch (Exception e)
+			{
+				exception = e;
+			}
+		}
+
 		public static void Run()
 		{
 			try
 			{
 				AllocateConsole();
 
-				if (ConfigShimHarmony.Value)
-					HarmonyDetourBridge.Init();
+				TryDo(() =>
+				{
+					if (!Utility.CLRSupportsDynamicAssemblies || ConfigShimHarmony.Value)
+						HarmonyDetourBridge.Init();
+                }, out var harmonyBridgeException);
 
-				if (ConfigApplyRuntimePatches.Value)
-					UnityPatches.Apply();
+				TryDo(() =>
+				{
+					if (ConfigApplyRuntimePatches.Value)
+						UnityPatches.Apply();
+                }, out var runtimePatchException);
 
 				Logger.Sources.Add(TraceLogSource.CreateSource());
 
@@ -72,7 +91,21 @@ namespace BepInEx.Preloader
 
 				Logger.LogInfo($"Running under Unity v{FileVersionInfo.GetVersionInfo(Paths.ExecutablePath).FileVersion}");
 				Logger.LogInfo($"CLR runtime version: {Environment.Version}");
-				Logger.LogMessage("Preloader started");
+				Logger.LogInfo($"Supports SRE: {Utility.CLRSupportsDynamicAssemblies}");
+
+				if (harmonyBridgeException != null)
+				{
+					Logger.LogWarning("Failed to enable fix for Harmony for .NET Standard API. See more info in the output log.");
+					Logger.LogDebug(harmonyBridgeException);
+				}
+
+				if (runtimePatchException != null)
+				{
+					Logger.LogWarning("Failed to apply runtime patches for Mono. See more info in the output log.");
+					Logger.LogDebug(runtimePatchException);
+				}
+
+                Logger.LogMessage("Preloader started");
 
 				AssemblyPatcher.AddPatcher(new PatcherPlugin
 				{

+ 19 - 0
BepInEx/Utility.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Reflection;
+using System.Reflection.Emit;
 using Mono.Cecil;
 
 namespace BepInEx
@@ -13,6 +14,24 @@ namespace BepInEx
 	public static class Utility
 	{
 		/// <summary>
+		/// Whether current Common Language Runtime supports dynamic method generation using <see cref="System.Reflection.Emit"/> namespace.
+		/// </summary>
+		public static bool CLRSupportsDynamicAssemblies { get; }
+
+		static Utility()
+		{
+			try
+			{
+				var m = new DynamicMethod("SRE_Test", null, null);
+				CLRSupportsDynamicAssemblies = true;
+			}
+			catch (PlatformNotSupportedException)
+			{
+				CLRSupportsDynamicAssemblies = false;
+			}
+		}
+
+		/// <summary>
 		/// Combines multiple paths together, as the specific method is not available in .NET 3.5.
 		/// </summary>
 		/// <param name="parts">The multiple paths to combine together.</param>