mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-05-09 14:04:57 +00:00
chore: use method static array for zoneloaderfactory
This commit is contained in:
parent
778361728c
commit
a1851b0ea0
@ -19,22 +19,15 @@
|
||||
|
||||
using namespace IW3;
|
||||
|
||||
class ZoneLoaderFactory::Impl
|
||||
namespace
|
||||
{
|
||||
static GameLanguage GetZoneLanguage(std::string& zoneName)
|
||||
{
|
||||
return GameLanguage::LANGUAGE_NONE;
|
||||
}
|
||||
|
||||
static bool CanLoad(ZoneHeader& header, bool* isSecure, bool* isOfficial)
|
||||
bool CanLoad(const ZoneHeader& header, bool* isSecure, bool* isOfficial)
|
||||
{
|
||||
assert(isSecure != nullptr);
|
||||
assert(isOfficial != nullptr);
|
||||
|
||||
if (header.m_version != ZoneConstants::ZONE_VERSION)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!memcmp(header.m_magic, ZoneConstants::MAGIC_UNSIGNED, std::char_traits<char>::length(ZoneConstants::MAGIC_UNSIGNED)))
|
||||
{
|
||||
@ -46,61 +39,54 @@ class ZoneLoaderFactory::Impl
|
||||
return false;
|
||||
}
|
||||
|
||||
static void SetupBlock(ZoneLoader* zoneLoader)
|
||||
void SetupBlock(ZoneLoader& zoneLoader)
|
||||
{
|
||||
#define XBLOCK_DEF(name, type) std::make_unique<XBlock>(STR(name), name, type)
|
||||
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_LARGE_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_PHYSICAL_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_LARGE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_VERTEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_INDEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_LARGE_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_PHYSICAL_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_LARGE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_VERTEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW3::XFILE_BLOCK_INDEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
|
||||
#undef XBLOCK_DEF
|
||||
}
|
||||
} // namespace
|
||||
|
||||
public:
|
||||
static ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
{
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
|
||||
// Check if this file is a supported IW4 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, &g_GameIW3);
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolIW3>(zonePtr, 0);
|
||||
zone->m_language = GetZoneLanguage(fileName);
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto* zoneLoader = new ZoneLoader(std::move(zone));
|
||||
|
||||
SetupBlock(zoneLoader);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorInflate>(ZoneConstants::AUTHED_CHUNK_SIZE)));
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
// Skip size and externalSize fields since they are not interesting for us
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepLoadZoneContent>(
|
||||
std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
// Return the fully setup zoneloader
|
||||
return zoneLoader;
|
||||
}
|
||||
};
|
||||
|
||||
ZoneLoader* ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
std::unique_ptr<ZoneLoader> ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const
|
||||
{
|
||||
return Impl::CreateLoaderForHeader(header, fileName);
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
|
||||
// Check if this file is a supported IW4 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, IGame::GetGameById(GameId::IW3));
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolIW3>(zonePtr, 0);
|
||||
zone->m_language = GameLanguage::LANGUAGE_NONE;
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto zoneLoader = std::make_unique<ZoneLoader>(std::move(zone));
|
||||
|
||||
SetupBlock(*zoneLoader);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorInflate>(ZoneConstants::AUTHED_CHUNK_SIZE)));
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
// Skip size and externalSize fields since they are not interesting for us
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(
|
||||
std::make_unique<StepLoadZoneContent>(std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
return zoneLoader;
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ namespace IW3
|
||||
{
|
||||
class ZoneLoaderFactory final : public IZoneLoaderFactory
|
||||
{
|
||||
class Impl;
|
||||
|
||||
public:
|
||||
ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) override;
|
||||
std::unique_ptr<ZoneLoader> CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const override;
|
||||
};
|
||||
} // namespace IW3
|
||||
|
@ -25,18 +25,14 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
using namespace IW4;
|
||||
|
||||
class ZoneLoaderFactory::Impl
|
||||
namespace
|
||||
{
|
||||
static GameLanguage GetZoneLanguage(std::string& zoneName)
|
||||
{
|
||||
return GameLanguage::LANGUAGE_NONE;
|
||||
}
|
||||
|
||||
static bool CanLoad(ZoneHeader& header, bool* isSecure, bool* isOfficial, bool* isIw4x)
|
||||
bool CanLoad(ZoneHeader& header, bool* isSecure, bool* isOfficial, bool* isIw4x)
|
||||
{
|
||||
assert(isSecure != nullptr);
|
||||
assert(isOfficial != nullptr);
|
||||
@ -79,23 +75,23 @@ class ZoneLoaderFactory::Impl
|
||||
return false;
|
||||
}
|
||||
|
||||
static void SetupBlock(ZoneLoader* zoneLoader)
|
||||
void SetupBlock(ZoneLoader& zoneLoader)
|
||||
{
|
||||
#define XBLOCK_DEF(name, type) std::make_unique<XBlock>(STR(name), name, type)
|
||||
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_LARGE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_CALLBACK, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_VERTEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_INDEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_LARGE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_CALLBACK, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_VERTEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_INDEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
|
||||
#undef XBLOCK_DEF
|
||||
}
|
||||
|
||||
static std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
{
|
||||
if (isOfficial)
|
||||
{
|
||||
@ -103,7 +99,7 @@ class ZoneLoaderFactory::Impl
|
||||
|
||||
if (!rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD, sizeof(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD)))
|
||||
{
|
||||
printf("Invalid public key for signature checking\n");
|
||||
std::cerr << "Invalid public key for signature checking\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -118,7 +114,7 @@ class ZoneLoaderFactory::Impl
|
||||
}
|
||||
}
|
||||
|
||||
static void AddAuthHeaderSteps(const bool isSecure, const bool isOfficial, ZoneLoader* zoneLoader, std::string& fileName)
|
||||
void AddAuthHeaderSteps(const bool isSecure, const bool isOfficial, ZoneLoader& zoneLoader, std::string& fileName)
|
||||
{
|
||||
// Unsigned zones do not have an auth header
|
||||
if (!isSecure)
|
||||
@ -127,99 +123,92 @@ class ZoneLoaderFactory::Impl
|
||||
// If file is signed setup a RSA instance.
|
||||
auto rsa = SetupRSA(isOfficial);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifyMagic>(ZoneConstants::MAGIC_AUTH_HEADER));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifyMagic>(ZoneConstants::MAGIC_AUTH_HEADER));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
|
||||
auto subHeaderHash = std::make_unique<StepLoadHash>(sizeof(DB_AuthHash::bytes), 1);
|
||||
auto* subHeaderHashPtr = subHeaderHash.get();
|
||||
zoneLoader->AddLoadingStep(std::move(subHeaderHash));
|
||||
zoneLoader.AddLoadingStep(std::move(subHeaderHash));
|
||||
|
||||
auto subHeaderHashSignature = std::make_unique<StepLoadSignature>(sizeof(DB_AuthSignature::bytes));
|
||||
auto* subHeaderHashSignaturePtr = subHeaderHashSignature.get();
|
||||
zoneLoader->AddLoadingStep(std::move(subHeaderHashSignature));
|
||||
zoneLoader.AddLoadingStep(std::move(subHeaderHashSignature));
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifySignature>(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifySignature>(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr));
|
||||
|
||||
auto subHeaderCapture = std::make_unique<ProcessorCaptureData>(sizeof(DB_AuthSubHeader));
|
||||
auto* subHeaderCapturePtr = subHeaderCapture.get();
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::move(subHeaderCapture)));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepAddProcessor>(std::move(subHeaderCapture)));
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifyFileName>(fileName, sizeof(DB_AuthSubHeader::fastfileName)));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifyFileName>(fileName, sizeof(DB_AuthSubHeader::fastfileName)));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
|
||||
auto masterBlockHashes = std::make_unique<StepLoadHash>(sizeof(DB_AuthHash::bytes), std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>);
|
||||
auto* masterBlockHashesPtr = masterBlockHashes.get();
|
||||
zoneLoader->AddLoadingStep(std::move(masterBlockHashes));
|
||||
zoneLoader.AddLoadingStep(std::move(masterBlockHashes));
|
||||
|
||||
zoneLoader->AddLoadingStep(
|
||||
zoneLoader.AddLoadingStep(
|
||||
std::make_unique<StepVerifyHash>(std::unique_ptr<IHashFunction>(Crypto::CreateSHA256()), 0, subHeaderHashPtr, subHeaderCapturePtr));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepRemoveProcessor>(subHeaderCapturePtr));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepRemoveProcessor>(subHeaderCapturePtr));
|
||||
|
||||
// Skip the rest of the first chunk
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader)));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader)));
|
||||
|
||||
zoneLoader->AddLoadingStep(
|
||||
zoneLoader.AddLoadingStep(
|
||||
std::make_unique<StepAddProcessor>(std::make_unique<ProcessorAuthedBlocks>(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP,
|
||||
ZoneConstants::AUTHED_CHUNK_SIZE,
|
||||
std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>,
|
||||
std::unique_ptr<IHashFunction>(Crypto::CreateSHA256()),
|
||||
Crypto::CreateSHA256(),
|
||||
masterBlockHashesPtr)));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
public:
|
||||
static ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
{
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
bool isIw4x;
|
||||
|
||||
// Check if this file is a supported IW4 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial, &isIw4x))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, &g_GameIW4);
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolIW4>(zonePtr, 0);
|
||||
zone->m_language = GetZoneLanguage(fileName);
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto* zoneLoader = new ZoneLoader(std::move(zone));
|
||||
|
||||
SetupBlock(zoneLoader);
|
||||
|
||||
// Skip unknown 1 byte field that the game ignores as well
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(1));
|
||||
|
||||
// Skip timestamp
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
|
||||
// Add steps for loading the auth header which also contain the signature of the zone if it is signed.
|
||||
AddAuthHeaderSteps(isSecure, isOfficial, zoneLoader, fileName);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorInflate>(ZoneConstants::AUTHED_CHUNK_SIZE)));
|
||||
|
||||
if (isIw4x) // IW4x has one extra byte of padding here for protection purposes
|
||||
{
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorIW4xDecryption>()));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(1));
|
||||
}
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
// Skip size and externalSize fields since they are not interesting for us
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepLoadZoneContent>(
|
||||
std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
// Return the fully setup zoneloader
|
||||
return zoneLoader;
|
||||
}
|
||||
};
|
||||
|
||||
ZoneLoader* ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
std::unique_ptr<ZoneLoader> ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const
|
||||
{
|
||||
return Impl::CreateLoaderForHeader(header, fileName);
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
bool isIw4x;
|
||||
|
||||
// Check if this file is a supported IW4 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial, &isIw4x))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, IGame::GetGameById(GameId::IW4));
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolIW4>(zonePtr, 0);
|
||||
zone->m_language = GameLanguage::LANGUAGE_NONE;
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto zoneLoader = std::make_unique<ZoneLoader>(std::move(zone));
|
||||
|
||||
SetupBlock(*zoneLoader);
|
||||
|
||||
// Skip unknown 1 byte field that the game ignores as well
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(1));
|
||||
|
||||
// Skip timestamp
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
|
||||
// Add steps for loading the auth header which also contain the signature of the zone if it is signed.
|
||||
AddAuthHeaderSteps(isSecure, isOfficial, *zoneLoader, fileName);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorInflate>(ZoneConstants::AUTHED_CHUNK_SIZE)));
|
||||
|
||||
if (isIw4x) // IW4x has one extra byte of padding here for protection purposes
|
||||
{
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorIW4xDecryption>()));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(1));
|
||||
}
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
// Skip size and externalSize fields since they are not interesting for us
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(
|
||||
std::make_unique<StepLoadZoneContent>(std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
return zoneLoader;
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ namespace IW4
|
||||
{
|
||||
class ZoneLoaderFactory final : public IZoneLoaderFactory
|
||||
{
|
||||
class Impl;
|
||||
|
||||
public:
|
||||
ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) override;
|
||||
std::unique_ptr<ZoneLoader> CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const override;
|
||||
};
|
||||
} // namespace IW4
|
||||
|
@ -24,18 +24,14 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
using namespace IW5;
|
||||
|
||||
class ZoneLoaderFactory::Impl
|
||||
namespace
|
||||
{
|
||||
static GameLanguage GetZoneLanguage(std::string& zoneName)
|
||||
{
|
||||
return GameLanguage::LANGUAGE_NONE;
|
||||
}
|
||||
|
||||
static bool CanLoad(ZoneHeader& header, bool* isSecure, bool* isOfficial)
|
||||
bool CanLoad(const ZoneHeader& header, bool* isSecure, bool* isOfficial)
|
||||
{
|
||||
assert(isSecure != nullptr);
|
||||
assert(isOfficial != nullptr);
|
||||
@ -62,24 +58,24 @@ class ZoneLoaderFactory::Impl
|
||||
return false;
|
||||
}
|
||||
|
||||
static void SetupBlock(ZoneLoader* zoneLoader)
|
||||
void SetupBlock(ZoneLoader& zoneLoader)
|
||||
{
|
||||
#define XBLOCK_DEF(name, type) std::make_unique<XBlock>(STR(name), name, type)
|
||||
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_LARGE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_CALLBACK, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_VERTEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_INDEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(XFILE_BLOCK_SCRIPT, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_LARGE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_CALLBACK, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_VERTEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_INDEX, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(XFILE_BLOCK_SCRIPT, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
|
||||
#undef XBLOCK_DEF
|
||||
}
|
||||
|
||||
static std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
{
|
||||
if (isOfficial)
|
||||
{
|
||||
@ -87,7 +83,7 @@ class ZoneLoaderFactory::Impl
|
||||
|
||||
if (!rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD, sizeof(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD)))
|
||||
{
|
||||
printf("Invalid public key for signature checking\n");
|
||||
std::cerr << "Invalid public key for signature checking\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -102,7 +98,7 @@ class ZoneLoaderFactory::Impl
|
||||
}
|
||||
}
|
||||
|
||||
static void AddAuthHeaderSteps(const bool isSecure, const bool isOfficial, ZoneLoader* zoneLoader, std::string& fileName)
|
||||
void AddAuthHeaderSteps(const bool isSecure, const bool isOfficial, ZoneLoader& zoneLoader, std::string& fileName)
|
||||
{
|
||||
// Unsigned zones do not have an auth header
|
||||
if (!isSecure)
|
||||
@ -111,92 +107,85 @@ class ZoneLoaderFactory::Impl
|
||||
// If file is signed setup a RSA instance.
|
||||
auto rsa = SetupRSA(isOfficial);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifyMagic>(ZoneConstants::MAGIC_AUTH_HEADER));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifyMagic>(ZoneConstants::MAGIC_AUTH_HEADER));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
|
||||
auto subHeaderHash = std::make_unique<StepLoadHash>(sizeof(DB_AuthHash::bytes), 1);
|
||||
auto* subHeaderHashPtr = subHeaderHash.get();
|
||||
zoneLoader->AddLoadingStep(std::move(subHeaderHash));
|
||||
zoneLoader.AddLoadingStep(std::move(subHeaderHash));
|
||||
|
||||
auto subHeaderHashSignature = std::make_unique<StepLoadSignature>(sizeof(DB_AuthSignature::bytes));
|
||||
auto* subHeaderHashSignaturePtr = subHeaderHashSignature.get();
|
||||
zoneLoader->AddLoadingStep(std::move(subHeaderHashSignature));
|
||||
zoneLoader.AddLoadingStep(std::move(subHeaderHashSignature));
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifySignature>(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifySignature>(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr));
|
||||
|
||||
auto subHeaderCapture = std::make_unique<ProcessorCaptureData>(sizeof(DB_AuthSubHeader));
|
||||
auto* subHeaderCapturePtr = subHeaderCapture.get();
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::move(subHeaderCapture)));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepAddProcessor>(std::move(subHeaderCapture)));
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifyFileName>(fileName, sizeof(DB_AuthSubHeader::fastfileName)));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifyFileName>(fileName, sizeof(DB_AuthSubHeader::fastfileName)));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Skip reserved
|
||||
|
||||
auto masterBlockHashes = std::make_unique<StepLoadHash>(sizeof(DB_AuthHash::bytes), std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>);
|
||||
auto* masterBlockHashesPtr = masterBlockHashes.get();
|
||||
zoneLoader->AddLoadingStep(std::move(masterBlockHashes));
|
||||
zoneLoader.AddLoadingStep(std::move(masterBlockHashes));
|
||||
|
||||
zoneLoader->AddLoadingStep(
|
||||
zoneLoader.AddLoadingStep(
|
||||
std::make_unique<StepVerifyHash>(std::unique_ptr<IHashFunction>(Crypto::CreateSHA256()), 0, subHeaderHashPtr, subHeaderCapturePtr));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepRemoveProcessor>(subHeaderCapturePtr));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepRemoveProcessor>(subHeaderCapturePtr));
|
||||
|
||||
// Skip the rest of the first chunk
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader)));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader)));
|
||||
|
||||
zoneLoader->AddLoadingStep(
|
||||
zoneLoader.AddLoadingStep(
|
||||
std::make_unique<StepAddProcessor>(std::make_unique<ProcessorAuthedBlocks>(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP,
|
||||
ZoneConstants::AUTHED_CHUNK_SIZE,
|
||||
std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>,
|
||||
std::unique_ptr<IHashFunction>(Crypto::CreateSHA256()),
|
||||
Crypto::CreateSHA256(),
|
||||
masterBlockHashesPtr)));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
public:
|
||||
static ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
{
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
|
||||
// Check if this file is a supported IW4 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, &g_GameIW5);
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolIW5>(zonePtr, 0);
|
||||
zone->m_language = GetZoneLanguage(fileName);
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto* zoneLoader = new ZoneLoader(std::move(zone));
|
||||
|
||||
SetupBlock(zoneLoader);
|
||||
|
||||
// Skip unknown 1 byte field that the game ignores as well
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(1));
|
||||
|
||||
// Skip timestamp
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
|
||||
// Add steps for loading the auth header which also contain the signature of the zone if it is signed.
|
||||
AddAuthHeaderSteps(isSecure, isOfficial, zoneLoader, fileName);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorInflate>(ZoneConstants::AUTHED_CHUNK_SIZE)));
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
// Skip size and externalSize fields since they are not interesting for us
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepLoadZoneContent>(
|
||||
std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
// Return the fully setup zoneloader
|
||||
return zoneLoader;
|
||||
}
|
||||
};
|
||||
|
||||
ZoneLoader* ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
std::unique_ptr<ZoneLoader> ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const
|
||||
{
|
||||
return Impl::CreateLoaderForHeader(header, fileName);
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
|
||||
// Check if this file is a supported IW4 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, IGame::GetGameById(GameId::IW5));
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolIW5>(zonePtr, 0);
|
||||
zone->m_language = GameLanguage::LANGUAGE_NONE;
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto zoneLoader = std::make_unique<ZoneLoader>(std::move(zone));
|
||||
|
||||
SetupBlock(*zoneLoader);
|
||||
|
||||
// Skip unknown 1 byte field that the game ignores as well
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(1));
|
||||
|
||||
// Skip timestamp
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
|
||||
// Add steps for loading the auth header which also contain the signature of the zone if it is signed.
|
||||
AddAuthHeaderSteps(isSecure, isOfficial, *zoneLoader, fileName);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorInflate>(ZoneConstants::AUTHED_CHUNK_SIZE)));
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
// Skip size and externalSize fields since they are not interesting for us
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(
|
||||
std::make_unique<StepLoadZoneContent>(std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
return zoneLoader;
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ namespace IW5
|
||||
{
|
||||
class ZoneLoaderFactory final : public IZoneLoaderFactory
|
||||
{
|
||||
class Impl;
|
||||
|
||||
public:
|
||||
ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) override;
|
||||
std::unique_ptr<ZoneLoader> CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const override;
|
||||
};
|
||||
} // namespace IW5
|
||||
|
@ -19,14 +19,9 @@
|
||||
|
||||
using namespace T5;
|
||||
|
||||
class ZoneLoaderFactory::Impl
|
||||
namespace
|
||||
{
|
||||
static GameLanguage GetZoneLanguage(std::string& zoneName)
|
||||
{
|
||||
return GameLanguage::LANGUAGE_NONE;
|
||||
}
|
||||
|
||||
static bool CanLoad(ZoneHeader& header, bool* isSecure, bool* isOfficial)
|
||||
bool CanLoad(const ZoneHeader& header, bool* isSecure, bool* isOfficial)
|
||||
{
|
||||
assert(isSecure != nullptr);
|
||||
assert(isOfficial != nullptr);
|
||||
@ -46,59 +41,52 @@ class ZoneLoaderFactory::Impl
|
||||
return false;
|
||||
}
|
||||
|
||||
static void SetupBlock(ZoneLoader* zoneLoader)
|
||||
void SetupBlock(ZoneLoader& zoneLoader)
|
||||
{
|
||||
#define XBLOCK_DEF(name, type) std::make_unique<XBlock>(STR(name), name, type)
|
||||
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_LARGE_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_PHYSICAL_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_LARGE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_LARGE_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_PHYSICAL_RUNTIME, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_LARGE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T5::XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
|
||||
#undef XBLOCK_DEF
|
||||
}
|
||||
} // namespace
|
||||
|
||||
public:
|
||||
static ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
{
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
|
||||
// Check if this file is a supported IW4 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, &g_GameT5);
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolT5>(zonePtr, 0);
|
||||
zone->m_language = GetZoneLanguage(fileName);
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto* zoneLoader = new ZoneLoader(std::move(zone));
|
||||
|
||||
SetupBlock(zoneLoader);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorInflate>(ZoneConstants::AUTHED_CHUNK_SIZE)));
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
// Skip size and externalSize fields since they are not interesting for us
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepLoadZoneContent>(
|
||||
std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
// Return the fully setup zoneloader
|
||||
return zoneLoader;
|
||||
}
|
||||
};
|
||||
|
||||
ZoneLoader* ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
std::unique_ptr<ZoneLoader> ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const
|
||||
{
|
||||
return Impl::CreateLoaderForHeader(header, fileName);
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
|
||||
// Check if this file is a supported IW4 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, IGame::GetGameById(GameId::T5));
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolT5>(zonePtr, 0);
|
||||
zone->m_language = GameLanguage::LANGUAGE_NONE;
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto zoneLoader = std::make_unique<ZoneLoader>(std::move(zone));
|
||||
|
||||
SetupBlock(*zoneLoader);
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::make_unique<ProcessorInflate>(ZoneConstants::AUTHED_CHUNK_SIZE)));
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(8));
|
||||
// Skip size and externalSize fields since they are not interesting for us
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(
|
||||
std::make_unique<StepLoadZoneContent>(std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
return zoneLoader;
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ namespace T5
|
||||
{
|
||||
class ZoneLoaderFactory final : public IZoneLoaderFactory
|
||||
{
|
||||
class Impl;
|
||||
|
||||
public:
|
||||
ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) override;
|
||||
std::unique_ptr<ZoneLoader> CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const override;
|
||||
};
|
||||
} // namespace T5
|
||||
|
@ -22,15 +22,16 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
using namespace T6;
|
||||
|
||||
class ZoneLoaderFactory::Impl
|
||||
namespace
|
||||
{
|
||||
static GameLanguage GetZoneLanguage(std::string& zoneName)
|
||||
GameLanguage GetZoneLanguage(const std::string& zoneName)
|
||||
{
|
||||
auto languagePrefixes = g_GameT6.GetLanguagePrefixes();
|
||||
const auto& languagePrefixes = IGame::GetGameById(GameId::T6)->GetLanguagePrefixes();
|
||||
|
||||
for (const auto& languagePrefix : languagePrefixes)
|
||||
{
|
||||
@ -43,7 +44,7 @@ class ZoneLoaderFactory::Impl
|
||||
return GameLanguage::LANGUAGE_NONE;
|
||||
}
|
||||
|
||||
static bool CanLoad(ZoneHeader& header, bool* isSecure, bool* isOfficial, bool* isEncrypted)
|
||||
bool CanLoad(const ZoneHeader& header, bool* isSecure, bool* isOfficial, bool* isEncrypted)
|
||||
{
|
||||
assert(isSecure != nullptr);
|
||||
assert(isOfficial != nullptr);
|
||||
@ -88,23 +89,23 @@ class ZoneLoaderFactory::Impl
|
||||
return false;
|
||||
}
|
||||
|
||||
static void SetupBlock(ZoneLoader* zoneLoader)
|
||||
void SetupBlock(ZoneLoader& zoneLoader)
|
||||
{
|
||||
#define XBLOCK_DEF(name, type) std::make_unique<XBlock>(STR(name), name, type)
|
||||
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_RUNTIME_VIRTUAL, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_RUNTIME_PHYSICAL, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_DELAY_VIRTUAL, XBlock::Type::BLOCK_TYPE_DELAY));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_DELAY_PHYSICAL, XBlock::Type::BLOCK_TYPE_DELAY));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader->AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_STREAMER_RESERVE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_TEMP, XBlock::Type::BLOCK_TYPE_TEMP));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_RUNTIME_VIRTUAL, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_RUNTIME_PHYSICAL, XBlock::Type::BLOCK_TYPE_RUNTIME));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_DELAY_VIRTUAL, XBlock::Type::BLOCK_TYPE_DELAY));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_DELAY_PHYSICAL, XBlock::Type::BLOCK_TYPE_DELAY));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_VIRTUAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_PHYSICAL, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
zoneLoader.AddXBlock(XBLOCK_DEF(T6::XFILE_BLOCK_STREAMER_RESERVE, XBlock::Type::BLOCK_TYPE_NORMAL));
|
||||
|
||||
#undef XBLOCK_DEF
|
||||
}
|
||||
|
||||
static std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
std::unique_ptr<IPublicKeyAlgorithm> SetupRSA(const bool isOfficial)
|
||||
{
|
||||
if (isOfficial)
|
||||
{
|
||||
@ -112,7 +113,7 @@ class ZoneLoaderFactory::Impl
|
||||
|
||||
if (!rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_TREYARCH, sizeof(ZoneConstants::RSA_PUBLIC_KEY_TREYARCH)))
|
||||
{
|
||||
printf("Invalid public key for signature checking\n");
|
||||
std::cerr << "Invalid public key for signature checking\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -127,24 +128,24 @@ class ZoneLoaderFactory::Impl
|
||||
}
|
||||
}
|
||||
|
||||
static ISignatureProvider* AddAuthHeaderSteps(const bool isSecure, ZoneLoader* zoneLoader, std::string& fileName)
|
||||
ISignatureProvider* AddAuthHeaderSteps(const bool isSecure, ZoneLoader& zoneLoader, std::string& fileName)
|
||||
{
|
||||
// Unsigned zones do not have an auth header
|
||||
if (!isSecure)
|
||||
return nullptr;
|
||||
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifyMagic>(ZoneConstants::MAGIC_AUTH_HEADER));
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Loading Flags which are always zero
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifyFileName>(fileName, 32));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifyMagic>(ZoneConstants::MAGIC_AUTH_HEADER));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepSkipBytes>(4)); // Loading Flags which are always zero
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepVerifyFileName>(fileName, 32));
|
||||
|
||||
auto signatureLoadStep = std::make_unique<StepLoadSignature>(256);
|
||||
auto* signatureLoadStepPtr = signatureLoadStep.get();
|
||||
zoneLoader->AddLoadingStep(std::move(signatureLoadStep));
|
||||
zoneLoader.AddLoadingStep(std::move(signatureLoadStep));
|
||||
|
||||
return signatureLoadStepPtr;
|
||||
}
|
||||
|
||||
static ICapturedDataProvider* AddXChunkProcessor(bool isEncrypted, ZoneLoader* zoneLoader, std::string& fileName)
|
||||
ICapturedDataProvider* AddXChunkProcessor(const bool isEncrypted, ZoneLoader& zoneLoader, std::string& fileName)
|
||||
{
|
||||
ICapturedDataProvider* result = nullptr;
|
||||
auto xChunkProcessor = std::make_unique<ProcessorXChunks>(ZoneConstants::STREAM_COUNT, ZoneConstants::XCHUNK_SIZE, ZoneConstants::VANILLA_BUFFER_SIZE);
|
||||
@ -160,62 +161,55 @@ class ZoneLoaderFactory::Impl
|
||||
|
||||
// Decompress the chunks using zlib
|
||||
xChunkProcessor->AddChunkProcessor(std::make_unique<XChunkProcessorInflate>());
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAddProcessor>(std::move(xChunkProcessor)));
|
||||
zoneLoader.AddLoadingStep(std::make_unique<StepAddProcessor>(std::move(xChunkProcessor)));
|
||||
|
||||
// If there is encryption, the signed data of the zone is the final hash blocks provided by the Salsa20 IV adaption algorithm
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
public:
|
||||
static ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
{
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
bool isEncrypted;
|
||||
|
||||
// Check if this file is a supported T6 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial, &isEncrypted))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, &g_GameT6);
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolT6>(zonePtr, 0);
|
||||
zone->m_language = GetZoneLanguage(fileName);
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto* zoneLoader = new ZoneLoader(std::move(zone));
|
||||
|
||||
SetupBlock(zoneLoader);
|
||||
|
||||
// If file is signed setup a RSA instance.
|
||||
auto rsa = isSecure ? SetupRSA(isOfficial) : nullptr;
|
||||
|
||||
// Add steps for loading the auth header which also contain the signature of the zone if it is signed.
|
||||
ISignatureProvider* signatureProvider = AddAuthHeaderSteps(isSecure, zoneLoader, fileName);
|
||||
|
||||
// Setup loading XChunks from the zone from this point on.
|
||||
ICapturedDataProvider* signatureDataProvider = AddXChunkProcessor(isEncrypted, zoneLoader, fileName);
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepLoadZoneSizes>());
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepLoadZoneContent>(
|
||||
std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
if (isSecure)
|
||||
{
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifySignature>(std::move(rsa), signatureProvider, signatureDataProvider));
|
||||
}
|
||||
|
||||
// Return the fully setup zoneloader
|
||||
return zoneLoader;
|
||||
}
|
||||
};
|
||||
|
||||
ZoneLoader* ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName)
|
||||
std::unique_ptr<ZoneLoader> ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const
|
||||
{
|
||||
return Impl::CreateLoaderForHeader(header, fileName);
|
||||
bool isSecure;
|
||||
bool isOfficial;
|
||||
bool isEncrypted;
|
||||
|
||||
// Check if this file is a supported T6 zone.
|
||||
if (!CanLoad(header, &isSecure, &isOfficial, &isEncrypted))
|
||||
return nullptr;
|
||||
|
||||
// Create new zone
|
||||
auto zone = std::make_unique<Zone>(fileName, 0, IGame::GetGameById(GameId::T6));
|
||||
auto* zonePtr = zone.get();
|
||||
zone->m_pools = std::make_unique<GameAssetPoolT6>(zonePtr, 0);
|
||||
zone->m_language = GetZoneLanguage(fileName);
|
||||
|
||||
// File is supported. Now setup all required steps for loading this file.
|
||||
auto zoneLoader = std::make_unique<ZoneLoader>(std::move(zone));
|
||||
|
||||
SetupBlock(*zoneLoader);
|
||||
|
||||
// If file is signed setup a RSA instance.
|
||||
auto rsa = isSecure ? SetupRSA(isOfficial) : nullptr;
|
||||
|
||||
// Add steps for loading the auth header which also contain the signature of the zone if it is signed.
|
||||
ISignatureProvider* signatureProvider = AddAuthHeaderSteps(isSecure, *zoneLoader, fileName);
|
||||
|
||||
// Setup loading XChunks from the zone from this point on.
|
||||
ICapturedDataProvider* signatureDataProvider = AddXChunkProcessor(isEncrypted, *zoneLoader, fileName);
|
||||
|
||||
// Start of the XFile struct
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepLoadZoneSizes>());
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepAllocXBlocks>());
|
||||
|
||||
// Start of the zone content
|
||||
zoneLoader->AddLoadingStep(
|
||||
std::make_unique<StepLoadZoneContent>(std::make_unique<ContentLoader>(), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK));
|
||||
|
||||
if (isSecure)
|
||||
{
|
||||
zoneLoader->AddLoadingStep(std::make_unique<StepVerifySignature>(std::move(rsa), signatureProvider, signatureDataProvider));
|
||||
}
|
||||
|
||||
return zoneLoader;
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ namespace T6
|
||||
{
|
||||
class ZoneLoaderFactory final : public IZoneLoaderFactory
|
||||
{
|
||||
class Impl;
|
||||
|
||||
public:
|
||||
ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) override;
|
||||
std::unique_ptr<ZoneLoader> CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const override;
|
||||
};
|
||||
} // namespace T6
|
||||
|
27
src/ZoneLoading/Loading/IZoneLoaderFactory.cpp
Normal file
27
src/ZoneLoading/Loading/IZoneLoaderFactory.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include "IZoneLoaderFactory.h"
|
||||
|
||||
#include "Game/IW3/ZoneLoaderFactoryIW3.h"
|
||||
#include "Game/IW4/ZoneLoaderFactoryIW4.h"
|
||||
#include "Game/IW5/ZoneLoaderFactoryIW5.h"
|
||||
#include "Game/T5/ZoneLoaderFactoryT5.h"
|
||||
#include "Game/T6/ZoneLoaderFactoryT6.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
const IZoneLoaderFactory* IZoneLoaderFactory::GetZoneLoaderFactoryForGame(GameId game)
|
||||
{
|
||||
static const IZoneLoaderFactory* zoneCreators[static_cast<unsigned>(GameId::COUNT)]{
|
||||
new IW3::ZoneLoaderFactory(),
|
||||
new IW4::ZoneLoaderFactory(),
|
||||
new IW5::ZoneLoaderFactory(),
|
||||
new T5::ZoneLoaderFactory(),
|
||||
new T6::ZoneLoaderFactory(),
|
||||
};
|
||||
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;
|
||||
}
|
@ -3,6 +3,8 @@
|
||||
#include "Zone/ZoneTypes.h"
|
||||
#include "ZoneLoader.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class IZoneLoaderFactory
|
||||
{
|
||||
public:
|
||||
@ -13,5 +15,7 @@ public:
|
||||
IZoneLoaderFactory& operator=(const IZoneLoaderFactory& other) = default;
|
||||
IZoneLoaderFactory& operator=(IZoneLoaderFactory&& other) noexcept = default;
|
||||
|
||||
virtual ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) = 0;
|
||||
virtual std::unique_ptr<ZoneLoader> CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const = 0;
|
||||
|
||||
static const IZoneLoaderFactory* GetZoneLoaderFactoryForGame(GameId game);
|
||||
};
|
||||
|
@ -1,34 +1,24 @@
|
||||
#include "ZoneLoading.h"
|
||||
|
||||
#include "Game/IW3/ZoneLoaderFactoryIW3.h"
|
||||
#include "Game/IW4/ZoneLoaderFactoryIW4.h"
|
||||
#include "Game/IW5/ZoneLoaderFactoryIW5.h"
|
||||
#include "Game/T5/ZoneLoaderFactoryT5.h"
|
||||
#include "Game/T6/ZoneLoaderFactoryT6.h"
|
||||
#include "Loading/IZoneLoaderFactory.h"
|
||||
#include "Loading/ZoneLoader.h"
|
||||
#include "Utils/ObjFileStream.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
IZoneLoaderFactory* ZoneLoaderFactories[]{
|
||||
new IW3::ZoneLoaderFactory(),
|
||||
new IW4::ZoneLoaderFactory(),
|
||||
new IW5::ZoneLoaderFactory(),
|
||||
new T5::ZoneLoaderFactory(),
|
||||
new T6::ZoneLoaderFactory(),
|
||||
};
|
||||
|
||||
std::unique_ptr<Zone> ZoneLoading::LoadZone(const std::string& path)
|
||||
{
|
||||
auto zoneName = fs::path(path).filename().replace_extension("").string();
|
||||
auto zoneName = fs::path(path).filename().replace_extension().string();
|
||||
std::ifstream file(path, std::fstream::in | std::fstream::binary);
|
||||
|
||||
if (!file.is_open())
|
||||
{
|
||||
printf("Could not open file '%s'.\n", path.c_str());
|
||||
std::cerr << std::format("Could not open file '{}'.\n", path);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -36,27 +26,27 @@ std::unique_ptr<Zone> ZoneLoading::LoadZone(const std::string& path)
|
||||
file.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
if (file.gcount() != sizeof(header))
|
||||
{
|
||||
std::cout << "Failed to read zone header from file '" << path << "'.\n";
|
||||
std::cerr << std::format("Failed to read zone header from file '{}'.\n", path);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ZoneLoader* zoneLoader = nullptr;
|
||||
for (auto* factory : ZoneLoaderFactories)
|
||||
std::unique_ptr<ZoneLoader> zoneLoader;
|
||||
for (auto game = 0u; game < static_cast<unsigned>(GameId::COUNT); game++)
|
||||
{
|
||||
const auto* factory = IZoneLoaderFactory::GetZoneLoaderFactoryForGame(static_cast<GameId>(game));
|
||||
zoneLoader = factory->CreateLoaderForHeader(header, zoneName);
|
||||
|
||||
if (zoneLoader != nullptr)
|
||||
if (zoneLoader)
|
||||
break;
|
||||
}
|
||||
|
||||
if (zoneLoader == nullptr)
|
||||
if (!zoneLoader)
|
||||
{
|
||||
printf("Could not create factory for zone '%s'.\n", zoneName.c_str());
|
||||
std::cerr << std::format("Could not create factory for zone '{}'.\n", zoneName);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto loadedZone = zoneLoader->LoadZone(file);
|
||||
delete zoneLoader;
|
||||
|
||||
file.close();
|
||||
return std::move(loadedZone);
|
||||
|
Loading…
x
Reference in New Issue
Block a user