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

225 lines
6.5 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
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
}
void AssetRegistry::importFile(const std::string &filePath)
2023-01-21 18:43:21 +01:00
{
importFile(filePath, "");
}
void AssetRegistry::importFile(const std::string &filePath, const std::string& importPath)
2020-06-02 11:46:18 +02:00
{
2020-06-08 01:44:47 +02:00
std::filesystem::path fsPath = std::filesystem::path(filePath);
std::string extension = fsPath.extension().string();
if (extension.compare(".fbx") == 0
2022-03-19 22:45:30 +01:00
|| extension.compare(".obj") == 0
|| extension.compare(".blend") == 0)
2020-06-02 11:46:18 +02:00
{
2023-01-21 18:43:21 +01:00
get().importMesh(fsPath, importPath);
2020-06-02 11:46:18 +02:00
}
2020-06-08 01:44:47 +02:00
if (extension.compare(".png") == 0
|| extension.compare(".jpg") == 0)
2020-06-02 11:46:18 +02:00
{
2023-01-21 18:43:21 +01:00
get().importTexture(fsPath, importPath);
2020-06-02 11:46:18 +02:00
}
2022-04-13 13:01:35 +02:00
if(extension.compare(".ttf") == 0)
{
2023-01-21 18:43:21 +01:00
get().importFont(fsPath, importPath);
2022-04-13 13:01:35 +02:00
}
2021-06-12 18:51:29 +02:00
if (extension.compare(".asset") == 0)
2020-06-02 11:46:18 +02:00
{
2023-01-21 18:43:21 +01:00
get().importMaterial(fsPath, importPath);
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()
{
static AssetRegistry instance;
return instance;
}
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::importMesh(const std::filesystem::path &filePath, const std::string& importPath)
2020-06-02 11:46:18 +02:00
{
2023-01-21 18:43:21 +01:00
meshLoader->importAsset(filePath, importPath);
2020-06-02 11:46:18 +02:00
}
2023-01-21 18:43:21 +01:00
void AssetRegistry::importTexture(const std::filesystem::path &filePath, const std::string& importPath)
2020-06-02 11:46:18 +02:00
{
2023-01-21 18:43:21 +01:00
textureLoader->importAsset(filePath, importPath);
2020-06-02 11:46:18 +02:00
}
2023-01-21 18:43:21 +01:00
void AssetRegistry::importFont(const std::filesystem::path& filePath, const std::string& importPath)
2022-04-13 13:01:35 +02:00
{
2023-01-21 18:43:21 +01:00
fontLoader->importAsset(filePath, importPath);
2022-04-13 13:01:35 +02:00
}
2023-01-21 18:43:21 +01:00
void AssetRegistry::importMaterial(const std::filesystem::path &filePath, const std::string& importPath)
2020-06-02 11:46:18 +02:00
{
2023-01-21 18:43:21 +01:00
materialLoader->importAsset(filePath, importPath);
2020-09-19 14:36:50 +02:00
}
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
}