home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quake_src / common.c.orig < prev    next >
Text File  |  2000-06-17  |  37KB  |  1,828 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  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.
  8.  
  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.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  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.
  18.  
  19. */
  20. // common.c -- misc functions used in client and server
  21.  
  22. #include "quakedef.h"
  23.  
  24. #define NUM_SAFE_ARGVS  7
  25.  
  26. static char     *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1];
  27. static char     *argvdummy = " ";
  28.  
  29. static char     *safeargvs[NUM_SAFE_ARGVS] =
  30.   {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse", "-dibonly"};
  31.  
  32. cvar_t  registered = {"registered","0"};
  33. cvar_t  cmdline = {"cmdline","0", false, true};
  34.  
  35. qboolean        com_modified;   // set true if using non-id files
  36.  
  37. qboolean    proghack;
  38.  
  39. int             static_registered = 1;  // only for startup check, then set
  40.  
  41. qboolean    msg_suppress_1 = 0;
  42.  
  43. void COM_InitFilesystem (void);
  44.  
  45. // if a packfile directory differs from this, it is assumed to be hacked
  46. #define PAK0_COUNT              339
  47. #define PAK0_CRC                32981
  48.  
  49. char  com_token[1024];
  50. int   com_argc;
  51. char  **com_argv;
  52.  
  53. #define CMDLINE_LENGTH  256
  54. char  com_cmdline[CMDLINE_LENGTH];
  55.  
  56. qboolean    standard_quake = true, rogue, hipnotic;
  57.  
  58. // this graphic needs to be in the pak file to use registered features
  59. unsigned short pop[] =
  60. {
  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
  77. };
  78.  
  79. /*
  80.  
  81.  
  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.
  83.  
  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.
  86.  
  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.
  88.  
  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.
  92.  
  93.  
  94.  
  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.
  97.   
  98. */
  99.  
  100. //============================================================================
  101.  
  102.  
  103. // ClearLink is used for new headnodes
  104. void ClearLink (link_t *l)
  105. {
  106.   l->prev = l->next = l;
  107. }
  108.  
  109. void RemoveLink (link_t *l)
  110. {
  111.   l->next->prev = l->prev;
  112.   l->prev->next = l->next;
  113. }
  114.  
  115. void InsertLinkBefore (link_t *l, link_t *before)
  116. {
  117.   l->next = before;
  118.   l->prev = before->prev;
  119.   l->prev->next = l;
  120.   l->next->prev = l;
  121. }
  122. void InsertLinkAfter (link_t *l, link_t *after)
  123. {
  124.   l->next = after->next;
  125.   l->prev = after;
  126.   l->prev->next = l;
  127.   l->next->prev = l;
  128. }
  129.  
  130. /*
  131. ============================================================================
  132.  
  133.           LIBRARY REPLACEMENT FUNCTIONS
  134.  
  135. ============================================================================
  136. */
  137.  
  138. void Q_memset (void *dest, int fill, int count)
  139. {
  140.   int             i;
  141.   
  142.   if ( (((long)dest | count) & 3) == 0)
  143.   {
  144.     count >>= 2;
  145.     fill = fill | (fill<<8) | (fill<<16) | (fill<<24);
  146.     for (i=0 ; i<count ; i++)
  147.       ((int *)dest)[i] = fill;
  148.   }
  149.   else
  150.     for (i=0 ; i<count ; i++)
  151.       ((byte *)dest)[i] = fill;
  152. }
  153.  
  154. void Q_memcpy (void *dest, void *src, int count)
  155. {
  156.   int             i;
  157.   
  158.   if (( ( (long)dest | (long)src | count) & 3) == 0 )
  159.   {
  160.     count>>=2;
  161.     for (i=0 ; i<count ; i++)
  162.       ((int *)dest)[i] = ((int *)src)[i];
  163.   }
  164.   else
  165.     for (i=0 ; i<count ; i++)
  166.       ((byte *)dest)[i] = ((byte *)src)[i];
  167. }
  168.  
  169. int Q_memcmp (void *m1, void *m2, int count)
  170. {
  171.   while(count)
  172.   {
  173.     count--;
  174.     if (((byte *)m1)[count] != ((byte *)m2)[count])
  175.       return -1;
  176.   }
  177.   return 0;
  178. }
  179.  
  180. void Q_strcpy (char *dest, char *src)
  181. {
  182.   while (*src)
  183.   {
  184.     *dest++ = *src++;
  185.   }
  186.   *dest++ = 0;
  187. }
  188.  
  189. void Q_strncpy (char *dest, char *src, int count)
  190. {
  191.   while (*src && count--)
  192.   {
  193.     *dest++ = *src++;
  194.   }
  195.   if (count)
  196.     *dest++ = 0;
  197. }
  198.  
  199. int Q_strlen (char *str)
  200. {
  201.   int             count;
  202.   
  203.   count = 0;
  204.   while (str[count])
  205.     count++;
  206.  
  207.   return count;
  208. }
  209.  
  210. char *Q_strrchr(char *s, char c)
  211. {
  212.     int len = Q_strlen(s);
  213.     s += len;
  214.     while (len--)
  215.   if (*--s == c) return s;
  216.     return 0;
  217. }
  218.  
  219. void Q_strcat (char *dest, char *src)
  220. {
  221.   dest += Q_strlen(dest);
  222.   Q_strcpy (dest, src);
  223. }
  224.  
  225. int Q_strcmp (char *s1, char *s2)
  226. {
  227.   while (1)
  228.   {
  229.     if (*s1 != *s2)
  230.       return -1;              // strings not equal    
  231.     if (!*s1)
  232.       return 0;               // strings are equal
  233.     s1++;
  234.     s2++;
  235.   }
  236.   
  237.   return -1;
  238. }
  239.  
  240. int Q_strncmp (char *s1, char *s2, int count)
  241. {
  242.   while (1)
  243.   {
  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++;
  252.   }
  253.   
  254.   return -1;
  255. }
  256.  
  257. int Q_strncasecmp (char *s1, char *s2, int n)
  258. {
  259.   int             c1, c2;
  260.   
  261.   while (1)
  262.   {
  263.     c1 = *s1++;
  264.     c2 = *s2++;
  265.  
  266.     if (!n--)
  267.       return 0;               // strings are equal until end point
  268.     
  269.     if (c1 != c2)
  270.     {
  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
  277.     }
  278.     if (!c1)
  279.       return 0;               // strings are equal
  280. //              s1++;
  281. //              s2++;
  282.   }
  283.   
  284.   return -1;
  285. }
  286.  
  287. int Q_strcasecmp (char *s1, char *s2)
  288. {
  289.   return Q_strncasecmp (s1, s2, 99999);
  290. }
  291.  
  292. int Q_atoi (char *str)
  293. {
  294.   int             val;
  295.   int             sign;
  296.   int             c;
  297.   
  298.   if (*str == '-')
  299.   {
  300.     sign = -1;
  301.     str++;
  302.   }
  303.   else
  304.     sign = 1;
  305.     
  306.   val = 0;
  307.  
  308. //
  309. // check for hex
  310. //
  311.   if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  312.   {
  313.     str += 2;
  314.     while (1)
  315.     {
  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;
  325.     }
  326.   }
  327.   
  328. //
  329. // check for character
  330. //
  331.   if (str[0] == '\'')
  332.   {
  333.     return sign * str[1];
  334.   }
  335.   
  336. //
  337. // assume decimal
  338. //
  339.   while (1)
  340.   {
  341.     c = *str++;
  342.     if (c <'0' || c > '9')
  343.       return val*sign;
  344.     val = val*10 + c - '0';
  345.   }
  346.   
  347.   return 0;
  348. }
  349.  
  350.  
  351. float Q_atof (char *str)
  352. {
  353.   double      val;
  354.   int             sign;
  355.   int             c;
  356.   int             decimal, total;
  357.   
  358.   if (*str == '-')
  359.   {
  360.     sign = -1;
  361.     str++;
  362.   }
  363.   else
  364.     sign = 1;
  365.     
  366.   val = 0;
  367.  
  368. //
  369. // check for hex
  370. //
  371.   if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  372.   {
  373.     str += 2;
  374.     while (1)
  375.     {
  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;
  385.     }
  386.   }
  387.   
  388. //
  389. // check for character
  390. //
  391.   if (str[0] == '\'')
  392.   {
  393.     return sign * str[1];
  394.   }
  395.   
  396. //
  397. // assume decimal
  398. //
  399.   decimal = -1;
  400.   total = 0;
  401.   while (1)
  402.   {
  403.     c = *str++;
  404.     if (c == '.')
  405.     {
  406.       decimal = total;
  407.       continue;
  408.     }
  409.     if (c <'0' || c > '9')
  410.       break;
  411.     val = val*10 + c - '0';
  412.     total++;
  413.   }
  414.  
  415.   if (decimal == -1)
  416.     return val*sign;
  417.   while (total > decimal)
  418.   {
  419.     val /= 10;
  420.     total--;
  421.   }
  422.   
  423.   return val*sign;
  424. }
  425.  
  426. /*
  427. ============================================================================
  428.  
  429.           BYTE ORDER FUNCTIONS
  430.  
  431. ============================================================================
  432. */
  433.  
  434. qboolean        bigendien;
  435.  
  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);
  442.  
  443. short   ShortSwap (short l)
  444. {
  445.   byte    b1,b2;
  446.  
  447.   b1 = l&255;
  448.   b2 = (l>>8)&255;
  449.  
  450.   return (b1<<8) + b2;
  451. }
  452.  
  453. short   ShortNoSwap (short l)
  454. {
  455.   return l;
  456. }
  457.  
  458. int    LongSwap (int l)
  459. {
  460.   byte    b1,b2,b3,b4;
  461.  
  462.   b1 = l&255;
  463.   b2 = (l>>8)&255;
  464.   b3 = (l>>16)&255;
  465.   b4 = (l>>24)&255;
  466.  
  467.   return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  468. }
  469.  
  470. int     LongNoSwap (int l)
  471. {
  472.   return l;
  473. }
  474.  
  475. float FloatSwap (float f)
  476. {
  477.   union
  478.   {
  479.     float   f;
  480.     byte    b[4];
  481.   } dat1, dat2;
  482.   
  483.   
  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;
  490. }
  491.  
  492. float FloatNoSwap (float f)
  493. {
  494.   return f;
  495. }
  496.  
  497. /*
  498. ==============================================================================
  499.  
  500.       MESSAGE IO FUNCTIONS
  501.  
  502. Handles byte ordering and avoids alignment errors
  503. ==============================================================================
  504. */
  505.  
  506. //
  507. // writing functions
  508. //
  509.  
  510. void MSG_WriteChar (sizebuf_t *sb, int c)
  511. {
  512.   byte    *buf;
  513.   
  514. #ifdef PARANOID
  515.   if (c < -128 || c > 127)
  516.     Sys_Error ("MSG_WriteChar: range error");
  517. #endif
  518.  
  519.   buf = SZ_GetSpace (sb, 1);
  520.   buf[0] = c;
  521. }
  522.  
  523. void MSG_WriteByte (sizebuf_t *sb, int c)
  524. {
  525.   byte    *buf;
  526.   
  527. #ifdef PARANOID
  528.   if (c < 0 || c > 255)
  529.     Sys_Error ("MSG_WriteByte: range error");
  530. #endif
  531.  
  532.   buf = SZ_GetSpace (sb, 1);
  533.   buf[0] = c;
  534. }
  535.  
  536. void MSG_WriteShort (sizebuf_t *sb, int c)
  537. {
  538.   byte    *buf;
  539.   
  540. #ifdef PARANOID
  541.   if (c < ((short)0x8000) || c > (short)0x7fff)
  542.     Sys_Error ("MSG_WriteShort: range error");
  543. #endif
  544.  
  545.   buf = SZ_GetSpace (sb, 2);
  546.   buf[0] = c&0xff;
  547.   buf[1] = c>>8;
  548. }
  549.  
  550. void MSG_WriteLong (sizebuf_t *sb, int c)
  551. {
  552.   byte    *buf;
  553.   
  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;
  559. }
  560.  
  561. void MSG_WriteFloat (sizebuf_t *sb, float f)
  562. {
  563.   union
  564.   {
  565.     float   f;
  566.     int     l;
  567.   } dat;
  568.   
  569.   
  570.   dat.f = f;
  571.   dat.l = LittleLong (dat.l);
  572.   
  573.   SZ_Write (sb, &dat.l, 4);
  574. }
  575.  
  576. void MSG_WriteString (sizebuf_t *sb, char *s)
  577. {
  578.   if (!s)
  579.     SZ_Write (sb, "", 1);
  580.   else
  581.     SZ_Write (sb, s, Q_strlen(s)+1);
  582. }
  583.  
  584. void MSG_WriteCoord (sizebuf_t *sb, float f)
  585. {
  586.   MSG_WriteShort (sb, (int)(f*8));
  587. }
  588.  
  589. void MSG_WriteAngle (sizebuf_t *sb, float f)
  590. {
  591.   MSG_WriteByte (sb, ((int)f*256/360) & 255);
  592. }
  593.  
  594. //
  595. // reading functions
  596. //
  597. int                     msg_readcount;
  598. qboolean        msg_badread;
  599.  
  600. void MSG_BeginReading (void)
  601. {
  602.   msg_readcount = 0;
  603.   msg_badread = false;
  604. }
  605.  
  606. // returns -1 and sets msg_badread if no more characters are available
  607. int MSG_ReadChar (void)
  608. {
  609.   int     c;
  610.   
  611.   if (msg_readcount+1 > net_message.cursize)
  612.   {
  613.     msg_badread = true;
  614.     return -1;
  615.   }
  616.     
  617.   c = (signed char)net_message.data[msg_readcount];
  618.   msg_readcount++;
  619.   
  620.   return c;
  621. }
  622.  
  623. int MSG_ReadByte (void)
  624. {
  625.   int     c;
  626.   
  627.   if (msg_readcount+1 > net_message.cursize)
  628.   {
  629.     msg_badread = true;
  630.     return -1;
  631.   }
  632.     
  633.   c = (unsigned char)net_message.data[msg_readcount];
  634.   msg_readcount++;
  635.   
  636.   return c;
  637. }
  638.  
  639. int MSG_ReadShort (void)
  640. {
  641.   int     c;
  642.   
  643.   if (msg_readcount+2 > net_message.cursize)
  644.   {
  645.     msg_badread = true;
  646.     return -1;
  647.   }
  648.     
  649.   c = (short)(net_message.data[msg_readcount]
  650.   + (net_message.data[msg_readcount+1]<<8));
  651.   
  652.   msg_readcount += 2;
  653.   
  654.   return c;
  655. }
  656.  
  657. int MSG_ReadLong (void)
  658. {
  659.   int     c;
  660.   
  661.   if (msg_readcount+4 > net_message.cursize)
  662.   {
  663.     msg_badread = true;
  664.     return -1;
  665.   }
  666.     
  667.   c = net_message.data[msg_readcount]
  668.   + (net_message.data[msg_readcount+1]<<8)
  669.   + (net_message.data[msg_readcount+2]<<16)
  670.   + (net_message.data[msg_readcount+3]<<24);
  671.   
  672.   msg_readcount += 4;
  673.   
  674.   return c;
  675. }
  676.  
  677. float MSG_ReadFloat (void)
  678. {
  679.   union
  680.   {
  681.     byte    b[4];
  682.     float   f;
  683.     int     l;
  684.   } dat;
  685.   
  686.   dat.b[0] =      net_message.data[msg_readcount];
  687.   dat.b[1] =      net_message.data[msg_readcount+1];
  688.   dat.b[2] =      net_message.data[msg_readcount+2];
  689.   dat.b[3] =      net_message.data[msg_readcount+3];
  690.   msg_readcount += 4;
  691.   
  692.   dat.l = LittleLong (dat.l);
  693.  
  694.   return dat.f;   
  695. }
  696.  
  697. char *MSG_ReadString (void)
  698. {
  699.   static char     string[2048];
  700.   int             l,c;
  701.   
  702.   l = 0;
  703.   do
  704.   {
  705.     c = MSG_ReadChar ();
  706.     if (c == -1 || c == 0)
  707.       break;
  708.     string[l] = c;
  709.     l++;
  710.   } while (l < sizeof(string)-1);
  711.   
  712.   string[l] = 0;
  713.   
  714.   return string;
  715. }
  716.  
  717. float MSG_ReadCoord (void)
  718. {
  719.   return MSG_ReadShort() * (1.0/8);
  720. }
  721.  
  722. float MSG_ReadAngle (void)
  723. {
  724.   return MSG_ReadChar() * (360.0/256);
  725. }
  726.  
  727.  
  728.  
  729. //===========================================================================
  730.  
  731. void SZ_Alloc (sizebuf_t *buf, int startsize)
  732. {
  733.   if (startsize < 256)
  734.     startsize = 256;
  735.   buf->data = Hunk_AllocName (startsize, "sizebuf");
  736.   buf->maxsize = startsize;
  737.   buf->cursize = 0;
  738. }
  739.  
  740.  
  741. void SZ_Free (sizebuf_t *buf)
  742. {
  743. //      Z_Free (buf->data);
  744. //      buf->data = NULL;
  745. //      buf->maxsize = 0;
  746.   buf->cursize = 0;
  747. }
  748.  
  749. void SZ_Clear (sizebuf_t *buf)
  750. {
  751.   buf->cursize = 0;
  752. }
  753.  
  754. void *SZ_GetSpace (sizebuf_t *buf, int length)
  755. {
  756.   void    *data;
  757.   
  758.   if (buf->cursize + length > buf->maxsize)
  759.   {
  760.     if (!buf->allowoverflow)
  761.       Sys_Error ("SZ_GetSpace: overflow without allowoverflow set");
  762.     
  763.     if (length > buf->maxsize)
  764.       Sys_Error ("SZ_GetSpace: %i is > full buffer size", length);
  765.       
  766.     buf->overflowed = true;
  767.     Con_Printf ("SZ_GetSpace: overflow");
  768.     SZ_Clear (buf); 
  769.   }
  770.  
  771.   data = buf->data + buf->cursize;
  772.   buf->cursize += length;
  773.   
  774.   return data;
  775. }
  776.  
  777. void SZ_Write (sizebuf_t *buf, void *data, int length)
  778. {
  779.   Q_memcpy (SZ_GetSpace(buf,length),data,length);         
  780. }
  781.  
  782. void SZ_Print (sizebuf_t *buf, char *data)
  783. {
  784.   int             len;
  785.   
  786.   len = Q_strlen(data)+1;
  787.  
  788. // byte * cast to keep VC++ happy
  789.   if (buf->data[buf->cursize-1])
  790.     Q_memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
  791.   else
  792.     Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
  793. }
  794.  
  795.  
  796. //============================================================================
  797.  
  798.  
  799. /*
  800. ============
  801. COM_SkipPath
  802. ============
  803. */
  804. char *COM_SkipPath (char *pathname)
  805. {
  806.   char    *last;
  807.   
  808.   last = pathname;
  809.   while (*pathname)
  810.   {
  811.     if (*pathname=='/')
  812.       last = pathname+1;
  813.     pathname++;
  814.   }
  815.   return last;
  816. }
  817.  
  818. /*
  819. ============
  820. COM_StripExtension
  821. ============
  822. */
  823. void COM_StripExtension (char *in, char *out)
  824. {
  825.   while (*in && *in != '.')
  826.     *out++ = *in++;
  827.   *out = 0;
  828. }
  829.  
  830. /*
  831. ============
  832. COM_FileExtension
  833. ============
  834. */
  835. char *COM_FileExtension (char *in)
  836. {
  837.   static char exten[8];
  838.   int             i;
  839.  
  840.   while (*in && *in != '.')
  841.     in++;
  842.   if (!*in)
  843.     return "";
  844.   in++;
  845.   for (i=0 ; i<7 && *in ; i++,in++)
  846.     exten[i] = *in;
  847.   exten[i] = 0;
  848.   return exten;
  849. }
  850.  
  851. /*
  852. ============
  853. COM_FileBase
  854. ============
  855. */
  856. void COM_FileBase (char *in, char *out)
  857. {
  858.   char *s, *s2;
  859.   
  860.   s = in + strlen(in) - 1;
  861.   
  862.   while (s != in && *s != '.')
  863.     s--;
  864.   
  865.   for (s2 = s ; *s2 && *s2 != '/' ; s2--)
  866.   ;
  867.   
  868.   if (s-s2 < 2)
  869.     strcpy (out,"?model?");
  870.   else
  871.   {
  872.     s--;
  873.     strncpy (out,s2+1, s-s2);
  874.     out[s-s2] = 0;
  875.   }
  876. }
  877.  
  878.  
  879. /*
  880. ==================
  881. COM_DefaultExtension
  882. ==================
  883. */
  884. void COM_DefaultExtension (char *path, char *extension)
  885. {
  886.   char    *src;
  887. //
  888. // if path doesn't have a .EXT, append extension
  889. // (extension should include the .)
  890. //
  891.   src = path + strlen(path) - 1;
  892.  
  893.   while (*src != '/' && src != path)
  894.   {
  895.     if (*src == '.')
  896.       return;                 // it has an extension
  897.     src--;
  898.   }
  899.  
  900.   strcat (path, extension);
  901. }
  902.  
  903.  
  904. /*
  905. ==============
  906. COM_Parse
  907.  
  908. Parse a token out of a string
  909. ==============
  910. */
  911. char *COM_Parse (char *data)
  912. {
  913.   int             c;
  914.   int             len;
  915.   
  916.   len = 0;
  917.   com_token[0] = 0;
  918.   
  919.   if (!data)
  920.     return NULL;
  921.     
  922. // skip whitespace
  923. skipwhite:
  924.   while ( (c = *data) <= ' ')
  925.   {
  926.     if (c == 0)
  927.       return NULL;                    // end of file;
  928.     data++;
  929.   }
  930.   
  931. // skip // comments
  932.   if (c=='/' && data[1] == '/')
  933.   {
  934.     while (*data && *data != '\n')
  935.       data++;
  936.     goto skipwhite;
  937.   }
  938.   
  939.  
  940. // handle quoted strings specially
  941.   if (c == '\"')
  942.   {
  943.     data++;
  944.     while (1)
  945.     {
  946.       c = *data++;
  947.       if (c=='\"' || !c)
  948.       {
  949.         com_token[len] = 0;
  950.         return data;
  951.       }
  952.       com_token[len] = c;
  953.       len++;
  954.     }
  955.   }
  956.  
  957. // parse single characters
  958.   if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  959.   {
  960.     com_token[len] = c;
  961.     len++;
  962.     com_token[len] = 0;
  963.     return data+1;
  964.   }
  965.  
  966. // parse a regular word
  967.   do
  968.   {
  969.     com_token[len] = c;
  970.     data++;
  971.     len++;
  972.     c = *data;
  973.   if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  974.       break;
  975.   } while (c>32);
  976.   
  977.   com_token[len] = 0;
  978.   return data;
  979. }
  980.  
  981.  
  982. /*
  983. ================
  984. COM_CheckParm
  985.  
  986. Returns the position (1 to argc-1) in the program's argument list
  987. where the given parameter apears, or 0 if not present
  988. ================
  989. */
  990. int COM_CheckParm (char *parm)
  991. {
  992.   int             i;
  993.   
  994.   for (i=1 ; i<com_argc ; i++)
  995.   {
  996.     if (!com_argv[i])
  997.       continue;               // NEXTSTEP sometimes clears appkit vars.
  998.     if (!Q_strcmp (parm,com_argv[i]))
  999.       return i;
  1000.   }
  1001.     
  1002.   return 0;
  1003. }
  1004.  
  1005. /*
  1006. ================
  1007. COM_CheckRegistered
  1008.  
  1009. Looks for the pop.txt file and verifies it.
  1010. Sets the "registered" cvar.
  1011. Immediately exits out if an alternate game was attempted to be started without
  1012. being registered.
  1013. ================
  1014. */
  1015. void COM_CheckRegistered (void)
  1016. {
  1017.   int             h;
  1018.   unsigned short  check[128];
  1019.   int                     i;
  1020.  
  1021.   COM_OpenFile("gfx/pop.lmp", &h);
  1022.   static_registered = 0;
  1023.  
  1024.   if (h == -1)
  1025.   {
  1026. #if WINDED
  1027.   Sys_Error ("This dedicated server requires a full registered copy of Quake");
  1028. #endif
  1029.     Con_Printf ("Playing shareware version.\n");
  1030.     if (com_modified)
  1031.       Sys_Error ("You must have the registered version to use modified games");
  1032.     return;
  1033.   }
  1034.  
  1035.   Sys_FileRead (h, check, sizeof(check));
  1036.   COM_CloseFile (h);
  1037.   
  1038.   for (i=0 ; i<128 ; i++)
  1039.     if (pop[i] != (unsigned short)BigShort (check[i]))
  1040.       Sys_Error ("Corrupted data file.");
  1041.   
  1042.   Cvar_Set ("cmdline", com_cmdline);
  1043.   Cvar_Set ("registered", "1");
  1044.   static_registered = 1;
  1045.   Con_Printf ("Playing registered version.\n");
  1046. }
  1047.  
  1048.  
  1049. void COM_Path_f (void);
  1050.  
  1051.  
  1052. /*
  1053. ================
  1054. COM_InitArgv
  1055. ================
  1056. */
  1057. void COM_InitArgv (int argc, char **argv)
  1058. {
  1059.   qboolean        safe;
  1060.   int             i, j, n;
  1061.  
  1062. // reconstitute the command line for the cmdline externally visible cvar
  1063.   n = 0;
  1064.  
  1065.   for (j=0 ; (j<MAX_NUM_ARGVS) && (j< argc) ; j++)
  1066.   {
  1067.     i = 0;
  1068.  
  1069.     while ((n < (CMDLINE_LENGTH - 1)) && argv[j][i])
  1070.     {
  1071.       com_cmdline[n++] = argv[j][i++];
  1072.     }
  1073.  
  1074.     if (n < (CMDLINE_LENGTH - 1))
  1075.       com_cmdline[n++] = ' ';
  1076.     else
  1077.       break;
  1078.   }
  1079.  
  1080.   com_cmdline[n] = 0;
  1081.  
  1082.   safe = false;
  1083.  
  1084.   for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
  1085.      com_argc++)
  1086.   {
  1087.     largv[com_argc] = argv[com_argc];
  1088.     if (!Q_strcmp ("-safe", argv[com_argc]))
  1089.       safe = true;
  1090.   }
  1091.  
  1092.   if (safe)
  1093.   {
  1094.   // force all the safe-mode switches. Note that we reserved extra space in
  1095.   // case we need to add these, so we don't need an overflow check
  1096.     for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
  1097.     {
  1098.       largv[com_argc] = safeargvs[i];
  1099.       com_argc++;
  1100.     }
  1101.   }
  1102.  
  1103.   largv[com_argc] = argvdummy;
  1104.   com_argv = largv;
  1105.  
  1106.   if (COM_CheckParm ("-rogue"))
  1107.   {
  1108.     rogue = true;
  1109.     standard_quake = false;
  1110.   }
  1111.  
  1112.   if (COM_CheckParm ("-hipnotic"))
  1113.   {
  1114.     hipnotic = true;
  1115.     standard_quake = false;
  1116.   }
  1117. }
  1118.  
  1119.  
  1120. /*
  1121. ================
  1122. COM_Init
  1123. ================
  1124. */
  1125. void COM_Init (char *basedir)
  1126. {
  1127.   byte    swaptest[2] = {1,0};
  1128.  
  1129. // set the byte swapping variables in a portable manner 
  1130.   if ( *(short *)swaptest == 1)
  1131.   {
  1132.     bigendien = false;
  1133.     BigShort = ShortSwap;
  1134.     LittleShort = ShortNoSwap;
  1135.     BigLong = LongSwap;
  1136.     LittleLong = LongNoSwap;
  1137.     BigFloat = FloatSwap;
  1138.     LittleFloat = FloatNoSwap;
  1139.   }
  1140.   else
  1141.   {
  1142.     bigendien = true;
  1143.     BigShort = ShortNoSwap;
  1144.     LittleShort = ShortSwap;
  1145.     BigLong = LongNoSwap;
  1146.     LittleLong = LongSwap;
  1147.     BigFloat = FloatNoSwap;
  1148.     LittleFloat = FloatSwap;
  1149.   }
  1150.  
  1151.   Cvar_RegisterVariable (®istered);
  1152.   Cvar_RegisterVariable (&cmdline);
  1153.   Cmd_AddCommand ("path", COM_Path_f);
  1154.  
  1155.   COM_InitFilesystem ();
  1156.   COM_CheckRegistered ();
  1157. }
  1158.  
  1159.  
  1160. /*
  1161. ============
  1162. va
  1163.  
  1164. does a varargs printf into a temp buffer, so I don't need to have
  1165. varargs versions of all text functions.
  1166. FIXME: make this buffer size safe someday
  1167. ============
  1168. */
  1169. char    *va(char *format, ...)
  1170. {
  1171.   va_list         argptr;
  1172.   static char             string[1024];
  1173.   
  1174.   va_start (argptr, format);
  1175.   vsprintf (string, format,argptr);
  1176.   va_end (argptr);
  1177.  
  1178.   return string;  
  1179. }
  1180.  
  1181.  
  1182. /// just for debugging
  1183. int     memsearch (byte *start, int count, int search)
  1184. {
  1185.   int             i;
  1186.   
  1187.   for (i=0 ; i<count ; i++)
  1188.     if (start[i] == search)
  1189.       return i;
  1190.   return -1;
  1191. }
  1192.  
  1193. /*
  1194. =============================================================================
  1195.  
  1196. QUAKE FILESYSTEM
  1197.  
  1198. =============================================================================
  1199. */
  1200.  
  1201. int     com_filesize;
  1202.  
  1203.  
  1204. //
  1205. // in memory
  1206. //
  1207.  
  1208. typedef struct
  1209. {
  1210.   char    name[MAX_QPATH];
  1211.   int             filepos, filelen;
  1212. } packfile_t;
  1213.  
  1214. typedef struct pack_s
  1215. {
  1216.   char    filename[MAX_OSPATH];
  1217.   int             handle;
  1218.   int             numfiles;
  1219.   packfile_t      *files;
  1220. } pack_t;
  1221.  
  1222. //
  1223. // on disk
  1224. //
  1225. typedef struct
  1226. {
  1227.   char    name[56];
  1228.   int             filepos, filelen;
  1229. } dpackfile_t;
  1230.  
  1231. typedef struct
  1232. {
  1233.   char    id[4];
  1234.   int             dirofs;
  1235.   int             dirlen;
  1236. } dpackheader_t;
  1237.  
  1238. #define MAX_FILES_IN_PACK       2048
  1239.  
  1240. char    com_cachedir[MAX_OSPATH];
  1241. char    com_gamedir[MAX_OSPATH];
  1242.  
  1243. typedef struct searchpath_s
  1244. {
  1245.   char    filename[MAX_OSPATH];
  1246.   pack_t  *pack;          // only one of filename / pack will be used
  1247.   struct searchpath_s *next;
  1248. } searchpath_t;
  1249.  
  1250. searchpath_t    *com_searchpaths;
  1251.  
  1252. /*
  1253. ============
  1254. COM_Path_f
  1255.  
  1256. ============
  1257. */
  1258. void COM_Path_f (void)
  1259. {
  1260.   searchpath_t    *s;
  1261.   
  1262.   Con_Printf ("Current search path:\n");
  1263.   for (s=com_searchpaths ; s ; s=s->next)
  1264.   {
  1265.     if (s->pack)
  1266.     {
  1267.       Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
  1268.     }
  1269.     else
  1270.       Con_Printf ("%s\n", s->filename);
  1271.   }
  1272. }
  1273.  
  1274. /*
  1275. ============
  1276. COM_WriteFile
  1277.  
  1278. The filename will be prefixed by the current game directory
  1279. ============
  1280. */
  1281. void COM_WriteFile (char *filename, void *data, int len)
  1282. {
  1283.   int             handle;
  1284.   char    name[MAX_OSPATH];
  1285.   
  1286.   sprintf (name, "%s/%s", com_gamedir, filename);
  1287.  
  1288.   handle = Sys_FileOpenWrite (name);
  1289.   if (handle == -1)
  1290.   {
  1291.     Sys_Printf ("COM_WriteFile: failed on %s\n", name);
  1292.     return;
  1293.   }
  1294.   
  1295.   Sys_Printf ("COM_WriteFile: %s\n", name);
  1296.   Sys_FileWrite (handle, data, len);
  1297.   Sys_FileClose (handle);
  1298. }
  1299.  
  1300.  
  1301. /*
  1302. ============
  1303. COM_CreatePath
  1304.  
  1305. Only used for CopyFile
  1306. ============
  1307. */
  1308. void    COM_CreatePath (char *path)
  1309. {
  1310.   char    *ofs;
  1311.   
  1312.   for (ofs = path+1 ; *ofs ; ofs++)
  1313.   {
  1314.     if (*ofs == '/')
  1315.     {       // create the directory
  1316.       *ofs = 0;
  1317.       Sys_mkdir (path);
  1318.       *ofs = '/';
  1319.     }
  1320.   }
  1321. }
  1322.  
  1323.  
  1324. /*
  1325. ===========
  1326. COM_CopyFile
  1327.  
  1328. Copies a file over from the net to the local cache, creating any directories
  1329. needed.  This is for the convenience of developers using ISDN from home.
  1330. ===========
  1331. */
  1332. void COM_CopyFile (char *netpath, char *cachepath)
  1333. {
  1334.   int             in, out;
  1335.   int             remaining, count;
  1336.   char    buf[4096];
  1337.   
  1338.   remaining = Sys_FileOpenRead (netpath, &in);            
  1339.   COM_CreatePath (cachepath);     // create directories up to the cache file
  1340.   out = Sys_FileOpenWrite (cachepath);
  1341.   
  1342.   while (remaining)
  1343.   {
  1344.     if (remaining < sizeof(buf))
  1345.       count = remaining;
  1346.     else
  1347.       count = sizeof(buf);
  1348.     Sys_FileRead (in, buf, count);
  1349.     Sys_FileWrite (out, buf, count);
  1350.     remaining -= count;
  1351.   }
  1352.  
  1353.   Sys_FileClose (in);
  1354.   Sys_FileClose (out);    
  1355. }
  1356.  
  1357. /*
  1358. ===========
  1359. COM_FindFile
  1360.  
  1361. Finds the file in the search path.
  1362. Sets com_filesize and one of handle or file
  1363. ===========
  1364. */
  1365. int COM_FindFile (char *filename, int *handle, FILE **file)
  1366. {
  1367.   searchpath_t    *search;
  1368.   char            netpath[MAX_OSPATH];
  1369.   char            cachepath[MAX_OSPATH];
  1370.   pack_t          *pak;
  1371.   int                     i;
  1372.   int                     findtime, cachetime;
  1373.  
  1374.   if (file && handle)
  1375.     Sys_Error ("COM_FindFile: both handle and file set");
  1376.   if (!file && !handle)
  1377.     Sys_Error ("COM_FindFile: neither handle or file set");
  1378.     
  1379. //
  1380. // search through the path, one element at a time
  1381. //
  1382.   search = com_searchpaths;
  1383.   if (proghack)
  1384.   { // gross hack to use quake 1 progs with quake 2 maps
  1385.     if (!strcmp(filename, "progs.dat"))
  1386.       search = search->next;
  1387.   }
  1388.  
  1389.   for ( ; search ; search = search->next)
  1390.   {
  1391.   // is the element a pak file?
  1392.     if (search->pack)
  1393.     {
  1394.     // look through all the pak file elements
  1395.       pak = search->pack;
  1396.       for (i=0 ; i<pak->numfiles ; i++)
  1397.         if (!strcmp (pak->files[i].name, filename))
  1398.         {       // found it!
  1399.           Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename);
  1400.           if (handle)
  1401.           {
  1402.             *handle = pak->handle;
  1403.             Sys_FileSeek (pak->handle, pak->files[i].filepos);
  1404.           }
  1405.           else
  1406.           {       // open a new file on the pakfile
  1407.             *file = fopen (pak->filename, "rb");
  1408.             if (*file)
  1409.               fseek (*file, pak->files[i].filepos, SEEK_SET);
  1410.           }
  1411.           com_filesize = pak->files[i].filelen;
  1412.           return com_filesize;
  1413.         }
  1414.     }
  1415.     else
  1416.     {               
  1417.   // check a file in the directory tree
  1418.       if (!static_registered)
  1419.       {       // if not a registered version, don't ever go beyond base
  1420.         if ( strchr (filename, '/') || strchr (filename,'\\'))
  1421.           continue;
  1422.       }
  1423.       
  1424.       sprintf (netpath, "%s/%s",search->filename, filename);
  1425.       
  1426.       findtime = Sys_FileTime (netpath);
  1427.       if (findtime == -1)
  1428.         continue;
  1429.         
  1430.     // see if the file needs to be updated in the cache
  1431.       if (!com_cachedir[0])
  1432.         strcpy (cachepath, netpath);
  1433.       else
  1434.       { 
  1435. #if defined(_WIN32)
  1436.         if ((strlen(netpath) < 2) || (netpath[1] != ':'))
  1437.           sprintf (cachepath,"%s%s", com_cachedir, netpath);
  1438.         else
  1439.           sprintf (cachepath,"%s%s", com_cachedir, netpath+2);
  1440. #else
  1441.         sprintf (cachepath,"%s%s", com_cachedir, netpath);
  1442. #endif
  1443.  
  1444.         cachetime = Sys_FileTime (cachepath);
  1445.       
  1446.         if (cachetime < findtime)
  1447.           COM_CopyFile (netpath, cachepath);
  1448.         strcpy (netpath, cachepath);
  1449.       } 
  1450.  
  1451.       Sys_Printf ("FindFile: %s\n",netpath);
  1452.       com_filesize = Sys_FileOpenRead (netpath, &i);
  1453.       if (handle)
  1454.         *handle = i;
  1455.       else
  1456.       {
  1457.         Sys_FileClose (i);
  1458.         *file = fopen (netpath, "rb");
  1459.       }
  1460.       return com_filesize;
  1461.     }
  1462.     
  1463.   }
  1464.   
  1465.   Sys_Printf ("FindFile: can't find %s\n", filename);
  1466.   
  1467.   if (handle)
  1468.     *handle = -1;
  1469.   else
  1470.     *file = NULL;
  1471.   com_filesize = -1;
  1472.   return -1;
  1473. }
  1474.  
  1475.  
  1476. /*
  1477. ===========
  1478. COM_OpenFile
  1479.  
  1480. filename never has a leading slash, but may contain directory walks
  1481. returns a handle and a length
  1482. it may actually be inside a pak file
  1483. ===========
  1484. */
  1485. int COM_OpenFile (char *filename, int *handle)
  1486. {
  1487.   return COM_FindFile (filename, handle, NULL);
  1488. }
  1489.  
  1490. /*
  1491. ===========
  1492. COM_FOpenFile
  1493.  
  1494. If the requested file is inside a packfile, a new FILE * will be opened
  1495. into the file.
  1496. ===========
  1497. */
  1498. int COM_FOpenFile (char *filename, FILE **file)
  1499. {
  1500.   return COM_FindFile (filename, NULL, file);
  1501. }
  1502.  
  1503. /*
  1504. ============
  1505. COM_CloseFile
  1506.  
  1507. If it is a pak file handle, don't really close it
  1508. ============
  1509. */
  1510. void COM_CloseFile (int h)
  1511. {
  1512.   searchpath_t    *s;
  1513.   
  1514.   for (s = com_searchpaths ; s ; s=s->next)
  1515.     if (s->pack && s->pack->handle == h)
  1516.       return;
  1517.       
  1518.   Sys_FileClose (h);
  1519. }
  1520.  
  1521.  
  1522. /*
  1523. ============
  1524. COM_LoadFile
  1525.  
  1526. Filename are reletive to the quake directory.
  1527. Allways appends a 0 byte.
  1528. ============
  1529. */
  1530. cache_user_t *loadcache;
  1531. byte    *loadbuf;
  1532. int             loadsize;
  1533. byte *COM_LoadFile (char *path, int usehunk)
  1534. {
  1535.   int             h;
  1536.   byte    *buf;
  1537.   char    base[32];
  1538.   int             len;
  1539.  
  1540.   buf = NULL;     // quiet compiler warning
  1541.  
  1542. // look for it in the filesystem or pack files
  1543.   len = COM_OpenFile (path, &h);
  1544.   if (h == -1)
  1545.     return NULL;
  1546.   
  1547. // extract the filename base name for hunk tag
  1548.   COM_FileBase (path, base);
  1549.   
  1550.   if (usehunk == 1)
  1551.     buf = Hunk_AllocName (len+1, base);
  1552.   else if (usehunk == 2)
  1553.     buf = Hunk_TempAlloc (len+1);
  1554.   else if (usehunk == 0)
  1555.     buf = Z_Malloc (len+1);
  1556.   else if (usehunk == 3)
  1557.     buf = Cache_Alloc (loadcache, len+1, base);
  1558.   else if (usehunk == 4)
  1559.   {
  1560.     if (len+1 > loadsize)
  1561.       buf = Hunk_TempAlloc (len+1);
  1562.     else
  1563.       buf = loadbuf;
  1564.   }
  1565.   else
  1566.     Sys_Error ("COM_LoadFile: bad usehunk");
  1567.  
  1568.   if (!buf)
  1569.     Sys_Error ("COM_LoadFile: not enough space for %s", path);
  1570.     
  1571.   ((byte *)buf)[len] = 0;
  1572.  
  1573.   Draw_BeginDisc ();
  1574.   Sys_FileRead (h, buf, len);                     
  1575.   COM_CloseFile (h);
  1576.   Draw_EndDisc ();
  1577.  
  1578.   return buf;
  1579. }
  1580.  
  1581. byte *COM_LoadHunkFile (char *path)
  1582. {
  1583.   return COM_LoadFile (path, 1);
  1584. }
  1585.  
  1586. byte *COM_LoadTempFile (char *path)
  1587. {
  1588.   return COM_LoadFile (path, 2);
  1589. }
  1590.  
  1591. void COM_LoadCacheFile (char *path, struct cache_user_s *cu)
  1592. {
  1593.   loadcache = cu;
  1594.   COM_LoadFile (path, 3);
  1595. }
  1596.  
  1597. // uses temp hunk if larger than bufsize
  1598. byte *COM_LoadStackFile (char *path, void *buffer, int bufsize)
  1599. {
  1600.   byte    *buf;
  1601.   
  1602.   loadbuf = (byte *)buffer;
  1603.   loadsize = bufsize;
  1604.   buf = COM_LoadFile (path, 4);
  1605.   
  1606.   return buf;
  1607. }
  1608.  
  1609. /*
  1610. =================
  1611. COM_LoadPackFile
  1612.  
  1613. Takes an explicit (not game tree related) path to a pak file.
  1614.  
  1615. Loads the header and directory, adding the files at the beginning
  1616. of the list so they override previous pack files.
  1617. =================
  1618. */
  1619. pack_t *COM_LoadPackFile (char *packfile)
  1620. {
  1621.   dpackheader_t   header;
  1622.   int                             i;
  1623.   packfile_t              *newfiles;
  1624.   int                             numpackfiles;
  1625.   pack_t                  *pack;
  1626.   int                             packhandle;
  1627.   dpackfile_t             info[MAX_FILES_IN_PACK];
  1628.   unsigned short          crc;
  1629.  
  1630.   if (Sys_FileOpenRead (packfile, &packhandle) == -1)
  1631.   {
  1632. //              Con_Printf ("Couldn't open %s\n", packfile);
  1633.     return NULL;
  1634.   }
  1635.   Sys_FileRead (packhandle, (void *)&header, sizeof(header));
  1636.   if (header.id[0] != 'P' || header.id[1] != 'A'
  1637.   || header.id[2] != 'C' || header.id[3] != 'K')
  1638.     Sys_Error ("%s is not a packfile", packfile);
  1639.   header.dirofs = LittleLong (header.dirofs);
  1640.   header.dirlen = LittleLong (header.dirlen);
  1641.  
  1642.   numpackfiles = header.dirlen / sizeof(dpackfile_t);
  1643.  
  1644.   if (numpackfiles > MAX_FILES_IN_PACK)
  1645.     Sys_Error ("%s has %i files", packfile, numpackfiles);
  1646.  
  1647.   if (numpackfiles != PAK0_COUNT)
  1648.     com_modified = true;    // not the original file
  1649.  
  1650.   newfiles = Hunk_AllocName (numpackfiles * sizeof(packfile_t), "packfile");
  1651.  
  1652.   Sys_FileSeek (packhandle, header.dirofs);
  1653.   Sys_FileRead (packhandle, (void *)info, header.dirlen);
  1654.  
  1655. // crc the directory to check for modifications
  1656.   CRC_Init (&crc);
  1657.   for (i=0 ; i<header.dirlen ; i++)
  1658.     CRC_ProcessByte (&crc, ((byte *)info)[i]);
  1659.   if (crc != PAK0_CRC)
  1660.     com_modified = true;
  1661.  
  1662. // parse the directory
  1663.   for (i=0 ; i<numpackfiles ; i++)
  1664.   {
  1665.     strcpy (newfiles[i].name, info[i].name);
  1666.     newfiles[i].filepos = LittleLong(info[i].filepos);
  1667.     newfiles[i].filelen = LittleLong(info[i].filelen);
  1668.   }
  1669.  
  1670.   pack = Hunk_Alloc (sizeof (pack_t));
  1671.   strcpy (pack->filename, packfile);
  1672.   pack->handle = packhandle;
  1673.   pack->numfiles = numpackfiles;
  1674.   pack->files = newfiles;
  1675.   
  1676.   Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
  1677.   return pack;
  1678. }
  1679.  
  1680.  
  1681. /*
  1682. ================
  1683. COM_AddGameDirectory
  1684.  
  1685. Sets com_gamedir, adds the directory to the head of the path,
  1686. then loads and adds pak1.pak pak2.pak ... 
  1687. ================
  1688. */
  1689. void COM_AddGameDirectory (char *dir)
  1690. {
  1691.   int                             i;
  1692.   searchpath_t    *search;
  1693.   pack_t                  *pak;
  1694.   char                    pakfile[MAX_OSPATH];
  1695.  
  1696.   strcpy (com_gamedir, dir);
  1697.  
  1698. //
  1699. // add the directory to the search path
  1700. //
  1701.   search = Hunk_Alloc (sizeof(searchpath_t));
  1702.   strcpy (search->filename, dir);
  1703.   search->next = com_searchpaths;
  1704.   com_searchpaths = search;
  1705.  
  1706. //
  1707. // add any pak files in the format pak0.pak pak1.pak, ...
  1708. //
  1709.   for (i=0 ; ; i++)
  1710.   {
  1711.     sprintf (pakfile, "%s/pak%i.pak", dir, i);
  1712.     pak = COM_LoadPackFile (pakfile);
  1713.     if (!pak)
  1714.       break;
  1715.     search = Hunk_Alloc (sizeof(searchpath_t));
  1716.     search->pack = pak;
  1717.     search->next = com_searchpaths;
  1718.     com_searchpaths = search;               
  1719.   }
  1720.  
  1721. //
  1722. // add the contents of the parms.txt file to the end of the command line
  1723. //
  1724.  
  1725. }
  1726.  
  1727. /*
  1728. ================
  1729. COM_InitFilesystem
  1730. ================
  1731. */
  1732. void COM_InitFilesystem (void)
  1733. {
  1734.   int             i, j;
  1735.   char    basedir[MAX_OSPATH];
  1736.   searchpath_t    *search;
  1737.  
  1738. //
  1739. // -basedir <path>
  1740. // Overrides the system supplied base directory (under GAMENAME)
  1741. //
  1742.   i = COM_CheckParm ("-basedir");
  1743.   if (i && i < com_argc-1)
  1744.     strcpy (basedir, com_argv[i+1]);
  1745.   else
  1746.     strcpy (basedir, host_parms.basedir);
  1747.  
  1748.   j = strlen (basedir);
  1749.  
  1750.   if (j > 0)
  1751.   {
  1752.     if ((basedir[j-1] == '\\') || (basedir[j-1] == '/'))
  1753.       basedir[j-1] = 0;
  1754.   }
  1755.  
  1756. //
  1757. // -cachedir <path>
  1758. // Overrides the system supplied cache directory (NULL or /qcache)
  1759. // -cachedir - will disable caching.
  1760. //
  1761.   i = COM_CheckParm ("-cachedir");
  1762.   if (i && i < com_argc-1)
  1763.   {
  1764.     if (com_argv[i+1][0] == '-')
  1765.       com_cachedir[0] = 0;
  1766.     else
  1767.       strcpy (com_cachedir, com_argv[i+1]);
  1768.   }
  1769.   else if (host_parms.cachedir)
  1770.     strcpy (com_cachedir, host_parms.cachedir);
  1771.   else
  1772.     com_cachedir[0] = 0;
  1773.  
  1774. //
  1775. // start up with GAMENAME by default (id1)
  1776. //
  1777.   COM_AddGameDirectory (va("%s/"GAMENAME, basedir) );
  1778.  
  1779.   if (COM_CheckParm ("-rogue"))
  1780.     COM_AddGameDirectory (va("%s/rogue", basedir) );
  1781.   if (COM_CheckParm ("-hipnotic"))
  1782.     COM_AddGameDirectory (va("%s/hipnotic", basedir) );
  1783.  
  1784. //
  1785. // -game <gamedir>
  1786. // Adds basedir/gamedir as an override game
  1787. //
  1788.   i = COM_CheckParm ("-game");
  1789.   if (i && i < com_argc-1)
  1790.   {
  1791.     com_modified = true;
  1792.     COM_AddGameDirectory (va("%s/%s", basedir, com_argv[i+1]));
  1793.   }
  1794.  
  1795. //
  1796. // -path <dir or packfile> [<dir or packfile>] ...
  1797. // Fully specifies the exact serach path, overriding the generated one
  1798. //
  1799.   i = COM_CheckParm ("-path");
  1800.   if (i)
  1801.   {
  1802.     com_modified = true;
  1803.     com_searchpaths = NULL;
  1804.     while (++i < com_argc)
  1805.     {
  1806.       if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
  1807.         break;
  1808.       
  1809.       search = Hunk_Alloc (sizeof(searchpath_t));
  1810.       if ( !strcmp(COM_FileExtension(com_argv[i]), "pak") )
  1811.       {
  1812.         search->pack = COM_LoadPackFile (com_argv[i]);
  1813.         if (!search->pack)
  1814.           Sys_Error ("Couldn't load packfile: %s", com_argv[i]);
  1815.       }
  1816.       else
  1817.         strcpy (search->filename, com_argv[i]);
  1818.       search->next = com_searchpaths;
  1819.       com_searchpaths = search;
  1820.     }
  1821.   }
  1822.  
  1823.   if (COM_CheckParm ("-proghack"))
  1824.     proghack = true;
  1825. }
  1826.  
  1827.  
  1828.