estandarditza la sortida de pack_resources

This commit is contained in:
2026-05-18 17:55:18 +02:00
parent 25a6832351
commit 5349c60c39
+61 -74
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 <filesystem>
#include <iostream>
#include <string>
void showHelp() { #include "core/resources/resource_pack.hpp"
std::cout << Version::APP_NAME << " - Resource Packer" << std::endl; #include "../../build/version.h" // Version::APP_NAME
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;
}
void listPackContents(const std::string& packFile) { 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";
}
void listPackContents(const std::string& pack_file) {
ResourcePack pack; ResourcePack pack;
if (!pack.loadPack(packFile)) { if (!pack.loadPack(pack_file)) {
std::cerr << "Error: Cannot open pack file: " << packFile << std::endl; std::cerr << "Error: cannot open pack file: " << pack_file << '\n';
return; return;
} }
auto resources = pack.getResourceList(); auto resources = pack.getResourceList();
std::cout << "Pack file: " << packFile << std::endl; std::cout << "Pack file: " << pack_file << '\n';
std::cout << "Resources: " << resources.size() << std::endl; std::cout << "Resources: " << resources.size() << '\n';
std::cout << "Contents:" << std::endl; for (const auto& r : resources) { std::cout << " " << r << '\n'; }
for (const auto& resource : resources) {
std::cout << " " << resource << std::endl;
} }
}
} // namespace
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::string dataDir = "data"; std::string data_dir = "data";
std::string outputFile = "resources.pack"; std::string output_file = "resources.pack";
bool listMode = false; bool list_mode = false;
bool dataDirSet = 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]; std::string arg = argv[i];
if (arg == "--help" || arg == "-h") { if (arg == "--help" || arg == "-h") {
showHelp(); showHelp();
return 0; 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 (arg == "--list") {
if (!dataDirSet) { list_mode = true;
dataDir = arg; if (i + 1 < argc) { output_file = argv[++i]; }
dataDirSet = true; continue;
}
if (!arg.empty() && arg[0] != '-') {
if (!data_dir_set) {
data_dir = arg;
data_dir_set = true;
} else { } else {
outputFile = arg; output_file = arg;
} }
} }
} }
if (listMode) { if (list_mode) {
listPackContents(outputFile); listPackContents(output_file);
return 0; return 0;
} }
std::cout << Version::APP_NAME << " - Resource Packer" << std::endl; std::cout << Version::APP_NAME << " - Resource Packer\n";
std::cout << "===============================================" << std::endl; std::cout << "==============================================\n";
std::cout << "Input directory: " << dataDir << std::endl; std::cout << "Input directory: " << data_dir << '\n';
std::cout << "Output file: " << outputFile << std::endl; std::cout << "Output file: " << output_file << '\n';
std::cout << std::endl;
if (!std::filesystem::exists(dataDir)) { if (!std::filesystem::exists(data_dir)) {
std::cerr << "Error: Input directory does not exist: " << dataDir << std::endl; std::cerr << "Error: input directory does not exist: " << data_dir << '\n';
return 1; return 1;
} }
ResourcePack pack; ResourcePack pack;
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\n";
std::cout << "Scanning and packing resources..." << std::endl; std::cout << "Saving pack file...\n";
if (!pack.addDirectory(dataDir)) { if (!pack.savePack(output_file)) {
std::cerr << "Error: Failed to add directory to pack" << std::endl; std::cerr << "Error: failed to save pack file\n";
return 1; return 1;
} }
std::cout << "Found " << pack.getResourceCount() << " resources" << std::endl; auto file_size = std::filesystem::file_size(std::filesystem::path(output_file));
std::cout << "Pack file created: " << output_file << " ("
std::cout << "Saving pack file..." << std::endl; << (static_cast<double>(file_size) / 1024.0 / 1024.0) << " MB)\n";
if (!pack.savePack(outputFile)) {
std::cerr << "Error: Failed to save pack file" << std::endl;
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;
return 0; return 0;
} }