forked from SpiritQuaddicted/reQuiem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_file.c
2175 lines (1821 loc) · 46.1 KB
/
common_file.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
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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1996-1997 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// common_file.c -- functions used in client and server file management
#include "quakedef.h"
#ifdef _WIN32
# include "io.h"
#else
# include <sys/stat.h>
#endif
#include "dzip.h"
#define MAX_FILES_IN_PACK 2048
// on disk
typedef struct
{
char name[56];
int filepos, filelen;
} dpackfile_t;
typedef struct
{
char id[4];
int dirofs;
int dirlen;
} dpackheader_t;
cvar_t com_matchfilecase = {"host_matchfilecase", "0", CVAR_FLAG_ARCHIVE};
searchpath_t *com_searchpaths;
int com_filesize = -1;
char com_netpath[MAX_OSPATH];
const searchpath_t *com_filepath = NULL; // JDH: to store path of last loaded file
char com_gamedir[MAX_OSPATH];
char com_basedir[MAX_OSPATH];
char com_gamedirname[MAX_QPATH];
#ifdef HEXEN2_SUPPORT
char com_savedir[MAX_OSPATH];
#endif
searchpath_t *com_base_searchpaths; // without id1 and its packs
searchpath_t *com_game_searchpaths; // without -hipnotic, -rogue, -nehahra, -quoth
int num_img_exts;
const char *com_img_exts[4]; // tga, jpg, pcx, and png (png and jpg iff library available)
#define NUM_DEMO_EXTS 3
const char * com_demo_exts[NUM_DEMO_EXTS] = {".dem", ".qwd", ".dz"};
const char * com_mdl_exts[NUM_MODEL_EXTS] = {".mdl", ".md2", ".md3"};
const char * com_music_exts[2] = {".mp3", ".ogg"};
qboolean hipnotic = false;
qboolean rogue = false;
qboolean nehahra = false;
#ifdef HEXEN2_SUPPORT
qboolean hexen2 = false;
#endif
qboolean dzlib_loaded = false;
void COM_InitFilesystem (void);
void COM_Path_f (cmd_source_t src);
void COM_Gamedir_f (cmd_source_t src);
extern qboolean png_available, jpg_available;
/*
All of Quake's data access is through a hierchal file system, but the contents
of the file system can be transparently merged from several sources.
The "base directory" is the path to the directory holding the quake.exe and all
game directories. The sys_* files pass this to host_init in quakeparms_t->basedir.
This can be overridden with the "-basedir" command line parm to allow code
debugging in a different directory. The base directory is only used during
filesystem initialization.
The "game directory" is the first tree on the search path and directory that all
generated files (savegames, screenshots, demos, config files) will be saved to.
This can be overridden with the "-game" command line parameter.
The game directory can never be changed while quake is executing.
This is a precacution against having a malicious server instruct clients to
write files over areas they shouldn't.
The "cache directory" is only used during development to save network bandwidth,
especially over ISDN / T1 lines. If there is a cache directory specified, when
a file is found by the normal search path, it will be mirrored into the cache
directory, then opened there.
FIXME/TODO:
The file "parms.txt" will be read out of the game directory and appended to the
current command line arguments to allow different games to initialize startup
parms differently. This could be used to add a "-sspeed 22050" for the high
quality sound edition. Because they are added at the end, they will not override
an explicit setting on the original command line.
*/
/*
============
COM_SkipPath
============
*/
char *COM_SkipPath (const char *pathname)
{
const char *last;
last = pathname;
while (*pathname)
{
if ((*pathname == '/') || (*pathname == '\\')) // 2010-03-26: added backslash
last = pathname+1;
pathname++;
}
return (char *) last;
}
/*
============
COM_StripExtension
============
*/
int COM_StripExtension (const char *in, char *out, int bufsize)
{
const char *dot;
int len;
/*if (!(dot = strrchr(in, '.')))
{
Q_strcpy (out, in, bufsize);
return;
}*/
if (bufsize <= 0)
return 0;
len = strlen (in);
if (len > 0)
{
dot = in + len - 1;
while (*dot != '.')
{
if (--dot <= in+len-6)
{
//Q_strncpyz (out, in, len + 1);
return Q_strcpy (out, in, bufsize);
}
}
len = 0;
while (*in && in != dot && --bufsize)
{
*out++ = *in++;
len++;
}
}
*out = 0;
return len;
}
/*
============
COM_FileExtension
============
*/
char *COM_FileExtension (const char *in)
{
// static char exten[8];
// int i;
if (!(in = strrchr(in, '.')))
return "";
/* in++;
for (i=0 ; i<7 && *in ; i++, in++)
exten[i] = *in;
exten[i] = 0;
return exten;
*/
return (char *)in + 1;
}
/*
============
COM_FileBase
============
*/
void COM_FileBase (const char *in, char *out, int bufsize)
{
const char *s, *s2;
s = in + strlen(in) - 1;
while (s != in && *s != '.')
s--;
for (s2 = s ; *s2 && *s2 != '/' ; s2--)
;
if (s-s2 < 2)
Q_strcpy (out, "?model?", bufsize);
else
{
s--;
Q_strncpy (out, bufsize, s2+1, s-s2);
//out[s-s2] = 0;
}
}
/*
==================
COM_ForceExtension
If path doesn't have an extension or has a different extension, append specified extension
Extension should include the '.'
==================
*/
void COM_ForceExtension (char *path, const char *extension, int bufsize)
{
int len;
char *src;
len = strlen (path);
src = path + len - 1;
while (*src != '/' && src != path)
{
if (*src-- == '.')
{
if (strcmp(src+1, extension))
{
Q_strcpy (src+1, extension, bufsize-(src+1-path));
}
return;
}
}
Q_strcpy (path + len, extension, bufsize-len);
}
/*
==================
COM_DefaultExtension
If path doesn't have an extension, append extension
Extension should include the '.'
==================
*/
void COM_DefaultExtension (char *path, const char *extension, int bufsize)
{
int len;
char *src;
len = strlen (path);
src = path + len - 1;
while (*src != '/' && *src != '\\' && src != path)
if (*src-- == '.')
return; // it has an extension
Q_strcpy (path+len, extension, bufsize-len);
}
/*
==================
COM_AddExtension
If path doesn't have the specified extension, append it
Extension should include the '.'
==================
*/
void COM_AddExtension (char *path, const char *extension, int bufsize)
{
int len;
char *src;
len = strlen (path);
src = path + len - 1;
while (*src != '/' && *src != '\\' && src != path)
{
if (*src == '.')
{
if (!Q_strcasecmp(src, extension))
return; // it has the correct extension
break;
}
src--;
}
Q_strcpy (path+len, extension, bufsize-len);
}
/*
==================
COM_ExpandPath (JDH)
Takes a filename or partial/relative path and returns a fully resolved path.
Uses com_gamedir as base for relative paths.
==================
*/
#define IS_LETTER(c) ((((c) >= 'A') && ((c) <= 'Z')) || (((c) >= 'a') && ((c) <= 'z')))
qboolean COM_ExpandPath (const char *fname, char *buf, int bufsize)
{
int len;
char *barename, *p/*, path[MAX_QPATH]*/;
// qboolean fullpath;
len = Q_strcpy (buf, com_gamedir, bufsize);
barename = COM_SkipPath (fname);
if (barename == fname)
{
Q_snprintfz (buf+len, bufsize-len, "/%s", fname);
return true;
}
if (*fname == '.')
{
do
{
fname++;
if (*fname == '.')
{
p = COM_SkipPath (buf);
if (p == buf)
{
*buf = 0;
return false;
}
p[-1] = 0;
len = p-buf-1;
fname++;
}
if (*fname != '/' && *fname != '\\')
{
*buf = 0;
return false;
}
fname++;
}
while (*fname == '.');
Q_snprintfz (buf+len, bufsize-len, "/%s", fname);
return true;
}
#ifdef _WIN32
if ((fname[0] == '/') || (fname[0] == '\\') || ((fname[1] == ':') && IS_LETTER(fname[0])))
#else
if (fname[0] == '/')
#endif
{
Q_strcpy (buf, fname, bufsize);
}
else
{
Q_snprintfz (buf+len, bufsize-len, "/%s", fname);
}
return true;
/* Q_strncpy (path, sizeof(path), fname, barename-fname-1);
if (Sys_FolderExists (path))
{
Q_strcpy (buf, fname, bufsize);
return true;
}
return false;
*/
}
/*
==================
COM_IsRealBSP
==================
*/
static const char *bsp_models[] =
{
"batt0", "batt1", "bh10", "bh100", "bh25", "explob", "nail0",
"nail1", "rock0", "rock1", "shell0", "shell1", "exbox2", NULL
};
qboolean COM_IsRealBSP (const char *bspname)
{
int len, i;
if (!COM_FilenamesEqualn (bspname, "b_", 2))
return true;
len = strlen(bspname);
if ((len < 5) || !COM_FilenamesEqual (bspname + len - 4, ".bsp"))
return true;
bspname += 2;
len -= 6;
for (i = 0; bsp_models[i]; i++)
{
if (COM_FilenamesEqualn(bspname, bsp_models[i], len))
return false;
}
return true;
}
/*
================
COM_FreeSearchpaths
================
*/
void COM_FreeSearchpaths (searchpath_t *lastsp)
{
searchpath_t *next;
pack_t *pak;
int curr_level = 99999;
while (com_searchpaths != lastsp)
{
if ((pak = com_searchpaths->pack))
{
if (pak->handle)
fclose (pak->handle);
else if (pak->dzhandle)
Dzip_Close (pak->dzhandle);
free (pak->files);
free (pak);
}
// each time we hit a new directory, check if it's a known mission pack
if (curr_level != com_searchpaths->dir_level)
{
curr_level = com_searchpaths->dir_level;
// if current gamedir is one of the recognized mission packs,
// and its switch isn't on the command-line (or if searchpaths
// are being reset to base), set its var to false
if (COM_FilenamesEqual(com_searchpaths->dir_name, "rogue"))
{
if ((lastsp == com_base_searchpaths) || !COM_CheckParm("-rogue"))
rogue = false;
}
else if (COM_FilenamesEqual(com_searchpaths->dir_name, "hipnotic"))
{
if ((lastsp == com_base_searchpaths) || !COM_CheckParm("-hipnotic"))
hipnotic = false;
}
else if (COM_FilenamesEqual(com_searchpaths->dir_name, "quoth"))
{
if ((lastsp == com_base_searchpaths) || (!COM_CheckParm("-quoth") && !COM_CheckParm("-hipnotic")))
hipnotic = false;
}
else if (COM_FilenamesEqual(com_searchpaths->dir_name, "nehahra"))
{
if (nehahra && ((lastsp == com_base_searchpaths) || !COM_CheckParm("-nehahra")))
{
Neh_UninitEnv ();
nehahra = false;
}
}
#ifdef HEXEN2_SUPPORT
else if (COM_FilenamesEqual(com_searchpaths->dir_name, "hexen2"))
{
if (hexen2 && ((lastsp == com_base_searchpaths) || !COM_CheckParm("-hexen2")))
{
Hexen2_UninitEnv ();
hexen2 = false;
}
}
#endif
}
next = com_searchpaths->next;
if (com_game_searchpaths == com_searchpaths)
com_game_searchpaths = next;
free (com_searchpaths);
com_searchpaths = next;
}
}
/*
================
COM_FindSearchpath (JDH)
================
*/
qboolean COM_FindSearchpath (const char *dir)
{
searchpath_t *search;
for (search = com_searchpaths ; search ; search = search->next)
{
if (COM_FilenamesEqual (search->dir_name, dir))
return true;
}
return false;
}
/*
================
COM_DzipIsMounted (JDH)
================
*/
qboolean COM_DzipIsMounted (const char *qpath, const char *dzname)
{
searchpath_t *search;
for (search = com_searchpaths ; search ; search = search->next)
{
if (search->pack && search->pack->dzhandle)
{
if (COM_FilenamesEqual (search->dir_name, qpath) &&
COM_FilenamesEqual (COM_SkipPath(search->pack->filename), dzname))
return true;
}
}
return false;
}
/*
=============
COM_LoadLibrary (JDH)
=============
*/
void * COM_LoadLibrary (const char *name)
{
void *lib_handle;
if ((lib_handle = Sys_LoadLibrary (va("./%s", name))))
return lib_handle;
if ((lib_handle = Sys_LoadLibrary (va("%s/%s", com_basedir, name))))
return lib_handle;
return Sys_LoadLibrary (name);
}
/*
=============
COM_UnloadLibrary (JDH)
=============
*/
void COM_UnloadLibrary (void *lib_handle)
{
Sys_UnloadLibrary (lib_handle);
}
/*
================
COM_DeleteFile (JDH)
================
*/
qboolean COM_DeleteFile (const char *path)
{
return (remove(path) == 0);
}
#ifdef HEXEN2_SUPPORT
/*
================
COM_GetTempPath (JDH)
- copies the temporary directory path to buf (includes trailing /)
- returns the length of the path string
================
*/
int COM_GetTempPath (char *buf, unsigned int buflen)
{
#ifdef _WIN32
return GetTempPathA (buflen, buf);
#else
int len;
/*** DOES THIS WORK??? ***/
len = Q_strcpy (buf, P_tmpdir, buflen);
if (len && buf[len-1] != '/')
buf[len++] = '/';
return len;
#endif
}
#endif
/*
================
COM_FileLength
================
*/
int COM_FileLength (FILE *f)
{
int pos, end;
pos = ftell (f);
fseek (f, 0, SEEK_END);
end = ftell (f);
fseek (f, pos, SEEK_SET);
return end;
}
/*
=================
COM_FileOpenRead
Use this instead of Sys_FileOpenRead
=================
*/
int COM_FileOpenRead (const char *path, FILE **hndl)
{
FILE *f;
if (!(f = fopen(path, "rb")))
{
*hndl = NULL;
return -1;
}
*hndl = f;
return COM_FileLength (f);
}
/*
=============
COM_FileExists
=============
*/
qboolean COM_FileExists (const char *path)
{
FILE *f;
qboolean retval;
/*#ifndef GLQUAKE
int t;
t = VID_ForceUnlockedAndReturnState ();
#endif*/
if ((f = fopen(path, "rb")))
{
fclose (f);
retval = true;
}
else
{
retval = false;
}
/*#ifndef GLQUAKE
VID_ForceLockState (t);
#endif*/
return retval;
}
/*
=============
COM_FilenamesEqual
=============
*/
qboolean COM_FilenamesEqualn (const char *f1, const char *f2, int len)
{
if (len < 0)
{
if (com_matchfilecase.value)
return !strcmp (f1, f2);
return !Q_strcasecmp (f1, f2);
}
if (com_matchfilecase.value)
return !strncmp (f1, f2, len);
return !Q_strncasecmp (f1, f2, len);
}
/*
qboolean COM_PakFileMatches (char *pakfile, char *testfile, int flags)
{
return COM_FilenameMatches (pakfile, testfile, -1, flags);
}
*/
/*
=================
COM_PakFileMatches
helper function for COM_FindFile
- checks whether the given filename from a pak matches the test filename
- if flags contains FILE_ANY_IMG, FILE_ANY_DEMO, FILE_ANY_MDL, or FILE_ANY_MUS,
pakfile is compared to testfile with each known extension.
=================
*/
qboolean COM_PakFileMatches (const char *pakfile, const char *testfile, int flags)
{
int i, len, num_exts;
char basename[MAX_OSPATH];
const char **ext_list;
if (!(flags & (FILE_ANY_IMG | FILE_ANY_DEMO | FILE_ANY_MDL | FILE_ANY_MUS)))
return COM_FilenamesEqual (pakfile, testfile);
if (flags & FILE_ANY_IMG)
{
ext_list = com_img_exts;
num_exts = num_img_exts;
}
else if (flags & FILE_ANY_DEMO)
{
// if dzlib is not loaded, include files with .dz extension
ext_list = com_demo_exts;
num_exts = (dzlib_loaded ? NUM_DEMO_EXTS-1 : NUM_DEMO_EXTS);
}
else if (flags & FILE_ANY_MDL)
{
ext_list = com_mdl_exts;
num_exts = NUM_MODEL_EXTS;
}
else
{
ext_list = com_music_exts;
num_exts = 2;
}
COM_StripExtension (testfile, basename, sizeof(basename));
len = strlen (basename);
if (!COM_FilenamesEqualn (pakfile, basename, len))
return false;
for (i = 0; i < num_exts; i++)
{
// Q_strcpy (basename+len, ext_list[i], sizeof(basename)-len);
if (COM_FilenamesEqual (pakfile+len, ext_list[i]))
{
// Q_snprintfz (testfile, MAX_QPATH, "%s%s", basename, ext_list[i]); removed 2010/03/03
return true;
}
}
return false;
}
// callback for COM_FindDirFiles in COM_FindInDir
// - does nothing but copy the filename & halt searching after first match
int COM_DummyCallback (com_fileinfo_t *fileinfo, int count, unsigned param)
{
Q_strcpy ((char *) param, fileinfo->name, MAX_QPATH); // sizeof(foundfile) in COM_FindInDir
return 1; // stop searching
}
qboolean COM_FindInDir (searchpath_t *search, char *filename, int flags) /*** modifies filename arg ***/
{
char fullpath[MAX_OSPATH], foundfile[MAX_QPATH], *name;
Q_snprintfz (fullpath, sizeof(fullpath), "%s/%s", search->filename, filename);
if (COM_FindDirFiles (fullpath, search, flags, COM_DummyCallback, (unsigned) foundfile))
{
// if (flags & (FILE_ANY_IMG | FILE_ANY_DEMO | FILE_ANY_MDL | FILE_ANY_MUS))
name = COM_SkipPath (filename);
Q_strcpy (name, foundfile, MAX_QPATH-(name-filename)); // so file case & extension are correct
return true;
}
return false;
}
/*
=============
COM_FindInDir
=============
*/
/*qboolean COM_FindInDir (searchpath_t *search, char *filename, int flags)
{
char netpath[MAX_OSPATH], **ext_list;
int i, len1, len2, num_exts;
// **** FIXME: should check host_matchfilecase ****
if (!(flags & (FILE_ANY_IMG | FILE_ANY_DEMO | FILE_ANY_MDL | FILE_ANY_MUS)))
{
Q_snprintfz (netpath, sizeof(netpath), "%s/%s", search->filename, filename);
return COM_FileExists(netpath);
}
if (flags & FILE_ANY_IMG)
{
ext_list = com_img_exts;
num_exts = num_img_exts;
}
else if (flags & FILE_ANY_DEMO)
{
ext_list = com_demo_exts;
num_exts = (dzlib_loaded ? NUM_DEMO_EXTS-1 : NUM_DEMO_EXTS);
}
else
{
ext_list = com_music_exts;
num_exts = 2;
}
len1 = Q_snprintfz (netpath, sizeof(netpath), "%s/", search->filename);
COM_StripExtension (filename, netpath+len1);
len2 = strlen (netpath);
for (i = 0; i < num_exts; i++)
{
Q_strcpy (netpath+len2, ext_list[i], sizeof(netpath)-len2);
if (COM_FileExists(netpath))
{
strcpy (filename, netpath+len1);
return true;
}
}
return false;
}
*/
int COM_FindNamesInDir (searchpath_t *search, char *path, int pathlen, const char *names[], int flags)
/**** call to COM_FindInDir modifies path arg ****/
{
int i;
for (i=0 ; names[i] ; i++)
{
Q_strcpy (path + pathlen, names[i], MAX_QPATH - pathlen);
if (COM_FindInDir (search, path, flags))
{
return i;
}
}
return -1;
}
/*
==============
COM_FindMultifile - FIXME: merge with COM_FindAllFiles
- non-empty paths must have trailing '/'
- last item in paths and names arrays must be NULL
- number of items may not exceed MAX_PATTERNS per array
- fileinfo may be NULL if file info is not needed
==============
*/
qboolean COM_FindMultifile (const char *paths[], const char *names[], int flags, com_fileinfo_t *info_out)
{
int pathlens[MAX_MULTISOURCE_NAMES], i, j, k;
searchpath_t *search;
pack_t *pak;
char qname[MAX_QPATH];
#if 0
//#if defined(_DEBUG) && defined(_WIN32)
extern HWND mainwindow;
for (i = 0; names[i]; i++)
if (strchr(names[i], '/'))
MessageBoxA (mainwindow, "COM_FindMultifile: Path separator found in filename", "Debug warning", MB_OK);
#endif
// calculate these just once to save time
if (paths && paths[0])
{
for (i=0 ; paths[i] ; i++)
pathlens[i] = strlen (paths[i]);
}
else pathlens[0] = 0;
// search through the path, one element at a time
for (search = com_searchpaths ; search ; search = search->next)
{
if (search->dir_level < (flags & 0x00FF))
break;
if ((pak = search->pack))
{
if (pak->handle && (flags & FILE_NO_PAKS))
continue;
if (pak->dzhandle && (flags & FILE_NO_DZIPS))
continue;
for (i=0 ; i<pak->numfiles ; i++)
{
// make sure the prefix matches a path:
j = 0;
if (paths && paths[0])
{
for ( ; paths[j] ; j++)
{
if (COM_FilenamesEqualn (pak->files[i].name, paths[j], pathlens[j]))
break;
}
if (!paths[j])
continue; // doesn't match any of the paths
}
for (k=0 ; names[k] ; k++)
{
if (COM_PakFileMatches (pak->files[i].name + pathlens[j], names[k], flags))
{
if (info_out)
{
Q_strcpy (info_out->name, pak->files[i].name, MAX_QPATH),
info_out->searchpath = search;
info_out->index = i;
info_out->filepos = pak->files[i].filepos;
info_out->filelen = pak->files[i].filelen;
info_out->isdir = false;
}
return true;
}
}
}
}
else
{
if (paths && paths[0])
{
i = 0; // shut up compiler warning
for (j=0 ; paths[j] ; j++)
{
Q_strcpy (qname, paths[j], MAX_QPATH);
i = COM_FindNamesInDir (search, qname, pathlens[j], names, flags);
if (i >= 0)
break;
}
}
else
{
i = COM_FindNamesInDir (search, qname, 0, names, flags);
}
if (i >= 0)
{
if (info_out)
{
Q_strcpy (info_out->name, qname, MAX_QPATH),
info_out->searchpath = search;
info_out->index = -1;
info_out->filepos = 0;
info_out->filelen = 0; // FIXME
info_out->isdir = false;
}
return true;
}
}
}
return false;
}
/*
=================
COM_FindFile
Searches the folder hierarchy for the given file (searches inside paks as well)
JDH: flags can be used to restrict where the file is looked for
- the low byte specifies the minimum directory level ID
(0 --> any folder; 1 --> all but base "ID1" folder; etc.)