Changed asset collection from vector to unordered_map for better id col check
Reduce use of for loops to iterate through asset collection
This commit is contained in:
parent
270205b0ba
commit
219492dedd
|
@ -10,6 +10,7 @@
|
|||
#include "SHpch.h"
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <ranges>
|
||||
#include "SHAssetManager.h"
|
||||
#include "SHAssetMetaHandler.h"
|
||||
|
||||
|
@ -29,8 +30,9 @@ namespace SHADE
|
|||
|
||||
std::vector<SHAssetLoader*> SHAssetManager::loaders(TYPE_COUNT);
|
||||
|
||||
std::vector<SHAsset> SHAssetManager::assetCollection;
|
||||
std::unordered_map<AssetID, SHAsset> SHAssetManager::assetCollection;
|
||||
std::unordered_map<AssetID, SHAssetData * const> 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<AssetName, AssetID>
|
||||
****************************************************************************/
|
||||
std::vector<SHAsset> const& SHAssetManager::GetAllAssets() noexcept
|
||||
std::vector<SHAsset> SHAssetManager::GetAllAssets() noexcept
|
||||
{
|
||||
return assetCollection;
|
||||
std::vector<SHAsset> 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<size_t>(asset.type)]->Write(data, asset.path);
|
||||
SHAssetMetaHandler::WriteMetaData(asset);
|
||||
auto const data = assetData.at(id);
|
||||
loaders[static_cast<size_t>(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<SHAsset> SHAssetManager::GetAllRecordOfType(AssetType type) noexcept
|
||||
{
|
||||
std::vector<SHAsset> 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<size_t>(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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ namespace SHADE
|
|||
*
|
||||
* \return const& to unordered_map<AssetName, AssetID>
|
||||
****************************************************************************/
|
||||
static std::vector<SHAsset> const& GetAllAssets() noexcept;
|
||||
static std::vector<SHAsset> GetAllAssets() noexcept;
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Create record for new resource. CAN ONLY CREATE FOR CUSTOM
|
||||
|
@ -110,7 +110,7 @@ namespace SHADE
|
|||
static std::vector<SHAssetLoader*> loaders;
|
||||
|
||||
// For all resources
|
||||
static std::vector<SHAsset> assetCollection;
|
||||
static std::unordered_map<AssetID, SHAsset> assetCollection;
|
||||
static std::unordered_map<AssetID, SHAssetData * const> assetData;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue