2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-08-30 21:53:15 +00:00

refactor: streamline obj compiling asset loading

This commit is contained in:
Jan Laupetin
2025-08-05 18:51:56 +02:00
parent 95a6a028ab
commit 33c09dfe61
31 changed files with 505 additions and 495 deletions

View File

@@ -4,6 +4,11 @@
namespace techset
{
std::string GetFileNameForStateMapName(const std::string& stateMapName)
{
return std::format("statemaps/{}.sm", stateMapName);
}
std::string GetFileNameForTechniqueName(const std::string& assetName)
{
return std::format("techniques/{}.tech", assetName);

View File

@@ -4,6 +4,7 @@
namespace techset
{
std::string GetFileNameForStateMapName(const std::string& stateMapName);
std::string GetFileNameForTechniqueName(const std::string& assetName);
std::string GetFileNameForTechsetName(const std::string& assetName);
} // namespace techset

View File

@@ -25,8 +25,8 @@ namespace
{
auto& memory = zone.Memory();
if (ImageIwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<ImageIwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
if (image::IwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<image::IwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
}
} // namespace

View File

@@ -14,6 +14,7 @@
#include "StateMap/StateMapHandler.h"
#include "Techset/TechniqueFileReader.h"
#include "Techset/TechniqueStateMapCache.h"
#include "Techset/TechsetCommon.h"
#include "Techset/TechsetDefinitionCache.h"
#include <cmath>
@@ -45,9 +46,9 @@ namespace
m_search_path(searchPath),
m_context(context),
m_registration(registration),
m_state_map_cache(context.GetZoneAssetCreationState<techset::TechniqueStateMapCache>()),
m_state_map_cache(context.GetZoneAssetCreationState<::techset::TechniqueStateMapCache>()),
m_base_state_bits{},
m_techset_creator(CreateTechsetLoader(memory, searchPath))
m_techset_creator(IW4::techset::CreateLoader(memory, searchPath))
{
}
@@ -793,7 +794,7 @@ namespace
m_registration.AddDependency(techset);
m_material.techniqueSet = techset->Asset();
auto& definitionCache = m_context.GetZoneAssetCreationState<techset::TechsetDefinitionCache>();
auto& definitionCache = m_context.GetZoneAssetCreationState<::techset::TechsetDefinitionCache>();
bool failure = false;
const auto* techsetDefinition = m_techset_creator->LoadTechsetDefinition(techsetName, m_context, failure);
@@ -806,7 +807,7 @@ namespace
SetTechniqueSetCameraRegion(techsetDefinition);
}
void SetTechniqueSetStateBits(const techset::TechsetDefinition* techsetDefinition)
void SetTechniqueSetStateBits(const ::techset::TechsetDefinition* techsetDefinition)
{
for (auto i = 0; i < TECHNIQUE_COUNT; i++)
{
@@ -854,19 +855,19 @@ namespace
return stateBits;
}
_NODISCARD const state_map::StateMapDefinition* GetStateMapForTechnique(const std::string& techniqueName) const
[[nodiscard]] const state_map::StateMapDefinition* GetStateMapForTechnique(const std::string& techniqueName) const
{
const auto* preloadedStateMap = m_state_map_cache.GetStateMapForTechnique(techniqueName);
if (preloadedStateMap)
return preloadedStateMap;
const auto techniqueFileName = GetTechniqueFileName(techniqueName);
const auto techniqueFileName = ::techset::GetFileNameForTechniqueName(techniqueName);
const auto file = m_search_path.Open(techniqueFileName);
if (!file.IsOpen())
return nullptr;
state_map::StateMapFromTechniqueExtractor extractor;
const techset::TechniqueFileReader reader(*file.m_stream, techniqueFileName, &extractor);
const ::techset::TechniqueFileReader reader(*file.m_stream, techniqueFileName, &extractor);
if (!reader.ReadTechniqueDefinition())
{
m_state_map_cache.SetTechniqueUsesStateMap(techniqueName, nullptr);
@@ -890,7 +891,7 @@ namespace
return outBits;
}
void SetTechniqueSetCameraRegion(const techset::TechsetDefinition* techsetDefinition) const
void SetTechniqueSetCameraRegion(const ::techset::TechsetDefinition* techsetDefinition) const
{
std::string tempName;
if (techsetDefinition->GetTechniqueByIndex(TECHNIQUE_LIT, tempName))
@@ -1316,7 +1317,7 @@ namespace
AssetCreationContext& m_context;
AssetRegistration<AssetMaterial>& m_registration;
techset::TechniqueStateMapCache& m_state_map_cache;
::techset::TechniqueStateMapCache& m_state_map_cache;
std::unordered_map<const state_map::StateMapDefinition*, GfxStateBits> m_state_bits_per_state_map;
GfxStateBits m_base_state_bits;
@@ -1324,7 +1325,7 @@ namespace
std::vector<MaterialTextureDef> m_textures;
std::vector<MaterialConstantDef> m_constants;
std::unique_ptr<ITechsetCreator> m_techset_creator;
std::unique_ptr<::IW4::techset::ICreator> m_techset_creator;
};
class MaterialLoader final : public AssetCreator<AssetMaterial>
@@ -1374,10 +1375,10 @@ namespace
};
} // namespace
namespace IW4
namespace IW4::material
{
std::unique_ptr<AssetCreator<AssetMaterial>> CreateMaterialCompiler(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt)
std::unique_ptr<AssetCreator<AssetMaterial>> CreateCompiler(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt)
{
return std::make_unique<MaterialLoader>(memory, searchPath, gdt);
}
} // namespace IW4
} // namespace IW4::material

View File

@@ -6,7 +6,7 @@
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
namespace IW4
namespace IW4::material
{
std::unique_ptr<AssetCreator<AssetMaterial>> CreateMaterialCompiler(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt);
} // namespace IW4
std::unique_ptr<AssetCreator<AssetMaterial>> CreateCompiler(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt);
} // namespace IW4::material

View File

