#pragma once #include #include #include #include #include #include #include #include #include #include #include #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& 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 GetDependencies(Asset* asset); static std::map 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 static T* Get(const int) { static_assert(std::is_base_of::value, "Get<> requires to be given an Asset type!"); return nullptr; } template static T* GetByPath(const std::string& filepath) { static_assert(std::is_base_of::value, "Get<> requires to be given an Asset type!"); return Get(GetID(filepath)); } template static T* GetByName(const std::string& filepath) { static_assert(std::is_base_of::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(asset.second); } return nullptr; } static std::vector GetDirectoryAssets(const std::string& directory); static std::map GetAssets() { std::map 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 GetMeshes() { return m_meshAssets; } static std::map GetTextures() { return m_textureAssets; } static std::map GetMaterials() { return m_materialAssets; } static std::map GetSkeletons() { return m_skeletonAssets; } static std::map GetMaps() { return m_mapAssets; } static std::map GetCubemaps() { return m_cubemapAssets; } static std::vector GetFilepaths() { std::vector paths; for(auto itr : m_filepaths) { std::string p = itr.first; paths.push_back(p); } return paths; } static EventDispatcher onAssetListUpdate; private: static std::map m_filepaths; static std::map m_modelAssets; static std::map m_meshAssets; static std::map m_materialAssets; static std::map m_textureAssets; static std::map m_skeletonAssets; static std::map m_mapAssets; static std::map m_cubemapAssets; static Renderer* m_renderer; }; #define GET_ASSET(X, y) template<> inline X* AssetManager::Get(const int id) { try { return y.at(id); } catch(std::out_of_range exception) { return nullptr; } } //Get 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)