From 795cdaa53e6c485c58f21e52f32848b704aaef76 Mon Sep 17 00:00:00 2001 From: Michael Steenbeek <1478678+Gymnasiast@users.noreply.github.com> Date: Sun, 18 Jan 2026 14:18:44 +0100 Subject: [PATCH] Fix #23636: Mismatched entity count in save file (#25731) This is based on #24552, with two changes: 1. The entity list is copied and used for both the count and the actual writes 2. It no longer asserts if the count mismatches (which is why #24552 was not merged). Co-authored-by: ZehMatt --- src/openrct2/park/ParkFile.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/openrct2/park/ParkFile.cpp b/src/openrct2/park/ParkFile.cpp index 438bbe557c..c5c3dba85c 100644 --- a/src/openrct2/park/ParkFile.cpp +++ b/src/openrct2/park/ParkFile.cpp @@ -2639,10 +2639,19 @@ namespace OpenRCT2 template void ParkFile::WriteEntitiesOfType(GameState_t& gameState, OrcaStream& os, OrcaStream::ChunkStream& cs) { - uint16_t count = gameState.entities.GetEntityListCount(T::cEntityType); + const auto entityList = EntityList(); + // We do not rely on GetEntityListCount as there is a potential issue (#23636) that needs investigating. + const auto count = [&]() -> uint16_t { + uint16_t res = 0; + for ([[maybe_unused]] auto* _ : entityList) + { + res++; + } + return res; + }(); cs.write(T::cEntityType); cs.write(count); - for (auto* ent : EntityList()) + for (auto* ent : entityList) { cs.write(ent->Id); ReadWriteEntity(os, cs, *ent);