Asset: passat a unordered_map i carrega desde fitxer
This commit is contained in:
@@ -1,64 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include <string> // Para string
|
||||
#include <utility> // Para move
|
||||
#include <vector> // Para vector
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
// Tipos de recursos gestionados por Asset
|
||||
enum class AssetType : int {
|
||||
BITMAP,
|
||||
MUSIC,
|
||||
SOUND,
|
||||
FONT,
|
||||
LANG,
|
||||
DATA,
|
||||
DEMODATA,
|
||||
ANIMATION,
|
||||
PALETTE,
|
||||
COUNT,
|
||||
};
|
||||
|
||||
// Clase Asset: gestor de recursos (singleton)
|
||||
class Asset { // Gestor de recursos (singleton)
|
||||
// Clase Asset: gestor optimizado de recursos (singleton)
|
||||
class Asset {
|
||||
public:
|
||||
// Tipos de recursos
|
||||
enum class Type : int {
|
||||
BITMAP,
|
||||
MUSIC,
|
||||
SOUND,
|
||||
FONT,
|
||||
LANG,
|
||||
DATA,
|
||||
DEMODATA,
|
||||
ANIMATION,
|
||||
PALETTE,
|
||||
SIZE,
|
||||
};
|
||||
|
||||
// --- Métodos de singleton ---
|
||||
static void init(const std::string &executable_path); // Inicializa el objeto Asset
|
||||
static void destroy(); // Libera el objeto Asset
|
||||
static auto get() -> Asset *; // Obtiene el puntero al objeto Asset
|
||||
Asset(const Asset &) = delete; // No se permite copiar
|
||||
auto operator=(const Asset &) -> Asset & = delete; // No se permite asignar
|
||||
static void init(const std::string &executable_path);
|
||||
static void destroy();
|
||||
static auto get() -> Asset *;
|
||||
Asset(const Asset &) = delete;
|
||||
auto operator=(const Asset &) -> Asset & = delete;
|
||||
|
||||
// --- Métodos para la gestión de recursos ---
|
||||
void add(const std::string &file, AssetType type, bool required = true, bool absolute = false); // Añade un recurso a la lista
|
||||
[[nodiscard]] auto get(const std::string &text) const -> std::string; // Obtiene la ruta completa de un recurso a partir de su nombre
|
||||
[[nodiscard]] auto check() const -> bool; // Verifica la existencia de todos los recursos requeridos
|
||||
[[nodiscard]] auto getListByType(AssetType type) const -> std::vector<std::string>; // Devuelve una lista de archivos de un tipo concreto
|
||||
void add(const std::string &file_path, Type type, bool required = true, bool absolute = false);
|
||||
void loadFromFile(const std::string &config_file_path, const std::string &prefix = "", const std::string &system_folder = ""); // Con soporte para variables
|
||||
[[nodiscard]] auto get(const std::string &filename) const -> std::string; // Mantener nombre original
|
||||
[[nodiscard]] auto check() const -> bool;
|
||||
[[nodiscard]] auto getListByType(Type type) const -> std::vector<std::string>;
|
||||
[[nodiscard]] auto exists(const std::string &filename) const -> bool; // Nueva función para verificar existencia
|
||||
|
||||
private:
|
||||
// --- Estructura interna para almacenar información de cada recurso ---
|
||||
struct AssetItem { // Estructura para cada recurso
|
||||
std::string file; // Ruta del fichero desde la raíz del directorio
|
||||
AssetType type; // Tipo de recurso
|
||||
bool required; // Indica si el fichero es obligatorio
|
||||
struct Item {
|
||||
std::string file; // Ruta completa del archivo (mantener nombre original)
|
||||
Type type; // Tipo de recurso
|
||||
bool required; // Indica si el archivo es obligatorio
|
||||
|
||||
AssetItem(std::string file_path, AssetType asset_type, bool is_required)
|
||||
: file(std::move(file_path)), type(asset_type), required(is_required) {} // Constructor
|
||||
Item(std::string path, Type asset_type, bool is_required)
|
||||
: file(std::move(path)), type(asset_type), required(is_required) {}
|
||||
};
|
||||
|
||||
// --- Variables internas ---
|
||||
int longest_name_ = 0; // Longitud del nombre más largo
|
||||
std::vector<AssetItem> file_list_; // Lista con todas las rutas de recursos
|
||||
std::string executable_path_; // Ruta del ejecutable
|
||||
std::unordered_map<std::string, Item> file_list_; // Mapa para búsqueda O(1) (mantener nombre original)
|
||||
std::string executable_path_; // Ruta del ejecutable
|
||||
|
||||
// --- Métodos internos ---
|
||||
[[nodiscard]] static auto checkFile(const std::string &path) -> bool; // Verifica si un archivo existe (interno)
|
||||
[[nodiscard]] static auto getTypeName(AssetType type) -> std::string; // Devuelve el nombre textual del tipo de recurso (interno)
|
||||
[[nodiscard]] static auto checkFile(const std::string &path) -> bool;
|
||||
[[nodiscard]] static auto getTypeName(Type type) -> std::string;
|
||||
[[nodiscard]] static auto parseAssetType(const std::string &type_str) -> Type;
|
||||
void addToMap(const std::string &file_path, Type type, bool required, bool absolute);
|
||||
[[nodiscard]] static auto replaceVariables(const std::string &path, const std::string &prefix, const std::string &system_folder) -> std::string;
|
||||
static auto parseOptions(const std::string &options, bool &required, bool &absolute) -> void;
|
||||
|
||||
// --- Patrón Singleton ---
|
||||
explicit Asset(std::string executable_path)
|
||||
: executable_path_(std::move(executable_path)) {} // Constructor privado
|
||||
~Asset() = default; // Destructor
|
||||
: executable_path_(std::move(executable_path)) {}
|
||||
~Asset() = default;
|
||||
|
||||
// --- Singleton ---
|
||||
static Asset *instance; // Instancia singleton
|
||||
static Asset *instance;
|
||||
};
|
||||
Reference in New Issue
Block a user