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