-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDemoFile.cpp
317 lines (262 loc) · 6.73 KB
/
DemoFile.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
/*****************************************************************************
The Dark Mod GPL Source Code
This file is part of the The Dark Mod Source Code, originally based
on the Doom 3 GPL Source Code as published in 2011.
The Dark Mod Source Code is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version. For details, see LICENSE.TXT.
Project: The Dark Mod (http://www.thedarkmod.com/)
******************************************************************************/
#include "precompiled.h"
#pragma hdrstop
idCVar idDemoFile::com_logDemos( "com_logDemos", "0", CVAR_SYSTEM | CVAR_BOOL, "Write demo.log with debug information in it" );
idCVar idDemoFile::com_compressDemos( "com_compressDemos", "1", CVAR_SYSTEM | CVAR_INTEGER | CVAR_ARCHIVE, "Compression scheme for demo files\n0: None (Fast, large files)\n1: LZW (Fast to compress, Fast to decompress, medium/small files)\n2: LZSS (Slow to compress, Fast to decompress, small files)\n3: Huffman (Fast to compress, Slow to decompress, medium files)\nSee also: The 'CompressDemo' command" );
idCVar idDemoFile::com_preloadDemos( "com_preloadDemos", "0", CVAR_SYSTEM | CVAR_BOOL | CVAR_ARCHIVE, "Load the whole demo in to RAM before running it" );
#define DEMO_MAGIC GAME_NAME " RDEMO"
/*
================
idDemoFile::idDemoFile
================
*/
idDemoFile::idDemoFile() {
f = NULL;
fLog = NULL;
log = false;
fileImage = NULL;
compressor = NULL;
writing = false;
}
/*
================
idDemoFile::~idDemoFile
================
*/
idDemoFile::~idDemoFile() {
Close();
}
/*
================
idDemoFile::AllocCompressor
================
*/
idCompressor *idDemoFile::AllocCompressor( int type ) {
switch ( type ) {
case 0: return idCompressor::AllocNoCompression();
default:
case 1: return idCompressor::AllocLZW();
case 2: return idCompressor::AllocLZSS();
case 3: return idCompressor::AllocHuffman();
}
}
/*
================
idDemoFile::OpenForReading
================
*/
bool idDemoFile::OpenForReading( const char *fileName ) {
static const int magicLen = sizeof(DEMO_MAGIC) / sizeof(DEMO_MAGIC[0]);
char magicBuffer[magicLen];
int compression;
int fileLength;
Close();
f = fileSystem->OpenFileRead( fileName );
if ( !f ) {
return false;
}
fileLength = f->Length();
if ( com_preloadDemos.GetBool() ) {
fileImage = (byte *)Mem_Alloc( fileLength );
f->Read( fileImage, fileLength );
fileSystem->CloseFile( f );
f = new idFile_Memory( va( "preloaded(%s)", fileName ), (const char *)fileImage, fileLength );
}
if ( com_logDemos.GetBool() ) {
fLog = fileSystem->OpenFileWrite( "demoread.log" );
}
writing = false;
f->Read(magicBuffer, magicLen);
if ( memcmp(magicBuffer, DEMO_MAGIC, magicLen) == 0 ) {
f->ReadInt( compression );
} else {
// Ideally we would error out if the magic string isn't there,
// but for backwards compatibility we are going to assume it's just an uncompressed demo file
compression = 0;
f->Rewind();
}
compressor = AllocCompressor( compression );
compressor->Init( f, false, 8 );
return true;
}
/*
================
idDemoFile::SetLog
================
*/
void idDemoFile::SetLog(bool b, const char *p) {
log = b;
if (p) {
logStr = p;
}
}
/*
================
idDemoFile::Log
================
*/
void idDemoFile::Log(const char *p) {
if ( fLog && p && *p ) {
fLog->Write(p, static_cast<int>(strlen(p)));
}
}
/*
================
idDemoFile::OpenForWriting
================
*/
bool idDemoFile::OpenForWriting( const char *fileName ) {
Close();
f = fileSystem->OpenFileWrite( fileName );
if ( f == NULL ) {
return false;
}
if ( com_logDemos.GetBool() ) {
fLog = fileSystem->OpenFileWrite( "demowrite.log" );
}
writing = true;
f->Write(DEMO_MAGIC, sizeof(DEMO_MAGIC));
f->WriteInt( com_compressDemos.GetInteger() );
f->Flush();
compressor = AllocCompressor( com_compressDemos.GetInteger() );
compressor->Init( f, true, 8 );
return true;
}
/*
================
idDemoFile::Close
================
*/
void idDemoFile::Close() {
if ( writing && compressor ) {
compressor->FinishCompress();
}
if ( f ) {
fileSystem->CloseFile( f );
f = NULL;
}
if ( fLog ) {
fileSystem->CloseFile( fLog );
fLog = NULL;
}
if ( fileImage ) {
Mem_Free( fileImage );
fileImage = NULL;
}
if ( compressor ) {
delete compressor;
compressor = NULL;
}
demoStrings.DeleteContents( true );
}
/*
================
idDemoFile::ReadHashString
================
*/
const char *idDemoFile::ReadHashString() {
int index;
if ( log && fLog ) {
const char *text = va( "%s > Reading hash string\n", logStr.c_str() );
fLog->Write(text, static_cast<int>(strlen(text)));
}
ReadInt( index );
if ( index == -1 ) {
// read a new string for the table
idStr *str = new idStr;
idStr data;
ReadString( data );
*str = data;
demoStrings.Append( str );
return *str;
}
if ( index < -1 || index >= demoStrings.Num() ) {
Close();
common->Error( "demo hash index out of range" );
}
return demoStrings[index]->c_str();
}
/*
================
idDemoFile::WriteHashString
================
*/
void idDemoFile::WriteHashString( const char *str ) {
if ( log && fLog ) {
const char *text = va( "%s > Writing hash string\n", logStr.c_str() );
fLog->Write(text, static_cast<int>(strlen(text)));
}
// see if it is already in the has table
for ( int i = 0 ; i < demoStrings.Num() ; i++ ) {
if ( !strcmp( demoStrings[i]->c_str(), str ) ) {
WriteInt( i );
return;
}
}
// add it to our table and the demo table
idStr *copy = new idStr( str );
//common->Printf( "hash:%i = %s\n", demoStrings.Num(), str );
demoStrings.Append( copy );
int cmd = -1;
WriteInt( cmd );
WriteString( str );
}
/*
================
idDemoFile::ReadDict
================
*/
void idDemoFile::ReadDict( idDict &dict ) {
int i, c;
idStr key, val;
dict.Clear();
ReadInt( c );
for ( i = 0; i < c; i++ ) {
key = ReadHashString();
val = ReadHashString();
dict.Set( key, val );
}
}
/*
================
idDemoFile::WriteDict
================
*/
void idDemoFile::WriteDict( const idDict &dict ) {
int i, c;
c = dict.GetNumKeyVals();
WriteInt( c );
for ( i = 0; i < c; i++ ) {
WriteHashString( dict.GetKeyVal( i )->GetKey() );
WriteHashString( dict.GetKeyVal( i )->GetValue() );
}
}
/*
================
idDemoFile::Read
================
*/
int idDemoFile::Read( void *buffer, int len ) {
int read = compressor->Read( buffer, len );
if ( read == 0 && len >= 4 ) {
*(demoSystem_t *)buffer = DS_FINISHED;
}
return read;
}
/*
================
idDemoFile::Write
================
*/
int idDemoFile::Write( const void *buffer, int len ) {
return compressor->Write( buffer, len );
}