estandarditza la sortida de pack_resources

This commit is contained in:
2026-05-18 17:55:18 +02:00
parent 25a6832351
commit 5349c60c39
+70 -83
View File
@@ -1,109 +1,96 @@
#include "core/resources/resource_pack.hpp"
#include "../../build/version.h" // Para Version::APP_NAME
#include <iostream>
#include <filesystem>
#include <iostream>
#include <string>
void showHelp() {
std::cout << Version::APP_NAME << " - Resource Packer" << std::endl;
std::cout << "===============================================" << std::endl;
std::cout << "Usage: pack_resources [options] [input_dir] [output_file]" << std::endl;
std::cout << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --help Show this help message" << std::endl;
std::cout << " --list List contents of an existing pack file" << std::endl;
std::cout << std::endl;
std::cout << "Arguments:" << std::endl;
std::cout << " input_dir Directory to pack (default: data)" << std::endl;
std::cout << " output_file Pack file name (default: resources.pack)" << std::endl;
std::cout << std::endl;
std::cout << "Examples:" << std::endl;
std::cout << " pack_resources # Pack 'data' to 'resources.pack'" << std::endl;
std::cout << " pack_resources mydata # Pack 'mydata' to 'resources.pack'" << std::endl;
std::cout << " pack_resources data my.pack # Pack 'data' to 'my.pack'" << std::endl;
std::cout << " pack_resources --list my.pack # List contents of 'my.pack'" << std::endl;
}
#include "core/resources/resource_pack.hpp"
#include "../../build/version.h" // Version::APP_NAME
void listPackContents(const std::string& packFile) {
ResourcePack pack;
if (!pack.loadPack(packFile)) {
std::cerr << "Error: Cannot open pack file: " << packFile << std::endl;
return;
namespace {
void showHelp() {
std::cout << Version::APP_NAME << " - Resource Packer\n";
std::cout << "==============================================\n";
std::cout << "Usage: pack_resources [options] [input_dir] [output_file]\n\n";
std::cout << "Options:\n";
std::cout << " --help Show this help message\n";
std::cout << " --list List contents of an existing pack file\n\n";
std::cout << "Arguments:\n";
std::cout << " input_dir Directory to pack (default: data)\n";
std::cout << " output_file Pack file name (default: resources.pack)\n";
}
auto resources = pack.getResourceList();
std::cout << "Pack file: " << packFile << std::endl;
std::cout << "Resources: " << resources.size() << std::endl;
std::cout << "Contents:" << std::endl;
for (const auto& resource : resources) {
std::cout << " " << resource << std::endl;
void listPackContents(const std::string& pack_file) {
ResourcePack pack;
if (!pack.loadPack(pack_file)) {
std::cerr << "Error: cannot open pack file: " << pack_file << '\n';
return;
}
auto resources = pack.getResourceList();
std::cout << "Pack file: " << pack_file << '\n';
std::cout << "Resources: " << resources.size() << '\n';
for (const auto& r : resources) { std::cout << " " << r << '\n'; }
}
}
} // namespace
int main(int argc, char* argv[]) {
std::string dataDir = "data";
std::string outputFile = "resources.pack";
bool listMode = false;
bool dataDirSet = false;
std::string data_dir = "data";
std::string output_file = "resources.pack";
bool list_mode = false;
bool data_dir_set = false;
// Parse arguments
for (int i = 1; i < argc; i++) {
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
showHelp();
return 0;
} else if (arg == "--list") {
listMode = true;
if (i + 1 < argc) {
outputFile = argv[++i]; // Next argument is pack file to list
}
} else if (!arg.empty() && arg[0] != '-') {
if (!dataDirSet) {
dataDir = arg;
dataDirSet = true;
}
if (arg == "--list") {
list_mode = true;
if (i + 1 < argc) { output_file = argv[++i]; }
continue;
}
if (!arg.empty() && arg[0] != '-') {
if (!data_dir_set) {
data_dir = arg;
data_dir_set = true;
} else {
outputFile = arg;
output_file = arg;
}
}
}
if (listMode) {
listPackContents(outputFile);
if (list_mode) {
listPackContents(output_file);
return 0;
}
std::cout << Version::APP_NAME << " - Resource Packer" << std::endl;
std::cout << "===============================================" << std::endl;
std::cout << "Input directory: " << dataDir << std::endl;
std::cout << "Output file: " << outputFile << std::endl;
std::cout << std::endl;
if (!std::filesystem::exists(dataDir)) {
std::cerr << "Error: Input directory does not exist: " << dataDir << std::endl;
std::cout << Version::APP_NAME << " - Resource Packer\n";
std::cout << "==============================================\n";
std::cout << "Input directory: " << data_dir << '\n';
std::cout << "Output file: " << output_file << '\n';
if (!std::filesystem::exists(data_dir)) {
std::cerr << "Error: input directory does not exist: " << data_dir << '\n';
return 1;
}
ResourcePack pack;
std::cout << "Scanning and packing resources..." << std::endl;
if (!pack.addDirectory(dataDir)) {
std::cerr << "Error: Failed to add directory to pack" << std::endl;
std::cout << "Scanning and packing resources...\n";
if (!pack.addDirectory(data_dir)) {
std::cerr << "Error: failed to add directory to pack\n";
return 1;
}
std::cout << "Found " << pack.getResourceCount() << " resources" << std::endl;
std::cout << "Saving pack file..." << std::endl;
if (!pack.savePack(outputFile)) {
std::cerr << "Error: Failed to save pack file" << std::endl;
std::cout << "Found " << pack.getResourceCount() << " resources\n";
std::cout << "Saving pack file...\n";
if (!pack.savePack(output_file)) {
std::cerr << "Error: failed to save pack file\n";
return 1;
}
std::filesystem::path packPath(outputFile);
auto fileSize = std::filesystem::file_size(packPath);
std::cout << "Pack file created successfully!" << std::endl;
std::cout << "File size: " << (fileSize / 1024.0 / 1024.0) << " MB" << std::endl;
auto file_size = std::filesystem::file_size(std::filesystem::path(output_file));
std::cout << "Pack file created: " << output_file << " ("
<< (static_cast<double>(file_size) / 1024.0 / 1024.0) << " MB)\n";
return 0;
}
}