From 8ff2c1088b95c95b12d78cf3f66fb0099639a9e2 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Tue, 2 Nov 2021 20:16:50 +0200 Subject: [PATCH] Clean up console listener logic --- BepInEx.Core/Bootstrap/BaseChainloader.cs | 2 +- BepInEx.Core/Console/ConsoleManager.cs | 43 +++++++++++------------ BepInEx.IL2CPP/Preloader.cs | 22 ++++++------ BepInEx.Preloader.Unity/UnityPreloader.cs | 2 +- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/BepInEx.Core/Bootstrap/BaseChainloader.cs b/BepInEx.Core/Bootstrap/BaseChainloader.cs index 00a786f..459ed07 100644 --- a/BepInEx.Core/Bootstrap/BaseChainloader.cs +++ b/BepInEx.Core/Bootstrap/BaseChainloader.cs @@ -151,7 +151,7 @@ namespace BepInEx.Bootstrap protected virtual void InitializeLoggers() { - if (ConsoleManager.ConfigConsoleEnabled.Value && !ConsoleManager.ConsoleActive) + if (ConsoleManager.ConsoleEnabled && !ConsoleManager.ConsoleActive) ConsoleManager.CreateConsole(); if (ConsoleManager.ConsoleActive) diff --git a/BepInEx.Core/Console/ConsoleManager.cs b/BepInEx.Core/Console/ConsoleManager.cs index 60dce5a..c054448 100644 --- a/BepInEx.Core/Console/ConsoleManager.cs +++ b/BepInEx.Core/Console/ConsoleManager.cs @@ -24,7 +24,7 @@ namespace BepInEx private const uint SHIFT_JIS_CP = 932; - public static bool ConsoleEnabled => EnableConsoleArgOverride ?? ConfigConsoleEnabled.Value; + private const string ENABLE_CONSOLE_ARG = "--enable-console"; public static readonly ConfigEntry ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind( "Logging.Console", "Enabled", @@ -53,27 +53,8 @@ namespace BepInEx .ToString() ); - internal static IConsoleDriver Driver { get; set; } - - /// - /// True if an external console has been started, false otherwise. - /// - public static bool ConsoleActive => Driver?.ConsoleActive ?? false; - - /// - /// The stream that writes to the standard out stream of the process. Should never be null. - /// - public static TextWriter StandardOutStream => Driver?.StandardOut; - - /// - /// The stream that writes to an external console. Null if no such console exists - /// - public static TextWriter ConsoleStream => Driver?.ConsoleOut; - private static readonly bool? EnableConsoleArgOverride; - private const string ENABLE_CONSOLE_ARG = "--enable-console"; - static ConsoleManager() { // Ensure GetCommandLineArgs failing (e.g. on unix) does not kill bepin @@ -93,6 +74,25 @@ namespace BepInEx } } + public static bool ConsoleEnabled => EnableConsoleArgOverride ?? ConfigConsoleEnabled.Value; + + internal static IConsoleDriver Driver { get; set; } + + /// + /// True if an external console has been started, false otherwise. + /// + public static bool ConsoleActive => Driver?.ConsoleActive ?? false; + + /// + /// The stream that writes to the standard out stream of the process. Should never be null. + /// + public static TextWriter StandardOutStream => Driver?.StandardOut; + + /// + /// The stream that writes to an external console. Null if no such console exists + /// + public static TextWriter ConsoleStream => Driver?.ConsoleOut; + public static void Initialize(bool alreadyActive) { @@ -123,10 +123,9 @@ namespace BepInEx // Apparently some versions of Mono throw a "Encoding name 'xxx' not supported" // if you use Encoding.GetEncoding // That's why we use of codepages directly and handle then in console drivers separately - var codepage = ConfigConsoleShiftJis.Value ? SHIFT_JIS_CP : (uint) Encoding.UTF8.CodePage; + var codepage = ConfigConsoleShiftJis.Value ? SHIFT_JIS_CP : (uint)Encoding.UTF8.CodePage; Driver.CreateConsole(codepage); - // Console.SetOut(ConsoleStream); if (ConfigPreventClose.Value) Driver.PreventClose(); diff --git a/BepInEx.IL2CPP/Preloader.cs b/BepInEx.IL2CPP/Preloader.cs index ddca8d1..279050b 100644 --- a/BepInEx.IL2CPP/Preloader.cs +++ b/BepInEx.IL2CPP/Preloader.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics; using System.Globalization; -using System.IO; using BepInEx.Configuration; using BepInEx.Logging; using BepInEx.Preloader.Core; @@ -14,6 +13,15 @@ namespace BepInEx.IL2CPP { public static class Preloader { + #region Config + + private static readonly ConfigEntry ConfigUnityVersion = ConfigFile.CoreConfig.Bind( + "IL2CPP", "UnityVersion", + string.Empty, + "Unity version to report to Il2CppUnhollower. If empty, version is automatically determined from the game process."); + + #endregion + public static string IL2CPPUnhollowedPath { get; internal set; } private static PreloaderConsoleListener PreloaderLog { get; set; } @@ -35,8 +43,7 @@ namespace BepInEx.IL2CPP PreloaderLog = new PreloaderConsoleListener(); Logger.Listeners.Add(PreloaderLog); - - if (ConsoleManager.ConfigConsoleEnabled.Value) + if (ConsoleManager.ConsoleEnabled) { ConsoleManager.CreateConsole(); Logger.Listeners.Add(new ConsoleLogListener()); @@ -135,14 +142,5 @@ namespace BepInEx.IL2CPP return false; } } - - #region Config - - private static readonly ConfigEntry ConfigUnityVersion = ConfigFile.CoreConfig.Bind( - "IL2CPP", "UnityVersion", - string.Empty, - "Unity version to report to Il2CppUnhollower. If empty, version is automatically determined from the game process."); - - #endregion } } diff --git a/BepInEx.Preloader.Unity/UnityPreloader.cs b/BepInEx.Preloader.Unity/UnityPreloader.cs index 078028f..1cdd880 100644 --- a/BepInEx.Preloader.Unity/UnityPreloader.cs +++ b/BepInEx.Preloader.Unity/UnityPreloader.cs @@ -47,7 +47,6 @@ namespace BepInEx.Preloader.Unity Logger.Sources.Add(new HarmonyLogSource()); Logger.Sources.Add(TraceLogSource.CreateSource()); - Logger.Listeners.Add(new ConsoleLogListener()); PreloaderLog = new PreloaderConsoleListener(); Logger.Listeners.Add(PreloaderLog); @@ -136,6 +135,7 @@ namespace BepInEx.Preloader.Unity try { ConsoleManager.CreateConsole(); + Logger.Listeners.Add(new ConsoleLogListener()); } catch (Exception ex) {