6#define WIN32_LEAN_AND_MEAN
22#include <ShlObj_core.h>
24#include "spdlog/spdlog.h"
38static BOOL __stdcall ProcessConsoleShutdown(DWORD p_fdwCtrlType)
40 switch (p_fdwCtrlType)
42 case CTRL_CLOSE_EVENT:
43 case CTRL_SHUTDOWN_EVENT:
53static std::string GetStringForError(
int p_ErrorNumber)
57 LPSTR t_ErrorMessage = NULL;
59 DWORD t_NumChars = FormatMessage(
60 FORMAT_MESSAGE_FROM_SYSTEM
61 | FORMAT_MESSAGE_ALLOCATE_BUFFER
62 | FORMAT_MESSAGE_IGNORE_INSERTS,
65 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
72 reinterpret_cast<LPTSTR
>(&t_ErrorMessage),
78 DWORD t_FormatError = GetLastError();
81 std::string(
"(could not get an error string for this error number). ") +
82 std::string(
"FormatMessage failed with error ") +
83 std::to_string(t_FormatError) +
88 t_Result = std::string(
static_cast<const char*
>(t_ErrorMessage));
91 LocalFree(t_ErrorMessage);
96static bool DoesWindowHaveFocus(
void)
98 HWND t_ConsoleWindow = GetConsoleWindow();
99 HWND t_HCurWnd = GetForegroundWindow();
101 return t_ConsoleWindow == t_HCurWnd;
106 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> t_Converter;
107 std::string t_Narrow = t_Converter.to_bytes(p_Wstring);
114 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> t_Converter;
115 std::wstring t_Wide = t_Converter.from_bytes(p_String);
125 if (SetConsoleOutputCP(CP_UTF8) == 0)
128 "Failed to set the console output to UTF-8. The error number was {}.",
134 if (SetConsoleCtrlHandler(ProcessConsoleShutdown,
true) ==
false)
136 spdlog::error(
"Failed to initialize console shutdown events.");
155 char*
const p_Target,
156 const size_t p_MaxLengthThatWillFitInTarget,
157 const std::string& p_Source)
161 const errno_t t_CopyResult = strcpy_s(
163 p_MaxLengthThatWillFitInTarget,
165 if (t_CopyResult != 0)
168 "Copying the string {} resulted in the error {}."
179 const short int p_ConsoleWidth,
180 const short int p_ConsoleHeight,
181 const short int p_ConsoleScrollback)
183 bool t_Absolute =
true;
184 HANDLE t_Console = GetStdHandle(STD_OUTPUT_HANDLE);
193 auto t_MaxConsoleSize = GetLargestConsoleWindowSize(t_Console);
194 const short int t_WindowHeight =
195 std::min(t_BufferSize.Y,
196 std::min(p_ConsoleHeight, t_MaxConsoleSize.Y));
197 _SMALL_RECT t_ConsoleRect =
201 std::min(t_BufferSize.X, t_MaxConsoleSize.X) - 1,
208 if (!SetConsoleScreenBufferSize(t_Console, t_BufferSize))
210 DWORD t_Error = GetLastError();
212 "Setting the console screen buffer size failed with error {}: {}",
214 GetStringForError(t_Error));
219 if (!SetConsoleWindowInfo(t_Console, t_Absolute, &t_ConsoleRect))
221 DWORD t_Error = GetLastError();
223 "Setting the console window size failed with error {}: {}",
225 GetStringForError(t_Error));
233 const int p_ConsoleCurrentOffset)
235 COORD t_Pos = { 0,
static_cast<SHORT
>(p_ConsoleCurrentOffset) };
236 HANDLE t_Output = GetStdHandle(STD_OUTPUT_HANDLE);
237 SetConsoleCursorPosition(t_Output, t_Pos);
242 COORD t_TopLeft = { 0, 0 };
243 HANDLE t_Console = GetStdHandle(STD_OUTPUT_HANDLE);
244 CONSOLE_SCREEN_BUFFER_INFO t_Screen;
247 GetConsoleScreenBufferInfo(t_Console, &t_Screen);
248 FillConsoleOutputCharacterA(
251 t_Screen.dwSize.X * t_Screen.dwSize.Y,
254 FillConsoleOutputAttribute(
256 FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
257 t_Screen.dwSize.X * t_Screen.dwSize.Y,
260 SetConsoleCursorPosition(t_Console, t_TopLeft);
265 if (DoesWindowHaveFocus())
267 bool t_Res = GetAsyncKeyState(p_Key) & 0x8000;
278 if (DoesWindowHaveFocus())
281 bool t_State = GetAsyncKeyState(p_Key) & 0x8000;
294 if (DoesWindowHaveFocus())
297 bool t_State = GetAsyncKeyState(p_Key) & 0x8000;
310 std::wstringstream t_Wss;
313 wchar_t* t_Path = NULL;
314 HRESULT t_Result = SHGetKnownFolderPath(
321 if (SUCCEEDED(t_Result))
323 std::wstringstream t_Wss;
326 CoTaskMemFree(t_Path);
328 std::string t_NarrowDocumentsDir =
331 return t_NarrowDocumentsDir;
335 spdlog::warn(
"Could not get the directory path for the documents.");
338 return std::string(
"");
342 std::string p_Path_UTF8)
346 return std::ifstream(t_WidePath, std::ifstream::binary);
350 std::string p_Path_UTF8)
354 return std::ofstream(t_WidePath, std::ofstream::binary);
361 return std::filesystem::exists(t_WidePath);
365 std::string p_Path_UTF8)
371 std::filesystem::create_directory(t_WidePath);
CORESDK_API SDKReturnCode CoreSdk_ShutDown()
Shut down the wrapper. This needs to be called last.
void ApplyConsolePosition(const int p_ConsoleCurrentOffset)
Set the current console position. This handles the platform-specific part of advancing the console po...
static void ClearConsole(void)
Clear the console window.
void UpdateInput(void)
Update the current keyboard state.
bool PlatformSpecificInitialization(void)
Initialise things only needed for this platform.
bool GetKeyDown(const int p_Key)
Returns true first time it is called when key is pressed.
bool GetKeyUp(const int p_Key)
Returns true first time it is called when key is released.
static bool CopyString(char *const p_Target, const size_t p_MaxLengthThatWillFitInTarget, const std::string &p_Source)
Copy the given string into the given target.
bool ResizeWindow(const short int p_ConsoleWidth, const short int p_ConsoleHeight, const short int p_ConsoleScrollback)
Resize the window, so the log messages will fit. Make sure not to enter illegal sizes in here or SetC...
bool DoesFolderOrFileExist(std::string p_Path_UTF8)
Check if the given folder or file exists. The folder path given should be in UTF-8 format.
bool PlatformSpecificShutdown(void)
Shut down things only needed for this platform.
bool GetKey(const int p_Key)
Gets key's current state. True is pressed false is not pressed.
std::ofstream GetOutputFileStream(std::string p_Path_UTF8)
Get an output stream for the given file. The file's path should be in UTF-8 format.
std::string GetDocumentsDirectoryPath_UTF8(void)
Get the path to the user's Documents folder. The string should be in UTF-8 format.
void CreateFolderIfItDoesNotExist(std::string p_Path_UTF8)
Create the given folder if it does not exist. The folder path given should be in UTF-8 format.
static const std::string s_SlashForFilesystemPath
The slash character that is used in the filesystem.
std::ifstream GetInputFileStream(std::string p_Path_UTF8)
Get an input stream for the given file. The file's path should be in UTF-8 format.