From 5bb0ff19bc16913cab5fc857195b15e532b04c24 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 26 Sep 2025 19:16:25 +0200 Subject: [PATCH] corregit: asset::checkFile() fallava desde fora del directori --- source/asset.cpp | 42 +++++++++++++++++++++++------------------- source/asset.h | 2 +- source/director.cpp | 14 ++++++++++---- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/source/asset.cpp b/source/asset.cpp index 9295c70..34069b1 100644 --- a/source/asset.cpp +++ b/source/asset.cpp @@ -2,11 +2,12 @@ #include // Para SDL_LogCategory, SDL_LogInfo, SDL_LogError, SDL_LogWarn -#include // Para size_t -#include // Para exception -#include // Para basic_istream, basic_ifstream, ifstream, istringstream -#include // Para basic_istringstream -#include // Para runtime_error +#include // Para size_t +#include // Para exception +#include // Para std::filesystem +#include // Para basic_istream, basic_ifstream, ifstream, istringstream +#include // Para basic_istringstream +#include // Para runtime_error #include "resource_helper.h" // Para ResourceHelper #include "utils.h" // Para getFileName @@ -205,25 +206,28 @@ auto Asset::check() const -> bool { } // Comprueba que existe un fichero -auto Asset::checkFile(const std::string &path) -> bool { - // Intentar primero con ResourceHelper - auto data = ResourceHelper::loadFile(path); - bool success = !data.empty(); +auto Asset::checkFile(const std::string &path) const -> bool { + // Construir ruta del pack usando executable_path_ + std::string pack_path = executable_path_ + "resources.pack"; + bool pack_exists = std::filesystem::exists(pack_path); - // Si no se encuentra en el pack, intentar con filesystem directo - if (!success) { + if (pack_exists) { + // MODO PACK: Usar ResourceHelper (igual que la carga real) + auto data = ResourceHelper::loadFile(path); + return !data.empty(); + } else { + // MODO FILESYSTEM: Verificación directa (modo desarrollo) std::ifstream file(path); - success = file.good(); + bool success = file.good(); file.close(); - } - if (!success) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Checking file: %s [ ERROR ]", - getFileName(path).c_str()); - } + if (!success) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error: Could not open file: %s", path.c_str()); + } - return success; + return success; + } } // Parsea string a Type diff --git a/source/asset.h b/source/asset.h index dcc4b76..c279f06 100644 --- a/source/asset.h +++ b/source/asset.h @@ -57,7 +57,7 @@ class Asset { std::string executable_path_; // Ruta del ejecutable // --- Métodos internos --- - [[nodiscard]] static auto checkFile(const std::string &path) -> bool; // Verifica si un archivo existe + [[nodiscard]] auto checkFile(const std::string &path) const -> bool; // Verifica si un archivo existe [[nodiscard]] static auto getTypeName(Type type) -> std::string; // Obtiene el nombre del tipo [[nodiscard]] static auto parseAssetType(const std::string &type_str) -> Type; // Convierte string a tipo void addToMap(const std::string &file_path, Type type, bool required, bool absolute); // Añade archivo al mapa diff --git a/source/director.cpp b/source/director.cpp index 31055e2..9fdca43 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -80,9 +80,9 @@ void Director::init() { Asset::init(executable_path_); // Inicializa el sistema de gestión de archivos #ifdef MACOS_BUNDLE - ResourceHelper::initializeResourceSystem(executable_path_ + "/../Resources/resources.pack"); + ResourceHelper::initializeResourceSystem(executable_path_ + "../Resources/resources.pack"); #else - ResourceHelper::initializeResourceSystem("resources.pack"); + ResourceHelper::initializeResourceSystem(executable_path_ + "resources.pack"); #endif loadAssets(); // Crea el índice de archivos Input::init(Asset::get()->get("gamecontrollerdb.txt"), Asset::get()->get("controllers.json")); // Carga configuración de controles @@ -174,8 +174,14 @@ void Director::loadAssets() { // Comprueba los parametros del programa void Director::checkProgramArguments(int argc, std::span argv) { - // Establece la ruta del programa - executable_path_ = getPath(argv[0]); + // Obtener la ruta absoluta del ejecutable + std::filesystem::path exe_path = std::filesystem::absolute(argv[0]); + executable_path_ = exe_path.parent_path().string(); + + // Asegurar que termine con separador de directorio + if (!executable_path_.empty() && executable_path_.back() != '/' && executable_path_.back() != '\\') { + executable_path_ += "/"; + } // Comprueba el resto de parámetros for (int i = 1; i < argc; ++i) {