Files
orni-attack/source/core/resources/resource_helper.cpp
T

86 lines
2.5 KiB
C++

// resource_helper.cpp - Implementació de funciones de ajuda
// © 2026 JailDesigner
#include "resource_helper.hpp"
#include <algorithm>
#include "resource_loader.hpp"
namespace Resource::Helper {
// Inicialitzar el sistema de recursos
auto initializeResourceSystem(const std::string& pack_file, bool fallback) -> bool {
return Loader::get().initialize(pack_file, fallback);
}
// Carregar un file
auto loadFile(const std::string& filepath) -> std::vector<uint8_t> {
// Normalitzar la ruta
std::string normalized = normalizePath(filepath);
// Carregar del sistema de recursos
return Loader::get().loadResource(normalized);
}
// Llistar recursos amb un prefix donat
auto listResources(const std::string& prefix) -> std::vector<std::string> {
return Loader::get().listResources(prefix);
}
// Comprovar si existeix un file
auto fileExists(const std::string& filepath) -> bool {
std::string normalized = normalizePath(filepath);
return Loader::get().resourceExists(normalized);
}
// Obtenir ruta normalitzada per al paquet
// Elimina prefixos "data/", rutes absolutes, etc.
auto getPackPath(const std::string& asset_path) -> std::string {
std::string path = asset_path;
// Eliminar rutes absolutes (detectar / o C:\ al principi)
if (!path.empty() && path[0] == '/') {
// Buscar "data/" i agafar el que ve después
size_t data_pos = path.find("/data/");
if (data_pos != std::string::npos) {
path = path.substr(data_pos + 6); // Saltar "/data/"
}
}
// Eliminar "./" i "../" del principi
while (path.starts_with("./")) {
path = path.substr(2);
}
while (path.starts_with("../")) {
path = path.substr(3);
}
// Eliminar "data/" del principi
if (path.starts_with("data/")) {
path = path.substr(5);
}
// Eliminar "Resources/" (macOS bundles)
if (path.starts_with("Resources/")) {
path = path.substr(10);
}
// Convertir barres invertides a normals
std::ranges::replace(path, '\\', '/');
return path;
}
// Normalitzar ruta (alias de getPackPath)
auto normalizePath(const std::string& path) -> std::string {
return getPackPath(path);
}
// Comprovar si hay paquet carregat
auto isPackLoaded() -> bool {
return Loader::get().isPackLoaded();
}
} // namespace Resource::Helper