common.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  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 <ctype.h>
  17. #ifdef SERVERONLY
  18. #include "qwsvdef.h"
  19. #else
  20. #include "quakedef.h"
  21. #endif
  22. #define MAX_NUM_ARGVS 50
  23. #define NUM_SAFE_ARGVS 6
  24. usercmd_t nullcmd; // guarenteed to be zero
  25. static char *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1];
  26. static char *argvdummy = " ";
  27. static char *safeargvs[NUM_SAFE_ARGVS] =
  28. {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse"};
  29. cvar_t registered = {"registered","0"};
  30. qboolean com_modified; // set true if using non-id files
  31. int static_registered = 1; // only for startup check, then set
  32. qboolean msg_suppress_1 = 0;
  33. void COM_InitFilesystem (void);
  34. void COM_Path_f (void);
  35. // if a packfile directory differs from this, it is assumed to be hacked
  36. #define PAK0_COUNT 339
  37. #define PAK0_CRC 52883
  38. qboolean standard_quake = true, rogue, hipnotic;
  39. char gamedirfile[MAX_OSPATH];
  40. // this graphic needs to be in the pak file to use registered features
  41. unsigned short pop[] =
  42. {
  43. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
  44. ,0x0000,0x0000,0x6600,0x0000,0x0000,0x0000,0x6600,0x0000
  45. ,0x0000,0x0066,0x0000,0x0000,0x0000,0x0000,0x0067,0x0000
  46. ,0x0000,0x6665,0x0000,0x0000,0x0000,0x0000,0x0065,0x6600
  47. ,0x0063,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6563
  48. ,0x0064,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6564
  49. ,0x0064,0x6564,0x0000,0x6469,0x6969,0x6400,0x0064,0x6564
  50. ,0x0063,0x6568,0x6200,0x0064,0x6864,0x0000,0x6268,0x6563
  51. ,0x0000,0x6567,0x6963,0x0064,0x6764,0x0063,0x6967,0x6500
  52. ,0x0000,0x6266,0x6769,0x6a68,0x6768,0x6a69,0x6766,0x6200
  53. ,0x0000,0x0062,0x6566,0x6666,0x6666,0x6666,0x6562,0x0000
  54. ,0x0000,0x0000,0x0062,0x6364,0x6664,0x6362,0x0000,0x0000
  55. ,0x0000,0x0000,0x0000,0x0062,0x6662,0x0000,0x0000,0x0000
  56. ,0x0000,0x0000,0x0000,0x0061,0x6661,0x0000,0x0000,0x0000
  57. ,0x0000,0x0000,0x0000,0x0000,0x6500,0x0000,0x0000,0x0000
  58. ,0x0000,0x0000,0x0000,0x0000,0x6400,0x0000,0x0000,0x0000
  59. };
  60. /*
  61. 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.
  62. 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
  63. only used during filesystem initialization.
  64. 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.
  65. The "cache directory" is only used during development to save network bandwidth, especially over ISDN / T1 lines. If there is a cache directory
  66. specified, when a file is found by the normal search path, it will be mirrored
  67. into the cache directory, then opened there.
  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. #if 0
  100. void Q_memset (void *dest, int fill, int count)
  101. {
  102. int i;
  103. if ( (((long)dest | count) & 3) == 0)
  104. {
  105. count >>= 2;
  106. fill = fill | (fill<<8) | (fill<<16) | (fill<<24);
  107. for (i=0 ; i<count ; i++)
  108. ((int *)dest)[i] = fill;
  109. }
  110. else
  111. for (i=0 ; i<count ; i++)
  112. ((byte *)dest)[i] = fill;
  113. }
  114. void Q_memcpy (void *dest, void *src, int count)
  115. {
  116. int i;
  117. if (( ( (long)dest | (long)src | count) & 3) == 0 )
  118. {
  119. count>>=2;
  120. for (i=0 ; i<count ; i++)
  121. ((int *)dest)[i] = ((int *)src)[i];
  122. }
  123. else
  124. for (i=0 ; i<count ; i++)
  125. ((byte *)dest)[i] = ((byte *)src)[i];
  126. }
  127. int Q_memcmp (void *m1, void *m2, int count)
  128. {
  129. while(count)
  130. {
  131. count--;
  132. if (((byte *)m1)[count] != ((byte *)m2)[count])
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. void Q_strcpy (char *dest, char *src)
  138. {
  139. while (*src)
  140. {
  141. *dest++ = *src++;
  142. }
  143. *dest++ = 0;
  144. }
  145. void Q_strncpy (char *dest, char *src, int count)
  146. {
  147. while (*src && count--)
  148. {
  149. *dest++ = *src++;
  150. }
  151. if (count)
  152. *dest++ = 0;
  153. }
  154. int Q_strlen (char *str)
  155. {
  156. int count;
  157. count = 0;
  158. while (str[count])
  159. count++;
  160. return count;
  161. }
  162. char *Q_strrchr(char *s, char c)
  163. {
  164. int len = Q_strlen(s);
  165. s += len;
  166. while (len--)
  167. if (*--s == c) return s;
  168. return 0;
  169. }
  170. void Q_strcat (char *dest, char *src)
  171. {
  172. dest += Q_strlen(dest);
  173. Q_strcpy (dest, src);
  174. }
  175. int Q_strcmp (char *s1, char *s2)
  176. {
  177. while (1)
  178. {
  179. if (*s1 != *s2)
  180. return -1; // strings not equal
  181. if (!*s1)
  182. return 0; // strings are equal
  183. s1++;
  184. s2++;
  185. }
  186. return -1;
  187. }
  188. int Q_strncmp (char *s1, char *s2, int count)
  189. {
  190. while (1)
  191. {
  192. if (!count--)
  193. return 0;
  194. if (*s1 != *s2)
  195. return -1; // strings not equal
  196. if (!*s1)
  197. return 0; // strings are equal
  198. s1++;
  199. s2++;
  200. }
  201. return -1;
  202. }
  203. int Q_strncasecmp (char *s1, char *s2, int n)
  204. {
  205. int c1, c2;
  206. while (1)
  207. {
  208. c1 = *s1++;
  209. c2 = *s2++;
  210. if (!n--)
  211. return 0; // strings are equal until end point
  212. if (c1 != c2)
  213. {
  214. if (c1 >= 'a' && c1 <= 'z')
  215. c1 -= ('a' - 'A');
  216. if (c2 >= 'a' && c2 <= 'z')
  217. c2 -= ('a' - 'A');
  218. if (c1 != c2)
  219. return -1; // strings not equal
  220. }
  221. if (!c1)
  222. return 0; // strings are equal
  223. // s1++;
  224. // s2++;
  225. }
  226. return -1;
  227. }
  228. int Q_strcasecmp (char *s1, char *s2)
  229. {
  230. return Q_strncasecmp (s1, s2, 99999);
  231. }
  232. #endif
  233. int Q_atoi (char *str)
  234. {
  235. int val;
  236. int sign;
  237. int c;
  238. if (*str == '-')
  239. {
  240. sign = -1;
  241. str++;
  242. }
  243. else
  244. sign = 1;
  245. val = 0;
  246. //
  247. // check for hex
  248. //
  249. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  250. {
  251. str += 2;
  252. while (1)
  253. {
  254. c = *str++;
  255. if (c >= '0' && c <= '9')
  256. val = (val<<4) + c - '0';
  257. else if (c >= 'a' && c <= 'f')
  258. val = (val<<4) + c - 'a' + 10;
  259. else if (c >= 'A' && c <= 'F')
  260. val = (val<<4) + c - 'A' + 10;
  261. else
  262. return val*sign;
  263. }
  264. }
  265. //
  266. // check for character
  267. //
  268. if (str[0] == '\'')
  269. {
  270. return sign * str[1];
  271. }
  272. //
  273. // assume decimal
  274. //
  275. while (1)
  276. {
  277. c = *str++;
  278. if (c <'0' || c > '9')
  279. return val*sign;
  280. val = val*10 + c - '0';
  281. }
  282. return 0;
  283. }
  284. float Q_atof (char *str)
  285. {
  286. double val;
  287. int sign;
  288. int c;
  289. int decimal, total;
  290. if (*str == '-')
  291. {
  292. sign = -1;
  293. str++;
  294. }
  295. else
  296. sign = 1;
  297. val = 0;
  298. //
  299. // check for hex
  300. //
  301. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  302. {
  303. str += 2;
  304. while (1)
  305. {
  306. c = *str++;
  307. if (c >= '0' && c <= '9')
  308. val = (val*16) + c - '0';
  309. else if (c >= 'a' && c <= 'f')
  310. val = (val*16) + c - 'a' + 10;
  311. else if (c >= 'A' && c <= 'F')
  312. val = (val*16) + c - 'A' + 10;
  313. else
  314. return val*sign;
  315. }
  316. }
  317. //
  318. // check for character
  319. //
  320. if (str[0] == '\'')
  321. {
  322. return sign * str[1];
  323. }
  324. //
  325. // assume decimal
  326. //
  327. decimal = -1;
  328. total = 0;
  329. while (1)
  330. {
  331. c = *str++;
  332. if (c == '.')
  333. {
  334. decimal = total;
  335. continue;
  336. }
  337. if (c <'0' || c > '9')
  338. break;
  339. val = val*10 + c - '0';
  340. total++;
  341. }
  342. if (decimal == -1)
  343. return val*sign;
  344. while (total > decimal)
  345. {
  346. val /= 10;
  347. total--;
  348. }
  349. return val*sign;
  350. }
  351. /*
  352. ============================================================================
  353. BYTE ORDER FUNCTIONS
  354. ============================================================================
  355. */
  356. qboolean bigendien;
  357. short (*BigShort) (short l);
  358. short (*LittleShort) (short l);
  359. int (*BigLong) (int l);
  360. int (*LittleLong) (int l);
  361. float (*BigFloat) (float l);
  362. float (*LittleFloat) (float l);
  363. short ShortSwap (short l)
  364. {
  365. byte b1,b2;
  366. b1 = l&255;
  367. b2 = (l>>8)&255;
  368. return (b1<<8) + b2;
  369. }
  370. short ShortNoSwap (short l)
  371. {
  372. return l;
  373. }
  374. int LongSwap (int l)
  375. {
  376. byte b1,b2,b3,b4;
  377. b1 = l&255;
  378. b2 = (l>>8)&255;
  379. b3 = (l>>16)&255;
  380. b4 = (l>>24)&255;
  381. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  382. }
  383. int LongNoSwap (int l)
  384. {
  385. return l;
  386. }
  387. float FloatSwap (float f)
  388. {
  389. union
  390. {
  391. float f;
  392. byte b[4];
  393. } dat1, dat2;
  394. dat1.f = f;
  395. dat2.b[0] = dat1.b[3];
  396. dat2.b[1] = dat1.b[2];
  397. dat2.b[2] = dat1.b[1];
  398. dat2.b[3] = dat1.b[0];
  399. return dat2.f;
  400. }
  401. float FloatNoSwap (float f)
  402. {
  403. return f;
  404. }
  405. /*
  406. ==============================================================================
  407. MESSAGE IO FUNCTIONS
  408. Handles byte ordering and avoids alignment errors
  409. ==============================================================================
  410. */
  411. //
  412. // writing functions
  413. //
  414. void MSG_WriteChar (sizebuf_t *sb, int c)
  415. {
  416. byte *buf;
  417. #ifdef PARANOID
  418. if (c < -128 || c > 127)
  419. Sys_Error ("MSG_WriteChar: range error");
  420. #endif
  421. buf = SZ_GetSpace (sb, 1);
  422. buf[0] = c;
  423. }
  424. void MSG_WriteByte (sizebuf_t *sb, int c)
  425. {
  426. byte *buf;
  427. #ifdef PARANOID
  428. if (c < 0 || c > 255)
  429. Sys_Error ("MSG_WriteByte: range error");
  430. #endif
  431. buf = SZ_GetSpace (sb, 1);
  432. buf[0] = c;
  433. }
  434. void MSG_WriteShort (sizebuf_t *sb, int c)
  435. {
  436. byte *buf;
  437. #ifdef PARANOID
  438. if (c < ((short)0x8000) || c > (short)0x7fff)
  439. Sys_Error ("MSG_WriteShort: range error");
  440. #endif
  441. buf = SZ_GetSpace (sb, 2);
  442. buf[0] = c&0xff;
  443. buf[1] = c>>8;
  444. }
  445. void MSG_WriteLong (sizebuf_t *sb, int c)
  446. {
  447. byte *buf;
  448. buf = SZ_GetSpace (sb, 4);
  449. buf[0] = c&0xff;
  450. buf[1] = (c>>8)&0xff;
  451. buf[2] = (c>>16)&0xff;
  452. buf[3] = c>>24;
  453. }
  454. void MSG_WriteFloat (sizebuf_t *sb, float f)
  455. {
  456. union
  457. {
  458. float f;
  459. int l;
  460. } dat;
  461. dat.f = f;
  462. dat.l = LittleLong (dat.l);
  463. SZ_Write (sb, &dat.l, 4);
  464. }
  465. void MSG_WriteString (sizebuf_t *sb, char *s)
  466. {
  467. if (!s)
  468. SZ_Write (sb, "", 1);
  469. else
  470. SZ_Write (sb, s, Q_strlen(s)+1);
  471. }
  472. void MSG_WriteCoord (sizebuf_t *sb, float f)
  473. {
  474. MSG_WriteShort (sb, (int)(f*8));
  475. }
  476. void MSG_WriteAngle (sizebuf_t *sb, float f)
  477. {
  478. MSG_WriteByte (sb, (int)(f*256/360) & 255);
  479. }
  480. void MSG_WriteAngle16 (sizebuf_t *sb, float f)
  481. {
  482. MSG_WriteShort (sb, (int)(f*65536/360) & 65535);
  483. }
  484. void MSG_WriteDeltaUsercmd (sizebuf_t *buf, usercmd_t *from, usercmd_t *cmd)
  485. {
  486. int bits;
  487. //
  488. // send the movement message
  489. //
  490. bits = 0;
  491. if (cmd->angles[0] != from->angles[0])
  492. bits |= CM_ANGLE1;
  493. if (cmd->angles[1] != from->angles[1])
  494. bits |= CM_ANGLE2;
  495. if (cmd->angles[2] != from->angles[2])
  496. bits |= CM_ANGLE3;
  497. if (cmd->forwardmove != from->forwardmove)
  498. bits |= CM_FORWARD;
  499. if (cmd->sidemove != from->sidemove)
  500. bits |= CM_SIDE;
  501. if (cmd->upmove != from->upmove)
  502. bits |= CM_UP;
  503. if (cmd->buttons != from->buttons)
  504. bits |= CM_BUTTONS;
  505. if (cmd->impulse != from->impulse)
  506. bits |= CM_IMPULSE;
  507. MSG_WriteByte (buf, bits);
  508. if (bits & CM_ANGLE1)
  509. MSG_WriteAngle16 (buf, cmd->angles[0]);
  510. if (bits & CM_ANGLE2)
  511. MSG_WriteAngle16 (buf, cmd->angles[1]);
  512. if (bits & CM_ANGLE3)
  513. MSG_WriteAngle16 (buf, cmd->angles[2]);
  514. if (bits & CM_FORWARD)
  515. MSG_WriteShort (buf, cmd->forwardmove);
  516. if (bits & CM_SIDE)
  517. MSG_WriteShort (buf, cmd->sidemove);
  518. if (bits & CM_UP)
  519. MSG_WriteShort (buf, cmd->upmove);
  520. if (bits & CM_BUTTONS)
  521. MSG_WriteByte (buf, cmd->buttons);
  522. if (bits & CM_IMPULSE)
  523. MSG_WriteByte (buf, cmd->impulse);
  524. MSG_WriteByte (buf, cmd->msec);
  525. }
  526. //
  527. // reading functions
  528. //
  529. int msg_readcount;
  530. qboolean msg_badread;
  531. void MSG_BeginReading (void)
  532. {
  533. msg_readcount = 0;
  534. msg_badread = false;
  535. }
  536. int MSG_GetReadCount(void)
  537. {
  538. return msg_readcount;
  539. }
  540. // returns -1 and sets msg_badread if no more characters are available
  541. int MSG_ReadChar (void)
  542. {
  543. int c;
  544. if (msg_readcount+1 > net_message.cursize)
  545. {
  546. msg_badread = true;
  547. return -1;
  548. }
  549. c = (signed char)net_message.data[msg_readcount];
  550. msg_readcount++;
  551. return c;
  552. }
  553. int MSG_ReadByte (void)
  554. {
  555. int c;
  556. if (msg_readcount+1 > net_message.cursize)
  557. {
  558. msg_badread = true;
  559. return -1;
  560. }
  561. c = (unsigned char)net_message.data[msg_readcount];
  562. msg_readcount++;
  563. return c;
  564. }
  565. int MSG_ReadShort (void)
  566. {
  567. int c;
  568. if (msg_readcount+2 > net_message.cursize)
  569. {
  570. msg_badread = true;
  571. return -1;
  572. }
  573. c = (short)(net_message.data[msg_readcount]
  574. + (net_message.data[msg_readcount+1]<<8));
  575. msg_readcount += 2;
  576. return c;
  577. }
  578. int MSG_ReadLong (void)
  579. {
  580. int c;
  581. if (msg_readcount+4 > net_message.cursize)
  582. {
  583. msg_badread = true;
  584. return -1;
  585. }
  586. c = net_message.data[msg_readcount]
  587. + (net_message.data[msg_readcount+1]<<8)
  588. + (net_message.data[msg_readcount+2]<<16)
  589. + (net_message.data[msg_readcount+3]<<24);
  590. msg_readcount += 4;
  591. return c;
  592. }
  593. float MSG_ReadFloat (void)
  594. {
  595. union
  596. {
  597. byte b[4];
  598. float f;
  599. int l;
  600. } dat;
  601. dat.b[0] = net_message.data[msg_readcount];
  602. dat.b[1] = net_message.data[msg_readcount+1];
  603. dat.b[2] = net_message.data[msg_readcount+2];
  604. dat.b[3] = net_message.data[msg_readcount+3];
  605. msg_readcount += 4;
  606. dat.l = LittleLong (dat.l);
  607. return dat.f;
  608. }
  609. char *MSG_ReadString (void)
  610. {
  611. static char string[2048];
  612. int l,c;
  613. l = 0;
  614. do
  615. {
  616. c = MSG_ReadChar ();
  617. if (c == -1 || c == 0)
  618. break;
  619. string[l] = c;
  620. l++;
  621. } while (l < sizeof(string)-1);
  622. string[l] = 0;
  623. return string;
  624. }
  625. char *MSG_ReadStringLine (void)
  626. {
  627. static char string[2048];
  628. int l,c;
  629. l = 0;
  630. do
  631. {
  632. c = MSG_ReadChar ();
  633. if (c == -1 || c == 0 || c == '\n')
  634. break;
  635. string[l] = c;
  636. l++;
  637. } while (l < sizeof(string)-1);
  638. string[l] = 0;
  639. return string;
  640. }
  641. float MSG_ReadCoord (void)
  642. {
  643. return MSG_ReadShort() * (1.0/8);
  644. }
  645. float MSG_ReadAngle (void)
  646. {
  647. return MSG_ReadChar() * (360.0/256);
  648. }
  649. float MSG_ReadAngle16 (void)
  650. {
  651. return MSG_ReadShort() * (360.0/65536);
  652. }
  653. void MSG_ReadDeltaUsercmd (usercmd_t *from, usercmd_t *move)
  654. {
  655. int bits;
  656. memcpy (move, from, sizeof(*move));
  657. bits = MSG_ReadByte ();
  658. // read current angles
  659. if (bits & CM_ANGLE1)
  660. move->angles[0] = MSG_ReadAngle16 ();
  661. if (bits & CM_ANGLE2)
  662. move->angles[1] = MSG_ReadAngle16 ();
  663. if (bits & CM_ANGLE3)
  664. move->angles[2] = MSG_ReadAngle16 ();
  665. // read movement
  666. if (bits & CM_FORWARD)
  667. move->forwardmove = MSG_ReadShort ();
  668. if (bits & CM_SIDE)
  669. move->sidemove = MSG_ReadShort ();
  670. if (bits & CM_UP)
  671. move->upmove = MSG_ReadShort ();
  672. // read buttons
  673. if (bits & CM_BUTTONS)
  674. move->buttons = MSG_ReadByte ();
  675. if (bits & CM_IMPULSE)
  676. move->impulse = MSG_ReadByte ();
  677. // read time to run command
  678. move->msec = MSG_ReadByte ();
  679. }
  680. //===========================================================================
  681. void SZ_Clear (sizebuf_t *buf)
  682. {
  683. buf->cursize = 0;
  684. buf->overflowed = false;
  685. }
  686. void *SZ_GetSpace (sizebuf_t *buf, int length)
  687. {
  688. void *data;
  689. if (buf->cursize + length > buf->maxsize)
  690. {
  691. if (!buf->allowoverflow)
  692. Sys_Error ("SZ_GetSpace: overflow without allowoverflow set (%d)", buf->maxsize);
  693. if (length > buf->maxsize)
  694. Sys_Error ("SZ_GetSpace: %i is > full buffer size", length);
  695. Sys_Printf ("SZ_GetSpace: overflow\n"); // because Con_Printf may be redirected
  696. SZ_Clear (buf);
  697. buf->overflowed = true;
  698. }
  699. data = buf->data + buf->cursize;
  700. buf->cursize += length;
  701. return data;
  702. }
  703. void SZ_Write (sizebuf_t *buf, void *data, int length)
  704. {
  705. Q_memcpy (SZ_GetSpace(buf,length),data,length);
  706. }
  707. void SZ_Print (sizebuf_t *buf, char *data)
  708. {
  709. int len;
  710. len = Q_strlen(data)+1;
  711. if (!buf->cursize || buf->data[buf->cursize-1])
  712. Q_memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
  713. else
  714. Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
  715. }
  716. //============================================================================
  717. /*
  718. ============
  719. COM_SkipPath
  720. ============
  721. */
  722. char *COM_SkipPath (char *pathname)
  723. {
  724. char *last;
  725. last = pathname;
  726. while (*pathname)
  727. {
  728. if (*pathname=='/')
  729. last = pathname+1;
  730. pathname++;
  731. }
  732. return last;
  733. }
  734. /*
  735. ============
  736. COM_StripExtension
  737. ============
  738. */
  739. void COM_StripExtension (char *in, char *out)
  740. {
  741. while (*in && *in != '.')
  742. *out++ = *in++;
  743. *out = 0;
  744. }
  745. /*
  746. ============
  747. COM_FileExtension
  748. ============
  749. */
  750. char *COM_FileExtension (char *in)
  751. {
  752. static char exten[8];
  753. int i;
  754. while (*in && *in != '.')
  755. in++;
  756. if (!*in)
  757. return "";
  758. in++;
  759. for (i=0 ; i<7 && *in ; i++,in++)
  760. exten[i] = *in;
  761. exten[i] = 0;
  762. return exten;
  763. }
  764. /*
  765. ============
  766. COM_FileBase
  767. ============
  768. */
  769. void COM_FileBase (char *in, char *out)
  770. {
  771. char *s, *s2;
  772. s = in + strlen(in) - 1;
  773. while (s != in && *s != '.')
  774. s--;
  775. for (s2 = s ; *s2 && *s2 != '/' ; s2--)
  776. ;
  777. if (s-s2 < 2)
  778. strcpy (out,"?model?");
  779. else
  780. {
  781. s--;
  782. strncpy (out,s2+1, s-s2);
  783. out[s-s2] = 0;
  784. }
  785. }
  786. /*
  787. ==================
  788. COM_DefaultExtension
  789. ==================
  790. */
  791. void COM_DefaultExtension (char *path, char *extension)
  792. {
  793. char *src;
  794. //
  795. // if path doesn't have a .EXT, append extension
  796. // (extension should include the .)
  797. //
  798. src = path + strlen(path) - 1;
  799. while (*src != '/' && src != path)
  800. {
  801. if (*src == '.')
  802. return; // it has an extension
  803. src--;
  804. }
  805. strcat (path, extension);
  806. }
  807. //============================================================================
  808. char com_token[1024];
  809. int com_argc;
  810. char **com_argv;
  811. /*
  812. ==============
  813. COM_Parse
  814. Parse a token out of a string
  815. ==============
  816. */
  817. char *COM_Parse (char *data)
  818. {
  819. int c;
  820. int len;
  821. len = 0;
  822. com_token[0] = 0;
  823. if (!data)
  824. return NULL;
  825. // skip whitespace
  826. skipwhite:
  827. while ( (c = *data) <= ' ')
  828. {
  829. if (c == 0)
  830. return NULL; // end of file;
  831. data++;
  832. }
  833. // skip // comments
  834. if (c=='/' && data[1] == '/')
  835. {
  836. while (*data && *data != '\n')
  837. data++;
  838. goto skipwhite;
  839. }
  840. // handle quoted strings specially
  841. if (c == '\"')
  842. {
  843. data++;
  844. while (1)
  845. {
  846. c = *data++;
  847. if (c=='\"' || !c)
  848. {
  849. com_token[len] = 0;
  850. return data;
  851. }
  852. com_token[len] = c;
  853. len++;
  854. }
  855. }
  856. // parse a regular word
  857. do
  858. {
  859. com_token[len] = c;
  860. data++;
  861. len++;
  862. c = *data;
  863. } while (c>32);
  864. com_token[len] = 0;
  865. return data;
  866. }
  867. /*
  868. ================
  869. COM_CheckParm
  870. Returns the position (1 to argc-1) in the program's argument list
  871. where the given parameter apears, or 0 if not present
  872. ================
  873. */
  874. int COM_CheckParm (char *parm)
  875. {
  876. int i;
  877. for (i=1 ; i<com_argc ; i++)
  878. {
  879. if (!com_argv[i])
  880. continue; // NEXTSTEP sometimes clears appkit vars.
  881. if (!Q_strcmp (parm,com_argv[i]))
  882. return i;
  883. }
  884. return 0;
  885. }
  886. /*
  887. ================
  888. COM_CheckRegistered
  889. Looks for the pop.txt file and verifies it.
  890. Sets the "registered" cvar.
  891. Immediately exits out if an alternate game was attempted to be started without
  892. being registered.
  893. ================
  894. */
  895. void COM_CheckRegistered (void)
  896. {
  897. FILE *h;
  898. unsigned short check[128];
  899. int i;
  900. COM_FOpenFile("gfx/pop.lmp", &h);
  901. static_registered = 0;
  902. if (!h)
  903. {
  904. Con_Printf ("Playing shareware version.\n");
  905. #ifndef SERVERONLY
  906. // FIXME DEBUG -- only temporary
  907. if (com_modified)
  908. Sys_Error ("You must have the registered version to play QuakeWorld");
  909. #endif
  910. return;
  911. }
  912. fread (check, 1, sizeof(check), h);
  913. fclose (h);
  914. for (i=0 ; i<128 ; i++)
  915. if (pop[i] != (unsigned short)BigShort (check[i]))
  916. Sys_Error ("Corrupted data file.");
  917. Cvar_Set ("registered", "1");
  918. static_registered = 1;
  919. Con_Printf ("Playing registered version.\n");
  920. }
  921. /*
  922. ================
  923. COM_InitArgv
  924. ================
  925. */
  926. void COM_InitArgv (int argc, char **argv)
  927. {
  928. qboolean safe;
  929. int i;
  930. safe = false;
  931. for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
  932. com_argc++)
  933. {
  934. largv[com_argc] = argv[com_argc];
  935. if (!Q_strcmp ("-safe", argv[com_argc]))
  936. safe = true;
  937. }
  938. if (safe)
  939. {
  940. // force all the safe-mode switches. Note that we reserved extra space in
  941. // case we need to add these, so we don't need an overflow check
  942. for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
  943. {
  944. largv[com_argc] = safeargvs[i];
  945. com_argc++;
  946. }
  947. }
  948. largv[com_argc] = argvdummy;
  949. com_argv = largv;
  950. }
  951. /*
  952. ================
  953. COM_AddParm
  954. Adds the given string at the end of the current argument list
  955. ================
  956. */
  957. void COM_AddParm (char *parm)
  958. {
  959. largv[com_argc++] = parm;
  960. }
  961. /*
  962. ================
  963. COM_Init
  964. ================
  965. */
  966. void COM_Init (void)
  967. {
  968. byte swaptest[2] = {1,0};
  969. // set the byte swapping variables in a portable manner
  970. if ( *(short *)swaptest == 1)
  971. {
  972. bigendien = false;
  973. BigShort = ShortSwap;
  974. LittleShort = ShortNoSwap;
  975. BigLong = LongSwap;
  976. LittleLong = LongNoSwap;
  977. BigFloat = FloatSwap;
  978. LittleFloat = FloatNoSwap;
  979. }
  980. else
  981. {
  982. bigendien = true;
  983. BigShort = ShortNoSwap;
  984. LittleShort = ShortSwap;
  985. BigLong = LongNoSwap;
  986. LittleLong = LongSwap;
  987. BigFloat = FloatNoSwap;
  988. LittleFloat = FloatSwap;
  989. }
  990. Cvar_RegisterVariable (&registered);
  991. Cmd_AddCommand ("path", COM_Path_f);
  992. COM_InitFilesystem ();
  993. COM_CheckRegistered ();
  994. }
  995. /*
  996. ============
  997. va
  998. does a varargs printf into a temp buffer, so I don't need to have
  999. varargs versions of all text functions.
  1000. FIXME: make this buffer size safe someday
  1001. ============
  1002. */
  1003. char *va(char *format, ...)
  1004. {
  1005. va_list argptr;
  1006. static char string[1024];
  1007. va_start (argptr, format);
  1008. vsprintf (string, format,argptr);
  1009. va_end (argptr);
  1010. return string;
  1011. }
  1012. /// just for debugging
  1013. int memsearch (byte *start, int count, int search)
  1014. {
  1015. int i;
  1016. for (i=0 ; i<count ; i++)
  1017. if (start[i] == search)
  1018. return i;
  1019. return -1;
  1020. }
  1021. /*
  1022. =============================================================================
  1023. QUAKE FILESYSTEM
  1024. =============================================================================
  1025. */
  1026. int com_filesize;
  1027. //
  1028. // in memory
  1029. //
  1030. typedef struct
  1031. {
  1032. char name[MAX_QPATH];
  1033. int filepos, filelen;
  1034. } packfile_t;
  1035. typedef struct pack_s
  1036. {
  1037. char filename[MAX_OSPATH];
  1038. FILE *handle;
  1039. int numfiles;
  1040. packfile_t *files;
  1041. } pack_t;
  1042. //
  1043. // on disk
  1044. //
  1045. typedef struct
  1046. {
  1047. char name[56];
  1048. int filepos, filelen;
  1049. } dpackfile_t;
  1050. typedef struct
  1051. {
  1052. char id[4];
  1053. int dirofs;
  1054. int dirlen;
  1055. } dpackheader_t;
  1056. #define MAX_FILES_IN_PACK 2048
  1057. char com_gamedir[MAX_OSPATH];
  1058. char com_basedir[MAX_OSPATH];
  1059. typedef struct searchpath_s
  1060. {
  1061. char filename[MAX_OSPATH];
  1062. pack_t *pack; // only one of filename / pack will be used
  1063. struct searchpath_s *next;
  1064. } searchpath_t;
  1065. searchpath_t *com_searchpaths;
  1066. searchpath_t *com_base_searchpaths; // without gamedirs
  1067. /*
  1068. ================
  1069. COM_filelength
  1070. ================
  1071. */
  1072. int COM_filelength (FILE *f)
  1073. {
  1074. int pos;
  1075. int end;
  1076. pos = ftell (f);
  1077. fseek (f, 0, SEEK_END);
  1078. end = ftell (f);
  1079. fseek (f, pos, SEEK_SET);
  1080. return end;
  1081. }
  1082. int COM_FileOpenRead (char *path, FILE **hndl)
  1083. {
  1084. FILE *f;
  1085. f = fopen(path, "rb");
  1086. if (!f)
  1087. {
  1088. *hndl = NULL;
  1089. return -1;
  1090. }
  1091. *hndl = f;
  1092. return COM_filelength(f);
  1093. }
  1094. /*
  1095. ============
  1096. COM_Path_f
  1097. ============
  1098. */
  1099. void COM_Path_f (void)
  1100. {
  1101. searchpath_t *s;
  1102. Con_Printf ("Current search path:\n");
  1103. for (s=com_searchpaths ; s ; s=s->next)
  1104. {
  1105. if (s == com_base_searchpaths)
  1106. Con_Printf ("----------\n");
  1107. if (s->pack)
  1108. Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
  1109. else
  1110. Con_Printf ("%s\n", s->filename);
  1111. }
  1112. }
  1113. /*
  1114. ============
  1115. COM_WriteFile
  1116. The filename will be prefixed by the current game directory
  1117. ============
  1118. */
  1119. void COM_WriteFile (char *filename, void *data, int len)
  1120. {
  1121. FILE *f;
  1122. char name[MAX_OSPATH];
  1123. sprintf (name, "%s/%s", com_gamedir, filename);
  1124. f = fopen (name, "wb");
  1125. if (!f) {
  1126. Sys_mkdir(com_gamedir);
  1127. f = fopen (name, "wb");
  1128. if (!f)
  1129. Sys_Error ("Error opening %s", filename);
  1130. }
  1131. Sys_Printf ("COM_WriteFile: %s\n", name);
  1132. fwrite (data, 1, len, f);
  1133. fclose (f);
  1134. }
  1135. /*
  1136. ============
  1137. COM_CreatePath
  1138. Only used for CopyFile and download
  1139. ============
  1140. */
  1141. void COM_CreatePath (char *path)
  1142. {
  1143. char *ofs;
  1144. for (ofs = path+1 ; *ofs ; ofs++)
  1145. {
  1146. if (*ofs == '/')
  1147. { // create the directory
  1148. *ofs = 0;
  1149. Sys_mkdir (path);
  1150. *ofs = '/';
  1151. }
  1152. }
  1153. }
  1154. /*
  1155. ===========
  1156. COM_CopyFile
  1157. Copies a file over from the net to the local cache, creating any directories
  1158. needed. This is for the convenience of developers using ISDN from home.
  1159. ===========
  1160. */
  1161. void COM_CopyFile (char *netpath, char *cachepath)
  1162. {
  1163. FILE *in, *out;
  1164. int remaining, count;
  1165. char buf[4096];
  1166. remaining = COM_FileOpenRead (netpath, &in);
  1167. COM_CreatePath (cachepath); // create directories up to the cache file
  1168. out = fopen(cachepath, "wb");
  1169. if (!out)
  1170. Sys_Error ("Error opening %s", cachepath);
  1171. while (remaining)
  1172. {
  1173. if (remaining < sizeof(buf))
  1174. count = remaining;
  1175. else
  1176. count = sizeof(buf);
  1177. fread (buf, 1, count, in);
  1178. fwrite (buf, 1, count, out);
  1179. remaining -= count;
  1180. }
  1181. fclose (in);
  1182. fclose (out);
  1183. }
  1184. /*
  1185. ===========
  1186. COM_FindFile
  1187. Finds the file in the search path.
  1188. Sets com_filesize and one of handle or file
  1189. ===========
  1190. */
  1191. int file_from_pak; // global indicating file came from pack file ZOID
  1192. int COM_FOpenFile (char *filename, FILE **file)
  1193. {
  1194. searchpath_t *search;
  1195. char netpath[MAX_OSPATH];
  1196. pack_t *pak;
  1197. int i;
  1198. int findtime;
  1199. file_from_pak = 0;
  1200. //
  1201. // search through the path, one element at a time
  1202. //
  1203. for (search = com_searchpaths ; search ; search = search->next)
  1204. {
  1205. // is the element a pak file?
  1206. if (search->pack)
  1207. {
  1208. // look through all the pak file elements
  1209. pak = search->pack;
  1210. for (i=0 ; i<pak->numfiles ; i++)
  1211. if (!strcmp (pak->files[i].name, filename))
  1212. { // found it!
  1213. Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename);
  1214. // open a new file on the pakfile
  1215. *file = fopen (pak->filename, "rb");
  1216. if (!*file)
  1217. Sys_Error ("Couldn't reopen %s", pak->filename);
  1218. fseek (*file, pak->files[i].filepos, SEEK_SET);
  1219. com_filesize = pak->files[i].filelen;
  1220. file_from_pak = 1;
  1221. return com_filesize;
  1222. }
  1223. }
  1224. else
  1225. {
  1226. // check a file in the directory tree
  1227. if (!static_registered)
  1228. { // if not a registered version, don't ever go beyond base
  1229. if ( strchr (filename, '/') || strchr (filename,'\\'))
  1230. continue;
  1231. }
  1232. sprintf (netpath, "%s/%s",search->filename, filename);
  1233. findtime = Sys_FileTime (netpath);
  1234. if (findtime == -1)
  1235. continue;
  1236. Sys_Printf ("FindFile: %s\n",netpath);
  1237. *file = fopen (netpath, "rb");
  1238. return COM_filelength (*file);
  1239. }
  1240. }
  1241. Sys_Printf ("FindFile: can't find %s\n", filename);
  1242. *file = NULL;
  1243. com_filesize = -1;
  1244. return -1;
  1245. }
  1246. /*
  1247. ============
  1248. COM_LoadFile
  1249. Filename are reletive to the quake directory.
  1250. Allways appends a 0 byte to the loaded data.
  1251. ============
  1252. */
  1253. cache_user_t *loadcache;
  1254. byte *loadbuf;
  1255. int loadsize;
  1256. byte *COM_LoadFile (char *path, int usehunk)
  1257. {
  1258. FILE *h;
  1259. byte *buf;
  1260. char base[32];
  1261. int len;
  1262. buf = NULL; // quiet compiler warning
  1263. // look for it in the filesystem or pack files
  1264. len = com_filesize = COM_FOpenFile (path, &h);
  1265. if (!h)
  1266. return NULL;
  1267. // extract the filename base name for hunk tag
  1268. COM_FileBase (path, base);
  1269. if (usehunk == 1)
  1270. buf = Hunk_AllocName (len+1, base);
  1271. else if (usehunk == 2)
  1272. buf = Hunk_TempAlloc (len+1);
  1273. else if (usehunk == 0)
  1274. buf = Z_Malloc (len+1);
  1275. else if (usehunk == 3)
  1276. buf = Cache_Alloc (loadcache, len+1, base);
  1277. else if (usehunk == 4)
  1278. {
  1279. if (len+1 > loadsize)
  1280. buf = Hunk_TempAlloc (len+1);
  1281. else
  1282. buf = loadbuf;
  1283. }
  1284. else
  1285. Sys_Error ("COM_LoadFile: bad usehunk");
  1286. if (!buf)
  1287. Sys_Error ("COM_LoadFile: not enough space for %s", path);
  1288. ((byte *)buf)[len] = 0;
  1289. #ifndef SERVERONLY
  1290. Draw_BeginDisc ();
  1291. #endif
  1292. fread (buf, 1, len, h);
  1293. fclose (h);
  1294. #ifndef SERVERONLY
  1295. Draw_EndDisc ();
  1296. #endif
  1297. return buf;
  1298. }
  1299. byte *COM_LoadHunkFile (char *path)
  1300. {
  1301. return COM_LoadFile (path, 1);
  1302. }
  1303. byte *COM_LoadTempFile (char *path)
  1304. {
  1305. return COM_LoadFile (path, 2);
  1306. }
  1307. void COM_LoadCacheFile (char *path, struct cache_user_s *cu)
  1308. {
  1309. loadcache = cu;
  1310. COM_LoadFile (path, 3);
  1311. }
  1312. // uses temp hunk if larger than bufsize
  1313. byte *COM_LoadStackFile (char *path, void *buffer, int bufsize)
  1314. {
  1315. byte *buf;
  1316. loadbuf = (byte *)buffer;
  1317. loadsize = bufsize;
  1318. buf = COM_LoadFile (path, 4);
  1319. return buf;
  1320. }
  1321. /*
  1322. =================
  1323. COM_LoadPackFile
  1324. Takes an explicit (not game tree related) path to a pak file.
  1325. Loads the header and directory, adding the files at the beginning
  1326. of the list so they override previous pack files.
  1327. =================
  1328. */
  1329. pack_t *COM_LoadPackFile (char *packfile)
  1330. {
  1331. dpackheader_t header;
  1332. int i;
  1333. packfile_t *newfiles;
  1334. int numpackfiles;
  1335. pack_t *pack;
  1336. FILE *packhandle;
  1337. dpackfile_t info[MAX_FILES_IN_PACK];
  1338. unsigned short crc;
  1339. if (COM_FileOpenRead (packfile, &packhandle) == -1)
  1340. return NULL;
  1341. fread (&header, 1, sizeof(header), packhandle);
  1342. if (header.id[0] != 'P' || header.id[1] != 'A'
  1343. || header.id[2] != 'C' || header.id[3] != 'K')
  1344. Sys_Error ("%s is not a packfile", packfile);
  1345. header.dirofs = LittleLong (header.dirofs);
  1346. header.dirlen = LittleLong (header.dirlen);
  1347. numpackfiles = header.dirlen / sizeof(dpackfile_t);
  1348. if (numpackfiles > MAX_FILES_IN_PACK)
  1349. Sys_Error ("%s has %i files", packfile, numpackfiles);
  1350. if (numpackfiles != PAK0_COUNT)
  1351. com_modified = true; // not the original file
  1352. newfiles = Z_Malloc (numpackfiles * sizeof(packfile_t));
  1353. fseek (packhandle, header.dirofs, SEEK_SET);
  1354. fread (&info, 1, header.dirlen, packhandle);
  1355. // crc the directory to check for modifications
  1356. crc = CRC_Block((byte *)info, header.dirlen);
  1357. // CRC_Init (&crc);
  1358. // for (i=0 ; i<header.dirlen ; i++)
  1359. // CRC_ProcessByte (&crc, ((byte *)info)[i]);
  1360. if (crc != PAK0_CRC)
  1361. com_modified = true;
  1362. // parse the directory
  1363. for (i=0 ; i<numpackfiles ; i++)
  1364. {
  1365. strcpy (newfiles[i].name, info[i].name);
  1366. newfiles[i].filepos = LittleLong(info[i].filepos);
  1367. newfiles[i].filelen = LittleLong(info[i].filelen);
  1368. }
  1369. pack = Z_Malloc (sizeof (pack_t));
  1370. strcpy (pack->filename, packfile);
  1371. pack->handle = packhandle;
  1372. pack->numfiles = numpackfiles;
  1373. pack->files = newfiles;
  1374. Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
  1375. return pack;
  1376. }
  1377. /*
  1378. ================
  1379. COM_AddGameDirectory
  1380. Sets com_gamedir, adds the directory to the head of the path,
  1381. then loads and adds pak1.pak pak2.pak ...
  1382. ================
  1383. */
  1384. void COM_AddGameDirectory (char *dir)
  1385. {
  1386. int i;
  1387. searchpath_t *search;
  1388. pack_t *pak;
  1389. char pakfile[MAX_OSPATH];
  1390. char *p;
  1391. if ((p = strrchr(dir, '/')) != NULL)
  1392. strcpy(gamedirfile, ++p);
  1393. else
  1394. strcpy(gamedirfile, p);
  1395. strcpy (com_gamedir, dir);
  1396. //
  1397. // add the directory to the search path
  1398. //
  1399. search = Hunk_Alloc (sizeof(searchpath_t));
  1400. strcpy (search->filename, dir);
  1401. search->next = com_searchpaths;
  1402. com_searchpaths = search;
  1403. //
  1404. // add any pak files in the format pak0.pak pak1.pak, ...
  1405. //
  1406. for (i=0 ; ; i++)
  1407. {
  1408. sprintf (pakfile, "%s/pak%i.pak", dir, i);
  1409. pak = COM_LoadPackFile (pakfile);
  1410. if (!pak)
  1411. break;
  1412. search = Hunk_Alloc (sizeof(searchpath_t));
  1413. search->pack = pak;
  1414. search->next = com_searchpaths;
  1415. com_searchpaths = search;
  1416. }
  1417. }
  1418. /*
  1419. ================
  1420. COM_Gamedir
  1421. Sets the gamedir and path to a different directory.
  1422. ================
  1423. */
  1424. void COM_Gamedir (char *dir)
  1425. {
  1426. searchpath_t *search, *next;
  1427. int i;
  1428. pack_t *pak;
  1429. char pakfile[MAX_OSPATH];
  1430. if (strstr(dir, "..") || strstr(dir, "/")
  1431. || strstr(dir, "\\") || strstr(dir, ":") )
  1432. {
  1433. Con_Printf ("Gamedir should be a single filename, not a path\n");
  1434. return;
  1435. }
  1436. if (!strcmp(gamedirfile, dir))
  1437. return; // still the same
  1438. strcpy (gamedirfile, dir);
  1439. //
  1440. // free up any current game dir info
  1441. //
  1442. while (com_searchpaths != com_base_searchpaths)
  1443. {
  1444. if (com_searchpaths->pack)
  1445. {
  1446. fclose (com_searchpaths->pack->handle);
  1447. Z_Free (com_searchpaths->pack->files);
  1448. Z_Free (com_searchpaths->pack);
  1449. }
  1450. next = com_searchpaths->next;
  1451. Z_Free (com_searchpaths);
  1452. com_searchpaths = next;
  1453. }
  1454. //
  1455. // flush all data, so it will be forced to reload
  1456. //
  1457. Cache_Flush ();
  1458. if (!strcmp(dir,"id1") || !strcmp(dir, "qw"))
  1459. return;
  1460. sprintf (com_gamedir, "%s/%s", com_basedir, dir);
  1461. //
  1462. // add the directory to the search path
  1463. //
  1464. search = Z_Malloc (sizeof(searchpath_t));
  1465. strcpy (search->filename, com_gamedir);
  1466. search->next = com_searchpaths;
  1467. com_searchpaths = search;
  1468. //
  1469. // add any pak files in the format pak0.pak pak1.pak, ...
  1470. //
  1471. for (i=0 ; ; i++)
  1472. {
  1473. sprintf (pakfile, "%s/pak%i.pak", com_gamedir, i);
  1474. pak = COM_LoadPackFile (pakfile);
  1475. if (!pak)
  1476. break;
  1477. search = Z_Malloc (sizeof(searchpath_t));
  1478. search->pack = pak;
  1479. search->next = com_searchpaths;
  1480. com_searchpaths = search;
  1481. }
  1482. }
  1483. /*
  1484. ================
  1485. COM_InitFilesystem
  1486. ================
  1487. */
  1488. void COM_InitFilesystem (void)
  1489. {
  1490. int i;
  1491. //
  1492. // -basedir <path>
  1493. // Overrides the system supplied base directory (under id1)
  1494. //
  1495. i = COM_CheckParm ("-basedir");
  1496. if (i && i < com_argc-1)
  1497. strcpy (com_basedir, com_argv[i+1]);
  1498. else
  1499. strcpy (com_basedir, host_parms.basedir);
  1500. //
  1501. // start up with id1 by default
  1502. //
  1503. COM_AddGameDirectory (va("%s/id1", com_basedir) );
  1504. COM_AddGameDirectory (va("%s/qw", com_basedir) );
  1505. // any set gamedirs will be freed up to here
  1506. com_base_searchpaths = com_searchpaths;
  1507. }
  1508. /*
  1509. =====================================================================
  1510. INFO STRINGS
  1511. =====================================================================
  1512. */
  1513. /*
  1514. ===============
  1515. Info_ValueForKey
  1516. Searches the string for the given
  1517. key and returns the associated value, or an empty string.
  1518. ===============
  1519. */
  1520. char *Info_ValueForKey (char *s, char *key)
  1521. {
  1522. char pkey[512];
  1523. static char value[4][512]; // use two buffers so compares
  1524. // work without stomping on each other
  1525. static int valueindex;
  1526. char *o;
  1527. valueindex = (valueindex + 1) % 4;
  1528. if (*s == '\\')
  1529. s++;
  1530. while (1)
  1531. {
  1532. o = pkey;
  1533. while (*s != '\\')
  1534. {
  1535. if (!*s)
  1536. return "";
  1537. *o++ = *s++;
  1538. }
  1539. *o = 0;
  1540. s++;
  1541. o = value[valueindex];
  1542. while (*s != '\\' && *s)
  1543. {
  1544. if (!*s)
  1545. return "";
  1546. *o++ = *s++;
  1547. }
  1548. *o = 0;
  1549. if (!strcmp (key, pkey) )
  1550. return value[valueindex];
  1551. if (!*s)
  1552. return "";
  1553. s++;
  1554. }
  1555. }
  1556. void Info_RemoveKey (char *s, char *key)
  1557. {
  1558. char *start;
  1559. char pkey[512];
  1560. char value[512];
  1561. char *o;
  1562. if (strstr (key, "\\"))
  1563. {
  1564. Con_Printf ("Can't use a key with a \\\n");
  1565. return;
  1566. }
  1567. while (1)
  1568. {
  1569. start = s;
  1570. if (*s == '\\')
  1571. s++;
  1572. o = pkey;
  1573. while (*s != '\\')
  1574. {
  1575. if (!*s)
  1576. return;
  1577. *o++ = *s++;
  1578. }
  1579. *o = 0;
  1580. s++;
  1581. o = value;
  1582. while (*s != '\\' && *s)
  1583. {
  1584. if (!*s)
  1585. return;
  1586. *o++ = *s++;
  1587. }
  1588. *o = 0;
  1589. if (!strcmp (key, pkey) )
  1590. {
  1591. strcpy (start, s); // remove this part
  1592. return;
  1593. }
  1594. if (!*s)
  1595. return;
  1596. }
  1597. }
  1598. void Info_RemovePrefixedKeys (char *start, char prefix)
  1599. {
  1600. char *s;
  1601. char pkey[512];
  1602. char value[512];
  1603. char *o;
  1604. s = start;
  1605. while (1)
  1606. {
  1607. if (*s == '\\')
  1608. s++;
  1609. o = pkey;
  1610. while (*s != '\\')
  1611. {
  1612. if (!*s)
  1613. return;
  1614. *o++ = *s++;
  1615. }
  1616. *o = 0;
  1617. s++;
  1618. o = value;
  1619. while (*s != '\\' && *s)
  1620. {
  1621. if (!*s)
  1622. return;
  1623. *o++ = *s++;
  1624. }
  1625. *o = 0;
  1626. if (pkey[0] == prefix)
  1627. {
  1628. Info_RemoveKey (start, pkey);
  1629. s = start;
  1630. }
  1631. if (!*s)
  1632. return;
  1633. }
  1634. }
  1635. void Info_SetValueForStarKey (char *s, char *key, char *value, int maxsize)
  1636. {
  1637. char new[1024], *v;
  1638. int c;
  1639. #ifdef SERVERONLY
  1640. extern cvar_t sv_highchars;
  1641. #endif
  1642. if (strstr (key, "\\") || strstr (value, "\\") )
  1643. {
  1644. Con_Printf ("Can't use keys or values with a \\\n");
  1645. return;
  1646. }
  1647. if (strstr (key, "\"") || strstr (value, "\"") )
  1648. {
  1649. Con_Printf ("Can't use keys or values with a \"\n");
  1650. return;
  1651. }
  1652. if (strlen(key) > 63 || strlen(value) > 63)
  1653. {
  1654. Con_Printf ("Keys and values must be < 64 characters.\n");
  1655. return;
  1656. }
  1657. // this next line is kinda trippy
  1658. if (*(v = Info_ValueForKey(s, key))) {
  1659. // key exists, make sure we have enough room for new value, if we don't,
  1660. // don't change it!
  1661. if (strlen(value) - strlen(v) + strlen(s) > maxsize) {
  1662. Con_Printf ("Info string length exceeded\n");
  1663. return;
  1664. }
  1665. }
  1666. Info_RemoveKey (s, key);
  1667. if (!value || !strlen(value))
  1668. return;
  1669. sprintf (new, "\\%s\\%s", key, value);
  1670. if ((int)(strlen(new) + strlen(s)) > maxsize)
  1671. {
  1672. Con_Printf ("Info string length exceeded\n");
  1673. return;
  1674. }
  1675. // only copy ascii values
  1676. s += strlen(s);
  1677. v = new;
  1678. while (*v)
  1679. {
  1680. c = (unsigned char)*v++;
  1681. #ifndef SERVERONLY
  1682. // client only allows highbits on name
  1683. if (stricmp(key, "name") != 0) {
  1684. c &= 127;
  1685. if (c < 32 || c > 127)
  1686. continue;
  1687. // auto lowercase team
  1688. if (stricmp(key, "team") == 0)
  1689. c = tolower(c);
  1690. }
  1691. #else
  1692. if (!sv_highchars.value) {
  1693. c &= 127;
  1694. if (c < 32 || c > 127)
  1695. continue;
  1696. }
  1697. #endif
  1698. // c &= 127; // strip high bits
  1699. if (c > 13) // && c < 127)
  1700. *s++ = c;
  1701. }
  1702. *s = 0;
  1703. }
  1704. void Info_SetValueForKey (char *s, char *key, char *value, int maxsize)
  1705. {
  1706. if (key[0] == '*')
  1707. {
  1708. Con_Printf ("Can't set * keys\n");
  1709. return;
  1710. }
  1711. Info_SetValueForStarKey (s, key, value, maxsize);
  1712. }
  1713. void Info_Print (char *s)
  1714. {
  1715. char key[512];
  1716. char value[512];
  1717. char *o;
  1718. int l;
  1719. if (*s == '\\')
  1720. s++;
  1721. while (*s)
  1722. {
  1723. o = key;
  1724. while (*s && *s != '\\')
  1725. *o++ = *s++;
  1726. l = o - key;
  1727. if (l < 20)
  1728. {
  1729. memset (o, ' ', 20-l);
  1730. key[20] = 0;
  1731. }
  1732. else
  1733. *o = 0;
  1734. Con_Printf ("%s", key);
  1735. if (!*s)
  1736. {
  1737. Con_Printf ("MISSING VALUE\n");
  1738. return;
  1739. }
  1740. o = value;
  1741. s++;
  1742. while (*s && *s != '\\')
  1743. *o++ = *s++;
  1744. *o = 0;
  1745. if (*s)
  1746. s++;
  1747. Con_Printf ("%s\n", value);
  1748. }
  1749. }
  1750. static byte chktbl[1024 + 4] = {
  1751. 0x78,0xd2,0x94,0xe3,0x41,0xec,0xd6,0xd5,0xcb,0xfc,0xdb,0x8a,0x4b,0xcc,0x85,0x01,
  1752. 0x23,0xd2,0xe5,0xf2,0x29,0xa7,0x45,0x94,0x4a,0x62,0xe3,0xa5,0x6f,0x3f,0xe1,0x7a,
  1753. 0x64,0xed,0x5c,0x99,0x29,0x87,0xa8,0x78,0x59,0x0d,0xaa,0x0f,0x25,0x0a,0x5c,0x58,
  1754. 0xfb,0x00,0xa7,0xa8,0x8a,0x1d,0x86,0x80,0xc5,0x1f,0xd2,0x28,0x69,0x71,0x58,0xc3,
  1755. 0x51,0x90,0xe1,0xf8,0x6a,0xf3,0x8f,0xb0,0x68,0xdf,0x95,0x40,0x5c,0xe4,0x24,0x6b,
  1756. 0x29,0x19,0x71,0x3f,0x42,0x63,0x6c,0x48,0xe7,0xad,0xa8,0x4b,0x91,0x8f,0x42,0x36,
  1757. 0x34,0xe7,0x32,0x55,0x59,0x2d,0x36,0x38,0x38,0x59,0x9b,0x08,0x16,0x4d,0x8d,0xf8,
  1758. 0x0a,0xa4,0x52,0x01,0xbb,0x52,0xa9,0xfd,0x40,0x18,0x97,0x37,0xff,0xc9,0x82,0x27,
  1759. 0xb2,0x64,0x60,0xce,0x00,0xd9,0x04,0xf0,0x9e,0x99,0xbd,0xce,0x8f,0x90,0x4a,0xdd,
  1760. 0xe1,0xec,0x19,0x14,0xb1,0xfb,0xca,0x1e,0x98,0x0f,0xd4,0xcb,0x80,0xd6,0x05,0x63,
  1761. 0xfd,0xa0,0x74,0xa6,0x86,0xf6,0x19,0x98,0x76,0x27,0x68,0xf7,0xe9,0x09,0x9a,0xf2,
  1762. 0x2e,0x42,0xe1,0xbe,0x64,0x48,0x2a,0x74,0x30,0xbb,0x07,0xcc,0x1f,0xd4,0x91,0x9d,
  1763. 0xac,0x55,0x53,0x25,0xb9,0x64,0xf7,0x58,0x4c,0x34,0x16,0xbc,0xf6,0x12,0x2b,0x65,
  1764. 0x68,0x25,0x2e,0x29,0x1f,0xbb,0xb9,0xee,0x6d,0x0c,0x8e,0xbb,0xd2,0x5f,0x1d,0x8f,
  1765. 0xc1,0x39,0xf9,0x8d,0xc0,0x39,0x75,0xcf,0x25,0x17,0xbe,0x96,0xaf,0x98,0x9f,0x5f,
  1766. 0x65,0x15,0xc4,0x62,0xf8,0x55,0xfc,0xab,0x54,0xcf,0xdc,0x14,0x06,0xc8,0xfc,0x42,
  1767. 0xd3,0xf0,0xad,0x10,0x08,0xcd,0xd4,0x11,0xbb,0xca,0x67,0xc6,0x48,0x5f,0x9d,0x59,
  1768. 0xe3,0xe8,0x53,0x67,0x27,0x2d,0x34,0x9e,0x9e,0x24,0x29,0xdb,0x69,0x99,0x86,0xf9,
  1769. 0x20,0xb5,0xbb,0x5b,0xb0,0xf9,0xc3,0x67,0xad,0x1c,0x9c,0xf7,0xcc,0xef,0xce,0x69,
  1770. 0xe0,0x26,0x8f,0x79,0xbd,0xca,0x10,0x17,0xda,0xa9,0x88,0x57,0x9b,0x15,0x24,0xba,
  1771. 0x84,0xd0,0xeb,0x4d,0x14,0xf5,0xfc,0xe6,0x51,0x6c,0x6f,0x64,0x6b,0x73,0xec,0x85,
  1772. 0xf1,0x6f,0xe1,0x67,0x25,0x10,0x77,0x32,0x9e,0x85,0x6e,0x69,0xb1,0x83,0x00,0xe4,
  1773. 0x13,0xa4,0x45,0x34,0x3b,0x40,0xff,0x41,0x82,0x89,0x79,0x57,0xfd,0xd2,0x8e,0xe8,
  1774. 0xfc,0x1d,0x19,0x21,0x12,0x00,0xd7,0x66,0xe5,0xc7,0x10,0x1d,0xcb,0x75,0xe8,0xfa,
  1775. 0xb6,0xee,0x7b,0x2f,0x1a,0x25,0x24,0xb9,0x9f,0x1d,0x78,0xfb,0x84,0xd0,0x17,0x05,
  1776. 0x71,0xb3,0xc8,0x18,0xff,0x62,0xee,0xed,0x53,0xab,0x78,0xd3,0x65,0x2d,0xbb,0xc7,
  1777. 0xc1,0xe7,0x70,0xa2,0x43,0x2c,0x7c,0xc7,0x16,0x04,0xd2,0x45,0xd5,0x6b,0x6c,0x7a,
  1778. 0x5e,0xa1,0x50,0x2e,0x31,0x5b,0xcc,0xe8,0x65,0x8b,0x16,0x85,0xbf,0x82,0x83,0xfb,
  1779. 0xde,0x9f,0x36,0x48,0x32,0x79,0xd6,0x9b,0xfb,0x52,0x45,0xbf,0x43,0xf7,0x0b,0x0b,
  1780. 0x19,0x19,0x31,0xc3,0x85,0xec,0x1d,0x8c,0x20,0xf0,0x3a,0xfa,0x80,0x4d,0x2c,0x7d,
  1781. 0xac,0x60,0x09,0xc0,0x40,0xee,0xb9,0xeb,0x13,0x5b,0xe8,0x2b,0xb1,0x20,0xf0,0xce,
  1782. 0x4c,0xbd,0xc6,0x04,0x86,0x70,0xc6,0x33,0xc3,0x15,0x0f,0x65,0x19,0xfd,0xc2,0xd3,
  1783. // map checksum goes here
  1784. 0x00,0x00,0x00,0x00
  1785. };
  1786. static byte chkbuf[16 + 60 + 4];
  1787. static unsigned last_mapchecksum = 0;
  1788. #if 0
  1789. /*
  1790. ====================
  1791. COM_BlockSequenceCheckByte
  1792. For proxy protecting
  1793. ====================
  1794. */
  1795. byte COM_BlockSequenceCheckByte (byte *base, int length, int sequence, unsigned mapchecksum)
  1796. {
  1797. int checksum;
  1798. byte *p;
  1799. if (last_mapchecksum != mapchecksum) {
  1800. last_mapchecksum = mapchecksum;
  1801. chktbl[1024] = (mapchecksum & 0xff000000) >> 24;
  1802. chktbl[1025] = (mapchecksum & 0x00ff0000) >> 16;
  1803. chktbl[1026] = (mapchecksum & 0x0000ff00) >> 8;
  1804. chktbl[1027] = (mapchecksum & 0x000000ff);
  1805. Com_BlockFullChecksum (chktbl, sizeof(chktbl), chkbuf);
  1806. }
  1807. p = chktbl + (sequence % (sizeof(chktbl) - 8));
  1808. if (length > 60)
  1809. length = 60;
  1810. memcpy (chkbuf + 16, base, length);
  1811. length += 16;
  1812. chkbuf[length] = (sequence & 0xff) ^ p[0];
  1813. chkbuf[length+1] = p[1];
  1814. chkbuf[length+2] = ((sequence>>8) & 0xff) ^ p[2];
  1815. chkbuf[length+3] = p[3];
  1816. length += 4;
  1817. checksum = LittleLong(Com_BlockChecksum (chkbuf, length));
  1818. checksum &= 0xff;
  1819. return checksum;
  1820. }
  1821. #endif
  1822. /*
  1823. ====================
  1824. COM_BlockSequenceCRCByte
  1825. For proxy protecting
  1826. ====================
  1827. */
  1828. byte COM_BlockSequenceCRCByte (byte *base, int length, int sequence)
  1829. {
  1830. unsigned short crc;
  1831. byte *p;
  1832. byte chkb[60 + 4];
  1833. p = chktbl + (sequence % (sizeof(chktbl) - 8));
  1834. if (length > 60)
  1835. length = 60;
  1836. memcpy (chkb, base, length);
  1837. chkb[length] = (sequence & 0xff) ^ p[0];
  1838. chkb[length+1] = p[1];
  1839. chkb[length+2] = ((sequence>>8) & 0xff) ^ p[2];
  1840. chkb[length+3] = p[3];
  1841. length += 4;
  1842. crc = CRC_Block(chkb, length);
  1843. crc &= 0xff;
  1844. return crc;
  1845. }
  1846. // char *date = "Oct 24 1996";
  1847. static char *date = __DATE__ ;
  1848. static char *mon[12] =
  1849. { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  1850. static char mond[12] =
  1851. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1852. // returns days since Oct 24 1996
  1853. int build_number( void )
  1854. {
  1855. int m = 0;
  1856. int d = 0;
  1857. int y = 0;
  1858. static int b = 0;
  1859. if (b != 0)
  1860. return b;
  1861. for (m = 0; m < 11; m++)
  1862. {
  1863. if (Q_strncasecmp( &date[0], mon[m], 3 ) == 0)
  1864. break;
  1865. d += mond[m];
  1866. }
  1867. d += atoi( &date[4] ) - 1;
  1868. y = atoi( &date[7] ) - 1900;
  1869. b = d + (int)((y - 1) * 365.25);
  1870. if (((y % 4) == 0) && m > 1)
  1871. {
  1872. b += 1;
  1873. }
  1874. b -= 35778; // Dec 16 1998
  1875. return b;
  1876. }