forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_recordingsession.cpp
451 lines (362 loc) · 15.2 KB
/
cl_recordingsession.cpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//=======================================================================================//
#include "cl_recordingsession.h"
#include "cl_sessioninfodownloader.h"
#include "cl_recordingsessionmanager.h"
#include "cl_replaymanager.h"
#include "cl_recordingsessionblock.h"
#include "cl_sessionblockdownloader.h"
#include "replay/ienginereplay.h"
#include "KeyValues.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//----------------------------------------------------------------------------------------
extern IEngineReplay *g_pEngine;
//----------------------------------------------------------------------------------------
#define MAX_SESSION_INFO_DOWNLOAD_ATTEMPTS 3
//----------------------------------------------------------------------------------------
CClientRecordingSession::CClientRecordingSession( IReplayContext *pContext )
: CBaseRecordingSession( pContext ),
m_iLastBlockToDownload( -1 ),
m_iGreatestConsecutiveBlockDownloaded( -1 ),
m_nSessionInfoDownloadAttempts( 0 ),
m_flLastUpdateTime( -1.0f ),
m_pSessionInfoDownloader( NULL ),
m_bTimedOut( false ),
m_bAllBlocksDownloaded( false )
{
}
CClientRecordingSession::~CClientRecordingSession()
{
delete m_pSessionInfoDownloader;
}
bool CClientRecordingSession::AllReplaysReconstructed() const
{
FOR_EACH_LL( m_lstReplays, it )
{
const CReplay *pCurReplay = m_lstReplays[ it ];
if ( !pCurReplay->HasReconstructedReplay() )
return false;
}
return true;
}
void CClientRecordingSession::DeleteBlocks()
{
// Only delete blocks if all replays have been reconstructed for this session
if ( !AllReplaysReconstructed() )
return;
// Delete each block
FOR_EACH_VEC( m_vecBlocks, i )
{
m_pContext->GetRecordingSessionBlockManager()->DeleteBlock( m_vecBlocks[ i ] );
}
// Clear out the list
m_vecBlocks.RemoveAll();
// Clear these out so we don't try to download the blocks again
m_iLastBlockToDownload = -1;
m_iGreatestConsecutiveBlockDownloaded = -1;
}
void CClientRecordingSession::SyncSessionBlocks()
{
// If the last update time hasn't been initialized yet, initialize it now since this will be the first time
// we are attempting to download the session info file.
if ( m_flLastUpdateTime < 0.0f )
{
m_flLastUpdateTime = g_pEngine->GetHostTime();
}
Assert( !m_pSessionInfoDownloader );
IF_REPLAY_DBG( Warning( "Downloading session info...\n" ) );
m_pSessionInfoDownloader = new CSessionInfoDownloader();
m_pSessionInfoDownloader->DownloadSessionInfoAndUpdateBlocks( this );
}
void CClientRecordingSession::OnReplayDeleted( CReplay *pReplay )
{
m_lstReplays.FindAndRemove( pReplay );
// This will load session blocks and delete them from disk if possible. In the case
// that all other replays for a session have already been reconstructed and pReplay
// was the last replay that was never reconstructed, we should delete session's blocks now.
// Note that these calls will only (a) load blocks if they aren't loaded already, and
// (b) Delete blocks if all associated replays have been reconstructed.
LoadBlocksForSession();
DeleteBlocks();
}
bool CClientRecordingSession::Read( KeyValues *pIn )
{
if ( !BaseClass::Read( pIn ) )
return false;
m_iLastBlockToDownload = pIn->GetInt( "last_block_to_download", -1 );
m_iGreatestConsecutiveBlockDownloaded = pIn->GetInt( "last_consec_block_downloaded", -1 );
// m_bTimedOut = pIn->GetBool( "timed_out" );
m_uServerSessionID = pIn->GetUint64( "server_session_id" );
m_bAllBlocksDownloaded = pIn->GetBool( "all_blocks_downloaded" );
return true;
}
void CClientRecordingSession::Write( KeyValues *pOut )
{
BaseClass::Write( pOut );
pOut->SetInt( "last_block_to_download", m_iLastBlockToDownload );
pOut->SetInt( "last_consec_block_downloaded", m_iGreatestConsecutiveBlockDownloaded );
// pOut->SetInt( "timed_out", (int)m_bTimedOut );
pOut->SetUint64( "server_session_id", m_uServerSessionID );
pOut->SetInt( "all_blocks_downloaded", (int)m_bAllBlocksDownloaded );
}
void CClientRecordingSession::AdjustLastBlockToDownload( int iNewLastBlockToDownload )
{
Assert( m_iLastBlockToDownload > iNewLastBlockToDownload );
m_iLastBlockToDownload = iNewLastBlockToDownload;
// Adjust any replays that refer to this session
FOR_EACH_LL( m_lstReplays, i )
{
CReplay *pCurReplay = m_lstReplays[ i ];
if ( pCurReplay->m_iMaxSessionBlockRequired > iNewLastBlockToDownload )
{
// Adjust replay
pCurReplay->m_iMaxSessionBlockRequired = iNewLastBlockToDownload;
}
}
}
int CClientRecordingSession::UpdateLastBlockToDownload()
{
// Here we calculate the block we'll need in order to reconstruct the replay at the post-death time,
// based on replay_postdeathrecordtime, NOT the current time. The index calculated here may be greater
// than the actual last block the server writes, since the round may end or the map may change. This
// is adjusted for when we actually download the blocks.
extern ConVar replay_postdeathrecordtime;
CClientRecordingSessionManager::ServerRecordingState_t *pServerState = &CL_GetRecordingSessionManager()->m_ServerRecordingState;
const int nCurBlock = pServerState->m_nCurrentBlock;
const int nDumpInterval = pServerState->m_nDumpInterval; Assert( nDumpInterval > 0 );
const int nAddedBlocks = (int)ceil( replay_postdeathrecordtime.GetFloat() / nDumpInterval ); // Round up
const int iPostDeathBlock = nCurBlock + nAddedBlocks;
IF_REPLAY_DBG( Warning( "nCurBlock: %i\n", nCurBlock ) );
IF_REPLAY_DBG( Warning( "nDumpInterval: %i\n", nDumpInterval ) );
IF_REPLAY_DBG( Warning( "nAddedBlocks: %i\n", nAddedBlocks ) );
IF_REPLAY_DBG( Warning( "iPostDeathBlock: %i\n", iPostDeathBlock ) );
// Never assign less blocks than we already need
m_iLastBlockToDownload = MAX( m_iLastBlockToDownload, iPostDeathBlock );
CL_GetRecordingSessionManager()->FlagForFlush( this, false );
IF_REPLAY_DBG( ConColorMsg( 0, Color(0,255,0), "Max block currently needed: %i\n", m_iLastBlockToDownload ) );
return m_iLastBlockToDownload;
}
void CClientRecordingSession::Think()
{
CBaseThinker::Think();
// If the session info downloader's done and can be deleted, free it.
if ( m_pSessionInfoDownloader &&
m_pSessionInfoDownloader->IsDone() &&
m_pSessionInfoDownloader->CanDelete() )
{
// Failure?
if ( m_pSessionInfoDownloader->m_nError != CSessionInfoDownloader::ERROR_NONE )
{
// If there was an error, increment the error count and update the appropriate replays if
// we've tried a sufficient number of times.
++m_nSessionInfoDownloadAttempts;
if ( m_nSessionInfoDownloadAttempts >= MAX_SESSION_INFO_DOWNLOAD_ATTEMPTS )
{
FOR_EACH_LL( m_lstReplays, i )
{
CReplay *pCurReplay = m_lstReplays[ i ];
// If this replay has already been set to "ready to convert" state (or beyond), skip.
if ( pCurReplay->m_nStatus >= CReplay::REPLAYSTATUS_READYTOCONVERT )
continue;
// Update status
pCurReplay->m_nStatus = CReplay::REPLAYSTATUS_ERROR;
// Display an error message
ShowDownloadFailedMessage( pCurReplay );
// Save now
CL_GetReplayManager()->FlagReplayForFlush( pCurReplay, true );
}
}
}
IF_REPLAY_DBG( Warning( "...session info download complete. Freeing.\n" ) );
delete m_pSessionInfoDownloader;
m_pSessionInfoDownloader = NULL;
}
}
float CClientRecordingSession::GetNextThinkTime() const
{
return g_pEngine->GetHostTime() + 0.5f;
}
void CClientRecordingSession::UpdateAllBlocksDownloaded()
{
// We're only "done" if this session is no longer recording and all blocks are downloaded.
const bool bOld = m_bAllBlocksDownloaded;
m_bAllBlocksDownloaded = !m_bRecording && ( m_iGreatestConsecutiveBlockDownloaded >= m_iLastBlockToDownload );
// Flag as modified if changed
if ( bOld != m_bAllBlocksDownloaded )
{
CL_GetRecordingSessionManager()->FlagForFlush( this, false );
}
}
void CClientRecordingSession::EnsureDownloadingEnabled()
{
m_bAllBlocksDownloaded = false;
}
void CClientRecordingSession::UpdateGreatestConsecutiveBlockDownloaded()
{
// Assumes m_vecBlocks is sorted in ascending order (for both reconstruction indices and handle, which should be parallel)
int j = 0;
int iGreatestConsecutiveBlockDownloaded = 0;
FOR_EACH_VEC( m_vecBlocks, i )
{
CClientRecordingSessionBlock *pCurBlock = CL_CastBlock( m_vecBlocks[ i ] );
AssertMsg( pCurBlock->m_iReconstruction == j, "Session blocks must be sorted!" );
// If the block hasn't been downloaded, stop here
if ( pCurBlock->m_nDownloadStatus != CClientRecordingSessionBlock::DOWNLOADSTATUS_DOWNLOADED )
break;
// Block has been downloaded - update the counter
iGreatestConsecutiveBlockDownloaded = MAX( iGreatestConsecutiveBlockDownloaded, pCurBlock->m_iReconstruction );
++j;
}
Assert( iGreatestConsecutiveBlockDownloaded >= 0 );
Assert( iGreatestConsecutiveBlockDownloaded < m_vecBlocks.Count() );
// Cache
m_iGreatestConsecutiveBlockDownloaded = iGreatestConsecutiveBlockDownloaded;
// Mark session as dirty
CL_GetRecordingSessionManager()->FlagForFlush( this, false );
}
void CClientRecordingSession::UpdateReplayStatuses( CClientRecordingSessionBlock *pBlock )
{
AssertMsg( m_vecBlocks.Find( pBlock ) != m_vecBlocks.InvalidIndex(), "Block doesn't belong to session or was not added" );
// If the download was successful, update the greatest consecutive block downloaded index
if ( pBlock->m_nDownloadStatus == CClientRecordingSessionBlock::DOWNLOADSTATUS_DOWNLOADED )
{
UpdateGreatestConsecutiveBlockDownloaded();
UpdateAllBlocksDownloaded();
}
// Block in error state?
const bool bFailed = pBlock->m_nDownloadStatus == CClientRecordingSessionBlock::DOWNLOADSTATUS_ERROR;
// Go through all replays that refer to this session and update their status if necessary
FOR_EACH_LL( m_lstReplays, i )
{
CReplay *pCurReplay = m_lstReplays[ i ];
// If this replay has already been set to "ready to convert" state (or beyond), skip.
if ( pCurReplay->m_nStatus >= CReplay::REPLAYSTATUS_READYTOCONVERT )
continue;
bool bFlush = false;
// If the download failed and the block is required for this replay, mark as such
if ( bFailed && pCurReplay->m_iMaxSessionBlockRequired >= pBlock->m_iReconstruction )
{
pCurReplay->m_nStatus = CReplay::REPLAYSTATUS_ERROR;
bFlush = true;
// Display an error message
ShowDownloadFailedMessage( pCurReplay );
}
// Have we downloaded all blocks required for the given replay?
else if ( !bFailed && pCurReplay->m_iMaxSessionBlockRequired <= m_iGreatestConsecutiveBlockDownloaded )
{
// Update replay's status and mark as dirty
pCurReplay->m_nStatus = CReplay::REPLAYSTATUS_READYTOCONVERT;
// Display a message on the client
g_pClient->DisplayReplayMessage( "#Replay_DownloadComplete", false, false, "replay\\downloadcomplete.wav" );
bFlush = true;
}
// Mark replay as dirty?
if ( bFlush )
{
CL_GetReplayManager()->FlagForFlush( pCurReplay, false );
}
}
}
void CClientRecordingSession::OnDownloadTimeout()
{
m_bTimedOut = true;
// Go through all replays that refer to this session and update their status if necessary
FOR_EACH_LL( m_lstReplays, i )
{
CReplay *pCurReplay = m_lstReplays[ i ];
// If this replay has already been set to "ready to convert" state (or beyond), skip.
if ( pCurReplay->m_nStatus >= CReplay::REPLAYSTATUS_READYTOCONVERT )
continue;
// Check to see if we have enough block info for the current replay
if ( m_iGreatestConsecutiveBlockDownloaded >= pCurReplay->m_iMaxSessionBlockRequired )
continue;
// Update replay status
pCurReplay->m_nStatus = CReplay::REPLAYSTATUS_ERROR;
// Display an error message
ShowDownloadFailedMessage( pCurReplay );
// Save the replay
CL_GetReplayManager()->FlagForFlush( pCurReplay, false );
}
}
void CClientRecordingSession::RefreshLastUpdateTime()
{
m_flLastUpdateTime = g_pEngine->GetHostTime();
}
void CClientRecordingSession::ShowDownloadFailedMessage( const CReplay *pReplay )
{
// Don't show the download failed message for replays that were saved during this run of the game.
if ( !pReplay || !pReplay->m_bSavedDuringThisSession )
return;
// Display an error message
g_pClient->DisplayReplayMessage( "#Replay_DownloadFailed", true, false, "replay\\downloadfailed.wav" );
}
void CClientRecordingSession::CacheReplay( CReplay *pReplay )
{
Assert( m_lstReplays.Find( pReplay ) == m_lstReplays.InvalidIndex() );
m_lstReplays.AddToTail( pReplay );
// We should no longer auto-delete this session if CacheReplay() is being called. This
// can happen if the user connects to a server, saves a replay, deletes the replay (at
// which point auto-delete is flagged for the recording session), and then saves another
// replay. In this situation, we obviously don't want to delete the session anymore.
if ( m_bAutoDelete )
{
m_bAutoDelete = false;
}
}
bool CClientRecordingSession::ShouldSyncBlocksWithServer() const
{
// Already downloaded all blocks?
if ( m_bAllBlocksDownloaded )
return false;
// If block count is out of sync with the m_iLastBlockDownloaded we need to sync up
const bool bReachedMaxDownloadAttempts = m_nSessionInfoDownloadAttempts >= MAX_SESSION_INFO_DOWNLOAD_ATTEMPTS;
const bool bNeedToDownloadBlocks = m_iLastBlockToDownload >= 0;
// const bool bAlreadyDownloadedAllNeededBlocks = m_iLastBlockToDownload <= m_iGreatestConsecutiveBlockDownloaded;
const bool bAlreadyDownloadedAllNeededBlocks = m_iLastBlockToDownload < m_vecBlocks.Count(); // NOTE/TODO: Shouldn't this look at m_iGreatestConsecutiveBlockDownloaded? Tried for a week, but it caused bugs. Reverting for now. TODO
const bool bTimedOut = false;//TimedOut();
const bool bResult = !bReachedMaxDownloadAttempts &&
bNeedToDownloadBlocks &&
!bAlreadyDownloadedAllNeededBlocks &&
!bTimedOut;
if ( bResult )
{
IF_REPLAY_DBG( Warning( "Blocks out of sync for session %i - downloading session info now.\n", GetHandle() ) );
}
else
{
DBG3( "NOT syncing because:\n" );
if ( bReachedMaxDownloadAttempts ) DBG3( " - Reached maximum download attempts\n" );
if ( !bNeedToDownloadBlocks ) DBG3( " - No replay saved yet\n" );
if ( bAlreadyDownloadedAllNeededBlocks ) DBG3( " - Already downloaded all needed blocks\n" );
if ( bTimedOut ) DBG3( " - Download timed out (session info file didn't change after 90 seconds)\n" );
}
return bResult;
}
void CClientRecordingSession::PopulateWithRecordingData( int nCurrentRecordingStartTick )
{
BaseClass::PopulateWithRecordingData( nCurrentRecordingStartTick );
CClientRecordingSessionManager::ServerRecordingState_t *pServerState = &CL_GetRecordingSessionManager()->m_ServerRecordingState;
m_strName = pServerState->m_strSessionName;
// Get download URL from replicated cvars
m_strBaseDownloadURL = Replay_GetDownloadURL();
// Get server session ID
m_uServerSessionID = g_pClient->GetServerSessionId();
}
bool CClientRecordingSession::ShouldDitchSession() const
{
return BaseClass::ShouldDitchSession() || m_lstReplays.Count() == 0;
}
void CClientRecordingSession::OnDelete()
{
// Abort any session block downloads now
CL_GetSessionBlockDownloader()->AbortDownloadsAndCleanup( this );
if ( m_pSessionInfoDownloader )
{
m_pSessionInfoDownloader->CleanupDownloader();
}
// Delete blocks
BaseClass::OnDelete();
}
//----------------------------------------------------------------------------------------