-
Notifications
You must be signed in to change notification settings - Fork 1
/
w_formats.cpp
242 lines (211 loc) · 6.24 KB
/
w_formats.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
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2012 James Haley
//
// 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:
// Resource archive file formats
//
//-----------------------------------------------------------------------------
#include "z_zone.h"
#include "m_misc.h"
#include "m_qstr.h"
#include "w_formats.h"
#include "w_wad.h"
enum
{
EXT_WAD,
EXT_PKE,
EXT_PK3,
EXT_ZIP,
EXT_LMP,
EXT_NUMEXTENSIONS
};
// Default extensions to try when a file is not found, in order.
static const char *W_defaultExtensions[] =
{
".wad",
".pke", // Recommended extension for EE-specific mods
".pk3",
".zip",
".lmp"
};
//
// W_TryOpenFile
//
// Tries to open a file using the given filename first. If that fails, it
// will try all of the available default extensions. Returns the open file
// if it was found (NULL if not), and sets filename equal to the filename
// that was successfully opened, with slashes normalized and the default
// extension added if one was needed.
//
FILE *W_TryOpenFile(qstring &filename, bool allowInexact)
{
FILE *f = NULL;
qstring basefn;
filename.normalizeSlashes();
basefn = filename;
// Try opening without an added extension first
if((f = fopen(basefn.constPtr(), "rb")) == NULL && allowInexact)
{
// Try default extensions
for(int i = 0; i < EXT_NUMEXTENSIONS; i++)
{
basefn = filename;
basefn.addDefaultExtension(W_defaultExtensions[i]);
if((f = fopen(basefn.constPtr(), "rb")))
{
filename = basefn;
break;
}
}
}
return f;
}
typedef bool (*FormatFunc)(FILE *, long);
//
// W_isWadFile
//
// Detect an id IWAD or PWAD file, as used in DOOM itself and its direct spawn,
// Heretic, Hexen, and Strife, as well as in numerous other games directly,
// such as RoTT, Amulets and Armor, Bloodmasters, etc. It also formed the
// basis of the GRP (Duke3D) and PAK (Quake, Quake 2) formats.
//
static bool W_isWadFile(FILE *f, long baseoffset)
{
bool result = false;
char header[12];
if(!fseek(f, baseoffset, SEEK_SET) && fread(header, 1, 12, f) == 12)
{
if(!memcmp(header, "IWAD", 4) || !memcmp(header, "PWAD", 4))
result = true;
}
fseek(f, baseoffset, SEEK_SET);
return result;
}
//
// W_isZipFile
//
// Detect a ZIP archive, originally defined by the PKZip archive tool
// and now used pretty much everywhere. Forms the basis of id Tech 3 and up
// "pack" formats PK3 and PK4, as well as being the underlying format of
// many other domain-specific archives such as Java JARs.
//
static bool W_isZipFile(FILE *f, long baseoffset)
{
bool result = false;
char header[30];
if(!fseek(f, baseoffset, SEEK_SET) && fread(header, 1, 30, f) == 30)
{
if(!memcmp(header, "PK\x3\x4", 4))
result = true;
}
fseek(f, baseoffset, SEEK_SET);
return result;
}
//
// W_isFile
//
// This one is a dummy which always returns true; if no other format could be
// positively detected first, we treat the file as an ordinary flat physical
// file. It's the lowest priority predicate as a result.
//
static bool W_isFile(FILE *f, long baseoffset)
{
return true;
}
static FormatFunc formatFuncs[W_FORMAT_MAX] =
{
W_isWadFile, // W_FORMAT_WAD
W_isZipFile, // W_FORMAT_ZIP
W_isFile, // W_FORMAT_FILE
};
//
// W_DetermineFileFormat
//
// Determine what format a file added with -iwad, -file, etc. is in according
// to the file's internal metadata (we do not trust file extensions for this
// purpose).
//
WResourceFmt W_DetermineFileFormat(FILE *f, long baseoffset)
{
int fmt = W_FORMAT_WAD;
while(!formatFuncs[fmt](f, baseoffset))
++fmt;
return static_cast<WResourceFmt>(fmt);
}
//
// W_LumpNameFromFilePath
//
// Creates a lump name given a filepath.
//
void W_LumpNameFromFilePath(const char *input, char output[9], int li_namespace)
{
// Strip off the path, and remove any extension
M_ExtractFileBase(input, output);
// Change '^' to '\' for benefit of sprite frames
if(li_namespace == lumpinfo_t::ns_sprites)
{
for(int i = 0; i < 8; i++)
{
if(output[i] == '^')
output[i] = '\\';
}
}
}
struct namespace_matcher_t
{
const char *prefix;
int li_namespace;
};
static namespace_matcher_t matchers[] =
{
{ "flats/", lumpinfo_t::ns_flats },
{ "graphics/", lumpinfo_t::ns_global }, // Treated as global in EE
{ "music/", lumpinfo_t::ns_global }, // Treated as global in EE
{ "sounds/", lumpinfo_t::ns_global }, // Treated as global in EE
{ "sprites/", lumpinfo_t::ns_sprites },
{ "textures/", lumpinfo_t::ns_textures },
{ NULL, -1 } // keep this last
};
//
// W_NamespaceForFilePath
//
// Given a relative filepath (such as from a zip), determine a lump namespace.
// Returns -1 if the path should not be exposed with a short lump name.
//
int W_NamespaceForFilePath(const char *path)
{
int li_namespace = -1;
namespace_matcher_t *matcher = matchers;
while(matcher->prefix)
{
if(!strncmp(path, matcher->prefix, strlen(matcher->prefix)))
{
li_namespace = matcher->li_namespace;
break;
}
++matcher;
}
// if the file is in the top-level directory, it is global
if(li_namespace == -1 && !strchr(path, '/'))
li_namespace = lumpinfo_t::ns_global;
return li_namespace;
}
// EOF