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.
This commit is contained in:
scawful
2025-09-28 01:42:46 -04:00
parent 5aa9fde4a4
commit 2a39cc644e
4 changed files with 47 additions and 33 deletions

View File

@@ -28,6 +28,14 @@ set(
absl::strings absl::strings
absl::str_format absl::str_format
absl::flags 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::status
absl::statusor absl::statusor
absl::examine_stack absl::examine_stack
@@ -42,15 +50,13 @@ set(
absl::synchronization absl::synchronization
absl::time absl::time
absl::symbolize 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::container_memory
absl::memory absl::memory
absl::utility 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 # Add int128 only on non-Windows platforms to avoid C++23 deprecation issues

View File

@@ -4,9 +4,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
#include <algorithm>
#include <cmath>
#
#include "absl/status/status.h" #include "absl/status/status.h"
@@ -134,8 +131,11 @@ absl::Status ReloadPackageFont(const FontConfig& config) {
} }
#ifdef _WIN32 #ifdef _WIN32
// Include Windows headers first to avoid namespace conflicts
#include <Windows.h> #include <Windows.h>
#include <ShlObj.h> #include <ShlObj.h>
#include <combaseapi.h>
#include <shlobj_core.h>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
@@ -155,11 +155,11 @@ namespace {
// Helper function to get Windows fonts directory // Helper function to get Windows fonts directory
std::string GetWindowsFontsDirectory() { std::string GetWindowsFontsDirectory() {
wchar_t* fontsPath = nullptr; 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) + "\\"; std::string result = WideToUtf8(fontsPath) + "\\";
CoTaskMemFree(fontsPath); ::CoTaskMemFree(fontsPath);
return result; return result;
} }
@@ -177,7 +177,7 @@ namespace {
// Check if file exists and is accessible // Check if file exists and is accessible
bool FontFileExists(const std::string& path) { 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) // Try to load additional fonts from registry (safer approach)
HKEY hKey = nullptr; ::HKEY hKey = nullptr;
LONG result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ::LONG result = ::RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts",
0, KEY_READ, &hKey); 0, KEY_READ, &hKey);
if (result == ERROR_SUCCESS && hKey) { if (result == ERROR_SUCCESS && hKey) {
DWORD valueCount = 0; ::DWORD valueCount = 0;
DWORD maxValueNameSize = 0; ::DWORD maxValueNameSize = 0;
DWORD maxValueDataSize = 0; ::DWORD maxValueDataSize = 0;
// Get registry info // Get registry info
result = RegQueryInfoKeyA(hKey, nullptr, nullptr, nullptr, nullptr, nullptr, result = ::RegQueryInfoKeyA(hKey, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, &valueCount, &maxValueNameSize, nullptr, &valueCount, &maxValueNameSize,
&maxValueDataSize, nullptr, nullptr); &maxValueDataSize, nullptr, nullptr);
if (result == ERROR_SUCCESS && valueCount > 0) { if (result == ERROR_SUCCESS && valueCount > 0) {
// Allocate buffers with proper size limits // Allocate buffers with proper size limits
@@ -257,21 +257,21 @@ void LoadSystemFonts() {
if (maxDataSize > 4096) maxDataSize = 4096; if (maxDataSize > 4096) maxDataSize = 4096;
std::vector<char> valueName(maxNameSize); std::vector<char> valueName(maxNameSize);
std::vector<BYTE> valueData(maxDataSize); std::vector<::BYTE> valueData(maxDataSize);
// Enumerate font entries (limit to prevent excessive loading) // Enumerate font entries (limit to prevent excessive loading)
DWORD maxFontsToLoad = valueCount; ::DWORD maxFontsToLoad = valueCount;
if (maxFontsToLoad > 50) maxFontsToLoad = 50; if (maxFontsToLoad > 50) maxFontsToLoad = 50;
for (DWORD i = 0; i < maxFontsToLoad; i++) { for (::DWORD i = 0; i < maxFontsToLoad; i++) {
DWORD valueNameSize = static_cast<DWORD>(maxNameSize); ::DWORD valueNameSize = static_cast<::DWORD>(maxNameSize);
DWORD valueDataSize = static_cast<DWORD>(maxDataSize); ::DWORD valueDataSize = static_cast<::DWORD>(maxDataSize);
DWORD valueType = 0; ::DWORD valueType = 0;
result = RegEnumValueA(hKey, i, valueName.data(), &valueNameSize, result = ::RegEnumValueA(hKey, i, valueName.data(), &valueNameSize,
nullptr, &valueType, valueData.data(), &valueDataSize); 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 // Ensure null termination
valueName[valueNameSize] = '\0'; valueName[valueNameSize] = '\0';
valueData[valueDataSize] = '\0'; valueData[valueDataSize] = '\0';
@@ -304,7 +304,7 @@ void LoadSystemFonts() {
} }
} }
RegCloseKey(hKey); ::RegCloseKey(hKey);
} }
} }

View File

@@ -434,7 +434,11 @@ class ModernCLI {
} // namespace cli } // namespace cli
} // namespace yaze } // namespace yaze
#ifdef _WIN32
extern "C" int SDL_main(int argc, char* argv[]) {
#else
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
#endif
absl::SetProgramUsageMessage( absl::SetProgramUsageMessage(
"z3ed - Yet Another Zelda3 Editor CLI Tool\n" "z3ed - Yet Another Zelda3 Editor CLI Tool\n"
"\n" "\n"

View File

@@ -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, file_size, "", "The size of the file to expand to.");
DEFINE_FLAG(std::string, dest_rom, "", "The destination ROM file."); 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[]) { int main(int argc, char *argv[]) {
#endif
yaze::util::FlagParser flag_parser(yaze::util::global_flag_registry()); yaze::util::FlagParser flag_parser(yaze::util::global_flag_registry());
RETURN_IF_EXCEPTION(flag_parser.Parse(argc, argv)); RETURN_IF_EXCEPTION(flag_parser.Parse(argc, argv));
yaze::cli::ShowMain(); yaze::cli::ShowMain();