fix(resources): resoldre rutes de data/ respecte a SDL_GetBasePath

Abans el binari només arrencava si la CWD era la que contenia data/
(típicament build/). Executat des de qualsevol altra carpeta no trobava
keys.yaml, paletes, etc. i es moria al carregar el primer recurs.

Patró seguit de ../aee: Resource::Helper guarda un data root absolut
i resolve(rel) el prefixa davant de rutes no absolutes. Director fa
setDataRoot(SDL_GetBasePath()) com a primera acció del ctor, així tots
els loaders posteriors hi passen sense canvis al call site.

Migrats a Resource::Helper::resolve els loaders que obrien std::ifstream
directament amb el path rebut: Locale, KeyConfig, Player, Map, Tilemap
i EntityLoader. Els que ja usaven Resource::Helper::loadFile
(Resource::Cache, PaletteLoader) hereten el fix automàticament.

Els fitxers de system_folder_ (config.yaml, debug.yaml, postfx.yaml,
crtpi.yaml) es passen com a rutes absolutes i resolve() les deixa
intactes, així no es veuen afectats.

Test targets (test_entities_smoke, test_scene_flow) enllacen ara
resource_helper.cpp perquè el seu codi passa per Helper::resolve. Els
tests corren amb CWD = repo root, on les rutes "data/..." funcionen
amb data root buit sense canviar cap assert.

Verificat executant el binari des de build/, /tmp i / — tots tres
troben els recursos i arrenquen correctament, tant en debug com en
release (release sempre arranca a LOGO).
This commit is contained in:
JailDesigner committed 2026-04-22 11:29:51 +02:00
1 parent 9c9b6028e7
commit 6800786194
10 files changed
+88 -27

No files matched your search

