summaryrefslogtreecommitdiff
path: root/replay/common/replay.cpp
blob: 1d56def43022eb66b43485410e7787142dbd3a84 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//=======================================================================================//

#include "replay/replay.h"
#include "replay/iclientreplaycontext.h"
#include "replay/ireplaymanager.h"
#include "replay/replayutils.h"
#include "replay/screenshot.h"
#include "replay/shared_defs.h"
#include "replay/ireplayscreenshotmanager.h"
#include "replay/ireplayperformancemanager.h"
#include "replay/performance.h"
#include "KeyValues.h"
#include "filesystem.h"
#include "vgui/ILocalize.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//----------------------------------------------------------------------------------------

extern IClientReplayContext *g_pClientReplayContext;
extern vgui::ILocalize *g_pVGuiLocalize;

//----------------------------------------------------------------------------------------

CReplay::CReplay()
:	m_pDownloadEventHandler( NULL ),
	m_pUserData( NULL ),
	m_bComplete( false ),
	m_bRequestedByUser( false ),
	m_bSaved( false ),
	m_bRendered( false ),
	m_bDirty( false ),
	m_bSavedDuringThisSession( true ),
	m_flLength( 0 ),
	m_nPlayerSlot( -1 ),
	m_nSpawnTick( -1 ),
	m_nDeathTick( -1 ),
	m_iMaxSessionBlockRequired( 0 ),
	m_nStatus( REPLAYSTATUS_INVALID ),
	m_hSession( REPLAY_HANDLE_INVALID ),
	m_pFileURL( NULL ),
	m_nPostDeathRecordTime( 0 ),
	m_flStartTime( 0.0f ),
	m_flNextUpdateTime( 0.0f )
{
	m_wszTitle[0] = L'\0';
	m_szMapName[0] = 0;
}

bool CReplay::IsDownloaded() const
{
	return m_nStatus == REPLAYSTATUS_READYTOCONVERT;
}

const CReplayPerformance *CReplay::GetPerformance( int i ) const
{
	return const_cast< CReplay * >( this )->GetPerformance( i );
}

CReplayPerformance *CReplay::GetPerformance( int i )
{
	if ( i < 0 || i >= m_vecPerformances.Count() )
		return NULL;

	return m_vecPerformances[ i ];
}

bool CReplay::FindPerformance( CReplayPerformance *pPerformance, int &iResult )
{
	const int it = m_vecPerformances.Find( pPerformance );
	if ( it == m_vecPerformances.InvalidIndex() )
	{
		iResult = -1;
		return false;
	}

	iResult = it;
	return true;
}

CReplayPerformance *CReplay::GetPerformanceWithTitle( const wchar_t *pTitle )
{
	FOR_EACH_VEC( m_vecPerformances, i )
	{
		CReplayPerformance *pCurPerformance = m_vecPerformances[ i ];
		if ( !V_wcscmp( pTitle, pCurPerformance->m_wszTitle ) )
		{
			return pCurPerformance;
		}
	}
	return NULL;
}

CReplayPerformance *CReplay::AddNewPerformance( bool bGenTitle/*=true*/, bool bGenFilename/*=true*/ )
{
	// Create a performance
	IReplayPerformanceManager *pPerformanceManager = g_pClientReplayContext->GetPerformanceManager();
	CReplayPerformance *pPerformance = pPerformanceManager->CreatePerformance( this );

	if ( bGenTitle )
	{
		// Give the performance a name
		pPerformance->AutoNameIfHasNoTitle( m_szMapName );
	}

	if ( bGenFilename )
	{
		// Generate a filename for the new performance
		pPerformance->SetFilename( pPerformanceManager->GeneratePerformanceFilename( this ) );
	}

	// Cache
	m_vecPerformances.AddToTail( pPerformance );
	
	return pPerformance;
}

void CReplay::AddPerformance( KeyValues *pIn )
{
	// Create a performance
	IReplayPerformanceManager *pPerformanceManager = g_pClientReplayContext->GetPerformanceManager();
	CReplayPerformance *pPerformance = pPerformanceManager->CreatePerformance( this );

	// Read
	pPerformance->Read( pIn );

	// Cache
	m_vecPerformances.AddToTail( pPerformance );
}

