From 0f461b7695834231fd87d8c6b3094d3bb02f0f84 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 2 Apr 2023 14:52:12 +0200 Subject: [PATCH] /W4: Fix unreferenced functions and light cleanup * Fully commented 'UTF8Decode' to suppress warnings (currently unsupported). * Cleaned up 'GetModuleInfo' and 'DbgPrint' to use the non-wide string versions instead. * Added error handling in 'PrintLastError'. --- r5dev/public/utility/utility.cpp | 63 +++++++++++++++----------------- r5dev/public/utility/utility.h | 2 +- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/r5dev/public/utility/utility.cpp b/r5dev/public/utility/utility.cpp index 4869b266..e3158416 100644 --- a/r5dev/public/utility/utility.cpp +++ b/r5dev/public/utility/utility.cpp @@ -41,19 +41,13 @@ BOOL IsBadReadPtrV2(void* ptr) MODULEINFO GetModuleInfo(const char* szModule) { MODULEINFO modinfo = { 0 }; + HMODULE hModule = GetModuleHandleA(szModule); - wchar_t szWtext[256]{}; - mbstowcs(szWtext, szModule, strlen(szModule) + 1); - - HMODULE hModule = GetModuleHandle(szWtext); - if (hModule == INVALID_HANDLE_VALUE) - { - return modinfo; - } - if (hModule) + if (hModule && hModule != INVALID_HANDLE_VALUE) { GetModuleInformation(GetCurrentProcess(), hModule, &modinfo, sizeof(MODULEINFO)); } + return modinfo; } @@ -61,21 +55,14 @@ MODULEINFO GetModuleInfo(const char* szModule) // For printing output to the debugger. void DbgPrint(LPCSTR sFormat, ...) { - CHAR sBuffer[512] = { 0 }; - va_list sArgs = {}; - - // Get the variable arg pointer. + va_list sArgs; va_start(sArgs, sFormat); - // Format print the string. - int length = vsnprintf(sBuffer, sizeof(sBuffer), sFormat, sArgs); + string result = FormatV(sFormat, sArgs); va_end(sArgs); - wchar_t szWtext[512]{}; // Convert to LPCWSTR. - mbstowcs(szWtext, sBuffer, strlen(sBuffer) + 1); - // Output the string to the debugger. - OutputDebugString(szWtext); + OutputDebugStringA(result.c_str()); } /////////////////////////////////////////////////////////////////////////////// @@ -85,12 +72,20 @@ void PrintLastError(void) DWORD errorMessageID = ::GetLastError(); if (errorMessageID != NULL) { - LPSTR messageBuffer = nullptr; - size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + LPSTR messageBuffer; + DWORD size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); - spdlog::error("{:s}\n", messageBuffer); - LocalFree(messageBuffer); + if (size > 0) + { + spdlog::error("{:s}\n", messageBuffer); + LocalFree(messageBuffer); + } + else // FormatMessageA failed. + { + spdlog::error("{:s}: Failed: {:s}\n", __FUNCTION__, + std::system_category().message(static_cast(::GetLastError()))); + } } } @@ -523,17 +518,17 @@ string UTF8Encode(const wstring& wsvInput) /////////////////////////////////////////////////////////////////////////////// // For decoding data in UTF-8. -string UTF8Decode(const string& svInput) -{ - //struct destructible_codecvt : public std::codecvt - //{ - // using std::codecvt::codecvt; - // ~destructible_codecvt() = default; - //}; - //std::wstring_convert utf32_converter; - //return utf32_converter.from_bytes(svInput); - return ""; -} +//string UTF8Decode(const string& svInput) +//{ +// struct destructible_codecvt : public std::codecvt +// { +// using std::codecvt::codecvt; +// ~destructible_codecvt() = default; +// }; +// std::wstring_convert utf32_converter; +// return utf32_converter.from_bytes(svInput); +// return ""; +//} /////////////////////////////////////////////////////////////////////////////// // For obtaining UTF-8 character length. diff --git a/r5dev/public/utility/utility.h b/r5dev/public/utility/utility.h index 8ca00d2f..b26d7e2e 100644 --- a/r5dev/public/utility/utility.h +++ b/r5dev/public/utility/utility.h @@ -40,7 +40,7 @@ string Base64Encode(const string& svInput); string Base64Decode(const string& svInput); string UTF8Encode(const wstring& wsvInput); -string UTF8Decode(const string& svInput); +//string UTF8Decode(const string& svInput); size_t UTF8CharLength(const uint8_t cInput); bool IsValidUTF8(char* pszString);