-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathw_wad.c
258 lines (198 loc) · 5.37 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
/* 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"
//#define BACKWARDS
//
// 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);
}
}
static void W_ReadDataFromFile(void __far* dest, uint32_t src, uint16_t length)
{
fseek(fileWAD, src, SEEK_SET);
_ffread(dest, length, fileWAD);
}
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)
{
printf("\tadding elksdoom.wad\n");
printf("\tshareware version.\n");
fileWAD = fopen("elksdoom.wad", "rb");
if (fileWAD == NULL)
I_Error("Can't open elksdoom.wad.");
wadinfo_t header;
W_ReadDataFromFile(&header, 0, sizeof(header));
fileinfo = Z_MallocStatic(header.numlumps * sizeof(filelump_t));
W_ReadDataFromFile(fileinfo, header.infotableofs, sizeof(filelump_t) * header.numlumps);
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)
{
int64_t nameint;
strncpy((char*)&nameint, name, 8);
#if BACKWARDS
for (int16_t i = numlumps - 1; i >= 0; i--)
#else
for (int16_t i = 0; i < numlumps; i++)
#endif
{
if (nameint == *(int64_t __far*)(fileinfo[i].name))
{
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];
W_ReadDataFromFile(ptr, lump->filepos, lump->size);
}
const void __far* PUREFUNC W_GetLumpByNumAutoFree(int16_t num)
{
const filelump_t __far* lump = &fileinfo[num];
void __far* ptr = Z_MallocLevel(lump->size, NULL);
W_ReadDataFromFile(ptr, lump->filepos, lump->size);
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);
W_ReadDataFromFile(ptr, lump->filepos, lump->size);
return ptr;
}
int16_t W_GetFirstInt16(int16_t num)
{
const filelump_t __far* lump = &fileinfo[num];
int16_t firstInt16;
W_ReadDataFromFile(&firstInt16, lump->filepos, sizeof(int16_t));
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;
}