From 5eee31f69a7b90703fcb4b7f67bf44f373b2b563 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 5 Feb 2017 12:18:07 +0000 Subject: [PATCH] Use SawyerChunkReader instead of SawyerEncoding --- src/openrct2/FileClassifier.cpp | 11 ++++--- src/openrct2/object/ObjectRepository.cpp | 18 ++++------- src/openrct2/rct12/SawyerChunkReader.cpp | 1 - src/openrct2/rct12/SawyerChunkReader.h | 13 ++++++++ src/openrct2/scenario/ScenarioRepository.cpp | 32 ++++++++------------ 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/openrct2/FileClassifier.cpp b/src/openrct2/FileClassifier.cpp index 897328a662..b1d978f641 100644 --- a/src/openrct2/FileClassifier.cpp +++ b/src/openrct2/FileClassifier.cpp @@ -16,7 +16,7 @@ #include "core/FileStream.hpp" #include "FileClassifier.h" -#include "rct12/SawyerEncoding.h" +#include "rct12/SawyerChunkReader.h" extern "C" { @@ -71,9 +71,10 @@ bool TryClassifyFile(IStream * stream, ClassifiedFile * result) static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result) { - rct_s6_header s6Header; - if (SawyerEncoding::TryReadChunk(&s6Header, stream)) + try { + auto chunkReader = SawyerChunkReader(stream); + auto s6Header = chunkReader.ReadChunkAs(); if (s6Header.type == S6_TYPE_SAVEDGAME) { result->Type = FILE_TYPE::SAVED_GAME; @@ -85,7 +86,9 @@ static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result) result->Version = s6Header.version; return true; } - + catch (Exception) + { + } return false; } diff --git a/src/openrct2/object/ObjectRepository.cpp b/src/openrct2/object/ObjectRepository.cpp index 4486075fa1..c498ce4367 100644 --- a/src/openrct2/object/ObjectRepository.cpp +++ b/src/openrct2/object/ObjectRepository.cpp @@ -31,7 +31,7 @@ #include "../core/Stopwatch.hpp" #include "../core/String.hpp" #include "../PlatformEnvironment.h" -#include "../rct12/SawyerEncoding.h" +#include "../rct12/SawyerChunkReader.h" #include "../scenario/ScenarioRepository.h" #include "Object.h" #include "ObjectFactory.h" @@ -226,25 +226,19 @@ public: bool TryExportPackedObject(IStream * stream) override { + auto chunkReader = SawyerChunkReader(stream); + // Check if we already have this object rct_object_entry entry = stream->ReadValue(); if (FindObject(&entry) != nullptr) { - SawyerEncoding::SkipChunk(stream); + chunkReader.SkipChunk(); } else { // Read object and save to new file - size_t chunkSize; - void * chunk = SawyerEncoding::ReadChunk(stream, &chunkSize); - if (chunk == nullptr) - { - log_error("Failed to reallocate buffer for packed object."); - return false; - } - - AddObject(&entry, chunk, chunkSize); - Memory::Free(chunk); + std::shared_ptr chunk = chunkReader.ReadChunk(); + AddObject(&entry, chunk->GetData(), chunk->GetLength()); } return true; } diff --git a/src/openrct2/rct12/SawyerChunkReader.cpp b/src/openrct2/rct12/SawyerChunkReader.cpp index a76d25cd46..62fbe3326a 100644 --- a/src/openrct2/rct12/SawyerChunkReader.cpp +++ b/src/openrct2/rct12/SawyerChunkReader.cpp @@ -18,7 +18,6 @@ #include "../core/IStream.hpp" #include "../core/Math.hpp" #include "SawyerChunkReader.h" -#include "SawyerEncoding.h" extern "C" { diff --git a/src/openrct2/rct12/SawyerChunkReader.h b/src/openrct2/rct12/SawyerChunkReader.h index 4448f1e816..3217feb0b2 100644 --- a/src/openrct2/rct12/SawyerChunkReader.h +++ b/src/openrct2/rct12/SawyerChunkReader.h @@ -83,4 +83,17 @@ public: * @param length The size of the destination buffer. */ void ReadChunk(void * dst, size_t length); + + /** + * Reads the next chunk from the stream into a buffer returned as the + * specified type. If the chunk is smaller than the size of the type + * then the remaining space is padded with zero. + */ + template + T ReadChunkAs() + { + T result; + ReadChunk(&result, sizeof(result)); + return result; + } }; diff --git a/src/openrct2/scenario/ScenarioRepository.cpp b/src/openrct2/scenario/ScenarioRepository.cpp index a5f394ed16..a060df524e 100644 --- a/src/openrct2/scenario/ScenarioRepository.cpp +++ b/src/openrct2/scenario/ScenarioRepository.cpp @@ -26,7 +26,7 @@ #include "../core/Util.hpp" #include "../ParkImporter.h" #include "../PlatformEnvironment.h" -#include "../rct12/SawyerEncoding.h" +#include "../rct12/SawyerChunkReader.h" #include "ScenarioRepository.h" #include "ScenarioSources.h" @@ -336,26 +336,18 @@ private: { // RCT2 scenario auto fs = FileStream(path, FILE_MODE_OPEN); - rct_s6_header header; - if (SawyerEncoding::TryReadChunk(&header, &fs)) + auto chunkReader = SawyerChunkReader(&fs); + + rct_s6_header header = chunkReader.ReadChunkAs(); + if (header.type == S6_TYPE_SCENARIO) { - if (header.type == S6_TYPE_SCENARIO) - { - rct_s6_info info; - if (SawyerEncoding::TryReadChunk(&info, &fs)) - { - *entry = CreateNewScenarioEntry(path, timestamp, &info); - return true; - } - else - { - throw new IOException("Unable to read S6_INFO chunk."); - } - } - else - { - log_verbose("%s is not a scenario", path.c_str()); - } + rct_s6_info info = chunkReader.ReadChunkAs(); + *entry = CreateNewScenarioEntry(path, timestamp, &info); + return true; + } + else + { + log_verbose("%s is not a scenario", path.c_str()); } } }