unificats els resources en un namespace
This commit is contained in:
@@ -7,19 +7,19 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace Jdd {
|
||||
namespace Resource {
|
||||
|
||||
// Get singleton instance
|
||||
auto ResourceLoader::get() -> ResourceLoader& {
|
||||
static ResourceLoader instance_;
|
||||
auto Loader::get() -> Loader& {
|
||||
static Loader instance_;
|
||||
return instance_;
|
||||
}
|
||||
|
||||
// Initialize with a pack file
|
||||
auto ResourceLoader::initialize(const std::string& pack_file, bool enable_fallback)
|
||||
auto Loader::initialize(const std::string& pack_file, bool enable_fallback)
|
||||
-> bool {
|
||||
if (initialized_) {
|
||||
std::cout << "ResourceLoader: Already initialized\n";
|
||||
std::cout << "Loader: Already initialized\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -27,35 +27,35 @@ auto ResourceLoader::initialize(const std::string& pack_file, bool enable_fallba
|
||||
|
||||
// Try to load the pack file
|
||||
if (!pack_file.empty() && fileExistsOnFilesystem(pack_file)) {
|
||||
std::cout << "ResourceLoader: Loading pack file: " << pack_file << '\n';
|
||||
resource_pack_ = std::make_unique<ResourcePack>();
|
||||
std::cout << "Loader: Loading pack file: " << pack_file << '\n';
|
||||
resource_pack_ = std::make_unique<Pack>();
|
||||
if (resource_pack_->loadPack(pack_file)) {
|
||||
std::cout << "ResourceLoader: Pack loaded successfully\n";
|
||||
std::cout << "Loader: Pack loaded successfully\n";
|
||||
initialized_ = true;
|
||||
return true;
|
||||
}
|
||||
std::cerr << "ResourceLoader: Failed to load pack file\n";
|
||||
std::cerr << "Loader: Failed to load pack file\n";
|
||||
resource_pack_.reset();
|
||||
} else {
|
||||
std::cout << "ResourceLoader: Pack file not found: " << pack_file << '\n';
|
||||
std::cout << "Loader: Pack file not found: " << pack_file << '\n';
|
||||
}
|
||||
|
||||
// If pack loading failed and fallback is disabled, fail
|
||||
if (!fallback_to_files_) {
|
||||
std::cerr << "ResourceLoader: Pack required but not found (fallback disabled)\n";
|
||||
std::cerr << "Loader: Pack required but not found (fallback disabled)\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Otherwise, fallback to filesystem
|
||||
std::cout << "ResourceLoader: Using filesystem fallback\n";
|
||||
std::cout << "Loader: Using filesystem fallback\n";
|
||||
initialized_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Load a resource
|
||||
auto ResourceLoader::loadResource(const std::string& filename) -> std::vector<uint8_t> {
|
||||
auto Loader::loadResource(const std::string& filename) -> std::vector<uint8_t> {
|
||||
if (!initialized_) {
|
||||
std::cerr << "ResourceLoader: Not initialized\n";
|
||||
std::cerr << "Loader: Not initialized\n";
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ auto ResourceLoader::loadResource(const std::string& filename) -> std::vector<ui
|
||||
if (!data.empty()) {
|
||||
return data;
|
||||
}
|
||||
std::cerr << "ResourceLoader: Failed to extract from pack: " << filename
|
||||
std::cerr << "Loader: Failed to extract from pack: " << filename
|
||||
<< '\n';
|
||||
}
|
||||
}
|
||||
@@ -76,12 +76,12 @@ auto ResourceLoader::loadResource(const std::string& filename) -> std::vector<ui
|
||||
return loadFromFilesystem(filename);
|
||||
}
|
||||
|
||||
std::cerr << "ResourceLoader: Resource not found: " << filename << '\n';
|
||||
std::cerr << "Loader: Resource not found: " << filename << '\n';
|
||||
return {};
|
||||
}
|
||||
|
||||
// Check if a resource exists
|
||||
auto ResourceLoader::resourceExists(const std::string& filename) -> bool {
|
||||
auto Loader::resourceExists(const std::string& filename) -> bool {
|
||||
if (!initialized_) {
|
||||
return false;
|
||||
}
|
||||
@@ -102,12 +102,12 @@ auto ResourceLoader::resourceExists(const std::string& filename) -> bool {
|
||||
}
|
||||
|
||||
// Check if pack is loaded
|
||||
auto ResourceLoader::isPackLoaded() const -> bool {
|
||||
auto Loader::isPackLoaded() const -> bool {
|
||||
return resource_pack_ && resource_pack_->isLoaded();
|
||||
}
|
||||
|
||||
// Get pack statistics
|
||||
auto ResourceLoader::getPackResourceCount() const -> size_t {
|
||||
auto Loader::getPackResourceCount() const -> size_t {
|
||||
if (resource_pack_ && resource_pack_->isLoaded()) {
|
||||
return resource_pack_->getResourceCount();
|
||||
}
|
||||
@@ -115,14 +115,14 @@ auto ResourceLoader::getPackResourceCount() const -> size_t {
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
void ResourceLoader::shutdown() {
|
||||
void Loader::shutdown() {
|
||||
resource_pack_.reset();
|
||||
initialized_ = false;
|
||||
std::cout << "ResourceLoader: Shutdown complete\n";
|
||||
std::cout << "Loader: Shutdown complete\n";
|
||||
}
|
||||
|
||||
// Load from filesystem
|
||||
auto ResourceLoader::loadFromFilesystem(const std::string& filepath)
|
||||
auto Loader::loadFromFilesystem(const std::string& filepath)
|
||||
-> std::vector<uint8_t> {
|
||||
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
@@ -134,7 +134,7 @@ auto ResourceLoader::loadFromFilesystem(const std::string& filepath)
|
||||
|
||||
std::vector<uint8_t> data(file_size);
|
||||
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
|
||||
std::cerr << "ResourceLoader: Failed to read file: " << filepath << '\n';
|
||||
std::cerr << "Loader: Failed to read file: " << filepath << '\n';
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -142,14 +142,14 @@ auto ResourceLoader::loadFromFilesystem(const std::string& filepath)
|
||||
}
|
||||
|
||||
// Check if file exists on filesystem
|
||||
auto ResourceLoader::fileExistsOnFilesystem(const std::string& filepath) -> bool {
|
||||
auto Loader::fileExistsOnFilesystem(const std::string& filepath) -> bool {
|
||||
return std::filesystem::exists(filepath);
|
||||
}
|
||||
|
||||
// Validate pack integrity
|
||||
auto ResourceLoader::validatePack() const -> bool {
|
||||
auto Loader::validatePack() const -> bool {
|
||||
if (!initialized_ || !resource_pack_ || !resource_pack_->isLoaded()) {
|
||||
std::cerr << "ResourceLoader: Cannot validate - pack not loaded\n";
|
||||
std::cerr << "Loader: Cannot validate - pack not loaded\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -157,20 +157,20 @@ auto ResourceLoader::validatePack() const -> bool {
|
||||
uint32_t checksum = resource_pack_->calculatePackChecksum();
|
||||
|
||||
if (checksum == 0) {
|
||||
std::cerr << "ResourceLoader: Pack checksum is zero (invalid)\n";
|
||||
std::cerr << "Loader: Pack checksum is zero (invalid)\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "ResourceLoader: Pack checksum: 0x" << std::hex << checksum << std::dec
|
||||
std::cout << "Loader: Pack checksum: 0x" << std::hex << checksum << std::dec
|
||||
<< '\n';
|
||||
std::cout << "ResourceLoader: Pack validation successful\n";
|
||||
std::cout << "Loader: Pack validation successful\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
// Load assets.txt from pack
|
||||
auto ResourceLoader::loadAssetsConfig() const -> std::string {
|
||||
auto Loader::loadAssetsConfig() const -> std::string {
|
||||
if (!initialized_ || !resource_pack_ || !resource_pack_->isLoaded()) {
|
||||
std::cerr << "ResourceLoader: Cannot load assets config - pack not loaded\n";
|
||||
std::cerr << "Loader: Cannot load assets config - pack not loaded\n";
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -178,22 +178,22 @@ auto ResourceLoader::loadAssetsConfig() const -> std::string {
|
||||
std::string config_path = "config/assets.txt";
|
||||
|
||||
if (!resource_pack_->hasResource(config_path)) {
|
||||
std::cerr << "ResourceLoader: assets.txt not found in pack: " << config_path << '\n';
|
||||
std::cerr << "Loader: assets.txt not found in pack: " << config_path << '\n';
|
||||
return "";
|
||||
}
|
||||
|
||||
auto data = resource_pack_->getResource(config_path);
|
||||
if (data.empty()) {
|
||||
std::cerr << "ResourceLoader: Failed to load assets.txt from pack\n";
|
||||
std::cerr << "Loader: Failed to load assets.txt from pack\n";
|
||||
return "";
|
||||
}
|
||||
|
||||
// Convert bytes to string
|
||||
std::string config_content(data.begin(), data.end());
|
||||
std::cout << "ResourceLoader: Loaded assets.txt from pack (" << data.size()
|
||||
std::cout << "Loader: Loaded assets.txt from pack (" << data.size()
|
||||
<< " bytes)\n";
|
||||
|
||||
return config_content;
|
||||
}
|
||||
|
||||
} // namespace Jdd
|
||||
} // namespace Resource
|
||||
|
||||
Reference in New Issue
Block a user