diff --git a/src/Ryujinx.Common/Logging/Logger.cs b/src/Ryujinx.Common/Logging/Logger.cs index f66e5574a..b3ce83bf8 100644 --- a/src/Ryujinx.Common/Logging/Logger.cs +++ b/src/Ryujinx.Common/Logging/Logger.cs @@ -136,11 +136,7 @@ namespace Ryujinx.Common.Logging _time = Stopwatch.StartNew(); - // Logger should log to console by default - AddTarget(new AsyncLogTargetWrapper( - new ConsoleLogTarget("console"), - 1000, - AsyncLogTargetOverflowAction.Discard)); + SetConsoleTargetEnabled(true); Notice = new Log(LogLevel.Notice); @@ -173,6 +169,21 @@ namespace Ryujinx.Common.Logging Updated += target.Log; } + public static void SetConsoleTargetEnabled(bool enabled) + { + if (enabled) + { + AddTarget(new AsyncLogTargetWrapper( + new ConsoleLogTarget("console"), + 1000, + AsyncLogTargetOverflowAction.Discard)); + } + else + { + RemoveTarget("console"); + } + } + public static void RemoveTarget(string target) { ILogTarget logTarget = GetTarget(target); diff --git a/src/Ryujinx.UI.Common/Helper/ConsoleHelper.cs b/src/Ryujinx.UI.Common/Helper/ConsoleHelper.cs index 99b209c6e..d183ba504 100644 --- a/src/Ryujinx.UI.Common/Helper/ConsoleHelper.cs +++ b/src/Ryujinx.UI.Common/Helper/ConsoleHelper.cs @@ -12,20 +12,12 @@ namespace Ryujinx.UI.Common.Helper private static partial nint GetConsoleWindow(); [SupportedOSPlatform("windows")] - [LibraryImport("user32")] + [LibraryImport("kernel32", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool ShowWindow(nint hWnd, int nCmdShow); - - [SupportedOSPlatform("windows")] - [LibraryImport("user32")] - private static partial nint GetForegroundWindow(); - - [SupportedOSPlatform("windows")] - [LibraryImport("user32")] - [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool SetForegroundWindow(nint hWnd); + private static partial bool FreeConsole(); public static bool SetConsoleWindowStateSupported => OperatingSystem.IsWindows(); + public static bool HasConsoleWindow => OperatingSystem.IsWindows() && GetConsoleWindow() != nint.Zero; public static void SetConsoleWindowState(bool show) { @@ -42,22 +34,31 @@ namespace Ryujinx.UI.Common.Helper [SupportedOSPlatform("windows")] private static void SetConsoleWindowStateWindows(bool show) { - const int SW_HIDE = 0; - const int SW_SHOW = 5; - - nint hWnd = GetConsoleWindow(); - - if (hWnd == nint.Zero) + if (show) { - Logger.Warning?.Print(LogClass.Application, "Attempted to show/hide console window but console window does not exist"); + if (GetConsoleWindow() != nint.Zero) + { + Logger.SetConsoleTargetEnabled(true); + } return; } - SetForegroundWindow(hWnd); + Logger.SetConsoleTargetEnabled(false); + DetachConsole(); + } - hWnd = GetForegroundWindow(); + [SupportedOSPlatform("windows")] + private static void DetachConsole() + { + if (GetConsoleWindow() == nint.Zero) + { + return; + } - ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE); + if (!FreeConsole()) + { + Logger.Warning?.Print(LogClass.Application, "Attempted to detach console window but the operation failed"); + } } } } diff --git a/src/Ryujinx/Assets/Locales/en_US.json b/src/Ryujinx/Assets/Locales/en_US.json index 04b5fcd51..6dda3382c 100644 --- a/src/Ryujinx/Assets/Locales/en_US.json +++ b/src/Ryujinx/Assets/Locales/en_US.json @@ -850,6 +850,7 @@ "SettingsTabGraphicsPreferredGpu": "Preferred GPU", "SettingsTabGraphicsPreferredGpuTooltip": "Select the graphics card that will be used with the Vulkan graphics backend.\n\nDoes not affect the GPU that OpenGL will use.\n\nSet to the GPU flagged as \"dGPU\" if unsure. If there isn't one, leave untouched.", "SettingsAppRequiredRestartMessage": "Ryujinx Restart Required", + "SettingsShowConsoleRestartMessage": "The console will be available the next time Ryujinx starts.", "SettingsGpuBackendRestartMessage": "Graphics Backend or GPU settings have been modified. This will require a restart to be applied", "SettingsGpuBackendRestartSubMessage": "Do you want to restart now?", "RyujinxUpdaterMessage": "Do you want to update Ryujinx to the latest version?", diff --git a/src/Ryujinx/Assets/Locales/es_ES.json b/src/Ryujinx/Assets/Locales/es_ES.json index 88f04109d..a79d9cf70 100644 --- a/src/Ryujinx/Assets/Locales/es_ES.json +++ b/src/Ryujinx/Assets/Locales/es_ES.json @@ -757,6 +757,7 @@ "SettingsTabGraphicsPreferredGpu": "GPU preferida", "SettingsTabGraphicsPreferredGpuTooltip": "Selecciona la tarjeta gráfica que se utilizará con los back-end de gráficos Vulkan.\n\nNo afecta la GPU que utilizará OpenGL.\n\nFije a la GPU marcada como \"dGUP\" ante dudas. Si no hay una, no haga modificaciones.", "SettingsAppRequiredRestartMessage": "Reinicio de Ryujinx requerido.", + "SettingsShowConsoleRestartMessage": "La consola estará disponible la próxima vez que se inicie Ryujinx.", "SettingsGpuBackendRestartMessage": "La configuración de la GPU o del back-end de los gráficos fue modificada. Es necesario reiniciar para que se aplique.", "SettingsGpuBackendRestartSubMessage": "¿Quieres reiniciar ahora?", "RyujinxUpdaterMessage": "¿Quieres actualizar Ryujinx a la última versión?", diff --git a/src/Ryujinx/Assets/Locales/fr_FR.json b/src/Ryujinx/Assets/Locales/fr_FR.json index 535f8d4ea..cc3b3469b 100644 --- a/src/Ryujinx/Assets/Locales/fr_FR.json +++ b/src/Ryujinx/Assets/Locales/fr_FR.json @@ -768,6 +768,7 @@ "SettingsTabGraphicsPreferredGpu": "GPU préféré", "SettingsTabGraphicsPreferredGpuTooltip": "Sélectionnez la carte graphique qui sera utilisée avec l'interface graphique Vulkan.\n\nCela ne change pas le GPU qu'OpenGL utilisera.\n\nChoisissez le GPU noté \"dGPU\" si vous n'êtes pas sûr. S'il n'y en a pas, ne pas modifier.", "SettingsAppRequiredRestartMessage": "Redémarrage de Ryujinx requis", + "SettingsShowConsoleRestartMessage": "La console sera disponible au prochain démarrage de Ryujinx.", "SettingsGpuBackendRestartMessage": "Les paramètres de l'interface graphique ou du GPU ont été modifiés. Cela nécessitera un redémarrage pour être appliqué", "SettingsGpuBackendRestartSubMessage": "\n\nVoulez-vous redémarrer maintenant ?", "RyujinxUpdaterMessage": "Voulez-vous mettre à jour Ryujinx vers la dernière version ?", diff --git a/src/Ryujinx/Assets/Locales/zh_CN.json b/src/Ryujinx/Assets/Locales/zh_CN.json index 0940ea32c..f2737e77d 100644 --- a/src/Ryujinx/Assets/Locales/zh_CN.json +++ b/src/Ryujinx/Assets/Locales/zh_CN.json @@ -762,6 +762,7 @@ "SettingsTabGraphicsPreferredGpu": "首选 GPU:", "SettingsTabGraphicsPreferredGpuTooltip": "选择 Vulkan 图形引擎使用的 GPU。\n\n此选项不会影响 OpenGL 使用的 GPU。\n\n如果不确定,建议选择\"独立显卡(dGPU)\"。如果没有独立显卡,则无需改动此选项。", "SettingsAppRequiredRestartMessage": "Ryujinx 模拟器需要重启", + "SettingsShowConsoleRestartMessage": "控制台将会在下次启动 Ryujinx 时可用。", "SettingsGpuBackendRestartMessage": "您修改了图形引擎或 GPU 设置,需要重启模拟器才能生效", "SettingsGpuBackendRestartSubMessage": "是否要立即重启模拟器?", "RyujinxUpdaterMessage": "是否更新 Ryujinx 到最新的版本?", diff --git a/src/Ryujinx/UI/ViewModels/MainWindowViewModel.cs b/src/Ryujinx/UI/ViewModels/MainWindowViewModel.cs index b28b24076..711d4dbf0 100644 --- a/src/Ryujinx/UI/ViewModels/MainWindowViewModel.cs +++ b/src/Ryujinx/UI/ViewModels/MainWindowViewModel.cs @@ -521,10 +521,19 @@ namespace Ryujinx.Ava.UI.ViewModels get => ConfigurationState.Instance.UI.ShowConsole; set { + bool restartRequired = value && !ConsoleHelper.HasConsoleWindow; + ConfigurationState.Instance.UI.ShowConsole.Value = value; ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath); + if (restartRequired) + { + NotificationHelper.ShowInformation( + LocaleManager.Instance[LocaleKeys.SettingsAppRequiredRestartMessage], + LocaleManager.Instance[LocaleKeys.SettingsShowConsoleRestartMessage]); + } + OnPropertyChanged(); } }