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.

feat(gameengine): play a replay from the command line (-replay)

Olcay Seygan4122c361d812
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index c3fe0e1a2..8fbdd3870 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -1162,6 +1162,25 @@ Int parseMaxGameFrames(char *args[], int num)
return 2;
}
+/* -replay <file> plays a replay back without the menus, the way -autoskirmish starts a match
+ without them. The only route into playback was ReplayMenu's list box and the _DEBUG/_INTERNAL
+ -file switch, so a Release build could record a game and then had no way to play it back
+ unattended - which is exactly what checking that a multiplayer replay still plays needs.
+
+ The name is resolved against the replay directory by RecorderClass::readReplayHeader, so pass
+ the bare file name; the extension is optional. */
+Int parseReplay(char *args[], int num)
+{
+ if (TheWritableGlobalData && num > 1)
+ {
+ AsciiString name = args[1];
+ if (!name.endsWithNoCase(".rep"))
+ name.concat(".rep");
+ TheWritableGlobalData->m_initialFile = name;
+ }
+ return 2;
+}
+
Int parseDumpAssetUsage(char *args[], int num)
{
if (TheWritableGlobalData)
@@ -1351,6 +1370,7 @@ static CommandLineParam params[] =
{ "-observer", parseObserver },
{ "-headless", parseHeadless },
{ "-maxframes", parseMaxGameFrames },
+ { "-replay", parseReplay },
//-allAdvice feature
//{ "-allAdvice", parseAllAdvice },
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index e7244eeab..616505e08 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -256,6 +256,10 @@ void GameEngine::setFramesPerSecondLimit( Int fps )
m_maxFPS = fps;
}
+/* -replay <file>: the name the command line asked for, opened after init()'s resetAll(). See
+ the .rep branch in init() for why it cannot be opened where it is parsed. */
+static AsciiString thePendingReplayFile;
+
/** -----------------------------------------------------------------------------------------------
* -autoskirmish <n>: build a skirmish slot list from the command line and launch it without going
* through the menus. This is reallyDoStart() in SkirmishGameOptionsMenu.cpp minus the GUI: the
@@ -768,7 +772,19 @@ void GameEngine::init( int argc, char *argv[] )
}
else if (fname.endsWithNoCase(".rep"))
{
- TheRecorder->playbackFile(fname);
+ /* Playback cannot start here. init() ends with TheSubsystemList->resetAll(), and
+ RecorderClass::reset() closes the file it is playing back and drops the mode to
+ NONE - after which the queued MSG_NEW_GAME starts a replay game with no command
+ stream behind it and the recorder, seeing mode NONE, starts *recording* that empty
+ game over the replay it was asked to play. The menu path is unaffected because by
+ then the reset is long past. So remember the name and open it below.
+
+ The shell map goes off for the same reason the .map branch turns it off: it is a
+ live game of its own, and a replay that fails to open would otherwise leave the
+ process sitting in it, which from the outside looks exactly like a hung playback. */
+ TheWritableGlobalData->m_shellMapOn = FALSE;
+ TheWritableGlobalData->m_playIntro = FALSE;
+ thePendingReplayFile = fname;
}
}
else if (TheGlobalData->m_autoSkirmishPlayers > 0)
@@ -830,6 +846,19 @@ void GameEngine::init( int argc, char *argv[] )
TheSubsystemList->resetAll();
HideControlBar();
+
+ /* Now that every subsystem has been reset, the recorder can be handed the replay and keep
+ it: the file stays open, the mode stays PLAYBACK, and the MSG_NEW_GAME appended here is
+ the one the logic acts on. */
+ if (thePendingReplayFile.isNotEmpty())
+ {
+ if (!TheRecorder->playbackFile( thePendingReplayFile ))
+ {
+ DEBUG_LOG(("-replay: '%s' could not be played back\n", thePendingReplayFile.str()));
+ TheWritableGlobalData->m_shellMapOn = TRUE;
+ }
+ thePendingReplayFile.clear();
+ }
} // end init
/** -----------------------------------------------------------------------------------------------