summaryrefslogtreecommitdiff
path: root/src/client/download.c
blob: 01896856a064f1c9fd2bed1568d84f4563905b96 (plain)
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program 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 2 of the License, or
(at your option) any later version.

This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

//
// cl_download.c -- queue manager and UDP downloads
//

#include "client.h"
#include "format/md2.h"
#include "format/sp2.h"

#define CL_DOWNLOAD_IGNORES     "download-ignores.txt"

typedef enum {
    PRECACHE_MODELS,
    PRECACHE_OTHER,
    PRECACHE_MAP,
    PRECACHE_FINAL
} precache_t;

static precache_t precache_check;
static int precache_sexed_sounds[MAX_SOUNDS];
static int precache_sexed_total;

/*
===============
CL_QueueDownload

Adds new download path into queue, incrementing pending count.
Entry will stay in queue for entire lifetime of server connection,
to make sure each path is tried exactly once.
===============
*/
qerror_t CL_QueueDownload(const char *path, dltype_t type)
{
    dlqueue_t *q;
    size_t len;

    FOR_EACH_DLQ(q) {
        // avoid sending duplicate requests
        if (!FS_pathcmp(path, q->path)) {
            Com_DPrintf("%s: %s [DUP]\n", __func__, path);
            return Q_ERR_EXIST;
        }
    }

    len = strlen(path);
    if (len >= MAX_QPATH) {
        Com_Error(ERR_DROP, "%s: oversize quake path", __func__);
    }

    q = Z_Malloc(sizeof(*q) + len);
    memcpy(q->path, path, len + 1);
    q->type = type;
    q->state = DL_PENDING;

#if USE_CURL
    // paks get bumped to the top and HTTP switches to single downloading.
    // this prevents someone on 28k dialup trying to do both the main .pak
    // and referenced configstrings data at once.
    if (type == DL_PAK)
        List_Insert(&cls.download.queue, &q->entry);
    else
#endif
        List_Append(&cls.download.queue, &q->entry);

    cls.download.pending++;
    Com_DPrintf("%s: %s [%d]\n", __func__, path, cls.download.pending);
    return Q_ERR_SUCCESS;
}

/*
===============
CL_IgnoreDownload

Returns true if specified path matches against an entry in download ignore
list.
===============
*/
qboolean CL_IgnoreDownload(const char *path)
{
    string_entry_t *entry;

    for (entry = cls.download.ignores; entry; entry = entry->next) {
        if (Com_WildCmp(entry->string, path)) {
            return qtrue;
        }
    }

    return qfalse;
}

/*
===============
CL_FinishDownload

Mark the queue entry as done, decrementing pending count.
===============
*/
void CL_FinishDownload(dlqueue_t *q)
{
    if (q->state == DL_DONE) {
        Com_Error(ERR_DROP, "%s: already done", __func__);
    }
    if (!cls.download.pending) {
        Com_Error(ERR_DROP, "%s: bad pending count", __func__);
    }

    q->state = DL_DONE;
    cls.download.pending--;
    Com_DPrintf("%s: %s [%d]\n", __func__, q->path, cls.download.pending);
}

/*
===============
CL_CleanupDownloads

Disconnected from server, clean up.
===============
*/
void CL_CleanupDownloads(void)
{
    dlqueue_t *q, *n;

    HTTP_CleanupDownloads();

    FOR_EACH_DLQ_SAFE(q, n) {
        Z_Free(q);
    }

    List_Init(&cls.download.queue);
    cls.download.pending = 0;

    cls.download.current = NULL;
    cls.download.percent = 0;
    cls.download.position = 0;

    if (cls.download.file) {
        FS_FCloseFile(cls.download.file);
        cls.download.file = 0;
    }

    cls.download.temp[0] = 0;

#if USE_ZLIB
    inflateEnd(&cls.download.z);
#endif
}

