Compare commits

..

No commits in common. "3e30c9d5da6ff16ed907f67bcd258fced3da9493" and "0b192c611f678d12e6521cdcfd26a7b71c6d0fe9" have entirely different histories.

21 changed files with 87 additions and 147 deletions

2
.gitignore vendored
View file

@ -1,2 +0,0 @@
build/
bin/

View file

@ -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<AssetEntry> 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<char*>(&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<char*>(&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<char*>(entries[i].data.data()), lenghts[i]);
}
}
std::vector<char> 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<char> 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<char> AssetLoad::Load(uint32_t index) {
return LoadOffset(entries[index].offset, entries[index].lenght);
}
std::vector<char> 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<char> AssetLoad::Load(const AssetEntry& entry) {
return LoadOffset(entry.offset, entry.lenght);
}
std::vector<char> 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<char> AssetLoad::LoadOffset(uint32_t offset, uint32_t lenght) {
f.seekg(offset);
std::vector<char> 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<char*>(data), lenght);
}
std::vector<char> AssetLoad::LoadAll() {
f.seekg(headerLength);
return std::vector<char>((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
}
AssetSave::AssetSave(std::span<AssetEntry const> entries, std::span<char const> 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<char const> data) {
entries.emplace_back(name, type, data.size());
buffer.insert(buffer.end(), data.begin(), data.end());
}
void Asset::LoadSpecific(fs::path path, std::vector<std::string> 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<std::uint32_t> entries) {
}
void Asset::Save(fs::path path) {
std::ofstream f(path, std::ios::binary);
std::uint32_t version = 0;
f.write(reinterpret_cast<const char*>(&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<const char*>(&typeSize), sizeof(std::uint32_t));
f.write(reinterpret_cast<const char*>(&offset), sizeof(std::uint32_t));
f.write(reinterpret_cast<const char*>(&entry.lenght), sizeof(std::uint32_t));
offset+=entry.lenght;
std::uint32_t dataSize = entry.data.size();
f.write(reinterpret_cast<const char*>(&dataSize), sizeof(std::uint32_t));
offset+=dataSize;
f.write(reinterpret_cast<const char*>(&entry.name[0]), entry.name.size());
f.write(reinterpret_cast<const char*>(&entry.type[0]), entry.type.size());
}
f.write(reinterpret_cast<const char*>(buffer.data()), buffer.size());
for(const AssetEntry& entry : entries) {
f.write(reinterpret_cast<const char*>(entry.data.data()), entry.data.size());
}
f.close();
}

View file

@ -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<char> data;
AssetEntry();
AssetEntry(std::string name, std::string type, std::vector<char> data);
AssetEntry(std::string name, std::string type, void* data);
};
class AssetLoad {
class Asset {
public:
std::vector<AssetEntry> entries;
AssetLoad(fs::path path);
AssetLoad() = default;
std::vector<char> Load(std::string_view name);
std::vector<char> 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<char> Load(uint32_t index);
std::vector<char> 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<char> Load(const AssetEntry& entry);
std::vector<char> 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<char> LoadOffset(uint32_t offset, uint32_t lenght);
void LoadOffset(uint32_t offset, uint32_t lenght, void* data);
std::vector<char> LoadAll();
private:
uint32_t headerLength;
std::ifstream f;
};
class AssetSave {
public:
std::vector<AssetEntry> entries;
std::vector<char> buffer;
AssetSave() = default;
AssetSave(std::span<AssetEntry const> entries, std::span<char const> buffer);
void AddEntry(std::string_view name, std::string_view type, std::span<char const> data);
Asset();
Asset(std::vector<AssetEntry> 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<std::string> entries);
void LoadSpecific(fs::path path, std::uint32_t entry);
void LoadSpecific(fs::path path, std::vector<std::uint32_t> entries);
void Save(fs::path path);
};
}

View file

@ -1,5 +1,5 @@
/*
Crafter®.Asset
Crafter.Build
Copyright (C) 2025 Catcrafts®
Catcrafts.net

BIN
bin/crafter-asset Executable file

Binary file not shown.

BIN
build/Crafter.Asset-Asset.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
build/Crafter.Asset.o Normal file

Binary file not shown.

BIN
build/Crafter.Asset.pcm Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
build/main_source.o Normal file

Binary file not shown.

View file

@ -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 << "<name>\t<type>\t<size>" << 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]);
entry.data = pos->second(argv[3]);
}
asset.AddEntry(entry.name, entry.type, 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<char> 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;
}
// 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());
}