@@ -17,10 +17,10 @@ namespace
auto& memory = zone.Memory();
#ifdef EXPERIMENTAL_MATERIAL_COMPILATION
collection.AddAssetCreator(CreateMaterialCompiler(memory, searchPath, gdt));
collection.AddAssetCreator(CreateTechsetLoader(memory, searchPath));
collection.AddAssetCreator(material::CreateCompiler(memory, searchPath, gdt));
collection.AddAssetCreator(IW4::techset::CreateLoader(memory, searchPath));
#endif
collection.AddAssetCreator(CreateVertexDeclLoader(memory));
collection.AddAssetCreator(vertex_decl::CreateLoader(memory));
}
void ConfigurePostProcessors(AssetCreatorCollection& collection,
@@ -32,8 +32,8 @@ namespace
{
auto& memory = zone.Memory();
if (ImageIwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<ImageIwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
if (image::IwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<image::IwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
}
} // namespace

View File

@@ -9,6 +9,7 @@
#include "StateMap/StateMapReader.h"
#include "Techset/TechniqueFileReader.h"
#include "Techset/TechniqueStateMapCache.h"
#include "Techset/TechsetCommon.h"
#include "Techset/TechsetDefinitionCache.h"
#include "Techset/TechsetFileReader.h"
#include "Utils/Alignment.h"
@@ -25,6 +26,7 @@
#include <unordered_map>
using namespace IW4;
using namespace ::techset;
using namespace std::string_literals;
namespace
@@ -62,7 +64,7 @@ namespace
.first->second.get();
}
literal_t GetAllocatedLiteral(MemoryManager& memory, techset::ShaderArgumentLiteralSource source)
literal_t GetAllocatedLiteral(MemoryManager& memory, ShaderArgumentLiteralSource source)
{
const auto& existingEntry = m_allocated_literals.find(source);
@@ -81,7 +83,7 @@ namespace
private:
std::unordered_map<std::string, std::unique_ptr<LoadedTechnique>> m_loaded_techniques;
std::map<techset::ShaderArgumentLiteralSource, literal_t> m_allocated_literals;
std::map<ShaderArgumentLiteralSource, literal_t> m_allocated_literals;
};
class ShaderInfoFromFileSystemCacheState final : public IZoneAssetCreationState
@@ -121,7 +123,7 @@ namespace
std::unordered_map<std::string, std::unique_ptr<d3d9::ShaderInfo>> m_cached_shader_info;
};
class TechniqueCreator final : public techset::ITechniqueDefinitionAcceptor
class TechniqueCreator final : public ITechniqueDefinitionAcceptor
{
public:
class PassShaderArgument
@@ -197,14 +199,17 @@ namespace
std::vector<PassShaderArgument> m_arguments;
};
TechniqueCreator(
const std::string& techniqueName, ISearchPath& searchPath, MemoryManager& memory, AssetCreationContext& context, ITechsetCreator* techsetCreator)
TechniqueCreator(const std::string& techniqueName,
ISearchPath& searchPath,
MemoryManager& memory,
AssetCreationContext& context,
IW4::techset::ICreator* techsetCreator)
: m_technique_name(techniqueName),
m_search_path(searchPath),
m_memory(memory),
m_context(context),
m_zone_state(context.GetZoneAssetCreationState<TechniqueZoneLoadingState>()),
m_state_map_cache(context.GetZoneAssetCreationState<techset::TechniqueStateMapCache>()),
m_state_map_cache(context.GetZoneAssetCreationState<TechniqueStateMapCache>()),
m_shader_info_cache(context.GetZoneAssetCreationState<ShaderInfoFromFileSystemCacheState>()),
m_techset_creator(techsetCreator)
{
@@ -230,7 +235,7 @@ namespace
|| constant.m_type == d3d9::ParameterType::SAMPLER_CUBE;
}
bool AutoCreateShaderArgument(const techset::ShaderSelector shaderType,
bool AutoCreateShaderArgument(const ShaderSelector shaderType,
const d3d9::ShaderConstant& shaderArgument,
const size_t elementOffset,
const size_t registerOffset)
@@ -239,7 +244,7 @@ namespace
auto& pass = m_passes.at(m_passes.size() - 1);
const auto isSamplerArgument = IsSamplerArgument(shaderArgument);
if (shaderType == techset::ShaderSelector::VERTEX_SHADER && isSamplerArgument)
if (shaderType == ShaderSelector::VERTEX_SHADER && isSamplerArgument)
return false;
MaterialShaderArgument argument;
@@ -270,7 +275,7 @@ namespace
if (!constantSource)
return false;
argument.type = shaderType == techset::ShaderSelector::VERTEX_SHADER ? MTL_ARG_CODE_VERTEX_CONST : MTL_ARG_CODE_PIXEL_CONST;
argument.type = shaderType == ShaderSelector::VERTEX_SHADER ? MTL_ARG_CODE_VERTEX_CONST : MTL_ARG_CODE_PIXEL_CONST;
argument.u.codeConst.index = static_cast<uint16_t>(constantSource->source + elementOffset);
argument.u.codeConst.firstRow = 0u;
argument.u.codeConst.rowCount = static_cast<unsigned char>(shaderArgument.m_type_rows);
@@ -301,7 +306,7 @@ namespace
{
if (!pass.m_handled_vertex_shader_arguments[argumentHandledIndex + elementIndex])
{
if (!AutoCreateShaderArgument(techset::ShaderSelector::VERTEX_SHADER, argument, elementIndex, registerIndex))
if (!AutoCreateShaderArgument(ShaderSelector::VERTEX_SHADER, argument, elementIndex, registerIndex))
{
std::string elementIndexStr;
if (argument.m_type_elements > 1)
@@ -336,7 +341,7 @@ namespace
{
if (!pass.m_handled_pixel_shader_arguments[argumentHandledIndex + elementIndex])
{
if (!AutoCreateShaderArgument(techset::ShaderSelector::PIXEL_SHADER, argument, elementIndex, registerIndex))
if (!AutoCreateShaderArgument(ShaderSelector::PIXEL_SHADER, argument, elementIndex, registerIndex))
{
std::ostringstream ss;
ss << "Unassigned pixel shader \"" << pass.m_pixel_shader->m_name << "\" arg: " << argument.m_name;
@@ -572,11 +577,8 @@ namespace
return foundSource;
}
static bool FindShaderArgument(const d3d9::ShaderInfo& shaderInfo,
const techset::ShaderArgument& argument,
size_t& constantIndex,
size_t& registerOffset,
std::string& errorMessage)
static bool FindShaderArgument(
const d3d9::ShaderInfo& shaderInfo, const ShaderArgument& argument, size_t& constantIndex, size_t& registerOffset, std::string& errorMessage)
{
const auto matchingShaderConstant = std::ranges::find_if(shaderInfo.m_constants,
[argument](const d3d9::ShaderConstant& constant)
@@ -622,7 +624,7 @@ namespace
}
static bool SetArgumentCodeConst(MaterialShaderArgument& argument,
const techset::ShaderArgumentCodeSource& source,
const ShaderArgumentCodeSource& source,
const d3d9::ShaderConstant& shaderConstant,
const unsigned sourceIndex,
const unsigned arrayCount,
@@ -660,7 +662,7 @@ namespace
}
static bool SetArgumentCodeSampler(MaterialShaderArgument& argument,
const techset::ShaderArgumentCodeSource& source,
const ShaderArgumentCodeSource& source,
const d3d9::ShaderConstant& shaderConstant,
const unsigned sourceIndex,
const unsigned arrayCount,
@@ -695,9 +697,7 @@ namespace
return true;
}
bool AcceptVertexShaderConstantArgument(const techset::ShaderArgument& shaderArgument,
const techset::ShaderArgumentCodeSource& source,
std::string& errorMessage)
bool AcceptVertexShaderConstantArgument(const ShaderArgument& shaderArgument, const ShaderArgumentCodeSource& source, std::string& errorMessage)
{
assert(!m_passes.empty());
auto& pass = m_passes.at(m_passes.size() - 1);
@@ -745,8 +745,8 @@ namespace
return true;
}
bool AcceptPixelShaderCodeArgument(const techset::ShaderArgument& shaderArgument,
const techset::ShaderArgumentCodeSource& source,
bool AcceptPixelShaderCodeArgument(const ShaderArgument& shaderArgument,
const ShaderArgumentCodeSource& source,
std::string& errorMessage,
const bool isSampler)
{
@@ -826,36 +826,36 @@ namespace
return true;
}
bool AcceptShaderConstantArgument(const techset::ShaderSelector shader,
const techset::ShaderArgument shaderArgument,
const techset::ShaderArgumentCodeSource source,
bool AcceptShaderConstantArgument(const ShaderSelector shader,
const ShaderArgument shaderArgument,
const ShaderArgumentCodeSource source,
std::string& errorMessage) override
{
if (shader == techset::ShaderSelector::VERTEX_SHADER)
if (shader == ShaderSelector::VERTEX_SHADER)
return AcceptVertexShaderConstantArgument(shaderArgument, source, errorMessage);
assert(shader == techset::ShaderSelector::PIXEL_SHADER);
assert(shader == ShaderSelector::PIXEL_SHADER);
return AcceptPixelShaderCodeArgument(shaderArgument, source, errorMessage, false);
}
bool AcceptShaderSamplerArgument(const techset::ShaderSelector shader,
const techset::ShaderArgument shaderArgument,
const techset::ShaderArgumentCodeSource source,
bool AcceptShaderSamplerArgument(const ShaderSelector shader,
const ShaderArgument shaderArgument,
const ShaderArgumentCodeSource source,
std::string& errorMessage) override
{
if (shader == techset::ShaderSelector::VERTEX_SHADER)
if (shader == ShaderSelector::VERTEX_SHADER)
{
errorMessage = "Vertex sampler are unsupported";
return false;
}
assert(shader == techset::ShaderSelector::PIXEL_SHADER);
assert(shader == ShaderSelector::PIXEL_SHADER);
return AcceptPixelShaderCodeArgument(shaderArgument, source, errorMessage, true);
}
bool AcceptShaderLiteralArgument(const techset::ShaderSelector shader,
const techset::ShaderArgument shaderArgument,
const techset::ShaderArgumentLiteralSource source,
bool AcceptShaderLiteralArgument(const ShaderSelector shader,
const ShaderArgument shaderArgument,
const ShaderArgumentLiteralSource source,
std::string& errorMessage) override
{
assert(!m_passes.empty());
@@ -864,14 +864,14 @@ namespace
MaterialShaderArgument argument;
const d3d9::ShaderInfo* shaderInfo;
if (shader == techset::ShaderSelector::VERTEX_SHADER)
if (shader == ShaderSelector::VERTEX_SHADER)
{
argument.type = MTL_ARG_LITERAL_VERTEX_CONST;
shaderInfo = pass.m_vertex_shader_info;
}
else
{
assert(shader == techset::ShaderSelector::PIXEL_SHADER);
assert(shader == ShaderSelector::PIXEL_SHADER);
argument.type = MTL_ARG_LITERAL_PIXEL_CONST;
shaderInfo = pass.m_pixel_shader_info;
}
@@ -892,7 +892,7 @@ namespace
const auto argumentIsSampler = IsSamplerArgument(shaderConstant);
if (argumentIsSampler)
{
if (shader == techset::ShaderSelector::VERTEX_SHADER)
if (shader == ShaderSelector::VERTEX_SHADER)
errorMessage = "Vertex shader argument expects sampler but got constant";
else
errorMessage = "Pixel shader argument expects sampler but got constant";
@@ -904,7 +904,7 @@ namespace
argument.u.literalConst = m_zone_state.GetAllocatedLiteral(m_memory, source);
pass.m_arguments.emplace_back(argument);
if (shader == techset::ShaderSelector::VERTEX_SHADER)
if (shader == ShaderSelector::VERTEX_SHADER)
pass.m_handled_vertex_shader_arguments[pass.m_vertex_shader_argument_handled_offset[shaderConstantIndex] + elementOffset] = true;
else
pass.m_handled_pixel_shader_arguments[pass.m_pixel_shader_argument_handled_offset[shaderConstantIndex] + elementOffset] = true;
@@ -912,9 +912,9 @@ namespace
return true;
}
bool AcceptShaderMaterialArgument(const techset::ShaderSelector shader,
const techset::ShaderArgument shaderArgument,
const techset::ShaderArgumentMaterialSource source,
bool AcceptShaderMaterialArgument(const ShaderSelector shader,
const ShaderArgument shaderArgument,
const ShaderArgumentMaterialSource source,
std::string& errorMessage) override
{
assert(!m_passes.empty());
@@ -923,13 +923,13 @@ namespace
MaterialShaderArgument argument;
const d3d9::ShaderInfo* shaderInfo;
if (shader == techset::ShaderSelector::VERTEX_SHADER)
if (shader == ShaderSelector::VERTEX_SHADER)
{
shaderInfo = pass.m_vertex_shader_info;
}
else
{
assert(shader == techset::ShaderSelector::PIXEL_SHADER);
assert(shader == ShaderSelector::PIXEL_SHADER);
shaderInfo = pass.m_pixel_shader_info;
}
@@ -947,7 +947,7 @@ namespace
const auto elementOffset = shaderArgument.m_argument_index_specified ? shaderArgument.m_argument_index : 0u;
const auto& shaderConstant = shaderInfo->m_constants[shaderConstantIndex];
const auto argumentIsSampler = IsSamplerArgument(shaderConstant);
if (shader == techset::ShaderSelector::VERTEX_SHADER)
if (shader == ShaderSelector::VERTEX_SHADER)
{
if (argumentIsSampler)
{
@@ -958,7 +958,7 @@ namespace
}
else
{
assert(shader == techset::ShaderSelector::PIXEL_SHADER);
assert(shader == ShaderSelector::PIXEL_SHADER);
argument.type = !argumentIsSampler ? MTL_ARG_MATERIAL_PIXEL_CONST : MTL_ARG_MATERIAL_PIXEL_SAMPLER;
}
@@ -970,7 +970,7 @@ namespace
argument.dest = static_cast<uint16_t>(shaderConstant.m_register_index + registerOffset);
pass.m_arguments.emplace_back(argument);
if (shader == techset::ShaderSelector::VERTEX_SHADER)
if (shader == ShaderSelector::VERTEX_SHADER)
pass.m_handled_vertex_shader_arguments[pass.m_vertex_shader_argument_handled_offset[shaderConstantIndex] + elementOffset] = true;
else
pass.m_handled_pixel_shader_arguments[pass.m_pixel_shader_argument_handled_offset[shaderConstantIndex] + elementOffset] = true;
@@ -1024,15 +1024,15 @@ namespace
MemoryManager& m_memory;
AssetCreationContext& m_context;
TechniqueZoneLoadingState& m_zone_state;
techset::TechniqueStateMapCache& m_state_map_cache;
TechniqueStateMapCache& m_state_map_cache;
ShaderInfoFromFileSystemCacheState& m_shader_info_cache;
ITechsetCreator* m_techset_creator;
IW4::techset::ICreator* m_techset_creator;
};
class TechniqueLoader
{
public:
TechniqueLoader(ISearchPath& searchPath, MemoryManager& memory, AssetCreationContext& context, ITechsetCreator* techsetCreator)
TechniqueLoader(ISearchPath& searchPath, MemoryManager& memory, AssetCreationContext& context, IW4::techset::ICreator* techsetCreator)
: m_search_path(searchPath),
m_memory(memory),
m_context(context),
@@ -1243,13 +1243,13 @@ namespace
MaterialTechnique* LoadTechniqueFromRaw(const std::string& techniqueName, std::vector<XAssetInfoGeneric*>& dependencies) const
{
const auto techniqueFileName = GetTechniqueFileName(techniqueName);
const auto techniqueFileName = GetFileNameForTechniqueName(techniqueName);
const auto file = m_search_path.Open(techniqueFileName);
if (!file.IsOpen())
return nullptr;
TechniqueCreator creator(techniqueName, m_search_path, m_memory, m_context, m_techset_creator);
const techset::TechniqueFileReader reader(*file.m_stream, techniqueFileName, &creator);
const TechniqueFileReader reader(*file.m_stream, techniqueFileName, &creator);
if (!reader.ReadTechniqueDefinition())
return nullptr;
@@ -1260,10 +1260,10 @@ namespace
MemoryManager& m_memory;
AssetCreationContext& m_context;
TechniqueZoneLoadingState& m_zone_state;
ITechsetCreator* m_techset_creator;
IW4::techset::ICreator* m_techset_creator;
};
class TechsetLoader final : public ITechsetCreator
class TechsetLoader final : public IW4::techset::ICreator
{
public:
TechsetLoader(MemoryManager& memory, ISearchPath& searchPath)
@@ -1283,8 +1283,7 @@ namespace
}
private:
AssetCreationResult
CreateTechsetFromDefinition(const std::string& assetName, const techset::TechsetDefinition& definition, AssetCreationContext& context)
AssetCreationResult CreateTechsetFromDefinition(const std::string& assetName, const TechsetDefinition& definition, AssetCreationContext& context)
{
auto* techset = m_memory.Alloc<MaterialTechniqueSet>();
techset->name = m_memory.Dup(assetName.c_str());
@@ -1312,20 +1311,20 @@ namespace
return AssetCreationResult::Success(context.AddAsset(std::move(registration)));
}
techset::TechsetDefinition* LoadTechsetDefinition(const std::string& assetName, AssetCreationContext& context, bool& failure) override
TechsetDefinition* LoadTechsetDefinition(const std::string& assetName, AssetCreationContext& context, bool& failure) override
{
failure = false;
auto& definitionCache = context.GetZoneAssetCreationState<techset::TechsetDefinitionCache>();
auto& definitionCache = context.GetZoneAssetCreationState<TechsetDefinitionCache>();
auto* cachedTechsetDefinition = definitionCache.GetCachedTechsetDefinition(assetName);
if (cachedTechsetDefinition)
return cachedTechsetDefinition;
const auto techsetFileName = GetTechsetFileName(assetName);
const auto techsetFileName = GetFileNameForTechsetName(assetName);
const auto file = m_search_path.Open(techsetFileName);
if (!file.IsOpen())
return nullptr;
const techset::TechsetFileReader reader(*file.m_stream, techsetFileName, techniqueTypeNames, std::extent_v<decltype(techniqueTypeNames)>);
const TechsetFileReader reader(*file.m_stream, techsetFileName, techniqueTypeNames, std::extent_v<decltype(techniqueTypeNames)>);
auto techsetDefinition = reader.ReadTechsetDefinition();
if (!techsetDefinition)
{
@@ -1342,12 +1341,12 @@ namespace
const state_map::StateMapDefinition* LoadStateMapDefinition(const std::string& stateMapName, AssetCreationContext& context) override
{
auto& stateMapCache = context.GetZoneAssetCreationState<techset::TechniqueStateMapCache>();
auto& stateMapCache = context.GetZoneAssetCreationState<TechniqueStateMapCache>();
auto* cachedStateMap = stateMapCache.GetCachedStateMap(stateMapName);
if (cachedStateMap)
return cachedStateMap;
const auto stateMapFileName = GetStateMapFileName(stateMapName);
const auto stateMapFileName = GetFileNameForStateMapName(stateMapName);
const auto file = m_search_path.Open(stateMapFileName);
if (!file.IsOpen())
return nullptr;
@@ -1370,25 +1369,10 @@ namespace
};
} // namespace
namespace IW4
namespace IW4::techset
{
std::string GetTechsetFileName(const std::string& techsetAssetName)
{
return std::format("techsets/{}.techset", techsetAssetName);
}
std::string GetTechniqueFileName(const std::string& techniqueName)
{
return std::format("techniques/{}.tech", techniqueName);
}
std::string GetStateMapFileName(const std::string& stateMapName)
{
return std::format("statemaps/{}.sm", stateMapName);
}
std::unique_ptr<ITechsetCreator> CreateTechsetLoader(MemoryManager& memory, ISearchPath& searchPath)
std::unique_ptr<ICreator> CreateLoader(MemoryManager& memory, ISearchPath& searchPath)
{
return std::make_unique<TechsetLoader>(memory, searchPath);
}
} // namespace IW4
} // namespace IW4::techset

View File

@@ -10,21 +10,17 @@
#include <memory>
#include <string>
namespace IW4
namespace IW4::techset
{
[[nodiscard]] std::string GetTechsetFileName(const std::string& techsetAssetName);
[[nodiscard]] std::string GetTechniqueFileName(const std::string& techniqueName);
[[nodiscard]] std::string GetStateMapFileName(const std::string& stateMapName);
class ITechsetCreator : public AssetCreator<AssetTechniqueSet>
class ICreator : public AssetCreator<AssetTechniqueSet>
{
public:
ITechsetCreator() = default;
virtual ~ITechsetCreator() = default;
ICreator() = default;
virtual ~ICreator() = default;
virtual techset::TechsetDefinition* LoadTechsetDefinition(const std::string& assetName, AssetCreationContext& context, bool& failure) = 0;
virtual ::techset::TechsetDefinition* LoadTechsetDefinition(const std::string& assetName, AssetCreationContext& context, bool& failure) = 0;
virtual const state_map::StateMapDefinition* LoadStateMapDefinition(const std::string& stateMapName, AssetCreationContext& context) = 0;
};
std::unique_ptr<ITechsetCreator> CreateTechsetLoader(MemoryManager& memory, ISearchPath& searchPath);
} // namespace IW4
std::unique_ptr<ICreator> CreateLoader(MemoryManager& memory, ISearchPath& searchPath);
} // namespace IW4::techset

View File

@@ -92,10 +92,10 @@ namespace
};
} // namespace
namespace IW4
namespace IW4::vertex_decl
{
std::unique_ptr<AssetCreator<AssetVertexDecl>> CreateVertexDeclLoader(MemoryManager& memory)
std::unique_ptr<AssetCreator<AssetVertexDecl>> CreateLoader(MemoryManager& memory)
{
return std::make_unique<LoaderVertexDecl>(memory);
}
} // namespace IW4
} // namespace IW4::vertex_decl

View File

@@ -7,7 +7,7 @@
#include <memory>
namespace IW4
namespace IW4::vertex_decl
{
std::unique_ptr<AssetCreator<AssetVertexDecl>> CreateVertexDeclLoader(MemoryManager& memory);
} // namespace IW4
std::unique_ptr<AssetCreator<AssetVertexDecl>> CreateLoader(MemoryManager& memory);
} // namespace IW4::vertex_decl

View File

@@ -25,8 +25,8 @@ namespace
{
auto& memory = zone.Memory();
if (ImageIwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<ImageIwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
if (image::IwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<image::IwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
}
} // namespace

View File

@@ -25,8 +25,8 @@ namespace
{
auto& memory = zone.Memory();
if (ImageIwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<ImageIwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
if (image::IwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<image::IwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
}
} // namespace

View File

@@ -18,7 +18,7 @@ namespace
: m_memory(memory),
m_zone(zone),
m_zone_definition(zoneDefinition),
m_kvp_creator(zoneStates.GetZoneAssetCreationState<KeyValuePairsCreator>())
m_kvp_creator(zoneStates.GetZoneAssetCreationState<::key_value_pairs::Creator>())
{
}
@@ -67,15 +67,15 @@ namespace
MemoryManager& m_memory;
const Zone& m_zone;
const ZoneDefinition& m_zone_definition;
KeyValuePairsCreator& m_kvp_creator;
::key_value_pairs::Creator m_kvp_creator;
};
} // namespace
namespace T6
namespace T6::key_value_pairs
{
std::unique_ptr<IAssetCreator>
CreateKeyValuePairsCompiler(MemoryManager& memory, const Zone& zone, const ZoneDefinition& zoneDefinition, ZoneAssetCreationStateContainer& zoneStates)
CreateCompiler(MemoryManager& memory, const Zone& zone, const ZoneDefinition& zoneDefinition, ZoneAssetCreationStateContainer& zoneStates)
{
return std::make_unique<KeyValuePairsCompiler>(memory, zone, zoneDefinition, zoneStates);
}
} // namespace T6
} // namespace T6::key_value_pairs

View File

@@ -9,8 +9,8 @@
#include <memory>
namespace T6
namespace T6::key_value_pairs
{
std::unique_ptr<IAssetCreator>
CreateKeyValuePairsCompiler(MemoryManager& memory, const Zone& zone, const ZoneDefinition& zoneDefinition, ZoneAssetCreationStateContainer& zoneStates);
} // namespace T6
CreateCompiler(MemoryManager& memory, const Zone& zone, const ZoneDefinition& zoneDefinition, ZoneAssetCreationStateContainer& zoneStates);
} // namespace T6::key_value_pairs

View File

@@ -19,7 +19,7 @@ namespace
{
auto& memory = zone.Memory();
collection.AddAssetCreator(CreateKeyValuePairsCompiler(memory, zone, zoneDefinition.m_zone_definition, zoneStates));
collection.AddAssetCreator(T6::key_value_pairs::CreateCompiler(memory, zone, zoneDefinition.m_zone_definition, zoneStates));
}
void ConfigurePostProcessors(AssetCreatorCollection& collection,
@@ -31,11 +31,11 @@ namespace
{
auto& memory = zone.Memory();
if (ImageIPakPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<ImageIPakPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
if (image::IPakPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<image::IPakPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
if (ImageIwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<ImageIwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
if (image::IwdPostProcessor<AssetImage>::AppliesToZoneDefinition(zoneDefinition))
collection.AddAssetPostProcessor(std::make_unique<image::IwdPostProcessor<AssetImage>>(zoneDefinition, searchPath, zoneStates, outDir));
}
} // namespace

View File

@@ -371,68 +371,71 @@ namespace
};
} // namespace
IPakToCreate::IPakToCreate(std::string name)
: m_name(std::move(name))
namespace image
{
}
void IPakToCreate::AddImage(std::string imageName)
{
m_image_names.emplace_back(std::move(imageName));
}
void IPakToCreate::Build(ISearchPath& searchPath, IOutputPath& outPath)
{
const auto file = outPath.Open(std::format("{}.ipak", m_name));
if (!file)
IPakToCreate::IPakToCreate(std::string name)
: m_name(std::move(name))
{
std::cerr << std::format("Failed to open file for ipak {}\n", m_name);
return;
}
IPakWriter writer(*file, searchPath, m_image_names);
writer.Write();
void IPakToCreate::AddImage(std::string imageName)
{
m_image_names.emplace_back(std::move(imageName));
}
std::cout << std::format("Created ipak {} with {} entries\n", m_name, m_image_names.size());
}
void IPakToCreate::Build(ISearchPath& searchPath, IOutputPath& outPath)
{
const auto file = outPath.Open(std::format("{}.ipak", m_name));
if (!file)
{
std::cerr << std::format("Failed to open file for ipak {}\n", m_name);
return;
}
const std::vector<std::string>& IPakToCreate::GetImageNames() const
{
return m_image_names;
}
IPakWriter writer(*file, searchPath, m_image_names);
writer.Write();
IPakCreator::IPakCreator()
: m_kvp_creator(nullptr)
{
}
std::cout << std::format("Created ipak {} with {} entries\n", m_name, m_image_names.size());
}
void IPakCreator::Inject(ZoneAssetCreationInjection& inject)
{
m_kvp_creator = &inject.m_zone_states.GetZoneAssetCreationState<KeyValuePairsCreator>();
}
const std::vector<std::string>& IPakToCreate::GetImageNames() const
{
return m_image_names;
}
IPakToCreate* IPakCreator::GetOrAddIPak(const std::string& ipakName)
{
const auto existingIPak = m_ipak_lookup.find(ipakName);
if (existingIPak != m_ipak_lookup.end())
return existingIPak->second;
IPakCreator::IPakCreator()
: m_kvp_creator(nullptr)
{
}
auto newIPak = std::make_unique<IPakToCreate>(ipakName);
auto* result = newIPak.get();
m_ipak_lookup.emplace(ipakName, result);
m_ipaks.emplace_back(std::move(newIPak));
void IPakCreator::Inject(ZoneAssetCreationInjection& inject)
{
m_kvp_creator = &inject.m_zone_states.GetZoneAssetCreationState<key_value_pairs::Creator>();
}
assert(m_kvp_creator);
m_kvp_creator->AddKeyValuePair(CommonKeyValuePair("ipak_read", ipakName));
IPakToCreate* IPakCreator::GetOrAddIPak(const std::string& ipakName)
{
const auto existingIPak = m_ipak_lookup.find(ipakName);
if (existingIPak != m_ipak_lookup.end())
return existingIPak->second;
return result;
}
auto newIPak = std::make_unique<IPakToCreate>(ipakName);
auto* result = newIPak.get();
m_ipak_lookup.emplace(ipakName, result);
m_ipaks.emplace_back(std::move(newIPak));
void IPakCreator::Finalize(ISearchPath& searchPath, IOutputPath& outPath)
{
for (const auto& ipakToCreate : m_ipaks)
ipakToCreate->Build(searchPath, outPath);
assert(m_kvp_creator);
m_kvp_creator->AddKeyValuePair(key_value_pairs::CommonKeyValuePair("ipak_read", ipakName));
m_ipaks.clear();
m_ipak_lookup.clear();
}
return result;
}
void IPakCreator::Finalize(ISearchPath& searchPath, IOutputPath& outPath)
{
for (const auto& ipakToCreate : m_ipaks)
ipakToCreate->Build(searchPath, outPath);
m_ipaks.clear();
m_ipak_lookup.clear();
}
} // namespace image

View File

@@ -9,32 +9,35 @@
#include <string>
#include <unordered_map>
class IPakToCreate
namespace image
{
public:
explicit IPakToCreate(std::string name);
class IPakToCreate
{
public:
explicit IPakToCreate(std::string name);
void AddImage(std::string imageName);
void Build(ISearchPath& searchPath, IOutputPath& outPath);
[[nodiscard]] const std::vector<std::string>& GetImageNames() const;
void AddImage(std::string imageName);
void Build(ISearchPath& searchPath, IOutputPath& outPath);
[[nodiscard]] const std::vector<std::string>& GetImageNames() const;
private:
std::string m_name;
std::vector<std::string> m_image_names;
};
private:
std::string m_name;
std::vector<std::string> m_image_names;
};
class IPakCreator final : public IZoneAssetCreationState
{
public:
IPakCreator();
class IPakCreator final : public IZoneAssetCreationState
{
public:
IPakCreator();
void Inject(ZoneAssetCreationInjection& inject) override;
void Inject(ZoneAssetCreationInjection& inject) override;
IPakToCreate* GetOrAddIPak(const std::string& ipakName);
void Finalize(ISearchPath& searchPath, IOutputPath& outPath);
IPakToCreate* GetOrAddIPak(const std::string& ipakName);
void Finalize(ISearchPath& searchPath, IOutputPath& outPath);
private:
KeyValuePairsCreator* m_kvp_creator;
std::unordered_map<std::string, IPakToCreate*> m_ipak_lookup;
std::vector<std::unique_ptr<IPakToCreate>> m_ipaks;
};
private:
key_value_pairs::Creator* m_kvp_creator;
std::unordered_map<std::string, IPakToCreate*> m_ipak_lookup;
std::vector<std::unique_ptr<IPakToCreate>> m_ipaks;
};
} // namespace image

View File

@@ -4,63 +4,66 @@
#include <algorithm>
AbstractImageIPakPostProcessor::AbstractImageIPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir)
: m_zone_definition(zoneDefinition),
m_search_path(searchPath),
m_ipak_creator(zoneStates.GetZoneAssetCreationState<IPakCreator>()),
m_out_dir(outDir),
m_obj_container_index(0u),
m_current_ipak(nullptr),
m_current_ipak_start_index(0u),
m_current_ipak_end_index(0u)
namespace image
{
FindNextObjContainer();
}
bool AbstractImageIPakPostProcessor::AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition)
{
return std::ranges::any_of(zoneDefinition.m_zone_definition.m_obj_containers,
[](const ZoneDefinitionObjContainer& objContainer)
{
return objContainer.m_type == ZoneDefinitionObjContainerType::IPAK;
});
}
void AbstractImageIPakPostProcessor::FindNextObjContainer()
{
const auto objContainerCount = m_zone_definition.m_zone_definition.m_obj_containers.size();
while (m_obj_container_index < objContainerCount)
AbstractIPakPostProcessor::AbstractIPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir)
: m_zone_definition(zoneDefinition),
m_search_path(searchPath),
m_ipak_creator(zoneStates.GetZoneAssetCreationState<IPakCreator>()),
m_out_dir(outDir),
m_obj_container_index(0u),
m_current_ipak(nullptr),
m_current_ipak_start_index(0u),
m_current_ipak_end_index(0u)
{
const auto& objContainer = m_zone_definition.m_zone_definition.m_obj_containers[m_obj_container_index++];
if (objContainer.m_type != ZoneDefinitionObjContainerType::IPAK)
continue;
m_current_ipak = m_ipak_creator.GetOrAddIPak(objContainer.m_name);
m_current_ipak_start_index = objContainer.m_asset_start;
m_current_ipak_end_index = objContainer.m_asset_end;
return;
FindNextObjContainer();
}
m_current_ipak = nullptr;
}
bool AbstractIPakPostProcessor::AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition)
{
return std::ranges::any_of(zoneDefinition.m_zone_definition.m_obj_containers,
[](const ZoneDefinitionObjContainer& objContainer)
{
return objContainer.m_type == ZoneDefinitionObjContainerType::IPAK;
});
}
void AbstractImageIPakPostProcessor::PostProcessAsset(XAssetInfoGeneric& assetInfo, AssetCreationContext& context)
{
if (assetInfo.m_name.empty() || assetInfo.m_name[0] == ',')
return;
void AbstractIPakPostProcessor::FindNextObjContainer()
{
const auto objContainerCount = m_zone_definition.m_zone_definition.m_obj_containers.size();
while (m_obj_container_index < objContainerCount)
{
const auto& objContainer = m_zone_definition.m_zone_definition.m_obj_containers[m_obj_container_index++];
while (m_current_ipak && m_zone_definition.m_asset_index_in_definition >= m_current_ipak_end_index)
FindNextObjContainer();
if (objContainer.m_type != ZoneDefinitionObjContainerType::IPAK)
continue;
if (m_current_ipak && m_zone_definition.m_asset_index_in_definition >= m_current_ipak_start_index)
m_current_ipak->AddImage(assetInfo.m_name);
}
m_current_ipak = m_ipak_creator.GetOrAddIPak(objContainer.m_name);
m_current_ipak_start_index = objContainer.m_asset_start;
m_current_ipak_end_index = objContainer.m_asset_end;
return;
}
void AbstractImageIPakPostProcessor::FinalizeZone(AssetCreationContext& context)
{
m_ipak_creator.Finalize(m_search_path, m_out_dir);
}
m_current_ipak = nullptr;
}
void AbstractIPakPostProcessor::PostProcessAsset(XAssetInfoGeneric& assetInfo, AssetCreationContext& context)
{
if (assetInfo.m_name.empty() || assetInfo.m_name[0] == ',')
return;
while (m_current_ipak && m_zone_definition.m_asset_index_in_definition >= m_current_ipak_end_index)
FindNextObjContainer();
if (m_current_ipak && m_zone_definition.m_asset_index_in_definition >= m_current_ipak_start_index)
m_current_ipak->AddImage(assetInfo.m_name);
}
void AbstractIPakPostProcessor::FinalizeZone(AssetCreationContext& context)
{
m_ipak_creator.Finalize(m_search_path, m_out_dir);
}
} // namespace image

View File

@@ -5,48 +5,51 @@
#include "Image/IPak/IPakCreator.h"
#include "SearchPath/IOutputPath.h"
class AbstractImageIPakPostProcessor : public IAssetPostProcessor
namespace image
{
public:
AbstractImageIPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir);
static bool AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition);
void PostProcessAsset(XAssetInfoGeneric& assetInfo, AssetCreationContext& context) override;
void FinalizeZone(AssetCreationContext& context) override;
private:
void FindNextObjContainer();
const ZoneDefinitionContext& m_zone_definition;
ISearchPath& m_search_path;
IPakCreator& m_ipak_creator;
IOutputPath& m_out_dir;
unsigned m_obj_container_index;
IPakToCreate* m_current_ipak;
unsigned m_current_ipak_start_index;
unsigned m_current_ipak_end_index;
};
template<typename AssetType> class ImageIPakPostProcessor final : public AbstractImageIPakPostProcessor
{
public:
static_assert(std::is_base_of_v<IAssetBase, AssetType>);
ImageIPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir)
: AbstractImageIPakPostProcessor(zoneDefinition, searchPath, zoneStates, outDir)
class AbstractIPakPostProcessor : public IAssetPostProcessor
{
}
public:
AbstractIPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir);
[[nodiscard]] asset_type_t GetHandlingAssetType() const override
static bool AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition);
void PostProcessAsset(XAssetInfoGeneric& assetInfo, AssetCreationContext& context) override;
void FinalizeZone(AssetCreationContext& context) override;
private:
void FindNextObjContainer();
const ZoneDefinitionContext& m_zone_definition;
ISearchPath& m_search_path;
IPakCreator& m_ipak_creator;
IOutputPath& m_out_dir;
unsigned m_obj_container_index;
IPakToCreate* m_current_ipak;
unsigned m_current_ipak_start_index;
unsigned m_current_ipak_end_index;
};
template<typename AssetType> class IPakPostProcessor final : public AbstractIPakPostProcessor
{
return AssetType::EnumEntry;
}
};
public:
static_assert(std::is_base_of_v<IAssetBase, AssetType>);
IPakPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir)
: AbstractIPakPostProcessor(zoneDefinition, searchPath, zoneStates, outDir)
{
}
[[nodiscard]] asset_type_t GetHandlingAssetType() const override
{
return AssetType::EnumEntry;
}
};
} // namespace image

