/
manifest.cpp
376 lines (317 loc) · 9.91 KB
/
manifest.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/** @file manifest.cpp Game resource manifest.
*
* @authors Copyright © 2010-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2010-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de_base.h"
#include "filesys/manifest.h"
#include "con_main.h"
#include "api_filesys.h"
#include "filesys/fs_main.h"
#include "filesys/lumpindex.h"
#include "resource/wad.h"
#include "resource/zip.h"
#include <de/Path>
using namespace de;
DENG2_PIMPL(ResourceManifest)
{
resourceclassid_t classId;
int flags; ///< @ref fileFlags.
QStringList names; ///< Known names in precedence order.
/// Vector of resource identifier keys (e.g., file or lump names).
/// Used for identification purposes.
QStringList identityKeys;
/// Index (in Manifest::Instance::names) of the name used to locate
/// this resource if found. Set during resource location.
int foundNameIndex;
/// Fully resolved absolute path to the located resource if found.
/// Set during resource location.
String foundPath;
Instance(Public *i, resourceclassid_t rclass, int rflags)
: Base(i)
, classId(rclass)
, flags(rflags & ~FF_FOUND)
, names()
, identityKeys()
, foundNameIndex(-1)
, foundPath()
{}
};
ResourceManifest::ResourceManifest(resourceclassid_t fClass, int fFlags, String *name)
: d(new Instance(this, fClass, fFlags))
{
if(name) addName(*name);
}
ResourceManifest &ResourceManifest::addName(String newName, bool *didAdd)
{
// Is this name unique? We don't want duplicates.
if(newName.isEmpty() || d->names.contains(newName, Qt::CaseInsensitive))
{
if(didAdd) *didAdd = false;
return *this;
}
// Add the new name.
d->names.prepend(newName);
if(didAdd) *didAdd = true;
return *this;
}
ResourceManifest &ResourceManifest::addIdentityKey(String newIdentityKey, bool *didAdd)
{
// Is this key unique? We don't want duplicates.
if(newIdentityKey.isEmpty() || d->identityKeys.contains(newIdentityKey, Qt::CaseInsensitive))
{
if(didAdd) *didAdd = false;
return *this;
}
// Add the new key.
d->identityKeys.append(newIdentityKey);
if(didAdd) *didAdd = true;
return *this;
}
enum lumpsizecondition_t
{
LSCOND_NONE,
LSCOND_EQUAL,
LSCOND_GREATER_OR_EQUAL,
LSCOND_LESS_OR_EQUAL
};
/**
* Modifies the idKey so that the size condition is removed.
*/
static void checkSizeConditionInIdentityKey(String &idKey, lumpsizecondition_t *pCond, size_t *pSize)
{
DENG_ASSERT(pCond != 0);
DENG_ASSERT(pSize != 0);
*pCond = LSCOND_NONE;
*pSize = 0;
int condPos = -1;
int argPos = -1;
if((condPos = idKey.indexOf("==")) >= 0)
{
*pCond = LSCOND_EQUAL;
argPos = condPos + 2;
}
else if((condPos = idKey.indexOf(">=")) >= 0)
{
*pCond = LSCOND_GREATER_OR_EQUAL;
argPos = condPos + 2;
}
else if((condPos = idKey.indexOf("<=")) >= 0)
{
*pCond = LSCOND_LESS_OR_EQUAL;
argPos = condPos + 2;
}
if(condPos < 0) return;
// Get the argument.
*pSize = idKey.mid(argPos).toULong();
// Remove it from the name.
idKey.truncate(condPos);
}
static lumpnum_t lumpNumForIdentityKey(LumpIndex const &lumpIndex, String idKey)
{
if(idKey.isEmpty()) return -1;
// The key may contain a size condition (==, >=, <=).
lumpsizecondition_t sizeCond;
size_t refSize;
checkSizeConditionInIdentityKey(idKey, &sizeCond, &refSize);
// We should now be left with just the name.
String name = idKey;
// Append a .lmp extension if none is specified.
if(idKey.fileNameExtension().isEmpty())
{
name += ".lmp";
}
lumpnum_t lumpNum = lumpIndex.lastIndexForPath(Path(name));
if(lumpNum < 0) return -1;
// Check the condition.
size_t lumpSize = lumpIndex.lump(lumpNum).info().size;
switch(sizeCond)
{
case LSCOND_EQUAL:
if(lumpSize != refSize) return -1;
break;
case LSCOND_GREATER_OR_EQUAL:
if(lumpSize < refSize) return -1;
break;
case LSCOND_LESS_OR_EQUAL:
if(lumpSize > refSize) return -1;
break;
default: break;
}
return lumpNum;
}
/// @return @c true, iff the resource appears to be what we think it is.
static bool validateWad(String const &filePath, QStringList const &identityKeys)
{
bool validated = true;
try
{
de::FileHandle &hndl = App_FileSystem().openFile(filePath, "rb", 0/*baseOffset*/, true /*allow duplicates*/);
if(Wad *wad = dynamic_cast<Wad *>(&hndl.file()))
{
// Ensure all identity lumps are present.
if(identityKeys.count())
{
if(wad->empty())
{
// Clear not what we are looking for.
validated = false;
}
else
{
// Publish lumps to a temporary index.
LumpIndex lumpIndex;
for(int i = 0; i < wad->lumpCount(); ++i)
{
lumpIndex.catalogLump(wad->lump(i));
}
// Check each lump.
DENG2_FOR_EACH_CONST(QStringList, i, identityKeys)
{
if(lumpNumForIdentityKey(lumpIndex, *i) < 0)
{
validated = false;
break;
}
}
}
}
}
else
{
validated = false;
}
// We're done with the file.
App_FileSystem().releaseFile(hndl.file());
delete &hndl;
}
catch(FS1::NotFoundError const &)
{} // Ignore this error.
return validated;
}
/// @return @c true, iff the resource appears to be what we think it is.
static bool validateZip(String const &filePath, QStringList const & /*identityKeys*/)
{
try
{
de::FileHandle &hndl = App_FileSystem().openFile(filePath, "rbf");
bool result = Zip::recognise(hndl);
/// @todo Check files. We should implement an auxiliary zip lump index...
App_FileSystem().releaseFile(hndl.file());
delete &hndl;
return result;
}
catch(FS1::NotFoundError const &)
{} // Ignore error.
return false;
}
ResourceManifest &ResourceManifest::locateFile()
{
// Already found?
if(d->flags & FF_FOUND) return *this;
// Perform the search.
int nameIndex = 0;
for(QStringList::const_iterator i = d->names.constBegin(); i != d->names.constEnd(); ++i, ++nameIndex)
{
// Attempt to resolve a path to the named resource.
try
{
String foundPath = App_FileSystem().findPath(de::Uri(*i, d->classId),
RLF_DEFAULT, App_ResourceClass(d->classId));
foundPath = App_BasePath() / foundPath; // Ensure the path is absolute.
// Perform identity validation.
bool validated = false;
if(d->classId == RC_PACKAGE)
{
/// @todo The identity configuration should declare the type of resource...
validated = validateWad(foundPath, d->identityKeys);
if(!validated)
validated = validateZip(foundPath, d->identityKeys);
}
else
{
// Other resource types are not validated.
validated = true;
}
if(validated)
{
// This is the resource we've been looking for.
d->flags |= FF_FOUND;
d->foundPath = foundPath;
d->foundNameIndex = nameIndex;
break;
}
}
catch(FS1::NotFoundError const&)
{} // Ignore this error.
}
return *this;
}
ResourceManifest &ResourceManifest::forgetFile()
{
if(d->flags & FF_FOUND)
{
d->foundPath.clear();
d->foundNameIndex = -1;
d->flags &= ~FF_FOUND;
}
return *this;
}
String const &ResourceManifest::resolvedPath(bool tryLocate)
{
if(tryLocate)
{
locateFile();
}
return d->foundPath;
}
resourceclassid_t ResourceManifest::resourceClass() const
{
return d->classId;
}
int ResourceManifest::fileFlags() const
{
return d->flags;
}
QStringList const &ResourceManifest::identityKeys() const
{
return d->identityKeys;
}
QStringList const &ResourceManifest::names() const
{
return d->names;
}
void ResourceManifest::consolePrint(ResourceManifest &manifest, bool showStatus)
{
bool const resourceFound = (manifest.fileFlags() & FF_FOUND) != 0;
String text;
if(showStatus)
{
text += (resourceFound? " " : ":");
}
// Format the resource name list.
text += manifest.names().join(" or ");
if(showStatus)
{
text += String(" - ") + (resourceFound? "found" : "missing");
if(resourceFound)
{
text += String(" ") + NativePath(manifest.resolvedPath(false/*don't try to locate*/)).expand().pretty();
}
}
LOG_MSG("") << text;
}