Skip to content

Commit

Permalink
Merge pull request #357 from Laupetin/fix/load-non-reference-assets
Browse files Browse the repository at this point in the history
fix: load non reference assets
  • Loading branch information
Laupetin authored Jan 21, 2025
2 parents 8ace49b + 4d0b065 commit f436cd6
Show file tree
Hide file tree
Showing 25 changed files with 518 additions and 978 deletions.
67 changes: 62 additions & 5 deletions src/ObjLoading/Asset/AssetCreationContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ std::unique_ptr<XAssetInfoGeneric> GenericAssetRegistration::CreateXAssetInfo()
AssetCreationContext::AssetCreationContext(Zone& zone, const AssetCreatorCollection* creators, const IgnoredAssetLookup* ignoredAssetLookup)
: ZoneAssetCreationStateContainer(zone),
m_zone(zone),
m_forced_asset_pools(ZoneAssetPools::CreateForGame(zone.m_game->GetId(), &zone, zone.m_priority)),
m_creators(creators),
m_ignored_asset_lookup(ignoredAssetLookup)
m_ignored_asset_lookup(ignoredAssetLookup),
m_forced_load_depth(0u)
{
}

Expand All @@ -78,10 +80,14 @@ XAssetInfoGeneric* AssetCreationContext::AddAssetGeneric(GenericAssetRegistratio
const auto assetType = xAssetInfo->m_type;
const auto* pAssetName = xAssetInfo->m_name.c_str();

auto* addedAsset = m_zone.m_pools->AddAsset(std::move(xAssetInfo));
XAssetInfoGeneric* addedAsset;
if (m_forced_load_depth > 0)
addedAsset = m_forced_asset_pools->AddAsset(std::move(xAssetInfo));
else
addedAsset = m_zone.m_pools->AddAsset(std::move(xAssetInfo));

if (addedAsset == nullptr)
std::cerr << std::format("Failed to add asset of type \"{}\" to pool: \"{}\"\n", *m_zone.m_pools->GetAssetTypeName(assetType), pAssetName);

return addedAsset;
}

