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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include <utlbuffer.h>
#include "tooldemofile.h"
#include "filesystem.h"
#include "demofile/demoformat.h"
extern IBaseFileSystem *g_pFileSystem;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CToolDemoFile::CToolDemoFile()
{
m_hDemoFile = FILESYSTEM_INVALID_HANDLE;
}
CToolDemoFile::~CToolDemoFile()
{
if ( IsOpen() )
{
Close();
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CToolDemoFile::ReadSequenceInfo(int &nSeqNrIn, int &nSeqNrOut)
{
Assert( m_hDemoFile != FILESYSTEM_INVALID_HANDLE );
g_pFileSystem->Read( &nSeqNrIn, sizeof(int), m_hDemoFile );
g_pFileSystem->Read( &nSeqNrOut, sizeof(int), m_hDemoFile );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CToolDemoFile::ReadCmdInfo( democmdinfo_t& info )
{
Assert( m_hDemoFile != FILESYSTEM_INVALID_HANDLE );
g_pFileSystem->Read( &info, sizeof( democmdinfo_t ), m_hDemoFile );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : cmd -
// dt -
// frame -
//-----------------------------------------------------------------------------
void CToolDemoFile::ReadCmdHeader( unsigned char& cmd, int& tick )
{
Assert( m_hDemoFile != FILESYSTEM_INVALID_HANDLE );
// Read the command
int r = g_pFileSystem->Read ( &cmd, sizeof(byte), m_hDemoFile );
if ( r <=0 )
{
Warning("Missing end tag in demo file.\n");
cmd = dem_stop;
return;
}
Assert( cmd >= 1 && cmd <= dem_lastcmd );
// Read the timestamp
g_pFileSystem->Read ( &tick, sizeof(int), m_hDemoFile );
tick = LittleDWord( tick );
}
const char *CToolDemoFile::ReadConsoleCommand()
{
static char cmdstring[1024];
ReadRawData( cmdstring, sizeof(cmdstring) );
return cmdstring;
}
unsigned int CToolDemoFile::GetCurPos()
{
if ( m_hDemoFile == FILESYSTEM_INVALID_HANDLE )
return 0;
return g_pFileSystem->Tell( m_hDemoFile );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : expected_length -
// &demofile -
//-----------------------------------------------------------------------------
int CToolDemoFile::ReadNetworkDataTables( CUtlBuffer *buf )
{
Assert( m_hDemoFile != FILESYSTEM_INVALID_HANDLE );
char data[ 1024 ];
int length;
g_pFileSystem->Read( &length, sizeof( int ), m_hDemoFile );
while( length > 0 )
{
int chunk = min( length, 1024 );
g_pFileSystem->Read( data, chunk, m_hDemoFile );
length -= chunk;
if ( buf )
{
buf->Put( data, chunk );
}
}
return length;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : discard -
//-----------------------------------------------------------------------------
int CToolDemoFile::ReadUserCmd( char *buffer, int &size )
{
Assert( m_hDemoFile != FILESYSTEM_INVALID_HANDLE );
int outgoing_sequence;
g_pFileSystem->Read( &outgoing_sequence, sizeof( int ), m_hDemoFile );
size = ReadRawData( buffer, size );
return outgoing_sequence;
}
//-----------------------------------------------------------------------------
// Purpose: Rewind from the current spot by the time stamp, byte code and frame counter offsets
//-----------------------------------------------------------------------------
void CToolDemoFile::SeekTo( int position )
{
Assert( m_hDemoFile != FILESYSTEM_INVALID_HANDLE );
g_pFileSystem->Seek( m_hDemoFile, position, FILESYSTEM_SEEK_HEAD );
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
int CToolDemoFile::ReadRawData( char *buffer, int length )
{
Assert( m_hDemoFile != FILESYSTEM_INVALID_HANDLE );
int size;
// read length of data block
g_pFileSystem->Read( &size, sizeof( int ), m_hDemoFile );
if ( buffer && (length < size) )
{
DevMsg("CToolDemoFile::ReadRawData: buffe overflow (%i).\n", size );
return -1;
}
if ( buffer )
{
if ( length < size )
{
// given buffer is too small
DevMsg("CToolDemoFile::ReadRawData: buffe overflow (%i).\n", size );
g_pFileSystem->Seek( m_hDemoFile, size, FILESYSTEM_SEEK_CURRENT );
size = -1;
}
else
{
// read data into buffer
int r = g_pFileSystem->Read( buffer, size, m_hDemoFile );
if ( r != size )
{
Warning( "Error reading demo message data.\n");
return -1;
}
}
}
else
{
// just skip it
g_pFileSystem->Seek( m_hDemoFile, size, FILESYSTEM_SEEK_CURRENT );
}
return size;
}
demoheader_t *CToolDemoFile::ReadDemoHeader()
{
if ( m_hDemoFile == FILESYSTEM_INVALID_HANDLE )
return NULL; // file not open
// goto file start
g_pFileSystem->Seek(m_hDemoFile, 0, FILESYSTEM_SEEK_HEAD);
Q_memset( &m_DemoHeader, 0, sizeof(m_DemoHeader) );
int r = g_pFileSystem->Read( &m_DemoHeader, sizeof(demoheader_t), m_hDemoFile );
if ( r != sizeof(demoheader_t) )
return NULL; // reading failed
if ( Q_strcmp ( m_DemoHeader.demofilestamp, DEMO_HEADER_ID ) )
{
Warning( "%s has invalid demo header ID.\n", m_szFileName );
return NULL;
}
/*
// The current network protocol version. Changing this makes clients and servers incompatible
#define PROTOCOL_VERSION 7
if ( m_DemoHeader.networkprotocol != PROTOCOL_VERSION )
{
Warning ("ERROR: demo network protocol %i outdated, engine version is %i \n",
m_DemoHeader.networkprotocol, PROTOCOL_VERSION );
return NULL;
}
*/
if ( m_DemoHeader.demoprotocol != DEMO_PROTOCOL )
{
Warning ("ERROR: demo file protocol %i outdated, engine version is %i \n",
m_DemoHeader.demoprotocol, DEMO_PROTOCOL );
return NULL;
}
return &m_DemoHeader;
}
bool CToolDemoFile::Open(const char *name, bool bReadOnly)
{
if ( m_hDemoFile != FILESYSTEM_INVALID_HANDLE )
{
Warning ("CToolDemoFile::Open: file already open.\n");
return false;
}
m_szFileName[0] = 0; // clear name
Q_memset( &m_DemoHeader, 0, sizeof(m_DemoHeader) ); // and demo header
if ( bReadOnly )
{
// open existing file for reading only
m_hDemoFile = g_pFileSystem->Open (name, "rb", "GAME" );
}
else
{
// create new file for writing only
m_hDemoFile = g_pFileSystem->Open (name, "wb", "GAME" );
}
if ( m_hDemoFile == FILESYSTEM_INVALID_HANDLE )
{
Warning ("CToolDemoFile::Open: couldn't open file %s for %s.\n",
name, bReadOnly?"reading":"writing" );
return false;
}
Q_strncpy( m_szFileName, name, sizeof(m_szFileName) );
return true;
}
bool CToolDemoFile::IsOpen()
{
return m_hDemoFile != FILESYSTEM_INVALID_HANDLE;
}
void CToolDemoFile::Close()
{
if ( m_hDemoFile != FILESYSTEM_INVALID_HANDLE )
{
g_pFileSystem->Close(m_hDemoFile);
m_hDemoFile = FILESYSTEM_INVALID_HANDLE;
}
}
int CToolDemoFile::GetSize()
{
return g_pFileSystem->Size( m_hDemoFile );
}
|