mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-11 07:18:11 -05:00
test: add test for KeyValuePairsCompilerT6
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
#include "Game/T6/CommonT6.h"
|
||||
#include "Game/T6/T6.h"
|
||||
#include "KeyValuePairs/KeyValuePairsCreator.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <format>
|
||||
@ -9,54 +10,73 @@
|
||||
|
||||
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>())
|
||||
namespace
|
||||
{
|
||||
}
|
||||
|
||||
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++)
|
||||
class KeyValuePairsCompiler final : public IAssetCreator
|
||||
{
|
||||
const auto& commonKvp = commonKvps[kvpIndex];
|
||||
auto& gameKvp = gameKvps->keyValuePairs[kvpIndex];
|
||||
public:
|
||||
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>())
|
||||
{
|
||||
}
|
||||
|
||||
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;
|
||||
[[nodiscard]] std::optional<asset_type_t> GetHandlingAssetType() const override
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
gameKvp.namespaceHash = namespaceHash;
|
||||
gameKvp.value = m_memory.Dup(commonKvp.m_value.c_str());
|
||||
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
|
||||
{
|
||||
return AssetCreationResult::NoAction();
|
||||
}
|
||||
|
||||
void FinalizeZone(AssetCreationContext& context) override
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryManager& m_memory;
|
||||
const Zone& m_zone;
|
||||
const ZoneDefinition& m_zone_definition;
|
||||
KeyValuePairsCreator& m_kvp_creator;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace T6
|
||||
{
|
||||
std::unique_ptr<IAssetCreator>
|
||||
CreateKeyValuePairsCompiler(MemoryManager& memory, const Zone& zone, const ZoneDefinition& zoneDefinition, ZoneAssetCreationStateContainer& zoneStates)
|
||||
{
|
||||
return std::make_unique<KeyValuePairsCompiler>(memory, zone, zoneDefinition, zoneStates);
|
||||
}
|
||||
|
||||
context.AddAsset(AssetRegistration<AssetKeyValuePairs>(m_zone.m_name, gameKvps));
|
||||
}
|
||||
} // namespace T6
|
||||
|
@ -1,27 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "Asset/IAssetCreator.h"
|
||||
#include "Game/T6/T6.h"
|
||||
#include "KeyValuePairs/KeyValuePairsCreator.h"
|
||||
#include "Asset/IZoneAssetCreationState.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
#include "Zone/Definition/ZoneDefinition.h"
|
||||
#include "Zone/Zone.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace T6
|
||||
{
|
||||
class KeyValuePairsCompiler final : public IAssetCreator
|
||||
{
|
||||
public:
|
||||
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;
|
||||
};
|
||||
std::unique_ptr<IAssetCreator>
|
||||
CreateKeyValuePairsCompiler(MemoryManager& memory, const Zone& zone, const ZoneDefinition& zoneDefinition, ZoneAssetCreationStateContainer& zoneStates);
|
||||
} // namespace T6
|
||||
|
@ -20,7 +20,7 @@ namespace
|
||||
{
|
||||
auto& memory = *zone.GetMemory();
|
||||
|
||||
collection.AddAssetCreator(std::make_unique<KeyValuePairsCompiler>(memory, zone, zoneDefinition.m_zone_definition, zoneStates));
|
||||
collection.AddAssetCreator(CreateKeyValuePairsCompiler(memory, zone, zoneDefinition.m_zone_definition, zoneStates));
|
||||
}
|
||||
|
||||
void ConfigurePostProcessors(AssetCreatorCollection& collection,
|
||||
|
Reference in New Issue
Block a user