-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathw_wad.c
306 lines (244 loc) · 6.71 KB
/
w_wad.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
*
* PrBoom: a Doom port merged with LxDoom and LSDLDoom
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* Copyright (C) 1999-2001 by
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
* Copyright 2005, 2006 by
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
* Copyright 2023, 2024 by
* Frenkel Smeijers
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* DESCRIPTION:
* Handles WAD file header, directory, lump I/O.
*
*-----------------------------------------------------------------------------
*/
// use config.h if autoconf made one -- josh
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdint.h>
#include "compiler.h"
#include "d_player.h"
#include "doomtype.h"
#include "i_system.h"
#include "w_wad.h"
#include "globdata.h"
//
// TYPES
//
typedef struct
{
int32_t filepos;
uint16_t size;
int16_t filler; // always zero
char name[8];
} filelump_t;
//
// GLOBALS
//
static FILE* fileWAD;
static int16_t numlumps;
static filelump_t __far* fileinfo;
static void __far*__far* lumpcache;
//
// LUMP BASED ROUTINES.
//
#define BUFFERSIZE 512
static void _ffread(void __far* ptr, uint16_t size, FILE* fp)
{
uint8_t __far* dest = ptr;
uint8_t buffer[BUFFERSIZE];
while (size >= BUFFERSIZE)
{
fread(&buffer[0], BUFFERSIZE, 1, fp);
_fmemcpy(dest, &buffer[0], BUFFERSIZE);
dest += BUFFERSIZE;
size -= BUFFERSIZE;
}
if (size > 0)
{
fread(&buffer[0], size, 1, fp);
_fmemcpy(dest, &buffer[0], size);
}
}
typedef struct
{
char identification[4]; // Should be "IWAD" or "PWAD".
int16_t numlumps;
int16_t filler; // always zero
int32_t infotableofs;
} wadinfo_t;
void W_Init(void)
{
#if BYTE_ORDER == LITTLE_ENDIAN
#define WAD_UPPERCASE "DOOMTD3L.WAD"
#define WAD_LOWERCASE "doomtd3l.wad"
#elif BYTE_ORDER == BIG_ENDIAN
#define WAD_UPPERCASE "DOOMTD3B.WAD"
#define WAD_LOWERCASE "doomtd3b.wad"
#else
#error unknown byte order
#endif
fileWAD = fopen(WAD_UPPERCASE, "rb");
if (fileWAD == NULL)
{
fileWAD = fopen(WAD_LOWERCASE, "rb");
if (fileWAD == NULL)
I_Error("Can't open " WAD_UPPERCASE);
}
wadinfo_t header;
fseek(fileWAD, 0, SEEK_SET);
fread(&header, sizeof(header), 1, fileWAD);
fileinfo = Z_MallocStatic(header.numlumps * sizeof(filelump_t));
fseek(fileWAD, header.infotableofs, SEEK_SET);
_ffread(fileinfo, sizeof(filelump_t) * header.numlumps, fileWAD);
lumpcache = Z_MallocStatic(header.numlumps * sizeof(*lumpcache));
_fmemset(lumpcache, 0, header.numlumps * sizeof(*lumpcache));
numlumps = header.numlumps;
}
const char __far* PUREFUNC W_GetNameForNum(int16_t num)
{
return fileinfo[num].name;
}
//
// W_LumpLength
// Returns the buffer size needed to load the given lump.
//
uint16_t PUREFUNC W_LumpLength(int16_t num)
{
return fileinfo[num].size;
}
// W_GetNumForName
// bombs out if not found.
//
int16_t PUREFUNC W_GetNumForName(const char *name)
{
char name8[8];
strncpy(name8, name, 8);
uint32_t name_int1 = *(uint32_t*)&name8[0];
uint32_t name_int2 = *(uint32_t*)&name8[4];
for (int16_t i = 0; i < numlumps; i++)
{
if (name_int1 == *(uint32_t __far*)&fileinfo[i].name[0]
&& name_int2 == *(uint32_t __far*)&fileinfo[i].name[4])
{
return i;
}
}
I_Error("W_GetNumForName: %.8s not found", name);
return -1;
}
void W_ReadLumpByNum(int16_t num, void __far* ptr)
{
const filelump_t __far* lump = &fileinfo[num];
fseek(fileWAD, lump->filepos, SEEK_SET);
_ffread(ptr, lump->size, fileWAD);
}
const void __far* PUREFUNC W_GetLumpByNumAutoFree(int16_t num)
{
const filelump_t __far* lump = &fileinfo[num];
void __far* ptr = Z_MallocLevel(lump->size, NULL);
fseek(fileWAD, lump->filepos, SEEK_SET);
_ffread(ptr, lump->size, fileWAD);
return ptr;
}
static void __far* PUREFUNC W_GetLumpByNumWithUser(int16_t num, void __far*__far* user)
{
const filelump_t __far* lump = &fileinfo[num];
void __far* ptr = Z_MallocStaticWithUser(lump->size, user);
fseek(fileWAD, lump->filepos, SEEK_SET);
_ffread(ptr, lump->size, fileWAD);
return ptr;
}
int16_t W_GetFirstInt16(int16_t num)
{
const filelump_t __far* lump = &fileinfo[num];
int16_t firstInt16;
fseek(fileWAD, lump->filepos, SEEK_SET);
fread(&firstInt16, sizeof(int16_t), 1, fileWAD);
return firstInt16;
}
const void __far* PUREFUNC W_GetLumpByNum(int16_t num)
{
if (lumpcache[num])
Z_ChangeTagToStatic(lumpcache[num]);
else
lumpcache[num] = W_GetLumpByNumWithUser(num, &lumpcache[num]);
return lumpcache[num];
}
boolean PUREFUNC W_IsLumpCached(int16_t num)
{
return lumpcache[num] != NULL;
}
const void __far* PUREFUNC W_TryGetLumpByNum(int16_t num)
{
if (lumpcache[num])
{
Z_ChangeTagToStatic(lumpcache[num]);
return lumpcache[num];
}
else if (Z_IsEnoughFreeMemory(W_LumpLength(num)))
return W_GetLumpByNum(num);
else
return NULL;
}
void W_CacheLumps(void)
{
int16_t cachelumpnum = W_GetNumForName("CACHE");
int16_t numlumps = W_LumpLength(cachelumpnum) / 8;
const char __far* lumpsToCache = W_GetLumpByNum(cachelumpnum);
const void __far* __far* lumps = (const void __far* __far*)lumpsToCache;
uint32_t freeBlockSize = Z_GetLargestFreeBlockSize();
int16_t cachecount = 0;
for (int16_t i = 0; i < numlumps; i++)
{
char name[8];
_fmemcpy(name, &lumpsToCache[i * 8], 8);
#if defined DISABLE_STATUS_BAR
if ((name[0] != 'S' && name[1] != 'T') || (name[0] == 'S' && name[1] == 'T' && name[2] == 'E' && name[3] == 'P'))
#endif
{
int16_t num = W_GetNumForName(name);
uint16_t length = W_LumpLength(num);
if (length > freeBlockSize)
{
freeBlockSize = Z_GetLargestFreeBlockSize();
if (length > freeBlockSize)
{
break;
}
}
freeBlockSize -= length;
*lumps++ = W_GetLumpByNum(num);
cachecount++;
}
}
lumps = (const void __far* __far*)lumpsToCache;
for (int16_t j = 0; j < cachecount; j++)
Z_ChangeTagToCache(*lumps++);
Z_Free(lumpsToCache);
}