chore: use IObjLoader directly instead of indirection of ObjLoading

This commit is contained in:
Jan 2024-10-19 12:07:12 +02:00
parent c034ac790a
commit 4c9a84777f
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
21 changed files with 62 additions and 136 deletions

View File

@ -3,6 +3,7 @@
#include "AssetLoading/AssetLoadingContext.h" #include "AssetLoading/AssetLoadingContext.h"
#include "Game/IW3/GameAssetPoolIW3.h" #include "Game/IW3/GameAssetPoolIW3.h"
#include "Game/IW3/GameIW3.h" #include "Game/IW3/GameIW3.h"
#include "IObjLoader.h"
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Utils/StringUtils.h" #include "Utils/StringUtils.h"
@ -53,13 +54,14 @@ std::unique_ptr<Zone> ZoneCreator::CreateZoneForDefinition(ZoneCreationContext&
const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(*zone, *context.m_asset_search_path, CreateGdtList(context)); const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(*zone, *context.m_asset_search_path, CreateGdtList(context));
ApplyIgnoredAssets(context, *assetLoadingContext); ApplyIgnoredAssets(context, *assetLoadingContext);
const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::IW3);
for (const auto& assetEntry : context.m_definition->m_assets) for (const auto& assetEntry : context.m_definition->m_assets)
{ {
if (!ObjLoading::LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name)) if (!objLoader->LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name))
return nullptr; return nullptr;
} }
ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); objLoader->FinalizeAssetsForZone(*assetLoadingContext);
return zone; return zone;
} }

View File

@ -2,6 +2,7 @@
#include "Game/IW4/GameAssetPoolIW4.h" #include "Game/IW4/GameAssetPoolIW4.h"
#include "Game/IW4/GameIW4.h" #include "Game/IW4/GameIW4.h"
#include "IObjLoader.h"
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Utils/StringUtils.h" #include "Utils/StringUtils.h"
@ -52,13 +53,14 @@ std::unique_ptr<Zone> ZoneCreator::CreateZoneForDefinition(ZoneCreationContext&
const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(*zone, *context.m_asset_search_path, CreateGdtList(context)); const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(*zone, *context.m_asset_search_path, CreateGdtList(context));
ApplyIgnoredAssets(context, *assetLoadingContext); ApplyIgnoredAssets(context, *assetLoadingContext);
const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::IW4);
for (const auto& assetEntry : context.m_definition->m_assets) for (const auto& assetEntry : context.m_definition->m_assets)
{ {
if (!ObjLoading::LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name)) if (!objLoader->LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name))
return nullptr; return nullptr;
} }
ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); objLoader->FinalizeAssetsForZone(*assetLoadingContext);
return zone; return zone;
} }

View File

@ -2,6 +2,7 @@
#include "Game/IW5/GameAssetPoolIW5.h" #include "Game/IW5/GameAssetPoolIW5.h"
#include "Game/IW5/GameIW5.h" #include "Game/IW5/GameIW5.h"
#include "IObjLoader.h"
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Utils/StringUtils.h" #include "Utils/StringUtils.h"
@ -52,13 +53,14 @@ std::unique_ptr<Zone> ZoneCreator::CreateZoneForDefinition(ZoneCreationContext&
const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(*zone, *context.m_asset_search_path, CreateGdtList(context)); const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(*zone, *context.m_asset_search_path, CreateGdtList(context));
ApplyIgnoredAssets(context, *assetLoadingContext); ApplyIgnoredAssets(context, *assetLoadingContext);
const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::IW5);
for (const auto& assetEntry : context.m_definition->m_assets) for (const auto& assetEntry : context.m_definition->m_assets)
{ {
if (!ObjLoading::LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name)) if (!objLoader->LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name))
return nullptr; return nullptr;
} }
ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); objLoader->FinalizeAssetsForZone(*assetLoadingContext);
return zone; return zone;
} }

View File

