SP3-103 SP3-102 Added call to mesh loader in asset manager. Removed old references to previous maps and vectors.

This commit is contained in:
Xiao Qi 2022-09-22 11:40:24 +08:00
parent 8ac6ef95d6
commit 6f1592e16f
6 changed files with 60 additions and 106 deletions

View File

@ -8,7 +8,7 @@ namespace SHADE
struct SHAsset
{
AssetName name;
AssetID ID;
AssetID id;
AssetType type;
AssetPath path;
FolderLocation location;

View File

@ -13,16 +13,19 @@
#include "SHAssetManager.h"
#include "SHAssetMetaHandler.h"
#include "Filesystem/SHFileSystem.h"
#include <assimp/Importer.hpp>
#include "Libraries/SHMeshLoader.h"
namespace SHADE
{
FMOD::System* SHAssetManager::audioSystem;
std::unordered_map<AssetID, SHSound >* SHAssetManager::audioSoundList;
std::vector<SHAssetMeta> SHAssetManager::metaCollection;
std::vector<SHAsset> SHAssetManager::assetCollection;
std::unordered_map<AssetID, SHAsset> SHAssetManager::assetRegistry;
std::unordered_map<AssetID, SHMeshAsset> SHAssetManager::meshCollection;
/****************************************************************************
* \brief Static function to generate resource ID.
****************************************************************************/
@ -44,9 +47,9 @@ namespace SHADE
****************************************************************************/
void SHAssetManager::Unload() noexcept
{
for (auto const& meta : metaCollection)
for (auto const& asset : assetCollection)
{
SHAssetMetaHandler::WriteMetaData(meta, pathRegistry[meta.id].string().append(META_EXTENSION));
SHAssetMetaHandler::WriteMetaData(asset);
}
}
@ -75,9 +78,9 @@ namespace SHADE
*
* \return const& to unordered_map<AssetName, AssetID>
****************************************************************************/
std::unordered_map<AssetName, AssetID> const& SHAssetManager::GetAllAssets() noexcept
std::vector<SHAsset> const& SHAssetManager::GetAllAssets() noexcept
{
return nameIDRegistry;
assetCollection;
}
/****************************************************************************
@ -91,7 +94,7 @@ namespace SHADE
AssetID SHAssetManager::CreateNewAsset(AssetType type, AssetName name) noexcept
{
AssetID id{ GenerateAssetID() };
SHAssetMeta meta;
SHAsset meta;
meta.id = id;
meta.type = type;
@ -104,15 +107,9 @@ namespace SHADE
}
AssetPath path{ ASSET_ROOT + folder + name + SHAssetMetaHandler::GetExtensionFromType(type) };
metaCollection.push_back(meta);
pathRegistry[id] = path;
typeRegistry[id] = type;
idNameRegistry[id] = name;
nameIDRegistry[name] = id;
filenameRegistry[name + SHAssetMetaHandler::GetExtensionFromType(type)] = id;
filenameReverse[id] = name + SHAssetMetaHandler::GetExtensionFromType(type);
SHAssetMetaHandler::WriteMetaData(meta);
SHAssetMetaHandler::WriteMetaData(meta, path.string() + META_EXTENSION);
assetCollection.push_back(meta);
return id;
}
@ -125,20 +122,12 @@ namespace SHADE
****************************************************************************/
AssetID SHAssetManager::ImportNewAsset(char const* p) noexcept
{
std::filesystem::path path{ p };
for (auto const& pa : pathRegistry)
{
if (std::strcmp(pa.second.string().c_str(), path.filename().string().c_str()) == 0)
{
return pa.first;
}
}
std::filesystem::path const path{ p };
std::filesystem::path newPath{ GenerateLocalPath(path) };
std::filesystem::path const newPath{ GenerateLocalPath(path) };
if (newPath.empty())
{
//TODO: Assert imported file is not recognised
std::cout << "Unsupported File Formate: " << path.filename() << "\n";
SHLOG_WARNING("Unsupported file format for asset: {}", path.string());
return 0;
}
@ -187,7 +176,7 @@ namespace SHADE
}
}
std::vector<SHAssetMeta> newLoad;
std::vector<SHAsset> newLoad;
newLoad.reserve(resourceFilesNew.size());
//TODO: Handle if meta does not match all resources (if meta exist and asset doesnt, vice versa)
@ -209,15 +198,9 @@ namespace SHADE
****************************************************************************/
void SHAssetManager::RegisterAsset(AssetPath const& metaPath, AssetPath const& path) noexcept
{
SHAssetMeta meta = SHAssetMetaHandler::RetrieveMetaData(metaPath);
SHAsset const meta = SHAssetMetaHandler::RetrieveMetaData(metaPath);
metaCollection.push_back(meta);
pathRegistry.emplace(meta.id, path);
typeRegistry.emplace(meta.id, meta.type);
nameIDRegistry.emplace(GetNameFromPath(path), meta.id);
idNameRegistry.emplace(meta.id, GetNameFromPath(path));
filenameRegistry.emplace(path.filename().string(), meta.id);
filenameReverse.emplace(meta.id, path.filename().string());
assetCollection.push_back(meta);
}
/****************************************************************************
@ -225,24 +208,16 @@ namespace SHADE
* \brief Creates new meta data for new resource.
****************************************************************************/
SHAssetMeta SHAssetManager::RegisterAssetNew(AssetPath const& resource) noexcept
SHAsset SHAssetManager::RegisterAssetNew(AssetPath const& resource) noexcept
{
SHAssetMeta meta;
SHAsset meta;
meta.id = GenerateAssetID();
meta.type = SHAssetMetaHandler::GetTypeFromExtension(resource.extension().string());
metaCollection.push_back(meta);
pathRegistry.emplace(meta.id, resource);
typeRegistry.emplace(meta.id, meta.type);
nameIDRegistry.emplace(GetNameFromPath(resource), meta.id);
idNameRegistry.emplace(meta.id, GetNameFromPath(resource));
filenameRegistry.emplace(resource.filename().string(), meta.id);
filenameReverse.emplace(meta.id, resource.filename().string());
assetCollection.push_back(meta);
//LoadData(meta.id);
SHAssetMetaHandler::WriteMetaData(meta, resource.string() + META_EXTENSION);
return metaCollection.back();
SHAssetMetaHandler::WriteMetaData(meta);
return assetCollection.back();
}
bool SHAssetManager::IsRecognised(char const* ext) noexcept
@ -258,6 +233,17 @@ namespace SHADE
return false;
}
void SHAssetManager::LoadGLTF(SHAsset asset) noexcept
{
std::vector<SHMeshAsset> meshes;
std::vector<AssetPath> imagePaths;
SHMeshLoader::LoadMesh(meshes, imagePaths, asset.path);
//TODO Recognise new meshes as asset as well and write mesh into binary
}
/****************************************************************************
* \brief Load all resources that are in the folder
****************************************************************************/
@ -272,14 +258,14 @@ namespace SHADE
****************************************************************************/
void SHAssetManager::LoadAllData() noexcept
{
for (auto const& meta : metaCollection)
for (auto const& asset : assetCollection)
{
}
}
void SHAssetManager::LoadData(AssetID id) noexcept
{
AssetType type{ typeRegistry[id] };
(void)id;
}
/****************************************************************************
@ -316,7 +302,7 @@ namespace SHADE
{
if (IsRecognised(file.extension().string().c_str()))
{
SHAssetMetaHandler::WriteMetaData(RegisterAssetNew(file), file.string() + META_EXTENSION);
SHAssetMetaHandler::WriteMetaData(RegisterAssetNew(file));
}
else
{
@ -330,8 +316,8 @@ namespace SHADE
std::filesystem::path p{ path };
if (IsRecognised(p.extension().string().c_str()))
{
SHAssetMeta const& meta{ RegisterAssetNew(p) };
SHAssetMetaHandler::WriteMetaData(meta, p.string() + META_EXTENSION);
SHAsset const& meta{ RegisterAssetNew(p) };
SHAssetMetaHandler::WriteMetaData(meta);
return meta.id;
}
else

View File

@ -14,11 +14,10 @@
#include <unordered_map>
#include <string>
#include <vector>
#include "SHAssetMeta.h"
#include "tinyddsloader.h"
#include "SHAsset.h"
#include <memory>
#include "Asset Types/SHMeshAsset.h"
struct _MonoMethod;
@ -49,23 +48,7 @@ namespace SHADE
*
* \return const& to unordered_map<AssetName, AssetID>
****************************************************************************/
static std::unordered_map<AssetName, AssetID> const& GetAllAssets() noexcept;
/****************************************************************************
* \brief Get type of asset from ID
*
* \param resource id of file
* \return AssetType enum class value
****************************************************************************/
static AssetType GetTypeFromID(AssetID) noexcept;
/****************************************************************************
* \brief Get registered path of asset.
*
* \param resource id of file
* \return std::filesystem::path
****************************************************************************/
static AssetPath GetPathFromID(AssetID) noexcept;
static std::vector<SHAsset> const& GetAllAssets() noexcept;
/****************************************************************************
* \brief Create record for new resource. CAN ONLY CREATE FOR CUSTOM
@ -129,16 +112,21 @@ namespace SHADE
* \brief Creates new meta data for new resource.
****************************************************************************/
static SHAssetMeta RegisterAssetNew(AssetPath const&) noexcept;
static SHAsset RegisterAssetNew(AssetPath const&) noexcept;
static bool IsRecognised(char const*) noexcept;
// Specialised load calls
void LoadGLTF(SHAsset asset) noexcept;
static FMOD::System* audioSystem;
static std::unordered_map<AssetID,SHSound>* audioSoundList;
// For all resources
static std::vector<SHAssetMeta> metaCollection;
static std::vector<SHAsset> assetCollection;
static std::unordered_map<AssetID, SHAsset> assetRegistry;
static std::unordered_map<AssetID, SHMeshAsset> meshCollection;
};
}