View File

@@ -1,67 +1,71 @@
#include "ImageIwdPostProcessor.h"
#include "Image/ImageCommon.h"
#include "Iwd/IwdCreator.h"
#include <algorithm>
#include <format>
AbstractImageIwdPostProcessor::AbstractImageIwdPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir)
: m_zone_definition(zoneDefinition),
m_search_path(searchPath),
m_iwd_creator(zoneStates.GetZoneAssetCreationState<IwdCreator>()),
m_out_dir(outDir),
m_obj_container_index(0u),
m_current_iwd(nullptr),
m_current_iwd_start_index(0u),
m_current_iwd_end_index(0u)
namespace image
{
FindNextObjContainer();
}
bool AbstractImageIwdPostProcessor::AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition)
{
return std::ranges::any_of(zoneDefinition.m_zone_definition.m_obj_containers,
[](const ZoneDefinitionObjContainer& objContainer)
{
return objContainer.m_type == ZoneDefinitionObjContainerType::IWD;
});
}
void AbstractImageIwdPostProcessor::FindNextObjContainer()
{
const auto objContainerCount = m_zone_definition.m_zone_definition.m_obj_containers.size();
while (m_obj_container_index < objContainerCount)
AbstractIwdPostProcessor::AbstractIwdPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir)
: m_zone_definition(zoneDefinition),
m_search_path(searchPath),
m_iwd_creator(zoneStates.GetZoneAssetCreationState<IwdCreator>()),
m_out_dir(outDir),
m_obj_container_index(0u),
m_current_iwd(nullptr),
m_current_iwd_start_index(0u),
m_current_iwd_end_index(0u)
{
const auto& objContainer = m_zone_definition.m_zone_definition.m_obj_containers[m_obj_container_index++];
if (objContainer.m_type != ZoneDefinitionObjContainerType::IWD)
continue;
m_current_iwd = m_iwd_creator.GetOrAddIwd(objContainer.m_name);
m_current_iwd_start_index = objContainer.m_asset_start;
m_current_iwd_end_index = objContainer.m_asset_end;
return;
FindNextObjContainer();
}
m_current_iwd = nullptr;
}
bool AbstractIwdPostProcessor::AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition)
{
return std::ranges::any_of(zoneDefinition.m_zone_definition.m_obj_containers,
[](const ZoneDefinitionObjContainer& objContainer)
{
return objContainer.m_type == ZoneDefinitionObjContainerType::IWD;
});
}
void AbstractImageIwdPostProcessor::PostProcessAsset(XAssetInfoGeneric& assetInfo, AssetCreationContext& context)
{
if (assetInfo.m_name.empty() || assetInfo.m_name[0] == ',')
return;
void AbstractIwdPostProcessor::FindNextObjContainer()
{
const auto objContainerCount = m_zone_definition.m_zone_definition.m_obj_containers.size();
while (m_obj_container_index < objContainerCount)
{
const auto& objContainer = m_zone_definition.m_zone_definition.m_obj_containers[m_obj_container_index++];
while (m_current_iwd && m_zone_definition.m_asset_index_in_definition >= m_current_iwd_end_index)
FindNextObjContainer();
if (objContainer.m_type != ZoneDefinitionObjContainerType::IWD)
continue;
if (m_current_iwd && m_zone_definition.m_asset_index_in_definition >= m_current_iwd_start_index)
m_current_iwd->AddFile(std::format("images/{}.iwi", assetInfo.m_name));
}
m_current_iwd = m_iwd_creator.GetOrAddIwd(objContainer.m_name);
m_current_iwd_start_index = objContainer.m_asset_start;
m_current_iwd_end_index = objContainer.m_asset_end;
return;
}
void AbstractImageIwdPostProcessor::FinalizeZone(AssetCreationContext& context)
{
m_iwd_creator.Finalize(m_search_path, m_out_dir);
}
m_current_iwd = nullptr;
}
void AbstractIwdPostProcessor::PostProcessAsset(XAssetInfoGeneric& assetInfo, AssetCreationContext& context)
{
if (assetInfo.m_name.empty() || assetInfo.m_name[0] == ',')
return;
while (m_current_iwd && m_zone_definition.m_asset_index_in_definition >= m_current_iwd_end_index)
FindNextObjContainer();
if (m_current_iwd && m_zone_definition.m_asset_index_in_definition >= m_current_iwd_start_index)
m_current_iwd->AddFile(GetFileNameForAsset(assetInfo.m_name, ".iwi"));
}
void AbstractIwdPostProcessor::FinalizeZone(AssetCreationContext& context)
{
m_iwd_creator.Finalize(m_search_path, m_out_dir);
}
} // namespace image

