using System; using System.IO; using System.Text; using BepInEx.Configuration; using BepInEx.Unix; using MonoMod.Utils; namespace BepInEx { internal static class ConsoleManager { private const uint SHIFT_JIS_CP = 932; 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) { switch (Utility.CurrentOs) { case Platform.MacOS: case Platform.Linux: { Driver = new LinuxConsoleDriver(); break; } case Platform.Windows: { Driver = new WindowsConsoleDriver(); break; } } Driver.Initialize(alreadyActive); } private static void DriverCheck() { if (Driver == null) throw new InvalidOperationException("Driver has not been initialized"); } public static void CreateConsole() { if (ConsoleActive) return; DriverCheck(); // 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; Driver.CreateConsole(codepage); } public static void DetachConsole() { if (!ConsoleActive) return; DriverCheck(); Driver.DetachConsole(); } public static void SetConsoleTitle(string title) { DriverCheck(); Driver.SetConsoleTitle(title); } public static void SetConsoleColor(ConsoleColor color) { DriverCheck(); Driver.SetConsoleColor(color); } public static readonly ConfigEntry ConfigConsoleEnabled = ConfigFile.CoreConfig.Bind( "Logging.Console", "Enabled", false, "Enables showing a console for log output."); public static readonly ConfigEntry ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind( "Logging.Console", "ShiftJisEncoding", false, "If true, console is set to the Shift-JIS encoding, otherwise UTF-8 encoding."); } }