Browse Source

Fix cout output encoding being reset by Unity

ghorsington 5 years ago
parent
commit
2364239105
3 changed files with 29 additions and 27 deletions
  1. 3 15
      BepInEx.Preloader/Preloader.cs
  2. 12 11
      BepInEx/Bootstrap/Chainloader.cs
  3. 14 1
      BepInEx/ConsoleUtil/ConsoleWindow.cs

+ 3 - 15
BepInEx.Preloader/Preloader.cs

@@ -295,7 +295,7 @@ namespace BepInEx.Preloader
 		/// </summary>
 		public static void AllocateConsole()
 		{
-			if (!ConfigConsoleEnabled.Value)
+			if (!ConsoleWindow.ConfigConsoleEnabled.Value)
 				return;
 
 			try
@@ -304,7 +304,7 @@ namespace BepInEx.Preloader
 
 				var encoding = (uint)Encoding.UTF8.CodePage;
 
-				if (ConfigConsoleShiftJis.Value)
+				if (ConsoleWindow.ConfigConsoleShiftJis.Value)
 					encoding = 932;
 
 				ConsoleEncoding.ConsoleCodePage = encoding;
@@ -354,18 +354,6 @@ namespace BepInEx.Preloader
 			"Redirects text from Console.Out during preloader patch loading to the BepInEx logging system.",
 			true);
 
-		private static readonly ConfigWrapper<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Wrap(
-			"Logging.Console",
-			"Enabled",
-			"Enables showing a console for log output.",
-			false);
-
-		private static readonly ConfigWrapper<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Wrap(
-			"Logging.Console",
-			"ShiftJisEncoding",
-			"If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.",
-			false);
-
-#endregion
+		#endregion
 	}
 }

+ 12 - 11
BepInEx/Bootstrap/Chainloader.cs

@@ -33,9 +33,9 @@ namespace BepInEx.Bootstrap
 		private static bool _initialized = false;
 
 		/// <summary>
-		/// Initializes BepInEx to be able to start the chainloader.
-		/// </summary>
-		public static void Initialize(string containerExePath, bool startConsole = true)
+        /// Initializes BepInEx to be able to start the chainloader.
+        /// </summary>
+        public static void Initialize(string containerExePath, bool startConsole = true)
 		{
 			if (_initialized)
 				return;
@@ -45,23 +45,24 @@ namespace BepInEx.Bootstrap
 
 			Paths.SetPluginPath(ConfigPluginsDirectory.Value);
 
-			//Start logging
-
-			if (startConsole)
+            //Start logging
+            if (ConsoleWindow.ConfigConsoleEnabled.Value && startConsole)
 			{
 				ConsoleWindow.Attach();
-
-				ConsoleEncoding.ConsoleCodePage = (uint)Encoding.UTF8.CodePage;
-				Console.OutputEncoding = Encoding.UTF8;
 				Logger.Listeners.Add(new ConsoleLogListener());
-			}
+            }
 
 			//Fix for standard output getting overwritten by UnityLogger
 			if (ConsoleWindow.StandardOut != null)
+			{
 				Console.SetOut(ConsoleWindow.StandardOut);
 
+				var encoding = ConsoleWindow.ConfigConsoleShiftJis.Value ? 932 : (uint)Encoding.UTF8.CodePage;
+				ConsoleEncoding.ConsoleCodePage = encoding;
+				Console.OutputEncoding = ConsoleEncoding.GetEncoding(encoding);
+			}
 
-			Logger.Listeners.Add(new UnityLogListener());
+            Logger.Listeners.Add(new UnityLogListener());
 			Logger.Listeners.Add(new DiskLogListener());
 
 			if (!TraceLogSource.IsListening)

+ 14 - 1
BepInEx/ConsoleUtil/ConsoleWindow.cs

@@ -7,12 +7,25 @@ using System;
 using System.IO;
 using System.Runtime.InteropServices;
 using System.Text;
+using BepInEx.Configuration;
 
 namespace UnityInjector.ConsoleUtil
 {
 	internal class ConsoleWindow
 	{
-		public static bool IsAttached { get; private set; }
+		public static readonly ConfigWrapper<bool> ConfigConsoleEnabled = ConfigFile.CoreConfig.Wrap(
+			"Logging.Console",
+			"Enabled",
+			"Enables showing a console for log output.",
+			false);
+
+		public static readonly ConfigWrapper<bool> ConfigConsoleShiftJis = ConfigFile.CoreConfig.Wrap(
+			"Logging.Console",
+			"ShiftJisEncoding",
+			"If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding.",
+			false);
+
+        public static bool IsAttached { get; private set; }
 		private static IntPtr _cOut;
 		private static IntPtr _oOut;