mmmmmmmmmmmmmmmm

This commit is contained in:
2025-12-13 22:33:50 -05:00
parent c23179c134
commit b9b0fb9974
5 changed files with 89 additions and 24 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -1,5 +1,6 @@
#include "texture.h"
#include <cstdio>
#include <filesystem>
#include <lunasvg.h>
#include <memory>
@@ -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())
{
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<FILE*>(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

View File

@@ -3,6 +3,8 @@
#include <SDL3/SDL_filesystem.h>
#include <SDL3/SDL_stdinc.h>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cwctype>
#include <filesystem>
#include <type_traits>
@@ -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; }
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include <cstdio>
#include <filesystem>
#include <string>
@@ -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: