Merge pull request #150 from Laupetin/fix/crash-on-linking-referenced-asset

fix: crash on trying to link asset with only a referenced asset loaded
This commit is contained in:
Jan 2024-03-30 19:58:49 +01:00 committed by GitHub
commit 89d80ca36a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 39 additions and 24 deletions

View File

@ -59,7 +59,7 @@ XAssetInfoGeneric* AssetLoadingManager::AddAsset(std::unique_ptr<XAssetInfoGener
XAssetInfoGeneric* AssetLoadingManager::LoadIgnoredDependency(const asset_type_t assetType, const std::string& assetName, IAssetLoader* loader) XAssetInfoGeneric* AssetLoadingManager::LoadIgnoredDependency(const asset_type_t assetType, const std::string& assetName, IAssetLoader* loader)
{ {
auto* alreadyLoadedAsset = m_context.m_zone->m_pools->GetAsset(assetType, assetName); auto* alreadyLoadedAsset = m_context.m_zone->m_pools->GetAssetOrAssetReference(assetType, assetName);
if (alreadyLoadedAsset) if (alreadyLoadedAsset)
return alreadyLoadedAsset; return alreadyLoadedAsset;
@ -147,7 +147,7 @@ XAssetInfoGeneric* AssetLoadingManager::LoadAssetDependency(const asset_type_t a
XAssetInfoGeneric* AssetLoadingManager::LoadDependency(const asset_type_t assetType, const std::string& assetName) XAssetInfoGeneric* AssetLoadingManager::LoadDependency(const asset_type_t assetType, const std::string& assetName)
{ {
auto* alreadyLoadedAsset = m_context.m_zone->m_pools->GetAsset(assetType, assetName); auto* alreadyLoadedAsset = m_context.m_zone->m_pools->GetAssetOrAssetReference(assetType, assetName);
if (alreadyLoadedAsset) if (alreadyLoadedAsset)
return alreadyLoadedAsset; return alreadyLoadedAsset;
@ -171,7 +171,7 @@ XAssetInfoGeneric* AssetLoadingManager::LoadDependency(const asset_type_t assetT
IndirectAssetReference AssetLoadingManager::LoadIndirectAssetReference(const asset_type_t assetType, const std::string& assetName) IndirectAssetReference AssetLoadingManager::LoadIndirectAssetReference(const asset_type_t assetType, const std::string& assetName)
{ {
const auto* alreadyLoadedAsset = m_context.m_zone->m_pools->GetAsset(assetType, assetName); const auto* alreadyLoadedAsset = m_context.m_zone->m_pools->GetAssetOrAssetReference(assetType, assetName);
if (alreadyLoadedAsset) if (alreadyLoadedAsset)
return IndirectAssetReference(assetType, assetName); return IndirectAssetReference(assetType, assetName);

View File

@ -115,7 +115,7 @@ class ZoneWriteTemplate::Internal final : BaseTemplate
<< "* asset, Zone* zone, IZoneOutputStream* stream)") << "* asset, Zone* zone, IZoneOutputStream* stream)")
m_intendation++; m_intendation++;
LINE_START(": AssetWriter(zone->m_pools->GetAsset(" << m_env.m_asset->m_asset_enum_entry->m_name << ", GetAssetName(asset))" LINE_START(": AssetWriter(zone->m_pools->GetAssetOrAssetReference(" << m_env.m_asset->m_asset_enum_entry->m_name << ", GetAssetName(asset))"
<< ", zone, stream)") << ", zone, stream)")
LINE_END("") LINE_END("")
m_intendation--; m_intendation--;

View File

@ -169,7 +169,7 @@ XAssetInfoGeneric* GameAssetPoolIW3::AddAssetToPool(std::unique_ptr<XAssetInfoGe
#undef CASE_ADD_TO_POOL #undef CASE_ADD_TO_POOL
} }
XAssetInfoGeneric* GameAssetPoolIW3::GetAsset(const asset_type_t type, std::string name) const XAssetInfoGeneric* GameAssetPoolIW3::GetAsset(const asset_type_t type, const std::string& name) const
{ {
#define CASE_GET_ASSET(assetType, poolName) \ #define CASE_GET_ASSET(assetType, poolName) \
case assetType: \ case assetType: \

View File

@ -52,7 +52,7 @@ public:
void InitPoolStatic(asset_type_t type, size_t capacity) override; void InitPoolStatic(asset_type_t type, size_t capacity) override;
void InitPoolDynamic(asset_type_t type) override; void InitPoolDynamic(asset_type_t type) override;
_NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, std::string name) const override; _NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, const std::string& name) const override;
static const char* AssetTypeNameByType(asset_type_t assetType); static const char* AssetTypeNameByType(asset_type_t assetType);
_NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override; _NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override;

View File

@ -200,13 +200,13 @@ XAssetInfoGeneric* GameAssetPoolIW4::AddAssetToPool(std::unique_ptr<XAssetInfoGe
#undef CASE_ADD_TO_POOL #undef CASE_ADD_TO_POOL
} }
XAssetInfoGeneric* GameAssetPoolIW4::GetAsset(const asset_type_t type, std::string name) const XAssetInfoGeneric* GameAssetPoolIW4::GetAsset(const asset_type_t type, const std::string& name) const
{ {
#define CASE_GET_ASSET(assetType, poolName) \ #define CASE_GET_ASSET(assetType, poolName) \
case assetType: \ case assetType: \
{ \ { \
if ((poolName) != nullptr) \ if ((poolName) != nullptr) \
return (poolName)->GetAsset(std::move(name)); \ return (poolName)->GetAsset(name); \
break; \ break; \
} }

View File

@ -60,7 +60,7 @@ public:
void InitPoolStatic(asset_type_t type, size_t capacity) override; void InitPoolStatic(asset_type_t type, size_t capacity) override;
void InitPoolDynamic(asset_type_t type) override; void InitPoolDynamic(asset_type_t type) override;
_NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, std::string name) const override; _NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, const std::string& name) const override;
static const char* AssetTypeNameByType(asset_type_t assetType); static const char* AssetTypeNameByType(asset_type_t assetType);
_NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override; _NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override;

View File

@ -253,7 +253,7 @@ XAssetInfoGeneric* GameAssetPoolIW5::AddAssetToPool(std::unique_ptr<XAssetInfoGe
#undef CASE_ADD_TO_POOL #undef CASE_ADD_TO_POOL
} }
XAssetInfoGeneric* GameAssetPoolIW5::GetAsset(const asset_type_t type, std::string name) const XAssetInfoGeneric* GameAssetPoolIW5::GetAsset(const asset_type_t type, const std::string& name) const
{ {
#define CASE_GET_ASSET(assetType, poolName) \ #define CASE_GET_ASSET(assetType, poolName) \
case assetType: \ case assetType: \

View File

@ -64,7 +64,7 @@ public:
void InitPoolStatic(asset_type_t type, size_t capacity) override; void InitPoolStatic(asset_type_t type, size_t capacity) override;
void InitPoolDynamic(asset_type_t type) override; void InitPoolDynamic(asset_type_t type) override;
_NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, std::string name) const override; _NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, const std::string& name) const override;
static const char* AssetTypeNameByType(asset_type_t assetType); static const char* AssetTypeNameByType(asset_type_t assetType);
_NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override; _NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override;

View File

@ -193,7 +193,7 @@ XAssetInfoGeneric* GameAssetPoolT5::AddAssetToPool(std::unique_ptr<XAssetInfoGen
#undef CASE_ADD_TO_POOL #undef CASE_ADD_TO_POOL
} }
XAssetInfoGeneric* GameAssetPoolT5::GetAsset(const asset_type_t type, std::string name) const XAssetInfoGeneric* GameAssetPoolT5::GetAsset(const asset_type_t type, const std::string& name) const
{ {
#define CASE_GET_ASSET(assetType, poolName) \ #define CASE_GET_ASSET(assetType, poolName) \
case assetType: \ case assetType: \

View File

@ -56,7 +56,7 @@ public:
void InitPoolStatic(asset_type_t type, size_t capacity) override; void InitPoolStatic(asset_type_t type, size_t capacity) override;
void InitPoolDynamic(asset_type_t type) override; void InitPoolDynamic(asset_type_t type) override;
_NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, std::string name) const override; _NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, const std::string& name) const override;
static const char* AssetTypeNameByType(asset_type_t assetType); static const char* AssetTypeNameByType(asset_type_t assetType);
_NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override; _NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override;

View File

@ -294,13 +294,13 @@ XAssetInfoGeneric* GameAssetPoolT6::AddAssetToPool(std::unique_ptr<XAssetInfoGen
#undef CASE_ADD_TO_POOL #undef CASE_ADD_TO_POOL
} }
XAssetInfoGeneric* GameAssetPoolT6::GetAsset(const asset_type_t type, std::string name) const XAssetInfoGeneric* GameAssetPoolT6::GetAsset(const asset_type_t type, const std::string& name) const
{ {
#define CASE_GET_ASSET(assetType, poolName) \ #define CASE_GET_ASSET(assetType, poolName) \
case assetType: \ case assetType: \
{ \ { \
if ((poolName) != nullptr) \ if ((poolName) != nullptr) \
return (poolName)->GetAsset(std::move(name)); \ return (poolName)->GetAsset(name); \
break; \ break; \
} }

View File

@ -72,7 +72,7 @@ public:
void InitPoolStatic(asset_type_t type, size_t capacity) override; void InitPoolStatic(asset_type_t type, size_t capacity) override;
void InitPoolDynamic(asset_type_t type) override; void InitPoolDynamic(asset_type_t type) override;
_NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, std::string name) const override; _NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, const std::string& name) const override;
static const char* AssetTypeNameByType(asset_type_t assetType); static const char* AssetTypeNameByType(asset_type_t assetType);
_NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override; _NODISCARD const char* GetAssetTypeName(asset_type_t assetType) const override;

View File

@ -1,5 +1,7 @@
#include "ZoneAssetPools.h" #include "ZoneAssetPools.h"
#include <format>
ZoneAssetPools::ZoneAssetPools(Zone* zone) ZoneAssetPools::ZoneAssetPools(Zone* zone)
: m_zone(zone) : m_zone(zone)
{ {
@ -25,6 +27,17 @@ XAssetInfoGeneric* ZoneAssetPools::AddAsset(std::unique_ptr<XAssetInfoGeneric> x
return assetInfo; return assetInfo;
} }
XAssetInfoGeneric* ZoneAssetPools::GetAssetOrAssetReference(const asset_type_t type, const std::string& name) const
{
auto* result = GetAsset(type, name);
if (result != nullptr || (!name.empty() && name[0] == ','))
return result;
result = GetAsset(type, std::format(",{}", name));
return result;
}
size_t ZoneAssetPools::GetTotalAssetCount() const size_t ZoneAssetPools::GetTotalAssetCount() const
{ {
return m_assets_in_order.size(); return m_assets_in_order.size();

View File

@ -38,7 +38,9 @@ public:
std::vector<XAssetInfoGeneric*> dependencies, std::vector<XAssetInfoGeneric*> dependencies,
std::vector<scr_string_t> usedScriptStrings, std::vector<scr_string_t> usedScriptStrings,
std::vector<IndirectAssetReference> indirectAssetReferences); std::vector<IndirectAssetReference> indirectAssetReferences);
_NODISCARD virtual XAssetInfoGeneric* GetAsset(asset_type_t type, std::string name) const = 0; _NODISCARD virtual XAssetInfoGeneric* GetAsset(asset_type_t type, const std::string& name) const = 0;
_NODISCARD virtual XAssetInfoGeneric* GetAssetOrAssetReference(asset_type_t type, const std::string& name) const;
_NODISCARD virtual asset_type_t GetAssetTypeCount() const = 0; _NODISCARD virtual asset_type_t GetAssetTypeCount() const = 0;
_NODISCARD virtual const char* GetAssetTypeName(asset_type_t assetType) const = 0; _NODISCARD virtual const char* GetAssetTypeName(asset_type_t assetType) const = 0;

View File

@ -20,7 +20,7 @@ XAssetInfoGeneric* AssetLoader::LinkAsset(std::string name,
m_asset_type, std::move(name), asset, std::move(dependencies), std::move(scriptStrings), std::move(indirectAssetReferences)); m_asset_type, std::move(name), asset, std::move(dependencies), std::move(scriptStrings), std::move(indirectAssetReferences));
} }
XAssetInfoGeneric* AssetLoader::GetAssetInfo(std::string name) const XAssetInfoGeneric* AssetLoader::GetAssetInfo(const std::string& name) const
{ {
return m_zone->m_pools->GetAsset(m_asset_type, std::move(name)); return m_zone->m_pools->GetAsset(m_asset_type, name);
} }

