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(gameengine): stop a network replay reporting a desync it does not have

Olcay Seygan9126172aa6cd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1df57b872..f4d5cdbfd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1158,7 +1158,13 @@ gives up the reason.
**And it can play itself over a network.** Two copies of the game on one machine, on opposite ends
of a real connection, playing the same match against each other with no lobby and nobody clicking
anything. That is the only way to reach the faults that live in a multiplayer game and nowhere else
-— the ones where two machines quietly stop agreeing about what just happened.
+— the ones where two machines quietly stop agreeing about what just happened. It turned one up on
+its first outing, and not the one it was sent to look for: every recorded multiplayer game had been
+accusing itself of falling out of step when you watched it back, since 2003, on virtually every
+replay. None of them ever had. The recordings were right; the check reading them back was a single
+moment out, from the first frame of every match, and the original team wrote that suspicion into a
+comment and left it there. Multiplayer replays are trustworthy now, and on the day one really does
+go wrong it says so out loud instead of silently.
**Every fix was proved non-vacuous.** Put the bug back, watch the exact test fail, restore the fix.
A test that passes both ways is worth nothing, and there are none of those here.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/Recorder.h b/GeneralsMD/Code/GameEngine/Include/Common/Recorder.h
index e3c7cdee6..7298439e9 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/Recorder.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/Recorder.h
@@ -25,6 +25,7 @@
#pragma once
+#include <list>
#include "Common/MessageStream.h"
#include "GameNetwork/GameInfo.h"
@@ -51,7 +52,45 @@ enum RecorderModeType {
RECORDERMODETYPE_NONE // this is a valid state to be in on the shell map, or in saved games
};
-class CRCInfo;
+/**
+ * The CRCs of a replay. Playback recomputes the logic CRC on every interval frame and pushes
+ * it here; every CRC that comes back out of the replay file is compared against the front of
+ * the queue. The two streams therefore have to line up entry for entry.
+ */
+class CRCInfo
+{
+public:
+ CRCInfo();
+ void addCRC(UnsignedInt val);
+ UnsignedInt readCRC(void);
+
+ void setLocalPlayer(UnsignedInt index) { m_localPlayer = index; }
+ UnsignedInt getLocalPlayer(void) { return m_localPlayer; }
+
+ void setSawCRCMismatch(void) { m_sawCRCMismatch = TRUE; }
+ Bool sawCRCMismatch(void) { return m_sawCRCMismatch; }
+
+ /// throw the next CRC away instead of queueing it - see replayIsMissingFirstCRC()
+ void skipFirstCRC(void) { m_skipOneCRC = TRUE; }
+
+protected:
+
+ Bool m_sawCRCMismatch;
+ Bool m_skipOneCRC;
+ std::list<UnsignedInt> m_data;
+ UnsignedInt m_localPlayer;
+};
+
+/**
+ * TRUE if a replay recorded in this game mode has no CRC for frame 0. A network game's CRCs
+ * travel as commands, and the one the logic makes on frame 0 is generated after that frame's
+ * commands have already gone out, so it is never sent, never executed and never recorded - the
+ * replay's CRC stream starts at frame 1. Playback has no network to lose it to and produces a
+ * frame 0 CRC of its own, so without dropping one the whole comparison sits one frame out and a
+ * replay that is perfectly in sync reports a desync on its first interval frame. Games recorded
+ * off the network - solo and skirmish - keep their frame 0 CRC and must not be shifted.
+ */
+Bool replayIsMissingFirstCRC( Int originalGameMode );
class RecorderClass : public SubsystemInterface {
public:
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
index dde1eca89..aba4a39d2 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
@@ -953,41 +953,25 @@ AsciiString RecorderClass::getCurrentReplayFilename( void )
return AsciiString::TheEmptyString;
}
-class CRCInfo
-{
-public:
- CRCInfo();
- void addCRC(UnsignedInt val);
- UnsignedInt readCRC(void);
-
- void setLocalPlayer(UnsignedInt index) { m_localPlayer = index; }
- UnsignedInt getLocalPlayer(void) { return m_localPlayer; }
-
- void setSawCRCMismatch(void) { m_sawCRCMismatch = TRUE; }
- Bool sawCRCMismatch(void) { return m_sawCRCMismatch; }
-
-protected:
-
- Bool m_sawCRCMismatch;
- Bool m_skippedOne;
- std::list<UnsignedInt> m_data;
- UnsignedInt m_localPlayer;
-};
-
CRCInfo::CRCInfo()
{
m_localPlayer = ~0;
- m_skippedOne = FALSE;
+ m_skipOneCRC = FALSE;
m_sawCRCMismatch = FALSE;
}
+Bool replayIsMissingFirstCRC( Int originalGameMode )
+{
+ return originalGameMode == GAME_LAN || originalGameMode == GAME_INTERNET;
+}
+
void CRCInfo::addCRC(UnsignedInt val)
{
- //if (!m_skippedOne)
- //{
- // m_skippedOne = TRUE;
- // return;
- //}
+ if (m_skipOneCRC)
+ {
+ m_skipOneCRC = FALSE;
+ return;
+ }
m_data.push_back(val);
//DEBUG_LOG(("CRCInfo::addCRC() - crc %8.8X pushes list to %d entries (full=%d)\n", val, m_data.size(), !m_data.empty()));
@@ -1031,6 +1015,12 @@ void RecorderClass::handleCRCMessage(UnsignedInt newCRC, Int playerIndex, Bool f
{
m_crcInfo->setSawCRCMismatch();
+ /* The DEBUG_CRASH below is compiled out of a release build, so a replay that played back a
+ different game than the one that was recorded used to do it in complete silence. Say so in
+ the log, which release builds do write. */
+ DEBUG_LOG(("Replay has gone out of sync on frame %d: recorded %8.8X, played back %8.8X\n",
+ TheGameLogic->getFrame(), playbackCRC, newCRC));
+
//Kris: Patch 1.01 November 10, 2003 (integrated changes from Matt Campbell)
// Since we don't seem to have any *visible* desyncs when replaying games, but get this warning
// virtually every replay, the assumption is our CRC checking is faulty. Since we're at the
@@ -1171,6 +1161,11 @@ Bool RecorderClass::playbackFile(AsciiString filename)
DEBUG_LOG(("RecorderClass::playbackFile() - original game was mode %d\n", m_originalGameMode));
+ if (replayIsMissingFirstCRC( m_originalGameMode ))
+ {
+ m_crcInfo->skipFirstCRC();
+ }
+
readNextFrame();
// send a message to the logic for a new game
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 81ad4e35a..e9c8866e2 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -51,6 +51,7 @@
#include "GameLogic/GameLogic.h"
#include "Common/EarlyCommandLine.h"
#include "GameNetwork/NetworkUtil.h"
+#include "Common/Recorder.h"
#include "Common/RadarShroudCache.h"
#include "GameClient/Gadget.h"
#include "GameNetwork/NetworkUtil.h"
@@ -5391,3 +5392,41 @@ TEST(a_netgame_slot_list_is_the_player_order_on_every_machine)
// a stray separator is not a slot - the list is whatever stands between the commas
CHECK_EQ( 2, ResolveHostList( "10.0.0.1,,10.0.0.2", ips, MAX_SLOTS ) );
}
+
+/* A replay is checked by comparing the CRCs it carries against the ones playback recomputes, one
+ for one, out of a queue. A game played over a network never gets its frame 0 CRC into the file
+ - the logic makes it after that frame's commands have already gone out, so it is never sent,
+ never executed and never recorded - and playback, having no network to lose it to, makes one
+ anyway. Unless playback throws that one away every comparison after it is a frame out, and a
+ replay that is perfectly in sync reports a desync on its first interval frame. */
+TEST(replay_crc_queue_drops_the_frame_the_network_never_recorded)
+{
+ // only a game that was played over a network is missing that first CRC
+ CHECK( replayIsMissingFirstCRC( GAME_LAN ) );
+ CHECK( replayIsMissingFirstCRC( GAME_INTERNET ) );
+ CHECK( !replayIsMissingFirstCRC( GAME_SKIRMISH ) );
+ CHECK( !replayIsMissingFirstCRC( GAME_SINGLE_PLAYER ) );
+ CHECK( !replayIsMissingFirstCRC( GAME_REPLAY ) );
+ CHECK( !replayIsMissingFirstCRC( GAME_SHELL ) );
+ CHECK( !replayIsMissingFirstCRC( GAME_NONE ) );
+
+ // left alone the queue hands back what it was given, in order
+ CRCInfo solo;
+ solo.addCRC( 0x11111111 );
+ solo.addCRC( 0x22222222 );
+ solo.addCRC( 0x33333333 );
+ CHECK_EQ( 0x11111111, solo.readCRC() );
+ CHECK_EQ( 0x22222222, solo.readCRC() );
+ CHECK_EQ( 0x33333333, solo.readCRC() );
+ CHECK_EQ( 0, solo.readCRC() ); // an empty queue reads as 0
+
+ // armed, it swallows exactly one - the frame the recording is missing - and no more
+ CRCInfo net;
+ net.skipFirstCRC();
+ net.addCRC( 0x11111111 );
+ net.addCRC( 0x22222222 );
+ net.addCRC( 0x33333333 );
+ CHECK_EQ( 0x22222222, net.readCRC() );
+ CHECK_EQ( 0x33333333, net.readCRC() );
+ CHECK_EQ( 0, net.readCRC() );
+}