From f0cc06105aad404736d256b4855180ac72cce4f9 Mon Sep 17 00:00:00 2001 From: z16 Date: Thu, 2 Nov 2017 18:51:22 +0100 Subject: [PATCH 1/4] Refactored console usage, minor formatting changes --- xiloader/console.cpp | 124 ++++++------------------------------ xiloader/console.h | 147 ++++++++++++++++++++++++------------------- xiloader/main.cpp | 16 +---- xiloader/network.cpp | 24 +++---- 4 files changed, 119 insertions(+), 192 deletions(-) diff --git a/xiloader/console.cpp b/xiloader/console.cpp index 4bcb29a..01d7a42 100644 --- a/xiloader/console.cpp +++ b/xiloader/console.cpp @@ -26,115 +26,31 @@ This file is part of DarkStar-server source code. #include /* Global Externs */ -extern CRITICAL_SECTION g_CriticalSection; extern bool g_Hide; namespace xiloader { /** - * @brief Operator overload to handle custom color tags in an ostream. - * - * @param s The incoming stream to append to. - * @param c The color tag to set the console foreground to. - * - * @return The incoming stream. + * @brief Prints a text fragment with the specified color to the console. + * + * @param c The color to print the fragment with. + * @param message The fragment to print. */ - inline std::ostream& operator << (std::ostream& s, const xiloader::color::colors& c) + void console::print(xiloader::color c, std::string const& message) { - EnterCriticalSection(&g_CriticalSection); - - /* Print timestamp if requested.. */ - if (c == xiloader::color::timestamp) + if (c == xiloader::color::none) { - __time32_t rawtime; - struct tm timeinfo; - - /* Get the current timestamp.. */ - _time32(&rawtime); - _localtime32_s(&timeinfo, &rawtime); - - /* Format the timestamp.. */ - char timestamp[256]; - strftime(timestamp, sizeof(timestamp), "[%m/%d/%y %H:%M:%S] ", &timeinfo); - - /* Output the timestamp.. */ - s << xiloader::color::lightyelllow << timestamp; - LeaveCriticalSection(&g_CriticalSection); - return s; + std::cout << message; + return; } - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)c); - LeaveCriticalSection(&g_CriticalSection); - return s; - } - - /** - * @brief Prints the given message to the console. - * - * @param format The format of the message to print. - * @param ... The arguments to fill the format. - */ - void console::output(const char* format, ...) - { - EnterCriticalSection(&g_CriticalSection); - - __time32_t rawtime; - struct tm timeinfo; - va_list args; - - /* Get the current timestamp.. */ - _time32(&rawtime); - _localtime32_s(&timeinfo, &rawtime); - - /* Parse the incoming message.. */ - char buffer[1024] = { 0 }; - va_start(args, format); - vsprintf_s(buffer, format, args); - va_end(args); - - /* Format the timestamp.. */ - char timestamp[256]; - strftime(timestamp, sizeof(timestamp), "[%m/%d/%y %H:%M:%S] ", &timeinfo); - - /* Output the timestamp and message.. */ - std::cout << std::endl << xiloader::color::lightyelllow << timestamp << xiloader::color::white << buffer; - - LeaveCriticalSection(&g_CriticalSection); - } - - /** - * @brief Prints the given message to the console with a specific color. - * - * @param c The color to print the message with. - * @param format The format of the message to print. - * @param ... The arguments to fill the format. - */ - void console::output(const xiloader::color::colors& c, const char* format, ...) - { - EnterCriticalSection(&g_CriticalSection); - - __time32_t rawtime; - struct tm timeinfo; - va_list args; - - /* Get the current timestamp.. */ - _time32(&rawtime); - _localtime32_s(&timeinfo, &rawtime); - - /* Parse the incoming message.. */ - char buffer[1024] = { 0 }; - va_start(args, format); - vsprintf_s(buffer, format, args); - va_end(args); - - /* Format the timestamp.. */ - char timestamp[256]; - strftime(timestamp, sizeof(timestamp), "[%m/%d/%y %H:%M:%S] ", &timeinfo); - - /* Output the timestamp and message.. */ - std::cout << std::endl << xiloader::color::lightyelllow << timestamp << c << buffer; - - LeaveCriticalSection(&g_CriticalSection); + auto stdout_handle = ::GetStdHandle(STD_OUTPUT_HANDLE); + ::CONSOLE_SCREEN_BUFFER_INFO info; + ::GetConsoleScreenBufferInfo(stdout_handle, &info); + auto attributes = info.wAttributes & 0xFFF0 | static_cast<::WORD>(c); + ::SetConsoleTextAttribute(stdout_handle, static_cast<::WORD>(attributes)); + std::cout << message; + ::SetConsoleTextAttribute(stdout_handle, info.wAttributes); } /** @@ -147,14 +63,14 @@ namespace xiloader if (!g_Hide) return; - HWND console = GetConsoleWindow(); + auto console = ::GetConsoleWindow(); // Adjust the task bar - ITaskbarList* taskbar = nullptr; - HRESULT hr = CoCreateInstance( + ::ITaskbarList* taskbar = nullptr; + auto hr = ::CoCreateInstance( CLSID_TaskbarList, nullptr, - CLSCTX_INPROC_SERVER, + ::CLSCTX_INPROC_SERVER, IID_ITaskbarList, reinterpret_cast(&taskbar)); if (SUCCEEDED(hr)) @@ -171,7 +87,7 @@ namespace xiloader } // Adjust the window's visibility - ShowWindow(console, visible ? SW_SHOW : SW_HIDE); + ::ShowWindow(console, visible ? SW_SHOW : SW_HIDE); } /** diff --git a/xiloader/console.h b/xiloader/console.h index adbea04..22a356a 100644 --- a/xiloader/console.h +++ b/xiloader/console.h @@ -31,70 +31,52 @@ This file is part of DarkStar-server source code. #include #include #include -#include +#include namespace xiloader { /** - * @brief Color namespace containing console color information. - */ - namespace color + * @brief Console color enumeration. + */ + enum class color { - /** - * @brief Console color enumeration. - */ - enum colors - { - /* Custom color code to print out the current timestamp. */ - timestamp = 0, - - /* Red color codes. */ - red = FOREGROUND_RED, - lightred = FOREGROUND_RED | FOREGROUND_INTENSITY, - - /* Green color codes. */ - green = FOREGROUND_GREEN, - lightgreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY, - - /* Blue color codes. */ - blue = FOREGROUND_BLUE, - lightblue = FOREGROUND_BLUE | FOREGROUND_INTENSITY, - - /* Cyan color codes. */ - cyan = FOREGROUND_BLUE | FOREGROUND_GREEN, - lightcyan = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY, - - /* Yellow color codes. */ - yellow = FOREGROUND_GREEN | FOREGROUND_RED, - lightyelllow = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY, - - /* Purple color codes. */ - purple = FOREGROUND_BLUE | FOREGROUND_RED, - lightpurple = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY, - - /* White color codes. */ - grey = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, - white = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, - - /* Common color codes. */ - debug = lightcyan, - error = lightred, - info = white, - success = lightgreen, - warning = lightyelllow - }; - - }; // namespace color - - /** - * @brief Operator overload to handle custom color tags in an ostream. - * - * @param s The incoming stream to append to. - * @param c The color tag to set the console foreground to. - * - * @return The incoming stream. - */ - inline std::ostream& operator << (std::ostream& s, const xiloader::color::colors& c); + none = 0, + + /* Red color codes. */ + red = FOREGROUND_RED, + lightred = FOREGROUND_RED | FOREGROUND_INTENSITY, + + /* Green color codes. */ + green = FOREGROUND_GREEN, + lightgreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY, + + /* Blue color codes. */ + blue = FOREGROUND_BLUE, + lightblue = FOREGROUND_BLUE | FOREGROUND_INTENSITY, + + /* Cyan color codes. */ + cyan = FOREGROUND_BLUE | FOREGROUND_GREEN, + lightcyan = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY, + + /* Yellow color codes. */ + yellow = FOREGROUND_GREEN | FOREGROUND_RED, + lightyelllow = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY, + + /* Purple color codes. */ + purple = FOREGROUND_BLUE | FOREGROUND_RED, + lightpurple = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY, + + /* White color codes. */ + grey = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, + white = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, + + /* Common color codes. */ + debug = lightcyan, + error = lightred, + info = white, + success = lightgreen, + warning = lightyelllow + }; /** * @brief Console class containing helper functions for console output. @@ -110,24 +92,61 @@ namespace xiloader */ static void visible(bool visible); + /** + * @brief Prints a text fragment with the specified color to the console. + * + * @param c The color to print the fragment with. + * @param message The fragment to print. + */ + static void print(xiloader::color c, std::string const& message); + public: /** * @brief Prints the given message to the console. * * @param format The format of the message to print. - * @param ... The arguments to fill the format. + * @param args The arguments to fill the format. */ - static void output(const char* format, ...); + template + static void output(char const* format, Args... args) + { + output(xiloader::color::none, format, args...); + } /** - * @brief Prints the given message to the console with a specific color. + * @brief Prints the given message to the console with the specific color. * * @param c The color to print the message with. * @param format The format of the message to print. - * @param ... The arguments to fill the format. + * @param args The arguments to fill the format. */ - static void output(const xiloader::color::colors& c, const char* format, ...); + template + static void output(xiloader::color c, char const* format, Args... args) + { + /* Get the current timestamp */ + ::__time32_t rawtime; + ::_time32(&rawtime); + + ::tm timeinfo; + ::_localtime32_s(&timeinfo, &rawtime); + + /* Parse the incoming message */ + char buffer[1024]; + ::snprintf(buffer, sizeof buffer, format, args...); + + /* Format the timestamp */ + char timestamp[256]; + ::strftime(timestamp, sizeof timestamp, "[%m/%d/%y %H:%M:%S] ", &timeinfo); + + /* Output the timestamp */ + print(xiloader::color::lightyelllow, timestamp); + + /* Output the message */ + print(c, buffer); + + std::cout << std::endl; + } /** * @brief Hides the console window. diff --git a/xiloader/main.cpp b/xiloader/main.cpp index 7deb2bd..91b6f37 100644 --- a/xiloader/main.cpp +++ b/xiloader/main.cpp @@ -36,7 +36,6 @@ std::string g_Password = ""; // The password being logged in with. char* g_CharacterList = NULL; // Pointer to the character list data being sent from the server. bool g_IsRunning = false; // Flag to determine if the network threads should hault. bool g_Hide = false; // Determines whether or not to hide the console window after FFXI starts. -CRITICAL_SECTION g_CriticalSection; // Critical section object to prevent console logging issues from threads. /* Hairpin Fix Variables */ DWORD g_NewServerAddress; // Hairpin server address to be overriden with. @@ -194,19 +193,12 @@ int __cdecl main(int argc, char* argv[]) { bool bUseHairpinFix = false; - /* Create critical section for console output.. */ - if (!InitializeCriticalSectionAndSpinCount(&g_CriticalSection, 0x00000400)) - { - xiloader::console::output(xiloader::color::error, "Failed to initialize critical section! Error code: %d", GetLastError()); - return 0; - } - /* Output the DarkStar banner.. */ - xiloader::console::output(xiloader::color::error, "=========================================================="); - xiloader::console::output(xiloader::color::success, "DarkStar Boot Loader (c) 2015 DarkStar Team"); + xiloader::console::output(xiloader::color::lightred, "=========================================================="); + xiloader::console::output(xiloader::color::lightgreen, "DarkStar Boot Loader (c) 2015 DarkStar Team"); xiloader::console::output(xiloader::color::lightpurple, "Bug Reports: https://github.com/DarkstarProject/darkstar/issues"); xiloader::console::output(xiloader::color::lightpurple, "Git Repo : https://github.com/DarkstarProject/darkstar"); - xiloader::console::output(xiloader::color::error, "=========================================================="); + xiloader::console::output(xiloader::color::lightred, "=========================================================="); /* Initialize Winsock */ WSADATA wsaData = { 0 }; @@ -418,7 +410,5 @@ int __cdecl main(int argc, char* argv[]) xiloader::console::output(xiloader::color::error, "Closing..."); Sleep(2000); - DeleteCriticalSection(&g_CriticalSection); - return ERROR_SUCCESS; } diff --git a/xiloader/network.cpp b/xiloader/network.cpp index 0f3f1fe..02241f4 100644 --- a/xiloader/network.cpp +++ b/xiloader/network.cpp @@ -217,7 +217,7 @@ namespace xiloader } /* Determine if we should auto-login.. */ - bool bUseAutoLogin = (g_Username.size() > 0 && g_Password.size() > 0) && bFirstLogin; + bool bUseAutoLogin = !g_Username.empty() && !g_Password.empty() && bFirstLogin; if (bUseAutoLogin) xiloader::console::output(xiloader::color::lightgreen, "Autologin activated!"); @@ -228,15 +228,16 @@ namespace xiloader xiloader::console::output(" 1.) Login"); xiloader::console::output(" 2.) Create New Account"); xiloader::console::output("=========================================================="); - printf("\n\nEnter a selection: "); + printf("\nEnter a selection: "); std::string input; std::cin >> input; + std::cout << std::endl; /* User wants to log into an existing account.. */ - if (input.c_str()[0] == '1') + if (input == "1") { - xiloader::console::output("Please enter your login information.."); + xiloader::console::output("Please enter your login information."); std::cout << "\nUsername: "; std::cin >> g_Username; std::cout << "Password: "; @@ -264,12 +265,11 @@ namespace xiloader sendBuffer[0x20] = 0x10; } - /* User wants to create a new account.. */ - if (input.c_str()[0] == '2') + else if (input == "2") { create_account: - xiloader::console::output("Please enter your desired login information.."); + xiloader::console::output("Please enter your desired login information."); std::cout << "\nUsername (3-15 characters): "; std::cin >> g_Username; std::cout << "Password (6-15 characters): "; @@ -286,6 +286,8 @@ namespace xiloader sendBuffer[0x20] = 0x20; } + + std::cout << std::endl; } else { @@ -306,26 +308,26 @@ namespace xiloader switch (recvBuffer[0]) { case 0x0001: // Success (Login) - xiloader::console::output("Successfully logged in as %s!", g_Username.c_str()); + xiloader::console::output(xiloader::color::success, "Successfully logged in as %s!", g_Username.c_str()); sock->AccountId = *(UINT32*)(recvBuffer + 0x01); closesocket(sock->s); sock->s = INVALID_SOCKET; return true; case 0x0002: // Error (Login) - xiloader::console::output("Failed to login. Invalid username / password."); + xiloader::console::output(xiloader::color::error, "Failed to login. Invalid username or password."); closesocket(sock->s); sock->s = INVALID_SOCKET; return false; case 0x0003: // Success (Create Account) - xiloader::console::output("Account successfully created!"); + xiloader::console::output(xiloader::color::success, "Account successfully created!"); closesocket(sock->s); sock->s = INVALID_SOCKET; return false; case 0x0004: // Error (Create Account) - xiloader::console::output("Failed to create the new account.\nUsername already taken!"); + xiloader::console::output(xiloader::color::error, "Failed to create the new account. Username already taken."); closesocket(sock->s); sock->s = INVALID_SOCKET; return false; From f3b8986c99477b379f5d0fe23674613331aa0f3e Mon Sep 17 00:00:00 2001 From: z16 Date: Thu, 2 Nov 2017 19:05:55 +0100 Subject: [PATCH 2/4] Added white as default color --- xiloader/console.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xiloader/console.h b/xiloader/console.h index 22a356a..257ee21 100644 --- a/xiloader/console.h +++ b/xiloader/console.h @@ -111,7 +111,7 @@ namespace xiloader template static void output(char const* format, Args... args) { - output(xiloader::color::none, format, args...); + output(xiloader::color::white, format, args...); } /** @@ -131,10 +131,6 @@ namespace xiloader ::tm timeinfo; ::_localtime32_s(&timeinfo, &rawtime); - /* Parse the incoming message */ - char buffer[1024]; - ::snprintf(buffer, sizeof buffer, format, args...); - /* Format the timestamp */ char timestamp[256]; ::strftime(timestamp, sizeof timestamp, "[%m/%d/%y %H:%M:%S] ", &timeinfo); @@ -142,7 +138,11 @@ namespace xiloader /* Output the timestamp */ print(xiloader::color::lightyelllow, timestamp); + /* Parse the incoming message */ + char buffer[1024]; + ::snprintf(buffer, sizeof buffer, format, args...); /* Output the message */ + print(c, buffer); std::cout << std::endl; From 11996d652e4598bec390f895dd667b05e9a76104 Mon Sep 17 00:00:00 2001 From: z16 Date: Thu, 2 Nov 2017 19:18:13 +0100 Subject: [PATCH 3/4] Normalized line endings --- xiloader/network.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xiloader/network.cpp b/xiloader/network.cpp index 02241f4..4ad6f93 100644 --- a/xiloader/network.cpp +++ b/xiloader/network.cpp @@ -249,10 +249,10 @@ namespace xiloader { if (ch == '\b') { - if (g_Password.size()) - { - g_Password.pop_back(); - std::cout << "\b \b"; + if (g_Password.size()) + { + g_Password.pop_back(); + std::cout << "\b \b"; } } else From 84c7d3c53a5d1d2000bce43d95086372fca401a5 Mon Sep 17 00:00:00 2001 From: z16 Date: Thu, 2 Nov 2017 19:20:36 +0100 Subject: [PATCH 4/4] Removed unused formatting specifier --- xiloader/console.cpp | 6 ------ xiloader/console.h | 2 -- 2 files changed, 8 deletions(-) diff --git a/xiloader/console.cpp b/xiloader/console.cpp index 01d7a42..165a3a7 100644 --- a/xiloader/console.cpp +++ b/xiloader/console.cpp @@ -38,12 +38,6 @@ namespace xiloader */ void console::print(xiloader::color c, std::string const& message) { - if (c == xiloader::color::none) - { - std::cout << message; - return; - } - auto stdout_handle = ::GetStdHandle(STD_OUTPUT_HANDLE); ::CONSOLE_SCREEN_BUFFER_INFO info; ::GetConsoleScreenBufferInfo(stdout_handle, &info); diff --git a/xiloader/console.h b/xiloader/console.h index 257ee21..f559b9a 100644 --- a/xiloader/console.h +++ b/xiloader/console.h @@ -40,8 +40,6 @@ namespace xiloader */ enum class color { - none = 0, - /* Red color codes. */ red = FOREGROUND_RED, lightred = FOREGROUND_RED | FOREGROUND_INTENSITY,