From ffd50e318a6aaddf71a0ef85a3c8c07851ea4854 Mon Sep 17 00:00:00 2001 From: Rachel Powers <508861+Ryex@users.noreply.github.com> Date: Fri, 12 Dec 2025 20:11:04 -0700 Subject: [PATCH] refactor(console): attach console early also use RAII guard to free it instead of tracking it with a member variable Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com> --- launcher/Application.cpp | 23 ------------- launcher/Application.h | 5 --- launcher/console/WindowsConsole.cpp | 32 +++++++++++++++++++ launcher/console/WindowsConsole.h | 18 ++++++++++- launcher/filelink/FileLink.cpp | 22 ------------- launcher/filelink/FileLink.h | 4 --- launcher/filelink/filelink_main.cpp | 9 ++++++ launcher/main.cpp | 12 ++++++- .../updater/prismupdater/PrismUpdater.cpp | 24 -------------- .../updater/prismupdater/updater_main.cpp | 10 ++++++ 10 files changed, 79 insertions(+), 80 deletions(-) diff --git a/launcher/Application.cpp b/launcher/Application.cpp index 50fd32d5b..ac5cd8f4f 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -157,7 +157,6 @@ #endif #include #include -#include "console/WindowsConsole.h" #endif #include "console/Console.h" @@ -291,21 +290,9 @@ std::tuple read_lock_File(const Q Application::Application(int& argc, char** argv) : QApplication(argc, argv) { -#if defined Q_OS_WIN32 - // attach the parent console if stdout not already captured - if (AttachWindowsConsole()) { - consoleAttached = true; - if (auto err = EnableAnsiSupport(); !err) { - isANSIColorConsole = true; - } else { - std::cout << "Error setting up ansi console" << err.message() << std::endl; - } - } -#else if (console::isConsole()) { isANSIColorConsole = true; } -#endif setOrganizationName(BuildConfig.LAUNCHER_NAME); setOrganizationDomain(BuildConfig.LAUNCHER_DOMAIN); @@ -1415,16 +1402,6 @@ Application::~Application() { // Shut down logger by setting the logger function to nothing qInstallMessageHandler(nullptr); - -#if defined Q_OS_WIN32 - // Detach from Windows console - if (consoleAttached) { - fclose(stdout); - fclose(stdin); - fclose(stderr); - FreeConsole(); - } -#endif } void Application::messageReceived(const QByteArray& message) diff --git a/launcher/Application.h b/launcher/Application.h index 041e5b892..591a6d8ef 100644 --- a/launcher/Application.h +++ b/launcher/Application.h @@ -274,11 +274,6 @@ class Application : public QApplication { Qt::ApplicationState m_prevAppState = Qt::ApplicationInactive; #endif -#if defined Q_OS_WIN32 - // used on Windows to attach the standard IO streams - bool consoleAttached = false; -#endif - // FIXME: attach to instances instead. struct InstanceXtras { InstanceWindow* window = nullptr; diff --git a/launcher/console/WindowsConsole.cpp b/launcher/console/WindowsConsole.cpp index 4a0eb3d3d..e12183624 100644 --- a/launcher/console/WindowsConsole.cpp +++ b/launcher/console/WindowsConsole.cpp @@ -24,12 +24,16 @@ #endif #include +#include #include +#include #include #include #include #include +namespace console { + void RedirectHandle(DWORD handle, FILE* stream, const char* mode) { HANDLE stdHandle = GetStdHandle(handle); @@ -157,3 +161,31 @@ std::error_code EnableAnsiSupport() return {}; } + +void FreeWindowsConsole() +{ + fclose(stdout); + fclose(stdin); + fclose(stderr); + FreeConsole(); +} + +WindowsConsoleGuard::WindowsConsoleGuard() : m_consoleAttached(false) +{ + if (console::AttachWindowsConsole()) { + m_consoleAttached = true; + if (auto err = console::EnableAnsiSupport(); err) { + std::cout << "Error setting up ansi console" << err.message() << std::endl; + } + } +} + +WindowsConsoleGuard::~WindowsConsoleGuard() +{ + // Detach from Windows console + if (m_consoleAttached) { + console::FreeWindowsConsole(); + } +} + +} // namespace console diff --git a/launcher/console/WindowsConsole.h b/launcher/console/WindowsConsole.h index 4c1f3ee28..52102217d 100644 --- a/launcher/console/WindowsConsole.h +++ b/launcher/console/WindowsConsole.h @@ -21,8 +21,24 @@ #pragma once -#include +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +namespace console { void BindCrtHandlesToStdHandles(bool bindStdIn, bool bindStdOut, bool bindStdErr); bool AttachWindowsConsole(); std::error_code EnableAnsiSupport(); +void FreeWindowsConsole(); + +class WindowsConsoleGuard { + public: + WindowsConsoleGuard(); + ~WindowsConsoleGuard(); + + private: + bool m_consoleAttached; +}; + +} // namespace console diff --git a/launcher/filelink/FileLink.cpp b/launcher/filelink/FileLink.cpp index b2c558a9e..d87a1078b 100644 --- a/launcher/filelink/FileLink.cpp +++ b/launcher/filelink/FileLink.cpp @@ -34,24 +34,11 @@ #include -#if defined Q_OS_WIN32 -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include "console/WindowsConsole.h" -#endif - #include namespace fs = std::filesystem; FileLinkApp::FileLinkApp(int& argc, char** argv) : QCoreApplication(argc, argv), socket(new QLocalSocket(this)) { -#if defined Q_OS_WIN32 - // attach the parent console - if (AttachWindowsConsole()) { - consoleAttached = true; - } -#endif setOrganizationName(BuildConfig.LAUNCHER_NAME); setOrganizationDomain(BuildConfig.LAUNCHER_DOMAIN); setApplicationName(BuildConfig.LAUNCHER_NAME + "FileLink"); @@ -234,13 +221,4 @@ FileLinkApp::~FileLinkApp() qDebug() << "link program shutting down"; // Shut down logger by setting the logger function to nothing qInstallMessageHandler(nullptr); - -#if defined Q_OS_WIN32 - // Detach from Windows console - if (consoleAttached) { - fclose(stdout); - fclose(stdin); - fclose(stderr); - } -#endif } diff --git a/launcher/filelink/FileLink.h b/launcher/filelink/FileLink.h index 583d0d43a..b767f7f74 100644 --- a/launcher/filelink/FileLink.h +++ b/launcher/filelink/FileLink.h @@ -64,8 +64,4 @@ class FileLinkApp : public QCoreApplication { QList m_links_to_make; QList m_path_results; -#if defined Q_OS_WIN32 - // used on Windows to attach the standard IO streams - bool consoleAttached = false; -#endif }; diff --git a/launcher/filelink/filelink_main.cpp b/launcher/filelink/filelink_main.cpp index 2a8bcb703..d34844370 100644 --- a/launcher/filelink/filelink_main.cpp +++ b/launcher/filelink/filelink_main.cpp @@ -22,8 +22,17 @@ #include "FileLink.h" +#if defined Q_OS_WIN32 +#include "console/WindowsConsole.h" +#endif + int main(int argc, char* argv[]) { +#if defined Q_OS_WIN32 + // attach the parent console + console::WindowsConsoleGuard _consoleGuard; +#endif + FileLinkApp ldh(argc, argv); switch (ldh.status()) { diff --git a/launcher/main.cpp b/launcher/main.cpp index 2bce655d2..9378387bb 100644 --- a/launcher/main.cpp +++ b/launcher/main.cpp @@ -33,13 +33,23 @@ * limitations under the License. */ +#include + #include "Application.h" +#if defined Q_OS_WIN32 +#include "console/WindowsConsole.h" +#endif + int main(int argc, char* argv[]) { +#if defined Q_OS_WIN32 + // used on Windows to attach the standard IO streams + console::WindowsConsoleGuard _consoleGuard; +#endif + // initialize Qt Application app(argc, argv); - switch (app.status()) { case Application::StartingUp: case Application::Initialized: { diff --git a/launcher/updater/prismupdater/PrismUpdater.cpp b/launcher/updater/prismupdater/PrismUpdater.cpp index e6ec7e141..29e9225f2 100644 --- a/launcher/updater/prismupdater/PrismUpdater.cpp +++ b/launcher/updater/prismupdater/PrismUpdater.cpp @@ -40,14 +40,6 @@ #include #include -#if defined Q_OS_WIN32 -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include -#include "console/WindowsConsole.h" -#endif - #include namespace fs = std::filesystem; @@ -84,12 +76,6 @@ void appDebugOutput(QtMsgType type, const QMessageLogContext& context, const QSt PrismUpdaterApp::PrismUpdaterApp(int& argc, char** argv) : QApplication(argc, argv) { -#if defined Q_OS_WIN32 - // attach the parent console if stdout not already captured - if (AttachWindowsConsole()) { - consoleAttached = true; - } -#endif setOrganizationName(BuildConfig.LAUNCHER_NAME); setOrganizationDomain(BuildConfig.LAUNCHER_DOMAIN); setApplicationName(BuildConfig.LAUNCHER_NAME + "Updater"); @@ -380,16 +366,6 @@ PrismUpdaterApp::~PrismUpdaterApp() qDebug() << "updater shutting down"; // Shut down logger by setting the logger function to nothing qInstallMessageHandler(nullptr); - -#if defined Q_OS_WIN32 - // Detach from Windows console - if (consoleAttached) { - fclose(stdout); - fclose(stdin); - fclose(stderr); - FreeConsole(); - } -#endif } void PrismUpdaterApp::fail(const QString& reason) diff --git a/launcher/updater/prismupdater/updater_main.cpp b/launcher/updater/prismupdater/updater_main.cpp index 89c1d1198..ddc38d5cd 100644 --- a/launcher/updater/prismupdater/updater_main.cpp +++ b/launcher/updater/prismupdater/updater_main.cpp @@ -21,8 +21,18 @@ */ #include "PrismUpdater.h" + +#if defined Q_OS_WIN32 +#include "console/WindowsConsole.h" +#endif + int main(int argc, char* argv[]) { +#if defined Q_OS_WIN32 + // attach the parent console if stdout not already captured + console::WindowsConsoleGuard _consoleGuard; +#endif + PrismUpdaterApp wUpApp(argc, argv); switch (wUpApp.status()) {