1 // SONIC ROBO BLAST 2
2 //-----------------------------------------------------------------------------
3 // Copyright (C) 1993-1996 by id Software, Inc.
4 // Copyright (C) 1998-2000 by DooM Legacy Team.
5 // Copyright (C) 1999-2020 by Sonic Team Junior.
6 //
7 // This program is free software distributed under the
8 // terms of the GNU General Public License, version 2.
9 // See the 'LICENSE' file for more details.
10 //-----------------------------------------------------------------------------
11 /// \file  w_wad.h
12 /// \brief WAD I/O functions, wad resource definitions (some)
13 
14 #ifndef __W_WAD__
15 #define __W_WAD__
16 
17 #ifdef HWRENDER
18 #include "hardware/hw_data.h"
19 #endif
20 
21 #ifdef __GNUG__
22 #pragma interface
23 #endif
24 
25 // a raw entry of the wad directory
26 // NOTE: This sits here and not in w_wad.c because p_setup.c makes use of it to load map WADs inside PK3s.
27 #if defined(_MSC_VER)
28 #pragma pack(1)
29 #endif
30 typedef struct
31 {
32 	UINT32 filepos; // file offset of the resource
33 	UINT32 size; // size of the resource
34 	char name[8]; // name of the resource
35 } ATTRPACK filelump_t;
36 #if defined(_MSC_VER)
37 #pragma pack()
38 #endif
39 
40 
41 // ==============================================================
42 //               WAD FILE STRUCTURE DEFINITIONS
43 // ==============================================================
44 
45 // header of a wad file
46 typedef struct
47 {
48 	char identification[4]; // should be "IWAD" or "PWAD"
49 	UINT32 numlumps; // how many resources
50 	UINT32 infotableofs; // the 'directory' of resources
51 } wadinfo_t;
52 
53 // Available compression methods for lumps.
54 typedef enum
55 {
56 	CM_NOCOMPRESSION,
57 #ifdef HAVE_ZLIB
58 	CM_DEFLATE,
59 #endif
60 	CM_LZF,
61 	CM_UNSUPPORTED
62 } compmethod;
63 
64 //  a memory entry of the wad directory
65 typedef struct
66 {
67 	unsigned long position; // filelump_t filepos
68 	unsigned long disksize; // filelump_t size
69 	char name[9];           // filelump_t name[] e.g. "LongEntr"
70 	char *longname;         //                   e.g. "LongEntryName"
71 	char *fullname;         //                   e.g. "Folder/Subfolder/LongEntryName.extension"
72 	size_t size;            // real (uncompressed) size
73 	compmethod compression; // lump compression method
74 } lumpinfo_t;
75 
76 // =========================================================================
77 //                         'VIRTUAL' RESOURCES
78 // =========================================================================
79 
80 typedef struct {
81 	char name[9];
82 	UINT8* data;
83 	size_t size;
84 } virtlump_t;
85 
86 typedef struct {
87 	size_t numlumps;
88 	virtlump_t* vlumps;
89 } virtres_t;
90 
91 virtres_t* vres_GetMap(lumpnum_t);
92 void vres_Free(virtres_t*);
93 virtlump_t* vres_Find(const virtres_t*, const char*);
94 
95 // =========================================================================
96 //                         DYNAMIC WAD LOADING
97 // =========================================================================
98 
99 #define MAX_WADPATH 512
100 #define MAX_WADFILES 48 // maximum of wad files used at the same time
101 // (there is a max of simultaneous open files anyway, and this should be plenty)
102 
103 #define lumpcache_t void *
104 
105 // Resource type of the WAD. Yeah, I know this sounds dumb, but I'll leave it like this until I clean up the code further.
106 typedef enum restype
107 {
108 	RET_WAD,
109 	RET_SOC,
110 	RET_LUA,
111 	RET_PK3,
112 	RET_UNKNOWN,
113 } restype_t;
114 
115 typedef struct wadfile_s
116 {
117 	char *filename;
118 	restype_t type;
119 	lumpinfo_t *lumpinfo;
120 	lumpcache_t *lumpcache;
121 	lumpcache_t *patchcache;
122 	UINT16 numlumps; // this wad's number of resources
123 	FILE *handle;
124 	UINT32 filesize; // for network
125 	UINT8 md5sum[16];
126 
127 	boolean important; // also network - !W_VerifyNMUSlumps
128 } wadfile_t;
129 
130 #define WADFILENUM(lumpnum) (UINT16)((lumpnum)>>16) // wad flumpnum>>16) // wad file number in upper word
131 #define LUMPNUM(lumpnum) (UINT16)((lumpnum)&0xFFFF) // lump number for this pwad
132 
133 extern UINT16 numwadfiles;
134 extern wadfile_t *wadfiles[MAX_WADFILES];
135 
136 // =========================================================================
137 
138 void W_Shutdown(void);
139 
140 // Opens a WAD file. Returns the FILE * handle for the file, or NULL if not found or could not be opened
141 FILE *W_OpenWadFile(const char **filename, boolean useerrors);
142 // Load and add a wadfile to the active wad files, returns numbers of lumps, INT16_MAX on error
143 UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup);
144 
145 // W_InitMultipleFiles exits if a file was not found, but not if all is okay.
146 void W_InitMultipleFiles(char **filenames);
147 
148 const char *W_CheckNameForNumPwad(UINT16 wad, UINT16 lump);
149 const char *W_CheckNameForNum(lumpnum_t lumpnum);
150 
151 UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump); // checks only in one pwad
152 UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump);
153 
154 /* Find the first lump after F_START for instance. */
155 UINT16 W_CheckNumForMarkerStartPwad(const char *name, UINT16 wad, UINT16 startlump);
156 
157 UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump);
158 UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlump);
159 UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump);
160 
161 lumpnum_t W_CheckNumForMap(const char *name);
162 lumpnum_t W_CheckNumForName(const char *name);
163 lumpnum_t W_CheckNumForLongName(const char *name);
164 lumpnum_t W_GetNumForName(const char *name); // like W_CheckNumForName but I_Error on LUMPERROR
165 lumpnum_t W_GetNumForLongName(const char *name);
166 lumpnum_t W_CheckNumForNameInBlock(const char *name, const char *blockstart, const char *blockend);
167 UINT8 W_LumpExists(const char *name); // Lua uses this.
168 
169 size_t W_LumpLengthPwad(UINT16 wad, UINT16 lump);
170 size_t W_LumpLength(lumpnum_t lumpnum);
171 
172 boolean W_IsLumpWad(lumpnum_t lumpnum); // for loading maps from WADs in PK3s
173 boolean W_IsLumpFolder(UINT16 wad, UINT16 lump); // for detecting folder "lumps"
174 
175 #ifdef HAVE_ZLIB
176 void zerr(int ret); // zlib error checking
177 #endif
178 
179 size_t W_ReadLumpHeaderPwad(UINT16 wad, UINT16 lump, void *dest, size_t size, size_t offset);
180 size_t W_ReadLumpHeader(lumpnum_t lump, void *dest, size_t size, size_t offest); // read all or a part of a lump
181 void W_ReadLumpPwad(UINT16 wad, UINT16 lump, void *dest);
182 void W_ReadLump(lumpnum_t lump, void *dest);
183 
184 void *W_CacheLumpNumPwad(UINT16 wad, UINT16 lump, INT32 tag);
185 void *W_CacheLumpNum(lumpnum_t lump, INT32 tag);
186 void *W_CacheLumpNumForce(lumpnum_t lumpnum, INT32 tag);
187 
188 boolean W_IsLumpCached(lumpnum_t lump, void *ptr);
189 boolean W_IsPatchCached(lumpnum_t lump, void *ptr);
190 
191 void *W_CacheLumpName(const char *name, INT32 tag);
192 void *W_CachePatchName(const char *name, INT32 tag);
193 void *W_CachePatchLongName(const char *name, INT32 tag);
194 
195 // Returns either a Software patch, or an OpenGL patch.
196 // Performs any necessary conversions from PNG images.
197 void *W_CachePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag);
198 void *W_CachePatchNum(lumpnum_t lumpnum, INT32 tag);
199 
200 // Returns a Software patch.
201 // Performs any necessary conversions from PNG images.
202 void *W_CacheSoftwarePatchNumPwad(UINT16 wad, UINT16 lump, INT32 tag);
203 void *W_CacheSoftwarePatchNum(lumpnum_t lumpnum, INT32 tag);
204 
205 void W_UnlockCachedPatch(void *patch);
206 
207 void W_VerifyFileMD5(UINT16 wadfilenum, const char *matchmd5);
208 
209 int W_VerifyNMUSlumps(const char *filename, boolean exit_on_error);
210 
211 #endif // __W_WAD__
212