1
/* ScummVM - Graphic Adventure Engine
3
* ScummVM is the legal property of its developers, whose names
4
* are too numerous to list here. Please refer to the COPYRIGHT
5
* file distributed with this source distribution.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
* $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/tags/release-1-2-1/tools/sci/sciunpack.cpp $
22
* $Id: sciunpack.cpp 38423 2009-02-17 15:59:52Z sev $
28
#endif /* HAVE_CONFIG_H */
30
#include "sciunpack.h"
32
#include <sciresource.h>
36
/* #define DRAW_GRAPHICS */
42
# define extern __declspec(dllimport) extern
49
# include <win32/getopt.h>
51
#endif /* HAVE_GETOPT_H */
55
# include "graphics_png.h"
56
# endif /* HAVE_LIBPNG */
57
#endif /* DRAW_GRAPHICS */
59
#if defined (_MSC_VER) || defined (__BEOS__) || defined(__amigaos4__)
60
/* [DJ] fchmod is not in Visual C++ RTL - and probably not needed,anyway */
61
/* [RS] (see comment above, but read MS-DOS instead of Visual C++ RTL) */
62
# define fchmod(file,mode)
63
# define CREAT_OPTIONS O_BINARY
67
# define CREAT_OPTIONS 0x640
74
#define ACT_SCRIPTDUMP 3
75
#define ACT_VOCABDUMP 4
77
#define ACT_DEFAULT ACT_UNPACK
79
static int conversion = 0;
81
static int verbose = 0;
82
static int with_header = 1;
83
static int color_mode = 0;
84
static int action = ACT_DEFAULT;
85
static guint8 midimask = 0x01; /* MT-32 */
87
resource_mgr_t *resmgr;
90
#define fchmod(arg1, arg2)
94
print_resource_filename(FILE* file, int type, int number) {
95
if (resmgr->sci_version < SCI_VERSION_1)
96
fprintf(file, "%s.%03d", sci_resource_types[type], number);
98
fprintf(file, "%d.%s", number, sci_resource_type_suffixes[type]);
102
sprint_resource_filename(char* buf, int type, int number) {
103
if (resmgr->sci_version < SCI_VERSION_1)
104
sprintf(buf, "%s.%03d", sci_resource_types[type], number);
106
sprintf(buf, "%d.%s", number, sci_resource_type_suffixes[type]);
109
#ifdef HAVE_GETOPT_LONG
110
static struct option options[] = {
111
{"conversion", no_argument, &conversion, 1},
112
{"version", no_argument, 0, 256},
113
{"verbose", no_argument, &verbose, 1},
114
{"help", no_argument, 0, 'h'},
115
{"output-file", required_argument, 0, 'o'},
116
{"unpack", no_argument, &action, ACT_UNPACK},
117
{"list", no_argument, &action, ACT_LIST},
118
{"words", no_argument, &action, ACT_WORDS},
119
{"vocab", no_argument, &action, ACT_VOCABDUMP},
120
{"objects", no_argument, &action, ACT_SCRIPTDUMP},
121
{"with-header", no_argument, &with_header, 1},
122
{"without-header", no_argument, &with_header, 0},
123
{"sort-alpha", no_argument, &vocab_sort, SORT_METHOD_ALPHA},
124
{"sort-group", no_argument, &vocab_sort, SORT_METHOD_GROUP},
126
{"palette-dither", no_argument, &color_mode, SCI_COLOR_DITHER},
127
{"palette-interpolate", no_argument, &color_mode, SCI_COLOR_INTERPOLATE},
128
{"palette-dither256", no_argument, &color_mode, SCI_COLOR_DITHER256},
129
#endif /* DRAW_GRAPHICS */
130
{"gamedir", required_argument, 0, 'd'},
131
{"midimask", required_argument, 0, 'M'},
135
#endif /* HAVE_GETOPT_LONG */
138
void unpack_resource(int stype, int snr, char *outfilename);
141
int main(int argc, char** argv) {
146
char *resourcenumber_string = 0;
147
char *outfilename = 0;
150
char *gamedir = sci_getcwd();
151
int res_version = SCI_VERSION_AUTODETECT;
153
#ifdef HAVE_GETOPT_LONG
154
while ((c = getopt_long(argc, argv, "WOVUvhLcr:o:d:M:", options, &optindex)) > -1) {
155
#else /* !HAVE_GETOPT_LONG */
156
while ((c = getopt(argc, argv, "WOVUvhLcr:o:d:M:")) > -1) {
157
#endif /* !HAVE_GETOPT_LONG */
161
printf("sciunpack ("PACKAGE") "VERSION"\n");
162
printf("This program is copyright (C) 1999, 2000, 2001 Christoph Reichenbach,\n"
163
" Lars Skovlund, Magnus Reftel\n"
164
"It comes WITHOUT WARRANTY of any kind.\n"
165
"This is free software, released under the GNU General Public License.\n");
169
char *gcc_3_0_can_kiss_my_ass =
170
"Usage: sciunpack [options] [-U] <resource.number>\n"
171
" sciunpack [options] [-U] <resource> <number>\n"
172
"Unpacks resource data\n"
173
"If * is specified instead of <number>, \n"
174
"all resources of given type will be unpacked.\n\n"
175
" sciunpack [options] -W\n"
176
"Lists vocabulary words\n\n"
177
" sciunpack [options] -O\n"
178
"Dumps the complete object hierarchy\n\n"
179
" sciunpack [options] -V\n"
180
"Prints selector names, opcodes, kernel names, and classes\n\n"
181
"\nAvalable operations:\n"
182
" --unpack -U Decompress resource\n"
183
" --list -L List all resources\n"
184
" --words -W List all vocabulary words\n"
185
" --objects -O Print all objects\n"
186
" --vocab -V Lists the complete vocabulary\n"
187
"\nAvailable options:\n"
189
" --version Prints the version number\n"
190
" --verbose -v Enables additional output\n"
191
" --help -h Displays this help message\n"
192
" --midimask -M What 'play mask' to use. Defaults to MT-32 (0x01)\n"
195
" --sort-alpha sort in alphabetical order\n"
196
" --sort-group sort in group order\n"
198
" --convert -c Converts selected resources\n"
199
" --output-file -o Selects output file\n"
200
" --gamedir -d Read game resources from dir\n"
201
" --with-header Forces the SCI header to be written (default)\n"
202
" --without-header Prevents the two SCI header bytes from being written\n"
204
" --palette-dither Forces colors in 16 color games to be dithered\n"
205
" --palette-interpolate Does color interpolation when drawing picture resources\n"
206
" --palette-dither256 Does dithering in 256 colors\n"
207
#endif /* DRAW_GRAPHICS */
208
"\nAs a default, 'resource.number' is the output filename.\n"
209
"If conversion is enabled, the following resources will be treated specially:\n"
210
" sound resources: Will be converted to MIDI, stored in <number>.midi\n"
211
" script resources: Will be dissected and stored in <number>.script\n"
213
" picture resources: Will be converted to PNG, stored in <number>.png\n"
215
#endif /* DRAW_GRAPHICS */
218
printf(gcc_3_0_can_kiss_my_ass);
235
action = ACT_VOCABDUMP;
239
action = ACT_SCRIPTDUMP;
243
outfilename = optarg;
247
if (gamedir) sci_free(gamedir);
248
gamedir = sci_strdup(optarg);
252
res_version = atoi(optarg);
260
midimask = (guint8) strtol(optarg, NULL, 0);
263
case 0: /* getopt_long already did this for us */
265
/* getopt_long already printed an error message. */
273
if (action == ACT_UNPACK) {
274
char *resstring = argv[optind];
276
if (optind == argc) {
277
fprintf(stderr, "Resource identifier required\n");
281
if ((resourcenumber_string = (char *) strchr(resstring, '.'))) {
282
*resourcenumber_string++ = 0;
283
} else if (optind + 1 == argc) {
284
fprintf(stderr, "Resource number required\n");
286
} else resourcenumber_string = argv[optind+1];
288
for (i = 0; i < 18; i++)
289
if ((strcmp(sci_resource_types[i], resstring) == 0)) stype = i;
291
printf("Could not find the resource type '%s'.\n", resstring);
297
if (chdir(gamedir)) {
298
printf("Error changing to game directory '%s'\n", gamedir);
302
if (!(resmgr = scir_new_resource_manager(gamedir, res_version,
304
fprintf(stderr, "Could not find any resources; quitting.\n");
308
if (verbose) printf("Autodetect determined: %s\n",
309
sci_version_types[resmgr->sci_version]);
318
for (i = 0; i < resmgr->resources_nr; i++) {
320
print_resource_filename(stdout,
321
resmgr->resources[i].type,
322
resmgr->resources[i].number);
323
printf(" has size %i\n", resmgr->resources[i].size);
326
fprintf(stderr, " Reading complete. Actual resource count is %i\n",
327
resmgr->resources_nr);
329
for (i = 0; i < resmgr->resources_nr; i++) {
330
print_resource_filename(stdout,
331
resmgr->resources[i].type,
332
resmgr->resources[i].number);
341
if (!strcmp(resourcenumber_string, "*")) {
343
for (i = 0; i < resmgr->resources_nr; i++)
344
if (resmgr->resources[i].type == stype)
345
unpack_resource(stype, resmgr->resources[i].number, NULL);
347
snr = atoi(resourcenumber_string);
348
unpack_resource(stype, snr, outfilename);
354
retval = vocab_print();
358
retval = script_dump();
362
retval = vocab_dump();
366
fprintf(stderr, "Invalid action %d- internal error!\n", action);
371
scir_free_resource_manager(resmgr);
376
void unpack_resource(int stype, int snr, char *outfilename) {
377
char fnamebuffer[12]; /* stores default file name */
380
if ((stype == sci_sound) && conversion && (resmgr->sci_version > SCI_VERSION_0)) {
381
fprintf(stderr, "MIDI conversion is only supported for SCI version 0\n");
386
outfilename = fnamebuffer;
387
if ((stype == sci_sound) && conversion) {
388
#ifdef HAVE_OBSTACK_H
389
map_MIDI_instruments(resmgr);
391
sprintf(outfilename, "%03d.midi", snr);
394
else if ((stype == sci_pic) && conversion)
395
sprintf(outfilename, "%03d.png", snr);
396
#endif /* DRAW_GRAPHICS */
398
sprint_resource_filename(outfilename, stype, snr);
403
print_resource_filename(stdout, stype, snr);
407
if ((found = scir_find_resource(resmgr, stype, snr, 0))) {
410
if ((stype == sci_pic) && conversion) {
412
picture_t pic = alloc_empty_picture(SCI_RESOLUTION_320X200, SCI_COLORDEPTH_8BPP);
413
draw_pic0(pic, 1, 0, found->data);
414
if ((i = write_pic_png(outfilename, pic->maps[0]))) {
415
fprintf(stderr, "Writing the png failed (%d)\n", i);
416
} else if (verbose) printf("Done.\n");
419
#endif /* DRAW_GRAPHICS */
420
if ((stype == sci_script) && conversion) {
421
sprintf(outfilename, "%03d.script", snr);
422
open_console_file(outfilename);
423
script_dissect(resmgr, snr, NULL, 0);
424
close_console_file();
427
/* Visual C++ doesn't allow to specify O_BINARY with creat() */
429
int outf = open(outfilename, _O_CREAT | _O_BINARY | _O_RDWR);
431
int outf = creat(outfilename, CREAT_OPTIONS);
434
#ifdef HAVE_OBSTACK_H
435
if ((stype == sci_sound) && conversion) {
437
guint8 *outdata = makeMIDI0(found->data, &midilength, midimask);
439
fprintf(stderr, "MIDI conversion failed. Aborting...\n");
442
if (verbose) printf("MIDI conversion from %d bytes of sound resource"
443
" to a %d bytes MIDI file.\n",
444
found->size, midilength);
445
write(outf, outdata, midilength);
448
#endif /* HAVE_OBSTACK_H */
449
guint8 header = 0x80 | found->type;
452
write(outf, &header, 1);
454
write(outf, &header, 1);
457
write(outf, found->data, found->size);
458
#ifdef HAVE_OBSTACK_H
460
#endif /* HAVE_OBSTACK_H */
466
if (verbose) printf("Done.\n");
469
} else printf("Resource not found.\n");