From dc8a22c05e92f5f93926cc849170eaa9709e06af Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Wed, 17 Apr 2024 20:44:54 +0200 Subject: [PATCH] Windows: move console color init code to separate function Reserve Console_Init() for actual console init, if code only needs colors (e.g. a console application that already has a console), then they should call Console_ColorInit(). --- r5dev/revpk/revpk.cpp | 11 +++++--- r5dev/windows/console.cpp | 54 +++++++++++++++++++++++++++------------ r5dev/windows/console.h | 3 ++- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/r5dev/revpk/revpk.cpp b/r5dev/revpk/revpk.cpp index 240d0aa9..b6fc6842 100644 --- a/r5dev/revpk/revpk.cpp +++ b/r5dev/revpk/revpk.cpp @@ -25,7 +25,7 @@ #define FRONTEND_ENABLE_FILE "enable.txt" static CKeyValuesSystem s_KeyValuesSystem; -static CFileSystem_Stdio g_FullFileSystem; +static CFileSystem_Stdio s_FullFileSystem; static bool s_bUseAnsiColors = true; //----------------------------------------------------------------------------- @@ -41,7 +41,7 @@ IKeyValuesSystem* KeyValuesSystem() //----------------------------------------------------------------------------- CFileSystem_Stdio* FileSystem() { - return &g_FullFileSystem; + return &s_FullFileSystem; } //----------------------------------------------------------------------------- @@ -51,10 +51,15 @@ static void ReVPK_Init() { CheckSystemCPUForSSE2(); + // Init time. + Plat_FloatTime(); + g_CoreMsgVCallback = EngineLoggerSink; lzham_enable_fail_exceptions(true); - Console_Init(s_bUseAnsiColors); + if (s_bUseAnsiColors) + Console_ColorInit(); + SpdLog_Init(s_bUseAnsiColors); } diff --git a/r5dev/windows/console.cpp b/r5dev/windows/console.cpp index 3324fd0c..4752ae99 100644 --- a/r5dev/windows/console.cpp +++ b/r5dev/windows/console.cpp @@ -81,20 +81,23 @@ void FlashConsoleBackground(int nFlashCount, int nFlashInterval, COLORREF color) //----------------------------------------------------------------------------- bool Console_Init(const bool bAnsiColor) { -#ifndef _TOOLS + char msgBuf[2048]; + /////////////////////////////////////////////////////////////////////////// // Create the console window if (AllocConsole() == FALSE) { - char szBuf[2048]; - snprintf(szBuf, sizeof(szBuf), "Failed to create console window! [%s]\n", std::system_category().message(static_cast(::GetLastError())).c_str()); + snprintf(msgBuf, sizeof(msgBuf), "Failed to create console window! [%s]\n", + std::system_category().message(static_cast(::GetLastError())).c_str()); - OutputDebugStringA(szBuf); + OutputDebugStringA(msgBuf); return false; } +#ifndef _TOOLS //-- Set the window title SetConsoleTitleA("R5"); +#endif // !_TOOLS //-- Open input/output streams FILE* fDummy; @@ -102,6 +105,7 @@ bool Console_Init(const bool bAnsiColor) freopen_s(&fDummy, "CONOUT$", "w", stdout); freopen_s(&fDummy, "CONOUT$", "w", stderr); +#ifndef _TOOLS //-- Create a worker thread to process console commands DWORD dwThreadId = NULL; DWORD __stdcall ProcessConsoleWorker(LPVOID); @@ -113,22 +117,16 @@ bool Console_Init(const bool bAnsiColor) } #endif // !_TOOLS - HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); - DWORD dwMode = NULL; - if (bAnsiColor) { - GetConsoleMode(hOutput, &dwMode); - dwMode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING; - - if (!SetConsoleMode(hOutput, dwMode)) // Some editions of Windows have 'VirtualTerminalLevel' disabled by default. + if (!Console_ColorInit()) { - // Warn the user if 'VirtualTerminalLevel' could not be set on users environment. - MessageBoxA(NULL, "Failed to set console mode 'VirtualTerminalLevel'; please disable ansi-colors and restart the program if output logging appears distorted.", "SDK Warning", MB_ICONEXCLAMATION | MB_OK); - } + Assert(0); + //snprintf(msgBuf, sizeof(msgBuf), "Failed to set color console mode! [%s]\n", + // std::system_category().message(static_cast(::GetLastError())).c_str()); - SetConsoleBackgroundColor(0x00000000); - AnsiColors_Init(); + //MessageBoxA(NULL, msgBuf, "SDK Warning", MB_ICONEXCLAMATION | MB_OK); + } } #ifndef _TOOLS @@ -138,6 +136,27 @@ bool Console_Init(const bool bAnsiColor) return true; } +//----------------------------------------------------------------------------- +// Purpose: terminal color setup +// Output : true on success, false otherwise +//----------------------------------------------------------------------------- +bool Console_ColorInit() +{ + HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD dwMode = NULL; + + GetConsoleMode(hOutput, &dwMode); // Some editions of Windows have 'VirtualTerminalLevel' disabled by default. + dwMode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING; + + if (!SetConsoleMode(hOutput, dwMode)) + return false; // Failure. + + SetConsoleBackgroundColor(0x00000000); + AnsiColors_Init(); + + return true; +} + //----------------------------------------------------------------------------- // Purpose: terminal window shutdown // Output : true on success, false otherwise @@ -149,7 +168,8 @@ bool Console_Shutdown() if (FreeConsole() == FALSE) { char szBuf[2048]; - snprintf(szBuf, sizeof(szBuf), "Failed to destroy console window! [%s]\n", std::system_category().message(static_cast(::GetLastError())).c_str()); + snprintf(szBuf, sizeof(szBuf), "Failed to destroy console window! [%s]\n", + std::system_category().message(static_cast(::GetLastError())).c_str()); OutputDebugStringA(szBuf); return false; diff --git a/r5dev/windows/console.h b/r5dev/windows/console.h index bd7c6559..32e89915 100644 --- a/r5dev/windows/console.h +++ b/r5dev/windows/console.h @@ -3,5 +3,6 @@ void SetConsoleBackgroundColor(COLORREF color); void FlashConsoleBackground(int nFlashCount, int nFlashInterval, COLORREF color); -bool Console_Init(const bool bAnsiColor); +bool Console_Init(const bool bAnsiColor, const bool bWarn = true); +bool Console_ColorInit(); bool Console_Shutdown();