1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id: w_wad.h 1544 2020-08-22 02:40:35Z wesleyjohnson $
5 //
6 // Copyright (C) 1993-1996 by id Software, Inc.
7 // Portions Copyright (C) 1998-2016 by DooM Legacy Team.
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 //
20 // $Log: w_wad.h,v $
21 // Revision 1.13  2001/05/16 17:12:52  crashrl
22 // Added md5-sum support, removed recursiv wad search
23 //
24 // Revision 1.12  2001/02/28 17:50:55  bpereira
25 // Revision 1.11  2001/02/24 13:35:21  bpereira
26 //
27 // Revision 1.10  2001/01/25 22:15:44  bpereira
28 // added heretic support
29 //
30 // Revision 1.9  2000/10/04 16:19:24  hurdler
31 // Change all those "3dfx names" to more appropriate names
32 //
33 // Revision 1.8  2000/09/28 20:57:19  bpereira
34 // Revision 1.7  2000/08/31 14:30:56  bpereira
35 // Revision 1.6  2000/04/16 18:38:07  bpereira
36 // Revision 1.5  2000/04/13 23:47:48  stroggonmeth
37 //
38 // Revision 1.4  2000/04/11 19:07:25  stroggonmeth
39 // Finished my logs, fixed a crashing bug.
40 //
41 // Revision 1.3  2000/04/04 00:32:48  stroggonmeth
42 // Initial Boom compatability plus few misc changes all around.
43 //
44 // Revision 1.2  2000/02/27 00:42:11  hurdler
45 // Revision 1.1.1.1  2000/02/22 20:32:33  hurdler
46 // Initial import into CVS (v1.29 pr3)
47 //
48 //
49 // DESCRIPTION:
50 //      WAD I/O functions, wad resource definitions (some).
51 //
52 //-----------------------------------------------------------------------------
53 
54 
55 #ifndef W_WAD_H
56 #define W_WAD_H
57 
58 #include "doomdef.h"
59   // ZIPWAD
60 #include "doomtype.h"
61 
62 #include "r_defs.h"
63 // patch_t
64 
65 #ifdef HWRENDER
66 #include "hardware/hw_data.h"
67 #endif
68 
69 #ifdef __GNUG__
70 #pragma interface
71 #endif
72 
73 
74 // ==============================================================
75 //               WAD FILE STRUCTURE DEFINITIONS
76 // ==============================================================
77 
78 // Wad header format.
79 typedef struct
80 {
81     char       identification[4];   // should be "IWAD" or "PWAD"
82     uint32_t   numlumps;            // how many resources
83     uint32_t   infotableofs;        // the 'directory' of resources
84 } wadinfo_t;
85 
86 // Wad lump format.
87 typedef struct
88 {
89     uint32_t   filepos;             // file offset of the resource
90     uint32_t   size;                // size of the resource
91     char       name[8];             // name of the resource
92 } filelump_t;
93 
94 
95 // in memory : initted at game startup
96 
97 // [WDJ] Fast lump name compare, transform to numerical.
98 typedef union {
99     char  s[9];  // extra byte for 0 term, needed for strupr
100     uint64_t  namecode;  // name as numerical  (8 bytes)
101 } lump_name_t;
102 
103 void  numerical_name( const char * name, lump_name_t * numname );
104 
105 
106 
107 // [WDJ] Track the lump namespace (Wad markers)
108 // Lump namespace
109 typedef enum {
110    LNS_any,    // find lump in any namespace
111  // Boom namespaces
112    LNS_global, // any lump not in a namespace section
113    LNS_sprite, // S_START to S_END, or SS_START to SS_END
114    LNS_flat,   // F_START to F_END, or FF_START to FF_END
115    LNS_colormap,  // C_START to C_END
116  // Other namespaces
117    LNS_patch,  // PP_START to PP_END
118    LNS_dehacked,
119    LNS_legacy
120 } lump_namespace_e;
121 
122 
123 // Lump lookup info.
124 typedef struct
125 {
126     char        name[8];            // filelump_t name[]
127     uint32_t    position;           // filelump_t filepos
128     uint32_t    size;               // filelump_t size
129     lump_namespace_e  lump_namespace;
130 } lumpinfo_t;
131 
132 
133 // =========================================================================
134 //                         DYNAMIC WAD LOADING
135 // =========================================================================
136 
137 // typedef int32_t   lumpnum_t;  // see doomdefs.h
138 // Format:
139 //  0x80000000 : NO_LUMP, tests as < 0
140 //  0x7FFF0000 : wad number field
141 //  0x0000FFFF : lump number field
142 typedef enum {
143   NO_LUMP = (lumpnum_t) -1,
144 } lump_spec_e;
145 
146 // [WDJ] Compatible with older signed tests for -1.
147 #define VALID_LUMP(lump)       (lump>=0)
148 //#define VALID_LUMP(lump)       (lump!=NO_LUMP)
149 
150 // wad file number in upper word
151 #define WADFILENUM(lump)       ((lump)>>16)
152 // lump number for this pwad
153 #define LUMPNUM(lump)          ((lump)&0xffff)
154 // Combined wad and lump parameter.
155 #define WADLUMP(wad,lump)      (((wad)<<16)+(lump))
156 // This is tricky math that is dependent upon the lumpnum representation.
157 #define ADD_TO_LUMPNUM(lump,offset)    ((lump)+(offset))
158 
159 // MAX_WADPATH moved to doomdef.h, for other users.
160 
161 // Maximum of wad files used at the same time (there is a max of
162 // simultaneous open files anyway, and this should be plenty)
163 #define MAX_WADFILES  32
164 
165 #define lumpcache_t  void*
166 
167 typedef enum {
168     FC_other,  // generic file
169     FC_wad,
170     FC_deh,
171     FC_bex,
172     FC_zip,
173 } file_classify_e;
174 
175 typedef struct wadfile_s
176 {
177     char *           filename;
178     lumpinfo_t *     lumpinfo;
179     lumpcache_t *    lumpcache;
180 #ifdef HWRENDER
181     MipPatch_t *     hwrcache;   // patches are cached in renderer's native format
182 #endif
183 #ifdef ZIPWAD
184     // Immediately after the archive wadfile_t are the wadfile_t for files within the archive.
185     byte             classify;   // file_classify_e
186     byte             archive_parent;    // archive wadfile index,  0xFF= unused
187     byte             archive_num_wadfile; // number of wadfile in this archive, 0= unused
188 #endif
189     int              numlumps;   // this wad's number of resources
190     int              handle;     // from system
191     uint32_t         filesize;   // for network
192     unsigned char    md5sum[16];
193 } wadfile_t;
194 
195 extern  int          numwadfiles;
196 extern  wadfile_t*   wadfiles[MAX_WADFILES];
197 
198 // Return the wadfile info for the lumpnum
199 wadfile_t * lumpnum_to_wad( lumpnum_t lumpnum );
200 
201 // [WDJ] Indicates cache miss, new lump read requires endian fixing.
202 extern boolean lump_read;
203 
204 // return file_classify_e
205 byte  W_filename_classify( const char * filename );
206 
207 
208 
209 // =========================================================================
210 // Patch handling
211 
212 // Patch Header
213 typedef struct
214 {
215     uint16_t            width;          // bounding box size
216     uint16_t            height;
217     int16_t             leftoffset;     // pixels to the left of origin
218     int16_t             topoffset;      // pixels below the origin
219 } pat_hdr_t;
220 
221 // =========================================================================
222 
223 void W_Shutdown(void);
224 
225 // load and add a wadfile to the active wad files, return wad file number
226 // (you can get wadfile_t pointer: the return value indexes wadfiles[])
227 int     W_Load_WadFile (const char *filename);
228 
229 //added 4-1-98 initmultiplefiles now return 1 if all ok 0 else
230 //             so that it stops with a message if a file was not found
231 //             but not if all is ok.
232 int     W_Init_MultipleFiles( char** filenames );
233 
234 //  WADFILE_RELOAD
235 void    W_Reload (void);
236 
237 //  Return lump id, or NO_LUMP if name not found.
238 lumpnum_t  W_Check_Namespace (const char* name, lump_namespace_e within_namespace);
239 //  Return lump id, or NO_LUMP if name not found.
240 lumpnum_t  W_CheckNumForName (const char* name);
241 // this one checks only in one pwad
242 lumpnum_t  W_CheckNumForNamePwad (const char* name, int wadid, int startlump);
243 lumpnum_t  W_GetNumForName (const char* name);
244 
245 // modified version that scan forwards
246 // used to get original lump instead of patched using -file
247 lumpnum_t  W_CheckNumForNameFirst (const char* name);
248 lumpnum_t  W_GetNumForNameFirst (const char* name);
249 
250 int     W_LumpLength (lumpnum_t lump);
251 //added:06-02-98: read all or a part of a lump size==0 meen read all
252 int     W_ReadLumpHeader (lumpnum_t lump, void* dest, int size);
253 //added:06-02-98: now calls W_ReadLumpHeader() with full lump size
254 void    W_ReadLump (lumpnum_t lump, void *dest);
255 
256 //  ztag : the Zone memory allocation tag (see memtag_e)
257 //  lump : lump number with embedded wad number
258 
259 void*   W_CacheLumpNum ( lumpnum_t lumpnum, int ztag );
260 void*   W_CacheLumpName (const char* name, int ztag);
261 
262 void*   W_CachePatchName (const char* name, int ztag);
263 
264 void*   W_CachePatchNum ( lumpnum_t lump, int ztag);  // return a patch_t
265 void*   W_CachePatchNum_Endian ( lumpnum_t lump, int ztag );
266 #ifdef HWRENDER
267 // [WDJ] Called from hardware render for special mapped sprites
268 void*   W_CacheMappedPatchNum ( lumpnum_t lumpnum, uint32_t drawflags );
269 #endif
270 
271 
272 // Release patches made with W_CachePatchNum, W_CachePatchName.
273 void W_release_patch( patch_t * patch );
274 
275 // These are used for loading, and releasing patches.
276 typedef struct {
277    patch_t ** patch_owner;  // ptr to patch owner
278    char     * name;
279 } load_patch_t;
280 
281 
282 //  pl : a patch list, maybe offset into a patch list
283 void load_patch_list( load_patch_t * pl );
284 
285 //  pl : a patch list, maybe offset into a patch list
286 void release_patch_list( load_patch_t * pl );
287 
288 //  pp : an array of patch_t ptr
289 //  count : number of patches to release
290 void release_patch_array( patch_t ** pp, int count );
291 
292 
293 // return a pic_t
294 void*   W_CacheRawAsPic( lumpnum_t lumpnum, int width, int height, int ztag);
295 
296 // Cache and endian convert a pic_t
297 void*   W_CachePicNum( lumpnum_t lumpnum, int ztag );
298 void*   W_CachePicName( const char* name, int ztag );
299 
300 
301 // [WDJ] Return a sum unique to a lump, to detect replacements.
302 // The lumpptr must be to a Z_Malloc lump.
303 uint64_t  W_lump_checksum( void* lumpptr );
304 
305 //SoM: 4/13/2000: Store lists of lumps for F_START/F_END etc.
306 typedef struct {
307   int         wadfile;
308   int         firstlump;
309   int         numlumps;
310 } lumplist_t;
311 
312 void    W_Load_DehackedLumps( int wadnum );
313 
314 
315 
316 #ifdef ZIPWAD
317 
318 // zip wad
319 typedef enum {
320     FH_none,
321     FH_file = 0x70, // generic file
322     FH_zip_file = 0x80,
323     FH_zip_archive = 0xF0,
324     FH_mask = 0xF0,
325 } file_handle_e;
326 
327 extern byte  ziplib_present;
328 extern byte  archive_open;
329 extern byte  archive_filenum;
330 
331 void  WZ_available( void );
332 byte  WZ_filename_cmp( const char * filename1, const char * filename2 );
333 byte  WZ_find_file_in_archive( const char * filename, const char * archive_name );
334 // Return filename with extension, otherwise NULL.
335 char *  WZ_make_name_with_extension( const char * filename, const char * extension, /*OUT*/ char * buf );
336 void    WZ_save_archive_name( const char * filename );
337 char *  WZ_make_archive_name( const char * filename );
338 unsigned int  WZ_filesize( const char * filename );
339 void  WZ_open_archive( const char * archive_name );
340 void  WZ_close_archive( void );
341 byte  WZ_open_file_z( const char * filename );
342 byte  WZ_open( const char * filename );
343 void  WZ_close( byte handle );
344 void  WZ_close_all( void );
345 int  WZ_seek( byte handle, uint32_t offset );
346 int  WZ_read( byte handle, uint32_t read_count, /*OUT*/ byte * dest );
347 int  WZ_read_archive_file( uint32_t offset, uint32_t read_size, /*OUT*/ byte * dest );
348 int  WZ_read_wadfile_from_archive_file_offset( byte fn, wadfile_t * wf, uint32_t offset, uint32_t read_size, /*OUT*/ byte * dest );
349 int  WZ_Load_zip_archive( const char * filename, int as_archive_filenum );
350 filestatus_e  WZ_md5_stream( const char * filename, byte * digest_block );
351 #endif
352 
353 
354 #endif // __W_WAD__
355