/*
===============
CL_LoadDownloadIgnores

Allow mods to provide a list of paths that are known to be non-existent and
should never be downloaded (e.g. model specific sounds).
===============
*/
void CL_LoadDownloadIgnores(void)
{
    string_entry_t *entry, *next;
    char *raw, *data, *p;
    int count, line;
    ssize_t len;

    // free previous entries
    for (entry = cls.download.ignores; entry; entry = next) {
        next = entry->next;
        Z_Free(entry);
    }

    cls.download.ignores = NULL;

    // load new list
    len = FS_LoadFile(CL_DOWNLOAD_IGNORES, (void **)&raw);
    if (!raw) {
        if (len != Q_ERR_NOENT)
            Com_EPrintf("Couldn't load %s: %s\n",
                        CL_DOWNLOAD_IGNORES, Q_ErrorString(len));
        return;
    }

    count = 0;
    line = 1;
    data = raw;

    while (*data) {
        p = strchr(data, '\n');
        if (p) {
            if (p > data && *(p - 1) == '\r')
                *(p - 1) = 0;
            *p = 0;
        }

        // ignore empty lines and comments
        if (*data && *data != '#' && *data != '/') {
            len = strlen(data);
            if (len < MAX_QPATH) {
                entry = Z_Malloc(sizeof(*entry) + len);
                memcpy(entry->string, data, len + 1);
                entry->next = cls.download.ignores;
                cls.download.ignores = entry;
                count++;
            } else {
                Com_WPrintf("Oversize filter on line %d in %s\n",
                            line, CL_DOWNLOAD_IGNORES);
            }
        }

        if (!p)
            break;

        data = p + 1;
        line++;
    }

    Com_DPrintf("Loaded %d filters from %s\n", count, CL_DOWNLOAD_IGNORES);

    FS_FreeFile(raw);
}

static qboolean start_udp_download(dlqueue_t *q)
{
    size_t len;
    qhandle_t f;
    ssize_t ret;

    len = strlen(q->path);
    if (len >= MAX_QPATH) {
        Com_Error(ERR_DROP, "%s: oversize quake path", __func__);
    }

    // download to a temp name, and only rename
    // to the real name when done, so if interrupted
    // a runt file wont be left
    memcpy(cls.download.temp, q->path, len);
    memcpy(cls.download.temp + len, ".tmp", 5);

    // check to see if we already have a tmp for this file, if so, try to resume
    // open the file if not opened yet
    ret = FS_FOpenFile(cls.download.temp, &f, FS_MODE_RDWR);
    if (ret >= 0) {  // it exists
        cls.download.file = f;
        cls.download.position = ret;
        // give the server an offset to start the download
        Com_DPrintf("[UDP] Resuming %s\n", q->path);
#if USE_ZLIB
        if (cls.serverProtocol == PROTOCOL_VERSION_R1Q2)
            CL_ClientCommand(va("download \"%s\" %"PRIz" udp-zlib", q->path, ret));
        else
#endif
            CL_ClientCommand(va("download \"%s\" %"PRIz, q->path, ret));
    } else if (ret == Q_ERR_NOENT) {  // it doesn't exist
        Com_DPrintf("[UDP] Downloading %s\n", q->path);
#if USE_ZLIB
        if (cls.serverProtocol == PROTOCOL_VERSION_R1Q2)
            CL_ClientCommand(va("download \"%s\" %"PRIz" udp-zlib", q->path, (size_t)0));
        else
#endif
            CL_ClientCommand(va("download \"%s\"", q->path));
    } else { // error happened
        Com_EPrintf("[UDP] Couldn't open %s for appending: %s\n",
                    cls.download.temp, Q_ErrorString(ret));
        CL_FinishDownload(q);
        return qfalse;
    }

    q->state = DL_RUNNING;
    cls.download.current = q;
    return qtrue;
}

/*
===============
CL_StartNextDownload

Start another UDP download if possible
===============
*/
void CL_StartNextDownload(void)
{
    dlqueue_t *q;

    if (!cls.download.pending || cls.download.current) {
        return;
    }

    FOR_EACH_DLQ(q) {
        if (q->state == DL_PENDING) {
            if (start_udp_download(q)) {
                break;
            }
        }
    }
}

static void finish_udp_download(const char *msg)
{
    dlqueue_t *q = cls.download.current;

    // finished with current path
    CL_FinishDownload(q);

    cls.download.current = NULL;
    cls.download.percent = 0;
    cls.download.position = 0;

    if (cls.download.file) {
        FS_FCloseFile(cls.download.file);
        cls.download.file = 0;
    }

    cls.download.temp[0] = 0;

#if USE_ZLIB
    inflateReset(&cls.download.z);
#endif

    if (msg) {
        Com_Printf("[UDP] %s [%s] [%d remaining file%s]\n",
                   q->path, msg, cls.download.pending,
                   cls.download.pending == 1 ? "" : "s");
    }

    // get another file if needed
    CL_RequestNextDownload();
    CL_StartNextDownload();
}

