From b9b0fb9974d545aa315736b22e92c677751753f3 Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 13 Dec 2025 22:33:50 -0500 Subject: [PATCH] mmmmmmmmmmmmmmmm --- src/anm2/anm2.cpp | 18 ++++-------------- src/resource/audio.cpp | 12 ++++++++---- src/resource/texture.cpp | 26 ++++++++++++++++++++------ src/util/filesystem_.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/util/filesystem_.h | 17 +++++++++++++++++ 5 files changed, 89 insertions(+), 24 deletions(-) diff --git a/src/anm2/anm2.cpp b/src/anm2/anm2.cpp index f7aefdb..a559ca4 100644 --- a/src/anm2/anm2.cpp +++ b/src/anm2/anm2.cpp @@ -23,12 +23,7 @@ namespace anm2ed::anm2 { XMLDocument document; -#ifdef _WIN32 - FILE* file = _wfopen(path.native().c_str(), L"rb"); -#else - FILE* file = fopen(path.c_str(), "rb"); -#endif - + filesystem::File file(path, "rb"); if (!file) { if (errorString) *errorString = localize.get(ERROR_FILE_NOT_FOUND); @@ -36,7 +31,7 @@ namespace anm2ed::anm2 return; } - if (document.LoadFile(file) != XML_SUCCESS) + if (document.LoadFile(file.get()) != XML_SUCCESS) { if (errorString) *errorString = document.ErrorStr(); isValid = false; @@ -70,19 +65,14 @@ namespace anm2ed::anm2 XMLDocument document; document.InsertFirstChild(to_element(document)); -#ifdef _WIN32 - FILE* file = _wfopen(path.native().c_str(), L"wb"); -#else - FILE* file = fopen(path.c_str(), "wb"); -#endif - + filesystem::File file(path, "wb"); if (!file) { if (errorString) *errorString = localize.get(ERROR_FILE_NOT_FOUND); return false; } - if (document.SaveFile(file) != XML_SUCCESS) + if (document.SaveFile(file.get()) != XML_SUCCESS) { if (errorString) *errorString = document.ErrorStr(); return false; diff --git a/src/resource/audio.cpp b/src/resource/audio.cpp index 260912f..f97814a 100644 --- a/src/resource/audio.cpp +++ b/src/resource/audio.cpp @@ -29,7 +29,8 @@ namespace anm2ed::resource SDL_free(fileData); SDL_IOStream* io = SDL_IOFromConstMem(data.data(), data.size()); - if (!io) data.clear(); + if (!io) + data.clear(); else { internal = MIX_LoadAudio_IO(mixer_get(), io, true, true); @@ -43,7 +44,8 @@ namespace anm2ed::resource data.assign(memory, memory + size); SDL_IOStream* io = SDL_IOFromConstMem(data.data(), data.size()); - if (!io) data.clear(); + if (!io) + data.clear(); else { internal = MIX_LoadAudio_IO(mixer_get(), io, true, true); @@ -112,7 +114,8 @@ namespace anm2ed::resource data = other.data; SDL_IOStream* io = SDL_IOFromConstMem(data.data(), data.size()); - if (!io) data.clear(); + if (!io) + data.clear(); else { internal = MIX_LoadAudio_IO(mixer_get(), io, true, true); @@ -149,7 +152,8 @@ namespace anm2ed::resource { data = other.data; SDL_IOStream* io = SDL_IOFromConstMem(data.data(), data.size()); - if (!io) data.clear(); + if (!io) + data.clear(); else { internal = MIX_LoadAudio_IO(mixer_get(), io, true, true); diff --git a/src/resource/texture.cpp b/src/resource/texture.cpp index f667b41..9a99144 100644 --- a/src/resource/texture.cpp +++ b/src/resource/texture.cpp @@ -1,5 +1,6 @@ #include "texture.h" +#include #include #include #include @@ -118,18 +119,31 @@ namespace anm2ed::resource Texture::Texture(const std::filesystem::path& pngPath) { - auto pngPathUtf8 = filesystem::path_to_utf8(pngPath); - if (const uint8_t* data = stbi_load(pngPathUtf8.c_str(), &size.x, &size.y, nullptr, CHANNELS); data) + filesystem::File file(pngPath, "rb"); + if (auto handle = file.get()) { - upload(data); - stbi_image_free((void*)data); + if (const uint8_t* data = stbi_load_from_file(handle, &size.x, &size.y, nullptr, CHANNELS); data) + { + upload(data); + stbi_image_free((void*)data); + } } } bool Texture::write_png(const std::filesystem::path& path) { - auto pathUtf8 = filesystem::path_to_utf8(path); - return stbi_write_png(pathUtf8.c_str(), size.x, size.y, CHANNELS, this->pixels.data(), size.x * CHANNELS); + if (pixels.empty()) return false; + + filesystem::File file(path, "wb"); + if (auto handle = file.get()) + { + auto write_func = [](void* context, void* data, int size) { fwrite(data, 1, size, static_cast(context)); }; + + auto result = + stbi_write_png_to_func(write_func, handle, size.x, size.y, CHANNELS, this->pixels.data(), size.x * CHANNELS); + return result != 0; + } + return false; } vec4 Texture::pixel_read(vec2 position) const diff --git a/src/util/filesystem_.cpp b/src/util/filesystem_.cpp index 3b5a8e8..975ef00 100644 --- a/src/util/filesystem_.cpp +++ b/src/util/filesystem_.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include @@ -58,6 +60,20 @@ namespace anm2ed::util::filesystem return std::filesystem::path(std::u8string(utf8.begin(), utf8.end())); } + FILE* open(const std::filesystem::path& path, const char* mode) + { +#ifdef _WIN32 + std::wstring wideMode{}; + if (mode) + wideMode.assign(mode, mode + std::strlen(mode)); + else + wideMode = L"rb"; + return _wfopen(path.native().c_str(), wideMode.c_str()); +#else + return std::fopen(path.string().c_str(), mode); +#endif + } + bool path_is_exist(const std::filesystem::path& path) { std::error_code errorCode; @@ -97,4 +113,28 @@ namespace anm2ed::util::filesystem } WorkingDirectory::~WorkingDirectory() { std::filesystem::current_path(previous); } + + File::File(const std::filesystem::path& path, const char* mode) { open(path, mode); } + + File::~File() { close(); } + + bool File::open(const std::filesystem::path& path, const char* mode) + { + close(); + handle = filesystem::open(path, mode); + return handle != nullptr; + } + + void File::close() + { + if (handle) + { + std::fclose(handle); + handle = nullptr; + } + } + + FILE* File::get() const { return handle; } + + File::operator bool() const { return handle != nullptr; } } diff --git a/src/util/filesystem_.h b/src/util/filesystem_.h index 0fd6808..9f72fea 100644 --- a/src/util/filesystem_.h +++ b/src/util/filesystem_.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -18,6 +19,22 @@ namespace anm2ed::util::filesystem FILE* open(const std::filesystem::path&, const char*); + class File + { + public: + File() = default; + File(const std::filesystem::path&, const char* mode); + ~File(); + + bool open(const std::filesystem::path&, const char* mode); + void close(); + FILE* get() const; + explicit operator bool() const; + + private: + FILE* handle{}; + }; + class WorkingDirectory { public: