diff --git a/src/Linker/ZoneCreation/ZoneCreator.cpp b/src/Linker/ZoneCreation/ZoneCreator.cpp index d8ba67d5..b2aab95a 100644 --- a/src/Linker/ZoneCreation/ZoneCreator.cpp +++ b/src/Linker/ZoneCreation/ZoneCreator.cpp @@ -65,12 +65,12 @@ namespace zone_creator AssetCreatorCollection creatorCollection(*zone); ZoneDefinitionContext zoneDefinitionContext(*context.m_definition); + AssetCreationContext creationContext(*zone, &creatorCollection, &ignoredAssetLookup); + objCompiler->ConfigureCreatorCollection( creatorCollection, *zone, zoneDefinitionContext, *context.m_asset_search_path, lookup, context.m_out_dir, context.m_cache_dir); objLoader->ConfigureCreatorCollection(creatorCollection, *zone, *context.m_asset_search_path, lookup); - AssetCreationContext creationContext(zone.get(), &creatorCollection, &ignoredAssetLookup); - for (const auto& assetEntry : context.m_definition->m_assets) { const auto* createdAsset = creationContext.LoadDependencyGeneric(assetEntry.m_asset_type, assetEntry.m_asset_name); diff --git a/src/ObjLoading/Asset/AssetCreationContext.cpp b/src/ObjLoading/Asset/AssetCreationContext.cpp index 77da6e65..307cc24c 100644 --- a/src/ObjLoading/Asset/AssetCreationContext.cpp +++ b/src/ObjLoading/Asset/AssetCreationContext.cpp @@ -62,8 +62,8 @@ std::unique_ptr GenericAssetRegistration::CreateXAssetInfo() m_type, std::move(m_name), m_asset, std::move(dependencies), std::move(scriptStrings), std::move(indirectAssetReferences)); } -AssetCreationContext::AssetCreationContext(Zone* zone, const AssetCreatorCollection* creators, const IgnoredAssetLookup* ignoredAssetLookup) - : m_zone(zone), +AssetCreationContext::AssetCreationContext(Zone& zone, const AssetCreatorCollection* creators, const IgnoredAssetLookup* ignoredAssetLookup) + : ZoneAssetLoaderContainer(zone), m_creators(creators), m_ignored_asset_lookup(ignoredAssetLookup) { @@ -72,14 +72,14 @@ AssetCreationContext::AssetCreationContext(Zone* zone, const AssetCreatorCollect XAssetInfoGeneric* AssetCreationContext::AddAssetGeneric(GenericAssetRegistration registration) const { auto xAssetInfo = registration.CreateXAssetInfo(); - xAssetInfo->m_zone = m_zone; + xAssetInfo->m_zone = &m_zone; const auto assetType = xAssetInfo->m_type; const auto* pAssetName = xAssetInfo->m_name.c_str(); - auto* addedAsset = m_zone->m_pools->AddAsset(std::move(xAssetInfo)); + auto* addedAsset = m_zone.m_pools->AddAsset(std::move(xAssetInfo)); if (addedAsset == nullptr) - std::cerr << std::format("Failed to add asset of type \"{}\" to pool: \"{}\"\n", *m_zone->m_pools->GetAssetTypeName(assetType), pAssetName); + std::cerr << std::format("Failed to add asset of type \"{}\" to pool: \"{}\"\n", *m_zone.m_pools->GetAssetTypeName(assetType), pAssetName); return addedAsset; } @@ -90,14 +90,14 @@ XAssetInfoGeneric* AssetCreationContext::LoadDefaultAssetDependency(const asset_ if (result.HasTakenAction() && !result.HasFailed()) return result.GetAssetInfo(); - std::cerr << std::format("Failed to create default asset of type {}\n", *m_zone->m_pools->GetAssetTypeName(assetType)); + std::cerr << std::format("Failed to create default asset of type {}\n", *m_zone.m_pools->GetAssetTypeName(assetType)); return nullptr; } XAssetInfoGeneric* AssetCreationContext::LoadDependencyGeneric(const asset_type_t assetType, const std::string& assetName) { - auto* alreadyLoadedAsset = m_zone->m_pools->GetAssetOrAssetReference(assetType, assetName); + auto* alreadyLoadedAsset = m_zone.m_pools->GetAssetOrAssetReference(assetType, assetName); if (alreadyLoadedAsset) return alreadyLoadedAsset; @@ -110,11 +110,11 @@ XAssetInfoGeneric* AssetCreationContext::LoadDependencyGeneric(const asset_type_ if (!result.HasFailed()) return result.GetAssetInfo(); - std::cerr << std::format("Could not load asset \"{}\" of type \"{}\"\n", assetName, *m_zone->m_pools->GetAssetTypeName(assetType)); + std::cerr << std::format("Could not load asset \"{}\" of type \"{}\"\n", assetName, *m_zone.m_pools->GetAssetTypeName(assetType)); } else { - std::cerr << std::format("Missing asset \"{}\" of type \"{}\"\n", assetName, *m_zone->m_pools->GetAssetTypeName(assetType)); + std::cerr << std::format("Missing asset \"{}\" of type \"{}\"\n", assetName, *m_zone.m_pools->GetAssetTypeName(assetType)); } return nullptr; @@ -122,7 +122,7 @@ XAssetInfoGeneric* AssetCreationContext::LoadDependencyGeneric(const asset_type_ IndirectAssetReference AssetCreationContext::LoadIndirectAssetReferenceGeneric(asset_type_t assetType, const std::string& assetName) { - auto* alreadyLoadedAsset = m_zone->m_pools->GetAssetOrAssetReference(assetType, assetName); + auto* alreadyLoadedAsset = m_zone.m_pools->GetAssetOrAssetReference(assetType, assetName); if (alreadyLoadedAsset) return IndirectAssetReference(assetType, assetName); @@ -132,8 +132,7 @@ IndirectAssetReference AssetCreationContext::LoadIndirectAssetReferenceGeneric(a const auto result = m_creators->CreateAsset(assetType, assetName, *this); if (!result.HasTakenAction() && !result.HasFailed()) { - std::cerr << std::format( - "Could not load indirectly referenced asset \"{}\" of type \"{}\"\n", assetName, *m_zone->m_pools->GetAssetTypeName(assetType)); + std::cerr << std::format("Could not load indirectly referenced asset \"{}\" of type \"{}\"\n", assetName, *m_zone.m_pools->GetAssetTypeName(assetType)); } return IndirectAssetReference(assetType, assetName); } diff --git a/src/ObjLoading/Asset/AssetCreationContext.h b/src/ObjLoading/Asset/AssetCreationContext.h index 812ff978..c05351be 100644 --- a/src/ObjLoading/Asset/AssetCreationContext.h +++ b/src/ObjLoading/Asset/AssetCreationContext.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -27,10 +26,10 @@ public: std::unordered_multimap m_ignored_asset_lookup; }; -class AssetCreationContext +class AssetCreationContext : public ZoneAssetLoaderContainer { public: - AssetCreationContext(Zone* zone, const AssetCreatorCollection* creators, const IgnoredAssetLookup* ignoredAssetLookup); + AssetCreationContext(Zone& zone, const AssetCreatorCollection* creators, const IgnoredAssetLookup* ignoredAssetLookup); template XAssetInfo* AddAsset(AssetRegistration registration) { @@ -66,29 +65,11 @@ public: IndirectAssetReference LoadIndirectAssetReferenceGeneric(asset_type_t assetType, const std::string& assetName); - template T* GetZoneAssetLoaderState() - { - static_assert(std::is_base_of_v, "T must inherit IZoneAssetLoaderState"); - // T must also have a public default constructor - - const auto foundEntry = m_zone_asset_loader_states.find(typeid(T)); - if (foundEntry != m_zone_asset_loader_states.end()) - return dynamic_cast(foundEntry->second.get()); - - auto newState = std::make_unique(); - newState->SetZone(m_zone); - auto* newStatePtr = newState.get(); - m_zone_asset_loader_states.emplace(std::make_pair>(typeid(T), std::move(newState))); - return newStatePtr; - } - private: [[nodiscard]] XAssetInfoGeneric* LoadDefaultAssetDependency(asset_type_t assetType, const std::string& assetName); - Zone* m_zone; const AssetCreatorCollection* m_creators; const IgnoredAssetLookup* m_ignored_asset_lookup; - std::unordered_map> m_zone_asset_loader_states; }; #include "AssetCreatorCollection.h" diff --git a/src/ObjLoading/Asset/IZoneAssetLoaderState.h b/src/ObjLoading/Asset/IZoneAssetLoaderState.h index a17ccd51..9d3e9070 100644 --- a/src/ObjLoading/Asset/IZoneAssetLoaderState.h +++ b/src/ObjLoading/Asset/IZoneAssetLoaderState.h @@ -1,6 +1,11 @@ #pragma once + #include "Zone/Zone.h" +#include +#include +#include + class IZoneAssetLoaderState { protected: @@ -18,3 +23,34 @@ public: // Do nothing by default } }; + +class ZoneAssetLoaderContainer +{ +public: + ZoneAssetLoaderContainer(Zone& zone) + : m_zone(zone) + { + } + + template T* GetZoneAssetLoaderState() + { + static_assert(std::is_base_of_v, "T must inherit IZoneAssetLoaderState"); + // T must also have a public default constructor + + const auto foundEntry = m_zone_asset_loader_states.find(typeid(T)); + if (foundEntry != m_zone_asset_loader_states.end()) + return dynamic_cast(foundEntry->second.get()); + + auto newState = std::make_unique(); + newState->SetZone(&m_zone); + auto* newStatePtr = newState.get(); + m_zone_asset_loader_states.emplace(std::make_pair>(typeid(T), std::move(newState))); + return newStatePtr; + } + +protected: + Zone& m_zone; + +private: + std::unordered_map> m_zone_asset_loader_states; +}; diff --git a/test/ObjLoadingTests/Game/IW3/StringTable/AssetLoaderStringTableIW3Test.cpp b/test/ObjLoadingTests/Game/IW3/StringTable/AssetLoaderStringTableIW3Test.cpp index fb567095..36ee0347 100644 --- a/test/ObjLoadingTests/Game/IW3/StringTable/AssetLoaderStringTableIW3Test.cpp +++ b/test/ObjLoadingTests/Game/IW3/StringTable/AssetLoaderStringTableIW3Test.cpp @@ -25,7 +25,7 @@ namespace MemoryManager memory; AssetCreatorCollection creatorCollection(zone); IgnoredAssetLookup ignoredAssetLookup; - AssetCreationContext context(&zone, &creatorCollection, &ignoredAssetLookup); + AssetCreationContext context(zone, &creatorCollection, &ignoredAssetLookup); auto loader = CreateStringTableLoader(memory, searchPath); auto result = loader->CreateAsset("mp/cooltable.csv", context); diff --git a/test/ObjLoadingTests/Game/IW4/AssetLoaders/LoaderStringTableIW4Test.cpp b/test/ObjLoadingTests/Game/IW4/AssetLoaders/LoaderStringTableIW4Test.cpp index fec3a8a1..88fbe000 100644 --- a/test/ObjLoadingTests/Game/IW4/AssetLoaders/LoaderStringTableIW4Test.cpp +++ b/test/ObjLoadingTests/Game/IW4/AssetLoaders/LoaderStringTableIW4Test.cpp @@ -25,7 +25,7 @@ namespace MemoryManager memory; AssetCreatorCollection creatorCollection(zone); IgnoredAssetLookup ignoredAssetLookup; - AssetCreationContext context(&zone, &creatorCollection, &ignoredAssetLookup); + AssetCreationContext context(zone, &creatorCollection, &ignoredAssetLookup); auto loader = CreateStringTableLoader(memory, searchPath); auto result = loader->CreateAsset("mp/cooltable.csv", context); diff --git a/test/ObjLoadingTests/Game/IW4/Menu/LoaderMenuListIW4Test.cpp b/test/ObjLoadingTests/Game/IW4/Menu/LoaderMenuListIW4Test.cpp index 28ca0639..4c231d78 100644 --- a/test/ObjLoadingTests/Game/IW4/Menu/LoaderMenuListIW4Test.cpp +++ b/test/ObjLoadingTests/Game/IW4/Menu/LoaderMenuListIW4Test.cpp @@ -34,7 +34,7 @@ namespace test::game::iw4::menu::parsing::it : m_zone("MockZone", 0, IGame::GetGameById(GameId::IW4)), m_creator_collection(m_zone), m_ignored_asset_lookup(), - m_context(&m_zone, &m_creator_collection, &m_ignored_asset_lookup) + m_context(m_zone, &m_creator_collection, &m_ignored_asset_lookup) { m_asset_creator = CreateMenuListLoader(*m_zone.GetMemory(), m_search_path); } diff --git a/test/ObjLoadingTests/Game/IW5/AssetLoaders/LoaderStringTableIW5Test.cpp b/test/ObjLoadingTests/Game/IW5/AssetLoaders/LoaderStringTableIW5Test.cpp index e4b1072b..ab5ac734 100644 --- a/test/ObjLoadingTests/Game/IW5/AssetLoaders/LoaderStringTableIW5Test.cpp +++ b/test/ObjLoadingTests/Game/IW5/AssetLoaders/LoaderStringTableIW5Test.cpp @@ -24,7 +24,7 @@ namespace MemoryManager memory; AssetCreatorCollection creatorCollection(zone); IgnoredAssetLookup ignoredAssetLookup; - AssetCreationContext context(&zone, &creatorCollection, &ignoredAssetLookup); + AssetCreationContext context(zone, &creatorCollection, &ignoredAssetLookup); auto loader = CreateStringTableLoader(memory, searchPath); auto result = loader->CreateAsset("mp/cooltable.csv", context); diff --git a/test/ObjLoadingTests/Game/T5/AssetLoaders/LoaderStringTableT5Test.cpp b/test/ObjLoadingTests/Game/T5/AssetLoaders/LoaderStringTableT5Test.cpp index 2e78deca..b86f889a 100644 --- a/test/ObjLoadingTests/Game/T5/AssetLoaders/LoaderStringTableT5Test.cpp +++ b/test/ObjLoadingTests/Game/T5/AssetLoaders/LoaderStringTableT5Test.cpp @@ -24,7 +24,7 @@ namespace MemoryManager memory; AssetCreatorCollection creatorCollection(zone); IgnoredAssetLookup ignoredAssetLookup; - AssetCreationContext context(&zone, &creatorCollection, &ignoredAssetLookup); + AssetCreationContext context(zone, &creatorCollection, &ignoredAssetLookup); auto loader = CreateStringTableLoader(memory, searchPath); auto result = loader->CreateAsset("mp/cooltable.csv", context); diff --git a/test/ObjLoadingTests/Game/T6/AssetLoaders/LoaderStringTableT6Test.cpp b/test/ObjLoadingTests/Game/T6/AssetLoaders/LoaderStringTableT6Test.cpp index 3d7ee81f..c6a9186f 100644 --- a/test/ObjLoadingTests/Game/T6/AssetLoaders/LoaderStringTableT6Test.cpp +++ b/test/ObjLoadingTests/Game/T6/AssetLoaders/LoaderStringTableT6Test.cpp @@ -24,7 +24,7 @@ namespace MemoryManager memory; AssetCreatorCollection creatorCollection(zone); IgnoredAssetLookup ignoredAssetLookup; - AssetCreationContext context(&zone, &creatorCollection, &ignoredAssetLookup); + AssetCreationContext context(zone, &creatorCollection, &ignoredAssetLookup); auto loader = CreateStringTableLoader(memory, searchPath); auto result = loader->CreateAsset("mp/cooltable.csv", context);