Move SawyerCoding from util into core, and into its own namespace

This commit is contained in:
Aaron van Geffen committed 2024-10-29 21:59:28 +01:00
1 parent d8a5643611
commit f40da09ab8
21 files changed
+534 -524

No files matched your search

+4 -4
View File
@@ -14,12 +14,12 @@
#include "core/FileStream.h"
#include "core/Memory.hpp"
#include "core/Path.hpp"
#include "core/SawyerCoding.h"
#include "core/String.hpp"
#include "park/ParkFile.h"
#include "rct12/SawyerChunkReader.h"
#include "rct2/RCT2.h"
#include "scenario/Scenario.h"
#include "util/SawyerCoding.h"
using namespace OpenRCT2;
@@ -135,7 +135,7 @@ static bool TryClassifyAsS4(OpenRCT2::IStream* stream, ClassifiedFileInfo* resul
size_t dataLength = static_cast<size_t>(stream->GetLength());
auto data = stream->ReadArray<uint8_t>(dataLength);
stream->SetPosition(originalPosition);
int32_t fileTypeVersion = SawyerCodingDetectFileType(data.get(), dataLength);
int32_t fileTypeVersion = SawyerCoding::DetectFileType(data.get(), dataLength);
int32_t type = fileTypeVersion & FILE_TYPE_MASK;
int32_t version = fileTypeVersion & FILE_VERSION_MASK;
@@ -173,11 +173,11 @@ static bool TryClassifyAsTD4_TD6(OpenRCT2::IStream* stream, ClassifiedFileInfo*
auto data = stream->ReadArray<uint8_t>(dataLength);
stream->SetPosition(originalPosition);
if (SawyerCodingValidateTrackChecksum(data.get(), dataLength))
if (SawyerCoding::ValidateTrackChecksum(data.get(), dataLength))
{
std::unique_ptr<uint8_t, decltype(&Memory::Free<uint8_t>)> td6data(
Memory::Allocate<uint8_t>(0x10000), &Memory::Free<uint8_t>);
size_t td6len = SawyerCodingDecodeTD6(data.get(), td6data.get(), dataLength);
size_t td6len = SawyerCoding::DecodeTD6(data.get(), td6data.get(), dataLength);
if (td6data != nullptr && td6len >= 8)
{
uint8_t version = (td6data.get()[7] >> 2) & 3;
+1 -1
View File
@@ -30,6 +30,7 @@
#include "core/FileScanner.h"
#include "core/Money.hpp"
#include "core/Path.hpp"
#include "core/SawyerCoding.h"
#include "entity/EntityRegistry.h"
#include "entity/PatrolArea.h"
#include "entity/Peep.h"
@@ -59,7 +60,6 @@
#include "scripting/ScriptEngine.h"
#include "ui/UiContext.h"
#include "ui/WindowManager.h"
#include "util/SawyerCoding.h"
#include "util/Util.h"
#include "windows/Intent.h"
#include "world/Banner.h"
+428
View File
@@ -0,0 +1,428 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "SawyerCoding.h"
#include "../platform/Platform.h"
#include "../scenario/Scenario.h"
#include "Numerics.hpp"
#include <cassert>
#include <cstring>
#include <stdexcept>
namespace OpenRCT2::SawyerCoding
{
static size_t DecodeChunkRLE(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length);
static size_t DecodeChunkRLEWithSize(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length, size_t dstSize);
static size_t EncodeChunkRLE(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length);
static size_t EncodeChunkRepeat(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length);
static void EncodeChunkRotate(uint8_t* buffer, size_t length);
uint32_t CalculateChecksum(const uint8_t* buffer, size_t length)
{
uint32_t checksum = 0;
for (size_t i = 0; i < length; i++)
checksum += buffer[i];
return checksum;
}
/**
*
* rct2: 0x006762E1
*
*/
size_t WriteChunkBuffer(uint8_t* dst_file, const uint8_t* buffer, ChunkHeader chunkHeader)
{
switch (chunkHeader.encoding)
{
case CHUNK_ENCODING_NONE:
std::memcpy(dst_file, &chunkHeader, sizeof(ChunkHeader));
dst_file += sizeof(ChunkHeader);
std::memcpy(dst_file, buffer, chunkHeader.length);
// fwrite(&chunkHeader, sizeof(ChunkHeader), 1, file);
// fwrite(buffer, 1, chunkHeader.length, file);
break;
case CHUNK_ENCODING_RLE:
{
auto encode_buffer = std::make_unique<uint8_t[]>(0x600000);
chunkHeader.length = static_cast<uint32_t>(EncodeChunkRLE(buffer, encode_buffer.get(), chunkHeader.length));
std::memcpy(dst_file, &chunkHeader, sizeof(ChunkHeader));
dst_file += sizeof(ChunkHeader);
std::memcpy(dst_file, encode_buffer.get(), chunkHeader.length);
}
break;
case CHUNK_ENCODING_RLECOMPRESSED:
{
auto encode_buffer = std::make_unique<uint8_t[]>(chunkHeader.length * 2);
auto encode_buffer2 = std::make_unique<uint8_t[]>(0x600000);
chunkHeader.length = static_cast<uint32_t>(EncodeChunkRepeat(buffer, encode_buffer.get(), chunkHeader.length));
chunkHeader.length = static_cast<uint32_t>(
EncodeChunkRLE(encode_buffer.get(), encode_buffer2.get(), chunkHeader.length));
std::memcpy(dst_file, &chunkHeader, sizeof(ChunkHeader));
dst_file += sizeof(ChunkHeader);
std::memcpy(dst_file, encode_buffer2.get(), chunkHeader.length);
}
break;
case CHUNK_ENCODING_ROTATE:
{
auto encode_buffer = std::make_unique<uint8_t[]>(chunkHeader.length);
std::memcpy(encode_buffer.get(), buffer, chunkHeader.length);
EncodeChunkRotate(encode_buffer.get(), chunkHeader.length);
std::memcpy(dst_file, &chunkHeader, sizeof(ChunkHeader));
dst_file += sizeof(ChunkHeader);
std::memcpy(dst_file, encode_buffer.get(), chunkHeader.length);
}
break;
}
return chunkHeader.length + sizeof(ChunkHeader);
}
size_t DecodeSV4(const uint8_t* src, uint8_t* dst, size_t length, size_t bufferLength)
{
// (0 to length - 4): RLE chunk
// (length - 4 to length): checksum
return DecodeChunkRLEWithSize(src, dst, length - 4, bufferLength);
}
size_t DecodeSC4(const uint8_t* src, uint8_t* dst, size_t length, size_t bufferLength)
{
// Uncompress
size_t decodedLength = DecodeChunkRLEWithSize(src, dst, length - 4, bufferLength);
// Decode
for (size_t i = 0x60018; i <= std::min(decodedLength - 1, static_cast<size_t>(0x1F8353)); i++)
dst[i] = dst[i] ^ 0x9C;
for (size_t i = 0x60018; i <= std::min(decodedLength - 1, static_cast<size_t>(0x1F8350)); i += 4)
{
dst[i + 1] = Numerics::ror8(dst[i + 1], 3);
uint32_t* code = reinterpret_cast<uint32_t*>(&dst[i]);
*code = Numerics::rol32(*code, 9);
}
return decodedLength;
}
size_t EncodeSV4(const uint8_t* src, uint8_t* dst, size_t length)
{
// Encode
size_t encodedLength = EncodeChunkRLE(src, dst, length);
// Append checksum
uint32_t checksum = CalculateChecksum(dst, encodedLength);
*(reinterpret_cast<uint32_t*>(&dst[encodedLength])) = checksum;
return encodedLength + 4;
}
size_t DecodeTD6(const uint8_t* src, uint8_t* dst, size_t length)
{
return DecodeChunkRLE(src, dst, length - 4);
}
size_t EncodeTD6(const uint8_t* src, uint8_t* dst, size_t length)
{
size_t output_length = EncodeChunkRLE(src, dst, length);
uint32_t checksum = 0;
for (size_t i = 0; i < output_length; i++)
{
uint8_t new_byte = ((checksum & 0xFF) + dst[i]) & 0xFF;
checksum = (checksum & 0xFFFFFF00) + new_byte;
checksum = Numerics::rol32(checksum, 3);
}
checksum -= 0x1D4C1;
*(reinterpret_cast<uint32_t*>(&dst[output_length])) = checksum;
output_length += 4;
return output_length;
}
/* Based off of rct2: 0x006770C1 */
int32_t ValidateTrackChecksum(const uint8_t* src, size_t length)
{
if (length < 4)
return 0;
uint32_t file_checksum = *(reinterpret_cast<const uint32_t*>(&src[length - 4]));
uint32_t checksum = 0;
for (size_t i = 0; i < length - 4; i++)
{
uint8_t new_byte = ((checksum & 0xFF) + src[i]) & 0xFF;
checksum = (checksum & 0xFFFFFF00) + new_byte;
checksum = Numerics::rol32(checksum, 3);
}
if (checksum - 0x1D4C1 == file_checksum)
return 1; // .TD6
else if (checksum - 0x1A67C == file_checksum)
return 1; // .TD4
else if (checksum - 0x1A650 == file_checksum)
return 1; // .TD4
else
return 0;
}
#pragma region Decoding
/**
*
* rct2: 0x0067693A
*/
static size_t DecodeChunkRLE(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length)
{
size_t count;
uint8_t *dst, rleCodeByte;
dst = dst_buffer;
for (size_t i = 0; i < length; i++)
{
rleCodeByte = src_buffer[i];
if (rleCodeByte & 128)
{
i++;
count = 257 - rleCodeByte;
std::fill_n(dst, count, src_buffer[i]);
dst = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(dst) + count);
}
else
{
std::memcpy(dst, src_buffer + i + 1, rleCodeByte + 1);
dst = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(dst) + rleCodeByte + 1);
i += rleCodeByte + 1;
}
}
// Return final size
return dst - dst_buffer;
}
/**
*
* rct2: 0x0067693A
*/
static size_t DecodeChunkRLEWithSize(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length, size_t dstSize)
{
size_t count;
uint8_t *dst, rleCodeByte;
dst = dst_buffer;
if (length <= 0 || dstSize <= 0)
throw std::out_of_range("Invalid RLE string!");
for (size_t i = 0; i < length; i++)
{
rleCodeByte = src_buffer[i];
if (rleCodeByte & 128)
{
i++;
count = 257 - rleCodeByte;
assert(dst + count <= dst_buffer + dstSize);
assert(i < length);
std::fill_n(dst, count, src_buffer[i]);
dst = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(dst) + count);
}
else
{
if ((dst + rleCodeByte + 1 > dst_buffer + dstSize) || (i + 1 >= length))
throw std::out_of_range("Invalid RLE string!");
std::memcpy(dst, src_buffer + i + 1, rleCodeByte + 1);
dst = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(dst) + rleCodeByte + 1);
i += rleCodeByte + 1;
}
}
// Return final size
return dst - dst_buffer;
}
#pragma endregion
#pragma region Encoding
/**
* Ensure dst_buffer is bigger than src_buffer then resize afterwards
* returns length of dst_buffer
*/
static size_t EncodeChunkRLE(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length)
{
const uint8_t* src = src_buffer;
uint8_t* dst = dst_buffer;
const uint8_t* end_src = src + length;
uint8_t count = 0;
const uint8_t* src_norm_start = src;
while (src < end_src - 1)
{
if ((count && *src == src[1]) || count > 125)
{
*dst++ = count - 1;
std::memcpy(dst, src_norm_start, count);
dst += count;
src_norm_start += count;
count = 0;
}
if (*src == src[1])
{
for (; (count < 125) && ((src + count) < end_src); count++)
{
if (*src != src[count])
break;
}
*dst++ = 257 - count;
*dst++ = *src;
src += count;
src_norm_start = src;
count = 0;
}
else
{
count++;
src++;
}
}
if (src == end_src - 1)
count++;
if (count)
{
*dst++ = count - 1;
std::memcpy(dst, src_norm_start, count);
dst += count;
}
return dst - dst_buffer;
}
static size_t EncodeChunkRepeat(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length)
{
if (length == 0)
return 0;
size_t outLength = 0;
// Need to emit at least one byte, otherwise there is nothing to repeat
*dst_buffer++ = 255;
*dst_buffer++ = src_buffer[0];
outLength += 2;
// Iterate through remainder of the source buffer
for (size_t i = 1; i < length;)
{
size_t searchIndex = (i < 32) ? 0 : (i - 32);
size_t searchEnd = i - 1;
size_t bestRepeatIndex = 0;
size_t bestRepeatCount = 0;
for (size_t repeatIndex = searchIndex; repeatIndex <= searchEnd; repeatIndex++)
{
size_t repeatCount = 0;
size_t maxRepeatCount = std::min(std::min(static_cast<size_t>(7), searchEnd - repeatIndex), length - i - 1);
// maxRepeatCount should not exceed length
assert(repeatIndex + maxRepeatCount < length);
assert(i + maxRepeatCount < length);
for (size_t j = 0; j <= maxRepeatCount; j++)
{
if (src_buffer[repeatIndex + j] == src_buffer[i + j])
{
repeatCount++;
}
else
{
break;
}
}
if (repeatCount > bestRepeatCount)
{
bestRepeatIndex = repeatIndex;
bestRepeatCount = repeatCount;
// Maximum repeat count is 8
if (repeatCount == 8)
break;
}
}
if (bestRepeatCount == 0)
{
*dst_buffer++ = 255;
*dst_buffer++ = src_buffer[i];
outLength += 2;
i++;
}
else
{
*dst_buffer++ = static_cast<uint8_t>((bestRepeatCount - 1) | ((32 - (i - bestRepeatIndex)) << 3));
outLength++;
i += bestRepeatCount;
}
}
return outLength;
}
static void EncodeChunkRotate(uint8_t* buffer, size_t length)
{
size_t i;
uint8_t code = 1;
for (i = 0; i < length; i++)
{
buffer[i] = Numerics::rol8(buffer[i], code);
code = (code + 2) % 8;
}
}
#pragma endregion
int32_t DetectFileType(const uint8_t* src, size_t length)
{
if (length < 4)
{
throw std::length_error("Stream is (nearly) empty!");
}
size_t i;
// Currently can't detect TD4, as the checksum is the same as SC4 (need alternative method)
uint32_t checksum = *(reinterpret_cast<const uint32_t*>(&src[length - 4]));
uint32_t actualChecksum = 0;
for (i = 0; i < length - 4; i++)
{
actualChecksum = (actualChecksum & 0xFFFFFF00) | (((actualChecksum & 0xFF) + static_cast<uint8_t>(src[i])) & 0xFF);
actualChecksum = Numerics::rol32(actualChecksum, 3);
}
return DetectRCT1Version(checksum - actualChecksum);
}
int32_t DetectRCT1Version(int32_t gameVersion)
{
int32_t fileType = (gameVersion) > 0 ? FILE_TYPE_SV4 : FILE_TYPE_SC4;
gameVersion = abs(gameVersion);
if (gameVersion >= 108000 && gameVersion < 110000)
return (FILE_VERSION_RCT1 | fileType);
if (gameVersion >= 110000 && gameVersion < 120000)
return (FILE_VERSION_RCT1_AA | fileType);
if (gameVersion >= 120000 && gameVersion < 130000)
return (FILE_VERSION_RCT1_LL | fileType);
// RCTOA Acres sets this, and possibly some other user-created scenarios as well
if (gameVersion == 0)
return (FILE_VERSION_RCT1_LL | fileType);
return -1;
}
} // namespace OpenRCT2::SawyerCoding
+63
View File
@@ -0,0 +1,63 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include <cstddef>
#include <cstdint>
namespace OpenRCT2
{
// TODO: make enum class in SawyerCoding namespace
enum
{
CHUNK_ENCODING_NONE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLECOMPRESSED,
CHUNK_ENCODING_ROTATE
};
// TODO: make enum class in SawyerCoding namespace
enum
{
FILE_VERSION_MASK = (3 << 0),
FILE_VERSION_RCT1 = (0 << 0),
FILE_VERSION_RCT1_AA = (1 << 0),
FILE_VERSION_RCT1_LL = (2 << 0),
FILE_TYPE_MASK = (3 << 2),
FILE_TYPE_TD4 = (0 << 2),
FILE_TYPE_SV4 = (1 << 2),
FILE_TYPE_SC4 = (2 << 2)
};
} // namespace OpenRCT2
namespace OpenRCT2::SawyerCoding
{
#pragma pack(push, 1)
struct ChunkHeader
{
uint8_t encoding;
uint32_t length;
};
static_assert(sizeof(ChunkHeader) == 5);
#pragma pack(pop)
uint32_t CalculateChecksum(const uint8_t* buffer, size_t length);
size_t WriteChunkBuffer(uint8_t* dst_file, const uint8_t* src_buffer, ChunkHeader chunkHeader);
size_t DecodeSV4(const uint8_t* src, uint8_t* dst, size_t length, size_t bufferLength);
size_t DecodeSC4(const uint8_t* src, uint8_t* dst, size_t length, size_t bufferLength);
size_t EncodeSV4(const uint8_t* src, uint8_t* dst, size_t length);
size_t DecodeTD6(const uint8_t* src, uint8_t* dst, size_t length);
size_t EncodeTD6(const uint8_t* src, uint8_t* dst, size_t length);
int32_t ValidateTrackChecksum(const uint8_t* src, size_t length);
int32_t DetectFileType(const uint8_t* src, size_t length);
int32_t DetectRCT1Version(int32_t gameVersion);
} // namespace OpenRCT2::SawyerCoding
+5 -5
View File
@@ -203,6 +203,8 @@
<ClInclude Include="core\FileSystem.hpp" />
<ClInclude Include="core\FileWatcher.h" />
<ClInclude Include="core\FixedPoint.hpp" />
<ClInclude Include="core\FixedVector.h" />
<ClInclude Include="core\FlagHolder.hpp" />
<ClInclude Include="core\GroupVector.hpp" />
<ClInclude Include="core\Guard.hpp" />
<ClInclude Include="core\Http.h" />
@@ -222,8 +224,7 @@
<ClInclude Include="core\Random.hpp" />
<ClInclude Include="core\Range.hpp" />
<ClInclude Include="core\RTL.h" />
<ClInclude Include="core\FixedVector.h" />
<ClInclude Include="core\FlagHolder.hpp" />
<ClInclude Include="core\SawyerCoding.h" />
<ClInclude Include="core\Speed.hpp" />
<ClInclude Include="core\String.hpp" />
<ClInclude Include="core\StringBuilder.h" />
@@ -595,8 +596,7 @@
<ClInclude Include="ui\UiContext.h" />
<ClInclude Include="ui\WindowManager.h" />
<ClInclude Include="util\Math.hpp" />
<ClInclude Include="util/Prefetch.h" />
<ClInclude Include="util\SawyerCoding.h" />
<ClInclude Include="util\Prefetch.h" />
<ClInclude Include="util\Util.h" />
<ClInclude Include="Version.h" />
<ClInclude Include="windows\Intent.h" />
@@ -768,6 +768,7 @@
<ClCompile Include="core\Path.cpp" />
<ClCompile Include="core\RTL.FriBidi.cpp" />
<ClCompile Include="core\RTL.ICU.cpp" />
<ClCompile Include="core\SawyerCoding.cpp" />
<ClCompile Include="core\String.cpp" />
<ClCompile Include="core\StringBuilder.cpp" />
<ClCompile Include="core\StringReader.cpp" />
@@ -1091,7 +1092,6 @@
<ClCompile Include="TrackImporter.cpp" />
<ClCompile Include="ui\DummyUiContext.cpp" />
<ClCompile Include="ui\DummyWindowManager.cpp" />
<ClCompile Include="util\SawyerCoding.cpp" />
<ClCompile Include="util\Util.cpp" />
<ClCompile Include="Version.cpp" />
<ClCompile Include="windows\Intent.cpp" />
+1 -1
View File
@@ -23,6 +23,7 @@
#include "../core/File.h"
#include "../core/Guard.hpp"
#include "../core/Json.hpp"
#include "../core/SawyerCoding.h"
#include "../entity/EntityList.h"
#include "../entity/EntityRegistry.h"
#include "../entity/EntityTweener.h"
@@ -35,7 +36,6 @@
#include "../scripting/ScriptEngine.h"
#include "../ui/UiContext.h"
#include "../ui/WindowManager.h"
#include "../util/SawyerCoding.h"
#include "../world/Location.hpp"
#include "network.h"
+1 -1
View File
@@ -11,8 +11,8 @@
#include "../Context.h"
#include "../Game.h"
#include "../core/SawyerCoding.h"
#include "../object/Object.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
#include "ObjectLimits.h"
#include "ObjectManager.h"
+3 -3
View File
@@ -23,6 +23,7 @@
#include "../core/MemoryStream.h"
#include "../core/Numerics.hpp"
#include "../core/Path.hpp"
#include "../core/SawyerCoding.h"
#include "../core/String.hpp"
#include "../localisation/LocalisationService.h"
#include "../object/Object.h"
@@ -31,7 +32,6 @@
#include "../rct12/SawyerChunkReader.h"
#include "../rct12/SawyerChunkWriter.h"
#include "../scenario/ScenarioRepository.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
#include "Object.h"
#include "ObjectFactory.h"
@@ -528,11 +528,11 @@ private:
// Encode data
ObjectType objectType = entry->GetType();
SawyerCodingChunkHeader chunkHeader;
SawyerCoding::ChunkHeader chunkHeader;
chunkHeader.encoding = kLegacyObjectEntryGroupEncoding[EnumValue(objectType)];
chunkHeader.length = static_cast<uint32_t>(dataSize);
uint8_t* encodedDataBuffer = Memory::Allocate<uint8_t>(0x600000);
size_t encodedDataSize = SawyerCodingWriteChunkBuffer(
size_t encodedDataSize = SawyerCoding::WriteChunkBuffer(
encodedDataBuffer, reinterpret_cast<const uint8_t*>(data), chunkHeader);
// Save to file
+1 -1
View File
@@ -35,6 +35,7 @@
# include "../core/Console.hpp"
# include "../core/Guard.hpp"
# include "../core/Path.hpp"
# include "../core/SawyerCoding.h"
# include "../core/String.hpp"
# include "../drawing/IDrawingEngine.h"
# include "../interface/Screenshot.h"
@@ -42,7 +43,6 @@
# include "../object/ObjectManager.h"
# include "../park/ParkFile.h"
# include "../scenario/Scenario.h"
# include "../util/SawyerCoding.h"
# include "Platform.h"
# define WSZ(x) L"" x
+5 -5
View File
@@ -24,6 +24,7 @@
#include "../core/IStream.hpp"
#include "../core/Memory.hpp"
#include "../core/Path.hpp"
#include "../core/SawyerCoding.h"
#include "../core/String.hpp"
#include "../entity/Balloon.h"
#include "../entity/Duck.h"
@@ -61,7 +62,6 @@
#include "../scenario/Scenario.h"
#include "../scenario/ScenarioRepository.h"
#include "../scenario/ScenarioSources.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
#include "../world/Climate.h"
#include "../world/Entrance.h"
@@ -175,7 +175,7 @@ namespace OpenRCT2::RCT1
_s4 = *ReadAndDecodeS4(stream, isScenario);
_s4Path = path;
_isScenario = isScenario;
_gameVersion = SawyerCodingDetectRCT1Version(_s4.GameVersion) & FILE_VERSION_MASK;
_gameVersion = SawyerCoding::DetectRCT1Version(_s4.GameVersion) & FILE_VERSION_MASK;
// Only determine what objects we required to import this saved game
InitialiseEntryMaps();
@@ -309,14 +309,14 @@ namespace OpenRCT2::RCT1
auto decodedData = std::make_unique<uint8_t[]>(sizeof(S4));
size_t decodedSize;
int32_t fileType = SawyerCodingDetectFileType(data.get(), dataSize);
int32_t fileType = SawyerCoding::DetectFileType(data.get(), dataSize);
if (isScenario && (fileType & FILE_VERSION_MASK) != FILE_VERSION_RCT1)
{
decodedSize = SawyerCodingDecodeSC4(data.get(), decodedData.get(), dataSize, sizeof(S4));
decodedSize = SawyerCoding::DecodeSC4(data.get(), decodedData.get(), dataSize, sizeof(S4));
}
else
{
decodedSize = SawyerCodingDecodeSV4(data.get(), decodedData.get(), dataSize, sizeof(S4));
decodedSize = SawyerCoding::DecodeSV4(data.get(), decodedData.get(), dataSize, sizeof(S4));
}
if (decodedSize == sizeof(S4))
+5 -5
View File
@@ -24,7 +24,7 @@ constexpr const char* EXCEPTION_MSG_DESTINATION_TOO_SMALL = "Chunk data larger t
constexpr const char* EXCEPTION_MSG_INVALID_CHUNK_ENCODING = "Invalid chunk encoding.";
constexpr const char* EXCEPTION_MSG_ZERO_SIZED_CHUNK = "Encountered zero-sized chunk.";
static MemoryStream DecodeChunk(const void* src, const SawyerCodingChunkHeader& header);
static MemoryStream DecodeChunk(const void* src, const SawyerCoding::ChunkHeader& header);
SawyerChunkReader::SawyerChunkReader(OpenRCT2::IStream* stream)
: _stream(stream)
@@ -36,7 +36,7 @@ void SawyerChunkReader::SkipChunk()
uint64_t originalPosition = _stream->GetPosition();
try
{
auto header = _stream->ReadValue<SawyerCodingChunkHeader>();
auto header = _stream->ReadValue<SawyerCoding::ChunkHeader>();
_stream->Seek(header.length, OpenRCT2::STREAM_SEEK_CURRENT);
}
catch (const std::exception&)
@@ -52,7 +52,7 @@ std::shared_ptr<SawyerChunk> SawyerChunkReader::ReadChunk()
uint64_t originalPosition = _stream->GetPosition();
try
{
auto header = _stream->ReadValue<SawyerCodingChunkHeader>();
auto header = _stream->ReadValue<SawyerCoding::ChunkHeader>();
if (header.length >= MAX_UNCOMPRESSED_CHUNK_SIZE)
throw SawyerChunkException(EXCEPTION_MSG_CORRUPT_CHUNK_SIZE);
@@ -108,7 +108,7 @@ std::shared_ptr<SawyerChunk> SawyerChunkReader::ReadChunkTrack()
throw SawyerChunkException(EXCEPTION_MSG_CORRUPT_CHUNK_SIZE);
}
SawyerCodingChunkHeader header{ CHUNK_ENCODING_RLE, compressedDataLength };
SawyerCoding::ChunkHeader header{ CHUNK_ENCODING_RLE, compressedDataLength };
auto buffer = DecodeChunk(compressedData.get(), header);
if (buffer.GetLength() == 0)
{
@@ -261,7 +261,7 @@ static MemoryStream DecodeChunkRotate(const void* src, size_t srcLength)
return buf;
}
static MemoryStream DecodeChunk(const void* src, const SawyerCodingChunkHeader& header)
static MemoryStream DecodeChunk(const void* src, const SawyerCoding::ChunkHeader& header)
{
MemoryStream buf;
+1 -1
View File
@@ -10,7 +10,7 @@
#pragma once
#include "../core/IStream.hpp"
#include "../util/SawyerCoding.h"
#include "../core/SawyerCoding.h"
#include "SawyerChunk.h"
#include <cstdint>
+3 -3
View File
@@ -11,7 +11,7 @@
#include "../core/IStream.hpp"
#include "../core/Numerics.hpp"
#include "../util/SawyerCoding.h"
#include "../core/SawyerCoding.h"
using namespace OpenRCT2;
@@ -30,12 +30,12 @@ void SawyerChunkWriter::WriteChunk(const SawyerChunk* chunk)
void SawyerChunkWriter::WriteChunk(const void* src, size_t length, SAWYER_ENCODING encoding)
{
SawyerCodingChunkHeader header;
SawyerCoding::ChunkHeader header;
header.encoding = static_cast<uint8_t>(encoding);
header.length = static_cast<uint32_t>(length);
auto data = std::make_unique<uint8_t[]>(MAX_COMPRESSED_CHUNK_SIZE);
size_t dataLength = SawyerCodingWriteChunkBuffer(data.get(), static_cast<const uint8_t*>(src), header);
size_t dataLength = SawyerCoding::WriteChunkBuffer(data.get(), static_cast<const uint8_t*>(src), header);
_stream->Write(data.get(), dataLength);
}
+1 -1
View File
@@ -21,6 +21,7 @@
#include "../core/Numerics.hpp"
#include "../core/Path.hpp"
#include "../core/Random.hpp"
#include "../core/SawyerCoding.h"
#include "../core/String.hpp"
#include "../entity/Balloon.h"
#include "../entity/Duck.h"
@@ -68,7 +69,6 @@
#include "../scenario/Scenario.h"
#include "../scenario/ScenarioRepository.h"
#include "../scenario/ScenarioSources.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
#include "../world/Climate.h"
#include "../world/Entrance.h"
+1 -1
View File
@@ -15,13 +15,13 @@
#include "../GameState.h"
#include "../audio/audio.h"
#include "../config/Config.h"
#include "../core/SawyerCoding.h"
#include "../interface/Viewport.h"
#include "../management/Finance.h"
#include "../network/network.h"
#include "../platform/Platform.h"
#include "../rct1/RCT1.h"
#include "../ride/RideColour.h"
#include "../util/SawyerCoding.h"
#include "../world/Footpath.h"
#include "../world/Map.h"
#include "../world/MapAnimation.h"
+1 -1
View File
@@ -34,6 +34,7 @@
#include "../core/DataSerialiser.h"
#include "../core/File.h"
#include "../core/Numerics.hpp"
#include "../core/SawyerCoding.h"
#include "../core/String.hpp"
#include "../core/UnitConversion.h"
#include "../drawing/X8DrawingEngine.h"
@@ -52,7 +53,6 @@
#include "../object/StationObject.h"
#include "../rct2/RCT2.h"
#include "../ride/RideConstruction.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
#include "../world/Footpath.h"
#include "../world/Park.h"
+1 -1
View File
@@ -11,6 +11,7 @@
#include "../Diagnostic.h"
#include "../Game.h"
#include "../audio/audio.h"
#include "../core/SawyerCoding.h"
#include "../interface/Viewport.h"
#include "../localisation/StringIds.h"
#include "../object/FootpathObject.h"
@@ -20,7 +21,6 @@
#include "../object/ObjectList.h"
#include "../object/ObjectManager.h"
#include "../rct2/RCT2.h"
#include "../util/SawyerCoding.h"
#include "../windows/Intent.h"
#include "../world/Footpath.h"
#include "../world/Scenery.h"
+1 -1
View File
@@ -24,6 +24,7 @@
#include "../core/Guard.hpp"
#include "../core/Path.hpp"
#include "../core/Random.hpp"
#include "../core/SawyerCoding.h"
#include "../core/UnitConversion.h"
#include "../entity/Duck.h"
#include "../entity/Guest.h"
@@ -47,7 +48,6 @@
#include "../rct12/RCT12.h"
#include "../ride/Ride.h"
#include "../ride/Track.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
#include "../windows/Intent.h"
#include "../world/Climate.h"
-428
View File
@@ -1,428 +0,0 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "SawyerCoding.h"
#include "../core/Numerics.hpp"
#include "../platform/Platform.h"
#include "../scenario/Scenario.h"
#include "Util.h"
#include <cassert>
#include <cstring>
#include <stdexcept>
using namespace OpenRCT2;
static size_t DecodeChunkRLE(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length);
static size_t DecodeChunkRLEWithSize(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length, size_t dstSize);
static size_t EncodeChunkRLE(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length);
static size_t EncodeChunkRepeat(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length);
static void EncodeChunkRotate(uint8_t* buffer, size_t length);
uint32_t SawyerCodingCalculateChecksum(const uint8_t* buffer, size_t length)
{
uint32_t checksum = 0;
for (size_t i = 0; i < length; i++)
checksum += buffer[i];
return checksum;
}
/**
*
* rct2: 0x006762E1
*
*/
size_t SawyerCodingWriteChunkBuffer(uint8_t* dst_file, const uint8_t* buffer, SawyerCodingChunkHeader chunkHeader)
{
switch (chunkHeader.encoding)
{
case CHUNK_ENCODING_NONE:
std::memcpy(dst_file, &chunkHeader, sizeof(SawyerCodingChunkHeader));
dst_file += sizeof(SawyerCodingChunkHeader);
std::memcpy(dst_file, buffer, chunkHeader.length);
// fwrite(&chunkHeader, sizeof(SawyerCodingChunkHeader), 1, file);
// fwrite(buffer, 1, chunkHeader.length, file);
break;
case CHUNK_ENCODING_RLE:
{
auto encode_buffer = std::make_unique<uint8_t[]>(0x600000);
chunkHeader.length = static_cast<uint32_t>(EncodeChunkRLE(buffer, encode_buffer.get(), chunkHeader.length));
std::memcpy(dst_file, &chunkHeader, sizeof(SawyerCodingChunkHeader));
dst_file += sizeof(SawyerCodingChunkHeader);
std::memcpy(dst_file, encode_buffer.get(), chunkHeader.length);
}
break;
case CHUNK_ENCODING_RLECOMPRESSED:
{
auto encode_buffer = std::make_unique<uint8_t[]>(chunkHeader.length * 2);
auto encode_buffer2 = std::make_unique<uint8_t[]>(0x600000);
chunkHeader.length = static_cast<uint32_t>(EncodeChunkRepeat(buffer, encode_buffer.get(), chunkHeader.length));
chunkHeader.length = static_cast<uint32_t>(
EncodeChunkRLE(encode_buffer.get(), encode_buffer2.get(), chunkHeader.length));
std::memcpy(dst_file, &chunkHeader, sizeof(SawyerCodingChunkHeader));
dst_file += sizeof(SawyerCodingChunkHeader);
std::memcpy(dst_file, encode_buffer2.get(), chunkHeader.length);
}
break;
case CHUNK_ENCODING_ROTATE:
{
auto encode_buffer = std::make_unique<uint8_t[]>(chunkHeader.length);
std::memcpy(encode_buffer.get(), buffer, chunkHeader.length);
EncodeChunkRotate(encode_buffer.get(), chunkHeader.length);
std::memcpy(dst_file, &chunkHeader, sizeof(SawyerCodingChunkHeader));
dst_file += sizeof(SawyerCodingChunkHeader);
std::memcpy(dst_file, encode_buffer.get(), chunkHeader.length);
}
break;
}
return chunkHeader.length + sizeof(SawyerCodingChunkHeader);
}
size_t SawyerCodingDecodeSV4(const uint8_t* src, uint8_t* dst, size_t length, size_t bufferLength)
{
// (0 to length - 4): RLE chunk
// (length - 4 to length): checksum
return DecodeChunkRLEWithSize(src, dst, length - 4, bufferLength);
}
size_t SawyerCodingDecodeSC4(const uint8_t* src, uint8_t* dst, size_t length, size_t bufferLength)
{
// Uncompress
size_t decodedLength = DecodeChunkRLEWithSize(src, dst, length - 4, bufferLength);
// Decode
for (size_t i = 0x60018; i <= std::min(decodedLength - 1, static_cast<size_t>(0x1F8353)); i++)
dst[i] = dst[i] ^ 0x9C;
for (size_t i = 0x60018; i <= std::min(decodedLength - 1, static_cast<size_t>(0x1F8350)); i += 4)
{
dst[i + 1] = Numerics::ror8(dst[i + 1], 3);
uint32_t* code = reinterpret_cast<uint32_t*>(&dst[i]);
*code = Numerics::rol32(*code, 9);
}
return decodedLength;
}
size_t SawyerCodingEencodeSV4(const uint8_t* src, uint8_t* dst, size_t length)
{
// Encode
size_t encodedLength = EncodeChunkRLE(src, dst, length);
// Append checksum
uint32_t checksum = SawyerCodingCalculateChecksum(dst, encodedLength);
*(reinterpret_cast<uint32_t*>(&dst[encodedLength])) = checksum;
return encodedLength + 4;
}
size_t SawyerCodingDecodeTD6(const uint8_t* src, uint8_t* dst, size_t length)
{
return DecodeChunkRLE(src, dst, length - 4);
}
size_t SawyerCodingEncodeTD6(const uint8_t* src, uint8_t* dst, size_t length)
{
size_t output_length = EncodeChunkRLE(src, dst, length);
uint32_t checksum = 0;
for (size_t i = 0; i < output_length; i++)
{
uint8_t new_byte = ((checksum & 0xFF) + dst[i]) & 0xFF;
checksum = (checksum & 0xFFFFFF00) + new_byte;
checksum = Numerics::rol32(checksum, 3);
}
checksum -= 0x1D4C1;
*(reinterpret_cast<uint32_t*>(&dst[output_length])) = checksum;
output_length += 4;
return output_length;
}
/* Based off of rct2: 0x006770C1 */
int32_t SawyerCodingValidateTrackChecksum(const uint8_t* src, size_t length)
{
if (length < 4)
return 0;
uint32_t file_checksum = *(reinterpret_cast<const uint32_t*>(&src[length - 4]));
uint32_t checksum = 0;
for (size_t i = 0; i < length - 4; i++)
{
uint8_t new_byte = ((checksum & 0xFF) + src[i]) & 0xFF;
checksum = (checksum & 0xFFFFFF00) + new_byte;
checksum = Numerics::rol32(checksum, 3);
}
if (checksum - 0x1D4C1 == file_checksum)
return 1; // .TD6
else if (checksum - 0x1A67C == file_checksum)
return 1; // .TD4
else if (checksum - 0x1A650 == file_checksum)
return 1; // .TD4
else
return 0;
}
#pragma region Decoding
/**
*
* rct2: 0x0067693A
*/
static size_t DecodeChunkRLE(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length)
{
size_t count;
uint8_t *dst, rleCodeByte;
dst = dst_buffer;
for (size_t i = 0; i < length; i++)
{
rleCodeByte = src_buffer[i];
if (rleCodeByte & 128)
{
i++;
count = 257 - rleCodeByte;
std::fill_n(dst, count, src_buffer[i]);
dst = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(dst) + count);
}
else
{
std::memcpy(dst, src_buffer + i + 1, rleCodeByte + 1);
dst = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(dst) + rleCodeByte + 1);
i += rleCodeByte + 1;
}
}
// Return final size
return dst - dst_buffer;
}
/**
*
* rct2: 0x0067693A
*/
static size_t DecodeChunkRLEWithSize(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length, size_t dstSize)
{
size_t count;
uint8_t *dst, rleCodeByte;
dst = dst_buffer;
if (length <= 0 || dstSize <= 0)
throw std::out_of_range("Invalid RLE string!");
for (size_t i = 0; i < length; i++)
{
rleCodeByte = src_buffer[i];
if (rleCodeByte & 128)
{
i++;
count = 257 - rleCodeByte;
assert(dst + count <= dst_buffer + dstSize);
assert(i < length);
std::fill_n(dst, count, src_buffer[i]);
dst = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(dst) + count);
}
else
{
if ((dst + rleCodeByte + 1 > dst_buffer + dstSize) || (i + 1 >= length))
throw std::out_of_range("Invalid RLE string!");
std::memcpy(dst, src_buffer + i + 1, rleCodeByte + 1);
dst = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(dst) + rleCodeByte + 1);
i += rleCodeByte + 1;
}
}
// Return final size
return dst - dst_buffer;
}
#pragma endregion
#pragma region Encoding
/**
* Ensure dst_buffer is bigger than src_buffer then resize afterwards
* returns length of dst_buffer
*/
static size_t EncodeChunkRLE(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length)
{
const uint8_t* src = src_buffer;
uint8_t* dst = dst_buffer;
const uint8_t* end_src = src + length;
uint8_t count = 0;
const uint8_t* src_norm_start = src;
while (src < end_src - 1)
{
if ((count && *src == src[1]) || count > 125)
{
*dst++ = count - 1;
std::memcpy(dst, src_norm_start, count);
dst += count;
src_norm_start += count;
count = 0;
}
if (*src == src[1])
{
for (; (count < 125) && ((src + count) < end_src); count++)
{
if (*src != src[count])
break;
}
*dst++ = 257 - count;
*dst++ = *src;
src += count;
src_norm_start = src;
count = 0;
}
else
{
count++;
src++;
}
}
if (src == end_src - 1)
count++;
if (count)
{
*dst++ = count - 1;
std::memcpy(dst, src_norm_start, count);
dst += count;
}
return dst - dst_buffer;
}
static size_t EncodeChunkRepeat(const uint8_t* src_buffer, uint8_t* dst_buffer, size_t length)
{
if (length == 0)
return 0;
size_t outLength = 0;
// Need to emit at least one byte, otherwise there is nothing to repeat
*dst_buffer++ = 255;
*dst_buffer++ = src_buffer[0];
outLength += 2;
// Iterate through remainder of the source buffer
for (size_t i = 1; i < length;)
{
size_t searchIndex = (i < 32) ? 0 : (i - 32);
size_t searchEnd = i - 1;
size_t bestRepeatIndex = 0;
size_t bestRepeatCount = 0;
for (size_t repeatIndex = searchIndex; repeatIndex <= searchEnd; repeatIndex++)
{
size_t repeatCount = 0;
size_t maxRepeatCount = std::min(std::min(static_cast<size_t>(7), searchEnd - repeatIndex), length - i - 1);
// maxRepeatCount should not exceed length
assert(repeatIndex + maxRepeatCount < length);
assert(i + maxRepeatCount < length);
for (size_t j = 0; j <= maxRepeatCount; j++)
{
if (src_buffer[repeatIndex + j] == src_buffer[i + j])
{
repeatCount++;
}
else
{
break;
}
}
if (repeatCount > bestRepeatCount)
{
bestRepeatIndex = repeatIndex;
bestRepeatCount = repeatCount;
// Maximum repeat count is 8
if (repeatCount == 8)
break;
}
}
if (bestRepeatCount == 0)
{
*dst_buffer++ = 255;
*dst_buffer++ = src_buffer[i];
outLength += 2;
i++;
}
else
{
*dst_buffer++ = static_cast<uint8_t>((bestRepeatCount - 1) | ((32 - (i - bestRepeatIndex)) << 3));
outLength++;
i += bestRepeatCount;
}
}
return outLength;
}
static void EncodeChunkRotate(uint8_t* buffer, size_t length)
{
size_t i;
uint8_t code = 1;
for (i = 0; i < length; i++)
{
buffer[i] = Numerics::rol8(buffer[i], code);
code = (code + 2) % 8;
}
}
#pragma endregion
int32_t SawyerCodingDetectFileType(const uint8_t* src, size_t length)
{
if (length < 4)
{
throw std::length_error("Stream is (nearly) empty!");
}
size_t i;
// Currently can't detect TD4, as the checksum is the same as SC4 (need alternative method)
uint32_t checksum = *(reinterpret_cast<const uint32_t*>(&src[length - 4]));
uint32_t actualChecksum = 0;
for (i = 0; i < length - 4; i++)
{
actualChecksum = (actualChecksum & 0xFFFFFF00) | (((actualChecksum & 0xFF) + static_cast<uint8_t>(src[i])) & 0xFF);
actualChecksum = Numerics::rol32(actualChecksum, 3);
}
return SawyerCodingDetectRCT1Version(checksum - actualChecksum);
}
int32_t SawyerCodingDetectRCT1Version(int32_t gameVersion)
{
int32_t fileType = (gameVersion) > 0 ? FILE_TYPE_SV4 : FILE_TYPE_SC4;
gameVersion = abs(gameVersion);
if (gameVersion >= 108000 && gameVersion < 110000)
return (FILE_VERSION_RCT1 | fileType);
if (gameVersion >= 110000 && gameVersion < 120000)
return (FILE_VERSION_RCT1_AA | fileType);
if (gameVersion >= 120000 && gameVersion < 130000)
return (FILE_VERSION_RCT1_LL | fileType);
// RCTOA Acres sets this, and possibly some other user-created scenarios as well
if (gameVersion == 0)
return (FILE_VERSION_RCT1_LL | fileType);
return -1;
}
-55
View File
@@ -1,55 +0,0 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include <cstddef>
#include <cstdint>
#pragma pack(push, 1)
struct SawyerCodingChunkHeader
{
uint8_t encoding;
uint32_t length;
};
static_assert(sizeof(SawyerCodingChunkHeader) == 5);
#pragma pack(pop)
enum
{
CHUNK_ENCODING_NONE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLECOMPRESSED,
CHUNK_ENCODING_ROTATE
};
enum
{
FILE_VERSION_MASK = (3 << 0),
FILE_VERSION_RCT1 = (0 << 0),
FILE_VERSION_RCT1_AA = (1 << 0),
FILE_VERSION_RCT1_LL = (2 << 0),
FILE_TYPE_MASK = (3 << 2),
FILE_TYPE_TD4 = (0 << 2),
FILE_TYPE_SV4 = (1 << 2),
FILE_TYPE_SC4 = (2 << 2)
};
uint32_t SawyerCodingCalculateChecksum(const uint8_t* buffer, size_t length);
size_t SawyerCodingWriteChunkBuffer(uint8_t* dst_file, const uint8_t* src_buffer, SawyerCodingChunkHeader chunkHeader);
size_t SawyerCodingDecodeSV4(const uint8_t* src, uint8_t* dst, size_t length, size_t bufferLength);
size_t SawyerCodingDecodeSC4(const uint8_t* src, uint8_t* dst, size_t length, size_t bufferLength);
size_t SawyerCodingEencodeSV4(const uint8_t* src, uint8_t* dst, size_t length);
size_t SawyerCodingDecodeTD6(const uint8_t* src, uint8_t* dst, size_t length);
size_t SawyerCodingEncodeTD6(const uint8_t* src, uint8_t* dst, size_t length);
int32_t SawyerCodingValidateTrackChecksum(const uint8_t* src, size_t length);
int32_t SawyerCodingDetectFileType(const uint8_t* src, size_t length);
int32_t SawyerCodingDetectRCT1Version(int32_t gameVersion);
+8 -6
View File
@@ -9,11 +9,13 @@
#include <gtest/gtest.h>
#include <openrct2/core/MemoryStream.h>
#include <openrct2/core/SawyerCoding.h>
#include <openrct2/rct12/SawyerChunkReader.h>
#include <openrct2/util/SawyerCoding.h>
constexpr size_t BUFFER_SIZE = 0x600000;
using namespace OpenRCT2;
class SawyerCodingTest : public testing::Test
{
protected:
@@ -34,13 +36,13 @@ protected:
void TestEncodeDecode(uint8_t encoding_type)
{
// Encode
SawyerCodingChunkHeader chdr_in;
SawyerCoding::ChunkHeader chdr_in;
chdr_in.encoding = encoding_type;
chdr_in.length = sizeof(randomdata);
uint8_t* encodedDataBuffer = new uint8_t[BUFFER_SIZE];
size_t encodedDataSize = SawyerCodingWriteChunkBuffer(
size_t encodedDataSize = SawyerCoding::WriteChunkBuffer(
encodedDataBuffer, reinterpret_cast<const uint8_t*>(randomdata), chdr_in);
ASSERT_GT(encodedDataSize, sizeof(SawyerCodingChunkHeader));
ASSERT_GT(encodedDataSize, sizeof(SawyerCoding::ChunkHeader));
// Decode
OpenRCT2::MemoryStream ms(encodedDataBuffer, encodedDataSize);
@@ -56,8 +58,8 @@ protected:
void TestDecode(const uint8_t* data, size_t size)
{
auto expectedLength = size - sizeof(SawyerCodingChunkHeader);
auto chdr_in = reinterpret_cast<const SawyerCodingChunkHeader*>(data);
auto expectedLength = size - sizeof(SawyerCoding::ChunkHeader);
auto chdr_in = reinterpret_cast<const SawyerCoding::ChunkHeader*>(data);
ASSERT_EQ(chdr_in->length, expectedLength);
OpenRCT2::MemoryStream ms(data, size);