-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathresgen.cpp
733 lines (645 loc) · 18.8 KB
/
resgen.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
/*
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
*/
/* Command line parameters:
-h help
-c credits
-w warranty
-v non-verbal mode
-s display status line
-i display found maps while generating map list
-j display found resources while generating resource list
-g display contents of generated .res files and missing resources
-d [folder] entire folder
-r [folder] entire folder + subfolders
-f [map] parse map
[map] parse map (same as -f)
-x [map] exclude map from res file generation (use with -d or -r)
-o overwrite existing res files
-a [rfafile] add contents of [rfafile] to res file(s)
-l do not convert res entries to lowercase
-m matches file case with resources found by -e
-e [modpath] check for resource existance
-p do not check for resource existance in pakfiles
-u parses wads for used textures and mdls for external textures (use with -e)
-b [rfafile] Excludes files from [rfafile]
-k *WIN32* do not wait for keypress before exit
-t *LINUX* ignore symbolic links when searching directories
// v1 -> v2 command line changes:
-w -> [CHANGE] Win32 exit mode to warranty
-i -> [OS] Not Linux specific anymore
-u -> [REMOVED] Function is prone to error
-k -> [NEW] Replacement for old -w
-e -> [NEW] Requested function.
-p -> [NEW] To go with -e
-x -> [NEW] Good for advanced users
// v2B3 -> v2RC1 command line changes:
-b -> [NEW] Excludes files from [rfafile]
-i -> [CHANGE] Now displays maps while generating map list (folders will never be displayed anymore)
-g -> [NEW] Display content of generated .res files
-m -> [NEW] matches file case with resources found by -e
-u -> [NEW] parses wads for used textures and mdls for external textures (use with -e)
-n do not ignore unused wads (use with -u)
// Param usage
abcdefghijklmnopqrstuvwxyz
xxxxxxxxxxxxx xx xxxxxxx
// Free: q y z
*/
// if you define NO_MULTIARG_FILES RESGen will reject any multiarg entries for:
// d, r, f, x, a, b and e
#define NO_MULTIARG_FILES
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef _WIN32
#include <conio.h>
#include <windows.h>
#endif
#include "listbuilder.h"
#include "resgenclass.h"
#include "resgen.h"
#include "resourcelistbuilder.h"
#include "util.h"
#ifdef _WIN32
void getexitkey(bool verbal, bool keypress)
{
if (verbal && keypress)
{
printf("Press any key to exit");
_kbhit();
}
}
#endif
// help & copyrights
void showcopyright()
{
printf("RESGen version %s, Copyright (C) 2000-2005 Jeroen Bogers and Zero3Cool\n", VERSION);
printf("RESGen comes with ABSOLUTELY NO WARRANTY; for details\n");
printf("use the command line switch '-w'. This is free software,\n");
printf("and you are welcome to redistribute it under certain\n");
printf("conditions; see the 'gpl.txt' file for details.\n\n");
}
void showhelp()
{
// Win32 console size
// 0 1 2 3 4 5 6 7 8
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
printf("Command line parameters:\n");
printf(" -h Displays this help screen\n");
printf(" -c Displays credits\n");
printf(" -w Show the extended copyright screen\n");
printf(" -v Puts RESGen in silent mode (little console output)\n");
printf(" -s Display the status line\n");
printf(" -i Display found maps while generating map list\n");
printf(" -j Display found resources while generating resource list\n");
printf(" -g Display contents of generated .res files and missing resources\n");
printf(" -d [folder] Process entire folder\n");
printf(" -r [folder] Same as -d, but processes subfolders too\n");
printf(" -f [map] Process this map\n");
printf(" [map] Same as -f\n");
printf(" -x [map] Exclude map from res file generation (use with -d or -r)\n");
printf(" -o Overwrite existing res files\n");
printf(" -a [rfafile] Adds the rfa file to generated res files\n");
printf(" -l Convert res entries to lowercase\n");
printf(" -m Match the file case with the resources on disk (use with -e)\n");
printf(" -e [modpath] Check for resource existance\n");
printf(" -p Do not check for resource existance in pakfiles\n");
printf(" -u Parse WAD and MDL files for external textures (use with -e)\n");
printf(" -n Do not ignore unused WAD files (use with -u)\n");
printf(" -b [rfafile] Excludes resources from [rfafile] from generated res files.\n");
#ifdef _WIN32
printf(" -k RESGen will not wait for a keypress to exit in verbal mode\n");
#else
printf(" -t Ignore symbolic links when searching folders\n");
#endif
printf("\nExample:\n");
printf(" resgen -f boot_camp -d . -r ../mappack -e hlds_l/cstrike\n\n");
printf("Read RESGen.txt for more information!\n");
}
void showwarranty()
{
printf("RESGen. A tool to create .res files for Half-Life.\n");
printf("Copyright (C) 2000-2005 Jeroen Bogers and Zero3Cool\n\n");
printf("RESGen is free software; you can redistribute it and/or modify\n");
printf("it under the terms of the GNU General Public License as published by\n");
printf("the Free Software Foundation; either version 2 of the License, or\n");
printf("(at your option) any later version.\n\n");
printf("RESGen is distributed in the hope that it will be useful,\n");
printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
printf("GNU General Public License for more details.\n\n");
printf("You should have received a copy of the GNU General Public License\n");
printf("along with RESGen; if not, write to the Free Software\n");
printf("Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
}
void showcredits()
{
printf("This program was made by Jeroen \"ShadowLord\" Bogers,\n");
printf("with serveral improvements and additions by Zero3Cool.\n\n");
printf("Special thanks to:\n");
printf(" \"HoundDawg\" and UnitedAdmins for helping RESGen grow.\n");
printf(" \"Zero3Cool\" for improving RESGen and keeping it alive.\n");
printf(" TheZProject.org for hosting RESGen.\n");
printf(" \"luvless\" for having the great idea to create this app.\n");
printf(" Valve for creating Half-Life!\n");
printf(" Everyone who uses RESGen.\n\n");
printf("http://resgen.hltools.com\n");
}
int main(int argc, char* argv[])
{
// init config struct
config_s config;
config.help = false;
config.credits = false;
config.warranty = false;
config.verbal = true;
config.statusline = false;
config.searchdisp = false;
config.resourcedisp = false;
config.contentdisp = false;
config.overwrite = false;
config.tolower = false;
config.matchcase = false;
config.checkpak = true;
config.parseresource = false;
config.preservewads = false;
#ifdef _WIN32
config.keypress = true;
#else
config.symlink = true;
#endif
// parse the command line
for (int i = 1; i < argc; i++) // arg 0 is the command line.
{
char *argstr = argv[i];
if (argstr[0] == '-')
{
// cmdline switch(es)
size_t arglen = strlen(argstr);
for (size_t j = 1; j < arglen; j++)
{
switch (argstr[j])
{
// -h
case 'h':
config.help = true;
break;
// -c
case 'c':
config.credits = true;
break;
// -w
case 'w':
config.warranty = true;
break;
// -v
case 'v':
config.verbal = false;
break;
// -s
case 's':
config.statusline = true;
break;
// -i
case 'i':
config.searchdisp = true;
break;
// -j
case 'j':
config.resourcedisp = true;
break;
// -g
case 'g':
config.contentdisp = true;
break;
// -d
case 'd':
{
#ifdef NO_MULTIARG_FILES
if (arglen != 2)
{
printf ("Ignoring 'd' argument: Cannot be used in multiple argument list\n");
break;
}
#endif
if (i == argc - 1)
{
printf ("Ignoring 'd' argument: No folder specified\n");
break;
}
if (argv[i+1][0] == '-')
{
printf ("Ignoring 'd' argument: No folder specified\n");
break;
}
file_s file;
file.folder = true;
file.recursive = false;
i++; // increase i.. we used that arg.
file.name = argv[i];
config.files.push_back(file); // add to linked list
break;
}
// -r
case 'r':
{
#ifdef NO_MULTIARG_FILES
if (arglen != 2)
{
printf ("Ignoring 'r' argument: Cannot be used in multiple argument list\n");
break;
}
#endif
if (i == argc - 1)
{
printf ("Ignoring 'r' argument: No folder specified\n");
break;
}
if (argv[i+1][0] == '-')
{
printf ("Ignoring 'r' argument: No folder specified\n");
break;
}
file_s file;
file.folder = true;
file.recursive = true;
i++; // increase i.. we used that arg.
file.name = argv[i];
config.files.push_back(file); // add to linked list
break;
}
// -f
case 'f':
{
#ifdef NO_MULTIARG_FILES
if (arglen != 2)
{
printf ("Ignoring 'f' argument: Cannot be used in multiple argument list\n");
break;
}
#endif
if (i == argc - 1)
{
printf ("Ignoring 'f' argument: No map specified\n");
break;
}
if (argv[i+1][0] == '-')
{
printf ("Ignoring 'f' argument: No map specified\n");
break;
}
file_s file;
file.folder = false;
file.recursive = false;
i++; // increase i.. we used that arg.
file.name = argv[i];
config.files.push_back(file); // add to linked list
break;
}
// -x
case 'x':
{
#ifdef NO_MULTIARG_FILES
if (arglen != 2)
{
printf ("Ignoring 'x' argument: Cannot be used in multiple argument list\n");
break;
}
#endif
if (i == argc - 1)
{
printf ("Ignoring 'x' argument: No map specified\n");
break;
}
if (argv[i+1][0] == '-')
{
printf ("Ignoring 'x' argument: No map specified\n");
break;
}
file_s file;
file.folder = false;
file.recursive = false;
i++; // increase i.. we used that arg.
file.name = argv[i];
config.excludes.push_back(file); // add to linked list
break;
}
// -o
case 'o':
config.overwrite = true;
break;
// -a
case 'a':
#ifdef NO_MULTIARG_FILES
if (arglen != 2)
{
printf ("Ignoring 'a' argument: Cannot be used in multiple argument list\n");
break;
}
#endif
if (i == argc - 1)
{
printf ("Ignoring 'a' argument: No rfa file specified\n");
break;
}
if (argv[i+1][0] == '-')
{
printf ("Ignoring 'a' argument: No rfa file specified\n");
break;
}
i++; // increase i.. we used that arg.
config.rfafile = argv[i];
break;
// -l
case 'l':
config.tolower = true;
break;
// -m
case 'm':
config.matchcase = true;
break;
// -e
case 'e':
#ifdef NO_MULTIARG_FILES
if (arglen != 2)
{
printf ("Ignoring 'e' argument: Cannot be used in multiple argument list\n");
break;
}
#endif
if (i == argc - 1)
{
printf ("Ignoring 'e' argument: No folder specified\n");
break;
}
if (argv[i+1][0] == '-')
{
printf ("Ignoring 'e' argument: No folder specified\n");
break;
}
i++; // increase i.. we used that arg.
config.resource_path = argv[i];
break;
// -p
case 'p':
config.checkpak = false;
break;
// -u
case 'u':
config.parseresource = true;
break;
// -n
case 'n':
config.preservewads = true;
break;
// -b
case 'b':
#ifdef NO_MULTIARG_FILES
if (arglen != 2)
{
printf ("Ignoring 'b' argument: Cannot be used in multiple argument list\n");
break;
}
#endif
if (i == argc - 1)
{
printf ("Ignoring 'b' argument: No rfa file specified\n");
break;
}
if (argv[i+1][0] == '-')
{
printf ("Ignoring 'b' argument: No rfa file specified\n");
break;
}
i++; // increase i.. we used that arg.
config.excludelists.push_back(argv[i]);
break;
#ifdef _WIN32
// -k
case 'k':
config.keypress = false;
break;
#else
// -t
case 't':
config.symlink = false;
break;
#endif // _WIN32
default:
printf("Ignoring '%c' argument: Argument not known\n", argstr[j]);
}
}
}
else
{
// not a switch. Assume it's a map.
file_s file;
file.folder = false;
file.recursive = false;
file.name = argv[i];
config.files.push_back(file); // add to linked list
}
}
// Make sure statusline and folderlist doesn't display in non verbal mode
if (!config.verbal)
{
config.statusline = false;
config.searchdisp = false;
config.resourcedisp = false;
config.contentdisp = false;
}
// check for stuff to display
if (config.warranty)
{
showwarranty();
#ifdef _WIN32
getexitkey(config.verbal, config.keypress);
#endif
exit(0);
}
if (config.verbal)
{
showcopyright();
}
if (config.help)
{
showhelp();
#ifdef _WIN32
getexitkey(config.verbal, config.keypress);
#endif
exit(0);
}
if (config.credits)
{
showcredits();
#ifdef _WIN32
getexitkey(config.verbal, config.keypress);
#endif
exit(0);
}
// check if we have anything to do
if (config.files.empty())
{
showhelp();
printf("\nERROR: You need to specify at least 1 folder or map to process\n");
#ifdef _WIN32
getexitkey(config.verbal, config.keypress);
#endif
exit(0);
}
if (!config.verbal)
{
printf("Generating RES files. Please stand by...\n");
}
// Build filelist
std::vector<std::string> FileList; // bsp files
std::vector<std::string> ErrorList; // failed bsp files
std::vector<std::string> MissingList; // bsp files with missing reources
ListBuilder listbuild(&FileList, config.excludes, config.verbal, config.searchdisp);
#ifndef _WIN32
listbuild.SetSymLink(config.symlink);
#endif
listbuild.BuildList(config.files);
// clean up config.files, we don't need it anymore
config.files.clear();
// Clean up config.exludes, we don't need it anymore
config.excludes.clear();
if (config.verbal) { printf("\n"); } // Make output look a bit cleaner
// list made. Now parse the res files.
RESGen resgen;
resgen.SetParams(
config.verbal, config.statusline, config.overwrite, config.tolower,
config.matchcase, config.parseresource, config.preservewads, config.contentdisp);
if(!resgen.LoadRfaFile(config.rfafile))
{
// Could not load RFA file, exit
#ifdef _WIN32
getexitkey(config.verbal, config.keypress);
#endif
return 0;
}
// Load all resource exclude lists
if (!config.excludelists.empty())
{
for(
std::vector<std::string>::iterator it(config.excludelists.begin());
it != config.excludelists.end();
++it
)
{
if (!resgen.LoadExludeFile(*it))
{
#ifdef _WIN32
getexitkey(config.verbal,config.keypress);
#endif
return 0;
}
else
{
if (config.verbal)
{
printf("Loaded resource exclude list %s\n", it->c_str());
}
}
}
config.excludelists.clear();
if (config.verbal) { printf("\n"); }
}
std::vector<std::string> resourcePaths;
if (!config.resource_path.empty())
{
// Make sure path ends with a separator
EndWithPathSep(config.resource_path);
resourcePaths.push_back(config.resource_path);
resourcePaths.push_back(BuildValvePath(config.resource_path));
}
ResourceListBuilder resourceListBuilder(config);
resourceListBuilder.BuildResourceList(resourcePaths, config.checkpak, config.resourcedisp);
int i = 1;
size_t filecount = FileList.size();
while (FileList.size() > 0)
{
if (config.contentdisp) { printf("\n"); } // Make output look a bit cleaner
std::string map(FileList[0]);
int retval = resgen.MakeRES(map, i, filecount, resourceListBuilder.resources, resourcePaths);
if(retval)
{
if (retval == 2)
{
// res file was made properly, but some resources were missing
MissingList.push_back(FileList[0]);
}
else
{
//
// an error occured. List them.
ErrorList.push_back(FileList[0]);
}
}
FileList.erase(FileList.begin());
if (config.verbal) { printf("\n"); } // Make output look a bit cleaner
i++; // keep i up to date!
}
// clean up errors
size_t errorcount = ErrorList.size();
size_t missingcount = MissingList.size();
if (errorcount)
{
if (config.verbal) { printf("Failed to create res file(s) for:\n"); }
while (!ErrorList.empty())
{
std::string strtmp = ErrorList[0];
if (config.verbal) { printf(" %s\n", strtmp.c_str()); } // only print of verbal
ErrorList.erase(ErrorList.begin());
}
if (config.verbal) { printf("\n"); }
}
if (missingcount)
{
// res file might not be complete
if (config.verbal) {
printf("Because one or more required files were not found in your installation,\n");
printf("the following map(s) might be missing resources:\n");
}
while (!MissingList.empty())
{
std::string strtmp = MissingList[0];
if (config.verbal) { printf(" %s\n", strtmp.c_str()); } // only print of verbal
MissingList.erase(MissingList.begin());
}
if (config.verbal) { printf("\n"); }
}
printf("Done creating res file(s)! " SIZE_T_SPECIFIER " map(s) were processed", filecount - errorcount);
if (errorcount)
{
printf(", skipped " SIZE_T_SPECIFIER " due to errors.\n", errorcount);
}
else
{
printf(".\n");
}
if (missingcount)
{
printf(SIZE_T_SPECIFIER " map(s) might be missing resources.\n", missingcount);
}
// res files made.. exit
// note we don't bother to clean up memory, the OS will do this for us.
#ifdef _WIN32
getexitkey(config.verbal, config.keypress);
#endif
return 0;
}