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
# 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 <windows.h>
@ -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

View File

@ -15,6 +15,7 @@
#include <openrct2/OpenRCT2.h>
#include <openrct2/PlatformEnvironment.h>
#include <openrct2/Version.h>
#include <openrct2/core/FileSystem.hpp>
#include <openrct2/core/String.hpp>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/localisation/Localisation.h>
@ -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);

View File

@ -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", "<path>", 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()
{

View File

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

View File

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

View File

@ -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 <algorithm>
#include <cctype>
#include <cwctype>

View File

@ -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];

View File

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

View File

@ -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 <shlobj.h>
# 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<int>(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<int>(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<int>(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<int>(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)