Skip to content

Commit

Permalink
Merge pull request #14 from z16/feature/console-refactoring
Browse files Browse the repository at this point in the history
Refactored console behavior
  • Loading branch information
teschnei authored Nov 3, 2017
2 parents 46f5dda + 84c7d3c commit 7bbbdf9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 199 deletions.
124 changes: 17 additions & 107 deletions xiloader/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,115 +26,25 @@ This file is part of DarkStar-server source code.
#include <ShObjIdl.h>

/* 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)
{
__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;
}

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);
}

/**
Expand All @@ -147,14 +57,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<void**>(&taskbar));
if (SUCCEEDED(hr))
Expand All @@ -171,7 +81,7 @@ namespace xiloader
}

// Adjust the window's visibility
ShowWindow(console, visible ? SW_SHOW : SW_HIDE);
::ShowWindow(console, visible ? SW_SHOW : SW_HIDE);
}

/**
Expand Down
145 changes: 81 additions & 64 deletions xiloader/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,70 +31,50 @@ This file is part of DarkStar-server source code.
#include <Windows.h>
#include <iostream>
#include <string>
#include <time.h>
#include <ctime>

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);
/* 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.
Expand All @@ -110,24 +90,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<typename... Args>
static void output(char const* format, Args... args)
{
output(xiloader::color::white, 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<typename... Args>
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);

/* 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);

/* Parse the incoming message */
char buffer[1024];
::snprintf(buffer, sizeof buffer, format, args...);
/* Output the message */

print(c, buffer);

std::cout << std::endl;
}

/**
* @brief Hides the console window.
Expand Down
16 changes: 3 additions & 13 deletions xiloader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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;
}
Loading

0 comments on commit 7bbbdf9

Please sign in to comment.