Fixing some stuff

This commit is contained in:
Dynamitos
2024-07-19 10:42:59 +02:00
parent 3b6eb3ebcc
commit 819b541ff2
14 changed files with 225 additions and 238 deletions
+179 -160
View File
@@ -11,7 +11,6 @@
#include <iostream>
#include <nlohmann/json.hpp>
using namespace Seele;
AssetRegistry* _instance = new AssetRegistry();
@@ -21,6 +20,7 @@ AssetRegistry::~AssetRegistry() { delete assetRoot; }
void AssetRegistry::init(std::filesystem::path rootFolder, Gfx::PGraphics graphics) { get().initialize(rootFolder, graphics); }
PMeshAsset AssetRegistry::findMesh(std::string_view folderPath, std::string_view filePath) {
std::unique_lock l(get().assetLock);
AssetFolder* folder = get().assetRoot;
if (!folderPath.empty()) {
folder = get().getOrCreateFolder(folderPath);
@@ -29,6 +29,7 @@ PMeshAsset AssetRegistry::findMesh(std::string_view folderPath, std::string_view
}
PTextureAsset AssetRegistry::findTexture(std::string_view folderPath, std::string_view filePath) {
std::unique_lock l(get().assetLock);
AssetFolder* folder = get().assetRoot;
if (!folderPath.empty()) {
folder = get().getOrCreateFolder(folderPath);
@@ -37,6 +38,7 @@ PTextureAsset AssetRegistry::findTexture(std::string_view folderPath, std::strin
}
PFontAsset AssetRegistry::findFont(std::string_view folderPath, std::string_view filePath) {
std::unique_lock l(get().assetLock);
AssetFolder* folder = get().assetRoot;
if (!folderPath.empty()) {
folder = get().getOrCreateFolder(folderPath);
@@ -45,6 +47,7 @@ PFontAsset AssetRegistry::findFont(std::string_view folderPath, std::string_view
}
PMaterialAsset AssetRegistry::findMaterial(std::string_view folderPath, std::string_view filePath) {
std::unique_lock l(get().assetLock);
AssetFolder* folder = get().assetRoot;
if (!folderPath.empty()) {
folder = get().getOrCreateFolder(folderPath);
@@ -53,6 +56,7 @@ PMaterialAsset AssetRegistry::findMaterial(std::string_view folderPath, std::str
}
PMaterialInstanceAsset AssetRegistry::findMaterialInstance(std::string_view folderPath, std::string_view filePath) {
std::unique_lock l(get().assetLock);
AssetFolder* folder = get().assetRoot;
if (!folderPath.empty()) {
folder = get().getOrCreateFolder(folderPath);
@@ -60,6 +64,31 @@ PMaterialInstanceAsset AssetRegistry::findMaterialInstance(std::string_view fold
return folder->instances.at(std::string(filePath));
}
void AssetRegistry::registerMesh(OMeshAsset mesh) {
std::unique_lock l(get().assetLock);
get().registerMeshInternal(std::move(mesh));
}
void AssetRegistry::registerTexture(OTextureAsset texture) {
std::unique_lock l(get().assetLock);
get().registerTextureInternal(std::move(texture));
}
void AssetRegistry::registerFont(OFontAsset font) {
std::unique_lock l(get().assetLock);
get().registerFontInternal(std::move(font));
}
void AssetRegistry::registerMaterial(OMaterialAsset material) {
std::unique_lock l(get().assetLock);
get().registerMaterialInternal(std::move(material));
}
void AssetRegistry::registerMaterialInstance(OMaterialInstanceAsset instance) {
std::unique_lock l(get().assetLock);
get().registerMaterialInstanceInternal(std::move(instance));
}
std::ofstream AssetRegistry::createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode) {
return get().internalCreateWriteStream(relativePath, openmode);
}
@@ -68,135 +97,6 @@ std::ifstream AssetRegistry::createReadStream(const std::filesystem::path& relat
return get().internalCreateReadStream(relativePath, openmode);
}
AssetRegistry& AssetRegistry::get() { return *_instance; }
AssetRegistry::AssetRegistry() : assetRoot(nullptr) {}
AssetRegistry* AssetRegistry::getInstance() { return _instance; }
void AssetRegistry::loadRegistry() { get().loadRegistryInternal(); }
void AssetRegistry::saveRegistry() { get().saveRegistryInternal(); }
AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string_view fullPath) {
AssetFolder* result = assetRoot;
std::string temp = std::string(fullPath);
while (!temp.empty()) {
size_t slashLoc = temp.find("/");
if (slashLoc == std::string::npos) {
if (!result->children.contains(temp)) {
result->children[temp] = new AssetFolder(temp);
}
return result->children[temp];
}
std::string folderName = temp.substr(0, slashLoc);
if (!result->children.contains(folderName)) {
result->children[folderName] = new AssetFolder(fullPath);
}
result = result->children[folderName];
temp = temp.substr(slashLoc + 1, temp.size());
}
return result;
}
void AssetRegistry::initialize(const std::filesystem::path& _rootFolder, Gfx::PGraphics _graphics) {
std::unique_lock l(get().assetLock);
this->graphics = _graphics;
this->rootFolder = _rootFolder;
this->assetRoot = new AssetFolder("");
loadRegistryInternal();
}
void AssetRegistry::loadRegistryInternal() {
peekFolder(assetRoot);
loadFolder(assetRoot);
}
void AssetRegistry::peekFolder(AssetFolder* folder) {
for (const auto& entry : std::filesystem::directory_iterator(rootFolder / folder->folderPath)) {
const auto& stem = entry.path().stem().string();
if (entry.is_directory()) {
if (folder->folderPath.empty()) {
folder->children[stem] = new AssetFolder(stem);
} else {
folder->children[stem] = new AssetFolder(folder->folderPath + "/" + stem);
}
peekFolder(folder->children[stem]);
continue;
}
if (entry.path().filename().compare(".DS_Store") == 0) {
continue;
}
auto stream = std::ifstream(entry.path(), std::ios::binary);
ArchiveBuffer buffer(graphics);
buffer.readFromStream(stream);
peekAsset(buffer);
}
}
void AssetRegistry::loadFolder(AssetFolder* folder) {
for (const auto& entry : std::filesystem::directory_iterator(rootFolder / folder->folderPath)) {
const auto& path = entry.path();
if (entry.is_directory()) {
auto relative = path.stem().string();
loadFolder(folder->children[relative]);
continue;
}
if (entry.path().filename().compare(".DS_Store") == 0) {
continue;
}
auto stream = std::ifstream(path, std::ios::binary);
ArchiveBuffer buffer(graphics);
buffer.readFromStream(stream);
loadAsset(buffer);
}
}
void AssetRegistry::peekAsset(ArchiveBuffer& buffer) {
// Read asset type
uint64 identifier;
Serialization::load(buffer, identifier);
// Read name
std::string name;
Serialization::load(buffer, name);
// Read folder
std::string folderPath;
Serialization::load(buffer, folderPath);
AssetFolder* folder = getOrCreateFolder(folderPath);
OAsset asset;
switch (identifier) {
case TextureAsset::IDENTIFIER:
asset = new TextureAsset(folderPath, name);
folder->textures[name] = std::move(asset);
break;
case MeshAsset::IDENTIFIER:
asset = new MeshAsset(folderPath, name);
folder->meshes[name] = std::move(asset);
break;
case MaterialAsset::IDENTIFIER:
asset = new MaterialAsset(folderPath, name);
folder->materials[name] = std::move(asset);
break;
case MaterialInstanceAsset::IDENTIFIER:
asset = new MaterialInstanceAsset(folderPath, name);
folder->instances[name] = std::move(asset);
break;
case FontAsset::IDENTIFIER:
asset = new FontAsset(folderPath, name);
folder->fonts[name] = std::move(asset);
break;
default:
throw new std::logic_error("Unknown Identifier");
}
}
void AssetRegistry::loadAsset(ArchiveBuffer& buffer) {
// Read asset type
uint64 identifier;
@@ -209,8 +109,7 @@ void AssetRegistry::loadAsset(ArchiveBuffer& buffer) {
// Read folder
std::string folderPath;
Serialization::load(buffer, folderPath);
AssetFolder* folder = getOrCreateFolder(folderPath);
AssetFolder* folder = get().getOrCreateFolder(folderPath);
PAsset asset;
switch (identifier) {
@@ -235,6 +134,149 @@ void AssetRegistry::loadAsset(ArchiveBuffer& buffer) {
asset->load(buffer);
}
void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesystem::path& folderPath, std::string name) {
if (name.empty())
return;
std::string path = (folderPath / name).string().append(".asset");
auto assetStream = createWriteStream(std::move(path), std::ios::binary);
ArchiveBuffer buffer(get().graphics);
// write identifier
Serialization::save(buffer, identifier);
// write name
Serialization::save(buffer, name);
// write folder
Serialization::save(buffer, folderPath.string());
// write asset data
asset->save(buffer);
buffer.writeToStream(assetStream);
}
AssetRegistry& AssetRegistry::get() { return *_instance; }
AssetRegistry::AssetRegistry() : assetRoot(nullptr) {}
AssetRegistry* AssetRegistry::getInstance() { return _instance; }
void AssetRegistry::loadRegistry() {
get().loadRegistryInternal();
}
void AssetRegistry::saveRegistry() {
get().saveRegistryInternal();
}
AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string_view fullPath) {
AssetFolder* result = assetRoot;
std::string temp = std::string(fullPath);
while (!temp.empty()) {
size_t slashLoc = temp.find("/");
if (slashLoc == std::string::npos) {
if (!result->children.contains(temp)) {
result->children[temp] = new AssetFolder(temp);
}
return result->children[temp];
}
std::string folderName = temp.substr(0, slashLoc);
if (!result->children.contains(folderName)) {
result->children[folderName] = new AssetFolder(fullPath);
}
result = result->children[folderName];
temp = temp.substr(slashLoc + 1, temp.size());
}
return result;
}
void AssetRegistry::initialize(const std::filesystem::path& _rootFolder, Gfx::PGraphics _graphics) {
this->graphics = _graphics;
this->rootFolder = _rootFolder;
this->assetRoot = new AssetFolder("");
loadRegistryInternal();
}
void AssetRegistry::loadRegistryInternal() {
List<Pair<PAsset, ArchiveBuffer>> peeked;
{
std::unique_lock l(get().assetLock);
peeked = peekFolder(assetRoot);
}
for (auto& [asset, buffer] : peeked) {
asset->load(buffer);
}
}
List<Pair<PAsset, ArchiveBuffer>> AssetRegistry::peekFolder(AssetFolder* folder) {
List<Pair<PAsset, ArchiveBuffer>> peeked;
for (const auto& entry : std::filesystem::directory_iterator(rootFolder / folder->folderPath)) {
const auto& stem = entry.path().stem().string();
if (entry.is_directory()) {
if (folder->folderPath.empty()) {
folder->children[stem] = new AssetFolder(stem);
} else {
folder->children[stem] = new AssetFolder(folder->folderPath + "/" + stem);
}
auto temp = peekFolder(folder->children[stem]);
for (auto t : temp) {
peeked.add(t);
}
continue;
}
if (entry.path().filename().compare(".DS_Store") == 0) {
continue;
}
auto stream = std::ifstream(entry.path(), std::ios::binary);
ArchiveBuffer buffer(graphics);
buffer.readFromStream(stream);
peeked.add(peekAsset(buffer));
}
return peeked;
}
Pair<PAsset, ArchiveBuffer> AssetRegistry::peekAsset(ArchiveBuffer& buffer) {
// Read asset type
uint64 identifier;
Serialization::load(buffer, identifier);
// Read name
std::string name;
Serialization::load(buffer, name);
// Read folder
std::string folderPath;
Serialization::load(buffer, folderPath);
AssetFolder* folder = getOrCreateFolder(folderPath);
PAsset asset;
switch (identifier) {
case TextureAsset::IDENTIFIER:
folder->textures[name] = new TextureAsset(folderPath, name);
asset = PTextureAsset(folder->textures[name]);
break;
case MeshAsset::IDENTIFIER:
folder->meshes[name] = new MeshAsset(folderPath, name);
asset = PMeshAsset(folder->meshes[name]);
break;
case MaterialAsset::IDENTIFIER:
folder->materials[name] = new MaterialAsset(folderPath, name);
asset = PMaterialAsset(folder->materials[name]);
break;
case MaterialInstanceAsset::IDENTIFIER:
folder->instances[name] = new MaterialInstanceAsset(folderPath, name);
asset = PMaterialInstanceAsset(folder->instances[name]);
break;
case FontAsset::IDENTIFIER:
folder->fonts[name] = new FontAsset(folderPath, name);
asset = PFontAsset(folder->fonts[name]);
break;
default:
throw new std::logic_error("Unknown Identifier");
}
return {asset, buffer};
}
void AssetRegistry::saveRegistryInternal() { saveFolder("", assetRoot); }
void AssetRegistry::saveFolder(const std::filesystem::path& folderPath, AssetFolder* folder) {
@@ -259,52 +301,29 @@ void AssetRegistry::saveFolder(const std::filesystem::path& folderPath, AssetFol
}
}
void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesystem::path& folderPath, std::string name) {
if (name.empty())
return;
std::string path = (folderPath / name).string().append(".asset");
auto assetStream = createWriteStream(std::move(path), std::ios::binary);
ArchiveBuffer buffer(graphics);
// write identifier
Serialization::save(buffer, identifier);
// write name
Serialization::save(buffer, name);
// write folder
Serialization::save(buffer, folderPath.string());
// write asset data
asset->save(buffer);
buffer.writeToStream(assetStream);
}
std::filesystem::path AssetRegistry::getRootFolder() { return get().rootFolder; }
void AssetRegistry::registerMesh(OMeshAsset mesh) {
std::unique_lock l(assetLock);
void AssetRegistry::registerMeshInternal(OMeshAsset mesh) {
AssetFolder* folder = getOrCreateFolder(mesh->getFolderPath());
folder->meshes[mesh->getName()] = std::move(mesh);
}
void AssetRegistry::registerTexture(OTextureAsset texture) {
std::unique_lock l(assetLock);
void AssetRegistry::registerTextureInternal(OTextureAsset texture) {
AssetFolder* folder = getOrCreateFolder(texture->getFolderPath());
folder->textures[texture->getName()] = std::move(texture);
}
void AssetRegistry::registerFont(OFontAsset font) {
std::unique_lock l(assetLock);
void AssetRegistry::registerFontInternal(OFontAsset font) {
AssetFolder* folder = getOrCreateFolder(font->getFolderPath());
folder->fonts[font->getName()] = std::move(font);
}
void AssetRegistry::registerMaterial(OMaterialAsset material) {
std::unique_lock l(assetLock);
void AssetRegistry::registerMaterialInternal(OMaterialAsset material) {
AssetFolder* folder = getOrCreateFolder(material->getFolderPath());
folder->materials[material->getName()] = std::move(material);
}
void AssetRegistry::registerMaterialInstance(OMaterialInstanceAsset material) {
std::unique_lock l(assetLock);
void AssetRegistry::registerMaterialInstanceInternal(OMaterialInstanceAsset material) {
AssetFolder* folder = getOrCreateFolder(material->getFolderPath());
folder->instances[material->getName()] = std::move(material);
}
+19 -18
View File
@@ -1,11 +1,12 @@
#pragma once
#include "Asset.h"
#include "Containers/Map.h"
#include "Containers/Pair.h"
#include "Containers/List.h"
#include "MinimalEngine.h"
#include <filesystem>
#include <string>
namespace Seele {
DECLARE_REF(TextureAsset)
DECLARE_REF(FontAsset)
@@ -26,9 +27,18 @@ class AssetRegistry {
static PMaterialAsset findMaterial(std::string_view folderPath, std::string_view filePath);
static PMaterialInstanceAsset findMaterialInstance(std::string_view folderPath, std::string_view filePath);
static void registerMesh(OMeshAsset mesh);
static void registerTexture(OTextureAsset texture);
static void registerFont(OFontAsset font);
static void registerMaterial(OMaterialAsset material);
static void registerMaterialInstance(OMaterialInstanceAsset instance);
static std::ofstream createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::out);
static std::ifstream createReadStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::in);
static void loadAsset(ArchiveBuffer& buffer);
static void saveAsset(PAsset asset, uint64 identifier, const std::filesystem::path& folderPath, std::string name);
static void set(AssetRegistry registry);
static void loadRegistry();
@@ -47,25 +57,21 @@ class AssetRegistry {
AssetFolder* getOrCreateFolder(std::string_view foldername);
AssetRegistry();
static AssetRegistry* getInstance();
private:
static AssetRegistry& get();
private:
void initialize(const std::filesystem::path& rootFolder, Gfx::PGraphics graphics);
void loadRegistryInternal();
void peekFolder(AssetFolder* folder);
void loadFolder(AssetFolder* folder);
void peekAsset(ArchiveBuffer& buffer);
void loadAsset(ArchiveBuffer& buffer);
List<Pair<PAsset, ArchiveBuffer>> peekFolder(AssetFolder* folder);
Pair<PAsset, ArchiveBuffer> peekAsset(ArchiveBuffer& buffer);
void saveRegistryInternal();
void saveFolder(const std::filesystem::path& folderPath, AssetFolder* folder);
void saveAsset(PAsset asset, uint64 identifier, const std::filesystem::path& folderPath, std::string name);
void registerMesh(OMeshAsset mesh);
void registerTexture(OTextureAsset texture);
void registerFont(OFontAsset font);
void registerMaterial(OMaterialAsset material);
void registerMaterialInstance(OMaterialInstanceAsset instance);
void registerMeshInternal(OMeshAsset mesh);
void registerTextureInternal(OTextureAsset texture);
void registerFontInternal(OFontAsset font);
void registerMaterialInternal(OMaterialAsset material);
void registerMaterialInstanceInternal(OMaterialInstanceAsset instance);
std::ofstream internalCreateWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::out);
std::ifstream internalCreateReadStream(const std::filesystem::path& relaitvePath, std::ios_base::openmode openmode = std::ios::in);
@@ -75,10 +81,5 @@ class AssetRegistry {
AssetFolder* assetRoot;
Gfx::PGraphics graphics;
bool release = false;
friend class MaterialAsset;
friend class TextureLoader;
friend class FontLoader;
friend class MaterialLoader;
friend class MeshLoader;
};
} // namespace Seele
+3 -3
View File
@@ -3,7 +3,7 @@
#include "Graphics/Graphics.h"
#include "Material/Material.h"
#include "MaterialInstanceAsset.h"
#include <fstream>
using namespace Seele;
@@ -28,7 +28,7 @@ PMaterialInstanceAsset MaterialAsset::instantiate(const InstantiationParameter&
asset->setHandle(std::move(instance));
asset->setBase(this);
PMaterialInstanceAsset ref = asset;
AssetRegistry::get().saveAsset(ref, MaterialInstanceAsset::IDENTIFIER, ref->getFolderPath(), ref->getName());
AssetRegistry::get().registerMaterialInstance(std::move(asset));
AssetRegistry::registerMaterialInstance(std::move(asset));
AssetRegistry::saveAsset(ref, MaterialInstanceAsset::IDENTIFIER, ref->getFolderPath(), ref->getName());
return ref;
}
+2 -3
View File
@@ -22,13 +22,12 @@ TextureAsset::TextureAsset(std::string_view folderPath, std::string_view name) :
TextureAsset::~TextureAsset() {}
void TextureAsset::save(ArchiveBuffer& buffer) const { /*
Serialization::save(buffer, ktxData);*/
void TextureAsset::save(ArchiveBuffer& buffer) const {
Serialization::save(buffer, ktxData);
}
void TextureAsset::load(ArchiveBuffer& buffer) {
ktxTexture2* ktxHandle;
Array<uint8> ktxData;
Serialization::load(buffer, ktxData);
KTX_ASSERT(
ktxTexture_CreateFromMemory(ktxData.data(), ktxData.size(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, (ktxTexture**)&ktxHandle));
+2 -1
View File
@@ -13,12 +13,13 @@ class TextureAsset : public Asset {
virtual void save(ArchiveBuffer& buffer) const override;
virtual void load(ArchiveBuffer& buffer) override;
Gfx::PTexture getTexture() { return texture; }
void setTexture(Array<uint8> data) { }
void setTexture(Array<uint8> data) { ktxData = std::move(data); }
uint32 getWidth();
uint32 getHeight();
private:
Gfx::OTexture texture;
Array<uint8> ktxData;
friend class TextureLoader;
};
DEFINE_REF(TextureAsset)