+2
View File
@@ -265,6 +265,7 @@ option(BUILD_TESTS "Build smoke tests" ON)
if(BUILD_TESTS) if(BUILD_TESTS)
add_executable(test_entities_smoke add_executable(test_entities_smoke
tests/test_entities_smoke.cpp tests/test_entities_smoke.cpp
source/core/resources/resource_helper.cpp
source/game/map/map.cpp source/game/map/map.cpp
${ENTITY_SOURCES} ${ENTITY_SOURCES}
) )
@@ -288,6 +289,7 @@ if(BUILD_TESTS)
source/game/debug_config.cpp source/game/debug_config.cpp
source/game/scenes/placeholder_input.cpp source/game/scenes/placeholder_input.cpp
source/core/input/key_config.cpp source/core/input/key_config.cpp
source/core/resources/resource_helper.cpp
) )
target_include_directories(test_scene_flow PRIVATE target_include_directories(test_scene_flow PRIVATE
"${CMAKE_SOURCE_DIR}/source" "${CMAKE_SOURCE_DIR}/source"
+7 -5
View File
@@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp" #include "external/fkyaml_node.hpp"
auto KeyConfig::get() -> KeyConfig& { auto KeyConfig::get() -> KeyConfig& {
@@ -12,16 +13,17 @@ auto KeyConfig::get() -> KeyConfig& {
} }
void KeyConfig::load(const std::string& file_path) { void KeyConfig::load(const std::string& file_path) {
std::ifstream file(file_path); const auto FULL = Resource::Helper::resolve(file_path);
std::ifstream file(FULL);
if (!file.is_open()) { if (!file.is_open()) {
std::cerr << "KeyConfig: no s'ha pogut obrir " << file_path << '\n'; std::cerr << "KeyConfig: no s'ha pogut obrir " << FULL << '\n';
return; return;
} }
try { try {
auto yaml = fkyaml::node::deserialize(file); auto yaml = fkyaml::node::deserialize(file);
if (!yaml.contains("keys") || !yaml["keys"].is_sequence()) { if (!yaml.contains("keys") || !yaml["keys"].is_sequence()) {
std::cerr << "KeyConfig: falta la secció 'keys' a " << file_path << '\n'; std::cerr << "KeyConfig: falta la secció 'keys' a " << FULL << '\n';
return; return;
} }
bindings_.clear(); bindings_.clear();
@@ -36,9 +38,9 @@ void KeyConfig::load(const std::string& file_path) {
} }
bindings_[ID] = SC; bindings_[ID] = SC;
} }
std::cout << "KeyConfig: " << bindings_.size() << " bindings des de " << file_path << '\n'; std::cout << "KeyConfig: " << bindings_.size() << " bindings des de " << FULL << '\n';
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << "KeyConfig: error parsejant " << file_path << ": " << e.what() << '\n'; std::cerr << "KeyConfig: error parsejant " << FULL << ": " << e.what() << '\n';
} }
} }
+6 -4
View File
@@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp" #include "external/fkyaml_node.hpp"
namespace { namespace {
@@ -33,9 +34,10 @@ auto Locale::get() -> Locale& {
} }
void Locale::load(const std::string& file_path) { void Locale::load(const std::string& file_path) {
std::ifstream file(file_path); const auto FULL = Resource::Helper::resolve(file_path);
std::ifstream file(FULL);
if (!file.is_open()) { if (!file.is_open()) {
std::cerr << "Locale: no s'ha pogut obrir " << file_path << '\n'; std::cerr << "Locale: no s'ha pogut obrir " << FULL << '\n';
return; return;
} }
@@ -43,9 +45,9 @@ void Locale::load(const std::string& file_path) {
auto yaml = fkyaml::node::deserialize(file); auto yaml = fkyaml::node::deserialize(file);
strings_.clear(); strings_.clear();
flatten(yaml, "", strings_); flatten(yaml, "", strings_);
std::cout << "Locale: " << strings_.size() << " traduccions des de " << file_path << '\n'; std::cout << "Locale: " << strings_.size() << " traduccions des de " << FULL << '\n';
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << "Locale: error parsejant " << file_path << ": " << e.what() << '\n'; std::cerr << "Locale: error parsejant " << FULL << ": " << e.what() << '\n';
} }
} }
+24 -3
View File
@@ -2,20 +2,41 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string>
namespace Resource::Helper { namespace Resource::Helper {
namespace {
std::string g_data_root;
} // namespace
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> { auto loadFile(const std::string& filepath) -> std::vector<std::uint8_t> {
std::ifstream file(filepath, std::ios::binary | std::ios::ate); const auto FULL = resolve(filepath);
std::ifstream file(FULL, std::ios::binary | std::ios::ate);
if (!file) { if (!file) {
std::cerr << "Resource::Helper: cannot open " << filepath << '\n'; std::cerr << "Resource::Helper: cannot open " << FULL << '\n';
return {}; return {};
} }
std::streamsize size = file.tellg(); std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg); file.seekg(0, std::ios::beg);
std::vector<std::uint8_t> data(static_cast<std::size_t>(size)); std::vector<std::uint8_t> data(static_cast<std::size_t>(size));
if (!file.read(reinterpret_cast<char*>(data.data()), size)) { if (!file.read(reinterpret_cast<char*>(data.data()), size)) {
std::cerr << "Resource::Helper: failed to read " << filepath << '\n'; std::cerr << "Resource::Helper: failed to read " << FULL << '\n';
return {}; return {};
} }
return data; return data;
+16
View File
@@ -6,7 +6,23 @@
namespace Resource::Helper { namespace Resource::Helper {
// Estableix el prefix absolut des d'on es resolen totes les rutes
// relatives passades a `loadFile`/`resolve`. Pensat per a cridar-se una
// sola vegada al primer pas del Director amb `SDL_GetBasePath()`: així el
// binari troba `data/` sense dependre del CWD des d'on s'ha llançat (és
// obligatori per a .app de macOS i útil a Linux per no haver d'estar
// dins de `build/`).
void setDataRoot(const std::string& absolute_path);
// Resol una ruta relativa contra el data root. Si la ruta ja és absoluta
// (comença per '/') la retorna sense modificar — així els fitxers de
// config (system_folder_ + "config.yaml") segueixen funcionant sense
// canvis al call site.
[[nodiscard]] auto resolve(const std::string& filepath) -> std::string;
// Carga un fitxer del filesystem (a aquesta fase encara no hi ha pack). // Carga un fitxer del filesystem (a aquesta fase encara no hi ha pack).
// Les rutes relatives es resolen via `resolve()`; les absolutes s'obrin
// directament.
auto loadFile(const std::string& filepath) -> std::vector<std::uint8_t>; auto loadFile(const std::string& filepath) -> std::vector<std::uint8_t>;
} // namespace Resource::Helper } // namespace Resource::Helper
+10
View File
@@ -19,6 +19,7 @@
#include "core/rendering/surface.hpp" #include "core/rendering/surface.hpp"
#include "core/rendering/text.hpp" #include "core/rendering/text.hpp"
#include "core/resources/resource_cache.hpp" #include "core/resources/resource_cache.hpp"
#include "core/resources/resource_helper.hpp"
#include "core/scene/scene.hpp" #include "core/scene/scene.hpp"
#include "core/system/event_buffer.hpp" #include "core/system/event_buffer.hpp"
#include "game/debug_config.hpp" #include "game/debug_config.hpp"
@@ -35,6 +36,15 @@ Director::Director() {
std::fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError()); std::fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
} }
// Rutes dels recursos relatives a l'executable, no al CWD. SDL_GetBasePath
// retorna la carpeta del binari al desplegament portable i Contents/
// Resources/ dins d'un .app a macOS. Ha de fer-se abans de qualsevol
// loader que passi per Resource::Helper::resolve (Locale, KeyConfig,
// Resource::Cache, PaletteLoader, etc.).
if (const char* base = SDL_GetBasePath()) {
Resource::Helper::setDataRoot(base);
}
// Prepara la carpeta de sistema (~/.config/jailgames/aee_2026/) i carrega // Prepara la carpeta de sistema (~/.config/jailgames/aee_2026/) i carrega
// les opcions; si no existeix config.yaml el crea amb els defaults. // les opcions; si no existeix config.yaml el crea amb els defaults.
createSystemFolder(); createSystemFolder();
+7 -5
View File
@@ -4,6 +4,7 @@
#include <stdexcept> #include <stdexcept>
#include <unordered_map> #include <unordered_map>
#include "core/resources/resource_helper.hpp"
#include "game/entities/entity.hpp" #include "game/entities/entity.hpp"
#include "game/entities/entity_factory.hpp" #include "game/entities/entity_factory.hpp"
@@ -30,10 +31,10 @@ namespace EntityLoader {
auto it = cache.find(name); auto it = cache.find(name);
if (it != cache.end()) { return it->second; } if (it != cache.end()) { return it->second; }
const std::string PATH = templatePath(name); const std::string FULL = Resource::Helper::resolve(templatePath(name));
std::ifstream file(PATH); std::ifstream file(FULL);
if (!file.is_open()) { if (!file.is_open()) {
throw std::runtime_error("EntityLoader: no s'ha pogut obrir " + PATH); throw std::runtime_error("EntityLoader: no s'ha pogut obrir " + FULL);
} }
fkyaml::node node = fkyaml::node::deserialize(file); fkyaml::node node = fkyaml::node::deserialize(file);
cache.emplace(name, node); cache.emplace(name, node);
@@ -41,9 +42,10 @@ namespace EntityLoader {
} }
auto readSpawns(const std::string& level_yaml_path) -> std::vector<fkyaml::node> { auto readSpawns(const std::string& level_yaml_path) -> std::vector<fkyaml::node> {
std::ifstream file(level_yaml_path); const auto FULL = Resource::Helper::resolve(level_yaml_path);
std::ifstream file(FULL);
if (!file.is_open()) { if (!file.is_open()) {
throw std::runtime_error("EntityLoader: no s'ha pogut obrir " + level_yaml_path); throw std::runtime_error("EntityLoader: no s'ha pogut obrir " + FULL);
} }
fkyaml::node root = fkyaml::node::deserialize(file); fkyaml::node root = fkyaml::node::deserialize(file);
std::vector<fkyaml::node> spawns; std::vector<fkyaml::node> spawns;
+5 -3
View File
@@ -9,6 +9,7 @@
#include <utility> #include <utility>
#include "core/resources/resource_cache.hpp" #include "core/resources/resource_cache.hpp"
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp" #include "external/fkyaml_node.hpp"
#include "game/entities/event_bus.hpp" #include "game/entities/event_bus.hpp"
#include "utils/defaults.hpp" #include "utils/defaults.hpp"
@@ -21,9 +22,10 @@ namespace {
} // namespace } // namespace
void Player::loadFrom(const std::string& yaml_path) { void Player::loadFrom(const std::string& yaml_path) {
std::ifstream file(yaml_path); const auto FULL = Resource::Helper::resolve(yaml_path);
std::ifstream file(FULL);
if (!file.is_open()) { if (!file.is_open()) {
throw std::runtime_error("Player: no s'ha pogut obrir " + yaml_path); throw std::runtime_error("Player: no s'ha pogut obrir " + FULL);
} }
auto yaml = fkyaml::node::deserialize(file); auto yaml = fkyaml::node::deserialize(file);
@@ -31,7 +33,7 @@ void Player::loadFrom(const std::string& yaml_path) {
sprite_h = yaml["frameHeight"].get_value<int>(); sprite_h = yaml["frameHeight"].get_value<int>();
if (!yaml.contains("collision")) { if (!yaml.contains("collision")) {
throw std::runtime_error("Player: manca bloc `collision:` a " + yaml_path); throw std::runtime_error("Player: manca bloc `collision:` a " + FULL);
} }
const auto& c = yaml["collision"]; const auto& c = yaml["collision"];
collision_size = c["size"].get_value<int>(); collision_size = c["size"].get_value<int>();
+6 -4
View File
@@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp" #include "external/fkyaml_node.hpp"
namespace { namespace {
@@ -21,13 +22,14 @@ namespace {
namespace Map { namespace Map {
void loadFrom(const std::string& yaml_path) { void loadFrom(const std::string& yaml_path) {
std::ifstream file(yaml_path); const auto FULL = Resource::Helper::resolve(yaml_path);
std::ifstream file(FULL);
if (!file.is_open()) { if (!file.is_open()) {
throw std::runtime_error("Map: no s'ha pogut obrir " + yaml_path); throw std::runtime_error("Map: no s'ha pogut obrir " + FULL);
} }
auto yaml = fkyaml::node::deserialize(file); auto yaml = fkyaml::node::deserialize(file);
if (!yaml.contains("geometry")) { if (!yaml.contains("geometry")) {
throw std::runtime_error("Map: manca bloc `geometry:` a " + yaml_path); throw std::runtime_error("Map: manca bloc `geometry:` a " + FULL);
} }
const auto& g = yaml["geometry"]; const auto& g = yaml["geometry"];
@@ -76,7 +78,7 @@ namespace Map {
std::cout << "Map: geometria " << grid_cols << "x" << grid_rows std::cout << "Map: geometria " << grid_cols << "x" << grid_rows
<< " sarcs " << sarc_w << "x" << sarc_h << " sarcs " << sarc_w << "x" << sarc_h
<< " pitch " << pitch_x << "x" << pitch_y << " pitch " << pitch_x << "x" << pitch_y
<< " des de " << yaml_path << '\n'; << " des de " << FULL << '\n';
} }
auto getSarcRect(int col, int row) -> SDL_FRect { auto getSarcRect(int col, int row) -> SDL_FRect {
+5 -3
View File
@@ -12,6 +12,7 @@
#include "core/rendering/screen.hpp" #include "core/rendering/screen.hpp"
#include "core/rendering/surface.hpp" #include "core/rendering/surface.hpp"
#include "core/resources/resource_cache.hpp" #include "core/resources/resource_cache.hpp"
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp" #include "external/fkyaml_node.hpp"
namespace { namespace {
@@ -67,9 +68,10 @@ namespace {
} // namespace } // namespace
Tilemap::Tilemap(const std::string& yaml_path) { Tilemap::Tilemap(const std::string& yaml_path) {
std::ifstream file(yaml_path); const auto FULL = Resource::Helper::resolve(yaml_path);
std::ifstream file(FULL);
if (!file.is_open()) { if (!file.is_open()) {
throw std::runtime_error("Tilemap: no s'ha pogut obrir " + yaml_path); throw std::runtime_error("Tilemap: no s'ha pogut obrir " + FULL);
} }
auto yaml = fkyaml::node::deserialize(file); auto yaml = fkyaml::node::deserialize(file);
@@ -111,7 +113,7 @@ Tilemap::Tilemap(const std::string& yaml_path) {
} }
std::cout << "Tilemap: " << COLS << "x" << ROWS << " tiles (" std::cout << "Tilemap: " << COLS << "x" << ROWS << " tiles ("
<< width_px_ << "x" << height_px_ << " px) des de " << yaml_path << '\n'; << width_px_ << "x" << height_px_ << " px) des de " << FULL << '\n';
} }
void Tilemap::render() const { void Tilemap::render() const {