eliminat el checkFiles inicial que a jaildoc li molestava

This commit is contained in:
2025-11-21 13:34:20 +01:00
parent b475c4d818
commit 1fed8f63bd
9 changed files with 233 additions and 142 deletions

View File

@@ -176,6 +176,15 @@ auto Cache::getRooms() -> std::vector<RoomResource>& {
return rooms_;
}
// Helper para lanzar errores de carga con formato consistente
[[noreturn]] void Cache::throwLoadError(const std::string& asset_type, const std::string& file_path, const std::exception& e) {
std::cerr << "\n[ ERROR ] Failed to load " << asset_type << ": " << getFileName(file_path) << '\n';
std::cerr << "[ ERROR ] Path: " << file_path << '\n';
std::cerr << "[ ERROR ] Reason: " << e.what() << '\n';
std::cerr << "[ ERROR ] Check config/assets.yaml configuration\n";
throw;
}
// Carga los sonidos
void Cache::loadSounds() {
std::cout << "\n>> SOUND FILES" << '\n';
@@ -183,23 +192,31 @@ void Cache::loadSounds() {
sounds_.clear();
for (const auto& l : list) {
auto name = getFileName(l);
JA_Sound_t* sound = nullptr;
try {
auto name = getFileName(l);
JA_Sound_t* sound = nullptr;
// Try loading from resource pack first
auto audio_data = Helper::loadFile(l);
if (!audio_data.empty()) {
sound = JA_LoadSound(audio_data.data(), static_cast<Uint32>(audio_data.size()));
// Try loading from resource pack first
auto audio_data = Helper::loadFile(l);
if (!audio_data.empty()) {
sound = JA_LoadSound(audio_data.data(), static_cast<Uint32>(audio_data.size()));
}
// Fallback to file path if memory loading failed
if (sound == nullptr) {
sound = JA_LoadSound(l.c_str());
}
if (sound == nullptr) {
throw std::runtime_error("Failed to decode audio file");
}
sounds_.emplace_back(SoundResource{.name = name, .sound = sound});
printWithDots("Sound : ", name, "[ LOADED ]");
updateLoadingProgress();
} catch (const std::exception& e) {
throwLoadError("SOUND", l, e);
}
// Fallback to file path if memory loading failed
if (sound == nullptr) {
sound = JA_LoadSound(l.c_str());
}
sounds_.emplace_back(SoundResource{.name = name, .sound = sound});
printWithDots("Sound : ", name, "[ LOADED ]");
updateLoadingProgress();
}
}
@@ -210,23 +227,31 @@ void Cache::loadMusics() {
musics_.clear();
for (const auto& l : list) {
auto name = getFileName(l);
JA_Music_t* music = nullptr;
try {
auto name = getFileName(l);
JA_Music_t* music = nullptr;
// Try loading from resource pack first
auto audio_data = Helper::loadFile(l);
if (!audio_data.empty()) {
music = JA_LoadMusic(audio_data.data(), static_cast<Uint32>(audio_data.size()));
// Try loading from resource pack first
auto audio_data = Helper::loadFile(l);
if (!audio_data.empty()) {
music = JA_LoadMusic(audio_data.data(), static_cast<Uint32>(audio_data.size()));
}
// Fallback to file path if memory loading failed
if (music == nullptr) {
music = JA_LoadMusic(l.c_str());
}
if (music == nullptr) {
throw std::runtime_error("Failed to decode music file");
}
musics_.emplace_back(MusicResource{.name = name, .music = music});
printWithDots("Music : ", name, "[ LOADED ]");
updateLoadingProgress(1);
} catch (const std::exception& e) {
throwLoadError("MUSIC", l, e);
}
// Fallback to file path if memory loading failed
if (music == nullptr) {
music = JA_LoadMusic(l.c_str());
}
musics_.emplace_back(MusicResource{.name = name, .music = music});
printWithDots("Music : ", name, "[ LOADED ]");
updateLoadingProgress(1);
}
}
@@ -237,10 +262,14 @@ void Cache::loadSurfaces() {
surfaces_.clear();
for (const auto& l : list) {
auto name = getFileName(l);
surfaces_.emplace_back(SurfaceResource{.name = name, .surface = std::make_shared<Surface>(l)});
surfaces_.back().surface->setTransparentColor(0);
updateLoadingProgress();
try {
auto name = getFileName(l);
surfaces_.emplace_back(SurfaceResource{.name = name, .surface = std::make_shared<Surface>(l)});
surfaces_.back().surface->setTransparentColor(0);
updateLoadingProgress();
} catch (const std::exception& e) {
throwLoadError("BITMAP", l, e);
}
}
// Reconfigura el color transparente de algunas surfaces
@@ -260,9 +289,13 @@ void Cache::loadPalettes() {
palettes_.clear();
for (const auto& l : list) {
auto name = getFileName(l);
palettes_.emplace_back(ResourcePalette{.name = name, .palette = readPalFile(l)});
updateLoadingProgress();
try {
auto name = getFileName(l);
palettes_.emplace_back(ResourcePalette{.name = name, .palette = readPalFile(l)});
updateLoadingProgress();
} catch (const std::exception& e) {
throwLoadError("PALETTE", l, e);
}
}
}
@@ -273,9 +306,13 @@ void Cache::loadTextFiles() {
text_files_.clear();
for (const auto& l : list) {
auto name = getFileName(l);
text_files_.emplace_back(TextFileResource{.name = name, .text_file = Text::loadTextFile(l)});
updateLoadingProgress();
try {
auto name = getFileName(l);
text_files_.emplace_back(TextFileResource{.name = name, .text_file = Text::loadTextFile(l)});
updateLoadingProgress();
} catch (const std::exception& e) {
throwLoadError("FONT", l, e);
}
}
}
@@ -286,14 +323,22 @@ void Cache::loadAnimations() {
animations_.clear();
for (const auto& l : list) {
auto name = getFileName(l);
try {
auto name = getFileName(l);
// Cargar bytes del archivo YAML sin parsear (carga lazy)
auto yaml_bytes = Helper::loadFile(l);
// Cargar bytes del archivo YAML sin parsear (carga lazy)
auto yaml_bytes = Helper::loadFile(l);
animations_.emplace_back(AnimationResource{.name = name, .yaml_data = yaml_bytes});
printWithDots("Animation : ", name, "[ LOADED ]");
updateLoadingProgress();
if (yaml_bytes.empty()) {
throw std::runtime_error("File is empty or could not be loaded");
}
animations_.emplace_back(AnimationResource{.name = name, .yaml_data = yaml_bytes});
printWithDots("Animation : ", name, "[ LOADED ]");
updateLoadingProgress();
} catch (const std::exception& e) {
throwLoadError("ANIMATION", l, e);
}
}
}
@@ -304,10 +349,14 @@ void Cache::loadRooms() {
rooms_.clear();
for (const auto& l : list) {
auto name = getFileName(l);
rooms_.emplace_back(RoomResource{.name = name, .room = std::make_shared<Room::Data>(Room::loadYAML(l))});
printWithDots("Room : ", name, "[ LOADED ]");
updateLoadingProgress();
try {
auto name = getFileName(l);
rooms_.emplace_back(RoomResource{.name = name, .room = std::make_shared<Room::Data>(Room::loadYAML(l))});
printWithDots("Room : ", name, "[ LOADED ]");
updateLoadingProgress();
} catch (const std::exception& e) {
throwLoadError("ROOM", l, e);
}
}
}

View File

@@ -66,6 +66,9 @@ class Cache {
static void checkEvents();
void updateLoadingProgress(int steps = 5);
// Helper para mensajes de error de carga
[[noreturn]] static void throwLoadError(const std::string& asset_type, const std::string& file_path, const std::exception& e);
// Constructor y destructor
Cache();
~Cache() = default;

View File

@@ -200,61 +200,6 @@ auto List::exists(const std::string& filename) const -> bool {
return file_list_.find(filename) != file_list_.end();
}
// Comprueba que existen todos los elementos
auto List::check() const -> bool {
bool success = true;
std::cout << "\n** CHECKING FILES" << '\n';
// Agrupar por tipo para mostrar organizado
std::unordered_map<Type, std::vector<const Item*>> by_type;
for (const auto& [filename, item] : file_list_) {
if (item.required) {
by_type[item.type].push_back(&item);
}
}
// Verificar por tipo
for (int type = 0; type < static_cast<int>(Type::SIZE); ++type) {
Type asset_type = static_cast<Type>(type);
if (by_type.find(asset_type) != by_type.end()) {
std::cout << "\n>> " << getTypeName(asset_type) << " FILES" << '\n';
bool type_success = true;
for (const auto* item : by_type[asset_type]) {
if (!checkFile(item->file)) {
success = false;
type_success = false;
}
}
if (type_success) {
std::cout << " All files are OK." << '\n';
}
}
}
// Resultado
std::cout << (success ? "\n** CHECKING FILES COMPLETED.\n" : "\n** CHECKING FILES FAILED.\n") << '\n';
return success;
}
// Comprueba que existe un fichero
auto List::checkFile(const std::string& path) -> bool {
std::ifstream file(path);
bool success = file.good();
file.close();
if (!success) {
printWithDots("Checking file : ", getFileName(path), "[ ERROR ]");
}
return success;
}
// Parsea string a Type
auto List::parseAssetType(const std::string& type_str) -> Type {
if (type_str == "DATA") {

View File

@@ -37,7 +37,6 @@ class List {
void loadFromString(const std::string& config_content, const std::string& prefix = "", const std::string& system_folder = ""); // Para cargar desde pack (release)
[[nodiscard]] auto get(const std::string& filename) const -> std::string; // Obtiene la ruta completa
[[nodiscard]] auto loadData(const std::string& filename) const -> std::vector<uint8_t>; // Carga datos del archivo
[[nodiscard]] auto check() const -> bool;
[[nodiscard]] auto getListByType(Type type) const -> std::vector<std::string>;
[[nodiscard]] auto exists(const std::string& filename) const -> bool; // Verifica si un asset existe
@@ -59,7 +58,6 @@ class List {
std::string executable_path_; // Ruta del ejecutable
// --- Métodos internos ---
[[nodiscard]] static auto checkFile(const std::string& path) -> bool; // Verifica si un archivo existe
[[nodiscard]] static auto getTypeName(Type type) -> std::string; // Obtiene el nombre del tipo
[[nodiscard]] static auto parseAssetType(const std::string& type_str) -> Type; // Convierte string a tipo
void addToMap(const std::string& file_path, Type type, bool required, bool absolute); // Añade archivo al mapa

View File

@@ -107,10 +107,9 @@ Director::Director(std::vector<std::string> const& args) {
// 1. Initialize Asset system from filesystem
Resource::List::init(executable_path_);
// 2. Load and verify assets from disk
if (!setFileList()) {
exit(EXIT_FAILURE);
}
// 2. Load asset configuration from disk
// Note: Asset verification happens during Resource::Cache::load()
setFileList();
// 3. Initialize resource pack system (optional, with fallback)
std::cout << "Initializing resource pack (development mode): " << pack_path << '\n';
@@ -259,8 +258,8 @@ void Director::createSystemFolder(const std::string& folder) {
}
}
// Crea el indice de ficheros
auto Director::setFileList() -> bool {
// Carga la configuración de assets desde assets.yaml
void Director::setFileList() {
// Determinar el prefijo de ruta según la plataforma
#ifdef MACOS_BUNDLE
const std::string PREFIX = "/../Resources";
@@ -272,10 +271,8 @@ auto Director::setFileList() -> bool {
std::string config_path = executable_path_ + PREFIX + "/config/assets.yaml";
// Cargar todos los assets desde el archivo de configuración
// La verificación de existencia de archivos se realiza durante Resource::Cache::load()
Resource::List::get()->loadFromFile(config_path, PREFIX, system_folder_);
// Verificar que todos los assets requeridos existen
return Resource::List::get()->check();
}
// Ejecuta la seccion de juego con el logo

View File

@@ -19,7 +19,7 @@ class Director {
// --- Funciones ---
void createSystemFolder(const std::string& folder); // Crea la carpeta del sistema donde guardar datos
auto setFileList() -> bool; // Crea el indice de ficheros
void setFileList(); // Carga la configuración de assets desde assets.yaml
static void runLogo(); // Ejecuta la seccion de juego con el logo
static void runLoadingScreen(); // Ejecuta la seccion de juego de la pantalla de carga
static void runTitle(); // Ejecuta la seccion de juego con el titulo y los menus