Crafter.Asset/main.cpp
2025-05-06 12:37:14 +02:00

162 lines
6.7 KiB
C++

#include <iostream>
#include <string>
#include <filesystem>
#include <unordered_map>
#include <vector>
#include <functional>
import Crafter.Asset;
using namespace Crafter;
namespace fs = std::filesystem;
int main(int argc, char* argv[]) {
if(argc == 1) {
std::cout << "No arguments provided, use help for help" << std::endl;
return -1;
}
std::string command = std::string(argv[1]);
if(command == "help") {
std::cout << "help: Displays this help message.\nadd {file} {entry file} {entry type} {serializer} {entry name}: Adds an entry to an existing assset file or creates a new file if the file in {file} does not exist, {entry file} is the path to the file you want to add to the asset, {entry type} is the type of the resulting entry, {serializer} is the serializer to use to add the entry file to the asset, {entry name} is the name of the resulting entry\nremove {file} {entry}\nextract {file} {entry name} {extractor} {extracted path}: Extracts a entry with the name {entry name} from the asset file in {file}, using the extractor in {extractor} writes the result to the file {extracted path}\nlist serialize: Shows a list of the support serializers\nlist {file}: Lists all entries in the asset file\nlist extract: Shows a list of supported extractors" << std::endl;
} else if(command == "list") {
if(argc == 2) {
std::cout << "Unkown command, use help for help" << std::endl;
return -1;
}
std::string command2 = std::string(argv[2]);
if(command2 == "serialize") {
for (const auto& pair : serializers) {
std::cout << pair.first << std::endl;
}
} else if(command2 == "extract") {
for (const auto& pair : extractors) {
std::cout << pair.first << std::endl;
}
} else {
if(!fs::exists(argv[2])) {
std::cout << "Asset file does not exist" << std::endl;
return -1;
}
Asset asset;
asset.LoadFull(argv[2]);
std::cout << "<name>\t<type>\t<size>" << std::endl;
for(const AssetEntry& entry : asset.entries) {
std::cout << std::format("{}\t{}\t{}", entry.name, entry.type, entry.data.size()) << std::endl;
}
}
} else if(command == "remove") {
if(argc == 2) {
std::cout << "Missing argument {file}. Run help for help" << std::endl;
return -1;
} else if(argc == 3) {
std::cout << "Missing argument {enty name}. Run help for help" << std::endl;
return -1;
}
if(!fs::exists(argv[2])) {
std::cout << "Asset file does not exist" << std::endl;
return -1;
}
Asset asset;
asset.LoadFull(argv[2]);
if(asset.entries.size() == 0) {
std::cout << "Nothing to remove, asset file empty" << std::endl;
}
for (auto it = asset.entries.begin(); it != asset.entries.end(); std::advance(it, 1)) {
if (it->name == argv[3]) {
asset.entries.erase(it);
fs::remove(argv[2]);
asset.Save(argv[2]);
return 0;
}
std::cout << std::format("Entry {} not found", argv[3]) << std::endl;
return -1;
}
} else if(command == "add") {
if(argc == 2) {
std::cout << "Missing argument {file}. Run help for help" << std::endl;
return -1;
} else if(argc == 3) {
std::cout << "Missing argument {entry file}. Run help for help" << std::endl;
return -1;
} else if(argc == 4) {
std::cout << "Missing argument {entry type}. Run help for help" << std::endl;
return -1;
} else if(argc == 5) {
std::cout << "Missing argument {serializer}. Run help for help" << std::endl;
return -1;
} else if(argc == 6) {
std::cout << "Missing argument {entry name}. Run help for help" << std::endl;
return -1;
}
Asset asset;
if(fs::exists(argv[2])) {
asset.LoadFull(argv[2]);
fs::remove(argv[2]);
}
AssetEntry entry;
entry.name = std::string(argv[6]);
entry.type = std::string(argv[4]);
std::unordered_map<std::string, std::function<std::vector<char>(fs::path)>>::const_iterator pos = serializers.find(argv[5]);
if (pos == serializers.end()) {
std::cout << std::format("Unkown serializer {}", argv[5]) << std::endl;
return -1;
} else {
entry.data = pos->second(argv[3]);
}
asset.entries.push_back(entry);
asset.Save(argv[2]);
} else if(command == "extract") {
if(argc == 2) {
std::cout << "Missing argument {file}. Run help for help" << std::endl;
return -1;
} else if(argc == 3) {
std::cout << "Missing argument {entry name}. Run help for help" << std::endl;
return -1;
} else if(argc == 4) {
std::cout << "Missing argument {extractor}. Run help for help" << std::endl;
return -1;
} else if(argc == 5) {
std::cout << "Missing argument {extracted path}. Run help for help" << std::endl;
return -1;
}
if(!fs::exists(argv[2])) {
std::cout << "Asset file does not exist" << std::endl;
return -1;
}
Asset asset;
asset.LoadFull(argv[2]);
auto pos = extractors.find(argv[4]);
if (pos == extractors.end()) {
std::cout << std::format("Unkown extractor {}", argv[4]) << std::endl;
return -1;
} else {
for(const AssetEntry& entry : asset.entries) {
if(entry.name == std::string(argv[3])) {
pos->second(argv[5], entry.data.data());
return 0;
}
}
std::cout << std::format("Entry {} not found", argv[3]) << std::endl;
return -1;
}
} else {
std::cout << "Unkown command, use help for help" << std::endl;
}
// Asset test;
// AssetEntry entry;
// entry.name = "Logo";
// entry.type = "Crafter.Graphics.TextureR8B8G8A8";
// //entry.data = serializers.find("PNG:Crafter.Graphics.TextureR8B8G8A8")->second("/home/jorijn/Pictures/3dfortslogo.png");
// test.entries.push_back(entry);
// test.Save("test.cras");
// Asset test2;
// test2.LoadFull("test.cras");
//extractors.find(std::format("{}:PNG", test2.entries[0].type))->second("/home/jorijn/repos/Crafter/Crafter.Asset/test.png", test2.entries[0].data.data());
}