From bd5a53dafc20e8a0041cb4211662d7df9333ab4d Mon Sep 17 00:00:00 2001 From: ghorsington Date: Sat, 14 May 2022 22:59:01 +0300 Subject: [PATCH] ConsoleManager: refactor useWinApiEncoder -> useManagedEncoder --- BepInEx.Core/Console/ConsoleManager.cs | 4 ++-- BepInEx.Core/Console/IConsoleDriver.cs | 2 +- BepInEx.Core/Console/Unix/LinuxConsoleDriver.cs | 2 +- BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs | 11 ++++++----- BepInEx.IL2CPP/Preloader.cs | 2 +- BepInEx.NetCore/HookEntrypoint.cs | 2 +- BepInEx.NetLauncher/Program.cs | 4 ++-- BepInEx.Preloader.Unity/UnityPreloader.cs | 2 +- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/BepInEx.Core/Console/ConsoleManager.cs b/BepInEx.Core/Console/ConsoleManager.cs index 74f2bca..4ebd2e4 100644 --- a/BepInEx.Core/Console/ConsoleManager.cs +++ b/BepInEx.Core/Console/ConsoleManager.cs @@ -94,7 +94,7 @@ public static class ConsoleManager public static TextWriter ConsoleStream => Driver?.ConsoleOut; - public static void Initialize(bool alreadyActive, bool useWinApiEncoder) + public static void Initialize(bool alreadyActive, bool useManagedEncoder) { if (PlatformHelper.Is(Platform.Unix)) Driver = new LinuxConsoleDriver(); @@ -104,7 +104,7 @@ public static class ConsoleManager throw new PlatformNotSupportedException("Was unable to determine console driver for platform " + PlatformHelper.Current); - Driver.Initialize(alreadyActive, useWinApiEncoder); + Driver.Initialize(alreadyActive, useManagedEncoder); } private static void DriverCheck() diff --git a/BepInEx.Core/Console/IConsoleDriver.cs b/BepInEx.Core/Console/IConsoleDriver.cs index 2cdc38c..ecc1ac6 100644 --- a/BepInEx.Core/Console/IConsoleDriver.cs +++ b/BepInEx.Core/Console/IConsoleDriver.cs @@ -13,7 +13,7 @@ internal interface IConsoleDriver void PreventClose(); - void Initialize(bool alreadyActive, bool useWinApiEncoder); + void Initialize(bool alreadyActive, bool useManagedEncoder); // Apparently Windows code-pages work in Mono. // https://stackoverflow.com/a/33456543 diff --git a/BepInEx.Core/Console/Unix/LinuxConsoleDriver.cs b/BepInEx.Core/Console/Unix/LinuxConsoleDriver.cs index afa0929..96ff64f 100644 --- a/BepInEx.Core/Console/Unix/LinuxConsoleDriver.cs +++ b/BepInEx.Core/Console/Unix/LinuxConsoleDriver.cs @@ -46,7 +46,7 @@ internal class LinuxConsoleDriver : IConsoleDriver // Not supported by all distros } - public void Initialize(bool alreadyActive, bool useWinApiEncoder) + public void Initialize(bool alreadyActive, bool useManagedEncoder) { // Console is always considered active on Unix ConsoleActive = true; diff --git a/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs b/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs index 706da35..e0875d7 100644 --- a/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs +++ b/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs @@ -30,7 +30,7 @@ internal class WindowsConsoleDriver : IConsoleDriver .PropertyGetter(typeof(Console), nameof(Console.WindowWidth)) ?.CreateDelegate>(); - private bool useWinApiEncoder; + private bool useManagedEncoder; private int ConsoleWidth { @@ -68,10 +68,10 @@ internal class WindowsConsoleDriver : IConsoleDriver public bool ConsoleActive { get; private set; } public bool ConsoleIsExternal => true; - public void Initialize(bool alreadyActive, bool useWinApiEncoder) + public void Initialize(bool alreadyActive, bool useManagedEncoder) { ConsoleActive = alreadyActive || TryCheckConsoleExists(); - this.useWinApiEncoder = useWinApiEncoder; + this.useManagedEncoder = useManagedEncoder; if (ConsoleActive) { @@ -92,7 +92,8 @@ internal class WindowsConsoleDriver : IConsoleDriver // Make sure of ConsoleEncoding helper class because on some Monos // Encoding.GetEncoding throws NotImplementedException on most codepages // NOTE: We don't set Console.OutputEncoding because it resets any existing Console.Out writers - ConsoleEncoding.ConsoleCodePage = codepage; + if (!useManagedEncoder) + ConsoleEncoding.ConsoleCodePage = codepage; // If stdout exists, write to it, otherwise make it the same as console out // Not sure if this is needed? Does the original Console.Out still work? @@ -113,7 +114,7 @@ internal class WindowsConsoleDriver : IConsoleDriver var consoleOutStream = OpenFileStream(ConsoleWindow.ConsoleOutHandle); // Can't use Console.OutputEncoding because it can be null (i.e. not preference by user) ConsoleOut = new StreamWriter(consoleOutStream, - useWinApiEncoder ? ConsoleEncoding.OutputEncoding : Utility.UTF8NoBom) + useManagedEncoder ? Utility.UTF8NoBom : ConsoleEncoding.OutputEncoding) { AutoFlush = true }; diff --git a/BepInEx.IL2CPP/Preloader.cs b/BepInEx.IL2CPP/Preloader.cs index 1181ac7..43a5343 100644 --- a/BepInEx.IL2CPP/Preloader.cs +++ b/BepInEx.IL2CPP/Preloader.cs @@ -37,7 +37,7 @@ public static class Preloader { HarmonyBackendFix.Initialize(); - ConsoleManager.Initialize(false, false); + ConsoleManager.Initialize(false, true); PreloaderLog = new PreloaderConsoleListener(); Logger.Listeners.Add(PreloaderLog); diff --git a/BepInEx.NetCore/HookEntrypoint.cs b/BepInEx.NetCore/HookEntrypoint.cs index 9163985..cf979a7 100644 --- a/BepInEx.NetCore/HookEntrypoint.cs +++ b/BepInEx.NetCore/HookEntrypoint.cs @@ -55,7 +55,7 @@ namespace BepInEx.NetCore { internal static void PreloaderMain() { - ConsoleManager.Initialize(false, false); + ConsoleManager.Initialize(false, true); ConsoleManager.CreateConsole(); diff --git a/BepInEx.NetLauncher/Program.cs b/BepInEx.NetLauncher/Program.cs index f802311..2d69e4b 100644 --- a/BepInEx.NetLauncher/Program.cs +++ b/BepInEx.NetLauncher/Program.cs @@ -42,7 +42,7 @@ internal class Program filename = Process.GetCurrentProcess().MainModule.FileName; ResolveDirectories.Add(Path.Combine(Path.GetDirectoryName(filename), "BepInEx", "core")); #endif - + AppDomain.CurrentDomain.AssemblyResolve += SharedEntrypoint.RemoteResolve(ResolveDirectories); NetPreloaderRunner.OuterMain(args, filename); @@ -64,7 +64,7 @@ internal static class NetPreloaderRunner { Logger.Listeners.Add(new ConsoleLogListener()); - ConsoleManager.Initialize(true, false); + ConsoleManager.Initialize(true, true); try { diff --git a/BepInEx.Preloader.Unity/UnityPreloader.cs b/BepInEx.Preloader.Unity/UnityPreloader.cs index be7798f..0cca52e 100644 --- a/BepInEx.Preloader.Unity/UnityPreloader.cs +++ b/BepInEx.Preloader.Unity/UnityPreloader.cs @@ -43,7 +43,7 @@ internal static class UnityPreloader { HarmonyBackendFix.Initialize(); - ConsoleManager.Initialize(false, true); + ConsoleManager.Initialize(false, false); AllocateConsole(); Utility.TryDo(() =>