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