/
fs_main.h
472 lines (380 loc) · 15.4 KB
/
fs_main.h
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/**
* @file fs_main.h
*
* Virtual file system and file (input) stream abstraction layer.
*
* This version supports runtime (un)loading.
*
* File input. Can read from real files or WAD lumps. Note that reading from
* WAD lumps means that a copy is taken of the lump when the corresponding
* 'file' is opened. With big files this uses considerable memory and time.
*
* @ingroup fs
*
* @author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright © 2009-2012 Daniel Swanson <danij@dengine.net>
*
* @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>
*/
#ifndef LIBDENG_FILESYS_MAIN_H
#define LIBDENG_FILESYS_MAIN_H
#include "file.h"
#include "filehandle.h"
#include "pathdirectory.h"
#ifdef __cplusplus
#include <QList>
#include <de/String>
#include "fileinfo.h"
/**
* @defgroup fs File System
*/
namespace de
{
namespace internal {
template <typename Type>
inline bool cannotCastFileTo(File1* file) {
return dynamic_cast<Type*>(file) == NULL;
}
}
/**
* Internally the lump index has two parts: the Primary index (which is populated
* with lumps from loaded data files) and the Auxiliary index (used to temporarily
* open a file that is not considered part of the filesystem).
*
* Functions that don't know the absolute/logical lumpnum of file will have to check
* both indexes (e.g., FS1::lumpNumForName()).
*
* @ingroup fs
*/
class FS1
{
public:
/// No files found. @ingroup errors
DENG2_ERROR(NotFoundError);
struct PathListItem
{
String path;
int attrib;
PathListItem(QString const& _path, int _attrib = 0)
: path(_path), attrib(_attrib)
{}
bool operator < (PathListItem const& other) const
{
return path.compareWithoutCase(other.path) < 0;
}
};
typedef QList<PathListItem> PathList;
typedef QList<FileHandle*> FileList;
public:
/**
* Constructs a new file system.
*/
FS1();
virtual ~FS1();
/// Register the console commands, variables, etc..., of this module.
static void consoleRegister();
/**
* @post No more WADs will be loaded in startup mode.
*/
void endStartup();
/**
* Add a new path mapping from source to destination in the vfs.
* @note Paths will be transformed into absolute paths if needed.
*/
void mapPath(char const* source, char const* destination);
/**
* Clears all virtual path mappings.
*
* @return This instance.
*/
FS1& clearPathMappings();
/**
* Add a new lump mapping so that @a lumpName becomes visible as @a symbolicPath
* throughout the vfs.
* @note @a symbolicPath will be transformed into an absolute path if needed.
*/
void mapPathToLump(char const* symbolicPath, char const* lumpName);
/**
* Clears all path => lump mappings.
*
* @return This instance.
*/
FS1& clearPathLumpMappings();
/**
* Reset known fileId records so that the next time checkFileId() is called for
* a filepath, it will pass.
*/
void resetFileIds();
/**
* Maintains a list of identifiers already seen.
*
* @return @c true if the given file can be opened, or
* @c false, if it has already been opened.
*/
bool checkFileId(char const* path);
/**
* @return @c true if a file exists at @a path which can be opened for reading.
*/
bool accessFile(char const* path);
/**
* Files with a .wad extension are archived data files with multiple 'lumps',
* other files are single lumps whose base filename will become the lump name.
*
* @param path Path to the file to be opened. Either a "real" file in the local
* file system, or a "virtual" file in the virtual file system.
* @param baseOffset Offset from the start of the file in bytes to begin.
*
* @return Newly added file instance if the operation is successful, else @c NULL.
*/
File1* addFile(char const* path, size_t baseOffset = 0);
/// @note All files are added with baseOffset = @c 0.
int addFiles(char const* const* paths, int num);
/**
* Attempt to remove a file from the virtual file system.
*
* @return @c true if the operation is successful.
*
*/
bool removeFile(File1& file);
int removeFiles(FileList& files);
lumpnum_t lumpNumForName(char const* name, bool silent = true);
/**
* Provides access to the currently active Wad lump name index. This can
* be used for efficiently looking up files based on name.
*/
LumpIndex const& nameIndex() const;
/**
* Provides access to the Wad lump name index which is applicable to the
* specified @a absoluteLumpNum. This can be used for efficiently looking
* up files based on name.
*
* @param absoluteLumpNum Determines which lump index to return. This
* number is then translated into the range for
* the selected index.
*/
LumpIndex const& nameIndexForLump(lumpnum_t& absoluteLumpNum) const;
/**
* Opens the given file (will be translated) for reading.
*
* @post If @a allowDuplicate = @c false a new file ID for this will have been
* added to the list of known file identifiers if this file hasn't yet been
* opened. It is the responsibility of the caller to release this identifier when done.
*
* @param path Possibly relative or mapped path to the resource being opened.
* @param mode 'b' = binary
* 't' = text mode (with real files, lumps are always binary)
*
* 'f' = must be a real file in the local file system
* @param baseOffset Offset from the start of the file in bytes to begin.
* @param allowDuplicate @c false = open only if not already opened.
*
* @return Handle to the opened file.
*
* @throws NotFoundError If the requested file could not be found.
*/
FileHandle& openFile(char const* path, char const* mode, size_t baseOffset = 0,
bool allowDuplicate = true);
/**
* Try to open the specified lump for reading.
*
* @param lump The file to be opened.
*
* @return Handle to the opened file.
*
* @todo This method is no longer necessary at this level. Opening a file which
* is already present in the file system should not require calling back to a
* method of the file system itself (bad OO design).
*/
FileHandle& openLump(File1& lump);
/// Clear all references to this file.
void releaseFile(File1& file);
/// Close this file handle.
void closeFile(FileHandle& hndl);
/// Completely destroy this file; close if open, clear references and any acquired identifiers.
void deleteFile(FileHandle& hndl);
/**
* Find a single file.
*
* @throws NotFoundError If the requested file could not be found.
*/
File1& find(char const* path);
/**
* Finds all files.
*
* @param found Set of files that match the result.
*
* @return Number of files found.
*/
int findAll(FileList& found) const;
/**
* Finds all files which meet the supplied @a predicate.
*
* @param predicate If not @c NULL, this predicate evaluator callback must
* return @c true for a given file to be included in the
* @a found FileList.
* @param parameters Passed to the predicate evaluator callback.
* @param found Set of files that match the result.
*
* @return Number of files found.
*/
int findAll(bool (*predicate)(File1& file, void* parameters), void* parameters,
FileList& found) const;
/**
* Finds all files of a specific type which meet the supplied @a predicate.
* Only files that can be represented as @a Type are included in the results.
*
* @param predicate If not @c NULL, this predicate evaluator callback must
* return @c true for a given file to be included in the
* @a found FileList.
* @param parameters Passed to the predicate evaluator callback.
* @param found Set of files that match the result.
*
* @return Number of files found.
*/
template <typename Type>
int findAll(bool (*predicate)(File1& file, void* parameters), void* parameters,
FileList& found) const
{
findAll(predicate, parameters, found);
// Filter out the wrong types.
for(QMutableListIterator<FileHandle*> i(found); i.hasNext(); )
{
if(internal::cannotCastFileTo<Type>(&i.value()->file()))
i.remove();
}
return found.count();
}
/**
* Finds all paths which match the search criteria. Will search the Zip
* lump index, lump => path mappings and native files in the local system.
*
* @param searchPattern Pattern which defines the scope of the search.
* @param flags @ref searchPathFlags
* @param found Set of (absolute) paths that match the result.
*
* @return Number of paths found.
*/
int findAllPaths(char const* searchPattern, int flags, PathList& found);
/**
* Print contents of the specified directory of the virtual file system.
*/
void printDirectory(ddstring_t const* path);
/**
* Calculate a CRC for the loaded file list.
*/
uint loadedFilesCRC();
/**
* Try to open the specified WAD archive into the auxiliary lump index.
*
* @return Base index for lumps in this archive.
*/
lumpnum_t openAuxiliary(char const* filePath, size_t baseOffset = 0);
/**
* Close the auxiliary lump index if open.
*/
void closeAuxiliary();
private:
struct Instance;
Instance* d;
/**
* @param hndl Handle to the file to be interpreted. Ownership is passed to
* the interpreted file instance.
* @param path Absolute VFS path by which the interpreted file will be known.
* @param info Prepared info metadata for the file.
*
* @return The interpreted File file instance.
*/
File1& interpret(FileHandle& hndl, char const* path, FileInfo const& info);
/**
* Adds a file to any relevant indexes.
*
* @param file File to index.
*/
void index(File1& file);
/**
* Removes a file from any lump indexes.
*
* @param file File to remove from the index.
*/
void deindex(File1& file);
bool unloadFile(File1& file, bool quiet = false);
public:
/**
* Unload all files loaded after startup.
* @return Number of files unloaded.
*/
FS1& unloadAllNonStartupFiles(int* numUnloaded = 0);
};
} // namespace de
de::FS1* App_FileSystem();
extern "C" {
#endif // __cplusplus
/**
* C wrapper API:
*/
struct filelist_s;
typedef struct filelist_s FileList;
void F_Register(void);
/// Initialize this module. Cannot be re-initialized, must shutdown first.
void F_Init(void);
/// Shutdown this module.
void F_Shutdown(void);
void F_EndStartup(void);
void F_UnloadAllNonStartupFiles(int* numUnloaded);
void F_AddVirtualDirectoryMapping(char const* source, char const* destination);
void F_AddLumpDirectoryMapping(char const* lumpName, char const* symbolicPath);
void F_ResetFileIds(void);
boolean F_CheckFileId(char const* path);
int F_LumpCount(void);
int F_Access(char const* path);
struct file1_s* F_AddFile2(char const* path, size_t baseOffset);
struct file1_s* F_AddFile(char const* path/*, baseOffset = 0*/);
boolean F_RemoveFile(char const* path);
FileHandle* F_Open3(char const* path, char const* mode, size_t baseOffset, boolean allowDuplicate);
FileHandle* F_Open2(char const* path, char const* mode, size_t baseOffset/*, allowDuplicate = true */);
FileHandle* F_Open(char const* path, char const* mode/*, baseOffset = 0 */);
FileHandle* F_OpenLump(lumpnum_t absoluteLumpNum);
boolean F_IsValidLumpNum(lumpnum_t absoluteLumpNum);
lumpnum_t F_LumpNumForName(char const* name);
AutoStr* F_ComposeLumpFilePath(lumpnum_t absoluteLumpNum);
boolean F_LumpIsCustom(lumpnum_t absoluteLumpNum);
ddstring_t const* F_LumpName(lumpnum_t absoluteLumpNum);
size_t F_LumpLength(lumpnum_t absoluteLumpNum);
uint F_LumpLastModified(lumpnum_t absoluteLumpNum);
struct file1_s* F_FindFileForLumpNum2(lumpnum_t absoluteLumpNum, int* lumpIdx);
struct file1_s* F_FindFileForLumpNum(lumpnum_t absoluteLumpNum/*, lumpIdx = 0 */);
void F_Close(struct filehandle_s* file);
void F_Delete(struct filehandle_s* file);
AutoStr* F_ComposePath(struct file1_s const* file);
void F_SetCustom(struct file1_s* file, boolean yes);
AutoStr* F_ComposeLumpPath2(struct file1_s* file, int lumpIdx, char delimiter);
AutoStr* F_ComposeLumpPath(struct file1_s* file, int lumpIdx); /*delimiter='/'*/
size_t F_ReadLump(struct file1_s* file, int lumpIdx, uint8_t* buffer);
size_t F_ReadLumpSection(struct file1_s* file, int lumpIdx, uint8_t* buffer,
size_t startOffset, size_t length);
uint8_t const* F_CacheLump(struct file1_s* file, int lumpIdx);
void F_UnlockLump(struct file1_s* file, int lumpIdx);
/**
* Compiles a list of file names, separated by @a delimiter.
*/
void F_ComposePWADFileList(char* outBuf, size_t outBufSize, const char* delimiter);
uint F_CRCNumber(void);
lumpnum_t F_OpenAuxiliary2(char const* fileName, size_t baseOffset);
lumpnum_t F_OpenAuxiliary(char const* fileName/*, baseOffset = 0 */);
void F_CloseAuxiliary(void);
#ifdef __cplusplus
} // extern "C"
#endif
#endif /* LIBDENG_FILESYS_MAIN_H */