static int write_udp_download(byte *data, int size)
{
    ssize_t ret;

    ret = FS_Write(data, size, cls.download.file);
    if (ret != size) {
        Com_EPrintf("[UDP] Couldn't write %s: %s\n",
                    cls.download.temp, Q_ErrorString(ret));
        finish_udp_download(NULL);
        return -1;
    }

    return 0;
}

// handles both continuous deflate stream for entire download and chunked
// per-packet streams for compatibility.
static int inflate_udp_download(byte *data, int inlen, int outlen)
{
#if USE_ZLIB

#define CHUNK   0x10000

    z_streamp   z = &cls.download.z;
    byte        buffer[CHUNK];
    int         ret;

    // initialize stream if not done yet
    if (z->state == NULL && inflateInit2(z, -MAX_WBITS) != Z_OK)
        Com_Error(ERR_FATAL, "%s: inflateInit2() failed", __func__);

    z->next_in = data;
    z->avail_in = inlen;

    // run inflate() until output buffer not full
    do {
        z->next_out = buffer;
        z->avail_out = CHUNK;

        ret = inflate(z, Z_SYNC_FLUSH);
        if (ret != Z_OK && ret != Z_STREAM_END) {
            Com_EPrintf("[UDP] inflate() failed: %s\n", z->msg);
            finish_udp_download(NULL);
            return -1;
        }

        Com_DDPrintf("%s: %u --> %u [%d]\n",
                     __func__,
                     inlen - z->avail_in,
                     CHUNK - z->avail_out,
                     ret);

        if (write_udp_download(buffer, CHUNK - z->avail_out))
            return -1;
    } while (z->avail_out == 0);

    // check uncompressed length if known
    if (outlen > 0 && outlen != z->total_out)
        Com_WPrintf("[UDP] Decompressed length mismatch: %d != %lu\n", outlen, z->total_out);

    // prepare for the next stream if done
    if (ret == Z_STREAM_END)
        inflateReset(z);

    return 0;
#else
    // should never happen
    Com_Error(ERR_DROP, "Compressed server packet received, "
              "but no zlib support linked in.");
#endif
}

/*
=====================
CL_HandleDownload

An UDP download data has been received from the server.
=====================
*/
void CL_HandleDownload(byte *data, int size, int percent, int compressed)
{
    dlqueue_t *q = cls.download.current;
    qerror_t ret;

    if (!q) {
        Com_Error(ERR_DROP, "%s: no download requested", __func__);
    }

    if (size == -1) {
        if (!percent) {
            finish_udp_download("FAIL");
        } else {
            finish_udp_download("STOP");
        }
        return;
    }

    // open the file if not opened yet
    if (!cls.download.file) {
        ret = FS_FOpenFile(cls.download.temp, &cls.download.file, FS_MODE_WRITE);
        if (!cls.download.file) {
            Com_EPrintf("[UDP] Couldn't open %s for writing: %s\n",
                        cls.download.temp, Q_ErrorString(ret));
            finish_udp_download(NULL);
            return;
        }
    }

    if (compressed) {
        if (inflate_udp_download(data, size, compressed))
            return;
    } else {
        if (write_udp_download(data, size))
            return;
    }

    if (percent != 100) {
        // request next block
        // change display routines by zoid
        cls.download.percent = percent;
        cls.download.position += size;

        CL_ClientCommand("nextdl");
    } else {
        // close the file before renaming
        FS_FCloseFile(cls.download.file);
        cls.download.file = 0;

        // rename the temp file to its final name
        ret = FS_RenameFile(cls.download.temp, q->path);
        if (ret) {
            Com_EPrintf("[UDP] Couldn't rename %s to %s: %s\n",
                        cls.download.temp, q->path, Q_ErrorString(ret));
            finish_udp_download(NULL);
        } else {
            finish_udp_download("DONE");
        }
    }
}


