mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-12-27 20:41:49 +00:00
chore: make marker reusable with visitor pattern
This commit is contained in:
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