View File

@@ -5,48 +5,48 @@
#include "Iwd/IwdCreator.h"
#include "SearchPath/IOutputPath.h"
class AbstractImageIwdPostProcessor : public IAssetPostProcessor
namespace image
{
public:
AbstractImageIwdPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir);
static bool AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition);
void PostProcessAsset(XAssetInfoGeneric& assetInfo, AssetCreationContext& context) override;
void FinalizeZone(AssetCreationContext& context) override;
private:
void FindNextObjContainer();
const ZoneDefinitionContext& m_zone_definition;
ISearchPath& m_search_path;
IwdCreator& m_iwd_creator;
IOutputPath& m_out_dir;
unsigned m_obj_container_index;
IwdToCreate* m_current_iwd;
unsigned m_current_iwd_start_index;
unsigned m_current_iwd_end_index;
};
template<typename AssetType> class ImageIwdPostProcessor final : public AbstractImageIwdPostProcessor
{
public:
static_assert(std::is_base_of_v<IAssetBase, AssetType>);
ImageIwdPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir)
: AbstractImageIwdPostProcessor(zoneDefinition, searchPath, zoneStates, outDir)
class AbstractIwdPostProcessor : public IAssetPostProcessor
{
}
public:
AbstractIwdPostProcessor(const ZoneDefinitionContext& zoneDefinition,
ISearchPath& searchPath,
ZoneAssetCreationStateContainer& zoneStates,
IOutputPath& outDir);
[[nodiscard]] asset_type_t GetHandlingAssetType() const override
static bool AppliesToZoneDefinition(const ZoneDefinitionContext& zoneDefinition);
void PostProcessAsset(XAssetInfoGeneric& assetInfo, AssetCreationContext& context) override;
void FinalizeZone(AssetCreationContext& context) override;
private:
void FindNextObjContainer();
const ZoneDefinitionContext& m_zone_definition;
ISearchPath& m_search_path;
IwdCreator& m_iwd_creator;
IOutputPath& m_out_dir;
unsigned m_obj_container_index;
IwdToCreate* m_current_iwd;
unsigned m_current_iwd_start_index;
unsigned m_current_iwd_end_index;
};
template<typename AssetType> class IwdPostProcessor final : public AbstractIwdPostProcessor
{
return AssetType::EnumEntry;
}
};
public:
static_assert(std::is_base_of_v<IAssetBase, AssetType>);
IwdPostProcessor(const ZoneDefinitionContext& zoneDefinition, ISearchPath& searchPath, ZoneAssetCreationStateContainer& zoneStates, IOutputPath& outDir)
: AbstractIwdPostProcessor(zoneDefinition, searchPath, zoneStates, outDir)
{
}
[[nodiscard]] asset_type_t GetHandlingAssetType() const override
{
return AssetType::EnumEntry;
}
};
} // namespace image

