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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//===========================================================================//
#include <tier0/dbg.h>
#include <tier1/strtools.h>
#include <utlbuffer.h>
#include "demofile.h"
#include "filesystem_engine.h"
#include "demo.h"
#include "proto_version.h"
#include "convar.h" // For dbg_demofile
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
void Host_EndGame (bool bShowMainMenu, const char *message, ...);
// Debug helpers - this class prints in a nested format
ConVar dbg_demofile( "dbg_demofile", "0", FCVAR_DEVELOPMENTONLY | FCVAR_HIDDEN );
//#define DEMOFILE_DBG_PRINT
#if defined( DEMOFILE_DBG_PRINT )
class CDbgPrint
{
public:
static int s_nIndent;
CDbgPrint( const char *pMsg )
{
++s_nIndent;
if ( dbg_demofile.GetInt() )
{
for (int i = 0; i < 3*s_nIndent; ++i)
DevMsg(" ");
DevMsg( pMsg );
}
}
~CDbgPrint() { --s_nIndent; }
};
int CDbgPrint::s_nIndent = 0;
#define DemoFileDbg(_txt) CDbgPrint printer( _txt )
#else
#define DemoFileDbg(_txt) (void)0
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDemoFile::CDemoFile() :
m_pBuffer( NULL ),
m_bAllowHeaderWrite( true ),
m_bIsStreamBuffer( false )
{
}
CDemoFile::~CDemoFile()
{
if ( IsOpen() )
{
Close();
}
}
void CDemoFile::WriteSequenceInfo(int nSeqNrIn, int nSeqNrOut)
{
DemoFileDbg( "WriteSequenceInfo()\n" );
Assert( m_pBuffer && m_pBuffer->IsValid() );
m_pBuffer->PutInt( nSeqNrIn );
m_pBuffer->PutInt( nSeqNrOut );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDemoFile::ReadSequenceInfo(int &nSeqNrIn, int &nSeqNrOut)
{
Assert( m_pBuffer && m_pBuffer->IsValid() );
nSeqNrIn = m_pBuffer->GetInt( );
nSeqNrOut = m_pBuffer->GetInt( );
}
inline void ByteSwap_democmdinfo_t( democmdinfo_t &swap )
{
swap.flags = LittleDWord( swap.flags );
LittleFloat( &swap.viewOrigin.x, &swap.viewOrigin.x );
LittleFloat( &swap.viewOrigin.y, &swap.viewOrigin.y );
LittleFloat( &swap.viewOrigin.z, &swap.viewOrigin.z );
LittleFloat( &swap.viewAngles.x, &swap.viewAngles.x );
LittleFloat( &swap.viewAngles.y, &swap.viewAngles.y );
LittleFloat( &swap.viewAngles.z, &swap.viewAngles.z );
LittleFloat( &swap.localViewAngles.x, &swap.localViewAngles.x );
LittleFloat( &swap.localViewAngles.y, &swap.localViewAngles.y );
LittleFloat( &swap.localViewAngles.z, &swap.localViewAngles.z );
LittleFloat( &swap.viewOrigin2.x, &swap.viewOrigin2.x );
LittleFloat( &swap.viewOrigin2.y, &swap.viewOrigin2.y );
LittleFloat( &swap.viewOrigin2.z, &swap.viewOrigin2.z );
LittleFloat( &swap.viewAngles2.x, &swap.viewAngles2.x );
LittleFloat( &swap.viewAngles2.y, &swap.viewAngles2.y );
LittleFloat( &swap.viewAngles2.z, &swap.viewAngles2.z );
LittleFloat( &swap.localViewAngles2.x, &swap.localViewAngles2.x );
LittleFloat( &swap.localViewAngles2.y, &swap.localViewAngles2.y );
LittleFloat( &swap.localViewAngles2.z, &swap.localViewAngles2.z );
}
void CDemoFile::WriteCmdInfo( democmdinfo_t& info )
{
DemoFileDbg( "WriteCmdInfo()\n" );
democmdinfo_t littleEndianInfo = info;
ByteSwap_democmdinfo_t( littleEndianInfo );
Assert( m_pBuffer && m_pBuffer->IsValid() );
m_pBuffer->Put( &littleEndianInfo, sizeof(democmdinfo_t) );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDemoFile::ReadCmdInfo( democmdinfo_t& info )
{
Assert( m_pBuffer && m_pBuffer->IsValid() );
m_pBuffer->Get( &info, sizeof(democmdinfo_t) );
ByteSwap_democmdinfo_t( info );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : cmd -
// *fp -
//-----------------------------------------------------------------------------
void CDemoFile::WriteCmdHeader( unsigned char cmd, int tick )
{
if ( dbg_demofile.GetInt() ) DevMsg( "----------------------------------------\n" );
Assert( cmd >= dem_signon && cmd <= dem_lastcmd );
Assert( m_pBuffer && m_pBuffer->IsValid() );
m_pBuffer->PutUnsignedChar( cmd );
m_pBuffer->PutInt( tick );
static const char *cmdname[] =
{
"dem_unknown",
"dem_signon",
"dem_packet",
"dem_synctick",
"dem_consolecmd",
"dem_usercmd",
"dem_datatables",
"dem_stop",
"dem_stringtables"
};
DemoFileDbg( "WriteCmdHeader()..." );
if ( dbg_demofile.GetInt() ) DevMsg( "tick %i, cmd %s \n", tick, cmdname[cmd] );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : cmd -
// dt -
// frame -
//-----------------------------------------------------------------------------
void CDemoFile::ReadCmdHeader( unsigned char& cmd, int& tick )
{
Assert( m_pBuffer && m_pBuffer->IsValid() );
cmd = m_pBuffer->GetUnsignedChar( );
if ( !m_pBuffer || !m_pBuffer->IsValid() )
{
ConDMsg("Missing end tag in demo file.\n");
cmd = dem_stop;
return;
}
if ( cmd <= 0 || cmd > dem_lastcmd )
{
ConDMsg("Unexepcted command token [%d] in .demo file\n", cmd );
cmd = dem_stop;
return;
}
tick = m_pBuffer->GetInt( );
}
void CDemoFile::WriteConsoleCommand( const char *cmdstring, int tick )
{
DemoFileDbg( "WriteConsoleCommand()\n" );
if ( !cmdstring || !cmdstring[0] )
return;
if ( !m_pBuffer || !m_pBuffer->IsValid() )
return;
int len = Q_strlen( cmdstring ) + 1;
if ( len >= 1024 )
{
DevMsg("CDemoFile::WriteConsoleCommand: command too long (>1024).\n");
return;
}
WriteCmdHeader( dem_consolecmd, tick );
WriteRawData( cmdstring, len );
}
const char *CDemoFile::ReadConsoleCommand()
{
static char cmdstring[1024];
ReadRawData( cmdstring, sizeof(cmdstring) );
return cmdstring;
}
unsigned int CDemoFile::GetCurPos( bool bRead )
{
if ( !m_pBuffer || !m_pBuffer->IsValid() )
return 0;
if ( bRead )
return m_pBuffer->TellGet();
return m_pBuffer->TellPut();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : buf -
//-----------------------------------------------------------------------------
void CDemoFile::WriteNetworkDataTables( bf_write *buf, int tick )
{
DemoFileDbg( "WriteNetworkDataTables()\n" );
MEM_ALLOC_CREDIT();
if ( !m_pBuffer || !m_pBuffer->IsValid() )
{
DevMsg("CDemoFile::WriteNetworkDataTables: Haven't opened file yet!\n" );
return;
}
WriteCmdHeader( dem_datatables, tick );
WriteRawData( (char*)buf->GetBasePointer(), buf->GetNumBytesWritten() );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : expected_length -
// &demofile -
//-----------------------------------------------------------------------------
int CDemoFile::ReadNetworkDataTables( bf_read *buf )
{
if ( buf )
return ReadRawData( (char*)buf->GetBasePointer(), buf->GetNumBytesLeft() );
return ReadRawData( NULL, 0 ); // skip data
}
void CDemoFile::WriteStringTables( bf_write *buf, int tick )
{
DemoFileDbg( "WriteStringTables()\n" );
MEM_ALLOC_CREDIT();
if ( !m_pBuffer || !m_pBuffer->IsValid() )
{
DevMsg("CDemoFile::WriteStringTables: Haven't opened file yet!\n" );
return;
}
WriteCmdHeader( dem_stringtables, tick );
WriteRawData( (char*)buf->GetBasePointer(), buf->GetNumBytesWritten() );
}
int CDemoFile::ReadStringTables( bf_read *buf )
{
if ( buf )
return ReadRawData( (char*)buf->GetBasePointer(), buf->GetNumBytesLeft() );
return ReadRawData( NULL, 0 ); // skip data
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : cmdnumber -
//-----------------------------------------------------------------------------
void CDemoFile::WriteUserCmd( int cmdnumber, const char *buffer, unsigned char bytes, int tick )
{
DemoFileDbg( "WriteUserCmd()\n" );
if ( !m_pBuffer || !m_pBuffer->IsValid() )
return;
WriteCmdHeader( dem_usercmd, tick );
m_pBuffer->PutInt( cmdnumber );
WriteRawData( buffer, bytes );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : discard -
//-----------------------------------------------------------------------------
int CDemoFile::ReadUserCmd( char *buffer, int &size )
{
int outgoing_sequence;
Assert( m_pBuffer && m_pBuffer->IsValid() );
outgoing_sequence = m_pBuffer->GetInt();
size = ReadRawData( buffer, size );
return outgoing_sequence;
}
//
// Purpose: Rewind from the current spot by the time stamp, byte code and frame counter offsets
//-----------------------------------------------------------------------------
void CDemoFile::SeekTo( int position, bool bRead )
{
Assert( m_pBuffer && m_pBuffer->IsValid() );
if ( bRead )
{
m_pBuffer->SeekGet( CUtlBuffer::SEEK_HEAD, position );
}
else
{
m_pBuffer->SeekPut( CUtlBuffer::SEEK_HEAD, position );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
int CDemoFile::ReadRawData( char *buffer, int length )
{
int size;
Assert( m_pBuffer && m_pBuffer->IsValid() );
if ( !m_pBuffer || !m_pBuffer->IsValid() )
{
Host_EndGame(true, "Error reading demo message data.\n");
return -1;
}
size = m_pBuffer->GetInt();
if ( !buffer )
{
// just skip it
m_pBuffer->SeekGet( CUtlBuffer::SEEK_CURRENT, size );
return size;
}
if ( length < size )
{
// given buffer is too small
DevMsg("CDemoFile::ReadRawData: buffer overflow (%i).\n", size );
m_pBuffer->SeekGet( CUtlBuffer::SEEK_CURRENT, -(int)sizeof( int ) ); // rewind our get pointer
return -1;
}
// read data into buffer
m_pBuffer->Get( buffer, size );
return size;
}
void CDemoFile::WriteRawData( const char *buffer, int length )
{
DemoFileDbg( "WriteRawData()\n" );
MEM_ALLOC_CREDIT();
Assert( m_pBuffer && m_pBuffer->IsValid() );
m_pBuffer->PutInt( length );
m_pBuffer->Put( buffer, length );
}
void CDemoFile::WriteDemoHeader()
{
if ( !m_bAllowHeaderWrite )
return;
DemoFileDbg( "WriteDemoHeader()\n" );
Assert( m_DemoHeader.networkprotocol == PROTOCOL_VERSION );
if ( dbg_demofile.GetInt() )
{
DevMsg( "\n" );
DevMsg( " demofilestamp: %s\n", m_DemoHeader.demofilestamp );
DevMsg( " demoprotocol (should be %i): %i\n", DEMO_PROTOCOL, m_DemoHeader.demoprotocol );
DevMsg( " networkprotocol (should be %i): %i\n", PROTOCOL_VERSION, m_DemoHeader.networkprotocol );
DevMsg( " servername: %s\n", m_DemoHeader.servername );
DevMsg( " clientname: %s\n", m_DemoHeader.clientname );
DevMsg( " mapname: %s\n", m_DemoHeader.mapname );
DevMsg( " gamedirectory: %s\n", m_DemoHeader.gamedirectory );
DevMsg( " playback_time: %f\n", m_DemoHeader.playback_time );
DevMsg( " playback_ticks: %i\n", m_DemoHeader.playback_ticks );
DevMsg( " playback_frames: %i\n", m_DemoHeader.playback_frames );
DevMsg( " signonlength: %i\n", m_DemoHeader.signonlength );
DevMsg( "\n" );
}
// Swaps endianness, goes to file start and writes header
demoheader_t littleEndianHeader = *((demoheader_t*)&m_DemoHeader);
ByteSwap_demoheader_t( littleEndianHeader );
// Goto file start
m_pBuffer->SeekPut( CUtlBuffer::SEEK_HEAD, 0 );
// Write
m_pBuffer->Put( &m_DemoHeader, sizeof( m_DemoHeader ) );
}
demoheader_t *CDemoFile::ReadDemoHeader()
{
bool bOk;
Q_memset( &m_DemoHeader, 0, sizeof(m_DemoHeader) );
if ( !m_pBuffer || !m_pBuffer->IsValid() )
return NULL;
m_pBuffer->SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
m_pBuffer->Get( &m_DemoHeader, sizeof(demoheader_t) );
bOk = m_pBuffer->IsValid();
ByteSwap_demoheader_t( m_DemoHeader );
if ( !bOk )
return NULL; // reading failed
if ( Q_strcmp( m_DemoHeader.demofilestamp, DEMO_HEADER_ID ) )
{
ConMsg( "%s has invalid demo header ID.\n", m_szFileName );
return NULL;
}
if ( m_DemoHeader.networkprotocol != PROTOCOL_VERSION
#if defined( DEMO_BACKWARDCOMPATABILITY )
&& m_DemoHeader.networkprotocol < PROTOCOL_VERSION_12
#endif
)
{
ConMsg ("ERROR: demo network protocol %i outdated, engine version is %i \n",
m_DemoHeader.networkprotocol, PROTOCOL_VERSION );
return NULL;
}
if ( ( m_DemoHeader.demoprotocol > DEMO_PROTOCOL) ||
( m_DemoHeader.demoprotocol < 2 ) )
{
ConMsg ("ERROR: demo file protocol %i outdated, engine vnoteersion is %i \n",
m_DemoHeader.demoprotocol, DEMO_PROTOCOL );
return NULL;
}
return &m_DemoHeader;
}
void CDemoFile::WriteFileBytes( FileHandle_t fh, int length )
{
DemoFileDbg( "WriteFileBytes()\n" );
int copysize = length;
char copybuf[COM_COPY_CHUNK_SIZE];
while ( copysize > COM_COPY_CHUNK_SIZE )
{
g_pFileSystem->Read ( copybuf, COM_COPY_CHUNK_SIZE, fh );
m_pBuffer->Put( copybuf, COM_COPY_CHUNK_SIZE );
copysize -= COM_COPY_CHUNK_SIZE;
}
g_pFileSystem->Read ( copybuf, copysize, fh );
m_pBuffer->Put( copybuf, copysize );
g_pFileSystem->Flush ( fh );
}
bool CDemoFile::Open(const char *name, bool bReadOnly, bool bMemoryBuffer, int nBufferSize/*=0*/, bool bAllowHeaderWrite/*=true*/)
{
if ( m_pBuffer && m_pBuffer->IsValid() )
{
ConMsg ("CDemoFile::Open: file already open.\n");
return false;
}
m_szFileName[0] = 0; // clear name
Q_memset( &m_DemoHeader, 0, sizeof(m_DemoHeader) ); // and demo header
// This is used by replay, which manually writes a header.
m_bAllowHeaderWrite = bAllowHeaderWrite;
if ( bMemoryBuffer )
{
Assert( !bReadOnly ); // Only read from files
Assert( nBufferSize > 0 );
m_pBuffer = new CUtlBuffer( nBufferSize, nBufferSize, 0 );
m_bIsStreamBuffer = false;
}
else
{
m_pBuffer = new CUtlStreamBuffer( name, NULL, bReadOnly ? CUtlBuffer::READ_ONLY : 0, false );
m_bIsStreamBuffer = true;
}
// Demo files are always little endian
m_pBuffer->SetBigEndian( false );
if ( !m_pBuffer || !m_pBuffer->IsValid() )
{
ConMsg ("CDemoFile::Open: couldn't open file %s for %s.\n",
name, bReadOnly?"reading":"writing" );
Close();
return false;
}
if ( name )
{
Q_strncpy( m_szFileName, name, sizeof(m_szFileName) );
}
return true;
}
bool CDemoFile::IsOpen()
{
return m_pBuffer && m_pBuffer->IsValid();
}
void CDemoFile::Close()
{
// CUtlBuffer base class does NOT have a virtual destructor!
if ( m_bIsStreamBuffer )
{
// Destructor will call Close() as needed
delete static_cast<CUtlStreamBuffer*>(m_pBuffer);
}
else
{
delete m_pBuffer;
}
m_pBuffer = NULL;
}
int CDemoFile::GetSize()
{
return m_pBuffer->TellMaxPut();
}
// Returns the PROTOCOL_VERSION used when .dem was recorded
int CDemoFile::GetProtocolVersion()
{
return m_DemoHeader.networkprotocol;
}
|