#include "resource_manager.hpp" #include "resource_pack.hpp" #include #include // Inicializar el puntero estático ResourcePack* ResourceManager::resourcePack_ = nullptr; bool ResourceManager::init(const std::string& packFilePath) { // Si ya estaba inicializado, liberar primero if (resourcePack_ != nullptr) { delete resourcePack_; resourcePack_ = nullptr; } // Intentar cargar el pack resourcePack_ = new ResourcePack(); if (!resourcePack_->loadPack(packFilePath)) { // Si falla, borrar instancia (usará fallback a disco) delete resourcePack_; resourcePack_ = nullptr; std::cout << "resources.pack no encontrado - usando carpeta data/" << std::endl; return false; } std::cout << "resources.pack cargado (" << resourcePack_->getResourceCount() << " recursos)" << std::endl; return true; } void ResourceManager::shutdown() { if (resourcePack_ != nullptr) { delete resourcePack_; resourcePack_ = nullptr; } } bool ResourceManager::loadResource(const std::string& resourcePath, unsigned char*& data, size_t& size) { data = nullptr; size = 0; // 1. Intentar cargar desde pack (si está disponible) if (resourcePack_ != nullptr) { ResourcePack::ResourceData packData = resourcePack_->loadResource(resourcePath); if (packData.data != nullptr) { data = packData.data; size = packData.size; return true; } } // 2. Fallback: cargar desde disco std::ifstream file(resourcePath, std::ios::binary | std::ios::ate); if (!file) { // Intentar con "data/" como prefijo si no se encontró std::string dataPath = "data/" + resourcePath; file.open(dataPath, std::ios::binary | std::ios::ate); if (!file) { return false; } } // Obtener tamaño del archivo size = static_cast(file.tellg()); file.seekg(0, std::ios::beg); // Alocar buffer y leer data = new unsigned char[size]; file.read(reinterpret_cast(data), size); file.close(); return true; } bool ResourceManager::isPackLoaded() { return resourcePack_ != nullptr; } std::vector ResourceManager::getResourceList() { if (resourcePack_ != nullptr) { return resourcePack_->getResourceList(); } return std::vector(); // Vacío si no hay pack } size_t ResourceManager::getResourceCount() { if (resourcePack_ != nullptr) { return resourcePack_->getResourceCount(); } return 0; }