forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathRecorder.h
More file actions
235 lines (198 loc) · 8.91 KB
/
Copy pathRecorder.h
File metadata and controls
235 lines (198 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
** 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/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "Common/MessageStream.h"
#include "GameNetwork/GameInfo.h"
class File;
/**
* The ReplayGameInfo class holds information about the replay game and
* the contents of its slot list for reconstructing multiplayer games.
*/
class ReplayGameInfo : public GameInfo
{
private:
GameSlot m_ReplaySlot[MAX_SLOTS];
public:
ReplayGameInfo()
{
for (Int i = 0; i< MAX_SLOTS; ++i)
setSlotPointer(i, &m_ReplaySlot[i]);
}
};
enum RecorderModeType CPP_11(: Int) {
RECORDERMODETYPE_RECORD,
RECORDERMODETYPE_PLAYBACK,
RECORDERMODETYPE_SIMULATION_PLAYBACK, // Play back replay without any graphics
RECORDERMODETYPE_NONE // this is a valid state to be in on the shell map, or in saved games
};
class RecorderClass : public SubsystemInterface
{
protected:
// TheSuperHackers @info helmutbuhler 03/04/2025 CRC overview:
// Each peer periodically computes a CRC from its local game state and broadcasts it to all peers, including itself,
// to verify synchronization. CRC messages are received a few frames later in network games to avoid stalling every
// frame while waiting for all peers. This works because all peers compare the same received CRCs on the same frame.
//
// Replays are different: recorded CRC messages appear on the frame they were originally received, so directly
// comparing them against the current local state would mismatch. To handle this, local CRCs must be queued until the
// corresponding replay CRC messages arrive. This class implements that queue.
class CRCInfo
{
public:
struct MismatchData
{
MismatchData() :
mismatched(false),
playerIndex(0),
queueSize(0),
playbackCRC(0),
playerCRC(0)
{}
MismatchData(Byte playerIndex, UnsignedShort queueSize, UnsignedInt playbackCRC, UnsignedInt playerCRC) :
mismatched(true),
playerIndex(playerIndex),
queueSize(queueSize),
playbackCRC(playbackCRC),
playerCRC(playerCRC)
{}
Bool mismatched;
Byte playerIndex;
UnsignedShort queueSize;
UnsignedInt playbackCRC;
UnsignedInt playerCRC;
};
CRCInfo();
void init(Bool isMultiplayer, Int localPlayerIndex);
void addPlaybackCRC(UnsignedInt val);
void addPlayerCRC(Int playerIndex, UnsignedInt val);
void setSawCRCMismatch();
Bool sawCRCMismatch() const;
Byte getLocalPlayerIndex() const;
MismatchData getMismatchData();
protected:
UnsignedInt getLargestQueueSize() const;
UnsignedInt getPlaybackCRC();
Bool m_skippedOne;
Bool m_sawCRCMismatch;
Byte m_localPlayerIndex;
std::list<UnsignedInt> m_playbackData;
std::vector<UnsignedInt> m_playerData[MAX_PLAYER_COUNT];
Bool m_inactivePlayer[MAX_PLAYER_COUNT];
};
public:
struct ReplayHeader;
RecorderClass(); ///< Constructor.
virtual ~RecorderClass() override; ///< Destructor.
virtual void init() override; ///< Initialize TheRecorder.
virtual void reset() override; ///< Reset the state of TheRecorder.
virtual void update() override; ///< General purpose update function.
// Methods dealing with recording.
void updateRecord(); ///< The update function for recording.
// Methods dealing with playback.
void updatePlayback(); ///< The update function for playing back a file.
Bool playbackFile(AsciiString filename); ///< Starts playback of the specified file.
Bool replayMatchesGameVersion(AsciiString filename); ///< Returns true if the playback is a valid playback file for this version.
static Bool replayMatchesGameVersion(const ReplayHeader& header); ///< Returns true if the playback is a valid playback file for this version.
AsciiString getCurrentReplayFilename(); ///< valid during playback only
UnsignedInt getPlaybackFrameCount() const { return m_playbackFrameCount; } ///< valid during playback only
void stopPlayback(); ///< Stops playback. Its fine to call this even if not playing back a file.
Bool simulateReplay(AsciiString filename);
#if defined(RTS_DEBUG)
Bool analyzeReplay( AsciiString filename );
#endif
Bool isPlaybackInProgress() const;
void handlePlaybackCRCMessage(UnsignedInt newCRC);
void handlePlayerCRCMessage(Int playerIndex, UnsignedInt newCRC);
void checkForMismatch();
// read in info relating to a replay, conditionally setting up m_file for playback
struct ReplayHeader
{
AsciiString filename;
Bool forPlayback;
UnicodeString replayName;
SYSTEMTIME timeVal;
UnicodeString versionString;
UnicodeString versionTimeString;
UnsignedInt versionNumber;
UnsignedInt exeCRC;
UnsignedInt iniCRC;
time_t startTime;
time_t endTime;
UnsignedInt frameCount;
Bool quitEarly;
Bool desyncGame;
Bool playerDiscons[MAX_SLOTS];
AsciiString gameOptions;
Int localPlayerIndex;
};
Bool readReplayHeader( ReplayHeader& header );
RecorderModeType getMode(); ///< Returns the current operating mode.
Bool isPlaybackMode() const { return m_mode == RECORDERMODETYPE_PLAYBACK || m_mode == RECORDERMODETYPE_SIMULATION_PLAYBACK; }
void initControls(); ///< Show or Hide the Replay controls
static AsciiString getReplayDir(); ///< Returns the directory that holds the replay files.
static AsciiString getReplayArchiveDir(); ///< Returns the directory that holds the archived replay files.
static AsciiString getReplayExtention(); ///< Returns the file extention for replay files.
static AsciiString getLastReplayFileName(); ///< Returns the filename used for the default replay.
GameInfo *getGameInfo() { return &m_gameInfo; } ///< Returns the slot list for playback game start
Bool isMultiplayer(); ///< is this a multiplayer game (record OR playback)?
Int getGameMode() { return m_originalGameMode; }
void logPlayerDisconnect(UnicodeString player, Int slot);
void logCRCMismatch();
Bool sawCRCMismatch() const;
void cleanUpReplayFile(); ///< after a crash, send replay/debug info to a central repository
void setArchiveEnabled(Bool enable) { m_archiveReplays = enable; } ///< Enable or disable replay archiving.
void stopRecording(); ///< Stop recording and close m_file.
protected:
void startRecording(GameDifficulty diff, Int originalGameMode, Int rankPoints, Int maxFPS); ///< Start recording to m_file.
void writeToFile(GameMessage *msg); ///< Write this GameMessage to m_file.
void archiveReplay(AsciiString fileName); ///< Move the specified replay file to the archive directory.
void logGameStart(AsciiString options);
void logGameEnd();
AsciiString readAsciiString(); ///< Read the next string from m_file using ascii characters.
UnicodeString readUnicodeString(); ///< Read the next string from m_file using unicode characters.
void readNextFrame(); ///< Read the next frame number to execute a command on.
void appendNextCommand(); ///< Read the next GameMessage and append it to TheCommandList.
void writeArgument(GameMessageArgumentDataType type, const GameMessageArgumentType arg);
void readArgument(GameMessageArgumentDataType type, GameMessage *msg);
struct CullBadCommandsResult
{
CullBadCommandsResult() : hasClearGameDataMessage(false) {}
Bool hasClearGameDataMessage;
};
CullBadCommandsResult cullBadCommands(); ///< prevent the user from giving mouse commands that he shouldn't be able to do during playback.
CRCInfo m_crcInfo;
File* m_file;
AsciiString m_fileName;
Int m_currentFilePosition;
RecorderModeType m_mode;
AsciiString m_currentReplayFilename; ///< valid during playback only
UnsignedInt m_playbackFrameCount;
ReplayGameInfo m_gameInfo;
Bool m_wasDesync;
Bool m_doingAnalysis;
Bool m_archiveReplays; ///< if true, each replay is archived to the replay archive folder after recording
Int m_originalGameMode; // valid in replays
UnsignedInt m_nextFrame; ///< The Frame that the next message is to be executed on. This can be -1.
};
extern RecorderClass *TheRecorder;
RecorderClass *createRecorder();