diff --git a/include/dusk/io.hpp b/include/dusk/io.hpp index fb71a77d68..57fda3feac 100644 --- a/include/dusk/io.hpp +++ b/include/dusk/io.hpp @@ -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 ReadAllBytes(const char* utf8Path); + /** + * \brief Read the byte contents of a file directly into a vector. + */ + static std::vector 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(); }; } diff --git a/src/dusk/achievements.cpp b/src/dusk/achievements.cpp index cac6f776b6..28e66c891e 100644 --- a/src/dusk/achievements.cpp +++ b/src/dusk/achievements.cpp @@ -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&) {} diff --git a/src/dusk/config.cpp b/src/dusk/config.cpp index 940797c9ce..8225536d34 100644 --- a/src/dusk/config.cpp +++ b/src/dusk/config.cpp @@ -23,8 +23,8 @@ aurora::Module DuskConfigLog("dusk::config"); static absl::flat_hash_map 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(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(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(configJsonPath.c_str()), j.dump(4)); } ConfigVarBase* dusk::config::GetConfigVar(std::string_view name) { diff --git a/src/dusk/io.cpp b/src/dusk/io.cpp index 50b5cab5bd..dca8a25e82 100644 --- a/src/dusk/io.cpp +++ b/src/dusk/io.cpp @@ -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 FileStream::ReadFull() { const auto fileHandle = ThrowIfNotOpen(*this); @@ -128,7 +136,11 @@ std::vector FileStream::ReadFull() { } std::vector FileStream::ReadAllBytes(const char* utf8Path) { - auto handle = OpenRead(utf8Path); + return ReadAllBytes(reinterpret_cast(utf8Path)); +} + +std::vector 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(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; +} \ No newline at end of file diff --git a/src/dusk/logging.cpp b/src/dusk/logging.cpp index 8b96fcffd5..67723af4df 100644 --- a/src/dusk/logging.cpp +++ b/src/dusk/logging.cpp @@ -8,6 +8,7 @@ #include #include +#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( + g_logState.filePath.empty() ? nullptr : g_logState.filePath.c_str()); } diff --git a/src/m_Do/m_Do_main.cpp b/src/m_Do/m_Do_main.cpp index 041c219fe9..55481cfeb5 100644 --- a/src/m_Do/m_Do_main.cpp +++ b/src/m_Do/m_Do_main.cpp @@ -398,7 +398,7 @@ static std::filesystem::path CalculateConfigPath() { DuskLog.fatal("Unable to get PrefPath: {}", SDL_GetError()); } - return result; + return reinterpret_cast(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(configPathString.c_str()); config.vsync = dusk::getSettings().video.enableVsync; config.startFullscreen = dusk::getSettings().video.enableFullscreen; config.windowPosX = -1;