// resource_loader.cpp // Resource loader implementation #include "resource_loader.hpp" #include #include #include namespace jdd { // Get singleton instance auto ResourceLoader::get() -> ResourceLoader& { static ResourceLoader instance; return instance; } // Initialize with a pack file auto ResourceLoader::initialize(const std::string& pack_file, bool enable_fallback) -> bool { if (initialized_) { std::cout << "ResourceLoader: Already initialized\n"; return true; } fallback_to_files_ = enable_fallback; // 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(); if (resource_pack_->loadPack(pack_file)) { std::cout << "ResourceLoader: Pack loaded successfully\n"; initialized_ = true; return true; } std::cerr << "ResourceLoader: Failed to load pack file\n"; resource_pack_.reset(); } else { std::cout << "ResourceLoader: 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"; return false; } // Otherwise, fallback to filesystem std::cout << "ResourceLoader: Using filesystem fallback\n"; initialized_ = true; return true; } // Load a resource auto ResourceLoader::loadResource(const std::string& filename) -> std::vector { if (!initialized_) { std::cerr << "ResourceLoader: Not initialized\n"; return {}; } // Try pack first if available if (resource_pack_ && resource_pack_->isLoaded()) { if (resource_pack_->hasResource(filename)) { auto data = resource_pack_->getResource(filename); if (!data.empty()) { return data; } std::cerr << "ResourceLoader: Failed to extract from pack: " << filename << '\n'; } } // Fallback to filesystem if enabled if (fallback_to_files_) { return loadFromFilesystem(filename); } std::cerr << "ResourceLoader: Resource not found: " << filename << '\n'; return {}; } // Check if a resource exists auto ResourceLoader::resourceExists(const std::string& filename) -> bool { if (!initialized_) { return false; } // Check pack first if (resource_pack_ && resource_pack_->isLoaded()) { if (resource_pack_->hasResource(filename)) { return true; } } // Check filesystem if fallback enabled if (fallback_to_files_) { return fileExistsOnFilesystem(filename); } return false; } // Check if pack is loaded auto ResourceLoader::isPackLoaded() const -> bool { return resource_pack_ && resource_pack_->isLoaded(); } // Get pack statistics auto ResourceLoader::getPackResourceCount() const -> size_t { if (resource_pack_ && resource_pack_->isLoaded()) { return resource_pack_->getResourceCount(); } return 0; } // Cleanup void ResourceLoader::shutdown() { resource_pack_.reset(); initialized_ = false; std::cout << "ResourceLoader: Shutdown complete\n"; } // Load from filesystem auto ResourceLoader::loadFromFilesystem(const std::string& filepath) -> std::vector { std::ifstream file(filepath, std::ios::binary | std::ios::ate); if (!file) { return {}; } std::streamsize file_size = file.tellg(); file.seekg(0, std::ios::beg); std::vector data(file_size); if (!file.read(reinterpret_cast(data.data()), file_size)) { std::cerr << "ResourceLoader: Failed to read file: " << filepath << '\n'; return {}; } return data; } // Check if file exists on filesystem auto ResourceLoader::fileExistsOnFilesystem(const std::string& filepath) -> bool { return std::filesystem::exists(filepath); } // Validate pack integrity auto ResourceLoader::validatePack() const -> bool { if (!initialized_ || !resource_pack_ || !resource_pack_->isLoaded()) { std::cerr << "ResourceLoader: Cannot validate - pack not loaded\n"; return false; } // Calculate pack checksum uint32_t checksum = resource_pack_->calculatePackChecksum(); if (checksum == 0) { std::cerr << "ResourceLoader: Pack checksum is zero (invalid)\n"; return false; } std::cout << "ResourceLoader: Pack checksum: 0x" << std::hex << checksum << std::dec << '\n'; std::cout << "ResourceLoader: Pack validation successful\n"; return true; } // Load assets.txt from pack auto ResourceLoader::loadAssetsConfig() const -> std::string { if (!initialized_ || !resource_pack_ || !resource_pack_->isLoaded()) { std::cerr << "ResourceLoader: Cannot load assets config - pack not loaded\n"; return ""; } // Try to load config/assets.txt from pack 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'; return ""; } auto data = resource_pack_->getResource(config_path); if (data.empty()) { std::cerr << "ResourceLoader: 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() << " bytes)\n"; return config_content; } } // namespace jdd