View File

@@ -5,72 +5,75 @@
#include <iostream>
#include <ranges>
CommonKeyValuePair::CommonKeyValuePair(std::string keyStr, std::string value)
: m_key_str(std::move(keyStr)),
m_value(std::move(value))
namespace key_value_pairs
{
}
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)
CommonKeyValuePair::CommonKeyValuePair(std::string keyStr, std::string value)
: m_key_str(std::move(keyStr)),
m_value(std::move(value))
{
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::ranges::sort(m_key_value_pairs,
[](const CommonKeyValuePair& v0, const CommonKeyValuePair& v1)
{
if (v0.m_key_str.has_value())
CommonKeyValuePair::CommonKeyValuePair(const unsigned keyHash, std::string value)
: m_key_hash(keyHash),
m_value(std::move(value))
{
}
void Creator::AddKeyValuePair(CommonKeyValuePair keyValuePair)
{
m_key_value_pairs.emplace_back(std::move(keyValuePair));
}
void Creator::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::ranges::sort(m_key_value_pairs,
[](const CommonKeyValuePair& v0, const CommonKeyValuePair& v1)
{
if (!v1.m_key_str.has_value())
return true;
if (v0.m_key_str.has_value())
{
if (!v1.m_key_str.has_value())
return true;
return *v0.m_key_str < *v1.m_key_str;
}
return *v0.m_key_str < *v1.m_key_str;
}
if (!v1.m_key_hash.has_value())
return false;
if (!v1.m_key_hash.has_value())
return false;
return *v0.m_key_hash < *v1.m_key_hash;
});
}
return *v0.m_key_hash < *v1.m_key_hash;
});
}
std::vector<CommonKeyValuePair> KeyValuePairsCreator::GetFinalKeyValuePairs()
{
return std::move(m_key_value_pairs);
}
std::vector<CommonKeyValuePair> Creator::GetFinalKeyValuePairs()
{
return std::move(m_key_value_pairs);
}
} // namespace key_value_pairs

