c45e524109
Pase automático de clang-tidy --fix sobre el conjunto de checks que son puro transform de sintaxis y no rompen API. Invocado con --format-style=none para que clang-tidy NO arrastre clang-format sobre las líneas tocadas (evita la regla NamespaceIndentation: All del .clang-format reformateando solo trozos del archivo). Checks aplicados: - modernize-use-trailing-return-type (193 hits): 'int foo()' → 'auto foo() -> int'. Estilo coherente con la convención del proyecto. - modernize-use-default-member-init (36 hits): inicialización de miembros pasa de la lista del constructor a la declaración. Reduce duplicación cuando hay varios constructores con los mismos defaults. - modernize-use-auto (6 hits): tipos largos sustituidos por auto donde el tipo es evidente del contexto (new T, dynamic_cast, etc). - modernize-use-starts-ends-with (2 hits): s.rfind(x) == 0 → s.starts_with(x), aprovechando C++20. - performance-enum-size (10 hits): enums pequeños declaran tipo subyacente (uint8_t / similar) para reducir tamaño y precisar layout. NO aplicado en este pase (riesgo de cambios semánticos o de API): - readability-identifier-naming (renames pueden romper callsites parciales) - readability-convert-member-functions-to-static (cambia firma) - readability-use-anyofallof (reescribe loops, side effects) - readability-function-cognitive-complexity (requiere refactor manual) - bugs reales (bugprone-*, clang-diagnostic-*) → uno a uno Cambios manuales asociados: - SDLManager::clear() ahora devuelve bool: propaga el resultado de beginFrame al caller para que Director::runFrameLoop salte draw+present cuando la swapchain no esté disponible (ventana minimizada). Antes la función ignoraba el [[nodiscard]] del beginFrame y los vértices se acumulaban en el batch sin nadie que los consumiera. - vector_text.cpp: borrada la línea suelta "// Test pre-commit hook" que quedó como cruft. clang-tidy crashea en LLVM 19.1 con performance-noexcept-move-constructor (recursión infinita en ExceptionSpecAnalyzer al procesar std::set); check deshabilitado en .clang-tidy con comentario explicativo. Build limpio, smoke test OK. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
144 lines
4.1 KiB
C++
144 lines
4.1 KiB
C++
// resource_loader.cpp - Implementació del carregador de recursos
|
|
// © 2026 JailDesigner
|
|
|
|
#include "resource_loader.hpp"
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
namespace Resource {
|
|
|
|
// Singleton
|
|
auto Loader::get() -> Loader& {
|
|
static Loader instance;
|
|
return instance;
|
|
}
|
|
|
|
// Inicialitzar el sistema de recursos
|
|
auto Loader::initialize(const std::string& pack_file, bool enable_fallback) -> bool {
|
|
fallback_enabled_ = enable_fallback;
|
|
|
|
// Intentar load el paquet
|
|
pack_ = std::make_unique<Pack>();
|
|
|
|
if (!pack_->loadPack(pack_file)) {
|
|
if (!fallback_enabled_) {
|
|
std::cerr << "[ResourceLoader] ERROR FATAL: No es pot load " << pack_file
|
|
<< " y el fallback está desactivat\n";
|
|
return false;
|
|
}
|
|
|
|
std::cout << "[ResourceLoader] Paquet no trobat, usant fallback al sistema de archivos\n";
|
|
pack_.reset(); // No hay paquet
|
|
return true;
|
|
}
|
|
|
|
std::cout << "[ResourceLoader] Paquet carregat: " << pack_file << "\n";
|
|
return true;
|
|
}
|
|
|
|
// Carregar un recurs
|
|
auto Loader::loadResource(const std::string& filename) -> std::vector<uint8_t> {
|
|
// Intentar load del paquet primer
|
|
if (pack_) {
|
|
if (pack_->hasResource(filename)) {
|
|
auto data = pack_->getResource(filename);
|
|
if (!data.empty()) {
|
|
return data;
|
|
}
|
|
std::cerr << "[ResourceLoader] Advertència: recurs buit al paquet: " << filename
|
|
<< "\n";
|
|
}
|
|
|
|
// Si no está al paquet y no hay fallback, falla
|
|
if (!fallback_enabled_) {
|
|
std::cerr << "[ResourceLoader] ERROR: Recurs no trobat al paquet i fallback desactivat: "
|
|
<< filename << "\n";
|
|
return {};
|
|
}
|
|
}
|
|
|
|
// Fallback al sistema de archivos
|
|
if (fallback_enabled_) {
|
|
return loadFromFilesystem(filename);
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
// Comprovar si existeix un recurs
|
|
auto Loader::resourceExists(const std::string& filename) -> bool {
|
|
// Comprovar al paquet
|
|
if (pack_ && pack_->hasResource(filename)) {
|
|
return true;
|
|
}
|
|
|
|
// Comprovar al sistema de archivos si está activat el fallback
|
|
if (fallback_enabled_) {
|
|
std::string fullpath = base_path_.empty() ? "data/" + filename : base_path_ + "/data/" + filename;
|
|
return std::filesystem::exists(fullpath);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Validar el paquet
|
|
auto Loader::validatePack() -> bool {
|
|
if (!pack_) {
|
|
std::cerr << "[ResourceLoader] Advertència: no hay paquet carregat per validar\n";
|
|
return false;
|
|
}
|
|
|
|
return pack_->validatePack();
|
|
}
|
|
|
|
// Comprovar si hay paquet carregat
|
|
auto Loader::isPackLoaded() const -> bool {
|
|
return pack_ != nullptr;
|
|
}
|
|
|
|
// Establir la ruta base
|
|
void Loader::setBasePath(const std::string& path) {
|
|
base_path_ = path;
|
|
std::cout << "[ResourceLoader] Ruta base establerta: " << base_path_ << "\n";
|
|
}
|
|
|
|
// Obtenir la ruta base
|
|
auto Loader::getBasePath() const -> const std::string& {
|
|
return base_path_;
|
|
}
|
|
|
|
// Carregar des del sistema de archivos (fallback)
|
|
auto Loader::loadFromFilesystem(const std::string& filename) -> std::vector<uint8_t> {
|
|
// The filename is already normalized (e.g., "shapes/logo/letra_j.shp")
|
|
// We need to prepend base_path + "data/"
|
|
std::string fullpath;
|
|
|
|
if (base_path_.empty()) {
|
|
fullpath = "data/" + filename;
|
|
} else {
|
|
fullpath = base_path_ + "/data/" + filename;
|
|
}
|
|
|
|
std::ifstream file(fullpath, std::ios::binary | std::ios::ate);
|
|
if (!file) {
|
|
std::cerr << "[ResourceLoader] Error: no es pot obrir " << fullpath << "\n";
|
|
return {};
|
|
}
|
|
|
|
std::streamsize file_size = file.tellg();
|
|
file.seekg(0, std::ios::beg);
|
|
|
|
std::vector<uint8_t> data(file_size);
|
|
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
|
|
std::cerr << "[ResourceLoader] Error: no es pot llegir " << fullpath << "\n";
|
|
return {};
|
|
}
|
|
|
|
std::cout << "[ResourceLoader] Carregat des del sistema de archivos: " << fullpath << "\n";
|
|
return data;
|
|
}
|
|
|
|
} // namespace Resource
|