Platform UserInterface PackInitialize/PackSingletons (40250 bootstrap, on-disk dbname resolution), EterBase/FileLoader.cpp mirror, mtpack40250 backend behind asset_io's pack:// scheme and a Metin2Pack node. port_eterpack_test now registers through PackInitialize. Nothing reads pack:// at runtime yet; port-map stays TODO. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
73 lines
1.6 KiB
C++
73 lines
1.6 KiB
C++
#include "asset_io.h"
|
|
|
|
#include <godot_cpp/classes/file_access.hpp>
|
|
|
|
#include "platform/PackBackend.h"
|
|
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace godot;
|
|
|
|
namespace mtgodot {
|
|
|
|
namespace {
|
|
bool pack_name(const String &path, std::string &name) {
|
|
if (!path.begins_with(PACK_SCHEME)) {
|
|
return false;
|
|
}
|
|
name = path.substr(String(PACK_SCHEME).length()).utf8().get_data();
|
|
return true;
|
|
}
|
|
} // namespace
|
|
|
|
PackedByteArray read_file(const String &path) {
|
|
if (path.is_empty()) {
|
|
return PackedByteArray();
|
|
}
|
|
std::string name;
|
|
if (pack_name(path, name)) {
|
|
std::vector<uint8_t> bytes;
|
|
PackedByteArray out;
|
|
if (mtpack40250::read(name, bytes)) {
|
|
out.resize((int64_t)bytes.size());
|
|
if (!bytes.empty()) {
|
|
std::memcpy(out.ptrw(), bytes.data(), bytes.size());
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
// FileAccess::get_file_as_bytes handles res:// / user:// / absolute OS paths.
|
|
return FileAccess::get_file_as_bytes(path);
|
|
}
|
|
|
|
bool file_exists(const String &path) {
|
|
std::string name;
|
|
if (pack_name(path, name)) {
|
|
return mtpack40250::exists(name);
|
|
}
|
|
return !path.is_empty() && FileAccess::file_exists(path);
|
|
}
|
|
|
|
Image dds_from_file(const String &path) {
|
|
PackedByteArray b = read_file(path);
|
|
if (b.is_empty()) {
|
|
return Image{};
|
|
}
|
|
return load_dds(b.ptr(), (size_t)b.size());
|
|
}
|
|
|
|
std::optional<gr2::File> gr2_from_file(const String &path, gr2::LoadError *err) {
|
|
PackedByteArray b = read_file(path);
|
|
if (b.is_empty()) {
|
|
if (err) {
|
|
*err = {"open", std::string("cannot read ") + path.utf8().get_data()};
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
return gr2::File::load(b.ptr(), (size_t)b.size(), err);
|
|
}
|
|
|
|
} // namespace mtgodot
|