View File

@@ -7,24 +7,27 @@
#include <string>
#include <vector>
class CommonKeyValuePair
namespace key_value_pairs
{
public:
CommonKeyValuePair(std::string keyStr, std::string value);
CommonKeyValuePair(unsigned keyHash, std::string value);
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;
};
std::optional<std::string> m_key_str;
std::optional<unsigned> m_key_hash;
std::string m_value;
};
class KeyValuePairsCreator final : public IZoneAssetCreationState
{
public:
void AddKeyValuePair(CommonKeyValuePair keyValuePair);
void Finalize(const ZoneDefinition& zoneDefinition);
std::vector<CommonKeyValuePair> GetFinalKeyValuePairs();
class Creator final : public IZoneAssetCreationState
{
public:
void AddKeyValuePair(CommonKeyValuePair keyValuePair);
void Finalize(const ZoneDefinition& zoneDefinition);
std::vector<CommonKeyValuePair> GetFinalKeyValuePairs();
private:
std::vector<CommonKeyValuePair> m_key_value_pairs;
};
private:
std::vector<CommonKeyValuePair> m_key_value_pairs;
};
} // namespace key_value_pairs

View File

@@ -11,14 +11,15 @@ namespace techset
{
class TechniqueFileReader
{
public:
TechniqueFileReader(std::istream& stream, std::string fileName, ITechniqueDefinitionAcceptor* acceptor);
[[nodiscard]] bool ReadTechniqueDefinition() const;
private:
std::string m_file_name;
ITechniqueDefinitionAcceptor* m_acceptor;
std::unique_ptr<IParserLineStream> m_base_stream;
std::unique_ptr<IParserLineStream> m_comment_proxy;
public:
TechniqueFileReader(std::istream& stream, std::string fileName, ITechniqueDefinitionAcceptor* acceptor);
_NODISCARD bool ReadTechniqueDefinition() const;
};
} // namespace techset

