From 3f663feec792820efa7256d9427befd735c2856a Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 15 Dec 2018 10:10:07 +0000 Subject: [PATCH] Automatically write array size --- src/openrct2/ParkFile.cpp | 37 ++++++++++++++++++++++++++++++++----- src/openrct2/ParkFile.h | 7 ++++++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/openrct2/ParkFile.cpp b/src/openrct2/ParkFile.cpp index 45ac9d21ff..4ab7e8778b 100644 --- a/src/openrct2/ParkFile.cpp +++ b/src/openrct2/ParkFile.cpp @@ -95,14 +95,41 @@ void ParkFile::EndChunk() _chunks.push_back(_currentChunk); } -void ParkFile::BeginArray(size_t count, size_t elementSize) +void ParkFile::BeginArray() { - WriteValue((uint32_t)count); - WriteValue((uint32_t)elementSize); + _currentArrayCount = 0; + _currentArrayElementSize = 0; + _currentArrayStartPos = _buffer.tellp(); + WriteValue(0); + WriteValue(0); + _currentArrayLastPos = _buffer.tellp(); +} + +void ParkFile::NextArrayElement() +{ + auto lastElSize = (size_t)_buffer.tellp() - _currentArrayLastPos; + if (_currentArrayCount == 0) + { + // Set array element size based on first element size + _currentArrayElementSize = lastElSize; + } + else if (_currentArrayElementSize != lastElSize) + { + // Array element size was different from first element so reset it + // to dynamic + _currentArrayElementSize = 0; + } + _currentArrayCount++; + _currentArrayLastPos = _buffer.tellp(); } void ParkFile::EndArray() { + auto backupPos = _buffer.tellp(); + _buffer.seekp(_currentArrayStartPos); + WriteValue((uint32_t)_currentArrayCount); + WriteValue((uint32_t)_currentArrayElementSize); + _buffer.seekp(backupPos); } void ParkFile::WriteBuffer(const void* buffer, size_t len) @@ -138,8 +165,7 @@ void ParkFile::WriteGeneralChunk() WriteValue(gGuestInitialHunger); WriteValue(gGuestInitialThirst); - auto numSpawns = (gPeepSpawns[0].isNull() ? 1 : 0) + (gPeepSpawns[1].isNull() ? 1 : 0); - BeginArray(numSpawns, 13); + BeginArray(); for (const auto& spawn : gPeepSpawns) { if (!gPeepSpawns->isNull()) @@ -149,6 +175,7 @@ void ParkFile::WriteGeneralChunk() WriteValue(spawn.z); WriteValue(spawn.direction); } + NextArrayElement(); } EndArray(); diff --git a/src/openrct2/ParkFile.h b/src/openrct2/ParkFile.h index 3593030bba..31a837dc07 100644 --- a/src/openrct2/ParkFile.h +++ b/src/openrct2/ParkFile.h @@ -37,6 +37,10 @@ namespace OpenRCT2 std::vector _chunks; std::stringstream _buffer; ChunkEntry _currentChunk; + std::streampos _currentArrayStartPos; + std::streampos _currentArrayLastPos; + size_t _currentArrayCount; + size_t _currentArrayElementSize; void WriteHeader(std::ostream& fs); void BeginChunk(uint32_t id); @@ -44,7 +48,8 @@ namespace OpenRCT2 void WriteBuffer(const void* buffer, size_t len); void WriteString(const std::string_view& s); - void BeginArray(size_t count, size_t elementSize); + void BeginArray(); + void NextArrayElement(); void EndArray(); template