w_wad.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2001 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION:
  30. * Handles WAD file header, directory, lump I/O.
  31. *
  32. *-----------------------------------------------------------------------------
  33. */
  34. // use config.h if autoconf made one -- josh
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #ifdef _MSC_VER
  42. #include <stddef.h>
  43. #include <io.h>
  44. #endif
  45. #include <fcntl.h>
  46. #include "doomstat.h"
  47. #include "d_net.h"
  48. #include "doomtype.h"
  49. #include "i_system.h"
  50. #ifdef __GNUG__
  51. #pragma implementation "w_wad.h"
  52. #endif
  53. #include "w_wad.h"
  54. #include "lprintf.h"
  55. //
  56. // GLOBALS
  57. //
  58. // Location of each lump on disk.
  59. lumpinfo_t *lumpinfo;
  60. int numlumps; // killough
  61. void ExtractFileBase (const char *path, char *dest)
  62. {
  63. const char *src = path + strlen(path) - 1;
  64. int length;
  65. // back up until a \ or the start
  66. while (src != path && src[-1] != ':' // killough 3/22/98: allow c:filename
  67. && *(src-1) != '\\'
  68. && *(src-1) != '/')
  69. {
  70. src--;
  71. }
  72. // copy up to eight characters
  73. memset(dest,0,8);
  74. length = 0;
  75. while ((*src) && (*src != '.') && (++length<9))
  76. {
  77. *dest++ = toupper(*src);
  78. src++;
  79. }
  80. /* cph - length check removed, just truncate at 8 chars.
  81. * If there are 8 or more chars, we'll copy 8, and no zero termination
  82. */
  83. }
  84. //
  85. // 1/18/98 killough: adds a default extension to a path
  86. // Note: Backslashes are treated specially, for MS-DOS.
  87. //
  88. char *AddDefaultExtension(char *path, const char *ext)
  89. {
  90. char *p = path;
  91. while (*p++);
  92. while (p-->path && *p!='/' && *p!='\\')
  93. if (*p=='.')
  94. return path;
  95. if (*ext!='.')
  96. strcat(path,".");
  97. return strcat(path,ext);
  98. }
  99. //
  100. // LUMP BASED ROUTINES.
  101. //
  102. //
  103. // W_AddFile
  104. // All files are optional, but at least one file must be
  105. // found (PWAD, if all required lumps are present).
  106. // Files with a .wad extension are wadlink files
  107. // with multiple lumps.
  108. // Other files are single lumps with the base filename
  109. // for the lump name.
  110. //
  111. // Reload hack removed by Lee Killough
  112. // CPhipps - source is an enum
  113. //
  114. // proff - changed using pointer to wadfile_info_t
  115. static void W_AddFile(wadfile_info_t *wadfile)
  116. // killough 1/31/98: static, const
  117. {
  118. wadinfo_t header;
  119. lumpinfo_t* lump_p;
  120. unsigned i;
  121. int length;
  122. int startlump;
  123. filelump_t *fileinfo, *fileinfo2free=NULL; //killough
  124. filelump_t singleinfo;
  125. // open the file and add to directory
  126. wadfile->handle = open(wadfile->name,O_RDONLY | O_BINARY);
  127. #ifdef HAVE_NET
  128. if (wadfile->handle == -1 && D_NetGetWad(wadfile->name)) // CPhipps
  129. wadfile->handle = open(wadfile->name,O_RDONLY | O_BINARY);
  130. #endif
  131. if (wadfile->handle == -1)
  132. {
  133. if ( strlen(wadfile->name)<=4 || // add error check -- killough
  134. (strcasecmp(wadfile->name+strlen(wadfile->name)-4 , ".lmp" ) &&
  135. strcasecmp(wadfile->name+strlen(wadfile->name)-4 , ".gwa" ) )
  136. )
  137. I_Error("W_AddFile: couldn't open %s",wadfile->name);
  138. return;
  139. }
  140. //jff 8/3/98 use logical output routine
  141. lprintf (LO_INFO," adding %s\n",wadfile->name);
  142. startlump = numlumps;
  143. if ( strlen(wadfile->name)<=4 ||
  144. (
  145. strcasecmp(wadfile->name+strlen(wadfile->name)-4,".wad") &&
  146. strcasecmp(wadfile->name+strlen(wadfile->name)-4,".gwa")
  147. )
  148. )
  149. {
  150. // single lump file
  151. fileinfo = &singleinfo;
  152. singleinfo.filepos = 0;
  153. singleinfo.size = LONG(I_Filelength(wadfile->handle));
  154. ExtractFileBase(wadfile->name, singleinfo.name);
  155. numlumps++;
  156. }
  157. else
  158. {
  159. // WAD file
  160. I_Read(wadfile->handle, &header, sizeof(header));
  161. if (strncmp(header.identification,"IWAD",4) &&
  162. strncmp(header.identification,"PWAD",4))
  163. I_Error("W_AddFile: Wad file %s doesn't have IWAD or PWAD id", wadfile->name);
  164. header.numlumps = LONG(header.numlumps);
  165. header.infotableofs = LONG(header.infotableofs);
  166. length = header.numlumps*sizeof(filelump_t);
  167. fileinfo2free = fileinfo = malloc(length); // killough
  168. lseek(wadfile->handle, header.infotableofs, SEEK_SET);
  169. I_Read(wadfile->handle, fileinfo, length);
  170. numlumps += header.numlumps;
  171. }
  172. // Fill in lumpinfo
  173. lumpinfo = realloc(lumpinfo, numlumps*sizeof(lumpinfo_t));
  174. lump_p = &lumpinfo[startlump];
  175. for (i=startlump ; (int)i<numlumps ; i++,lump_p++, fileinfo++)
  176. {
  177. lump_p->wadfile = wadfile; // killough 4/25/98
  178. lump_p->position = LONG(fileinfo->filepos);
  179. lump_p->size = LONG(fileinfo->size);
  180. lump_p->li_namespace = ns_global; // killough 4/17/98
  181. strncpy (lump_p->name, fileinfo->name, 8);
  182. lump_p->source = wadfile->src; // Ty 08/29/98
  183. }
  184. free(fileinfo2free); // killough
  185. }
  186. // jff 1/23/98 Create routines to reorder the master directory
  187. // putting all flats into one marked block, and all sprites into another.
  188. // This will allow loading of sprites and flats from a PWAD with no
  189. // other changes to code, particularly fast hashes of the lumps.
  190. //
  191. // killough 1/24/98 modified routines to be a little faster and smaller
  192. static int IsMarker(const char *marker, const char *name)
  193. {
  194. return !strncasecmp(name, marker, 8) ||
  195. (*name == *marker && !strncasecmp(name+1, marker, 7));
  196. }
  197. // killough 4/17/98: add namespace tags
  198. static void W_CoalesceMarkedResource(const char *start_marker,
  199. const char *end_marker, int li_namespace)
  200. {
  201. lumpinfo_t *marked = malloc(sizeof(*marked) * numlumps);
  202. size_t i, num_marked = 0, num_unmarked = 0;
  203. int is_marked = 0, mark_end = 0;
  204. lumpinfo_t *lump = lumpinfo;
  205. for (i=numlumps; i--; lump++)
  206. if (IsMarker(start_marker, lump->name)) // start marker found
  207. { // If this is the first start marker, add start marker to marked lumps
  208. if (!num_marked)
  209. {
  210. strncpy(marked->name, start_marker, 8);
  211. marked->size = 0; // killough 3/20/98: force size to be 0
  212. marked->li_namespace = ns_global; // killough 4/17/98
  213. marked->wadfile = NULL;
  214. num_marked = 1;
  215. }
  216. is_marked = 1; // start marking lumps
  217. }
  218. else
  219. if (IsMarker(end_marker, lump->name)) // end marker found
  220. {
  221. mark_end = 1; // add end marker below
  222. is_marked = 0; // stop marking lumps
  223. }
  224. else
  225. if (is_marked) // if we are marking lumps,
  226. { // move lump to marked list
  227. marked[num_marked] = *lump;
  228. marked[num_marked++].li_namespace = li_namespace; // killough 4/17/98
  229. }
  230. else
  231. lumpinfo[num_unmarked++] = *lump; // else move down THIS list
  232. // Append marked list to end of unmarked list
  233. memcpy(lumpinfo + num_unmarked, marked, num_marked * sizeof(*marked));
  234. free(marked); // free marked list
  235. numlumps = num_unmarked + num_marked; // new total number of lumps
  236. if (mark_end) // add end marker
  237. {
  238. lumpinfo[numlumps].size = 0; // killough 3/20/98: force size to be 0
  239. lumpinfo[numlumps].wadfile = NULL;
  240. lumpinfo[numlumps].li_namespace = ns_global; // killough 4/17/98
  241. strncpy(lumpinfo[numlumps++].name, end_marker, 8);
  242. }
  243. }
  244. // Hash function used for lump names.
  245. // Must be mod'ed with table size.
  246. // Can be used for any 8-character names.
  247. // by Lee Killough
  248. unsigned W_LumpNameHash(const char *s)
  249. {
  250. unsigned hash;
  251. (void) ((hash = toupper(s[0]), s[1]) &&
  252. (hash = hash*3+toupper(s[1]), s[2]) &&
  253. (hash = hash*2+toupper(s[2]), s[3]) &&
  254. (hash = hash*2+toupper(s[3]), s[4]) &&
  255. (hash = hash*2+toupper(s[4]), s[5]) &&
  256. (hash = hash*2+toupper(s[5]), s[6]) &&
  257. (hash = hash*2+toupper(s[6]),
  258. hash = hash*2+toupper(s[7]))
  259. );
  260. return hash;
  261. }
  262. //
  263. // W_CheckNumForName
  264. // Returns -1 if name not found.
  265. //
  266. // Rewritten by Lee Killough to use hash table for performance. Significantly
  267. // cuts down on time -- increases Doom performance over 300%. This is the
  268. // single most important optimization of the original Doom sources, because
  269. // lump name lookup is used so often, and the original Doom used a sequential
  270. // search. For large wads with > 1000 lumps this meant an average of over
  271. // 500 were probed during every search. Now the average is under 2 probes per
  272. // search. There is no significant benefit to packing the names into longwords
  273. // with this new hashing algorithm, because the work to do the packing is
  274. // just as much work as simply doing the string comparisons with the new
  275. // algorithm, which minimizes the expected number of comparisons to under 2.
  276. //
  277. // killough 4/17/98: add namespace parameter to prevent collisions
  278. // between different resources such as flats, sprites, colormaps
  279. //
  280. int (W_CheckNumForName)(register const char *name, register int li_namespace)
  281. {
  282. // Hash function maps the name to one of possibly numlump chains.
  283. // It has been tuned so that the average chain length never exceeds 2.
  284. // proff 2001/09/07 - check numlumps==0, this happens when called before WAD loaded
  285. register int i = (numlumps==0)?(-1):(lumpinfo[W_LumpNameHash(name) % (unsigned) numlumps].index);
  286. // We search along the chain until end, looking for case-insensitive
  287. // matches which also match a namespace tag. Separate hash tables are
  288. // not used for each namespace, because the performance benefit is not
  289. // worth the overhead, considering namespace collisions are rare in
  290. // Doom wads.
  291. while (i >= 0 && (strncasecmp(lumpinfo[i].name, name, 8) ||
  292. lumpinfo[i].li_namespace != li_namespace))
  293. i = lumpinfo[i].next;
  294. // Return the matching lump, or -1 if none found.
  295. return i;
  296. }
  297. //
  298. // killough 1/31/98: Initialize lump hash table
  299. //
  300. void W_HashLumps(void)
  301. {
  302. int i;
  303. for (i=0; i<numlumps; i++)
  304. lumpinfo[i].index = -1; // mark slots empty
  305. // Insert nodes to the beginning of each chain, in first-to-last
  306. // lump order, so that the last lump of a given name appears first
  307. // in any chain, observing pwad ordering rules. killough
  308. for (i=0; i<numlumps; i++)
  309. { // hash function:
  310. int j = W_LumpNameHash(lumpinfo[i].name) % (unsigned) numlumps;
  311. lumpinfo[i].next = lumpinfo[j].index; // Prepend to list
  312. lumpinfo[j].index = i;
  313. }
  314. }
  315. // End of lump hashing -- killough 1/31/98
  316. // W_GetNumForName
  317. // Calls W_CheckNumForName, but bombs out if not found.
  318. //
  319. int W_GetNumForName (const char* name) // killough -- const added
  320. {
  321. int i = W_CheckNumForName (name);
  322. if (i == -1)
  323. I_Error("W_GetNumForName: %.8s not found", name);
  324. return i;
  325. }
  326. // W_Init
  327. // Loads each of the files in the wadfiles array.
  328. // All files are optional, but at least one file
  329. // must be found.
  330. // Files with a .wad extension are idlink files
  331. // with multiple lumps.
  332. // Other files are single lumps with the base filename
  333. // for the lump name.
  334. // Lump names can appear multiple times.
  335. // The name searcher looks backwards, so a later file
  336. // does override all earlier ones.
  337. //
  338. // CPhipps - modified to use the new wadfiles array
  339. //
  340. wadfile_info_t *wadfiles=NULL;
  341. size_t numwadfiles = 0; // CPhipps - size of the wadfiles array (dynamic, no limit)
  342. void W_Init(void)
  343. {
  344. // CPhipps - start with nothing
  345. numlumps = 0;
  346. free(lumpinfo);
  347. lumpinfo = NULL;
  348. numlumps = 0; lumpinfo = NULL;
  349. { // CPhipps - new wadfiles array used
  350. // open all the files, load headers, and count lumps
  351. int i;
  352. for (i=0; (size_t)i<numwadfiles; i++)
  353. W_AddFile(&wadfiles[i]);
  354. }
  355. if (!numlumps)
  356. I_Error ("W_Init: No files found");
  357. //jff 1/23/98
  358. // get all the sprites and flats into one marked block each
  359. // killough 1/24/98: change interface to use M_START/M_END explicitly
  360. // killough 4/17/98: Add namespace tags to each entry
  361. // killough 4/4/98: add colormap markers
  362. W_CoalesceMarkedResource("S_START", "S_END", ns_sprites);
  363. W_CoalesceMarkedResource("F_START", "F_END", ns_flats);
  364. W_CoalesceMarkedResource("C_START", "C_END", ns_colormaps);
  365. W_CoalesceMarkedResource("B_START", "B_END", ns_prboom);
  366. // killough 1/31/98: initialize lump hash table
  367. W_HashLumps();
  368. /* cph 2001/07/07 - separated cache setup */
  369. lprintf(LO_INFO,"W_InitCache\n");
  370. W_InitCache();
  371. }
  372. void W_ReleaseAllWads(void)
  373. {
  374. W_DoneCache();
  375. numwadfiles = 0;
  376. free(wadfiles);
  377. wadfiles = NULL;
  378. numlumps = 0;
  379. free(lumpinfo);
  380. lumpinfo = NULL;
  381. }
  382. //
  383. // W_LumpLength
  384. // Returns the buffer size needed to load the given lump.
  385. //
  386. int W_LumpLength (int lump)
  387. {
  388. if (lump >= numlumps)
  389. I_Error ("W_LumpLength: %i >= numlumps",lump);
  390. return lumpinfo[lump].size;
  391. }
  392. //
  393. // W_ReadLump
  394. // Loads the lump into the given buffer,
  395. // which must be >= W_LumpLength().
  396. //
  397. void W_ReadLump(int lump, void *dest)
  398. {
  399. lumpinfo_t *l = lumpinfo + lump;
  400. #ifdef RANGECHECK
  401. if (lump >= numlumps)
  402. I_Error ("W_ReadLump: %i >= numlumps",lump);
  403. #endif
  404. {
  405. if (l->wadfile)
  406. {
  407. lseek(l->wadfile->handle, l->position, SEEK_SET);
  408. I_Read(l->wadfile->handle, dest, l->size);
  409. }
  410. }
  411. }