From 5e725913f8cd7e429db221408930af44ecbc206d Mon Sep 17 00:00:00 2001 From: Silent Date: Fri, 11 Mar 2022 11:59:09 +0100 Subject: [PATCH] Close #16566: Favour WinNT preprocessor version checks over MinGW (#16677) --- src/openrct2-ui/UiContext.Win32.cpp | 8 +-- src/openrct2-ui/windows/Changelog.cpp | 8 +-- src/openrct2/cmdline/RootCommands.cpp | 8 +-- src/openrct2/core/FileWatcher.cpp | 2 +- src/openrct2/core/Imaging.cpp | 15 ++---- src/openrct2/core/String.cpp | 6 --- src/openrct2/network/NetworkBase.cpp | 16 +----- src/openrct2/network/Socket.cpp | 2 +- src/openrct2/platform/Platform.Win32.cpp | 63 ++++++++++-------------- 9 files changed, 40 insertions(+), 88 deletions(-) diff --git a/src/openrct2-ui/UiContext.Win32.cpp b/src/openrct2-ui/UiContext.Win32.cpp index c33267f4ba..30a8c79161 100644 --- a/src/openrct2-ui/UiContext.Win32.cpp +++ b/src/openrct2-ui/UiContext.Win32.cpp @@ -9,12 +9,6 @@ #ifdef _WIN32 -# if defined(__MINGW32__) && !defined(WINVER) && !defined(_WIN32_WINNT) -// 0x0600 == vista -# define WINVER 0x0600 -# define _WIN32_WINNT 0x0600 -# endif // __MINGW32__ - // Windows.h needs to be included first // clang-format off # include @@ -41,7 +35,7 @@ static std::wstring SHGetPathFromIDListLongPath(LPCITEMIDLIST pidl) { -# if defined(__MINGW32__) +# if _WIN32_WINNT < 0x0600 std::wstring pszPath(MAX_PATH, 0); auto result = SHGetPathFromIDListW(pidl, pszPath.data()); # else diff --git a/src/openrct2-ui/windows/Changelog.cpp b/src/openrct2-ui/windows/Changelog.cpp index 483a3d4962..8a1b68c7df 100644 --- a/src/openrct2-ui/windows/Changelog.cpp +++ b/src/openrct2-ui/windows/Changelog.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -65,12 +66,7 @@ public: const std::string GetChangelogText() { auto path = GetChangelogPath(); -#if defined(_WIN32) && !defined(__MINGW32__) - auto pathW = String::ToWideChar(path); - auto fs = std::ifstream(pathW, std::ios::in); -#else - auto fs = std::ifstream(path, std::ios::in); -#endif + auto fs = std::ifstream(u8path(path), std::ios::in); if (!fs.is_open()) { throw std::runtime_error("Unable to open " + path); diff --git a/src/openrct2/cmdline/RootCommands.cpp b/src/openrct2/cmdline/RootCommands.cpp index 46cc4d9b17..bf60cba39d 100644 --- a/src/openrct2/cmdline/RootCommands.cpp +++ b/src/openrct2/cmdline/RootCommands.cpp @@ -96,7 +96,7 @@ static exitcode_t HandleCommandJoin(CommandLineArgEnumerator * enumerator); static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator * enumerator); static exitcode_t HandleCommandScanObjects(CommandLineArgEnumerator * enumerator); -#if defined(_WIN32) && !defined(__MINGW32__) +#if defined(_WIN32) && _WIN32_WINNT >= 0x0600 static bool _removeShell = false; @@ -133,7 +133,7 @@ const CommandLineCommand CommandLine::RootCommands[] DefineCommand("scan-objects", "", StandardOptions, HandleCommandScanObjects), DefineCommand("handle-uri", "openrct2://.../", StandardOptions, CommandLine::HandleCommandUri), -#if defined(_WIN32) && !defined(__MINGW32__) +#if defined(_WIN32) && _WIN32_WINNT >= 0x0600 DefineCommand("register-shell", "", RegisterShellOptions, HandleCommandRegisterShell), #endif @@ -400,7 +400,7 @@ static exitcode_t HandleCommandScanObjects([[maybe_unused]] CommandLineArgEnumer return EXITCODE_OK; } -#if defined(_WIN32) && !defined(__MINGW32__) +#if defined(_WIN32) && _WIN32_WINNT >= 0x0600 static exitcode_t HandleCommandRegisterShell([[maybe_unused]] CommandLineArgEnumerator* enumerator) { exitcode_t result = CommandLine::HandleCommandDefault(); @@ -419,7 +419,7 @@ static exitcode_t HandleCommandRegisterShell([[maybe_unused]] CommandLineArgEnum } return EXITCODE_OK; } -#endif // defined(_WIN32) && !defined(__MINGW32__) +#endif // defined(_WIN32) && _WIN32_WINNT >= 0x0600 static void PrintAbout() { diff --git a/src/openrct2/core/FileWatcher.cpp b/src/openrct2/core/FileWatcher.cpp index 666e0d7497..c4f36c55f4 100644 --- a/src/openrct2/core/FileWatcher.cpp +++ b/src/openrct2/core/FileWatcher.cpp @@ -111,7 +111,7 @@ FileWatcher::FileWatcher(const std::string& directoryPath) FileWatcher::~FileWatcher() { #ifdef _WIN32 -# ifdef __MINGW32__ +# if _WIN32_WINNT < 0x0600 // TODO CancelIo is documented as not working across a different thread but // CancelIoEx is not available. CancelIo(_directoryHandle); diff --git a/src/openrct2/core/Imaging.cpp b/src/openrct2/core/Imaging.cpp index e27d072b57..f842189ad4 100644 --- a/src/openrct2/core/Imaging.cpp +++ b/src/openrct2/core/Imaging.cpp @@ -13,6 +13,7 @@ #include "../Version.h" #include "../drawing/Drawing.h" +#include "FileSystem.hpp" #include "Guard.hpp" #include "IStream.hpp" #include "Memory.hpp" @@ -311,12 +312,7 @@ namespace Imaging return ReadFromFile(path, GetImageFormatFromPath(path)); default: { -#if defined(_WIN32) && !defined(__MINGW32__) - auto pathW = String::ToWideChar(path); - std::ifstream fs(pathW, std::ios::binary); -#else - std::ifstream fs(std::string(path), std::ios::binary); -#endif + std::ifstream fs(u8path(path), std::ios::binary); return ReadFromStream(fs, format); } } @@ -337,12 +333,7 @@ namespace Imaging break; case IMAGE_FORMAT::PNG: { -#if defined(_WIN32) && !defined(__MINGW32__) - auto pathW = String::ToWideChar(path); - std::ofstream fs(pathW, std::ios::binary); -#else - std::ofstream fs(std::string(path), std::ios::binary); -#endif + std::ofstream fs(u8path(path), std::ios::binary); WritePng(fs, image); break; } diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index c3c9ab78e1..d7e153f7fd 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -7,12 +7,6 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#if defined(__MINGW32__) && !defined(WINVER) && !defined(_WIN32_WINNT) -// 0x0600 == vista -# define WINVER 0x0600 -# define _WIN32_WINNT 0x0600 -#endif // __MINGW32__ - #include #include #include diff --git a/src/openrct2/network/NetworkBase.cpp b/src/openrct2/network/NetworkBase.cpp index 3c3d97cbd9..5be1d4622d 100644 --- a/src/openrct2/network/NetworkBase.cpp +++ b/src/openrct2/network/NetworkBase.cpp @@ -1085,13 +1085,7 @@ void NetworkBase::BeginChatLog() auto env = GetContext().GetPlatformEnvironment(); auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_CHAT); _chatLogPath = BeginLog(directory, "", _chatLogFilenameFormat); - -# if defined(_WIN32) && !defined(__MINGW32__) - auto pathW = String::ToWideChar(_chatLogPath); - _chat_log_fs.open(pathW.c_str(), std::ios::out | std::ios::app); -# else - _chat_log_fs.open(_chatLogPath, std::ios::out | std::ios::app); -# endif + _chat_log_fs.open(u8path(_chatLogPath), std::ios::out | std::ios::app); } void NetworkBase::AppendChatLog(std::string_view s) @@ -1112,13 +1106,7 @@ void NetworkBase::BeginServerLog() auto env = GetContext().GetPlatformEnvironment(); auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_SERVER); _serverLogPath = BeginLog(directory, ServerName, _serverLogFilenameFormat); - -# if defined(_WIN32) && !defined(__MINGW32__) - auto pathW = String::ToWideChar(_serverLogPath); - _server_log_fs.open(pathW.c_str(), std::ios::out | std::ios::app | std::ios::binary); -# else - _server_log_fs.open(_serverLogPath, std::ios::out | std::ios::app | std::ios::binary); -# endif + _server_log_fs.open(u8path(_serverLogPath), std::ios::out | std::ios::app | std::ios::binary); // Log server start event utf8 logMessage[256]; diff --git a/src/openrct2/network/Socket.cpp b/src/openrct2/network/Socket.cpp index c2f566d1ec..51dcb2516b 100644 --- a/src/openrct2/network/Socket.cpp +++ b/src/openrct2/network/Socket.cpp @@ -643,7 +643,7 @@ private: std::string GetIpAddressFromSocket(const sockaddr_in* addr) const { std::string result; -# if defined(__MINGW32__) +# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600 if (addr->sin_family == AF_INET) { result = inet_ntoa(addr->sin_addr); diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 8e8371af50..a31d966237 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -7,12 +7,6 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ -#if defined(__MINGW32__) && !defined(WINVER) && !defined(_WIN32_WINNT) -// 0x0600 == vista -# define WINVER 0x0600 -# define _WIN32_WINNT 0x0600 -#endif // __MINGW32__ - #ifdef _WIN32 // Windows.h needs to be included first @@ -27,7 +21,7 @@ # include # undef GetEnvironmentVariable -# if !defined(__MINGW32__) && ((NTDDI_VERSION >= NTDDI_VISTA) && !defined(_USING_V110_SDK71_) && !defined(_ATL_XP_TARGETING)) +# if _WIN32_WINNT >= 0x0600 # define __USE_SHGETKNOWNFOLDERPATH__ # define __USE_GETDATEFORMATEX__ # else @@ -229,34 +223,39 @@ namespace Platform std::string FormatShortDate(std::time_t timestamp) { SYSTEMTIME st = TimeToSystemTime(timestamp); + std::string result; -# ifdef __USE_GETDATEFORMATEX__ wchar_t date[20]; - GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, (int)std::size(date), nullptr); - std::string result = String::ToUtf8(std::wstring(date)); +# ifdef __USE_GETDATEFORMATEX__ + ptrdiff_t charsWritten = GetDateFormatEx( + LOCALE_NAME_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, static_cast(std::size(date)), nullptr); # else - char date[20]; - GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, sizeof(date)); - std::string result(date); + ptrdiff_t charsWritten = GetDateFormatW( + LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, static_cast(std::size(date))); # endif - + if (charsWritten != 0) + { + result = String::ToUtf8(std::wstring_view(date, charsWritten - 1)); + } return result; } std::string FormatTime(std::time_t timestamp) { SYSTEMTIME st = TimeToSystemTime(timestamp); + std::string result; -# ifdef __USE_GETDATEFORMATEX__ wchar_t time[20]; - GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &st, nullptr, time, (int)std::size(time)); - std::string result = String::ToUtf8(std::wstring(time)); +# ifdef __USE_GETDATEFORMATEX__ + ptrdiff_t charsWritten = GetTimeFormatEx( + LOCALE_NAME_USER_DEFAULT, 0, &st, nullptr, time, static_cast(std::size(time))); # else - char time[20]; - GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, nullptr, time, sizeof(time)); - std::string result(time); + ptrdiff_t charsWritten = GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, nullptr, time, static_cast(std::size(time))); # endif - + if (charsWritten != 0) + { + result = String::ToUtf8(std::wstring_view(time, charsWritten - 1)); + } return result; } @@ -355,7 +354,7 @@ namespace Platform wchar_t* wpath = nullptr; if (SUCCEEDED(SHGetKnownFolderPath(rfid, KF_FLAG_CREATE, nullptr, &wpath))) { - path = String::ToUtf8(std::wstring(wpath)); + path = String::ToUtf8(wpath); } CoTaskMemFree(wpath); return path; @@ -367,7 +366,7 @@ namespace Platform wchar_t wpath[MAX_PATH]; if (SUCCEEDED(SHGetFolderPathW(nullptr, nFolder | CSIDL_FLAG_CREATE, nullptr, 0, wpath))) { - path = String::ToUtf8(std::wstring(wpath)); + path = String::ToUtf8(wpath); } return path; } @@ -871,22 +870,12 @@ namespace Platform std::string GetFontPath(const TTFFontDescriptor& font) { -# if !defined(__MINGW32__) && ((NTDDI_VERSION >= NTDDI_VISTA) && !defined(_USING_V110_SDK71_) && !defined(_ATL_XP_TARGETING)) - wchar_t* fontFolder; - if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Fonts, 0, nullptr, &fontFolder))) - { - // Convert wchar to utf8, then copy the font folder path to the buffer. - auto outPathTemp = String::ToUtf8(fontFolder); - CoTaskMemFree(fontFolder); - - return Path::Combine(outPathTemp, font.filename); - } - - return {}; +# if defined(__USE_SHGETKNOWNFOLDERPATH__) + auto path = WIN32_GetKnownFolderPath(FOLDERID_Fonts); # else - log_warning("Compatibility hack: falling back to C:\\Windows\\Fonts"); - return Path::Combine(u8"C:\\Windows\\Fonts\\", font.filename); + auto path = WIN32_GetFolderPath(CSIDL_FONTS); # endif + return !path.empty() ? Path::Combine(path, font.filename) : std::string(); } bool EnsureDirectoryExists(u8string_view path)