-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathfs.c
3229 lines (2686 loc) · 74 KB
/
fs.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.
$Id: fs.c,v 1.60 2007-10-25 14:06:20 dkure Exp $
*/
/**
File System related code
- old Quake FS - declarations in common.h
- Virtual Quake System - vfs.h
- GZIP/ZIP support - fs.h
*/
#include "quakedef.h"
#include "common.h"
#include "hash.h"
#include "fs.h"
#include "vfs.h"
#include "utils.h"
#ifdef _WIN32
#include <errno.h>
#include <shlobj.h>
#else
#include <unistd.h>
#include <strings.h>
#endif
char *com_filesearchpath;
/*
=============================================================================
QUAKE FILESYSTEM
=============================================================================
*/
int fs_filepos;
char fs_netpath[MAX_OSPATH];
// WARNING: if u add some FS related global variable then made appropriate change to FS_ShutDown() too, if required.
char com_gamedirfile[MAX_QPATH]; // qw tf ctf and etc. In other words single dir name without path
char com_gamedir[MAX_OSPATH]; // c:/quake/qw
char com_basedir[MAX_OSPATH]; // c:/quake
char com_homedir[MAX_PATH]; // something really long C:/Documents and Settings/qqshka
char userdirfile[MAX_OSPATH] = {0};
char com_userdir[MAX_OSPATH] = {0};
int userdir_type = -1;
searchpath_t *fs_searchpaths = NULL;
searchpath_t *fs_base_searchpaths = NULL; // without gamedirs
searchpath_t *fs_purepaths;
static int FS_AddPak(char *pathto, char *pakname, searchpath_t *search, searchpathfuncs_t *funcs);
static qbool FS_RemovePak (const char *pakfile);
//============================================================================
#include "q_shared.h"
#ifndef CLIENTONLY
#include "server.h"
#endif // CLIENTONLY
// To include pak3 support add this define
//#define WITH_PK3
hashtable_t *filesystemhash;
qbool filesystemchanged = true;
int fs_hash_dups;
int fs_hash_files;
cvar_t fs_cache = {"fs_cache", "1"};
typedef enum {
FSLFRT_IFFOUND,
FSLFRT_LENGTH,
FSLFRT_DEPTH_OSONLY,
FSLFRT_DEPTH_ANYPATH
} FSLF_ReturnType_e;
void FS_CreatePathRelative(char *pname, int relativeto);
void FS_ForceToPure(char *str, char *crcs, int seed);
int FS_FLocateFile(const char *filename, FSLF_ReturnType_e returntype, flocation_t *loc);
void FS_EnumerateFiles (char *match, int (*func)(char *, int, void *), void *parm);
int FS_FileOpenRead (char *path, FILE **hndl);
void FS_ReloadPackFiles_f(void);
void FS_ListFiles_f(void);
void FS_FlushFSHash(void);
void FS_AddHomeDirectory(char *dir, FS_Load_File_Types loadstuff);
static void FS_AddDataFiles(char *pathto, searchpath_t *search, char *extension, searchpathfuncs_t *funcs);
searchpath_t *FS_AddPathHandle(char *probablepath, searchpathfuncs_t *funcs, void *handle, qbool copyprotect, qbool istemporary, FS_Load_File_Types loadstuff);
qbool Sys_PathProtection(const char *pattern);
void FS_Dir_f (void);
void FS_Locate_f (void);
// VFS-FIXME: Debug file for trying to open files
static void FS_DiffFile_f(void);
//============================================================================
/*
================
FS_FileLength
================
*/
int FS_FileLength (FILE *f)
{
int pos;
int end;
pos = ftell (f);
fseek (f, 0, SEEK_END);
end = ftell (f);
fseek (f, pos, SEEK_SET);
return end;
}
/*
================
FS_FileOpenRead
================
*/
int FS_FileOpenRead (char *path, FILE **hndl)
{
FILE *f;
if (!(f = fopen(path, "rb"))) {
*hndl = NULL;
return -1;
}
*hndl = f;
return FS_FileLength(f);
}
/*
============
FS_Path_f
============
*/
void FS_Path_f (void)
{
searchpath_t *search;
Com_Printf ("Current search path:\n");
if (fs_purepaths)
{
for (search=fs_purepaths ; search ; search=search->nextpure)
{
search->funcs->PrintPath(search->handle);
}
Com_Printf ("----------\n");
}
for (search=fs_searchpaths ; search ; search=search->next)
{
if (search == fs_base_searchpaths)
Com_Printf ("----------\n");
search->funcs->PrintPath(search->handle);
}
}
// VFS-FIXME: D-Kure This removes a sanity check
int FS_FCreateFile (char *filename, FILE **file, char *path, char *mode)
{
char fullpath[MAX_OSPATH];
if (path == NULL)
path = com_gamedir;
if (mode == NULL)
mode = "wb";
// try to create
snprintf(fullpath, sizeof(fullpath), "%s/%s/%s", com_basedir, path, filename);
FS_CreatePath(fullpath);
*file = fopen(fullpath, mode);
if (*file == NULL) {
// no Sys_Error, quake can be run from read-only media like cd-rom
return 0;
}
Sys_Printf ("FCreateFile: %s\n", filename);
return 1;
}
//The filename will be prefixed by com_basedir
qbool FS_WriteFileRelative(char *filename, void *data, int len, int relativeto)
{
vfsfile_t *f;
FS_CreatePathRelative(filename, relativeto);
f = FS_OpenVFS(filename, "wb", relativeto);
if (f)
{
VFS_WRITE(f, data, len);
VFS_CLOSE(f);
} else {
return false;
}
filesystemchanged=true;
return true;
}
/*
============
FS_WriteFile
The filename will be prefixed by the current game directory
============
*/
qbool FS_WriteFile (char *filename, void *data, int len)
{
Sys_Printf ("FS_WriteFile: %s\n", filename);
return FS_WriteFileRelative(filename, data, len, FS_GAME_OS);
}
//The filename used as is
qbool FS_WriteFile_2 (char *filename, void *data, int len)
{
Sys_Printf ("FS_WriteFile_2: %s\n", filename);
return FS_WriteFileRelative(filename, data, len, FS_NONE_OS);
}
#if 0
FILE *FS_WriteFileOpen (char *filename) //like fopen, but based around quake's paths.
{
FILE *f;
char name[MAX_OSPATH];
snprintf (name, sizeof (name), "%s/%s", com_gamedir, filename);
FS_CreatePath(name);
f = fopen (name, "wb");
return f;
}
#endif
//Only used for CopyFile and download
/*
============
FS_CreatePath
Only used for CopyFile and download
============
*/
void FS_CreatePath(char *path)
{
char *s, save;
if (!*path)
return;
for (s = path + 1; *s; s++) {
#ifdef _WIN32
if (*s == '/' || *s == '\\') {
#else
if (*s == '/') {
#endif
save = *s;
*s = 0;
Sys_mkdir(path);
*s = save;
}
}
}
int FS_FOpenPathFile (const char *filename, FILE **file) {
*file = NULL;
fs_filepos = 0;
fs_netpath[0] = 0;
if ((*file = fopen (filename, "rb"))) {
if (developer.value)
Sys_Printf ("FindFile: %s\n", fs_netpath);
return FS_FileLength (*file);
}
if (developer.value)
Sys_Printf ("FindFile: can't find %s\n", filename);
return -1;
}
//Finds the file in the search path.
//Sets fs_netpath and one of handle or file
//Sets fs_filepos to 0 for non paks, and to beging of file in pak file
qbool file_from_pak; // global indicating file came from a packfile
qbool file_from_gamedir; // global indicating file came from a gamedir (and gamedir wasn't id1/qw)
// VFS-FIXME: D-Kure: This function will be removed once we have the VFS layer
// Filename are relative to the quake directory.
// Always appends a 0 byte to the loaded data.
static byte *FS_LoadFile (const char *path, int usehunk, int *file_length)
{
vfsfile_t *f = NULL;
vfserrno_t err;
flocation_t loc;
byte *buf;
char base[32];
int len;
// Look for it in the filesystem or pack files.
//blanket-bans - Avoid combination of / & \ for directories
if (Sys_PathProtection(path))
return NULL;
// VFS-FIXME: This only checks the pak files, not the base dir's
FS_FLocateFile(path, FSLFRT_LENGTH, &loc);
if (loc.search) {
f = loc.search->funcs->OpenVFS(loc.search->handle, &loc, "rb");
} else {
f = FS_OpenVFS(path, "rb", FS_ANY);
}
if (!f)
return NULL;
len = VFS_GETLEN(f);
if (file_length)
*file_length = len;
// Extract the filename base name for hunk tag.
COM_FileBase (path, base);
// TODO: Make these into defines.
if (usehunk == 1)
{
buf = (byte *) Hunk_AllocName (len + 1, base);
}
else if (usehunk == 2)
{
buf = (byte *) Hunk_TempAlloc (len + 1);
}
else if (usehunk == 5)
{
buf = Q_malloc (len + 1);
}
else
{
Sys_Error ("FS_LoadFile: bad usehunk\n");
return NULL;
}
if (!buf) {
Sys_Error ("FS_LoadFile: not enough space for %s\n", path);
return NULL;
}
((byte *)buf)[len] = 0;
Draw_BeginDisc ();
VFS_READ(f, buf, len, &err);
VFS_CLOSE(f);
Draw_EndDisc ();
return buf;
}
byte *FS_LoadHunkFile (char *path, int *len) {
return FS_LoadFile (path, 1, len);
}
byte *FS_LoadTempFile (char *path, int *len) {
return FS_LoadFile (path, 2, len);
}
// use Q_malloc, do not forget Q_free when no needed more
byte *FS_LoadHeapFile (const char *path, int *len)
{
return FS_LoadFile (path, 5, len);
}
// QW262 -->
/*
================
FS_SetUserDirectory
================
*/
void FS_SetUserDirectory (char *dir, char *type) {
char tmp[sizeof(com_gamedirfile)];
if (strstr(dir, "..") || strstr(dir, "/")
|| strstr(dir, "\\") || strstr(dir, ":") ) {
Com_Printf ("Userdir should be a single filename, not a path\n");
return;
}
strlcpy(userdirfile, dir, sizeof(userdirfile));
userdir_type = Q_atoi(type);
strlcpy(tmp, com_gamedirfile, sizeof(tmp)); // save
com_gamedirfile[0]='\0'; // force reread
FS_SetGamedir(tmp); // restore
}
// ==========
// FS_AddPak
// ==========
// Return Value:
// 0 - Pak added succesfully
// 1 - File does not exsist
// -1 - Error loading file
static int FS_AddPak(char *pathto, char *pakname, searchpath_t *search, searchpathfuncs_t *funcs)
{
vfsfile_t *vfs;
flocation_t loc;
char pakfile[MAX_OSPATH];
void *handle;
char *ext;
ext = COM_FileExtension(pakname);
if (!funcs) {
if (strcasecmp(ext, "pak") == 0)
funcs = &packfilefuncs;
#ifdef WITH_ZIP
else if (strcasecmp(ext, "pk3") == 0)
funcs = &zipfilefuncs;
else if (strcasecmp(ext, "pk4") == 0)
funcs = &zipfilefuncs;
#endif // WITH_ZIP
#ifdef DOOMWADS
else if (strcasecmp(ext, "wad") == 0)
funcs = &doomwadfilefuncs;
#endif
else
{
Com_Printf("Unknown pak file type");
return -1;
}
}
/* Check the pak exists */
if (!search->funcs->FindFile(search->handle, &loc, pakname, NULL))
return 1; //not found..
/* Load the pak file */
snprintf (pakfile, sizeof(pakfile), "%s%s", pathto, pakname);
vfs = search->funcs->OpenVFS(search->handle, &loc, "r");
if (!vfs)
return -1;
Com_Printf("Opened %s\n", pakfile);
handle = funcs->OpenNew (vfs, pakfile);
if (!handle) {
VFS_CLOSE(vfs);
return -1;
}
snprintf (pakfile, sizeof(pakfile), "%s%s/", pathto, pakname);
FS_AddPathHandle(pakfile, funcs, handle, true, false, FS_LOAD_FILE_ALL);
return 0;
}
static qbool FS_RemovePak (const char *pakfile) {
searchpath_t *cur = fs_searchpaths;
qbool ret = false;
while (cur) {
cur = cur->next;
}
return ret;
}
// ==============
// FS_AddUserPaks
// ==============
// Reads the pak.lst from the give directory
// and adds the given paks
static void FS_AddUserPaks(char *dir, searchpath_t *parent, FS_Load_File_Types loadstuff)
{
FILE *f;
char pakfile[MAX_OSPATH];
char userpak[MAX_OSPATH];
// add paks listed in paks.lst
snprintf (pakfile, sizeof (pakfile), "%s/pak.lst", dir);
f = fopen(pakfile, "r");
if (f) {
int len;
while (1) {
if (!fgets(userpak, MAX_OSPATH, f))
break;
len = strlen(userpak);
// strip endline
if (userpak[len-1] == '\n') {
userpak[len-1] = '\0';
--len;
}
if (userpak[len-1] == '\r') {
userpak[len-1] = '\0';
--len;
}
if (len < 5)
continue;
if (!strncasecmp(userpak,"soft",4))
continue;
FS_AddPak(dir, userpak, parent, NULL);
}
fclose(f);
}
// add userdir.pak
if (UserdirSet) {
snprintf (pakfile, sizeof (pakfile), "%s.pak", userdirfile);
FS_AddPak(dir, pakfile, parent, NULL);
#ifdef WITH_ZIP
snprintf (pakfile, sizeof (pakfile), "%s.pk3", userdirfile);
FS_AddPak(dir, pakfile, parent, NULL);
#endif // WITH_ZIP
}
}
// <-- QW262
void FS_AddUserDirectory ( char *dir ) {
size_t dir_len;
char *malloc_dir;
if ( !UserdirSet )
return;
switch (userdir_type) {
case 0: snprintf (com_userdir, sizeof(com_userdir), "%s/%s", com_gamedir, userdirfile); break;
case 1: snprintf (com_userdir, sizeof(com_userdir), "%s/%s/%s", com_basedir, userdirfile, dir); break;
case 2: snprintf (com_userdir, sizeof(com_userdir), "%s/qw/%s/%s", com_basedir, userdirfile, dir); break;
case 3: snprintf (com_userdir, sizeof(com_userdir), "%s/qw/%s", com_basedir, userdirfile); break;
case 4: snprintf (com_userdir, sizeof(com_userdir), "%s/%s", com_basedir, userdirfile); break;
case 5: {
char* homedir = getenv("HOME");
if (homedir)
snprintf (com_userdir, sizeof(com_userdir), "%s/qw/%s", homedir, userdirfile);
break;
}
default:
return;
}
dir_len = strlen(com_userdir) + 2;
malloc_dir = Q_malloc(sizeof(char)*dir_len);
snprintf(malloc_dir, dir_len, "%s/", com_userdir);
FS_AddPathHandle(com_userdir, &osfilefuncs, malloc_dir, false, false, FS_LOAD_FILE_ALL);
}
void Draw_InitConback(void);
// Sets the gamedir and path to a different directory.
void FS_SetGamedir (char *dir)
{
searchpath_t *next;
if (strstr(dir, "..") || strstr(dir, "/")
|| strstr(dir, "\\") || strstr(dir, ":") )
{
Com_Printf ("Gamedir should be a single filename, not a path\n");
return;
}
if (!strcmp(com_gamedirfile, dir))
return; // Still the same.
strlcpy (com_gamedirfile, dir, sizeof(com_gamedirfile));
// Free up any current game dir info.
FS_FlushFSHash();
// free up any current game dir info
while (fs_searchpaths != fs_base_searchpaths)
{
fs_searchpaths->funcs->ClosePath(fs_searchpaths->handle);
next = fs_searchpaths->next;
Q_free (fs_searchpaths);
fs_searchpaths = next;
}
filesystemchanged=true;
// Flush all data, so it will be forced to reload.
Cache_Flush ();
snprintf(com_gamedir, sizeof(com_gamedir), "%s/%s", com_basedir, dir);
FS_AddGameDirectory(com_gamedir, FS_LOAD_FILE_ALL);
if (*com_homedir) {
char tmp[MAX_OSPATH];
snprintf(&tmp[0], sizeof(tmp), "%s/%s", com_homedir, dir);
FS_AddHomeDirectory(tmp, FS_LOAD_FILE_ALL);
}
// Reload gamedir specific conback as its not flushed
Draw_InitConback();
FS_AddUserDirectory(dir);
}
char *FS_NextPath (char *prevpath)
{
searchpath_t *s;
char *prev;
if (!prevpath)
return com_gamedir;
prev = com_gamedir;
for (s=fs_searchpaths ; s ; s=s->next)
{
if (s->funcs != &osfilefuncs)
continue;
if (prevpath == prev)
return s->handle;
prev = s->handle;
}
return NULL;
}
void FS_ShutDown( void ) {
// free data
while (fs_searchpaths) {
searchpath_t *next;
next = fs_searchpaths->next;
Q_free (fs_searchpaths);
fs_searchpaths = next;
}
// flush all data, so it will be forced to reload
Cache_Flush ();
// reset globals
fs_base_searchpaths = fs_searchpaths = NULL;
com_gamedirfile[0] = 0;
com_gamedir[0] = 0;
com_basedir[0] = 0;
com_homedir[0] = 0;
userdirfile[0] = 0;
com_userdir[0] = 0;
userdir_type = -1;
}
void FS_InitFilesystemEx( qbool guess_cwd ) {
int i;
#ifndef _WIN32
char *ev;
#endif
char tmp_path[MAX_OSPATH];
FS_ShutDown();
if (guess_cwd) { // so, com_basedir directory will be where ezquake*.exe located
char *e;
#if defined(_WIN32)
if(!(i = GetModuleFileName(NULL, com_basedir, sizeof(com_basedir)-1)))
Sys_Error("FS_InitFilesystemEx: GetModuleFileName failed");
com_basedir[i] = 0; // ensure null terminator
#elif defined(__linux__)
if (!Sys_fullpath(com_basedir, "/proc/self/exe", sizeof(com_basedir)))
Sys_Error("FS_InitFilesystemEx: Sys_fullpath failed");
#else
com_basedir[0] = 0; // FIXME: MAC / FreeBSD
#endif
// strip ezquake*.exe, we need only path
for (e = com_basedir+strlen(com_basedir)-1; e >= com_basedir; e--)
if (*e == '/' || *e == '\\')
{
*e = 0;
break;
}
}
else if ((i = COM_CheckParm ("-basedir")) && i < COM_Argc() - 1) {
// -basedir <path>
// Overrides the system supplied base directory (under id1)
strlcpy (com_basedir, COM_Argv(i + 1), sizeof(com_basedir));
}
else { // made com_basedir equa to cwd
//#ifdef __FreeBSD__
// strlcpy(com_basedir, DATADIR, sizeof(com_basedir) - 1);
//#else
Sys_getcwd(com_basedir, sizeof(com_basedir) - 1); // FIXME strlcpy (com_basedir, ".", sizeof(com_basedir)); ?
//#endif
}
for (i = 0; i < (int) strlen(com_basedir); i++)
if (com_basedir[i] == '\\')
com_basedir[i] = '/';
i = strlen(com_basedir) - 1;
if (i >= 0 && com_basedir[i] == '/')
com_basedir[i] = 0;
#ifdef _WIN32
// gets "C:\documents and settings\johnny\my documents" path
if (!SHGetSpecialFolderPath(0, com_homedir, CSIDL_PERSONAL, 0)) {
*com_homedir = 0;
}
// <Cokeman> yea, but it shouldn't be in My Documents
// <Cokeman> it should be in the application data dir
// c:\documents and settings\<user>\application data
//if (!SHGetSpecialFolderPath(0, com_homedir, CSIDL_APPDATA, 0)) {
// *com_homedir = 0;
//}
#else
ev = getenv("HOME");
if (ev)
strlcpy(com_homedir, ev, sizeof(com_homedir));
else
com_homedir[0] = 0;
#endif
if (COM_CheckParm("-nohome"))
com_homedir[0] = 0;
if (com_homedir[0])
{
#ifdef _WIN32
strlcat(com_homedir, "/ezQuake", sizeof(com_homedir));
#else
strlcat(com_homedir, "/.ezquake", sizeof(com_homedir));
#endif
Com_Printf("Using home directory \"%s\"\n", com_homedir);
}
// start up with id1 by default
snprintf(&tmp_path[0], sizeof(tmp_path), "%s/%s", com_basedir, "id1");
FS_AddGameDirectory(tmp_path, FS_LOAD_FILE_ALL);
snprintf(&tmp_path[0], sizeof(tmp_path), "%s/%s", com_basedir, "ezquake");
FS_AddGameDirectory(tmp_path, FS_LOAD_FILE_ALL);
snprintf(&tmp_path[0], sizeof(tmp_path), "%s/%s", com_basedir, "qw");
FS_AddGameDirectory(tmp_path, FS_LOAD_FILE_ALL);
if (*com_homedir) {
FS_AddHomeDirectory(com_homedir, FS_LOAD_FILE_ALL);
}
// -data <datadir>
// Adds datadirs similar to "-game"
//
//Tei: original code from qbism.
i = 1;
while((i = COM_CheckParmOffset ("-data", i))) {
if (i && i < COM_Argc()-1) {
char tmp_path[MAX_OSPATH];
snprintf(&tmp_path[0], sizeof(tmp_path), "%s%s", com_basedir, COM_Argv(i+1)); /* FIXME: No slash?? Intentional?? */
FS_AddGameDirectory(tmp_path, FS_LOAD_FILE_ALL);
}
i++;
}
// any set gamedirs will be freed up to here
fs_base_searchpaths = fs_searchpaths;
if ((i = COM_CheckParm("-userdir")) && i < COM_Argc() - 2)
FS_SetUserDirectory(COM_Argv(i+1), COM_Argv(i+2));
// the user might want to override default game directory
if (!(i = COM_CheckParm ("-game")))
i = COM_CheckParm ("+gamedir");
if (i && i < COM_Argc() - 1)
FS_SetGamedir (COM_Argv(i + 1));
}
void FS_InitFilesystem( void ) {
vfsfile_t *vfs;
FS_InitModuleFS();
FS_InitFilesystemEx( false ); // first attempt, simplified
vfs = FS_OpenVFS("gfx.wad", "rb", FS_ANY);
if (vfs) { // // we found gfx.wad, seems we have proper com_basedir
VFS_CLOSE(vfs);
return;
}
FS_InitFilesystemEx( true ); // second attempt
}
// allow user select differet "style" how/where open/save different media files.
// so user select media_dir is relative to quake base dir or some system HOME dir or may be full path
// NOTE: using static buffer, use with care
char *FS_LegacyDir(char *media_dir)
{
static char dir[MAX_PATH];
// dir empty, return gamedir
if (!media_dir || !media_dir[0]) {
strlcpy(dir, cls.gamedir, sizeof(dir));
return dir;
}
switch (cl_mediaroot.integer) {
case 1: // /home/qqshka/ezquake/<demo_dir>
while(media_dir[0] == '/' || media_dir[0] == '\\')
media_dir++; // skip precending / probably smart
snprintf(dir, sizeof(dir), "%s/%s", com_homedir, media_dir);
return dir;
case 2: // /fullpath
snprintf(dir, sizeof(dir), "%s", media_dir);
return dir;
default: // /basedir/<demo_dir>
while(media_dir[0] == '/' || media_dir[0] == '\\')
media_dir++; // skip precending / probably smart
snprintf(dir, sizeof(dir), "%s/%s", com_basedir, media_dir);
return dir;
}
}
//=============================================================================
// VFS
void VFS_CHECKCALL (struct vfsfile_s *vf, void *fld, char *emsg) {
if (!fld)
Sys_Error("%s", emsg);
}
void VFS_CLOSE (struct vfsfile_s *vf) {
assert(vf);
VFS_CHECKCALL(vf, vf->Close, "VFS_CLOSE");
vf->Close(vf);
}
unsigned long VFS_TELL (struct vfsfile_s *vf) {
assert(vf);
VFS_CHECKCALL(vf, vf->Tell, "VFS_TELL");
return vf->Tell(vf);
}
unsigned long VFS_GETLEN (struct vfsfile_s *vf) {
assert(vf);
VFS_CHECKCALL(vf, vf->GetLen, "VFS_GETLEN");
return vf->GetLen(vf);
}
/**
* VFS_SEEK() reposition a stream
* If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is
* relative to the start of the file, the current position indicator, or
* end-of-file, respectively.
* Return Value
* Upon successful completion, VFS_SEEK(), returns 0.
* Otherwise, -1 is returned
*/
int VFS_SEEK (struct vfsfile_s *vf, unsigned long pos, int whence) {
assert(vf);
VFS_CHECKCALL(vf, vf->Seek, "VFS_SEEK");
return vf->Seek(vf, pos, whence);
}
int VFS_READ (struct vfsfile_s *vf, void *buffer, int bytestoread, vfserrno_t *err) {
assert(vf);
VFS_CHECKCALL(vf, vf->ReadBytes, "VFS_READ");
return vf->ReadBytes(vf, buffer, bytestoread, err);
}
int VFS_WRITE (struct vfsfile_s *vf, const void *buffer, int bytestowrite) {
assert(vf);
VFS_CHECKCALL(vf, vf->WriteBytes, "VFS_WRITE");
return vf->WriteBytes(vf, buffer, bytestowrite);
}
void VFS_FLUSH (struct vfsfile_s *vf) {
assert(vf);
if(vf->Flush)
vf->Flush(vf);
}
// return null terminated string
char *VFS_GETS(struct vfsfile_s *vf, char *buffer, int buflen)
{
char in;
char *out = buffer;
int len = buflen-1;
assert(vf);
VFS_CHECKCALL(vf, vf->ReadBytes, "VFS_GETS");
// if (len == 0)
// return NULL;
// FIXME: I am not sure how to handle this better
if (len <= 0)
Sys_Error("VFS_GETS: len <= 0");
while (len > 0)
{
if (!VFS_READ(vf, &in, 1, NULL))
{
if (len == buflen-1)
return NULL;
*out = '\0';
return buffer;
}
if (in == '\n')
break;
*out++ = in;
len--;
}
*out = '\0';
return buffer;
}
qbool VFS_COPYPROTECTED(struct vfsfile_s *vf) {
assert(vf);
return vf->copyprotected;
}
//
// some general function to open VFS file, except VFSTCP
//
#ifdef WITH_VFS_ARCHIVE_LOADING
/*
* ====================
* FS_ExtensionToSearchFunctions
* ====================
* Given a file extension the search
* functions for operating on that file type
* is returned.
* If the file type is unknown, NULL is returned.
*/
searchpathfuncs_t *FS_FileNameToSearchFunctions(const char *filename) {
char *ext = COM_FileExtension(filename);
if (strcmp(ext, "pak") == 0) {
return &packfilefuncs;
} else if (strcmp(ext, "tar") == 0) {
return &tarfilefuncs;
#ifdef WITH_ZLIB
} else if (strcmp(ext, "zip") == 0 || strcmp(ext, "pk3") == 0) {
return &zipfilefuncs;
} else if (strcmp(ext, "gz") == 0) {
return &gzipfilefuncs;
#endif // WITH_ZLIB
} else {
return NULL;
}
}
/* ===================
* FS_BreakUpArchivePath
* ===================
* Given the following path /opt/zip1.zip/zip2.zip/file
* ext = "zip"
* archive = "/opt/zip1.zip/zip2.zip"
* inside = "file"
*
* Return = true if the files contain a
* file inside an archive otherwise false
*/
int FS_BreakUpArchivePath(const char *filename,