- 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/`.
210 lines
7.6 KiB
C++
210 lines
7.6 KiB
C++
#include "core/resources/resource_pack.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
namespace Resource {
|
|
|
|
const std::string Pack::DEFAULT_ENCRYPT_KEY = "AEE_2026_PACK_KEY";
|
|
|
|
namespace {
|
|
constexpr const char* MAGIC = "AE26";
|
|
constexpr std::uint32_t VERSION = 1;
|
|
} // namespace
|
|
|
|
auto Pack::calculateChecksum(const std::vector<std::uint8_t>& data) -> std::uint32_t {
|
|
// djb2: seed 0x12345678. Compatible amb aee/projecte_2026 per si mai
|
|
// reutilitzem eines de diagnòstic entre projectes.
|
|
std::uint32_t checksum = 0x12345678;
|
|
for (unsigned char b : data) {
|
|
// cppcheck-suppress useStlAlgorithm
|
|
checksum = ((checksum << 5) + checksum) + b;
|
|
}
|
|
return checksum;
|
|
}
|
|
|
|
void Pack::xorInPlace(std::vector<std::uint8_t>& data, const std::string& key) {
|
|
if (key.empty()) { return; }
|
|
for (std::size_t i = 0; i < data.size(); ++i) {
|
|
data[i] ^= static_cast<std::uint8_t>(key[i % key.length()]);
|
|
}
|
|
}
|
|
|
|
auto Pack::loadPack(const std::string& pack_file) -> bool {
|
|
std::ifstream file(pack_file, std::ios::binary);
|
|
if (!file) { return false; }
|
|
|
|
std::array<char, 4> header{};
|
|
file.read(header.data(), 4);
|
|
if (std::string(header.data(), 4) != MAGIC) {
|
|
std::cerr << "Resource::Pack: invalid pack file format (bad magic): " << pack_file << '\n';
|
|
return false;
|
|
}
|
|
|
|
std::uint32_t version = 0;
|
|
file.read(reinterpret_cast<char*>(&version), sizeof(version));
|
|
if (version != VERSION) {
|
|
std::cerr << "Resource::Pack: unsupported pack version: " << version << '\n';
|
|
return false;
|
|
}
|
|
|
|
std::uint32_t resource_count = 0;
|
|
file.read(reinterpret_cast<char*>(&resource_count), sizeof(resource_count));
|
|
|
|
resources_.clear();
|
|
resources_.reserve(resource_count);
|
|
|
|
for (std::uint32_t i = 0; i < resource_count; ++i) {
|
|
std::uint32_t filename_length = 0;
|
|
file.read(reinterpret_cast<char*>(&filename_length), sizeof(filename_length));
|
|
|
|
std::string filename(filename_length, '\0');
|
|
file.read(filename.data(), filename_length);
|
|
|
|
PackEntry entry;
|
|
entry.filename = filename;
|
|
file.read(reinterpret_cast<char*>(&entry.offset), sizeof(entry.offset));
|
|
file.read(reinterpret_cast<char*>(&entry.size), sizeof(entry.size));
|
|
file.read(reinterpret_cast<char*>(&entry.checksum), sizeof(entry.checksum));
|
|
|
|
resources_[filename] = entry;
|
|
}
|
|
|
|
std::uint64_t data_size = 0;
|
|
file.read(reinterpret_cast<char*>(&data_size), sizeof(data_size));
|
|
|
|
data_.resize(data_size);
|
|
file.read(reinterpret_cast<char*>(data_.data()), static_cast<std::streamsize>(data_size));
|
|
|
|
xorInPlace(data_, DEFAULT_ENCRYPT_KEY);
|
|
return true;
|
|
}
|
|
|
|
auto Pack::savePack(const std::string& pack_file) const -> bool {
|
|
std::ofstream file(pack_file, std::ios::binary);
|
|
if (!file) {
|
|
std::cerr << "Resource::Pack: could not create pack file: " << pack_file << '\n';
|
|
return false;
|
|
}
|
|
|
|
file.write(MAGIC, 4);
|
|
|
|
std::uint32_t version = VERSION;
|
|
file.write(reinterpret_cast<const char*>(&version), sizeof(version));
|
|
|
|
auto resource_count = static_cast<std::uint32_t>(resources_.size());
|
|
file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));
|
|
|
|
for (const auto& [filename, entry] : resources_) {
|
|
auto filename_length = static_cast<std::uint32_t>(filename.length());
|
|
file.write(reinterpret_cast<const char*>(&filename_length), sizeof(filename_length));
|
|
file.write(filename.c_str(), filename_length);
|
|
file.write(reinterpret_cast<const char*>(&entry.offset), sizeof(entry.offset));
|
|
file.write(reinterpret_cast<const char*>(&entry.size), sizeof(entry.size));
|
|
file.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(entry.checksum));
|
|
}
|
|
|
|
std::vector<std::uint8_t> encrypted = data_;
|
|
xorInPlace(encrypted, DEFAULT_ENCRYPT_KEY);
|
|
|
|
std::uint64_t data_size = encrypted.size();
|
|
file.write(reinterpret_cast<const char*>(&data_size), sizeof(data_size));
|
|
file.write(reinterpret_cast<const char*>(encrypted.data()), static_cast<std::streamsize>(data_size));
|
|
|
|
return true;
|
|
}
|
|
|
|
auto Pack::addFile(const std::string& filename, const std::string& filepath) -> bool {
|
|
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
|
if (!file) {
|
|
std::cerr << "Resource::Pack: could not open file: " << filepath << '\n';
|
|
return false;
|
|
}
|
|
|
|
std::streamsize file_size = file.tellg();
|
|
file.seekg(0, std::ios::beg);
|
|
|
|
std::vector<std::uint8_t> file_data(file_size);
|
|
if (!file.read(reinterpret_cast<char*>(file_data.data()), file_size)) {
|
|
std::cerr << "Resource::Pack: could not read file: " << filepath << '\n';
|
|
return false;
|
|
}
|
|
|
|
PackEntry entry;
|
|
entry.filename = filename;
|
|
entry.offset = data_.size();
|
|
entry.size = file_data.size();
|
|
entry.checksum = calculateChecksum(file_data);
|
|
|
|
data_.insert(data_.end(), file_data.begin(), file_data.end());
|
|
resources_[filename] = entry;
|
|
return true;
|
|
}
|
|
|
|
auto Pack::addDirectory(const std::string& directory) -> bool {
|
|
if (!std::filesystem::exists(directory)) {
|
|
std::cerr << "Resource::Pack: directory does not exist: " << directory << '\n';
|
|
return false;
|
|
}
|
|
|
|
// cppcheck-suppress useStlAlgorithm
|
|
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
|
|
if (!entry.is_regular_file()) { continue; }
|
|
|
|
std::string filepath = entry.path().string();
|
|
std::string filename = std::filesystem::relative(entry.path(), directory).string();
|
|
std::ranges::replace(filename, '\\', '/');
|
|
|
|
if (!addFile(filename, filepath)) { return false; }
|
|
}
|
|
return true;
|
|
}
|
|
|
|
auto Pack::getResource(const std::string& filename) const -> std::vector<std::uint8_t> {
|
|
auto it = resources_.find(filename);
|
|
if (it == resources_.end()) { return {}; }
|
|
|
|
const PackEntry& entry = it->second;
|
|
if (entry.offset + entry.size > data_.size()) {
|
|
std::cerr << "Resource::Pack: invalid resource data: " << filename << '\n';
|
|
return {};
|
|
}
|
|
|
|
std::vector<std::uint8_t> result(
|
|
data_.begin() + static_cast<std::ptrdiff_t>(entry.offset),
|
|
data_.begin() + static_cast<std::ptrdiff_t>(entry.offset + entry.size));
|
|
|
|
std::uint32_t checksum = calculateChecksum(result);
|
|
if (checksum != entry.checksum) {
|
|
std::cerr << "Resource::Pack: checksum mismatch for: " << filename << '\n';
|
|
}
|
|
return result;
|
|
}
|
|
|
|
auto Pack::hasResource(const std::string& filename) const -> bool {
|
|
return resources_.contains(filename);
|
|
}
|
|
|
|
void Pack::clear() {
|
|
resources_.clear();
|
|
data_.clear();
|
|
}
|
|
|
|
auto Pack::getResourceCount() const -> std::size_t {
|
|
return resources_.size();
|
|
}
|
|
|
|
auto Pack::getResourceList() const -> std::vector<std::string> {
|
|
std::vector<std::string> result;
|
|
result.reserve(resources_.size());
|
|
for (const auto& [filename, entry] : resources_) {
|
|
result.push_back(filename);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace Resource
|