diff --git a/Crafter.Asset-Asset.cpp b/Crafter.Asset-Asset.cpp index 82aa17c..fd24c52 100644 --- a/Crafter.Asset-Asset.cpp +++ b/Crafter.Asset-Asset.cpp @@ -1,5 +1,5 @@ /* -Crafter.Build +Crafter®.Asset Copyright (C) 2025 Catcrafts® Catcrafts.net @@ -33,15 +33,6 @@ 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; @@ -49,12 +40,10 @@ struct HeaderData { std::uint32_t lenght; }; -void Asset::LoadFull(fs::path path) { - std::ifstream f(path, std::ios::binary); +AssetLoad::AssetLoad(fs::path path) : f(path, std::ios::binary) { if(!f) { - throw std::runtime_error(std::format("File {} not found", path.generic_string())); + throw std::runtime_error(std::format("Error opening file {} not found", path.generic_string())); } - std::uint32_t version; f.read(reinterpret_cast(&version), sizeof(std::uint32_t)); std::uint32_t entryCount; @@ -70,27 +59,86 @@ void Asset::LoadFull(fs::path path) { f.read(reinterpret_cast(&entries[i].type[0]), header.typeLenght); lenghts[i] = header.lenght; } - 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]); - } + headerLength = sizeof(std::uint32_t)*2 + (sizeof(std::uint32_t)*4*entryCount); } -void Asset::LoadHeaders(fs::path path) { + +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::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 Asset::LoadSpecific(fs::path path, std::uint32_t entry) { - -} -void Asset::LoadSpecific(fs::path path, std::vector entries) { - -} -void Asset::Save(fs::path path) { +void AssetSave::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)); @@ -103,14 +151,10 @@ void Asset::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)); - std::uint32_t dataSize = entry.data.size(); - f.write(reinterpret_cast(&dataSize), sizeof(std::uint32_t)); - offset+=dataSize; + f.write(reinterpret_cast(&entry.lenght), sizeof(std::uint32_t)); + offset+=entry.lenght; f.write(reinterpret_cast(&entry.name[0]), entry.name.size()); f.write(reinterpret_cast(&entry.type[0]), entry.type.size()); } - for(const AssetEntry& entry : entries) { - f.write(reinterpret_cast(entry.data.data()), entry.data.size()); - } - f.close(); + f.write(reinterpret_cast(buffer.data()), buffer.size()); } \ No newline at end of file diff --git a/Crafter.Asset-Asset.cppm b/Crafter.Asset-Asset.cppm index 8ef80a9..db7c6b7 100644 --- a/Crafter.Asset-Asset.cppm +++ b/Crafter.Asset-Asset.cppm @@ -1,5 +1,5 @@ /* -Crafter.Build +Crafter®.Asset Copyright (C) 2025 Catcrafts® Catcrafts.net @@ -193,22 +193,52 @@ export namespace Crafter { public: std::string name; std::string type; - std::vector data; - AssetEntry(); - AssetEntry(std::string name, std::string type, std::vector data); - AssetEntry(std::string name, std::string type, void* data); + 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) { + + } }; - class Asset { + + class AssetLoad { public: std::vector entries; - 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); + 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); void Save(fs::path path); }; } \ No newline at end of file diff --git a/Crafter.Asset.cppm b/Crafter.Asset.cppm index 9d5a857..2016956 100644 --- a/Crafter.Asset.cppm +++ b/Crafter.Asset.cppm @@ -1,5 +1,5 @@ /* -Crafter.Build +Crafter®.Asset Copyright (C) 2025 Catcrafts® Catcrafts.net diff --git a/bin/Crafter.Asset-Asset.pcm b/bin/Crafter.Asset-Asset.pcm new file mode 100644 index 0000000..74bcd03 Binary files /dev/null and b/bin/Crafter.Asset-Asset.pcm differ diff --git a/bin/Crafter.Asset.pcm b/bin/Crafter.Asset.pcm new file mode 100644 index 0000000..280e206 Binary files /dev/null and b/bin/Crafter.Asset.pcm differ diff --git a/bin/crafter-asset b/bin/crafter-asset index 90f04dc..8f09d0f 100755 Binary files a/bin/crafter-asset and b/bin/crafter-asset differ diff --git a/bin/libcrafter-asset.a b/bin/libcrafter-asset.a new file mode 100644 index 0000000..ecbab12 Binary files /dev/null and b/bin/libcrafter-asset.a differ diff --git a/build/executable-debug/Crafter.Asset-Asset.o b/build/executable-debug/Crafter.Asset-Asset.o new file mode 100644 index 0000000..57a48ba Binary files /dev/null and b/build/executable-debug/Crafter.Asset-Asset.o differ diff --git a/build/executable-debug/Crafter.Asset-Asset.pcm b/build/executable-debug/Crafter.Asset-Asset.pcm new file mode 100644 index 0000000..f002456 Binary files /dev/null and b/build/executable-debug/Crafter.Asset-Asset.pcm differ diff --git a/build/executable-debug/Crafter.Asset-Asset_source.o b/build/executable-debug/Crafter.Asset-Asset_source.o new file mode 100644 index 0000000..16fa0e3 Binary files /dev/null and b/build/executable-debug/Crafter.Asset-Asset_source.o differ diff --git a/build/executable-debug/Crafter.Asset.o b/build/executable-debug/Crafter.Asset.o new file mode 100644 index 0000000..be926d1 Binary files /dev/null and b/build/executable-debug/Crafter.Asset.o differ diff --git a/build/executable-debug/Crafter.Asset.pcm b/build/executable-debug/Crafter.Asset.pcm new file mode 100644 index 0000000..4780c6b Binary files /dev/null and b/build/executable-debug/Crafter.Asset.pcm differ diff --git a/build/executable-debug/main_source.o b/build/executable-debug/main_source.o new file mode 100644 index 0000000..3a2b880 Binary files /dev/null and b/build/executable-debug/main_source.o differ diff --git a/build/executable-release/Crafter.Asset-Asset.o b/build/executable-release/Crafter.Asset-Asset.o new file mode 100644 index 0000000..37157ba Binary files /dev/null and b/build/executable-release/Crafter.Asset-Asset.o differ diff --git a/build/executable-release/Crafter.Asset-Asset.pcm b/build/executable-release/Crafter.Asset-Asset.pcm new file mode 100644 index 0000000..b9e3069 Binary files /dev/null and b/build/executable-release/Crafter.Asset-Asset.pcm differ diff --git a/build/executable-release/Crafter.Asset-Asset_source.o b/build/executable-release/Crafter.Asset-Asset_source.o new file mode 100644 index 0000000..dd6637d Binary files /dev/null and b/build/executable-release/Crafter.Asset-Asset_source.o differ diff --git a/build/executable-release/Crafter.Asset.o b/build/executable-release/Crafter.Asset.o new file mode 100644 index 0000000..ad88031 Binary files /dev/null and b/build/executable-release/Crafter.Asset.o differ diff --git a/build/executable-release/Crafter.Asset.pcm b/build/executable-release/Crafter.Asset.pcm new file mode 100644 index 0000000..eb7124b Binary files /dev/null and b/build/executable-release/Crafter.Asset.pcm differ diff --git a/build/lib-debug/Crafter.Asset-Asset.o b/build/lib-debug/Crafter.Asset-Asset.o index a42684c..c477b1d 100644 Binary files a/build/lib-debug/Crafter.Asset-Asset.o 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 index eb4acbd..50e1527 100644 Binary files a/build/lib-debug/Crafter.Asset-Asset_source.o 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 index 465b18f..be926d1 100644 Binary files a/build/lib-debug/Crafter.Asset.o and b/build/lib-debug/Crafter.Asset.o differ diff --git a/main.cpp b/main.cpp index 0053db9..61edd59 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ /* -Crafter.Build +Crafter®.Asset Copyright (C) 2025 Catcrafts® Catcrafts.net @@ -35,7 +35,6 @@ int main(int argc, char* argv[]) { return -1; } - std::string command = std::string(argv[1]); if(command == "help") { @@ -59,11 +58,10 @@ int main(int argc, char* argv[]) { std::cout << "Asset file does not exist" << std::endl; return -1; } - Asset asset; - asset.LoadFull(argv[2]); + AssetLoad asset(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.data.size()) << std::endl; + std::cout << std::format("{}\t{}\t{}", entry.name, entry.type, entry.lenght) << std::endl; } } } else if(command == "remove") { @@ -78,8 +76,8 @@ int main(int argc, char* argv[]) { std::cout << "Asset file does not exist" << std::endl; return -1; } - Asset asset; - asset.LoadFull(argv[2]); + AssetLoad asset(argv[2]); + if(asset.entries.size() == 0) { std::cout << "Nothing to remove, asset file empty" << std::endl; } @@ -87,7 +85,8 @@ int main(int argc, char* argv[]) { if (it->name == argv[3]) { asset.entries.erase(it); fs::remove(argv[2]); - asset.Save(argv[2]); + AssetSave ass2(asset.entries, std::move(asset.LoadAll())); + ass2.Save(argv[2]); return 0; } std::cout << std::format("Entry {} not found", argv[3]) << std::endl; @@ -110,11 +109,6 @@ 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]); @@ -123,11 +117,16 @@ int main(int argc, char* argv[]) { 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]); + 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]); + } } else if(command == "extract") { if(argc == 2) { std::cout << "Missing argument {file}. Run help for help" << std::endl; @@ -146,8 +145,7 @@ int main(int argc, char* argv[]) { std::cout << "Asset file does not exist" << std::endl; return -1; } - Asset asset; - asset.LoadFull(argv[2]); + AssetLoad asset(argv[2]); auto pos = extractors.find(argv[4]); if (pos == extractors.end()) { std::cout << std::format("Unkown extractor {}", argv[4]) << std::endl; @@ -155,8 +153,8 @@ int main(int argc, char* argv[]) { } else { for(const AssetEntry& entry : asset.entries) { if(entry.name == std::string(argv[3])) { - pos->second(argv[5], entry.data.data()); - return 0; + std::vector loaded = asset.Load(entry); + pos->second(argv[5], loaded.data()); } } std::cout << std::format("Entry {} not found", argv[3]) << std::endl; @@ -165,18 +163,4 @@ int main(int argc, char* argv[]) { } 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()); -} - - +} \ No newline at end of file