From 040c4fe82f6cca6eeeeace2293a51075e6727119 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 17 Sep 2023 07:06:27 +0100 Subject: [PATCH] Generalize support for Windows without wchar APIs Replaces `NXDK` ifdefs that relate to the lack of wchar APIs with the more general `DEVILUTIONX_WINDOWS_NO_WCHAR` ifdefs. This should make it much easier to port to Windows 98. --- CMake/Definitions.cmake | 1 + CMake/platforms/xbox_nxdk.cmake | 1 + Source/dvlnet/zerotier_native.cpp | 2 +- Source/init.cpp | 4 +-- Source/platform/locale.cpp | 6 ++-- Source/utils/console.cpp | 2 +- Source/utils/file_util.cpp | 50 +++++++++++++------------- Source/utils/file_util.h | 2 +- Source/utils/sdl2_to_1_2_backports.cpp | 4 +-- 9 files changed, 37 insertions(+), 35 deletions(-) diff --git a/CMake/Definitions.cmake b/CMake/Definitions.cmake index 995821b8c..5d6b2081f 100644 --- a/CMake/Definitions.cmake +++ b/CMake/Definitions.cmake @@ -20,6 +20,7 @@ foreach( DEVILUTIONX_PALETTE_TRANSPARENCY_BLACK_16_LUT UNPACKED_MPQS UNPACKED_SAVES + DEVILUTIONX_WINDOWS_NO_WCHAR ) if(${def_name}) list(APPEND DEVILUTIONX_DEFINITIONS ${def_name}) diff --git a/CMake/platforms/xbox_nxdk.cmake b/CMake/platforms/xbox_nxdk.cmake index 8070ee25d..9a0137566 100644 --- a/CMake/platforms/xbox_nxdk.cmake +++ b/CMake/platforms/xbox_nxdk.cmake @@ -9,6 +9,7 @@ set(DEVILUTIONX_SYSTEM_LIBFMT OFF) set(BUILD_ASSETS_MPQ OFF) set(DEVILUTIONX_ASSETS_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/pkg/assets") +set(DEVILUTIONX_WINDOWS_NO_WCHAR ON) set(DEVILUTIONX_RESAMPLER_SPEEX OFF) set(DEFAULT_AUDIO_BUFFER_SIZE 5120) diff --git a/Source/dvlnet/zerotier_native.cpp b/Source/dvlnet/zerotier_native.cpp index feaf9b8ce..921b13a78 100644 --- a/Source/dvlnet/zerotier_native.cpp +++ b/Source/dvlnet/zerotier_native.cpp @@ -10,7 +10,7 @@ #include "utils/sdl2_backports.h" #endif -#if (defined(_WIN64) || defined(_WIN32)) && !defined(NXDK) +#if defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) #include "utils/stdcompat/filesystem.hpp" #ifdef DVL_HAS_FILESYSTEM #define DVL_ZT_SYMLINK diff --git a/Source/init.cpp b/Source/init.cpp index 160a59ba6..739a78b66 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -12,7 +12,7 @@ #include #include -#if (defined(_WIN64) || defined(_WIN32)) && !defined(__UWP__) && !defined(NXDK) +#if defined(_WIN32) && !defined(__UWP__) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) #include #endif @@ -147,7 +147,7 @@ std::vector GetMPQSearchPaths() } #elif defined(NXDK) paths.emplace_back("D:\\"); -#elif (defined(_WIN64) || defined(_WIN32)) && !defined(__UWP__) && !defined(NXDK) +#elif defined(_WIN32) && !defined(__UWP__) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) char gogpath[_FSG_PATH_MAX]; fsg_get_gog_game_path(gogpath, "1412601690"); if (strlen(gogpath) > 0) { diff --git a/Source/platform/locale.cpp b/Source/platform/locale.cpp index 76fb40fce..dba90787d 100644 --- a/Source/platform/locale.cpp +++ b/Source/platform/locale.cpp @@ -14,7 +14,7 @@ #include #elif defined(__3DS__) #include "platform/ctr/locale.hpp" -#elif defined(_WIN32) && !defined(NXDK) +#elif defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) // Suppress definitions of `min` and `max` macros by : #define NOMINMAX 1 #define WIN32_LEAN_AND_MEAN @@ -31,7 +31,7 @@ namespace devilution { namespace { -#if (defined(_WIN32) && WINVER >= 0x0600 && !defined(NXDK)) || (defined(__APPLE__) && defined(USE_COREFOUNDATION)) +#if (defined(_WIN32) && WINVER >= 0x0600 && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR)) || (defined(__APPLE__) && defined(USE_COREFOUNDATION)) std::string IetfToPosix(std::string_view langCode) { /* @@ -112,7 +112,7 @@ std::vector GetLocales() // use default #elif defined(__3DS__) locales.push_back(n3ds::GetLocale()); -#elif defined(_WIN32) && !defined(NXDK) +#elif defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) #if WINVER >= 0x0600 auto wideCharToUtf8 = [](PWSTR wideString) { // WideCharToMultiByte potentially leaves the buffer unterminated, default initialise here as a workaround diff --git a/Source/utils/console.cpp b/Source/utils/console.cpp index 8550e8222..af300423f 100644 --- a/Source/utils/console.cpp +++ b/Source/utils/console.cpp @@ -1,6 +1,6 @@ #include "./console.h" -#if (defined(_WIN64) || defined(_WIN32)) && !defined(NXDK) +#if defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) #include #include #include diff --git a/Source/utils/file_util.cpp b/Source/utils/file_util.cpp index 3d615d39c..4f67a1324 100644 --- a/Source/utils/file_util.cpp +++ b/Source/utils/file_util.cpp @@ -17,7 +17,7 @@ #include "utils/sdl2_to_1_2_backports.h" #endif -#if defined(_WIN64) || defined(_WIN32) +#ifdef _WIN32 #include // Suppress definitions of `min` and `max` macros by : @@ -25,12 +25,12 @@ #define WIN32_LEAN_AND_MEAN #include -#ifndef NXDK +#ifndef DEVILUTIONX_WINDOWS_NO_WCHAR #include #endif #endif -#if (_POSIX_C_SOURCE >= 200112L || defined(_BSD_SOURCE) || defined(__APPLE__)) && !defined(NXDK) +#if (_POSIX_C_SOURCE >= 200112L || defined(_BSD_SOURCE) || defined(__APPLE__)) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) #include #include #endif @@ -48,7 +48,7 @@ namespace devilution { -#if (defined(_WIN64) || defined(_WIN32)) && !defined(NXDK) +#if defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) std::unique_ptr ToWideChar(std::string_view path) { constexpr std::uint32_t flags = MB_ERR_INVALID_CHARS; @@ -77,8 +77,8 @@ std::string_view Dirname(std::string_view path) bool FileExists(const char *path) { -#if defined(_WIN64) || defined(_WIN32) -#if defined(NXDK) +#ifdef _WIN32 +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR const bool exists = ::GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES; #else const auto pathUtf16 = ToWideChar(path); @@ -92,7 +92,7 @@ bool FileExists(const char *path) if (::GetLastError() == ERROR_FILE_NOT_FOUND || ::GetLastError() == ERROR_PATH_NOT_FOUND) { ::SetLastError(ERROR_SUCCESS); } else { -#if defined(NXDK) +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR LogError("GetFileAttributesA: error code {}", ::GetLastError()); #else LogError("PathFileExistsW: error code {}", ::GetLastError()); @@ -115,11 +115,11 @@ bool FileExists(const char *path) #endif } -#if defined(_WIN64) || defined(_WIN32) +#ifdef _WIN32 namespace { DWORD WindowsGetFileAttributes(const char *path) { -#if defined(NXDK) +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR const DWORD attr = ::GetFileAttributesA(path); #else const auto pathUtf16 = ToWideChar(path); @@ -133,7 +133,7 @@ DWORD WindowsGetFileAttributes(const char *path) if (::GetLastError() == ERROR_FILE_NOT_FOUND || ::GetLastError() == ERROR_PATH_NOT_FOUND) { ::SetLastError(ERROR_SUCCESS); } else { -#if defined(NXDK) +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR LogError("GetFileAttributesA: error code {}", ::GetLastError()); #else LogError("GetFileAttributesW: error code {}", ::GetLastError()); @@ -148,7 +148,7 @@ DWORD WindowsGetFileAttributes(const char *path) bool DirectoryExists(const char *path) { -#if defined(_WIN64) || defined(_WIN32) +#ifdef _WIN32 const DWORD attr = WindowsGetFileAttributes(path); return attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0; #elif defined(DVL_HAS_FILESYSTEM) @@ -162,7 +162,7 @@ bool DirectoryExists(const char *path) bool FileExistsAndIsWriteable(const char *path) { -#if defined(_WIN64) || defined(_WIN32) +#ifdef _WIN32 const DWORD attr = WindowsGetFileAttributes(path); return attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_READONLY) == 0; #elif (_POSIX_C_SOURCE >= 200112L || defined(_BSD_SOURCE) || defined(__APPLE__)) && !defined(__ANDROID__) @@ -180,9 +180,9 @@ bool FileExistsAndIsWriteable(const char *path) bool GetFileSize(const char *path, std::uintmax_t *size) { -#if defined(_WIN64) || defined(_WIN32) +#ifdef _WIN32 WIN32_FILE_ATTRIBUTE_DATA attr; -#if defined(NXDK) +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR if (!GetFileAttributesExA(path, GetFileExInfoStandard, &attr)) { return false; } @@ -218,8 +218,8 @@ bool CreateDir(const char *path) return false; } return true; -#elif defined(_WIN64) || defined(_WIN32) -#ifdef NXDK +#elif defined(_WIN32) +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR if (::CreateDirectoryA(path, /*lpSecurityAttributes=*/nullptr) != 0) return true; if (::GetLastError() == ERROR_ALREADY_EXISTS) @@ -278,13 +278,13 @@ void RecursivelyCreateDir(const char *path) bool ResizeFile(const char *path, std::uintmax_t size) { -#if defined(_WIN64) || defined(_WIN32) +#ifdef _WIN32 LARGE_INTEGER lisize; lisize.QuadPart = static_cast(size); if (lisize.QuadPart < 0) { return false; } -#ifdef NXDK +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR HANDLE file = ::CreateFileA(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); #else const auto pathUtf16 = ToWideChar(path); @@ -311,9 +311,9 @@ bool ResizeFile(const char *path, std::uintmax_t size) void RenameFile(const char *from, const char *to) { -#if defined(NXDK) +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR ::MoveFile(from, to); -#elif defined(_WIN64) || defined(_WIN32) +#elif defined(_WIN32) const auto fromUtf16 = ToWideChar(from); const auto toUtf16 = ToWideChar(to); if (fromUtf16 == nullptr || toUtf16 == nullptr) { @@ -331,11 +331,11 @@ void RenameFile(const char *from, const char *to) void CopyFileOverwrite(const char *from, const char *to) { -#if defined(NXDK) +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR if (!::CopyFile(from, to, /*bFailIfExists=*/false)) { LogError("Failed to copy {} to {}", from, to); } -#elif defined(_WIN64) || defined(_WIN32) +#elif defined(_WIN32) const auto fromUtf16 = ToWideChar(from); const auto toUtf16 = ToWideChar(to); if (fromUtf16 == nullptr || toUtf16 == nullptr) { @@ -380,9 +380,9 @@ void CopyFileOverwrite(const char *from, const char *to) void RemoveFile(const char *path) { -#if defined(NXDK) +#ifdef DEVILUTIONX_WINDOWS_NO_WCHAR ::DeleteFileA(path); -#elif defined(_WIN64) || defined(_WIN32) +#elif defined(_WIN32) const auto pathUtf16 = ToWideChar(path); if (pathUtf16 == nullptr) { LogError("UTF-8 -> UTF-16 conversion error code {}", ::GetLastError()); @@ -406,7 +406,7 @@ void RemoveFile(const char *path) FILE *OpenFile(const char *path, const char *mode) { -#if (defined(_WIN64) || defined(_WIN32)) && !defined(NXDK) +#if defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) std::unique_ptr pathUtf16; std::unique_ptr modeUtf16; if ((pathUtf16 = ToWideChar(path)) == nullptr diff --git a/Source/utils/file_util.h b/Source/utils/file_util.h index d3fd3677f..a8a311164 100644 --- a/Source/utils/file_util.h +++ b/Source/utils/file_util.h @@ -42,7 +42,7 @@ void CopyFileOverwrite(const char *from, const char *to); void RemoveFile(const char *path); FILE *OpenFile(const char *path, const char *mode); -#if (defined(_WIN64) || defined(_WIN32)) && !defined(NXDK) +#if defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) std::unique_ptr ToWideChar(std::string_view path); #endif diff --git a/Source/utils/sdl2_to_1_2_backports.cpp b/Source/utils/sdl2_to_1_2_backports.cpp index 7e4879de2..cfe5c0472 100644 --- a/Source/utils/sdl2_to_1_2_backports.cpp +++ b/Source/utils/sdl2_to_1_2_backports.cpp @@ -5,7 +5,7 @@ #include "./console.h" -#if defined(_WIN32) && !defined(NXDK) +#if defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) #define WIN32_LEAN_AND_MEAN #define NOMINMAX 1 #define UNICODE 1 @@ -505,7 +505,7 @@ Sint64 SDL_RWsize(SDL_RWops *context) return end - begin; } -#if defined(_WIN32) && !defined(NXDK) +#if defined(_WIN32) && !defined(DEVILUTIONX_WINDOWS_NO_WCHAR) namespace {