-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
372 lines (314 loc) · 8.59 KB
/
Copy pathmain.cpp
File metadata and controls
372 lines (314 loc) · 8.59 KB
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
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2013 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:
// Main module
//
//-----------------------------------------------------------------------------
#include "z_zone.h"
#include "d_scripts.h"
#include "d_wads.h"
#include "i_system.h"
#include "m_argv.h"
#include "m_qstr.h"
#include "main.h"
#include "s_sfxgen.h"
#include "v_psx.h"
#include "w_formats.h"
#include "zip_write.h"
// Defines
// Default output file names for supported target formats
#define DEF_OUTPUTNAME_WAD "psxdoom.wad"
#define DEF_OUTPUTNAME_ZIP "psxdoom.pke"
#define DEF_RESOURCEDIR "./res"
// Globals
// Output format
WResourceFmt gOutputFormat = W_FORMAT_ZIP;
// sound effects format
int s_sfxfmt = SFX_FMT_WAV;
// Output targets
ziparchive_t gZipArchive; // zip file (ie. pke archive)
// Statics
static qstring baseinputdir; // root input directory; ex: J:\PSXDOOM
static qstring outputname; // name of output file
static qstring resourcedir; // directory with resources to inject as lumps
//
// D_ExtractMovie
//
// Mini-program to extract the MOVIE.STR file
//
static void D_ExtractMovie()
{
qstring infile, outfile;
int p, start = 0, length = 0;
// input file name - required
if((p = M_CheckParm("-movie")) && p < myargc - 1)
infile = myargv[p+1];
else
I_Error("D_ExtractMovie: need a raw CD image file for input\n");
// output file name - optional
if((p = M_CheckParm("-output")) && p < myargc - 1)
outfile = myargv[p+1];
else
outfile = "movie.str";
// start and length sector overrides - optional
if((p = M_CheckParm("-start")) && p < myargc - 1)
start = atoi(myargv[p+1]);
if((p = M_CheckParm("-length")) && p < myargc - 1)
length = atoi(myargv[p+1]);
V_ExtractMovie(infile, outfile, start, length);
exit(0);
}
//
// D_setResourceDir
//
// Finds the directory to use for external resources that will be merged into
// the output archive.
//
static void D_setResourceDir()
{
int p;
if((p = M_CheckParm("-res")) && p < myargc - 1)
resourcedir = myargv[p + 1];
else
resourcedir = DEF_RESOURCEDIR;
}
//
// D_MakeResourceFilePath
//
// Given a file name, appends the resource path to the beginning.
//
void D_MakeResourceFilePath(qstring &filename)
{
qstring tmp = resourcedir;
tmp.pathConcatenate(filename.constPtr());
filename = tmp;
}
static const char *usagestr =
"\n"
"psxwadgen options:\n"
"\n"
"-input <dirpath> [-output <filename>]\n"
" Generates a psxdoom.wad or psxdoom.pke output file, given the path to the\n"
" PSXDOOM directory on a disc or mounted disc image of PlayStation DOOM.\n"
"\n"
"-sfxfmt <format>\n"
" Set the sound effect output format:\n"
" * dmx = DMX format 0x03\n"
" * wav = Microsoft WAVE (default)\n"
"\n"
"-movie <imgfile> [-output <filename>] [-start <secnum>] [-length <seclen>]\n"
" Extracts the MOVIE.STR file from a raw CD image. Default output file name\n"
" is movie.str; default sector start position is 822 and length is 1377,\n"
" suitable for use with a North American release image.\n"
"\n"
"-vanillamaps [<directory>]\n"
" Write out vanilla-compatible WAD files containing each map.\n"
"\n"
"-res <directory>\n"
" Sets the external resource directory.\n";
//
// D_PrintUsage
//
// Output documentation on all supported command-line parameters and exit.
//
static void D_PrintUsage()
{
puts(usagestr);
exit(0);
}
//
// D_CheckForParameters
//
// Check for command line parameters
//
static void D_CheckForParameters()
{
int p;
static const char *helpParams[] = { "-help", "-?", nullptr };
// check for help
if(myargc < 2 || M_CheckMultiParm(helpParams, 0))
D_PrintUsage();
// check for movie file extraction
if(M_CheckParm("-movie"))
{
printf("D_ExtractMovie: extracting MOVIE.STR from CD image\n");
D_ExtractMovie();
}
if((p = M_CheckParm("-input")) && p < myargc - 1)
baseinputdir = myargv[p+1];
else
I_Error("Need a directory name for input!\n");
// TODO: look for output format parameter
// set default output filename based on selected output format
if(gOutputFormat == W_FORMAT_ZIP)
outputname = DEF_OUTPUTNAME_ZIP;
// allow command-line override
if((p = M_CheckParm("-output")) && p < myargc - 1)
outputname = myargv[p+1];
// sound effects format
if((p = M_CheckParm("-sfxfmt")) && p < myargc - 1)
{
if(!strcasecmp(myargv[p + 1], "dmx"))
s_sfxfmt = SFX_FMT_DMX;
else if(!strcasecmp(myargv[p + 1], "wav"))
s_sfxfmt = SFX_FMT_WAV;
}
// set resource directory
D_setResourceDir();
}
//
// Startup Banner
//
static void D_PrintStartupBanner()
{
puts("psxwadgen\n"
"Copyright 2014 James Haley et al.\n"
"This program is free software distributed under the terms of the GNU General\n"
"Public License. See the file \"COPYING\" for full details.\n");
}
//
// Initialization
//
static void D_Init()
{
D_PrintStartupBanner();
// Zone init
printf("Z_Init: Init zone allocation daemon.\n");
Z_Init();
// Check for command line parameters
printf("D_CheckForParameters: Checking command line parameters.\n");
D_CheckForParameters();
// Load PSX wad files from input directory
printf("D_LoadInputFiles: Loading PSX wad files...\n");
D_LoadInputFiles(baseinputdir);
// Load PLAYPAL
printf("V_LoadPLAYPAL: Decompressing palettes.\n");
V_LoadPLAYPAL(psxIWAD);
// Generate COLORMAP from palette 0
printf("V_GenerateCOLORMAP: Generating colormap data.\n");
V_GenerateCOLORMAP();
// Load LIGHTS
printf("V_LoadLIGHTS: Converting LIGHTS to palette.\n");
V_LoadLIGHTS(psxIWAD);
}
//
// Output Management
//
//
// D_OpenOutputFile
//
// Create the output structure that will be passed to other modules to
// accumulate data entries (ie., lumps).
//
static void D_OpenOutputFile()
{
switch(gOutputFormat)
{
case W_FORMAT_ZIP:
Zip_Create(&gZipArchive, outputname.constPtr());
break;
default:
I_Error("D_OpenOutputFile: unsupported output format value %d\n",
gOutputFormat);
break;
}
printf("D_OpenOutputFile: output directed to %s\n", outputname.constPtr());
}
//
// D_TransformToZip
//
// Perform all steps necessary to create the psxdoom.pke archive.
//
static void D_TransformToZip()
{
// sounds
S_ProcessSoundsForZip(baseinputdir, &gZipArchive);
// sprites
V_ConvertSpritesToZip(psxIWAD, &gZipArchive);
// textures
V_ConvertTexturesToZip(psxIWAD, &gZipArchive);
// flats
V_ConvertFlatsToZip(psxIWAD, &gZipArchive);
// graphics
V_ConvertGraphicsToZip(psxIWAD, &gZipArchive);
// palettes and color lumps
V_ConvertPLAYPALToZip(&gZipArchive);
V_ConvertCOLORMAPToZip(&gZipArchive);
V_ConvertLIGHTSToZip(&gZipArchive);
// maps
D_AddMapsToZip(&gZipArchive, baseinputdir);
// scripts
D_ProcessScriptsForZip(&gZipArchive);
}
//
// D_TransformInput
//
// Call the proper routine to transform the input to the output format.
//
static void D_TransformInput()
{
switch(gOutputFormat)
{
case W_FORMAT_ZIP:
D_TransformToZip();
break;
default:
break;
}
}
//
// D_CloseOutputFile
//
// Write and close the output file once everything has been added to it.
//
static void D_CloseOutputFile()
{
switch(gOutputFormat)
{
case W_FORMAT_ZIP:
printf("Writing output... ");
Zip_Write(&gZipArchive);
printf("\nSuccessfully created output file '%s'\n", gZipArchive.filename);
break;
default:
break;
}
}
//
// Main Program
//
int main(int argc, char **argv)
{
// setup m_argv
myargc = argc;
myargv = argv;
// perform initialization
D_Init();
// open output
D_OpenOutputFile();
// transform
D_TransformInput();
// close output
D_CloseOutputFile();
return 0;
}
// EOF