Files
aee-arcade/source/core/resources/resource_helper.cpp
T
JailDesigner 4da05b3d76 chore(release): v0.21.0 — empaquetat de recursos (AE26) i neteja de fonts
- Nou format de pack AE26 v1 (XOR + djb2), eina `pack_resources`,
  regeneració automàtica de `build/resources.pack` a cada build.
- `Resource::Helper` amb init/shutdown, fallback a disc només en Debug
  (apunta al source tree); Release exigeix pack present.
- Callers de YAML migrats a `loadFile`+deserialize des de buffer.
- Arrel neta: el pack viu a `build/`, eliminat `sync_data`.
- Placeholder font -> `ari-w9500-condensed-display`; esborrades variants
  bold/condensed no usades a `data/fonts/`.
2026-04-23 08:51:21 +02:00

93 lines
3.1 KiB
C++

#include "core/resources/resource_helper.hpp"
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include "core/resources/resource_pack.hpp"
namespace Resource::Helper {
namespace {
std::string g_data_root;
Pack g_pack;
bool g_pack_loaded = false;
bool g_fallback_enabled = true;
auto readFromDisk(const std::string& filepath) -> std::vector<std::uint8_t> {
const auto FULL = resolve(filepath);
std::ifstream file(FULL, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "Resource::Helper: cannot open " << FULL << '\n';
return {};
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::uint8_t> data(static_cast<std::size_t>(size));
if (!file.read(reinterpret_cast<char*>(data.data()), size)) {
std::cerr << "Resource::Helper: failed to read " << FULL << '\n';
return {};
}
return data;
}
} // namespace
auto initializeResourceSystem(const std::string& pack_file, bool enable_fallback) -> bool {
g_fallback_enabled = enable_fallback;
g_pack_loaded = g_pack.loadPack(pack_file);
if (g_pack_loaded) {
std::cout << "Resource::Helper: pack loaded (" << g_pack.getResourceCount()
<< " entries) from " << pack_file << '\n';
} else if (enable_fallback) {
std::cout << "Resource::Helper: no pack at " << pack_file
<< " — using filesystem fallback\n";
} else {
std::cerr << "Resource::Helper: FATAL — no pack at " << pack_file
<< " and fallback disabled\n";
return false;
}
return true;
}
void shutdownResourceSystem() {
g_pack.clear();
g_pack_loaded = false;
}
auto hasPack() -> bool {
return g_pack_loaded;
}
void setDataRoot(const std::string& absolute_path) {
g_data_root = absolute_path;
if (!g_data_root.empty() && g_data_root.back() != '/') {
g_data_root += '/';
}
}
auto resolve(const std::string& filepath) -> std::string {
if (filepath.empty() || filepath.front() == '/') { return filepath; }
if (g_data_root.empty()) { return filepath; }
return g_data_root + filepath;
}
auto loadFile(const std::string& filepath) -> std::vector<std::uint8_t> {
if (g_pack_loaded) {
// El pack està indexat relatiu a `data/` (la carpeta que
// s'empaqueta), però els callers passen "data/foo.yaml" amb
// prefix. Fem el strip ací perquè no hagin de tenir-ho en compte.
constexpr std::string_view PREFIX = "data/";
std::string key = filepath;
if (key.starts_with(PREFIX)) { key.erase(0, PREFIX.size()); }
if (g_pack.hasResource(key)) { return g_pack.getResource(key); }
}
if (g_fallback_enabled) { return readFromDisk(filepath); }
return {};
}
} // namespace Resource::Helper