/*
===============
CL_CheckDownloadExtension

Only predefined set of filename extensions is allowed,
to prevent the server from uploading arbitrary files.
===============
*/
qboolean CL_CheckDownloadExtension(const char *ext)
{
    static const char allowed[][4] = {
        "pcx", "wal", "wav", "md2", "sp2", "tga", "png",
        "jpg", "bsp", "ent", "txt", "dm2", "loc", "md3"
    };
    int i;

    for (i = 0; i < q_countof(allowed); i++)
        if (!Q_stricmp(ext, allowed[i]))
            return qtrue;

    return qfalse;
}

// attempts to start a download from the server if file doesn't exist.
static qerror_t check_file_len(const char *path, size_t len, dltype_t type)
{
    char buffer[MAX_QPATH], *ext;
    qerror_t ret;
    int valid;

    // check for oversize path
    if (len >= MAX_QPATH)
        return Q_ERR_NAMETOOLONG;

    // normalize path
    len = FS_NormalizePath(buffer, path);

    // check for empty path
    if (len == 0)
        return Q_ERR_NAMETOOSHORT;

    valid = FS_ValidatePath(buffer);

    // check path
    if (valid == PATH_INVALID
        || !Q_ispath(buffer[0])
        || !Q_ispath(buffer[len - 1])
        || strstr(buffer, "..")
        || !strchr(buffer, '/')) {
        // some of these checks are too conservative or even redundant
        // once we have normalized the path, however they have to stay for
        // compatibility reasons with older servers (some would even ban us
        // for sending .. for example)
        return Q_ERR_INVALID_PATH;
    }

    // check extension
    ext = COM_FileExtension(buffer);
    if (*ext != '.' || !CL_CheckDownloadExtension(ext + 1))
        return Q_ERR_INVALID_PATH;

    if (FS_FileExists(buffer))
        // it exists, no need to download
        return Q_ERR_EXIST;

    if (valid == PATH_MIXED_CASE)
        // convert to lower case to make download server happy
        Q_strlwr(buffer);

    if (CL_IgnoreDownload(buffer))
        return Q_ERR_PERM;

    ret = HTTP_QueueDownload(buffer, type);
    if (ret != Q_ERR_NOSYS)
        return ret;

    // queue and start legacy UDP download
    ret = CL_QueueDownload(buffer, type);
    if (ret == Q_ERR_SUCCESS)
        CL_StartNextDownload();

    return ret;
}

#define check_file(path, type) \
    check_file_len(path, strlen(path), type)

static void check_skins(const char *name)
{
    size_t          i, num_skins, ofs_skins, end_skins;
    byte            *model;
    size_t          len;
    dmd2header_t    *md2header;
    dsp2header_t    *sp2header;
    char            *md2skin;
    dsp2frame_t     *sp2frame;
    uint32_t        ident;
    char            fn[MAX_QPATH];

    len = FS_LoadFile(name, (void **)&model);
    if (!model) {
        // couldn't load it
        return;
    }

    if (len < sizeof(ident)) {
        // file too small
        goto done;
    }

    // check ident
    ident = LittleLong(*(uint32_t *)model);
    switch (ident) {
    case MD2_IDENT:
        // alias model
        md2header = (dmd2header_t *)model;
        if (len < sizeof(*md2header) ||
            LittleLong(md2header->ident) != MD2_IDENT ||
            LittleLong(md2header->version) != MD2_VERSION) {
            // not an alias model
            goto done;
        }

        num_skins = LittleLong(md2header->num_skins);
        ofs_skins = LittleLong(md2header->ofs_skins);
        end_skins = ofs_skins + num_skins * MD2_MAX_SKINNAME;
        if (num_skins > MD2_MAX_SKINS || end_skins < ofs_skins || end_skins > len) {
            // bad alias model
            goto done;
        }

        md2skin = (char *)model + ofs_skins;
        for (i = 0; i < num_skins; i++) {
            if (!Q_memccpy(fn, md2skin, 0, sizeof(fn))) {
                // bad alias model
                goto done;
            }
            check_file(fn, DL_OTHER);
            md2skin += MD2_MAX_SKINNAME;
        }
        break;

    case SP2_IDENT:
        // sprite model
        sp2header = (dsp2header_t *)model;
        if (len < sizeof(*sp2header) ||
            LittleLong(sp2header->ident) != SP2_IDENT ||
            LittleLong(sp2header->version) != SP2_VERSION) {
            // not a sprite model
            goto done;
        }

        num_skins = LittleLong(sp2header->numframes);
        ofs_skins = sizeof(*sp2header);
        end_skins = ofs_skins + num_skins * sizeof(dsp2frame_t);
        if (num_skins > SP2_MAX_FRAMES || end_skins < ofs_skins || end_skins > len) {
            // bad sprite model
            goto done;
        }

        sp2frame = (dsp2frame_t *)(model + ofs_skins);
        for (i = 0; i < num_skins; i++) {
            if (!Q_memccpy(fn, sp2frame->name, 0, sizeof(fn))) {
                // bad sprite model
                goto done;
            }
            check_file(fn, DL_OTHER);
            sp2frame++;
        }
        break;

    default:
        // unknown file format
        goto done;
    }

done:
    FS_FreeFile(model);
}

