asset rework

This commit is contained in:
Jorijn van der Graaf 2025-05-23 23:53:15 +02:00
commit df4ee20976
22 changed files with 145 additions and 87 deletions

View file

@ -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());
}