Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graphite/engine/assets/include/assetmanager.hpp

269 lines
6.5 KiB
C++
Raw Normal View History

2024-01-03 16:05:02 -05:00
#pragma once
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <istream>
#include <sstream>
#include <json.hpp>
#include <glm/glm.hpp>
#include <utility.hpp>
#include <log.hpp>
#include <eventdispatcher.hpp>
#include "asset.hpp"
#include "texture.hpp"
#include "material.hpp"
#include "model.hpp"
#include "mesh.hpp"
#include "skeleton.hpp"
#include "map.hpp"
#include "cubemap.hpp"
class World;
class AssetManager
{
public:
static void SetRenderer(Renderer* renderer);
static void Cleanup();
static void SearchDisk(const std::string& path);
static void SearchPacked();
static void LoadAssets(const std::vector<int>& ids);
static nlohmann::json ConstructMeta(Asset* asset, const int id);
static void SaveMeta(const int id);
static void LoadMeta(const std::string& path);
static void ParseMeta(nlohmann::json file, std::string path, Asset* parentAsset = nullptr);
static void ImportAsset(const std::string& filepath, const std::string& dest);
static void ImportModel(const std::string& filepath);
static Texture* ImportTexture(const std::string& filepath);
static void DeleteAsset(Asset* asset);
static Material* CreateMaterial(const std::string& filepath);
static Cubemap* CreateCubemap(const std::string& filepath);
static Map* CreateMap(World* world, const std::string& filepath);
static std::vector<Asset*> GetDependencies(Asset* asset);
static std::map<int, Asset*> GetChildren(Asset* asset);
static int GetID(const std::string& filepath)
{
return m_filepaths[filepath];
}
static int GetID(const Asset* asset)
{
for(auto iter : m_modelAssets)
{
if(iter.second == asset)
return iter.first;
}
for(auto iter : m_meshAssets)
{
if(iter.second == asset)
return iter.first;
}
for(auto iter : m_materialAssets)
{
if(iter.second == asset)
return iter.first;
}
for(auto iter : m_textureAssets)
{
if(iter.second == asset)
return iter.first;
}
for(auto iter : m_skeletonAssets)
{
if(iter.second == asset)
return iter.first;
}
for(auto iter : m_mapAssets)
{
if(iter.second == asset)
return iter.first;
}
for(auto iter : m_cubemapAssets)
{
if(iter.second == asset)
return iter.first;
}
return 0; //if no asset is found
}
static Asset* GetAsset(const int id)
{
try
{
if(m_modelAssets.count(id))
return m_modelAssets.at(id);
if(m_meshAssets.count(id))
return m_meshAssets.at(id);
if(m_materialAssets.count(id))
return m_materialAssets.at(id);
if(m_textureAssets.count(id))
return m_textureAssets.at(id);
if(m_skeletonAssets.count(id))
return m_skeletonAssets.at(id);
if(m_mapAssets.count(id))
return m_mapAssets.at(id);
if(m_cubemapAssets.count(id))
return m_cubemapAssets.at(id);
}
catch(std::out_of_range exception)
{
Log::Error("Could not find asset %s!", id);
}
return nullptr;
}
template<class T>
static T* Get(const int)
{
static_assert(std::is_base_of<Asset, T>::value, "Get<> requires to be given an Asset type!");
return nullptr;
}
template<class T>
static T* GetByPath(const std::string& filepath)
{
static_assert(std::is_base_of<Asset, T>::value, "Get<> requires to be given an Asset type!");
return Get<T>(GetID(filepath));
}
template<class T>
static T* GetByName(const std::string& filepath)
{
static_assert(std::is_base_of<Asset, T>::value, "Get<> requires to be given an Asset type!");
auto assets = GetAssets();
for(auto asset : assets)
{
if(asset.second->GetPath().find(filepath) != std::string::npos)
return static_cast<T*>(asset.second);
}
return nullptr;
}
static std::vector<Asset*> GetDirectoryAssets(const std::string& directory);
static std::map<int, Asset*> GetAssets()
{
std::map<int, Asset*> tmp;
for(auto& itr : m_modelAssets)
tmp.emplace(itr.first, itr.second);
for(auto& itr : m_meshAssets)
tmp.emplace(itr.first, itr.second);
for(auto& itr : m_materialAssets)
tmp.emplace(itr.first, itr.second);
for(auto& itr : m_textureAssets)
tmp.emplace(itr.first, itr.second);
for(auto& itr : m_skeletonAssets)
tmp.emplace(itr.first, itr.second);
for(auto& itr : m_mapAssets)
tmp.emplace(itr.first, itr.second);
for(auto& itr : m_cubemapAssets)
tmp.emplace(itr.first, itr.second);
return tmp;
}
static std::map<int, Mesh*> GetMeshes()
{
return m_meshAssets;
}
static std::map<int, Texture*> GetTextures()
{
return m_textureAssets;
}
static std::map<int, Material*> GetMaterials()
{
return m_materialAssets;
}
static std::map<int, Skeleton*> GetSkeletons()
{
return m_skeletonAssets;
}
static std::map<int, Map*> GetMaps()
{
return m_mapAssets;
}
static std::map<int, Cubemap*> GetCubemaps()
{
return m_cubemapAssets;
}
static std::vector<std::string> GetFilepaths()
{
std::vector<std::string> paths;
for(auto itr : m_filepaths)
{
std::string p = itr.first;
paths.push_back(p);
}
return paths;
}
static EventDispatcher<void()> onAssetListUpdate;
private:
static std::map<std::string, int> m_filepaths;
static std::map<int, Model*> m_modelAssets;
static std::map<int, Mesh*> m_meshAssets;
static std::map<int, Material*> m_materialAssets;
static std::map<int, Texture*> m_textureAssets;
static std::map<int, Skeleton*> m_skeletonAssets;
static std::map<int, Map*> m_mapAssets;
static std::map<int, Cubemap*> m_cubemapAssets;
static Renderer* m_renderer;
};
#define GET_ASSET(X, y) template<> inline X* AssetManager::Get<X>(const int id) { try { return y.at(id); } catch(std::out_of_range exception) { return nullptr; } }
//Get<T> template definitions
GET_ASSET(Model, m_modelAssets)
GET_ASSET(Mesh, m_meshAssets)
GET_ASSET(Material, m_materialAssets)
GET_ASSET(Texture, m_textureAssets)
GET_ASSET(Map, m_mapAssets)
GET_ASSET(Cubemap, m_cubemapAssets)