View File

@ -22,5 +22,5 @@ protected:
std::vector<scr_string_t> scriptStrings, std::vector<scr_string_t> scriptStrings,
std::vector<IndirectAssetReference> indirectAssetReferences) const; std::vector<IndirectAssetReference> indirectAssetReferences) const;
_NODISCARD XAssetInfoGeneric* GetAssetInfo(std::string name) const; _NODISCARD XAssetInfoGeneric* GetAssetInfo(const std::string& name) const;
}; };

View File

@ -55,9 +55,9 @@ void AssetMarker::MarkArray_IndirectAssetRef(const asset_type_t type, const char
Mark_IndirectAssetRef(type, assetRefNames[index]); Mark_IndirectAssetRef(type, assetRefNames[index]);
} }
XAssetInfoGeneric* AssetMarker::GetAssetInfoByName(std::string name) const XAssetInfoGeneric* AssetMarker::GetAssetInfoByName(const std::string& name) const
{ {
return m_zone->m_pools->GetAsset(m_asset_type, std::move(name)); return m_zone->m_pools->GetAsset(m_asset_type, name);
} }
std::vector<XAssetInfoGeneric*> AssetMarker::GetDependencies() const std::vector<XAssetInfoGeneric*> AssetMarker::GetDependencies() const

View File

@ -26,7 +26,7 @@ protected:
void Mark_IndirectAssetRef(asset_type_t type, const char* assetRefName); void Mark_IndirectAssetRef(asset_type_t type, const char* assetRefName);
void MarkArray_IndirectAssetRef(asset_type_t type, const char** assetRefNames, size_t count); void MarkArray_IndirectAssetRef(asset_type_t type, const char** assetRefNames, size_t count);
_NODISCARD XAssetInfoGeneric* GetAssetInfoByName(std::string name) const; _NODISCARD XAssetInfoGeneric* GetAssetInfoByName(const std::string& name) const;
Zone* m_zone; Zone* m_zone;