Fix handling of Unicode paths on Windows (#767)

I love C++
This commit is contained in:
Pieter-Jan Briers
2026-05-10 04:24:50 +02:00
committed by GitHub
parent 3b1118229b
commit c66cccf660
6 changed files with 73 additions and 19 deletions
+30 -2
View File
@@ -7,6 +7,10 @@
// C++ (no error codes) have a file system API functional enough for me to use.
// Here you go, this one's inspired by C#. I only wrote the functions I need.
namespace std::filesystem {
class path;
}
namespace dusk::io {
/**
@@ -15,7 +19,7 @@ namespace dusk::io {
* Methods on this class throw appropriate C++ exceptions when an error occurs.
*/
class FileStream {
void* file;
FILE* file;
public:
FileStream() noexcept;
@@ -23,7 +27,7 @@ public:
/**
* \brief Take ownership of a FILE* handle.
*/
explicit FileStream(void* file);
explicit FileStream(FILE* file);
FileStream(const FileStream& other) = delete;
FileStream(FileStream&& other) noexcept;
@@ -34,6 +38,11 @@ public:
*/
static FileStream OpenRead(const char* utf8Path);
/**
* \brief Open a file for reading at the given path.
*/
static FileStream OpenRead(const std::filesystem::path& path);
/**
* \brief Create a file for writing.
*
@@ -41,16 +50,33 @@ public:
*/
static FileStream Create(const char* utf8Path);
/**
* \brief Create a file for writing.
*
* If there is an existing file, its contents are demolished.
*/
static FileStream Create(const std::filesystem::path& path);
/**
* \brief Read the byte contents of a file directly into a vector.
*/
static std::vector<u8> ReadAllBytes(const char* utf8Path);
/**
* \brief Read the byte contents of a file directly into a vector.
*/
static std::vector<u8> ReadAllBytes(const std::filesystem::path& path);
/**
* \brief Read the byte contents of a file directly into a vector.
*/
static void WriteAllText(const char* utf8Path, std::string_view text);
/**
* \brief Read the byte contents of a file directly into a vector.
*/
static void WriteAllText(const std::filesystem::path& path, std::string_view text);
/**
* \brief Read the remaining contents of the file directly into a vector.
*/
@@ -67,6 +93,8 @@ public:
* Write data to the file.
*/
void Write(const char* data, size_t dataLen);
FILE* ToInner();
};
}
+2 -2
View File
@@ -1030,7 +1030,7 @@ void AchievementSystem::load() {
return;
}
try {
auto data = io::FileStream::ReadAllBytes(filePath.string().c_str());
auto data = io::FileStream::ReadAllBytes(filePath);
auto j = json::parse(data);
if (!j.is_object()) {
return;
@@ -1067,7 +1067,7 @@ void AchievementSystem::save() {
}
try {
io::FileStream::WriteAllText(
(dusk::ConfigPath / ACHIEVEMENTS_FILENAME).string().c_str(),
dusk::ConfigPath / ACHIEVEMENTS_FILENAME,
j.dump(2)
);
} catch (const std::exception&) {}
+7 -5
View File
@@ -23,8 +23,8 @@ aurora::Module DuskConfigLog("dusk::config");
static absl::flat_hash_map<std::string_view, ConfigVarBase*> RegisteredConfigVars;
static bool RegistrationDone = false;
static std::string GetConfigJsonPath() {
return (dusk::ConfigPath / ConfigFileName).string();
static std::u8string GetConfigJsonPath() {
return (dusk::ConfigPath / ConfigFileName).u8string();
}
ConfigVarBase::ConfigVarBase(const char* name, const ConfigImplBase* impl) : name(name), registered(false), layer(ConfigVarLayer::Default), impl(impl) {
@@ -189,7 +189,7 @@ void dusk::config::LoadFromUserPreferences() {
if (configJsonPath.empty()) {
return;
}
LoadFromFileName(configJsonPath.c_str());
LoadFromFileName(reinterpret_cast<const char*>(configJsonPath.c_str()));
}
static void LoadFromPath(const char* path) {
@@ -241,7 +241,9 @@ void dusk::config::Save() {
return;
}
DuskConfigLog.info("Saving config to '{}'", configJsonPath);
DuskConfigLog.info(
"Saving config to '{}'",
reinterpret_cast<const char*>(configJsonPath.c_str()));
json j;
@@ -251,7 +253,7 @@ void dusk::config::Save() {
}
}
io::FileStream::WriteAllText(configJsonPath.c_str(), j.dump(4));
io::FileStream::WriteAllText(reinterpret_cast<const char*>(configJsonPath.c_str()), j.dump(4));
}
ConfigVarBase* dusk::config::GetConfigVar(std::string_view name) {
+25 -3
View File
@@ -58,7 +58,7 @@ static FILE* OpenCore(const char* path, const MODE_TYPE* mode) {
FileStream::FileStream() noexcept : file(nullptr) {
}
FileStream::FileStream(void* file) : file(file) {
FileStream::FileStream(FILE* file) : file(file) {
if (!file) {
CRASH("Invalid file handle");
}
@@ -78,10 +78,18 @@ FileStream FileStream::OpenRead(const char* utf8Path) {
return FileStream(OpenCore(utf8Path, MODE("rb")));
}
FileStream FileStream::OpenRead(const std::filesystem::path& path) {
return FileStream(OpenCore(path, MODE("rb")));
}
FileStream FileStream::Create(const char* utf8Path) {
return FileStream(OpenCore(utf8Path, MODE("wb")));
}
FileStream FileStream::Create(const std::filesystem::path& path) {
return FileStream(OpenCore(path, MODE("wb")));
}
std::vector<u8> FileStream::ReadFull() {
const auto fileHandle = ThrowIfNotOpen(*this);
@@ -128,7 +136,11 @@ std::vector<u8> FileStream::ReadFull() {
}
std::vector<u8> FileStream::ReadAllBytes(const char* utf8Path) {
auto handle = OpenRead(utf8Path);
return ReadAllBytes(reinterpret_cast<const char8_t*>(utf8Path));
}
std::vector<u8> FileStream::ReadAllBytes(const std::filesystem::path& path) {
auto handle = OpenRead(path);
return std::move(handle.ReadFull());
}
@@ -142,6 +154,16 @@ void FileStream::Write(const char* data, size_t dataLen) {
}
void FileStream::WriteAllText(const char* utf8Path, const std::string_view text) {
auto handle = Create(utf8Path);
WriteAllText(reinterpret_cast<const char8_t*>(utf8Path), text);
}
void FileStream::WriteAllText(const std::filesystem::path& path, const std::string_view text) {
auto handle = Create(path);
handle.Write(text.data(), text.size());
}
FILE* FileStream::ToInner() {
auto handle = file;
file = nullptr;
return handle;
}
+6 -4
View File
@@ -8,6 +8,7 @@
#include <mutex>
#include <string>
#include "dusk/io.hpp"
#include "tracy/Tracy.hpp"
#if TARGET_ANDROID
@@ -40,7 +41,7 @@ std::atomic g_logStateAlive(true);
struct LogState {
std::mutex mutex;
FILE* file = nullptr;
std::string filePath;
std::u8string filePath;
~LogState() {
CloseFile();
@@ -212,14 +213,14 @@ void dusk::InitializeFileLogging(const std::filesystem::path& configDir, AuroraL
}
const std::filesystem::path logPath = logsDir / MakeTimestampedLogName();
g_logState.file = std::fopen(logPath.string().c_str(), "wb");
g_logState.file = io::FileStream::Create(logPath).ToInner();
if (g_logState.file == nullptr) {
std::fprintf(stderr, "[WARNING | dusk] Failed to open log file '%s'\n",
logPath.string().c_str());
return;
}
g_logState.filePath = logPath.string();
g_logState.filePath = logPath.u8string();
aurora::g_config.logCallback = &aurora_log_callback;
aurora::g_config.logLevel = logLevel;
WriteLogLine(g_logState.file, "INFO", "dusk", "File logging initialized", 24);
@@ -237,5 +238,6 @@ const char* dusk::GetLogFilePath() {
return nullptr;
}
std::lock_guard lock(g_logState.mutex);
return g_logState.filePath.empty() ? nullptr : g_logState.filePath.c_str();
return reinterpret_cast<const char*>(
g_logState.filePath.empty() ? nullptr : g_logState.filePath.c_str());
}
+3 -3
View File
@@ -398,7 +398,7 @@ static std::filesystem::path CalculateConfigPath() {
DuskLog.fatal("Unable to get PrefPath: {}", SDL_GetError());
}
return result;
return reinterpret_cast<const char8_t*>(result);
}
static void EnsureInitialPipelineCache(const std::filesystem::path& configDir) {
@@ -548,10 +548,10 @@ int game_main(int argc, char* argv[]) {
//PADSetDefaultMapping(&defaultPadMapping, PAD_TYPE_STANDARD);
{
const auto configPathString = dusk::ConfigPath.string();
const auto configPathString = dusk::ConfigPath.u8string();
AuroraConfig config{};
config.appName = dusk::AppName;
config.configPath = configPathString.c_str();
config.configPath = reinterpret_cast<const char*>(configPathString.c_str());
config.vsync = dusk::getSettings().video.enableVsync;
config.startFullscreen = dusk::getSettings().video.enableFullscreen;
config.windowPosX = -1;