Debian Codegraph @

prboom2/src/w_wad.c

raw
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
35 // use config.h if autoconf made one -- josh
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #ifdef _MSC_VER
43 #include <stddef.h>
44 #include <io.h>
45 #endif
46
47 #include "doomstat.h"
48 #include "d_net.h"
49 #include "doomtype.h"
50 #include "i_system.h"
51 #include "m_file.h"
52 #include "r_main.h"
53
54 #include "w_wad.h"
55 #include "lprintf.h"
56 #include "e6y.h"
57
58 #include "dsda/utility.h"
59
60 //
61 // GLOBALS
62 //
63
64 // Location of each lump on disk.
65 lumpinfo_t *lumpinfo;
66 int numlumps; // killough
67
68 int MainLumpCache = false;
69
70 void ExtractFileBase (const char *path, char *dest)
71 {
72 const char *src = path + strlen(path) - 1;
73 int length;
74
75 // back up until a \ or the start
76 while (src != path && src[-1] != ':' // killough 3/22/98: allow c:filename
77 && *(src-1) != '\\'
78 && *(src-1) != '/')
79 {
80 src--;
81 }
82
83 // copy up to eight characters
84 memset(dest,0,8);
85 length = 0;
86
87 while ((*src) && (*src != '.') && (++length<9))
88 {
89 *dest++ = toupper(*src++);
90 }
91 /* cph - length check removed, just truncate at 8 chars.
92 * If there are 8 or more chars, we'll copy 8, and no zero termination
93 */
94 }
95
96 //
97 // 1/18/98 killough: adds a default extension to a path
98 // Note: Backslashes are treated specially, for MS-DOS.
99 //
100
101 char *AddDefaultExtension(char *path, const char *ext)
102 {
103 char *p = path;
104 while (*p++);
105 while (p-->path && *p!='/' && *p!='\\')
106 if (*p=='.')
107 return path;
108 if (*ext!='.')
109 strcat(path,".");
110 return strcat(path,ext);
111 }
112
113 //
114 // LUMP BASED ROUTINES.
115 //
116
117 //
118 // W_AddFile
119 // All files are optional, but at least one file must be
120 // found (PWAD, if all required lumps are present).
121 // Files with a .wad extension are wadlink files
122 // with multiple lumps.
123 // Other files are single lumps with the base filename
124 // for the lump name.
125 //
126 // Reload hack removed by Lee Killough
127 // CPhipps - source is an enum
128 //
129 // proff - changed using pointer to wadfile_info_t
130 static void W_AddFile(wadfile_info_t *wadfile)
131 // killough 1/31/98: static, const
132 {
133 wadinfo_t header;
134 lumpinfo_t* lump_p;
135 unsigned i;
136 int length;
137 int startlump;
138 filelump_t *fileinfo, *fileinfo2free=NULL; //killough
139 filelump_t singleinfo;
140 int flags = 0;
141
142 if (wadfile->src == source_skip)
143 {
144 return;
145 }
146
147 // Close any existing handle
148 if (wadfile->handle > 0)
149 {
150 close(wadfile->handle);
151 wadfile->handle = 0;
152 }
153
154 // open the file and add to directory
155
156 wadfile->handle = M_OpenRB(wadfile->name);
157 if (wadfile->handle == -1)
158 {
159 if (!dsda_HasFileExt(wadfile->name, ".lmp"))
160 I_Error("W_AddFile: couldn't open %s",wadfile->name);
161 return;
162 }
163
164 //jff 8/3/98 use logical output routine
165 if (MainLumpCache)
166 lprintf (LO_INFO," adding %s\n",wadfile->name);
167 startlump = numlumps;
168
169 // mark lumps from internal resource
170 if (wadfile->src == source_auto_load)
171 {
172 int len = strlen(WAD_DATA);
173 int len_file = strlen(wadfile->name);
174 if (len_file >= len)
175 {
176 if (!strcasecmp(wadfile->name + len_file - len, WAD_DATA))
177 {
178 flags = LUMP_PRBOOM;
179 }
180 }
181 }
182
183 if (!dsda_HasFileExt(wadfile->name, ".wad"))
184 {
185 // single lump file
186 fileinfo = &singleinfo;
187 singleinfo.filepos = 0;
188 singleinfo.size = LittleLong(I_Filelength(wadfile->handle));
189 ExtractFileBase(wadfile->name, singleinfo.name);
190 numlumps++;
191 }
192 else
193 {
194 // WAD file
195 I_Read(wadfile->handle, &header, sizeof(header));
196 if (strncmp(header.identification,"IWAD",4) &&
197 strncmp(header.identification,"PWAD",4))
198 I_Error("W_AddFile: Wad file %s doesn't have IWAD or PWAD id", wadfile->name);
199 header.numlumps = LittleLong(header.numlumps);
200 header.infotableofs = LittleLong(header.infotableofs);
201 length = header.numlumps*sizeof(filelump_t);
202 fileinfo2free = fileinfo = Z_Malloc(length); // killough
203 lseek(wadfile->handle, header.infotableofs, SEEK_SET),
204 I_Read(wadfile->handle, fileinfo, length);
205 numlumps += header.numlumps;
206 }
207
208 // Fill in lumpinfo
209 lumpinfo = Z_Realloc(lumpinfo, numlumps*sizeof(lumpinfo_t));
210
211 lump_p = &lumpinfo[startlump];
212
213 for (i=startlump ; (int)i<numlumps ; i++,lump_p++, fileinfo++)
214 {
215 lump_p->flags = flags;
216 lump_p->wadfile = wadfile; // killough 4/25/98
217 lump_p->position = LittleLong(fileinfo->filepos);
218 lump_p->size = LittleLong(fileinfo->size);
219 if (wadfile->src == source_lmp)
220 {
221 // Modifications to place command-line-added demo lumps
222 // into a separate "ns_demos" namespace so that they cannot
223 // conflict with other lump names
224 lump_p->li_namespace = ns_demos;
225 }
226 else
227 {
228 lump_p->li_namespace = ns_global; // killough 4/17/98
229 }
230 strncpy (lump_p->name, fileinfo->name, 8);
231 lump_p->source = wadfile->src; // Ty 08/29/98
232 }
233
234 Z_Free(fileinfo2free); // killough
235 }
236
237 // jff 1/23/98 Create routines to reorder the master directory
238 // putting all flats into one marked block, and all sprites into another.
239 // This will allow loading of sprites and flats from a PWAD with no
240 // other changes to code, particularly fast hashes of the lumps.
241 //
242 // killough 1/24/98 modified routines to be a little faster and smaller
243
244 static int IsMarker(const char *marker, const char *name)
245 {
246 return !strncasecmp(name, marker, 8) ||
247 // doubled first character test for single-character prefixes only
248 // FF_* is valid alias for F_*, but HI_* should not allow HHI_*
249 (marker[1] == '_' && *name == *marker && !strncasecmp(name+1, marker, 7));
250 }
251
252 // killough 4/17/98: add namespace tags
253
254 static int W_CoalesceMarkedResource(const char *start_marker,
255 const char *end_marker, li_namespace_e li_namespace)
256 {
257 int result = 0;
258 lumpinfo_t *marked = Z_Malloc(sizeof(*marked) * numlumps);
259 size_t i, num_marked = 0, num_unmarked = 0;
260 int is_marked = 0, mark_end = 0;
261 lumpinfo_t *lump = lumpinfo;
262
263 for (i=numlumps; i--; lump++)
264 if (IsMarker(start_marker, lump->name)) // start marker found
265 { // If this is the first start marker, add start marker to marked lumps
266 if (!num_marked)
267 {
268 strncpy(marked->name, start_marker, 8);
269 marked->size = 0; // killough 3/20/98: force size to be 0
270 marked->li_namespace = ns_global; // killough 4/17/98
271 marked->wadfile = NULL;
272 num_marked = 1;
273 }
274 is_marked = 1; // start marking lumps
275 }
276 else
277 if (IsMarker(end_marker, lump->name)) // end marker found
278 {
279 mark_end = 1; // add end marker below
280 is_marked = 0; // stop marking lumps
281 }
282 else
283 if (is_marked || lump->li_namespace == li_namespace)
284 {
285 // if we are marking lumps,
286 // move lump to marked list
287 // sf: check for namespace already set
288
289 // sf 26/10/99:
290 // ignore sprite lumps smaller than 8 bytes (the smallest possible)
291 // in size -- this was used by some dmadds wads
292 // as an 'empty' graphics resource
293 if(li_namespace != ns_sprites || lump->size > 8)
294 {
295 marked[num_marked] = *lump;
296 marked[num_marked++].li_namespace = li_namespace; // killough 4/17/98
297 result++;
298 }
299 }
300 else
301 lumpinfo[num_unmarked++] = *lump; // else move down THIS list
302
303 // Append marked list to end of unmarked list
304 memcpy(lumpinfo + num_unmarked, marked, num_marked * sizeof(*marked));
305
306 Z_Free(marked); // free marked list
307
308 numlumps = num_unmarked + num_marked; // new total number of lumps
309
310 if (mark_end) // add end marker
311 {
312 lumpinfo[numlumps].size = 0; // killough 3/20/98: force size to be 0
313 lumpinfo[numlumps].wadfile = NULL;
314 lumpinfo[numlumps].li_namespace = ns_global; // killough 4/17/98
315 strncpy(lumpinfo[numlumps++].name, end_marker, 8);
316 }
317
318 return result;
319 }
320
321 // Hash function used for lump names.
322 // Must be mod'ed with table size.
323 // Can be used for any 8-character names.
324 // by Lee Killough
325
326 unsigned W_LumpNameHash(const char *s)
327 {
328 unsigned hash;
329 (void) ((hash = toupper(s[0]), s[1]) &&
330 (hash = hash*3+toupper(s[1]), s[2]) &&
331 (hash = hash*2+toupper(s[2]), s[3]) &&
332 (hash = hash*2+toupper(s[3]), s[4]) &&
333 (hash = hash*2+toupper(s[4]), s[5]) &&
334 (hash = hash*2+toupper(s[5]), s[6]) &&
335 (hash = hash*2+toupper(s[6]),
336 hash = hash*2+toupper(s[7]))
337 );
338 return hash;
339 }
340
341 //
342 // W_CheckNumForName
343 // Returns LUMP_NOT_FOUND if name not found.
344 //
345 // Rewritten by Lee Killough to use hash table for performance. Significantly
346 // cuts down on time -- increases Doom performance over 300%. This is the
347 // single most important optimization of the original Doom sources, because
348 // lump name lookup is used so often, and the original Doom used a sequential
349 // search. For large wads with > 1000 lumps this meant an average of over
350 // 500 were probed during every search. Now the average is under 2 probes per
351 // search. There is no significant benefit to packing the names into longwords
352 // with this new hashing algorithm, because the work to do the packing is
353 // just as much work as simply doing the string comparisons with the new
354 // algorithm, which minimizes the expected number of comparisons to under 2.
355 //
356 // killough 4/17/98: add namespace parameter to prevent collisions
357 // between different resources such as flats, sprites, colormaps
358 //
359
360 // W_FindNumFromName, an iterative version of W_CheckNumForName
361 // returns list of lump numbers for a given name (latest first)
362 //
363 int W_FindNumFromName2(const char *name, int li_namespace, int i)
364 {
365 // Hash function maps the name to one of possibly numlump chains.
366 // It has been tuned so that the average chain length never exceeds 2.
367
368 // proff 2001/09/07 - check numlumps==0, this happens when called before WAD loaded
369 if (numlumps == 0)
370 i = LUMP_NOT_FOUND;
371 else
372 {
373 if (i < 0)
374 i = lumpinfo[W_LumpNameHash(name) % (unsigned) numlumps].index;
375 else
376 i = lumpinfo[i].next;
377
378 // We search along the chain until end, looking for case-insensitive
379 // matches which also match a namespace tag. Separate hash tables are
380 // not used for each namespace, because the performance benefit is not
381 // worth the overhead, considering namespace collisions are rare in
382 // Doom wads.
383
384 while (i != LUMP_NOT_FOUND && (strncasecmp(lumpinfo[i].name, name, 8) ||
385 lumpinfo[i].li_namespace != li_namespace))
386 i = lumpinfo[i].next;
387 }
388
389 // Return the matching lump, or LUMP_NOT_FOUND if none found.
390
391 return i;
392 }
393
394 //
395 // killough 1/31/98: Initialize lump hash table
396 //
397
398 void W_HashLumps(void)
399 {
400 int i;
401
402 for (i=0; i<numlumps; i++)
403 lumpinfo[i].index = LUMP_NOT_FOUND; // mark slots empty
404
405 // Insert nodes to the beginning of each chain, in first-to-last
406 // lump order, so that the last lump of a given name appears first
407 // in any chain, observing pwad ordering rules. killough
408
409 for (i=0; i<numlumps; i++)
410 { // hash function:
411 int j = W_LumpNameHash(lumpinfo[i].name) % (unsigned) numlumps;
412 lumpinfo[i].next = lumpinfo[j].index; // Prepend to list
413 lumpinfo[j].index = i;
414 }
415 }
416
417 // End of lump hashing -- killough 1/31/98
418
419
420
421 // W_GetNumForName
422 // Calls W_CheckNumForName, but bombs out if not found.
423 //
424 int W_GetNumForName (const char* name) // killough -- const added
425 {
426 int i = W_CheckNumForName (name);
427 if (i == LUMP_NOT_FOUND)
428 I_Error("W_GetNumForName: %.8s not found", name);
429 return i;
430 }
431
432 const lumpinfo_t* W_GetLumpInfoByNum(int lump)
433 {
434 if (lump < 0 || lump >= numlumps)
435 I_Error("W_GetLumpInfoByNum: lump num %d out of range", lump);
436
437 return &lumpinfo[lump];
438 }
439
440 // W_CheckNumForNameInternal
441 // checks only internal resource
442 //
443 int W_CheckNumForNameInternal(const char *name)
444 {
445 int p;
446 for (p = LUMP_NOT_FOUND; (p = W_ListNumFromName(name, p)) != LUMP_NOT_FOUND; )
447 {
448 if (lumpinfo[p].flags == LUMP_PRBOOM)
449 {
450 return p;
451 }
452 }
453 return LUMP_NOT_FOUND;
454 }
455
456 // W_ListNumFromName
457 // calls W_FindNumFromName and returns the lumps in ascending order
458 //
459 int W_ListNumFromName(const char *name, int lump)
460 {
461 int i, next;
462
463 for (i = LUMP_NOT_FOUND; (next = W_FindNumFromName(name, i)) != LUMP_NOT_FOUND; i = next)
464 if (next == lump)
465 break;
466
467 return i;
468 }
469
470 // W_Init
471 // Loads each of the files in the wadfiles array.
472 // All files are optional, but at least one file
473 // must be found.
474 // Files with a .wad extension are idlink files
475 // with multiple lumps.
476 // Other files are single lumps with the base filename
477 // for the lump name.
478 // Lump names can appear multiple times.
479 // The name searcher looks backwards, so a later file
480 // does override all earlier ones.
481 //
482 // CPhipps - modified to use the new wadfiles array
483 //
484 wadfile_info_t *wadfiles=NULL;
485
486 size_t numwadfiles = 0; // CPhipps - size of the wadfiles array (dynamic, no limit)
487
488 void W_Init(void)
489 {
490 // CPhipps - start with nothing
491
492 numlumps = 0; lumpinfo = NULL;
493
494 { // CPhipps - new wadfiles array used
495 // open all the files, load headers, and count lumps
496 int i;
497 for (i=0; (size_t)i<numwadfiles; i++)
498 W_AddFile(&wadfiles[i]);
499 }
500
501 if (!numlumps)
502 {
503 if (!MainLumpCache) return;
504 I_Error ("W_Init: No files found");
505 }
506
507 //jff 1/23/98
508 // get all the sprites and flats into one marked block each
509 // killough 1/24/98: change interface to use M_START/M_END explicitly
510 // killough 4/17/98: Add namespace tags to each entry
511 // killough 4/4/98: add colormap markers
512 W_CoalesceMarkedResource("S_START", "S_END", ns_sprites);
513 W_CoalesceMarkedResource("F_START", "F_END", ns_flats);
514 W_CoalesceMarkedResource("C_START", "C_END", ns_colormaps);
515 W_CoalesceMarkedResource("B_START", "B_END", ns_prboom);
516 W_CoalesceMarkedResource("HI_START", "HI_END", ns_hires);
517
518 // killough 1/31/98: initialize lump hash table
519 W_HashLumps();
520
521 /* cph 2001/07/07 - separated cache setup */
522 lprintf(LO_DEBUG, "W_InitCache\n");
523 W_InitCache();
524
525 V_FreePlaypal();
526 }
527
528 //
529 // W_LumpLength
530 // Returns the buffer size needed to load the given lump.
531 //
532 int W_LumpLength (int lump)
533 {
534 if (lump >= numlumps)
535 I_Error ("W_LumpLength: %i >= numlumps",lump);
536 return lumpinfo[lump].size;
537 }
538
539 int W_SafeLumpLength (int lump)
540 {
541 return W_LumpNumExists(lump) ? lumpinfo[lump].size : 0;
542 }
543
544 const char *W_LumpName(int lump)
545 {
546 return W_LumpNumExists(lump) ? lumpinfo[lump].name : NULL;
547 }
548
549 //
550 // W_ReadLump
551 // Loads the lump into the given buffer,
552 // which must be >= W_LumpLength().
553 //
554
555 void W_ReadLump(int lump, void *dest)
556 {
557 lumpinfo_t *l = lumpinfo + lump;
558
559 #ifdef RANGECHECK
560 if (lump >= numlumps)
561 I_Error ("W_ReadLump: %i >= numlumps",lump);
562 #endif
563
564 {
565 if (l->wadfile)
566 {
567 lseek(l->wadfile->handle, l->position, SEEK_SET);
568 I_Read(l->wadfile->handle, dest, l->size);
569 }
570 }
571 }
572
573 char* W_ReadLumpToString(int lump)
574 {
575 char* buffer = NULL;
576 lumpinfo_t *l = lumpinfo + lump;
577
578 if (lump >= 0 && lump < numlumps && l->wadfile)
579 {
580 buffer = Z_Malloc(l->size + 1);
581 lseek(l->wadfile->handle, l->position, SEEK_SET);
582 I_Read(l->wadfile->handle, buffer, l->size);
583 buffer[l->size] = '\0';
584 }
585
586 return buffer;
587 }
588
589 int W_LumpNumInPortWad(int lump) {
590 const lumpinfo_t *info;
591 size_t name_length, default_name_length;
592
593 info = W_GetLumpInfoByNum(lump);
594 name_length = strlen(info->wadfile->name);
595 default_name_length = strlen(WAD_DATA);
596
597 return name_length >= default_name_length &&
598 !strcmp(info->wadfile->name + name_length - default_name_length, WAD_DATA);
599 }
600
601 const void *W_SafeLumpByNum(int lump)
602 {
603 return W_LumpNumExists(lump) ? W_LumpByNum(lump) : NULL;
604 }
605
606 int W_LumpNumExists(int lump)
607 {
608 return lump != LUMP_NOT_FOUND && lump < numlumps;
609 }
610
611 int W_PWADLumpNumExists(int lump)
612 {
613 return W_LumpNumExists(lump) && (lumpinfo[lump].source == source_pwad);
614 }
615
616 int W_LumpNameExists(const char *name)
617 {
618 return W_CheckNumForName(name) != LUMP_NOT_FOUND;
619 }
620
621 int W_LumpNameExists2(const char *name, int ns)
622 {
623 return W_CheckNumForName2(name, ns) != LUMP_NOT_FOUND;
624 }
625
626 int W_PWADLumpNameExists(const char *name)
627 {
628 return W_PWADLumpNumExists(W_CheckNumForName(name));
629 }
630
631 int W_PWADMapExists(void)
632 {
633 return W_PWADLumpNameExists("THINGS") || W_PWADLumpNameExists("TEXTMAP");
634 }
635
636 void W_Shutdown(void)
637 {
638 int i;
639
640 W_DoneCache();
641
642 for (i = 0; i < numwadfiles; ++i)
643 {
644 if (wadfiles[i].handle > 0)
645 {
646 close(wadfiles[i].handle);
647 wadfiles[i].handle = -1;
648 }
649 }
650 }
651
652 void dsda_ResetInitLumpCache(void)
653 {
654 W_Shutdown();
655
656 wadfiles = NULL;
657 numwadfiles = 0;
658 lumpinfo = NULL;
659 numlumps = 0;
660 }