-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.cc
404 lines (311 loc) · 9.05 KB
/
main.cc
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
//------------------------------------------------------------------------
// QPAKMAN Main program
//------------------------------------------------------------------------
//
// Copyright (c) 2008 Andrew J Apted
//
// 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.
//
//------------------------------------------------------------------------
#include "headers.h"
#include "main.h"
#include <time.h>
#include "archive.h"
#include "im_color.h"
#include "im_gen.h"
#include "im_mip.h"
#include "im_tex.h"
#include "pakfile.h"
#define VERSION "0.67"
std::string output_name;
std::vector<std::string> input_names;
std::string color_name;
typedef enum
{
ACT_None = 0,
ACT_Create,
ACT_List,
ACT_Extract,
ACT_MakeTex
}
prog_action_type_e;
static prog_action_type_e program_action = ACT_None;
game_kind_e game_type = GAME_Quake1;
bool opt_force = false;
bool opt_raw = false;
bool opt_picture = false;
bool opt_dither = false;
void FatalError(const char *message, ...)
{
fprintf(stdout, "FATAL ERROR: ");
va_list argptr;
va_start(argptr, message);
vfprintf(stdout, message, argptr);
va_end(argptr);
fprintf(stdout, "\n");
fflush(stdout);
exit(9);
}
void ShowTitle(void)
{
printf("\n");
printf("**** QPAKMAN v" VERSION " (C) 2008 Andrew Apted ****\n");
printf("\n");
}
void ShowUsage(void)
{
printf("USAGE: qpakman [OPTIONS...] FILES... -o OUTPUT.pak\n");
printf(" qpakman [OPTIONS...] IMAGES.. -o OUTPUT.wad\n");
printf(" qpakman [OPTIONS...] INPUT.pak/wad\n");
printf("\n");
printf("OPTIONS:\n");
printf(" -l -list list contents of PAK/WAD file\n");
printf(" -e -extract extract PAK/WAD contents into current dir\n");
printf(" -m -maketex make a texture WAD from BSP files\n");
printf("\n");
printf(" -c -colors XXX load color palette from given file\n");
printf(" -g -game XXX select game (quake1, quake2, hexen2)\n");
printf(" -f -force overwrite existing files when extracting\n");
printf(" -p -pic create PIC format images in the WAD\n");
printf(" -r -raw do not convert anything\n");
printf("\n");
printf("This program is free software, under the terms of the GNU General\n");
printf("Public License, and comes with ABSOLUTELY NO WARRANTY. See the\n");
printf("accompanying documentation for more details. USE AT OWN RISK.\n");
printf("\n");
}
/* returns number of arguments used, at least 1 */
int HandleOption(int argc, char **argv)
{
const char *opt = argv[0];
// GNU style options begin with two dashes
if (opt[0] == '-' && opt[1] == '-')
opt++;
if (StringCaseCmp(opt, "-o") == 0 || StringCaseCmp(opt, "-output") == 0)
{
if (argc <= 1 || argv[1][0] == '-')
FatalError("Missing output filename after %s\n", argv[0]);
output_name = std::string(argv[1]);
// automatically enable -pic option for gfx.wad
if (StringCaseCmp(FindBaseName(argv[1]), "gfx.wad") == 0)
opt_picture = true;
if (program_action == ACT_None)
program_action = ACT_Create;
return 2;
}
if (StringCaseCmp(opt, "-l") == 0 || StringCaseCmp(opt, "-list") == 0)
{
program_action = ACT_List;
return 1;
}
if (StringCaseCmp(opt, "-e") == 0 || StringCaseCmp(opt, "-extract") == 0)
{
program_action = ACT_Extract;
return 1;
}
if (StringCaseCmp(opt, "-m") == 0 || StringCaseCmp(opt, "-maketex") == 0)
{
program_action = ACT_MakeTex;
return 1;
}
if (StringCaseCmp(opt, "-c") == 0 || StringCaseCmp(opt, "-color") == 0 ||
StringCaseCmp(opt, "-colors") == 0)
{
if (argc <= 1 || argv[1][0] == '-')
FatalError("Missing filename after %s\n", argv[0]);
color_name = std::string(argv[1]);
return 2;
}
if (StringCaseCmp(opt, "-f") == 0 || StringCaseCmp(opt, "-force") == 0 ||
StringCaseCmp(opt, "-overwrite") == 0)
{
opt_force = true;
return 1;
}
if (StringCaseCmp(opt, "-g") == 0 || StringCaseCmp(opt, "-game") == 0)
{
if (argc <= 1 || argv[1][0] == '-')
FatalError("Missing keyword after %s\n", argv[0]);
if (StringCaseCmp(argv[1], "q1") == 0 ||
StringCaseCmp(argv[1], "quake") == 0 ||
StringCaseCmp(argv[1], "quake1") == 0)
{
game_type = GAME_Quake1;
}
else if (StringCaseCmp(argv[1], "q2") == 0 ||
StringCaseCmp(argv[1], "quake2") == 0)
{
game_type = GAME_Quake2;
}
else if (StringCaseCmp(argv[1], "h2") == 0 ||
StringCaseCmp(argv[1], "hexen2") == 0)
{
game_type = GAME_Hexen2;
}
else if (StringCaseCmp(argv[1], "hak") == 0 ||
StringCaseCmp(argv[1], "haktoria") == 0)
{
game_type = GAME_Haktoria;
}
else
FatalError("Unknown game type: %s\n", argv[1]);
return 2;
}
if (StringCaseCmp(opt, "-p") == 0 || StringCaseCmp(opt, "-pic") == 0)
{
opt_picture = true;
return 1;
}
if (StringCaseCmp(opt, "-r") == 0 || StringCaseCmp(opt, "-raw") == 0)
{
opt_raw = true;
return 1;
}
FatalError("Unknown option: %s\n", argv[0]);
return 0; // NOT REACHED
}
void AddInputFile(const char *filename)
{
input_names.push_back(std::string(filename));
}
void Main_Create(void)
{
const char *filename = output_name.c_str();
if (CheckExtension(filename, "wad"))
MIP_CreateWAD(filename);
else if (CheckExtension(filename, "pak"))
ARC_CreatePAK(filename);
else if (GEN_TryCreateSpecial(filename))
{ /* OK */ }
else
FatalError("Unknown output file format: %s\n", output_name.c_str());
}
void Main_List(void)
{
if (input_names.size() == 0)
FatalError("Missing input file to list\n");
if (input_names.size() > 1)
FatalError("Can only list one input file\n");
const char *filename = input_names[0].c_str();
if (CheckExtension(filename, "pak"))
{
if (! PAK_OpenRead(filename))
FatalError("Could not open PAK file: %s\n", filename);
PAK_ListEntries();
PAK_CloseRead();
}
else if (CheckExtension(filename, "wad"))
{
if (! WAD2_OpenRead(filename))
FatalError("Could not open WAD2 file: %s\n", filename);
WAD2_ListEntries();
WAD2_CloseRead();
}
else
FatalError("Unknown input file format: %s\n", filename);
}
void Main_Extract(void)
{
if (input_names.size() == 0)
FatalError("Missing input file to extract\n");
if (input_names.size() > 1)
FatalError("Can only extract one input file\n");
const char *filename = input_names[0].c_str();
if (CheckExtension(filename, "wad"))
MIP_ExtractWAD(filename);
else if (CheckExtension(filename, "pak"))
ARC_ExtractPAK(filename);
else
FatalError("Unknown input file format: %s\n", filename);
}
void Main_MakeTex(void)
{
if (input_names.size() == 0)
FatalError("Missing input files for maketex\n");
if (! WAD2_OpenWrite(output_name.c_str()))
FatalError("Could not create texture file: %s", output_name.c_str());
printf("\n");
printf("--------------------------------------------------\n");
TEX_ExtractStart();
for (unsigned int i = 0; i < input_names.size(); i++)
{
const char *filename = input_names[i].c_str();
if (CheckExtension(filename, "pak"))
TEX_ExtractFromPAK(filename);
else if (CheckExtension(filename, "bsp"))
TEX_ExtractFromBSP(filename);
else if (CheckExtension(filename, "wad"))
TEX_ExtractFromWAD(filename);
else
{
printf("FAILURE: unknown file type for maketex: %s\n", filename);
break;
}
}
TEX_ExtractDone();
printf("--------------------------------------------------\n");
WAD2_CloseWrite();
}
int main(int argc, char **argv)
{
// skip program name itself
argv++, argc--;
if (argc <= 0 ||
StringCaseCmp(argv[0], "/?") == 0 ||
StringCaseCmp(argv[0], "-h") == 0 ||
StringCaseCmp(argv[0], "-help") == 0 ||
StringCaseCmp(argv[0], "--help") == 0)
{
ShowTitle();
ShowUsage();
exit(1);
}
ShowTitle();
// handle command-line arguments
while (argc > 0)
{
if (argv[0][0] == '-')
{
int num = HandleOption(argc, argv);
argv += num;
argc -= num;
continue;
}
AddInputFile(argv[0]);
argv++;
argc--;
}
if (color_name.empty())
COL_SetPalette(game_type);
else
COL_LoadPaletteFromFile(color_name.c_str());
switch (program_action)
{
case ACT_Create:
Main_Create();
break;
case ACT_List:
Main_List();
break;
case ACT_Extract:
Main_Extract();
break;
case ACT_MakeTex:
Main_MakeTex();
break;
default:
FatalError("Nothing to do (missing output file?)\n");
break; // NOT REACHED
}
return 0;
}
//--- editor settings ---
// vi:ts=2:sw=2:expandtab