View File

@@ -26,13 +26,13 @@ namespace
m_creators(m_zone),
m_ignored_assets(),
m_context(m_zone, &m_creators, &m_ignored_assets),
m_kvp_creator(m_zone_states.GetZoneAssetCreationState<KeyValuePairsCreator>())
m_kvp_creator(m_zone_states.GetZoneAssetCreationState<::key_value_pairs::Creator>())
{
}
std::unique_ptr<IAssetCreator> CreateSut()
{
return CreateKeyValuePairsCompiler(m_memory, m_zone, m_zone_definition, m_zone_states);
return T6::key_value_pairs::CreateCompiler(m_memory, m_zone, m_zone_definition, m_zone_states);
}
TestMemoryManager m_memory;
@@ -43,7 +43,7 @@ namespace
IgnoredAssetLookup m_ignored_assets;
AssetCreationContext m_context;
KeyValuePairsCreator& m_kvp_creator;
::key_value_pairs::Creator& m_kvp_creator;
};
} // namespace
@@ -81,7 +81,7 @@ namespace test::game::t6::keyvaluepairs
TestContext testContext;
const auto sut = testContext.CreateSut();
testContext.m_kvp_creator.AddKeyValuePair(CommonKeyValuePair("ipak_read", "test_ipak"));
testContext.m_kvp_creator.AddKeyValuePair(::key_value_pairs::CommonKeyValuePair("ipak_read", "test_ipak"));
sut->FinalizeZone(testContext.m_context);
@@ -107,7 +107,7 @@ namespace test::game::t6::keyvaluepairs
TestContext testContext;
const auto sut = testContext.CreateSut();
testContext.m_kvp_creator.AddKeyValuePair(CommonKeyValuePair(0xDDEEFFAA, "hello_there"));
testContext.m_kvp_creator.AddKeyValuePair(::key_value_pairs::CommonKeyValuePair(0xDDEEFFAA, "hello_there"));
sut->FinalizeZone(testContext.m_context);

