From 2a39cc644ec39762ba8f40f13aacfa83a6e3bddd Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 28 Sep 2025 01:42:46 -0400 Subject: [PATCH] Enhance Abseil integration and improve Windows font loading functionality - Added additional Abseil flags modules to the CMake configuration for better support. - Refactored Windows-specific font loading code to avoid namespace conflicts by using the `::` prefix for Windows API calls. - Improved error handling and ensured proper memory management when retrieving font paths from the Windows registry. --- cmake/absl.cmake | 18 ++++++---- src/app/core/platform/font_loader.cc | 54 ++++++++++++++-------------- src/cli/cli_main.cc | 4 +++ src/cli/z3ed.cc | 4 +++ 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/cmake/absl.cmake b/cmake/absl.cmake index 6d84ce35..e2b2dfa9 100644 --- a/cmake/absl.cmake +++ b/cmake/absl.cmake @@ -28,6 +28,14 @@ set( absl::strings absl::str_format absl::flags + absl::flags_parse + absl::flags_usage + absl::flags_commandlineflag + absl::flags_marshalling + absl::flags_private_handle_accessor + absl::flags_program_name + absl::flags_config + absl::flags_reflection absl::status absl::statusor absl::examine_stack @@ -42,15 +50,13 @@ set( absl::synchronization absl::time absl::symbolize - absl::flags_commandlineflag - absl::flags_marshalling - absl::flags_private_handle_accessor - absl::flags_program_name - absl::flags_config - absl::flags_reflection absl::container_memory absl::memory absl::utility + absl::strings_internal + absl::str_format_internal + absl::flags_internal + absl::container_internal ) # Add int128 only on non-Windows platforms to avoid C++23 deprecation issues diff --git a/src/app/core/platform/font_loader.cc b/src/app/core/platform/font_loader.cc index 53b01b79..cdb6664b 100644 --- a/src/app/core/platform/font_loader.cc +++ b/src/app/core/platform/font_loader.cc @@ -4,9 +4,6 @@ #include #include #include -#include -#include -# #include "absl/status/status.h" @@ -134,8 +131,11 @@ absl::Status ReloadPackageFont(const FontConfig& config) { } #ifdef _WIN32 +// Include Windows headers first to avoid namespace conflicts #include #include +#include +#include #include #include @@ -155,11 +155,11 @@ namespace { // Helper function to get Windows fonts directory std::string GetWindowsFontsDirectory() { wchar_t* fontsPath = nullptr; - HRESULT hr = SHGetKnownFolderPath(FOLDERID_Fonts, 0, NULL, &fontsPath); + ::HRESULT hr = ::SHGetKnownFolderPath(FOLDERID_Fonts, 0, NULL, &fontsPath); - if (SUCCEEDED(hr) && fontsPath) { + if (::SUCCEEDED(hr) && fontsPath) { std::string result = WideToUtf8(fontsPath) + "\\"; - CoTaskMemFree(fontsPath); + ::CoTaskMemFree(fontsPath); return result; } @@ -177,7 +177,7 @@ namespace { // Check if file exists and is accessible bool FontFileExists(const std::string& path) { - return GetFileAttributesA(path.c_str()) != INVALID_FILE_ATTRIBUTES; + return ::GetFileAttributesA(path.c_str()) != INVALID_FILE_ATTRIBUTES; } } @@ -234,20 +234,20 @@ void LoadSystemFonts() { } // Try to load additional fonts from registry (safer approach) - HKEY hKey = nullptr; - LONG result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, - "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", - 0, KEY_READ, &hKey); + ::HKEY hKey = nullptr; + ::LONG result = ::RegOpenKeyExA(HKEY_LOCAL_MACHINE, + "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", + 0, KEY_READ, &hKey); if (result == ERROR_SUCCESS && hKey) { - DWORD valueCount = 0; - DWORD maxValueNameSize = 0; - DWORD maxValueDataSize = 0; + ::DWORD valueCount = 0; + ::DWORD maxValueNameSize = 0; + ::DWORD maxValueDataSize = 0; // Get registry info - result = RegQueryInfoKeyA(hKey, nullptr, nullptr, nullptr, nullptr, nullptr, - nullptr, &valueCount, &maxValueNameSize, - &maxValueDataSize, nullptr, nullptr); + result = ::RegQueryInfoKeyA(hKey, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, &valueCount, &maxValueNameSize, + &maxValueDataSize, nullptr, nullptr); if (result == ERROR_SUCCESS && valueCount > 0) { // Allocate buffers with proper size limits @@ -257,21 +257,21 @@ void LoadSystemFonts() { if (maxDataSize > 4096) maxDataSize = 4096; std::vector valueName(maxNameSize); - std::vector valueData(maxDataSize); + std::vector<::BYTE> valueData(maxDataSize); // Enumerate font entries (limit to prevent excessive loading) - DWORD maxFontsToLoad = valueCount; + ::DWORD maxFontsToLoad = valueCount; if (maxFontsToLoad > 50) maxFontsToLoad = 50; - for (DWORD i = 0; i < maxFontsToLoad; i++) { - DWORD valueNameSize = static_cast(maxNameSize); - DWORD valueDataSize = static_cast(maxDataSize); - DWORD valueType = 0; + for (::DWORD i = 0; i < maxFontsToLoad; i++) { + ::DWORD valueNameSize = static_cast<::DWORD>(maxNameSize); + ::DWORD valueDataSize = static_cast<::DWORD>(maxDataSize); + ::DWORD valueType = 0; - result = RegEnumValueA(hKey, i, valueName.data(), &valueNameSize, - nullptr, &valueType, valueData.data(), &valueDataSize); + result = ::RegEnumValueA(hKey, i, valueName.data(), &valueNameSize, + nullptr, &valueType, valueData.data(), &valueDataSize); - if (result == ERROR_SUCCESS && valueType == REG_SZ && valueDataSize > 0) { + if (result == ERROR_SUCCESS && valueType == ::REG_SZ && valueDataSize > 0) { // Ensure null termination valueName[valueNameSize] = '\0'; valueData[valueDataSize] = '\0'; @@ -304,7 +304,7 @@ void LoadSystemFonts() { } } - RegCloseKey(hKey); + ::RegCloseKey(hKey); } } diff --git a/src/cli/cli_main.cc b/src/cli/cli_main.cc index c4c5eb3d..63e8fe77 100644 --- a/src/cli/cli_main.cc +++ b/src/cli/cli_main.cc @@ -434,7 +434,11 @@ class ModernCLI { } // namespace cli } // namespace yaze +#ifdef _WIN32 +extern "C" int SDL_main(int argc, char* argv[]) { +#else int main(int argc, char* argv[]) { +#endif absl::SetProgramUsageMessage( "z3ed - Yet Another Zelda3 Editor CLI Tool\n" "\n" diff --git a/src/cli/z3ed.cc b/src/cli/z3ed.cc index e7a4f534..158d82eb 100644 --- a/src/cli/z3ed.cc +++ b/src/cli/z3ed.cc @@ -24,7 +24,11 @@ DEFINE_FLAG(std::string, length, "", "The length of the data to read."); DEFINE_FLAG(std::string, file_size, "", "The size of the file to expand to."); DEFINE_FLAG(std::string, dest_rom, "", "The destination ROM file."); +#ifdef _WIN32 +extern "C" int SDL_main(int argc, char *argv[]) { +#else int main(int argc, char *argv[]) { +#endif yaze::util::FlagParser flag_parser(yaze::util::global_flag_registry()); RETURN_IF_EXCEPTION(flag_parser.Parse(argc, argv)); yaze::cli::ShowMain();