static void check_player(const char *name)
{
    char fn[MAX_QPATH], model[MAX_QPATH], skin[MAX_QPATH], *p;
    size_t len;
    int i, j;

    CL_ParsePlayerSkin(NULL, model, skin, name);

    // model
    len = Q_concat(fn, sizeof(fn), "players/", model, "/tris.md2", NULL);
    check_file_len(fn, len, DL_OTHER);

    // weapon models
    for (i = 0; i < cl.numWeaponModels; i++) {
        len = Q_concat(fn, sizeof(fn), "players/", model, "/", cl.weaponModels[i], NULL);
        check_file_len(fn, len, DL_OTHER);
    }

    // default weapon skin
    len = Q_concat(fn, sizeof(fn), "players/", model, "/weapon.pcx", NULL);
    check_file_len(fn, len, DL_OTHER);

    // skin
    len = Q_concat(fn, sizeof(fn), "players/", model, "/", skin, ".pcx", NULL);
    check_file_len(fn, len, DL_OTHER);

    // skin_i
    len = Q_concat(fn, sizeof(fn), "players/", model, "/", skin, "_i.pcx", NULL);
    check_file_len(fn, len, DL_OTHER);

    // sexed sounds
    for (i = 0; i < precache_sexed_total; i++) {
        j = precache_sexed_sounds[i];
        p = cl.configstrings[CS_SOUNDS + j];

        if (*p == '*') {
            len = Q_concat(fn, sizeof(fn), "players/", model, "/", p + 1, NULL);
            check_file_len(fn, len, DL_OTHER);
        }
    }
}

// for precaching dependencies
static qboolean downloads_pending(dltype_t type)
{
    dlqueue_t *q;

    // DL_OTHER just checks for any download
    if (type == DL_OTHER) {
        return !!cls.download.pending;
    }

    // see if there are pending downloads of the given type
    FOR_EACH_DLQ(q) {
        if (q->state != DL_DONE && q->type == type) {
            return qtrue;
        }
    }

    return qfalse;
}

