diff --git a/config/assets.txt b/config/assets.txt index 4df3c9b..e028cd0 100644 --- a/config/assets.txt +++ b/config/assets.txt @@ -16,10 +16,12 @@ DATA|${PREFIX}/config/param_320x256.txt DATA|${PREFIX}/config/param_red.txt DATA|${PREFIX}/config/pools.txt DATA|${PREFIX}/config/stages.txt -DEMODATA|${PREFIX}/config/demo1.bin -DEMODATA|${PREFIX}/config/demo2.bin -# Música (archivos empaquetados) +# Archivos con los datos de la demo +DEMODATA|${PREFIX}/data/demo/demo1.bin +DEMODATA|${PREFIX}/data/demo/demo2.bin + +# Música MUSIC|${PREFIX}/data/music/credits.ogg MUSIC|${PREFIX}/data/music/intro.ogg MUSIC|${PREFIX}/data/music/playing.ogg diff --git a/config/demo1.bin b/data/demo/demo1.bin similarity index 100% rename from config/demo1.bin rename to data/demo/demo1.bin diff --git a/config/demo2.bin b/data/demo/demo2.bin similarity index 100% rename from config/demo2.bin rename to data/demo/demo2.bin diff --git a/resources.pack b/resources.pack index 1f060f9..86771f7 100644 Binary files a/resources.pack and b/resources.pack differ diff --git a/source/lang.cpp b/source/lang.cpp index 92e26ac..1c75020 100644 --- a/source/lang.cpp +++ b/source/lang.cpp @@ -11,6 +11,7 @@ #include "difficulty.h" // Para Difficulty #include "external/json.hpp" // Para basic_json, iteration_proxy_value, oper... #include "options.h" // Para SettingsOpt... +#include "resource_helper.h" // Para ResourceHelper using json = nlohmann::json; @@ -27,14 +28,24 @@ std::vector languages = { auto loadFromFile(const std::string &file_path) -> bool { texts.clear(); - std::ifstream rfile(file_path); - if (!rfile.is_open()) { - return false; - } - + // Intentar cargar desde ResourceHelper primero + auto resource_data = ResourceHelper::loadFile(file_path); + try { json j; - rfile >> j; + + if (!resource_data.empty()) { + // Cargar desde datos del pack + std::string content(resource_data.begin(), resource_data.end()); + j = json::parse(content); + } else { + // Fallback a filesystem directo + std::ifstream rfile(file_path); + if (!rfile.is_open()) { + return false; + } + rfile >> j; + } for (const auto &el : j.items()) { texts[el.key()] = el.value(); diff --git a/source/texture.cpp b/source/texture.cpp index b2b54d8..ce4e600 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -225,22 +225,30 @@ auto Texture::loadSurface(const std::string &file_path) -> std::shared_ptr buffer; + + // Intentar cargar desde ResourceHelper primero + auto resource_data = ResourceHelper::loadFile(file_path); + if (!resource_data.empty()) { + buffer = resource_data; + } else { + // Fallback a filesystem directo + std::ifstream file(file_path, std::ios::binary | std::ios::ate); + if (!file) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str()); + throw std::runtime_error("Fichero no encontrado: " + file_path); + } - // Obtener el tamaño del archivo - std::streamsize size = file.tellg(); - file.seekg(0, std::ios::beg); + // Obtener el tamaño del archivo + std::streamsize size = file.tellg(); + file.seekg(0, std::ios::beg); - // Leer el contenido del archivo en un buffer - std::vector buffer(size); - if (!file.read(reinterpret_cast(buffer.data()), size)) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al leer el fichero %s", file_path.c_str()); - throw std::runtime_error("Error al leer el fichero: " + file_path); + // Leer el contenido del archivo en un buffer + buffer.resize(size); + if (!file.read(reinterpret_cast(buffer.data()), size)) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al leer el fichero %s", file_path.c_str()); + throw std::runtime_error("Error al leer el fichero: " + file_path); + } } // Crear un objeto Gif y llamar a la función loadGif diff --git a/source/utils.cpp b/source/utils.cpp index 044f759..95dc4e1 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -13,6 +13,7 @@ #include // Para basic_string, allocator, string, operator==, operator+, char_traits #include "lang.h" // Para getText +#include "resource_helper.h" // Para ResourceHelper // Variables Overrides overrides = Overrides(); @@ -323,8 +324,17 @@ void printWithDots(const std::string &text1, const std::string &text2, const std auto loadDemoDataFromFile(const std::string &file_path) -> DemoData { DemoData dd; - // Indicador de éxito en la carga - auto *file = SDL_IOFromFile(file_path.c_str(), "r+b"); + SDL_IOStream *file = nullptr; + + // Intentar cargar desde ResourceHelper primero + auto resource_data = ResourceHelper::loadFile(file_path); + if (!resource_data.empty()) { + file = SDL_IOFromConstMem(resource_data.data(), resource_data.size()); + } else { + // Fallback a filesystem directo + file = SDL_IOFromFile(file_path.c_str(), "r+b"); + } + if (file == nullptr) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str()); throw std::runtime_error("Fichero no encontrado: " + file_path); diff --git a/tools/pack_resources b/tools/pack_resources new file mode 100755 index 0000000..6c4dd02 Binary files /dev/null and b/tools/pack_resources differ diff --git a/tools/pack_resources.cpp b/tools/pack_resources.cpp index 6b68ffa..c8ba3bf 100644 --- a/tools/pack_resources.cpp +++ b/tools/pack_resources.cpp @@ -2,22 +2,77 @@ #include #include +void showHelp() { + std::cout << "Coffee Crisis Arcade Edition - Resource Packer" << std::endl; + std::cout << "===============================================" << std::endl; + std::cout << "Usage: pack_resources [options] [input_dir] [output_file]" << std::endl; + std::cout << std::endl; + std::cout << "Options:" << std::endl; + std::cout << " --help Show this help message" << std::endl; + std::cout << " --list List contents of an existing pack file" << std::endl; + std::cout << std::endl; + std::cout << "Arguments:" << std::endl; + std::cout << " input_dir Directory to pack (default: data)" << std::endl; + std::cout << " output_file Pack file name (default: resources.pack)" << std::endl; + std::cout << std::endl; + std::cout << "Examples:" << std::endl; + std::cout << " pack_resources # Pack 'data' to 'resources.pack'" << std::endl; + std::cout << " pack_resources mydata # Pack 'mydata' to 'resources.pack'" << std::endl; + std::cout << " pack_resources data my.pack # Pack 'data' to 'my.pack'" << std::endl; + std::cout << " pack_resources --list my.pack # List contents of 'my.pack'" << std::endl; +} + +void listPackContents(const std::string& packFile) { + ResourcePack pack; + if (!pack.loadPack(packFile)) { + std::cerr << "Error: Cannot open pack file: " << packFile << std::endl; + return; + } + + auto resources = pack.getResourceList(); + std::cout << "Pack file: " << packFile << std::endl; + std::cout << "Resources: " << resources.size() << std::endl; + std::cout << "Contents:" << std::endl; + + for (const auto& resource : resources) { + std::cout << " " << resource << std::endl; + } +} + int main(int argc, char* argv[]) { std::string dataDir = "data"; std::string outputFile = "resources.pack"; + bool listMode = false; - if (argc > 1) { - dataDir = argv[1]; + // Parse arguments + for (int i = 1; i < argc; i++) { + std::string arg = argv[i]; + if (arg == "--help" || arg == "-h") { + showHelp(); + return 0; + } else if (arg == "--list") { + listMode = true; + if (i + 1 < argc) { + outputFile = argv[++i]; // Next argument is pack file to list + } + } else if (!arg.empty() && arg[0] != '-') { + if (dataDir == "data") { + dataDir = arg; + } else { + outputFile = arg; + } + } } - if (argc > 2) { - outputFile = argv[2]; + + if (listMode) { + listPackContents(outputFile); + return 0; } std::cout << "Coffee Crisis Arcade Edition - Resource Packer" << std::endl; std::cout << "===============================================" << std::endl; std::cout << "Input directory: " << dataDir << std::endl; std::cout << "Output file: " << outputFile << std::endl; - std::cout << "Note: config/ directory is excluded from packing" << std::endl; std::cout << std::endl; if (!std::filesystem::exists(dataDir)) {