common.c 34 KB

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