@ -3,6 +3,7 @@
#include "AssetLoading/AssetLoadingContext.h" #include "AssetLoading/AssetLoadingContext.h"
#include "Game/T5/GameAssetPoolT5.h" #include "Game/T5/GameAssetPoolT5.h"
#include "Game/T5/GameT5.h" #include "Game/T5/GameT5.h"
#include "IObjLoader.h"
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Utils/StringUtils.h" #include "Utils/StringUtils.h"
@ -53,13 +54,14 @@ std::unique_ptr<Zone> ZoneCreator::CreateZoneForDefinition(ZoneCreationContext&
const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(*zone, *context.m_asset_search_path, CreateGdtList(context)); const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(*zone, *context.m_asset_search_path, CreateGdtList(context));
ApplyIgnoredAssets(context, *assetLoadingContext); ApplyIgnoredAssets(context, *assetLoadingContext);
const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::T5);
for (const auto& assetEntry : context.m_definition->m_assets) for (const auto& assetEntry : context.m_definition->m_assets)
{ {
if (!ObjLoading::LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name)) if (!objLoader->LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name))
return nullptr; return nullptr;
} }
ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); objLoader->FinalizeAssetsForZone(*assetLoadingContext);
return zone; return zone;
} }

View File

@ -4,6 +4,7 @@
#include "Game/T6/GameAssetPoolT6.h" #include "Game/T6/GameAssetPoolT6.h"
#include "Game/T6/GameT6.h" #include "Game/T6/GameT6.h"
#include "Game/T6/T6.h" #include "Game/T6/T6.h"
#include "IObjLoader.h"
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Utils/StringUtils.h" #include "Utils/StringUtils.h"
@ -106,13 +107,14 @@ std::unique_ptr<Zone> ZoneCreator::CreateZoneForDefinition(ZoneCreationContext&
HandleMetadata(zone.get(), context); HandleMetadata(zone.get(), context);
const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::T6);
for (const auto& assetEntry : context.m_definition->m_assets) for (const auto& assetEntry : context.m_definition->m_assets)
{ {
if (!ObjLoading::LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name)) if (!objLoader->LoadAssetForZone(*assetLoadingContext, assetEntry.m_asset_type, assetEntry.m_asset_name))
return nullptr; return nullptr;
} }
ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); objLoader->FinalizeAssetsForZone(*assetLoadingContext);
return zone; return zone;
} }

View File

@ -1,5 +1,6 @@
#include "ZoneCreator.h" #include "ZoneCreator.h"
#include "AssetLoading/AssetLoadingContext.h"
#include "Game/IW3/ZoneCreatorIW3.h" #include "Game/IW3/ZoneCreatorIW3.h"
#include "Game/IW4/ZoneCreatorIW4.h" #include "Game/IW4/ZoneCreatorIW4.h"
#include "Game/IW5/ZoneCreatorIW5.h" #include "Game/IW5/ZoneCreatorIW5.h"

View File

