feat: resource.pack estil coffee_crisis — Fase 1 (pack + helper + eina pack_resources)
This commit is contained in:
39
CLAUDE.md
39
CLAUDE.md
@@ -228,6 +228,44 @@ JD8_Flip produces ABGR byte order: `0xFF000000 + R + (G<<8) + (B<<16)`. SDL text
|
|||||||
| `~/.config/jailgames/aee/postfx.yaml` | PostFX shader presets (6 defaults: CRT, NTSC, CURVED, SCANLINES, SUBTLE, CRT LIVE) |
|
| `~/.config/jailgames/aee/postfx.yaml` | PostFX shader presets (6 defaults: CRT, NTSC, CURVED, SCANLINES, SUBTLE, CRT LIVE) |
|
||||||
| `~/.config/jailgames/aee/crtpi.yaml` | CRT-Pi shader presets (4 defaults: DEFAULT, CURVED, SHARP, MINIMAL) |
|
| `~/.config/jailgames/aee/crtpi.yaml` | CRT-Pi shader presets (4 defaults: DEFAULT, CURVED, SHARP, MINIMAL) |
|
||||||
|
|
||||||
|
### Resource Pack (`source/core/resources/`) — **en construcció**
|
||||||
|
|
||||||
|
Sistema d'empaquetat d'assets a l'estil `coffee_crisis_arcade_edition`. Genera un sol fitxer binari opac `resource.pack` que substitueix la carpeta `data/` als releases natius.
|
||||||
|
|
||||||
|
**Format AEE1** (fidel a CCAE amb clau pròpia):
|
||||||
|
```
|
||||||
|
Header: "AEE1" (4B) + version uint32 + resource_count uint32
|
||||||
|
Index: per recurs → filename_len uint32 + filename + offset uint64 + size uint64 + checksum uint32
|
||||||
|
Payload: data_size uint64 + bytes XOR-xifrats amb "AEE_RESOURCES__2026"
|
||||||
|
```
|
||||||
|
Checksum: djb2-like amb seed `0x12345678`. Càrrega full-to-RAM (sense mmap).
|
||||||
|
|
||||||
|
**Fitxers**:
|
||||||
|
- [source/core/resources/resource_pack.hpp/cpp](source/core/resources/) — classe `ResourcePack`: `loadPack`, `savePack`, `addFile`, `addDirectory`, `getResource(name) → std::vector<uint8_t>`, `hasResource`
|
||||||
|
- [source/core/resources/resource_helper.hpp/cpp](source/core/resources/) — namespace `ResourceHelper`: `initializeResourceSystem(pack, enable_fallback)`, `loadFile(relative_path)`, `shutdownResourceSystem`. Prova el pack primer, cau a `file_getresourcefolder()+path` si el fallback està actiu.
|
||||||
|
- [tools/pack_resources/pack_resources.cpp](tools/pack_resources/pack_resources.cpp) — eina standalone CLI: `pack_resources [input_dir=data] [output=resource.pack]` + `--list pack`.
|
||||||
|
|
||||||
|
**Build**:
|
||||||
|
- `make pack` compila l'eina (target `pack_resources` a `EXCLUDE_FROM_ALL` de [CMakeLists.txt](CMakeLists.txt)) i genera `resource.pack` a la rel. 33 entrades ≈ 4 MB.
|
||||||
|
- `./build/pack_resources --list resource.pack` inspecciona el pack.
|
||||||
|
|
||||||
|
**Estat actual (Fase 1 completada, 2026-04-16)**:
|
||||||
|
- Classes + eina compilen i funcionen. El pack es genera correctament i el `--list` mostra les 33 entrades (totes les GIFs, OGGs, font `.fnt`+`.gif`, `locale/ca.yaml`, `shaders/*`).
|
||||||
|
- **Encara no està cablejat al joc**: cap callsite usa `ResourceHelper::loadFile`. Tots segueixen cridant `file_readfile`. El joc funciona exactament igual que abans.
|
||||||
|
- Nou getter `file_getresourcefolder()` afegit a [jfile.hpp](source/core/jail/jfile.hpp) perquè ResourceHelper puga construir el path del fallback.
|
||||||
|
|
||||||
|
**Pendent (Fases 2-6 del pla [.claude/plans/declarative-popping-breeze.md](/home/sergio/.claude/plans/declarative-popping-breeze.md))**:
|
||||||
|
1. **Fase 2** — Afegir `ResourceHelper::initializeResourceSystem(pack_path, enable_fallback=true)` a [main.cpp](source/main.cpp) just després de `file_setresourcefolder`, i `shutdownResourceSystem()` a `SDL_AppQuit`.
|
||||||
|
2. **Fase 3** — Migrar callsites `file_readfile` → `ResourceHelper::loadFile` (tipus canvia `std::vector<char>` → `std::vector<uint8_t>`):
|
||||||
|
- [locale.cpp:30](source/core/locale/locale.cpp#L30)
|
||||||
|
- [text.cpp:65, 129](source/core/rendering/text.cpp) (`.fnt` + bitmap)
|
||||||
|
- [scene_utils.cpp:12](source/scenes/scene_utils.cpp) (música escenes)
|
||||||
|
- [modulegame.cpp:52](source/game/modulegame.cpp) (música gameplay)
|
||||||
|
- [jdraw8.cpp:47, 65](source/core/jail/jdraw8.cpp) (`JD8_LoadSurface`, `JD8_LoadPalette`)
|
||||||
|
3. **Fase 4** — Eliminar scaffold `.jrf` de [jfile.cpp](source/core/jail/jfile.cpp): `file_setresourcefilename`, `file_setsource`, `SOURCE_FILE`/`SOURCE_FOLDER`, `dictionary_loaded()`, `file_getfilepointer()`, `file_readfile()`. Mantenir només config-folder + `file_setresourcefolder` + `file_getresourcefolder`.
|
||||||
|
4. **Fase 5** — Als targets release de [Makefile](Makefile) (`_linux_release`/`_windows_release`/`_macos_release`): afegir dependència `pack` i canviar `cp -r data` → `cp resource.pack`. WASM intacte (segueix usant `--preload-file data@/data`).
|
||||||
|
5. **Fase 6** — A [main.cpp](source/main.cpp): `enable_fallback = false` només per a `NDEBUG && !__EMSCRIPTEN__` (pack obligatori a Release natiu; Debug i WASM mantenen fallback).
|
||||||
|
|
||||||
### External Libraries (`source/external/`)
|
### External Libraries (`source/external/`)
|
||||||
|
|
||||||
- `gif.h` — Header-only GIF decoder. **Cannot be included from more than one .cpp** (no include guards on functions). Other files use `extern` declarations for `LoadGif()`
|
- `gif.h` — Header-only GIF decoder. **Cannot be included from more than one .cpp** (no include guards on functions). Other files use `extern` declarations for `LoadGif()`
|
||||||
@@ -279,6 +317,7 @@ JD8_Flip produces ABGR byte order: `0xFF000000 + R + (G<<8) + (B<<16)`. SDL text
|
|||||||
- **FPS counter jitter**: time segment width changes per frame (100 Hz centi updates) causes ~1-2 px horizontal jitter in centered layout. Could lock to max-width or use monospace digits.
|
- **FPS counter jitter**: time segment width changes per frame (100 Hz centi updates) causes ~1-2 px horizontal jitter in centered layout. Could lock to max-width or use monospace digits.
|
||||||
- **Notification messages partially hardcoded**: overlay/global_inputs/director now use Locale, but the window title (`Texts::WINDOW_TITLE`) and some game-layer strings remain hardcoded.
|
- **Notification messages partially hardcoded**: overlay/global_inputs/director now use Locale, but the window title (`Texts::WINDOW_TITLE`) and some game-layer strings remain hardcoded.
|
||||||
- **jail_audio `JA_Sound_t` RAII**: `JA_Music_t` ja està net (vector + string), però `JA_Sound_t` encara usa `Uint8*` via `SDL_LoadWAV` out-param. Petit polish per a completar la coherència RAII.
|
- **jail_audio `JA_Sound_t` RAII**: `JA_Music_t` ja està net (vector + string), però `JA_Sound_t` encara usa `Uint8*` via `SDL_LoadWAV` out-param. Petit polish per a completar la coherència RAII.
|
||||||
|
- **Resource Pack — Fases 2-6**: la classe `ResourcePack`, `ResourceHelper` i l'eina `pack_resources` ja estan fetes (Fase 1). Queda cablejar-ho al joc: init a `main.cpp`, migrar 5 callsites de `file_readfile` a `ResourceHelper::loadFile`, eliminar l'scaffold `.jrf` de `jfile`, integrar `resource.pack` als bundles release, i flip `enable_fallback=false` per a Release natiu. Detall complet a la secció *Resource Pack* i al pla [.claude/plans/declarative-popping-breeze.md](/home/sergio/.claude/plans/declarative-popping-breeze.md).
|
||||||
|
|
||||||
### Previously Fixed (kept for reference)
|
### Previously Fixed (kept for reference)
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ set(APP_SOURCES
|
|||||||
# Core - Locale (nova capa)
|
# Core - Locale (nova capa)
|
||||||
source/core/locale/locale.cpp
|
source/core/locale/locale.cpp
|
||||||
|
|
||||||
|
# Core - Resources (pack binari AEE1, estil coffee_crisis)
|
||||||
|
source/core/resources/resource_pack.cpp
|
||||||
|
source/core/resources/resource_helper.cpp
|
||||||
|
|
||||||
# Core - Capa de presentación (nueva)
|
# Core - Capa de presentación (nueva)
|
||||||
source/core/rendering/menu.cpp
|
source/core/rendering/menu.cpp
|
||||||
source/core/rendering/overlay.cpp
|
source/core/rendering/overlay.cpp
|
||||||
@@ -225,6 +229,20 @@ if(NOT EMSCRIPTEN)
|
|||||||
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
|
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# --- EINA STANDALONE: pack_resources ---
|
||||||
|
# Executable auxiliar que empaqueta `data/` a `resource.pack` (format AEE1).
|
||||||
|
# No es compila per defecte (EXCLUDE_FROM_ALL). Build explícit:
|
||||||
|
# cmake --build build --target pack_resources
|
||||||
|
# Després executar: ./build/pack_resources data resource.pack
|
||||||
|
if(NOT EMSCRIPTEN)
|
||||||
|
add_executable(pack_resources EXCLUDE_FROM_ALL
|
||||||
|
tools/pack_resources/pack_resources.cpp
|
||||||
|
source/core/resources/resource_pack.cpp
|
||||||
|
)
|
||||||
|
target_include_directories(pack_resources PRIVATE "${CMAKE_SOURCE_DIR}/source")
|
||||||
|
target_compile_options(pack_resources PRIVATE -Wall)
|
||||||
|
endif()
|
||||||
|
|
||||||
# --- CLANG-FORMAT TARGETS ---
|
# --- CLANG-FORMAT TARGETS ---
|
||||||
find_program(CLANG_FORMAT_EXE NAMES clang-format)
|
find_program(CLANG_FORMAT_EXE NAMES clang-format)
|
||||||
|
|
||||||
|
|||||||
8
Makefile
8
Makefile
@@ -76,6 +76,12 @@ debug:
|
|||||||
@cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
|
@cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
|
||||||
@cmake --build build
|
@cmake --build build
|
||||||
|
|
||||||
|
# Empaqueta data/ a resource.pack (format AEE1). Build previ de l'eina + execució.
|
||||||
|
pack:
|
||||||
|
@cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
||||||
|
@cmake --build build --target pack_resources
|
||||||
|
@./build/pack_resources data resource.pack
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# RELEASE AUTOMÁTICO (detecta SO)
|
# RELEASE AUTOMÁTICO (detecta SO)
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
@@ -273,4 +279,4 @@ _linux_release:
|
|||||||
# Elimina la carpeta temporal
|
# Elimina la carpeta temporal
|
||||||
$(RMDIR) "$(RELEASE_FOLDER)"
|
$(RMDIR) "$(RELEASE_FOLDER)"
|
||||||
|
|
||||||
.PHONY: all debug release wasm _windows_release _linux_release _macos_release
|
.PHONY: all debug pack release wasm _windows_release _linux_release _macos_release
|
||||||
|
|||||||
BIN
resource.pack
Normal file
BIN
resource.pack
Normal file
Binary file not shown.
@@ -105,6 +105,10 @@ void file_setresourcefolder(const char* str) {
|
|||||||
resource_folder = str;
|
resource_folder = str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* file_getresourcefolder() {
|
||||||
|
return resource_folder.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
void file_setsource(const int src) {
|
void file_setsource(const int src) {
|
||||||
file_source = src % 2;
|
file_source = src % 2;
|
||||||
if (src == SOURCE_FOLDER && resource_folder.empty()) file_setresourcefolder(DEFAULT_FOLDER);
|
if (src == SOURCE_FOLDER && resource_folder.empty()) file_setresourcefolder(DEFAULT_FOLDER);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const char* file_getconfigfolder();
|
|||||||
|
|
||||||
void file_setresourcefilename(const char* str);
|
void file_setresourcefilename(const char* str);
|
||||||
void file_setresourcefolder(const char* str);
|
void file_setresourcefolder(const char* str);
|
||||||
|
const char* file_getresourcefolder();
|
||||||
void file_setsource(const int src);
|
void file_setsource(const int src);
|
||||||
|
|
||||||
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary = false);
|
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary = false);
|
||||||
|
|||||||
67
source/core/resources/resource_helper.cpp
Normal file
67
source/core/resources/resource_helper.cpp
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#include "core/resources/resource_helper.hpp"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "core/jail/jfile.hpp"
|
||||||
|
#include "core/resources/resource_pack.hpp"
|
||||||
|
|
||||||
|
namespace ResourceHelper {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
ResourcePack pack_;
|
||||||
|
bool pack_loaded_ = false;
|
||||||
|
bool fallback_enabled_ = true;
|
||||||
|
|
||||||
|
auto readFromDisk(const std::string& relative_path) -> std::vector<uint8_t> {
|
||||||
|
const std::string full = std::string(file_getresourcefolder()) + relative_path;
|
||||||
|
std::ifstream file(full, std::ios::binary | std::ios::ate);
|
||||||
|
if (!file) return {};
|
||||||
|
|
||||||
|
std::streamsize size = file.tellg();
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
std::vector<uint8_t> data(size);
|
||||||
|
if (!file.read(reinterpret_cast<char*>(data.data()), size)) return {};
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
auto initializeResourceSystem(const std::string& pack_file, bool enable_fallback) -> bool {
|
||||||
|
fallback_enabled_ = enable_fallback;
|
||||||
|
pack_loaded_ = pack_.loadPack(pack_file);
|
||||||
|
|
||||||
|
if (pack_loaded_) {
|
||||||
|
std::cout << "ResourceHelper: pack loaded (" << pack_.getResourceCount()
|
||||||
|
<< " entries) from " << pack_file << '\n';
|
||||||
|
} else if (enable_fallback) {
|
||||||
|
std::cout << "ResourceHelper: no pack at " << pack_file
|
||||||
|
<< " — using filesystem fallback\n";
|
||||||
|
} else {
|
||||||
|
std::cerr << "ResourceHelper: FATAL — no pack at " << pack_file
|
||||||
|
<< " and fallback disabled\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void shutdownResourceSystem() {
|
||||||
|
pack_.clear();
|
||||||
|
pack_loaded_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto loadFile(const std::string& relative_path) -> std::vector<uint8_t> {
|
||||||
|
if (pack_loaded_ && pack_.hasResource(relative_path)) {
|
||||||
|
return pack_.getResource(relative_path);
|
||||||
|
}
|
||||||
|
if (fallback_enabled_) {
|
||||||
|
return readFromDisk(relative_path);
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto hasPack() -> bool {
|
||||||
|
return pack_loaded_;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ResourceHelper
|
||||||
27
source/core/resources/resource_helper.hpp
Normal file
27
source/core/resources/resource_helper.hpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// API d'alt nivell per a llegir recursos. Prova primer el pack (si està
|
||||||
|
// carregat), després cau al fitxer solt dins `file_getresourcefolder()`
|
||||||
|
// si el fallback està activat.
|
||||||
|
namespace ResourceHelper {
|
||||||
|
|
||||||
|
// Inicialitza el sistema. `pack_file` és la ruta absoluta (o relativa al
|
||||||
|
// CWD) al fitxer de recursos. `enable_fallback` permet llegir de disc
|
||||||
|
// quan el pack no conté l'entrada (útil per a Debug i WASM).
|
||||||
|
auto initializeResourceSystem(const std::string& pack_file, bool enable_fallback) -> bool;
|
||||||
|
|
||||||
|
// Allibera el pack carregat a memòria.
|
||||||
|
void shutdownResourceSystem();
|
||||||
|
|
||||||
|
// Llegeix un recurs per ruta relativa (p.ex. "logo.gif", "fonts/8bithud.fnt").
|
||||||
|
// Retorna un vector buit si no es troba.
|
||||||
|
auto loadFile(const std::string& relative_path) -> std::vector<uint8_t>;
|
||||||
|
|
||||||
|
// True si el sistema es va inicialitzar amb un pack vàlid.
|
||||||
|
[[nodiscard]] auto hasPack() -> bool;
|
||||||
|
|
||||||
|
} // namespace ResourceHelper
|
||||||
220
source/core/resources/resource_pack.cpp
Normal file
220
source/core/resources/resource_pack.cpp
Normal file
@@ -0,0 +1,220 @@
|
|||||||
|
#include "core/resources/resource_pack.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
const std::string ResourcePack::DEFAULT_ENCRYPT_KEY = "AEE_RESOURCES__2026";
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr const char* MAGIC = "AEE1";
|
||||||
|
constexpr uint32_t VERSION = 1;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
ResourcePack::ResourcePack() = default;
|
||||||
|
|
||||||
|
ResourcePack::~ResourcePack() {
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t {
|
||||||
|
// djb2-like hash, seed 0x12345678 (idèntic a CCAE).
|
||||||
|
uint32_t checksum = 0x12345678;
|
||||||
|
for (unsigned char b : data) {
|
||||||
|
checksum = ((checksum << 5) + checksum) + b;
|
||||||
|
}
|
||||||
|
return checksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ResourcePack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||||
|
if (key.empty()) return;
|
||||||
|
for (size_t i = 0; i < data.size(); ++i) {
|
||||||
|
data[i] ^= static_cast<uint8_t>(key[i % key.length()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ResourcePack::decryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||||
|
encryptData(data, key); // XOR és simètric
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ResourcePack::loadPack(const std::string& pack_file) -> bool {
|
||||||
|
std::ifstream file(pack_file, std::ios::binary);
|
||||||
|
if (!file) {
|
||||||
|
return false; // No imprimim error: el caller decideix si cal fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
std::array<char, 4> header{};
|
||||||
|
file.read(header.data(), 4);
|
||||||
|
if (std::string(header.data(), 4) != MAGIC) {
|
||||||
|
std::cerr << "ResourcePack: invalid pack file format (bad magic): " << pack_file << '\n';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t version = 0;
|
||||||
|
file.read(reinterpret_cast<char*>(&version), sizeof(version));
|
||||||
|
if (version != VERSION) {
|
||||||
|
std::cerr << "ResourcePack: unsupported pack version: " << version << '\n';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t resource_count = 0;
|
||||||
|
file.read(reinterpret_cast<char*>(&resource_count), sizeof(resource_count));
|
||||||
|
|
||||||
|
resources_.clear();
|
||||||
|
resources_.reserve(resource_count);
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < resource_count; ++i) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
ResourceEntry 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
decryptData(data_, DEFAULT_ENCRYPT_KEY);
|
||||||
|
|
||||||
|
loaded_ = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ResourcePack::savePack(const std::string& pack_file) -> bool {
|
||||||
|
std::ofstream file(pack_file, std::ios::binary);
|
||||||
|
if (!file) {
|
||||||
|
std::cerr << "ResourcePack: could not create pack file: " << pack_file << '\n';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
file.write(MAGIC, 4);
|
||||||
|
|
||||||
|
uint32_t version = VERSION;
|
||||||
|
file.write(reinterpret_cast<const char*>(&version), sizeof(version));
|
||||||
|
|
||||||
|
auto resource_count = static_cast<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<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<uint8_t> encrypted = data_;
|
||||||
|
encryptData(encrypted, DEFAULT_ENCRYPT_KEY);
|
||||||
|
|
||||||
|
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 ResourcePack::addFile(const std::string& filename, const std::string& filepath) -> bool {
|
||||||
|
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||||
|
if (!file) {
|
||||||
|
std::cerr << "ResourcePack: could not open file: " << filepath << '\n';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::streamsize file_size = file.tellg();
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
std::vector<uint8_t> file_data(file_size);
|
||||||
|
if (!file.read(reinterpret_cast<char*>(file_data.data()), file_size)) {
|
||||||
|
std::cerr << "ResourcePack: could not read file: " << filepath << '\n';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourceEntry 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 ResourcePack::addDirectory(const std::string& directory) -> bool {
|
||||||
|
if (!std::filesystem::exists(directory)) {
|
||||||
|
std::cerr << "ResourcePack: directory does not exist: " << directory << '\n';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ResourcePack::getResource(const std::string& filename) -> std::vector<uint8_t> {
|
||||||
|
auto it = resources_.find(filename);
|
||||||
|
if (it == resources_.end()) return {};
|
||||||
|
|
||||||
|
const ResourceEntry& entry = it->second;
|
||||||
|
if (entry.offset + entry.size > data_.size()) {
|
||||||
|
std::cerr << "ResourcePack: invalid resource data: " << filename << '\n';
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> result(data_.begin() + entry.offset,
|
||||||
|
data_.begin() + entry.offset + entry.size);
|
||||||
|
|
||||||
|
uint32_t checksum = calculateChecksum(result);
|
||||||
|
if (checksum != entry.checksum) {
|
||||||
|
std::cerr << "ResourcePack: checksum mismatch for: " << filename << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ResourcePack::hasResource(const std::string& filename) const -> bool {
|
||||||
|
return resources_.contains(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ResourcePack::clear() {
|
||||||
|
resources_.clear();
|
||||||
|
data_.clear();
|
||||||
|
loaded_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ResourcePack::getResourceCount() const -> size_t {
|
||||||
|
return resources_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ResourcePack::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;
|
||||||
|
}
|
||||||
52
source/core/resources/resource_pack.hpp
Normal file
52
source/core/resources/resource_pack.hpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// Entrada d'un recurs dins el pack (format AEE, equivalent a CCAE).
|
||||||
|
struct ResourceEntry {
|
||||||
|
std::string filename;
|
||||||
|
uint64_t offset{0};
|
||||||
|
uint64_t size{0};
|
||||||
|
uint32_t checksum{0};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Pack binari de recursos carregat a memòria. Formato:
|
||||||
|
// Header: "AEE1" (4 bytes) + version uint32 + resource_count uint32
|
||||||
|
// Index: per cada recurs -> filename_len uint32 + filename + offset uint64
|
||||||
|
// + size uint64 + checksum uint32
|
||||||
|
// Payload: data_size uint64 + bytes xifrats amb XOR (DEFAULT_ENCRYPT_KEY)
|
||||||
|
class ResourcePack {
|
||||||
|
public:
|
||||||
|
ResourcePack();
|
||||||
|
~ResourcePack();
|
||||||
|
|
||||||
|
// I/O del fitxer
|
||||||
|
auto loadPack(const std::string& pack_file) -> bool;
|
||||||
|
auto savePack(const std::string& pack_file) -> bool;
|
||||||
|
|
||||||
|
// Builders usats per l'eina pack_resources
|
||||||
|
auto addFile(const std::string& filename, const std::string& filepath) -> bool;
|
||||||
|
auto addDirectory(const std::string& directory) -> bool;
|
||||||
|
|
||||||
|
[[nodiscard]] auto getResource(const std::string& filename) -> std::vector<uint8_t>;
|
||||||
|
[[nodiscard]] auto hasResource(const std::string& filename) const -> bool;
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
[[nodiscard]] auto getResourceCount() const -> size_t;
|
||||||
|
[[nodiscard]] auto getResourceList() const -> std::vector<std::string>;
|
||||||
|
|
||||||
|
static const std::string DEFAULT_ENCRYPT_KEY;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, ResourceEntry> resources_;
|
||||||
|
std::vector<uint8_t> data_;
|
||||||
|
bool loaded_{false};
|
||||||
|
|
||||||
|
static auto calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t;
|
||||||
|
static void encryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||||
|
static void decryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||||
|
};
|
||||||
98
tools/pack_resources/pack_resources.cpp
Normal file
98
tools/pack_resources/pack_resources.cpp
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "core/resources/resource_pack.hpp"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void showHelp() {
|
||||||
|
std::cout << "AEE - Resource Packer\n";
|
||||||
|
std::cout << "=====================\n";
|
||||||
|
std::cout << "Usage: pack_resources [options] [input_dir] [output_file]\n\n";
|
||||||
|
std::cout << "Options:\n";
|
||||||
|
std::cout << " --help Show this help message\n";
|
||||||
|
std::cout << " --list List contents of an existing pack file\n\n";
|
||||||
|
std::cout << "Arguments:\n";
|
||||||
|
std::cout << " input_dir Directory to pack (default: data)\n";
|
||||||
|
std::cout << " output_file Pack file name (default: resource.pack)\n\n";
|
||||||
|
std::cout << "Examples:\n";
|
||||||
|
std::cout << " pack_resources # Pack 'data' to 'resource.pack'\n";
|
||||||
|
std::cout << " pack_resources mydata mypack.pack # Pack 'mydata' to 'mypack.pack'\n";
|
||||||
|
std::cout << " pack_resources --list my.pack # List contents of 'my.pack'\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void listPackContents(const std::string& pack_file) {
|
||||||
|
ResourcePack pack;
|
||||||
|
if (!pack.loadPack(pack_file)) {
|
||||||
|
std::cerr << "Error: cannot open pack file: " << pack_file << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto resources = pack.getResourceList();
|
||||||
|
std::cout << "Pack file: " << pack_file << '\n';
|
||||||
|
std::cout << "Resources: " << resources.size() << '\n';
|
||||||
|
for (const auto& r : resources) std::cout << " " << r << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
std::string data_dir = "data";
|
||||||
|
std::string output_file = "resource.pack";
|
||||||
|
bool list_mode = false;
|
||||||
|
bool data_dir_set = false;
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; ++i) {
|
||||||
|
std::string arg = argv[i];
|
||||||
|
if (arg == "--help" || arg == "-h") {
|
||||||
|
showHelp();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (arg == "--list") {
|
||||||
|
list_mode = true;
|
||||||
|
if (i + 1 < argc) output_file = argv[++i];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!arg.empty() && arg[0] != '-') {
|
||||||
|
if (!data_dir_set) {
|
||||||
|
data_dir = arg;
|
||||||
|
data_dir_set = true;
|
||||||
|
} else {
|
||||||
|
output_file = arg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (list_mode) {
|
||||||
|
listPackContents(output_file);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "AEE - Resource Packer\n=====================\n";
|
||||||
|
std::cout << "Input directory: " << data_dir << '\n';
|
||||||
|
std::cout << "Output file: " << output_file << '\n';
|
||||||
|
|
||||||
|
if (!std::filesystem::exists(data_dir)) {
|
||||||
|
std::cerr << "Error: input directory does not exist: " << data_dir << '\n';
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourcePack pack;
|
||||||
|
std::cout << "Scanning and packing resources...\n";
|
||||||
|
if (!pack.addDirectory(data_dir)) {
|
||||||
|
std::cerr << "Error: failed to add directory to pack\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::cout << "Found " << pack.getResourceCount() << " resources\n";
|
||||||
|
|
||||||
|
std::cout << "Saving pack file...\n";
|
||||||
|
if (!pack.savePack(output_file)) {
|
||||||
|
std::cerr << "Error: failed to save pack file\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto file_size = std::filesystem::file_size(std::filesystem::path(output_file));
|
||||||
|
std::cout << "Pack file created: " << output_file << " ("
|
||||||
|
<< (static_cast<double>(file_size) / 1024.0 / 1024.0) << " MB)\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user