diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 4e9c87a..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -build/ -bin/ \ No newline at end of file diff --git a/Crafter.Asset-Asset.cpp b/Crafter.Asset-Asset.cpp index fd24c52..82aa17c 100644 --- a/Crafter.Asset-Asset.cpp +++ b/Crafter.Asset-Asset.cpp @@ -1,5 +1,5 @@ /* -Crafter®.Asset +Crafter.Build Copyright (C) 2025 Catcrafts® Catcrafts.net @@ -33,6 +33,15 @@ module Crafter.Asset; using namespace Crafter; namespace fs = std::filesystem; + +AssetEntry::AssetEntry() { + +} + + +Asset::Asset() {} +Asset::Asset(std::vector entries): entries(entries) {} + struct HeaderData { std::uint32_t nameLenght; std::uint32_t typeLenght; @@ -40,10 +49,12 @@ struct HeaderData { std::uint32_t lenght; }; -AssetLoad::AssetLoad(fs::path path) : f(path, std::ios::binary) { +void Asset::LoadFull(fs::path path) { + std::ifstream f(path, std::ios::binary); if(!f) { - throw std::runtime_error(std::format("Error opening file {} not found", path.generic_string())); + throw std::runtime_error(std::format("File {} not found", path.generic_string())); } + std::uint32_t version; f.read(reinterpret_cast(&version), sizeof(std::uint32_t)); std::uint32_t entryCount; @@ -59,86 +70,27 @@ AssetLoad::AssetLoad(fs::path path) : f(path, std::ios::binary) { f.read(reinterpret_cast(&entries[i].type[0]), header.typeLenght); lenghts[i] = header.lenght; } - headerLength = sizeof(std::uint32_t)*2 + (sizeof(std::uint32_t)*4*entryCount); + for(std::uint32_t i = 0; i < entryCount; i++) { + entries[i].data.resize(lenghts[i]); + f.read(reinterpret_cast(entries[i].data.data()), lenghts[i]); + } } - -std::vector AssetLoad::Load(std::string_view name) { - auto it = std::find_if(entries.begin(), entries.end(), [&](const auto& entry) { - return entry.name == name; - }); - return LoadOffset(it->offset, it->lenght); -} -std::vector AssetLoad::Load(std::string_view name, uint32_t lenght) { - auto it = std::find_if(entries.begin(), entries.end(), [&](const auto& entry) { - return entry.name == name; - }); - return LoadOffset(it->offset, lenght); -} -void AssetLoad::Load(std::string_view name, void* data) { - auto it = std::find_if(entries.begin(), entries.end(), [&](const auto& entry) { - return entry.name == name; - }); - return LoadOffset(it->offset, it->lenght, data); -} -void AssetLoad::Load(std::string_view name, void* data, uint32_t lenght) { - auto it = std::find_if(entries.begin(), entries.end(), [&](const auto& entry) { - return entry.name == name; - }); - return LoadOffset(it->offset, lenght, data); -} - -std::vector AssetLoad::Load(uint32_t index) { - return LoadOffset(entries[index].offset, entries[index].lenght); -} -std::vector AssetLoad::Load(uint32_t index, uint32_t lenght) { - return LoadOffset(entries[index].offset, lenght); -} -void AssetLoad::Load(uint32_t index, void* data) { - return LoadOffset(entries[index].offset, entries[index].lenght, data); -} -void AssetLoad::Load(uint32_t index, void* data, uint32_t lenght) { - return LoadOffset(entries[index].offset, lenght, data); -} - -std::vector AssetLoad::Load(const AssetEntry& entry) { - return LoadOffset(entry.offset, entry.lenght); -} -std::vector AssetLoad::Load(const AssetEntry& entry, uint32_t lenght) { - return LoadOffset(entry.offset, lenght); -} -void AssetLoad::Load(const AssetEntry& entry, void* data) { - return LoadOffset(entry.offset, entry.lenght, data); -} -void AssetLoad::Load(const AssetEntry& entry, void* data, uint32_t lenght) { - return LoadOffset(entry.offset, lenght, data); -} - -std::vector AssetLoad::LoadOffset(uint32_t offset, uint32_t lenght) { - f.seekg(offset); - std::vector vector(lenght); - f.read(vector.data(), lenght); - return vector; -} -void AssetLoad::LoadOffset(uint32_t offset, uint32_t lenght, void* data) { - f.seekg(offset); - f.read(reinterpret_cast(data), lenght); -} - -std::vector AssetLoad::LoadAll() { - f.seekg(headerLength); - return std::vector((std::istreambuf_iterator(f)), std::istreambuf_iterator()); -} - -AssetSave::AssetSave(std::span entries, std::span buffer) : entries(entries.begin(), entries.end()), buffer(buffer.begin(), buffer.end()) { +void Asset::LoadHeaders(fs::path path) { } +void Asset::LoadSpecific(fs::path path, std::string entry) { -void AssetSave::AddEntry(std::string_view name, std::string_view type, std::span data) { - entries.emplace_back(name, type, data.size()); - buffer.insert(buffer.end(), data.begin(), data.end()); } +void Asset::LoadSpecific(fs::path path, std::vector entries) { -void AssetSave::Save(fs::path path) { +} +void Asset::LoadSpecific(fs::path path, std::uint32_t entry) { + +} +void Asset::LoadSpecific(fs::path path, std::vector entries) { + +} +void Asset::Save(fs::path path) { std::ofstream f(path, std::ios::binary); std::uint32_t version = 0; f.write(reinterpret_cast(&version), sizeof(std::uint32_t)); @@ -151,10 +103,14 @@ void AssetSave::Save(fs::path path) { std::uint32_t typeSize = entry.type.size(); f.write(reinterpret_cast(&typeSize), sizeof(std::uint32_t)); f.write(reinterpret_cast(&offset), sizeof(std::uint32_t)); - f.write(reinterpret_cast(&entry.lenght), sizeof(std::uint32_t)); - offset+=entry.lenght; + std::uint32_t dataSize = entry.data.size(); + f.write(reinterpret_cast(&dataSize), sizeof(std::uint32_t)); + offset+=dataSize; f.write(reinterpret_cast(&entry.name[0]), entry.name.size()); f.write(reinterpret_cast(&entry.type[0]), entry.type.size()); } - f.write(reinterpret_cast(buffer.data()), buffer.size()); + for(const AssetEntry& entry : entries) { + f.write(reinterpret_cast(entry.data.data()), entry.data.size()); + } + f.close(); } \ No newline at end of file diff --git a/Crafter.Asset-Asset.cppm b/Crafter.Asset-Asset.cppm index db7c6b7..8ef80a9 100644 --- a/Crafter.Asset-Asset.cppm +++ b/Crafter.Asset-Asset.cppm @@ -1,5 +1,5 @@ /* -Crafter®.Asset +Crafter.Build Copyright (C) 2025 Catcrafts® Catcrafts.net @@ -193,52 +193,22 @@ export namespace Crafter { public: std::string name; std::string type; - uint32_t offset; - uint32_t lenght; - AssetEntry() = default; - AssetEntry(std::string_view name, std::string_view type, uint32_t lenght) : name(name), type(type), lenght(lenght) { - - } + std::vector data; + AssetEntry(); + AssetEntry(std::string name, std::string type, std::vector data); + AssetEntry(std::string name, std::string type, void* data); }; - - class AssetLoad { + class Asset { public: std::vector entries; - AssetLoad(fs::path path); - AssetLoad() = default; - - std::vector Load(std::string_view name); - std::vector Load(std::string_view name, uint32_t lenght); - void Load(std::string_view name, void* data); - void Load(std::string_view name, void* data, uint32_t lenght); - - std::vector Load(uint32_t index); - std::vector Load(uint32_t index, uint32_t lenght); - void Load(uint32_t index, void* data); - void Load(uint32_t index, void* data, uint32_t lenght); - - std::vector Load(const AssetEntry& entry); - std::vector Load(const AssetEntry& entry, uint32_t lenght); - void Load(const AssetEntry& entry, void* data); - void Load(const AssetEntry& entry, void* data, uint32_t lenght); - - std::vector LoadOffset(uint32_t offset, uint32_t lenght); - void LoadOffset(uint32_t offset, uint32_t lenght, void* data); - - std::vector LoadAll(); - private: - uint32_t headerLength; - std::ifstream f; - }; - - - class AssetSave { - public: - std::vector entries; - std::vector buffer; - AssetSave() = default; - AssetSave(std::span entries, std::span buffer); - void AddEntry(std::string_view name, std::string_view type, std::span data); + Asset(); + Asset(std::vector entries); + void LoadFull(fs::path path); + void LoadHeaders(fs::path path); + void LoadSpecific(fs::path path, std::string entry); + void LoadSpecific(fs::path path, std::vector entries); + void LoadSpecific(fs::path path, std::uint32_t entry); + void LoadSpecific(fs::path path, std::vector entries); void Save(fs::path path); }; } \ No newline at end of file diff --git a/Crafter.Asset.cppm b/Crafter.Asset.cppm index 2016956..9d5a857 100644 --- a/Crafter.Asset.cppm +++ b/Crafter.Asset.cppm @@ -1,5 +1,5 @@ /* -Crafter®.Asset +Crafter.Build Copyright (C) 2025 Catcrafts® Catcrafts.net diff --git a/bin/crafter-asset b/bin/crafter-asset new file mode 100755 index 0000000..90f04dc Binary files /dev/null and b/bin/crafter-asset differ diff --git a/build/Crafter.Asset-Asset.o b/build/Crafter.Asset-Asset.o new file mode 100644 index 0000000..2a23377 Binary files /dev/null and b/build/Crafter.Asset-Asset.o differ diff --git a/build/Crafter.Asset-Asset.pcm b/build/Crafter.Asset-Asset.pcm new file mode 100644 index 0000000..6113287 Binary files /dev/null and b/build/Crafter.Asset-Asset.pcm differ diff --git a/build/Crafter.Asset-Asset_source.o b/build/Crafter.Asset-Asset_source.o new file mode 100644 index 0000000..9e8244c Binary files /dev/null and b/build/Crafter.Asset-Asset_source.o differ diff --git a/build/Crafter.Asset.o b/build/Crafter.Asset.o new file mode 100644 index 0000000..ad88031 Binary files /dev/null and b/build/Crafter.Asset.o differ diff --git a/build/Crafter.Asset.pcm b/build/Crafter.Asset.pcm new file mode 100644 index 0000000..8eb7796 Binary files /dev/null and b/build/Crafter.Asset.pcm differ diff --git a/build/executable-linux-debug/Crafter.Asset-Asset.o b/build/executable-linux-debug/Crafter.Asset-Asset.o new file mode 100644 index 0000000..a6a6189 Binary files /dev/null and b/build/executable-linux-debug/Crafter.Asset-Asset.o differ diff --git a/build/executable-linux-debug/Crafter.Asset-Asset.pcm b/build/executable-linux-debug/Crafter.Asset-Asset.pcm new file mode 100644 index 0000000..addd40e Binary files /dev/null and b/build/executable-linux-debug/Crafter.Asset-Asset.pcm differ diff --git a/build/executable-linux-debug/Crafter.Asset-Asset_source.o b/build/executable-linux-debug/Crafter.Asset-Asset_source.o new file mode 100644 index 0000000..afa6afb Binary files /dev/null and b/build/executable-linux-debug/Crafter.Asset-Asset_source.o differ diff --git a/build/executable-linux-debug/Crafter.Asset.o b/build/executable-linux-debug/Crafter.Asset.o new file mode 100644 index 0000000..94b638e Binary files /dev/null and b/build/executable-linux-debug/Crafter.Asset.o differ diff --git a/build/executable-linux-debug/Crafter.Asset.pcm b/build/executable-linux-debug/Crafter.Asset.pcm new file mode 100644 index 0000000..4e792fd Binary files /dev/null and b/build/executable-linux-debug/Crafter.Asset.pcm differ diff --git a/build/executable-linux-debug/main_source.o b/build/executable-linux-debug/main_source.o new file mode 100644 index 0000000..ab23a05 Binary files /dev/null and b/build/executable-linux-debug/main_source.o differ diff --git a/build/lib-debug/Crafter.Asset-Asset.o b/build/lib-debug/Crafter.Asset-Asset.o new file mode 100644 index 0000000..a42684c Binary files /dev/null and b/build/lib-debug/Crafter.Asset-Asset.o differ diff --git a/build/lib-debug/Crafter.Asset-Asset_source.o b/build/lib-debug/Crafter.Asset-Asset_source.o new file mode 100644 index 0000000..eb4acbd Binary files /dev/null and b/build/lib-debug/Crafter.Asset-Asset_source.o differ diff --git a/build/lib-debug/Crafter.Asset.o b/build/lib-debug/Crafter.Asset.o new file mode 100644 index 0000000..465b18f Binary files /dev/null and b/build/lib-debug/Crafter.Asset.o differ diff --git a/build/main_source.o b/build/main_source.o new file mode 100644 index 0000000..f206b16 Binary files /dev/null and b/build/main_source.o differ diff --git a/main.cpp b/main.cpp index 61edd59..0053db9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ /* -Crafter®.Asset +Crafter.Build Copyright (C) 2025 Catcrafts® Catcrafts.net @@ -35,6 +35,7 @@ int main(int argc, char* argv[]) { return -1; } + std::string command = std::string(argv[1]); if(command == "help") { @@ -58,10 +59,11 @@ int main(int argc, char* argv[]) { std::cout << "Asset file does not exist" << std::endl; return -1; } - AssetLoad asset(argv[2]); + Asset asset; + asset.LoadFull(argv[2]); std::cout << "\t\t" << std::endl; for(const AssetEntry& entry : asset.entries) { - std::cout << std::format("{}\t{}\t{}", entry.name, entry.type, entry.lenght) << std::endl; + std::cout << std::format("{}\t{}\t{}", entry.name, entry.type, entry.data.size()) << std::endl; } } } else if(command == "remove") { @@ -76,8 +78,8 @@ int main(int argc, char* argv[]) { std::cout << "Asset file does not exist" << std::endl; return -1; } - AssetLoad asset(argv[2]); - + Asset asset; + asset.LoadFull(argv[2]); if(asset.entries.size() == 0) { std::cout << "Nothing to remove, asset file empty" << std::endl; } @@ -85,8 +87,7 @@ int main(int argc, char* argv[]) { if (it->name == argv[3]) { asset.entries.erase(it); fs::remove(argv[2]); - AssetSave ass2(asset.entries, std::move(asset.LoadAll())); - ass2.Save(argv[2]); + asset.Save(argv[2]); return 0; } std::cout << std::format("Entry {} not found", argv[3]) << std::endl; @@ -109,6 +110,11 @@ int main(int argc, char* argv[]) { 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]); @@ -117,16 +123,11 @@ int main(int argc, char* argv[]) { std::cout << std::format("Unkown serializer {}", argv[5]) << std::endl; return -1; } else { - AssetSave asset; - if(fs::exists(argv[2])) { - AssetLoad assload(argv[2]); - asset = AssetSave(assload.entries, std::move(assload.LoadAll())); - fs::remove(argv[2]); - } - - asset.AddEntry(entry.name, entry.type, pos->second(argv[3])); - asset.Save(argv[2]); + 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; @@ -145,7 +146,8 @@ int main(int argc, char* argv[]) { std::cout << "Asset file does not exist" << std::endl; return -1; } - AssetLoad asset(argv[2]); + 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; @@ -153,8 +155,8 @@ int main(int argc, char* argv[]) { } else { for(const AssetEntry& entry : asset.entries) { if(entry.name == std::string(argv[3])) { - std::vector loaded = asset.Load(entry); - pos->second(argv[5], loaded.data()); + pos->second(argv[5], entry.data.data()); + return 0; } } std::cout << std::format("Entry {} not found", argv[3]) << std::endl; @@ -163,4 +165,18 @@ int main(int argc, char* argv[]) { } else { std::cout << "Unkown command, use help for help" << std::endl; } -} \ No newline at end of file + + // 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()); +} + +