diff --git a/src/ZoneCommon/Pool/GlobalAssetPool.h b/src/ZoneCommon/Pool/GlobalAssetPool.h index ef6f8fe4..c185de77 100644 --- a/src/ZoneCommon/Pool/GlobalAssetPool.h +++ b/src/ZoneCommon/Pool/GlobalAssetPool.h @@ -5,10 +5,11 @@ #include #include #include +#include #include "AssetPool.h" -template +template class GlobalAssetPool { struct LinkedAssetPool @@ -24,12 +25,12 @@ class GlobalAssetPool LinkedAssetPool* m_asset_pool; }; - static std::vector m_linked_asset_pools; + static std::vector> m_linked_asset_pools; static std::unordered_map m_assets; static void SortLinkedAssetPools() { - std::sort(m_linked_asset_pools.begin(), m_linked_asset_pools.end(), [](const LinkedAssetPool* a, const LinkedAssetPool* b) -> bool + std::sort(m_linked_asset_pools.begin(), m_linked_asset_pools.end(), [](const std::unique_ptr& a, const std::unique_ptr& b) -> bool { return a->m_priority < b->m_priority; }); @@ -39,17 +40,17 @@ class GlobalAssetPool { int occurrences = 0; - for(auto linkedAssetPool : m_linked_asset_pools) + for (const auto& linkedAssetPool : m_linked_asset_pools) { XAssetInfo* foundAsset = linkedAssetPool->m_asset_pool->GetAsset(assetEntry.m_asset->m_name); - if(foundAsset != nullptr) + if (foundAsset != nullptr) { - if(++occurrences == 1) + if (++occurrences == 1) { assetEntry.m_asset = foundAsset; assetEntry.m_duplicate = false; - assetEntry.m_asset_pool = linkedAssetPool; + assetEntry.m_asset_pool = linkedAssetPool.get(); } else { @@ -68,7 +69,7 @@ class GlobalAssetPool auto existingAsset = m_assets.find(assetName); - if(existingAsset == m_assets.end()) + if (existingAsset == m_assets.end()) { GameAssetPoolEntry entry{}; entry.m_asset = asset; @@ -83,7 +84,7 @@ class GlobalAssetPool existingEntry.m_duplicate = true; - if(existingEntry.m_asset_pool->m_priority < link->m_priority) + if (existingEntry.m_asset_pool->m_priority < link->m_priority) { existingEntry.m_asset_pool = link; existingEntry.m_asset = asset; @@ -94,16 +95,17 @@ class GlobalAssetPool public: static void LinkAssetPool(AssetPool* assetPool, const int priority) { - auto* newLink = new LinkedAssetPool(); + auto newLink = std::make_unique(); newLink->m_asset_pool = assetPool; newLink->m_priority = priority; - m_linked_asset_pools.push_back(newLink); + auto* newLinkPtr = newLink.get(); + m_linked_asset_pools.emplace_back(std::move(newLink)); SortLinkedAssetPools(); - for(auto asset : *assetPool) + for (auto asset : *assetPool) { - LinkAsset(newLink, asset); + LinkAsset(newLinkPtr, asset); } } @@ -111,17 +113,17 @@ public: { LinkedAssetPool* link = nullptr; - for(auto existingLink : m_linked_asset_pools) + for (const auto& existingLink : m_linked_asset_pools) { - if(existingLink->m_asset_pool == assetPool) + if (existingLink->m_asset_pool == assetPool) { - link = existingLink; + link = existingLink.get(); break; } } assert(link != nullptr); - if(link == nullptr) + if (link == nullptr) return; LinkAsset(link, asset); @@ -131,26 +133,33 @@ public: { auto iLinkEntry = m_linked_asset_pools.begin(); - for(; iLinkEntry != m_linked_asset_pools.end(); ++iLinkEntry) + for (; iLinkEntry != m_linked_asset_pools.end(); ++iLinkEntry) { - LinkedAssetPool* linkEntry = *iLinkEntry; - if(linkEntry->m_asset_pool == assetPool) + LinkedAssetPool* linkEntry = iLinkEntry->get(); + if (linkEntry->m_asset_pool == assetPool) { break; } } assert(iLinkEntry != m_linked_asset_pools.end()); - if(iLinkEntry == m_linked_asset_pools.end()) + if (iLinkEntry == m_linked_asset_pools.end()) return; + auto assetPoolToUnlink = std::move(*iLinkEntry); m_linked_asset_pools.erase(iLinkEntry); - for(auto iAssetEntry = m_assets.begin(); iAssetEntry != m_assets.end(); ) + for (auto iAssetEntry = m_assets.begin(); iAssetEntry != m_assets.end();) { auto& assetEntry = *iAssetEntry; - if(assetEntry.second.m_duplicate && ReplaceAssetPoolEntry(assetEntry.second)) + if (assetEntry.second.m_asset_pool != assetPoolToUnlink.get()) + { + ++iAssetEntry; + continue; + } + + if (assetEntry.second.m_duplicate && ReplaceAssetPoolEntry(assetEntry.second)) { ++iAssetEntry; continue; @@ -170,8 +179,8 @@ public: } }; -template -std::vector::LinkedAssetPool*> GlobalAssetPool::m_linked_asset_pools = std::vector(); +template +std::vector::LinkedAssetPool>> GlobalAssetPool::m_linked_asset_pools = std::vector>(); -template -std::unordered_map::GameAssetPoolEntry> GlobalAssetPool::m_assets = std::unordered_map(); \ No newline at end of file +template +std::unordered_map::GameAssetPoolEntry> GlobalAssetPool::m_assets = std::unordered_map();