/*
=====================
CL_RequestNextDownload

Runs precache check and dispatches downloads.
=====================
*/
void CL_RequestNextDownload(void)
{
    char fn[MAX_QPATH], *name;
    size_t len;
    int i;

    if (cls.state != ca_connected && cls.state != ca_loading)
        return;

    if (allow_download->integer <= 0 || NET_IsLocalAddress(&cls.serverAddress)) {
        if (precache_check <= PRECACHE_MAP) {
            CL_RegisterBspModels();
        }

        CL_Begin();
        return;
    }

    switch (precache_check) {
    case PRECACHE_MODELS:
        // confirm map
        if (allow_download_maps->integer)
            check_file(cl.configstrings[CS_MODELS + 1], DL_MAP);

        // checking for models
        if (allow_download_models->integer) {
            for (i = 2; i < MAX_MODELS; i++) {
                name = cl.configstrings[CS_MODELS + i];
                if (!name[0]) {
                    break;
                }
                if (name[0] == '*' || name[0] == '#') {
                    continue;
                }
                check_file(name, DL_MODEL);
            }
        }

        precache_check = PRECACHE_OTHER;
        // fall through

    case PRECACHE_OTHER:
        if (allow_download_models->integer) {
            if (downloads_pending(DL_MODEL)) {
                // pending downloads (models), let's wait here before we can check skins.
                Com_DPrintf("%s: waiting for models...\n", __func__);
                return;
            }

            for (i = 2; i < MAX_MODELS; i++) {
                name = cl.configstrings[CS_MODELS + i];
                if (!name[0]) {
                    break;
                }
                if (name[0] == '*' || name[0] == '#') {
                    continue;
                }
                check_skins(name);
            }
        }

        if (allow_download_sounds->integer) {
            for (i = 1; i < MAX_SOUNDS; i++) {
                name = cl.configstrings[CS_SOUNDS + i];
                if (!name[0]) {
                    break;
                }
                if (name[0] == '*') {
                    continue;
                }
                if (name[0] == '#') {
                    len = Q_strlcpy(fn, name + 1, sizeof(fn));
                } else {
                    len = Q_concat(fn, sizeof(fn), "sound/", name, NULL);
                }
                check_file_len(fn, len, DL_OTHER);
            }
        }

        if (allow_download_pics->integer) {
            for (i = 1; i < MAX_IMAGES; i++) {
                name = cl.configstrings[CS_IMAGES + i];
                if (!name[0]) {
                    break;
                }
                if (name[0] == '/' || name[0] == '\\') {
                    len = Q_strlcpy(fn, name + 1, sizeof(fn));
                } else {
                    len = Q_concat(fn, sizeof(fn), "pics/", name, ".pcx", NULL);
                }
                check_file_len(fn, len, DL_OTHER);
            }
        }

        if (allow_download_players->integer) {
            // find sexed sounds
            precache_sexed_total = 0;
            for (i = 1; i < MAX_SOUNDS; i++) {
                if (cl.configstrings[CS_SOUNDS + i][0] == '*') {
                    precache_sexed_sounds[precache_sexed_total++] = i;
                }
            }

            for (i = 0; i < MAX_CLIENTS; i++) {
                name = cl.configstrings[CS_PLAYERSKINS + i];
                if (!name[0]) {
                    continue;
                }
                check_player(name);
            }
        }

        if (allow_download_textures->integer) {
            static const char env_suf[6][3] = {
                "rt", "bk", "lf", "ft", "up", "dn"
            };

            for (i = 0; i < 6; i++) {
                len = Q_concat(fn, sizeof(fn), "env/", cl.configstrings[CS_SKY], env_suf[i], ".tga", NULL);
                check_file_len(fn, len, DL_OTHER);
            }
        }

        precache_check = PRECACHE_MAP;
        // fall through

    case PRECACHE_MAP:
        if (downloads_pending(DL_MAP)) {
            // map might still be downloading?
            Com_DPrintf("%s: waiting for map...\n", __func__);
            return;
        }

        // load the map file before checking textures
        CL_RegisterBspModels();

        if (allow_download_textures->integer) {
            for (i = 0; i < cl.bsp->numtexinfo; i++) {
                len = Q_concat(fn, sizeof(fn), "textures/", cl.bsp->texinfo[i].name, ".wal", NULL);
                check_file_len(fn, len, DL_OTHER);
            }
        }

        precache_check = PRECACHE_FINAL;
        // fall through

    case PRECACHE_FINAL:
        if (downloads_pending(DL_OTHER)) {
            // pending downloads (possibly textures), let's wait here.
            Com_DPrintf("%s: waiting for others...\n", __func__);
            return;
        }

        // all done, tell server we are ready
        CL_Begin();
        break;

    default:
        Com_Error(ERR_DROP, "%s: bad precache_check\n", __func__);
    }
}

void CL_ResetPrecacheCheck(void)
{
    precache_check = PRECACHE_MODELS;
}

/*
===============
CL_Download_f

Request a download from the server
===============
*/
static void CL_Download_f(void)
{
    char *path;
    qerror_t ret;

    if (cls.state < ca_connected) {
        Com_Printf("Must be connected to a server.\n");
        return;
    }

    if (allow_download->integer == -1) {
        Com_Printf("Downloads are permanently disabled.\n");
        return;
    }

    if (Cmd_Argc() != 2) {
        Com_Printf("Usage: download <filename>\n");
        return;
    }

    path = Cmd_Argv(1);

    ret = check_file(path, DL_OTHER);
    if (ret) {
        Com_Printf("Couldn't download %s: %s\n", path, Q_ErrorString(ret));
    }
}

void CL_InitDownloads(void)
{
    Cmd_AddCommand("download", CL_Download_f);

    List_Init(&cls.download.queue);
}