/
w_wad.cpp
1693 lines (1444 loc) · 43.5 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
/*
** w_wad.cpp
**
**---------------------------------------------------------------------------
** Copyright 1998-2009 Randy Heit
** Copyright 2005-2009 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
**
*/
// HEADER FILES ------------------------------------------------------------
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "doomtype.h"
#include "m_argv.h"
#include "cmdlib.h"
#include "c_dispatch.h"
#include "w_wad.h"
#include "m_crc32.h"
#include "v_text.h"
#include "gi.h"
#include "resourcefiles/resourcefile.h"
#include "md5.h"
#include "doomstat.h"
#include "vm.h"
// MACROS ------------------------------------------------------------------
#define NULL_INDEX (0xffffffff)
//
// WADFILE I/O related stuff.
//
struct FWadCollection::LumpRecord
{
int wadnum;
FResourceLump *lump;
};
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
extern bool nospriterename;
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void PrintLastError ();
// PUBLIC DATA DEFINITIONS -------------------------------------------------
FWadCollection Wads;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// CODE --------------------------------------------------------------------
//==========================================================================
//
// uppercoppy
//
// [RH] Copy up to 8 chars, upper-casing them in the process
//==========================================================================
void uppercopy (char *to, const char *from)
{
int i;
for (i = 0; i < 8 && from[i]; i++)
to[i] = toupper (from[i]);
for (; i < 8; i++)
to[i] = 0;
}
FWadCollection::FWadCollection ()
{
}
FWadCollection::~FWadCollection ()
{
DeleteAll();
}
void FWadCollection::DeleteAll ()
{
LumpInfo.Clear();
NumLumps = 0;
// we must count backward to ensure that embedded WADs are deleted before
// the ones that contain their data.
for (int i = Files.Size() - 1; i >= 0; --i)
{
delete Files[i];
}
Files.Clear();
}
//==========================================================================
//
// W_InitMultipleFiles
//
// Pass a null terminated list of files to use. All files are optional,
// but at least one file must be found. Lump names can appear multiple
// times. The name searcher looks backwards, so a later file can
// override an earlier one.
//
//==========================================================================
void FWadCollection::InitMultipleFiles (TArray<FString> &filenames, const TArray<FString> &deletelumps)
{
int numfiles;
// open all the files, load headers, and count lumps
DeleteAll();
numfiles = 0;
for(unsigned i=0;i<filenames.Size(); i++)
{
int baselump = NumLumps;
AddFile (filenames[i]);
}
NumLumps = LumpInfo.Size();
if (NumLumps == 0)
{
I_FatalError ("W_InitMultipleFiles: no files found");
}
RenameNerve();
RenameSprites(deletelumps);
FixMacHexen();
// [RH] Set up hash table
Hashes.Resize(6 * NumLumps);
FirstLumpIndex = &Hashes[0];
NextLumpIndex = &Hashes[NumLumps];
FirstLumpIndex_FullName = &Hashes[NumLumps*2];
NextLumpIndex_FullName = &Hashes[NumLumps*3];
FirstLumpIndex_NoExt = &Hashes[NumLumps*4];
NextLumpIndex_NoExt = &Hashes[NumLumps*5];
InitHashChains ();
LumpInfo.ShrinkToFit();
Files.ShrinkToFit();
}
//-----------------------------------------------------------------------
//
// Adds an external file to the lump list but not to the hash chains
// It's just a simple means to assign a lump number to some file so that
// the texture manager can read from it.
//
//-----------------------------------------------------------------------
int FWadCollection::AddExternalFile(const char *filename)
{
FResourceLump *lump = new FExternalLump(filename);
FWadCollection::LumpRecord *lumprec = &LumpInfo[LumpInfo.Reserve(1)];
lumprec->lump = lump;
lumprec->wadnum = -1;
return LumpInfo.Size()-1; // later
}
//==========================================================================
//
// W_AddFile
//
// Files with a .wad extension are wadlink files with multiple lumps,
// other files are single lumps with the base filename for the lump name.
//
// [RH] Removed reload hack
//==========================================================================
void FWadCollection::AddFile (const char *filename, FileReader *wadr)
{
int startlump;
bool isdir = false;
FileReader wadreader;
if (wadr == nullptr)
{
// Does this exist? If so, is it a directory?
if (!DirEntryExists(filename, &isdir))
{
Printf(TEXTCOLOR_RED "%s: File or Directory not found\n", filename);
PrintLastError();
return;
}
if (!isdir)
{
if (!wadreader.OpenFile(filename))
{ // Didn't find file
Printf (TEXTCOLOR_RED "%s: File not found\n", filename);
PrintLastError ();
return;
}
}
}
else wadreader = std::move(*wadr);
if (!batchrun) Printf (" adding %s", filename);
startlump = NumLumps;
FResourceFile *resfile;
if (!isdir)
resfile = FResourceFile::OpenResourceFile(filename, wadreader);
else
resfile = FResourceFile::OpenDirectory(filename);
if (resfile != NULL)
{
uint32_t lumpstart = LumpInfo.Size();
resfile->SetFirstLump(lumpstart);
for (uint32_t i=0; i < resfile->LumpCount(); i++)
{
FResourceLump *lump = resfile->GetLump(i);
FWadCollection::LumpRecord *lump_p = &LumpInfo[LumpInfo.Reserve(1)];
lump_p->lump = lump;
lump_p->wadnum = Files.Size();
}
if (static_cast<int>(Files.Size()) == GetIwadNum() && gameinfo.gametype == GAME_Strife && gameinfo.flags & GI_SHAREWARE)
{
resfile->FindStrifeTeaserVoices();
}
Files.Push(resfile);
for (uint32_t i=0; i < resfile->LumpCount(); i++)
{
FResourceLump *lump = resfile->GetLump(i);
if (lump->Flags & LUMPF_EMBEDDED)
{
FString path;
path.Format("%s:%s", filename, lump->FullName.GetChars());
auto embedded = lump->NewReader();
AddFile(path, &embedded);
}
}
if (hashfile)
{
uint8_t cksum[16];
char cksumout[33];
memset(cksumout, 0, sizeof(cksumout));
if (wadreader.isOpen())
{
MD5Context md5;
wadreader.Seek(0, FileReader::SeekSet);
md5.Update(wadreader, (unsigned)wadreader.GetLength());
md5.Final(cksum);
for (size_t j = 0; j < sizeof(cksum); ++j)
{
sprintf(cksumout + (j * 2), "%02X", cksum[j]);
}
fprintf(hashfile, "file: %s, hash: %s, size: %d\n", filename, cksumout, (int)wadreader.GetLength());
}
else
fprintf(hashfile, "file: %s, Directory structure\n", filename);
for (uint32_t i = 0; i < resfile->LumpCount(); i++)
{
FResourceLump *lump = resfile->GetLump(i);
if (!(lump->Flags & LUMPF_EMBEDDED))
{
MD5Context md5;
auto reader = lump->NewReader();
md5.Update(reader, lump->LumpSize);
md5.Final(cksum);
for (size_t j = 0; j < sizeof(cksum); ++j)
{
sprintf(cksumout + (j * 2), "%02X", cksum[j]);
}
fprintf(hashfile, "file: %s, lump: %s, hash: %s, size: %d\n", filename,
lump->FullName.IsNotEmpty() ? lump->FullName.GetChars() : lump->Name,
cksumout, lump->LumpSize);
}
}
}
return;
}
}
//==========================================================================
//
// W_CheckIfWadLoaded
//
// Returns true if the specified wad is loaded, false otherwise.
// If a fully-qualified path is specified, then the wad must match exactly.
// Otherwise, any wad with that name will work, whatever its path.
// Returns the wads index if found, or -1 if not.
//
//==========================================================================
int FWadCollection::CheckIfWadLoaded (const char *name)
{
unsigned int i;
if (strrchr (name, '/') != NULL)
{
for (i = 0; i < Files.Size(); ++i)
{
if (stricmp (GetWadFullName (i), name) == 0)
{
return i;
}
}
}
else
{
for (i = 0; i < Files.Size(); ++i)
{
if (stricmp (GetWadName (i), name) == 0)
{
return i;
}
}
}
return -1;
}
//==========================================================================
//
// W_NumLumps
//
//==========================================================================
int FWadCollection::GetNumLumps () const
{
return NumLumps;
}
//==========================================================================
//
// GetNumFiles
//
//==========================================================================
int FWadCollection::GetNumWads () const
{
return Files.Size();
}
//==========================================================================
//
// W_CheckNumForName
//
// Returns -1 if name not found. The version with a third parameter will
// look exclusively in the specified wad for the lump.
//
// [RH] Changed to use hash lookup ala BOOM instead of a linear search
// and namespace parameter
//==========================================================================
int FWadCollection::CheckNumForName (const char *name, int space)
{
union
{
char uname[8];
uint64_t qname;
};
uint32_t i;
if (name == NULL)
{
return -1;
}
// Let's not search for names that are longer than 8 characters and contain path separators
// They are almost certainly full path names passed to this function.
if (strlen(name) > 8 && strpbrk(name, "/."))
{
return -1;
}
uppercopy (uname, name);
i = FirstLumpIndex[LumpNameHash (uname) % NumLumps];
while (i != NULL_INDEX)
{
FResourceLump *lump = LumpInfo[i].lump;
if (lump->qwName == qname)
{
if (lump->Namespace == space) break;
// If the lump is from one of the special namespaces exclusive to Zips
// the check has to be done differently:
// If we find a lump with this name in the global namespace that does not come
// from a Zip return that. WADs don't know these namespaces and single lumps must
// work as well.
if (space > ns_specialzipdirectory && lump->Namespace == ns_global &&
!(lump->Flags & LUMPF_ZIPFILE)) break;
}
i = NextLumpIndex[i];
}
return i != NULL_INDEX ? i : -1;
}
int FWadCollection::CheckNumForName (const char *name, int space, int wadnum, bool exact)
{
FResourceLump *lump;
union
{
char uname[8];
uint64_t qname;
};
uint32_t i;
if (wadnum < 0)
{
return CheckNumForName (name, space);
}
uppercopy (uname, name);
i = FirstLumpIndex[LumpNameHash (uname) % NumLumps];
// If exact is true if will only find lumps in the same WAD, otherwise
// also those in earlier WADs.
while (i != NULL_INDEX &&
(lump = LumpInfo[i].lump, lump->qwName != qname ||
lump->Namespace != space ||
(exact? (LumpInfo[i].wadnum != wadnum) : (LumpInfo[i].wadnum > wadnum)) ))
{
i = NextLumpIndex[i];
}
return i != NULL_INDEX ? i : -1;
}
DEFINE_ACTION_FUNCTION(_Wads, CheckNumForName)
{
PARAM_PROLOGUE;
PARAM_STRING(name);
PARAM_INT(ns);
PARAM_INT(wadnum);
PARAM_BOOL(exact);
ACTION_RETURN_INT(Wads.CheckNumForName(name, ns, wadnum, exact));
}
//==========================================================================
//
// W_GetNumForName
//
// Calls W_CheckNumForName, but bombs out if not found.
//
//==========================================================================
int FWadCollection::GetNumForName (const char *name, int space)
{
int i;
i = CheckNumForName (name, space);
if (i == -1)
I_Error ("W_GetNumForName: %s not found!", name);
return i;
}
//==========================================================================
//
// W_CheckNumForFullName
//
// Same as above but looks for a fully qualified name from a .zip
// These don't care about namespaces though because those are part
// of the path.
//
//==========================================================================
int FWadCollection::CheckNumForFullName (const char *name, bool trynormal, int namespc, bool ignoreext)
{
uint32_t i;
if (name == NULL)
{
return -1;
}
uint32_t *fli = ignoreext ? FirstLumpIndex_NoExt : FirstLumpIndex_FullName;
uint32_t *nli = ignoreext ? NextLumpIndex_NoExt : NextLumpIndex_FullName;
auto len = strlen(name);
for (i = fli[MakeKey(name) % NumLumps]; i != NULL_INDEX; i = nli[i])
{
if (strnicmp(name, LumpInfo[i].lump->FullName, len)) continue;
if (LumpInfo[i].lump->FullName[len] == 0) break; // this is a full match
if (ignoreext && LumpInfo[i].lump->FullName[len] == '.')
{
// is this the last '.' in the last path element, indicating that the remaining part of the name is only an extension?
if (strpbrk(LumpInfo[i].lump->FullName.GetChars() + len + 1, "./") == nullptr) break;
}
}
if (i != NULL_INDEX) return i;
if (trynormal && strlen(name) <= 8 && !strpbrk(name, "./"))
{
return CheckNumForName(name, namespc);
}
return -1;
}
DEFINE_ACTION_FUNCTION(_Wads, CheckNumForFullName)
{
PARAM_PROLOGUE;
PARAM_STRING(name);
ACTION_RETURN_INT(Wads.CheckNumForFullName(name));
}
int FWadCollection::CheckNumForFullName (const char *name, int wadnum)
{
uint32_t i;
if (wadnum < 0)
{
return CheckNumForFullName (name);
}
i = FirstLumpIndex_FullName[MakeKey (name) % NumLumps];
while (i != NULL_INDEX &&
(stricmp(name, LumpInfo[i].lump->FullName) || LumpInfo[i].wadnum != wadnum))
{
i = NextLumpIndex_FullName[i];
}
return i != NULL_INDEX ? i : -1;
}
//==========================================================================
//
// W_GetNumForFullName
//
// Calls W_CheckNumForFullName, but bombs out if not found.
//
//==========================================================================
int FWadCollection::GetNumForFullName (const char *name)
{
int i;
i = CheckNumForFullName (name);
if (i == -1)
I_Error ("GetNumForFullName: %s not found!", name);
return i;
}
//==========================================================================
//
// link a texture with a given lump
//
//==========================================================================
void FWadCollection::SetLinkedTexture(int lump, FTexture *tex)
{
if ((size_t)lump < NumLumps)
{
FResourceLump *reslump = LumpInfo[lump].lump;
reslump->LinkedTexture = tex;
}
}
//==========================================================================
//
// retrieve linked texture
//
//==========================================================================
FTexture *FWadCollection::GetLinkedTexture(int lump)
{
if ((size_t)lump < NumLumps)
{
FResourceLump *reslump = LumpInfo[lump].lump;
return reslump->LinkedTexture;
}
return NULL;
}
//==========================================================================
//
// W_LumpLength
//
// Returns the buffer size needed to load the given lump.
//
//==========================================================================
int FWadCollection::LumpLength (int lump) const
{
if ((size_t)lump >= NumLumps)
{
I_Error ("W_LumpLength: %i >= NumLumps",lump);
}
return LumpInfo[lump].lump->LumpSize;
}
//==========================================================================
//
// GetLumpOffset
//
// Returns the offset from the beginning of the file to the lump.
// Returns -1 if the lump is compressed or can't be read directly
//
//==========================================================================
int FWadCollection::GetLumpOffset (int lump)
{
if ((size_t)lump >= NumLumps)
{
I_Error ("GetLumpOffset: %i >= NumLumps",lump);
}
return LumpInfo[lump].lump->GetFileOffset();
}
//==========================================================================
//
// GetLumpOffset
//
//==========================================================================
int FWadCollection::GetLumpFlags (int lump)
{
if ((size_t)lump >= NumLumps)
{
return 0;
}
return LumpInfo[lump].lump->Flags;
}
//==========================================================================
//
// W_LumpNameHash
//
// NOTE: s should already be uppercase, in contrast to the BOOM version.
//
// Hash function used for lump names.
// Must be mod'ed with table size.
// Can be used for any 8-character names.
//
//==========================================================================
uint32_t FWadCollection::LumpNameHash (const char *s)
{
const uint32_t *table = GetCRCTable ();;
uint32_t hash = 0xffffffff;
int i;
for (i = 8; i > 0 && *s; --i, ++s)
{
hash = CRC1 (hash, *s, table);
}
return hash ^ 0xffffffff;
}
//==========================================================================
//
// W_InitHashChains
//
// Prepares the lumpinfos for hashing.
// (Hey! This looks suspiciously like something from Boom! :-)
//
//==========================================================================
void FWadCollection::InitHashChains (void)
{
char name[8];
unsigned int i, j;
// Mark all buckets as empty
memset (FirstLumpIndex, 255, NumLumps*sizeof(FirstLumpIndex[0]));
memset (NextLumpIndex, 255, NumLumps*sizeof(NextLumpIndex[0]));
memset (FirstLumpIndex_FullName, 255, NumLumps*sizeof(FirstLumpIndex_FullName[0]));
memset (NextLumpIndex_FullName, 255, NumLumps*sizeof(NextLumpIndex_FullName[0]));
memset(FirstLumpIndex_NoExt, 255, NumLumps * sizeof(FirstLumpIndex_NoExt[0]));
memset(NextLumpIndex_NoExt, 255, NumLumps * sizeof(NextLumpIndex_NoExt[0]));
// Now set up the chains
for (i = 0; i < (unsigned)NumLumps; i++)
{
uppercopy (name, LumpInfo[i].lump->Name);
j = LumpNameHash (name) % NumLumps;
NextLumpIndex[i] = FirstLumpIndex[j];
FirstLumpIndex[j] = i;
// Do the same for the full paths
if (LumpInfo[i].lump->FullName.IsNotEmpty())
{
j = MakeKey(LumpInfo[i].lump->FullName) % NumLumps;
NextLumpIndex_FullName[i] = FirstLumpIndex_FullName[j];
FirstLumpIndex_FullName[j] = i;
FString nameNoExt = LumpInfo[i].lump->FullName;
auto dot = nameNoExt.LastIndexOf('.');
auto slash = nameNoExt.LastIndexOf('/');
if (dot > slash) nameNoExt.Truncate(dot);
j = MakeKey(nameNoExt) % NumLumps;
NextLumpIndex_NoExt[i] = FirstLumpIndex_NoExt[j];
FirstLumpIndex_NoExt[j] = i;
}
}
}
//==========================================================================
//
// RenameSprites
//
// Renames sprites in IWADs so that unique actors can have unique sprites,
// making it possible to import any actor from any game into any other
// game without jumping through hoops to resolve duplicate sprite names.
// You just need to know what the sprite's new name is.
//
//==========================================================================
void FWadCollection::RenameSprites (const TArray<FString> &deletelumps)
{
bool renameAll;
bool MNTRZfound = false;
const char *altbigfont = gameinfo.gametype == GAME_Strife? "SBIGFONT" : (gameinfo.gametype & GAME_Raven)? "HBIGFONT" : "DBIGFONT";
static const uint32_t HereticRenames[] =
{ MAKE_ID('H','E','A','D'), MAKE_ID('L','I','C','H'), // Ironlich
};
static const uint32_t HexenRenames[] =
{ MAKE_ID('B','A','R','L'), MAKE_ID('Z','B','A','R'), // ZBarrel
MAKE_ID('A','R','M','1'), MAKE_ID('A','R','_','1'), // MeshArmor
MAKE_ID('A','R','M','2'), MAKE_ID('A','R','_','2'), // FalconShield
MAKE_ID('A','R','M','3'), MAKE_ID('A','R','_','3'), // PlatinumHelm
MAKE_ID('A','R','M','4'), MAKE_ID('A','R','_','4'), // AmuletOfWarding
MAKE_ID('S','U','I','T'), MAKE_ID('Z','S','U','I'), // ZSuitOfArmor and ZArmorChunk
MAKE_ID('T','R','E','1'), MAKE_ID('Z','T','R','E'), // ZTree and ZTreeDead
MAKE_ID('T','R','E','2'), MAKE_ID('T','R','E','S'), // ZTreeSwamp150
MAKE_ID('C','A','N','D'), MAKE_ID('B','C','A','N'), // ZBlueCandle
MAKE_ID('R','O','C','K'), MAKE_ID('R','O','K','K'), // rocks and dirt in a_debris.cpp
MAKE_ID('W','A','T','R'), MAKE_ID('H','W','A','T'), // Strife also has WATR
MAKE_ID('G','I','B','S'), MAKE_ID('P','O','L','5'), // RealGibs
MAKE_ID('E','G','G','M'), MAKE_ID('P','R','K','M'), // PorkFX
MAKE_ID('I','N','V','U'), MAKE_ID('D','E','F','N'), // Icon of the Defender
};
static const uint32_t StrifeRenames[] =
{ MAKE_ID('M','I','S','L'), MAKE_ID('S','M','I','S'), // lots of places
MAKE_ID('A','R','M','1'), MAKE_ID('A','R','M','3'), // MetalArmor
MAKE_ID('A','R','M','2'), MAKE_ID('A','R','M','4'), // LeatherArmor
MAKE_ID('P','M','A','P'), MAKE_ID('S','M','A','P'), // StrifeMap
MAKE_ID('T','L','M','P'), MAKE_ID('T','E','C','H'), // TechLampSilver and TechLampBrass
MAKE_ID('T','R','E','1'), MAKE_ID('T','R','E','T'), // TreeStub
MAKE_ID('B','A','R','1'), MAKE_ID('B','A','R','C'), // BarricadeColumn
MAKE_ID('S','H','T','2'), MAKE_ID('M','P','U','F'), // MaulerPuff
MAKE_ID('B','A','R','L'), MAKE_ID('B','B','A','R'), // StrifeBurningBarrel
MAKE_ID('T','R','C','H'), MAKE_ID('T','R','H','L'), // SmallTorchLit
MAKE_ID('S','H','R','D'), MAKE_ID('S','H','A','R'), // glass shards
MAKE_ID('B','L','S','T'), MAKE_ID('M','A','U','L'), // Mauler
MAKE_ID('L','O','G','G'), MAKE_ID('L','O','G','W'), // StickInWater
MAKE_ID('V','A','S','E'), MAKE_ID('V','A','Z','E'), // Pot and Pitcher
MAKE_ID('C','N','D','L'), MAKE_ID('K','N','D','L'), // Candle
MAKE_ID('P','O','T','1'), MAKE_ID('M','P','O','T'), // MetalPot
MAKE_ID('S','P','I','D'), MAKE_ID('S','T','L','K'), // Stalker
};
const uint32_t *renames;
int numrenames;
switch (gameinfo.gametype)
{
case GAME_Doom:
default:
// Doom's sprites don't get renamed.
renames = nullptr;
numrenames = 0;
break;
case GAME_Heretic:
renames = HereticRenames;
numrenames = sizeof(HereticRenames)/8;
break;
case GAME_Hexen:
renames = HexenRenames;
numrenames = sizeof(HexenRenames)/8;
break;
case GAME_Strife:
renames = StrifeRenames;
numrenames = sizeof(StrifeRenames)/8;
break;
}
for (uint32_t i=0; i< LumpInfo.Size(); i++)
{
// check for full Minotaur animations. If this is not found
// some frames need to be renamed.
if (LumpInfo[i].lump->Namespace == ns_sprites)
{
if (LumpInfo[i].lump->dwName == MAKE_ID('M', 'N', 'T', 'R') && LumpInfo[i].lump->Name[4] == 'Z' )
{
MNTRZfound = true;
break;
}
}
}
renameAll = !!Args->CheckParm ("-oldsprites") || nospriterename;
for (uint32_t i = 0; i < LumpInfo.Size(); i++)
{
if (LumpInfo[i].lump->Namespace == ns_sprites)
{
// Only sprites in the IWAD normally get renamed
if (renameAll || LumpInfo[i].wadnum == GetIwadNum())
{
for (int j = 0; j < numrenames; ++j)
{
if (LumpInfo[i].lump->dwName == renames[j*2])
{
LumpInfo[i].lump->dwName = renames[j*2+1];
}
}
if (gameinfo.gametype == GAME_Hexen)
{
if (CheckLumpName (i, "ARTIINVU"))
{
LumpInfo[i].lump->Name[4]='D'; LumpInfo[i].lump->Name[5]='E';
LumpInfo[i].lump->Name[6]='F'; LumpInfo[i].lump->Name[7]='N';
}
}
}
if (!MNTRZfound)
{
if (LumpInfo[i].lump->dwName == MAKE_ID('M', 'N', 'T', 'R'))
{
if (LumpInfo[i].lump->Name[4] >= 'F' && LumpInfo[i].lump->Name[4] <= 'K')
{
LumpInfo[i].lump->Name[4] += 'U' - 'F';
}
}
}
// When not playing Doom rename all BLOD sprites to BLUD so that
// the same blood states can be used everywhere
if (!(gameinfo.gametype & GAME_DoomChex))
{
if (LumpInfo[i].lump->dwName == MAKE_ID('B', 'L', 'O', 'D'))
{
LumpInfo[i].lump->dwName = MAKE_ID('B', 'L', 'U', 'D');
}
}
}
else if (LumpInfo[i].lump->Namespace == ns_global)
{
if (LumpInfo[i].wadnum == GetIwadNum() && deletelumps.Find(LumpInfo[i].lump->Name) < deletelumps.Size())
{
LumpInfo[i].lump->Name[0] = 0; // Lump must be deleted from directory.
}
// Rename the game specific big font lumps so that the font manager does not have to do problematic special checks for them.
else if (!strcmp(LumpInfo[i].lump->Name, altbigfont))
{
strcpy(LumpInfo[i].lump->Name, "BIGFONT");
}
}
}
}
//==========================================================================
//
// RenameNerve
//
// Renames map headers and map name pictures in nerve.wad so as to load it
// alongside Doom II and offer both episodes without causing conflicts.
// MD5 checksum for NERVE.WAD: 967d5ae23daf45196212ae1b605da3b0
//
//==========================================================================
void FWadCollection::RenameNerve ()
{
if (gameinfo.gametype != GAME_Doom)
return;
bool found = false;
uint8_t cksum[16];
static const uint8_t nerve[16] = { 0x96, 0x7d, 0x5a, 0xe2, 0x3d, 0xaf, 0x45, 0x19,
0x62, 0x12, 0xae, 0x1b, 0x60, 0x5d, 0xa3, 0xb0 };
size_t nervesize = 3819855; // NERVE.WAD's file size
int w = GetIwadNum();
while (++w < GetNumWads())
{
auto fr = GetFileReader(w);
if (fr == NULL)
{
continue;
}
if (fr->GetLength() != (long)nervesize)
{
// Skip MD5 computation when there is a
// cheaper way to know this is not the file
continue;
}
fr->Seek(0, FileReader::SeekSet);
MD5Context md5;
md5.Update(*fr, (unsigned)fr->GetLength());
md5.Final(cksum);
if (memcmp(nerve, cksum, 16) == 0)
{
found = true;
break;
}
}
if (!found)
return;
for (int i = GetFirstLump(w); i <= GetLastLump(w); i++)
{
// Only rename the maps from NERVE.WAD
assert(LumpInfo[i].wadnum == w);
if (LumpInfo[i].lump->dwName == MAKE_ID('C', 'W', 'I', 'L'))
{
LumpInfo[i].lump->Name[0] = 'N';
}
else if (LumpInfo[i].lump->dwName == MAKE_ID('M', 'A', 'P', '0'))
{
LumpInfo[i].lump->Name[6] = LumpInfo[i].lump->Name[4];
LumpInfo[i].lump->Name[5] = '0';
LumpInfo[i].lump->Name[4] = 'L';
LumpInfo[i].lump->dwName = MAKE_ID('L', 'E', 'V', 'E');
}
}
}
//==========================================================================
//
// FixMacHexen
//
// Discard all extra lumps in Mac version of Hexen IWAD (demo or full)
// to avoid any issues caused by names of these lumps, including:
// * Wrong height of small font
// * Broken life bar of mage class
//
//==========================================================================
void FWadCollection::FixMacHexen()
{
if (GAME_Hexen != gameinfo.gametype)
{
return;
}
FileReader *reader = GetFileReader(GetIwadNum());
auto iwadSize = reader->GetLength();
static const long DEMO_SIZE = 13596228;
static const long BETA_SIZE = 13749984;
static const long FULL_SIZE = 21078584;
if ( DEMO_SIZE != iwadSize