diff --git a/BepInEx/Console/ConsoleManager.cs b/BepInEx/Console/ConsoleManager.cs index 139763a..337d6d0 100644 --- a/BepInEx/Console/ConsoleManager.cs +++ b/BepInEx/Console/ConsoleManager.cs @@ -81,6 +81,9 @@ namespace BepInEx uint codepage = ConfigConsoleShiftJis.Value ? SHIFT_JIS_CP : (uint)Encoding.UTF8.CodePage; Driver.CreateConsole(codepage); + + if (ConfigPreventClose.Value) + Driver.PreventClose(); } public static void DetachConsole() @@ -113,6 +116,11 @@ namespace BepInEx "Logging.Console", "Enabled", false, "Enables showing a console for log output."); + + public static readonly ConfigEntry ConfigPreventClose = ConfigFile.CoreConfig.Bind( + "Logging.Console", "PreventClose", + false, + "If enabled, will prevent closing the console (either by deleting the close button or in other platform-specific way)."); public static readonly ConfigEntry ConfigConsoleShiftJis = ConfigFile.CoreConfig.Bind( "Logging.Console", "ShiftJisEncoding", diff --git a/BepInEx/Console/IConsoleDriver.cs b/BepInEx/Console/IConsoleDriver.cs index 59a6630..1d33e88 100644 --- a/BepInEx/Console/IConsoleDriver.cs +++ b/BepInEx/Console/IConsoleDriver.cs @@ -12,6 +12,8 @@ namespace BepInEx bool ConsoleActive { get; } bool ConsoleIsExternal { get; } + void PreventClose(); + void Initialize(bool alreadyActive); // Apparently Windows code-pages work in Mono. @@ -22,5 +24,7 @@ namespace BepInEx void SetConsoleColor(ConsoleColor color); void SetConsoleTitle(string title); + + } } \ No newline at end of file diff --git a/BepInEx/Console/Unix/LinuxConsoleDriver.cs b/BepInEx/Console/Unix/LinuxConsoleDriver.cs index e4e95f2..f4887b0 100644 --- a/BepInEx/Console/Unix/LinuxConsoleDriver.cs +++ b/BepInEx/Console/Unix/LinuxConsoleDriver.cs @@ -41,6 +41,11 @@ namespace BepInEx.Unix public TtyInfo TtyInfo { get; private set; } + public void PreventClose() + { + // Not supported by all distros + } + public void Initialize(bool alreadyActive) { // Console is always considered active on Unix diff --git a/BepInEx/Console/Windows/ConsoleWindow.cs b/BepInEx/Console/Windows/ConsoleWindow.cs index a069309..87f7b69 100644 --- a/BepInEx/Console/Windows/ConsoleWindow.cs +++ b/BepInEx/Console/Windows/ConsoleWindow.cs @@ -46,6 +46,17 @@ namespace UnityInjector.ConsoleUtil IsAttached = true; } + public static void PreventClose() + { + if (!IsAttached) + return; + + var hwnd = GetConsoleWindow(); + var hmenu = GetSystemMenu(hwnd, false); + if (hmenu != IntPtr.Zero) + DeleteMenu(hmenu, SC_CLOSE, MF_BYCOMMAND); + } + public static string Title { set @@ -126,5 +137,14 @@ namespace UnityInjector.ConsoleUtil [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Auto, SetLastError = true)] private static extern bool SetConsoleTitle(string title); + + [DllImport("user32.dll")] + private static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert); + + const UInt32 SC_CLOSE = 0xF060; + const UInt32 MF_BYCOMMAND = 0x00000000; + + [DllImport("user32.dll")] + static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags); } } \ No newline at end of file diff --git a/BepInEx/Console/Windows/WindowsConsoleDriver.cs b/BepInEx/Console/Windows/WindowsConsoleDriver.cs index 96f4422..1e95ce4 100644 --- a/BepInEx/Console/Windows/WindowsConsoleDriver.cs +++ b/BepInEx/Console/Windows/WindowsConsoleDriver.cs @@ -18,6 +18,11 @@ namespace BepInEx public bool ConsoleActive { get; private set; } public bool ConsoleIsExternal => true; + public void PreventClose() + { + ConsoleWindow.PreventClose(); + } + public void Initialize(bool alreadyActive) { ConsoleActive = alreadyActive;