From b92e85dc14c25e6d4f1ed91ae217652e4bba1a93 Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 2 May 2025 21:12:48 +0100 Subject: [PATCH] refactor: hide implementation of loading steps inside file --- .../Game/IW3/ZoneLoaderFactoryIW3.cpp | 12 +-- .../Game/IW4/ZoneLoaderFactoryIW4.cpp | 46 +++++----- .../Game/IW5/ZoneLoaderFactoryIW5.cpp | 42 +++++----- .../Game/T5/ZoneLoaderFactoryT5.cpp | 12 +-- .../Game/T6/ZoneLoaderFactoryT6.cpp | 24 +++--- .../Loading/Steps/StepAddProcessor.cpp | 36 ++++++-- .../Loading/Steps/StepAddProcessor.h | 12 +-- .../Loading/Steps/StepAllocXBlocks.cpp | 54 +++++++----- .../Loading/Steps/StepAllocXBlocks.h | 9 +- .../Loading/Steps/StepDumpData.cpp | 74 ++++++++++------- src/ZoneLoading/Loading/Steps/StepDumpData.h | 14 ++-- .../Loading/Steps/StepLoadHash.cpp | 80 +++++++++++------- src/ZoneLoading/Loading/Steps/StepLoadHash.h | 25 ++---- .../Loading/Steps/StepLoadSignature.cpp | 57 ++++++++----- .../Loading/Steps/StepLoadSignature.h | 16 ++-- .../Loading/Steps/StepLoadZoneContent.cpp | 45 ++++++---- .../Loading/Steps/StepLoadZoneContent.h | 16 +--- .../Loading/Steps/StepLoadZoneSizes.cpp | 51 ++++++++---- .../Loading/Steps/StepLoadZoneSizes.h | 23 +++-- .../Loading/Steps/StepRemoveProcessor.cpp | 34 ++++++-- .../Loading/Steps/StepRemoveProcessor.h | 15 ++-- .../Loading/Steps/StepSkipBytes.cpp | 60 +++++++++----- src/ZoneLoading/Loading/Steps/StepSkipBytes.h | 14 ++-- .../Loading/Steps/StepVerifyFileName.cpp | 81 +++++++++++------- .../Loading/Steps/StepVerifyFileName.h | 16 ++-- .../Loading/Steps/StepVerifyHash.cpp | 83 ++++++++++++------- .../Loading/Steps/StepVerifyHash.h | 21 ++--- .../Loading/Steps/StepVerifyMagic.cpp | 47 +++++++---- .../Loading/Steps/StepVerifyMagic.h | 15 ++-- .../Loading/Steps/StepVerifySignature.cpp | 72 ++++++++++------ .../Loading/Steps/StepVerifySignature.h | 20 ++--- 31 files changed, 649 insertions(+), 477 deletions(-) diff --git a/src/ZoneLoading/Game/IW3/ZoneLoaderFactoryIW3.cpp b/src/ZoneLoading/Game/IW3/ZoneLoaderFactoryIW3.cpp index 199f65af..5de47662 100644 --- a/src/ZoneLoading/Game/IW3/ZoneLoaderFactoryIW3.cpp +++ b/src/ZoneLoading/Game/IW3/ZoneLoaderFactoryIW3.cpp @@ -10,6 +10,7 @@ #include "Loading/Steps/StepAddProcessor.h" #include "Loading/Steps/StepAllocXBlocks.h" #include "Loading/Steps/StepLoadZoneContent.h" +#include "Loading/Steps/StepLoadZoneSizes.h" #include "Loading/Steps/StepSkipBytes.h" #include "Utils/ClassUtils.h" @@ -77,16 +78,15 @@ std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& SetupBlock(*zoneLoader); - zoneLoader->AddLoadingStep(std::make_unique(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); + zoneLoader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); // Start of the XFile struct - zoneLoader->AddLoadingStep(std::make_unique(8)); - // Skip size and externalSize fields since they are not interesting for us - zoneLoader->AddLoadingStep(std::make_unique()); + zoneLoader->AddLoadingStep(step::CreateStepLoadZoneSizes()); + zoneLoader->AddLoadingStep(step::CreateStepAllocXBlocks()); // Start of the zone content - zoneLoader->AddLoadingStep(std::make_unique( - std::make_unique(*zonePtr), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); + zoneLoader->AddLoadingStep( + step::CreateStepLoadZoneContent(std::make_unique(*zonePtr), ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); return zoneLoader; } diff --git a/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp b/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp index 47987241..4cc808e0 100644 --- a/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp +++ b/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp @@ -15,6 +15,7 @@ #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/StepVerifyFileName.h" @@ -123,38 +124,38 @@ namespace // If file is signed setup a RSA instance. auto rsa = SetupRsa(isOfficial); - zoneLoader.AddLoadingStep(std::make_unique(ZoneConstants::MAGIC_AUTH_HEADER)); - zoneLoader.AddLoadingStep(std::make_unique(4)); // Skip reserved + zoneLoader.AddLoadingStep(step::CreateStepVerifyMagic(ZoneConstants::MAGIC_AUTH_HEADER)); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Skip reserved - auto subHeaderHash = std::make_unique(sizeof(DB_AuthHash::bytes), 1); + auto subHeaderHash = step::CreateStepLoadHash(sizeof(DB_AuthHash::bytes), 1); auto* subHeaderHashPtr = subHeaderHash.get(); zoneLoader.AddLoadingStep(std::move(subHeaderHash)); - auto subHeaderHashSignature = std::make_unique(sizeof(DB_AuthSignature::bytes)); + auto subHeaderHashSignature = step::CreateStepLoadSignature(sizeof(DB_AuthSignature::bytes)); auto* subHeaderHashSignaturePtr = subHeaderHashSignature.get(); zoneLoader.AddLoadingStep(std::move(subHeaderHashSignature)); - zoneLoader.AddLoadingStep(std::make_unique(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr)); + zoneLoader.AddLoadingStep(step::CreateStepVerifySignature(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr)); auto subHeaderCapture = processor::CreateProcessorCaptureData(sizeof(DB_AuthSubHeader)); auto* subHeaderCapturePtr = subHeaderCapture.get(); - zoneLoader.AddLoadingStep(std::make_unique(std::move(subHeaderCapture))); + zoneLoader.AddLoadingStep(step::CreateStepAddProcessor(std::move(subHeaderCapture))); - zoneLoader.AddLoadingStep(std::make_unique(fileName, sizeof(DB_AuthSubHeader::fastfileName))); - zoneLoader.AddLoadingStep(std::make_unique(4)); // Skip reserved + zoneLoader.AddLoadingStep(step::CreateStepVerifyFileName(fileName, sizeof(DB_AuthSubHeader::fastfileName))); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Skip reserved auto masterBlockHashes = - std::make_unique(sizeof(DB_AuthHash::bytes), static_cast(std::extent_v)); + step::CreateStepLoadHash(sizeof(DB_AuthHash::bytes), static_cast(std::extent_v)); auto* masterBlockHashesPtr = masterBlockHashes.get(); zoneLoader.AddLoadingStep(std::move(masterBlockHashes)); - zoneLoader.AddLoadingStep(std::make_unique(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr)); - zoneLoader.AddLoadingStep(std::make_unique(subHeaderCapturePtr)); + zoneLoader.AddLoadingStep(step::CreateStepVerifyHash(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr)); + zoneLoader.AddLoadingStep(step::CreateStepRemoveProcessor(subHeaderCapturePtr)); // Skip the rest of the first chunk - zoneLoader.AddLoadingStep(std::make_unique(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader))); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader))); - zoneLoader.AddLoadingStep(std::make_unique( + zoneLoader.AddLoadingStep(step::CreateStepAddProcessor( processor::CreateProcessorAuthedBlocks(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP, ZoneConstants::AUTHED_CHUNK_SIZE, static_cast(std::extent_v), @@ -185,30 +186,29 @@ std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& SetupBlock(*zoneLoader); // Skip unknown 1 byte field that the game ignores as well - zoneLoader->AddLoadingStep(std::make_unique(1)); + zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(1)); // Skip timestamp - zoneLoader->AddLoadingStep(std::make_unique(8)); + zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(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(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); + zoneLoader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); if (isIw4x) // IW4x has one extra byte of padding here for protection purposes { - zoneLoader->AddLoadingStep(std::make_unique(processor::CreateProcessorIW4xDecryption())); - zoneLoader->AddLoadingStep(std::make_unique(1)); + zoneLoader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorIW4xDecryption())); + zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(1)); } // Start of the XFile struct - zoneLoader->AddLoadingStep(std::make_unique(8)); - // Skip size and externalSize fields since they are not interesting for us - zoneLoader->AddLoadingStep(std::make_unique()); + zoneLoader->AddLoadingStep(step::CreateStepLoadZoneSizes()); + zoneLoader->AddLoadingStep(step::CreateStepAllocXBlocks()); // Start of the zone content - zoneLoader->AddLoadingStep(std::make_unique( - std::make_unique(*zonePtr), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); + zoneLoader->AddLoadingStep( + step::CreateStepLoadZoneContent(std::make_unique(*zonePtr), ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); return zoneLoader; } diff --git a/src/ZoneLoading/Game/IW5/ZoneLoaderFactoryIW5.cpp b/src/ZoneLoading/Game/IW5/ZoneLoaderFactoryIW5.cpp index d88e7610..4e77a1ca 100644 --- a/src/ZoneLoading/Game/IW5/ZoneLoaderFactoryIW5.cpp +++ b/src/ZoneLoading/Game/IW5/ZoneLoaderFactoryIW5.cpp @@ -14,6 +14,7 @@ #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/StepVerifyFileName.h" @@ -107,38 +108,38 @@ namespace // If file is signed setup a RSA instance. auto rsa = SetupRsa(isOfficial); - zoneLoader.AddLoadingStep(std::make_unique(ZoneConstants::MAGIC_AUTH_HEADER)); - zoneLoader.AddLoadingStep(std::make_unique(4)); // Skip reserved + zoneLoader.AddLoadingStep(step::CreateStepVerifyMagic(ZoneConstants::MAGIC_AUTH_HEADER)); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Skip reserved - auto subHeaderHash = std::make_unique(sizeof(DB_AuthHash::bytes), 1); + auto subHeaderHash = step::CreateStepLoadHash(sizeof(DB_AuthHash::bytes), 1); auto* subHeaderHashPtr = subHeaderHash.get(); zoneLoader.AddLoadingStep(std::move(subHeaderHash)); - auto subHeaderHashSignature = std::make_unique(sizeof(DB_AuthSignature::bytes)); + auto subHeaderHashSignature = step::CreateStepLoadSignature(sizeof(DB_AuthSignature::bytes)); auto* subHeaderHashSignaturePtr = subHeaderHashSignature.get(); zoneLoader.AddLoadingStep(std::move(subHeaderHashSignature)); - zoneLoader.AddLoadingStep(std::make_unique(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr)); + zoneLoader.AddLoadingStep(step::CreateStepVerifySignature(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr)); auto subHeaderCapture = processor::CreateProcessorCaptureData(sizeof(DB_AuthSubHeader)); auto* subHeaderCapturePtr = subHeaderCapture.get(); - zoneLoader.AddLoadingStep(std::make_unique(std::move(subHeaderCapture))); + zoneLoader.AddLoadingStep(step::CreateStepAddProcessor(std::move(subHeaderCapture))); - zoneLoader.AddLoadingStep(std::make_unique(fileName, sizeof(DB_AuthSubHeader::fastfileName))); - zoneLoader.AddLoadingStep(std::make_unique(4)); // Skip reserved + zoneLoader.AddLoadingStep(step::CreateStepVerifyFileName(fileName, sizeof(DB_AuthSubHeader::fastfileName))); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Skip reserved auto masterBlockHashes = - std::make_unique(sizeof(DB_AuthHash::bytes), static_cast(std::extent_v)); + step::CreateStepLoadHash(sizeof(DB_AuthHash::bytes), static_cast(std::extent_v)); auto* masterBlockHashesPtr = masterBlockHashes.get(); zoneLoader.AddLoadingStep(std::move(masterBlockHashes)); - zoneLoader.AddLoadingStep(std::make_unique(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr)); - zoneLoader.AddLoadingStep(std::make_unique(subHeaderCapturePtr)); + zoneLoader.AddLoadingStep(step::CreateStepVerifyHash(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr)); + zoneLoader.AddLoadingStep(step::CreateStepRemoveProcessor(subHeaderCapturePtr)); // Skip the rest of the first chunk - zoneLoader.AddLoadingStep(std::make_unique(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader))); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader))); - zoneLoader.AddLoadingStep(std::make_unique( + zoneLoader.AddLoadingStep(step::CreateStepAddProcessor( processor::CreateProcessorAuthedBlocks(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP, ZoneConstants::AUTHED_CHUNK_SIZE, static_cast(std::extent_v), @@ -168,24 +169,23 @@ std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& SetupBlock(*zoneLoader); // Skip unknown 1 byte field that the game ignores as well - zoneLoader->AddLoadingStep(std::make_unique(1)); + zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(1)); // Skip timestamp - zoneLoader->AddLoadingStep(std::make_unique(8)); + zoneLoader->AddLoadingStep(step::CreateStepSkipBytes(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(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); + zoneLoader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); // Start of the XFile struct - zoneLoader->AddLoadingStep(std::make_unique(8)); - // Skip size and externalSize fields since they are not interesting for us - zoneLoader->AddLoadingStep(std::make_unique()); + zoneLoader->AddLoadingStep(step::CreateStepLoadZoneSizes()); + zoneLoader->AddLoadingStep(step::CreateStepAllocXBlocks()); // Start of the zone content - zoneLoader->AddLoadingStep(std::make_unique( - std::make_unique(*zonePtr), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); + zoneLoader->AddLoadingStep( + step::CreateStepLoadZoneContent(std::make_unique(*zonePtr), ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); return zoneLoader; } diff --git a/src/ZoneLoading/Game/T5/ZoneLoaderFactoryT5.cpp b/src/ZoneLoading/Game/T5/ZoneLoaderFactoryT5.cpp index 4e516974..e04c9ead 100644 --- a/src/ZoneLoading/Game/T5/ZoneLoaderFactoryT5.cpp +++ b/src/ZoneLoading/Game/T5/ZoneLoaderFactoryT5.cpp @@ -10,6 +10,7 @@ #include "Loading/Steps/StepAddProcessor.h" #include "Loading/Steps/StepAllocXBlocks.h" #include "Loading/Steps/StepLoadZoneContent.h" +#include "Loading/Steps/StepLoadZoneSizes.h" #include "Loading/Steps/StepSkipBytes.h" #include "Utils/ClassUtils.h" @@ -77,16 +78,15 @@ std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& SetupBlock(*zoneLoader); - zoneLoader->AddLoadingStep(std::make_unique(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); + zoneLoader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); // Start of the XFile struct - zoneLoader->AddLoadingStep(std::make_unique(8)); - // Skip size and externalSize fields since they are not interesting for us - zoneLoader->AddLoadingStep(std::make_unique()); + zoneLoader->AddLoadingStep(step::CreateStepLoadZoneSizes()); + zoneLoader->AddLoadingStep(step::CreateStepAllocXBlocks()); // Start of the zone content - zoneLoader->AddLoadingStep(std::make_unique( - std::make_unique(*zonePtr), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); + zoneLoader->AddLoadingStep( + step::CreateStepLoadZoneContent(std::make_unique(*zonePtr), ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); return zoneLoader; } diff --git a/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp b/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp index 413db7e3..e2bb2d24 100644 --- a/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp +++ b/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp @@ -35,7 +35,7 @@ namespace for (const auto& languagePrefix : languagePrefixes) { - if (zoneName.compare(0, languagePrefix.m_prefix.length(), languagePrefix.m_prefix) == 0) + if (zoneName.starts_with(languagePrefix.m_prefix)) { return languagePrefix.m_language; } @@ -128,17 +128,17 @@ namespace } } - ISignatureProvider* AddAuthHeaderSteps(const bool isSecure, ZoneLoader& zoneLoader, std::string& fileName) + ISignatureProvider* AddAuthHeaderSteps(const bool isSecure, ZoneLoader& zoneLoader, const std::string& fileName) { // Unsigned zones do not have an auth header if (!isSecure) return nullptr; - zoneLoader.AddLoadingStep(std::make_unique(ZoneConstants::MAGIC_AUTH_HEADER)); - zoneLoader.AddLoadingStep(std::make_unique(4)); // Loading Flags which are always zero - zoneLoader.AddLoadingStep(std::make_unique(fileName, 32)); + zoneLoader.AddLoadingStep(step::CreateStepVerifyMagic(ZoneConstants::MAGIC_AUTH_HEADER)); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Loading Flags which are always zero + zoneLoader.AddLoadingStep(step::CreateStepVerifyFileName(fileName, 32)); - auto signatureLoadStep = std::make_unique(256); + auto signatureLoadStep = step::CreateStepLoadSignature(256); auto* signatureLoadStepPtr = signatureLoadStep.get(); zoneLoader.AddLoadingStep(std::move(signatureLoadStep)); @@ -161,7 +161,7 @@ namespace // Decompress the chunks using zlib xChunkProcessor->AddChunkProcessor(std::make_unique()); - zoneLoader.AddLoadingStep(std::make_unique(std::move(xChunkProcessor))); + zoneLoader.AddLoadingStep(step::CreateStepAddProcessor(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; @@ -199,16 +199,16 @@ std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& ICapturedDataProvider* signatureDataProvider = AddXChunkProcessor(isEncrypted, *zoneLoader, fileName); // Start of the XFile struct - zoneLoader->AddLoadingStep(std::make_unique()); - zoneLoader->AddLoadingStep(std::make_unique()); + zoneLoader->AddLoadingStep(step::CreateStepLoadZoneSizes()); + zoneLoader->AddLoadingStep(step::CreateStepAllocXBlocks()); // Start of the zone content - zoneLoader->AddLoadingStep(std::make_unique( - std::make_unique(*zonePtr), zonePtr, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); + zoneLoader->AddLoadingStep( + step::CreateStepLoadZoneContent(std::make_unique(*zonePtr), ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK)); if (isSecure) { - zoneLoader->AddLoadingStep(std::make_unique(std::move(rsa), signatureProvider, signatureDataProvider)); + zoneLoader->AddLoadingStep(step::CreateStepVerifySignature(std::move(rsa), signatureProvider, signatureDataProvider)); } return zoneLoader; diff --git a/src/ZoneLoading/Loading/Steps/StepAddProcessor.cpp b/src/ZoneLoading/Loading/Steps/StepAddProcessor.cpp index cf7545a1..42483dcc 100644 --- a/src/ZoneLoading/Loading/Steps/StepAddProcessor.cpp +++ b/src/ZoneLoading/Loading/Steps/StepAddProcessor.cpp @@ -2,15 +2,33 @@ #include -StepAddProcessor::StepAddProcessor(std::unique_ptr streamProcessor) - : m_stream_processor(std::move(streamProcessor)) +namespace { -} + class StepAddProcessor final : public ILoadingStep + { + public: + explicit StepAddProcessor(std::unique_ptr streamProcessor) + : m_stream_processor(std::move(streamProcessor)) + { + } -void StepAddProcessor::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + assert(m_stream_processor != nullptr); + + zoneLoader.AddStreamProcessor(std::move(m_stream_processor)); + m_stream_processor = nullptr; + } + + private: + std::unique_ptr m_stream_processor; + }; +} // namespace + +namespace step { - assert(m_stream_processor != nullptr); - - zoneLoader.AddStreamProcessor(std::move(m_stream_processor)); - m_stream_processor = nullptr; -} + std::unique_ptr CreateStepAddProcessor(std::unique_ptr streamProcessor) + { + return std::make_unique(std::move(streamProcessor)); + } +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepAddProcessor.h b/src/ZoneLoading/Loading/Steps/StepAddProcessor.h index f91bdfdf..83f8a49f 100644 --- a/src/ZoneLoading/Loading/Steps/StepAddProcessor.h +++ b/src/ZoneLoading/Loading/Steps/StepAddProcessor.h @@ -4,13 +4,7 @@ #include -class StepAddProcessor final : public ILoadingStep +namespace step { -public: - explicit StepAddProcessor(std::unique_ptr streamProcessor); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - std::unique_ptr m_stream_processor; -}; + std::unique_ptr CreateStepAddProcessor(std::unique_ptr streamProcessor); +} diff --git a/src/ZoneLoading/Loading/Steps/StepAllocXBlocks.cpp b/src/ZoneLoading/Loading/Steps/StepAllocXBlocks.cpp index a92c9dce..c4b9d59e 100644 --- a/src/ZoneLoading/Loading/Steps/StepAllocXBlocks.cpp +++ b/src/ZoneLoading/Loading/Steps/StepAllocXBlocks.cpp @@ -5,28 +5,40 @@ namespace { constexpr uint64_t MAX_XBLOCK_SIZE = 0x3C000000; -} -void StepAllocXBlocks::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) + class StepAllocXBlocks final : public ILoadingStep + { + public: + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + const auto blockCount = static_cast(zoneLoader.m_blocks.size()); + + const auto blockSizes = std::make_unique(blockCount); + stream.Load(blockSizes.get(), sizeof(xblock_size_t) * blockCount); + + uint64_t totalMemory = 0; + for (unsigned int block = 0; block < blockCount; block++) + { + totalMemory += blockSizes[block]; + } + + if (totalMemory > MAX_XBLOCK_SIZE) + { + throw InvalidXBlockSizeException(totalMemory, MAX_XBLOCK_SIZE); + } + + for (unsigned int block = 0; block < blockCount; block++) + { + zoneLoader.m_blocks[block]->Alloc(blockSizes[block]); + } + } + }; +} // namespace + +namespace step { - const auto blockCount = static_cast(zoneLoader.m_blocks.size()); - - const auto blockSizes = std::make_unique(blockCount); - stream.Load(blockSizes.get(), sizeof(xblock_size_t) * blockCount); - - uint64_t totalMemory = 0; - for (unsigned int block = 0; block < blockCount; block++) + std::unique_ptr CreateStepAllocXBlocks() { - totalMemory += blockSizes[block]; + return std::make_unique(); } - - if (totalMemory > MAX_XBLOCK_SIZE) - { - throw InvalidXBlockSizeException(totalMemory, MAX_XBLOCK_SIZE); - } - - for (unsigned int block = 0; block < blockCount; block++) - { - zoneLoader.m_blocks[block]->Alloc(blockSizes[block]); - } -} +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepAllocXBlocks.h b/src/ZoneLoading/Loading/Steps/StepAllocXBlocks.h index b567e326..0b280d2f 100644 --- a/src/ZoneLoading/Loading/Steps/StepAllocXBlocks.h +++ b/src/ZoneLoading/Loading/Steps/StepAllocXBlocks.h @@ -2,8 +2,9 @@ #include "Loading/ILoadingStep.h" -class StepAllocXBlocks final : public ILoadingStep +#include + +namespace step { -public: - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; -}; + std::unique_ptr CreateStepAllocXBlocks(); +} diff --git a/src/ZoneLoading/Loading/Steps/StepDumpData.cpp b/src/ZoneLoading/Loading/Steps/StepDumpData.cpp index 0ea0225f..3999dc4d 100644 --- a/src/ZoneLoading/Loading/Steps/StepDumpData.cpp +++ b/src/ZoneLoading/Loading/Steps/StepDumpData.cpp @@ -2,39 +2,57 @@ #include -StepDumpData::StepDumpData(const size_t dumpCount) - : m_dump_count(dumpCount) +namespace { -} - -void StepDumpData::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) -{ - uint8_t tempBuffer[128]; - auto dumpedBytes = 0uz; - - std::ofstream tempFile("dump.dat", std::fstream::out | std::fstream::binary); - - while (dumpedBytes < m_dump_count) + class StepDumpData final : public ILoadingStep { - size_t toDump; - - if (m_dump_count - dumpedBytes < sizeof(tempBuffer)) + public: + explicit StepDumpData(const size_t dumpCount) + : m_dump_count(dumpCount) { - toDump = m_dump_count - dumpedBytes; - } - else - { - toDump = sizeof(tempBuffer); } - const auto loadedSize = stream.Load(tempBuffer, toDump); - dumpedBytes += loadedSize; + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + uint8_t tempBuffer[128]; + auto dumpedBytes = 0uz; - if (loadedSize == 0) - break; + std::ofstream tempFile("dump.dat", std::fstream::out | std::fstream::binary); - tempFile.write(reinterpret_cast(tempBuffer), static_cast(loadedSize)); + while (dumpedBytes < m_dump_count) + { + size_t toDump; + + if (m_dump_count - dumpedBytes < sizeof(tempBuffer)) + { + toDump = m_dump_count - dumpedBytes; + } + else + { + toDump = sizeof(tempBuffer); + } + + const auto loadedSize = stream.Load(tempBuffer, toDump); + dumpedBytes += loadedSize; + + if (loadedSize == 0) + break; + + tempFile.write(reinterpret_cast(tempBuffer), static_cast(loadedSize)); + } + + tempFile.close(); + } + + private: + size_t m_dump_count; + }; +} // namespace + +namespace step +{ + std::unique_ptr CreateStepDumpData(size_t dumpCount) + { + return std::make_unique(dumpCount); } - - tempFile.close(); -} +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepDumpData.h b/src/ZoneLoading/Loading/Steps/StepDumpData.h index 043fc215..07beac1a 100644 --- a/src/ZoneLoading/Loading/Steps/StepDumpData.h +++ b/src/ZoneLoading/Loading/Steps/StepDumpData.h @@ -2,13 +2,9 @@ #include "Loading/ILoadingStep.h" -class StepDumpData final : public ILoadingStep +#include + +namespace step { -public: - explicit StepDumpData(size_t dumpCount); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - size_t m_dump_count; -}; + std::unique_ptr CreateStepDumpData(size_t dumpCount); +} diff --git a/src/ZoneLoading/Loading/Steps/StepLoadHash.cpp b/src/ZoneLoading/Loading/Steps/StepLoadHash.cpp index 85f3d3dd..750f6cac 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadHash.cpp +++ b/src/ZoneLoading/Loading/Steps/StepLoadHash.cpp @@ -4,38 +4,56 @@ #include -StepLoadHash::StepLoadHash(const size_t hashSize, const unsigned hashCount) - : m_hash_size(hashSize), - m_hash_count(hashCount), - m_hashes(std::make_unique(hashSize * hashCount)) +namespace { -} + class StepLoadHash final : public step::IStepLoadHash + { + public: + StepLoadHash(const size_t hashSize, const unsigned hashCount) + : m_hash_size(hashSize), + m_hash_count(hashCount), + m_hashes(std::make_unique(hashSize * hashCount)) + { + } -StepLoadHash::~StepLoadHash() = default; + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + if (stream.Load(m_hashes.get(), m_hash_size * m_hash_count) != m_hash_size * m_hash_count) + throw UnexpectedEndOfFileException(); + } -void StepLoadHash::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) + void GetHash(const unsigned hashIndex, const uint8_t** pHash, size_t* pSize) override + { + assert(pHash != nullptr); + assert(pSize != nullptr); + assert(hashIndex < m_hash_count); + + assert(m_hashes); + + *pHash = &m_hashes[m_hash_size * hashIndex]; + *pSize = m_hash_size; + } + + void GetCapturedData(const uint8_t** pCapturedData, size_t* pSize) override + { + assert(pCapturedData != nullptr); + assert(pSize != nullptr); + + *pCapturedData = m_hashes.get(); + *pSize = m_hash_size * m_hash_count; + } + + private: + size_t m_hash_size; + unsigned m_hash_count; + std::unique_ptr m_hashes; + }; +} // namespace + +namespace step { - if (stream.Load(m_hashes.get(), m_hash_size * m_hash_count) != m_hash_size * m_hash_count) - throw UnexpectedEndOfFileException(); -} - -void StepLoadHash::GetHash(const unsigned hashIndex, const uint8_t** pHash, size_t* pSize) -{ - assert(pHash != nullptr); - assert(pSize != nullptr); - assert(hashIndex < m_hash_count); - - assert(m_hashes); - - *pHash = &m_hashes[m_hash_size * hashIndex]; - *pSize = m_hash_size; -} - -void StepLoadHash::GetCapturedData(const uint8_t** pCapturedData, size_t* pSize) -{ - assert(pCapturedData != nullptr); - assert(pSize != nullptr); - - *pCapturedData = m_hashes.get(); - *pSize = m_hash_size * m_hash_count; -} + std::unique_ptr CreateStepLoadHash(const size_t hashSize, const unsigned hashCount) + { + return std::make_unique(hashSize, hashCount); + } +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepLoadHash.h b/src/ZoneLoading/Loading/Steps/StepLoadHash.h index 2ebe258b..4bca2b9d 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadHash.h +++ b/src/ZoneLoading/Loading/Steps/StepLoadHash.h @@ -4,26 +4,13 @@ #include "Loading/ILoadingStep.h" #include "Utils/ICapturedDataProvider.h" -#include #include -class StepLoadHash final : public ILoadingStep, public IHashProvider, public ICapturedDataProvider +namespace step { -public: - StepLoadHash(size_t hashSize, unsigned hashCount); - ~StepLoadHash() override; + class IStepLoadHash : public ILoadingStep, public IHashProvider, public ICapturedDataProvider + { + }; - StepLoadHash(const StepLoadHash& other) = delete; - StepLoadHash(StepLoadHash&& other) noexcept = default; - StepLoadHash& operator=(const StepLoadHash& other) = delete; - StepLoadHash& operator=(StepLoadHash&& other) noexcept = delete; - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - void GetHash(unsigned hashIndex, const uint8_t** pHash, size_t* pSize) override; - void GetCapturedData(const uint8_t** pCapturedData, size_t* pSize) override; - -private: - const size_t m_hash_size; - const unsigned m_hash_count; - std::unique_ptr m_hashes; -}; + std::unique_ptr CreateStepLoadHash(size_t hashSize, unsigned hashCount); +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepLoadSignature.cpp b/src/ZoneLoading/Loading/Steps/StepLoadSignature.cpp index 0e5c5e3e..789c61ac 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadSignature.cpp +++ b/src/ZoneLoading/Loading/Steps/StepLoadSignature.cpp @@ -4,25 +4,44 @@ #include -StepLoadSignature::StepLoadSignature(const size_t signatureSize) - : m_signature(std::make_unique(signatureSize)), - m_signature_size(signatureSize) +namespace { -} + class StepLoadSignature final : public step::IStepLoadSignature + { + public: + explicit StepLoadSignature(const size_t signatureSize) + : m_signature(std::make_unique(signatureSize)), + m_signature_size(signatureSize) + { + } -void StepLoadSignature::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + if (stream.Load(m_signature.get(), m_signature_size) != m_signature_size) + throw UnexpectedEndOfFileException(); + } + + void GetSignature(const uint8_t** pSignature, size_t* pSize) override + { + assert(pSignature != nullptr); + assert(pSize != nullptr); + + assert(m_signature != nullptr); + + *pSignature = m_signature.get(); + *pSize = m_signature_size; + } + + private: + std::unique_ptr m_signature; + size_t m_signature_size; + }; +} // namespace + +namespace step { - if (stream.Load(m_signature.get(), m_signature_size) != m_signature_size) - throw UnexpectedEndOfFileException(); -} - -void StepLoadSignature::GetSignature(const uint8_t** pSignature, size_t* pSize) -{ - assert(pSignature != nullptr); - assert(pSize != nullptr); - - assert(m_signature != nullptr); - - *pSignature = m_signature.get(); - *pSize = m_signature_size; -} + std::unique_ptr CreateStepLoadSignature(const size_t signatureSize) + { + return std::make_unique(signatureSize); + } +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepLoadSignature.h b/src/ZoneLoading/Loading/Steps/StepLoadSignature.h index 5e0b1688..ec0de07e 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadSignature.h +++ b/src/ZoneLoading/Loading/Steps/StepLoadSignature.h @@ -5,15 +5,11 @@ #include -class StepLoadSignature final : public ILoadingStep, public ISignatureProvider +namespace step { -public: - explicit StepLoadSignature(size_t signatureSize); + class IStepLoadSignature : public ILoadingStep, public ISignatureProvider + { + }; - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - void GetSignature(const uint8_t** pSignature, size_t* pSize) override; - -private: - std::unique_ptr m_signature; - size_t m_signature_size; -}; + std::unique_ptr CreateStepLoadSignature(size_t signatureSize); +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.cpp b/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.cpp index d0c64cc1..5b0ed359 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.cpp +++ b/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.cpp @@ -2,20 +2,37 @@ #include "Zone/Stream/ZoneInputStream.h" -StepLoadZoneContent::StepLoadZoneContent(std::unique_ptr entryPoint, - Zone* zone, - const int offsetBlockBitCount, - const block_t insertBlock) - : m_content_loader(std::move(entryPoint)), - m_zone(zone), - m_offset_block_bit_count(offsetBlockBitCount), - m_insert_block(insertBlock) +namespace { -} + class StepLoadZoneContent final : public ILoadingStep + { + public: + StepLoadZoneContent(std::unique_ptr entryPoint, const int offsetBlockBitCount, const block_t insertBlock) + : m_content_loader(std::move(entryPoint)), + m_offset_block_bit_count(offsetBlockBitCount), + m_insert_block(insertBlock) + { + } -void StepLoadZoneContent::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + const auto inputStream = ZoneInputStream::Create(zoneLoader.m_blocks, stream, m_offset_block_bit_count, m_insert_block); + + m_content_loader->Load(*inputStream); + } + + private: + std::unique_ptr m_content_loader; + int m_offset_block_bit_count; + block_t m_insert_block; + }; +} // namespace + +namespace step { - const auto inputStream = ZoneInputStream::Create(zoneLoader.m_blocks, stream, m_offset_block_bit_count, m_insert_block); - - m_content_loader->Load(*inputStream); -} + std::unique_ptr + CreateStepLoadZoneContent(std::unique_ptr entryPoint, const int offsetBlockBitCount, const block_t insertBlock) + { + return std::make_unique(std::move(entryPoint), offsetBlockBitCount, insertBlock); + } +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.h b/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.h index 7fe26282..2e1c2614 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.h +++ b/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.h @@ -5,16 +5,8 @@ #include -class StepLoadZoneContent final : public ILoadingStep +namespace step { -public: - StepLoadZoneContent(std::unique_ptr entryPoint, Zone* zone, int offsetBlockBitCount, block_t insertBlock); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - std::unique_ptr m_content_loader; - Zone* m_zone; - int m_offset_block_bit_count; - block_t m_insert_block; -}; + std::unique_ptr + CreateStepLoadZoneContent(std::unique_ptr entryPoint, int offsetBlockBitCount, block_t insertBlock); +} diff --git a/src/ZoneLoading/Loading/Steps/StepLoadZoneSizes.cpp b/src/ZoneLoading/Loading/Steps/StepLoadZoneSizes.cpp index 9f13f255..6cd9abec 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadZoneSizes.cpp +++ b/src/ZoneLoading/Loading/Steps/StepLoadZoneSizes.cpp @@ -1,23 +1,42 @@ #include "StepLoadZoneSizes.h" -StepLoadZoneSizes::StepLoadZoneSizes() - : m_size(0), - m_external_size(0) +namespace { -} + class StepLoadZoneSizes final : public step::IStepLoadZoneSizes + { + public: + StepLoadZoneSizes() + : m_size(0), + m_external_size(0) + { + } -void StepLoadZoneSizes::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) -{ - stream.Load(&m_size, sizeof(m_size)); - stream.Load(&m_external_size, sizeof(m_external_size)); -} + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + stream.Load(&m_size, sizeof(m_size)); + stream.Load(&m_external_size, sizeof(m_external_size)); + } -size_t StepLoadZoneSizes::GetSize() const -{ - return m_size; -} + [[nodiscard]] uint32_t GetSize() const override + { + return m_size; + } -size_t StepLoadZoneSizes::GetExternalSize() const + [[nodiscard]] uint32_t GetExternalSize() const override + { + return m_external_size; + } + + private: + uint32_t m_size; + uint32_t m_external_size; + }; +} // namespace + +namespace step { - return m_external_size; -} + std::unique_ptr CreateStepLoadZoneSizes() + { + return std::make_unique(); + } +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepLoadZoneSizes.h b/src/ZoneLoading/Loading/Steps/StepLoadZoneSizes.h index efb9643f..5c7eb74a 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadZoneSizes.h +++ b/src/ZoneLoading/Loading/Steps/StepLoadZoneSizes.h @@ -2,19 +2,16 @@ #include "Loading/ILoadingStep.h" -#include +#include -class StepLoadZoneSizes final : public ILoadingStep +namespace step { -public: - StepLoadZoneSizes(); + class IStepLoadZoneSizes : public ILoadingStep + { + public: + [[nodiscard]] virtual uint32_t GetSize() const = 0; + [[nodiscard]] virtual uint32_t GetExternalSize() const = 0; + }; - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - - [[nodiscard]] size_t GetSize() const; - [[nodiscard]] size_t GetExternalSize() const; - -private: - size_t m_size; - size_t m_external_size; -}; + std::unique_ptr CreateStepLoadZoneSizes(); +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepRemoveProcessor.cpp b/src/ZoneLoading/Loading/Steps/StepRemoveProcessor.cpp index 8ceeb800..dee13b8b 100644 --- a/src/ZoneLoading/Loading/Steps/StepRemoveProcessor.cpp +++ b/src/ZoneLoading/Loading/Steps/StepRemoveProcessor.cpp @@ -2,14 +2,32 @@ #include -StepRemoveProcessor::StepRemoveProcessor(StreamProcessor* streamProcessor) - : m_stream_processor(streamProcessor) +namespace { -} + class StepRemoveProcessor final : public ILoadingStep + { + public: + explicit StepRemoveProcessor(const StreamProcessor* streamProcessor) + : m_stream_processor(streamProcessor) + { + } -void StepRemoveProcessor::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + assert(m_stream_processor != nullptr); + + zoneLoader.RemoveStreamProcessor(m_stream_processor); + } + + private: + const StreamProcessor* m_stream_processor; + }; +} // namespace + +namespace step { - assert(m_stream_processor != nullptr); - - zoneLoader.RemoveStreamProcessor(m_stream_processor); -} + std::unique_ptr CreateStepRemoveProcessor(const StreamProcessor* streamProcessor) + { + return std::make_unique(streamProcessor); + } +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepRemoveProcessor.h b/src/ZoneLoading/Loading/Steps/StepRemoveProcessor.h index 9cb1ff77..fef6acf3 100644 --- a/src/ZoneLoading/Loading/Steps/StepRemoveProcessor.h +++ b/src/ZoneLoading/Loading/Steps/StepRemoveProcessor.h @@ -1,14 +1,11 @@ #pragma once #include "Loading/ILoadingStep.h" +#include "Loading/StreamProcessor.h" -class StepRemoveProcessor final : public ILoadingStep +#include + +namespace step { -public: - explicit StepRemoveProcessor(StreamProcessor* streamProcessor); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - StreamProcessor* m_stream_processor; -}; + std::unique_ptr CreateStepRemoveProcessor(const StreamProcessor* streamProcessor); +} diff --git a/src/ZoneLoading/Loading/Steps/StepSkipBytes.cpp b/src/ZoneLoading/Loading/Steps/StepSkipBytes.cpp index e963e096..b2874f54 100644 --- a/src/ZoneLoading/Loading/Steps/StepSkipBytes.cpp +++ b/src/ZoneLoading/Loading/Steps/StepSkipBytes.cpp @@ -1,30 +1,48 @@ #include "StepSkipBytes.h" -StepSkipBytes::StepSkipBytes(const size_t skipCount) - : m_skip_count(skipCount) +namespace { -} - -void StepSkipBytes::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) -{ - uint8_t tempBuffer[128]; - auto skippedBytes = 0uz; - - while (skippedBytes < m_skip_count) + class StepSkipBytes final : public ILoadingStep { - size_t toSkip; - - if (m_skip_count - skippedBytes < sizeof(tempBuffer)) + public: + explicit StepSkipBytes(const size_t skipCount) + : m_skip_count(skipCount) { - toSkip = m_skip_count - skippedBytes; - } - else - { - toSkip = sizeof(tempBuffer); } - stream.Load(tempBuffer, toSkip); + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + uint8_t tempBuffer[128]; + auto skippedBytes = 0uz; - skippedBytes += toSkip; + while (skippedBytes < m_skip_count) + { + size_t toSkip; + + if (m_skip_count - skippedBytes < sizeof(tempBuffer)) + { + toSkip = m_skip_count - skippedBytes; + } + else + { + toSkip = sizeof(tempBuffer); + } + + stream.Load(tempBuffer, toSkip); + + skippedBytes += toSkip; + } + } + + private: + size_t m_skip_count; + }; +} // namespace + +namespace step +{ + std::unique_ptr CreateStepSkipBytes(size_t skipCount) + { + return std::make_unique(skipCount); } -} +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepSkipBytes.h b/src/ZoneLoading/Loading/Steps/StepSkipBytes.h index d554e68d..e1800f3f 100644 --- a/src/ZoneLoading/Loading/Steps/StepSkipBytes.h +++ b/src/ZoneLoading/Loading/Steps/StepSkipBytes.h @@ -2,13 +2,9 @@ #include "Loading/ILoadingStep.h" -class StepSkipBytes final : public ILoadingStep +#include + +namespace step { -public: - explicit StepSkipBytes(size_t skipCount); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - size_t m_skip_count; -}; + std::unique_ptr CreateStepSkipBytes(size_t skipCount); +} diff --git a/src/ZoneLoading/Loading/Steps/StepVerifyFileName.cpp b/src/ZoneLoading/Loading/Steps/StepVerifyFileName.cpp index 1a70ed08..98962132 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifyFileName.cpp +++ b/src/ZoneLoading/Loading/Steps/StepVerifyFileName.cpp @@ -4,42 +4,61 @@ #include -StepVerifyFileName::StepVerifyFileName(std::string fileName, const size_t fileNameBufferSize) - : m_expected_file_name(std::move(fileName)), - m_file_name_buffer_size(fileNameBufferSize) +namespace { - if (m_expected_file_name.length() > (m_file_name_buffer_size - 1)) - m_expected_file_name.erase(m_file_name_buffer_size - 1); -} - -void StepVerifyFileName::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) -{ - std::stringstream originalFilenameStream; - unsigned bufferOffset = 0; - char c; - - for (; bufferOffset < m_file_name_buffer_size; bufferOffset++) + class StepVerifyFileName final : public ILoadingStep { - stream.Load(&c, sizeof(char)); - - if (c == '\00') + public: + StepVerifyFileName(std::string fileName, const size_t fileNameBufferSize) + : m_expected_file_name(std::move(fileName)), + m_file_name_buffer_size(fileNameBufferSize) { - bufferOffset++; - break; + if (m_expected_file_name.length() > (m_file_name_buffer_size - 1)) + m_expected_file_name.erase(m_file_name_buffer_size - 1); } - originalFilenameStream << c; - } + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + std::stringstream originalFilenameStream; + unsigned bufferOffset = 0; + char c; - // Skip the rest of the buffer which should be null bytes - while (bufferOffset < m_file_name_buffer_size) + for (; bufferOffset < m_file_name_buffer_size; bufferOffset++) + { + stream.Load(&c, sizeof(char)); + + if (c == '\00') + { + bufferOffset++; + break; + } + + originalFilenameStream << c; + } + + // Skip the rest of the buffer which should be null bytes + while (bufferOffset < m_file_name_buffer_size) + { + stream.Load(&c, sizeof(char)); + bufferOffset++; + } + + const auto originalFileName = originalFilenameStream.str(); + + if (originalFileName != m_expected_file_name) + throw InvalidFileNameException(m_expected_file_name, originalFileName); + } + + private: + std::string m_expected_file_name; + size_t m_file_name_buffer_size; + }; +} // namespace + +namespace step +{ + std::unique_ptr CreateStepVerifyFileName(std::string fileName, const size_t fileNameBufferSize) { - stream.Load(&c, sizeof(char)); - bufferOffset++; + return std::make_unique(std::move(fileName), fileNameBufferSize); } - - const auto originalFileName = originalFilenameStream.str(); - - if (originalFileName != m_expected_file_name) - throw InvalidFileNameException(m_expected_file_name, originalFileName); -} +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepVerifyFileName.h b/src/ZoneLoading/Loading/Steps/StepVerifyFileName.h index bad81584..23bda199 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifyFileName.h +++ b/src/ZoneLoading/Loading/Steps/StepVerifyFileName.h @@ -2,14 +2,10 @@ #include "Loading/ILoadingStep.h" -class StepVerifyFileName final : public ILoadingStep +#include +#include + +namespace step { -public: - explicit StepVerifyFileName(std::string fileName, size_t fileNameBufferSize); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - std::string m_expected_file_name; - size_t m_file_name_buffer_size; -}; + std::unique_ptr CreateStepVerifyFileName(std::string fileName, size_t fileNameBufferSize); +} diff --git a/src/ZoneLoading/Loading/Steps/StepVerifyHash.cpp b/src/ZoneLoading/Loading/Steps/StepVerifyHash.cpp index c9413687..e5d4fdfc 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifyHash.cpp +++ b/src/ZoneLoading/Loading/Steps/StepVerifyHash.cpp @@ -2,38 +2,61 @@ #include "Loading/Exception/InvalidHashException.h" -#include #include -StepVerifyHash::StepVerifyHash(std::unique_ptr hashFunction, - const unsigned hashIndex, - IHashProvider* hashProvider, - ICapturedDataProvider* dataProvider) - : m_hash_function(std::move(hashFunction)), - m_hash_index(hashIndex), - m_hash_provider(hashProvider), - m_data_provider(dataProvider) +namespace { -} + class StepVerifyHash final : public ILoadingStep + { + public: + StepVerifyHash(std::unique_ptr hashFunction, + const unsigned hashIndex, + IHashProvider* hashProvider, + ICapturedDataProvider* dataProvider) + : m_hash_function(std::move(hashFunction)), + m_hash_index(hashIndex), + m_hash_provider(hashProvider), + m_data_provider(dataProvider) + { + } -void StepVerifyHash::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + const uint8_t* dataToHash = nullptr; + size_t dataToHashSize = 0; + m_data_provider->GetCapturedData(&dataToHash, &dataToHashSize); + + const uint8_t* hashData = nullptr; + size_t hashSize = 0; + m_hash_provider->GetHash(m_hash_index, &hashData, &hashSize); + + if (hashSize != m_hash_function->GetHashSize()) + throw InvalidHashException(); + + const auto hashMemory = std::make_unique(m_hash_function->GetHashSize()); + m_hash_function->Init(); + m_hash_function->Process(dataToHash, dataToHashSize); + m_hash_function->Finish(hashMemory.get()); + + if (std::memcmp(hashData, hashMemory.get(), m_hash_function->GetHashSize()) != 0) + throw InvalidHashException(); + } + + private: + std::unique_ptr m_hash_function; + unsigned m_hash_index; + IHashProvider* m_hash_provider; + ICapturedDataProvider* m_data_provider; + }; +} // namespace + +namespace step { - const uint8_t* dataToHash = nullptr; - size_t dataToHashSize = 0; - m_data_provider->GetCapturedData(&dataToHash, &dataToHashSize); - - const uint8_t* hashData = nullptr; - size_t hashSize = 0; - m_hash_provider->GetHash(m_hash_index, &hashData, &hashSize); - - if (hashSize != m_hash_function->GetHashSize()) - throw InvalidHashException(); - - const auto hashMemory = std::make_unique(m_hash_function->GetHashSize()); - m_hash_function->Init(); - m_hash_function->Process(dataToHash, dataToHashSize); - m_hash_function->Finish(hashMemory.get()); - - if (std::memcmp(hashData, hashMemory.get(), m_hash_function->GetHashSize()) != 0) - throw InvalidHashException(); -} + std::unique_ptr CreateStepVerifyHash(std::unique_ptr hashFunction, + unsigned hashIndex, + IHashProvider* hashProvider, + ICapturedDataProvider* dataProvider) + { + return std::make_unique(std::move(hashFunction), hashIndex, hashProvider, dataProvider); + } +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepVerifyHash.h b/src/ZoneLoading/Loading/Steps/StepVerifyHash.h index 3f92fac6..db54fd28 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifyHash.h +++ b/src/ZoneLoading/Loading/Steps/StepVerifyHash.h @@ -7,19 +7,10 @@ #include -class StepVerifyHash final : public ILoadingStep +namespace step { -public: - StepVerifyHash(std::unique_ptr hashFunction, - unsigned hashIndex, - IHashProvider* hashProvider, - ICapturedDataProvider* dataProvider); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - std::unique_ptr m_hash_function; - unsigned m_hash_index; - IHashProvider* m_hash_provider; - ICapturedDataProvider* m_data_provider; -}; + std::unique_ptr CreateStepVerifyHash(std::unique_ptr hashFunction, + unsigned hashIndex, + IHashProvider* hashProvider, + ICapturedDataProvider* dataProvider); +} diff --git a/src/ZoneLoading/Loading/Steps/StepVerifyMagic.cpp b/src/ZoneLoading/Loading/Steps/StepVerifyMagic.cpp index ea9a6fd9..4827db28 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifyMagic.cpp +++ b/src/ZoneLoading/Loading/Steps/StepVerifyMagic.cpp @@ -4,21 +4,40 @@ #include -StepVerifyMagic::StepVerifyMagic(const char* magic) +namespace { - m_magic = magic; - m_magic_len = strlen(m_magic); -} - -void StepVerifyMagic::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) -{ - char currentCharacter; - - for (unsigned i = 0; i < m_magic_len; i++) + class StepVerifyMagic final : public ILoadingStep { - stream.Load(¤tCharacter, sizeof(char)); + public: + explicit StepVerifyMagic(const char* magic) + : m_magic(magic), + m_magic_len(strlen(m_magic)) + { + } - if (currentCharacter != m_magic[i]) - throw InvalidMagicException(m_magic); + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + char currentCharacter; + + for (unsigned i = 0; i < m_magic_len; i++) + { + stream.Load(¤tCharacter, sizeof(char)); + + if (currentCharacter != m_magic[i]) + throw InvalidMagicException(m_magic); + } + } + + private: + const char* m_magic; + size_t m_magic_len; + }; +} // namespace + +namespace step +{ + std::unique_ptr CreateStepVerifyMagic(const char* magic) + { + return std::make_unique(magic); } -} +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepVerifyMagic.h b/src/ZoneLoading/Loading/Steps/StepVerifyMagic.h index 1e2834cb..206ab1b9 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifyMagic.h +++ b/src/ZoneLoading/Loading/Steps/StepVerifyMagic.h @@ -2,14 +2,9 @@ #include "Loading/ILoadingStep.h" -class StepVerifyMagic final : public ILoadingStep +#include + +namespace step { -public: - explicit StepVerifyMagic(const char* magic); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - const char* m_magic; - size_t m_magic_len; -}; + std::unique_ptr CreateStepVerifyMagic(const char* magic); +} diff --git a/src/ZoneLoading/Loading/Steps/StepVerifySignature.cpp b/src/ZoneLoading/Loading/Steps/StepVerifySignature.cpp index 3a1126d4..6fb7780f 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifySignature.cpp +++ b/src/ZoneLoading/Loading/Steps/StepVerifySignature.cpp @@ -4,31 +4,53 @@ #include -StepVerifySignature::StepVerifySignature(std::unique_ptr signatureAlgorithm, - ISignatureProvider* signatureProvider, - ICapturedDataProvider* signatureDataProvider) - : m_algorithm(std::move(signatureAlgorithm)), - m_signature_provider(signatureProvider), - m_signature_data_provider(signatureDataProvider) +namespace { -} - -void StepVerifySignature::PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) -{ - assert(m_algorithm != nullptr); - assert(m_signature_provider != nullptr); - assert(m_signature_data_provider != nullptr); - - const uint8_t* signature; - size_t signatureSize; - m_signature_provider->GetSignature(&signature, &signatureSize); - - const uint8_t* signatureData; - size_t signatureDataSize; - m_signature_data_provider->GetCapturedData(&signatureData, &signatureDataSize); - - if (!m_algorithm->Verify(signatureData, signatureDataSize, signature, signatureSize)) + class StepVerifySignature final : public ILoadingStep { - throw InvalidSignatureException(); + public: + StepVerifySignature(std::unique_ptr signatureAlgorithm, + ISignatureProvider* signatureProvider, + ICapturedDataProvider* signatureDataProvider) + : m_algorithm(std::move(signatureAlgorithm)), + m_signature_provider(signatureProvider), + m_signature_data_provider(signatureDataProvider) + { + } + + void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override + { + assert(m_algorithm != nullptr); + assert(m_signature_provider != nullptr); + assert(m_signature_data_provider != nullptr); + + const uint8_t* signature; + size_t signatureSize; + m_signature_provider->GetSignature(&signature, &signatureSize); + + const uint8_t* signatureData; + size_t signatureDataSize; + m_signature_data_provider->GetCapturedData(&signatureData, &signatureDataSize); + + if (!m_algorithm->Verify(signatureData, signatureDataSize, signature, signatureSize)) + { + throw InvalidSignatureException(); + } + } + + private: + std::unique_ptr m_algorithm; + ISignatureProvider* m_signature_provider; + ICapturedDataProvider* m_signature_data_provider; + }; +} // namespace + +namespace step +{ + std::unique_ptr CreateStepVerifySignature(std::unique_ptr signatureAlgorithm, + ISignatureProvider* signatureProvider, + ICapturedDataProvider* signatureDataProvider) + { + return std::make_unique(std::move(signatureAlgorithm), signatureProvider, signatureDataProvider); } -} +} // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepVerifySignature.h b/src/ZoneLoading/Loading/Steps/StepVerifySignature.h index c963640d..7d6f6abc 100644 --- a/src/ZoneLoading/Loading/Steps/StepVerifySignature.h +++ b/src/ZoneLoading/Loading/Steps/StepVerifySignature.h @@ -5,17 +5,11 @@ #include "Loading/ISignatureProvider.h" #include "Utils/ICapturedDataProvider.h" -class StepVerifySignature final : public ILoadingStep +#include + +namespace step { -public: - StepVerifySignature(std::unique_ptr signatureAlgorithm, - ISignatureProvider* signatureProvider, - ICapturedDataProvider* signatureDataProvider); - - void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override; - -private: - std::unique_ptr m_algorithm; - ISignatureProvider* m_signature_provider; - ICapturedDataProvider* m_signature_data_provider; -}; + std::unique_ptr CreateStepVerifySignature(std::unique_ptr signatureAlgorithm, + ISignatureProvider* signatureProvider, + ICapturedDataProvider* signatureDataProvider); +}