Files
jaildoctors_dilemma/source/utils/asset.cpp

160 lines
3.1 KiB
C++

#include "asset.h"
// Constructor
Asset::Asset(std::string path)
{
executablePath = path;
longestName = 0;
}
// Destructor
Asset::~Asset()
{
}
// Añade un elemento a la lista
void Asset::add(std::string file, enum assetType type, bool required)
{
item_t temp;
temp.file = executablePath + "/.." + file;
temp.type = type;
temp.required = required;
fileList.push_back(temp);
const std::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
std::string Asset::get(std::string text)
{
for (auto f : fileList)
{
if (f.file.find(text) != std::string::npos)
{
return f.file;
}
}
printf("Warning: file %s not found\n", text.c_str());
return "";
}
// Comprueba que existen todos los elementos
bool Asset::check()
{
bool success = true;
printf("\n** Checking files.\n");
// 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)
{
printf("\n>> %s FILES\n", getTypeName(type).c_str());
for (auto f : fileList)
{
if ((f.required) && (f.type == type))
{
success &= checkFile(f.file);
}
}
}
}
// Resultado
if (success)
{
printf("\n** All files OK.\n\n");
}
else
{
printf("\n** A file is missing. Exiting.\n\n");
}
return success;
}
// Comprueba que existe un fichero
bool Asset::checkFile(std::string path)
{
bool success = false;
std::string result = "ERROR";
// Comprueba si existe el fichero
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "r+b");
if (file != nullptr)
{
result = "OK";
success = true;
SDL_RWclose(file);
}
const std::string s = "Checking file %-" + std::to_string(longestName) + "s [" + result + "]\n";
printf(s.c_str(), filename.c_str());
return success;
}
// Devuelve el nombre del tipo de recurso
std::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;
}
}