/
cl_update.c
457 lines (382 loc) · 12.1 KB
/
cl_update.c
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
/*
* Wolfenstein: Enemy Territory GPL Source Code
* Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
*
* ET: Legacy
* Copyright (C) 2012 Jan Simek <mail@etlegacy.com>
*
* This file is part of ET: Legacy - http://www.etlegacy.com
*
* ET: Legacy 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.
*
* ET: Legacy 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 ET: Legacy. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, Wolfenstein: Enemy Territory GPL 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 source code.
* If not, please request a copy in writing from id Software at the address below.
*
* id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
*/
/**
* @file cl_update.c
*/
#include "client.h"
#ifdef _WIN32
#define UPDATE_BINARY "updater.exe"
#define UPDATE_CMD "\"%s\" --install-dir \"%s\" --package-dir \"%s\" --script \"%s\" --wait \"%s\" --auto-close --execute \"etl.exe\" --execute-args \"%s\""
#else
#define UPDATE_BINARY "updater"
#define UPDATE_CMD "'%s' --install-dir '%s' --package-dir '%s' --script '%s' --wait '%s' --auto-close --execute 'etl' --execute-args '%s'"
#endif
#define UPDATE_PACKAGE "updater.zip"
#define UPDATE_CONFIG "updater.xml"
#define MIN_PACK_LEN 4
//#define UPDATE_SERVER_DLFILE UPDATE_SERVER_NAME "?sys=" CPUSTRING "&ver=" ETLEGACY_VERSION_SHORT "&file=%s"
autoupdate_t autoupdate;
void CL_CheckAutoUpdate(void)
{
char info[MAX_INFO_STRING];
#ifndef FEATURE_AUTOUPDATE
return;
#endif
if (!com_autoupdate->integer)
{
Com_DPrintf("Updater is disabled by com_autoupdate 0.\n");
return;
}
// Only check once per session
if (autoupdate.updateChecked)
{
return;
}
// Resolve update server
Com_Printf("Updater: resolving %s... ", UPDATE_SERVER_NAME);
if (!NET_StringToAdr(va("%s:%i", UPDATE_SERVER_NAME, PORT_UPDATE), &autoupdate.autoupdateServer, NA_UNSPEC))
{
Com_Printf("couldn't resolve address\n");
autoupdate.updateChecked = qtrue;
return;
}
else
{
Com_Printf("resolved to %s\n", NET_AdrToString(autoupdate.autoupdateServer));
}
info[0] = 0;
Info_SetValueForKey(info, "version", ETLEGACY_VERSION_SHORT);
Info_SetValueForKey(info, "platform", CPUSTRING);
Info_SetValueForKey(info, va("etl_bin_%s.pk3", ETLEGACY_VERSION_SHORT),
Com_MD5File(va("legacy/etl_bin_%s.pk3", ETLEGACY_VERSION_SHORT), 0, NULL, 0));
Info_SetValueForKey(info, va("pak3_%s.pk3", ETLEGACY_VERSION_SHORT),
Com_MD5File(va("legacy/pak3_%s.pk3", ETLEGACY_VERSION_SHORT), 0, NULL, 0));
NET_OutOfBandPrint(NS_CLIENT, autoupdate.autoupdateServer, "getUpdateInfo \"%s\"", info);
autoupdate.updateChecked = qtrue;
}
void CL_GetAutoUpdate(void)
{
#ifdef FEATURE_AUTOUPDATE
// Don't try and get an update if we haven't checked for one
if (!autoupdate.updateChecked)
{
return;
}
// Make sure there's a valid update file to request
if (strlen(com_updatefiles->string) < 5)
{
return;
}
Com_DPrintf("Connecting to auto-update server...\n");
S_StopAllSounds();
// starting to load a map so we get out of full screen ui mode
Cvar_Set("r_uiFullScreen", "0");
// toggle on all the download related cvars
Cvar_Set("cl_allowDownload", "1"); // general flag
Cvar_Set("cl_wwwDownload", "1"); // ftp/http support
// clear any previous "server full" type messages
clc.serverMessage[0] = 0;
if (com_sv_running->integer)
{
// if running a local server, kill it
SV_Shutdown("Server quit\n");
}
// make sure a local server is killed
Cvar_Set("sv_killserver", "1");
SV_Frame(0);
CL_Disconnect(qtrue);
Con_Close();
Q_strncpyz(cls.servername, "ET:L Update Server", sizeof(cls.servername));
if (autoupdate.autoupdateServer.type == NA_BAD)
{
Com_Printf("Bad server address\n");
cls.state = CA_DISCONNECTED;
Cvar_Set("ui_connecting", "0");
return;
}
// Copy auto-update server address to Server connect address
memcpy(&clc.serverAddress, &autoupdate.autoupdateServer, sizeof(netadr_t));
Com_DPrintf("%s resolved to %s\n", cls.servername,
NET_AdrToString(clc.serverAddress));
cls.state = CA_DISCONNECTED;
Cvar_Set("ui_connecting", "1");
Cvar_Set("ui_dl_running", "1");
cls.keyCatchers = 0;
clc.connectTime = -99999; // CL_CheckForResend() will fire immediately
clc.connectPacketCount = 0;
// server connection string
Cvar_Set("cl_currentServerAddress", "ET:L Update Server");
CL_CheckUpdateStarted();
#endif /* FEATURE_AUTOUPDATE */
}
static void CL_RunUpdateBinary(const char *updateBinary, const char *updateConfig)
{
static char fn[MAX_OSPATH];
Q_strncpyz(fn, FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, updateBinary), MAX_OSPATH);
#ifndef _WIN32
Sys_Chmod(fn, S_IXUSR);
#endif
// will either exit with a successful process spawn, or will Com_Error ERR_DROP
// so we need to clear the disconnected download data if needed
if (cls.bWWWDlDisconnected)
{
cls.bWWWDlDisconnected = qfalse;
CL_ClearStaticDownload();
}
Sys_StartProcess(va(UPDATE_CMD, fn, Cvar_VariableString("fs_basepath"), FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, NULL), FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, updateConfig), Cvar_VariableString("com_pid"), Com_GetCommandLine()), qtrue);
// reinitialize the filesystem if the game directory or checksum has changed
// - after Legacy mod update
FS_ConditionalRestart(clc.checksumFeed);
}
static qboolean CL_UnpackUpdatePackage(const char *pack, const char *bin, const char *config)
{
char *fn1 = FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, pack);
char *fn2 = FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, NULL);
if (FS_UnzipTo(fn1, fn2, qtrue))
{
fn1 = FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, bin);
fn2 = FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, config);
if (FS_FileInPathExists(fn1) && FS_FileInPathExists(fn2))
{
return qtrue;
}
else
{
Com_Printf("The updater binary or config does not exist\n");
}
}
else
{
Com_Printf("Failed to unpack the update package\n");
}
return qfalse;
}
static void CL_CLeanUpdateFolder(const char *bin)
{
//We just remove the old updater here, if it exists.
//The update installer itself does other cleanups
char *fn1 = FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, bin);
if (FS_FileInPathExists(fn1))
{
FS_Remove(fn1);
}
}
qboolean CL_CheckUpdateDownloads(void)
{
#ifdef FEATURE_AUTOUPDATE
// Auto-update
if (autoupdate.updateStarted)
{
if (strlen(com_updatefiles->string) > MIN_PACK_LEN)
{
CL_InitDownloads();
return qtrue;
}
CL_CLeanUpdateFolder(UPDATE_BINARY);
if (CL_UnpackUpdatePackage(UPDATE_PACKAGE, UPDATE_BINARY, UPDATE_CONFIG))
{
CL_RunUpdateBinary(UPDATE_BINARY, UPDATE_CONFIG);
}
else
{
Cvar_Set("ui_connecting", "0");
Cvar_Set("ui_dl_running", "0");
}
autoupdate.updateStarted = qfalse;
CL_Disconnect(qtrue);
// we can reset that now
cls.bWWWDlDisconnected = qfalse;
CL_ClearStaticDownload();
return qtrue;
}
#endif // FEATURE_AUTOUPDATE
return qfalse;
}
qboolean CL_InitUpdateDownloads(void)
{
#ifdef FEATURE_AUTOUPDATE
if (autoupdate.updateStarted && NET_CompareAdr(autoupdate.autoupdateServer, clc.serverAddress))
{
if (strlen(com_updatefiles->string) > MIN_PACK_LEN)
{
char *updateFile;
char updateFilesRemaining[MAX_TOKEN_CHARS] = "";
clc.bWWWDl = qtrue;
cls.bWWWDlDisconnected = qtrue;
updateFile = strtok(com_updatefiles->string, ";");
if (updateFile == NULL)
{
Com_Error(ERR_AUTOUPDATE, "Could not parse update string.");
}
else
{
// download format: @remotename@localname
Q_strncpyz(clc.downloadList, va("@%s@%s", updateFile, updateFile), MAX_INFO_STRING);
Q_strncpyz(cls.originalDownloadName, va("%s/%s", AUTOUPDATE_DIR, updateFile), sizeof(cls.originalDownloadName));
if (!Q_stricmp(updateFile, UPDATE_PACKAGE))
{
Q_strncpyz(cls.downloadName, va("%s/updater/%s-%s-%s", UPDATE_SERVER_NAME, ETLEGACY_VERSION_SHORT, CPUSTRING, updateFile), sizeof(cls.downloadName));
}
else
{
Q_strncpyz(cls.downloadName, va("%s/packages/%s", UPDATE_SERVER_NAME, updateFile), sizeof(cls.downloadName));
}
Q_strncpyz(cls.downloadTempName, FS_BuildOSPath(Cvar_VariableString("fs_homepath"), AUTOUPDATE_DIR, va("%s.tmp", updateFile)), sizeof(cls.downloadTempName));
// TODO: add file size, so UI can show progress bar
//Cvar_SetValue("cl_downloadSize", clc.downloadSize);
if (!DL_BeginDownload(cls.downloadTempName, cls.downloadName))
{
Com_Error(ERR_AUTOUPDATE, "Could not download an update file: \"%s\"", cls.downloadName);
clc.bWWWDlAborting = qtrue;
}
while (1)
{
updateFile = strtok(NULL, ";");
if (updateFile == NULL)
{
break;
}
Q_strcat(updateFilesRemaining, sizeof(updateFilesRemaining), va("%s;", updateFile));
}
if (strlen(updateFilesRemaining) > MIN_PACK_LEN)
{
Cvar_Set("com_updatefiles", updateFilesRemaining);
}
else
{
Cvar_Set("com_updatefiles", "");
}
}
}
return qtrue;
}
#endif // FEATURE_AUTOUPDATE
return qfalse;
}
qboolean CL_UpdatePacketEvent(netadr_t from)
{
#ifdef FEATURE_AUTOUPDATE
static qboolean autoupdateRedirected = qfalse;
// Update server doesn't understand netchan packets
if (NET_CompareAdr(autoupdate.autoupdateServer, from))
{
if (autoupdate.updateStarted && !autoupdateRedirected)
{
autoupdateRedirected = qtrue;
//CL_InitDownloads();
return qtrue;
}
}
#endif /* FEATURE_AUTOUPDATE */
return qfalse;
}
/*
===================
CL_UpdateInfoPacket
===================
*/
void CL_UpdateInfoPacket(netadr_t from)
{
if (autoupdate.autoupdateServer.type == NA_BAD)
{
Com_DPrintf("CL_UpdateInfoPacket: Update server has bad address\n");
return;
}
Com_DPrintf("Update server resolved to %s\n",
NET_AdrToString(autoupdate.autoupdateServer));
if (!NET_CompareAdr(from, autoupdate.autoupdateServer))
{
// TODO: when the updater is server-side as well, write this message to the Attack log
Com_DPrintf("CL_UpdateInfoPacket: Ignoring packet from %s, because the update server is located at %s\n",
NET_AdrToString(from), NET_AdrToString(autoupdate.autoupdateServer));
return;
}
Cvar_Set("com_updateavailable", Cmd_Argv(1));
Cvar_Set("com_updatefiles", "");
if (com_updateavailable->integer)
{
Cvar_Set("com_updatemessage", Cmd_Argv(2));
if (com_updateavailable->integer == 2)
{
Cvar_Set("com_updatefiles", Cmd_Argv(3));
if (autoupdate.forceUpdate)
{
CL_GetAutoUpdate();
autoupdate.forceUpdate = qfalse;
return;
}
}
#ifdef FEATURE_AUTOUPDATE
VM_Call(uivm, UI_SET_ACTIVE_MENU, UIMENU_WM_AUTOUPDATE);
#endif /* FEATURE_AUTOUPDATE */
}
}
void CL_CheckUpdateStarted(void)
{
// If we have completed a connection to the Auto-Update server...
if (autoupdate.updateChecked && NET_CompareAdr(autoupdate.autoupdateServer, clc.serverAddress))
{
// Mark the client as being in the process of getting an update
if (com_updateavailable->integer)
{
autoupdate.updateStarted = qtrue;
CL_InitDownloads();
}
}
}
void CL_UpdateVarsClean(int flags)
{
switch (flags)
{
case CLEAR_ALL:
Cvar_Set("com_updatefiles", "");
Cvar_Set("com_updatemessage", "");
Cvar_Set("com_updatefiles", "");
case CLEAR_FLAGS:
autoupdate.updateChecked = qfalse;
autoupdate.forceUpdate = qfalse;
default:
autoupdate.updateStarted = qfalse;
break;
}
}
void CL_RunUpdate(void)
{
if (!autoupdate.updateChecked)
{
autoupdate.forceUpdate = qtrue;
CL_CheckAutoUpdate();
}
else
{
CL_GetAutoUpdate();
}
}