Close #16566: Favour WinNT preprocessor version checks over MinGW (#16677)

This commit is contained in:
Silent 2022-03-11 11:59:09 +01:00 committed by GitHub
parent 49fbc30a97
commit 5e725913f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 40 additions and 88 deletions

View File

@ -9,12 +9,6 @@
#ifdef _WIN32 #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 // Windows.h needs to be included first
// clang-format off // clang-format off
# include <windows.h> # include <windows.h>
@ -41,7 +35,7 @@
static std::wstring SHGetPathFromIDListLongPath(LPCITEMIDLIST pidl) static std::wstring SHGetPathFromIDListLongPath(LPCITEMIDLIST pidl)
{ {
# if defined(__MINGW32__) # if _WIN32_WINNT < 0x0600
std::wstring pszPath(MAX_PATH, 0); std::wstring pszPath(MAX_PATH, 0);
auto result = SHGetPathFromIDListW(pidl, pszPath.data()); auto result = SHGetPathFromIDListW(pidl, pszPath.data());
# else # else

View File

@ -15,6 +15,7 @@
#include <openrct2/OpenRCT2.h> #include <openrct2/OpenRCT2.h>
#include <openrct2/PlatformEnvironment.h> #include <openrct2/PlatformEnvironment.h>
#include <openrct2/Version.h> #include <openrct2/Version.h>
#include <openrct2/core/FileSystem.hpp>
#include <openrct2/core/String.hpp> #include <openrct2/core/String.hpp>
#include <openrct2/drawing/Drawing.h> #include <openrct2/drawing/Drawing.h>
#include <openrct2/localisation/Localisation.h> #include <openrct2/localisation/Localisation.h>
@ -65,12 +66,7 @@ public:
const std::string GetChangelogText() const std::string GetChangelogText()
{ {
auto path = GetChangelogPath(); auto path = GetChangelogPath();
#if defined(_WIN32) && !defined(__MINGW32__) auto fs = std::ifstream(u8path(path), std::ios::in);
auto pathW = String::ToWideChar(path);
auto fs = std::ifstream(pathW, std::ios::in);
#else
auto fs = std::ifstream(path, std::ios::in);
#endif
if (!fs.is_open()) if (!fs.is_open())
{ {
throw std::runtime_error("Unable to open " + path); throw std::runtime_error("Unable to open " + path);

View File

@ -96,7 +96,7 @@ static exitcode_t HandleCommandJoin(CommandLineArgEnumerator * enumerator);
static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator * enumerator); static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator * enumerator);
static exitcode_t HandleCommandScanObjects(CommandLineArgEnumerator * enumerator); static exitcode_t HandleCommandScanObjects(CommandLineArgEnumerator * enumerator);
#if defined(_WIN32) && !defined(__MINGW32__) #if defined(_WIN32) && _WIN32_WINNT >= 0x0600
static bool _removeShell = false; static bool _removeShell = false;
@ -133,7 +133,7 @@ const CommandLineCommand CommandLine::RootCommands[]
DefineCommand("scan-objects", "<path>", StandardOptions, HandleCommandScanObjects), DefineCommand("scan-objects", "<path>", StandardOptions, HandleCommandScanObjects),
DefineCommand("handle-uri", "openrct2://.../", StandardOptions, CommandLine::HandleCommandUri), DefineCommand("handle-uri", "openrct2://.../", StandardOptions, CommandLine::HandleCommandUri),
#if defined(_WIN32) && !defined(__MINGW32__) #if defined(_WIN32) && _WIN32_WINNT >= 0x0600
DefineCommand("register-shell", "", RegisterShellOptions, HandleCommandRegisterShell), DefineCommand("register-shell", "", RegisterShellOptions, HandleCommandRegisterShell),
#endif #endif
@ -400,7 +400,7 @@ static exitcode_t HandleCommandScanObjects([[maybe_unused]] CommandLineArgEnumer
return EXITCODE_OK; return EXITCODE_OK;
} }
#if defined(_WIN32) && !defined(__MINGW32__) #if defined(_WIN32) && _WIN32_WINNT >= 0x0600
static exitcode_t HandleCommandRegisterShell([[maybe_unused]] CommandLineArgEnumerator* enumerator) static exitcode_t HandleCommandRegisterShell([[maybe_unused]] CommandLineArgEnumerator* enumerator)
{ {
exitcode_t result = CommandLine::HandleCommandDefault(); exitcode_t result = CommandLine::HandleCommandDefault();
@ -419,7 +419,7 @@ static exitcode_t HandleCommandRegisterShell([[maybe_unused]] CommandLineArgEnum
} }
return EXITCODE_OK; return EXITCODE_OK;
} }
#endif // defined(_WIN32) && !defined(__MINGW32__) #endif // defined(_WIN32) && _WIN32_WINNT >= 0x0600
static void PrintAbout() static void PrintAbout()
{ {

View File

@ -111,7 +111,7 @@ FileWatcher::FileWatcher(const std::string& directoryPath)
FileWatcher::~FileWatcher() FileWatcher::~FileWatcher()
{ {
#ifdef _WIN32 #ifdef _WIN32
# ifdef __MINGW32__ # if _WIN32_WINNT < 0x0600
// TODO CancelIo is documented as not working across a different thread but // TODO CancelIo is documented as not working across a different thread but
// CancelIoEx is not available. // CancelIoEx is not available.
CancelIo(_directoryHandle); CancelIo(_directoryHandle);

View File

@ -13,6 +13,7 @@
#include "../Version.h" #include "../Version.h"
#include "../drawing/Drawing.h" #include "../drawing/Drawing.h"
#include "FileSystem.hpp"
#include "Guard.hpp" #include "Guard.hpp"
#include "IStream.hpp" #include "IStream.hpp"
#include "Memory.hpp" #include "Memory.hpp"
@ -311,12 +312,7 @@ namespace Imaging
return ReadFromFile(path, GetImageFormatFromPath(path)); return ReadFromFile(path, GetImageFormatFromPath(path));
default: default:
{ {
#if defined(_WIN32) && !defined(__MINGW32__) std::ifstream fs(u8path(path), std::ios::binary);
auto pathW = String::ToWideChar(path);
std::ifstream fs(pathW, std::ios::binary);
#else
std::ifstream fs(std::string(path), std::ios::binary);
#endif
return ReadFromStream(fs, format); return ReadFromStream(fs, format);
} }
} }
@ -337,12 +333,7 @@ namespace Imaging
break; break;
case IMAGE_FORMAT::PNG: case IMAGE_FORMAT::PNG:
{ {
#if defined(_WIN32) && !defined(__MINGW32__) std::ofstream fs(u8path(path), std::ios::binary);
auto pathW = String::ToWideChar(path);
std::ofstream fs(pathW, std::ios::binary);
#else
std::ofstream fs(std::string(path), std::ios::binary);
#endif
WritePng(fs, image); WritePng(fs, image);
break; break;
} }

View File

@ -7,12 +7,6 @@
* OpenRCT2 is licensed under the GNU General Public License version 3. * 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 <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cwctype> #include <cwctype>

View File

@ -1085,13 +1085,7 @@ void NetworkBase::BeginChatLog()
auto env = GetContext().GetPlatformEnvironment(); auto env = GetContext().GetPlatformEnvironment();
auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_CHAT); auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_CHAT);
_chatLogPath = BeginLog(directory, "", _chatLogFilenameFormat); _chatLogPath = BeginLog(directory, "", _chatLogFilenameFormat);
_chat_log_fs.open(u8path(_chatLogPath), std::ios::out | std::ios::app);
# 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
} }
void NetworkBase::AppendChatLog(std::string_view s) void NetworkBase::AppendChatLog(std::string_view s)
@ -1112,13 +1106,7 @@ void NetworkBase::BeginServerLog()
auto env = GetContext().GetPlatformEnvironment(); auto env = GetContext().GetPlatformEnvironment();
auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_SERVER); auto directory = env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_SERVER);
_serverLogPath = BeginLog(directory, ServerName, _serverLogFilenameFormat); _serverLogPath = BeginLog(directory, ServerName, _serverLogFilenameFormat);
_server_log_fs.open(u8path(_serverLogPath), std::ios::out | std::ios::app | std::ios::binary);
# 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
// Log server start event // Log server start event
utf8 logMessage[256]; utf8 logMessage[256];

