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): never record the replay that is being played back

Olcay Seygan85b3cb1fecdd
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
index f5d325fad..a4cfb5ef8 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
@@ -84,6 +84,16 @@ enum
GAME_NONE
};
+/* Which of those modes is worth writing a replay for. EA asked this the other way round, in
+ RecorderClass::updateRecord(), as a list of the modes *not* to record - shell, single player and
+ none - which quietly said yes to GAME_REPLAY: a replay game whose recorder is in mode NONE (the
+ -replay path used to leave it there) started recording the replay it was playing, over the file
+ it was reading. A whitelist cannot make that mistake, and a mode added later has to opt in. */
+inline Bool isRecordableGameMode( Int gameMode )
+{
+ return gameMode == GAME_LAN || gameMode == GAME_SKIRMISH || gameMode == GAME_INTERNET;
+}
+
enum
{
CRC_CACHED,
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
index bdab7facf..dde1eca89 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
@@ -483,9 +483,10 @@ void RecorderClass::updateRecord()
GameMessage *msg = TheCommandList->getFirstMessage();
while (msg != NULL) {
if (msg->getType() == GameMessage::MSG_NEW_GAME &&
- msg->getArgument(0)->integer != GAME_SHELL &&
- msg->getArgument(0)->integer != GAME_SINGLE_PLAYER && // Due to the massive amount of scripts that use <local player> in GC and single player, replays have been cut for them.
- msg->getArgument(0)->integer != GAME_NONE)
+ // Due to the massive amount of scripts that use <local player> in GC and single player,
+ // replays have been cut for them - so shell, single player, none and replay itself are
+ // all out. See isRecordableGameMode() in GameLogic.h.
+ isRecordableGameMode( msg->getArgument(0)->integer ))
{
m_originalGameMode = msg->getArgument(0)->integer;
DEBUG_LOG(("RecorderClass::updateRecord() - original game is mode %d\n", m_originalGameMode));
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index a76950f60..7fb38942c 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -48,6 +48,7 @@
#include <float.h>
#include "Common/AudioRandomValue.h"
#include "GameNetwork/CrcAgreement.h"
+#include "GameLogic/GameLogic.h"
#include "Common/RadarShroudCache.h"
#include "GameClient/Gadget.h"
#include "GameNetwork/NetworkUtil.h"
@@ -5302,3 +5303,25 @@ TEST(headless_run_ends_on_a_decision_or_on_the_frame_limit)
// a real result outranks the limit on the frame they land together
CHECK_STR( GameEngine_headlessRunResult( 3000, 2000, 3000 ), "decided" );
}
+
+TEST(a_replay_game_is_never_itself_recorded)
+{
+ // what a replay is worth writing: a game a human played in real time
+ CHECK( isRecordableGameMode( GAME_LAN ) );
+ CHECK( isRecordableGameMode( GAME_SKIRMISH ) );
+ CHECK( isRecordableGameMode( GAME_INTERNET ) );
+
+ // EA's three exclusions, kept
+ CHECK( !isRecordableGameMode( GAME_SHELL ) );
+ CHECK( !isRecordableGameMode( GAME_SINGLE_PLAYER ) );
+ CHECK( !isRecordableGameMode( GAME_NONE ) );
+
+ /* the one the blacklist let through. RecorderClass::update() runs updateRecord() when the mode
+ is NONE as well as RECORD, so a replay game started while the recorder sat in NONE used to
+ start a recording of the replay - over the file being played back. */
+ CHECK( !isRecordableGameMode( GAME_REPLAY ) );
+
+ // and a mode nobody has invented yet has to opt in rather than default to being recorded
+ CHECK( !isRecordableGameMode( GAME_NONE + 1 ) );
+ CHECK( !isRecordableGameMode( -1 ) );
+}