/
w_wad.cpp
1821 lines (1528 loc) · 48.3 KB
/
w_wad.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
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
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2000 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:
// Handles WAD file header, directory, lump I/O.
//
//-----------------------------------------------------------------------------
#ifdef _MSC_VER
// for Visual C++:
#include "i_opndir.h"
#else
// for SANE compilers:
#include <dirent.h>
#endif
#include <memory>
#include "z_zone.h"
#include "i_system.h"
#include "d_io.h" // SoM 3/12/2002: moved unistd stuff into d_io.h
#include "d_dehtbl.h"
#include "m_argv.h"
#include "m_collection.h"
#include "m_dllist.h"
#include "m_misc.h"
#include "m_qstr.h"
#include "m_swap.h"
#include "w_formats.h"
#include "w_wad.h"
#include "w_zip.h"
#include "z_auto.h"
//
// GLOBALS
//
// Location of each lump on disk.
WadDirectory wGlobalDir;
int WadDirectory::source; // haleyjd 03/18/10: next source ID# to use
int WadDirectory::IWADSource = -1; // sf: the handle of the main iwad
int WadDirectory::ResWADSource = -1; // haleyjd: track handle of first wad added
//
// haleyjd 07/12/07: structure for transparently manipulating lumps of
// different types
//
struct lumptype_t
{
size_t (*readLump)(lumpinfo_t *, void *);
};
static size_t W_DirectReadLump(lumpinfo_t *, void *);
static size_t W_MemoryReadLump(lumpinfo_t *, void *);
static size_t W_FileReadLump (lumpinfo_t *, void *);
static size_t W_ZipReadLump (lumpinfo_t *, void *);
static size_t W_JagReadLump (lumpinfo_t *, void *);
static lumptype_t LumpHandlers[lumpinfo_t::lump_numtypes] =
{
// direct lump
{
W_DirectReadLump,
},
// memory lump
{
W_MemoryReadLump,
},
// directory file lump
{
W_FileReadLump,
},
// zip file lump
{
W_ZipReadLump,
},
// jag compressed direct lump
{
W_JagReadLump
},
};
//=============================================================================
//
// WadDirectoryPimpl
//
//
// haleyjd 09/01/12: private implementation object for hidden portions of the
// WadDirectory class.
//
class WadDirectoryPimpl : public ZoneObject
{
public:
// Static collection of source filenames
static qstring FnPrototype;
static Collection<qstring> SourceFileNames;
//
// Add a source filename
//
static void AddFileName(const char *fn)
{
SourceFileNames.setPrototype(&FnPrototype);
SourceFileNames.addNew() << fn;
}
//
// Get a filename for a source number
//
static const char *FileNameForSource(size_t source)
{
if(source >= SourceFileNames.getLength())
return NULL;
return SourceFileNames[source].constPtr();
}
PODCollection<lumpinfo_t *> infoptrs; // lumpinfo_t allocations
DLListItem<ZipFile> *zipFiles; // zip files attached to this waddir
WadDirectoryPimpl()
: ZoneObject(), infoptrs(), zipFiles(NULL)
{
}
};
qstring WadDirectoryPimpl::FnPrototype;
Collection<qstring> WadDirectoryPimpl::SourceFileNames;
//=============================================================================
//
// WadDirectory
//
// LUMP BASED ROUTINES.
//
//
// WadDirectory Constructor
//
WadDirectory::WadDirectory()
: ZoneObject(), lumpinfo(NULL), numlumps(0), ispublic(false), type(0)
{
pImpl = new WadDirectoryPimpl;
memset(m_namespaces, 0, sizeof(m_namespaces));
}
//
// Destructor
//
WadDirectory::~WadDirectory()
{
if(pImpl)
{
delete pImpl;
pImpl = NULL;
}
}
//
// W_addInfoPtr
//
// haleyjd 06/06/10: I need to track all lumpinfo_t allocations that are
// added to a waddir_t's lumpinfo directory, or else these allocations get
// orphaned when freeing a private wad directory. Oops! ;)
//
void WadDirectory::addInfoPtr(lumpinfo_t *infoptr)
{
pImpl->infoptrs.add(infoptr);
}
//
// WadDirectory::incrementSource
//
// Add the source filename and increment the static source counter.
//
void WadDirectory::incrementSource(openwad_t &openData)
{
// haleyjd: push source filename
WadDirectoryPimpl::AddFileName(openData.filename);
// haleyjd: increment source
++source;
}
//
// WadDirectory::handleOpenError
//
// Protected method to handle errors during archive file open.
//
void WadDirectory::handleOpenError(openwad_t &openData, wfileadd_t &addInfo,
const char *filename)
{
if(addInfo.flags & WFA_OPENFAILFATAL)
I_Error("Error: couldn't open %s\n", filename);
else
printf("Warning: couldn't open %s\n", filename);
openData.error = true;
}
//
// WadDirectory::openFile
//
// haleyjd 04/06/11: For normal wad files, the file needs to be found and
// opened.
//
WadDirectory::openwad_t WadDirectory::openFile(wfileadd_t &addInfo)
{
openwad_t openData;
qstring filename;
bool allowInexact = (addInfo.flags & WFA_ALLOWINEXACTFN) == WFA_ALLOWINEXACTFN;
memset(&openData, 0, sizeof(openData));
// Try opening the file
filename = addInfo.filename;
if(!(openData.handle = W_TryOpenFile(filename, allowInexact)))
{
handleOpenError(openData, addInfo, filename.constPtr());
return openData;
}
// Figure out the file format
openData.format = W_DetermineFileFormat(openData.handle, 0);
// Check against format requirements
if(addInfo.flags & WFA_REQUIREFORMAT &&
openData.format != addInfo.requiredFmt)
{
handleOpenError(openData, addInfo, filename.constPtr());
return openData;
}
// Successfully opened!
openData.filename = filename.duplicateAuto();
openData.error = false;
return openData;
}
//
// WadDirectory::reAllocLumpInfo
//
// Reallocate the lumpinfo array to contain the indicated number of pointers,
// and, if makelumps is true, will also allocate that number of new lumpinfo_t
// structures and return them.
//
lumpinfo_t *WadDirectory::reAllocLumpInfo(int numnew, int startlump)
{
lumpinfo_t *newlumps = NULL;
numlumps += numnew;
// Fill in lumpinfo
lumpinfo = erealloc(lumpinfo_t **, lumpinfo, numlumps * sizeof(lumpinfo_t *));
// space for new lumps
newlumps = estructalloc(lumpinfo_t, numlumps - startlump);
// haleyjd: keep track of this allocation of lumps
addInfoPtr(newlumps);
// haleyjd: fill in new pointers here, instead of everywhere this is used.
for(int i = startlump; i < numlumps; i++)
lumpinfo[i] = newlumps + (i - startlump);
return newlumps;
}
//
// WadDirectory::addSingleFile
//
// haleyjd 10/27/12: Load a single file into the wad directory.
//
bool WadDirectory::addSingleFile(openwad_t &openData, wfileadd_t &addInfo,
int startlump)
{
filelump_t singleinfo;
lumpinfo_t *lump_p;
singleinfo.filepos = 0;
singleinfo.size = static_cast<int>(M_FileLength(openData.handle));
M_ExtractFileBase(openData.filename, singleinfo.name);
lump_p = reAllocLumpInfo(1, startlump);
lump_p->type = lumpinfo_t::lump_direct; // haleyjd: lump type
lump_p->size = static_cast<size_t>(singleinfo.size);
lump_p->source = source; // haleyjd: source id
// setup for direct file IO
lump_p->direct.file = openData.handle;
lump_p->direct.position = static_cast<size_t>(singleinfo.filepos);
lump_p->li_namespace = addInfo.li_namespace; // killough 4/17/98
strncpy(lump_p->name, singleinfo.name, 8);
incrementSource(openData);
return true;
}
//
// WadDirectory::addMemoryWad
//
// haleyjd 10/31/12: Add an id wadlink file stored in memory into the directory.
//
bool WadDirectory::addMemoryWad(openwad_t &openData, wfileadd_t &addInfo,
int startlump)
{
// haleyjd 04/07/11
wadinfo_t header;
ZAutoBuffer fileinfo2free; // killough
filelump_t *fileinfo;
size_t length;
size_t info_offset;
lumpinfo_t *lump_p;
// Read in the header
memcpy(&header, openData.base, sizeof(header));
header.numlumps = SwapLong(header.numlumps);
header.infotableofs = SwapLong(header.infotableofs);
// allocate enough fileinfo_t's to hold the wad directory
length = header.numlumps * sizeof(filelump_t);
fileinfo2free.alloc(length, true); // killough
fileinfo = fileinfo2free.getAs<filelump_t *>();
info_offset = static_cast<size_t>(header.infotableofs);
// seek to the directory
if(info_offset + header.numlumps * sizeof(filelump_t) > openData.size)
{
if(addInfo.flags & WFA_OPENFAILFATAL)
I_Error("Failed reading directory for in-memory file\n");
else
{
printf("Failed reading directory for in-memory file\n");
return false;
}
}
// read it in.
byte *directoryBase = static_cast<byte *>(openData.base) + info_offset;
memcpy(fileinfo, directoryBase, header.numlumps * sizeof(filelump_t));
// Add lumpinfo_t's for all lumps in the wad file
lump_p = reAllocLumpInfo(header.numlumps, startlump);
// Merge into the directory
for(int i = startlump; i < numlumps; i++, lump_p++, fileinfo++)
{
lump_p->type = lumpinfo_t::lump_memory; // haleyjd
lump_p->size = (size_t)(SwapLong(fileinfo->size));
lump_p->source = source; // haleyjd
// setup for memory IO
lump_p->memory.data = openData.base;
lump_p->memory.position = (size_t)(SwapLong(fileinfo->filepos));
lump_p->li_namespace = addInfo.li_namespace; // killough 4/17/98
strncpy(lump_p->name, fileinfo->name, 8);
}
incrementSource(openData);
return true;
}
//
// WadDirectory::addWadFile
//
// haleyjd 10/28/12: Add an id wadlink file into the directory.
//
bool WadDirectory::addWadFile(openwad_t &openData, wfileadd_t &addInfo,
int startlump)
{
// haleyjd 04/07/11
//HashData wadHash = HashData(HashData::SHA1);
bool showHash = false;
bool doHacks = (addInfo.flags & WFA_ALLOWHACKS) == WFA_ALLOWHACKS;
long baseoffset = static_cast<long>(addInfo.baseoffset);
wadinfo_t header;
ZAutoBuffer fileinfo2free; // killough
filelump_t *fileinfo;
size_t length;
long info_offset;
lumpinfo_t *lump_p;
// check for in-memory wads
if(addInfo.flags & WFA_INMEMORY)
return addMemoryWad(openData, addInfo, startlump);
// haleyjd: seek to baseoffset first when loading a subfile
if(addInfo.flags & WFA_SUBFILE)
fseek(openData.handle, baseoffset, SEEK_SET);
// -nowadhacks disables all wad directory hacks, in case of unforeseen
// compatibility problems that would last until the next release
if(M_CheckParm("-nowadhacks"))
doHacks = false;
// -showhashes enables display of the computed SHA-1 hash used to apply
// wad directory hacks. Quite useful for when a new hack needs to be added.
if(M_CheckParm("-showhashes"))
showHash = true;
if(fread(&header, sizeof(header), 1, openData.handle) < 1)
{
if(addInfo.flags & WFA_OPENFAILFATAL)
I_Error("Failed reading header for wad file %s\n", openData.filename);
else
{
fclose(openData.handle);
printf("Failed reading header for wad file %s\n", openData.filename);
return false;
}
}
// Feed the wad header data into the hash computation
//if(doHacks || showHash)
// wadHash.addData((const uint8_t *)&header, (uint32_t)sizeof(header));
header.numlumps = SwapLong(header.numlumps);
header.infotableofs = SwapLong(header.infotableofs);
// allocate enough fileinfo_t's to hold the wad directory
length = header.numlumps * sizeof(filelump_t);
fileinfo2free.alloc(length, true); // killough
fileinfo = fileinfo2free.getAs<filelump_t *>();
info_offset = static_cast<long>(header.infotableofs);
// subfile wads may exist at a positive base offset in the container file
if(addInfo.flags & WFA_SUBFILE)
info_offset += baseoffset;
// seek to the directory
fseek(openData.handle, info_offset, SEEK_SET);
// read it in.
if(fread(fileinfo, length, 1, openData.handle) < 1)
{
if(addInfo.flags & WFA_OPENFAILFATAL)
I_Error("Failed reading directory for wad file %s\n", openData.filename);
else
{
fclose(openData.handle);
printf("Failed reading directory for wad file %s\n", openData.filename);
return false;
}
}
// Feed the wad directory into the hash computation, wrap it up, and if requested,
// output it to the console.
#if 0
if(doHacks || showHash)
{
wadHash.addData((const uint8_t *)fileinfo, (uint32_t)length);
wadHash.wrapUp();
if(in_textmode && showHash)
printf("\thash = %s\n", wadHash.digestToString());
// haleyjd 04/08/11: apply wad directory hacks as needed
if(doHacks)
W_CheckDirectoryHacks(wadHash, fileinfo, header.numlumps);
}
#endif
// update IWAD handle?
// haleyjd: Must be a public wad file.
if(!(addInfo.flags & WFA_PRIVATE) && this->ispublic)
{
// haleyjd 06/21/04: track handle of first wad added also
if(ResWADSource == -1)
ResWADSource = source;
// haleyjd 07/13/09: only track the first IWAD found
// haleyjd 11/03/12: Status as the IWAD is now determined by how the file
// was added to the game (ie., -iwad or -disk, vs. -file, autoloads, etc.)
if(IWADSource < 0 && (addInfo.flags & WFA_ISIWADFILE))
IWADSource = source;
}
// Add lumpinfo_t's for all lumps in the wad file
lump_p = reAllocLumpInfo(header.numlumps, startlump);
// Merge into the directory
for(int i = startlump; i < this->numlumps; i++, lump_p++, fileinfo++)
{
strncpy(lump_p->name, fileinfo->name, 8);
if(lump_p->name[0] & 0x80) // For psxwadgen, detect compressed lumps
{
lump_p->name[0] &= 0x7f;
lump_p->type = lumpinfo_t::lump_direct_jag;
}
else
lump_p->type = lumpinfo_t::lump_direct; // haleyjd
lump_p->size = (size_t)(SwapLong(fileinfo->size));
lump_p->source = source; // haleyjd
// setup for direct IO
lump_p->direct.file = openData.handle;
lump_p->direct.position = (size_t)(SwapLong(fileinfo->filepos));
// for subfiles, add baseoffset to the lump offset
if(addInfo.flags & WFA_SUBFILE)
lump_p->direct.position += static_cast<size_t>(baseoffset);
lump_p->li_namespace = addInfo.li_namespace; // killough 4/17/98
}
#if 0
if(ispublic)
D_NewWadLumps(source);
#endif
incrementSource(openData);
return true;
}
//
// WadDirectory::addZipFile
//
// Add a zip file into the directory.
//
bool WadDirectory::addZipFile(openwad_t &openData, wfileadd_t &addInfo,
int startlump)
{
std::auto_ptr<ZipFile> zip(new ZipFile());
int numZipLumps;
lumpinfo_t *lump_p;
// Read in the ZIP file's header and directory information
if(!zip->readFromFile(openData.handle))
{
handleOpenError(openData, addInfo, openData.filename);
return false;
}
if(!(numZipLumps = zip->getNumLumps()))
{
// load was successful, but this zip file is useless.
return true;
}
// Allocate lumpinfo_t structures for the zip file's internal file lumps
lump_p = reAllocLumpInfo(numZipLumps, startlump);
// Initialize the wad directory copies of the zip lumps
for(int i = startlump; i < numlumps; i++, lump_p++)
{
ZipLump &zipLump = zip->getLump(i - startlump);
lump_p->type = lumpinfo_t::lump_zip;
lump_p->size = zipLump.size;
lump_p->source = source;
// setup for zip file IO
lump_p->zip.zipLump = &zipLump;
// Initialize short name, if found appropriate to do so. Lumps that are
// not given a short name or namespace here will not be hashed by
// WadDirectory::initLumpHash.
int li_namespace;
if((li_namespace = W_NamespaceForFilePath(zipLump.name)) != -1)
{
lump_p->li_namespace = li_namespace;
W_LumpNameFromFilePath(zipLump.name, lump_p->name, li_namespace);
}
// Copy lfn
lump_p->lfn = estrdup(zipLump.name);
}
// Hook the ZipFile instance into the WadDirectory's list of zips
zip->linkTo(&pImpl->zipFiles);
incrementSource(openData);
// Check for embedded wad files
zip->checkForWadFiles(*this);
zip.release(); // don't destroy the ZipFile
return true;
}
//
// WadDirectory::addFile
//
// All files are optional, but at least one file must be found (PWAD, if all
// required lumps are present).
// Files with a .wad extension are wadlink files with multiple lumps.
// Other files are single lumps with the base filename for the lump name.
//
// Reload hack removed by Lee Killough
// killough 1/31/98: static, const
//
bool WadDirectory::addFile(wfileadd_t &addInfo)
{
// Directory file addition callback type
typedef bool (WadDirectory::* AddFileCB)(openwad_t &, wfileadd_t &, int);
static AddFileCB fileadders[W_FORMAT_MAX] =
{
&WadDirectory::addWadFile, // W_FORMAT_WAD
&WadDirectory::addZipFile, // W_FORMAT_ZIP
&WadDirectory::addSingleFile // W_FORMAT_FILE
};
openwad_t openData;
// When loading a subfile, the physical file is already open.
if(addInfo.flags & WFA_SUBFILE)
{
openData.filename = addInfo.filename;
openData.handle = addInfo.f;
// Only WAD files are currently supported as subfiles.
if(W_DetermineFileFormat(openData.handle,
static_cast<long>(addInfo.baseoffset)) != W_FORMAT_WAD)
{
addInfo.flags |= WFA_OPENFAILFATAL;
handleOpenError(openData, addInfo, openData.filename);
return false; // it'll actually I_Error, but whatever.
}
openData.format = W_FORMAT_WAD;
}
else if(addInfo.flags & WFA_INMEMORY)
{
openData.base = addInfo.memory;
openData.size = addInfo.size;
openData.filename = "memory";
openData.format = W_FORMAT_WAD; // wad handler will deal with this.
}
else
{
// Open the physical archive file and determine its format
openData = openFile(addInfo);
if(openData.error)
return false;
}
// Show adding message if at startup and not a private directory or
// in-memory wad
/*
if(!(addInfo.flags & (WFA_PRIVATE|WFA_INMEMORY)) && this->ispublic)
{
printf(" adding %s\n", openData.filename); // killough 8/8/98
}
*/
// Call the appropriate file directory addition routine for this format
#ifdef RANGECHECK
if(openData.format < 0 || openData.format >= W_FORMAT_MAX)
I_Error("WadDirectory::addFile: invalid file format %d\n", openData.format);
#endif
if(!(this->*fileadders[openData.format])(openData, addInfo, numlumps))
{
handleOpenError(openData, addInfo, openData.filename);
return false;
}
return true; // no error
}
struct dirfile_t
{
char *fullfn;
bool isdir;
size_t size;
};
//
// WadDirectory::addDirectory
//
// Add an on-disk file directory to the WadDirectory.
//
int WadDirectory::addDirectory(const char *dirpath)
{
DIR *dir;
dirent *ent;
int localcount = 0;
int totalcount = 0;
int startlump;
int usinglump = 0;
int globallump = 0;
size_t i, fileslen;
PODCollection<dirfile_t> files;
lumpinfo_t *newlumps;
if(!(dir = opendir(dirpath)))
return 0;
// count the files in the directory
while((ent = readdir(dir)))
{
dirfile_t newfile;
struct stat sbuf;
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
continue;
newfile.fullfn = M_SafeFilePath(dirpath, ent->d_name);
if(!stat(newfile.fullfn, &sbuf)) // check for existence
{
if(S_ISDIR(sbuf.st_mode)) // if it's a directory, mark it
newfile.isdir = true;
else
{
newfile.isdir = false;
newfile.size = (size_t)(sbuf.st_size);
++localcount;
}
files.add(newfile);
}
}
closedir(dir);
fileslen = files.getLength();
for(i = 0; i < fileslen; i++)
{
// recurse into subdirectories first.
if(files[i].isdir)
totalcount += addDirectory(files[i].fullfn);
}
// No lumps to add for this directory?
if(!localcount)
return totalcount;
// Fill in lumpinfo
startlump = numlumps;
numlumps += localcount;
lumpinfo = erealloc(lumpinfo_t **, lumpinfo, numlumps * sizeof(lumpinfo_t *));
// create lumpinfo_t structures for the files
newlumps = estructalloc(lumpinfo_t, localcount);
// keep track of this allocation of lumps
addInfoPtr(newlumps);
for(i = 0, globallump = startlump; i < fileslen; i++)
{
if(!files[i].isdir)
{
lumpinfo_t *lump = &newlumps[usinglump++];
M_ExtractFileBase(files[i].fullfn, lump->name);
M_Strupr(lump->name);
lump->li_namespace = lumpinfo_t::ns_global; // TODO
lump->type = lumpinfo_t::lump_file;
lump->lfn = estrdup(files[i].fullfn);
lump->source = source;
lump->size = files[i].size;
lumpinfo[globallump++] = lump;
}
}
// push source filename
WadDirectoryPimpl::AddFileName(dirpath);
// increment source
++source;
if(ispublic)
printf(" adding directory %s\n", dirpath);
return totalcount + localcount;
}
//
// WadDirectory::addInMemoryWad
//
// Externally trigger addition of a wad file that is loaded completely
// into memory.
//
bool WadDirectory::addInMemoryWad(void *buffer, size_t size)
{
wfileadd_t addInfo;
memset(&addInfo, 0, sizeof(addInfo));
addInfo.memory = buffer;
addInfo.size = size;
addInfo.flags = WFA_OPENFAILFATAL | WFA_INMEMORY;
if(!ispublic)
addInfo.flags |= WFA_PRIVATE;
return addFile(addInfo);
}
// jff 1/23/98 Create routines to reorder the master directory
// putting all flats into one marked block, and all sprites into another.
// This will allow loading of sprites and flats from a PWAD with no
// other changes to code, particularly fast hashes of the lumps.
//
// killough 1/24/98 modified routines to be a little faster and smaller
static int W_IsMarker(const char *marker, const char *name)
{
return !strncasecmp(name, marker, 8) ||
(*name == *marker && !strncasecmp(name+1, marker, 7));
}
// namespace data
struct nsdata_t
{
const char *startMarker;
const char *endMarker;
int li_namespace;
};
// namespaces
static nsdata_t wadNameSpaces[lumpinfo_t::ns_max] =
{
{ NULL, NULL, lumpinfo_t::ns_global },
{ "S_START", "S_END", lumpinfo_t::ns_sprites },
{ "T_START", "T_END", lumpinfo_t::ns_textures }, // PSX specific; is TX_START/TX_END in EE
{ "F_START", "F_END", lumpinfo_t::ns_flats },
};
//
// WadNamespace
//
// This class is a utility used only during coalesceMarkedResources, to build
// the namespace_t structures for the WadDirectory instance.
//
class WadNamespace
{
protected:
bool inMarkers; // currently between markers?
nsdata_t *nsdata;
PODCollection<lumpinfo_t *> marked; // lumps in namespace
int numMarked; // number added
// Returns true if the lump is a start marker for this namespace
bool lumpIsStartMarker(lumpinfo_t *lump) const
{
return (nsdata->startMarker &&
W_IsMarker(nsdata->startMarker, lump->name));
}
// Returns false if the lump is an end marker for this namespace
bool lumpIsEndMarker(lumpinfo_t *lump) const
{
return (nsdata->endMarker &&
W_IsMarker(nsdata->endMarker, lump->name));
}
public:
WadNamespace()
: inMarkers(false), nsdata(NULL), marked(), numMarked(0)
{
}
// Initialize from static data
void setNSData(nsdata_t *pNsData) { nsdata = pNsData; }
// Add a lump into this namespace
void addLump(lumpinfo_t *lump)
{
marked.add(lump);
lump->li_namespace = nsdata->li_namespace;
++numMarked;
}
// Check if a lump belongs in this namespace
void checkLump(lumpinfo_t *lump)
{
if(lumpIsStartMarker(lump))
inMarkers = true;
else if(lumpIsEndMarker(lump))
inMarkers = false;
else if(inMarkers || lump->li_namespace == nsdata->li_namespace)
{
// we are either between markers, or dealing with a pre-marked lump;
// special cases:
switch(nsdata->li_namespace)
{
case lumpinfo_t::ns_sprites:
// sf 10/26/99: ignore sprite lumps smaller than 8 bytes (the
// smallest possible) in size -- this was used by some dmadds
// wads as an 'empty' graphics resource
if(lump->size > 8)
addLump(lump);
break;
case lumpinfo_t::ns_flats:
// SoM: Ignore marker lumps inside F_START and F_END
if(lump->size > 0)
addLump(lump);
break;
default:
addLump(lump);
break;
}
}
}
// Get the number of lumps marked as belonging
int getNumMarked() const { return numMarked; }
// Merge this namespace back into the master directory
int mergeToDirectory(lumpinfo_t **lumpinfo, int index)
{
int i;
// append the marked list to the current end of the directory
for(i = 0; i < numMarked; i++)
lumpinfo[index + i] = marked[i];
return index + i;
}
};
//
// WadDirectory::coalesceMarkedResources
//
// killough 4/17/98: add namespace tags
//
void WadDirectory::coalesceMarkedResources()
{
WadNamespace namespaces[lumpinfo_t::ns_max];
int i, ns;
for(i = 0; i < lumpinfo_t::ns_max; i++)
namespaces[i].setNSData(&wadNameSpaces[i]);
// scan the entire wad directory; save off lumps that belong in each
// namespace
for(ns = lumpinfo_t::ns_sprites; ns < lumpinfo_t::ns_max; ns++)
{
for(int lumpnum = 0; lumpnum < numlumps; lumpnum++)
namespaces[ns].checkLump(lumpinfo[lumpnum]);
}
// now add any remaining unmarked lumps to ns_global
for(i = 0; i < numlumps; i++)
namespaces[lumpinfo_t::ns_global].checkLump(lumpinfo[i]);
// re-allocate lumpinfo if necessary
int totalLumps = 0;
for(ns = 0; ns < lumpinfo_t::ns_max; ns++)
{
int numMarked = namespaces[ns].getNumMarked();
m_namespaces[ns].numLumps = numMarked;
totalLumps += numMarked;
}
if(totalLumps != numlumps)
{
lumpinfo = erealloc(lumpinfo_t **, lumpinfo, totalLumps * sizeof(*lumpinfo));
numlumps = totalLumps;
}
// re-order the directory so that all namespaces are contiguous
int curLump = 0;
for(ns = 0; ns < lumpinfo_t::ns_max; ns++)
{
m_namespaces[ns].firstLump = curLump;
curLump = namespaces[ns].mergeToDirectory(lumpinfo, curLump);