Expand All @@ -102,6 +108,16 @@ XAssetInfoGeneric* AssetCreationContext::LoadDependencyGeneric(const asset_type_
if (alreadyLoadedAsset)
return alreadyLoadedAsset;

if (m_forced_load_depth > 0)
{
alreadyLoadedAsset = m_forced_asset_pools->GetAssetOrAssetReference(assetType, assetName);
if (alreadyLoadedAsset)
return alreadyLoadedAsset;

// If we are already force loading an asset we should not load its dependencies
return LoadDefaultAssetDependency(assetType, std::format(",{}", assetName));
}

if (m_ignored_asset_lookup->IsAssetIgnored(assetType, assetName))
return LoadDefaultAssetDependency(assetType, std::format(",{}", assetName));

Expand All @@ -121,9 +137,9 @@ XAssetInfoGeneric* AssetCreationContext::LoadDependencyGeneric(const asset_type_
return nullptr;
}

IndirectAssetReference AssetCreationContext::LoadIndirectAssetReferenceGeneric(asset_type_t assetType, const std::string& assetName)
IndirectAssetReference AssetCreationContext::LoadIndirectAssetReferenceGeneric(const asset_type_t assetType, const std::string& assetName)
{
auto* alreadyLoadedAsset = m_zone.m_pools->GetAssetOrAssetReference(assetType, assetName);
const auto* alreadyLoadedAsset = m_zone.m_pools->GetAssetOrAssetReference(assetType, assetName);
if (alreadyLoadedAsset)
return IndirectAssetReference(assetType, assetName);

Expand All @@ -137,3 +153,44 @@ IndirectAssetReference AssetCreationContext::LoadIndirectAssetReferenceGeneric(a
}
return IndirectAssetReference(assetType, assetName);
}

XAssetInfoGeneric* AssetCreationContext::ForceLoadDependencyGeneric(const asset_type_t assetType, const std::string& assetName)
{
auto* alreadyLoadedAsset = m_zone.m_pools->GetAssetOrAssetReference(assetType, assetName);
if (alreadyLoadedAsset && !alreadyLoadedAsset->IsReference())
return alreadyLoadedAsset;
alreadyLoadedAsset = m_forced_asset_pools->GetAssetOrAssetReference(assetType, assetName);
if (alreadyLoadedAsset && !alreadyLoadedAsset->IsReference())
return alreadyLoadedAsset;

auto result = AssetCreationResult::NoAction();
if (m_ignored_asset_lookup->IsAssetIgnored(assetType, assetName))
{
// Load default asset to zone
if (!LoadDefaultAssetDependency(assetType, std::format(",{}", assetName)))
return nullptr;

++m_forced_load_depth;

result = m_creators->CreateAsset(assetType, assetName, *this);

assert(m_forced_load_depth > 0);
m_forced_load_depth = std::min(m_forced_load_depth - 1u, 0u);
}
else
result = m_creators->CreateAsset(assetType, assetName, *this);

if (result.HasTakenAction())
{
if (!result.HasFailed())
return result.GetAssetInfo();

std::cerr << std::format("Could not load asset \"{}\" of type \"{}\"\n", assetName, *m_zone.m_pools->GetAssetTypeName(assetType));
}
else
{
std::cerr << std::format("Missing asset \"{}\" of type \"{}\"\n", assetName, *m_zone.m_pools->GetAssetTypeName(assetType));
}

return nullptr;
}
19 changes: 19 additions & 0 deletions src/ObjLoading/Asset/AssetCreationContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,31 @@ class AssetCreationContext : public ZoneAssetCreationStateContainer

IndirectAssetReference LoadIndirectAssetReferenceGeneric(asset_type_t assetType, const std::string& assetName);

/**
* \brief Loads an asset dependency like \c LoadDependency but guarantees that the returned asset is not a reference.
* If normally a reference would be created, the actual asset is loaded but a reference is added to the zone.
* \tparam AssetType The type of the asset
* \param assetName The name of the asset
* \return XAssetInfo of the asset that is guaranteed to not be a reference or \c nullptr
*/
template<typename AssetType> XAssetInfo<typename AssetType::Type>* ForceLoadDependency(const std::string& assetName)
{
static_assert(std::is_base_of_v<IAssetBase, AssetType>);

return static_cast<XAssetInfo<typename AssetType::Type>*>(ForceLoadDependencyGeneric(AssetType::EnumEntry, assetName));
}

XAssetInfoGeneric* ForceLoadDependencyGeneric(asset_type_t assetType, const std::string& assetName);

private:
[[nodiscard]] XAssetInfoGeneric* LoadDefaultAssetDependency(asset_type_t assetType, const std::string& assetName);

Zone& m_zone;
std::unique_ptr<ZoneAssetPools> m_forced_asset_pools;
const AssetCreatorCollection* m_creators;
const IgnoredAssetLookup* m_ignored_asset_lookup;

unsigned m_forced_load_depth;
};

#include "AssetCreatorCollection.h"
4 changes: 2 additions & 2 deletions src/ObjLoading/Game/T6/Weapon/InfoStringLoaderWeaponT6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ namespace

