133 lines
3.8 KiB
C++
133 lines
3.8 KiB
C++
#include "resource_loader.h"
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <filesystem>
|
|
#include <algorithm>
|
|
|
|
std::unique_ptr<ResourceLoader> ResourceLoader::instance = nullptr;
|
|
|
|
ResourceLoader::ResourceLoader()
|
|
: resourcePack(nullptr), fallbackToFiles(true) {}
|
|
|
|
ResourceLoader& ResourceLoader::getInstance() {
|
|
if (!instance) {
|
|
instance = std::unique_ptr<ResourceLoader>(new ResourceLoader());
|
|
}
|
|
return *instance;
|
|
}
|
|
|
|
ResourceLoader::~ResourceLoader() {
|
|
shutdown();
|
|
}
|
|
|
|
bool ResourceLoader::initialize(const std::string& packFile, bool enableFallback) {
|
|
shutdown();
|
|
|
|
fallbackToFiles = enableFallback;
|
|
packPath = packFile;
|
|
|
|
if (std::filesystem::exists(packFile)) {
|
|
resourcePack = new ResourcePack();
|
|
if (resourcePack->loadPack(packFile)) {
|
|
std::cout << "Resource pack loaded successfully: " << packFile << std::endl;
|
|
std::cout << "Resources available: " << resourcePack->getResourceCount() << std::endl;
|
|
return true;
|
|
} else {
|
|
delete resourcePack;
|
|
resourcePack = nullptr;
|
|
std::cerr << "Failed to load resource pack: " << packFile << std::endl;
|
|
}
|
|
}
|
|
|
|
if (fallbackToFiles) {
|
|
std::cout << "Using fallback mode: loading resources from data/ directory" << std::endl;
|
|
return true;
|
|
}
|
|
|
|
std::cerr << "Resource pack not found and fallback disabled: " << packFile << std::endl;
|
|
return false;
|
|
}
|
|
|
|
void ResourceLoader::shutdown() {
|
|
if (resourcePack) {
|
|
delete resourcePack;
|
|
resourcePack = nullptr;
|
|
}
|
|
}
|
|
|
|
std::vector<uint8_t> ResourceLoader::loadResource(const std::string& filename) {
|
|
if (resourcePack && resourcePack->hasResource(filename)) {
|
|
return resourcePack->getResource(filename);
|
|
}
|
|
|
|
if (fallbackToFiles) {
|
|
return loadFromFile(filename);
|
|
}
|
|
|
|
std::cerr << "Resource not found: " << filename << std::endl;
|
|
return {};
|
|
}
|
|
|
|
bool ResourceLoader::resourceExists(const std::string& filename) {
|
|
if (resourcePack && resourcePack->hasResource(filename)) {
|
|
return true;
|
|
}
|
|
|
|
if (fallbackToFiles) {
|
|
std::string fullPath = getDataPath(filename);
|
|
return std::filesystem::exists(fullPath);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
std::vector<uint8_t> ResourceLoader::loadFromFile(const std::string& filename) {
|
|
std::string fullPath = getDataPath(filename);
|
|
|
|
std::ifstream file(fullPath, std::ios::binary | std::ios::ate);
|
|
if (!file) {
|
|
std::cerr << "Error: Could not open file: " << fullPath << std::endl;
|
|
return {};
|
|
}
|
|
|
|
std::streamsize fileSize = file.tellg();
|
|
file.seekg(0, std::ios::beg);
|
|
|
|
std::vector<uint8_t> data(fileSize);
|
|
if (!file.read(reinterpret_cast<char*>(data.data()), fileSize)) {
|
|
std::cerr << "Error: Could not read file: " << fullPath << std::endl;
|
|
return {};
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
std::string ResourceLoader::getDataPath(const std::string& filename) {
|
|
return "data/" + filename;
|
|
}
|
|
|
|
size_t ResourceLoader::getLoadedResourceCount() const {
|
|
if (resourcePack) {
|
|
return resourcePack->getResourceCount();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
std::vector<std::string> ResourceLoader::getAvailableResources() const {
|
|
if (resourcePack) {
|
|
return resourcePack->getResourceList();
|
|
}
|
|
|
|
std::vector<std::string> result;
|
|
if (fallbackToFiles && std::filesystem::exists("data")) {
|
|
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(), '\\', '/');
|
|
result.push_back(filename);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
} |