chore: inject ZoneAssetCreationStateCollection to states

This commit is contained in:
Jan 2025-01-03 12:45:59 +01:00
parent ef862ff246
commit 2313da1c12
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
7 changed files with 29 additions and 14 deletions

View File

@ -64,6 +64,7 @@ std::unique_ptr<XAssetInfoGeneric> GenericAssetRegistration::CreateXAssetInfo()
AssetCreationContext::AssetCreationContext(Zone& zone, const AssetCreatorCollection* creators, const IgnoredAssetLookup* ignoredAssetLookup) AssetCreationContext::AssetCreationContext(Zone& zone, const AssetCreatorCollection* creators, const IgnoredAssetLookup* ignoredAssetLookup)
: ZoneAssetCreationStateContainer(zone), : ZoneAssetCreationStateContainer(zone),
m_zone(zone),
m_creators(creators), m_creators(creators),
m_ignored_asset_lookup(ignoredAssetLookup) m_ignored_asset_lookup(ignoredAssetLookup)
{ {

View File

@ -68,6 +68,7 @@ public:
private: private:
[[nodiscard]] XAssetInfoGeneric* LoadDefaultAssetDependency(asset_type_t assetType, const std::string& assetName); [[nodiscard]] XAssetInfoGeneric* LoadDefaultAssetDependency(asset_type_t assetType, const std::string& assetName);
Zone& m_zone;
const AssetCreatorCollection* m_creators; const AssetCreatorCollection* m_creators;
const IgnoredAssetLookup* m_ignored_asset_lookup; const IgnoredAssetLookup* m_ignored_asset_lookup;
}; };

View File

@ -6,6 +6,21 @@
#include <typeindex> #include <typeindex>
#include <unordered_map> #include <unordered_map>
class ZoneAssetCreationStateContainer;
class ZoneAssetCreationInjection
{
public:
ZoneAssetCreationInjection(ZoneAssetCreationStateContainer& zoneStates, Zone& zone)
: m_zone_states(zoneStates),
m_zone(zone)
{
}
ZoneAssetCreationStateContainer& m_zone_states;
Zone& m_zone;
};
class IZoneAssetCreationState class IZoneAssetCreationState
{ {
protected: protected:
@ -18,7 +33,7 @@ public:
IZoneAssetCreationState& operator=(const IZoneAssetCreationState& other) = default; IZoneAssetCreationState& operator=(const IZoneAssetCreationState& other) = default;
IZoneAssetCreationState& operator=(IZoneAssetCreationState&& other) noexcept = default; IZoneAssetCreationState& operator=(IZoneAssetCreationState&& other) noexcept = default;
virtual void SetZone(Zone* zone) virtual void Inject(ZoneAssetCreationInjection& inject)
{ {
// Do nothing by default // Do nothing by default
} }
@ -28,7 +43,7 @@ class ZoneAssetCreationStateContainer
{ {
public: public:
ZoneAssetCreationStateContainer(Zone& zone) ZoneAssetCreationStateContainer(Zone& zone)
: m_zone(zone) : m_injection(*this, zone)
{ {
} }
@ -42,16 +57,14 @@ public:
return *dynamic_cast<T*>(foundEntry->second.get()); return *dynamic_cast<T*>(foundEntry->second.get());
auto newState = std::make_unique<T>(); auto newState = std::make_unique<T>();
newState->SetZone(&m_zone); newState->Inject(m_injection);
auto* newStatePtr = newState.get(); auto* newStatePtr = newState.get();
m_zone_asset_creation_states.emplace(std::make_pair<std::type_index, std::unique_ptr<IZoneAssetCreationState>>(typeid(T), std::move(newState))); m_zone_asset_creation_states.emplace(std::make_pair<std::type_index, std::unique_ptr<IZoneAssetCreationState>>(typeid(T), std::move(newState)));
return *newStatePtr; return *newStatePtr;
} }
protected:
Zone& m_zone;
private: private:
ZoneAssetCreationInjection m_injection;
std::unordered_map<std::type_index, std::unique_ptr<IZoneAssetCreationState>> m_zone_asset_creation_states; std::unordered_map<std::type_index, std::unique_ptr<IZoneAssetCreationState>> m_zone_asset_creation_states;
}; };

View File

@ -10,11 +10,11 @@ MenuConversionZoneState::MenuConversionZoneState()
{ {
} }
void MenuConversionZoneState::SetZone(Zone* zone) void MenuConversionZoneState::Inject(ZoneAssetCreationInjection& inject)
{ {
auto* memory = zone->GetMemory(); auto* memory = inject.m_zone.GetMemory();
m_zone = zone; m_zone = &inject.m_zone;
m_supporting_data = memory->Create<ExpressionSupportingData>(); m_supporting_data = memory->Create<ExpressionSupportingData>();
memset(m_supporting_data, 0, sizeof(ExpressionSupportingData)); memset(m_supporting_data, 0, sizeof(ExpressionSupportingData));
} }

View File

@ -24,7 +24,7 @@ namespace IW4
ExpressionSupportingData* m_supporting_data; ExpressionSupportingData* m_supporting_data;
MenuConversionZoneState(); MenuConversionZoneState();
void SetZone(Zone* zone) override; void Inject(ZoneAssetCreationInjection& inject) override;
Statement_s* FindFunction(const std::string& functionName); Statement_s* FindFunction(const std::string& functionName);

View File

@ -10,11 +10,11 @@ MenuConversionZoneState::MenuConversionZoneState()
{ {
} }
void MenuConversionZoneState::SetZone(Zone* zone) void MenuConversionZoneState::Inject(ZoneAssetCreationInjection& inject)
{ {
auto* memory = zone->GetMemory(); auto* memory = inject.m_zone.GetMemory();
m_zone = zone; m_zone = &inject.m_zone;
m_supporting_data = memory->Create<ExpressionSupportingData>(); m_supporting_data = memory->Create<ExpressionSupportingData>();
memset(m_supporting_data, 0, sizeof(ExpressionSupportingData)); memset(m_supporting_data, 0, sizeof(ExpressionSupportingData));
} }

View File

@ -24,7 +24,7 @@ namespace IW5
ExpressionSupportingData* m_supporting_data; ExpressionSupportingData* m_supporting_data;
MenuConversionZoneState(); MenuConversionZoneState();
void SetZone(Zone* zone) override; void Inject(ZoneAssetCreationInjection& inject) override;
Statement_s* FindFunction(const std::string& functionName); Statement_s* FindFunction(const std::string& functionName);