View File

@ -643,7 +643,7 @@ private:
std::string GetIpAddressFromSocket(const sockaddr_in* addr) const std::string GetIpAddressFromSocket(const sockaddr_in* addr) const
{ {
std::string result; std::string result;
# if defined(__MINGW32__) # if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
if (addr->sin_family == AF_INET) if (addr->sin_family == AF_INET)
{ {
result = inet_ntoa(addr->sin_addr); result = inet_ntoa(addr->sin_addr);

View File

@ -7,12 +7,6 @@
* OpenRCT2 is licensed under the GNU General Public License version 3. * 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 #ifdef _WIN32
// Windows.h needs to be included first // Windows.h needs to be included first
@ -27,7 +21,7 @@
# include <shlobj.h> # include <shlobj.h>
# undef GetEnvironmentVariable # 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_SHGETKNOWNFOLDERPATH__
# define __USE_GETDATEFORMATEX__ # define __USE_GETDATEFORMATEX__
# else # else
@ -229,34 +223,39 @@ namespace Platform
std::string FormatShortDate(std::time_t timestamp) std::string FormatShortDate(std::time_t timestamp)
{ {
SYSTEMTIME st = TimeToSystemTime(timestamp); SYSTEMTIME st = TimeToSystemTime(timestamp);
std::string result;
# ifdef __USE_GETDATEFORMATEX__
wchar_t date[20]; wchar_t date[20];
GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, (int)std::size(date), nullptr); # ifdef __USE_GETDATEFORMATEX__
std::string result = String::ToUtf8(std::wstring(date)); ptrdiff_t charsWritten = GetDateFormatEx(
LOCALE_NAME_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, static_cast<int>(std::size(date)), nullptr);
# else # else
char date[20]; ptrdiff_t charsWritten = GetDateFormatW(
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, sizeof(date)); LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, static_cast<int>(std::size(date)));
std::string result(date);
# endif # endif
if (charsWritten != 0)
{
result = String::ToUtf8(std::wstring_view(date, charsWritten - 1));
}
return result; return result;
} }
std::string FormatTime(std::time_t timestamp) std::string FormatTime(std::time_t timestamp)
{ {
SYSTEMTIME st = TimeToSystemTime(timestamp); SYSTEMTIME st = TimeToSystemTime(timestamp);
std::string result;
# ifdef __USE_GETDATEFORMATEX__
wchar_t time[20]; wchar_t time[20];
GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &st, nullptr, time, (int)std::size(time)); # ifdef __USE_GETDATEFORMATEX__
std::string result = String::ToUtf8(std::wstring(time)); ptrdiff_t charsWritten = GetTimeFormatEx(
LOCALE_NAME_USER_DEFAULT, 0, &st, nullptr, time, static_cast<int>(std::size(time)));
# else # else
char time[20]; ptrdiff_t charsWritten = GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, nullptr, time, static_cast<int>(std::size(time)));
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, nullptr, time, sizeof(time));
std::string result(time);
# endif # endif
if (charsWritten != 0)
{
result = String::ToUtf8(std::wstring_view(time, charsWritten - 1));
}
return result; return result;
} }
@ -355,7 +354,7 @@ namespace Platform
wchar_t* wpath = nullptr; wchar_t* wpath = nullptr;
if (SUCCEEDED(SHGetKnownFolderPath(rfid, KF_FLAG_CREATE, nullptr, &wpath))) if (SUCCEEDED(SHGetKnownFolderPath(rfid, KF_FLAG_CREATE, nullptr, &wpath)))
{ {
path = String::ToUtf8(std::wstring(wpath)); path = String::ToUtf8(wpath);
} }
CoTaskMemFree(wpath); CoTaskMemFree(wpath);
return path; return path;
@ -367,7 +366,7 @@ namespace Platform
wchar_t wpath[MAX_PATH]; wchar_t wpath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(nullptr, nFolder | CSIDL_FLAG_CREATE, nullptr, 0, wpath))) if (SUCCEEDED(SHGetFolderPathW(nullptr, nFolder | CSIDL_FLAG_CREATE, nullptr, 0, wpath)))
{ {
path = String::ToUtf8(std::wstring(wpath)); path = String::ToUtf8(wpath);
} }
return path; return path;
} }
@ -871,22 +870,12 @@ namespace Platform
std::string GetFontPath(const TTFFontDescriptor& font) std::string GetFontPath(const TTFFontDescriptor& font)
{ {
# if !defined(__MINGW32__) && ((NTDDI_VERSION >= NTDDI_VISTA) && !defined(_USING_V110_SDK71_) && !defined(_ATL_XP_TARGETING)) # if defined(__USE_SHGETKNOWNFOLDERPATH__)
wchar_t* fontFolder; auto path = WIN32_GetKnownFolderPath(FOLDERID_Fonts);
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 {};
# else # else
log_warning("Compatibility hack: falling back to C:\\Windows\\Fonts"); auto path = WIN32_GetFolderPath(CSIDL_FONTS);
return Path::Combine(u8"C:\\Windows\\Fonts\\", font.filename);
# endif # endif
return !path.empty() ? Path::Combine(path, font.filename) : std::string();
} }
bool EnsureDirectoryExists(u8string_view path) bool EnsureDirectoryExists(u8string_view path)