View File

@@ -26,9 +26,9 @@ namespace
{
}
IPakCreator& CreateSut()
image::IPakCreator& CreateSut()
{
return m_zone_states.GetZoneAssetCreationState<IPakCreator>();
return m_zone_states.GetZoneAssetCreationState<image::IPakCreator>();
}
Zone m_zone;

View File

@@ -28,13 +28,13 @@ namespace
m_ignored_assets(),
m_out_dir(),
m_context(m_zone, &m_creators, &m_ignored_assets),
m_ipak_creator(m_zone_states.GetZoneAssetCreationState<IPakCreator>())
m_ipak_creator(m_zone_states.GetZoneAssetCreationState<image::IPakCreator>())
{
}
std::unique_ptr<IAssetPostProcessor> CreateSut()
{
return std::make_unique<ImageIPakPostProcessor<AssetImage>>(m_zone_definition_context, m_search_path, m_zone_states, m_out_dir);
return std::make_unique<image::IPakPostProcessor<AssetImage>>(m_zone_definition_context, m_search_path, m_zone_states, m_out_dir);
}
Zone m_zone;
@@ -47,7 +47,7 @@ namespace
MockOutputPath m_out_dir;
AssetCreationContext m_context;
IPakCreator& m_ipak_creator;
image::IPakCreator& m_ipak_creator;
};
} // namespace

View File

@@ -33,7 +33,7 @@ namespace
std::unique_ptr<IAssetPostProcessor> CreateSut()
{
return std::make_unique<ImageIwdPostProcessor<AssetImage>>(m_zone_definition_context, m_search_path, m_zone_states, m_out_dir);
return std::make_unique<image::IwdPostProcessor<AssetImage>>(m_zone_definition_context, m_search_path, m_zone_states, m_out_dir);
}
Zone m_zone;

View File

@@ -13,7 +13,7 @@ namespace test::keyvaluepairs
{
TEST_CASE("KeyValuePairsCreator: ZoneDefinition with no properties produces no KeyValuePairs", "[keyvaluepairs]")
{
KeyValuePairsCreator sut;
key_value_pairs::Creator sut;
ZoneDefinition zoneDefinition;
sut.Finalize(zoneDefinition);
@@ -25,7 +25,7 @@ namespace test::keyvaluepairs
TEST_CASE("KeyValuePairsCreator: ZoneDefinition with unrelated properties produce no KeyValuePairs", "[keyvaluepairs]")
{
KeyValuePairsCreator sut;
key_value_pairs::Creator sut;
ZoneDefinition zoneDefinition;
zoneDefinition.m_properties.AddProperty("linker.test", "yes");
@@ -38,7 +38,7 @@ namespace test::keyvaluepairs
TEST_CASE("KeyValuePairsCreator: ZoneDefinition with level properties produce KeyValuePairs", "[keyvaluepairs]")
{
KeyValuePairsCreator sut;
key_value_pairs::Creator sut;
ZoneDefinition zoneDefinition;
zoneDefinition.m_properties.AddProperty("linker.test", "yes");
@@ -56,7 +56,7 @@ namespace test::keyvaluepairs
TEST_CASE("KeyValuePairsCreator: ZoneDefinition can have level properties with hash", "[keyvaluepairs]")
{
KeyValuePairsCreator sut;
key_value_pairs::Creator sut;
ZoneDefinition zoneDefinition;
zoneDefinition.m_properties.AddProperty("level.@D34DB33F", "yes");
@@ -77,7 +77,7 @@ namespace test::keyvaluepairs
TEST_CASE("KeyValuePairsCreator: ZoneDefinition can have level properties with name and/or hash", "[keyvaluepairs]")
{
KeyValuePairsCreator sut;
key_value_pairs::Creator sut;
ZoneDefinition zoneDefinition;
zoneDefinition.m_properties.AddProperty("level.ipak_read", "asdf");