chore: make keyvaluepairscompiler use a zonestate

This commit is contained in:
Jan 2025-01-03 12:36:44 +01:00
parent 3c3161448f
commit ef862ff246
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
9 changed files with 166 additions and 84 deletions

View File

@ -2114,7 +2114,7 @@ namespace T6
struct KeyValuePairs
{
const char* name;
int numVariables;
unsigned int numVariables;
KeyValuePair* keyValuePairs;
};
@ -5569,8 +5569,8 @@ namespace T6
struct KeyValuePair
{
int keyHash;
int namespaceHash;
unsigned int keyHash;
unsigned int namespaceHash;
const char* value;
};

View File

@ -1,74 +0,0 @@
#include "KeyValuePairsCompiler.h"
#include "Game/T6/CommonT6.h"
#include "Game/T6/T6.h"
#include <format>
#include <iostream>
using namespace T6;
KeyValuePairsCompiler::KeyValuePairsCompiler(const Zone& zone, const ZoneDefinition& zoneDefinition)
: m_zone(zone),
m_zone_definition(zoneDefinition)
{
}
std::optional<asset_type_t> KeyValuePairsCompiler::GetHandlingAssetType() const
{
return std::nullopt;
}
AssetCreationResult KeyValuePairsCompiler::CreateAsset(const std::string& assetName, AssetCreationContext& context)
{
return AssetCreationResult::NoAction();
}
void KeyValuePairsCompiler::FinalizeZone(AssetCreationContext& context)
{
std::vector<KeyValuePair> kvpList;
auto& memory = *m_zone.GetMemory();
for (const auto& metaData : m_zone_definition.m_properties.m_properties)
{
if (metaData.first.rfind("level.", 0) == 0)
{
const std::string strValue = metaData.first.substr(std::char_traits<char>::length("level."));
if (strValue.empty())
continue;
int keyHash;
if (strValue[0] == '@')
{
char* endPtr;
keyHash = strtol(&strValue[1], &endPtr, 16);
if (endPtr != &strValue[strValue.size()])
{
std::cerr << std::format("Could not parse metadata key \"{}\" as hash\n", metaData.first);
continue;
}
}
else
{
keyHash = Common::Com_HashKey(strValue.c_str(), 64);
}
KeyValuePair kvp{keyHash, Common::Com_HashKey(m_zone.m_name.c_str(), 64), memory.Dup(metaData.second.c_str())};
kvpList.push_back(kvp);
}
}
if (!kvpList.empty())
{
auto* kvps = memory.Create<KeyValuePairs>();
kvps->name = memory.Dup(m_zone.m_name.c_str());
kvps->numVariables = static_cast<int>(kvpList.size());
kvps->keyValuePairs = m_zone.GetMemory()->Alloc<KeyValuePair>(kvpList.size());
for (auto i = 0u; i < kvpList.size(); i++)
kvps->keyValuePairs[i] = kvpList[i];
context.AddAsset(AssetRegistration<AssetKeyValuePairs>(m_zone.m_name, kvps));
}
}

View File

