From f8101011a0383c8fe86bb126d440eeeca9c9afb5 Mon Sep 17 00:00:00 2001 From: clasher113 Date: Tue, 20 Oct 2026 14:53:27 +0300 Subject: [PATCH] fix cyrillic in file path (Windows) --- src/coders/imageio.cpp | 4 ++-- src/coders/ogg.cpp | 42 ++++++++++++++++++++++++++++++++++++++---- src/coders/png.cpp | 18 ++++++++++++------ src/coders/png.hpp | 5 ++++- src/debug/Logger.cpp | 4 ++-- src/debug/Logger.hpp | 3 ++- src/main.cpp | 2 +- 7 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/coders/imageio.cpp b/src/coders/imageio.cpp index b63c65fc0..f8aab2d3c 100644 --- a/src/coders/imageio.cpp +++ b/src/coders/imageio.cpp @@ -12,7 +12,7 @@ using namespace imageio; using image_reader = std::function(const ubyte*, size_t)>; -using image_writer = std::function; +using image_writer = std::function; static std::unordered_map readers { {ImageFileFormat::PNG, png::load_image}, @@ -75,7 +75,7 @@ void imageio::write(const io::path& file, const ImageData* image) { "file format is not supported (write): " + file.string() ); } - return found->second(io::resolve(file).u8string(), image); + return found->second(file, image); } util::Buffer imageio::encode( diff --git a/src/coders/ogg.cpp b/src/coders/ogg.cpp index c5c27f495..6df31bc63 100644 --- a/src/coders/ogg.cpp +++ b/src/coders/ogg.cpp @@ -47,8 +47,25 @@ std::unique_ptr ogg::load_pcm( const io::path& file, bool headerOnly ) { OggVorbis_File vf; - int code; - if ((code = ov_fopen(io::resolve(file).u8string().c_str(), &vf))) { + FILE* fileHandle = nullptr; +#ifdef _WIN32 + if (errno_t err = + _wfopen_s( + &fileHandle, io::resolve(file).wstring().c_str(), L"rb" + ) != 0) { +#else + if ((fileHandle = fopen(io::resolve(file).u8string().c_str(), "rb")) == + nullptr) { +#endif + throw std::runtime_error( + "could not open file " + file.string() + " for reading" + ); + } + + if (int code = ov_open(fileHandle, &vf, nullptr, 0)) { + if (fileHandle) { + fclose(fileHandle); + } throw std::runtime_error("vorbis: " + vorbis_error_message(code)); } std::vector data; @@ -169,8 +186,25 @@ public: std::unique_ptr ogg::create_stream(const io::path& file) { OggVorbis_File vf; - int code; - if ((code = ov_fopen(io::resolve(file).u8string().c_str(), &vf))) { + FILE* fileHandle = nullptr; +#ifdef _WIN32 + if (errno_t err = + _wfopen_s( + &fileHandle, io::resolve(file).wstring().c_str(), L"rb" + ) != 0) { +#else + if ((fileHandle = fopen(io::resolve(file).u8string().c_str(), "rb")) == + nullptr) { +#endif + throw std::runtime_error( + "could not open file " + file.string() + " for reading" + ); + } + + if (int code = ov_open(fileHandle, &vf, nullptr, 0)) { + if (fileHandle) { + fclose(fileHandle); + } throw std::runtime_error("vorbis: " + vorbis_error_message(code)); } return std::make_unique(vf); diff --git a/src/coders/png.cpp b/src/coders/png.cpp index 18cc0a474..17973c133 100644 --- a/src/coders/png.cpp +++ b/src/coders/png.cpp @@ -67,14 +67,20 @@ static util::Buffer write_to_memory(uint width, uint height, const ubyte* // returns 0 if all-right, 1 otherwise static int png_write( - const char* filename, uint width, uint height, const ubyte* data, bool alpha + const io::path& file, uint width, uint height, const ubyte* data, bool alpha ) { uint pixsize = alpha ? 4 : 3; // Open file for writing (binary mode) - FILE* fp = fopen(filename, "wb"); - if (fp == nullptr) { - logger.error() << "could not open file " << filename << " for writing"; + FILE* fp = nullptr; +#ifdef _WIN32 + if (errno_t err = + _wfopen_s(&fp, io::resolve(file).wstring().c_str(), L"wb") != 0) { +#else + if ((fp = fopen(io::resolve(file).u8string().c_str(), "wb")) == nullptr) { +#endif + logger.error() << "could not open file " << file.string() + << " for writing"; return 1; } @@ -290,9 +296,9 @@ std::unique_ptr png::load_texture(const std::string& filename) { } } -void png::write_image(const std::string& filename, const ImageData* image) { +void png::write_image(const io::path& file, const ImageData* image) { png_write( - filename.c_str(), + file, image->getWidth(), image->getHeight(), (const ubyte*)image->getData(), diff --git a/src/coders/png.hpp b/src/coders/png.hpp index 468efc4b3..fe315a632 100644 --- a/src/coders/png.hpp +++ b/src/coders/png.hpp @@ -8,10 +8,13 @@ class Texture; class ImageData; +namespace io { + class path; +} namespace png { std::unique_ptr load_image(const ubyte* bytes, size_t size); - void write_image(const std::string& filename, const ImageData* image); + void write_image(const io::path& file, const ImageData* image); util::Buffer encode_image(const ImageData& image); std::unique_ptr load_texture(const ubyte* bytes, size_t size); std::unique_ptr load_texture(const std::string& filename); diff --git a/src/debug/Logger.cpp b/src/debug/Logger.cpp index 12d8975a7..fc6fac301 100644 --- a/src/debug/Logger.cpp +++ b/src/debug/Logger.cpp @@ -68,8 +68,8 @@ static void write( } } -void Logger::init(const std::string& filename) { - file.open(filename); +void Logger::init(const std::filesystem::path& path) { + file.open(path); time_t tm = std::time(nullptr); std::stringstream ss; diff --git a/src/debug/Logger.hpp b/src/debug/Logger.hpp index 598dc4efb..2279ff298 100644 --- a/src/debug/Logger.hpp +++ b/src/debug/Logger.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace debug { enum class LogLevel { print, debug, info, warning, error }; @@ -27,7 +28,7 @@ namespace debug { class Logger { std::string name; public: - static void init(const std::string& filename); + static void init(const std::filesystem::path& path); static void flush(); Logger(const std::string& name) : name(name) { diff --git a/src/main.cpp b/src/main.cpp index 9d8d8e551..4e739acde 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,7 +44,7 @@ int main(int argc, char** argv) { ? ".sub" + std::to_string(coreParameters.subProcessDepth) : "") + ".log"s; } - debug::Logger::init(logFile.u8string()); + debug::Logger::init(logFile); platform::configure_encoding(); auto& engine = Engine::getInstance();