void CReplay::AddPerformance( CReplayPerformance *pPerformance )
{
	Assert( pPerformance );
	m_vecPerformances.AddToTail( pPerformance );
}

const CReplayTime &CReplay::GetItemDate() const
{
	return m_RecordTime;
}

bool CReplay::IsItemRendered() const
{
	return m_bRendered;
}

CReplay *CReplay::GetItemReplay()
{
	return this;
}

ReplayHandle_t	CReplay::GetItemReplayHandle() const
{
	return GetHandle();
}

QueryableReplayItemHandle_t	CReplay::GetItemHandle() const
{
	return GetHandle();
}

const wchar_t *CReplay::GetItemTitle() const
{
	return m_wszTitle;
}

void CReplay::SetItemTitle( const wchar_t *pTitle )
{
	V_wcsncpy( m_wszTitle, pTitle, sizeof( m_wszTitle ) );
}

float CReplay::GetItemLength() const
{
	return m_flLength;
}

void *CReplay::GetUserData()
{
	return m_pUserData;
}

void CReplay::SetUserData( void* pUserData )
{
	m_pUserData = pUserData;
}

bool CReplay::IsItemAMovie() const
{
	return false;
}

void CReplay::AddScreenshot( int nWidth, int nHeight, const char *pBaseFilename )
{
	m_vecScreenshots.AddToTail( new CReplayScreenshot( nWidth, nHeight, pBaseFilename ) );
}

void CReplay::AutoNameTitleIfEmpty()
{
	// Autoname it
	if ( !m_wszTitle[0] )
	{
		Replay_GetAutoName( m_wszTitle, sizeof( m_wszTitle ), m_szMapName );
	}
}

const char *CReplay::GetSubKeyTitle() const
{
	return Replay_va( "replay_%i", GetHandle() );
}

const char *CReplay::GetPath() const
{
	return Replay_va( "%s%s%c", g_pClientReplayContext->GetBaseDir(), SUBDIR_REPLAYS, CORRECT_PATH_SEPARATOR );
}

void CReplay::OnDelete()
{
	BaseClass::OnDelete();

	// Delete reconstructed replay if one exists
	if ( HasReconstructedReplay() )
	{
		g_pFullFileSystem->RemoveFile( m_strReconstructedFilename.Get() );
	}

	// Delete screenshots
	g_pClientReplayContext->GetScreenshotManager()->DeleteScreenshotsForReplay( this );

	// TODO: Delete performance(s)
}

bool CReplay::Read( KeyValues *pIn )
{
	if ( !BaseClass::Read( pIn ) )
		return false;

	m_hSession = (ReplayHandle_t)pIn->GetInt( "session", REPLAY_HANDLE_INVALID );
	V_strcpy_safe( m_szMapName, pIn->GetString( "map", "" ) );
	m_nSpawnTick = pIn->GetInt( "spawn_tick", -1 );
	m_nDeathTick = pIn->GetInt( "death_tick", -1 );
	m_nStatus = static_cast< CReplay::ReplayStatus_t >( pIn->GetInt( "status", (int)CReplay::REPLAYSTATUS_INVALID ) );
	m_bComplete = pIn->GetInt( "complete" ) != 0;
	m_flLength = pIn->GetFloat( "length" );
	m_nPostDeathRecordTime = pIn->GetInt( "postdeathrecordtime" );
	m_bRendered = pIn->GetInt( "rendered" ) != 0;
	m_nPlayerSlot = pIn->GetInt( "player_slot", -1 );
	m_iMaxSessionBlockRequired = pIn->GetInt( "max_block", 0 );	Assert( m_iMaxSessionBlockRequired >= 0 );
	m_flStartTime = pIn->GetFloat( "start_time", -1.0f );		Assert( m_flStartTime >= 0.0f );
	V_wcsncpy( m_wszTitle, pIn->GetWString( "title" ), sizeof( m_wszTitle ) );

	// Read reconstructed filename and infer path
	const char *pReplaysDir = g_pClientReplayContext->GetReplayManager()->GetReplaysDir();
	const char *pReconFilename = pIn->GetString( "recon_filename" );
	if ( pReconFilename[0] != 0 )
	{
		m_strReconstructedFilename = Replay_va( "%s%s", pReplaysDir, pReconFilename );
	}

	// Read screenshots
	KeyValues *pScreenshots = pIn->FindKey( "screenshots" );
	if ( pScreenshots )
	{
		FOR_EACH_TRUE_SUBKEY( pScreenshots, pScreenshot )
		{
			int nWidth = pScreenshot->GetInt( "width" );
			int nHeight = pScreenshot->GetInt( "height" );
			const char *pBaseFilename = pScreenshot->GetString( "base_filename" );
			AddScreenshot( nWidth, nHeight, pBaseFilename );
		}
	}

	// Read performances
	KeyValues *pPerformances = pIn->FindKey( "edits" );
	if ( pPerformances )
	{
		FOR_EACH_TRUE_SUBKEY( pPerformances, pPerformance )
		{
			AddPerformance( pPerformance );
		}
	}

	// Record time
	KeyValues *pRecordTimeSubKey = pIn->FindKey( "record_time" );
	if ( pRecordTimeSubKey )
	{
		m_RecordTime.Read( pRecordTimeSubKey );
	}

	// Mark replay as saved, since it was just loaded from disk
	m_bSaved = true;

	return true;
}