@ -48,11 +48,6 @@ ObjLoader::ObjLoader()
#undef REGISTER_ASSET_LOADER #undef REGISTER_ASSET_LOADER
} }
bool ObjLoader::SupportsZone(const Zone& zone) const
{
return zone.m_game == &g_GameIW3;
}
bool ObjLoader::IsMpZone(const Zone& zone) bool ObjLoader::IsMpZone(const Zone& zone)
{ {
return zone.m_name.compare(0, 3, "mp_") == 0 || zone.m_name.compare(zone.m_name.length() - 3, 3, "_mp") == 0; return zone.m_name.compare(0, 3, "mp_") == 0 || zone.m_name.compare(zone.m_name.length() - 3, 3, "_mp") == 0;

View File

@ -19,8 +19,6 @@ namespace IW3
public: public:
ObjLoader(); ObjLoader();
[[nodiscard]] bool SupportsZone(const Zone& zone) const override;
void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override;
void UnloadContainersOfZone(Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override;

View File

@ -92,11 +92,6 @@ ObjLoader::ObjLoader()
#undef REGISTER_ASSET_LOADER #undef REGISTER_ASSET_LOADER
} }
bool ObjLoader::SupportsZone(const Zone& zone) const
{
return zone.m_game == &g_GameIW4;
}
bool ObjLoader::IsMpZone(const Zone& zone) bool ObjLoader::IsMpZone(const Zone& zone)
{ {
return zone.m_name.compare(0, 3, "mp_") == 0 || zone.m_name.compare(zone.m_name.length() - 3, 3, "_mp") == 0; return zone.m_name.compare(0, 3, "mp_") == 0 || zone.m_name.compare(zone.m_name.length() - 3, 3, "_mp") == 0;

View File

@ -14,8 +14,6 @@ namespace IW4
public: public:
ObjLoader(); ObjLoader();
[[nodiscard]] bool SupportsZone(const Zone& zone) const override;
void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override;
void UnloadContainersOfZone(Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override;

View File

@ -99,11 +99,6 @@ ObjLoader::ObjLoader()
#undef REGISTER_ASSET_LOADER #undef REGISTER_ASSET_LOADER
} }
bool ObjLoader::SupportsZone(const Zone& zone) const
{
return zone.m_game == &g_GameIW5;
}
bool ObjLoader::IsMpZone(const Zone& zone) bool ObjLoader::IsMpZone(const Zone& zone)
{ {
return zone.m_name.compare(0, 3, "mp_") == 0 || zone.m_name.compare(zone.m_name.length() - 3, 3, "_mp") == 0; return zone.m_name.compare(0, 3, "mp_") == 0 || zone.m_name.compare(zone.m_name.length() - 3, 3, "_mp") == 0;

View File

@ -14,8 +14,6 @@ namespace IW5
public: public:
ObjLoader(); ObjLoader();
[[nodiscard]] bool SupportsZone(const Zone& zone) const override;
void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override;
void UnloadContainersOfZone(Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override;

View File

@ -56,11 +56,6 @@ ObjLoader::ObjLoader()
#undef REGISTER_ASSET_LOADER #undef REGISTER_ASSET_LOADER
} }
bool ObjLoader::SupportsZone(const Zone& zone) const
{
return zone.m_game == &g_GameT5;
}
bool ObjLoader::IsMpZone(const Zone& zone) bool ObjLoader::IsMpZone(const Zone& zone)
{ {
return zone.m_name.compare(0, 3, "mp_") == 0 || zone.m_name.compare(zone.m_name.length() - 3, 3, "_mp") == 0; return zone.m_name.compare(0, 3, "mp_") == 0 || zone.m_name.compare(zone.m_name.length() - 3, 3, "_mp") == 0;

View File

@ -14,8 +14,6 @@ namespace T5
public: public:
ObjLoader(); ObjLoader();
bool SupportsZone(const Zone& zone) const override;
void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override;
void UnloadContainersOfZone(Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override;

View File

@ -125,11 +125,6 @@ namespace T6
#undef REGISTER_ASSET_LOADER #undef REGISTER_ASSET_LOADER
} }
bool ObjLoader::SupportsZone(const Zone& zone) const
{
return zone.m_game == &g_GameT6;
}
bool ObjLoader::VerifySoundBankChecksum(const SoundBank& soundBank, const SndRuntimeAssetBank& sndRuntimeAssetBank) bool ObjLoader::VerifySoundBankChecksum(const SoundBank& soundBank, const SndRuntimeAssetBank& sndRuntimeAssetBank)
{ {
SoundAssetBankChecksum checksum{}; SoundAssetBankChecksum checksum{};

View File

@ -19,8 +19,6 @@ namespace T6
public: public:
ObjLoader(); ObjLoader();
[[nodiscard]] bool SupportsZone(const Zone& zone) const override;
void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override;
void UnloadContainersOfZone(Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override;

View File

@ -0,0 +1,27 @@
#include "IObjLoader.h"
#include "Game/IW3/ObjLoaderIW3.h"
#include "Game/IW4/ObjLoaderIW4.h"
#include "Game/IW5/ObjLoaderIW5.h"
#include "Game/T5/ObjLoaderT5.h"
#include "Game/T6/ObjLoaderT6.h"
#include <cassert>
const IObjLoader* IObjLoader::GetObjLoaderForGame(GameId game)
{
static const IObjLoader* zoneCreators[static_cast<unsigned>(GameId::COUNT)]{
new IW3::ObjLoader(),
new IW4::ObjLoader(),
new IW5::ObjLoader(),
new T5::ObjLoader(),
new T6::ObjLoader(),
};
static_assert(std::extent_v<decltype(zoneCreators)> == static_cast<unsigned>(GameId::COUNT));
assert(static_cast<unsigned>(game) < static_cast<unsigned>(GameId::COUNT));
const auto* result = zoneCreators[static_cast<unsigned>(game)];
assert(result);
return result;
}

View File

@ -14,13 +14,6 @@ public:
IObjLoader& operator=(const IObjLoader& other) = default; IObjLoader& operator=(const IObjLoader& other) = default;
IObjLoader& operator=(IObjLoader&& other) noexcept = default; IObjLoader& operator=(IObjLoader&& other) noexcept = default;
/**
* \brief Checks whether this ObjLoader supports a specified zone.
* \param zone The zone to check.
* \return \c true if the specified zone is supported.
*/
[[nodiscard]] virtual bool SupportsZone(const Zone& zone) const = 0;
/** /**
* \brief Loads all containers that are referenced by a specified zone. * \brief Loads all containers that are referenced by a specified zone.
* \param searchPath The search path object to use to find the referenced containers. * \param searchPath The search path object to use to find the referenced containers.
@ -36,4 +29,6 @@ public:
virtual bool LoadAssetForZone(AssetLoadingContext& context, asset_type_t assetType, const std::string& assetName) const = 0; virtual bool LoadAssetForZone(AssetLoadingContext& context, asset_type_t assetType, const std::string& assetName) const = 0;
virtual void FinalizeAssetsForZone(AssetLoadingContext& context) const = 0; virtual void FinalizeAssetsForZone(AssetLoadingContext& context) const = 0;
static const IObjLoader* GetObjLoaderForGame(GameId game);
}; };

View File

@ -1,10 +1,5 @@
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Game/IW3/ObjLoaderIW3.h"
#include "Game/IW4/ObjLoaderIW4.h"
#include "Game/IW5/ObjLoaderIW5.h"
#include "Game/T5/ObjLoaderT5.h"
#include "Game/T6/ObjLoaderT6.h"
#include "IObjLoader.h" #include "IObjLoader.h"
#include "ObjContainer/IWD/IWD.h" #include "ObjContainer/IWD/IWD.h"
#include "SearchPath/SearchPaths.h" #include "SearchPath/SearchPaths.h"
@ -14,38 +9,6 @@
ObjLoading::Configuration_t ObjLoading::Configuration; ObjLoading::Configuration_t ObjLoading::Configuration;
const IObjLoader* const OBJ_LOADERS[]{
new IW3::ObjLoader(),
new IW4::ObjLoader(),
new IW5::ObjLoader(),
new T5::ObjLoader(),
new T6::ObjLoader(),
};
void ObjLoading::LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone)
{
for (const auto* loader : OBJ_LOADERS)
{
if (loader->SupportsZone(zone))
{
loader->LoadReferencedContainersForZone(searchPath, zone);
return;
}
}
}
void ObjLoading::UnloadContainersOfZone(Zone& zone)
{
for (const auto* loader : OBJ_LOADERS)
{
if (loader->SupportsZone(zone))
{
loader->UnloadContainersOfZone(zone);
return;
}
}
}
void ObjLoading::LoadIWDsInSearchPath(ISearchPath& searchPath) void ObjLoading::LoadIWDsInSearchPath(ISearchPath& searchPath)
{ {
searchPath.Find(SearchPathSearchOptions().IncludeSubdirectories(false).FilterExtensions("iwd"), searchPath.Find(SearchPathSearchOptions().IncludeSubdirectories(false).FilterExtensions("iwd"),
@ -81,28 +44,3 @@ SearchPaths ObjLoading::GetIWDSearchPaths()
return iwdPaths; return iwdPaths;
} }
bool ObjLoading::LoadAssetForZone(AssetLoadingContext& context, const asset_type_t assetType, const std::string& assetName)
{
for (const auto* loader : OBJ_LOADERS)
{
if (loader->SupportsZone(context.m_zone))
{
return loader->LoadAssetForZone(context, assetType, assetName);
}
}
return false;
}
void ObjLoading::FinalizeAssetsForZone(AssetLoadingContext& context)
{
for (const auto* loader : OBJ_LOADERS)
{
if (loader->SupportsZone(context.m_zone))
{
loader->FinalizeAssetsForZone(context);
return;
}
}
}

View File

@ -1,9 +1,7 @@
#pragma once #pragma once
#include "AssetLoading/AssetLoadingContext.h"
#include "SearchPath/ISearchPath.h" #include "SearchPath/ISearchPath.h"
#include "SearchPath/SearchPaths.h" #include "SearchPath/SearchPaths.h"
#include "Zone/Zone.h"
class ObjLoading class ObjLoading
{ {
@ -16,19 +14,6 @@ public:
bool MenuNoOptimization = false; bool MenuNoOptimization = false;
} Configuration; } Configuration;
/**
* \brief Loads all containers that are being referenced by the specified zone.
* \param searchPath The search path to use to find the referenced containers.
* \param zone The zone to load all referenced containers of.
*/
static void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone);
/**
* \brief Unloads all containers that were referenced by a specified zone. If referenced by more than one zone a container will only be unloaded once all
* referencing zones were unloaded the container. \param zone The zone to unload all referenced containers for.
*/
static void UnloadContainersOfZone(Zone& zone);
/** /**
* \brief Loads all IWDs that can be found in a specified search path. * \brief Loads all IWDs that can be found in a specified search path.
* \param searchPath The search path that contains IWDs to be loaded. * \param searchPath The search path that contains IWDs to be loaded.
@ -46,7 +31,4 @@ public:
* \return A \c SearchPaths object containing all IWDs that are currently loaded. * \return A \c SearchPaths object containing all IWDs that are currently loaded.
*/ */
static SearchPaths GetIWDSearchPaths(); static SearchPaths GetIWDSearchPaths();
static bool LoadAssetForZone(AssetLoadingContext& context, asset_type_t assetType, const std::string& assetName);
static void FinalizeAssetsForZone(AssetLoadingContext& context);
}; };

View File

@ -2,6 +2,7 @@
#include "ContentLister/ContentPrinter.h" #include "ContentLister/ContentPrinter.h"
#include "ContentLister/ZoneDefWriter.h" #include "ContentLister/ZoneDefWriter.h"
#include "IObjLoader.h"
#include "ObjContainer/IWD/IWD.h" #include "ObjContainer/IWD/IWD.h"
#include "ObjLoading.h" #include "ObjLoading.h"
#include "ObjWriting.h" #include "ObjWriting.h"
@ -325,7 +326,10 @@ private:
std::cout << std::format("Loaded zone \"{}\"\n", zone->m_name); std::cout << std::format("Loaded zone \"{}\"\n", zone->m_name);
if (ShouldLoadObj()) if (ShouldLoadObj())
ObjLoading::LoadReferencedContainersForZone(searchPathsForZone, *zone); {
const auto* objLoader = IObjLoader::GetObjLoaderForGame(zone->m_game->GetId());
objLoader->LoadReferencedContainersForZone(searchPathsForZone, *zone);
}
m_loaded_zones.emplace_back(std::move(zone)); m_loaded_zones.emplace_back(std::move(zone));
} }
@ -343,7 +347,10 @@ private:
const auto zoneName = loadedZone->m_name; const auto zoneName = loadedZone->m_name;
if (ShouldLoadObj()) if (ShouldLoadObj())
ObjLoading::UnloadContainersOfZone(*loadedZone); {
const auto* objLoader = IObjLoader::GetObjLoaderForGame(loadedZone->m_game->GetId());
objLoader->UnloadContainersOfZone(*loadedZone);
}
loadedZone.reset(); loadedZone.reset();
@ -383,14 +390,15 @@ private:
if (m_args.m_verbose) if (m_args.m_verbose)
std::cout << std::format("Loaded zone \"{}\"\n", zoneName); std::cout << std::format("Loaded zone \"{}\"\n", zoneName);
const auto* objLoader = IObjLoader::GetObjLoaderForGame(zone->m_game->GetId());
if (ShouldLoadObj()) if (ShouldLoadObj())
ObjLoading::LoadReferencedContainersForZone(searchPathsForZone, *zone); objLoader->LoadReferencedContainersForZone(searchPathsForZone, *zone);
if (!HandleZone(searchPathsForZone, *zone)) if (!HandleZone(searchPathsForZone, *zone))
return false; return false;
if (ShouldLoadObj()) if (ShouldLoadObj())
ObjLoading::UnloadContainersOfZone(*zone); objLoader->UnloadContainersOfZone(*zone);
zone.reset(); zone.reset();
if (m_args.m_verbose) if (m_args.m_verbose)