treballant en resources.pack
This commit is contained in:
105
source/asset_integrated.cpp
Normal file
105
source/asset_integrated.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "asset_integrated.h"
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
bool AssetIntegrated::resource_pack_enabled_ = false;
|
||||
|
||||
void AssetIntegrated::initWithResourcePack(const std::string &executable_path,
|
||||
const std::string &resource_pack_path) {
|
||||
// Inicializar Asset normal
|
||||
Asset::init(executable_path);
|
||||
|
||||
// Inicializar ResourceLoader
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
if (loader.initialize(resource_pack_path, true)) {
|
||||
resource_pack_enabled_ = true;
|
||||
std::cout << "Asset system initialized with resource pack: " << resource_pack_path << std::endl;
|
||||
} else {
|
||||
resource_pack_enabled_ = false;
|
||||
std::cout << "Asset system initialized in fallback mode (filesystem)" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> AssetIntegrated::loadFile(const std::string &filename) {
|
||||
if (shouldUseResourcePack(filename) && resource_pack_enabled_) {
|
||||
// Intentar cargar del pack de recursos
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
|
||||
// Convertir ruta completa a ruta relativa para el pack
|
||||
std::string relativePath = filename;
|
||||
|
||||
// Si la ruta contiene "data/", extraer la parte relativa
|
||||
size_t dataPos = filename.find("data/");
|
||||
if (dataPos != std::string::npos) {
|
||||
relativePath = filename.substr(dataPos + 5); // +5 para saltar "data/"
|
||||
}
|
||||
|
||||
auto data = loader.loadResource(relativePath);
|
||||
if (!data.empty()) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: cargar del filesystem
|
||||
std::ifstream file(filename, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
std::cerr << "Error: Could not open file: " << filename << 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: " << filename << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
bool AssetIntegrated::fileExists(const std::string &filename) const {
|
||||
if (shouldUseResourcePack(filename) && resource_pack_enabled_) {
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
|
||||
// Convertir ruta completa a ruta relativa para el pack
|
||||
std::string relativePath = filename;
|
||||
size_t dataPos = filename.find("data/");
|
||||
if (dataPos != std::string::npos) {
|
||||
relativePath = filename.substr(dataPos + 5);
|
||||
}
|
||||
|
||||
if (loader.resourceExists(relativePath)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Verificar en filesystem
|
||||
return std::filesystem::exists(filename);
|
||||
}
|
||||
|
||||
std::string AssetIntegrated::getSystemPath(const std::string &filename) const {
|
||||
// Los archivos de sistema/config siempre van al filesystem
|
||||
return filename;
|
||||
}
|
||||
|
||||
bool AssetIntegrated::shouldUseResourcePack(const std::string &filepath) const {
|
||||
// Los archivos que NO van al pack:
|
||||
// - Archivos de config/ (ahora están fuera de data/)
|
||||
// - Archivos con absolute=true en assets.txt
|
||||
// - Archivos de sistema (${SYSTEM_FOLDER})
|
||||
|
||||
if (filepath.find("/config/") != std::string::npos ||
|
||||
filepath.find("config/") == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filepath.find("/data/") != std::string::npos ||
|
||||
filepath.find("data/") == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
28
source/asset_integrated.h
Normal file
28
source/asset_integrated.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "asset.h"
|
||||
#include "resource_loader.h"
|
||||
#include <memory>
|
||||
|
||||
// Extensión de Asset que integra ResourceLoader
|
||||
class AssetIntegrated : public Asset {
|
||||
public:
|
||||
// Inicializa Asset con ResourceLoader
|
||||
static void initWithResourcePack(const std::string &executable_path,
|
||||
const std::string &resource_pack_path = "resources.pack");
|
||||
|
||||
// Carga un archivo usando ResourceLoader como primera opción
|
||||
std::vector<uint8_t> loadFile(const std::string &filename);
|
||||
|
||||
// Verifica si un archivo existe (pack o filesystem)
|
||||
bool fileExists(const std::string &filename) const;
|
||||
|
||||
// Obtiene la ruta completa para archivos del sistema/config
|
||||
std::string getSystemPath(const std::string &filename) const;
|
||||
|
||||
private:
|
||||
static bool resource_pack_enabled_;
|
||||
|
||||
// Determina si un archivo debe cargarse del pack o del filesystem
|
||||
bool shouldUseResourcePack(const std::string &filepath) const;
|
||||
};
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "manage_hiscore_table.h" // Para ManageHiScoreTable
|
||||
#include "options.h" // Para loadFromFile, saveToFile, Settings, settings, setConfigFile, setControllersFile
|
||||
#include "param.h" // Para loadParamsFromFile
|
||||
#include "resource_helper.h" // Para ResourceHelper
|
||||
#include "player.h" // Para Player
|
||||
#include "resource.h" // Para Resource
|
||||
#include "screen.h" // Para Screen
|
||||
@@ -77,6 +78,7 @@ Director::~Director() {
|
||||
void Director::init() {
|
||||
// Configuración inicial de parametros
|
||||
Asset::init(executable_path_); // Inicializa el sistema de gestión de archivos
|
||||
ResourceHelper::initializeResourceSystem("resources.pack");
|
||||
loadAssets(); // Crea el índice de archivos
|
||||
Input::init(Asset::get()->get("gamecontrollerdb.txt"), Asset::get()->get("controllers.json")); // Carga configuración de controles
|
||||
Options::setConfigFile(Asset::get()->get("config.txt")); // Establece el fichero de configuración
|
||||
@@ -154,7 +156,7 @@ void Director::loadAssets() {
|
||||
#endif
|
||||
|
||||
// Cargar la configuración de assets (también aplicar el prefijo al archivo de configuración)
|
||||
std::string config_path = executable_path_ + PREFIX + "/data/config/assets.txt";
|
||||
std::string config_path = executable_path_ + PREFIX + "/config/assets.txt";
|
||||
Asset::get()->loadFromFile(config_path, PREFIX, system_folder_);
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Assets configuration loaded successfully");
|
||||
|
||||
91
source/resource_helper.cpp
Normal file
91
source/resource_helper.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "resource_helper.h"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace ResourceHelper {
|
||||
static bool resource_system_initialized = false;
|
||||
|
||||
bool initializeResourceSystem(const std::string& pack_file) {
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
resource_system_initialized = loader.initialize(pack_file, true);
|
||||
|
||||
if (resource_system_initialized) {
|
||||
std::cout << "Resource system initialized with pack: " << pack_file << std::endl;
|
||||
} else {
|
||||
std::cout << "Resource system using fallback mode (filesystem only)" << std::endl;
|
||||
}
|
||||
|
||||
return true; // Always return true as fallback is acceptable
|
||||
}
|
||||
|
||||
void shutdownResourceSystem() {
|
||||
if (resource_system_initialized) {
|
||||
ResourceLoader::getInstance().shutdown();
|
||||
resource_system_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> loadFile(const std::string& filepath) {
|
||||
if (resource_system_initialized && shouldUseResourcePack(filepath)) {
|
||||
auto& loader = ResourceLoader::getInstance();
|
||||
std::string pack_path = getPackPath(filepath);
|
||||
|
||||
auto data = loader.loadResource(pack_path);
|
||||
if (!data.empty()) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback a filesystem
|
||||
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
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)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
bool shouldUseResourcePack(const std::string& filepath) {
|
||||
// Archivos que NO van al pack:
|
||||
// - config/ (ahora está fuera de data/)
|
||||
// - archivos absolutos del sistema
|
||||
|
||||
if (filepath.find("config/") != std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Si contiene "data/" es candidato para el pack
|
||||
if (filepath.find("data/") != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string getPackPath(const std::string& asset_path) {
|
||||
std::string pack_path = asset_path;
|
||||
|
||||
// Remover prefijo "data/" si existe
|
||||
size_t data_pos = pack_path.find("data/");
|
||||
if (data_pos != std::string::npos) {
|
||||
pack_path = pack_path.substr(data_pos + 5); // +5 para saltar "data/"
|
||||
}
|
||||
|
||||
// Remover cualquier prefijo de path absoluto
|
||||
size_t last_data = pack_path.rfind("data/");
|
||||
if (last_data != std::string::npos) {
|
||||
pack_path = pack_path.substr(last_data + 5);
|
||||
}
|
||||
|
||||
return pack_path;
|
||||
}
|
||||
}
|
||||
46
source/resource_helper.h
Normal file
46
source/resource_helper.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "resource_loader.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
// Helper functions para integrar ResourceLoader con el sistema existente
|
||||
namespace ResourceHelper {
|
||||
// Inicializa ResourceLoader (llamar al inicio del programa)
|
||||
bool initializeResourceSystem(const std::string& pack_file = "resources.pack");
|
||||
|
||||
// Cierra ResourceLoader
|
||||
void shutdownResourceSystem();
|
||||
|
||||
// Carga un archivo usando ResourceLoader o fallback a filesystem
|
||||
std::vector<uint8_t> loadFile(const std::string& filepath);
|
||||
|
||||
// Verifica si un archivo debería cargarse del pack vs filesystem
|
||||
bool shouldUseResourcePack(const std::string& filepath);
|
||||
|
||||
// Convierte ruta Asset a ruta relativa para ResourceLoader
|
||||
std::string getPackPath(const std::string& asset_path);
|
||||
|
||||
// Wrappea la carga de archivos para mantener compatibilidad
|
||||
template<typename T>
|
||||
T* loadResourceFile(const std::string& asset_path, T* (*loader_func)(const char*)) {
|
||||
auto data = loadFile(asset_path);
|
||||
if (data.empty()) {
|
||||
return loader_func(asset_path.c_str());
|
||||
}
|
||||
|
||||
// Crear archivo temporal para funciones que esperan path
|
||||
std::string temp_path = "/tmp/ccae_" + std::to_string(std::hash<std::string>{}(asset_path));
|
||||
std::ofstream temp_file(temp_path, std::ios::binary);
|
||||
temp_file.write(reinterpret_cast<const char*>(data.data()), data.size());
|
||||
temp_file.close();
|
||||
|
||||
T* result = loader_func(temp_path.c_str());
|
||||
std::filesystem::remove(temp_path);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
133
source/resource_loader.cpp
Normal file
133
source/resource_loader.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#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;
|
||||
}
|
||||
37
source/resource_loader.h
Normal file
37
source/resource_loader.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef RESOURCE_LOADER_H
|
||||
#define RESOURCE_LOADER_H
|
||||
|
||||
#include "resource_pack.h"
|
||||
#include <memory>
|
||||
|
||||
class ResourceLoader {
|
||||
private:
|
||||
static std::unique_ptr<ResourceLoader> instance;
|
||||
ResourcePack* resourcePack;
|
||||
std::string packPath;
|
||||
bool fallbackToFiles;
|
||||
|
||||
ResourceLoader();
|
||||
|
||||
public:
|
||||
static ResourceLoader& getInstance();
|
||||
~ResourceLoader();
|
||||
|
||||
bool initialize(const std::string& packFile, bool enableFallback = true);
|
||||
void shutdown();
|
||||
|
||||
std::vector<uint8_t> loadResource(const std::string& filename);
|
||||
bool resourceExists(const std::string& filename);
|
||||
|
||||
void setFallbackToFiles(bool enable) { fallbackToFiles = enable; }
|
||||
bool getFallbackToFiles() const { return fallbackToFiles; }
|
||||
|
||||
size_t getLoadedResourceCount() const;
|
||||
std::vector<std::string> getAvailableResources() const;
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> loadFromFile(const std::string& filename);
|
||||
std::string getDataPath(const std::string& filename);
|
||||
};
|
||||
|
||||
#endif
|
||||
222
source/resource_pack.cpp
Normal file
222
source/resource_pack.cpp
Normal file
@@ -0,0 +1,222 @@
|
||||
#include "resource_pack.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
|
||||
const std::string ResourcePack::DEFAULT_ENCRYPT_KEY = "CCAE_RESOURCES_2024";
|
||||
|
||||
ResourcePack::ResourcePack() : loaded(false) {}
|
||||
|
||||
ResourcePack::~ResourcePack() {
|
||||
clear();
|
||||
}
|
||||
|
||||
uint32_t ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) {
|
||||
uint32_t checksum = 0x12345678;
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
checksum = ((checksum << 5) + checksum) + data[i];
|
||||
}
|
||||
return checksum;
|
||||
}
|
||||
|
||||
void ResourcePack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||
if (key.empty()) return;
|
||||
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
data[i] ^= key[i % key.length()];
|
||||
}
|
||||
}
|
||||
|
||||
void ResourcePack::decryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||
encryptData(data, key);
|
||||
}
|
||||
|
||||
bool ResourcePack::loadPack(const std::string& packFile) {
|
||||
std::ifstream file(packFile, std::ios::binary);
|
||||
if (!file) {
|
||||
std::cerr << "Error: Could not open pack file: " << packFile << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
char header[4];
|
||||
file.read(header, 4);
|
||||
if (std::string(header, 4) != "CCAE") {
|
||||
std::cerr << "Error: Invalid pack file format" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t version;
|
||||
file.read(reinterpret_cast<char*>(&version), sizeof(version));
|
||||
if (version != 1) {
|
||||
std::cerr << "Error: Unsupported pack version: " << version << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t resourceCount;
|
||||
file.read(reinterpret_cast<char*>(&resourceCount), sizeof(resourceCount));
|
||||
|
||||
resources.clear();
|
||||
resources.reserve(resourceCount);
|
||||
|
||||
for (uint32_t i = 0; i < resourceCount; ++i) {
|
||||
uint32_t filenameLength;
|
||||
file.read(reinterpret_cast<char*>(&filenameLength), sizeof(filenameLength));
|
||||
|
||||
std::string filename(filenameLength, '\0');
|
||||
file.read(&filename[0], filenameLength);
|
||||
|
||||
ResourceEntry entry;
|
||||
entry.filename = filename;
|
||||
file.read(reinterpret_cast<char*>(&entry.offset), sizeof(entry.offset));
|
||||
file.read(reinterpret_cast<char*>(&entry.size), sizeof(entry.size));
|
||||
file.read(reinterpret_cast<char*>(&entry.checksum), sizeof(entry.checksum));
|
||||
|
||||
resources[filename] = entry;
|
||||
}
|
||||
|
||||
uint64_t dataSize;
|
||||
file.read(reinterpret_cast<char*>(&dataSize), sizeof(dataSize));
|
||||
|
||||
data.resize(dataSize);
|
||||
file.read(reinterpret_cast<char*>(data.data()), dataSize);
|
||||
|
||||
decryptData(data, DEFAULT_ENCRYPT_KEY);
|
||||
|
||||
loaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourcePack::savePack(const std::string& packFile) {
|
||||
std::ofstream file(packFile, std::ios::binary);
|
||||
if (!file) {
|
||||
std::cerr << "Error: Could not create pack file: " << packFile << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
file.write("CCAE", 4);
|
||||
|
||||
uint32_t version = 1;
|
||||
file.write(reinterpret_cast<const char*>(&version), sizeof(version));
|
||||
|
||||
uint32_t resourceCount = static_cast<uint32_t>(resources.size());
|
||||
file.write(reinterpret_cast<const char*>(&resourceCount), sizeof(resourceCount));
|
||||
|
||||
for (const auto& [filename, entry] : resources) {
|
||||
uint32_t filenameLength = static_cast<uint32_t>(filename.length());
|
||||
file.write(reinterpret_cast<const char*>(&filenameLength), sizeof(filenameLength));
|
||||
file.write(filename.c_str(), filenameLength);
|
||||
|
||||
file.write(reinterpret_cast<const char*>(&entry.offset), sizeof(entry.offset));
|
||||
file.write(reinterpret_cast<const char*>(&entry.size), sizeof(entry.size));
|
||||
file.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(entry.checksum));
|
||||
}
|
||||
|
||||
std::vector<uint8_t> encryptedData = data;
|
||||
encryptData(encryptedData, DEFAULT_ENCRYPT_KEY);
|
||||
|
||||
uint64_t dataSize = encryptedData.size();
|
||||
file.write(reinterpret_cast<const char*>(&dataSize), sizeof(dataSize));
|
||||
file.write(reinterpret_cast<const char*>(encryptedData.data()), dataSize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourcePack::addFile(const std::string& filename, const std::string& filepath) {
|
||||
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
std::cerr << "Error: Could not open file: " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::streamsize fileSize = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> fileData(fileSize);
|
||||
if (!file.read(reinterpret_cast<char*>(fileData.data()), fileSize)) {
|
||||
std::cerr << "Error: Could not read file: " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
ResourceEntry entry;
|
||||
entry.filename = filename;
|
||||
entry.offset = data.size();
|
||||
entry.size = fileData.size();
|
||||
entry.checksum = calculateChecksum(fileData);
|
||||
|
||||
data.insert(data.end(), fileData.begin(), fileData.end());
|
||||
resources[filename] = entry;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourcePack::addDirectory(const std::string& directory) {
|
||||
if (!std::filesystem::exists(directory)) {
|
||||
std::cerr << "Error: Directory does not exist: " << directory << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
|
||||
if (entry.is_regular_file()) {
|
||||
std::string filepath = entry.path().string();
|
||||
std::string filename = std::filesystem::relative(entry.path(), directory).string();
|
||||
|
||||
std::replace(filename.begin(), filename.end(), '\\', '/');
|
||||
|
||||
if (!addFile(filename, filepath)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ResourcePack::getResource(const std::string& filename) {
|
||||
auto it = resources.find(filename);
|
||||
if (it == resources.end()) {
|
||||
std::cerr << "Error: Resource not found: " << filename << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
const ResourceEntry& entry = it->second;
|
||||
if (entry.offset + entry.size > data.size()) {
|
||||
std::cerr << "Error: Invalid resource data: " << filename << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<uint8_t> result(data.begin() + entry.offset,
|
||||
data.begin() + entry.offset + entry.size);
|
||||
|
||||
uint32_t checksum = calculateChecksum(result);
|
||||
if (checksum != entry.checksum) {
|
||||
std::cerr << "Warning: Checksum mismatch for resource: " << filename << std::endl;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ResourcePack::hasResource(const std::string& filename) const {
|
||||
return resources.find(filename) != resources.end();
|
||||
}
|
||||
|
||||
void ResourcePack::clear() {
|
||||
resources.clear();
|
||||
data.clear();
|
||||
loaded = false;
|
||||
}
|
||||
|
||||
size_t ResourcePack::getResourceCount() const {
|
||||
return resources.size();
|
||||
}
|
||||
|
||||
std::vector<std::string> ResourcePack::getResourceList() const {
|
||||
std::vector<std::string> result;
|
||||
result.reserve(resources.size());
|
||||
|
||||
for (const auto& [filename, entry] : resources) {
|
||||
result.push_back(filename);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
46
source/resource_pack.h
Normal file
46
source/resource_pack.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef RESOURCE_PACK_H
|
||||
#define RESOURCE_PACK_H
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
struct ResourceEntry {
|
||||
std::string filename;
|
||||
uint64_t offset;
|
||||
uint64_t size;
|
||||
uint32_t checksum;
|
||||
};
|
||||
|
||||
class ResourcePack {
|
||||
private:
|
||||
std::unordered_map<std::string, ResourceEntry> resources;
|
||||
std::vector<uint8_t> data;
|
||||
bool loaded;
|
||||
|
||||
uint32_t calculateChecksum(const std::vector<uint8_t>& data);
|
||||
void encryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||
void decryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||
|
||||
public:
|
||||
ResourcePack();
|
||||
~ResourcePack();
|
||||
|
||||
bool loadPack(const std::string& packFile);
|
||||
bool savePack(const std::string& packFile);
|
||||
|
||||
bool addFile(const std::string& filename, const std::string& filepath);
|
||||
bool addDirectory(const std::string& directory);
|
||||
|
||||
std::vector<uint8_t> getResource(const std::string& filename);
|
||||
bool hasResource(const std::string& filename) const;
|
||||
|
||||
void clear();
|
||||
size_t getResourceCount() const;
|
||||
std::vector<std::string> getResourceList() const;
|
||||
|
||||
static const std::string DEFAULT_ENCRYPT_KEY;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user