@ -0,0 +1,62 @@
#include "KeyValuePairsCompilerT6.h"
#include "Game/T6/CommonT6.h"
#include "Game/T6/T6.h"
#include <cassert>
#include <format>
#include <iostream>
using namespace T6;
KeyValuePairsCompiler::KeyValuePairsCompiler(MemoryManager& memory,
const Zone& zone,
const ZoneDefinition& zoneDefinition,
ZoneAssetCreationStateContainer& zoneStates)
: m_memory(memory),
m_zone(zone),
m_zone_definition(zoneDefinition),
m_kvp_creator(zoneStates.GetZoneAssetCreationState<KeyValuePairsCreator>())
{
}
std::optional<asset_type_t> KeyValuePairsCompiler::GetHandlingAssetType() const
{
return std::nullopt;
}
AssetCreationResult KeyValuePairsCompiler::CreateAsset(const std::string& assetName, AssetCreationContext& context)
{
return AssetCreationResult::NoAction();
}
void KeyValuePairsCompiler::FinalizeZone(AssetCreationContext& context)
{
m_kvp_creator.Finalize(m_zone_definition);
const auto commonKvps = m_kvp_creator.GetFinalKeyValuePairs();
if (commonKvps.empty())
return;
auto* gameKvps = m_memory.Alloc<KeyValuePairs>();
gameKvps->name = m_memory.Dup(m_zone.m_name.c_str());
gameKvps->numVariables = commonKvps.size();
gameKvps->keyValuePairs = m_memory.Alloc<KeyValuePair>(commonKvps.size());
const auto namespaceHash = Common::Com_HashKey(m_zone.m_name.c_str(), 64);
for (auto kvpIndex = 0u; kvpIndex < gameKvps->numVariables; kvpIndex++)
{
const auto& commonKvp = commonKvps[kvpIndex];
auto& gameKvp = gameKvps->keyValuePairs[kvpIndex];
assert(commonKvp.m_key_str || commonKvp.m_key_hash);
if (commonKvp.m_key_str)
gameKvp.keyHash = Common::Com_HashKey(commonKvp.m_key_str->c_str(), 64);
else
gameKvp.keyHash = *commonKvp.m_key_hash;
gameKvp.namespaceHash = namespaceHash;
gameKvp.value = m_memory.Dup(commonKvp.m_value.c_str());
}
context.AddAsset(AssetRegistration<AssetKeyValuePairs>(m_zone.m_name, gameKvps));
}

View File

@ -2,21 +2,26 @@
#include "Asset/IAssetCreator.h"
#include "Game/T6/T6.h"
#include "KeyValuePairs/KeyValuePairsCreator.h"
#include "Utils/MemoryManager.h"
#include "Zone/Definition/ZoneDefinition.h"
#include "Zone/Zone.h"
namespace T6
{
class KeyValuePairsCompiler final : public IAssetCreator
{
public:
KeyValuePairsCompiler(const Zone& zone, const ZoneDefinition& zoneDefinition);
KeyValuePairsCompiler(MemoryManager& memory, const Zone& zone, const ZoneDefinition& zoneDefinition, ZoneAssetCreationStateContainer& zoneStates);
[[nodiscard]] std::optional<asset_type_t> GetHandlingAssetType() const override;
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override;
void FinalizeZone(AssetCreationContext& context) override;
private:
MemoryManager& m_memory;
const Zone& m_zone;
const ZoneDefinition& m_zone_definition;
KeyValuePairsCreator& m_kvp_creator;
};
} // namespace T6

View File

