From 301f6e3e7af3fcc3355d7049cc9ded9cc8ae2bb0 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 14 Mar 2021 12:34:50 +0100 Subject: [PATCH] Use unique_ptr for ZoneLoading --- src/ZoneCommon/Zone/ZoneMemory.cpp | 13 +--- src/ZoneCommon/Zone/ZoneMemory.h | 9 +-- .../Game/IW4/ZoneLoaderFactoryIW4.cpp | 61 ++++++++++--------- .../Game/T6/ZoneLoaderFactoryT6.cpp | 35 +++++------ src/ZoneLoading/Loading/IZoneLoaderFactory.h | 5 ++ .../Loading/Processor/ProcessorXChunks.cpp | 37 ++++------- .../Loading/Processor/ProcessorXChunks.h | 4 +- .../Loading/Steps/StepAddProcessor.cpp | 12 +--- .../Loading/Steps/StepAddProcessor.h | 7 ++- .../Loading/Steps/StepLoadZoneContent.cpp | 18 ++---- .../Loading/Steps/StepLoadZoneContent.h | 7 ++- src/ZoneLoading/Loading/ZoneLoader.cpp | 49 ++++++--------- src/ZoneLoading/Loading/ZoneLoader.h | 11 ++-- src/ZoneLoading/ZoneLoading.h | 3 +- 14 files changed, 118 insertions(+), 153 deletions(-) diff --git a/src/ZoneCommon/Zone/ZoneMemory.cpp b/src/ZoneCommon/Zone/ZoneMemory.cpp index 135e9f04..0b3745d2 100644 --- a/src/ZoneCommon/Zone/ZoneMemory.cpp +++ b/src/ZoneCommon/Zone/ZoneMemory.cpp @@ -3,16 +3,7 @@ ZoneMemory::ZoneMemory() = default; -ZoneMemory::~ZoneMemory() +void ZoneMemory::AddBlock(std::unique_ptr block) { - for (auto block : m_blocks) - { - delete block; - } - m_blocks.clear(); -} - -void ZoneMemory::AddBlock(XBlock* block) -{ - m_blocks.push_back(block); + m_blocks.emplace_back(std::move(block)); } diff --git a/src/ZoneCommon/Zone/ZoneMemory.h b/src/ZoneCommon/Zone/ZoneMemory.h index 7d8eb667..d5120dee 100644 --- a/src/ZoneCommon/Zone/ZoneMemory.h +++ b/src/ZoneCommon/Zone/ZoneMemory.h @@ -1,16 +1,17 @@ #pragma once +#include +#include + #include "Utils/MemoryManager.h" #include "Zone/XBlock.h" -#include class ZoneMemory : public MemoryManager { - std::vector m_blocks; + std::vector> m_blocks; public: ZoneMemory(); - ~ZoneMemory() override; - void AddBlock(XBlock* block); + void AddBlock(std::unique_ptr block); }; diff --git a/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp b/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp index ddbd6116..e170de7b 100644 --- a/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp +++ b/src/ZoneLoading/Game/IW4/ZoneLoaderFactoryIW4.cpp @@ -76,7 +76,7 @@ const size_t ZoneLoaderFactory::AUTHED_CHUNK_SIZE = 0x2000; const size_t ZoneLoaderFactory::AUTHED_CHUNK_COUNT_PER_GROUP = 256; const int ZoneLoaderFactory::OFFSET_BLOCK_BIT_COUNT = 4; -const block_t ZoneLoaderFactory::INSERT_BLOCK = IW4::XFILE_BLOCK_VIRTUAL; +const block_t ZoneLoaderFactory::INSERT_BLOCK = XFILE_BLOCK_VIRTUAL; class ZoneLoaderFactory::Impl { @@ -114,7 +114,7 @@ class ZoneLoaderFactory::Impl static void SetupBlock(ZoneLoader* zoneLoader) { -#define XBLOCK_DEF(name, type) new XBlock(STR(name), name, type) +#define XBLOCK_DEF(name, type) std::make_unique(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)); @@ -164,35 +164,39 @@ class ZoneLoaderFactory::Impl // If file is signed setup a RSA instance. IPublicKeyAlgorithm* rsa = SetupRSA(isOfficial); - zoneLoader->AddLoadingStep(new StepVerifyMagic(MAGIC_AUTH_HEADER.c_str())); - zoneLoader->AddLoadingStep(new StepSkipBytes(4)); // Skip reserved + zoneLoader->AddLoadingStep(std::make_unique(MAGIC_AUTH_HEADER.c_str())); + zoneLoader->AddLoadingStep(std::make_unique(4)); // Skip reserved - auto* subheaderHash = new StepLoadHash(sizeof IW4::DB_AuthHash::bytes, 1); - zoneLoader->AddLoadingStep(subheaderHash); + auto subHeaderHash = std::make_unique(sizeof DB_AuthHash::bytes, 1); + auto* subHeaderHashPtr = subHeaderHash.get(); + zoneLoader->AddLoadingStep(std::move(subHeaderHash)); - auto* subheaderHashSignature = new StepLoadSignature(sizeof IW4::DB_AuthSignature::bytes); - zoneLoader->AddLoadingStep(subheaderHashSignature); + auto subHeaderHashSignature = std::make_unique(sizeof DB_AuthSignature::bytes); + auto* subHeaderHashSignaturePtr = subHeaderHashSignature.get(); + zoneLoader->AddLoadingStep(std::move(subHeaderHashSignature)); - zoneLoader->AddLoadingStep(new StepVerifySignature(rsa, subheaderHashSignature, subheaderHash)); + zoneLoader->AddLoadingStep(std::make_unique(rsa, subHeaderHashSignaturePtr, subHeaderHashPtr)); - auto* subHeaderCapture = new ProcessorCaptureData(sizeof(IW4::DB_AuthSubHeader)); - zoneLoader->AddLoadingStep(new StepAddProcessor(subHeaderCapture)); + auto subHeaderCapture = std::make_unique(sizeof(DB_AuthSubHeader)); + auto* subHeaderCapturePtr = subHeaderCapture.get(); + zoneLoader->AddLoadingStep(std::make_unique(std::move(subHeaderCapture))); - zoneLoader->AddLoadingStep(new StepVerifyFileName(fileName, sizeof IW4::DB_AuthSubHeader::fastfileName)); - zoneLoader->AddLoadingStep(new StepSkipBytes(4)); // Skip reserved - auto* masterBlockHashes = new StepLoadHash(sizeof IW4::DB_AuthHash::bytes, std::extent::value); - zoneLoader->AddLoadingStep(masterBlockHashes); + zoneLoader->AddLoadingStep(std::make_unique(fileName, sizeof(DB_AuthSubHeader::fastfileName))); + zoneLoader->AddLoadingStep(std::make_unique(4)); // Skip reserved - zoneLoader->AddLoadingStep(new StepRemoveProcessor(subHeaderCapture)); - zoneLoader->AddLoadingStep(new StepVerifyHash(std::unique_ptr(Crypto::CreateSHA256()), 0, - subheaderHash, subHeaderCapture)); + auto masterBlockHashes = std::make_unique(sizeof DB_AuthHash::bytes, std::extent::value); + auto* masterBlockHashesPtr = masterBlockHashes.get(); + zoneLoader->AddLoadingStep(std::move(masterBlockHashes)); + + zoneLoader->AddLoadingStep(std::make_unique(subHeaderCapturePtr)); + zoneLoader->AddLoadingStep(std::make_unique(std::unique_ptr(Crypto::CreateSHA256()), 0, subHeaderHashPtr, subHeaderCapturePtr)); // Skip the rest of the first chunk - zoneLoader->AddLoadingStep(new StepSkipBytes(AUTHED_CHUNK_SIZE - sizeof(IW4::DB_AuthHeader))); + zoneLoader->AddLoadingStep(std::make_unique(AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader))); - zoneLoader->AddLoadingStep(new StepAddProcessor(new ProcessorAuthedBlocks( - AUTHED_CHUNK_COUNT_PER_GROUP, AUTHED_CHUNK_SIZE, std::extent::value, - std::unique_ptr(Crypto::CreateSHA256()), masterBlockHashes))); + zoneLoader->AddLoadingStep(std::make_unique(std::make_unique( + AUTHED_CHUNK_COUNT_PER_GROUP, AUTHED_CHUNK_SIZE, std::extent::value, + std::unique_ptr(Crypto::CreateSHA256()), masterBlockHashesPtr))); } public: @@ -216,24 +220,23 @@ public: SetupBlock(zoneLoader); // Skip unknown 1 byte field that the game ignores as well - zoneLoader->AddLoadingStep(new StepSkipBytes(1)); + zoneLoader->AddLoadingStep(std::make_unique(1)); // Skip timestamp - zoneLoader->AddLoadingStep(new StepSkipBytes(8)); + zoneLoader->AddLoadingStep(std::make_unique(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(new StepAddProcessor(new ProcessorInflate(AUTHED_CHUNK_SIZE))); + zoneLoader->AddLoadingStep(std::make_unique(std::make_unique(AUTHED_CHUNK_SIZE))); // Start of the XFile struct - zoneLoader->AddLoadingStep(new StepSkipBytes(8)); + zoneLoader->AddLoadingStep(std::make_unique(8)); // Skip size and externalSize fields since they are not interesting for us - zoneLoader->AddLoadingStep(new StepAllocXBlocks()); + zoneLoader->AddLoadingStep(std::make_unique()); // Start of the zone content - zoneLoader->AddLoadingStep( - new StepLoadZoneContent(new ContentLoader(), zone, OFFSET_BLOCK_BIT_COUNT, INSERT_BLOCK)); + zoneLoader->AddLoadingStep(std::make_unique(std::make_unique(), zone, OFFSET_BLOCK_BIT_COUNT, INSERT_BLOCK)); // Return the fully setup zoneloader return zoneLoader; diff --git a/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp b/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp index 4cdf87e2..d367a57c 100644 --- a/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp +++ b/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp @@ -151,7 +151,7 @@ class ZoneLoaderFactory::Impl static void SetupBlock(ZoneLoader* zoneLoader) { -#define XBLOCK_DEF(name, type) new XBlock(STR(name), name, type) +#define XBLOCK_DEF(name, type) std::make_unique(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)); @@ -196,32 +196,33 @@ class ZoneLoaderFactory::Impl if(!isSecure) return nullptr; - zoneLoader->AddLoadingStep(new StepVerifyMagic(MAGIC_AUTH_HEADER.c_str())); - zoneLoader->AddLoadingStep(new StepSkipBytes(4)); // Loading Flags which are always zero - zoneLoader->AddLoadingStep(new StepVerifyFileName(fileName, 32)); + zoneLoader->AddLoadingStep(std::make_unique(MAGIC_AUTH_HEADER.c_str())); + zoneLoader->AddLoadingStep(std::make_unique(4)); // Loading Flags which are always zero + zoneLoader->AddLoadingStep(std::make_unique(fileName, 32)); - auto* signatureLoadStep = new StepLoadSignature(256); - zoneLoader->AddLoadingStep(signatureLoadStep); + auto signatureLoadStep = std::make_unique(256); + auto* signatureLoadStepPtr = signatureLoadStep.get(); + zoneLoader->AddLoadingStep(std::move(signatureLoadStep)); - return signatureLoadStep; + return signatureLoadStepPtr; } static ICapturedDataProvider* AddXChunkProcessor(bool isEncrypted, ZoneLoader* zoneLoader, std::string& fileName) { ICapturedDataProvider* result = nullptr; - auto* xChunkProcessor = new ProcessorXChunks(STREAM_COUNT, XCHUNK_SIZE, VANILLA_BUFFER_SIZE); + auto xChunkProcessor = std::make_unique(STREAM_COUNT, XCHUNK_SIZE, VANILLA_BUFFER_SIZE); if(isEncrypted) { // If zone is encrypted, the decryption is applied before the decompression. T6 Zones always use Salsa20. - auto* chunkProcessorSalsa20 = new ChunkProcessorSalsa20(STREAM_COUNT, fileName, SALSA20_KEY_TREYARCH, sizeof(SALSA20_KEY_TREYARCH)); - xChunkProcessor->AddChunkProcessor(chunkProcessorSalsa20); - result = chunkProcessorSalsa20; + auto chunkProcessorSalsa20 = std::make_unique(STREAM_COUNT, fileName, SALSA20_KEY_TREYARCH, sizeof(SALSA20_KEY_TREYARCH)); + result = chunkProcessorSalsa20.get(); + xChunkProcessor->AddChunkProcessor(std::move(chunkProcessorSalsa20)); } // Decompress the chunks using zlib - xChunkProcessor->AddChunkProcessor(new ChunkProcessorInflate()); - zoneLoader->AddLoadingStep(new StepAddProcessor(xChunkProcessor)); + xChunkProcessor->AddChunkProcessor(std::make_unique()); + zoneLoader->AddLoadingStep(std::make_unique(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; @@ -258,15 +259,15 @@ public: ICapturedDataProvider* signatureDataProvider = AddXChunkProcessor(isEncrypted, zoneLoader, fileName); // Start of the XFile struct - zoneLoader->AddLoadingStep(new StepSkipBytes(8)); // Skip size and externalSize fields since they are not interesting for us - zoneLoader->AddLoadingStep(new StepAllocXBlocks()); + zoneLoader->AddLoadingStep(std::make_unique(8)); // Skip size and externalSize fields since they are not interesting for us + zoneLoader->AddLoadingStep(std::make_unique()); // Start of the zone content - zoneLoader->AddLoadingStep(new StepLoadZoneContent(new ContentLoader(), zone, OFFSET_BLOCK_BIT_COUNT, INSERT_BLOCK)); + zoneLoader->AddLoadingStep(std::make_unique(std::make_unique(), zone, OFFSET_BLOCK_BIT_COUNT, INSERT_BLOCK)); if(isSecure) { - zoneLoader->AddLoadingStep(new StepVerifySignature(rsa, signatureProvider, signatureDataProvider)); + zoneLoader->AddLoadingStep(std::make_unique(rsa, signatureProvider, signatureDataProvider)); } // Return the fully setup zoneloader diff --git a/src/ZoneLoading/Loading/IZoneLoaderFactory.h b/src/ZoneLoading/Loading/IZoneLoaderFactory.h index 15515e0a..7cd03092 100644 --- a/src/ZoneLoading/Loading/IZoneLoaderFactory.h +++ b/src/ZoneLoading/Loading/IZoneLoaderFactory.h @@ -6,7 +6,12 @@ class IZoneLoaderFactory { public: + IZoneLoaderFactory() = default; virtual ~IZoneLoaderFactory() = default; + IZoneLoaderFactory(const IZoneLoaderFactory& other) = default; + IZoneLoaderFactory(IZoneLoaderFactory&& other) noexcept = default; + IZoneLoaderFactory& operator=(const IZoneLoaderFactory& other) = default; + IZoneLoaderFactory& operator=(IZoneLoaderFactory&& other) noexcept = default; virtual ZoneLoader* CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) = 0; }; \ No newline at end of file diff --git a/src/ZoneLoading/Loading/Processor/ProcessorXChunks.cpp b/src/ZoneLoading/Loading/Processor/ProcessorXChunks.cpp index 9c1f9772..509d212a 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorXChunks.cpp +++ b/src/ZoneLoading/Loading/Processor/ProcessorXChunks.cpp @@ -26,7 +26,7 @@ class DBLoadStream std::condition_variable m_loading_finished; std::thread m_load_thread; - std::vector& m_processors; + std::vector>& m_processors; void Load() { @@ -34,7 +34,7 @@ class DBLoadStream bool firstProcessor = true; - for (auto processor : m_processors) + for (const auto& processor : m_processors) { if (!firstProcessor) { @@ -57,7 +57,7 @@ class DBLoadStream public: DBLoadStream(const int streamIndex, const size_t chunkSize, - std::vector& chunkProcessors) : m_processors(chunkProcessors) + std::vector>& chunkProcessors) : m_processors(chunkProcessors) { m_index = streamIndex; m_chunk_size = chunkSize; @@ -127,10 +127,10 @@ class ProcessorXChunks::ProcessorXChunksImpl { ProcessorXChunks* m_base; - std::vector m_streams; + std::vector> m_streams; size_t m_chunk_size; size_t m_vanilla_buffer_size; - std::vector m_chunk_processors; + std::vector> m_chunk_processors; bool m_initialized_streams; unsigned int m_current_stream; @@ -175,7 +175,7 @@ class ProcessorXChunks::ProcessorXChunksImpl throw InvalidChunkSizeException(chunkSize, m_chunk_size); } - auto* stream = m_streams[streamNum]; + const auto& stream = m_streams[streamNum]; const size_t loadedChunkSize = m_base->m_base_stream->Load(stream->GetInputBuffer(), chunkSize); if (loadedChunkSize != chunkSize) @@ -232,7 +232,7 @@ public: for (int streamIndex = 0; streamIndex < numStreams; streamIndex++) { - m_streams.push_back(new DBLoadStream(streamIndex, xChunkSize, m_chunk_processors)); + m_streams.emplace_back(std::make_unique(streamIndex, xChunkSize, m_chunk_processors)); } m_chunk_size = xChunkSize; @@ -255,26 +255,11 @@ public: m_vanilla_buffer_size = vanillaBufferSize; } - ~ProcessorXChunksImpl() - { - for (auto* stream : m_streams) - { - delete stream; - } - m_streams.clear(); - - for (auto* processor : m_chunk_processors) - { - delete processor; - } - m_chunk_processors.clear(); - } - - void AddChunkProcessor(IXChunkProcessor* streamProcessor) + void AddChunkProcessor(std::unique_ptr streamProcessor) { assert(streamProcessor != nullptr); - m_chunk_processors.push_back(streamProcessor); + m_chunk_processors.emplace_back(std::move(streamProcessor)); } size_t Load(void* buffer, const size_t length) @@ -339,9 +324,9 @@ ProcessorXChunks::~ProcessorXChunks() m_impl = nullptr; } -void ProcessorXChunks::AddChunkProcessor(IXChunkProcessor* chunkProcessor) const +void ProcessorXChunks::AddChunkProcessor(std::unique_ptr chunkProcessor) const { - m_impl->AddChunkProcessor(chunkProcessor); + m_impl->AddChunkProcessor(std::move(chunkProcessor)); } size_t ProcessorXChunks::Load(void* buffer, const size_t length) diff --git a/src/ZoneLoading/Loading/Processor/ProcessorXChunks.h b/src/ZoneLoading/Loading/Processor/ProcessorXChunks.h index b8a7cced..1f2df3e3 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorXChunks.h +++ b/src/ZoneLoading/Loading/Processor/ProcessorXChunks.h @@ -1,4 +1,6 @@ #pragma once +#include + #include "Loading/StreamProcessor.h" #include "XChunks/IXChunkProcessor.h" @@ -15,5 +17,5 @@ public: size_t Load(void* buffer, size_t length) override; int64_t Pos() override; - void AddChunkProcessor(IXChunkProcessor* chunkProcessor) const; + void AddChunkProcessor(std::unique_ptr chunkProcessor) const; }; diff --git a/src/ZoneLoading/Loading/Steps/StepAddProcessor.cpp b/src/ZoneLoading/Loading/Steps/StepAddProcessor.cpp index 5901f283..9c8d23d9 100644 --- a/src/ZoneLoading/Loading/Steps/StepAddProcessor.cpp +++ b/src/ZoneLoading/Loading/Steps/StepAddProcessor.cpp @@ -1,15 +1,9 @@ #include "StepAddProcessor.h" #include -StepAddProcessor::StepAddProcessor(StreamProcessor* streamProcessor) +StepAddProcessor::StepAddProcessor(std::unique_ptr streamProcessor) + : m_stream_processor(std::move(streamProcessor)) { - m_stream_processor = streamProcessor; -} - -StepAddProcessor::~StepAddProcessor() -{ - delete m_stream_processor; - m_stream_processor = nullptr; } void StepAddProcessor::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream) @@ -17,6 +11,6 @@ void StepAddProcessor::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* strea assert(zoneLoader != nullptr); assert(m_stream_processor != nullptr); - zoneLoader->AddStreamProcessor(m_stream_processor); + zoneLoader->AddStreamProcessor(std::move(m_stream_processor)); m_stream_processor = nullptr; } \ No newline at end of file diff --git a/src/ZoneLoading/Loading/Steps/StepAddProcessor.h b/src/ZoneLoading/Loading/Steps/StepAddProcessor.h index 589d44cf..72bf92b4 100644 --- a/src/ZoneLoading/Loading/Steps/StepAddProcessor.h +++ b/src/ZoneLoading/Loading/Steps/StepAddProcessor.h @@ -1,14 +1,15 @@ #pragma once +#include + #include "Loading/ILoadingStep.h" class StepAddProcessor final : public ILoadingStep { - StreamProcessor* m_stream_processor; + std::unique_ptr m_stream_processor; public: - explicit StepAddProcessor(StreamProcessor* streamProcessor); - ~StepAddProcessor() override; + explicit StepAddProcessor(std::unique_ptr streamProcessor); void PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream) override; }; diff --git a/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.cpp b/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.cpp index 04176c5e..16360fe6 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.cpp +++ b/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.cpp @@ -2,18 +2,12 @@ #include "Zone/Stream/Impl/XBlockInputStream.h" #include -StepLoadZoneContent::StepLoadZoneContent(IContentLoadingEntryPoint* entryPoint, Zone* zone, const int offsetBlockBitCount, const block_t insertBlock) +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) { - m_content_loader = entryPoint; - m_zone = zone; - m_offset_block_bit_count = offsetBlockBitCount; - m_insert_block = insertBlock; -} - -StepLoadZoneContent::~StepLoadZoneContent() -{ - delete m_content_loader; - m_content_loader = nullptr; } void StepLoadZoneContent::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream) @@ -23,4 +17,4 @@ void StepLoadZoneContent::PerformStep(ZoneLoader* zoneLoader, ILoadingStream* st m_content_loader->Load(m_zone, inputStream); delete inputStream; -} \ No newline at end of file +} diff --git a/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.h b/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.h index 349c05d6..d0c5bcbe 100644 --- a/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.h +++ b/src/ZoneLoading/Loading/Steps/StepLoadZoneContent.h @@ -1,18 +1,19 @@ #pragma once +#include + #include "Loading/ILoadingStep.h" #include "Loading/IContentLoadingEntryPoint.h" class StepLoadZoneContent final : public ILoadingStep { - IContentLoadingEntryPoint* m_content_loader; + std::unique_ptr m_content_loader; Zone* m_zone; int m_offset_block_bit_count; block_t m_insert_block; public: - StepLoadZoneContent(IContentLoadingEntryPoint* entryPoint, Zone* zone, int offsetBlockBitCount, block_t insertBlock); - ~StepLoadZoneContent(); + StepLoadZoneContent(std::unique_ptr entryPoint, Zone* zone, int offsetBlockBitCount, block_t insertBlock); void PerformStep(ZoneLoader* zoneLoader, ILoadingStream* stream) override; }; diff --git a/src/ZoneLoading/Loading/ZoneLoader.cpp b/src/ZoneLoading/Loading/ZoneLoader.cpp index 1f5669c8..2853dc65 100644 --- a/src/ZoneLoading/Loading/ZoneLoader.cpp +++ b/src/ZoneLoading/Loading/ZoneLoader.cpp @@ -10,67 +10,54 @@ ZoneLoader::ZoneLoader(Zone* zone) m_processor_chain_dirty = false; } -ZoneLoader::~ZoneLoader() -{ - for(auto* step : m_steps) - { - delete step; - } - m_steps.clear(); - - for(auto* processor : m_processors) - { - delete processor; - } - m_processors.clear(); -} - ILoadingStream* ZoneLoader::BuildLoadingChain(ILoadingStream* rootStream) { auto* currentStream = rootStream; - for(auto* processor : m_processors) + for(const auto& processor : m_processors) { processor->SetBaseStream(currentStream); - currentStream = processor; + currentStream = processor.get(); } m_processor_chain_dirty = false; return currentStream; } -void ZoneLoader::AddXBlock(XBlock* block) +void ZoneLoader::AddXBlock(std::unique_ptr block) { - m_blocks.push_back(block); + m_blocks.push_back(block.get()); std::sort(m_blocks.begin(), m_blocks.end(), [](XBlock* b1, XBlock* b2) -> bool { return b1->m_index < b2->m_index; }); - m_zone->GetMemory()->AddBlock(block); + m_zone->GetMemory()->AddBlock(std::move(block)); } -void ZoneLoader::AddLoadingStep(ILoadingStep* step) +void ZoneLoader::AddLoadingStep(std::unique_ptr step) { - m_steps.push_back(step); + m_steps.emplace_back(std::move(step)); } -void ZoneLoader::AddStreamProcessor(StreamProcessor* streamProcessor) +void ZoneLoader::AddStreamProcessor(std::unique_ptr streamProcessor) { - m_processors.push_back(streamProcessor); + m_processors.push_back(std::move(streamProcessor)); m_processor_chain_dirty = true; } void ZoneLoader::RemoveStreamProcessor(StreamProcessor* streamProcessor) { - const auto foundEntry = std::find(m_processors.begin(), m_processors.end(), streamProcessor); - - if(foundEntry != m_processors.end()) + for(auto i = m_processors.begin(); i < m_processors.end(); ++i) { - m_processors.erase(foundEntry); - m_processor_chain_dirty = true; + if(i->get() == streamProcessor) + { + m_processors.erase(i); + m_processor_chain_dirty = true; + break; + } } } @@ -81,7 +68,7 @@ Zone* ZoneLoader::LoadZone(std::istream& stream) try { - for(auto* step : m_steps) + for(const auto& step : m_steps) { step->PerformStep(this, endStream); @@ -93,7 +80,7 @@ Zone* ZoneLoader::LoadZone(std::istream& stream) } catch (LoadingException& e) { - const std::string detailedMessage = e.DetailedMessage(); + const auto detailedMessage = e.DetailedMessage(); printf("Loading fastfile failed: %s\n", detailedMessage.c_str()); delete m_zone; diff --git a/src/ZoneLoading/Loading/ZoneLoader.h b/src/ZoneLoading/Loading/ZoneLoader.h index f9d2d642..5a721906 100644 --- a/src/ZoneLoading/Loading/ZoneLoader.h +++ b/src/ZoneLoading/Loading/ZoneLoader.h @@ -12,8 +12,8 @@ class ILoadingStep; class ZoneLoader { - std::vector m_steps; - std::vector m_processors; + std::vector> m_steps; + std::vector> m_processors; bool m_processor_chain_dirty; @@ -25,11 +25,10 @@ public: std::vector m_blocks; explicit ZoneLoader(Zone* zone); - ~ZoneLoader(); - void AddXBlock(XBlock* block); - void AddLoadingStep(ILoadingStep* step); - void AddStreamProcessor(StreamProcessor* streamProcessor); + void AddXBlock(std::unique_ptr block); + void AddLoadingStep(std::unique_ptr step); + void AddStreamProcessor(std::unique_ptr streamProcessor); void RemoveStreamProcessor(StreamProcessor* streamProcessor); diff --git a/src/ZoneLoading/ZoneLoading.h b/src/ZoneLoading/ZoneLoading.h index 293bb4b0..c99ddf33 100644 --- a/src/ZoneLoading/ZoneLoading.h +++ b/src/ZoneLoading/ZoneLoading.h @@ -1,7 +1,8 @@ #pragma once -#include "Zone/Zone.h" #include +#include "Zone/Zone.h" + class ZoneLoading { public: