From c034ac790ac62366e0680bf10ac7765441964434 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 13 Oct 2024 00:57:35 +0200 Subject: [PATCH 1/4] refactor: refactor ZoneDefWriter --- src/Common/Game/IGame.h | 1 + src/Common/Game/IW3/GameIW3.cpp | 5 ++ src/Common/Game/IW3/GameIW3.h | 8 +-- src/Common/Game/IW4/GameIW4.cpp | 5 ++ src/Common/Game/IW4/GameIW4.h | 8 +-- src/Common/Game/IW5/GameIW5.cpp | 5 ++ src/Common/Game/IW5/GameIW5.h | 8 +-- src/Common/Game/T5/GameT5.cpp | 5 ++ src/Common/Game/T5/GameT5.h | 8 +-- src/Common/Game/T6/GameT6.cpp | 5 ++ src/Common/Game/T6/GameT6.h | 8 +-- src/Unlinker/ContentLister/ContentPrinter.cpp | 8 +-- src/Unlinker/ContentLister/ContentPrinter.h | 7 +-- src/Unlinker/ContentLister/ZoneDefWriter.cpp | 35 +++++++++++-- src/Unlinker/ContentLister/ZoneDefWriter.h | 16 +++--- src/Unlinker/Game/IW3/ZoneDefWriterIW3.cpp | 16 ++---- src/Unlinker/Game/IW3/ZoneDefWriterIW3.h | 7 +-- src/Unlinker/Game/IW4/ZoneDefWriterIW4.cpp | 16 ++---- src/Unlinker/Game/IW4/ZoneDefWriterIW4.h | 7 +-- src/Unlinker/Game/IW5/ZoneDefWriterIW5.cpp | 16 ++---- src/Unlinker/Game/IW5/ZoneDefWriterIW5.h | 7 +-- src/Unlinker/Game/T5/ZoneDefWriterT5.cpp | 16 ++---- src/Unlinker/Game/T5/ZoneDefWriterT5.h | 7 +-- src/Unlinker/Game/T6/ZoneDefWriterT6.cpp | 48 +++++++---------- src/Unlinker/Game/T6/ZoneDefWriterT6.h | 10 +--- src/Unlinker/Unlinker.cpp | 51 +++++-------------- src/Unlinker/UnlinkerArgs.cpp | 4 +- src/Unlinker/UnlinkerArgs.h | 2 +- 28 files changed, 159 insertions(+), 180 deletions(-) diff --git a/src/Common/Game/IGame.h b/src/Common/Game/IGame.h index b57c0e9b6..065e2ff0d 100644 --- a/src/Common/Game/IGame.h +++ b/src/Common/Game/IGame.h @@ -36,6 +36,7 @@ class IGame IGame& operator=(const IGame& other) = default; IGame& operator=(IGame&& other) noexcept = default; + virtual GameId GetId() = 0; virtual std::string GetFullName() = 0; virtual std::string GetShortName() = 0; virtual void AddZone(Zone* zone) = 0; diff --git a/src/Common/Game/IW3/GameIW3.cpp b/src/Common/Game/IW3/GameIW3.cpp index e3eb45498..788c90176 100644 --- a/src/Common/Game/IW3/GameIW3.cpp +++ b/src/Common/Game/IW3/GameIW3.cpp @@ -8,6 +8,11 @@ using namespace IW3; GameIW3 g_GameIW3; +GameId GameIW3::GetId() +{ + return GameId::IW3; +} + std::string GameIW3::GetFullName() { return "Call Of Duty 4: Modern Warfare"; diff --git a/src/Common/Game/IW3/GameIW3.h b/src/Common/Game/IW3/GameIW3.h index 025533b93..a4f10716f 100644 --- a/src/Common/Game/IW3/GameIW3.h +++ b/src/Common/Game/IW3/GameIW3.h @@ -1,17 +1,19 @@ #pragma once #include "Game/IGame.h" -class GameIW3 : public IGame +class GameIW3 final : public IGame { - std::vector m_zones; - public: + GameId GetId() override; std::string GetFullName() override; std::string GetShortName() override; void AddZone(Zone* zone) override; void RemoveZone(Zone* zone) override; std::vector GetZones() override; std::vector GetLanguagePrefixes() override; + +private: + std::vector m_zones; }; extern GameIW3 g_GameIW3; diff --git a/src/Common/Game/IW4/GameIW4.cpp b/src/Common/Game/IW4/GameIW4.cpp index bc391d45b..beb22d849 100644 --- a/src/Common/Game/IW4/GameIW4.cpp +++ b/src/Common/Game/IW4/GameIW4.cpp @@ -8,6 +8,11 @@ using namespace IW4; GameIW4 g_GameIW4; +GameId GameIW4::GetId() +{ + return GameId::IW4; +} + std::string GameIW4::GetFullName() { return "Call Of Duty: Modern Warfare 2"; diff --git a/src/Common/Game/IW4/GameIW4.h b/src/Common/Game/IW4/GameIW4.h index 5e70acd59..de9cb0b7e 100644 --- a/src/Common/Game/IW4/GameIW4.h +++ b/src/Common/Game/IW4/GameIW4.h @@ -1,17 +1,19 @@ #pragma once #include "Game/IGame.h" -class GameIW4 : public IGame +class GameIW4 final : public IGame { - std::vector m_zones; - public: + GameId GetId() override; std::string GetFullName() override; std::string GetShortName() override; void AddZone(Zone* zone) override; void RemoveZone(Zone* zone) override; std::vector GetZones() override; std::vector GetLanguagePrefixes() override; + +private: + std::vector m_zones; }; extern GameIW4 g_GameIW4; diff --git a/src/Common/Game/IW5/GameIW5.cpp b/src/Common/Game/IW5/GameIW5.cpp index d11f93e9a..d5c6141c2 100644 --- a/src/Common/Game/IW5/GameIW5.cpp +++ b/src/Common/Game/IW5/GameIW5.cpp @@ -8,6 +8,11 @@ using namespace IW5; GameIW5 g_GameIW5; +GameId GameIW5::GetId() +{ + return GameId::IW5; +} + std::string GameIW5::GetFullName() { return "Call Of Duty: Modern Warfare 3"; diff --git a/src/Common/Game/IW5/GameIW5.h b/src/Common/Game/IW5/GameIW5.h index bebf3bbd8..9632c657a 100644 --- a/src/Common/Game/IW5/GameIW5.h +++ b/src/Common/Game/IW5/GameIW5.h @@ -1,17 +1,19 @@ #pragma once #include "Game/IGame.h" -class GameIW5 : public IGame +class GameIW5 final : public IGame { - std::vector m_zones; - public: + GameId GetId() override; std::string GetFullName() override; std::string GetShortName() override; void AddZone(Zone* zone) override; void RemoveZone(Zone* zone) override; std::vector GetZones() override; std::vector GetLanguagePrefixes() override; + +private: + std::vector m_zones; }; extern GameIW5 g_GameIW5; diff --git a/src/Common/Game/T5/GameT5.cpp b/src/Common/Game/T5/GameT5.cpp index 5f4f790b1..28cd06e2d 100644 --- a/src/Common/Game/T5/GameT5.cpp +++ b/src/Common/Game/T5/GameT5.cpp @@ -8,6 +8,11 @@ using namespace T5; GameT5 g_GameT5; +GameId GameT5::GetId() +{ + return GameId::T5; +} + std::string GameT5::GetFullName() { return "Call Of Duty: Black Ops"; diff --git a/src/Common/Game/T5/GameT5.h b/src/Common/Game/T5/GameT5.h index 890e7c81a..df413e5c6 100644 --- a/src/Common/Game/T5/GameT5.h +++ b/src/Common/Game/T5/GameT5.h @@ -1,17 +1,19 @@ #pragma once #include "Game/IGame.h" -class GameT5 : public IGame +class GameT5 final : public IGame { - std::vector m_zones; - public: + GameId GetId() override; std::string GetFullName() override; std::string GetShortName() override; void AddZone(Zone* zone) override; void RemoveZone(Zone* zone) override; std::vector GetZones() override; std::vector GetLanguagePrefixes() override; + +private: + std::vector m_zones; }; extern GameT5 g_GameT5; diff --git a/src/Common/Game/T6/GameT6.cpp b/src/Common/Game/T6/GameT6.cpp index 61149e03f..15a9c590d 100644 --- a/src/Common/Game/T6/GameT6.cpp +++ b/src/Common/Game/T6/GameT6.cpp @@ -8,6 +8,11 @@ using namespace T6; GameT6 g_GameT6; +GameId GameT6::GetId() +{ + return GameId::T6; +} + std::string GameT6::GetFullName() { return "Call Of Duty: Black Ops II"; diff --git a/src/Common/Game/T6/GameT6.h b/src/Common/Game/T6/GameT6.h index 56bdf23bd..ad55f99d1 100644 --- a/src/Common/Game/T6/GameT6.h +++ b/src/Common/Game/T6/GameT6.h @@ -1,17 +1,19 @@ #pragma once #include "Game/IGame.h" -class GameT6 : public IGame +class GameT6 final : public IGame { - std::vector m_zones; - public: + GameId GetId() override; std::string GetFullName() override; std::string GetShortName() override; void AddZone(Zone* zone) override; void RemoveZone(Zone* zone) override; std::vector GetZones() override; std::vector GetLanguagePrefixes() override; + +private: + std::vector m_zones; }; extern GameT6 g_GameT6; diff --git a/src/Unlinker/ContentLister/ContentPrinter.cpp b/src/Unlinker/ContentLister/ContentPrinter.cpp index 612940db0..99e226d11 100644 --- a/src/Unlinker/ContentLister/ContentPrinter.cpp +++ b/src/Unlinker/ContentLister/ContentPrinter.cpp @@ -3,15 +3,15 @@ #include #include -ContentPrinter::ContentPrinter(Zone* zone) +ContentPrinter::ContentPrinter(const Zone& zone) + : m_zone(zone) { - m_zone = zone; } void ContentPrinter::PrintContent() const { - const auto* pools = m_zone->m_pools.get(); - std::cout << std::format("Zone '{}' ({})\n", m_zone->m_name, m_zone->m_game->GetShortName()); + const auto* pools = m_zone.m_pools.get(); + std::cout << std::format("Zone '{}' ({})\n", m_zone.m_name, m_zone.m_game->GetShortName()); std::cout << "Content:\n"; for (const auto& asset : *pools) diff --git a/src/Unlinker/ContentLister/ContentPrinter.h b/src/Unlinker/ContentLister/ContentPrinter.h index 9e13402a5..a58c587ee 100644 --- a/src/Unlinker/ContentLister/ContentPrinter.h +++ b/src/Unlinker/ContentLister/ContentPrinter.h @@ -4,10 +4,11 @@ class ContentPrinter { - Zone* m_zone; - public: - explicit ContentPrinter(Zone* zone); + explicit ContentPrinter(const Zone& zone); void PrintContent() const; + +private: + const Zone& m_zone; }; diff --git a/src/Unlinker/ContentLister/ZoneDefWriter.cpp b/src/Unlinker/ContentLister/ZoneDefWriter.cpp index 8ee121f11..51ec55397 100644 --- a/src/Unlinker/ContentLister/ZoneDefWriter.cpp +++ b/src/Unlinker/ContentLister/ZoneDefWriter.cpp @@ -1,17 +1,42 @@ #include "ZoneDefWriter.h" -void AbstractZoneDefWriter::WriteZoneDef(std::ostream& stream, const UnlinkerArgs* args, Zone* zone) const +#include "Game/IW3/ZoneDefWriterIW3.h" +#include "Game/IW4/ZoneDefWriterIW4.h" +#include "Game/IW5/ZoneDefWriterIW5.h" +#include "Game/T5/ZoneDefWriterT5.h" +#include "Game/T6/ZoneDefWriterT6.h" + +#include + +const IZoneDefWriter* IZoneDefWriter::GetZoneDefWriterForGame(GameId game) +{ + static const IZoneDefWriter* zoneDefWriters[static_cast(GameId::COUNT)]{ + new IW3::ZoneDefWriter(), + new IW4::ZoneDefWriter(), + new IW5::ZoneDefWriter(), + new T5::ZoneDefWriter(), + new T6::ZoneDefWriter(), + }; + + assert(static_cast(game) < static_cast(GameId::COUNT)); + const auto* result = zoneDefWriters[static_cast(game)]; + assert(result); + + return result; +} + +void AbstractZoneDefWriter::WriteZoneDef(std::ostream& stream, const UnlinkerArgs& args, const Zone& zone) const { ZoneDefinitionOutputStream out(stream); - out.WriteComment(zone->m_game->GetFullName()); - out.WriteMetaData(META_DATA_KEY_GAME, zone->m_game->GetShortName()); + out.WriteComment(zone.m_game->GetFullName()); + out.WriteMetaData(META_DATA_KEY_GAME, zone.m_game->GetShortName()); out.EmptyLine(); - if (args->m_use_gdt) + if (args.m_use_gdt) { out.WriteComment("Load asset gdt files"); - out.WriteMetaData(META_DATA_KEY_GDT, zone->m_name); + out.WriteMetaData(META_DATA_KEY_GDT, zone.m_name); out.EmptyLine(); } diff --git a/src/Unlinker/ContentLister/ZoneDefWriter.h b/src/Unlinker/ContentLister/ZoneDefWriter.h index 89370784b..4cb7f6a4e 100644 --- a/src/Unlinker/ContentLister/ZoneDefWriter.h +++ b/src/Unlinker/ContentLister/ZoneDefWriter.h @@ -2,7 +2,6 @@ #include "UnlinkerArgs.h" #include "Zone/Definition/ZoneDefinitionStream.h" -#include "Zone/Zone.h" class IZoneDefWriter { @@ -14,19 +13,20 @@ class IZoneDefWriter IZoneDefWriter& operator=(const IZoneDefWriter& other) = default; IZoneDefWriter& operator=(IZoneDefWriter&& other) noexcept = default; - virtual bool CanHandleZone(Zone* zone) const = 0; - virtual void WriteZoneDef(std::ostream& stream, const UnlinkerArgs* args, Zone* zone) const = 0; + virtual void WriteZoneDef(std::ostream& stream, const UnlinkerArgs& args, const Zone& zone) const = 0; + + static const IZoneDefWriter* GetZoneDefWriterForGame(GameId game); }; class AbstractZoneDefWriter : public IZoneDefWriter { protected: - static constexpr const char* META_DATA_KEY_GAME = "game"; - static constexpr const char* META_DATA_KEY_GDT = "gdt"; + static constexpr auto META_DATA_KEY_GAME = "game"; + static constexpr auto META_DATA_KEY_GDT = "gdt"; - virtual void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const = 0; - virtual void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const = 0; + virtual void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const = 0; + virtual void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const = 0; public: - void WriteZoneDef(std::ostream& stream, const UnlinkerArgs* args, Zone* zone) const override; + void WriteZoneDef(std::ostream& stream, const UnlinkerArgs& args, const Zone& zone) const override; }; diff --git a/src/Unlinker/Game/IW3/ZoneDefWriterIW3.cpp b/src/Unlinker/Game/IW3/ZoneDefWriterIW3.cpp index bde21a42d..3850cba77 100644 --- a/src/Unlinker/Game/IW3/ZoneDefWriterIW3.cpp +++ b/src/Unlinker/Game/IW3/ZoneDefWriterIW3.cpp @@ -1,22 +1,16 @@ #include "ZoneDefWriterIW3.h" #include "Game/IW3/GameAssetPoolIW3.h" -#include "Game/IW3/GameIW3.h" #include using namespace IW3; -bool ZoneDefWriter::CanHandleZone(Zone* zone) const -{ - return zone->m_game == &g_GameIW3; -} - -void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const {} +void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const {} -void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const +void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const { - const auto* pools = dynamic_cast(zone->m_pools.get()); + const auto* pools = dynamic_cast(zone.m_pools.get()); assert(pools); if (!pools) @@ -24,9 +18,7 @@ void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const Unlin // Localized strings are all collected in one string file. So only add this to the zone file. if (!pools->m_localize->m_asset_lookup.empty()) - { - stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone->m_name); - } + stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone.m_name); for (const auto& asset : *pools) { diff --git a/src/Unlinker/Game/IW3/ZoneDefWriterIW3.h b/src/Unlinker/Game/IW3/ZoneDefWriterIW3.h index 17aa55598..3f378d9da 100644 --- a/src/Unlinker/Game/IW3/ZoneDefWriterIW3.h +++ b/src/Unlinker/Game/IW3/ZoneDefWriterIW3.h @@ -7,10 +7,7 @@ namespace IW3 class ZoneDefWriter final : public AbstractZoneDefWriter { protected: - void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - - public: - bool CanHandleZone(Zone* zone) const override; + void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; + void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; }; } // namespace IW3 diff --git a/src/Unlinker/Game/IW4/ZoneDefWriterIW4.cpp b/src/Unlinker/Game/IW4/ZoneDefWriterIW4.cpp index f9de09bb5..7e100945c 100644 --- a/src/Unlinker/Game/IW4/ZoneDefWriterIW4.cpp +++ b/src/Unlinker/Game/IW4/ZoneDefWriterIW4.cpp @@ -1,22 +1,16 @@ #include "ZoneDefWriterIW4.h" #include "Game/IW4/GameAssetPoolIW4.h" -#include "Game/IW4/GameIW4.h" #include using namespace IW4; -bool ZoneDefWriter::CanHandleZone(Zone* zone) const -{ - return zone->m_game == &g_GameIW4; -} - -void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const {} +void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const {} -void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const +void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const { - const auto* pools = dynamic_cast(zone->m_pools.get()); + const auto* pools = dynamic_cast(zone.m_pools.get()); assert(pools); if (!pools) @@ -24,9 +18,7 @@ void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const Unlin // Localized strings are all collected in one string file. So only add this to the zone file. if (!pools->m_localize->m_asset_lookup.empty()) - { - stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone->m_name); - } + stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone.m_name); for (const auto& asset : *pools) { diff --git a/src/Unlinker/Game/IW4/ZoneDefWriterIW4.h b/src/Unlinker/Game/IW4/ZoneDefWriterIW4.h index 901800ba1..83bf16421 100644 --- a/src/Unlinker/Game/IW4/ZoneDefWriterIW4.h +++ b/src/Unlinker/Game/IW4/ZoneDefWriterIW4.h @@ -7,10 +7,7 @@ namespace IW4 class ZoneDefWriter final : public AbstractZoneDefWriter { protected: - void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - - public: - bool CanHandleZone(Zone* zone) const override; + void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; + void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; }; } // namespace IW4 diff --git a/src/Unlinker/Game/IW5/ZoneDefWriterIW5.cpp b/src/Unlinker/Game/IW5/ZoneDefWriterIW5.cpp index 1de38c5ee..ff54d6224 100644 --- a/src/Unlinker/Game/IW5/ZoneDefWriterIW5.cpp +++ b/src/Unlinker/Game/IW5/ZoneDefWriterIW5.cpp @@ -1,22 +1,16 @@ #include "ZoneDefWriterIW5.h" #include "Game/IW5/GameAssetPoolIW5.h" -#include "Game/IW5/GameIW5.h" #include using namespace IW5; -bool ZoneDefWriter::CanHandleZone(Zone* zone) const -{ - return zone->m_game == &g_GameIW5; -} - -void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const {} +void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const {} -void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const +void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const { - const auto* pools = dynamic_cast(zone->m_pools.get()); + const auto* pools = dynamic_cast(zone.m_pools.get()); assert(pools); if (!pools) @@ -24,9 +18,7 @@ void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const Unlin // Localized strings are all collected in one string file. So only add this to the zone file. if (!pools->m_localize->m_asset_lookup.empty()) - { - stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone->m_name); - } + stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone.m_name); for (const auto& asset : *pools) { diff --git a/src/Unlinker/Game/IW5/ZoneDefWriterIW5.h b/src/Unlinker/Game/IW5/ZoneDefWriterIW5.h index 92402fbff..fac80d00a 100644 --- a/src/Unlinker/Game/IW5/ZoneDefWriterIW5.h +++ b/src/Unlinker/Game/IW5/ZoneDefWriterIW5.h @@ -7,10 +7,7 @@ namespace IW5 class ZoneDefWriter final : public AbstractZoneDefWriter { protected: - void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - - public: - bool CanHandleZone(Zone* zone) const override; + void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; + void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; }; } // namespace IW5 diff --git a/src/Unlinker/Game/T5/ZoneDefWriterT5.cpp b/src/Unlinker/Game/T5/ZoneDefWriterT5.cpp index c875c6ba5..5fa7774ae 100644 --- a/src/Unlinker/Game/T5/ZoneDefWriterT5.cpp +++ b/src/Unlinker/Game/T5/ZoneDefWriterT5.cpp @@ -1,22 +1,16 @@ #include "ZoneDefWriterT5.h" #include "Game/T5/GameAssetPoolT5.h" -#include "Game/T5/GameT5.h" #include using namespace T5; -bool ZoneDefWriter::CanHandleZone(Zone* zone) const -{ - return zone->m_game == &g_GameT5; -} - -void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const {} +void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const {} -void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const +void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const { - const auto* pools = dynamic_cast(zone->m_pools.get()); + const auto* pools = dynamic_cast(zone.m_pools.get()); assert(pools); if (!pools) @@ -24,9 +18,7 @@ void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const Unlin // Localized strings are all collected in one string file. So only add this to the zone file. if (!pools->m_localize->m_asset_lookup.empty()) - { - stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone->m_name); - } + stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone.m_name); for (const auto& asset : *pools) { diff --git a/src/Unlinker/Game/T5/ZoneDefWriterT5.h b/src/Unlinker/Game/T5/ZoneDefWriterT5.h index a284dffd1..8b206436e 100644 --- a/src/Unlinker/Game/T5/ZoneDefWriterT5.h +++ b/src/Unlinker/Game/T5/ZoneDefWriterT5.h @@ -7,10 +7,7 @@ namespace T5 class ZoneDefWriter final : public AbstractZoneDefWriter { protected: - void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - - public: - bool CanHandleZone(Zone* zone) const override; + void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; + void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; }; } // namespace T5 diff --git a/src/Unlinker/Game/T6/ZoneDefWriterT6.cpp b/src/Unlinker/Game/T6/ZoneDefWriterT6.cpp index cada1f6a1..ad7b197db 100644 --- a/src/Unlinker/Game/T6/ZoneDefWriterT6.cpp +++ b/src/Unlinker/Game/T6/ZoneDefWriterT6.cpp @@ -2,7 +2,6 @@ #include "Game/T6/CommonT6.h" #include "Game/T6/GameAssetPoolT6.h" -#include "Game/T6/GameT6.h" #include #include @@ -10,7 +9,7 @@ using namespace T6; -namespace T6 +namespace { class KeyValuePairKnownKey { @@ -31,50 +30,43 @@ namespace T6 KeyValuePairKnownKey("initial_xmodels"), KeyValuePairKnownKey("initial_materials"), }; -} // namespace T6 -bool ZoneDefWriter::CanHandleZone(Zone* zone) const -{ - return zone->m_game == &g_GameT6; -} - -void ZoneDefWriter::WriteKeyValuePair(ZoneDefinitionOutputStream& stream, KeyValuePair* kvp) -{ - for (const auto& knownKey : KEY_VALUE_PAIR_KNOWN_KEYS) + void WriteKeyValuePair(ZoneDefinitionOutputStream& stream, const KeyValuePair& kvp) { - if (knownKey.m_hash == kvp->keyHash) + for (const auto& knownKey : KEY_VALUE_PAIR_KNOWN_KEYS) { - stream.WriteMetaData("level." + knownKey.m_key, kvp->value); - return; + if (knownKey.m_hash == kvp.keyHash) + { + stream.WriteMetaData("level." + knownKey.m_key, kvp.value); + return; + } } - } - std::ostringstream str; - str << "level.@" << std::setfill('0') << std::setw(sizeof(int) * 2) << std::hex << kvp->keyHash; - stream.WriteMetaData(str.str(), kvp->value); -} + std::ostringstream str; + str << "level.@" << std::setfill('0') << std::setw(sizeof(int) * 2) << std::hex << kvp.keyHash; + stream.WriteMetaData(str.str(), kvp.value); + } +} // namespace -void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const +void ZoneDefWriter::WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const { - auto* assetPoolT6 = dynamic_cast(zone->m_pools.get()); + const auto* assetPoolT6 = dynamic_cast(zone.m_pools.get()); if (assetPoolT6 && !assetPoolT6->m_key_value_pairs->m_asset_lookup.empty()) { for (const auto* kvpAsset : *assetPoolT6->m_key_value_pairs) { const auto* keyValuePairs = kvpAsset->Asset(); for (auto varIndex = 0; varIndex < keyValuePairs->numVariables; varIndex++) - { - WriteKeyValuePair(stream, &keyValuePairs->keyValuePairs[varIndex]); - } + WriteKeyValuePair(stream, keyValuePairs->keyValuePairs[varIndex]); } stream.EmptyLine(); } } -void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const +void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const { - const auto* pools = dynamic_cast(zone->m_pools.get()); + const auto* pools = dynamic_cast(zone.m_pools.get()); assert(pools); if (!pools) @@ -82,9 +74,7 @@ void ZoneDefWriter::WriteContent(ZoneDefinitionOutputStream& stream, const Unlin // Localized strings are all collected in one string file. So only add this to the zone file. if (!pools->m_localize->m_asset_lookup.empty()) - { - stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone->m_name); - } + stream.WriteEntry(*pools->GetAssetTypeName(ASSET_TYPE_LOCALIZE_ENTRY), zone.m_name); for (const auto& asset : *pools) { diff --git a/src/Unlinker/Game/T6/ZoneDefWriterT6.h b/src/Unlinker/Game/T6/ZoneDefWriterT6.h index 3eb04ec81..5af9672c1 100644 --- a/src/Unlinker/Game/T6/ZoneDefWriterT6.h +++ b/src/Unlinker/Game/T6/ZoneDefWriterT6.h @@ -1,19 +1,13 @@ #pragma once #include "ContentLister/ZoneDefWriter.h" -#include "Game/T6/T6.h" namespace T6 { class ZoneDefWriter final : public AbstractZoneDefWriter { - static void WriteKeyValuePair(ZoneDefinitionOutputStream& stream, KeyValuePair* kvp); - protected: - void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs* args, Zone* zone) const override; - - public: - bool CanHandleZone(Zone* zone) const override; + void WriteMetaData(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; + void WriteContent(ZoneDefinitionOutputStream& stream, const UnlinkerArgs& args, const Zone& zone) const override; }; } // namespace T6 diff --git a/src/Unlinker/Unlinker.cpp b/src/Unlinker/Unlinker.cpp index 8dae19b9d..e16d38258 100644 --- a/src/Unlinker/Unlinker.cpp +++ b/src/Unlinker/Unlinker.cpp @@ -2,11 +2,6 @@ #include "ContentLister/ContentPrinter.h" #include "ContentLister/ZoneDefWriter.h" -#include "Game/IW3/ZoneDefWriterIW3.h" -#include "Game/IW4/ZoneDefWriterIW4.h" -#include "Game/IW5/ZoneDefWriterIW5.h" -#include "Game/T5/ZoneDefWriterT5.h" -#include "Game/T6/ZoneDefWriterT6.h" #include "ObjContainer/IWD/IWD.h" #include "ObjLoading.h" #include "ObjWriting.h" @@ -25,14 +20,6 @@ namespace fs = std::filesystem; -const IZoneDefWriter* const ZONE_DEF_WRITERS[]{ - new IW3::ZoneDefWriter(), - new IW4::ZoneDefWriter(), - new IW5::ZoneDefWriter(), - new T5::ZoneDefWriter(), - new T6::ZoneDefWriter(), -}; - class Unlinker::Impl { public: @@ -166,51 +153,41 @@ class Unlinker::Impl return true; } - bool WriteZoneDefinitionFile(Zone* zone, const fs::path& zoneDefinitionFileFolder) const + bool WriteZoneDefinitionFile(const Zone& zone, const fs::path& zoneDefinitionFileFolder) const { auto zoneDefinitionFilePath(zoneDefinitionFileFolder); - zoneDefinitionFilePath.append(zone->m_name); + zoneDefinitionFilePath.append(zone.m_name); zoneDefinitionFilePath.replace_extension(".zone"); std::ofstream zoneDefinitionFile(zoneDefinitionFilePath, std::fstream::out | std::fstream::binary); if (!zoneDefinitionFile.is_open()) { - std::cerr << std::format("Failed to open file for zone definition file of zone \"{}\".\n", zone->m_name); + std::cerr << std::format("Failed to open file for zone definition file of zone \"{}\".\n", zone.m_name); return false; } - auto result = false; - for (const auto* zoneDefWriter : ZONE_DEF_WRITERS) - { - if (zoneDefWriter->CanHandleZone(zone)) - { - zoneDefWriter->WriteZoneDef(zoneDefinitionFile, &m_args, zone); - result = true; - break; - } - } - - if (!result) - std::cerr << std::format("Failed to find writer for zone definition file of zone \"{}\".\n", zone->m_name); + const auto* zoneDefWriter = IZoneDefWriter::GetZoneDefWriterForGame(zone.m_game->GetId()); + zoneDefWriter->WriteZoneDef(zoneDefinitionFile, m_args, zone); zoneDefinitionFile.close(); - return result; + + return true; } - static bool OpenGdtFile(Zone* zone, const fs::path& outputFolder, std::ofstream& stream) + static bool OpenGdtFile(const Zone& zone, const fs::path& outputFolder, std::ofstream& stream) { auto gdtFilePath(outputFolder); gdtFilePath.append("source_data"); fs::create_directories(gdtFilePath); - gdtFilePath.append(zone->m_name); + gdtFilePath.append(zone.m_name); gdtFilePath.replace_extension(".gdt"); stream = std::ofstream(gdtFilePath, std::fstream::out | std::fstream::binary); if (!stream.is_open()) { - std::cerr << std::format("Failed to open file for zone definition file of zone \"{}\".\n", zone->m_name); + std::cerr << std::format("Failed to open file for zone definition file of zone \"{}\".\n", zone.m_name); return false; } @@ -274,7 +251,7 @@ class Unlinker::Impl * \param zone The zone to handle. * \return \c true if handling the zone was successful, otherwise \c false. */ - bool HandleZone(ISearchPath& searchPath, Zone* zone) const + bool HandleZone(ISearchPath& searchPath, Zone& zone) const { if (m_args.m_task == UnlinkerArgs::ProcessingTask::LIST) { @@ -295,7 +272,7 @@ class Unlinker::Impl std::ofstream gdtStream; AssetDumpingContext context; - context.m_zone = zone; + context.m_zone = &zone; context.m_base_path = outputFolderPath; context.m_obj_search_path = &searchPath; @@ -305,7 +282,7 @@ class Unlinker::Impl return false; auto gdt = std::make_unique(gdtStream); gdt->BeginStream(); - gdt->WriteVersion(GdtVersion(zone->m_game->GetShortName(), 1)); + gdt->WriteVersion(GdtVersion(zone.m_game->GetShortName(), 1)); context.m_gdt = std::move(gdt); } @@ -409,7 +386,7 @@ class Unlinker::Impl if (ShouldLoadObj()) ObjLoading::LoadReferencedContainersForZone(searchPathsForZone, *zone); - if (!HandleZone(searchPathsForZone, zone.get())) + if (!HandleZone(searchPathsForZone, *zone)) return false; if (ShouldLoadObj()) diff --git a/src/Unlinker/UnlinkerArgs.cpp b/src/Unlinker/UnlinkerArgs.cpp index ddefb61e0..aa2102e62 100644 --- a/src/Unlinker/UnlinkerArgs.cpp +++ b/src/Unlinker/UnlinkerArgs.cpp @@ -377,7 +377,7 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldCont return true; } -std::string UnlinkerArgs::GetOutputFolderPathForZone(const Zone* zone) const +std::string UnlinkerArgs::GetOutputFolderPathForZone(const Zone& zone) const { - return std::regex_replace(m_output_folder, m_zone_pattern, zone->m_name); + return std::regex_replace(m_output_folder, m_zone_pattern, zone.m_name); } diff --git a/src/Unlinker/UnlinkerArgs.h b/src/Unlinker/UnlinkerArgs.h index 9e2867e2d..fea432b8d 100644 --- a/src/Unlinker/UnlinkerArgs.h +++ b/src/Unlinker/UnlinkerArgs.h @@ -68,5 +68,5 @@ class UnlinkerArgs * \param zone The zone to resolve the path input for. * \return An output path for the zone based on the user input. */ - std::string GetOutputFolderPathForZone(const Zone* zone) const; + std::string GetOutputFolderPathForZone(const Zone& zone) const; }; From 4c9a84777f762b120d1b05d46fd4f0c6598f3fb9 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 19 Oct 2024 12:07:12 +0200 Subject: [PATCH 2/4] chore: use IObjLoader directly instead of indirection of ObjLoading --- src/Linker/Game/IW3/ZoneCreatorIW3.cpp | 6 ++- src/Linker/Game/IW4/ZoneCreatorIW4.cpp | 6 ++- src/Linker/Game/IW5/ZoneCreatorIW5.cpp | 6 ++- src/Linker/Game/T5/ZoneCreatorT5.cpp | 6 ++- src/Linker/Game/T6/ZoneCreatorT6.cpp | 6 ++- src/Linker/ZoneCreation/ZoneCreator.cpp | 1 + src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp | 5 -- src/ObjLoading/Game/IW3/ObjLoaderIW3.h | 2 - src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp | 5 -- src/ObjLoading/Game/IW4/ObjLoaderIW4.h | 2 - src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp | 5 -- src/ObjLoading/Game/IW5/ObjLoaderIW5.h | 2 - src/ObjLoading/Game/T5/ObjLoaderT5.cpp | 5 -- src/ObjLoading/Game/T5/ObjLoaderT5.h | 2 - src/ObjLoading/Game/T6/ObjLoaderT6.cpp | 5 -- src/ObjLoading/Game/T6/ObjLoaderT6.h | 2 - src/ObjLoading/IObjLoader.cpp | 27 +++++++++++ src/ObjLoading/IObjLoader.h | 9 +--- src/ObjLoading/ObjLoading.cpp | 62 ------------------------ src/ObjLoading/ObjLoading.h | 18 ------- src/Unlinker/Unlinker.cpp | 16 ++++-- 21 files changed, 62 insertions(+), 136 deletions(-) create mode 100644 src/ObjLoading/IObjLoader.cpp diff --git a/src/Linker/Game/IW3/ZoneCreatorIW3.cpp b/src/Linker/Game/IW3/ZoneCreatorIW3.cpp index 280d32b5d..a512f1eb9 100644 --- a/src/Linker/Game/IW3/ZoneCreatorIW3.cpp +++ b/src/Linker/Game/IW3/ZoneCreatorIW3.cpp @@ -3,6 +3,7 @@ #include "AssetLoading/AssetLoadingContext.h" #include "Game/IW3/GameAssetPoolIW3.h" #include "Game/IW3/GameIW3.h" +#include "IObjLoader.h" #include "ObjLoading.h" #include "Utils/StringUtils.h" @@ -53,13 +54,14 @@ std::unique_ptr ZoneCreator::CreateZoneForDefinition(ZoneCreationContext& const auto assetLoadingContext = std::make_unique(*zone, *context.m_asset_search_path, CreateGdtList(context)); ApplyIgnoredAssets(context, *assetLoadingContext); + const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::IW3); 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; } - ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); + objLoader->FinalizeAssetsForZone(*assetLoadingContext); return zone; } diff --git a/src/Linker/Game/IW4/ZoneCreatorIW4.cpp b/src/Linker/Game/IW4/ZoneCreatorIW4.cpp index 937b96804..34c01b14b 100644 --- a/src/Linker/Game/IW4/ZoneCreatorIW4.cpp +++ b/src/Linker/Game/IW4/ZoneCreatorIW4.cpp @@ -2,6 +2,7 @@ #include "Game/IW4/GameAssetPoolIW4.h" #include "Game/IW4/GameIW4.h" +#include "IObjLoader.h" #include "ObjLoading.h" #include "Utils/StringUtils.h" @@ -52,13 +53,14 @@ std::unique_ptr ZoneCreator::CreateZoneForDefinition(ZoneCreationContext& const auto assetLoadingContext = std::make_unique(*zone, *context.m_asset_search_path, CreateGdtList(context)); ApplyIgnoredAssets(context, *assetLoadingContext); + const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::IW4); 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; } - ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); + objLoader->FinalizeAssetsForZone(*assetLoadingContext); return zone; } diff --git a/src/Linker/Game/IW5/ZoneCreatorIW5.cpp b/src/Linker/Game/IW5/ZoneCreatorIW5.cpp index 6f9b913c3..83023066e 100644 --- a/src/Linker/Game/IW5/ZoneCreatorIW5.cpp +++ b/src/Linker/Game/IW5/ZoneCreatorIW5.cpp @@ -2,6 +2,7 @@ #include "Game/IW5/GameAssetPoolIW5.h" #include "Game/IW5/GameIW5.h" +#include "IObjLoader.h" #include "ObjLoading.h" #include "Utils/StringUtils.h" @@ -52,13 +53,14 @@ std::unique_ptr ZoneCreator::CreateZoneForDefinition(ZoneCreationContext& const auto assetLoadingContext = std::make_unique(*zone, *context.m_asset_search_path, CreateGdtList(context)); ApplyIgnoredAssets(context, *assetLoadingContext); + const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::IW5); 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; } - ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); + objLoader->FinalizeAssetsForZone(*assetLoadingContext); return zone; } diff --git a/src/Linker/Game/T5/ZoneCreatorT5.cpp b/src/Linker/Game/T5/ZoneCreatorT5.cpp index d14e35100..583302677 100644 --- a/src/Linker/Game/T5/ZoneCreatorT5.cpp +++ b/src/Linker/Game/T5/ZoneCreatorT5.cpp @@ -3,6 +3,7 @@ #include "AssetLoading/AssetLoadingContext.h" #include "Game/T5/GameAssetPoolT5.h" #include "Game/T5/GameT5.h" +#include "IObjLoader.h" #include "ObjLoading.h" #include "Utils/StringUtils.h" @@ -53,13 +54,14 @@ std::unique_ptr ZoneCreator::CreateZoneForDefinition(ZoneCreationContext& const auto assetLoadingContext = std::make_unique(*zone, *context.m_asset_search_path, CreateGdtList(context)); ApplyIgnoredAssets(context, *assetLoadingContext); + const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::T5); 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; } - ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); + objLoader->FinalizeAssetsForZone(*assetLoadingContext); return zone; } diff --git a/src/Linker/Game/T6/ZoneCreatorT6.cpp b/src/Linker/Game/T6/ZoneCreatorT6.cpp index c50b53fe7..388ab9628 100644 --- a/src/Linker/Game/T6/ZoneCreatorT6.cpp +++ b/src/Linker/Game/T6/ZoneCreatorT6.cpp @@ -4,6 +4,7 @@ #include "Game/T6/GameAssetPoolT6.h" #include "Game/T6/GameT6.h" #include "Game/T6/T6.h" +#include "IObjLoader.h" #include "ObjLoading.h" #include "Utils/StringUtils.h" @@ -106,13 +107,14 @@ std::unique_ptr ZoneCreator::CreateZoneForDefinition(ZoneCreationContext& HandleMetadata(zone.get(), context); + const auto* objLoader = IObjLoader::GetObjLoaderForGame(GameId::T6); 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; } - ObjLoading::FinalizeAssetsForZone(*assetLoadingContext); + objLoader->FinalizeAssetsForZone(*assetLoadingContext); return zone; } diff --git a/src/Linker/ZoneCreation/ZoneCreator.cpp b/src/Linker/ZoneCreation/ZoneCreator.cpp index 7d8b49582..9e1f10043 100644 --- a/src/Linker/ZoneCreation/ZoneCreator.cpp +++ b/src/Linker/ZoneCreation/ZoneCreator.cpp @@ -1,5 +1,6 @@ #include "ZoneCreator.h" +#include "AssetLoading/AssetLoadingContext.h" #include "Game/IW3/ZoneCreatorIW3.h" #include "Game/IW4/ZoneCreatorIW4.h" #include "Game/IW5/ZoneCreatorIW5.h" diff --git a/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp b/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp index 9faea56b3..4ea79be1d 100644 --- a/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp +++ b/src/ObjLoading/Game/IW3/ObjLoaderIW3.cpp @@ -48,11 +48,6 @@ ObjLoader::ObjLoader() #undef REGISTER_ASSET_LOADER } -bool ObjLoader::SupportsZone(const Zone& zone) const -{ - return zone.m_game == &g_GameIW3; -} - 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; diff --git a/src/ObjLoading/Game/IW3/ObjLoaderIW3.h b/src/ObjLoading/Game/IW3/ObjLoaderIW3.h index ec733cbe9..f027ecb49 100644 --- a/src/ObjLoading/Game/IW3/ObjLoaderIW3.h +++ b/src/ObjLoading/Game/IW3/ObjLoaderIW3.h @@ -19,8 +19,6 @@ namespace IW3 public: ObjLoader(); - [[nodiscard]] bool SupportsZone(const Zone& zone) const override; - void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override; diff --git a/src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp b/src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp index c055156e7..0960b7375 100644 --- a/src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp +++ b/src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp @@ -92,11 +92,6 @@ ObjLoader::ObjLoader() #undef REGISTER_ASSET_LOADER } -bool ObjLoader::SupportsZone(const Zone& zone) const -{ - return zone.m_game == &g_GameIW4; -} - 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; diff --git a/src/ObjLoading/Game/IW4/ObjLoaderIW4.h b/src/ObjLoading/Game/IW4/ObjLoaderIW4.h index e19a58054..ab4e10b39 100644 --- a/src/ObjLoading/Game/IW4/ObjLoaderIW4.h +++ b/src/ObjLoading/Game/IW4/ObjLoaderIW4.h @@ -14,8 +14,6 @@ namespace IW4 public: ObjLoader(); - [[nodiscard]] bool SupportsZone(const Zone& zone) const override; - void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override; diff --git a/src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp b/src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp index 6dbcec543..de35f87ae 100644 --- a/src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp +++ b/src/ObjLoading/Game/IW5/ObjLoaderIW5.cpp @@ -99,11 +99,6 @@ ObjLoader::ObjLoader() #undef REGISTER_ASSET_LOADER } -bool ObjLoader::SupportsZone(const Zone& zone) const -{ - return zone.m_game == &g_GameIW5; -} - 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; diff --git a/src/ObjLoading/Game/IW5/ObjLoaderIW5.h b/src/ObjLoading/Game/IW5/ObjLoaderIW5.h index e60ee44d4..2b2590326 100644 --- a/src/ObjLoading/Game/IW5/ObjLoaderIW5.h +++ b/src/ObjLoading/Game/IW5/ObjLoaderIW5.h @@ -14,8 +14,6 @@ namespace IW5 public: ObjLoader(); - [[nodiscard]] bool SupportsZone(const Zone& zone) const override; - void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override; diff --git a/src/ObjLoading/Game/T5/ObjLoaderT5.cpp b/src/ObjLoading/Game/T5/ObjLoaderT5.cpp index dd8f8522d..3ec02cf42 100644 --- a/src/ObjLoading/Game/T5/ObjLoaderT5.cpp +++ b/src/ObjLoading/Game/T5/ObjLoaderT5.cpp @@ -56,11 +56,6 @@ ObjLoader::ObjLoader() #undef REGISTER_ASSET_LOADER } -bool ObjLoader::SupportsZone(const Zone& zone) const -{ - return zone.m_game == &g_GameT5; -} - 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; diff --git a/src/ObjLoading/Game/T5/ObjLoaderT5.h b/src/ObjLoading/Game/T5/ObjLoaderT5.h index f8fadd5f2..4ede43c40 100644 --- a/src/ObjLoading/Game/T5/ObjLoaderT5.h +++ b/src/ObjLoading/Game/T5/ObjLoaderT5.h @@ -14,8 +14,6 @@ namespace T5 public: ObjLoader(); - bool SupportsZone(const Zone& zone) const override; - void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override; diff --git a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp index 846c34d8e..e272d4548 100644 --- a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp +++ b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp @@ -125,11 +125,6 @@ namespace T6 #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) { SoundAssetBankChecksum checksum{}; diff --git a/src/ObjLoading/Game/T6/ObjLoaderT6.h b/src/ObjLoading/Game/T6/ObjLoaderT6.h index e6cb0ee98..3e9aa7412 100644 --- a/src/ObjLoading/Game/T6/ObjLoaderT6.h +++ b/src/ObjLoading/Game/T6/ObjLoaderT6.h @@ -19,8 +19,6 @@ namespace T6 public: ObjLoader(); - [[nodiscard]] bool SupportsZone(const Zone& zone) const override; - void LoadReferencedContainersForZone(ISearchPath& searchPath, Zone& zone) const override; void UnloadContainersOfZone(Zone& zone) const override; diff --git a/src/ObjLoading/IObjLoader.cpp b/src/ObjLoading/IObjLoader.cpp new file mode 100644 index 000000000..de8411381 --- /dev/null +++ b/src/ObjLoading/IObjLoader.cpp @@ -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 + +const IObjLoader* IObjLoader::GetObjLoaderForGame(GameId game) +{ + static const IObjLoader* zoneCreators[static_cast(GameId::COUNT)]{ + new IW3::ObjLoader(), + new IW4::ObjLoader(), + new IW5::ObjLoader(), + new T5::ObjLoader(), + new T6::ObjLoader(), + }; + static_assert(std::extent_v == static_cast(GameId::COUNT)); + + assert(static_cast(game) < static_cast(GameId::COUNT)); + const auto* result = zoneCreators[static_cast(game)]; + assert(result); + + return result; +} diff --git a/src/ObjLoading/IObjLoader.h b/src/ObjLoading/IObjLoader.h index b494fe332..9302b9436 100644 --- a/src/ObjLoading/IObjLoader.h +++ b/src/ObjLoading/IObjLoader.h @@ -14,13 +14,6 @@ class IObjLoader IObjLoader& operator=(const IObjLoader& other) = 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. * \param searchPath The search path object to use to find the referenced containers. @@ -36,4 +29,6 @@ class IObjLoader virtual bool LoadAssetForZone(AssetLoadingContext& context, asset_type_t assetType, const std::string& assetName) const = 0; virtual void FinalizeAssetsForZone(AssetLoadingContext& context) const = 0; + + static const IObjLoader* GetObjLoaderForGame(GameId game); }; diff --git a/src/ObjLoading/ObjLoading.cpp b/src/ObjLoading/ObjLoading.cpp index 2a0524e7f..b50fac753 100644 --- a/src/ObjLoading/ObjLoading.cpp +++ b/src/ObjLoading/ObjLoading.cpp @@ -1,10 +1,5 @@ #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 "ObjContainer/IWD/IWD.h" #include "SearchPath/SearchPaths.h" @@ -14,38 +9,6 @@ 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) { searchPath.Find(SearchPathSearchOptions().IncludeSubdirectories(false).FilterExtensions("iwd"), @@ -81,28 +44,3 @@ SearchPaths ObjLoading::GetIWDSearchPaths() 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; - } - } -} diff --git a/src/ObjLoading/ObjLoading.h b/src/ObjLoading/ObjLoading.h index c0baa1ad5..372ef610d 100644 --- a/src/ObjLoading/ObjLoading.h +++ b/src/ObjLoading/ObjLoading.h @@ -1,9 +1,7 @@ #pragma once -#include "AssetLoading/AssetLoadingContext.h" #include "SearchPath/ISearchPath.h" #include "SearchPath/SearchPaths.h" -#include "Zone/Zone.h" class ObjLoading { @@ -16,19 +14,6 @@ class ObjLoading bool MenuNoOptimization = false; } 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. * \param searchPath The search path that contains IWDs to be loaded. @@ -46,7 +31,4 @@ class ObjLoading * \return A \c SearchPaths object containing all IWDs that are currently loaded. */ static SearchPaths GetIWDSearchPaths(); - - static bool LoadAssetForZone(AssetLoadingContext& context, asset_type_t assetType, const std::string& assetName); - static void FinalizeAssetsForZone(AssetLoadingContext& context); }; diff --git a/src/Unlinker/Unlinker.cpp b/src/Unlinker/Unlinker.cpp index e16d38258..c2ff656b2 100644 --- a/src/Unlinker/Unlinker.cpp +++ b/src/Unlinker/Unlinker.cpp @@ -2,6 +2,7 @@ #include "ContentLister/ContentPrinter.h" #include "ContentLister/ZoneDefWriter.h" +#include "IObjLoader.h" #include "ObjContainer/IWD/IWD.h" #include "ObjLoading.h" #include "ObjWriting.h" @@ -325,7 +326,10 @@ class Unlinker::Impl std::cout << std::format("Loaded zone \"{}\"\n", zone->m_name); 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)); } @@ -343,7 +347,10 @@ class Unlinker::Impl const auto zoneName = loadedZone->m_name; if (ShouldLoadObj()) - ObjLoading::UnloadContainersOfZone(*loadedZone); + { + const auto* objLoader = IObjLoader::GetObjLoaderForGame(loadedZone->m_game->GetId()); + objLoader->UnloadContainersOfZone(*loadedZone); + } loadedZone.reset(); @@ -383,14 +390,15 @@ class Unlinker::Impl if (m_args.m_verbose) std::cout << std::format("Loaded zone \"{}\"\n", zoneName); + const auto* objLoader = IObjLoader::GetObjLoaderForGame(zone->m_game->GetId()); if (ShouldLoadObj()) - ObjLoading::LoadReferencedContainersForZone(searchPathsForZone, *zone); + objLoader->LoadReferencedContainersForZone(searchPathsForZone, *zone); if (!HandleZone(searchPathsForZone, *zone)) return false; if (ShouldLoadObj()) - ObjLoading::UnloadContainersOfZone(*zone); + objLoader->UnloadContainersOfZone(*zone); zone.reset(); if (m_args.m_verbose) From bb845f68e9155839f9d2578ae33ca1fa19157c6f Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 19 Oct 2024 12:41:20 +0200 Subject: [PATCH 3/4] chore: use IObjWriter directly instead of indirection of ObjWriting --- src/ObjWriting/Dumping/IZoneDumper.h | 17 ---------- .../{ZoneDumperIW3.cpp => ObjWriterIW3.cpp} | 10 ++---- src/ObjWriting/Game/IW3/ObjWriterIW3.h | 11 ++++++ src/ObjWriting/Game/IW3/ZoneDumperIW3.h | 12 ------- .../{ZoneDumperIW4.cpp => ObjWriterIW4.cpp} | 10 ++---- src/ObjWriting/Game/IW4/ObjWriterIW4.h | 11 ++++++ src/ObjWriting/Game/IW4/ZoneDumperIW4.h | 12 ------- .../{ZoneDumperIW5.cpp => ObjWriterIW5.cpp} | 10 ++---- src/ObjWriting/Game/IW5/ObjWriterIW5.h | 11 ++++++ src/ObjWriting/Game/IW5/ZoneDumperIW5.h | 12 ------- .../T5/{ZoneDumperT5.cpp => ObjWriterT5.cpp} | 10 ++---- src/ObjWriting/Game/T5/ObjWriterT5.h | 11 ++++++ src/ObjWriting/Game/T5/ZoneDumperT5.h | 12 ------- .../T6/{ZoneDumperT6.cpp => ObjWriterT6.cpp} | 10 ++---- src/ObjWriting/Game/T6/ObjWriterT6.h | 11 ++++++ src/ObjWriting/Game/T6/ZoneDumperT6.h | 12 ------- src/ObjWriting/IObjWriter.cpp | 27 +++++++++++++++ src/ObjWriting/IObjWriter.h | 18 ++++++++++ src/ObjWriting/ObjWriting.cpp | 34 ------------------- src/ObjWriting/ObjWriting.h | 1 - src/Unlinker/Unlinker.cpp | 12 ++++++- 21 files changed, 121 insertions(+), 153 deletions(-) delete mode 100644 src/ObjWriting/Dumping/IZoneDumper.h rename src/ObjWriting/Game/IW3/{ZoneDumperIW3.cpp => ObjWriterIW3.cpp} (93%) create mode 100644 src/ObjWriting/Game/IW3/ObjWriterIW3.h delete mode 100644 src/ObjWriting/Game/IW3/ZoneDumperIW3.h rename src/ObjWriting/Game/IW4/{ZoneDumperIW4.cpp => ObjWriterIW4.cpp} (95%) create mode 100644 src/ObjWriting/Game/IW4/ObjWriterIW4.h delete mode 100644 src/ObjWriting/Game/IW4/ZoneDumperIW4.h rename src/ObjWriting/Game/IW5/{ZoneDumperIW5.cpp => ObjWriterIW5.cpp} (95%) create mode 100644 src/ObjWriting/Game/IW5/ObjWriterIW5.h delete mode 100644 src/ObjWriting/Game/IW5/ZoneDumperIW5.h rename src/ObjWriting/Game/T5/{ZoneDumperT5.cpp => ObjWriterT5.cpp} (94%) create mode 100644 src/ObjWriting/Game/T5/ObjWriterT5.h delete mode 100644 src/ObjWriting/Game/T5/ZoneDumperT5.h rename src/ObjWriting/Game/T6/{ZoneDumperT6.cpp => ObjWriterT6.cpp} (96%) create mode 100644 src/ObjWriting/Game/T6/ObjWriterT6.h delete mode 100644 src/ObjWriting/Game/T6/ZoneDumperT6.h create mode 100644 src/ObjWriting/IObjWriter.cpp create mode 100644 src/ObjWriting/IObjWriter.h diff --git a/src/ObjWriting/Dumping/IZoneDumper.h b/src/ObjWriting/Dumping/IZoneDumper.h deleted file mode 100644 index 8b3a30a62..000000000 --- a/src/ObjWriting/Dumping/IZoneDumper.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "AssetDumpingContext.h" - -class IZoneDumper -{ -public: - IZoneDumper() = default; - virtual ~IZoneDumper() = default; - IZoneDumper(const IZoneDumper& other) = default; - IZoneDumper(IZoneDumper&& other) noexcept = default; - IZoneDumper& operator=(const IZoneDumper& other) = default; - IZoneDumper& operator=(IZoneDumper&& other) noexcept = default; - - virtual bool CanHandleZone(AssetDumpingContext& assetDumpingContext) const = 0; - virtual bool DumpZone(AssetDumpingContext& assetDumpingContext) const = 0; -}; diff --git a/src/ObjWriting/Game/IW3/ZoneDumperIW3.cpp b/src/ObjWriting/Game/IW3/ObjWriterIW3.cpp similarity index 93% rename from src/ObjWriting/Game/IW3/ZoneDumperIW3.cpp rename to src/ObjWriting/Game/IW3/ObjWriterIW3.cpp index 9424d8cef..997f5e72c 100644 --- a/src/ObjWriting/Game/IW3/ZoneDumperIW3.cpp +++ b/src/ObjWriting/Game/IW3/ObjWriterIW3.cpp @@ -1,4 +1,4 @@ -#include "ZoneDumperIW3.h" +#include "ObjWriterIW3.h" #include "AssetDumpers/AssetDumperGfxImage.h" #include "AssetDumpers/AssetDumperLoadedSound.h" @@ -10,17 +10,11 @@ #include "AssetDumpers/AssetDumperWeapon.h" #include "AssetDumpers/AssetDumperXModel.h" #include "Game/IW3/GameAssetPoolIW3.h" -#include "Game/IW3/GameIW3.h" #include "ObjWriting.h" using namespace IW3; -bool ZoneDumper::CanHandleZone(AssetDumpingContext& context) const -{ - return context.m_zone->m_game == &g_GameIW3; -} - -bool ZoneDumper::DumpZone(AssetDumpingContext& context) const +bool ObjWriter::DumpZone(AssetDumpingContext& context) const { #define DUMP_ASSET_POOL(dumperType, poolName, assetType) \ if (assetPools->poolName && ObjWriting::ShouldHandleAssetType(assetType)) \ diff --git a/src/ObjWriting/Game/IW3/ObjWriterIW3.h b/src/ObjWriting/Game/IW3/ObjWriterIW3.h new file mode 100644 index 000000000..6e6bdbe4e --- /dev/null +++ b/src/ObjWriting/Game/IW3/ObjWriterIW3.h @@ -0,0 +1,11 @@ +#pragma once +#include "IObjWriter.h" + +namespace IW3 +{ + class ObjWriter final : public IObjWriter + { + public: + bool DumpZone(AssetDumpingContext& context) const override; + }; +} // namespace IW3 diff --git a/src/ObjWriting/Game/IW3/ZoneDumperIW3.h b/src/ObjWriting/Game/IW3/ZoneDumperIW3.h deleted file mode 100644 index 4e0aa6f3c..000000000 --- a/src/ObjWriting/Game/IW3/ZoneDumperIW3.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "Dumping/IZoneDumper.h" - -namespace IW3 -{ - class ZoneDumper final : public IZoneDumper - { - public: - bool CanHandleZone(AssetDumpingContext& context) const override; - bool DumpZone(AssetDumpingContext& context) const override; - }; -} // namespace IW3 diff --git a/src/ObjWriting/Game/IW4/ZoneDumperIW4.cpp b/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp similarity index 95% rename from src/ObjWriting/Game/IW4/ZoneDumperIW4.cpp rename to src/ObjWriting/Game/IW4/ObjWriterIW4.cpp index 94aa8787f..9e996b970 100644 --- a/src/ObjWriting/Game/IW4/ZoneDumperIW4.cpp +++ b/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp @@ -1,4 +1,4 @@ -#include "ZoneDumperIW4.h" +#include "ObjWriterIW4.h" #include "AssetDumpers/AssetDumperAddonMapEnts.h" #include "AssetDumpers/AssetDumperGfxImage.h" @@ -23,17 +23,11 @@ #include "AssetDumpers/AssetDumperWeapon.h" #include "AssetDumpers/AssetDumperXModel.h" #include "Game/IW4/GameAssetPoolIW4.h" -#include "Game/IW4/GameIW4.h" #include "ObjWriting.h" using namespace IW4; -bool ZoneDumper::CanHandleZone(AssetDumpingContext& context) const -{ - return context.m_zone->m_game == &g_GameIW4; -} - -bool ZoneDumper::DumpZone(AssetDumpingContext& context) const +bool ObjWriter::DumpZone(AssetDumpingContext& context) const { #define DUMP_ASSET_POOL(dumperType, poolName, assetType) \ if (assetPools->poolName && ObjWriting::ShouldHandleAssetType(assetType)) \ diff --git a/src/ObjWriting/Game/IW4/ObjWriterIW4.h b/src/ObjWriting/Game/IW4/ObjWriterIW4.h new file mode 100644 index 000000000..4519fc442 --- /dev/null +++ b/src/ObjWriting/Game/IW4/ObjWriterIW4.h @@ -0,0 +1,11 @@ +#pragma once +#include "IObjWriter.h" + +namespace IW4 +{ + class ObjWriter final : public IObjWriter + { + public: + bool DumpZone(AssetDumpingContext& context) const override; + }; +} // namespace IW4 diff --git a/src/ObjWriting/Game/IW4/ZoneDumperIW4.h b/src/ObjWriting/Game/IW4/ZoneDumperIW4.h deleted file mode 100644 index bfc934d74..000000000 --- a/src/ObjWriting/Game/IW4/ZoneDumperIW4.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "Dumping/IZoneDumper.h" - -namespace IW4 -{ - class ZoneDumper final : public IZoneDumper - { - public: - bool CanHandleZone(AssetDumpingContext& context) const override; - bool DumpZone(AssetDumpingContext& context) const override; - }; -} // namespace IW4 diff --git a/src/ObjWriting/Game/IW5/ZoneDumperIW5.cpp b/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp similarity index 95% rename from src/ObjWriting/Game/IW5/ZoneDumperIW5.cpp rename to src/ObjWriting/Game/IW5/ObjWriterIW5.cpp index f102023cc..e618dbccb 100644 --- a/src/ObjWriting/Game/IW5/ZoneDumperIW5.cpp +++ b/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp @@ -1,4 +1,4 @@ -#include "ZoneDumperIW5.h" +#include "ObjWriterIW5.h" #include "AssetDumpers/AssetDumperAddonMapEnts.h" #include "AssetDumpers/AssetDumperGfxImage.h" @@ -15,17 +15,11 @@ #include "AssetDumpers/AssetDumperWeaponAttachment.h" #include "AssetDumpers/AssetDumperXModel.h" #include "Game/IW5/GameAssetPoolIW5.h" -#include "Game/IW5/GameIW5.h" #include "ObjWriting.h" using namespace IW5; -bool ZoneDumper::CanHandleZone(AssetDumpingContext& context) const -{ - return context.m_zone->m_game == &g_GameIW5; -} - -bool ZoneDumper::DumpZone(AssetDumpingContext& context) const +bool ObjWriter::DumpZone(AssetDumpingContext& context) const { #define DUMP_ASSET_POOL(dumperType, poolName, assetType) \ if (assetPools->poolName && ObjWriting::ShouldHandleAssetType(assetType)) \ diff --git a/src/ObjWriting/Game/IW5/ObjWriterIW5.h b/src/ObjWriting/Game/IW5/ObjWriterIW5.h new file mode 100644 index 000000000..dd38ba5da --- /dev/null +++ b/src/ObjWriting/Game/IW5/ObjWriterIW5.h @@ -0,0 +1,11 @@ +#pragma once +#include "IObjWriter.h" + +namespace IW5 +{ + class ObjWriter final : public IObjWriter + { + public: + bool DumpZone(AssetDumpingContext& context) const override; + }; +} // namespace IW5 diff --git a/src/ObjWriting/Game/IW5/ZoneDumperIW5.h b/src/ObjWriting/Game/IW5/ZoneDumperIW5.h deleted file mode 100644 index e471fe115..000000000 --- a/src/ObjWriting/Game/IW5/ZoneDumperIW5.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "Dumping/IZoneDumper.h" - -namespace IW5 -{ - class ZoneDumper final : public IZoneDumper - { - public: - bool CanHandleZone(AssetDumpingContext& context) const override; - bool DumpZone(AssetDumpingContext& context) const override; - }; -} // namespace IW5 diff --git a/src/ObjWriting/Game/T5/ZoneDumperT5.cpp b/src/ObjWriting/Game/T5/ObjWriterT5.cpp similarity index 94% rename from src/ObjWriting/Game/T5/ZoneDumperT5.cpp rename to src/ObjWriting/Game/T5/ObjWriterT5.cpp index 891fb7c76..be552546f 100644 --- a/src/ObjWriting/Game/T5/ZoneDumperT5.cpp +++ b/src/ObjWriting/Game/T5/ObjWriterT5.cpp @@ -1,4 +1,4 @@ -#include "ZoneDumperT5.h" +#include "ObjWriterT5.h" #include "AssetDumpers/AssetDumperGfxImage.h" #include "AssetDumpers/AssetDumperLocalizeEntry.h" @@ -10,17 +10,11 @@ #include "AssetDumpers/AssetDumperWeapon.h" #include "AssetDumpers/AssetDumperXModel.h" #include "Game/T5/GameAssetPoolT5.h" -#include "Game/T5/GameT5.h" #include "ObjWriting.h" using namespace T5; -bool ZoneDumper::CanHandleZone(AssetDumpingContext& context) const -{ - return context.m_zone->m_game == &g_GameT5; -} - -bool ZoneDumper::DumpZone(AssetDumpingContext& context) const +bool ObjWriter::DumpZone(AssetDumpingContext& context) const { #define DUMP_ASSET_POOL(dumperType, poolName, assetType) \ if (assetPools->poolName && ObjWriting::ShouldHandleAssetType(assetType)) \ diff --git a/src/ObjWriting/Game/T5/ObjWriterT5.h b/src/ObjWriting/Game/T5/ObjWriterT5.h new file mode 100644 index 000000000..1fa6971f0 --- /dev/null +++ b/src/ObjWriting/Game/T5/ObjWriterT5.h @@ -0,0 +1,11 @@ +#pragma once +#include "IObjWriter.h" + +namespace T5 +{ + class ObjWriter final : public IObjWriter + { + public: + bool DumpZone(AssetDumpingContext& context) const override; + }; +} // namespace T5 diff --git a/src/ObjWriting/Game/T5/ZoneDumperT5.h b/src/ObjWriting/Game/T5/ZoneDumperT5.h deleted file mode 100644 index 6e07068d8..000000000 --- a/src/ObjWriting/Game/T5/ZoneDumperT5.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "Dumping/IZoneDumper.h" - -namespace T5 -{ - class ZoneDumper final : public IZoneDumper - { - public: - bool CanHandleZone(AssetDumpingContext& context) const override; - bool DumpZone(AssetDumpingContext& context) const override; - }; -} // namespace T5 diff --git a/src/ObjWriting/Game/T6/ZoneDumperT6.cpp b/src/ObjWriting/Game/T6/ObjWriterT6.cpp similarity index 96% rename from src/ObjWriting/Game/T6/ZoneDumperT6.cpp rename to src/ObjWriting/Game/T6/ObjWriterT6.cpp index 9fe2ba765..7f344eed6 100644 --- a/src/ObjWriting/Game/T6/ZoneDumperT6.cpp +++ b/src/ObjWriting/Game/T6/ObjWriterT6.cpp @@ -1,4 +1,4 @@ -#include "ZoneDumperT6.h" +#include "ObjWriterT6.h" #include "AssetDumpers/AssetDumperFontIcon.h" #include "AssetDumpers/AssetDumperGfxImage.h" @@ -24,17 +24,11 @@ #include "AssetDumpers/AssetDumperXModel.h" #include "AssetDumpers/AssetDumperZBarrier.h" #include "Game/T6/GameAssetPoolT6.h" -#include "Game/T6/GameT6.h" #include "ObjWriting.h" using namespace T6; -bool ZoneDumper::CanHandleZone(AssetDumpingContext& context) const -{ - return context.m_zone->m_game == &g_GameT6; -} - -bool ZoneDumper::DumpZone(AssetDumpingContext& context) const +bool ObjWriter::DumpZone(AssetDumpingContext& context) const { #define DUMP_ASSET_POOL(dumperType, poolName, assetType) \ if (assetPools->poolName && ObjWriting::ShouldHandleAssetType(assetType)) \ diff --git a/src/ObjWriting/Game/T6/ObjWriterT6.h b/src/ObjWriting/Game/T6/ObjWriterT6.h new file mode 100644 index 000000000..e52907386 --- /dev/null +++ b/src/ObjWriting/Game/T6/ObjWriterT6.h @@ -0,0 +1,11 @@ +#pragma once +#include "IObjWriter.h" + +namespace T6 +{ + class ObjWriter final : public IObjWriter + { + public: + bool DumpZone(AssetDumpingContext& context) const override; + }; +} // namespace T6 diff --git a/src/ObjWriting/Game/T6/ZoneDumperT6.h b/src/ObjWriting/Game/T6/ZoneDumperT6.h deleted file mode 100644 index 908b32636..000000000 --- a/src/ObjWriting/Game/T6/ZoneDumperT6.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "Dumping/IZoneDumper.h" - -namespace T6 -{ - class ZoneDumper final : public IZoneDumper - { - public: - bool CanHandleZone(AssetDumpingContext& context) const override; - bool DumpZone(AssetDumpingContext& context) const override; - }; -} // namespace T6 diff --git a/src/ObjWriting/IObjWriter.cpp b/src/ObjWriting/IObjWriter.cpp new file mode 100644 index 000000000..685e7b4d8 --- /dev/null +++ b/src/ObjWriting/IObjWriter.cpp @@ -0,0 +1,27 @@ +#include "IObjWriter.h" + +#include "Game/IW3/ObjWriterIW3.h" +#include "Game/IW4/ObjWriterIW4.h" +#include "Game/IW5/ObjWriterIW5.h" +#include "Game/T5/ObjWriterT5.h" +#include "Game/T6/ObjWriterT6.h" + +#include + +const IObjWriter* IObjWriter::GetObjWriterForGame(GameId game) +{ + static const IObjWriter* zoneCreators[static_cast(GameId::COUNT)]{ + new IW3::ObjWriter(), + new IW4::ObjWriter(), + new IW5::ObjWriter(), + new T5::ObjWriter(), + new T6::ObjWriter(), + }; + static_assert(std::extent_v == static_cast(GameId::COUNT)); + + assert(static_cast(game) < static_cast(GameId::COUNT)); + const auto* result = zoneCreators[static_cast(game)]; + assert(result); + + return result; +} diff --git a/src/ObjWriting/IObjWriter.h b/src/ObjWriting/IObjWriter.h new file mode 100644 index 000000000..515d5f4fc --- /dev/null +++ b/src/ObjWriting/IObjWriter.h @@ -0,0 +1,18 @@ +#pragma once + +#include "Dumping/AssetDumpingContext.h" + +class IObjWriter +{ +public: + IObjWriter() = default; + virtual ~IObjWriter() = default; + IObjWriter(const IObjWriter& other) = default; + IObjWriter(IObjWriter&& other) noexcept = default; + IObjWriter& operator=(const IObjWriter& other) = default; + IObjWriter& operator=(IObjWriter&& other) noexcept = default; + + virtual bool DumpZone(AssetDumpingContext& assetDumpingContext) const = 0; + + static const IObjWriter* GetObjWriterForGame(GameId game); +}; diff --git a/src/ObjWriting/ObjWriting.cpp b/src/ObjWriting/ObjWriting.cpp index 46627e65d..8ecffa1b4 100644 --- a/src/ObjWriting/ObjWriting.cpp +++ b/src/ObjWriting/ObjWriting.cpp @@ -1,41 +1,7 @@ #include "ObjWriting.h" -#include "Dumping/IZoneDumper.h" -#include "Game/IW3/ZoneDumperIW3.h" -#include "Game/IW4/ZoneDumperIW4.h" -#include "Game/IW5/ZoneDumperIW5.h" -#include "Game/T5/ZoneDumperT5.h" -#include "Game/T6/ZoneDumperT6.h" - ObjWriting::Configuration_t ObjWriting::Configuration; -const IZoneDumper* const ZONE_DUMPER[]{ - new IW3::ZoneDumper(), - new IW4::ZoneDumper(), - new IW5::ZoneDumper(), - new T5::ZoneDumper(), - new T6::ZoneDumper(), -}; - -bool ObjWriting::DumpZone(AssetDumpingContext& context) -{ - for (const auto* dumper : ZONE_DUMPER) - { - if (dumper->CanHandleZone(context)) - { - if (dumper->DumpZone(context)) - { - return true; - } - - printf("Dumper for zone '%s' failed!\n", context.m_zone->m_name.c_str()); - return false; - } - } - - return false; -} - bool ObjWriting::ShouldHandleAssetType(const asset_type_t assetType) { if (assetType < 0) diff --git a/src/ObjWriting/ObjWriting.h b/src/ObjWriting/ObjWriting.h index e8796b2bb..b27beb25d 100644 --- a/src/ObjWriting/ObjWriting.h +++ b/src/ObjWriting/ObjWriting.h @@ -34,6 +34,5 @@ class ObjWriting } Configuration; - static bool DumpZone(AssetDumpingContext& context); static bool ShouldHandleAssetType(asset_type_t assetType); }; diff --git a/src/Unlinker/Unlinker.cpp b/src/Unlinker/Unlinker.cpp index c2ff656b2..a4c5df9e6 100644 --- a/src/Unlinker/Unlinker.cpp +++ b/src/Unlinker/Unlinker.cpp @@ -3,6 +3,7 @@ #include "ContentLister/ContentPrinter.h" #include "ContentLister/ZoneDefWriter.h" #include "IObjLoader.h" +#include "IObjWriter.h" #include "ObjContainer/IWD/IWD.h" #include "ObjLoading.h" #include "ObjWriting.h" @@ -288,13 +289,22 @@ class Unlinker::Impl } UpdateAssetIncludesAndExcludes(context); - ObjWriting::DumpZone(context); + + const auto* objWriter = IObjWriter::GetObjWriterForGame(zone.m_game->GetId()); + + auto result = objWriter->DumpZone(context); if (m_args.m_use_gdt) { context.m_gdt->EndStream(); gdtStream.close(); } + + if (!result) + { + std::cerr << "Dumping zone failed!\n"; + return false; + } } return true; From e80b9d7460d3aea1e6337d0a64cd8f768a328e42 Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 16 Oct 2024 18:59:10 +0200 Subject: [PATCH 4/4] chore: use std format --- src/Linker/Linker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Linker/Linker.cpp b/src/Linker/Linker.cpp index 421022df4..ed21b47e0 100644 --- a/src/Linker/Linker.cpp +++ b/src/Linker/Linker.cpp @@ -139,7 +139,7 @@ class LinkerImpl final : public Linker { std::unique_ptr zoneDefinition; { - const auto definitionFileName = targetName + ".zone"; + const auto definitionFileName = std::format("{}.zone", targetName); const auto definitionStream = sourceSearchPath->Open(definitionFileName); if (!definitionStream.IsOpen()) {