-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWadfile.cpp
291 lines (238 loc) · 5.21 KB
/
Wadfile.cpp
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
#include "doombsp.h"
#include "Wadfile.h"
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct
{
char identification[4]; // should be IWAD
int numlumps;
int infotableofs;
} wadinfo_t;
typedef struct
{
int filepos;
int size;
char name[8];
} lumpinfo_t;
//=============================================================================
/*
============
=
= initFromFile:
=
============
*/
Wadfile * Wadfile::initFromFile(const char *path)
{
Wadfile *newWad = (Wadfile *)malloc(sizeof(*newWad));
wadinfo_t wad;
lumpinfo_t *lumps;
int i;
newWad->pathname = (char *)malloc(strlen(path)+1);
strcpy (newWad->pathname, path);
newWad->dirty = false;
newWad->handle = open (newWad->pathname, O_RDWR, 0666);
if ( newWad->handle == -1 ) {
newWad->dealloc();
return NULL;
}
//
// read in the header
//
read (newWad->handle, &wad, sizeof(wad));
if (strncmp(wad.identification,"IWAD",4))
{
close (newWad->handle);
newWad->dealloc();
return NULL;
}
wad.numlumps = LONG (wad.numlumps);
wad.infotableofs = LONG (wad.infotableofs);
//
// read in the lumpinfo
//
lseek (newWad->handle, wad.infotableofs, L_SET);
newWad->info = Storage::initCount(wad.numlumps, sizeof(lumpinfo_t), "Wadfile::info");
lumps = (lumpinfo_t *)newWad->info->elementAt(0);
read (newWad->handle, lumps, wad.numlumps*sizeof(lumpinfo_t));
for (i=0 ; i<wad.numlumps ; i++, lumps++)
{
lumps->filepos = LONG (lumps->filepos);
lumps->size = LONG (lumps->size);
}
return newWad;
}
/*
============
=
= initNew:
=
============
*/
Wadfile *Wadfile::initNew(const char *path)
{
Wadfile *newWad = (Wadfile *)malloc(sizeof(*newWad));
if (newWad == NULL)
Error("Could not allocate wad file");
wadinfo_t wad;
newWad->pathname = (char *)malloc(strlen(path)+1);
strcpy (newWad->pathname, path);
newWad->info = Storage::initCount(0, sizeof(lumpinfo_t), "Wadfile::info");
newWad->dirty = true;
newWad->handle = open (newWad->pathname, O_CREAT | O_TRUNC | O_RDWR, 0666);
if (newWad->handle== -1) {
printf("could not open wad handle\n");
return NULL;
}
// leave space for wad header
write (newWad->handle, &wad, sizeof(wad));
return newWad;
}
void Wadfile::closeHandle()
{
close (handle);
}
void Wadfile::dealloc()
{
close (handle);
info->dealloc();
free (pathname);
}
//=============================================================================
int Wadfile::numLumps()
{
return info->count();
}
int Wadfile::lumpsize(int lump)
{
lumpinfo_t *inf;
inf = (lumpinfo_t *)info->elementAt(lump);
return inf->size;
}
int Wadfile::lumpstart(int lump)
{
lumpinfo_t *inf;
inf = (lumpinfo_t *)info->elementAt(lump);
return inf->filepos;
}
const char *Wadfile::lumpname(int lump)
{
lumpinfo_t *inf;
inf = (lumpinfo_t *)info->elementAt(lump);
return inf->name;
}
/*
================
=
= lumpNamed:
=
================
*/
int Wadfile::lumpNamed(const char *name)
{
lumpinfo_t *inf;
int i, count;
char name8[9];
int v1,v2;
// make the name into two integers for easy compares
memset(name8,0,9);
if (strlen(name) < 9)
strncpy (name8,name,9);
for (i=0 ; i<9 ; i++)
name8[i] = toupper(name8[i]); // case insensitive
v1 = *(int *)name8;
v2 = *(int *)&name8[4];
// scan backwards so patch lump files take precedence
count = info->count();
for (i=count-1 ; i>=0 ; i--)
{
inf = (lumpinfo_t *)info->elementAt(i);
if ( *(int *)inf->name == v1 && *(int *)&inf->name[4] == v2)
return i;
}
return -1;
}
/*
================
=
= loadLump:
=
================
*/
void *Wadfile::loadLump(int lump)
{
lumpinfo_t *inf;
byte *buf;
inf = (lumpinfo_t *)info->elementAt(lump);
buf = (byte *)malloc (inf->size);
lseek (handle, inf->filepos, L_SET);
read (handle, buf, inf->size);
return buf;
}
void *Wadfile::loadLumpNamed(const char *name)
{
return loadLump(lumpNamed(name));
}
//============================================================================
/*
================
=
= addName:data:size:
=
================
*/
void Wadfile::addName(const char *name, void *data, int size)
{
int i;
lumpinfo_t newLump;
dirty = true;
memset (newLump.name,0,sizeof(newLump.name));
strncpy (newLump.name, name, 8);
for (i=0 ; i<8 ; i++)
newLump.name[i] = toupper(newLump.name[i]);
newLump.filepos = lseek(handle,0, L_XTND);
newLump.size = size;
info->addElement(&newLump);
write (handle, data, size);
}
/*
================
=
= writeDirectory:
=
char identification[4]; // should be IWAD
int numlumps;
int infotableofs;
================
*/
void Wadfile::writeDirectory()
{
wadinfo_t wad;
int i,count;
lumpinfo_t *inf;
//
// write the directory
//
count = info->count();
inf = (lumpinfo_t *)info->elementAt(0);
for (i=0 ; i<count ; i++)
{
inf[i].filepos = LONG (inf[i].filepos);
inf[i].size = LONG (inf[i].size);
}
wad.infotableofs = LONG (lseek(handle,0, L_XTND));
write (handle, inf, count*sizeof(lumpinfo_t));
for (i=0 ; i<count ; i++)
{
inf[i].filepos = LONG (inf[i].filepos);
inf[i].size = LONG (inf[i].size);
}
//
// write the header
//
strncpy (wad.identification, "IWAD",4);
wad.numlumps = LONG (info->count());
lseek (handle, 0, L_SET);
write (handle, &wad, sizeof(wad));
}