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