mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-12-27 12:31:50 +00:00
chore: make marker reusable with visitor pattern
This commit is contained in:
@@ -146,6 +146,8 @@ namespace
|
||||
LINEF("#include \"{0}_load_db.h\"", Lower(m_env.m_asset->m_definition->m_name))
|
||||
LINE("")
|
||||
LINEF("#include \"{0}_mark_db.h\"", Lower(m_env.m_asset->m_definition->m_name))
|
||||
LINE("")
|
||||
LINE("#include \"Loading/AssetInfoCollector.h\"")
|
||||
|
||||
if (!m_env.m_referenced_assets.empty())
|
||||
{
|
||||
@@ -2155,14 +2157,16 @@ namespace
|
||||
|
||||
LINE("assert(pAsset != nullptr);")
|
||||
LINE("")
|
||||
LINEF("{0} marker(m_zone);", MarkerClassName(m_env.m_asset))
|
||||
LINE("AssetInfoCollector assetInfo(m_zone);")
|
||||
LINEF("{0} marker(assetInfo);", MarkerClassName(m_env.m_asset))
|
||||
LINE("marker.Mark(*pAsset);")
|
||||
LINE("")
|
||||
LINEF("auto* reallocatedAsset = m_zone.Memory().Alloc<{0}>();", info->m_definition->GetFullName())
|
||||
LINEF("std::memcpy(reallocatedAsset, *pAsset, sizeof({0}));", info->m_definition->GetFullName())
|
||||
LINE("")
|
||||
LINEF("m_asset_info = reinterpret_cast<XAssetInfo<{0}>*>(LinkAsset(AssetNameAccessor<{1}>()(**pAsset), reallocatedAsset, marker.GetDependencies(), "
|
||||
"marker.GetUsedScriptStrings(), marker.GetIndirectAssetReferences()));",
|
||||
LINEF("m_asset_info = reinterpret_cast<XAssetInfo<{0}>*>(LinkAsset(AssetNameAccessor<{1}>()(**pAsset), reallocatedAsset, "
|
||||
"assetInfo.GetDependencies(), "
|
||||
"assetInfo.GetUsedScriptStrings(), assetInfo.GetIndirectAssetReferences()));",
|
||||
info->m_definition->GetFullName(),
|
||||
info->m_asset_name)
|
||||
LINE("*pAsset = m_asset_info->Asset();")
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace
|
||||
LINE("")
|
||||
LINEF("#include \"Game/{0}/{0}.h\"", m_env.m_game)
|
||||
LINE("#include \"Marking/AssetMarker.h\"")
|
||||
LINE("#include \"Marking/AssetVisitor.h\"")
|
||||
LINE("")
|
||||
LINE("#include <string>")
|
||||
LINE("")
|
||||
@@ -48,7 +49,6 @@ namespace
|
||||
m_intendation++;
|
||||
PrintHeaderConstructor();
|
||||
PrintHeaderMainMarkMethodDeclaration(m_env.m_asset);
|
||||
PrintHeaderGetAssetInfoMethodDeclaration(m_env.m_asset);
|
||||
LINE("")
|
||||
|
||||
m_intendation--;
|
||||
@@ -136,8 +136,6 @@ namespace
|
||||
PrintConstructorMethod();
|
||||
LINE("")
|
||||
PrintMainMarkMethod();
|
||||
LINE("")
|
||||
PrintGetAssetInfoMethod();
|
||||
|
||||
for (const auto* type : m_env.m_used_types)
|
||||
{
|
||||
@@ -210,14 +208,9 @@ namespace
|
||||
LINEF("void Mark_{0}();", MakeSafeTypeName(info->m_definition))
|
||||
}
|
||||
|
||||
void PrintHeaderGetAssetInfoMethodDeclaration(const StructureInformation* info) const
|
||||
{
|
||||
LINEF("XAssetInfo<{0}>* GetAssetInfo({0}* pAsset) const;", info->m_definition->GetFullName())
|
||||
}
|
||||
|
||||
void PrintHeaderConstructor() const
|
||||
{
|
||||
LINEF("{0}(Zone& zone);", MarkerClassName(m_env.m_asset))
|
||||
LINEF("explicit {0}(AssetVisitor& visitor);", MarkerClassName(m_env.m_asset))
|
||||
}
|
||||
|
||||
void PrintHeaderMainMarkMethodDeclaration(const StructureInformation* info) const
|
||||
@@ -237,10 +230,10 @@ namespace
|
||||
|
||||
void PrintConstructorMethod()
|
||||
{
|
||||
LINEF("{0}::{0}(Zone& zone)", MarkerClassName(m_env.m_asset))
|
||||
LINEF("{0}::{0}(AssetVisitor& visitor)", MarkerClassName(m_env.m_asset))
|
||||
|
||||
m_intendation++;
|
||||
LINEF(": AssetMarker({0}::EnumEntry, zone)", m_env.m_asset->m_asset_name)
|
||||
LINE(": AssetMarker(visitor)")
|
||||
m_intendation--;
|
||||
|
||||
LINE("{")
|
||||
@@ -286,7 +279,7 @@ namespace
|
||||
|
||||
if (info && StructureComputations(info).IsAsset())
|
||||
{
|
||||
LINEF("AddDependency({0}(m_zone).GetAssetInfo(*{1}));", MarkerClassName(info), MakeTypePtrVarName(def))
|
||||
LINEF("Mark_Dependency<{0}>(*{1});", info->m_asset_name, MakeTypePtrVarName(def))
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -413,7 +406,7 @@ namespace
|
||||
{
|
||||
if (loadType == MemberLoadType::SINGLE_POINTER)
|
||||
{
|
||||
LINEF("AddDependency({0}(m_zone).GetAssetInfo({1}));", MarkerClassName(member->m_type), MakeMemberAccess(info, member, modifier))
|
||||
LINEF("Mark_Dependency<{0}>({1});", member->m_type->m_asset_name, MakeMemberAccess(info, member, modifier))
|
||||
}
|
||||
else if (loadType == MemberLoadType::POINTER_ARRAY)
|
||||
{
|
||||
@@ -752,20 +745,6 @@ namespace
|
||||
LINE("}")
|
||||
}
|
||||
|
||||
void PrintGetAssetInfoMethod()
|
||||
{
|
||||
LINEF("XAssetInfo<{0}>* {1}::GetAssetInfo({0}* pAsset) const", m_env.m_asset->m_definition->GetFullName(), MarkerClassName(m_env.m_asset))
|
||||
LINE("{")
|
||||
m_intendation++;
|
||||
|
||||
LINEF("return reinterpret_cast<XAssetInfo<{0}>*>(GetAssetInfoByName(AssetNameAccessor<{1}>()(*pAsset)));",
|
||||
m_env.m_asset->m_definition->GetFullName(),
|
||||
m_env.m_asset->m_asset_name)
|
||||
|
||||
m_intendation--;
|
||||
LINE("}")
|
||||
}
|
||||
|
||||
void PrintMainMarkMethod()
|
||||
{
|
||||
LINEF("void {0}::Mark({1}* pAsset)", MarkerClassName(m_env.m_asset), m_env.m_asset->m_definition->GetFullName())
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
29
src/ZoneCommon/Marking/AssetVisitor.h
Normal file
29
src/ZoneCommon/Marking/AssetVisitor.h
Normal 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
|
||||
}
|
||||
};
|
||||
85
src/ZoneLoading/Loading/AssetInfoCollector.cpp
Normal file
85
src/ZoneLoading/Loading/AssetInfoCollector.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "AssetInfoCollector.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
AssetInfoCollector::AssetInfoCollector(Zone& zone)
|
||||
: m_zone(zone)
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<XAssetInfoGeneric*> AssetInfoCollector::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> AssetInfoCollector::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> AssetInfoCollector::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;
|
||||
}
|
||||
|
||||
std::optional<XAssetInfoGeneric> AssetInfoCollector::Visit_Dependency(const asset_type_t assetType, const char* assetName)
|
||||
{
|
||||
auto* assetInfo = m_zone.m_pools->GetAsset(assetType, assetName);
|
||||
if (assetInfo == nullptr)
|
||||
return std::nullopt;
|
||||
|
||||
const auto existingEntry = m_dependencies.find(assetInfo);
|
||||
if (existingEntry != m_dependencies.end())
|
||||
return std::nullopt;
|
||||
|
||||
m_dependencies.emplace(assetInfo);
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<scr_string_t> AssetInfoCollector::Visit_ScriptString(scr_string_t scriptString)
|
||||
{
|
||||
assert(scriptString < m_zone.m_script_strings.Count());
|
||||
|
||||
if (scriptString >= m_zone.m_script_strings.Count())
|
||||
return std::nullopt;
|
||||
|
||||
m_used_script_strings.emplace(scriptString);
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void AssetInfoCollector::Visit_IndirectAssetRef(asset_type_t assetType, const char* assetName)
|
||||
{
|
||||
if (!assetName || !assetName[0])
|
||||
return;
|
||||
|
||||
m_indirect_asset_references.emplace(assetType, assetName);
|
||||
}
|
||||
31
src/ZoneLoading/Loading/AssetInfoCollector.h
Normal file
31
src/ZoneLoading/Loading/AssetInfoCollector.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "Marking/AssetVisitor.h"
|
||||
#include "Pool/XAssetInfo.h"
|
||||
#include "Zone/ZoneTypes.h"
|
||||
|
||||
#include <optional>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
class AssetInfoCollector : public AssetVisitor
|
||||
{
|
||||
public:
|
||||
explicit AssetInfoCollector(Zone& zone);
|
||||
~AssetInfoCollector() override = default;
|
||||
|
||||
[[nodiscard]] std::vector<XAssetInfoGeneric*> GetDependencies() const;
|
||||
[[nodiscard]] std::vector<scr_string_t> GetUsedScriptStrings() const;
|
||||
[[nodiscard]] std::vector<IndirectAssetReference> GetIndirectAssetReferences() const;
|
||||
|
||||
std::optional<XAssetInfoGeneric> Visit_Dependency(asset_type_t assetType, const char* assetName) override;
|
||||
std::optional<scr_string_t> Visit_ScriptString(scr_string_t scriptString) override;
|
||||
void Visit_IndirectAssetRef(asset_type_t assetType, const char* assetName) override;
|
||||
|
||||
private:
|
||||
std::unordered_set<XAssetInfoGeneric*> m_dependencies;
|
||||
std::unordered_set<scr_string_t> m_used_script_strings;
|
||||
std::unordered_set<IndirectAssetReference> m_indirect_asset_references;
|
||||
|
||||
Zone& m_zone;
|
||||
};
|
||||
Reference in New Issue
Block a user