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

197 lines
5.4 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"
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-01-21 18:43:21 +01:00
using json = nlohmann::json;
2020-06-02 11:46:18 +02:00
2023-02-01 22:13:04 +01:00
extern AssetRegistry* instance;
2020-06-02 11:46:18 +02:00
AssetRegistry::~AssetRegistry()
{
}
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-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-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)
{
get().fontLoader->importAsset(args);
}
void AssetRegistry::importMaterial(MaterialImportArgs args)
{
get().materialLoader->importAsset(args);
}
2020-06-02 11:46:18 +02:00
PMeshAsset AssetRegistry::findMesh(const std::string &filePath)
{
2023-01-21 18:43:21 +01:00
AssetFolder& folder = get().assetRoot;
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());
}
auto it = folder.meshes.find(fileName);
assert(it != folder.meshes.end());
2021-04-25 23:43:40 +02:00
return it->second;
2020-06-02 11:46:18 +02:00
}
2020-10-03 11:00:10 +02:00
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
{
2023-01-21 18:43:21 +01:00
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
if(slashLoc != -1)
{
AssetFolder& folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
fileName = filePath.substr(slashLoc+1, filePath.size());
return folder.textures[fileName];
}
else
{
return get().assetRoot.textures[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-01-21 18:43:21 +01:00
AssetFolder& folder = get().assetRoot;
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());
}
return folder.fonts[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-01-21 18:43:21 +01:00
AssetFolder& folder = get().assetRoot;
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());
}
return folder.materials[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-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;
this->fontLoader = new FontLoader(graphics);
this->meshLoader = new MeshLoader(graphics);
this->textureLoader = new TextureLoader(graphics);
this->materialLoader = new MaterialLoader(graphics);
2020-06-02 11:46:18 +02:00
}
2020-09-19 14:36:50 +02:00
std::string AssetRegistry::getRootFolder()
{
return get().rootFolder.generic_string();
}
2023-01-21 18:43:21 +01:00
void AssetRegistry::registerMesh(PMeshAsset mesh, const std::string& importPath)
2020-09-19 14:36:50 +02:00
{
2023-01-21 18:43:21 +01:00
AssetFolder& folder = getOrCreateFolder(importPath);
folder.meshes[mesh->getFileName()] = mesh;
}
void AssetRegistry::registerTexture(PTextureAsset texture, const std::string& importPath)
{
AssetFolder& folder = getOrCreateFolder(importPath);
folder.textures[texture->getFileName()] = texture;
}
void AssetRegistry::registerFont(PFontAsset font, const std::string& importPath)
{
AssetFolder& folder = getOrCreateFolder(importPath);
folder.fonts[font->getFileName()] = font;
}
void AssetRegistry::registerMaterial(PMaterialAsset material, const std::string& importPath)
{
AssetFolder& folder = getOrCreateFolder(importPath);
folder.materials[material->getFileName()] = material;
}
AssetRegistry::AssetFolder& AssetRegistry::getOrCreateFolder(std::string fullPath)
{
AssetFolder& result = assetRoot;
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-01-21 18:43:21 +01:00
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);
result = result.children[folderName];
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
}