forked from Triang3l/BS2PC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbs2pc_wad.c
289 lines (254 loc) · 8.12 KB
/
bs2pc_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
#pragma warning(disable : 4996)
#include "bs2pc.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QSORT_WAD // [SDP] fix #2: WAD qsort/dumb search switch (IDK what is better, left both)
typedef struct bs2pc_wadDirectory_s {
char *path;
size_t pathLength;
struct bs2pc_wadDirectory_s *next;
} bs2pc_wadDirectory_t;
static bs2pc_wadDirectory_t *bs2pc_wadDirectories = NULL;
static size_t bs2pc_longestWadDirectoryPath = 0;
static unsigned int bs2pc_maxWadLumpSize = 0;
void BS2PC_AddWadDirectory(const char *path) {
size_t length = strlen(path);
bs2pc_wadDirectory_t *directory;
if (length == 0) {
return;
}
directory = BS2PC_Alloc(sizeof(bs2pc_wadDirectory_t), false);
directory->path = BS2PC_Alloc(length + 1, false);
strcpy(directory->path, path);
directory->pathLength = length;
directory->next = bs2pc_wadDirectories;
bs2pc_wadDirectories = directory;
if (length > bs2pc_longestWadDirectoryPath) {
bs2pc_longestWadDirectoryPath = length;
}
}
#pragma pack(push, 4)
typedef struct {
char identification[4];
unsigned int numlumps;
unsigned int infotableofs;
} bs2pc_wadHeader_t;
typedef struct {
unsigned int filepos;
unsigned int disksize;
unsigned int size;
char type;
char compression;
char pad[2];
char name[16];
} bs2pc_wadLumpInfo_t;
#pragma pack(pop)
typedef struct bs2pc_wad_s {
FILE *file;
unsigned int lumpCount;
bs2pc_wadLumpInfo_t *lumps;
struct bs2pc_wad_s *next;
} bs2pc_wad_t;
static bs2pc_wad_t *bs2pc_wads = NULL;
#ifdef QSORT_WAD
int lmpcmp(const void * p1, const void * p2) {
bs2pc_wadLumpInfo_t *pl1 = (bs2pc_wadLumpInfo_t *) p1;
bs2pc_wadLumpInfo_t *pl2 = (bs2pc_wadLumpInfo_t *) p2;
return BS2PC_CompareTextureNames((const char *) pl1->name, (const char *) pl2->name);
}
#endif
static void BS2PC_LoadWad(const char *fileName /* Assuming / slash */) {
FILE *file;
char *fileNameWithGame;
bs2pc_wadHeader_t header;
bs2pc_wad_t *wad;
unsigned int index;
const bs2pc_wadLumpInfo_t *lump;
file = fopen(fileName, "rb");
if (file != NULL) {
fprintf(stderr, "%s loaded.\n", fileName);
} else if (bs2pc_wadDirectories != NULL) {
const bs2pc_wadDirectory_t *wadDirectory;
const char *fileNameRelative = strrchr(fileName, '/');
fileNameRelative = (fileNameRelative != NULL ? fileNameRelative + 1 : fileName);
fileNameWithGame = bs2pc_alloca(bs2pc_longestWadDirectoryPath + 1 + strlen(fileNameRelative) + 1);
for (wadDirectory = bs2pc_wadDirectories; wadDirectory != NULL; wadDirectory = wadDirectory->next) {
strcpy(fileNameWithGame, wadDirectory->path);
fileNameWithGame[wadDirectory->pathLength] = '/';
strcpy(fileNameWithGame + wadDirectory->pathLength + 1, fileNameRelative);
file = fopen(fileNameWithGame, "rb");
if (file != NULL) {
fprintf(stderr, "%s loaded.\n", fileNameWithGame);
break;
}
}
}
if (file == NULL) {
fprintf(stderr, "Couldn't find %s.\n", fileName);
return;
}
if (fread(&header, sizeof(bs2pc_wadHeader_t), 1, file) == 0) {
fprintf(stderr, "Couldn't read the header of %s.\n", fileName);
fclose(file);
return;
}
if (header.identification[0] != 'W' || header.identification[1] != 'A' ||
header.identification[2] != 'D' || header.identification[3] != '3' ||
header.numlumps == 0) {
fprintf(stderr, "%s is invalid or empty.\n", fileName);
fclose(file);
return;
}
if (fseek(file, header.infotableofs, SEEK_SET) != 0) {
fprintf(stderr, "Couldn't seek to the information table of %s.\n", fileName);
fclose(file);
return;
}
wad = BS2PC_Alloc(sizeof(bs2pc_wad_t), false);
wad->file = file;
wad->lumpCount = header.numlumps;
wad->lumps = (bs2pc_wadLumpInfo_t *) BS2PC_Alloc(header.numlumps * sizeof(bs2pc_wadLumpInfo_t), false);
if (fread(wad->lumps, header.numlumps * sizeof(bs2pc_wadLumpInfo_t), 1, file) == 0) {
fprintf(stderr, "Couldn't read the information table of %s.\n", fileName);
BS2PC_Free(wad->lumps);
BS2PC_Free(wad);
fclose(file);
return;
}
#ifdef QSORT_WAD
if (wad->lumpCount > 1) {
fputs("Qsorting WAD lumps...\n", stderr);
qsort(wad->lumps, wad->lumpCount, sizeof(*wad->lumps), lmpcmp);
}
#endif
wad->next = bs2pc_wads;
bs2pc_wads = wad;
lump = wad->lumps;
for (index = 0; index < header.numlumps; ++index, ++lump) {
if (lump->size > bs2pc_maxWadLumpSize) {
bs2pc_maxWadLumpSize = lump->size;
}
}
}
static void BS2PC_LoadWadsFromList(const char *list, unsigned int listLength) {
char *fileName;
size_t fileNameLength = 0;
size_t index;
fileName = (char *) bs2pc_alloca(listLength + 1);
for (index = 0; index <= listLength /* For +1 */; ++index) {
int character = list[index];
if (character == ';' || character == '"') {
if (fileNameLength != 0) {
fileName[fileNameLength] = '\0';
BS2PC_LoadWad(fileName);
}
fileNameLength = 0;
} else {
fileName[fileNameLength++] = (character == '\\' ? '/' : character);
}
}
}
void BS2PC_LoadWadsFromEntities(const char *entities, unsigned int entitiesSize) {
unsigned int index;
bool isKey = false; // Will be flipped next quote.
bool isWadList = false;
const char *stringStart = NULL;
unsigned int stringLength;
for (index = 0; index < entitiesSize; ++index) {
char character = entities[index];
if (stringStart != NULL) {
if (character == '"') {
if (isKey) {
isWadList = (stringLength == 3 && bs2pc_strncasecmp(stringStart, "wad", 3) == 0) ||
(stringLength == 4 && bs2pc_strncasecmp(stringStart, "_wad", 4) == 0);
} else if (isWadList) {
BS2PC_LoadWadsFromList(stringStart, stringLength);
}
stringStart = NULL;
} else {
++stringLength;
}
} else {
if (character == '}') {
// Only parse worldspawn.
break;
}
if (character == '"') {
isKey = !isKey;
stringStart = &entities[index + 1];
stringLength = 0;
}
}
}
}
static inline int BS2PC_WadSearchComparison(const void *keyLump, const void *lump) {
return bs2pc_strncasecmp(((const bs2pc_wadLumpInfo_t *) keyLump)->name,
((const bs2pc_wadLumpInfo_t *) lump)->name,
sizeof(((const bs2pc_wadLumpInfo_t *) keyLump)->name) - 1);
}
static unsigned char *bs2pc_wadLumpBuffer = NULL;
static unsigned int bs2pc_wadLumpBufferSize = 0;
unsigned char *BS2PC_LoadTextureFromWad(const char *name) {
const bs2pc_wad_t *wad;
const bs2pc_wadLumpInfo_t *lumpInfo = NULL;
if (bs2pc_maxWadLumpSize == 0) {
return NULL;
}
for (wad = bs2pc_wads; wad != NULL; wad = wad->next) {
const bs2pc_wadLumpInfo_t *lumps = wad->lumps;
int difference;
#ifdef QSORT_WAD
int low = 0, mid, high = wad->lumpCount;
while (low <= high) {
mid = low + ((high - low) >> 1);
difference = BS2PC_CompareTextureNames(lumps[mid].name, name);
if (difference == 0) {
lumpInfo = &lumps[mid];
break;
}
if (difference > 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
#else // QSORT_WAD
unsigned int lmp;
// [SDP] fix #2: use dumb search for WAD textures because many WADs are unsorted
for (lmp = 0; lmp < wad->lumpCount; lmp++) {
difference = BS2PC_CompareTextureNames(lumps[lmp].name, name);
if (difference == 0) {
lumpInfo = &lumps[lmp];
break;
}
}
#endif // QSORT_WAD
if (lumpInfo == NULL) {
continue;
}
if (lumpInfo->type != (64 + 3) || lumpInfo->compression != 0) {
fprintf(stderr, "Lump %s is not a texture or compressed in one of the WADs, skipping.\n", name);
lumpInfo = NULL;
continue;
}
break;
}
if (lumpInfo == NULL) {
return NULL;
}
if (bs2pc_wadLumpBufferSize < bs2pc_maxWadLumpSize) {
bs2pc_wadLumpBufferSize = bs2pc_maxWadLumpSize;
BS2PC_AllocReplace((void **) &bs2pc_wadLumpBuffer, bs2pc_wadLumpBufferSize, false);
}
if (fseek(wad->file, lumpInfo->filepos, SEEK_SET) != 0) {
fprintf(stderr, "Couldn't seek to texture %s in a WAD file.\n", name);
return NULL;
}
if (fread(bs2pc_wadLumpBuffer, lumpInfo->disksize, 1, wad->file) == 0) {
fprintf(stderr, "Couldn't read texture %s from a WAD file.\n", name);
return NULL;
}
return bs2pc_wadLumpBuffer;
}