Compare commits
3 commits
0b192c611f
...
3e30c9d5da
| Author | SHA1 | Date | |
|---|---|---|---|
| 3e30c9d5da | |||
| e5373908bb | |||
| df4ee20976 |
21 changed files with 147 additions and 87 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
build/
|
||||
bin/
|
||||
|
|
@ -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<AssetEntry> 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<char*>(&version), sizeof(std::uint32_t));
|
||||
std::uint32_t entryCount;
|
||||
|
|
@ -70,27 +59,86 @@ void Asset::LoadFull(fs::path path) {
|
|||
f.read(reinterpret_cast<char*>(&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<char*>(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<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::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 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) {
|
||||
void AssetSave::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));
|
||||
|
|
@ -103,14 +151,10 @@ void Asset::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));
|
||||
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.lenght), sizeof(std::uint32_t));
|
||||
offset+=entry.lenght;
|
||||
f.write(reinterpret_cast<const char*>(&entry.name[0]), entry.name.size());
|
||||
f.write(reinterpret_cast<const char*>(&entry.type[0]), entry.type.size());
|
||||
}
|
||||
for(const AssetEntry& entry : entries) {
|
||||
f.write(reinterpret_cast<const char*>(entry.data.data()), entry.data.size());
|
||||
}
|
||||
f.close();
|
||||
f.write(reinterpret_cast<const char*>(buffer.data()), buffer.size());
|
||||
}
|
||||
|
|
@ -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<char> data;
|
||||
AssetEntry();
|
||||
AssetEntry(std::string name, std::string type, std::vector<char> 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<AssetEntry> entries;
|
||||
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);
|
||||
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);
|
||||
void Save(fs::path path);
|
||||
};
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Crafter.Build
|
||||
Crafter®.Asset
|
||||
Copyright (C) 2025 Catcrafts®
|
||||
Catcrafts.net
|
||||
|
||||
|
|
|
|||
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.
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.
52
main.cpp
52
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 << "<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;
|
||||
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]);
|
||||
AssetSave asset;
|
||||
if(fs::exists(argv[2])) {
|
||||
AssetLoad assload(argv[2]);
|
||||
asset = AssetSave(assload.entries, std::move(assload.LoadAll()));
|
||||
fs::remove(argv[2]);
|
||||
}
|
||||
asset.entries.push_back(entry);
|
||||
asset.Save(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<char> 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());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue