Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gamelogic): stop on a mismatch and name the object that diverged

Olcay Seyganfba0aafc1bf6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f8f9e5f3..c1c95b8d5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -185,6 +185,22 @@ map. Ctrl+A is still everything at once.
around, backs off when a link is genuinely down rather than hammering it, and never waits longer
than the two seconds it used to, so nobody ends up worse off than before.
+- **A desynced match stops, instead of playing on as two different games.** The game already
+ noticed when two machines stopped agreeing about the world — and then kept going anyway, letting
+ both sides play out a match that had already stopped being the same match, and telling you at the
+ score screen. Every order given after that point lands somewhere different on each screen. It now
+ stops the moment it notices, on every machine at once.
+- **A player leaving is no longer reported as a desync.** Someone quitting stays listed as present
+ for a few moments longer than their last heartbeat survives, and the game read that gap — a
+ packet that is not here yet — as proof that the two sides disagreed. Losing a player is now
+ losing a player.
+- **When a match does desync, the game writes down what happened.** Until now the whole report was
+ one number per player: they differ. The game now keeps a rolling record of every object in the
+ world — where it was, how hurt it was, and its own fingerprint — for the last several checkpoints,
+ and writes it out on both machines when the check fails. Lining up the two files points at the
+ single unit, building or projectile that went its own way, which is the difference between fixing
+ a desync and guessing at it.
+
## Sharper textures, for free
Zero Hour shipped downscaled copies of a lot of the base game's textures, and because of the order
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/CRCSnapshotRing.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/CRCSnapshotRing.h
new file mode 100644
index 000000000..c20e29f7d
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/CRCSnapshotRing.h
@@ -0,0 +1,86 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** This program is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation, either version 3 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// FILE: CRCSnapshotRing.h ////////////////////////////////////////////////////////////////////////
+// Evidence for a mismatch, kept before anyone knows there will be one.
+////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#ifndef _CRC_SNAPSHOT_RING_H_
+#define _CRC_SNAPSHOT_RING_H_
+
+#include "Lib/BaseType.h"
+#include <vector>
+
+/* A mismatch is reported as one number: "the world CRC differs". Worse, it is reported the
+ run-ahead's worth of frames after the frame it was computed for, by which time the world has
+ moved on and the divergent object is unrecoverable. So keep the evidence while it is free to
+ keep: as the world CRC is computed, write down the running CRC after each object, into a ring
+ of the last few CRC frames. Nothing reads the ring unless a mismatch is found, and then the
+ first object whose running CRC differs between two players' dumps is the object that diverged. */
+
+#define CRC_SNAPSHOT_RING_SIZE 16
+
+struct CRCObjectEntry
+{
+ UnsignedInt m_id; ///< the ObjectID, as a plain int so this file needs no engine types
+ UnsignedInt m_runningCRC; ///< world CRC after this object was folded in
+ Real m_x, m_y, m_z; ///< where it was, to read the dump without cross-referencing
+ Real m_health;
+};
+
+struct CRCSnapshot
+{
+ UnsignedInt m_frame;
+ UnsignedInt m_totalCRC;
+ UnsignedInt m_randomSeed;
+ Bool m_valid; ///< FALSE until endSnapshot() closes it
+ std::vector<CRCObjectEntry> m_objects;
+};
+
+//-------------------------------------------------------------------------------------------------
+/** A fixed ring of per-object CRC snapshots. Deliberately free of engine singletons so it can be
+ * tested on its own. */
+//-------------------------------------------------------------------------------------------------
+class CRCSnapshotRing
+{
+public:
+ CRCSnapshotRing( void );
+
+ void clear( void ); ///< forget everything (new game)
+
+ void beginSnapshot( UnsignedInt frame ); ///< start filling the next slot
+ void addObject( UnsignedInt id, UnsignedInt runningCRC,
+ Real x, Real y, Real z, Real health ); ///< record one object's running CRC
+ void endSnapshot( UnsignedInt totalCRC, UnsignedInt randomSeed ); ///< close the slot being filled
+
+ Int getNewestSlot( void ) const; ///< -1 when no snapshot is complete
+ Int getNthNewestSlot( Int n ) const; ///< n==0 is the newest; -1 when out of range
+ Int findSlotByCRC( UnsignedInt crc ) const; ///< newest first; -1 when no slot has that CRC
+ const CRCSnapshot *getSlot( Int slot ) const; ///< NULL for an out of range or unfilled slot
+
+ Int getNumSnapshots( void ) const { return m_numSnapshots; }
+
+private:
+ CRCSnapshot m_snapshots[ CRC_SNAPSHOT_RING_SIZE ];
+ Int m_writeSlot; ///< slot beginSnapshot() last opened, -1 before the first
+ Int m_numSnapshots; ///< how many slots hold real data
+};
+
+#endif // _CRC_SNAPSHOT_RING_H_
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
index dda843f3f..0e681ff16 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
@@ -38,6 +38,7 @@
#include "Common/STLTypedefs.h"
#include "Common/ObjectStatusTypes.h"
#include "GameNetwork/NetworkDefs.h"
+#include "GameLogic/CRCSnapshotRing.h"
#include "Common/STLTypedefs.h"
#include "GameLogic/Module/UpdateModule.h" // needed for DIRECT_UPDATEMODULE_ACCESS
@@ -296,6 +297,8 @@ private:
UnsignedInt m_CRC; ///< Cache of previous CRC value
std::map<Int, UnsignedInt> m_cachedCRCs; ///< CRCs we've seen this frame
Bool m_shouldValidateCRCs; ///< Should we validate CRCs this frame?
+ CRCSnapshotRing m_crcSnapshots; ///< per object CRCs for the last few CRC frames, for mismatch diagnosis
+ void writeMismatchDump( Int numPlayers ); ///< write down what the world looked like on the mismatching frame
//-----------------------------------------------------------------------------------------------
//Added By Sadullah Nader
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/CRCSnapshotRing.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/CRCSnapshotRing.cpp
new file mode 100644
index 000000000..fd294d91c
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/CRCSnapshotRing.cpp
@@ -0,0 +1,155 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** This program is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation, either version 3 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// FILE: CRCSnapshotRing.cpp //////////////////////////////////////////////////////////////////////
+
+#include "PreRTS.h"
+
+#include "GameLogic/CRCSnapshotRing.h"
+
+//-------------------------------------------------------------------------------------------------
+CRCSnapshotRing::CRCSnapshotRing( void )
+{
+ clear();
+}
+
+//-------------------------------------------------------------------------------------------------
+void CRCSnapshotRing::clear( void )
+{
+ for( Int i = 0; i < CRC_SNAPSHOT_RING_SIZE; ++i )
+ {
+ m_snapshots[i].m_frame = 0;
+ m_snapshots[i].m_totalCRC = 0;
+ m_snapshots[i].m_randomSeed = 0;
+ m_snapshots[i].m_valid = FALSE;
+ m_snapshots[i].m_objects.clear();
+ }
+ m_writeSlot = -1;
+ m_numSnapshots = 0;
+}
+
+//-------------------------------------------------------------------------------------------------
+void CRCSnapshotRing::beginSnapshot( UnsignedInt frame )
+{
+ m_writeSlot = (m_writeSlot + 1) % CRC_SNAPSHOT_RING_SIZE;
+
+ CRCSnapshot &snap = m_snapshots[ m_writeSlot ];
+
+ /* The oldest slot is being reused; if it held a complete snapshot it no longer counts as one
+ until this snapshot closes. */
+ if( snap.m_valid )
+ --m_numSnapshots;
+
+ snap.m_frame = frame;
+ snap.m_totalCRC = 0;
+ snap.m_randomSeed = 0;
+ snap.m_valid = FALSE;
+ // keep the vector's capacity - the object count barely changes from one CRC frame to the next
+ snap.m_objects.clear();
+}
+
+//-------------------------------------------------------------------------------------------------
+void CRCSnapshotRing::addObject( UnsignedInt id, UnsignedInt runningCRC,
+ Real x, Real y, Real z, Real health )
+{
+ if( m_writeSlot < 0 )
+ return;
+
+ CRCObjectEntry entry;
+ entry.m_id = id;
+ entry.m_runningCRC = runningCRC;
+ entry.m_x = x;
+ entry.m_y = y;
+ entry.m_z = z;
+ entry.m_health = health;
+
+ m_snapshots[ m_writeSlot ].m_objects.push_back( entry );
+}
+
+//-------------------------------------------------------------------------------------------------
+void CRCSnapshotRing::endSnapshot( UnsignedInt totalCRC, UnsignedInt randomSeed )
+{
+ if( m_writeSlot < 0 )
+ return;
+
+ CRCSnapshot &snap = m_snapshots[ m_writeSlot ];
+ snap.m_totalCRC = totalCRC;
+ snap.m_randomSeed = randomSeed;
+ snap.m_valid = TRUE;
+ ++m_numSnapshots;
+}
+
+//-------------------------------------------------------------------------------------------------
+Int CRCSnapshotRing::getNewestSlot( void ) const
+{
+ if( m_writeSlot < 0 )
+ return -1;
+
+ // only the slot being filled can be incomplete, so at worst we step back over that one
+ for( Int k = 0; k < CRC_SNAPSHOT_RING_SIZE; ++k )
+ {
+ Int slot = m_writeSlot - k;
+ while( slot < 0 )
+ slot += CRC_SNAPSHOT_RING_SIZE;
+
+ if( m_snapshots[ slot ].m_valid )
+ return slot;
+ }
+
+ return -1;
+}
+
+//-------------------------------------------------------------------------------------------------
+Int CRCSnapshotRing::getNthNewestSlot( Int n ) const
+{
+ Int newest = getNewestSlot();
+ if( newest < 0 || n < 0 || n >= m_numSnapshots )
+ return -1;
+
+ Int slot = newest - n;
+ while( slot < 0 )
+ slot += CRC_SNAPSHOT_RING_SIZE;
+
+ return m_snapshots[ slot ].m_valid ? slot : -1;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** Our own CRC for the mismatching frame is one of the values that were just compared, so the slot
+ * carrying it names the frame the mismatch happened on - which is several frames behind the frame
+ * it was detected on. */
+//-------------------------------------------------------------------------------------------------
+Int CRCSnapshotRing::findSlotByCRC( UnsignedInt crc ) const
+{
+ for( Int n = 0; n < m_numSnapshots; ++n )
+ {
+ Int slot = getNthNewestSlot( n );
+ if( slot >= 0 && m_snapshots[ slot ].m_totalCRC == crc )
+ return slot;
+ }
+
+ return -1;
+}
+
+//-------------------------------------------------------------------------------------------------
+const CRCSnapshot *CRCSnapshotRing::getSlot( Int slot ) const
+{
+ if( slot < 0 || slot >= CRC_SNAPSHOT_RING_SIZE || !m_snapshots[ slot ].m_valid )
+ return NULL;
+
+ return &m_snapshots[ slot ];
+}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index 2e0422893..aaada1ce9 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -406,6 +406,7 @@ void GameLogic::init( void )
//ThePlayerList->setLocalPlayer(0);
m_CRC = 0;
+ m_crcSnapshots.clear();
m_gamePaused = FALSE;
m_inputEnabledMemory = TRUE;
m_mouseVisibleMemory = TRUE;
@@ -460,6 +461,7 @@ void GameLogic::reset( void )
TheScriptEngine->reset();
m_CRC = 0;
+ m_crcSnapshots.clear();
for(Int i = 0; i < MAX_SLOTS; ++i)
{
m_progressComplete[i] = FALSE;
@@ -2567,10 +2569,15 @@ void GameLogic::processCommandList( CommandList *list )
++numPlayers;
}
- if (m_cachedCRCs.size() < numPlayers)
+ /* A player who leaves stays "connected" for a few more frames than their last CRC
+ message survives, and at a short CRC interval that gap is hit routinely. Missing CRCs
+ are not evidence that anybody computed anything differently - they are evidence that a
+ packet is not here yet. Say so and let the next CRC frame decide, instead of blaming
+ the leaver for a desync that never happened. */
+ if (m_cachedCRCs.size() < (size_t)numPlayers)
{
- DEBUG_CRASH(("Not enough CRCs!"));
- sawCRCMismatch = TRUE;
+ DEBUG_LOG(("Only %d CRCs from %d connected players on frame %d - not comparing\n",
+ m_cachedCRCs.size(), numPlayers, m_frame));
}
else
{
@@ -2603,11 +2610,102 @@ void GameLogic::processCommandList( CommandList *list )
}
#endif DEBUG_LOGGING
TheNetwork->setSawCRCMismatch();
+
+ // write down what the world looked like on the frame that diverged, while we still have it
+ writeMismatchDump( numPlayers );
+
+ /* Playing on is the worst of the options: two worlds that already disagree keep diverging
+ for the rest of the match, every order after this point lands somewhere different on
+ each machine, and the players only find out at the score screen. Stop here instead -
+ every machine detects the same mismatch on the same frame, so everyone stops together. */
+ TheMessageStream->appendMessage( GameMessage::MSG_CLEAR_GAME_DATA );
}
}
} // end processCommandList
+// ------------------------------------------------------------------------------------------------
+/** A mismatch says the worlds diverged; it does not say where. This writes down everything we
+ * kept about the frame that diverged, so that two players' dumps can be diffed: the first object
+ * whose running CRC differs is the object whose state differs, and the fields printed next to it
+ * usually say how. Every machine writes its own file at the same frame. */
+// ------------------------------------------------------------------------------------------------
+void GameLogic::writeMismatchDump( Int numPlayers )
+{
+ /* The CRCs just compared were computed the run-ahead's worth of frames ago. Ours is one of
+ them, so whichever retained snapshot carries one of these values names that frame exactly. */
+ Int slot = -1;
+ for( std::map<Int, UnsignedInt>::const_iterator crcIt = m_cachedCRCs.begin();
+ crcIt != m_cachedCRCs.end() && slot < 0; ++crcIt )
+ {
+ slot = m_crcSnapshots.findSlotByCRC( crcIt->second );
+ }
+ if( slot < 0 )
+ slot = m_crcSnapshots.getNewestSlot();
+
+ AsciiString fname = TheGlobalData->getPath_UserData();
+ fname.concat( "MismatchDump.txt" );
+
+ // a file the user's disk may refuse to give us - the one place a guard is earned
+ FILE *fp = fopen( fname.str(), "w" );
+ if( fp == NULL )
+ {
+ DEBUG_LOG(( "writeMismatchDump - could not open %s\n", fname.str() ));
+ return;
+ }
+
+ fprintf( fp, "Mismatch detected on frame %d\n", m_frame );
+ fprintf( fp, "Local player is slot %d of %d connected players; %d CRCs were compared\n\n",
+ TheNetwork ? TheNetwork->getLocalPlayerID() : 0, numPlayers, m_cachedCRCs.size() );
+
+ for( std::map<Int, UnsignedInt>::const_iterator crcIt = m_cachedCRCs.begin();
+ crcIt != m_cachedCRCs.end(); ++crcIt )
+ {
+ Player *player = ThePlayerList->getNthPlayer( crcIt->first );
+ AsciiString name;
+ if( player )
+ name.translate( player->getPlayerDisplayName() );
+ fprintf( fp, " player %d (%s) CRC 0x%8.8X\n", crcIt->first, name.str(), crcIt->second );
+ }
+
+ const CRCSnapshot *snap = m_crcSnapshots.getSlot( slot );
+ if( snap == NULL )
+ {
+ fprintf( fp, "\nNo CRC snapshot was retained for this frame.\n" );
+ fclose( fp );
+ return;
+ }
+
+ fprintf( fp, "\nDiverging frame is %d (world CRC 0x%8.8X, logic random seed CRC 0x%8.8X, %d objects)\n",
+ snap->m_frame, snap->m_totalCRC, snap->m_randomSeed, snap->m_objects.size() );
+ fprintf( fp, "Diff this section against another player's file; the first differing running CRC is the object that diverged.\n\n" );
+ fprintf( fp, "%6s %8s %10s %12s %12s %12s %10s %s\n",
+ "index", "objectID", "runningCRC", "x", "y", "z", "health", "template" );
+
+ for( size_t i = 0; i < snap->m_objects.size(); ++i )
+ {
+ const CRCObjectEntry &e = snap->m_objects[i];
+ Object *obj = findObjectByID( (ObjectID)e.m_id );
+ const char *templateName = obj ? obj->getTemplate()->getName().str() : "<destroyed since>";
+ fprintf( fp, "%6d %8d 0x%8.8X %12.4f %12.4f %12.4f %10.3f %s\n",
+ i, e.m_id, e.m_runningCRC, e.m_x, e.m_y, e.m_z, e.m_health, templateName );
+ }
+
+ fprintf( fp, "\nOther retained CRC frames:\n" );
+ for( Int n = 0; n < m_crcSnapshots.getNumSnapshots(); ++n )
+ {
+ const CRCSnapshot *other = m_crcSnapshots.getSlot( m_crcSnapshots.getNthNewestSlot( n ) );
+ if( other == NULL || other == snap )
+ continue;
+ fprintf( fp, " frame %d: world CRC 0x%8.8X, %d objects\n",
+ other->m_frame, other->m_totalCRC, other->m_objects.size() );
+ }
+
+ fclose( fp );
+
+ DEBUG_LOG(( "Mismatch on frame %d (diverging frame %d) - wrote %s\n", m_frame, snap->m_frame, fname.str() ));
+}
+
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Bool GameLogic::isIntroMoviePlaying()
@@ -4183,11 +4281,26 @@ UnsignedInt GameLogic::getCRC( Int mode, AsciiString deepCRCFileName )
CRCGEN_LOG(("CRC at start of frame %d is 0x%8.8X\n", m_frame, xferCRC->getCRC()));
}
+ /* Keep the evidence for a mismatch nobody knows about yet: the running CRC after each object,
+ for the last few CRC frames. It costs one vector fill on the frames the game already CRCs
+ itself on, and it is the difference between "the world diverged" and "this object diverged".
+ See CRCSnapshotRing.h. */
+ Bool recordSnapshot = isInGameLogicUpdate() && TheRecorder && TheRecorder->isMultiplayer();
+ if (recordSnapshot)
+ m_crcSnapshots.beginSnapshot( m_frame );
+
marker = "MARKER:Objects";
xferCRC->xferAsciiString(&marker);
for( obj = m_objList; obj; obj=obj->getNextObject() )
{
xferCRC->xferSnapshot( obj );
+ if (recordSnapshot)
+ {
+ const Coord3D *pos = obj->getPosition();
+ BodyModuleInterface *body = obj->getBodyModule();
+ m_crcSnapshots.addObject( (UnsignedInt)obj->getID(), xferCRC->getCRC(),
+ pos->x, pos->y, pos->z, body->getHealth() );
+ }
}
UnsignedInt seed = GetGameLogicRandomSeedCRC();
if (isInGameLogicUpdate())
@@ -4255,6 +4368,9 @@ UnsignedInt GameLogic::getCRC( Int mode, AsciiString deepCRCFileName )
delete xferCRC;
xferCRC = NULL;
+ if (recordSnapshot)
+ m_crcSnapshots.endSnapshot( theCRC, seed );
+
if (isInGameLogicUpdate())
{
CRCGEN_LOG(("CRC for frame %d is 0x%8.8X\n", m_frame, theCRC));
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index c5015ba3a..5140e4478 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -28,6 +28,7 @@
#include "Common/STLTypedefs.h"
#include "Common/StackDump.h"
#include "GameNetwork/Connection.h"
+#include "GameLogic/CRCSnapshotRing.h"
#include "GameClient/Water.h"
#include "GameLogic/Module/PhysicsUpdate.h"
#include "GameClient/ParticleSys.h"
@@ -1930,3 +1931,108 @@ TEST(connection_retry_backs_off_when_a_command_keeps_going_unacked)
/* A command that has never been sent is not a retry, and must not read as a negative shift. */
CHECK_EQ( (Int)Connection_retryDelayFor( 200, 0 ), 200 );
}
+
+// ------------------------------------------------------------------------------------------------
+// CRCSnapshotRing - the evidence a mismatch report is missing. A mismatch is detected several
+// frames after the frame it happened on, so the ring has to still hold that frame when asked, and
+// it has to be able to name it from nothing but a CRC value.
+// ------------------------------------------------------------------------------------------------
+
+static void fillSnapshot( CRCSnapshotRing &ring, UnsignedInt frame, UnsignedInt totalCRC, Int numObjects )
+{
+ ring.beginSnapshot( frame );
+ for( Int i = 0; i < numObjects; ++i )
+ ring.addObject( 100 + i, 0xAB000000 + frame * 1000 + i, (Real)i, (Real)(i * 2), 0.0f, 50.0f + i );
+ ring.endSnapshot( totalCRC, 0xF00D0000 + frame );
+}
+
+TEST(crcsnapshotring_names_the_diverging_frame_from_a_crc_value)
+{
+ CRCSnapshotRing ring;
+ CHECK_EQ( ring.getNewestSlot(), -1 );
+ CHECK_EQ( ring.getNumSnapshots(), 0 );
+
+ fillSnapshot( ring, 30, 0x11111111, 3 );
+ fillSnapshot( ring, 60, 0x22222222, 4 );
+ fillSnapshot( ring, 90, 0x33333333, 5 );
+
+ CHECK_EQ( ring.getNumSnapshots(), 3 );
+
+ // the mismatch is reported on a later frame; all we have to go on is our own CRC for that frame
+ Int slot = ring.findSlotByCRC( 0x22222222 );
+ CHECK( slot >= 0 );
+
+ const CRCSnapshot *snap = ring.getSlot( slot );
+ CHECK( snap != NULL );
+ if( snap == NULL )
+ return;
+ CHECK_EQ( (Int)snap->m_frame, 60 );
+ CHECK_EQ( (Int)snap->m_objects.size(), 4 );
+ CHECK_EQ( (Int)snap->m_randomSeed, (Int)(0xF00D0000 + 60) );
+
+ // the per object running CRCs are what a diff between two players' dumps compares
+ CHECK_EQ( (Int)snap->m_objects[0].m_id, 100 );
+ CHECK_EQ( (Int)snap->m_objects[3].m_id, 103 );
+ CHECK_EQ( (Int)snap->m_objects[2].m_runningCRC, (Int)(0xAB000000 + 60 * 1000 + 2) );
+ CHECK_NEAR( snap->m_objects[3].m_health, 53.0f, 0.001f );
+
+ // a CRC nobody recorded names no frame at all, rather than the wrong one
+ CHECK_EQ( ring.findSlotByCRC( 0x44444444 ), -1 );
+}
+
+TEST(crcsnapshotring_still_holds_the_frame_a_late_mismatch_report_asks_for)
+{
+ CRCSnapshotRing ring;
+
+ // the run ahead can be 64 frames; at the default CRC interval the ring has to outlive that
+ for( Int i = 1; i <= CRC_SNAPSHOT_RING_SIZE; ++i )
+ fillSnapshot( ring, i * 10, 0x1000 + i, 2 );
+
+ CHECK_EQ( ring.getNumSnapshots(), CRC_SNAPSHOT_RING_SIZE );
+ CHECK( ring.findSlotByCRC( 0x1000 + 1 ) >= 0 );
+
+ // one more frame pushes the oldest out, and nothing else
+ fillSnapshot( ring, (CRC_SNAPSHOT_RING_SIZE + 1) * 10, 0x2000, 2 );
+ CHECK_EQ( ring.getNumSnapshots(), CRC_SNAPSHOT_RING_SIZE );
+ CHECK_EQ( ring.findSlotByCRC( 0x1000 + 1 ), -1 );
+ CHECK( ring.findSlotByCRC( 0x1000 + 2 ) >= 0 );
+ CHECK( ring.findSlotByCRC( 0x2000 ) >= 0 );
+
+ // newest first ordering survives the wrap
+ const CRCSnapshot *newest = ring.getSlot( ring.getNthNewestSlot( 0 ) );
+ const CRCSnapshot *older = ring.getSlot( ring.getNthNewestSlot( 1 ) );
+ CHECK( newest != NULL && older != NULL );
+ if( newest == NULL || older == NULL )
+ return;
+ CHECK_EQ( (Int)newest->m_frame, (CRC_SNAPSHOT_RING_SIZE + 1) * 10 );
+ CHECK_EQ( (Int)older->m_frame, CRC_SNAPSHOT_RING_SIZE * 10 );
+ CHECK_EQ( ring.getNthNewestSlot( CRC_SNAPSHOT_RING_SIZE ), -1 );
+ CHECK_EQ( ring.getNthNewestSlot( -1 ), -1 );
+}
+
+TEST(crcsnapshotring_never_hands_back_a_half_written_frame)
+{
+ CRCSnapshotRing ring;
+ fillSnapshot( ring, 30, 0x11111111, 3 );
+
+ // a CRC pass that starts but does not finish must not become the newest snapshot
+ ring.beginSnapshot( 60 );
+ ring.addObject( 100, 0xDEADBEEF, 0.0f, 0.0f, 0.0f, 1.0f );
+
+ CHECK_EQ( ring.getNumSnapshots(), 1 );
+ const CRCSnapshot *snap = ring.getSlot( ring.getNewestSlot() );
+ CHECK( snap != NULL );
+ if( snap == NULL )
+ return;
+ CHECK_EQ( (Int)snap->m_frame, 30 );
+ CHECK_EQ( ring.findSlotByCRC( 0x11111111 ), ring.getNewestSlot() );
+
+ ring.clear();
+ CHECK_EQ( ring.getNumSnapshots(), 0 );
+ CHECK_EQ( ring.getNewestSlot(), -1 );
+ CHECK_EQ( ring.findSlotByCRC( 0x11111111 ), -1 );
+
+ // addObject with no snapshot open is a no-op, not a write through a bad index
+ ring.addObject( 1, 2, 0.0f, 0.0f, 0.0f, 0.0f );
+ CHECK_EQ( ring.getNumSnapshots(), 0 );
+}