treballant en resources.pack
This commit is contained in:
91
source/resource_helper.cpp
Normal file
91
source/resource_helper.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "resource_helper.h"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace ResourceHelper {
|
||||
static bool resource_system_initialized = false;
|
||||
|
||||
bool initializeResourceSystem(const std::string& pack_file) {
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
resource_system_initialized = loader.initialize(pack_file, true);
|
||||
|
||||
if (resource_system_initialized) {
|
||||
std::cout << "Resource system initialized with pack: " << pack_file << std::endl;
|
||||
} else {
|
||||
std::cout << "Resource system using fallback mode (filesystem only)" << std::endl;
|
||||
}
|
||||
|
||||
return true; // Always return true as fallback is acceptable
|
||||
}
|
||||
|
||||
void shutdownResourceSystem() {
|
||||
if (resource_system_initialized) {
|
||||
ResourceLoader::getInstance().shutdown();
|
||||
resource_system_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> loadFile(const std::string& filepath) {
|
||||
if (resource_system_initialized && shouldUseResourcePack(filepath)) {
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
std::string pack_path = getPackPath(filepath);
|
||||
|
||||
auto data = loader.loadResource(pack_path);
|
||||
if (!data.empty()) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback a filesystem
|
||||
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::streamsize fileSize = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> data(fileSize);
|
||||
if (!file.read(reinterpret_cast<char*>(data.data()), fileSize)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
bool shouldUseResourcePack(const std::string& filepath) {
|
||||
// Archivos que NO van al pack:
|
||||
// - config/ (ahora está fuera de data/)
|
||||
// - archivos absolutos del sistema
|
||||
|
||||
if (filepath.find("config/") != std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Si contiene "data/" es candidato para el pack
|
||||
if (filepath.find("data/") != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string getPackPath(const std::string& asset_path) {
|
||||
std::string pack_path = asset_path;
|
||||
|
||||
// Remover prefijo "data/" si existe
|
||||
size_t data_pos = pack_path.find("data/");
|
||||
if (data_pos != std::string::npos) {
|
||||
pack_path = pack_path.substr(data_pos + 5); // +5 para saltar "data/"
|
||||
}
|
||||
|
||||
// Remover cualquier prefijo de path absoluto
|
||||
size_t last_data = pack_path.rfind("data/");
|
||||
if (last_data != std::string::npos) {
|
||||
pack_path = pack_path.substr(last_data + 5);
|
||||
}
|
||||
|
||||
return pack_path;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user