diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index 90002b3d..78c94da7 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -10,6 +10,7 @@ #include "SHpch.h" #include #include +#include #include "SHAssetManager.h" #include "SHAssetMetaHandler.h" @@ -29,8 +30,9 @@ namespace SHADE std::vector SHAssetManager::loaders(TYPE_COUNT); - std::vector SHAssetManager::assetCollection; + std::unordered_map SHAssetManager::assetCollection; std::unordered_map SHAssetManager::assetData; + /**************************************************************************** * \brief Static function to generate asset ID. @@ -45,13 +47,7 @@ namespace SHADE result |= unique; - while (result == 0 || - std::ranges::any_of( - assetCollection.begin(), - assetCollection.end(), - [result](SHAsset const& asset) { return asset.id == result; } - ) - ) + while (result == 0 || assetCollection.contains(result)) { result = GenerateAssetID(type); } @@ -133,9 +129,17 @@ namespace SHADE * * \return const& to unordered_map ****************************************************************************/ - std::vector const& SHAssetManager::GetAllAssets() noexcept + std::vector SHAssetManager::GetAllAssets() noexcept { - return assetCollection; + std::vector result; + result.reserve(assetCollection.size()); + + for (auto const& asset : std::ranges::views::values(assetCollection)) + { + result.push_back(asset); + } + + return result; } /**************************************************************************** @@ -169,43 +173,50 @@ namespace SHADE } auto id = GenerateAssetID(type); - - assetCollection.emplace_back( + SHAsset asset{ name, id, type, newPath - ); + }; + + assetCollection.insert({ + id, + SHAsset( + name, + id, + type, + newPath + ) + }); return id; } bool SHAssetManager::SaveAsset(AssetID id) noexcept { - for (auto const& asset : assetCollection) + if (assetCollection.contains(id)) { - if (asset.id == id) + auto const& asset = assetCollection[id]; + if ( + asset.type == AssetType::SCENE || + asset.type == AssetType::PREFAB || + asset.type == AssetType::MATERIAL + ) { - if ( - asset.type == AssetType::SCENE || - asset.type == AssetType::PREFAB || - asset.type == AssetType::MATERIAL - ) + if (assetData.contains(id)) { - if (assetData.contains(id)) - { - auto const data = assetData.at(id); - loaders[static_cast(asset.type)]->Write(data, asset.path); - SHAssetMetaHandler::WriteMetaData(asset); + auto const data = assetData.at(id); + loaders[static_cast(asset.type)]->Write(data, asset.path); + SHAssetMetaHandler::WriteMetaData(asset); - return true; - } - - SHLOG_ERROR("Asset data has not been written into, cannot be saved: {}", - asset.path.filename().string()); - - return false; + return true; } + + SHLOG_ERROR("Asset data has not been written into, cannot be saved: {}", + asset.path.filename().string()); + + return false; } } @@ -249,7 +260,8 @@ namespace SHADE std::filesystem::copy(path, newPath); - assetCollection.push_back(CreateAssetFromPath(newPath)); + auto asset = CreateAssetFromPath(newPath); + assetCollection.insert({asset.id, asset}); return id; } @@ -279,7 +291,7 @@ namespace SHADE std::vector SHAssetManager::GetAllRecordOfType(AssetType type) noexcept { std::vector result; - for (auto const& asset : assetCollection) + for (auto const& asset : std::ranges::views::values(assetCollection)) { if (asset.type == type) { @@ -305,7 +317,7 @@ namespace SHADE newAsset.type = AssetType::SHADER_BUILT_IN; } - assetCollection.push_back(newAsset); + assetCollection.insert({ newAsset.id, newAsset }); SHAssetMetaHandler::WriteMetaData(newAsset); return newAsset.id; @@ -388,13 +400,13 @@ namespace SHADE meshAsset.path = SHMeshCompiler::CompileMeshBinary(*mesh, path).value(); meshAsset.id = GenerateAssetID(AssetType::MESH); meshAsset.type = AssetType::MESH; - assetCollection.push_back(meshAsset); + assetCollection.insert({ meshAsset.id, meshAsset }); SHAssetMetaHandler::WriteMetaData(meshAsset); } continue; } - assetCollection.push_back(newAsset); + assetCollection.insert({ newAsset.id, newAsset }); SHAssetMetaHandler::WriteMetaData(newAsset); } } @@ -426,7 +438,7 @@ namespace SHADE ****************************************************************************/ void SHAssetManager::LoadAllData() noexcept { - for (auto const& asset : assetCollection) + for (auto const& asset : std::ranges::views::values(assetCollection)) { SHAssetData* data = loaders[static_cast(asset.type)]->Load(asset.path); assetData.emplace(asset.id, data); @@ -457,7 +469,8 @@ namespace SHADE { if (dir.path().extension().string() == META_EXTENSION.data()) { - assetCollection.push_back(SHAssetMetaHandler::RetrieveMetaData(dir.path())); + auto asset = SHAssetMetaHandler::RetrieveMetaData(dir.path()); + assetCollection.insert({ asset.id, asset }); } } } diff --git a/SHADE_Engine/src/Assets/SHAssetManager.h b/SHADE_Engine/src/Assets/SHAssetManager.h index 28950646..fca38865 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.h +++ b/SHADE_Engine/src/Assets/SHAssetManager.h @@ -49,7 +49,7 @@ namespace SHADE * * \return const& to unordered_map ****************************************************************************/ - static std::vector const& GetAllAssets() noexcept; + static std::vector GetAllAssets() noexcept; /**************************************************************************** * \brief Create record for new resource. CAN ONLY CREATE FOR CUSTOM @@ -110,7 +110,7 @@ namespace SHADE static std::vector loaders; // For all resources - static std::vector assetCollection; + static std::unordered_map assetCollection; static std::unordered_map assetData; }; } diff --git a/SHADE_Engine/src/Assets/SHAssetManager.hpp b/SHADE_Engine/src/Assets/SHAssetManager.hpp index baf8a2f3..4f372938 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.hpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.hpp @@ -8,7 +8,7 @@ namespace SHADE { if (!assetData.contains(id)) { - for (auto const& asset : assetCollection) + for (auto const& asset : std::ranges::views::values(assetCollection)) { if (asset.id == id) { @@ -29,7 +29,7 @@ namespace SHADE { if (!assetData.contains(id)) { - for (auto const& asset : assetCollection) + for (auto const& asset : std::ranges::views::values(assetCollection)) { if (asset.id == id) {