2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // common.c -- misc functions used in client and server
24 #define NUM_SAFE_ARGVS 7
26 static char *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1];
27 static char *argvdummy = " ";
29 static char *safeargvs[NUM_SAFE_ARGVS] =
30 {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse", "-dibonly"};
32 cvar_t registered = {"registered","0"};
33 cvar_t cmdline = {"cmdline","0", false, true};
35 qboolean com_modified; // set true if using non-id files
39 int static_registered = 1; // only for startup check, then set
41 qboolean msg_suppress_1 = 0;
43 void COM_InitFilesystem (void);
45 // if a packfile directory differs from this, it is assumed to be hacked
46 #define PAK0_COUNT 339
47 #define PAK0_CRC 32981
53 #define CMDLINE_LENGTH 256
54 char com_cmdline[CMDLINE_LENGTH];
56 qboolean standard_quake = true, rogue = false, hipnotic = false, nehahra = false;
58 // this graphic needs to be in the pak file to use registered features
59 unsigned short pop[] =
61 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
62 ,0x0000,0x0000,0x6600,0x0000,0x0000,0x0000,0x6600,0x0000
63 ,0x0000,0x0066,0x0000,0x0000,0x0000,0x0000,0x0067,0x0000
64 ,0x0000,0x6665,0x0000,0x0000,0x0000,0x0000,0x0065,0x6600
65 ,0x0063,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6563
66 ,0x0064,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6564
67 ,0x0064,0x6564,0x0000,0x6469,0x6969,0x6400,0x0064,0x6564
68 ,0x0063,0x6568,0x6200,0x0064,0x6864,0x0000,0x6268,0x6563
69 ,0x0000,0x6567,0x6963,0x0064,0x6764,0x0063,0x6967,0x6500
70 ,0x0000,0x6266,0x6769,0x6a68,0x6768,0x6a69,0x6766,0x6200
71 ,0x0000,0x0062,0x6566,0x6666,0x6666,0x6666,0x6562,0x0000
72 ,0x0000,0x0000,0x0062,0x6364,0x6664,0x6362,0x0000,0x0000
73 ,0x0000,0x0000,0x0000,0x0062,0x6662,0x0000,0x0000,0x0000
74 ,0x0000,0x0000,0x0000,0x0061,0x6661,0x0000,0x0000,0x0000
75 ,0x0000,0x0000,0x0000,0x0000,0x6500,0x0000,0x0000,0x0000
76 ,0x0000,0x0000,0x0000,0x0000,0x6400,0x0000,0x0000,0x0000
82 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.
84 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
85 only used during filesystem initialization.
87 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.
89 The "cache directory" is only used during development to save network bandwidth, especially over ISDN / T1 lines. If there is a cache directory
90 specified, when a file is found by the normal search path, it will be mirrored
91 into the cache directory, then opened there.
96 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.
100 //============================================================================
103 // ClearLink is used for new headnodes
104 void ClearLink (link_t *l)
106 l->prev = l->next = l;
109 void RemoveLink (link_t *l)
111 l->next->prev = l->prev;
112 l->prev->next = l->next;
115 void InsertLinkBefore (link_t *l, link_t *before)
118 l->prev = before->prev;
122 void InsertLinkAfter (link_t *l, link_t *after)
124 l->next = after->next;
131 ============================================================================
133 LIBRARY REPLACEMENT FUNCTIONS
135 ============================================================================
139 void Q_memset (void *dest, int fill, int count)
143 if ( (((long)dest | count) & 3) == 0)
146 fill = fill | (fill<<8) | (fill<<16) | (fill<<24);
147 for (i=0 ; i<count ; i++)
148 ((int *)dest)[i] = fill;
151 for (i=0 ; i<count ; i++)
152 ((byte *)dest)[i] = fill;
155 void Q_memcpy (void *dest, void *src, int count)
159 if (( ( (long)dest | (long)src | count) & 3) == 0 )
162 for (i=0 ; i<count ; i++)
163 ((int *)dest)[i] = ((int *)src)[i];
166 for (i=0 ; i<count ; i++)
167 ((byte *)dest)[i] = ((byte *)src)[i];
170 int Q_memcmp (void *m1, void *m2, int count)
175 if (((byte *)m1)[count] != ((byte *)m2)[count])
181 void Q_strcpy (char *dest, char *src)
190 void Q_strncpy (char *dest, char *src, int count)
192 while (*src && count--)
200 int Q_strlen (char *str)
211 char *Q_strrchr(char *s, char c)
213 int len = Q_strlen(s);
216 if (*--s == c) return s;
220 void Q_strcat (char *dest, char *src)
222 dest += Q_strlen(dest);
223 Q_strcpy (dest, src);
226 int Q_strcmp (char *s1, char *s2)
231 return -1; // strings not equal
233 return 0; // strings are equal
241 int Q_strncmp (char *s1, char *s2, int count)
248 return -1; // strings not equal
250 return 0; // strings are equal
258 int Q_strncasecmp (char *s1, char *s2, int n)
268 return 0; // strings are equal until end point
272 if (c1 >= 'a' && c1 <= 'z')
274 if (c2 >= 'a' && c2 <= 'z')
277 return -1; // strings not equal
280 return 0; // strings are equal
288 int Q_strcasecmp (char *s1, char *s2)
290 return Q_strncasecmp (s1, s2, 99999);
293 int Q_atoi (char *str)
312 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
318 if (c >= '0' && c <= '9')
319 val = (val<<4) + c - '0';
320 else if (c >= 'a' && c <= 'f')
321 val = (val<<4) + c - 'a' + 10;
322 else if (c >= 'A' && c <= 'F')
323 val = (val<<4) + c - 'A' + 10;
330 // check for character
334 return sign * str[1];
343 if (c <'0' || c > '9')
345 val = val*10 + c - '0';
352 float Q_atof (char *str)
372 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
378 if (c >= '0' && c <= '9')
379 val = (val*16) + c - '0';
380 else if (c >= 'a' && c <= 'f')
381 val = (val*16) + c - 'a' + 10;
382 else if (c >= 'A' && c <= 'F')
383 val = (val*16) + c - 'A' + 10;
390 // check for character
394 return sign * str[1];
410 if (c <'0' || c > '9')
412 val = val*10 + c - '0';
418 while (total > decimal)
429 ============================================================================
433 ============================================================================
437 short (*BigShort) (short l);
438 short (*LittleShort) (short l);
439 int (*BigLong) (int l);
440 int (*LittleLong) (int l);
441 float (*BigFloat) (float l);
442 float (*LittleFloat) (float l);
445 short ShortSwap (short l)
455 short ShortNoSwap (short l)
469 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
472 int LongNoSwap (int l)
477 float FloatSwap (float f)
487 dat2.b[0] = dat1.b[3];
488 dat2.b[1] = dat1.b[2];
489 dat2.b[2] = dat1.b[1];
490 dat2.b[3] = dat1.b[0];
494 float FloatNoSwap (float f)
500 ==============================================================================
504 Handles byte ordering and avoids alignment errors
505 ==============================================================================
512 void MSG_WriteChar (sizebuf_t *sb, int c)
517 // if (c < -128 || c > 127)
518 // Sys_Error ("MSG_WriteChar: range error");
521 buf = SZ_GetSpace (sb, 1);
525 void MSG_WriteByte (sizebuf_t *sb, int c)
530 // if (c < 0 || c > 255)
531 // Sys_Error ("MSG_WriteByte: range error");
534 buf = SZ_GetSpace (sb, 1);
538 void MSG_WriteShort (sizebuf_t *sb, int c)
543 // if (c < ((short)0x8000) || c > (short)0x7fff)
544 // Sys_Error ("MSG_WriteShort: range error");
547 buf = SZ_GetSpace (sb, 2);
552 void MSG_WriteLong (sizebuf_t *sb, int c)
556 buf = SZ_GetSpace (sb, 4);
558 buf[1] = (c>>8)&0xff;
559 buf[2] = (c>>16)&0xff;
563 void MSG_WriteFloat (sizebuf_t *sb, float f)
573 dat.l = LittleLong (dat.l);
575 SZ_Write (sb, &dat.l, 4);
578 void MSG_WriteString (sizebuf_t *sb, char *s)
581 SZ_Write (sb, "", 1);
583 SZ_Write (sb, s, strlen(s)+1);
586 void MSG_WriteCoord (sizebuf_t *sb, float f)
589 MSG_WriteFloat(sb, f);
591 MSG_WriteShort (sb, (int)(f*8.0f + 0.5f));
594 void MSG_WritePreciseAngle (sizebuf_t *sb, float f)
596 MSG_WriteShort (sb, (int)(f*(65536.0f/360.0f) + 0.5f) & 65535);
599 // LordHavoc: round to nearest value, rather than rounding down, fixes crosshair problem
600 void MSG_WriteAngle (sizebuf_t *sb, float f)
602 MSG_WriteByte (sb, (int)(f*(256.0f/360.0f) + 0.5f) & 255);
609 qboolean msg_badread;
611 void MSG_BeginReading (void)
618 // returns -1 and sets msg_badread if no more characters are available
619 int MSG_ReadChar (void)
623 // LordHavoc: minor optimization
624 if (msg_readcount >= net_message.cursize)
625 // if (msg_readcount+1 > net_message.cursize)
631 c = (signed char)net_message.data[msg_readcount];
637 int MSG_ReadByte (void)
641 // LordHavoc: minor optimization
642 if (msg_readcount >= net_message.cursize)
643 // if (msg_readcount+1 > net_message.cursize)
649 c = (unsigned char)net_message.data[msg_readcount];
656 int MSG_ReadShort (void)
660 if (msg_readcount+2 > net_message.cursize)
666 c = (short)(net_message.data[msg_readcount]
667 + (net_message.data[msg_readcount+1]<<8));
674 int MSG_ReadLong (void)
678 if (msg_readcount+4 > net_message.cursize)
684 c = net_message.data[msg_readcount]
685 + (net_message.data[msg_readcount+1]<<8)
686 + (net_message.data[msg_readcount+2]<<16)
687 + (net_message.data[msg_readcount+3]<<24);
694 float MSG_ReadFloat (void)
703 dat.b[0] = net_message.data[msg_readcount];
704 dat.b[1] = net_message.data[msg_readcount+1];
705 dat.b[2] = net_message.data[msg_readcount+2];
706 dat.b[3] = net_message.data[msg_readcount+3];
709 dat.l = LittleLong (dat.l);
714 char *MSG_ReadString (void)
716 static char string[2048];
723 if (c == -1 || c == 0)
727 } while (l < sizeof(string)-1);
734 float MSG_ReadCoord (void)
737 return MSG_ReadFloat();
739 return MSG_ReadShort() * (1.0f/8.0f);
743 float MSG_ReadCoord (void)
745 return MSG_ReadShort() * (1.0f/8.0f);
748 float MSG_ReadAngle (void)
750 return MSG_ReadChar() * (360.0f/256.0f);
753 float MSG_ReadPreciseAngle (void)
755 return MSG_ReadShort() * (360.0f/65536);
760 //===========================================================================
762 void SZ_Alloc (sizebuf_t *buf, int startsize)
766 buf->data = Hunk_AllocName (startsize, "sizebuf");
767 buf->maxsize = startsize;
772 void SZ_Free (sizebuf_t *buf)
774 // Z_Free (buf->data);
780 void SZ_Clear (sizebuf_t *buf)
785 void *SZ_GetSpace (sizebuf_t *buf, int length)
789 if (buf->cursize + length > buf->maxsize)
791 if (!buf->allowoverflow)
792 Host_Error ("SZ_GetSpace: overflow without allowoverflow set - use -zone on the commandline for more zone memory, default: 128k (quake original default was 48k)");
794 if (length > buf->maxsize)
795 Host_Error ("SZ_GetSpace: %i is > full buffer size", length);
797 buf->overflowed = true;
798 Con_Printf ("SZ_GetSpace: overflow");
802 data = buf->data + buf->cursize;
803 buf->cursize += length;
808 void SZ_Write (sizebuf_t *buf, void *data, int length)
810 memcpy (SZ_GetSpace(buf,length),data,length);
813 void SZ_Print (sizebuf_t *buf, char *data)
817 len = strlen(data)+1;
819 // byte * cast to keep VC++ happy
820 if (buf->data[buf->cursize-1])
821 memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
823 memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
827 //============================================================================
835 char *COM_SkipPath (char *pathname)
854 void COM_StripExtension (char *in, char *out)
856 while (*in && *in != '.')
866 char *COM_FileExtension (char *in)
868 static char exten[8];
871 while (*in && *in != '.')
876 for (i=0 ; i<7 && *in ; i++,in++)
887 void COM_FileBase (char *in, char *out)
891 s = in + strlen(in) - 1;
893 while (s != in && *s != '.')
896 // LordHavoc: EWW bug bug bug bug bug... added previously missing s2 != in (yes, could go hunting through mem for /)
897 for (s2 = s ; s2 != in && *s2 && *s2 != '/' ; s2--)
901 strcpy (out,"?model?");
904 // LordHavoc: FIXME: examine this
906 strncpy (out,s2+1, s-s2);
917 void COM_DefaultExtension (char *path, char *extension)
921 // if path doesn't have a .EXT, append extension
922 // (extension should include the .)
924 src = path + strlen(path) - 1;
926 while (*src != '/' && src != path)
929 return; // it has an extension
933 strcat (path, extension);
941 Parse a token out of a string
944 char *COM_Parse (char *data)
957 while ( (c = *data) <= ' ')
960 return NULL; // end of file;
965 if (c=='/' && data[1] == '/')
967 while (*data && *data != '\n')
973 // handle quoted strings specially
990 // parse single characters
991 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
999 // parse a regular word
1006 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
1019 Returns the position (1 to argc-1) in the program's argument list
1020 where the given parameter apears, or 0 if not present
1023 int COM_CheckParm (char *parm)
1027 for (i=1 ; i<com_argc ; i++)
1030 continue; // NEXTSTEP sometimes clears appkit vars.
1031 if (!strcmp (parm,com_argv[i]))
1042 Looks for the pop.txt file and verifies it.
1043 Sets the "registered" cvar.
1044 Immediately exits out if an alternate game was attempted to be started without
1048 void COM_CheckRegistered (void)
1051 unsigned short check[128];
1054 Cvar_Set ("cmdline", com_cmdline);
1056 COM_OpenFile("gfx/pop.lmp", &h, false);
1057 static_registered = 0;
1062 Con_Printf ("Playing shareware version, with modification.\nwarning: most mods require full quake data.\n");
1064 Con_Printf ("Playing shareware version.\n");
1066 // Sys_Error ("This dedicated server requires a full registered copy of Quake");
1068 // Con_Printf ("Playing shareware version.\n");
1069 // if (com_modified)
1070 // Sys_Error ("You must have the registered version to use modified games");
1074 Sys_FileRead (h, check, sizeof(check));
1077 for (i=0 ; i<128 ; i++)
1078 if (pop[i] != (unsigned short)BigShort (check[i]))
1079 Sys_Error ("Corrupted data file.");
1081 // Cvar_Set ("cmdline", com_cmdline);
1082 Cvar_Set ("registered", "1");
1083 static_registered = 1;
1084 Con_Printf ("Playing registered version.\n");
1088 void COM_Path_f (void);
1096 void COM_InitArgv (int argc, char **argv)
1101 // reconstitute the command line for the cmdline externally visible cvar
1104 for (j=0 ; (j<MAX_NUM_ARGVS) && (j< argc) ; j++)
1108 while ((n < (CMDLINE_LENGTH - 1)) && argv[j][i])
1110 com_cmdline[n++] = argv[j][i++];
1113 if (n < (CMDLINE_LENGTH - 1))
1114 com_cmdline[n++] = ' ';
1123 for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
1126 largv[com_argc] = argv[com_argc];
1127 if (!strcmp ("-safe", argv[com_argc]))
1133 // force all the safe-mode switches. Note that we reserved extra space in
1134 // case we need to add these, so we don't need an overflow check
1135 for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
1137 largv[com_argc] = safeargvs[i];
1142 largv[com_argc] = argvdummy;
1147 standard_quake = false;
1149 if (COM_CheckParm ("-rogue"))
1152 standard_quake = false;
1155 if (COM_CheckParm ("-hipnotic"))
1158 standard_quake = false;
1161 if (COM_CheckParm ("-nehahra"))
1164 standard_quake = false;
1170 unsigned int qmalloctotal_alloc, qmalloctotal_alloccount, qmalloctotal_free, qmalloctotal_freecount;
1172 void *qmalloc(unsigned int size)
1175 qmalloctotal_alloc += size;
1176 qmalloctotal_alloccount++;
1177 mem = malloc(size+sizeof(unsigned int));
1179 return (void *)(mem + 1);
1182 void qfree(void *mem)
1186 m--; // back up to size
1187 qmalloctotal_free += *m; // size
1188 qmalloctotal_freecount++;
1192 void GL_TextureStats_PrintTotal(void);
1193 extern int hunk_low_used, hunk_high_used, hunk_size;
1194 void COM_Memstats_f(void)
1196 Con_Printf("%i malloc calls totalling %i bytes (%.4gMB)\n%i free calls totalling %i bytes (%.4gMB)\n%i bytes (%.4gMB) currently allocated\n", qmalloctotal_alloccount, qmalloctotal_alloc, qmalloctotal_alloc / 1048576.0, qmalloctotal_freecount, qmalloctotal_free, qmalloctotal_free / 1048576.0, qmalloctotal_alloc - qmalloctotal_free, (qmalloctotal_alloc - qmalloctotal_free) / 1048576.0);
1197 GL_TextureStats_PrintTotal();
1198 Con_Printf ("%i bytes (%.4gMB) of %.4gMB hunk in use\n", hunk_low_used + hunk_high_used, (hunk_low_used + hunk_high_used) / 1048576.0, hunk_size / 1048576.0);
1207 void COM_Init (char *basedir)
1210 byte swaptest[2] = {1,0};
1212 // set the byte swapping variables in a portable manner
1213 if ( *(short *)swaptest == 1)
1215 BigShort = ShortSwap;
1216 LittleShort = ShortNoSwap;
1218 LittleLong = LongNoSwap;
1219 BigFloat = FloatSwap;
1220 LittleFloat = FloatNoSwap;
1224 BigShort = ShortNoSwap;
1225 LittleShort = ShortSwap;
1226 BigLong = LongNoSwap;
1227 LittleLong = LongSwap;
1228 BigFloat = FloatNoSwap;
1229 LittleFloat = FloatSwap;
1233 Cvar_RegisterVariable (®istered);
1234 Cvar_RegisterVariable (&cmdline);
1235 Cmd_AddCommand ("path", COM_Path_f);
1236 Cmd_AddCommand ("memstats", COM_Memstats_f);
1238 COM_InitFilesystem ();
1239 COM_CheckRegistered ();
1247 does a varargs printf into a temp buffer, so I don't need to have
1248 varargs versions of all text functions.
1249 FIXME: make this buffer size safe someday
1252 char *va(char *format, ...)
1255 static char string[1024];
1257 va_start (argptr, format);
1258 vsprintf (string, format,argptr);
1265 /// just for debugging
1266 int memsearch (byte *start, int count, int search)
1270 for (i=0 ; i<count ; i++)
1271 if (start[i] == search)
1277 =============================================================================
1281 =============================================================================
1293 char name[MAX_QPATH];
1294 int filepos, filelen;
1297 typedef struct pack_s
1299 char filename[MAX_OSPATH];
1311 int filepos, filelen;
1321 // LordHavoc: was 2048, increased to 16384 and changed info[MAX_PACK_FILES] to a temporary malloc to avoid stack overflows
1322 #define MAX_FILES_IN_PACK 16384
1324 char com_cachedir[MAX_OSPATH];
1325 char com_gamedir[MAX_OSPATH];
1327 typedef struct searchpath_s
1329 char filename[MAX_OSPATH];
1330 pack_t *pack; // only one of filename / pack will be used
1331 struct searchpath_s *next;
1334 searchpath_t *com_searchpaths;
1342 void COM_Path_f (void)
1346 Con_Printf ("Current search path:\n");
1347 for (s=com_searchpaths ; s ; s=s->next)
1351 Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
1354 Con_Printf ("%s\n", s->filename);
1362 The filename will be prefixed by the current game directory
1365 void COM_WriteFile (char *filename, void *data, int len)
1368 char name[MAX_OSPATH];
1370 sprintf (name, "%s/%s", com_gamedir, filename);
1372 handle = Sys_FileOpenWrite (name);
1375 Sys_Printf ("COM_WriteFile: failed on %s\n", name);
1379 Sys_Printf ("COM_WriteFile: %s\n", name);
1380 Sys_FileWrite (handle, data, len);
1381 Sys_FileClose (handle);
1389 Only used for CopyFile
1392 void COM_CreatePath (char *path)
1396 for (ofs = path+1 ; *ofs ; ofs++)
1399 { // create the directory
1412 Copies a file over from the net to the local cache, creating any directories
1413 needed. This is for the convenience of developers using ISDN from home.
1416 void COM_CopyFile (char *netpath, char *cachepath)
1419 int remaining, count;
1422 remaining = Sys_FileOpenRead (netpath, &in);
1423 COM_CreatePath (cachepath); // create directories up to the cache file
1424 out = Sys_FileOpenWrite (cachepath);
1428 if (remaining < sizeof(buf))
1431 count = sizeof(buf);
1432 Sys_FileRead (in, buf, count);
1433 Sys_FileWrite (out, buf, count);
1438 Sys_FileClose (out);
1445 Finds the file in the search path.
1446 Sets com_filesize and one of handle or file
1449 int COM_FindFile (char *filename, int *handle, FILE **file, qboolean quiet)
1451 searchpath_t *search;
1452 char netpath[MAX_OSPATH];
1453 char cachepath[MAX_OSPATH];
1456 int findtime, cachetime;
1459 Sys_Error ("COM_FindFile: both handle and file set");
1460 if (!file && !handle)
1461 Sys_Error ("COM_FindFile: neither handle or file set");
1464 // search through the path, one element at a time
1466 search = com_searchpaths;
1468 { // gross hack to use quake 1 progs with quake 2 maps
1469 if (!strcmp(filename, "progs.dat"))
1470 search = search->next;
1473 for ( ; search ; search = search->next)
1475 // is the element a pak file?
1478 // look through all the pak file elements
1480 for (i=0 ; i<pak->numfiles ; i++)
1481 if (!strcmp (pak->files[i].name, filename))
1484 Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename);
1487 *handle = pak->handle;
1488 Sys_FileSeek (pak->handle, pak->files[i].filepos);
1491 { // open a new file on the pakfile
1492 *file = fopen (pak->filename, "rb");
1494 fseek (*file, pak->files[i].filepos, SEEK_SET);
1496 com_filesize = pak->files[i].filelen;
1497 return com_filesize;
1502 // check a file in the directory tree
1503 // if (!static_registered)
1504 // { // if not a registered version, don't ever go beyond base
1505 // if ( strchr (filename, '/') || strchr (filename,'\\'))
1509 sprintf (netpath, "%s/%s",search->filename, filename);
1511 findtime = Sys_FileTime (netpath);
1515 // see if the file needs to be updated in the cache
1516 if (!com_cachedir[0])
1517 strcpy (cachepath, netpath);
1521 if ((strlen(netpath) < 2) || (netpath[1] != ':'))
1522 sprintf (cachepath,"%s%s", com_cachedir, netpath);
1524 sprintf (cachepath,"%s%s", com_cachedir, netpath+2);
1526 sprintf (cachepath,"%s%s", com_cachedir, netpath);
1529 cachetime = Sys_FileTime (cachepath);
1531 if (cachetime < findtime)
1532 COM_CopyFile (netpath, cachepath);
1533 strcpy (netpath, cachepath);
1537 Sys_Printf ("FindFile: %s\n",netpath);
1538 com_filesize = Sys_FileOpenRead (netpath, &i);
1544 *file = fopen (netpath, "rb");
1546 return com_filesize;
1552 Sys_Printf ("FindFile: can't find %s\n", filename);
1567 filename never has a leading slash, but may contain directory walks
1568 returns a handle and a length
1569 it may actually be inside a pak file
1572 int COM_OpenFile (char *filename, int *handle, qboolean quiet)
1574 return COM_FindFile (filename, handle, NULL, quiet);
1581 If the requested file is inside a packfile, a new FILE * will be opened
1585 int COM_FOpenFile (char *filename, FILE **file, qboolean quiet)
1587 return COM_FindFile (filename, NULL, file, quiet);
1594 If it is a pak file handle, don't really close it
1597 void COM_CloseFile (int h)
1601 for (s = com_searchpaths ; s ; s=s->next)
1602 if (s->pack && s->pack->handle == h)
1613 Filename are reletive to the quake directory.
1614 Allways appends a 0 byte.
1617 cache_user_t *loadcache;
1620 byte *COM_LoadFile (char *path, int usehunk, qboolean quiet)
1627 buf = NULL; // quiet compiler warning
1629 // look for it in the filesystem or pack files
1630 len = COM_OpenFile (path, &h, quiet);
1634 // extract the filename base name for hunk tag
1635 COM_FileBase (path, base);
1640 buf = Hunk_AllocName (len+1, va("%s (file)", path));
1643 // buf = Z_Malloc (len+1);
1646 buf = Cache_Alloc (loadcache, len+1, base);
1649 buf = qmalloc (len+1);
1652 // Sys_Error ("COM_LoadFile: bad usehunk");
1657 Sys_Error ("COM_LoadFile: not enough space for %s", path);
1659 ((byte *)buf)[len] = 0;
1661 Sys_FileRead (h, buf, len);
1667 byte *COM_LoadHunkFile (char *path, qboolean quiet)
1669 return COM_LoadFile (path, 1, quiet);
1672 // LordHavoc: returns malloc'd memory
1673 byte *COM_LoadMallocFile (char *path, qboolean quiet)
1675 return COM_LoadFile (path, 5, quiet);
1678 void COM_LoadCacheFile (char *path, struct cache_user_s *cu, qboolean quiet)
1681 COM_LoadFile (path, 3, quiet);
1688 Takes an explicit (not game tree related) path to a pak file.
1690 Loads the header and directory, adding the files at the beginning
1691 of the list so they override previous pack files.
1694 pack_t *COM_LoadPackFile (char *packfile)
1696 dpackheader_t header;
1698 packfile_t *newfiles;
1702 // LordHavoc: changed from stack array to temporary malloc, allowing huge pack directories
1706 if (Sys_FileOpenRead (packfile, &packhandle) == -1)
1708 // Con_Printf ("Couldn't open %s\n", packfile);
1711 Sys_FileRead (packhandle, (void *)&header, sizeof(header));
1712 if (header.id[0] != 'P' || header.id[1] != 'A'
1713 || header.id[2] != 'C' || header.id[3] != 'K')
1714 Sys_Error ("%s is not a packfile", packfile);
1715 header.dirofs = LittleLong (header.dirofs);
1716 header.dirlen = LittleLong (header.dirlen);
1718 numpackfiles = header.dirlen / sizeof(dpackfile_t);
1720 if (numpackfiles > MAX_FILES_IN_PACK)
1721 Sys_Error ("%s has %i files", packfile, numpackfiles);
1723 if (numpackfiles != PAK0_COUNT)
1724 com_modified = true; // not the original file
1726 newfiles = Hunk_AllocName (numpackfiles * sizeof(packfile_t), "pack file-table");
1728 info = qmalloc(sizeof(*info)*MAX_FILES_IN_PACK);
1729 Sys_FileSeek (packhandle, header.dirofs);
1730 Sys_FileRead (packhandle, (void *)info, header.dirlen);
1732 // crc the directory to check for modifications
1734 // LordHavoc: speedup
1735 CRC_ProcessBytes(&crc, (byte *)info, header.dirlen);
1736 // for (i=0 ; i<header.dirlen ; i++)
1737 // CRC_ProcessByte (&crc, ((byte *)info)[i]);
1738 if (crc != PAK0_CRC)
1739 com_modified = true;
1741 // parse the directory
1742 for (i=0 ; i<numpackfiles ; i++)
1744 strcpy (newfiles[i].name, info[i].name);
1745 newfiles[i].filepos = LittleLong(info[i].filepos);
1746 newfiles[i].filelen = LittleLong(info[i].filelen);
1750 pack = Hunk_AllocName (sizeof (pack_t), packfile);
1751 strcpy (pack->filename, packfile);
1752 pack->handle = packhandle;
1753 pack->numfiles = numpackfiles;
1754 pack->files = newfiles;
1756 Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
1763 COM_AddGameDirectory
1765 Sets com_gamedir, adds the directory to the head of the path,
1766 then loads and adds pak1.pak pak2.pak ...
1769 void COM_AddGameDirectory (char *dir)
1772 searchpath_t *search;
1774 char pakfile[MAX_OSPATH];
1776 strcpy (com_gamedir, dir);
1779 // add the directory to the search path
1781 search = Hunk_AllocName (sizeof(searchpath_t), "pack info");
1782 strcpy (search->filename, dir);
1783 search->next = com_searchpaths;
1784 com_searchpaths = search;
1787 // add any pak files in the format pak0.pak pak1.pak, ...
1791 sprintf (pakfile, "%s/pak%i.pak", dir, i);
1792 pak = COM_LoadPackFile (pakfile);
1795 search = Hunk_AllocName (sizeof(searchpath_t), "pack info");
1797 search->next = com_searchpaths;
1798 com_searchpaths = search;
1802 // add the contents of the parms.txt file to the end of the command line
1812 void COM_InitFilesystem (void)
1815 char basedir[MAX_OSPATH];
1816 searchpath_t *search;
1820 // Overrides the system supplied base directory (under GAMENAME)
1822 i = COM_CheckParm ("-basedir");
1823 if (i && i < com_argc-1)
1824 strcpy (basedir, com_argv[i+1]);
1826 strcpy (basedir, host_parms.basedir);
1828 j = strlen (basedir);
1832 if ((basedir[j-1] == '\\') || (basedir[j-1] == '/'))
1838 // Overrides the system supplied cache directory (NULL or /qcache)
1839 // -cachedir - will disable caching.
1841 i = COM_CheckParm ("-cachedir");
1842 if (i && i < com_argc-1)
1844 if (com_argv[i+1][0] == '-')
1845 com_cachedir[0] = 0;
1847 strcpy (com_cachedir, com_argv[i+1]);
1849 else if (host_parms.cachedir)
1850 strcpy (com_cachedir, host_parms.cachedir);
1852 com_cachedir[0] = 0;
1855 // start up with GAMENAME by default (id1)
1857 COM_AddGameDirectory (va("%s/"GAMENAME, basedir) );
1860 COM_AddGameDirectory (va("%s/nehahra", basedir) );
1862 if (COM_CheckParm ("-rogue"))
1863 COM_AddGameDirectory (va("%s/rogue", basedir) );
1864 if (COM_CheckParm ("-hipnotic"))
1865 COM_AddGameDirectory (va("%s/hipnotic", basedir) );
1866 if (COM_CheckParm ("-nehahra"))
1867 COM_AddGameDirectory (va("%s/nehahra", basedir) );
1872 // Adds basedir/gamedir as an override game
1874 i = COM_CheckParm ("-game");
1875 if (i && i < com_argc-1)
1877 com_modified = true;
1878 COM_AddGameDirectory (va("%s/%s", basedir, com_argv[i+1]));
1882 // -path <dir or packfile> [<dir or packfile>] ...
1883 // Fully specifies the exact search path, overriding the generated one
1885 i = COM_CheckParm ("-path");
1888 com_modified = true;
1889 com_searchpaths = NULL;
1890 while (++i < com_argc)
1892 if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
1895 search = Hunk_AllocName (sizeof(searchpath_t), "pack info");
1896 if ( !strcmp(COM_FileExtension(com_argv[i]), "pak") )
1898 search->pack = COM_LoadPackFile (com_argv[i]);
1900 Sys_Error ("Couldn't load packfile: %s", com_argv[i]);
1903 strcpy (search->filename, com_argv[i]);
1904 search->next = com_searchpaths;
1905 com_searchpaths = search;
1909 if (COM_CheckParm ("-proghack"))
1913 int COM_FileExists(char *filename)
1915 searchpath_t *search;
1916 char netpath[MAX_OSPATH];
1921 for (search = com_searchpaths;search;search = search->next)
1926 for (i = 0;i < pak->numfiles;i++)
1927 if (!strcmp (pak->files[i].name, filename))
1932 sprintf (netpath, "%s/%s",search->filename, filename);
1933 findtime = Sys_FileTime (netpath);