@ -3,7 +3,7 @@
#include "Game/T6/T6.h"
#include "Image/ImageIPakPostProcessor.h"
#include "Image/ImageIwdPostProcessor.h"
#include "KeyValuePairs/KeyValuePairsCompiler.h"
#include "KeyValuePairs/KeyValuePairsCompilerT6.h"
#include <memory>
@ -12,11 +12,15 @@ namespace fs = std::filesystem;
namespace
{
void ConfigureCompilers(AssetCreatorCollection& collection, Zone& zone, const ZoneDefinitionContext& zoneDefinition, ISearchPath& searchPath)
void ConfigureCompilers(AssetCreatorCollection& collection,
Zone& zone,
const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates)
{
auto& memory = *zone.GetMemory();
collection.AddAssetCreator(std::make_unique<KeyValuePairsCompiler>(zone, zoneDefinition.m_zone_definition));
collection.AddAssetCreator(std::make_unique<KeyValuePairsCompiler>(memory, zone, zoneDefinition.m_zone_definition, zoneStates));
}
void ConfigurePostProcessors(AssetCreatorCollection& collection,
@ -45,6 +49,6 @@ void ObjCompiler::ConfigureCreatorCollection(AssetCreatorCollection& collection,
const fs::path& outDir,
const fs::path& cacheDir) const
{
ConfigureCompilers(collection, zone, zoneDefinition, searchPath);
ConfigureCompilers(collection, zone, zoneDefinition, searchPath, zoneStates);
ConfigurePostProcessors(collection, zone, zoneDefinition, searchPath, zoneStates, outDir);
}

View File

@ -0,0 +1,57 @@
#include "KeyValuePairsCreator.h"
#include <format>
#include <iostream>
CommonKeyValuePair::CommonKeyValuePair(std::string keyStr, std::string value)
: m_key_str(std::move(keyStr)),
m_value(std::move(value))
{
}
CommonKeyValuePair::CommonKeyValuePair(const unsigned keyHash, std::string value)
: m_key_hash(keyHash),
m_value(std::move(value))
{
}
void KeyValuePairsCreator::AddKeyValuePair(CommonKeyValuePair keyValuePair)
{
m_key_value_pairs.emplace_back(std::move(keyValuePair));
}
void KeyValuePairsCreator::Finalize(const ZoneDefinition& zoneDefinition)
{
for (const auto& metaData : zoneDefinition.m_properties.m_properties)
{
if (metaData.first.rfind("level.", 0) == 0)
{
std::string strValue = metaData.first.substr(std::char_traits<char>::length("level."));
if (strValue.empty())
continue;
if (strValue[0] == '@')
{
char* endPtr;
const unsigned keyHash = strtoul(&strValue[1], &endPtr, 16);
if (endPtr != &strValue[strValue.size()])
{
std::cerr << std::format("Could not parse metadata key \"{}\" as hash\n", metaData.first);
continue;
}
m_key_value_pairs.emplace_back(keyHash, metaData.second);
}
else
{
m_key_value_pairs.emplace_back(std::move(strValue), metaData.second);
}
}
}
}
std::vector<CommonKeyValuePair> KeyValuePairsCreator::GetFinalKeyValuePairs()
{
return std::move(m_key_value_pairs);
}

View File

@ -0,0 +1,28 @@
#pragma once
#include "Asset/IZoneAssetCreationState.h"
#include "Zone/Definition/ZoneDefinition.h"
#include <string>
class CommonKeyValuePair
{
public:
CommonKeyValuePair(std::string keyStr, std::string value);
CommonKeyValuePair(unsigned keyHash, std::string value);
std::optional<std::string> m_key_str;
std::optional<unsigned> m_key_hash;
std::string m_value;
};
class KeyValuePairsCreator : public IZoneAssetCreationState
{
public:
void AddKeyValuePair(CommonKeyValuePair keyValuePair);
void Finalize(const ZoneDefinition& zoneDefinition);
std::vector<CommonKeyValuePair> GetFinalKeyValuePairs();
private:
std::vector<CommonKeyValuePair> m_key_value_pairs;
};

View File

@ -252,7 +252,7 @@ namespace T6
for (auto* keyValuePairsEntry : *assetPoolT6->m_key_value_pairs)
{
const auto* keyValuePairs = keyValuePairsEntry->Asset();
for (auto variableIndex = 0; variableIndex < keyValuePairs->numVariables; variableIndex++)
for (auto variableIndex = 0u; variableIndex < keyValuePairs->numVariables; variableIndex++)
{
auto* variable = &keyValuePairs->keyValuePairs[variableIndex];

View File

@ -56,7 +56,7 @@ void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const Unli
for (const auto* kvpAsset : *assetPoolT6->m_key_value_pairs)
{
const auto* keyValuePairs = kvpAsset->Asset();
for (auto varIndex = 0; varIndex < keyValuePairs->numVariables; varIndex++)
for (auto varIndex = 0u; varIndex < keyValuePairs->numVariables; varIndex++)
WriteKeyValuePair(stream, keyValuePairs->keyValuePairs[varIndex]);
}