|
|
1.1 root 1:
2: #include "qcommon.h"
3:
4: // define this to dissalow any data but the demo pak file
5: //#define NO_ADDONS
6:
7: // if a packfile directory differs from this, it is assumed to be hacked
8: // Full version
9: #define PAK0_CHECKSUM 0x40e614e0
10: // Demo
11: //#define PAK0_CHECKSUM 0xb2c6d7ea
12: // OEM
13: //#define PAK0_CHECKSUM 0x78e135c
14:
15: /*
16: =============================================================================
17:
18: QUAKE FILESYSTEM
19:
20: =============================================================================
21: */
22:
23:
24: #define BASEDIRNAME "baseq2"
25:
26: //
27: // in memory
28: //
29:
30: typedef struct
31: {
32: char name[MAX_QPATH];
33: int filepos, filelen;
34: } packfile_t;
35:
36: typedef struct pack_s
37: {
38: char filename[MAX_OSPATH];
39: FILE *handle;
40: int numfiles;
41: packfile_t *files;
42: } pack_t;
43:
44: char fs_gamedir[MAX_OSPATH];
45: cvar_t *fs_basedir;
46: cvar_t *fs_cddir;
47: cvar_t *fs_gamedirvar;
48:
49: typedef struct filelink_s
50: {
51: struct filelink_s *next;
52: char *from;
53: int fromlength;
54: char *to;
55: } filelink_t;
56:
57: filelink_t *fs_links;
58:
59: typedef struct searchpath_s
60: {
61: char filename[MAX_OSPATH];
62: pack_t *pack; // only one of filename / pack will be used
63: struct searchpath_s *next;
64: } searchpath_t;
65:
66: searchpath_t *fs_searchpaths;
67: searchpath_t *fs_base_searchpaths; // without gamedirs
68:
69:
70: /*
71:
72: 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.
73:
74: 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
75: only used during filesystem initialization.
76:
77: 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.
78:
79: */
80:
81:
82: /*
83: ================
84: FS_filelength
85: ================
86: */
87: int FS_filelength (FILE *f)
88: {
89: int pos;
90: int end;
91:
92: pos = ftell (f);
93: fseek (f, 0, SEEK_END);
94: end = ftell (f);
95: fseek (f, pos, SEEK_SET);
96:
97: return end;
98: }
99:
100:
101: /*
102: ============
103: FS_CreatePath
104:
105: Creates any directories needed to store the given filename
106: ============
107: */
108: void FS_CreatePath (char *path)
109: {
110: char *ofs;
111:
112: for (ofs = path+1 ; *ofs ; ofs++)
113: {
114: if (*ofs == '/')
115: { // create the directory
116: *ofs = 0;
117: Sys_Mkdir (path);
118: *ofs = '/';
119: }
120: }
121: }
122:
123:
124: /*
125: ==============
126: FS_FCloseFile
127:
128: For some reason, other dll's can't just cal fclose()
129: on files returned by FS_FOpenFile...
130: ==============
131: */
132: void FS_FCloseFile (FILE *f)
133: {
134: fclose (f);
135: }
136:
137: /*
138: ===========
139: FS_FOpenFile
140:
141: Finds the file in the search path.
142: returns filesize and an open FILE *
143: Used for streaming data out of either a pak file or
144: a seperate file.
145: ===========
146: */
147: #ifndef NO_ADDONS
148: int FS_FOpenFile (char *filename, FILE **file)
149: {
150: searchpath_t *search;
151: char netpath[MAX_OSPATH];
152: pack_t *pak;
153: int i;
154: filelink_t *link;
155:
156: // check for links first
157: for (link = fs_links ; link ; link=link->next)
158: {
159: if (!strncmp (filename, link->from, link->fromlength))
160: {
161: Com_sprintf (netpath, sizeof(netpath), "%s%s",link->to, filename+link->fromlength);
162: *file = fopen (netpath, "rb");
163: if (*file)
164: {
165: Com_DPrintf ("link file: %s\n",netpath);
166: return FS_filelength (*file);
167: }
168: return -1;
169: }
170: }
171:
172: //
173: // search through the path, one element at a time
174: //
175: for (search = fs_searchpaths ; search ; search = search->next)
176: {
177: // is the element a pak file?
178: if (search->pack)
179: {
180: // look through all the pak file elements
181: pak = search->pack;
182: for (i=0 ; i<pak->numfiles ; i++)
183: if (!Q_strcasecmp (pak->files[i].name, filename))
184: { // found it!
185: Com_DPrintf ("PackFile: %s : %s\n",pak->filename, filename);
186: // open a new file on the pakfile
187: *file = fopen (pak->filename, "rb");
188: if (!*file)
189: Com_Error (ERR_FATAL, "Couldn't reopen %s", pak->filename);
190: fseek (*file, pak->files[i].filepos, SEEK_SET);
191: return pak->files[i].filelen;
192: }
193: }
194: else
195: {
196: // check a file in the directory tree
197:
198: Com_sprintf (netpath, sizeof(netpath), "%s/%s",search->filename, filename);
199:
200: *file = fopen (netpath, "rb");
201: if (!*file)
202: continue;
203:
204: Com_DPrintf ("FindFile: %s\n",netpath);
205:
206: return FS_filelength (*file);
207: }
208:
209: }
210:
211: Com_DPrintf ("FindFile: can't find %s\n", filename);
212:
213: *file = NULL;
214: return -1;
215: }
216:
217: #else
218:
219: // this is just for demos to prevent add on hacking
220:
221: int FS_FOpenFile (char *filename, FILE **file)
222: {
223: searchpath_t *search;
224: char netpath[MAX_OSPATH];
225: pack_t *pak;
226: int i;
227:
228: // get config from directory, everything else from pak
229: if (!strcmp(filename, "config.cfg") || !strncmp(filename, "players/", 8))
230: {
231: Com_sprintf (netpath, sizeof(netpath), "%s/%s",FS_Gamedir(), filename);
232:
233: *file = fopen (netpath, "rb");
234: if (!*file)
235: return -1;
236:
237: Com_DPrintf ("FindFile: %s\n",netpath);
238:
239: return FS_filelength (*file);
240: }
241:
242: for (search = fs_searchpaths ; search ; search = search->next)
243: if (search->pack)
244: break;
245: if (!search)
246: {
247: *file = NULL;
248: return -1;
249: }
250:
251: pak = search->pack;
252: for (i=0 ; i<pak->numfiles ; i++)
253: if (!Q_strcasecmp (pak->files[i].name, filename))
254: { // found it!
255: Com_DPrintf ("PackFile: %s : %s\n",pak->filename, filename);
256: // open a new file on the pakfile
257: *file = fopen (pak->filename, "rb");
258: if (!*file)
259: Com_Error (ERR_FATAL, "Couldn't reopen %s", pak->filename);
260: fseek (*file, pak->files[i].filepos, SEEK_SET);
261: return pak->files[i].filelen;
262: }
263:
264: Com_DPrintf ("FindFile: can't find %s\n", filename);
265:
266: *file = NULL;
267: return -1;
268: }
269:
270: #endif
271:
272:
273: /*
274: =================
275: FS_ReadFile
276:
277: Properly handles partial reads
278: =================
279: */
280: void CDAudio_Stop(void);
281: #define MAX_READ 0x10000 // read in blocks of 64k
282: void FS_Read (void *buffer, int len, FILE *f)
283: {
284: int block, remaining;
285: int read;
286: byte *buf;
287: int tries;
288:
289: buf = (byte *)buffer;
290:
291: // read in chunks for progress bar
292: remaining = len;
293: tries = 0;
294: while (remaining)
295: {
296: block = remaining;
297: if (block > MAX_READ)
298: block = MAX_READ;
299: read = fread (buf, 1, block, f);
300: if (read == 0)
301: {
302: // we might have been trying to read from a CD
303: if (!tries)
304: {
305: tries = 1;
306: CDAudio_Stop();
307: }
308: else
309: Com_Error (ERR_FATAL, "FS_Read: 0 bytes read");
310: }
311:
312: if (read == -1)
313: Com_Error (ERR_FATAL, "FS_Read: -1 bytes read");
314:
315: // do some progress bar thing here...
316:
317: remaining -= read;
318: buf += read;
319: }
320: }
321:
322: /*
323: ============
324: FS_LoadFile
325:
326: Filename are reletive to the quake search path
327: a null buffer will just return the file length without loading
328: ============
329: */
330: int FS_LoadFile (char *path, void **buffer)
331: {
332: FILE *h;
333: byte *buf;
334: int len;
335:
336: buf = NULL; // quiet compiler warning
337:
338: // look for it in the filesystem or pack files
339: len = FS_FOpenFile (path, &h);
340: if (!h)
341: {
342: if (buffer)
343: *buffer = NULL;
344: return -1;
345: }
346:
347: if (!buffer)
348: {
349: fclose (h);
350: return len;
351: }
352:
353: buf = Z_Malloc(len);
354: *buffer = buf;
355:
356: FS_Read (buf, len, h);
357:
358: fclose (h);
359:
360: return len;
361: }
362:
363:
364: /*
365: =============
366: FS_FreeFile
367: =============
368: */
369: void FS_FreeFile (void *buffer)
370: {
371: Z_Free (buffer);
372: }
373:
374: /*
375: =================
376: FS_LoadPackFile
377:
378: Takes an explicit (not game tree related) path to a pak file.
379:
380: Loads the header and directory, adding the files at the beginning
381: of the list so they override previous pack files.
382: =================
383: */
384: pack_t *FS_LoadPackFile (char *packfile)
385: {
386: dpackheader_t header;
387: int i;
388: packfile_t *newfiles;
389: int numpackfiles;
390: pack_t *pack;
391: FILE *packhandle;
392: dpackfile_t info[MAX_FILES_IN_PACK];
393: unsigned checksum;
394:
395: packhandle = fopen(packfile, "rb");
396: if (!packhandle)
397: return NULL;
398:
399: fread (&header, 1, sizeof(header), packhandle);
400: if (LittleLong(header.ident) != IDPAKHEADER)
401: Com_Error (ERR_FATAL, "%s is not a packfile", packfile);
402: header.dirofs = LittleLong (header.dirofs);
403: header.dirlen = LittleLong (header.dirlen);
404:
405: numpackfiles = header.dirlen / sizeof(dpackfile_t);
406:
407: if (numpackfiles > MAX_FILES_IN_PACK)
408: Com_Error (ERR_FATAL, "%s has %i files", packfile, numpackfiles);
409:
410: newfiles = Z_Malloc (numpackfiles * sizeof(packfile_t));
411:
412: fseek (packhandle, header.dirofs, SEEK_SET);
413: fread (info, 1, header.dirlen, packhandle);
414:
415: // crc the directory to check for modifications
416: checksum = Com_BlockChecksum ((void *)info, header.dirlen);
417:
418: #ifdef NO_ADDONS
419: if (checksum != PAK0_CHECKSUM)
420: return NULL;
421: #endif
422: // parse the directory
423: for (i=0 ; i<numpackfiles ; i++)
424: {
425: strcpy (newfiles[i].name, info[i].name);
426: newfiles[i].filepos = LittleLong(info[i].filepos);
427: newfiles[i].filelen = LittleLong(info[i].filelen);
428: }
429:
430: pack = Z_Malloc (sizeof (pack_t));
431: strcpy (pack->filename, packfile);
432: pack->handle = packhandle;
433: pack->numfiles = numpackfiles;
434: pack->files = newfiles;
435:
436: Com_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
437: return pack;
438: }
439:
440:
441: /*
442: ================
443: FS_AddGameDirectory
444:
445: Sets fs_gamedir, adds the directory to the head of the path,
446: then loads and adds pak1.pak pak2.pak ...
447: ================
448: */
449: void FS_AddGameDirectory (char *dir)
450: {
451: int i;
452: searchpath_t *search;
453: pack_t *pak;
454: char pakfile[MAX_OSPATH];
455:
456: strcpy (fs_gamedir, dir);
457:
458: //
459: // add the directory to the search path
460: //
461: search = Z_Malloc (sizeof(searchpath_t));
462: strcpy (search->filename, dir);
463: search->next = fs_searchpaths;
464: fs_searchpaths = search;
465:
466: //
467: // add any pak files in the format pak0.pak pak1.pak, ...
468: //
469: for (i=0; i<10; i++)
470: {
471: Com_sprintf (pakfile, sizeof(pakfile), "%s/pak%i.pak", dir, i);
472: pak = FS_LoadPackFile (pakfile);
473: if (!pak)
474: continue;
475: search = Z_Malloc (sizeof(searchpath_t));
476: search->pack = pak;
477: search->next = fs_searchpaths;
478: fs_searchpaths = search;
479: }
480:
481:
482: }
483:
484: /*
485: ============
486: FS_Gamedir
487:
488: Called to find where to write a file (demos, savegames, etc)
489: ============
490: */
491: char *FS_Gamedir (void)
492: {
493: return fs_gamedir;
494: }
495:
496: /*
497: =============
498: FS_ExecAutoexec
499: =============
500: */
501: void FS_ExecAutoexec (void)
502: {
503: char *dir;
504: char name [MAX_QPATH];
505:
506: dir = Cvar_VariableString("gamedir");
507: if (*dir)
508: Com_sprintf(name, sizeof(name), "%s/%s/autoexec.cfg", fs_basedir->string, dir);
509: else
510: Com_sprintf(name, sizeof(name), "%s/%s/autoexec.cfg", fs_basedir->string, BASEDIRNAME);
511: if (Sys_FindFirst(name, 0, SFF_SUBDIR | SFF_HIDDEN | SFF_SYSTEM))
512: Cbuf_AddText ("exec autoexec.cfg\n");
513: Sys_FindClose();
514: }
515:
516:
517: /*
518: ================
519: FS_SetGamedir
520:
521: Sets the gamedir and path to a different directory.
522: ================
523: */
524: void FS_SetGamedir (char *dir)
525: {
526: searchpath_t *next;
527:
528: if (strstr(dir, "..") || strstr(dir, "/")
529: || strstr(dir, "\\") || strstr(dir, ":") )
530: {
531: Com_Printf ("Gamedir should be a single filename, not a path\n");
532: return;
533: }
534:
535: //
536: // free up any current game dir info
537: //
538: while (fs_searchpaths != fs_base_searchpaths)
539: {
540: if (fs_searchpaths->pack)
541: {
542: fclose (fs_searchpaths->pack->handle);
543: Z_Free (fs_searchpaths->pack->files);
544: Z_Free (fs_searchpaths->pack);
545: }
546: next = fs_searchpaths->next;
547: Z_Free (fs_searchpaths);
548: fs_searchpaths = next;
549: }
550:
551: //
552: // flush all data, so it will be forced to reload
553: //
554: if (dedicated && !dedicated->value)
555: Cbuf_AddText ("vid_restart\nsnd_restart\n");
556:
557: Com_sprintf (fs_gamedir, sizeof(fs_gamedir), "%s/%s", fs_basedir->string, dir);
558:
559: if (!strcmp(dir,BASEDIRNAME) || (*dir == 0))
560: {
561: Cvar_FullSet ("gamedir", "", CVAR_SERVERINFO|CVAR_NOSET);
562: Cvar_FullSet ("game", "", CVAR_LATCH|CVAR_SERVERINFO);
563: }
564: else
565: {
566: Cvar_FullSet ("gamedir", dir, CVAR_SERVERINFO|CVAR_NOSET);
567: if (fs_cddir->string[0])
568: FS_AddGameDirectory (va("%s/%s", fs_cddir->string, dir) );
569: FS_AddGameDirectory (va("%s/%s", fs_basedir->string, dir) );
570: }
571: }
572:
573:
574: /*
575: ================
576: FS_Link_f
577:
578: Creates a filelink_t
579: ================
580: */
581: void FS_Link_f (void)
582: {
583: filelink_t *l, **prev;
584:
585: if (Cmd_Argc() != 3)
586: {
587: Com_Printf ("USAGE: link <from> <to>\n");
588: return;
589: }
590:
591: // see if the link already exists
592: prev = &fs_links;
593: for (l=fs_links ; l ; l=l->next)
594: {
595: if (!strcmp (l->from, Cmd_Argv(1)))
596: {
597: Z_Free (l->to);
598: if (!strlen(Cmd_Argv(2)))
599: { // delete it
600: *prev = l->next;
601: Z_Free (l->from);
602: Z_Free (l);
603: return;
604: }
605: l->to = CopyString (Cmd_Argv(2));
606: return;
607: }
608: prev = &l->next;
609: }
610:
611: // create a new link
612: l = Z_Malloc(sizeof(*l));
613: l->next = fs_links;
614: fs_links = l;
615: l->from = CopyString(Cmd_Argv(1));
616: l->fromlength = strlen(l->from);
617: l->to = CopyString(Cmd_Argv(2));
618: }
619:
620: /*
621: ** FS_ListFiles
622: */
623: char **FS_ListFiles( char *findname, int *numfiles, unsigned musthave, unsigned canthave )
624: {
625: char *s;
626: int nfiles = 0;
627: char **list = 0;
628:
629: s = Sys_FindFirst( findname, musthave, canthave );
630: while ( s )
631: {
632: if ( s[strlen(s)-1] != '.' )
633: nfiles++;
634: s = Sys_FindNext( musthave, canthave );
635: }
636: Sys_FindClose ();
637:
638: if ( !nfiles )
639: return NULL;
640:
641: nfiles++; // add space for a guard
642: *numfiles = nfiles;
643:
644: list = malloc( sizeof( char * ) * nfiles );
645: memset( list, 0, sizeof( char * ) * nfiles );
646:
647: s = Sys_FindFirst( findname, musthave, canthave );
648: nfiles = 0;
649: while ( s )
650: {
651: if ( s[strlen(s)-1] != '.' )
652: {
653: list[nfiles] = strdup( s );
654: #ifdef _WIN32
655: strlwr( list[nfiles] );
656: #endif
657: nfiles++;
658: }
659: s = Sys_FindNext( musthave, canthave );
660: }
661: Sys_FindClose ();
662:
663: return list;
664: }
665:
666: /*
667: ** FS_Dir_f
668: */
669: void FS_Dir_f( void )
670: {
671: char *path = NULL;
672: char findname[1024];
673: char wildcard[1024] = "*.*";
674: char **dirnames;
675: int ndirs;
676:
677: if ( Cmd_Argc() != 1 )
678: {
679: strcpy( wildcard, Cmd_Argv( 1 ) );
680: }
681:
682: while ( ( path = FS_NextPath( path ) ) != NULL )
683: {
684: char *tmp = findname;
685:
686: Com_sprintf( findname, sizeof(findname), "%s/%s", path, wildcard );
687:
688: while ( *tmp != 0 )
689: {
690: if ( *tmp == '\\' )
691: *tmp = '/';
692: tmp++;
693: }
694: Com_Printf( "Directory of %s\n", findname );
695: Com_Printf( "----\n" );
696:
697: if ( ( dirnames = FS_ListFiles( findname, &ndirs, 0, 0 ) ) != 0 )
698: {
699: int i;
700:
701: for ( i = 0; i < ndirs-1; i++ )
702: {
703: if ( strrchr( dirnames[i], '/' ) )
704: Com_Printf( "%s\n", strrchr( dirnames[i], '/' ) + 1 );
705: else
706: Com_Printf( "%s\n", dirnames[i] );
707:
708: free( dirnames[i] );
709: }
710: free( dirnames );
711: }
712: Com_Printf( "\n" );
713: };
714: }
715:
716: /*
717: ============
718: FS_Path_f
719:
720: ============
721: */
722: void FS_Path_f (void)
723: {
724: searchpath_t *s;
725: filelink_t *l;
726:
727: Com_Printf ("Current search path:\n");
728: for (s=fs_searchpaths ; s ; s=s->next)
729: {
730: if (s == fs_base_searchpaths)
731: Com_Printf ("----------\n");
732: if (s->pack)
733: Com_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
734: else
735: Com_Printf ("%s\n", s->filename);
736: }
737:
738: Com_Printf ("\nLinks:\n");
739: for (l=fs_links ; l ; l=l->next)
740: Com_Printf ("%s : %s\n", l->from, l->to);
741: }
742:
743: /*
744: ================
745: FS_NextPath
746:
747: Allows enumerating all of the directories in the search path
748: ================
749: */
750: char *FS_NextPath (char *prevpath)
751: {
752: searchpath_t *s;
753: char *prev;
754:
755: if (!prevpath)
756: return fs_gamedir;
757:
758: prev = fs_gamedir;
759: for (s=fs_searchpaths ; s ; s=s->next)
760: {
761: if (s->pack)
762: continue;
763: if (prevpath == prev)
764: return s->filename;
765: prev = s->filename;
766: }
767:
768: return NULL;
769: }
770:
771:
772: /*
773: ================
774: FS_InitFilesystem
775: ================
776: */
777: void FS_InitFilesystem (void)
778: {
779: Cmd_AddCommand ("path", FS_Path_f);
780: Cmd_AddCommand ("link", FS_Link_f);
781: Cmd_AddCommand ("dir", FS_Dir_f );
782:
783: //
784: // basedir <path>
785: // allows the game to run from outside the data tree
786: //
787: fs_basedir = Cvar_Get ("basedir", ".", CVAR_NOSET);
788:
789: //
790: // cddir <path>
791: // Logically concatenates the cddir after the basedir for
792: // allows the game to run from outside the data tree
793: //
794: fs_cddir = Cvar_Get ("cddir", "", CVAR_NOSET);
795: if (fs_cddir->string[0])
796: FS_AddGameDirectory (va("%s/"BASEDIRNAME, fs_cddir->string) );
797:
798: //
799: // start up with baseq2 by default
800: //
801: FS_AddGameDirectory (va("%s/"BASEDIRNAME, fs_basedir->string) );
802:
803: // any set gamedirs will be freed up to here
804: fs_base_searchpaths = fs_searchpaths;
805:
806: // check for game override
807: fs_gamedirvar = Cvar_Get ("game", "", CVAR_LATCH|CVAR_SERVERINFO);
808: if (fs_gamedirvar->string[0])
809: FS_SetGamedir (fs_gamedirvar->string);
810: }
811:
812:
813:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.