/
fs_main.c
2431 lines (2068 loc) · 68 KB
/
fs_main.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file fs_main.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*\author Copyright © 1993-1996 by id Software, Inc.
*
* 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
*/
#include <stdlib.h>
#include <ctype.h>
#include "de_base.h"
#include "de_console.h"
#include "de_filesys.h"
#include "de_misc.h" // For M_LimitedStrCat
#include "m_md5.h"
#include "lumpinfo.h"
#include "lumpdirectory.h"
#include "lumpfile.h"
#include "wadfile.h"
#include "zipfile.h"
#include "filelist.h"
D_CMD(Dir);
D_CMD(DumpLump);
D_CMD(ListFiles);
/**
* Lump Directory Mapping. Maps lump to resource path.
*/
typedef struct {
lumpname_t lumpName;
ddstring_t path; // Absolute path.
} ldmapping_t;
/**
* Virtual Directory Mapping. Maps a resource path to another resource.
*/
typedef struct {
ddstring_t source; // Absolute path.
ddstring_t destination; // Absolute path.
} vdmapping_t;
/**
* FileIdentifier
*/
#define FILEIDENTIFIERID_T_MAXLEN 16
#define FILEIDENTIFIERID_T_LASTINDEX 15
typedef byte fileidentifierid_t[FILEIDENTIFIERID_T_MAXLEN];
typedef struct fileidentifier_s {
fileidentifierid_t hash;
} fileidentifier_t;
static boolean inited = false;
static boolean loadingForStartup;
static FileList* openFiles;
static FileList* loadedFiles;
static uint fileIdentifiersCount = 0;
static uint fileIdentifiersMax = 0;
static fileidentifier_t* fileIdentifiers = NULL;
static LumpDirectory* zipLumpDirectory;
static LumpDirectory* primaryWadLumpDirectory;
static LumpDirectory* auxiliaryWadLumpDirectory;
// @c true = one or more files have been opened using the auxiliary directory.
static boolean auxiliaryWadLumpDirectoryInUse;
// Currently selected lump directory.
static LumpDirectory* ActiveWadLumpDirectory;
// Lump directory mappings.
static uint ldMappingsCount = 0;
static uint ldMappingsMax = 0;
static ldmapping_t* ldMappings = NULL;
// Virtual(-File) directory mappings.
static uint vdMappingsCount = 0;
static uint vdMappingsMax = 0;
static vdmapping_t* vdMappings = NULL;
static void clearLDMappings(void);
static void clearVDMappings(void);
static boolean applyVDMapping(ddstring_t* path, vdmapping_t* vdm);
static FILE* findRealFile(const char* path, const char* mymode, ddstring_t** foundPath);
void F_Register(void)
{
C_CMD("dir", "", Dir);
C_CMD("ls", "", Dir); // Alias
C_CMD("dir", "s*", Dir);
C_CMD("ls", "s*", Dir); // Alias
C_CMD("dump", "s", DumpLump);
C_CMD("listfiles", "", ListFiles);
}
static void errorIfNotInited(const char* callerName)
{
if(inited) return;
Con_Error("%s: VFS module is not presently initialized.", callerName);
// Unreachable. Prevents static analysers from getting rather confused, poor things.
exit(1);
}
static fileidentifier_t* findFileIdentifierForId(fileidentifierid_t id)
{
fileidentifier_t* found = NULL;
if(fileIdentifiersCount)
{
uint i = 0;
do
{
if(!memcmp(fileIdentifiers[i].hash, id, FILEIDENTIFIERID_T_LASTINDEX))
found = &fileIdentifiers[i];
} while(!found && ++i < fileIdentifiersCount);
}
return found;
}
static int findFileNodeIndexForPath(FileList* list, const char* path_)
{
int found = -1;
if(path_ && path_[0] && !FileList_Empty(list))
{
abstractfile_t* file;
ddstring_t path;
int i;
// Transform the path into one we can process.
Str_Init(&path); Str_Set(&path, path_);
F_FixSlashes(&path, &path);
// Perform the search.
i = 0;
do
{
file = FileList_GetFile(list, i);
if(!Str_CompareIgnoreCase(AbstractFile_Path(file), Str_Text(&path)))
{
found = i;
}
} while(found == -1 && ++i < FileList_Size(list));
Str_Free(&path);
}
return found;
}
static abstractfile_t* newUnknownFile(DFile* file, const char* path, const LumpInfo* info)
{
abstractfile_t* af = (abstractfile_t*)malloc(sizeof *af);
if(!af) Con_Error("newUnknownFile: Failed on allocation of %lu bytes for new abstractfile_t.",
(unsigned long) sizeof *af);
return AbstractFile_Init(af, FT_UNKNOWNFILE, path, file, info);
}
static abstractfile_t* newZipFile(DFile* file, const char* path, const LumpInfo* info)
{
return (abstractfile_t*)ZipFile_New(file, path, info);
}
static abstractfile_t* newWadFile(DFile* file, const char* path, const LumpInfo* info)
{
return (abstractfile_t*)WadFile_New(file, path, info);
}
static abstractfile_t* newLumpFile(DFile* file, const char* path, const LumpInfo* info)
{
return (abstractfile_t*)LumpFile_New(file, path, info);
}
static int pruneLumpsFromDirectorysByFile(abstractfile_t* af)
{
int pruned = LumpDirectory_PruneByFile(zipLumpDirectory, af);
pruned += LumpDirectory_PruneByFile(primaryWadLumpDirectory, af);
if(auxiliaryWadLumpDirectoryInUse)
pruned += LumpDirectory_PruneByFile(auxiliaryWadLumpDirectory, af);
return pruned;
}
static boolean removeLoadedFile(int loadedFilesNodeIndex)
{
DFile* file = FileList_Get(loadedFiles, loadedFilesNodeIndex);
abstractfile_t* af = DFile_File(file);
switch(AbstractFile_Type(af))
{
case FT_ZIPFILE: ZipFile_ClearLumpCache((ZipFile*)af); break;
case FT_WADFILE: WadFile_ClearLumpCache((WadFile*)af); break;
default: break;
}
F_ReleaseFileId(Str_Text(AbstractFile_Path(af)));
pruneLumpsFromDirectorysByFile(af);
F_Delete(FileList_RemoveAt(loadedFiles, loadedFilesNodeIndex));
return true;
}
static void clearLoadedFiles(LumpDirectory* directory)
{
abstractfile_t* file;
int i;
for(i = FileList_Size(loadedFiles) - 1; i >= 0; i--)
{
file = FileList_GetFile(loadedFiles, i);
if(!directory || LumpDirectory_Catalogues(directory, file))
{
removeLoadedFile(i);
}
}
}
/**
* Handles conversion to a logical index that is independent of the lump directory currently in use.
*/
static __inline lumpnum_t logicalLumpNum(lumpnum_t lumpNum)
{
return (lumpNum < 0? -1 :
ActiveWadLumpDirectory == auxiliaryWadLumpDirectory? lumpNum += AUXILIARY_BASE : lumpNum);
}
static void usePrimaryWadLumpDirectory(void)
{
ActiveWadLumpDirectory = primaryWadLumpDirectory;
}
static boolean useAuxiliaryWadLumpDirectory(void)
{
if(!auxiliaryWadLumpDirectoryInUse)
return false;
ActiveWadLumpDirectory = auxiliaryWadLumpDirectory;
return true;
}
static void clearLumpDirectorys(void)
{
LumpDirectory_Delete(primaryWadLumpDirectory), primaryWadLumpDirectory = NULL;
LumpDirectory_Delete(auxiliaryWadLumpDirectory), auxiliaryWadLumpDirectory = NULL;
ActiveWadLumpDirectory = NULL;
LumpDirectory_Delete(zipLumpDirectory), zipLumpDirectory = NULL;
}
static void clearOpenFiles(void)
{
int i;
for(i = FileList_Size(openFiles) - 1; i >= 0; i--)
{
F_Delete(FileList_Get(openFiles, i));
}
FileList_Clear(openFiles);
}
/**
* Selects which lump directory to use, given a logical lump index.
* This should be called in all functions that access directories by lump index.
*/
static lumpnum_t chooseWadLumpDirectory(lumpnum_t lumpNum)
{
errorIfNotInited("chooseWadLumpDirectory");
if(lumpNum >= AUXILIARY_BASE)
{
useAuxiliaryWadLumpDirectory();
lumpNum -= AUXILIARY_BASE;
}
else
{
usePrimaryWadLumpDirectory();
}
return lumpNum;
}
static boolean unloadFile2(const char* path, boolean permitRequired, boolean quiet)
{
int idx;
errorIfNotInited("unloadFile2");
idx = findFileNodeIndexForPath(loadedFiles, path);
if(idx >= 0)
{
// Do not attempt to unload a resource required by the current game.
if(!permitRequired && Game_IsRequiredResource(theGame, path))
{
if(!quiet)
{
Con_Message("\"%s\" is required by the current game.\n"
"Required game files cannot be unloaded in isolation.\n", F_PrettyPath(path));
}
return false;
}
if(!quiet && verbose >= 1)
{
Con_Message("Unloading \"%s\"...\n", F_PrettyPath(path));
}
return removeLoadedFile(idx);
}
return false; // No such file loaded.
}
static boolean unloadFile(const char* path, boolean permitRequired)
{
return unloadFile2(path, permitRequired, false/*do log issues*/);
}
static void clearFileIds(void)
{
if(fileIdentifiers)
{
free(fileIdentifiers), fileIdentifiers = NULL;
}
if(fileIdentifiersCount)
{
fileIdentifiersCount = fileIdentifiersMax = 0;
}
}
void F_PrintFileId(byte identifier[16])
{
uint i;
if(!identifier) return;
for(i = 0; i < 16; ++i)
{
Con_Printf("%02x", identifier[i]);
}
}
void F_GenerateFileId(const char* str, byte identifier[16])
{
md5_ctx_t context;
ddstring_t absPath;
// First normalize the name.
Str_Init(&absPath); Str_Set(&absPath, str);
F_MakeAbsolute(&absPath, &absPath);
F_FixSlashes(&absPath, &absPath);
#if defined(WIN32) || defined(MACOSX)
// This is a case insensitive operation.
strupr(Str_Text(&absPath));
#endif
md5_init(&context);
md5_update(&context, (byte*) Str_Text(&absPath), (unsigned int) Str_Length(&absPath));
md5_final(&context, identifier);
Str_Free(&absPath);
}
boolean F_CheckFileId(const char* path)
{
assert(path);
{
fileidentifierid_t id;
if(!F_Access(path))
return false;
// Calculate the identifier.
F_GenerateFileId(path, id);
if(findFileIdentifierForId(id))
return false;
// Allocate a new entry.
fileIdentifiersCount++;
if(fileIdentifiersCount > fileIdentifiersMax)
{
if(!fileIdentifiersMax)
fileIdentifiersMax = 16;
else
fileIdentifiersMax *= 2;
fileIdentifiers = (fileidentifier_t*)realloc(fileIdentifiers, fileIdentifiersMax * sizeof *fileIdentifiers);
if(!fileIdentifiers)
Con_Error("F_CheckFileId: Failed on (re)allocation of %lu bytes while "
"enlarging fileIdentifiers.", (unsigned long) (fileIdentifiersMax * sizeof *fileIdentifiers));
memset(fileIdentifiers + fileIdentifiersCount, 0, (fileIdentifiersMax - fileIdentifiersCount) * sizeof *fileIdentifiers);
}
#if _DEBUG
VERBOSE(
Con_Printf("Added file identifier ");
F_PrintFileId(id);
Con_Printf(" - \"%s\"\n", F_PrettyPath(path)) )
#endif
memcpy(fileIdentifiers[fileIdentifiersCount - 1].hash, id, sizeof(id));
return true;
}
}
boolean F_ReleaseFileId(const char* path)
{
if(path && path[0])
{
fileidentifierid_t id;
fileidentifier_t* fileIdentifier;
F_GenerateFileId(path, id);
fileIdentifier = findFileIdentifierForId(id);
if(fileIdentifier)
{
size_t index = fileIdentifier - fileIdentifiers;
if(index < fileIdentifiersCount-1)
memmove(fileIdentifiers + index, fileIdentifiers + index + 1, fileIdentifiersCount - index - 1);
memset(fileIdentifiers + fileIdentifiersCount, 0, sizeof *fileIdentifiers);
--fileIdentifiersCount;
#if _DEBUG
VERBOSE(
Con_Printf("Released file identifier ");
F_PrintFileId(id);
Con_Printf(" - \"%s\"\n", F_PrettyPath(path)) )
#endif
return true;
}
}
return false;
}
void F_ResetFileIds(void)
{
fileIdentifiersCount = 0;
}
void F_InitLumpInfo(LumpInfo* info)
{
assert(info);
info->lumpIdx = 0;
info->size = 0;
info->compressedSize = 0;
info->lastModified = 0;
info->container = NULL;
}
void F_CopyLumpInfo(LumpInfo* dst, const LumpInfo* src)
{
assert(dst && src);
dst->lumpIdx = src->lumpIdx;
dst->size = src->size;
dst->compressedSize = src->compressedSize;
dst->lastModified = src->lastModified;
dst->container = src->container;
}
void F_DestroyLumpInfo(LumpInfo* info)
{
// Nothing to do.
}
void F_Init(void)
{
if(inited) return; // Already been here.
// This'll force the loader NOT to flag new files as "runtime".
loadingForStartup = true;
DFileBuilder_Init();
openFiles = FileList_New();
loadedFiles = FileList_New();
zipLumpDirectory = LumpDirectory_NewWithFlags(LDF_UNIQUE_PATHS);
primaryWadLumpDirectory = LumpDirectory_New();
auxiliaryWadLumpDirectory = LumpDirectory_New();
auxiliaryWadLumpDirectoryInUse = false;
ActiveWadLumpDirectory = primaryWadLumpDirectory;
inited = true;
}
void F_Shutdown(void)
{
if(!inited) return;
F_CloseAuxiliary();
clearVDMappings();
clearLDMappings();
clearLoadedFiles(0);
FileList_Delete(loadedFiles), loadedFiles = NULL;
clearOpenFiles();
FileList_Delete(openFiles), openFiles = NULL;
clearFileIds(); // Should be null-op if bookkeeping is correct.
clearLumpDirectorys();
DFileBuilder_Shutdown();
inited = false;
}
void F_EndStartup(void)
{
errorIfNotInited("F_EndStartup");
loadingForStartup = false;
usePrimaryWadLumpDirectory();
}
static int unloadListFiles(FileList* list, boolean nonStartup)
{
abstractfile_t* file;
int i, unloaded = 0;
assert(list);
for(i = FileList_Size(list) - 1; i >= 0; i--)
{
file = FileList_GetFile(list, i);
if(!nonStartup || !AbstractFile_HasStartup(file))
{
if(unloadFile2(Str_Text(AbstractFile_Path(file)),
true/*allow unloading game resources*/, true/*quiet please*/))
{
++unloaded;
}
}
}
return unloaded;
}
#if _DEBUG
static void logOrphanedFileIdentifiers(void)
{
fileidentifierid_t nullId;
uint i, orphanCount = 0;
memset(nullId, 0, sizeof nullId);
for(i = 0; i < fileIdentifiersCount; ++i)
{
fileidentifier_t* id = fileIdentifiers + i;
if(!memcmp(id->hash, &nullId, FILEIDENTIFIERID_T_LASTINDEX)) continue;
if(!orphanCount)
{
Con_Printf("Warning: Orphan file identifiers:\n");
}
Con_Printf(" %u - ", orphanCount);
F_PrintFileId(id->hash);
Con_Printf("\n");
orphanCount++;
}
}
#endif
int F_Reset(void)
{
int unloaded = 0;
if(!inited) return 0;
#if _DEBUG
// List all open files with their identifiers.
VERBOSE(
Con_Printf("Open files at reset:\n");
FileList_Print(openFiles);
Con_Printf("End\n")
)
#endif
// Perform non-startup file unloading...
unloaded = unloadListFiles(loadedFiles, true/*non-startup*/);
#if _DEBUG
// Sanity check: look for orphaned identifiers.
logOrphanedFileIdentifiers();
#endif
// Reset file IDs so previously seen files can be processed again.
/// @todo this releases the ID of startup files too but given the
/// only startup file is doomsday.pk3 which we never attempt to load
/// again post engine startup, this isn't an immediate problem.
F_ResetFileIds();
// Update the dir/WAD translations.
F_InitLumpDirectoryMappings();
F_InitVirtualDirectoryMappings();
return unloaded;
}
boolean F_IsValidLumpNum(lumpnum_t absoluteLumpNum)
{
lumpnum_t lumpNum;
errorIfNotInited("F_IsValidLumpNum");
lumpNum = chooseWadLumpDirectory(absoluteLumpNum);
return LumpDirectory_IsValidIndex(ActiveWadLumpDirectory, lumpNum);
}
typedef enum lumpsizecondition_e {
LSCOND_NONE,
LSCOND_EQUAL,
LSCOND_GREATER_OR_EQUAL,
LSCOND_LESS_OR_EQUAL
} lumpsizecondition_t;
/**
* Modifies the name so that the size condition is removed.
*/
static void checkSizeConditionInName(ddstring_t* name, lumpsizecondition_t* pCond, size_t* pSize)
{
int i;
int len = Str_Length(name);
const char* txt = Str_Text(name);
int argPos = 0;
assert(pCond != 0);
assert(pSize != 0);
*pCond = LSCOND_NONE;
*pSize = 0;
for(i = 0; i < len - 2; ++i, ++txt)
{
if(!strncmp(txt, "==", 2))
{
*pCond = LSCOND_EQUAL;
argPos = i + 2;
break;
}
if(!strncmp(txt, ">=", 2))
{
*pCond = LSCOND_GREATER_OR_EQUAL;
argPos = i + 2;
break;
}
if(!strncmp(txt, "<=", 2))
{
*pCond = LSCOND_LESS_OR_EQUAL;
argPos = i + 2;
break;
}
}
if(!argPos) return;
// Get the argument.
*pSize = strtoul(txt + 2, NULL, 10);
// Remove it from the name.
Str_Truncate(name, i);
}
lumpnum_t F_CheckLumpNumForName2(const char* name, boolean silent)
{
lumpnum_t lumpNum = -1;
lumpsizecondition_t sizeCond;
size_t lumpSize = 0;
size_t refSize;
errorIfNotInited("F_CheckLumpNumForName");
if(name && name[0])
{
ddstring_t searchPath;
Str_Init(&searchPath); Str_Set(&searchPath, name);
// The name may contain a size condition (==, >=, <=).
checkSizeConditionInName(&searchPath, &sizeCond, &refSize);
// Append a .lmp extension if none is specified.
if(!F_FindFileExtension(name))
{
Str_Append(&searchPath, ".lmp");
}
// We have to check both the primary and auxiliary caches because
// we've only got a name and don't know where it is located. Start with
// the auxiliary lumps because they take precedence.
if(useAuxiliaryWadLumpDirectory())
{
lumpNum = LumpDirectory_IndexForPath(ActiveWadLumpDirectory, Str_Text(&searchPath));
if(lumpNum >= 0 && sizeCond != LSCOND_NONE)
{
// Get the size as well for the condition check.
lumpSize = LumpDirectory_LumpInfo(ActiveWadLumpDirectory, lumpNum)->size;
}
}
// Found it yet?
if(lumpNum < 0)
{
usePrimaryWadLumpDirectory();
lumpNum = LumpDirectory_IndexForPath(ActiveWadLumpDirectory, Str_Text(&searchPath));
if(lumpNum >= 0 && sizeCond != LSCOND_NONE)
{
// Get the size as well for the condition check.
lumpSize = LumpDirectory_LumpInfo(ActiveWadLumpDirectory, lumpNum)->size;
}
}
// Check the condition.
switch(sizeCond)
{
case LSCOND_EQUAL:
if(lumpSize != refSize) lumpNum = -1;
break;
case LSCOND_GREATER_OR_EQUAL:
if(lumpSize < refSize) lumpNum = -1;
break;
case LSCOND_LESS_OR_EQUAL:
if(lumpSize > refSize) lumpNum = -1;
break;
default:
break;
}
// If still not found, warn the user.
if(!silent && lumpNum < 0)
{
if(sizeCond == LSCOND_NONE)
{
Con_Message("Warning: F_CheckLumpNumForName: Lump \"%s\" not found.\n", name);
}
else
{
Con_Message("Warning: F_CheckLumpNumForName: Lump \"%s\" with size%s%i not found.\n",
Str_Text(&searchPath), sizeCond==LSCOND_EQUAL? "==" :
sizeCond==LSCOND_GREATER_OR_EQUAL? ">=" : "<=", (int)refSize);
}
}
Str_Free(&searchPath);
}
else if(!silent)
{
Con_Message("Warning: F_CheckLumpNumForName: Empty name, returning invalid lumpnum.\n");
}
return logicalLumpNum(lumpNum);
}
lumpnum_t F_CheckLumpNumForName(const char* name)
{
return F_CheckLumpNumForName2(name, false);
}
const LumpInfo* F_FindInfoForLumpNum2(lumpnum_t absoluteLumpNum, int* lumpIdx)
{
lumpnum_t translated = chooseWadLumpDirectory(absoluteLumpNum);
const LumpInfo* lumpInfo = LumpDirectory_LumpInfo(ActiveWadLumpDirectory, translated);
if(lumpIdx) *lumpIdx = (lumpInfo? lumpInfo->lumpIdx : -1);
return lumpInfo;
}
const LumpInfo* F_FindInfoForLumpNum(lumpnum_t absoluteLumpNum)
{
return F_FindInfoForLumpNum2(absoluteLumpNum, NULL);
}
const char* F_LumpName(lumpnum_t absoluteLumpNum)
{
const LumpInfo* info = F_FindInfoForLumpNum(absoluteLumpNum);
if(info)
{
PathDirectoryNode* node = F_LumpDirectoryNode(info->container, info->lumpIdx);
return Str_Text(PathDirectory_GetFragment(PathDirectoryNode_Directory(node), node));
}
return "";
}
size_t F_LumpLength(lumpnum_t absoluteLumpNum)
{
const LumpInfo* info = F_FindInfoForLumpNum(absoluteLumpNum);
if(info) return info->size;
return 0;
}
uint F_LumpLastModified(lumpnum_t absoluteLumpNum)
{
const LumpInfo* info = F_FindInfoForLumpNum(absoluteLumpNum);
if(info) return info->lastModified;
return 0;
}
abstractfile_t* F_FindFileForLumpNum2(lumpnum_t absoluteLumpNum, int* lumpIdx)
{
const LumpInfo* lumpInfo = F_FindInfoForLumpNum2(absoluteLumpNum, lumpIdx);
if(!lumpInfo) return NULL;
return lumpInfo->container;
}
abstractfile_t* F_FindFileForLumpNum(lumpnum_t absoluteLumpNum)
{
return F_FindFileForLumpNum2(absoluteLumpNum, NULL);
}
const char* F_LumpSourceFile(lumpnum_t absoluteLumpNum)
{
abstractfile_t* fsObject = F_FindFileForLumpNum(absoluteLumpNum);
if(fsObject) return Str_Text(AbstractFile_Path(fsObject));
return "";
}
boolean F_LumpIsCustom(lumpnum_t absoluteLumpNum)
{
abstractfile_t* fsObject = F_FindFileForLumpNum(absoluteLumpNum);
if(fsObject) return AbstractFile_HasCustom(fsObject);
return false;
}
int F_LumpCount(void)
{
if(inited)
return LumpDirectory_Size(ActiveWadLumpDirectory);
return 0;
}
lumpnum_t F_OpenAuxiliary3(const char* path, size_t baseOffset, boolean silent)
{
ddstring_t* foundPath = NULL;
ddstring_t searchPath;
DFile* dfile;
FILE* file;
errorIfNotInited("F_OpenAuxiliary");
if(!path || !path[0]) return -1;
// Make it a full path.
Str_Init(&searchPath); Str_Set(&searchPath, path);
F_ExpandBasePath(&searchPath, &searchPath);
// We must have an absolute path, so prepend the current working directory if necessary.
F_PrependWorkPath(&searchPath, &searchPath);
/// @todo Allow opening WAD/ZIP files from lumps in other containers.
file = findRealFile(Str_Text(&searchPath), "rb", &foundPath);
Str_Free(&searchPath);
if(!file)
{
if(foundPath)
{
Str_Delete(foundPath);
}
if(!silent)
{
Con_Message("Warning:F_OpenAuxiliary: Resource \"%s\" not found, aborting.\n", path);
}
return -1;
}
dfile = DFileBuilder_NewFromFile(file, baseOffset);
if(WadFile_Recognise(dfile))
{
LumpInfo info;
if(auxiliaryWadLumpDirectoryInUse)
{
F_CloseAuxiliary();
}
ActiveWadLumpDirectory = auxiliaryWadLumpDirectory;
auxiliaryWadLumpDirectoryInUse = true;
// Prepare the temporary info descriptor.
F_InitLumpInfo(&info);
info.lastModified = F_LastModified(Str_Text(foundPath));
dfile = DFileBuilder_NewFromAbstractFile(newWadFile(dfile, Str_Text(foundPath), &info));
FileList_AddBack(openFiles, dfile);
FileList_AddBack(loadedFiles, DFileBuilder_NewCopy(dfile));
WadFile_PublishLumpsToDirectory((WadFile*)DFile_File(dfile), ActiveWadLumpDirectory);
Str_Delete(foundPath);
// We're done with the descriptor.
F_DestroyLumpInfo(&info);
return AUXILIARY_BASE;
}
if(!silent)
{
if(path && path[0])
Con_Message("Warning:F_OpenAuxiliary: Resource \"%s\" cannot be found or does not appear to be of recognised format.\n", path);
else
Con_Message("Warning:F_OpenAuxiliary: Cannot open a resource with neither a path nor a handle to it.\n");
}
if(foundPath) Str_Delete(foundPath);
DFile_Delete(dfile, true);
return -1;
}
lumpnum_t F_OpenAuxiliary2(const char* path, size_t baseOffset)
{
return F_OpenAuxiliary3(path, baseOffset, false);
}
lumpnum_t F_OpenAuxiliary(const char* path)
{
return F_OpenAuxiliary2(path, 0);
}
void F_CloseAuxiliary(void)
{
errorIfNotInited("F_CloseAuxiliary");
if(useAuxiliaryWadLumpDirectory())
{
clearLoadedFiles(auxiliaryWadLumpDirectory);
auxiliaryWadLumpDirectoryInUse = false;
}
usePrimaryWadLumpDirectory();
}
void F_ReleaseFile(abstractfile_t* fsObject)
{
DFile* file;
int i;
if(!fsObject) return;
for(i = FileList_Size(openFiles) - 1; i >= 0; i--)
{
file = FileList_Get(openFiles, i);
if(DFile_File(file) == fsObject)
{
FileList_RemoveAt(openFiles, i);
}
}
}
void F_Close(DFile* file)
{
DFile_Close(file);
}
void F_Delete(DFile* file)
{
abstractfile_t* af = DFile_File(file);
DFile_Close(file);
switch(AbstractFile_Type(af))
{
case FT_UNKNOWNFILE:
F_ReleaseFile(af);
AbstractFile_Destroy(af);
free(af);
break;
case FT_ZIPFILE: ZipFile_Delete( ( ZipFile*)af); break;
case FT_WADFILE: WadFile_Delete( ( WadFile*)af); break;
case FT_LUMPFILE: LumpFile_Delete((LumpFile*)af); break;
default:
Con_Error("F_Delete: Invalid file type %i.", AbstractFile_Type(af));
exit(1); // Unreachable.
}
DFile_Delete(file, true);
}
const LumpInfo* F_LumpInfo(abstractfile_t* fsObject, int lumpIdx)
{
assert(fsObject);
switch(AbstractFile_Type(fsObject))
{
case FT_ZIPFILE: return ZipFile_LumpInfo( (ZipFile*)fsObject, lumpIdx);
case FT_WADFILE: return WadFile_LumpInfo( (WadFile*)fsObject, lumpIdx);
case FT_LUMPFILE: return LumpFile_LumpInfo((LumpFile*)fsObject, lumpIdx);
default:
Con_Error("F_LumpInfo: Invalid file type %i.", AbstractFile_Type(fsObject));
exit(1); // Unreachable.
}
}
PathDirectoryNode* F_LumpDirectoryNode(abstractfile_t* fsObject, int lumpIdx)
{
assert(fsObject);
switch(AbstractFile_Type(fsObject))
{
case FT_ZIPFILE: return ZipFile_LumpDirectoryNode((ZipFile*)fsObject, lumpIdx);
case FT_WADFILE: return WadFile_LumpDirectoryNode((WadFile*)fsObject, lumpIdx);
case FT_LUMPFILE: return F_LumpDirectoryNode(AbstractFile_Container(fsObject),