Files
Seele/src/Engine/Asset/AssetRegistry.cpp
T

423 lines
12 KiB
C++
Raw Normal View History

2020-06-02 11:46:18 +02:00
#include "AssetRegistry.h"
#include "MeshAsset.h"
2022-04-13 13:01:35 +02:00
#include "FontAsset.h"
2020-06-02 11:46:18 +02:00
#include "TextureAsset.h"
2023-01-21 18:43:21 +01:00
#include "MaterialAsset.h"
2023-02-13 14:56:13 +01:00
#include "MaterialInstanceAsset.h"
2022-04-13 13:01:35 +02:00
#include "FontLoader.h"
2020-06-02 11:46:18 +02:00
#include "TextureLoader.h"
#include "MaterialLoader.h"
#include "MeshLoader.h"
#include "Graphics/Mesh.h"
2020-06-02 11:46:18 +02:00
#include "Graphics/Graphics.h"
2021-01-19 15:30:00 +01:00
#include "Window/WindowManager.h"
2021-04-01 16:40:14 +02:00
#include "MeshAsset.h"
2023-01-21 18:43:21 +01:00
#include <nlohmann/json.hpp>
2020-08-11 22:38:19 +02:00
#include <iostream>
2020-06-02 11:46:18 +02:00
using namespace Seele;
2023-02-01 22:13:04 +01:00
extern AssetRegistry* instance;
2020-06-02 11:46:18 +02:00
AssetRegistry::~AssetRegistry()
{
2023-02-13 14:56:13 +01:00
delete assetRoot;
2020-06-02 11:46:18 +02:00
}
2023-01-29 18:58:59 +01:00
void AssetRegistry::init(const std::string& rootFolder, Gfx::PGraphics graphics)
2020-06-02 11:46:18 +02:00
{
2023-01-29 18:58:59 +01:00
get().initialize(rootFolder, graphics);
2020-06-02 11:46:18 +02:00
}
2023-02-01 22:13:04 +01:00
void AssetRegistry::importMesh(MeshImportArgs args)
2023-01-21 18:43:21 +01:00
{
2023-02-13 14:56:13 +01:00
if (get().getOrCreateFolder(args.importPath)->meshes.contains(args.filePath.stem().string()))
{
// skip importing duplicates
return;
}
2023-02-01 22:13:04 +01:00
get().meshLoader->importAsset(args);
2023-01-21 18:43:21 +01:00
}
2023-02-01 22:13:04 +01:00
void AssetRegistry::importTexture(TextureImportArgs args)
2020-06-02 11:46:18 +02:00
{
2023-02-13 14:56:13 +01:00
if (get().getOrCreateFolder(args.importPath)->textures.contains(args.filePath.stem().string()))
{
// skip importing duplicates
return;
}
2023-02-01 22:13:04 +01:00
get().textureLoader->importAsset(args);
2020-06-02 11:46:18 +02:00
}
2023-02-01 22:13:04 +01:00
void AssetRegistry::importFont(FontImportArgs args)
{
2023-02-13 14:56:13 +01:00
if (get().getOrCreateFolder(args.importPath)->fonts.contains(args.filePath.stem().string()))
{
// skip importing duplicates
return;
}
2023-02-01 22:13:04 +01:00
get().fontLoader->importAsset(args);
}
void AssetRegistry::importMaterial(MaterialImportArgs args)
{
2023-02-13 14:56:13 +01:00
if (get().getOrCreateFolder(args.importPath)->materials.contains(args.filePath.stem().string()))
{
// skip importing duplicates
return;
}
2023-02-01 22:13:04 +01:00
get().materialLoader->importAsset(args);
}
2020-06-02 11:46:18 +02:00
PMeshAsset AssetRegistry::findMesh(const std::string &filePath)
{
2023-02-13 14:56:13 +01:00
AssetFolder* folder = get().assetRoot;
2023-01-21 18:43:21 +01:00
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
if(slashLoc != -1)
{
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
fileName = filePath.substr(slashLoc+1, filePath.size());
}
2023-02-13 14:56:13 +01:00
return folder->meshes.at(fileName);
2020-06-02 11:46:18 +02:00
}
2020-10-03 11:00:10 +02:00
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
{
2023-02-13 14:56:13 +01:00
AssetFolder* folder = get().assetRoot;
2023-01-21 18:43:21 +01:00
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
2023-02-13 14:56:13 +01:00
if (slashLoc != -1)
2023-01-21 18:43:21 +01:00
{
2023-02-13 14:56:13 +01:00
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
fileName = filePath.substr(slashLoc + 1, filePath.size());
2023-01-21 18:43:21 +01:00
}
2023-02-13 14:56:13 +01:00
return folder->textures.at(fileName);
2020-10-03 11:00:10 +02:00
}
2023-01-21 18:43:21 +01:00
PFontAsset AssetRegistry::findFont(const std::string& filePath)
2022-04-13 13:01:35 +02:00
{
2023-02-13 14:56:13 +01:00
AssetFolder* folder = get().assetRoot;
2023-01-21 18:43:21 +01:00
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
if(slashLoc != -1)
{
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
fileName = filePath.substr(slashLoc+1, filePath.size());
}
2023-02-13 14:56:13 +01:00
return folder->fonts.at(fileName);
2022-04-13 13:01:35 +02:00
}
2020-06-02 11:46:18 +02:00
PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
{
2023-02-13 14:56:13 +01:00
AssetFolder* folder = get().assetRoot;
2023-01-21 18:43:21 +01:00
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
2023-02-13 14:56:13 +01:00
if (slashLoc != -1)
2023-01-21 18:43:21 +01:00
{
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
2023-02-13 14:56:13 +01:00
fileName = filePath.substr(slashLoc + 1, filePath.size());
2023-01-21 18:43:21 +01:00
}
2023-02-13 14:56:13 +01:00
return folder->materials.at(fileName);
2020-06-02 11:46:18 +02:00
}
2020-09-19 14:36:50 +02:00
std::ofstream AssetRegistry::createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
return get().internalCreateWriteStream(relativePath, openmode);
}
std::ifstream AssetRegistry::createReadStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
return get().internalCreateReadStream(relativePath, openmode);
}
2020-06-02 11:46:18 +02:00
AssetRegistry &AssetRegistry::get()
{
2023-02-01 22:13:04 +01:00
return *instance;
2020-06-02 11:46:18 +02:00
}
AssetRegistry::AssetRegistry()
{
}
2023-02-13 14:56:13 +01:00
void AssetRegistry::loadRegistry()
{
get().loadRegistryInternal();
}
void AssetRegistry::saveRegistry()
{
get().saveRegistryInternal();
}
2023-01-29 18:58:59 +01:00
void AssetRegistry::initialize(const std::filesystem::path &_rootFolder, Gfx::PGraphics _graphics)
2020-06-02 11:46:18 +02:00
{
2023-01-29 18:58:59 +01:00
this->graphics = _graphics;
this->rootFolder = _rootFolder;
2023-02-13 14:56:13 +01:00
this->assetRoot = new AssetFolder("");
2023-01-29 18:58:59 +01:00
this->fontLoader = new FontLoader(graphics);
this->meshLoader = new MeshLoader(graphics);
this->textureLoader = new TextureLoader(graphics);
this->materialLoader = new MaterialLoader(graphics);
2023-02-13 14:56:13 +01:00
loadRegistryInternal();
2020-06-02 11:46:18 +02:00
}
2023-02-13 14:56:13 +01:00
void AssetRegistry::loadRegistryInternal()
2020-09-19 14:36:50 +02:00
{
2023-02-13 14:56:13 +01:00
peekFolder(assetRoot);
loadFolder(assetRoot);
2020-09-19 14:36:50 +02:00
}
2023-02-13 14:56:13 +01:00
void AssetRegistry::peekFolder(AssetFolder* folder)
2020-09-19 14:36:50 +02:00
{
2023-02-13 14:56:13 +01:00
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;
}
auto stream = std::ifstream(entry.path(), std::ios::binary);
ArchiveBuffer buffer(graphics);
buffer.readFromStream(stream);
// 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);
PAsset asset;
switch (identifier)
{
case TextureAsset::IDENTIFIER:
asset = new TextureAsset(folderPath, name);
folder->textures[asset->getName()] = asset;
break;
case MeshAsset::IDENTIFIER:
asset = new MeshAsset(folderPath, name);
folder->meshes[asset->getName()] = asset;
break;
case MaterialAsset::IDENTIFIER:
asset = new MaterialAsset(folderPath, name);
folder->materials[asset->getName()] = asset;
break;
case MaterialInstanceAsset::IDENTIFIER:
asset = new MaterialInstanceAsset(folderPath, name);
// TODO
break;
case FontAsset::IDENTIFIER:
asset = new FontAsset(folderPath, name);
folder->fonts[asset->getName()] = asset;
break;
default:
throw new std::logic_error("Unknown Identifier");
}
}
2023-01-21 18:43:21 +01:00
}
2023-02-13 14:56:13 +01:00
void AssetRegistry::loadFolder(AssetFolder* folder)
2023-01-21 18:43:21 +01:00
{
2023-02-13 14:56:13 +01:00
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;
}
auto stream = std::ifstream(path, std::ios::binary);
ArchiveBuffer buffer(graphics);
buffer.readFromStream(stream);
// 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);
PAsset asset;
switch (identifier)
{
case TextureAsset::IDENTIFIER:
asset = folder->textures.at(name);
break;
case MeshAsset::IDENTIFIER:
asset = folder->meshes.at(name);
break;
case MaterialAsset::IDENTIFIER:
asset = folder->materials.at(name);
break;
case MaterialInstanceAsset::IDENTIFIER:
//asset = new MaterialInstanceAsset(path);
// TODO
break;
case FontAsset::IDENTIFIER:
asset = folder->fonts.at(name);
break;
default:
throw new std::logic_error("Unknown Identifier");
}
asset->load(buffer);
}
2023-01-21 18:43:21 +01:00
}
2023-02-13 14:56:13 +01:00
void AssetRegistry::saveRegistryInternal()
{
saveFolder("", assetRoot);
2023-01-21 18:43:21 +01:00
}
2023-02-13 14:56:13 +01:00
void AssetRegistry::saveFolder(const std::filesystem::path& folderPath, AssetFolder* folder)
2023-01-21 18:43:21 +01:00
{
2023-02-13 14:56:13 +01:00
std::filesystem::create_directory(rootFolder / folderPath);
for (const auto& [name, texture] : folder->textures)
{
saveAsset(texture, TextureAsset::IDENTIFIER, folderPath, name);
}
for (const auto& [name, mesh] : folder->meshes)
{
saveAsset(mesh, MeshAsset::IDENTIFIER, folderPath, name);
}
for (const auto& [name, material] : folder->materials)
{
saveAsset(material, MaterialAsset::IDENTIFIER, folderPath, name);
}
for (const auto& [name, font] : folder->fonts)
{
saveAsset(font, FontAsset::IDENTIFIER, folderPath, name);
}
for (auto& [name, child] : folder->children)
{
saveFolder(folderPath / name, child);
}
2023-01-21 18:43:21 +01:00
}
2023-02-13 14:56:13 +01:00
void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesystem::path& folderPath, std::string name)
2023-01-21 18:43:21 +01:00
{
2023-02-13 14:56:13 +01:00
if (name.empty())
return;
std::string path = (folderPath / name).string().append(".asset");
auto assetStream = createWriteStream(std::move(path), std::ios::binary);
ArchiveBuffer assetBuffer(graphics);
// write identifier
Serialization::save(assetBuffer, identifier);
// write name
Serialization::save(assetBuffer, name);
// write folder
Serialization::save(assetBuffer, folderPath.string());
// write asset data
asset->save(assetBuffer);
assetBuffer.writeToStream(assetStream);
}
std::filesystem::path AssetRegistry::getRootFolder()
{
return get().rootFolder;
}
void AssetRegistry::registerMesh(PMeshAsset mesh)
{
AssetFolder* folder = getOrCreateFolder(mesh->getFolderPath());
folder->meshes[mesh->getName()] = mesh;
}
void AssetRegistry::registerTexture(PTextureAsset texture)
{
AssetFolder* folder = getOrCreateFolder(texture->getFolderPath());
folder->textures[texture->getName()] = texture;
}
void AssetRegistry::registerFont(PFontAsset font)
{
AssetFolder* folder = getOrCreateFolder(font->getFolderPath());
folder->fonts[font->getName()] = font;
}
void AssetRegistry::registerMaterial(PMaterialAsset material)
{
AssetFolder* folder = getOrCreateFolder(material->getFolderPath());
folder->materials[material->getName()] = material;
}
AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string fullPath)
{
AssetFolder* result = assetRoot;
std::string temp = fullPath;
2023-01-21 18:43:21 +01:00
while(!fullPath.empty())
2020-09-19 14:36:50 +02:00
{
2023-01-21 18:43:21 +01:00
size_t slashLoc = fullPath.find("/");
if(slashLoc == -1)
2020-09-19 14:36:50 +02:00
{
2023-02-13 14:56:13 +01:00
if (!result->children.contains(fullPath))
{
result->children[fullPath] = new AssetFolder(fullPath);
}
return result->children[fullPath];
2020-09-19 14:36:50 +02:00
}
2023-01-21 18:43:21 +01:00
std::string folderName = fullPath.substr(0, slashLoc);
2023-02-13 14:56:13 +01:00
if (!result->children.contains(folderName))
{
result->children[folderName] = new AssetFolder(temp);
}
result = result->children[folderName];
2023-01-21 18:43:21 +01:00
fullPath = fullPath.substr(slashLoc+1, fullPath.size());
2020-09-19 14:36:50 +02:00
}
2023-01-21 18:43:21 +01:00
return result;
2020-10-03 11:00:10 +02:00
}
2020-09-19 14:36:50 +02:00
std::ofstream AssetRegistry::internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
2021-01-19 15:30:00 +01:00
auto fullPath = rootFolder;
fullPath.append(relativePath);
return std::ofstream(fullPath.string(), openmode);
2020-09-19 14:36:50 +02:00
}
std::ifstream AssetRegistry::internalCreateReadStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
2021-01-19 15:30:00 +01:00
auto fullPath = rootFolder;
fullPath.append(relativePath);
return std::ifstream(fullPath.string(), openmode);
2020-09-19 14:36:50 +02:00
}
2023-02-13 14:56:13 +01:00
AssetRegistry::AssetFolder::AssetFolder(std::string_view folderPath)
: folderPath(folderPath)
{}
AssetRegistry::AssetFolder::~AssetFolder()
{
for (const auto& [_, child] : children)
{
delete child;
}
}