neteja tidy a source/core i encamina Texture::loadFromFile pel ResourceHelper
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#include "core/resources/asset.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
#include <cstddef> // for size_t
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
||||
|
||||
#include "core/resources/resource_helper.h"
|
||||
@@ -26,9 +26,7 @@ auto Asset::get() -> Asset * {
|
||||
|
||||
// Constructor
|
||||
Asset::Asset(const std::string &executablePath)
|
||||
: longestName(0),
|
||||
executablePath(executablePath.substr(0, executablePath.find_last_of("\\/"))),
|
||||
verbose(true) {
|
||||
: executablePath(executablePath.substr(0, executablePath.find_last_of("\\/"))) {
|
||||
}
|
||||
|
||||
// Añade un elemento a la lista
|
||||
@@ -44,10 +42,10 @@ void Asset::add(const std::string &file, enum assetType type, bool required, boo
|
||||
}
|
||||
|
||||
// Devuelve el fichero de un elemento de la lista a partir de una cadena
|
||||
std::string Asset::get(const std::string &text) {
|
||||
auto Asset::get(const std::string &text) -> std::string {
|
||||
for (const auto &f : fileList) {
|
||||
const size_t lastIndex = f.file.find_last_of("/") + 1;
|
||||
const std::string file = f.file.substr(lastIndex, std::string::npos);
|
||||
const size_t lastIndex = f.file.find_last_of('/') + 1;
|
||||
const std::string file = f.file.substr(lastIndex);
|
||||
|
||||
if (file == text) {
|
||||
return f.file;
|
||||
@@ -55,20 +53,20 @@ std::string Asset::get(const std::string &text) {
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
|
||||
std::cout << "Warning: file " << text.c_str() << " not found" << '\n';
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Comprueba que existen todos los elementos
|
||||
bool Asset::check() {
|
||||
auto Asset::check() -> bool {
|
||||
bool success = true;
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "\n** Checking files" << std::endl;
|
||||
std::cout << "\n** Checking files" << '\n';
|
||||
|
||||
std::cout << "Executable path is: " << executablePath << std::endl;
|
||||
std::cout << "Sample filepath: " << fileList.back().file << std::endl;
|
||||
std::cout << "Executable path is: " << executablePath << '\n';
|
||||
std::cout << "Sample filepath: " << fileList.back().file << '\n';
|
||||
}
|
||||
|
||||
// Comprueba la lista de ficheros clasificandolos por tipo
|
||||
@@ -85,7 +83,7 @@ bool Asset::check() {
|
||||
// Si hay ficheros de ese tipo, comprueba si existen
|
||||
if (any) {
|
||||
if (verbose) {
|
||||
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << std::endl;
|
||||
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << '\n';
|
||||
}
|
||||
|
||||
for (const auto &f : fileList) {
|
||||
@@ -100,10 +98,10 @@ bool Asset::check() {
|
||||
if (verbose) {
|
||||
if (success) {
|
||||
std::cout << "\n** All files OK.\n"
|
||||
<< std::endl;
|
||||
<< '\n';
|
||||
} else {
|
||||
std::cout << "\n** A file is missing. Exiting.\n"
|
||||
<< std::endl;
|
||||
<< '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +109,7 @@ bool Asset::check() {
|
||||
}
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
bool Asset::checkFile(const std::string &path) {
|
||||
auto Asset::checkFile(const std::string &path) const -> bool {
|
||||
bool success = false;
|
||||
std::string result = "ERROR";
|
||||
|
||||
@@ -138,14 +136,14 @@ bool Asset::checkFile(const std::string &path) {
|
||||
std::cout.width(longestName + 2);
|
||||
std::cout.fill('.');
|
||||
std::cout << filename + " ";
|
||||
std::cout << " [" + result + "]" << std::endl;
|
||||
std::cout << " [" + result + "]" << '\n';
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Devuelve el nombre del tipo de recurso
|
||||
std::string Asset::getTypeName(int type) {
|
||||
auto Asset::getTypeName(int type) -> std::string {
|
||||
switch (type) {
|
||||
case t_bitmap:
|
||||
return "BITMAP";
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <string> // for string, basic_string
|
||||
#include <vector> // for vector
|
||||
#include <cstdint> // for uint8_t
|
||||
#include <string> // for string, basic_string
|
||||
#include <vector> // for vector
|
||||
|
||||
enum assetType {
|
||||
enum assetType : std::uint8_t {
|
||||
t_bitmap,
|
||||
t_music,
|
||||
t_sound,
|
||||
@@ -28,16 +29,16 @@ class Asset {
|
||||
|
||||
private:
|
||||
// Variables
|
||||
int longestName; // Contiene la longitud del nombre de fichero mas largo
|
||||
int longestName{0}; // Contiene la longitud del nombre de fichero mas largo
|
||||
std::vector<item_t> fileList; // Listado con todas las rutas a los ficheros
|
||||
std::string executablePath; // Ruta al ejecutable
|
||||
bool verbose; // Indica si ha de mostrar información por pantalla
|
||||
bool verbose{true}; // Indica si ha de mostrar información por pantalla
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
bool checkFile(const std::string &executablePath);
|
||||
[[nodiscard]] auto checkFile(const std::string &executablePath) const -> bool;
|
||||
|
||||
// Devuelve el nombre del tipo de recurso
|
||||
std::string getTypeName(int type);
|
||||
static auto getTypeName(int type) -> std::string;
|
||||
|
||||
// Constructor privado (usar Asset::init)
|
||||
explicit Asset(const std::string &path);
|
||||
@@ -55,13 +56,13 @@ class Asset {
|
||||
void add(const std::string &file, enum assetType type, bool required = true, bool absolute = false);
|
||||
|
||||
// Devuelve un elemento de la lista a partir de una cadena
|
||||
std::string get(const std::string &text);
|
||||
auto get(const std::string &text) -> std::string;
|
||||
|
||||
// Devuelve toda la lista de items registrados
|
||||
const std::vector<item_t> &getAll() const { return fileList; }
|
||||
[[nodiscard]] auto getAll() const -> const std::vector<item_t> & { return fileList; }
|
||||
|
||||
// Comprueba que existen todos los elementos
|
||||
bool check();
|
||||
auto check() -> bool;
|
||||
|
||||
// Establece si ha de mostrar texto por pantalla
|
||||
void setVerbose(bool value);
|
||||
|
||||
@@ -17,14 +17,16 @@
|
||||
|
||||
Resource *Resource::instance_ = nullptr;
|
||||
|
||||
static std::string basename(const std::string &path) {
|
||||
static auto basename(const std::string &path) -> std::string {
|
||||
return path.substr(path.find_last_of("\\/") + 1);
|
||||
}
|
||||
|
||||
static std::string stem(const std::string &path) {
|
||||
static auto stem(const std::string &path) -> std::string {
|
||||
std::string b = basename(path);
|
||||
size_t dot = b.find_last_of('.');
|
||||
if (dot == std::string::npos) return b;
|
||||
if (dot == std::string::npos) {
|
||||
return b;
|
||||
}
|
||||
return b.substr(0, dot);
|
||||
}
|
||||
|
||||
@@ -40,7 +42,7 @@ void Resource::destroy() {
|
||||
instance_ = nullptr;
|
||||
}
|
||||
|
||||
Resource *Resource::get() {
|
||||
auto Resource::get() -> Resource * {
|
||||
return instance_;
|
||||
}
|
||||
|
||||
@@ -48,19 +50,29 @@ Resource::Resource(SDL_Renderer *renderer)
|
||||
: renderer_(renderer) {}
|
||||
|
||||
Resource::~Resource() {
|
||||
for (auto &[name, m] : menus_) delete m;
|
||||
for (auto &[name, m] : menus_) {
|
||||
delete m;
|
||||
}
|
||||
menus_.clear();
|
||||
|
||||
for (auto &[name, t] : texts_) delete t;
|
||||
for (auto &[name, t] : texts_) {
|
||||
delete t;
|
||||
}
|
||||
texts_.clear();
|
||||
|
||||
for (auto &[name, t] : textures_) delete t;
|
||||
for (auto &[name, t] : textures_) {
|
||||
delete t;
|
||||
}
|
||||
textures_.clear();
|
||||
|
||||
for (auto &[name, s] : sounds_) JA_DeleteSound(s);
|
||||
for (auto &[name, s] : sounds_) {
|
||||
JA_DeleteSound(s);
|
||||
}
|
||||
sounds_.clear();
|
||||
|
||||
for (auto &[name, m] : musics_) JA_DeleteMusic(m);
|
||||
for (auto &[name, m] : musics_) {
|
||||
JA_DeleteMusic(m);
|
||||
}
|
||||
musics_.clear();
|
||||
}
|
||||
|
||||
@@ -74,7 +86,9 @@ void Resource::preloadAll() {
|
||||
continue;
|
||||
}
|
||||
auto bytes = ResourceHelper::loadFile(it.file);
|
||||
if (bytes.empty()) continue;
|
||||
if (bytes.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::string bname = basename(it.file);
|
||||
|
||||
@@ -86,12 +100,16 @@ void Resource::preloadAll() {
|
||||
}
|
||||
case t_sound: {
|
||||
JA_Sound_t *s = JA_LoadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
if (s) sounds_[bname] = s;
|
||||
if (s != nullptr) {
|
||||
sounds_[bname] = s;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case t_music: {
|
||||
JA_Music_t *m = JA_LoadMusic(bytes.data(), (Uint32)bytes.size());
|
||||
if (m) musics_[bname] = m;
|
||||
if (m != nullptr) {
|
||||
musics_[bname] = m;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case t_data: {
|
||||
@@ -103,7 +121,9 @@ void Resource::preloadAll() {
|
||||
while (std::getline(ss, line)) {
|
||||
// Normalitza CRLF perquè loadFromVector compari línies amb literals
|
||||
// ("[animation]", "[/animation]") sense \r residual.
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
if (!line.empty() && line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
lines.push_back(line);
|
||||
}
|
||||
animationLines_[bname] = std::move(lines);
|
||||
@@ -114,12 +134,8 @@ void Resource::preloadAll() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case t_font:
|
||||
// Fonts: se emparejan en pass 2
|
||||
break;
|
||||
case t_lang:
|
||||
// Lenguaje: lo sigue leyendo la clase Lang a través de ResourceHelper
|
||||
break;
|
||||
case t_font: // Fonts: se emparejan en pass 2
|
||||
case t_lang: // Lenguaje: lo sigue leyendo la clase Lang via ResourceHelper
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -131,9 +147,13 @@ void Resource::preloadAll() {
|
||||
std::unordered_map<std::string, std::vector<uint8_t>> fontPngs;
|
||||
std::unordered_map<std::string, std::vector<uint8_t>> fontTxts;
|
||||
for (const auto &it : items) {
|
||||
if (it.type != t_font) continue;
|
||||
if (it.type != t_font) {
|
||||
continue;
|
||||
}
|
||||
auto bytes = ResourceHelper::loadFile(it.file);
|
||||
if (bytes.empty()) continue;
|
||||
if (bytes.empty()) {
|
||||
continue;
|
||||
}
|
||||
const std::string s = stem(it.file);
|
||||
const std::string bname = basename(it.file);
|
||||
if (bname.size() >= 4 && bname.substr(bname.size() - 4) == ".png") {
|
||||
@@ -144,7 +164,9 @@ void Resource::preloadAll() {
|
||||
}
|
||||
for (const auto &[s, png] : fontPngs) {
|
||||
auto itTxt = fontTxts.find(s);
|
||||
if (itTxt == fontTxts.end()) continue;
|
||||
if (itTxt == fontTxts.end()) {
|
||||
continue;
|
||||
}
|
||||
Text *t = new Text(png, itTxt->second, renderer_);
|
||||
texts_[s] = t;
|
||||
}
|
||||
@@ -154,11 +176,17 @@ void Resource::preloadAll() {
|
||||
// requiere que Menu se adapte a cargar desde ResourceHelper. Por ahora
|
||||
// lo dejamos así y será una migración del paso 7.
|
||||
for (const auto &it : items) {
|
||||
if (it.type != t_data) continue;
|
||||
if (it.type != t_data) {
|
||||
continue;
|
||||
}
|
||||
const std::string bname = basename(it.file);
|
||||
if (bname.size() < 4 || bname.substr(bname.size() - 4) != ".men") continue;
|
||||
if (bname.size() < 4 || bname.substr(bname.size() - 4) != ".men") {
|
||||
continue;
|
||||
}
|
||||
auto bytes = ResourceHelper::loadFile(it.file);
|
||||
if (bytes.empty()) continue;
|
||||
if (bytes.empty()) {
|
||||
continue;
|
||||
}
|
||||
Menu *m = new Menu(renderer_, "");
|
||||
m->loadFromBytes(bytes, bname);
|
||||
const std::string s = stem(it.file);
|
||||
@@ -166,7 +194,7 @@ void Resource::preloadAll() {
|
||||
}
|
||||
}
|
||||
|
||||
Texture *Resource::getTexture(const std::string &name) {
|
||||
auto Resource::getTexture(const std::string &name) -> Texture * {
|
||||
auto it = textures_.find(name);
|
||||
if (it == textures_.end()) {
|
||||
std::cerr << "Resource::getTexture: missing " << name << '\n';
|
||||
@@ -175,7 +203,7 @@ Texture *Resource::getTexture(const std::string &name) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
JA_Sound_t *Resource::getSound(const std::string &name) {
|
||||
auto Resource::getSound(const std::string &name) -> JA_Sound_t * {
|
||||
auto it = sounds_.find(name);
|
||||
if (it == sounds_.end()) {
|
||||
std::cerr << "Resource::getSound: missing " << name << '\n';
|
||||
@@ -184,7 +212,7 @@ JA_Sound_t *Resource::getSound(const std::string &name) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
JA_Music_t *Resource::getMusic(const std::string &name) {
|
||||
auto Resource::getMusic(const std::string &name) -> JA_Music_t * {
|
||||
auto it = musics_.find(name);
|
||||
if (it == musics_.end()) {
|
||||
std::cerr << "Resource::getMusic: missing " << name << '\n';
|
||||
@@ -193,7 +221,7 @@ JA_Music_t *Resource::getMusic(const std::string &name) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::vector<std::string> &Resource::getAnimationLines(const std::string &name) {
|
||||
auto Resource::getAnimationLines(const std::string &name) -> std::vector<std::string> & {
|
||||
auto it = animationLines_.find(name);
|
||||
if (it == animationLines_.end()) {
|
||||
static std::vector<std::string> empty;
|
||||
@@ -203,7 +231,7 @@ std::vector<std::string> &Resource::getAnimationLines(const std::string &name) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
Text *Resource::getText(const std::string &name) {
|
||||
auto Resource::getText(const std::string &name) -> Text * {
|
||||
auto it = texts_.find(name);
|
||||
if (it == texts_.end()) {
|
||||
std::cerr << "Resource::getText: missing " << name << '\n';
|
||||
@@ -212,7 +240,7 @@ Text *Resource::getText(const std::string &name) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
Menu *Resource::getMenu(const std::string &name) {
|
||||
auto Resource::getMenu(const std::string &name) -> Menu * {
|
||||
auto it = menus_.find(name);
|
||||
if (it == menus_.end()) {
|
||||
std::cerr << "Resource::getMenu: missing " << name << '\n';
|
||||
|
||||
@@ -19,15 +19,15 @@ class Resource {
|
||||
public:
|
||||
static void init(SDL_Renderer *renderer);
|
||||
static void destroy();
|
||||
static Resource *get();
|
||||
static auto get() -> Resource *;
|
||||
|
||||
Texture *getTexture(const std::string &name);
|
||||
JA_Sound_t *getSound(const std::string &name);
|
||||
JA_Music_t *getMusic(const std::string &name);
|
||||
std::vector<std::string> &getAnimationLines(const std::string &name);
|
||||
Text *getText(const std::string &name); // name sin extensión: "smb2", "nokia2", ...
|
||||
Menu *getMenu(const std::string &name); // name sin extensión: "title", "options", ...
|
||||
const std::vector<uint8_t> &getDemoBytes() const { return demoBytes_; }
|
||||
auto getTexture(const std::string &name) -> Texture *;
|
||||
auto getSound(const std::string &name) -> JA_Sound_t *;
|
||||
auto getMusic(const std::string &name) -> JA_Music_t *;
|
||||
auto getAnimationLines(const std::string &name) -> std::vector<std::string> &;
|
||||
auto getText(const std::string &name) -> Text *; // name sin extensión: "smb2", "nokia2", ...
|
||||
auto getMenu(const std::string &name) -> Menu *; // name sin extensión: "title", "options", ...
|
||||
auto getDemoBytes() const -> const std::vector<uint8_t> & { return demoBytes_; }
|
||||
|
||||
private:
|
||||
explicit Resource(SDL_Renderer *renderer);
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
namespace ResourceHelper {
|
||||
static bool resource_system_initialized = false;
|
||||
|
||||
bool initializeResourceSystem(const std::string& pack_file, bool enable_fallback) {
|
||||
auto initializeResourceSystem(const std::string& pack_file, bool enable_fallback) -> bool {
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
bool ok = loader.initialize(pack_file, enable_fallback);
|
||||
resource_system_initialized = ok;
|
||||
@@ -31,7 +31,7 @@ namespace ResourceHelper {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> loadFile(const std::string& filepath) {
|
||||
auto loadFile(const std::string& filepath) -> std::vector<uint8_t> {
|
||||
if (resource_system_initialized && shouldUseResourcePack(filepath)) {
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
std::string pack_path = getPackPath(filepath);
|
||||
@@ -58,14 +58,14 @@ namespace ResourceHelper {
|
||||
return data;
|
||||
}
|
||||
|
||||
bool shouldUseResourcePack(const std::string& filepath) {
|
||||
auto shouldUseResourcePack(const std::string& filepath) -> bool {
|
||||
// Solo entran al pack los ficheros dentro de data/
|
||||
return filepath.find("data/") != std::string::npos;
|
||||
}
|
||||
|
||||
std::string getPackPath(const std::string& asset_path) {
|
||||
auto getPackPath(const std::string& asset_path) -> std::string {
|
||||
std::string pack_path = asset_path;
|
||||
std::replace(pack_path.begin(), pack_path.end(), '\\', '/');
|
||||
std::ranges::replace(pack_path, '\\', '/');
|
||||
|
||||
// Toma la última aparición de "data/" como prefijo a quitar
|
||||
size_t last_data = pack_path.rfind("data/");
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
#include <vector>
|
||||
|
||||
namespace ResourceHelper {
|
||||
bool initializeResourceSystem(const std::string& pack_file = "resources.pack", bool enable_fallback = true);
|
||||
auto initializeResourceSystem(const std::string& pack_file = "resources.pack", bool enable_fallback = true) -> bool;
|
||||
void shutdownResourceSystem();
|
||||
|
||||
std::vector<uint8_t> loadFile(const std::string& filepath);
|
||||
auto loadFile(const std::string& filepath) -> std::vector<uint8_t>;
|
||||
|
||||
bool shouldUseResourcePack(const std::string& filepath);
|
||||
std::string getPackPath(const std::string& asset_path);
|
||||
auto shouldUseResourcePack(const std::string& filepath) -> bool;
|
||||
auto getPackPath(const std::string& asset_path) -> std::string;
|
||||
} // namespace ResourceHelper
|
||||
|
||||
@@ -9,11 +9,9 @@
|
||||
|
||||
std::unique_ptr<ResourceLoader> ResourceLoader::instance = nullptr;
|
||||
|
||||
ResourceLoader::ResourceLoader()
|
||||
: resource_pack_(nullptr),
|
||||
fallback_to_files_(true) {}
|
||||
ResourceLoader::ResourceLoader() = default;
|
||||
|
||||
ResourceLoader& ResourceLoader::getInstance() {
|
||||
auto ResourceLoader::getInstance() -> ResourceLoader& {
|
||||
if (!instance) {
|
||||
instance = std::unique_ptr<ResourceLoader>(new ResourceLoader());
|
||||
}
|
||||
@@ -24,7 +22,7 @@ ResourceLoader::~ResourceLoader() {
|
||||
shutdown();
|
||||
}
|
||||
|
||||
bool ResourceLoader::initialize(const std::string& pack_file, bool enable_fallback) {
|
||||
auto ResourceLoader::initialize(const std::string& pack_file, bool enable_fallback) -> bool {
|
||||
shutdown();
|
||||
|
||||
fallback_to_files_ = enable_fallback;
|
||||
@@ -55,7 +53,7 @@ void ResourceLoader::shutdown() {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ResourceLoader::loadResource(const std::string& filename) {
|
||||
auto ResourceLoader::loadResource(const std::string& filename) -> std::vector<uint8_t> {
|
||||
if ((resource_pack_ != nullptr) && resource_pack_->hasResource(filename)) {
|
||||
return resource_pack_->getResource(filename);
|
||||
}
|
||||
@@ -68,7 +66,7 @@ std::vector<uint8_t> ResourceLoader::loadResource(const std::string& filename) {
|
||||
return {};
|
||||
}
|
||||
|
||||
bool ResourceLoader::resourceExists(const std::string& filename) {
|
||||
auto ResourceLoader::resourceExists(const std::string& filename) -> bool {
|
||||
if ((resource_pack_ != nullptr) && resource_pack_->hasResource(filename)) {
|
||||
return true;
|
||||
}
|
||||
@@ -81,7 +79,7 @@ bool ResourceLoader::resourceExists(const std::string& filename) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ResourceLoader::loadFromFile(const std::string& filename) {
|
||||
auto ResourceLoader::loadFromFile(const std::string& filename) -> std::vector<uint8_t> {
|
||||
std::string full_path = getDataPath(filename);
|
||||
|
||||
std::ifstream file(full_path, std::ios::binary | std::ios::ate);
|
||||
@@ -102,18 +100,18 @@ std::vector<uint8_t> ResourceLoader::loadFromFile(const std::string& filename) {
|
||||
return data;
|
||||
}
|
||||
|
||||
std::string ResourceLoader::getDataPath(const std::string& filename) {
|
||||
auto ResourceLoader::getDataPath(const std::string& filename) -> std::string {
|
||||
return "data/" + filename;
|
||||
}
|
||||
|
||||
size_t ResourceLoader::getLoadedResourceCount() const {
|
||||
auto ResourceLoader::getLoadedResourceCount() const -> size_t {
|
||||
if (resource_pack_ != nullptr) {
|
||||
return resource_pack_->getResourceCount();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<std::string> ResourceLoader::getAvailableResources() const {
|
||||
auto ResourceLoader::getAvailableResources() const -> std::vector<std::string> {
|
||||
if (resource_pack_ != nullptr) {
|
||||
return resource_pack_->getResourceList();
|
||||
}
|
||||
@@ -123,7 +121,7 @@ std::vector<std::string> ResourceLoader::getAvailableResources() const {
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator("data")) {
|
||||
if (entry.is_regular_file()) {
|
||||
std::string filename = std::filesystem::relative(entry.path(), "data").string();
|
||||
std::replace(filename.begin(), filename.end(), '\\', '/');
|
||||
std::ranges::replace(filename, '\\', '/');
|
||||
result.push_back(filename);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,29 +11,29 @@ class ResourcePack;
|
||||
class ResourceLoader {
|
||||
private:
|
||||
static std::unique_ptr<ResourceLoader> instance;
|
||||
ResourcePack* resource_pack_;
|
||||
ResourcePack* resource_pack_{nullptr};
|
||||
std::string pack_path_;
|
||||
bool fallback_to_files_;
|
||||
bool fallback_to_files_{true};
|
||||
|
||||
ResourceLoader();
|
||||
|
||||
public:
|
||||
static ResourceLoader& getInstance();
|
||||
static auto getInstance() -> ResourceLoader&;
|
||||
~ResourceLoader();
|
||||
|
||||
bool initialize(const std::string& pack_file, bool enable_fallback = true);
|
||||
auto initialize(const std::string& pack_file, bool enable_fallback = true) -> bool;
|
||||
void shutdown();
|
||||
|
||||
std::vector<uint8_t> loadResource(const std::string& filename);
|
||||
bool resourceExists(const std::string& filename);
|
||||
auto loadResource(const std::string& filename) -> std::vector<uint8_t>;
|
||||
auto resourceExists(const std::string& filename) -> bool;
|
||||
|
||||
void setFallbackToFiles(bool enable) { fallback_to_files_ = enable; }
|
||||
bool getFallbackToFiles() const { return fallback_to_files_; }
|
||||
[[nodiscard]] auto getFallbackToFiles() const -> bool { return fallback_to_files_; }
|
||||
|
||||
size_t getLoadedResourceCount() const;
|
||||
std::vector<std::string> getAvailableResources() const;
|
||||
[[nodiscard]] auto getLoadedResourceCount() const -> size_t;
|
||||
[[nodiscard]] auto getAvailableResources() const -> std::vector<std::string>;
|
||||
|
||||
private:
|
||||
static std::vector<uint8_t> loadFromFile(const std::string& filename);
|
||||
static std::string getDataPath(const std::string& filename);
|
||||
static auto loadFromFile(const std::string& filename) -> std::vector<uint8_t>;
|
||||
static auto getDataPath(const std::string& filename) -> std::string;
|
||||
};
|
||||
|
||||
@@ -10,19 +10,20 @@
|
||||
|
||||
const std::string ResourcePack::DEFAULT_ENCRYPT_KEY = "CCRS_RESOURCES__2026";
|
||||
|
||||
ResourcePack::ResourcePack()
|
||||
: loaded_(false) {}
|
||||
ResourcePack::ResourcePack() = default;
|
||||
|
||||
ResourcePack::~ResourcePack() {
|
||||
clear();
|
||||
}
|
||||
|
||||
uint32_t ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) {
|
||||
auto ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t {
|
||||
return std::accumulate(data.begin(), data.end(), uint32_t(0x12345678), [](uint32_t acc, uint8_t b) { return ((acc << 5) + acc) + b; });
|
||||
}
|
||||
|
||||
void ResourcePack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||
if (key.empty()) return;
|
||||
if (key.empty()) {
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
data[i] ^= key[i % key.length()];
|
||||
}
|
||||
@@ -32,7 +33,7 @@ void ResourcePack::decryptData(std::vector<uint8_t>& data, const std::string& ke
|
||||
encryptData(data, key);
|
||||
}
|
||||
|
||||
bool ResourcePack::loadPack(const std::string& pack_file) {
|
||||
auto ResourcePack::loadPack(const std::string& pack_file) -> bool {
|
||||
std::ifstream file(pack_file, std::ios::binary);
|
||||
if (!file) {
|
||||
std::cerr << "Error: Could not open pack file: " << pack_file << '\n';
|
||||
@@ -87,7 +88,7 @@ bool ResourcePack::loadPack(const std::string& pack_file) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourcePack::savePack(const std::string& pack_file) {
|
||||
auto ResourcePack::savePack(const std::string& pack_file) -> bool {
|
||||
std::ofstream file(pack_file, std::ios::binary);
|
||||
if (!file) {
|
||||
std::cerr << "Error: Could not create pack file: " << pack_file << '\n';
|
||||
@@ -99,11 +100,11 @@ bool ResourcePack::savePack(const std::string& pack_file) {
|
||||
uint32_t version = 1;
|
||||
file.write(reinterpret_cast<const char*>(&version), sizeof(version));
|
||||
|
||||
uint32_t resource_count = static_cast<uint32_t>(resources_.size());
|
||||
auto resource_count = static_cast<uint32_t>(resources_.size());
|
||||
file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));
|
||||
|
||||
for (const auto& [filename, entry] : resources_) {
|
||||
uint32_t filename_length = static_cast<uint32_t>(filename.length());
|
||||
auto filename_length = static_cast<uint32_t>(filename.length());
|
||||
file.write(reinterpret_cast<const char*>(&filename_length), sizeof(filename_length));
|
||||
file.write(filename.c_str(), filename_length);
|
||||
|
||||
@@ -122,7 +123,7 @@ bool ResourcePack::savePack(const std::string& pack_file) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourcePack::addFile(const std::string& filename, const std::string& filepath) {
|
||||
auto ResourcePack::addFile(const std::string& filename, const std::string& filepath) -> bool {
|
||||
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
std::cerr << "Error: Could not open file: " << filepath << '\n';
|
||||
@@ -150,7 +151,7 @@ bool ResourcePack::addFile(const std::string& filename, const std::string& filep
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourcePack::addDirectory(const std::string& directory) {
|
||||
auto ResourcePack::addDirectory(const std::string& directory) -> bool {
|
||||
if (!std::filesystem::exists(directory)) {
|
||||
std::cerr << "Error: Directory does not exist: " << directory << '\n';
|
||||
return false;
|
||||
@@ -162,7 +163,7 @@ bool ResourcePack::addDirectory(const std::string& directory) {
|
||||
std::string filepath = entry.path().string();
|
||||
std::string filename = std::filesystem::relative(entry.path(), directory).string();
|
||||
|
||||
std::replace(filename.begin(), filename.end(), '\\', '/');
|
||||
std::ranges::replace(filename, '\\', '/');
|
||||
|
||||
if (!addFile(filename, filepath)) {
|
||||
return false;
|
||||
@@ -173,7 +174,7 @@ bool ResourcePack::addDirectory(const std::string& directory) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ResourcePack::getResource(const std::string& filename) {
|
||||
auto ResourcePack::getResource(const std::string& filename) -> std::vector<uint8_t> {
|
||||
auto it = resources_.find(filename);
|
||||
if (it == resources_.end()) {
|
||||
std::cerr << "Error: Resource not found: " << filename << '\n';
|
||||
@@ -197,7 +198,7 @@ std::vector<uint8_t> ResourcePack::getResource(const std::string& filename) {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ResourcePack::hasResource(const std::string& filename) const {
|
||||
auto ResourcePack::hasResource(const std::string& filename) const -> bool {
|
||||
return resources_.find(filename) != resources_.end();
|
||||
}
|
||||
|
||||
@@ -207,11 +208,11 @@ void ResourcePack::clear() {
|
||||
loaded_ = false;
|
||||
}
|
||||
|
||||
size_t ResourcePack::getResourceCount() const {
|
||||
auto ResourcePack::getResourceCount() const -> size_t {
|
||||
return resources_.size();
|
||||
}
|
||||
|
||||
std::vector<std::string> ResourcePack::getResourceList() const {
|
||||
auto ResourcePack::getResourceList() const -> std::vector<std::string> {
|
||||
std::vector<std::string> result;
|
||||
result.reserve(resources_.size());
|
||||
for (const auto& [filename, entry] : resources_) {
|
||||
|
||||
@@ -17,9 +17,9 @@ class ResourcePack {
|
||||
private:
|
||||
std::unordered_map<std::string, ResourceEntry> resources_;
|
||||
std::vector<uint8_t> data_;
|
||||
bool loaded_;
|
||||
bool loaded_{false};
|
||||
|
||||
static uint32_t calculateChecksum(const std::vector<uint8_t>& data);
|
||||
static auto calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t;
|
||||
static void encryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||
static void decryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||
|
||||
@@ -27,18 +27,18 @@ class ResourcePack {
|
||||
ResourcePack();
|
||||
~ResourcePack();
|
||||
|
||||
bool loadPack(const std::string& pack_file);
|
||||
bool savePack(const std::string& pack_file);
|
||||
auto loadPack(const std::string& pack_file) -> bool;
|
||||
auto savePack(const std::string& pack_file) -> bool;
|
||||
|
||||
bool addFile(const std::string& filename, const std::string& filepath);
|
||||
bool addDirectory(const std::string& directory);
|
||||
auto addFile(const std::string& filename, const std::string& filepath) -> bool;
|
||||
auto addDirectory(const std::string& directory) -> bool;
|
||||
|
||||
std::vector<uint8_t> getResource(const std::string& filename);
|
||||
bool hasResource(const std::string& filename) const;
|
||||
auto getResource(const std::string& filename) -> std::vector<uint8_t>;
|
||||
auto hasResource(const std::string& filename) const -> bool;
|
||||
|
||||
void clear();
|
||||
size_t getResourceCount() const;
|
||||
std::vector<std::string> getResourceList() const;
|
||||
auto getResourceCount() const -> size_t;
|
||||
auto getResourceList() const -> std::vector<std::string>;
|
||||
|
||||
static const std::string DEFAULT_ENCRYPT_KEY;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user