void CReplay::Write( KeyValues *pOut )
{
	BaseClass::Write( pOut );

	pOut->SetString( "map", m_szMapName );
	pOut->SetInt( "session", m_hSession );
	pOut->SetInt( "spawn_tick", m_nSpawnTick );
	pOut->SetInt( "death_tick", m_nDeathTick );
	pOut->SetInt( "status", static_cast< int >( m_nStatus ) );
	pOut->SetInt( "complete", static_cast< int >( m_bComplete ) );
	pOut->SetFloat( "length", m_flLength );
	pOut->SetInt( "postdeathrecordtime", m_nPostDeathRecordTime );
	pOut->SetInt( "rendered", m_bRendered );
	pOut->SetInt( "player_slot", m_nPlayerSlot );
	pOut->SetInt( "max_block", m_iMaxSessionBlockRequired );
	pOut->SetFloat( "start_time", m_flStartTime );
	pOut->SetWString( "title", m_wszTitle );

	// Store only filename for reconstructed .dem
	if ( !m_strReconstructedFilename.IsEmpty() )
	{
		const char *pReconFilename = V_UnqualifiedFileName( m_strReconstructedFilename.Get() );
		if ( pReconFilename[0] )
		{
			pOut->SetString( "recon_filename", pReconFilename );
		}
	}

	// Write screenshots
	KeyValues *pScreenshots = new KeyValues( "screenshots" );
	pOut->AddSubKey( pScreenshots );
	for ( int i = 0; i < m_vecScreenshots.Count(); ++i )
	{
		KeyValues *pScreenshotOut = new KeyValues( "screenshot" );
		CReplayScreenshot *pScreenshot = m_vecScreenshots[ i ];
		pScreenshotOut->SetInt( "width", pScreenshot->m_nWidth );
		pScreenshotOut->SetInt( "height", pScreenshot->m_nHeight );
		pScreenshotOut->SetString( "base_filename", pScreenshot->m_szBaseFilename );
		pScreenshots->AddSubKey( pScreenshotOut );
	}

	// Write performances
	KeyValues *pPerformances = new KeyValues( "edits" );
	pOut->AddSubKey( pPerformances );
	for ( int i = 0; i < m_vecPerformances.Count(); ++i )
	{
		KeyValues *pPerfOut = new KeyValues( "edit" );
		CReplayPerformance *pPerformance = m_vecPerformances[ i ];
		pPerformance->Write( pPerfOut );
		pPerformances->AddSubKey( pPerfOut );
	}

	KeyValues *pRecordTime = new KeyValues( "record_time" );
	pOut->AddSubKey( pRecordTime );
	m_RecordTime.Write( pRecordTime );

	// Mark as saved
	m_bSaved = true;
}

bool CReplay::HasReconstructedReplay() const
{
	return !m_strReconstructedFilename.IsEmpty() &&
		   g_pFullFileSystem->FileExists( m_strReconstructedFilename.Get() );
}

bool CReplay::IsSignificantBlock( int iBlockReconstruction ) const
{
	return iBlockReconstruction <= m_iMaxSessionBlockRequired;
}

void CReplay::OnComplete()
{
	AutoNameTitleIfEmpty();
}

//----------------------------------------------------------------------------------------