1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
6 // Copyright (C) 1993-1996 by id Software, Inc.
8 // This source is available for distribution and/or modification
9 // only under the terms of the DOOM Source Code License as
10 // published by id Software. All rights reserved.
12 // The source is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
20 // Handles WAD file header, directory, lump I/O.
22 //-----------------------------------------------------------------------------
26 rcsid
[] = "$Id: w_wad.c,v 1.5 1997/02/03 16:47:57 b1 Exp $";
40 #pragma implementation "w_wad.h"
53 // Location of each lump on disk.
60 #if defined(linux) || defined(__BEOS__) || defined(__SVR4)
63 while (*s
) { *s
= toupper(*s
); s
++; }
67 int filelength (FILE *handle
)
69 unsigned long pos
, size
;
72 printf("Position was %lu\n", pos
);
73 fseek(handle
, 0, SEEK_END
);
75 fseek(handle
, pos
, SEEK_SET
);
76 printf("Size is %lu\n", size
);
90 src
= path
+ strlen(path
) - 1;
92 // back up until a \ or the start
100 // copy up to eight characters
104 while (*src
&& *src
!= '.')
107 I_Error ("Filename base of %s >8 chars",path
);
109 *dest
++ = toupper((int)*src
++);
118 // LUMP BASED ROUTINES.
123 // All files are optional, but at least one file must be
124 // found (PWAD, if all required lumps are present).
125 // Files with a .wad extension are wadlink files
126 // with multiple lumps.
127 // Other files are single lumps with the base filename
128 // for the lump name.
130 // If filename starts with a tilde, the file is handled
131 // specially to allow map reloads.
132 // But: the reload feature is a fragile hack...
136 extern void *alloca(int);
140 #endif /* __BEOS__ */
146 void W_AddFile (char *filename
)
154 filelump_t
* fileinfo
;
155 filelump_t singleinfo
;
158 // open the file and add to directory
160 // handle reload indicator.
161 if (filename
[0] == '~')
164 reloadname
= filename
;
165 reloadlump
= numlumps
;
168 if ( (handle
= fopen (filename
,"rb")) == NULL
)
170 printf (" couldn't open %s\n",filename
);
174 printf (" adding %s\n",filename
);
175 startlump
= numlumps
;
177 if (I_strncasecmp (filename
+strlen(filename
)-3 , "wad", 3 ) )
180 fileinfo
= &singleinfo
;
181 singleinfo
.filepos
= 0;
182 singleinfo
.size
= LONG(filelength(handle
));
183 ExtractFileBase (filename
, singleinfo
.name
);
189 fread (&header
, 1, sizeof(header
), handle
);
190 if (strncmp(header
.identification
,"IWAD",4))
193 if (strncmp(header
.identification
,"PWAD",4))
195 I_Error ("Wad file %s doesn't have IWAD "
196 "or PWAD id\n", filename
);
199 // ???modifiedgame = true;
201 header
.numlumps
= LONG(header
.numlumps
);
202 header
.infotableofs
= LONG(header
.infotableofs
);
203 length
= header
.numlumps
*sizeof(filelump_t
);
204 fileinfo
= alloca (length
);
205 fseek (handle
, header
.infotableofs
, SEEK_SET
);
206 fread (fileinfo
, 1, length
, handle
);
207 numlumps
+= header
.numlumps
;
212 lumpinfo
= realloc (lumpinfo
, numlumps
*sizeof(lumpinfo_t
));
215 I_Error ("Couldn't realloc lumpinfo");
217 lump_p
= &lumpinfo
[startlump
];
219 storehandle
= reloadname
? -1 : (int)handle
;
221 for (i
=startlump
; i
<numlumps
; i
++,lump_p
++, fileinfo
++)
223 lump_p
->handle
= storehandle
;
224 lump_p
->position
= LONG(fileinfo
->filepos
);
225 lump_p
->size
= LONG(fileinfo
->size
);
226 strncpy (lump_p
->name
, fileinfo
->name
, 8);
238 // Flushes any of the reloadable lumps in memory
239 // and reloads the directory.
249 filelump_t
* fileinfo
;
254 if ( (handle
= fopen (reloadname
,"rb")) == NULL
)
255 I_Error ("W_Reload: couldn't open %s",reloadname
);
257 fread (&header
, 1, sizeof(header
), handle
);
258 lumpcount
= LONG(header
.numlumps
);
259 header
.infotableofs
= LONG(header
.infotableofs
);
260 length
= lumpcount
*sizeof(filelump_t
);
261 fileinfo
= alloca (length
);
262 fseek (handle
, header
.infotableofs
, SEEK_SET
);
263 fread (fileinfo
, 1, length
, handle
);
266 lump_p
= &lumpinfo
[reloadlump
];
269 i
<reloadlump
+lumpcount
;
270 i
++,lump_p
++, fileinfo
++)
273 Z_Free (lumpcache
[i
]);
275 lump_p
->position
= LONG(fileinfo
->filepos
);
276 lump_p
->size
= LONG(fileinfo
->size
);
285 // W_InitMultipleFiles
286 // Pass a null terminated list of files to use.
287 // All files are optional, but at least one file
289 // Files with a .wad extension are idlink files
290 // with multiple lumps.
291 // Other files are single lumps with the base filename
292 // for the lump name.
293 // Lump names can appear multiple times.
294 // The name searcher looks backwards, so a later file
295 // does override all earlier ones.
297 void W_InitMultipleFiles (char** filenames
)
301 // open all the files, load headers, and count lumps
304 // will be realloced as lumps are added
305 lumpinfo
= malloc(1);
307 for ( ; *filenames
; filenames
++)
308 W_AddFile (*filenames
);
311 I_Error ("W_InitFiles: no files found");
314 size
= numlumps
* sizeof(*lumpcache
);
315 lumpcache
= malloc (size
);
318 I_Error ("Couldn't allocate lumpcache");
320 memset (lumpcache
,0, size
);
328 // Just initialize from a single file.
330 void W_InitFile (char* filename
)
336 W_InitMultipleFiles (names
);
344 int W_NumLumps (void)
353 // Returns -1 if name not found.
356 int W_CheckNumForName (char* name
)
368 // make the name into two integers for easy compares
369 strncpy (name8
.s
,name
,8);
371 // in case the name was a fill 8 chars
381 // scan backwards so patch lump files take precedence
382 lump_p
= lumpinfo
+ numlumps
;
384 while (lump_p
-- != lumpinfo
)
386 if ( *(int *)lump_p
->name
== v1
387 && *(int *)&lump_p
->name
[4] == v2
)
389 return lump_p
- lumpinfo
;
402 // Calls W_CheckNumForName, but bombs out if not found.
404 int W_GetNumForName (char* name
)
408 i
= W_CheckNumForName (name
);
411 I_Error ("W_GetNumForName: %s not found!", name
);
419 // Returns the buffer size needed to load the given lump.
421 int W_LumpLength (int lump
)
423 if (lump
>= numlumps
)
424 I_Error ("W_LumpLength: %i >= numlumps",lump
);
426 return lumpinfo
[lump
].size
;
433 // Loads the lump into the given buffer,
434 // which must be >= W_LumpLength().
445 if (lump
>= numlumps
)
446 I_Error ("W_ReadLump: %i >= numlumps",lump
);
450 // ??? I_BeginRead ();
454 // reloadable file, so use open / read / close
455 if ( (handle
= fopen (reloadname
,"rb")) == NULL
)
456 I_Error ("W_ReadLump: couldn't open %s",reloadname
);
459 handle
= (FILE *)l
->handle
;
461 fseek (handle
, l
->position
, SEEK_SET
);
462 c
= fread (dest
, 1, l
->size
, handle
);
465 I_Error ("W_ReadLump: only read %i of %i on lump %i",
487 if ((unsigned)lump
>= numlumps
)
488 I_Error ("W_CacheLumpNum: %i >= numlumps",lump
);
490 if (!lumpcache
[lump
])
494 //printf ("cache miss on lump %i\n",lump);
495 ptr
= Z_Malloc (W_LumpLength (lump
), tag
, &lumpcache
[lump
]);
496 W_ReadLump (lump
, lumpcache
[lump
]);
500 //printf ("cache hit on lump %i\n",lump);
501 Z_ChangeTag (lumpcache
[lump
],tag
);
504 return lumpcache
[lump
];
517 return W_CacheLumpNum (W_GetNumForName(name
), tag
);
527 void W_Profile (void)
538 for (i
=0 ; i
<numlumps
; i
++)
548 block
= (memblock_t
*) ( (byte
*)ptr
- sizeof(memblock_t
));
549 if (block
->tag
< PU_PURGELEVEL
)
554 info
[i
][profilecount
] = ch
;
558 f
= fopen ("waddump.txt","w");
561 for (i
=0 ; i
<numlumps
; i
++)
563 memcpy (name
,lumpinfo
[i
].name
,8);
565 for (j
=0 ; j
<8 ; j
++)
572 fprintf (f
,"%s ",name
);
574 for (j
=0 ; j
<profilecount
; j
++)
575 fprintf (f
," %c",info
[i
][j
]);