#include "asset.h" #include // Constructor Asset::Asset(string executablePath) { this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/")); longestName = 0; verbose = true; } // Destructot Asset::~Asset() { } // AƱade un elemento a la lista void Asset::add(string file, enum assetType type, bool required, bool absolute) { item_t temp; temp.file = absolute ? file : executablePath + file; temp.type = type; temp.required = required; fileList.push_back(temp); const string filename = file.substr(file.find_last_of("\\/") + 1); longestName = SDL_max(longestName, filename.size()); } // Devuelve el fichero de un elemento de la lista a partir de una cadena string Asset::get(string text) { for (auto f : fileList) { const size_t lastIndex = f.file.find_last_of("/") + 1; const string file = f.file.substr(lastIndex, string::npos); if (file == text) { return f.file; } } if (verbose) { cout << "Warning: file " << text.c_str() << " not found" << endl; } return ""; } // Comprueba que existen todos los elementos bool Asset::check() { bool success = true; if (verbose) { cout << "\n** Checking files" << endl; cout << "Executable path is: " << executablePath << endl; cout << "Sample filepath: " << fileList.back().file << endl; } // Comprueba la lista de ficheros clasificandolos por tipo for (int type = 0; type < t_maxAssetType; ++type) { // Comprueba si hay ficheros de ese tipo bool any = false; for (auto f : fileList) { if ((f.required) && (f.type == type)) { any = true; } } // Si hay ficheros de ese tipo, comprueba si existen if (any) { if (verbose) { cout << "\n>> " << getTypeName(type).c_str() << " FILES" << endl; } for (auto f : fileList) { if ((f.required) && (f.type == type)) { success &= checkFile(f.file); } } } } // Resultado if (verbose) { if (success) { cout << "\n** All files OK.\n" << endl; } else { cout << "\n** A file is missing. Exiting.\n" << endl; } } return success; } // Comprueba que existe un fichero bool Asset::checkFile(string path) { bool success = false; string result = "ERROR"; // Comprueba si existe el fichero const string filename = path.substr(path.find_last_of("\\/") + 1); SDL_RWops *file = SDL_RWFromFile(path.c_str(), "rb"); if (file != nullptr) { result = "OK"; success = true; SDL_RWclose(file); } if (verbose) { cout.setf(ios::left, ios::adjustfield); cout << "Checking file: "; cout.width(longestName + 2); cout.fill('.'); cout << filename + " "; cout << " [" + result + "]" << endl; } return success; } // Devuelve el nombre del tipo de recurso string Asset::getTypeName(int type) { switch (type) { case t_bitmap: return "BITMAP"; break; case t_music: return "MUSIC"; break; case t_sound: return "SOUND"; break; case t_font: return "FONT"; break; case t_lang: return "LANG"; break; case t_data: return "DATA"; break; case t_room: return "ROOM"; break; case t_enemy: return "ENEMY"; break; case t_item: return "ITEM"; break; default: return "ERROR"; break; } } // Establece si ha de mostrar texto por pantalla void Asset::setVerbose(bool value) { verbose = value; }