Jelajahi Sumber

Improve platform check and fix Unity engine check for Linux

Bepis 4 tahun lalu
induk
melakukan
2723687942

+ 10 - 1
BepInEx.Preloader/Preloader.cs

@@ -11,6 +11,7 @@ using HarmonyLib;
 using Mono.Cecil;
 using Mono.Cecil.Cil;
 using MonoMod.RuntimeDetour;
+using MonoMod.Utils;
 using MethodAttributes = Mono.Cecil.MethodAttributes;
 
 namespace BepInEx.Preloader
@@ -71,7 +72,7 @@ namespace BepInEx.Preloader
 					Logger.LogMessage(attribute.Info);
 				}
 
-				Logger.LogInfo($"Running under Unity v{FileVersionInfo.GetVersionInfo(Paths.ExecutablePath).FileVersion}");
+				Logger.LogInfo($"Running under Unity v{GetUnityVersion()}");
 				Logger.LogInfo($"CLR runtime version: {Environment.Version}");
 				Logger.LogInfo($"Supports SRE: {Utility.CLRSupportsDynamicAssemblies}");
 
@@ -239,6 +240,14 @@ namespace BepInEx.Preloader
 			}
 		}
 
+		public static string GetUnityVersion()
+		{
+			if (Utility.CurrentOs == Platform.Windows)
+				return FileVersionInfo.GetVersionInfo(Paths.ExecutablePath).FileVersion;
+
+			return $"Unknown ({(IsPostUnity2017 ? "post" : "pre")}-2017)";
+		}
+
 		#region Config
 
 		private static readonly ConfigEntry<string> ConfigEntrypointAssembly = ConfigFile.CoreConfig.Bind(

+ 2 - 1
BepInEx.Preloader/RuntimeFixes/XTermFix.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Reflection.Emit;
 using HarmonyLib;
+using MonoMod.Utils;
 
 namespace BepInEx.Preloader.RuntimeFixes
 {
@@ -10,7 +11,7 @@ namespace BepInEx.Preloader.RuntimeFixes
 	{
 		public static void Apply()
 		{
-			if (Environment.OSVersion.Platform != PlatformID.Unix)
+			if (Utility.CurrentOs == Platform.Windows)
 				return;
 
 			if (typeof(Console).Assembly.GetType("System.ConsoleDriver") == null)

+ 6 - 0
BepInEx/Bootstrap/Chainloader.cs

@@ -8,6 +8,7 @@ using System.Linq;
 using System.Reflection;
 using System.Text.RegularExpressions;
 using Mono.Cecil;
+using MonoMod.Utils;
 using UnityEngine;
 using Logger = BepInEx.Logging.Logger;
 
@@ -112,6 +113,11 @@ namespace BepInEx.Bootstrap
 			if (logListener != null)
 				Logger.Listeners.Add(logListener);
 
+			if (Utility.CurrentOs == Platform.Linux)
+			{
+				Logger.LogInfo($"Detected Unity version: v{Application.unityVersion}");
+			}
+
 			Logger.LogMessage("Chainloader ready");
 
 			_initialized = true;

+ 5 - 4
BepInEx/Console/ConsoleManager.cs

@@ -3,6 +3,7 @@ using System.IO;
 using System.Text;
 using BepInEx.Configuration;
 using BepInEx.Unix;
+using MonoMod.Utils;
 
 namespace BepInEx
 {
@@ -28,16 +29,16 @@ namespace BepInEx
 
 		public static void Initialize(bool alreadyActive)
 		{
-			switch (Environment.OSVersion.Platform)
+			switch (Utility.CurrentOs)
 			{
-				case PlatformID.MacOSX:
-				case PlatformID.Unix:
+				case Platform.MacOS:
+				case Platform.Linux:
 				{
 					Driver = new LinuxConsoleDriver();
 					break;
 				}
 
-				case PlatformID.Win32NT:
+				case Platform.Windows:
 				{
 					Driver = new WindowsConsoleDriver();
 					break;

+ 28 - 0
BepInEx/Utility.cs

@@ -5,6 +5,7 @@ using System.Linq;
 using System.Reflection;
 using System.Reflection.Emit;
 using Mono.Cecil;
+using MonoMod.Utils;
 
 namespace BepInEx
 {
@@ -34,6 +35,8 @@ namespace BepInEx
 			{
 				// Suppress ArgumentNullException
 			}
+
+			CheckPlatform();
 		}
 
 		/// <summary>
@@ -237,5 +240,30 @@ namespace BepInEx
 				return false;
 			}
 		}
+
+
+
+		// Adapted from https://github.com/MonoMod/MonoMod.Common/blob/master/Utils/PlatformHelper.cs#L13
+		private static void CheckPlatform()
+		{
+			var pPlatform = typeof(Environment).GetProperty("Platform", BindingFlags.NonPublic | BindingFlags.Static);
+			string platId = pPlatform != null ? pPlatform.GetValue(null, new object[0]).ToString() : Environment.OSVersion.Platform.ToString();
+			platId = platId.ToLowerInvariant();
+
+			var cur = Platform.Unknown;
+			if (platId.Contains("win"))
+				cur = Platform.Windows;
+			else if (platId.Contains("mac") || platId.Contains("osx"))
+				cur = Platform.MacOS;
+			else if (platId.Contains("lin") || platId.Contains("unix"))
+				cur = Platform.Linux;
+
+			CurrentOs = cur;
+		}
+
+		/// <summary>
+		/// Current OS BepInEx is running on.
+		/// </summary>
+		public static Platform CurrentOs { get; private set; }
 	}
 }