summaryrefslogtreecommitdiff
path: root/replay/baserecordingsession.cpp
blob: 62f302dae786a75066c58465430d322b54968894 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//=======================================================================================//

#include "baserecordingsession.h"
#include "baserecordingsessionblock.h"
#include "replay/irecordingsessionblockmanager.h"
#include "replay/replayutils.h"
#include "replay/iclientreplaycontext.h"
#include "replay/shared_defs.h"
#include "KeyValues.h"
#include "replay/replayutils.h"
#include "replay/ireplaycontext.h"
#include "filesystem.h"
#include "iserver.h"
#include "replaysystem.h"
#include "utlbuffer.h"

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

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

CBaseRecordingSession::CBaseRecordingSession( IReplayContext *pContext )
:	m_pContext( pContext ),
	m_bRecording( false ),
	m_bAutoDelete( false ),
	m_bBlocksLoaded( false ),
	m_flStartTime( 0.0f )
{
}

CBaseRecordingSession::~CBaseRecordingSession()
{
}

void CBaseRecordingSession::AddBlock( CBaseRecordingSessionBlock *pBlock )
{
	AddBlock( pBlock, false );
}

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

	m_strName = pIn->GetString( "name" );
	
	if ( m_strName.IsEmpty() )
	{
		CUtlBuffer buf;
		pIn->RecursiveSaveToFile( buf, 0 );
		IF_REPLAY_DBG( Warning( "Session with no session name found - aborting load for this session.  Data:\n---\n%s\n---\n", (const char *)buf.Base() ) );
		return false;
	}

	m_bRecording = pIn->GetBool( "recording" );
	m_strBaseDownloadURL = pIn->GetString( "base_download_url" );
	m_nServerStartRecordTick = pIn->GetInt( "server_start_record_tick", -1 );

	return true;
}

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

	pOut->SetString( "name", m_strName.Get() );
	pOut->SetInt( "recording", m_bRecording ? 1 : 0 );
	pOut->SetString( "base_download_url", m_strBaseDownloadURL.Get() );
	pOut->SetInt( "server_start_record_tick", m_nServerStartRecordTick );
}

const char *CBaseRecordingSession::GetSubKeyTitle() const
{
	return m_strName.Get();
}

const char *CBaseRecordingSession::GetPath() const
{
	return Replay_va( "%s%s%c", m_pContext->GetBaseDir(), SUBDIR_SESSIONS, CORRECT_PATH_SEPARATOR );
}

const char *CBaseRecordingSession::GetSessionInfoURL() const
{
	return Replay_va( "%s%s.%s", m_strBaseDownloadURL.Get(), m_strName.Get(), GENERIC_FILE_EXTENSION );
}

void CBaseRecordingSession::LoadBlocksForSession()
{
	if ( m_bBlocksLoaded )
		return;

	IRecordingSessionBlockManager *pBlockManager = m_pContext->GetRecordingSessionBlockManager();

	// Peek in directory and load files based on what's there
	FileFindHandle_t hFind;
	CFmtStr fmtPath( "%s%s*.%s", pBlockManager->GetBlockPath(), m_strName.Get(), GENERIC_FILE_EXTENSION );
	const char *pFilename = g_pFullFileSystem->FindFirst( fmtPath.Access(), &hFind );
	while ( pFilename )
	{
		// Load the block - this will add the block to this session
		pBlockManager->LoadBlockFromFileName( pFilename, this );	

		// Get next file
		pFilename = g_pFullFileSystem->FindNext( hFind );
	}

	// Blocks loaded
	m_bBlocksLoaded = true;
}

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

	// Dynamically load blocks if necessary, then delete from the block manager and from disk
	DeleteBlocks();
}

void CBaseRecordingSession::DeleteBlocks()
{
	if ( !m_bBlocksLoaded )
	{
		// Load blocks now based on the session name
		LoadBlocksForSession();
	}

	// Delete all blocks associated w/ the session
	FOR_EACH_VEC( m_vecBlocks, i )
	{
		CBaseRecordingSessionBlock *pCurBlock = m_vecBlocks[ i ];
		m_pContext->GetRecordingSessionBlockManager()->DeleteBlock( pCurBlock );
	}
}

void CBaseRecordingSession::OnUnload()
{
	BaseClass::OnUnload();

	FOR_EACH_VEC( m_vecBlocks, i )
	{
		CBaseRecordingSessionBlock *pCurBlock = m_vecBlocks[ i ];
		m_pContext->GetRecordingSessionBlockManager()->UnloadBlock( pCurBlock );
	}
}

void CBaseRecordingSession::PopulateWithRecordingData( int nCurrentRecordingStartTick )
{
	Assert( nCurrentRecordingStartTick >= 0 );

	m_strBaseDownloadURL = Replay_GetDownloadURL();
	m_bRecording = true;
	m_nServerStartRecordTick = nCurrentRecordingStartTick;
}

void CBaseRecordingSession::AddBlock( CBaseRecordingSessionBlock *pBlock, bool bFlagForFlush )
{
	Assert( pBlock->m_hSession == GetHandle() );

	Assert( m_vecBlocks.Find( pBlock ) == m_vecBlocks.InvalidIndex() );
	m_vecBlocks.Insert( pBlock );

	if ( bFlagForFlush )
	{
		// Mark as dirty
		m_pContext->GetRecordingSessionManager()->FlagSessionForFlush( this, false );
	}

	m_bBlocksLoaded = true;
}

int CBaseRecordingSession::FindBlock( CBaseRecordingSessionBlock *pBlock ) const
{
	int itResult = m_vecBlocks.Find( pBlock );
	if ( itResult == m_vecBlocks.InvalidIndex() )
		return -1;

	return itResult;
}

bool CBaseRecordingSession::ShouldDitchSession() const
{
	return m_bAutoDelete;
}

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

bool CBaseRecordingSession::CLessFunctor::Less( const CBaseRecordingSessionBlock *pSrc1, const CBaseRecordingSessionBlock *pSrc2, void *pContext )
{
	return pSrc1->m_iReconstruction < pSrc2->m_iReconstruction;
}

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