-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcl_download.cpp
More file actions
499 lines (400 loc) · 12.8 KB
/
cl_download.cpp
File metadata and controls
499 lines (400 loc) · 12.8 KB
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
/*
===========================================================================
Daemon GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Daemon GPL Source Code (Daemon Source Code).
Daemon 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.
Daemon Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Daemon Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Daemon Source Code is also subject to certain additional terms.
You should have received a copy of these additional terms immediately following the
terms and conditions of the GNU General Public License which accompanied the Daemon
Source Code. If not, please request a copy in writing from id Software at the address
below.
If you have questions concerning this license or the applicable additional terms, you
may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville,
Maryland 20850 USA.
===========================================================================
*/
#include "common/Common.h"
#include "client.h"
Log::Logger downloadLogger("client.pakDownload", "", Log::Level::NOTICE);
Cvar::Cvar<int> cl_downloadCount("cl_downloadCount", "bytes of a file downloaded", Cvar::NONE, 0);
/*
=====================
CL_ClearStaticDownload
Clear download information that we keep in cls (disconnected download support)
=====================
*/
void CL_ClearStaticDownload()
{
downloadLogger.Debug("Clearing the download info");
cls.downloadRestart = false;
cls.downloadTempName.clear();
cls.downloadName.clear();
cls.originalDownloadName.clear();
}
/*
=================
CL_DownloadsComplete
Called when all downloading has been completed
=================
*/
static void CL_DownloadsComplete()
{
// if we downloaded files we need to restart the file system
if ( cls.downloadRestart )
{
cls.downloadRestart = false;
downloadLogger.Debug("Downloaded something, reload the paks");
downloadLogger.Debug(" The paks to load are '%s'", Cvar_VariableString("sv_paks"));
FS::PakPath::ClearPaks();
FS_LoadServerPaks(Cvar_VariableString("sv_paks"), clc.demoplaying); // We possibly downloaded a pak, restart the file system to load it
// inform the server so we get new gamestate info
CL_AddReliableCommand( "donedl" );
// we can reset that now
CL_ClearStaticDownload();
// by sending the donedl command we request a new gamestate
// so we don't want to load stuff yet
return;
}
// let the client game init and load data
cls.state = connstate_t::CA_LOADING;
// Pump the loop, this may change gamestate!
Com_EventLoop();
// if the gamestate was changed by calling Com_EventLoop
// then we loaded everything already and we don't want to do it again.
if ( cls.state != connstate_t::CA_LOADING )
{
return;
}
// flush client memory and start loading stuff
// this will also (re)load the UI
CL_ShutdownAll();
CL_StartHunkUsers();
// initialize the CGame
cls.cgameStarted = true;
CL_InitCGame();
CL_WritePacket();
CL_WritePacket();
CL_WritePacket();
}
/*
=================
CL_BeginDownload
Requests a file to download from the server. Stores it in the current
game directory.
=================
*/
static void CL_BeginDownload( const std::string& localName, const std::string& remoteName )
{
downloadLogger.Debug("Requesting the download of '%s', with remote name '%s'", localName, remoteName);
cls.downloadName = localName;
cls.downloadTempName = localName;
cls.downloadTempName += ".tmp";
// Set so UI gets access to it
Cvar_Set( "cl_downloadName", remoteName.c_str() );
Cvar_Set( "cl_downloadSize", "0" );
cl_downloadCount.Set( 0 );
Cvar_SetValue( "cl_downloadTime", cls.realtime );
clc.downloadBlock = 0; // Starting new file
clc.downloadCount = 0;
CL_AddReliableCommand( Str::Format( "download %s", Cmd_QuoteString( remoteName.c_str() ) ).c_str() );
}
/*
=================
CL_NextDownload
A download completed or failed
=================
*/
static void CL_NextDownload()
{
// We are looking to start a download here
if ( clc.downloadList.size() )
{
downloadLogger.Debug( "CL_NextDownload downloadList is '%s'", clc.downloadList );
// format is:
// @remotename@localname@remotename@localname, etc.
size_t offset = clc.downloadList[0] == '@' ? 1 : 0;
size_t offset2 = clc.downloadList.find( "@", offset + 1 );
const std::string remoteName = clc.downloadList.substr( offset, offset2 - offset );
if ( offset2 == std::string::npos )
{
CL_DownloadsComplete();
return;
}
offset = offset2 + 1;
offset2 = clc.downloadList.find( "@", offset2 + 1 );
const std::string localName = clc.downloadList.substr( offset, offset2 - 1 );
CL_BeginDownload( localName, remoteName );
cls.downloadRestart = true;
// move over the rest
if ( offset2 == std::string::npos ) {
clc.downloadList.clear();
} else {
clc.downloadList = clc.downloadList.substr( offset2 );
}
return;
}
CL_DownloadsComplete();
}
/*
=================
CL_InitDownloads
After receiving a valid game state, we valid the cgame and local zip files here
and determine if we need to download them
=================
*/
void CL_InitDownloads()
{
// TTimo
// init some of the www dl data
clc.bWWWDl = false;
clc.bWWWDlAborting = false;
CL_ClearStaticDownload();
if ( cl_allowDownload->integer ) {
FS_DeletePaksWithBadChecksum();
}
// reset the redirect checksum tracking
clc.redirectedList.clear();
if ( cl_allowDownload->integer && FS_ComparePaks( clc.downloadList ) )
{
downloadLogger.Debug( "Need paks: '%s'", clc.downloadList );
if ( clc.downloadList.size() )
{
// if autodownloading is not enabled on the server
cls.state = connstate_t::CA_DOWNLOADING;
CL_NextDownload();
return;
}
}
CL_DownloadsComplete();
}
/*
==================
CL_WWWDownload
==================
*/
void CL_WWWDownload()
{
dlStatus_t ret;
static bool bAbort = false;
if ( clc.bWWWDlAborting )
{
if ( !bAbort )
{
Log::Debug( "CL_WWWDownload: WWWDlAborting" );
bAbort = true;
}
return;
}
if ( bAbort )
{
Log::Debug( "CL_WWWDownload: WWWDlAborting done" );
bAbort = false;
}
ret = DL_DownloadLoop();
if ( ret == dlStatus_t::DL_CONTINUE )
{
return;
}
if ( ret == dlStatus_t::DL_DONE )
{
downloadLogger.Debug("Finished WWW download of '%s', moving it to '%s'", cls.downloadTempName, cls.originalDownloadName);
// taken from CL_ParseDownload
clc.download = 0;
FS_SV_Rename( cls.downloadTempName.c_str(), cls.originalDownloadName.c_str() );
cls.downloadTempName.clear();
cls.downloadName.clear();
Cvar_Set( "cl_downloadName", "" );
CL_AddReliableCommand( "wwwdl done" );
// tracking potential web redirects leading us to wrong checksum - only works in connected mode
clc.redirectedList += "@";
clc.redirectedList += cls.originalDownloadName;
}
else
{
// see CL_ParseDownload, same abort strategy
Log::Notice( "Download failure while getting '%s'", cls.downloadName );
CL_AddReliableCommand( "wwwdl fail" );
clc.bWWWDlAborting = true;
return;
}
clc.bWWWDl = false;
CL_NextDownload();
}
/*
==================
CL_WWWBadChecksum
FS code calls this when doing FS_ComparePaks
we can detect files that we got from a www dl redirect with a wrong checksum
this indicates that the redirect setup is broken, and next dl attempt should NOT redirect
==================
*/
bool CL_WWWBadChecksum( const char *pakname )
{
if ( clc.redirectedList.find( Str::Format( "@%s@", pakname ) ) != std::string::npos )
{
Log::Warn("file %s obtained through download redirect has wrong checksum"
"\tthis likely means the server configuration is broken", pakname );
clc.badChecksumList += "@";
clc.badChecksumList += pakname;
Log::Debug( "bad checksums: %s", clc.badChecksumList );
return true;
}
return false;
}
/*
=====================
CL_ParseDownload
A download message has been received from the server
=====================
*/
void CL_ParseDownload( msg_t *msg )
{
int size;
unsigned char data[ MAX_MSGLEN ];
int block;
if ( cls.downloadTempName.empty() )
{
Log::Notice( "Server sending download, but no download was requested" );
// Eat the packet anyway
block = MSG_ReadShort( msg );
if (block == -1) {
MSG_ReadString( msg );
MSG_ReadLong( msg );
MSG_ReadLong( msg );
} else if (block != 0) {
size = MSG_ReadShort( msg );
if ( size < 0 || size > (int) sizeof( data ) )
{
Sys::Drop( "CL_ParseDownload: Invalid size %d for download chunk.", size );
}
MSG_ReadData( msg, data, size );
}
CL_AddReliableCommand( "stopdl" );
return;
}
// read the data
block = MSG_ReadShort( msg );
// TTimo - www dl
// if we haven't acked the download redirect yet
if ( block == -1 )
{
if ( !clc.bWWWDl )
{
// server is sending us a www download
cls.originalDownloadName = cls.downloadName;
cls.downloadName = MSG_ReadString( msg );
clc.downloadSize = MSG_ReadLong( msg );
int basePathLen = MSG_ReadLong( msg );
downloadLogger.Debug( "Server sent us a new WWW DL '%s', size %i, prefix len %i",
cls.downloadName, clc.downloadSize, basePathLen );
Cvar_SetValue( "cl_downloadSize", clc.downloadSize );
clc.bWWWDl = true; // activate wwwdl client loop
CL_AddReliableCommand( "wwwdl ack" );
cls.state = connstate_t::CA_DOWNLOADING;
// make sure the server is not trying to redirect us again on a bad checksum
if ( clc.badChecksumList.find( Str::Format( "@%s", cls.originalDownloadName ) ) != std::string::npos ) {
Log::Notice( "refusing redirect to %s by server (bad checksum)", cls.downloadName );
CL_AddReliableCommand( "wwwdl fail" );
clc.bWWWDlAborting = true;
return;
}
if ( !DL_BeginDownload( cls.downloadTempName.c_str(), cls.downloadName.c_str(), basePathLen ) )
{
// setting bWWWDl to false after sending the wwwdl fail doesn't work
// not sure why, but I suspect we have to eat all remaining block -1 that the server has sent us
// still leave a flag so that CL_WWWDownload is inactive
// we count on server sending us a gamestate to start up clean again
CL_AddReliableCommand( "wwwdl fail" );
clc.bWWWDlAborting = true;
Log::Notice( "Failed to initialize download for '%s'", cls.downloadName );
}
return;
}
else
{
// server keeps sending that message till we ack it, eat and ignore
MSG_ReadString( msg );
MSG_ReadLong( msg );
MSG_ReadLong( msg );
return;
}
}
if ( !block )
{
// block zero is special, contains file size
clc.downloadSize = MSG_ReadLong( msg );
downloadLogger.Debug("Starting new direct download of size %i for '%s'", clc.downloadSize, cls.downloadTempName);
Cvar_SetValue( "cl_downloadSize", clc.downloadSize );
if ( clc.downloadSize < 0 )
{
Sys::Drop( MSG_ReadString( msg ) );
}
}
size = MSG_ReadShort( msg );
if ( size < 0 || size > (int) sizeof( data ) )
{
Sys::Drop( "CL_ParseDownload: Invalid size %d for download chunk.", size );
}
downloadLogger.Debug("Received block of size %i", size);
MSG_ReadData( msg, data, size );
if ( clc.downloadBlock != block )
{
downloadLogger.Debug( "CL_ParseDownload: Expected block %i, got %i", clc.downloadBlock, block );
return;
}
// open the file if not opened yet
if ( !clc.download )
{
clc.download = FS_SV_FOpenFileWrite( cls.downloadTempName.c_str() );
if ( !clc.download )
{
Log::Notice( "Could not create %s", cls.downloadTempName );
CL_AddReliableCommand( "stopdl" );
CL_NextDownload();
return;
}
}
if ( size )
{
FS_Write( data, size, clc.download );
}
CL_AddReliableCommand( va( "nextdl %d", clc.downloadBlock ) );
clc.downloadBlock++;
clc.downloadCount += size;
// So UI gets access to it
cl_downloadCount.Set(clc.downloadCount);
if ( !size )
{
downloadLogger.Debug("Received EOF, closing '%s'", cls.downloadTempName);
// A zero length block means EOF
if ( clc.download )
{
FS_FCloseFile( clc.download );
clc.download = 0;
// rename the file
FS_SV_Rename( cls.downloadTempName.c_str(), cls.downloadName.c_str() );
}
cls.downloadTempName.clear();
cls.downloadName.clear();
Cvar_Set( "cl_downloadName", "" );
// send intentions now
// We need this because without it, we would hold the last nextdl and then start
// loading right away. If we take a while to load, the server is happily trying
// to send us that last block over and over.
// Write it twice to help make sure we acknowledge the download
CL_WritePacket();
CL_WritePacket();
// get another file if needed
CL_NextDownload();
}
}