forked from kriswema/resgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resgenclass.cpp
1068 lines (894 loc) · 25.3 KB
/
resgenclass.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
/*
RESGen. A tool to create .res files for Half-Life.
Copyright (C) 2000-2005 Jeroen Bogers
This file is part of RESGen.
RESGen 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.
RESGen 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 RESGen; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// resgenclass.cpp: implementation of the RESGen class.
//
//////////////////////////////////////////////////////////////////////
// RESGen does it's own security checks, no need to add VS2005's layer
#define _CRT_SECURE_NO_DEPRECATE
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <glob.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "enttokenizer.h"
#include "hltypes.h"
#include "resgenclass.h"
#include "resgen.h"
#include "resourcelistbuilder.h"
#include "util.h"
// Half-Life BSP version
#define BSPVERSION 30
// Update status bar every x parsings.
// The higher the number, the less excess statbar writing, but also makes the statbar 'lag'
#define STAT_MAX 15
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
std::vector<std::string>::iterator findStringNoCase(std::vector<std::string> &vec, const std::string &element)
{
for(std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); ++it)
{
if(ICompareStrings(*it, element) == 0)
{
return it;
}
}
return vec.end();
}
RESGen::RESGen()
{
checkforexcludes = false;
}
RESGen::~RESGen()
{
}
void RESGen::SetParams(bool beverbal, bool statline, bool overwrt, bool lcase, bool mcase, bool prsresource, bool preservewads_, bool cdisp)
{
verbal = beverbal;
statusline = statline;
overwrite = overwrt;
tolower = lcase;
matchcase = mcase;
parseresource = prsresource;
preservewads = preservewads_;
contentdisp = cdisp;
}
int RESGen::MakeRES(std::string &map, int fileindex, size_t filecount, const StringMap &resources, std::vector<std::string> &resourcePaths_)
{
resourcePaths = resourcePaths_;
std::string basefolder;
std::string basefilename;
splitPath(map, basefolder, basefilename);
const std::string resName = basefolder + basefilename + ".res";
if (verbal)
{
printf("Creating .res file %s [%d/" SIZE_T_SPECIFIER "].\n", resName.c_str(), fileindex, filecount);
}
// Check if resfile doesn't already exist
const bool fileexists = fileExists(resName);
if (!overwrite && fileexists)
{
// File found, but we don't want to overwrite.
printf("%s already exists. Skipping file.\n", resName.c_str());
return 1;
}
// Clear the resfile list to be sure (SHOULD be empty)
resfile.clear();
// Clear the texture list to be sure (SHOULD be empty)
texturelist.clear();
// first, get the enity data
std::string entdata;
if(!LoadBSPData(map, entdata, texturelist))
{
// error. return
return 1;
}
statcount = STAT_MAX; // make statbar print at once
EntTokenizer entDataTokenizer(entdata);
try
{
const EntTokenizer::KeyValuePair* kv = entDataTokenizer.NextPair();
// Note that we reparse the mapinfo.
if(!kv)
{
printf("Error parsing \"%s\".\n", map.c_str());
return 1;
}
while (kv && (entDataTokenizer.GetNumBlocksRead() == 0))
{
if (!strcmp(kv->first, "wad"))
{
std::string value(kv->second);
if (!value.empty()) // Don't try to parse an empty listing
{
// seperate the WAD files and save
size_t i = 0;
size_t seppos;
while ((seppos = value.find(';', i)) != std::string::npos)
{
AddWad(value, i, seppos - i); // Add wad to reslist
i = seppos + 1;
}
// There might be a wad file left in the list, check for it
if (i < value.length())
{
// it should be equal, there is a wadfile left!
AddWad(value, i, value.length() - i);
}
}
}
else if (!strcmp(kv->first, "skyname"))
{
std::string value(kv->second);
// Add al 6 sky textures here
AddRes(value, "gfx/env/", "up.tga");
AddRes(value, "gfx/env/", "dn.tga");
AddRes(value, "gfx/env/", "lf.tga");
AddRes(value, "gfx/env/", "rt.tga");
AddRes(value, "gfx/env/", "ft.tga");
AddRes(value, "gfx/env/", "bk.tga");
}
kv = entDataTokenizer.NextPair();
}
while (kv)
{
const ptrdiff_t keyLength = entDataTokenizer.GetLatestKeyLength();
const ptrdiff_t valueLength = entDataTokenizer.GetLatestValueLength();
// Early out - check if key ends in 'speak'
if(
(keyLength >= 5)
&& !strcmp(kv->first + keyLength - 5, "speak")
)
{
// Guessing which keys have spoken sentences is too likely to
// cause false positives - instead use a whitelist of keys
// known to contain sentences
// TODO: This can still cause false positives if used on a different entity/mod
// TODO: Ideally parse FGD corresponding to map
if(
!strcmp(kv->first, "AP_speak")
|| !strcmp(kv->first, "non_owners_team_speak")
|| !strcmp(kv->first, "non_team_speak")
|| !strcmp(kv->first, "owners_team_speak")
|| !strcmp(kv->first, "speak")
|| !strcmp(kv->first, "team_speak")
)
{
ParseSentence(kv->second);
}
}
const char *token = kv->second;
// TODO: This is fast, but should be made more robust if possible
// Need at least 5 chars, assuming filename is:
// [alpha][.][alpha]{3}
if(valueLength >= 5)
{
if(token[valueLength - 4] == '.')
{
const int c1 = ::tolower(token[valueLength - 3]);
const int c2 = ::tolower(token[valueLength - 2]);
const int c3 = ::tolower(token[valueLength - 1]);
if(c1 == 'm' && c2 == 'd' && c3 == 'l')
{
// mdl file
AddRes(token);
}
if(c1 == 'w' && c2 == 'a' && c3 == 'v')
{
// wave file
AddRes(token, "sound/");
}
if(c1 == 's' && c2 == 'p' && c3 == 'r')
{
// sprite file
AddRes(token);
}
if(c1 == 'b' && c2 == 'm' && c3 == 'p')
{
// bitmap file
AddRes(token);
}
if(c1 == 't' && c2 == 'g' && c3 == 'a')
{
// targa file
AddRes(token);
}
}
}
// update statbar
if (statusline && statcount == STAT_MAX)
{
// Reset the statcount
statcount = 0;
// Calculate the percentage completed of the current file.
size_t progress = static_cast<size_t>(token - &entdata[0]);
size_t percentage = ((progress + 1) * 101) / entdata.length(); // Make the length one too long.
if (percentage > 100)
{
// Make sure we don;t go over 100%
percentage = 100;
}
printf("\r(" SIZE_T_SPECIFIER "%%) [%d/" SIZE_T_SPECIFIER "]", percentage, fileindex, filecount);
}
else
{
statcount++;
}
kv = entDataTokenizer.NextPair();
}
}
catch(ParseException &parseException)
{
if(parseException.GetCharNum() >= 0)
{
printf("Failed to parse '%s': %s (char: %d)\n", map.c_str(), parseException.what(), parseException.GetCharNum());
}
else
{
printf("Failed to parse '%s': %s\n", map.c_str(), parseException.what());
}
return 1;
}
if (statusline)
{
// erase statusline
printf("\r%-21s\r", ""); // easier to adjust length this way
}
entdata.clear();
// Try to find info txt and overview data
std::string overviewPath = basefolder + ".." + PATH_SEPARATOR + "overviews" + PATH_SEPARATOR + basefilename;
if(fileExists(overviewPath + ".txt"))
{
// file found, but we need the tga or bmp too
if(fileExists(overviewPath + ".tga"))
{
// txt found too, add both files to res list
AddRes(basefilename, "overviews/", ".tga");
AddRes(basefilename, "overviews/", ".txt");
}
else if(fileExists(overviewPath + ".bmp"))
{
// txt found too, add both files to res list
AddRes(basefilename, "overviews/", ".bmp");
AddRes(basefilename, "overviews/", ".txt");
}
}
// Resource list has been made.
int status = 0; // RES status, 0 means ok, 2 means missing resource
std::vector<std::string> extraResources;
// Check for resources on disk
if (!resourcePaths.empty())
{
//printf("\nStarting resource check:\n");
StringMap::iterator it = resfile.begin();
while(it != resfile.end())
{
bool bErase = false;
StringMap::const_iterator resourceIt = resources.find(it->first);
if(resourceIt == resources.end())
{
// file not found - maybe it's excluded?
if(excludelist.find(it->first) != excludelist.end())
{
// file found - it's an exclude
if (contentdisp)
{
printf("Resource is excluded: %s\n", it->second.c_str());
}
}
else if (CompareStrEnd(it->first, ".wad"))
{
// not a wad file
if (verbal)
{
printf("Resource file not found: %s\n", it->second.c_str());
}
status = 2; // res file might not be complete
}
else
{
// wad file is not critical, so no status change
if (contentdisp)
{
printf("Resource file not found: %s\n", it->second.c_str());
}
}
bErase = true;
}
else
{
if (matchcase)
{
// match case
it->second = resourceIt->second;
}
if (parseresource)
{
if (!CompareStrEnd(it->first, ".wad"))
{
// Check if wad file is used
if (!CheckWadUse(resourceIt)) // We MUST have the right file
{
// Wad is NOT being used
if (contentdisp)
{
printf("WAD file not used: %s\n", it->second.c_str());
}
if(!preservewads)
{
bErase = true;
}
}
}
else if (!CompareStrEnd(it->first, ".mdl"))
{
// Check model for external texture
if (CheckModelExtTexture(resourceIt->second))
{
// Uses external texture, add
std::string extmdltex = it->second.substr(0, it->second.length() - 4); // strip extention
extmdltex += "T.mdl"; // add T and extention
if(
(resfile.find(strToLowerCopy(extmdltex)) == resfile.end())
&& (findStringNoCase(extraResources, extmdltex) == extraResources.end())
)
{
extraResources.push_back(extmdltex);
if (contentdisp)
{
printf("MDL texture file added: %s\n", extmdltex.c_str());
}
}
}
}
}
}
if(bErase)
{
it = resfile.erase(it);
}
else
{
++it;
}
}
for(std::vector<std::string>::const_iterator extraIt = extraResources.begin(); extraIt != extraResources.end(); ++extraIt)
{
resfile[strToLowerCopy(*extraIt)] = *extraIt;
}
}
// Check if resource has to be excluded
if (checkforexcludes)
{
//printf("\nStarting exclude check:\n");
StringMap::iterator it = resfile.begin();
while(it != resfile.end())
{
if(excludelist.find(it->first) != excludelist.end())
{
// file found
if (contentdisp)
{
printf("Resource is excluded: %s\n", it->second.c_str());
}
it = resfile.erase(it);
}
else
{
++it;
}
}
}
// Give a list of missing textures
if (parseresource && !resourcePaths.empty() && verbal)
{
if (!texturelist.empty())
{
status = 2; // res file might not be complete
for(StringMap::const_iterator it = texturelist.begin(); it != texturelist.end(); ++it)
{
printf("Texture not found in wad files: %s\n", it->second.c_str());
}
}
}
if (resfile.empty() && rfastring.empty())
{
// no resources!
if (verbal) { printf("No resources were found for \"%s.res\".", basefilename.c_str()); }
if (fileexists)
{
// File exists, delete it.
// WHAT? No check for overwrite? No!
// Think of it, if the file exists we MUST be in overwrite mode to even get to this point!
remove(resName.c_str());
if (verbal)
{
printf(" Deleting existing res file.\n");
}
}
else
{
// File doesn't exist, so we don't have to delete it.
if (verbal)
{
printf(" Skipping file.\n");
}
}
return status;
}
// Collecting resfile entries is done, now write the res file.
if (!WriteRes(basefolder, basefilename))
{
return 1;
}
// File written successfully. We can safely erase the resfile and texture list
resfile.clear();
texturelist.clear();
return status;
}
bool RESGen::LoadBSPData(const std::string &file, std::string &entdata, StringMap & texlist)
{
// first open the file.
File bsp(file, "rb"); // read in binary mode.
if (bsp == NULL)
{
printf("Error opening \"%s\"\n", file.c_str());
return false;
}
// file open.. read header
bsp_header header;
if (fread(&header, sizeof(bsp_header), 1, bsp) != 1)
{
// header NOT read properly!
printf("Error opening \"%s\". Corrupt BSP file.\n", file.c_str());
return false;
}
if (header.version != BSPVERSION)
{
printf("Error opening \"%s\". Incorrect BSP version.\n", file.c_str());
return false;
}
if (header.ent_header.fileofs <= 0)
{
// File corrupted
printf("Error opening \"%s\". Corrupt BSP header.\n", file.c_str());
return false;
}
// read entity data
entdata.resize(header.ent_header.filelen);
fseek(bsp, header.ent_header.fileofs, SEEK_SET);
if (fread(&entdata[0], header.ent_header.filelen, 1, bsp) != 1)
{
// not the right ammount of data was read
printf("Error opening \"%s\". BSP file corrupt.\n", file.c_str());
return false;
}
if (parseresource && !resourcePaths.empty())
{
// Load names of external textures
fseek(bsp, header.tex_header.fileofs, SEEK_SET); // go to start of texture data
size_t texcount;
if (fread(&texcount, sizeof(int), 1, bsp) != 1) // first we want to know the number of files.
{
// header NOT read properly!
printf("Error opening \"%s\". Corrupt texture header.\n", file.c_str());
return false;
}
if (texcount > 0)
{
// Textures available, read all offsets
std::vector<char> offsets(sizeof(int) * texcount);
size_t i = fread(offsets.data(), sizeof(int), texcount, bsp);
if (i != texcount) // load texture offsets
{
// header NOT read properly!
printf("Error opening \"%s\". Corrupt texture data.\n read: " SIZE_T_SPECIFIER ", expect: " SIZE_T_SPECIFIER "\n", file.c_str(), i, texcount);
return false;
}
for (i = 0; i < texcount; i++)
{
// go to texture location
fseek(bsp, header.tex_header.fileofs + offsets[i], SEEK_SET);
// read texture data
texdata_s texdata;
if (fread(&texdata, sizeof(texdata_s), 1, bsp) != 1)
{
// header NOT read properly!
printf("Error opening \"%s\". Corrupt BSP file.\n", file.c_str());
return false;
}
// is this a wad based texture?
if (texdata.offsets[0] == 0 && texdata.offsets[1] == 0 && texdata.offsets[2] == 0 && texdata.offsets[3] == 0)
{
// No texture for any mip level, so must be in a wad
std::string texfileLower = strToLowerCopy(texdata.name);
texlist[texfileLower] = texdata.name;
}
}
}
}
#ifdef _DEBUG
// Debug write entity data to file
File tmp(file + "_ent.txt", "w");
fprintf(tmp, "%s", entdata.c_str());
#endif
return true;
}
void RESGen::AddRes(std::string res, const char * const prefix, const char * const suffix)
{
// Sometimes res entries start with a non alphanumeric character. Strip
// until valid char found
while (
!res.empty()
&& !isalnum(res[0])
)
{
// Remove character
res.erase(res.begin());
}
if(res.empty())
{
// Nothing to add
return;
}
replaceCharAll(res, '\\', '/');
// Add prefix and suffix
if (prefix)
{
res.insert(0, prefix);
}
if (suffix)
{
res += suffix;
}
if (tolower)
{
// Convert name to lowercase
strToLower(res);
}
// Add file to list if it isn't in it yet.
// We shouldn't care if this overwrites a previous entry (it shouldn't) -
// it'll only differ by case
resfile[strToLowerCopy(res)] = res;
// Report file found
if (contentdisp)
{
printf("\r%-21s\n", res.c_str()); // With 21 chars, there is support for up to 999999 bsp's to parse untill the statbar might remain in screen
}
statcount = STAT_MAX; // Make statbar print on next update
return;
}
void RESGen::AddWad(const std::string &wadlist, size_t start, size_t len)
{
std::string wadfile = wadlist.substr(start, len);
replaceCharAll(wadfile, '\\', '/');
// strip folders
wadfile = wadfile.substr(wadfile.rfind('/') + 1);
// Add file to reslist
AddRes(wadfile);
}
bool RESGen::WriteRes(const std::string &folder, const std::string &mapname)
{
// This function writes a standard res file.
// Open the file
File f(folder + mapname + ".res", "w");
if (f == NULL)
{
printf("Failed to open %s for writing.\n", (folder + mapname + ".res").c_str());
return false;
}
// Header
fprintf(f, "// %s - created with RESGen v%s.\n", (mapname + ".res").c_str(), VERSION);
fprintf(f, "// RESGen is made by Jeroen \"ShadowLord\" Bogers,\n");
fprintf(f, "// with serveral improvements and additions by Zero3Cool.\n");
fprintf(f, "// For more info go to http://resgen.hltools.com\n");
fprintf(f, "\n// .res entries (" SIZE_T_SPECIFIER "):\n", resfile.size());
// Resources
for (StringMap::const_iterator it = resfile.begin(); it != resfile.end(); ++it)
{
fprintf(f, "%s\n", it->second.c_str());
}
// RFA file, if needed
if (!rfastring.empty())
{
fprintf(f, "\n// Added .res content:\n%s\n", rfastring.c_str());
}
return true;
}
bool RESGen::LoadRfaFile(std::string &filename)
{
if (filename.empty())
{
// no rfa file, ignore
return true;
}
// Add .rfa extention if needed
if (CompareStrEndNoCase(filename,".rfa"))
{
// not found, add
filename += ".rfa";
}
std::string str;
const bool bSuccess = readFile(filename, str);
if(bSuccess)
{
rfastring = str;
}
else
{
printf("Error reading rfa file: \"%s\"\n", filename.c_str());
}
return bSuccess;
}
bool RESGen::LoadExludeFile(std::string &listfile)
{
if (listfile.empty())
{
// Was called without a proper argument, fail
return false;
}
if (CompareStrEndNoCase(listfile, ".rfa"))
{
// .rfa extension missing, add it
listfile += ".rfa";
}
File f(listfile, "rt"); // Text mode
if (f == NULL)
{
// Error opening file, abort
printf("Error: Could not open the specified exclude list %s!\n", listfile.c_str());
return false;
}
checkforexcludes = true; // We want to check for excludes
// loop to read file.. each line is an exclude
std::string line;
char linebuf[1024]; // optimal size for VString allocs
while (fgets(linebuf, 1024, f))
{
line += linebuf;
if (line[line.length() - 1] == '\n')
{
leftTrim(line);
rightTrim(line);
if (line.compare(0, 2, "//") && line.length() != 0)
{
// Convert backslashes to slashes
replaceCharAll(line, '\\', '/');
// Not a comment or empty line
excludelist[strToLowerCopy(line)] = line;
}
line.clear();
}
}
if (line.length() > 0)
{
leftTrim(line);
rightTrim(line);
if (line.compare(0, 2, "//") && line.length() != 0)
{
// Convert backslashes to slashes
replaceCharAll(line, '\\', '/');
// Not a comment or empty line
excludelist[strToLowerCopy(line)] = line;
}
}
return true;
}
bool RESGen::CacheWad(const std::string &wadfile)
{
File wad;
if(!OpenFirstValidPath(wad, wadfile, "rb"))
{
printf("Failed to open WAD file \"%s\".\n", wadfile.c_str());
return false;
}
wadheader_s header;
if (fread(&header, sizeof(wadheader_s), 1, wad) != 1)
{
printf("WAD file \"%s\" is corrupt.\n", wadfile.c_str());
return false;
}
if (strncmp(header.identification, "WAD", 3))
{
printf("\"%s\" is not a WAD file.\n", wadfile.c_str());
return false;
}
if (header.identification[3] != '2' && header.identification[3] != '3')
{
printf("Incorrect WAD file version for \"%s\"\n", wadfile.c_str());
return false;
}
if (fseek(wad, header.infotableofs, SEEK_SET))
{
printf("Cannot find WAD info table in \"%s\"\n", wadfile.c_str());
return false;
}
const std::string wadFileLower(strToLowerCopy(wadfile));
for (int i = 0; i < header.numlumps; i++)
{
wadlumpinfo_s lumpinfo;
if (fread(&lumpinfo, sizeof(wadlumpinfo_s), 1, wad) != 1)
{
printf("WAD file info table \"%s\" is corrupt.\n", wadfile.c_str());
return false;
}
std::string lumpNameLower(lumpinfo.name);
strToLower(lumpNameLower);
wadcache[wadFileLower].insert(lumpNameLower);
}
return true;
}
bool RESGen::CheckWadUse(const StringMap::const_iterator &wadfileIt)
{
WadCache::const_iterator wadIt = wadcache.find(wadfileIt->first);
if(wadIt == wadcache.end())
{
// Haven't read this wad yet
if(!CacheWad(wadfileIt->second))
{
// Failed to read wad
// Cache this failure with an empty set to prevent wad being marked
// as used
wadcache[wadfileIt->first] = TextureSet();
return false;
}
wadIt = wadcache.find(wadfileIt->first);
}
assert(wadIt != wadcache.end());
bool bWadUsed = false;
const TextureSet& textureSet = wadIt->second;
StringMap::iterator it = texturelist.begin();
// Look through all unfound textures and remove any that appear in this wad
while(it != texturelist.end())
{
TextureSet::const_iterator textureIt = textureSet.find(it->first);
if(textureIt != textureSet.end())
{
// found a texture, so wad is used
bWadUsed = true;
// update texture list
it = texturelist.erase(it);
}
else
{
++it;
}
}
return bWadUsed;
}
bool RESGen::CheckModelExtTexture(const std::string &model)
{
File mdl;
if(!OpenFirstValidPath(mdl, model, "rb"))
{
printf("Failed to open MDL file \"%s\".\n", model.c_str());
return false;
}
modelheader_s header;
if (fread(&header, sizeof(modelheader_s), 1, mdl) != 1)
{
printf("MDL file \"%s\" is corrupt.\n", model.c_str());
return false;
}
if (strncmp(header.id, "IDST", 4))
{
printf("\"%s\" is not a MDL file.\n", model.c_str());
return false;
}
if (header.version != 10)
{
printf("Incorrect MDL file version for \"%s\"\n", model.c_str());
return false;
}
if (header.textureindex == 0)
{
return true; // Uses seperate texture
}
return false;
}
void RESGen::ParseSentence(const char* const sentence)
{
// Not so performance critical here
std::string sentenceLower(sentence);
strToLower(sentenceLower);
if(
(sentenceLower.length() > 0)
// References string in sentences.txt
&& (sentenceLower[0] != '!')
// References string in Titles.txt
&& (sentenceLower[0] != '#')
)
{
// See if value references a wav file
if(
// Only one word
(sentenceLower.find(' ') == std::string::npos)
&& !CompareStrEnd(sentenceLower, ".wav")
)
{
// TODO: Check that specifying a wav actually
// works, and not that people are just misusing
// this entity
std::string resource = sentenceLower;
std::string soundPrefix("sound/");
// Add sound/ to start if it isn't already
if(resource.compare(0, soundPrefix.length(), soundPrefix))
{
resource.insert(0, soundPrefix);
}