8c6bea897c
migrat a resources.pack
179 lines
5.3 KiB
C++
179 lines
5.3 KiB
C++
// resource_helper.cpp
|
|
// Resource helper implementation
|
|
|
|
#include "resource_helper.hpp"
|
|
|
|
#include "resource_loader.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
namespace jdd {
|
|
namespace ResourceHelper {
|
|
|
|
static bool resource_system_initialized = false;
|
|
|
|
// Initialize the resource system
|
|
auto initializeResourceSystem(const std::string& pack_file, bool enable_fallback)
|
|
-> bool {
|
|
if (resource_system_initialized) {
|
|
std::cout << "ResourceHelper: Already initialized\n";
|
|
return true;
|
|
}
|
|
|
|
std::cout << "ResourceHelper: Initializing with pack: " << pack_file << '\n';
|
|
std::cout << "ResourceHelper: Fallback enabled: " << (enable_fallback ? "Yes" : "No")
|
|
<< '\n';
|
|
|
|
bool success = ResourceLoader::get().initialize(pack_file, enable_fallback);
|
|
if (success) {
|
|
resource_system_initialized = true;
|
|
std::cout << "ResourceHelper: Initialization successful\n";
|
|
} else {
|
|
std::cerr << "ResourceHelper: Initialization failed\n";
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
// Shutdown the resource system
|
|
void shutdownResourceSystem() {
|
|
if (resource_system_initialized) {
|
|
ResourceLoader::get().shutdown();
|
|
resource_system_initialized = false;
|
|
std::cout << "ResourceHelper: Shutdown complete\n";
|
|
}
|
|
}
|
|
|
|
// Load a file
|
|
auto loadFile(const std::string& filepath) -> std::vector<uint8_t> {
|
|
if (!resource_system_initialized) {
|
|
std::cerr << "ResourceHelper: System not initialized, loading from filesystem\n";
|
|
// Fallback to direct filesystem access
|
|
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<uint8_t> data(file_size);
|
|
file.read(reinterpret_cast<char*>(data.data()), file_size);
|
|
return data;
|
|
}
|
|
|
|
// Determine if we should use the pack
|
|
if (shouldUseResourcePack(filepath)) {
|
|
// Convert to pack path
|
|
std::string pack_path = getPackPath(filepath);
|
|
|
|
// Try to load from pack
|
|
auto data = ResourceLoader::get().loadResource(pack_path);
|
|
if (!data.empty()) {
|
|
return data;
|
|
}
|
|
|
|
// If pack loading failed, try filesystem as fallback
|
|
std::cerr << "ResourceHelper: Pack failed for " << pack_path
|
|
<< ", trying filesystem\n";
|
|
}
|
|
|
|
// Load from filesystem
|
|
return ResourceLoader::get().loadResource(filepath);
|
|
}
|
|
|
|
// Check if a file exists
|
|
auto fileExists(const std::string& filepath) -> bool {
|
|
if (!resource_system_initialized) {
|
|
return std::filesystem::exists(filepath);
|
|
}
|
|
|
|
// Check pack if appropriate
|
|
if (shouldUseResourcePack(filepath)) {
|
|
std::string pack_path = getPackPath(filepath);
|
|
if (ResourceLoader::get().resourceExists(pack_path)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// Check filesystem
|
|
return std::filesystem::exists(filepath);
|
|
}
|
|
|
|
// Convert asset path to pack path
|
|
auto getPackPath(const std::string& asset_path) -> std::string {
|
|
std::string path = asset_path;
|
|
|
|
// Convert backslashes to forward slashes
|
|
std::replace(path.begin(), path.end(), '\\', '/');
|
|
|
|
// Remove leading slashes
|
|
while (!path.empty() && path[0] == '/') {
|
|
path = path.substr(1);
|
|
}
|
|
|
|
// Remove "./" prefix if present
|
|
if (path.find("./") == 0) {
|
|
path = path.substr(2);
|
|
}
|
|
|
|
// Remove "../" prefixes (for macOS bundle paths in development)
|
|
while (path.find("../") == 0) {
|
|
path = path.substr(3);
|
|
}
|
|
|
|
// Remove "Resources/" prefix if present (for macOS bundle)
|
|
const std::string resources_prefix = "Resources/";
|
|
if (path.find(resources_prefix) == 0) {
|
|
path = path.substr(resources_prefix.length());
|
|
}
|
|
|
|
// Remove "data/" prefix if present
|
|
const std::string data_prefix = "data/";
|
|
if (path.find(data_prefix) == 0) {
|
|
path = path.substr(data_prefix.length());
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
// Check if file should use resource pack
|
|
auto shouldUseResourcePack(const std::string& filepath) -> bool {
|
|
std::string path = filepath;
|
|
std::replace(path.begin(), path.end(), '\\', '/');
|
|
|
|
// Don't use pack for most config files (except config/assets.txt which is loaded
|
|
// directly via ResourceLoader::loadAssetsConfig() in release builds)
|
|
if (path.find("config/") != std::string::npos) {
|
|
return false;
|
|
}
|
|
|
|
// Use pack for data files
|
|
if (path.find("data/") != std::string::npos) {
|
|
return true;
|
|
}
|
|
|
|
// Check if it looks like a data file (has common extensions)
|
|
if (path.find(".ogg") != std::string::npos || path.find(".wav") != std::string::npos ||
|
|
path.find(".gif") != std::string::npos || path.find(".png") != std::string::npos ||
|
|
path.find(".pal") != std::string::npos || path.find(".ani") != std::string::npos ||
|
|
path.find(".tmx") != std::string::npos || path.find(".room") != std::string::npos ||
|
|
path.find(".txt") != std::string::npos || path.find(".glsl") != std::string::npos) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Check if pack is loaded
|
|
auto isPackLoaded() -> bool {
|
|
if (!resource_system_initialized) {
|
|
return false;
|
|
}
|
|
return ResourceLoader::get().isPackLoaded();
|
|
}
|
|
|
|
} // namespace ResourceHelper
|
|
} // namespace jdd
|