Files
OpenAssetTools/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp
T

396 lines
18 KiB
C++

#include "ZoneLoaderFactoryIW4.h"
#include "ContentLoaderIW4.h"
#include "Game/GameLanguage.h"
#include "Game/IW4/GameIW4.h"
#include "Game/IW4/IW4.h"
#include "Game/IW4/ZoneConstantsIW4.h"
#include "Loading/Processor/ProcessorAuthedBlocks.h"
#include "Loading/Processor/ProcessorCaptureData.h"
#include "Loading/Processor/ProcessorIW4xDecryption.h"
#include "Loading/Processor/ProcessorInflate.h"
#include "Loading/Processor/ProcessorSkipAuthedBlocks.h"
#include "Loading/Steps/StepAddProcessor.h"
#include "Loading/Steps/StepAllocXBlocks.h"
#include "Loading/Steps/StepDumpData.h"
#include "Loading/Steps/StepLoadHash.h"
#include "Loading/Steps/StepLoadSignature.h"
#include "Loading/Steps/StepLoadZoneContent.h"
#include "Loading/Steps/StepLoadZoneSizes.h"
#include "Loading/Steps/StepRemoveProcessor.h"
#include "Loading/Steps/StepSkipBytes.h"
#include "Loading/Steps/StepSkipZoneImageHeaders.h"
#include "Loading/Steps/StepVerifyFileName.h"
#include "Loading/Steps/StepVerifyHash.h"
#include "Loading/Steps/StepVerifyMagic.h"
#include "Loading/Steps/StepVerifySignature.h"
#include "Loading/WordSizeDeterminator.h"
#include "Utils/ClassUtils.h"
#include "Utils/Endianness.h"
#include "Utils/Logging/Log.h"
#include <cassert>
#include <cstring>
#include <filesystem>
#include <type_traits>
using namespace IW4;
namespace fs = std::filesystem;
namespace
{
struct ZoneLoaderInspectionResultIW4
{
ZoneLoaderInspectionResult m_generic_result;
bool m_is_iw4x;
};
GameVariantId DeterminePcVariant(const ZoneDataPeeking& filePeek, const bool isSigned)
{
std::optional<GameWordSize> wordSize;
filePeek.PeekWithStream(
[&wordSize, isSigned](std::istream& stream)
{
auto zoneReader = std::make_unique<ZoneReader>();
size_t skipSizePreInflate = sizeof(ZoneHeader) //
+ 1 // unknown
+ 8 // timestamp
;
if (isSigned)
{
skipSizePreInflate += sizeof(DB_AuthHeader) // header
+ ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader) // rest of the first chunk
;
}
constexpr size_t skipSizePostInflate = 8 // Zone sizes
+ 4 * 8 // Xblock sizes
;
zoneReader->AddLoadingStep(step::CreateStepSkipBytes(skipSizePreInflate));
if (isSigned)
{
zoneReader->AddLoadingStep(step::CreateStepAddProcessor(
processor::CreateProcessorSkipAuthedBlocks(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP,
ZoneConstants::AUTHED_CHUNK_SIZE,
static_cast<unsigned>(std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>),
32u)));
}
zoneReader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE)));
zoneReader->AddLoadingStep(step::CreateStepSkipBytes(skipSizePostInflate));
wordSize = DetermineWordSizeFromXAssetList(stream,
std::move(zoneReader),
XAssetListOffsets{
.offsetStringCount = 0,
.offsetStringPointer = 4,
.offsetAssetCount = 8,
.offsetAssetPointer = 12,
},
XAssetListOffsets{
.offsetStringCount = 0,
.offsetStringPointer = 8,
.offsetAssetCount = 16,
.offsetAssetPointer = 24,
});
});
if (wordSize && *wordSize == GameWordSize::ARCH_64)
return GameVariantId::IW4_PC64;
return GameVariantId::IW4_PC32;
}
std::optional<ZoneLoaderInspectionResultIW4> InspectZoneHeaderIw4(ZoneDataPeeking& filePeek)
{
const auto& header = filePeek.PeekStruct<ZoneHeader>();
if (endianness::FromLittleEndian(header.m_version) == ZoneConstants::ZONE_VERSION_PC)
{
if (!memcmp(header.m_magic, ZoneConstants::MAGIC_IW4X, std::char_traits<char>::length(ZoneConstants::MAGIC_IW4X)))
{
if (*reinterpret_cast<const uint32_t*>(&header.m_magic[std::char_traits<char>::length(ZoneConstants::MAGIC_IW4X)])
== ZoneConstants::ZONE_VERSION_IW4x)
{
constexpr ZoneLoaderInspectionResult generic{
.m_game_id = GameId::IW4,
// IW4x magic zones are always 32bit
.m_variant_id = GameVariantId::IW4_PC32,
.m_is_official = false,
.m_is_signed = false,
.m_is_encrypted = false,
};
return ZoneLoaderInspectionResultIW4{
.m_generic_result = generic,
.m_is_iw4x = true,
};
}
return std::nullopt;
}
if (!memcmp(header.m_magic, ZoneConstants::MAGIC_SIGNED_INFINITY_WARD, std::char_traits<char>::length(ZoneConstants::MAGIC_SIGNED_INFINITY_WARD)))
{
const ZoneLoaderInspectionResult generic{
.m_game_id = GameId::IW4,
.m_variant_id = DeterminePcVariant(filePeek, true),
.m_is_official = true,
.m_is_signed = true,
.m_is_encrypted = false,
};
return ZoneLoaderInspectionResultIW4{
.m_generic_result = generic,
.m_is_iw4x = false,
};
}
if (!memcmp(header.m_magic, ZoneConstants::MAGIC_UNSIGNED, std::char_traits<char>::length(ZoneConstants::MAGIC_UNSIGNED)))
{
const ZoneLoaderInspectionResult generic{
.m_game_id = GameId::IW4,
.m_variant_id = DeterminePcVariant(filePeek, false),
.m_is_official = false,
.m_is_signed = false,
.m_is_encrypted = false,
};
return ZoneLoaderInspectionResultIW4{
.m_generic_result = generic,
.m_is_iw4x = false,
};
}
}
else if (endianness::FromBigEndian(header.m_version) == ZoneConstants::ZONE_VERSION_XENON
|| endianness::FromBigEndian(header.m_version) == ZoneConstants::ZONE_VERSION_XENON_ALPHA)
{
if (!memcmp(header.m_magic, ZoneConstants::MAGIC_UNSIGNED, std::char_traits<char>::length(ZoneConstants::MAGIC_UNSIGNED)))
{
constexpr ZoneLoaderInspectionResult generic{
.m_game_id = GameId::IW4,
.m_variant_id = GameVariantId::IW4_XBOX,
.m_is_official = false,
.m_is_signed = false,
.m_is_encrypted = false,
};
return ZoneLoaderInspectionResultIW4{
.m_generic_result = generic,
.m_is_iw4x = false,
};
}
if (!memcmp(header.m_magic, ZoneConstants::MAGIC_SIGNED_INFINITY_WARD, std::char_traits<char>::length(ZoneConstants::MAGIC_SIGNED_INFINITY_WARD)))
{
constexpr ZoneLoaderInspectionResult generic{
.m_game_id = GameId::IW4,
.m_variant_id = GameVariantId::IW4_XBOX,
.m_is_official = true,
.m_is_signed = true,
.m_is_encrypted = false,
};
return ZoneLoaderInspectionResultIW4{
.m_generic_result = generic,
.m_is_iw4x = false,
};
}
}
return std::nullopt;
}
bool CanLoadZone(const ZoneLoaderInspectionResultIW4& inspectionResult)
{
#ifdef ARCH_x86
if (GameVariant::GetVariantById(inspectionResult.m_generic_result.m_variant_id)->GetWordSize() == GameWordSize::ARCH_64)
{
con::warn("x64 zones are only supported by a x64 OAT build!");
return false;
}
#endif
return true;
}
void SetupBlock(ZoneMemory& zoneMemory)
{
#define XBLOCK_DEF(name, type) std::make_unique<XBlock>(STR(name), name, type)
zoneMemory.AddBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_TEMP, XBlockType::BLOCK_TYPE_TEMP));
zoneMemory.AddBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_PHYSICAL, XBlockType::BLOCK_TYPE_NORMAL));
zoneMemory.AddBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_RUNTIME, XBlockType::BLOCK_TYPE_RUNTIME));
zoneMemory.AddBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_VIRTUAL, XBlockType::BLOCK_TYPE_NORMAL));
zoneMemory.AddBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_LARGE, XBlockType::BLOCK_TYPE_NORMAL));
zoneMemory.AddBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_CALLBACK, XBlockType::BLOCK_TYPE_NORMAL));
zoneMemory.AddBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_VERTEX, XBlockType::BLOCK_TYPE_NORMAL));
zoneMemory.AddBlock(XBLOCK_DEF(IW4::XFILE_BLOCK_INDEX, XBlockType::BLOCK_TYPE_NORMAL));
#undef XBLOCK_DEF
}
std::unique_ptr<cryptography::IPublicKeyAlgorithm> SetupRsa(const bool isOfficial, const GameVariantId variant)
{
if (isOfficial)
{
auto rsa = cryptography::CreateRsa(cryptography::HashingAlgorithm::RSA_HASH_SHA256, cryptography::RsaPaddingMode::RSA_PADDING_PSS);
bool keySetSuccessful;
if (variant == GameVariantId::IW4_PC32 || variant == GameVariantId::IW4_PC64)
{
keySetSuccessful = rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD_PC, sizeof(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD_PC));
}
else if (variant == GameVariantId::IW4_XBOX)
{
keySetSuccessful = rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD_XENON, sizeof(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD_XENON));
}
else
{
con::error("No public key for platform");
return nullptr;
}
if (!keySetSuccessful)
{
con::error("Invalid public key for signature checking");
return nullptr;
}
return rsa;
}
else
{
assert(false);
// TODO: Load custom RSA key here
return nullptr;
}
}
void AddAuthHeaderSteps(const ZoneLoaderInspectionResultIW4& inspectResult, ZoneReader& zoneReader, const std::string& fileName)
{
// Unsigned zones do not have an auth header
if (!inspectResult.m_generic_result.m_is_signed)
return;
// If file is signed setup a RSA instance.
auto rsa = SetupRsa(inspectResult.m_generic_result.m_is_official, inspectResult.m_generic_result.m_variant_id);
zoneReader.AddLoadingStep(step::CreateStepVerifyMagic(ZoneConstants::MAGIC_AUTH_HEADER));
zoneReader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Skip reserved
auto subHeaderHash = step::CreateStepLoadHash(sizeof(DB_AuthHash::bytes), 1);
auto* subHeaderHashPtr = subHeaderHash.get();
zoneReader.AddLoadingStep(std::move(subHeaderHash));
auto subHeaderHashSignature = step::CreateStepLoadSignature(sizeof(DB_AuthSignature::bytes));
auto* subHeaderHashSignaturePtr = subHeaderHashSignature.get();
zoneReader.AddLoadingStep(std::move(subHeaderHashSignature));
zoneReader.AddLoadingStep(step::CreateStepVerifySignature(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr));
auto subHeaderCapture = processor::CreateProcessorCaptureData(sizeof(DB_AuthSubHeader));
auto* subHeaderCapturePtr = subHeaderCapture.get();
zoneReader.AddLoadingStep(step::CreateStepAddProcessor(std::move(subHeaderCapture)));
zoneReader.AddLoadingStep(step::CreateStepVerifyFileName(fileName, sizeof(DB_AuthSubHeader::fastfileName)));
zoneReader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Skip reserved
auto masterBlockHashes =
step::CreateStepLoadHash(sizeof(DB_AuthHash::bytes), static_cast<unsigned>(std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>));
auto* masterBlockHashesPtr = masterBlockHashes.get();
zoneReader.AddLoadingStep(std::move(masterBlockHashes));
zoneReader.AddLoadingStep(step::CreateStepVerifyHash(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr));
zoneReader.AddLoadingStep(step::CreateStepRemoveProcessor(subHeaderCapturePtr));
// Skip the rest of the first chunk
zoneReader.AddLoadingStep(step::CreateStepSkipBytes(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader)));
zoneReader.AddLoadingStep(step::CreateStepAddProcessor(
processor::CreateProcessorAuthedBlocks(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP,
ZoneConstants::AUTHED_CHUNK_SIZE,
static_cast<unsigned>(std::extent_v<decltype(DB_AuthSubHeader::masterBlockHashes)>),
cryptography::CreateSha256(),
masterBlockHashesPtr)));
}
} // namespace
std::optional<ZoneLoaderInspectionResult> ZoneLoaderFactory::InspectZoneHeader(ZoneDataPeeking& filePeek) const
{
auto resultIw4 = InspectZoneHeaderIw4(filePeek);
if (!resultIw4 || !CanLoadZone(*resultIw4))
return std::nullopt;
return resultIw4->m_generic_result;
}
std::unique_ptr<ZoneLoader> ZoneLoaderFactory::CreateLoaderForHeader(ZoneDataPeeking& filePeek,
const std::string& fileName,
std::optional<std::unique_ptr<ProgressCallback>> progressCallback) const
{
const auto inspectResult = InspectZoneHeaderIw4(filePeek);
if (!inspectResult)
return nullptr;
const auto* variant = GameVariant::GetVariantById(inspectResult->m_generic_result.m_variant_id);
// Create new zone
auto zone = std::make_unique<Zone>(fileName, 0, variant->GetId());
auto* zonePtr = zone.get();
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(zonePtr->Memory());
// Skip the initial header that we peeked at before
zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(sizeof(ZoneHeader)));
// Skip unknown 1 byte field that the game ignores as well
zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(1));
// Skip timestamp
zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(8));
// Xbox fastfiles have an additional header of all included images outside the zone data
if (inspectResult->m_generic_result.m_variant_id == GameVariantId::IW4_XBOX)
zoneLoader->AddLoadingStep(step::CreateStepSkipZoneImageHeaders());
// Add steps for loading the auth header which also contain the signature of the zone if it is signed.
AddAuthHeaderSteps(*inspectResult, *zoneLoader, fileName);
zoneLoader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE)));
if (inspectResult->m_is_iw4x) // IW4x has one extra byte of padding here for protection purposes
{
zoneLoader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorIW4xDecryption()));
zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(1));
}
if (variant->GetPlatform() == GamePlatform::PC)
{
// Start of the XFile struct
zoneLoader->AddLoadingStep(step::CreateStepLoadZoneSizes());
zoneLoader->AddLoadingStep(step::CreateStepAllocXBlocks(zonePtr));
// Start of the zone content
zoneLoader->AddLoadingStep(step::CreateStepLoadZoneContent(
[zonePtr](ZoneInputStream& stream)
{
return std::make_unique<ContentLoader>(*zonePtr, stream);
},
variant->GetWordSize() == GameWordSize::ARCH_64 ? 64u : 32u,
ZoneConstants::ZONE_POINTER_OFFSET_BIT_COUNT,
ZoneConstants::ZONE_POINTER_BLOCK_BIT_COUNT,
ZoneConstants::INSERT_BLOCK,
zonePtr,
std::move(progressCallback)));
}
else
{
fs::path dumpFileNamePath = fs::path(fileName).filename();
dumpFileNamePath.replace_extension(".dat");
std::string dumpFileName = dumpFileNamePath.string();
con::warn("Dumping xbox assets is not supported, making a full fastfile data dump to {}", dumpFileName);
zoneLoader->AddLoadingStep(step::CreateStepDumpData(dumpFileName, 0xFFFFFFFF));
}
return zoneLoader;
}