for (const auto& attachmentName : valueArray)
{
auto* attachmentAssetInfo = m_context.LoadDependency<AssetAttachment>(attachmentName);
auto* attachmentAssetInfo = m_context.ForceLoadDependency<AssetAttachment>(attachmentName);
if (attachmentAssetInfo == nullptr)
{
std::cerr << std::format("Failed to load attachment asset \"{}\"\n", attachmentName);
Expand Down Expand Up @@ -314,7 +314,7 @@ namespace

for (const auto& attachmentUniqueName : valueArray)
{
auto* attachmentUniqueAssetInfo = m_context.LoadDependency<AssetAttachmentUnique>(attachmentUniqueName);
auto* attachmentUniqueAssetInfo = m_context.ForceLoadDependency<AssetAttachmentUnique>(attachmentUniqueName);
if (attachmentUniqueAssetInfo == nullptr)
{
std::cerr << std::format("Failed to load attachment unique asset \"{}\"\n", attachmentUniqueName);
Expand Down
148 changes: 41 additions & 107 deletions src/ZoneCommon/Game/IW3/GameAssetPoolIW3.cpp
Original file line number Diff line number Diff line change
@@ -1,131 +1,65 @@
#include "GameAssetPoolIW3.h"

#include "Pool/AssetPoolDynamic.h"
#include "Pool/AssetPoolStatic.h"

#include <cassert>
#include <type_traits>

using namespace IW3;

const char* GameAssetPoolIW3::ASSET_TYPE_NAMES[]{
"xmodelpieces", "physpreset", "xanim", "xmodel", "material", "techniqueset", "image", "sound", "soundcurve", "loadedsound",
"clipmap", "clipmap", "comworld", "gameworldsp", "gameworldmp", "mapents", "gfxworld", "lightdef", "uimap", "font",
"menulist", "menu", "localize", "weapon", "snddriverglobals", "fx", "impactfx", "aitype", "mptype", "character",
"xmodelalias", "rawfile", "stringtable",
};
namespace
{
constexpr const char* ASSET_TYPE_NAMES[]{
"xmodelpieces", "physpreset", "xanim", "xmodel", "material", "techniqueset", "image", "sound", "soundcurve", "loadedsound",
"clipmap", "clipmap", "comworld", "gameworldsp", "gameworldmp", "mapents", "gfxworld", "lightdef", "uimap", "font",
"menulist", "menu", "localize", "weapon", "snddriverglobals", "fx", "impactfx", "aitype", "mptype", "character",
"xmodelalias", "rawfile", "stringtable",
};
}

GameAssetPoolIW3::GameAssetPoolIW3(Zone* zone, const int priority)
GameAssetPoolIW3::GameAssetPoolIW3(Zone* zone, const zone_priority_t priority)
: ZoneAssetPools(zone),
m_priority(priority)
{
static_assert(std::extent_v<decltype(ASSET_TYPE_NAMES)> == ASSET_TYPE_COUNT);
}

void GameAssetPoolIW3::InitPoolStatic(const asset_type_t type, const size_t capacity)
{
#define CASE_INIT_POOL_STATIC(assetType, poolName) \
case assetType: \
{ \
if ((poolName) == nullptr && capacity > 0) \
{ \
(poolName) = std::make_unique<AssetPoolStatic<decltype(poolName)::element_type::type>>(capacity, m_priority, (assetType)); \
} \
break; \
}

switch (type)
{
CASE_INIT_POOL_STATIC(ASSET_TYPE_PHYSPRESET, m_phys_preset)
CASE_INIT_POOL_STATIC(ASSET_TYPE_XANIMPARTS, m_xanim_parts)
CASE_INIT_POOL_STATIC(ASSET_TYPE_XMODEL, m_xmodel)
CASE_INIT_POOL_STATIC(ASSET_TYPE_MATERIAL, m_material)
CASE_INIT_POOL_STATIC(ASSET_TYPE_TECHNIQUE_SET, m_technique_set)
CASE_INIT_POOL_STATIC(ASSET_TYPE_IMAGE, m_image)
CASE_INIT_POOL_STATIC(ASSET_TYPE_SOUND, m_sound)
CASE_INIT_POOL_STATIC(ASSET_TYPE_SOUND_CURVE, m_sound_curve)
CASE_INIT_POOL_STATIC(ASSET_TYPE_LOADED_SOUND, m_loaded_sound)
CASE_INIT_POOL_STATIC(ASSET_TYPE_CLIPMAP, m_clip_map)
CASE_INIT_POOL_STATIC(ASSET_TYPE_CLIPMAP_PVS, m_clip_map)
CASE_INIT_POOL_STATIC(ASSET_TYPE_COMWORLD, m_com_world)
CASE_INIT_POOL_STATIC(ASSET_TYPE_GAMEWORLD_SP, m_game_world_sp)
CASE_INIT_POOL_STATIC(ASSET_TYPE_GAMEWORLD_MP, m_game_world_mp)
CASE_INIT_POOL_STATIC(ASSET_TYPE_MAP_ENTS, m_map_ents)
CASE_INIT_POOL_STATIC(ASSET_TYPE_GFXWORLD, m_gfx_world)
CASE_INIT_POOL_STATIC(ASSET_TYPE_LIGHT_DEF, m_gfx_light_def)
CASE_INIT_POOL_STATIC(ASSET_TYPE_FONT, m_font)
CASE_INIT_POOL_STATIC(ASSET_TYPE_MENULIST, m_menu_list)
CASE_INIT_POOL_STATIC(ASSET_TYPE_MENU, m_menu_def)
CASE_INIT_POOL_STATIC(ASSET_TYPE_LOCALIZE_ENTRY, m_localize)
CASE_INIT_POOL_STATIC(ASSET_TYPE_WEAPON, m_weapon)
CASE_INIT_POOL_STATIC(ASSET_TYPE_FX, m_fx)
CASE_INIT_POOL_STATIC(ASSET_TYPE_IMPACT_FX, m_fx_impact_table)
CASE_INIT_POOL_STATIC(ASSET_TYPE_RAWFILE, m_raw_file)
CASE_INIT_POOL_STATIC(ASSET_TYPE_STRINGTABLE, m_string_table)

default:
assert(type >= 0 && type < ASSET_TYPE_COUNT);
break;
}

#undef CASE_INIT_POOL_STATIC
}

void GameAssetPoolIW3::InitPoolDynamic(const asset_type_t type)
{
#define CASE_INIT_POOL_DYNAMIC(assetType, poolName) \
case assetType: \
{ \
if ((poolName) == nullptr) \
{ \
(poolName) = std::make_unique<AssetPoolDynamic<decltype(poolName)::element_type::type>>(m_priority, (assetType)); \
} \
break; \
}

switch (type)
{
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_PHYSPRESET, m_phys_preset)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_XANIMPARTS, m_xanim_parts)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_XMODEL, m_xmodel)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_MATERIAL, m_material)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_TECHNIQUE_SET, m_technique_set)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_IMAGE, m_image)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_SOUND, m_sound)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_SOUND_CURVE, m_sound_curve)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_LOADED_SOUND, m_loaded_sound)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_CLIPMAP, m_clip_map)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_CLIPMAP_PVS, m_clip_map)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_COMWORLD, m_com_world)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_GAMEWORLD_SP, m_game_world_sp)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_GAMEWORLD_MP, m_game_world_mp)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_MAP_ENTS, m_map_ents)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_GFXWORLD, m_gfx_world)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_LIGHT_DEF, m_gfx_light_def)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_FONT, m_font)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_MENULIST, m_menu_list)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_MENU, m_menu_def)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_LOCALIZE_ENTRY, m_localize)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_WEAPON, m_weapon)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_FX, m_fx)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_IMPACT_FX, m_fx_impact_table)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_RAWFILE, m_raw_file)
CASE_INIT_POOL_DYNAMIC(ASSET_TYPE_STRINGTABLE, m_string_table)

