git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / common.c
blob3aadb84056804b9a2cb47971e57a03b6e1aa316e
1 /*
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
22 #include "quakedef.h"
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
37 qboolean proghack;
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
49 char com_token[1024];
50 int com_argc;
51 char **com_argv;
53 #define CMDLINE_LENGTH 256
54 char com_cmdline[CMDLINE_LENGTH];
56 qboolean standard_quake = true, rogue, hipnotic, kurok;
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.
95 FIXME:
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)
117 l->next = before;
118 l->prev = before->prev;
119 l->prev->next = l;
120 l->next->prev = l;
122 void InsertLinkAfter (link_t *l, link_t *after)
124 l->next = after->next;
125 l->prev = after;
126 l->prev->next = l;
127 l->next->prev = l;
131 ============================================================================
133 LIBRARY REPLACEMENT FUNCTIONS
135 ============================================================================
138 void Q_memset (void *dest, int fill, int count)
140 int i;
142 if ( (((long)dest | count) & 3) == 0)
144 count >>= 2;
145 fill = fill | (fill<<8) | (fill<<16) | (fill<<24);
146 for (i=0 ; i<count ; i++)
147 ((int *)dest)[i] = fill;
149 else
150 for (i=0 ; i<count ; i++)
151 ((byte *)dest)[i] = fill;
154 void Q_memcpy (void *dest, void *src, int count)
156 int i;
158 if (( ( (long)dest | (long)src | count) & 3) == 0 )
160 count>>=2;
161 for (i=0 ; i<count ; i++)
162 ((int *)dest)[i] = ((int *)src)[i];
164 else
165 for (i=0 ; i<count ; i++)
166 ((byte *)dest)[i] = ((byte *)src)[i];
169 int Q_memcmp (void *m1, void *m2, int count)
171 while(count)
173 count--;
174 if (((byte *)m1)[count] != ((byte *)m2)[count])
175 return -1;
177 return 0;
180 void Q_strcpy (char *dest, char *src)
182 while (*src)
184 *dest++ = *src++;
186 *dest++ = 0;
189 void Q_strncpy (char *dest, char *src, int count)
191 while (*src && count--)
193 *dest++ = *src++;
195 if (count)
196 *dest++ = 0;
199 int Q_strlen (char *str)
201 int count;
203 count = 0;
204 while (str[count])
205 count++;
207 return count;
210 char *Q_strrchr(char *s, char c)
212 int len = Q_strlen(s);
213 s += len;
214 while (len--)
215 if (*--s == c) return s;
216 return 0;
219 void Q_strcat (char *dest, char *src)
221 dest += Q_strlen(dest);
222 Q_strcpy (dest, src);
225 int Q_strcmp (char *s1, char *s2)
227 while (1)
229 if (*s1 != *s2)
230 return -1; // strings not equal
231 if (!*s1)
232 return 0; // strings are equal
233 s1++;
234 s2++;
237 return -1;
240 int Q_strncmp (char *s1, char *s2, int count)
242 while (1)
244 if (!count--)
245 return 0;
246 if (*s1 != *s2)
247 return -1; // strings not equal
248 if (!*s1)
249 return 0; // strings are equal
250 s1++;
251 s2++;
254 return -1;
257 int Q_strncasecmp (char *s1, char *s2, int n)
259 int c1, c2;
261 while (1)
263 c1 = *s1++;
264 c2 = *s2++;
266 if (!n--)
267 return 0; // strings are equal until end point
269 if (c1 != c2)
271 if (c1 >= 'a' && c1 <= 'z')
272 c1 -= ('a' - 'A');
273 if (c2 >= 'a' && c2 <= 'z')
274 c2 -= ('a' - 'A');
275 if (c1 != c2)
276 return -1; // strings not equal
278 if (!c1)
279 return 0; // strings are equal
280 // s1++;
281 // s2++;
284 return -1;
287 int Q_strcasecmp (char *s1, char *s2)
289 return Q_strncasecmp (s1, s2, 99999);
292 int Q_atoi (char *str)
294 int val;
295 int sign;
296 int c;
298 if (*str == '-')
300 sign = -1;
301 str++;
303 else
304 sign = 1;
306 val = 0;
309 // check for hex
311 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
313 str += 2;
314 while (1)
316 c = *str++;
317 if (c >= '0' && c <= '9')
318 val = (val<<4) + c - '0';
319 else if (c >= 'a' && c <= 'f')
320 val = (val<<4) + c - 'a' + 10;
321 else if (c >= 'A' && c <= 'F')
322 val = (val<<4) + c - 'A' + 10;
323 else
324 return val*sign;
329 // check for character
331 if (str[0] == '\'')
333 return sign * str[1];
337 // assume decimal
339 while (1)
341 c = *str++;
342 if (c <'0' || c > '9')
343 return val*sign;
344 val = val*10 + c - '0';
347 return 0;
351 float Q_atof (char *str)
353 float val;
354 int sign;
355 int c;
356 int decimal, total;
358 if (*str == '-')
360 sign = -1;
361 str++;
363 else
364 sign = 1;
366 val = 0;
369 // check for hex
371 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
373 str += 2;
374 while (1)
376 c = *str++;
377 if (c >= '0' && c <= '9')
378 val = (val*16) + c - '0';
379 else if (c >= 'a' && c <= 'f')
380 val = (val*16) + c - 'a' + 10;
381 else if (c >= 'A' && c <= 'F')
382 val = (val*16) + c - 'A' + 10;
383 else
384 return val*sign;
389 // check for character
391 if (str[0] == '\'')
393 return sign * str[1];
397 // assume decimal
399 decimal = -1;
400 total = 0;
401 while (1)
403 c = *str++;
404 if (c == '.')
406 decimal = total;
407 continue;
409 if (c <'0' || c > '9')
410 break;
411 val = val*10 + c - '0';
412 total++;
415 if (decimal == -1)
416 return val*sign;
417 while (total > decimal)
419 val /= 10;
420 total--;
423 return val*sign;
427 ============================================================================
429 BYTE ORDER FUNCTIONS
431 ============================================================================
434 qboolean bigendien;
436 short (*BigShort) (short l);
437 short (*LittleShort) (short l);
438 int (*BigLong) (int l);
439 int (*LittleLong) (int l);
440 float (*BigFloat) (float l);
441 float (*LittleFloat) (float l);
443 short ShortSwap (short l)
445 byte b1,b2;
447 b1 = l&255;
448 b2 = (l>>8)&255;
450 return (b1<<8) + b2;
453 short ShortNoSwap (short l)
455 return l;
458 int LongSwap (int l)
460 byte b1,b2,b3,b4;
462 b1 = l&255;
463 b2 = (l>>8)&255;
464 b3 = (l>>16)&255;
465 b4 = (l>>24)&255;
467 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
470 int LongNoSwap (int l)
472 return l;
475 float FloatSwap (float f)
477 union
479 float f;
480 byte b[4];
481 } dat1, dat2;
484 dat1.f = f;
485 dat2.b[0] = dat1.b[3];
486 dat2.b[1] = dat1.b[2];
487 dat2.b[2] = dat1.b[1];
488 dat2.b[3] = dat1.b[0];
489 return dat2.f;
492 float FloatNoSwap (float f)
494 return f;
498 ==============================================================================
500 MESSAGE IO FUNCTIONS
502 Handles byte ordering and avoids alignment errors
503 ==============================================================================
507 // writing functions
510 void MSG_WriteChar (sizebuf_t *sb, int c)
512 byte *buf;
514 #ifdef PARANOID
515 if (c < -128 || c > 127)
516 Sys_Error ("MSG_WriteChar: range error");
517 #endif
519 buf = SZ_GetSpace (sb, 1);
520 buf[0] = c;
523 void MSG_WriteByte (sizebuf_t *sb, int c)
525 byte *buf;
527 #ifdef PARANOID
528 if (c < 0 || c > 255)
529 Sys_Error ("MSG_WriteByte: range error");
530 #endif
532 buf = SZ_GetSpace (sb, 1);
533 buf[0] = c;
536 void MSG_WriteShort (sizebuf_t *sb, int c)
538 byte *buf;
540 #ifdef PARANOID
541 if (c < ((short)0x8000) || c > (short)0x7fff)
542 Sys_Error ("MSG_WriteShort: range error");
543 #endif
545 buf = SZ_GetSpace (sb, 2);
546 buf[0] = c&0xff;
547 buf[1] = c>>8;
550 void MSG_WriteLong (sizebuf_t *sb, int c)
552 byte *buf;
554 buf = SZ_GetSpace (sb, 4);
555 buf[0] = c&0xff;
556 buf[1] = (c>>8)&0xff;
557 buf[2] = (c>>16)&0xff;
558 buf[3] = c>>24;
561 void MSG_WriteFloat (sizebuf_t *sb, float f)
563 union
565 float f;
566 int l;
567 } dat;
570 dat.f = f;
571 dat.l = LittleLong (dat.l);
573 SZ_Write (sb, &dat.l, 4);
576 void MSG_WriteString (sizebuf_t *sb, char *s)
578 if (!s)
579 SZ_Write (sb, "", 1);
580 else
581 SZ_Write (sb, s, Q_strlen(s)+1);
584 void MSG_WriteCoord (sizebuf_t *sb, float f)
586 MSG_WriteShort (sb, (int)(f*8));
589 void MSG_WriteAngle (sizebuf_t *sb, float f)
591 MSG_WriteByte (sb, ((int)f*256/360) & 255);
594 // JPG - precise aim for ProQuake!
595 void MSG_WritePreciseAngle (sizebuf_t *sb, float f)
597 if (f >= 0)
598 MSG_WriteShort (sb, (int)(f*(65536.0/360.0) + 0.5) & 65535);
599 else
600 MSG_WriteShort (sb, (int)(f*(65536.0/360.0) - 0.5) & 65535);
601 // int val = (int)f*65536/360;
602 // MSG_WriteShort (sb, val & 65535);
606 // reading functions
608 int msg_readcount;
609 qboolean msg_badread;
611 void MSG_BeginReading (void)
613 msg_readcount = 0;
614 msg_badread = false;
617 // returns -1 and sets msg_badread if no more characters are available
618 int MSG_ReadChar (void)
620 int c;
622 if (msg_readcount+1 > net_message.cursize)
624 msg_badread = true;
625 return -1;
628 c = (signed char)net_message.data[msg_readcount];
629 msg_readcount++;
631 return c;
634 int MSG_ReadByte (void)
636 int c;
638 if (msg_readcount+1 > net_message.cursize)
640 msg_badread = true;
641 return -1;
644 c = (unsigned char)net_message.data[msg_readcount];
645 msg_readcount++;
647 return c;
650 int MSG_ReadShort (void)
652 int c;
654 if (msg_readcount+2 > net_message.cursize)
656 msg_badread = true;
657 return -1;
660 c = (short)(net_message.data[msg_readcount]
661 + (net_message.data[msg_readcount+1]<<8));
663 msg_readcount += 2;
665 return c;
668 int MSG_ReadLong (void)
670 int c;
672 if (msg_readcount+4 > net_message.cursize)
674 msg_badread = true;
675 return -1;
678 c = net_message.data[msg_readcount]
679 + (net_message.data[msg_readcount+1]<<8)
680 + (net_message.data[msg_readcount+2]<<16)
681 + (net_message.data[msg_readcount+3]<<24);
683 msg_readcount += 4;
685 return c;
688 float MSG_ReadFloat (void)
690 union
692 byte b[4];
693 float f;
694 int l;
695 } dat;
697 dat.b[0] = net_message.data[msg_readcount];
698 dat.b[1] = net_message.data[msg_readcount+1];
699 dat.b[2] = net_message.data[msg_readcount+2];
700 dat.b[3] = net_message.data[msg_readcount+3];
701 msg_readcount += 4;
703 dat.l = LittleLong (dat.l);
705 return dat.f;
708 char *MSG_ReadString (void)
710 static char string[2048];
711 int l,c;
713 l = 0;
716 c = MSG_ReadChar ();
717 if (c == -1 || c == 0)
718 break;
719 string[l] = c;
720 l++;
721 } while (l < sizeof(string)-1);
723 string[l] = 0;
725 return string;
728 float MSG_ReadCoord (void)
730 return MSG_ReadShort() * (1.0/8);
733 float MSG_ReadAngle (void)
735 return MSG_ReadChar() * (360.0/256);
738 // JPG - exact aim for proquake!
739 float MSG_ReadPreciseAngle (void)
741 return (signed short)MSG_ReadShort () * (360.0/65536.0);
742 // int val = MSG_ReadShort();
743 // return val * (360.0/65536);
746 //===========================================================================
748 void SZ_Alloc (sizebuf_t *buf, int startsize)
750 if (startsize < 256)
751 startsize = 256;
752 buf->data = Hunk_AllocName (startsize, "sizebuf");
753 buf->maxsize = startsize;
754 buf->cursize = 0;
758 void SZ_Free (sizebuf_t *buf)
760 // Z_Free (buf->data);
761 // buf->data = NULL;
762 // buf->maxsize = 0;
763 buf->cursize = 0;
766 void SZ_Clear (sizebuf_t *buf)
768 buf->cursize = 0;
771 void *SZ_GetSpace (sizebuf_t *buf, int length)
773 void *data;
775 if (buf->cursize + length > buf->maxsize)
777 if (!buf->allowoverflow)
778 Sys_Error ("SZ_GetSpace: overflow without allowoverflow set");
780 if (length > buf->maxsize)
781 Sys_Error ("SZ_GetSpace: %i is > full buffer size", length);
783 buf->overflowed = true;
784 Con_Printf ("SZ_GetSpace: overflow");
785 SZ_Clear (buf);
788 data = buf->data + buf->cursize;
789 buf->cursize += length;
791 return data;
794 void SZ_Write (sizebuf_t *buf, void *data, int length)
796 Q_memcpy (SZ_GetSpace(buf,length),data,length);
799 void SZ_Print (sizebuf_t *buf, char *data)
801 int len;
803 len = Q_strlen(data)+1;
805 // byte * cast to keep VC++ happy
806 if (buf->data[buf->cursize-1])
807 Q_memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
808 else
809 Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
813 //============================================================================
817 ============
818 COM_SkipPath
819 ============
821 char *COM_SkipPath (char *pathname)
823 char *last;
825 last = pathname;
826 while (*pathname)
828 if (*pathname=='/')
829 last = pathname+1;
830 pathname++;
832 return last;
836 ============
837 COM_StripExtension
838 ============
840 void COM_StripExtension (char *in, char *out)
842 while (*in && *in != '.')
843 *out++ = *in++;
844 *out = 0;
848 ============
849 COM_FileExtension
850 ============
852 char *COM_FileExtension (char *in)
854 static char exten[8];
855 int i;
857 while (*in && *in != '.')
858 in++;
859 if (!*in)
860 return "";
861 in++;
862 for (i=0 ; i<7 && *in ; i++,in++)
863 exten[i] = *in;
864 exten[i] = 0;
865 return exten;
869 ============
870 COM_FileBase
871 ============
873 void COM_FileBase (char *in, char *out)
875 char *s, *s2;
877 s = in + strlen(in) - 1;
879 while (s != in && *s != '.')
880 s--;
882 for (s2 = s ; *s2 && *s2 != '/' ; s2--)
885 if (s-s2 < 2)
886 strcpy (out,"?model?");
887 else
889 s--;
890 strncpy (out,s2+1, s-s2);
891 out[s-s2] = 0;
897 ==================
898 COM_DefaultExtension
899 ==================
901 void COM_DefaultExtension (char *path, char *extension)
903 char *src;
905 // if path doesn't have a .EXT, append extension
906 // (extension should include the .)
908 src = path + strlen(path) - 1;
910 while (*src != '/' && src != path)
912 if (*src == '.')
913 return; // it has an extension
914 src--;
917 strcat (path, extension);
922 ==============
923 COM_Parse
925 Parse a token out of a string
926 ==============
928 char *COM_Parse (char *data)
930 int c;
931 int len;
933 len = 0;
934 com_token[0] = 0;
936 if (!data)
937 return NULL;
939 // skip whitespace
940 skipwhite:
941 while ( (c = *data) <= ' ')
943 if (c == 0)
944 return NULL; // end of file;
945 data++;
948 // skip // comments
949 if (c=='/' && data[1] == '/')
951 while (*data && *data != '\n')
952 data++;
953 goto skipwhite;
957 // handle quoted strings specially
958 if (c == '\"')
960 data++;
961 while (1)
963 c = *data++;
964 if (c=='\"' || !c)
966 com_token[len] = 0;
967 return data;
969 com_token[len] = c;
970 len++;
974 // parse single characters
975 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
977 com_token[len] = c;
978 len++;
979 com_token[len] = 0;
980 return data+1;
983 // parse a regular word
986 com_token[len] = c;
987 data++;
988 len++;
989 c = *data;
990 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
991 break;
992 } while (c>32);
994 com_token[len] = 0;
995 return data;
1000 ================
1001 COM_CheckParm
1003 Returns the position (1 to argc-1) in the program's argument list
1004 where the given parameter apears, or 0 if not present
1005 ================
1007 int COM_CheckParm (char *parm)
1009 int i;
1011 for (i=1 ; i<com_argc ; i++)
1013 if (!com_argv[i])
1014 continue; // NEXTSTEP sometimes clears appkit vars.
1015 if (!Q_strcmp (parm,com_argv[i]))
1016 return i;
1019 return 0;
1023 ================
1024 COM_CheckRegistered
1026 Looks for the pop.txt file and verifies it.
1027 Sets the "registered" cvar.
1028 Immediately exits out if an alternate game was attempted to be started without
1029 being registered.
1030 ================
1032 void COM_CheckRegistered (void)
1035 int h;
1036 unsigned short check[128];
1037 int i;
1039 if (kurok)
1041 Cvar_Set ("cmdline", com_cmdline);
1042 Cvar_Set ("registered", "1");
1043 static_registered = 1;
1044 return;
1047 COM_OpenFile("gfx/pop.lmp", &h);
1048 static_registered = 0;
1050 if (h == -1)
1052 #if WINDED
1053 Sys_Error ("This dedicated server requires a full registered copy of Quake");
1054 #endif
1055 Con_Printf ("Playing shareware version.\n");
1056 if (com_modified)
1057 Sys_Error ("You must have the registered version to use modified games");
1058 return;
1061 Sys_FileRead (h, check, sizeof(check));
1062 COM_CloseFile (h);
1064 for (i=0 ; i<128 ; i++)
1065 if (pop[i] != (unsigned short)BigShort (check[i]))
1066 Sys_Error ("Corrupted data file.");
1068 Cvar_Set ("cmdline", com_cmdline);
1069 Cvar_Set ("registered", "1");
1070 static_registered = 1;
1071 Con_Printf ("Playing registered version.\n");
1075 void COM_Path_f (void);
1079 ================
1080 COM_InitArgv
1081 ================
1083 void COM_InitArgv (int argc, char **argv)
1085 qboolean safe;
1086 int i, j, n;
1088 // reconstitute the command line for the cmdline externally visible cvar
1089 n = 0;
1091 for (j=0 ; (j<MAX_NUM_ARGVS) && (j< argc) ; j++)
1093 i = 0;
1095 while ((n < (CMDLINE_LENGTH - 1)) && argv[j][i])
1097 com_cmdline[n++] = argv[j][i++];
1100 if (n < (CMDLINE_LENGTH - 1))
1101 com_cmdline[n++] = ' ';
1102 else
1103 break;
1106 com_cmdline[n] = 0;
1108 safe = false;
1110 for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
1111 com_argc++)
1113 largv[com_argc] = argv[com_argc];
1114 if (!Q_strcmp ("-safe", argv[com_argc]))
1115 safe = true;
1118 if (safe)
1120 // force all the safe-mode switches. Note that we reserved extra space in
1121 // case we need to add these, so we don't need an overflow check
1122 for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
1124 largv[com_argc] = safeargvs[i];
1125 com_argc++;
1129 largv[com_argc] = argvdummy;
1130 com_argv = largv;
1132 if (COM_CheckParm ("-rogue"))
1134 rogue = true;
1135 standard_quake = false;
1138 if (COM_CheckParm ("-hipnotic"))
1140 hipnotic = true;
1141 standard_quake = false;
1144 if (COM_CheckParm ("-kurok"))
1146 kurok = true;
1147 standard_quake = false;
1154 ================
1155 COM_Init
1156 ================
1158 void COM_Init (char *basedir)
1160 byte swaptest[2] = {1,0};
1162 // set the byte swapping variables in a portable manner
1163 if ( *(short *)swaptest == 1)
1165 bigendien = false;
1166 BigShort = ShortSwap;
1167 LittleShort = ShortNoSwap;
1168 BigLong = LongSwap;
1169 LittleLong = LongNoSwap;
1170 BigFloat = FloatSwap;
1171 LittleFloat = FloatNoSwap;
1173 else
1175 bigendien = true;
1176 BigShort = ShortNoSwap;
1177 LittleShort = ShortSwap;
1178 BigLong = LongNoSwap;
1179 LittleLong = LongSwap;
1180 BigFloat = FloatNoSwap;
1181 LittleFloat = FloatSwap;
1184 Cvar_RegisterVariable (&registered);
1185 Cvar_RegisterVariable (&cmdline);
1186 Cmd_AddCommand ("path", COM_Path_f);
1188 COM_InitFilesystem ();
1189 COM_CheckRegistered ();
1194 ============
1197 does a varargs printf into a temp buffer, so I don't need to have
1198 varargs versions of all text functions.
1199 FIXME: make this buffer size safe someday
1200 ============
1202 char *va(char *format, ...)
1204 va_list argptr;
1205 static char string[1024];
1207 va_start (argptr, format);
1208 vsprintf (string, format,argptr);
1209 va_end (argptr);
1211 return string;
1215 /// just for debugging
1216 int memsearch (byte *start, int count, int search)
1218 int i;
1220 for (i=0 ; i<count ; i++)
1221 if (start[i] == search)
1222 return i;
1223 return -1;
1227 =============================================================================
1229 QUAKE FILESYSTEM
1231 =============================================================================
1234 int com_filesize;
1238 // in memory
1241 typedef struct
1243 char name[MAX_QPATH];
1244 int filepos, filelen;
1245 } packfile_t;
1247 typedef struct pack_s
1249 char filename[MAX_OSPATH];
1250 int handle;
1251 int numfiles;
1252 packfile_t *files;
1253 } pack_t;
1256 // on disk
1258 typedef struct
1260 char name[56];
1261 int filepos, filelen;
1262 } dpackfile_t;
1264 typedef struct
1266 char id[4];
1267 int dirofs;
1268 int dirlen;
1269 } dpackheader_t;
1271 #define MAX_FILES_IN_PACK 2048
1273 char com_cachedir[MAX_OSPATH];
1274 char com_gamedir[MAX_OSPATH];
1276 typedef struct searchpath_s
1278 char filename[MAX_OSPATH];
1279 pack_t *pack; // only one of filename / pack will be used
1280 struct searchpath_s *next;
1281 } searchpath_t;
1283 searchpath_t *com_searchpaths;
1286 ============
1287 COM_Path_f
1289 ============
1291 void COM_Path_f (void)
1293 searchpath_t *s;
1295 Con_Printf ("Current search path:\n");
1296 for (s=com_searchpaths ; s ; s=s->next)
1298 if (s->pack)
1300 Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
1302 else
1303 Con_Printf ("%s\n", s->filename);
1308 ============
1309 COM_WriteFile
1311 The filename will be prefixed by the current game directory
1312 ============
1314 void COM_WriteFile (char *filename, void *data, int len)
1316 int handle;
1317 char name[MAX_OSPATH];
1319 sprintf (name, "%s/%s", com_gamedir, filename);
1321 handle = Sys_FileOpenWrite (name);
1322 if (handle == -1)
1324 Sys_Printf ("COM_WriteFile: failed on %s\n", name);
1325 return;
1328 Sys_Printf ("COM_WriteFile: %s\n", name);
1329 Sys_FileWrite (handle, data, len);
1330 Sys_FileClose (handle);
1335 ============
1336 COM_CreatePath
1338 Only used for CopyFile
1339 ============
1341 void COM_CreatePath (char *path)
1343 char *ofs;
1345 for (ofs = path+1 ; *ofs ; ofs++)
1347 if (*ofs == '/')
1348 { // create the directory
1349 *ofs = 0;
1350 Sys_mkdir (path);
1351 *ofs = '/';
1358 ===========
1359 COM_CopyFile
1361 Copies a file over from the net to the local cache, creating any directories
1362 needed. This is for the convenience of developers using ISDN from home.
1363 ===========
1365 void COM_CopyFile (char *netpath, char *cachepath)
1367 int in, out;
1368 int remaining, count;
1369 char buf[4096];
1371 remaining = Sys_FileOpenRead (netpath, &in);
1372 COM_CreatePath (cachepath); // create directories up to the cache file
1373 out = Sys_FileOpenWrite (cachepath);
1375 while (remaining)
1377 if (remaining < sizeof(buf))
1378 count = remaining;
1379 else
1380 count = sizeof(buf);
1381 Sys_FileRead (in, buf, count);
1382 Sys_FileWrite (out, buf, count);
1383 remaining -= count;
1386 Sys_FileClose (in);
1387 Sys_FileClose (out);
1391 ===========
1392 COM_FindFile
1394 Finds the file in the search path.
1395 Sets com_filesize and one of handle or file
1396 ===========
1398 int COM_FindFile (char *filename, int *handle, int *file)
1400 searchpath_t *search;
1401 char netpath[MAX_OSPATH];
1402 char cachepath[MAX_OSPATH];
1403 pack_t *pak;
1404 int i;
1405 int findtime, cachetime;
1407 if (file && handle)
1408 Sys_Error ("COM_FindFile: both handle and file set");
1409 if (!file && !handle)
1410 Sys_Error ("COM_FindFile: neither handle or file set");
1413 // search through the path, one element at a time
1415 search = com_searchpaths;
1416 if (proghack)
1417 { // gross hack to use quake 1 progs with quake 2 maps
1418 if (!strcmp(filename, "progs.dat"))
1419 search = search->next;
1422 for ( ; search ; search = search->next)
1424 // is the element a pak file?
1425 if (search->pack)
1427 // look through all the pak file elements
1428 pak = search->pack;
1429 for (i=0 ; i<pak->numfiles ; i++)
1430 if (!strcmp (pak->files[i].name, filename))
1431 { // found it!
1432 Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename);
1433 if (handle)
1435 *handle = pak->handle;
1436 Sys_FileSeek (pak->handle, pak->files[i].filepos);
1438 else
1439 { // open a new file on the pakfile
1440 Sys_FileOpenRead(pak->filename, file);
1441 if ((*file) >= 0)
1442 Sys_FileSeek(*file, pak->files[i].filepos);
1444 com_filesize = pak->files[i].filelen;
1445 return com_filesize;
1448 else
1450 // check a file in the directory tree
1451 if (!static_registered)
1452 { // if not a registered version, don't ever go beyond base
1453 if ( strchr (filename, '/') || strchr (filename,'\\'))
1454 continue;
1457 sprintf (netpath, "%s/%s",search->filename, filename);
1459 findtime = Sys_FileTime (netpath);
1460 if (findtime == -1)
1461 continue;
1463 // see if the file needs to be updated in the cache
1464 if (!com_cachedir[0])
1465 strcpy (cachepath, netpath);
1466 else
1468 #if defined(WIN32)
1469 if ((strlen(netpath) < 2) || (netpath[1] != ':'))
1470 sprintf (cachepath,"%s%s", com_cachedir, netpath);
1471 else
1472 sprintf (cachepath,"%s%s", com_cachedir, netpath+2);
1473 #else
1474 sprintf (cachepath,"%s%s", com_cachedir, netpath);
1475 #endif
1477 cachetime = Sys_FileTime (cachepath);
1479 if (cachetime < findtime)
1480 COM_CopyFile (netpath, cachepath);
1481 strcpy (netpath, cachepath);
1484 Sys_Printf ("FindFile: %s\n",netpath);
1485 com_filesize = Sys_FileOpenRead (netpath, &i);
1486 if (handle)
1487 *handle = i;
1488 else
1490 Sys_FileClose (i);
1491 Sys_FileOpenRead(netpath, file);
1493 return com_filesize;
1498 Sys_Printf ("FindFile: can't find %s\n", filename);
1500 if (handle)
1501 *handle = -1;
1502 else
1503 *file = -1;
1504 com_filesize = -1;
1505 return -1;
1510 ===========
1511 COM_OpenFile
1513 filename never has a leading slash, but may contain directory walks
1514 returns a handle and a length
1515 it may actually be inside a pak file
1516 ===========
1518 int COM_OpenFile (char *filename, int *handle)
1520 return COM_FindFile (filename, handle, NULL);
1524 ===========
1525 COM_FOpenFile
1527 If the requested file is inside a packfile, a new file will be opened
1528 into the file.
1529 ===========
1531 int COM_FOpenFile (char *filename, int *file)
1533 return COM_FindFile (filename, NULL, file);
1537 ============
1538 COM_CloseFile
1540 If it is a pak file handle, don't really close it
1541 ============
1543 void COM_CloseFile (int h)
1545 searchpath_t *s;
1547 for (s = com_searchpaths ; s ; s=s->next)
1548 if (s->pack && s->pack->handle == h)
1549 return;
1551 Sys_FileClose (h);
1556 ============
1557 COM_LoadFile
1559 Filename are reletive to the quake directory.
1560 Allways appends a 0 byte.
1561 ============
1563 cache_user_t *loadcache;
1564 byte *loadbuf;
1565 int loadsize;
1566 byte *COM_LoadFile (char *path, int usehunk)
1568 int h;
1569 byte *buf;
1570 char base[32];
1571 int len;
1573 buf = NULL; // quiet compiler warning
1575 // look for it in the filesystem or pack files
1576 len = COM_OpenFile (path, &h);
1577 if (h == -1)
1578 return NULL;
1580 // extract the filename base name for hunk tag
1581 COM_FileBase (path, base);
1583 if (usehunk == 1)
1584 buf = Hunk_AllocName (len+1, base);
1585 else if (usehunk == 2)
1586 buf = Hunk_TempAlloc (len+1);
1587 else if (usehunk == 0)
1588 buf = Z_Malloc (len+1);
1589 else if (usehunk == 3)
1590 buf = Cache_Alloc (loadcache, len+1, base);
1591 else if (usehunk == 4)
1593 if (len+1 > loadsize)
1594 buf = Hunk_TempAlloc (len+1);
1595 else
1596 buf = loadbuf;
1598 else
1599 Sys_Error ("COM_LoadFile: bad usehunk");
1601 if (!buf)
1602 Sys_Error ("COM_LoadFile: not enough space for %s", path);
1604 ((byte *)buf)[len] = 0;
1606 Draw_BeginDisc ();
1607 Sys_FileRead (h, buf, len);
1608 COM_CloseFile (h);
1609 Draw_EndDisc ();
1611 return buf;
1614 byte *COM_LoadHunkFile (char *path)
1616 return COM_LoadFile (path, 1);
1619 byte *COM_LoadTempFile (char *path)
1621 return COM_LoadFile (path, 2);
1624 void COM_LoadCacheFile (char *path, struct cache_user_s *cu)
1626 loadcache = cu;
1627 COM_LoadFile (path, 3);
1630 // uses temp hunk if larger than bufsize
1631 byte *COM_LoadStackFile (char *path, void *buffer, int bufsize)
1633 byte *buf;
1635 loadbuf = (byte *)buffer;
1636 loadsize = bufsize;
1637 buf = COM_LoadFile (path, 4);
1639 return buf;
1643 =================
1644 COM_LoadPackFile
1646 Takes an explicit (not game tree related) path to a pak file.
1648 Loads the header and directory, adding the files at the beginning
1649 of the list so they override previous pack files.
1650 =================
1652 pack_t *COM_LoadPackFile (char *packfile)
1654 dpackheader_t header;
1655 int i;
1656 packfile_t *newfiles;
1657 int numpackfiles;
1658 pack_t *pack;
1659 int packhandle;
1660 dpackfile_t info[MAX_FILES_IN_PACK];
1661 unsigned short crc;
1663 if (Sys_FileOpenRead (packfile, &packhandle) == -1)
1665 // Con_Printf ("Couldn't open %s\n", packfile);
1666 return NULL;
1668 Sys_FileRead (packhandle, (void *)&header, sizeof(header));
1669 if (header.id[0] != 'P' || header.id[1] != 'A'
1670 || header.id[2] != 'C' || header.id[3] != 'K')
1671 Sys_Error ("%s is not a packfile", packfile);
1672 header.dirofs = LittleLong (header.dirofs);
1673 header.dirlen = LittleLong (header.dirlen);
1675 numpackfiles = header.dirlen / sizeof(dpackfile_t);
1677 if (numpackfiles > MAX_FILES_IN_PACK)
1678 Sys_Error ("%s has %i files", packfile, numpackfiles);
1680 if (numpackfiles != PAK0_COUNT)
1681 com_modified = true; // not the original file
1683 newfiles = Hunk_AllocName (numpackfiles * sizeof(packfile_t), "packfile");
1685 Sys_FileSeek (packhandle, header.dirofs);
1686 Sys_FileRead (packhandle, (void *)info, header.dirlen);
1688 // crc the directory to check for modifications
1689 CRC_Init (&crc);
1690 for (i=0 ; i<header.dirlen ; i++)
1691 CRC_ProcessByte (&crc, ((byte *)info)[i]);
1692 if (crc != PAK0_CRC)
1693 com_modified = true;
1695 // parse the directory
1696 for (i=0 ; i<numpackfiles ; i++)
1698 strcpy (newfiles[i].name, info[i].name);
1699 newfiles[i].filepos = LittleLong(info[i].filepos);
1700 newfiles[i].filelen = LittleLong(info[i].filelen);
1703 pack = Hunk_Alloc (sizeof (pack_t));
1704 strcpy (pack->filename, packfile);
1705 pack->handle = packhandle;
1706 pack->numfiles = numpackfiles;
1707 pack->files = newfiles;
1709 Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
1710 return pack;
1715 ================
1716 COM_AddGameDirectory
1718 Sets com_gamedir, adds the directory to the head of the path,
1719 then loads and adds pak1.pak pak2.pak ...
1720 ================
1722 void COM_AddGameDirectory (char *dir)
1724 int i;
1725 searchpath_t *search;
1726 pack_t *pak;
1727 char pakfile[MAX_OSPATH];
1729 strcpy (com_gamedir, dir);
1732 // add the directory to the search path
1734 search = Hunk_Alloc (sizeof(searchpath_t));
1735 strcpy (search->filename, dir);
1736 search->next = com_searchpaths;
1737 com_searchpaths = search;
1740 // add any pak files in the format pak0.pak pak1.pak, ...
1743 // Changed so it will search for the next pak if one is missing which we need to do for
1744 // Kurok to be standalone AND be able to run Quake alongside it.
1746 for (i=0 ; i<99; i++)
1748 sprintf (pakfile, "%s/pak%i.pak", dir, i);
1749 pak = COM_LoadPackFile (pakfile);
1751 // Don't stop if a pak is missing when running Kurok.
1752 // if (!kurok)
1753 if (!pak)
1754 break;
1756 search = Hunk_Alloc (sizeof(searchpath_t));
1757 search->pack = pak;
1758 search->next = com_searchpaths;
1759 com_searchpaths = search;
1763 // add the contents of the parms.txt file to the end of the command line
1769 ================
1770 COM_InitFilesystem
1771 ================
1773 void COM_InitFilesystem (void)
1775 int i, j;
1776 char basedir[MAX_OSPATH];
1777 searchpath_t *search;
1780 // -basedir <path>
1781 // Overrides the system supplied base directory (under GAMENAME)
1783 i = COM_CheckParm ("-basedir");
1784 if (i && i < com_argc-1)
1785 strcpy (basedir, com_argv[i+1]);
1786 else
1787 strcpy (basedir, host_parms.basedir);
1789 j = strlen (basedir);
1791 if (j > 0)
1793 if ((basedir[j-1] == '\\') || (basedir[j-1] == '/'))
1794 basedir[j-1] = 0;
1798 // -cachedir <path>
1799 // Overrides the system supplied cache directory (NULL or /qcache)
1800 // -cachedir - will disable caching.
1802 i = COM_CheckParm ("-cachedir");
1803 if (i && i < com_argc-1)
1805 if (com_argv[i+1][0] == '-')
1806 com_cachedir[0] = 0;
1807 else
1808 strcpy (com_cachedir, com_argv[i+1]);
1810 else if (host_parms.cachedir)
1811 strcpy (com_cachedir, host_parms.cachedir);
1812 else
1813 com_cachedir[0] = 0;
1816 // start up with GAMENAME by default (id1)
1818 COM_AddGameDirectory (va("%s/"GAMENAME, basedir) );
1820 if (COM_CheckParm ("-rogue"))
1821 COM_AddGameDirectory (va("%s/rogue", basedir) );
1822 if (COM_CheckParm ("-hipnotic"))
1823 COM_AddGameDirectory (va("%s/hipnotic", basedir) );
1824 if (COM_CheckParm ("-kurok"))
1825 COM_AddGameDirectory (va("%s/kurok", basedir) );
1828 // -game <gamedir>
1829 // Adds basedir/gamedir as an override game
1831 i = COM_CheckParm ("-game");
1832 if (i && i < com_argc-1)
1834 com_modified = true;
1835 COM_AddGameDirectory (va("%s/%s", basedir, com_argv[i+1]));
1839 // -path <dir or packfile> [<dir or packfile>] ...
1840 // Fully specifies the exact serach path, overriding the generated one
1842 i = COM_CheckParm ("-path");
1843 if (i)
1845 com_modified = true;
1846 com_searchpaths = NULL;
1847 while (++i < com_argc)
1849 if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
1850 break;
1852 search = Hunk_Alloc (sizeof(searchpath_t));
1853 if ( !strcmp(COM_FileExtension(com_argv[i]), "pak") )
1855 search->pack = COM_LoadPackFile (com_argv[i]);
1856 if (!search->pack)
1857 Sys_Error ("Couldn't load packfile: %s", com_argv[i]);
1859 else
1860 strcpy (search->filename, com_argv[i]);
1861 search->next = com_searchpaths;
1862 com_searchpaths = search;
1866 if (COM_CheckParm ("-proghack"))
1867 proghack = true;