W_WAD.C 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // W_wad.c
  2. #ifdef NeXT
  3. #include <libc.h>
  4. #include <ctype.h>
  5. // next doesn't need a binary flag in open call
  6. #define O_BINARY 0
  7. #else
  8. #include <malloc.h>
  9. #include <io.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #endif
  13. #include "DoomDef.h"
  14. //===============
  15. // TYPES
  16. //===============
  17. typedef struct
  18. {
  19. char identification[4]; // should be IWAD
  20. int numlumps;
  21. int infotableofs;
  22. } wadinfo_t;
  23. typedef struct
  24. {
  25. int filepos;
  26. int size;
  27. char name[8];
  28. } filelump_t;
  29. //=============
  30. // GLOBALS
  31. //=============
  32. lumpinfo_t *lumpinfo; // location of each lump on disk
  33. int numlumps;
  34. void **lumpcache;
  35. //===================
  36. #ifdef NeXT
  37. #define strcmpi strcasecmp
  38. void strupr (char *s)
  39. {
  40. while (*s)
  41. *s++ = toupper(*s);
  42. }
  43. /*
  44. ================
  45. =
  46. = filelength
  47. =
  48. ================
  49. */
  50. int filelength (int handle)
  51. {
  52. struct stat fileinfo;
  53. if (fstat (handle,&fileinfo) == -1)
  54. I_Error ("Error fstating");
  55. return fileinfo.st_size;
  56. }
  57. #endif
  58. void ExtractFileBase (char *path, char *dest)
  59. {
  60. char *src;
  61. int length;
  62. src = path + strlen(path) - 1;
  63. //
  64. // back up until a \ or the start
  65. //
  66. while (src != path && *(src-1) != '\\' && *(src-1) != '/')
  67. src--;
  68. //
  69. // copy up to eight characters
  70. //
  71. memset (dest,0,8);
  72. length = 0;
  73. while (*src && *src != '.')
  74. {
  75. if (++length == 9)
  76. I_Error ("Filename base of %s >8 chars",path);
  77. *dest++ = toupper((int)*src++);
  78. }
  79. }
  80. /*
  81. ============================================================================
  82. LUMP BASED ROUTINES
  83. ============================================================================
  84. */
  85. /*
  86. ====================
  87. =
  88. = W_AddFile
  89. =
  90. = All files are optional, but at least one file must be found
  91. = Files with a .wad extension are wadlink files with multiple lumps
  92. = Other files are single lumps with the base filename for the lump name
  93. =
  94. ====================
  95. */
  96. void W_AddFile (char *filename)
  97. {
  98. wadinfo_t header;
  99. lumpinfo_t *lump_p;
  100. unsigned i;
  101. int handle, length;
  102. int startlump;
  103. filelump_t *fileinfo, singleinfo;
  104. //
  105. // open the file and add to directory
  106. //
  107. if ( (handle = open (filename,O_RDONLY | O_BINARY)) == -1)
  108. return;
  109. startlump = numlumps;
  110. if (strcmpi (filename+strlen(filename)-3 , "wad" ) )
  111. {
  112. // single lump file
  113. fileinfo = &singleinfo;
  114. singleinfo.filepos = 0;
  115. singleinfo.size = LONG(filelength(handle));
  116. ExtractFileBase (filename, singleinfo.name);
  117. numlumps++;
  118. }
  119. else
  120. {
  121. // WAD file
  122. read (handle, &header, sizeof(header));
  123. if (strncmp(header.identification,"IWAD",4))
  124. {
  125. if (strncmp(header.identification,"PWAD",4))
  126. I_Error ("Wad file %s doesn't have IWAD or PWAD id\n"
  127. ,filename);
  128. }
  129. header.numlumps = LONG(header.numlumps);
  130. header.infotableofs = LONG(header.infotableofs);
  131. length = header.numlumps*sizeof(filelump_t);
  132. fileinfo = alloca (length);
  133. lseek (handle, header.infotableofs, SEEK_SET);
  134. read (handle, fileinfo, length);
  135. numlumps += header.numlumps;
  136. }
  137. //
  138. // Fill in lumpinfo
  139. //
  140. lumpinfo = realloc (lumpinfo, numlumps*sizeof(lumpinfo_t));
  141. if (!lumpinfo)
  142. I_Error ("Couldn't realloc lumpinfo");
  143. lump_p = &lumpinfo[startlump];
  144. for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++)
  145. {
  146. lump_p->handle = handle;
  147. lump_p->position = LONG(fileinfo->filepos);
  148. lump_p->size = LONG(fileinfo->size);
  149. strncpy (lump_p->name, fileinfo->name, 8);
  150. }
  151. }
  152. /*
  153. ====================
  154. =
  155. = W_InitMultipleFiles
  156. =
  157. = Pass a null terminated list of files to use.
  158. =
  159. = All files are optional, but at least one file must be found
  160. =
  161. = Files with a .wad extension are idlink files with multiple lumps
  162. =
  163. = Other files are single lumps with the base filename for the lump name
  164. =
  165. = Lump names can appear multiple times. The name searcher looks backwards,
  166. = so a later file can override an earlier one.
  167. =
  168. ====================
  169. */
  170. void W_InitMultipleFiles (char **filenames)
  171. {
  172. int size;
  173. //
  174. // open all the files, load headers, and count lumps
  175. //
  176. numlumps = 0;
  177. lumpinfo = malloc(1); // will be realloced as lumps are added
  178. for ( ; *filenames ; filenames++)
  179. W_AddFile (*filenames);
  180. if (!numlumps)
  181. I_Error ("W_InitFiles: no files found");
  182. //
  183. // set up caching
  184. //
  185. size = numlumps * sizeof(*lumpcache);
  186. lumpcache = malloc (size);
  187. if (!lumpcache)
  188. I_Error ("Couldn't allocate lumpcache");
  189. memset (lumpcache,0, size);
  190. }
  191. /*
  192. ====================
  193. =
  194. = W_InitFile
  195. =
  196. = Just initialize from a single file
  197. =
  198. ====================
  199. */
  200. void W_InitFile (char *filename)
  201. {
  202. char *names[2];
  203. names[0] = filename;
  204. names[1] = NULL;
  205. W_InitMultipleFiles (names);
  206. }
  207. /*
  208. ====================
  209. =
  210. = W_NumLumps
  211. =
  212. ====================
  213. */
  214. int W_NumLumps (void)
  215. {
  216. return numlumps;
  217. }
  218. /*
  219. ====================
  220. =
  221. = W_CheckNumForName
  222. =
  223. = Returns -1 if name not found
  224. =
  225. ====================
  226. */
  227. int W_CheckNumForName (char *name)
  228. {
  229. char name8[9];
  230. int v1,v2;
  231. lumpinfo_t *lump_p;
  232. // make the name into two integers for easy compares
  233. strncpy (name8,name,8);
  234. name8[8] = 0; // in case the name was a fill 8 chars
  235. strupr (name8); // case insensitive
  236. v1 = *(int *)name8;
  237. v2 = *(int *)&name8[4];
  238. // scan backwards so patch lump files take precedence
  239. lump_p = lumpinfo + numlumps;
  240. while (lump_p-- != lumpinfo)
  241. if ( *(int *)lump_p->name == v1 && *(int *)&lump_p->name[4] == v2)
  242. return lump_p - lumpinfo;
  243. return -1;
  244. }
  245. /*
  246. ====================
  247. =
  248. = W_GetNumForName
  249. =
  250. = Calls W_CheckNumForName, but bombs out if not found
  251. =
  252. ====================
  253. */
  254. int W_GetNumForName (char *name)
  255. {
  256. int i;
  257. i = W_CheckNumForName (name);
  258. if (i != -1)
  259. return i;
  260. I_Error ("W_GetNumForName: %s not found!",name);
  261. return -1;
  262. }
  263. /*
  264. ====================
  265. =
  266. = W_LumpLength
  267. =
  268. = Returns the buffer size needed to load the given lump
  269. =
  270. ====================
  271. */
  272. int W_LumpLength (int lump)
  273. {
  274. if (lump >= numlumps)
  275. I_Error ("W_LumpLength: %i >= numlumps",lump);
  276. return lumpinfo[lump].size;
  277. }
  278. /*
  279. ====================
  280. =
  281. = W_ReadLump
  282. =
  283. = Loads the lump into the given buffer, which must be >= W_LumpLength()
  284. =
  285. ====================
  286. */
  287. void W_ReadLump (int lump, void *dest)
  288. {
  289. int c;
  290. lumpinfo_t *l;
  291. if (lump >= numlumps)
  292. I_Error ("W_ReadLump: %i >= numlumps",lump);
  293. l = lumpinfo+lump;
  294. I_BeginRead ();
  295. lseek (l->handle, l->position, SEEK_SET);
  296. c = read (l->handle, dest, l->size);
  297. if (c < l->size)
  298. I_Error ("W_ReadLump: only read %i of %i on lump %i",c,l->size,lump);
  299. I_EndRead ();
  300. }
  301. /*
  302. ====================
  303. =
  304. = W_CacheLumpNum
  305. =
  306. ====================
  307. */
  308. void *W_CacheLumpNum (int lump, int tag)
  309. {
  310. byte *ptr;
  311. if ((unsigned)lump >= numlumps)
  312. I_Error ("W_CacheLumpNum: %i >= numlumps",lump);
  313. if (!lumpcache[lump])
  314. { // read the lump in
  315. //printf ("cache miss on lump %i\n",lump);
  316. ptr = Z_Malloc (W_LumpLength (lump), tag, &lumpcache[lump]);
  317. W_ReadLump (lump, lumpcache[lump]);
  318. }
  319. else
  320. {
  321. //printf ("cache hit on lump %i\n",lump);
  322. Z_ChangeTag (lumpcache[lump],tag);
  323. }
  324. return lumpcache[lump];
  325. }
  326. /*
  327. ====================
  328. =
  329. = W_CacheLumpName
  330. =
  331. ====================
  332. */
  333. void *W_CacheLumpName (char *name, int tag)
  334. {
  335. return W_CacheLumpNum (W_GetNumForName(name), tag);
  336. }
  337. /*
  338. ====================
  339. =
  340. = W_Profile
  341. =
  342. ====================
  343. */
  344. // Ripped out for Heretic
  345. /*
  346. int info[2500][10];
  347. int profilecount;
  348. void W_Profile (void)
  349. {
  350. int i;
  351. memblock_t *block;
  352. void *ptr;
  353. char ch;
  354. FILE *f;
  355. int j;
  356. char name[9];
  357. for (i=0 ; i<numlumps ; i++)
  358. {
  359. ptr = lumpcache[i];
  360. if (!ptr)
  361. {
  362. ch = ' ';
  363. continue;
  364. }
  365. else
  366. {
  367. block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
  368. if (block->tag < PU_PURGELEVEL)
  369. ch = 'S';
  370. else
  371. ch = 'P';
  372. }
  373. info[i][profilecount] = ch;
  374. }
  375. profilecount++;
  376. f = fopen ("waddump.txt","w");
  377. name[8] = 0;
  378. for (i=0 ; i<numlumps ; i++)
  379. {
  380. memcpy (name,lumpinfo[i].name,8);
  381. for (j=0 ; j<8 ; j++)
  382. if (!name[j])
  383. break;
  384. for ( ; j<8 ; j++)
  385. name[j] = ' ';
  386. fprintf (f,"%s ",name);
  387. for (j=0 ; j<profilecount ; j++)
  388. fprintf (f," %c",info[i][j]);
  389. fprintf (f,"\n");
  390. }
  391. fclose (f);
  392. }
  393. */