default:
assert(type >= 0 && type < ASSET_TYPE_COUNT);
break;
}

#undef CASE_INIT_POOL_STATIC
#define INIT_POOL(poolName) (poolName) = std::make_unique<AssetPoolDynamic<decltype(poolName)::element_type::type>>(m_priority)

INIT_POOL(m_phys_preset);
INIT_POOL(m_xanim_parts);
INIT_POOL(m_xmodel);
INIT_POOL(m_material);
INIT_POOL(m_technique_set);
INIT_POOL(m_image);
INIT_POOL(m_sound);
INIT_POOL(m_sound_curve);
INIT_POOL(m_loaded_sound);
INIT_POOL(m_clip_map);
INIT_POOL(m_com_world);
INIT_POOL(m_game_world_sp);
INIT_POOL(m_game_world_mp);
INIT_POOL(m_map_ents);
INIT_POOL(m_gfx_world);
INIT_POOL(m_gfx_light_def);
INIT_POOL(m_font);
INIT_POOL(m_menu_list);
INIT_POOL(m_menu_def);
INIT_POOL(m_localize);
INIT_POOL(m_weapon);
INIT_POOL(m_fx);
INIT_POOL(m_fx_impact_table);
INIT_POOL(m_raw_file);
INIT_POOL(m_string_table);

