1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-16 11:33:03 +01:00

Use SawyerChunkReader instead of SawyerEncoding

This commit is contained in:
Ted John
2017-02-05 12:18:07 +00:00
parent 7ab2723936
commit 5eee31f69a
5 changed files with 38 additions and 37 deletions

View File

@@ -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<rct_s6_header>();
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;
}

View File

@@ -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<rct_object_entry>();
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<SawyerChunk> chunk = chunkReader.ReadChunk();
AddObject(&entry, chunk->GetData(), chunk->GetLength());
}
return true;
}

View File

@@ -18,7 +18,6 @@
#include "../core/IStream.hpp"
#include "../core/Math.hpp"
#include "SawyerChunkReader.h"
#include "SawyerEncoding.h"
extern "C"
{

View File

@@ -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<typename T>
T ReadChunkAs()
{
T result;
ReadChunk(&result, sizeof(result));
return result;
}
};

View File

@@ -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<rct_s6_header>();
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<rct_s6_info>();
*entry = CreateNewScenarioEntry(path, timestamp, &info);
return true;
}
else
{
log_verbose("%s is not a scenario", path.c_str());
}
}
}