Преглед на файлове

Prevent crashes inside preloader from echoing

Bepis преди 6 години
родител
ревизия
d9db464093
променени са 2 файла, в които са добавени 32 реда и са изтрити 25 реда
  1. 6 2
      BepInEx/Bootstrap/Preloader.cs
  2. 26 23
      BepInEx/ConsoleUtil/ConsoleWindow.cs

+ 6 - 2
BepInEx/Bootstrap/Preloader.cs

@@ -107,8 +107,12 @@ namespace BepInEx.Bootstrap
 
 				try
 				{
-					AllocateConsole();
-					Console.Write(PreloaderLog);
+					if (!ConsoleWindow.IsAttatched)
+					{
+						//if we've already attached the console, then the log will already be written to the console
+						AllocateConsole();
+						Console.Write(PreloaderLog);
+					}
 				}
 				finally
 				{

+ 26 - 23
BepInEx/ConsoleUtil/ConsoleWindow.cs

@@ -12,14 +12,15 @@ namespace UnityInjector.ConsoleUtil
 {
     internal class ConsoleWindow
     {
-        private static bool _attached;
+        public static bool IsAttatched { get; private set; }
         private static IntPtr _cOut;
         private static IntPtr _oOut;
 
         public static void Attach()
         {
-            if (_attached)
+            if (IsAttatched)
                 return;
+
             if (_oOut == IntPtr.Zero)
                 _oOut = GetStdHandle(-11);
 
@@ -40,36 +41,38 @@ namespace UnityInjector.ConsoleUtil
             if (!SetStdHandle(-11, _cOut))
                 throw new Exception("SetStdHandle() failed");
             Init();
-            _attached = true;
+
+	        IsAttatched = true;
         }
 
         public static string Title
         {
             set
             {
-                if (_attached)
-                {
-                    if (value == null)
-                    {
-                        throw new ArgumentNullException(nameof(value));
-                    }
-                    if (value.Length > 24500)
-                    {
-                        throw new InvalidOperationException("Console title too long");
-                    }
-
-                    if (!SetConsoleTitle(value))
-                    {
-                        throw new InvalidOperationException("Console title invalid");
-                    }
-                }
+	            if (!IsAttatched)
+		            return;
+
+	            if (value == null)
+	            {
+		            throw new ArgumentNullException(nameof(value));
+	            }
+	            if (value.Length > 24500)
+	            {
+		            throw new InvalidOperationException("Console title too long");
+	            }
+
+	            if (!SetConsoleTitle(value))
+	            {
+		            throw new InvalidOperationException("Console title invalid");
+	            }
             }
         }
 
         public static void Detach()
         {
-            if (!_attached)
+            if (!IsAttatched)
                 return;
+
             if (!CloseHandle(_cOut))
                 throw new Exception("CloseHandle() failed");
             _cOut = IntPtr.Zero;
@@ -78,8 +81,8 @@ namespace UnityInjector.ConsoleUtil
             if (!SetStdHandle(-11, _oOut))
                 throw new Exception("SetStdHandle() failed");
             Init();
-            _attached = false;
-            
+
+	        IsAttatched = false;
         }
 
         [DllImport("user32.dll")]
@@ -131,4 +134,4 @@ namespace UnityInjector.ConsoleUtil
         [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Auto, SetLastError = true)]
         private static extern bool SetConsoleTitle(string title);
     }
-}
+}