2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-01-25 09:23:03 +00:00

chore: make marker reusable with visitor pattern

This commit is contained in:
Jan Laupetin
2025-12-17 23:53:40 +00:00
parent c73b2ed838
commit 0ef4979873
7 changed files with 188 additions and 131 deletions

View File

@@ -3,100 +3,35 @@
#include <algorithm>
#include <cassert>
AssetMarker::AssetMarker(const asset_type_t assetType, Zone& zone)
: m_zone(zone),
m_asset_type(assetType)
AssetMarker::AssetMarker(AssetVisitor& visitor)
: m_visitor(visitor)
{
}
void AssetMarker::AddDependency(XAssetInfoGeneric* assetInfo)
void AssetMarker::Mark_ScriptString(scr_string_t& scriptString) const
{
if (assetInfo == nullptr)
return;
const auto existingEntry = m_dependencies.find(assetInfo);
if (existingEntry != m_dependencies.end())
return;
m_dependencies.emplace(assetInfo);
const auto result = m_visitor.Visit_ScriptString(scriptString);
if (result.has_value())
scriptString = *result;
}
void AssetMarker::Mark_ScriptString(const scr_string_t scrString)
void AssetMarker::MarkArray_ScriptString(scr_string_t* scriptStringArray, const size_t count) const
{
assert(scrString < m_zone.m_script_strings.Count());
if (scrString >= m_zone.m_script_strings.Count())
return;
m_used_script_strings.emplace(scrString);
}
void AssetMarker::MarkArray_ScriptString(const scr_string_t* scrStringArray, const size_t count)
{
assert(scrStringArray != nullptr);
assert(scriptStringArray != nullptr);
for (size_t index = 0; index < count; index++)
Mark_ScriptString(scrStringArray[index]);
Mark_ScriptString(scriptStringArray[index]);
}
void AssetMarker::Mark_IndirectAssetRef(asset_type_t type, const char* assetRefName)
void AssetMarker::Mark_IndirectAssetRef(const asset_type_t assetType, const char* assetName) const
{
if (!assetRefName || !assetRefName[0])
return;
m_indirect_asset_references.emplace(type, assetRefName);
m_visitor.Visit_IndirectAssetRef(assetType, assetName);
}
void AssetMarker::MarkArray_IndirectAssetRef(const asset_type_t type, const char** assetRefNames, const size_t count)
void AssetMarker::MarkArray_IndirectAssetRef(const asset_type_t assetType, const char** assetNames, const size_t count) const
{
assert(assetRefNames != nullptr);
assert(assetNames != nullptr);
for (size_t index = 0; index < count; index++)
Mark_IndirectAssetRef(type, assetRefNames[index]);
}
XAssetInfoGeneric* AssetMarker::GetAssetInfoByName(const std::string& name) const
{
return m_zone.m_pools->GetAsset(m_asset_type, name);
}
std::vector<XAssetInfoGeneric*> AssetMarker::GetDependencies() const
{
std::vector<XAssetInfoGeneric*> dependencies;
if (!m_dependencies.empty())
{
dependencies.reserve(m_dependencies.size());
for (auto dependency : m_dependencies)
dependencies.push_back(dependency);
}
return dependencies;
}
std::vector<scr_string_t> AssetMarker::GetUsedScriptStrings() const
{
std::vector<scr_string_t> usedScriptStrings;
if (!m_used_script_strings.empty())
{
usedScriptStrings.reserve(m_used_script_strings.size());
for (auto scrString : m_used_script_strings)
usedScriptStrings.push_back(scrString);
std::ranges::sort(usedScriptStrings);
}
return usedScriptStrings;
}
std::vector<IndirectAssetReference> AssetMarker::GetIndirectAssetReferences() const
{
std::vector<IndirectAssetReference> assetReferences;
if (!m_indirect_asset_references.empty())
{
assetReferences.reserve(m_indirect_asset_references.size());
for (const auto& assetReference : m_indirect_asset_references)
assetReferences.emplace_back(assetReference);
}
return assetReferences;
Mark_IndirectAssetRef(assetType, assetNames[index]);
}

View File

@@ -1,36 +1,30 @@
#pragma once
#include "Pool/XAssetInfo.h"
#include "Game/IAsset.h"
#include "Marking/AssetVisitor.h"
#include "Zone/ZoneTypes.h"
#include <unordered_set>
#include <type_traits>
class AssetMarker
{
public:
[[nodiscard]] std::vector<XAssetInfoGeneric*> GetDependencies() const;
[[nodiscard]] std::vector<scr_string_t> GetUsedScriptStrings() const;
[[nodiscard]] std::vector<IndirectAssetReference> GetIndirectAssetReferences() const;
protected:
AssetMarker(asset_type_t assetType, Zone& zone);
explicit AssetMarker(AssetVisitor& visitor);
void AddDependency(XAssetInfoGeneric* assetInfo);
template<typename AssetType> void Mark_Dependency(std::add_lvalue_reference_t<std::add_pointer_t<typename AssetType::Type>> asset)
{
static_assert(std::is_base_of_v<IAssetBase, AssetType>);
void Mark_ScriptString(scr_string_t scrString);
void MarkArray_ScriptString(const scr_string_t* scrStringArray, size_t count);
const auto result = m_visitor.Visit_Dependency(AssetType::EnumEntry, AssetNameAccessor<AssetType>()(*asset));
if (result.has_value())
asset = static_cast<std::add_pointer_t<typename AssetType::Type>>(result->m_ptr);
}
void Mark_IndirectAssetRef(asset_type_t type, const char* assetRefName);
void MarkArray_IndirectAssetRef(asset_type_t type, const char** assetRefNames, size_t count);
void Mark_ScriptString(scr_string_t& scriptString) const;
void MarkArray_ScriptString(scr_string_t* scriptStringArray, size_t count) const;
[[nodiscard]] XAssetInfoGeneric* GetAssetInfoByName(const std::string& name) const;
void Mark_IndirectAssetRef(asset_type_t assetType, const char* assetName) const;
void MarkArray_IndirectAssetRef(asset_type_t assetType, const char** assetNames, size_t count) const;
Zone& m_zone;
private:
asset_type_t m_asset_type;
std::unordered_set<XAssetInfoGeneric*> m_dependencies;
std::unordered_set<scr_string_t> m_used_script_strings;
std::unordered_set<IndirectAssetReference> m_indirect_asset_references;
AssetVisitor& m_visitor;
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include "Pool/XAssetInfo.h"
#include "Zone/ZoneTypes.h"
#include <optional>
class AssetVisitor
{
public:
virtual ~AssetVisitor() = default;
virtual std::optional<XAssetInfoGeneric> Visit_Dependency(asset_type_t assetType, const char* assetName)
{
// Do nothing by default
return std::nullopt;
}
virtual std::optional<scr_string_t> Visit_ScriptString(scr_string_t scriptString)
{
// Do nothing by default
return std::nullopt;
}
virtual void Visit_IndirectAssetRef(asset_type_t assetType, const char* assetName)
{
// Do nothing by default
}
};