#undef INIT_POOL
}

XAssetInfoGeneric* GameAssetPoolIW3::AddAssetToPool(std::unique_ptr<XAssetInfoGeneric> xAssetInfo)
{
#define CASE_ADD_TO_POOL(assetType, poolName) \
case assetType: \
{ \
assert((poolName) != nullptr); \
assert(poolName); \
return (poolName)->AddAsset(std::unique_ptr<XAssetInfo<decltype(poolName)::element_type::type>>( \
static_cast<XAssetInfo<decltype(poolName)::element_type::type>*>(xAssetInfo.release()))); \
}
Expand Down Expand Up @@ -174,7 +108,7 @@ XAssetInfoGeneric* GameAssetPoolIW3::GetAsset(const asset_type_t type, const std
#define CASE_GET_ASSET(assetType, poolName) \
case assetType: \
{ \
if ((poolName) != nullptr) \
if (poolName) \
return (poolName)->GetAsset(std::move(name)); \
break; \
}
Expand Down
24 changes: 10 additions & 14 deletions src/ZoneCommon/Game/IW3/GameAssetPoolIW3.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

class GameAssetPoolIW3 final : public ZoneAssetPools
{
int m_priority;

static const char* ASSET_TYPE_NAMES[];

protected:
XAssetInfoGeneric* AddAssetToPool(std::unique_ptr<XAssetInfoGeneric> xAssetInfo) override;

public:
std::unique_ptr<AssetPool<IW3::PhysPreset>> m_phys_preset;
std::unique_ptr<AssetPool<IW3::XAnimParts>> m_xanim_parts;
Expand Down Expand Up @@ -46,17 +39,20 @@ class GameAssetPoolIW3 final : public ZoneAssetPools
std::unique_ptr<AssetPool<IW3::RawFile>> m_raw_file;
std::unique_ptr<AssetPool<IW3::StringTable>> m_string_table;

GameAssetPoolIW3(Zone* zone, int priority);
GameAssetPoolIW3(Zone* zone, zone_priority_t priority);
~GameAssetPoolIW3() override = default;

void InitPoolStatic(asset_type_t type, size_t capacity) override;
void InitPoolDynamic(asset_type_t type) override;

_NODISCARD XAssetInfoGeneric* GetAsset(asset_type_t type, const std::string& name) const override;
[[nodiscard]] XAssetInfoGeneric* GetAsset(asset_type_t type, const std::string& name) const override;

static std::optional<const char*> AssetTypeNameByType(asset_type_t assetType);
_NODISCARD std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const override;
[[nodiscard]] std::optional<const char*> GetAssetTypeName(asset_type_t assetType) const override;

static asset_type_t AssetTypeCount();
_NODISCARD asset_type_t GetAssetTypeCount() const override;
[[nodiscard]] asset_type_t GetAssetTypeCount() const override;

protected:
XAssetInfoGeneric* AddAssetToPool(std::unique_ptr<XAssetInfoGeneric> xAssetInfo) override;

private:
zone_priority_t m_priority;
};
Loading

0 comments on commit f436cd6

Please sign in to comment.