View File

@ -1,24 +0,0 @@
/******************************************************************************
* \file SHAssetMeta.h
* \author Loh Xiao Qi
* \brief Class to hold meta data values for resources
*
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
* or disclosure of this file or its contents without the prior
* written consent of Digipen Institute of Technology is prohibited
******************************************************************************/
#ifndef SH_RESOURCE_META_H
#define SH_RESOURCE_META_H
#include "SHAssetMacros.h"
namespace SHADE
{
struct SHAssetMeta
{
AssetID id;
AssetType type;
};
}
#endif // !SH_RESOURCE_META_H

View File

@ -59,7 +59,7 @@ namespace SHADE
* \brief path to meta data file
****************************************************************************/
SHAssetMeta SHAssetMetaHandler::RetrieveMetaData(AssetPath const& path) noexcept
SHAsset SHAssetMetaHandler::RetrieveMetaData(AssetPath const& path) noexcept
{
std::ifstream metaFile{ path.string(), std::ios_base::in };
if (!metaFile.is_open())
@ -68,7 +68,7 @@ namespace SHADE
}
std::string line;
SHAssetMeta meta;
SHAsset meta;
// Get resource id
GetFieldValue(metaFile, line);
@ -95,13 +95,17 @@ namespace SHADE
* \brief Writes meta data into text file
****************************************************************************/
void SHAssetMetaHandler::WriteMetaData(SHAssetMeta const& meta, AssetPath const& path) noexcept
void SHAssetMetaHandler::WriteMetaData(SHAsset const& meta) noexcept
{
std::string path{ meta.path.string() };
path.append(META_EXTENSION);
std::ofstream metaFile{ path, std::ios_base::out };
if (!metaFile.is_open())
{
// Log error
SHLOG_ERROR("Asset write path is invalid: {}", path);
return;
}
metaFile << "ID: " << meta.id << "\n";

View File

@ -11,7 +11,7 @@
#define SH_RESOURCE_META_HANDLER_H
#include "SHAssetMacros.h"
#include "SHAssetMeta.h"
#include "SHAsset.h"
namespace SHADE
{
@ -36,7 +36,7 @@ namespace SHADE
* \brief path to meta data file
****************************************************************************/
static SHAssetMeta RetrieveMetaData(AssetPath const&) noexcept;
static SHAsset RetrieveMetaData(AssetPath const&) noexcept;
/****************************************************************************
* \param Asset meta data to be written into
@ -44,7 +44,7 @@ namespace SHADE
* \brief Writes meta data into text file
****************************************************************************/
static void WriteMetaData(SHAssetMeta const&, AssetPath const&) noexcept;
static void WriteMetaData(SHAsset const&) noexcept;
};
}