File: console.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (498 lines) | stat: -rw-r--r-- 12,999 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/memstream.h"
#include "common/system.h"

#include "graphics/palette.h"
#include "graphics/paletteman.h"

#include "image/png.h"

#include "dgds/ads.h"
#include "dgds/console.h"
#include "dgds/decompress.h"
#include "dgds/dgds.h"
#include "dgds/globals.h"
#include "dgds/includes.h"
#include "dgds/image.h"
#include "dgds/parser.h"
#include "dgds/resource.h"
#include "dgds/scene.h"
#include "dgds/game_palettes.h"
#include "gui/debugger.h"

namespace Dgds {

Console::Console(DgdsEngine *vm) : _vm(vm) {
	registerCmd("fileinfo", WRAP_METHOD(Console, cmdFileInfo));
	registerCmd("filesearch", WRAP_METHOD(Console, cmdFileSearch));
	registerCmd("filedump", WRAP_METHOD(Console, cmdFileDump));
	registerCmd("imagedump", WRAP_METHOD(Console, cmdImageDump));
	registerCmd("imagedumpall", WRAP_METHOD(Console, cmdImageDumpAll));
	registerCmd("global", WRAP_METHOD(Console, cmdGlobal));
	registerCmd("scene", WRAP_METHOD(Console, cmdScene));
	registerCmd("scriptdump", WRAP_METHOD(Console, cmdScriptDump));
	registerCmd("trigger", WRAP_METHOD(Console, cmdTrigger));
}

bool Console::cmdFileInfo(int argc, const char **argv) {
	if (argc != 2) {
		debugPrintf("Usage: %s <file>\n", argv[0]);
		return true;
	}

	const Resource &res = _vm->getResourceManager()->getResourceInfo(argv[1]);
	debugPrintf("Resource volume: %d, position: %d, size: %d, checksum: %d\n", res.volume, res.pos, res.size, res.checksum);

	return true;
}

bool Console::cmdFileSearch(int argc, const char **argv) {
	if (argc != 2) {
		debugPrintf("Usage: %s <file to search>\n", argv[0]);
		return true;
	}

	const ResourceList &resources = _vm->getResourceManager()->getResources();
	for (ResourceList::const_iterator i = resources.begin(), end = resources.end(); i != end; ++i) {
		if (i->_key.contains(argv[1])) {
			Resource res = i->_value;
			debugPrintf("Resource: %s, volume: %d, position: %d, size: %d, checksum: %d\n", i->_key.c_str(), res.volume, res.pos, res.size, res.checksum);
		}
	}

	return true;
}

bool Console::cmdFileDump(int argc, const char **argv) {
	if (argc < 2) {
		debugPrintf("Usage: %s <file> [ignore patches] [unpack] [outputpath] [chunktype]\n", argv[0]);
		debugPrintf("   eg: %s CLLIVING.ADH 0 1 clliving_unpack.adscript SCR:\n", argv[0]);
		return true;
	}

	Common::String fileName = argv[1];
	bool ignorePatches = (argc > 2) && (!scumm_stricmp(argv[2], "true") || !strcmp(argv[2], "1"));
	bool unpack = (argc > 3) && (!scumm_stricmp(argv[3], "true") || !strcmp(argv[3], "1"));
	Common::String dstPath = (argc > 4) ? argv[4] : "";
	Common::String chunkType = (argc > 5) ? argv[5] : "";
	Common::SeekableReadStream *resStream = _vm->getResource(fileName, ignorePatches);
	if (resStream == nullptr) {
		debugPrintf("Resource not found\n");
		return true;
	}
	int32 size = resStream->size();
	byte *data;

	if (!unpack) {
		data = new byte[size];
		resStream->read(data, size);
	} else {
		data = new byte[2000000]; // about 2MB, but maximum container size is around 1.5MB
		byte *ptr = data;
		const char *dot;
		DGDS_EX ex = 0;
		size = 0;

		if ((dot = strrchr(fileName.c_str(), '.'))) {
			ex = MKTAG24(toupper(dot[1]), toupper(dot[2]), toupper(dot[3]));
		}

		DgdsChunkReader chunk(resStream);
		while (chunk.readNextHeader(ex, fileName)) {
			if (!chunkType.empty() && !chunkType.equals(chunk.getIdStr())) {
				if (!chunk.isContainer())
					chunk.skipContent();
				continue;
			}

			bool readSuccess = chunk.readContent(_vm->getDecompressor());
			if (readSuccess) {
				memcpy(ptr, chunk.getIdStr(), 4);
				ptr += 4;

				chunk.getContent()->read(ptr, chunk.getContent()->size());
				ptr += chunk.getContent()->size();

				size += 4 + chunk.getContent()->size();
			} else {
				warning("Failed to read content for chunk with id %s", chunk.getIdStr());
			}
		}

	}

	delete resStream;

	Common::DumpFile out;
	out.open(Common::Path(dstPath + fileName));
	if (!out.isOpen()) {
		warning("Couldn't open path %s%s", dstPath.c_str(), fileName.c_str());
	} else {
		out.write(data, size);
		out.flush();
		out.close();
	}
	delete[] data;

	return true;
}

bool Console::dumpImageFrame(const char *fname, int frameno, const char *outpath) {
	Image img(_vm->getResourceManager(), _vm->getDecompressor());
	int maxframe = img.frameCount(fname);
	if (frameno > maxframe) {
		debugPrintf("Image only has %d frames\n", maxframe);
		return false;
	}
	GamePalettes pal(_vm->getResourceManager(), _vm->getDecompressor());
	img.loadBitmap(fname);
	int width = img.width(frameno);
	int height = img.height(frameno);
	if (!width || !height) {
		debugPrintf("Image %s:%d not valid\n", fname, frameno);
		return false;
	}

	Common::DumpFile outf;
	Common::String outfname = Common::String::format("%s-%d.png", fname, frameno);

	if (outpath) {
		Common::Path path(outpath);
		path.joinInPlace(outfname);
		outf.open(path);
	} else {
		outf.open(Common::Path(outfname));
	}
	if (!outf.isOpen()) {
		debugPrintf("Couldn't open %s\n", outfname.c_str());
		return false;
	}

	byte palbuf[768];
	g_system->getPaletteManager()->grabPalette(palbuf, 0, 256);
	::Image::writePNG(outf, *(img.getSurface(frameno)->surfacePtr()), palbuf);
	outf.close();
	debugPrintf("wrote %dx%d png to %s\n", width, height, outfname.c_str());
	return true;
}

bool Console::cmdImageDumpAll(int argc, const char **argv) {
#ifdef USE_PNG
	if (argc < 2) {
		debugPrintf("Usage: %s <imagefilename> [outputdir]\n", argv[0]);
		debugPrintf("  eg: %s CLGAME2.BMP /tmp\n", argv[0]);
		return true;
	}

	const char *fname = argv[1];
	if (!_vm->getResourceManager()->hasResource(fname))  {
		debugPrintf("Resource %s not found\n", fname);
		return true;
	}

	Image img(_vm->getResourceManager(), _vm->getDecompressor());
	int maxframe = img.frameCount(fname);

	const char *outpath = (argc > 2 ? argv[2] : nullptr);

	for (int i = 0; i < maxframe; i++) {
		if (!dumpImageFrame(fname, i, outpath))
			break;
	}
	return true;
#else
	warning("dumpimage needs png support");
	return true;
#endif // USE_PNG

}

bool Console::cmdImageDump(int argc, const char **argv) {
#ifdef USE_PNG
	if (argc < 3) {
		debugPrintf("Usage: %s <imagefilename> <frameno> [outputdir]\n", argv[0]);
		debugPrintf("  eg: %s CLGAME2.BMP 2 /tmp\n", argv[0]);
		return true;
	}

	const char *fname = argv[1];
	int frameno = atoi(argv[2]);

	if (!_vm->getResourceManager()->hasResource(fname))  {
		debugPrintf("Resource %s not found\n", fname);
		return true;
	}

	const char *outpath = (argc > 3 ? argv[3] : nullptr);

	dumpImageFrame(fname, frameno, outpath);
	return true;
#else
	warning("dumpimage needs png support");
	return true;
#endif // USE_PNG
}

bool Console::cmdGlobal(int argc, const char **argv) {
	if (argc < 2) {
		debugPrintf("Usage: %s <num> <val>\n", argv[0]);
		debugPrintf("%s <num> returns the value of a global\n", argv[0]);
		debugPrintf("%s <num> <val> sets the value of a global\n", argv[0]);
		return true;
	}

	GDSScene *scene = _vm->getGDSScene();
	int num = atoi(argv[1]);

	if (argc == 2) {
		int16 val = scene->getGlobal(num);
		debugPrintf("Global %d is %d\n", num, val);
	} else if (argc == 3) {
		int16 newVal = atoi(argv[2]);
		scene->setGlobal(num, newVal);
		debugPrintf("Global %d set to %d\n", num, newVal);
	}

	return true;
}

bool Console::cmdScene(int argc, const char **argv) {
	if (argc == 1) {
		int curSceneNum = _vm->getScene()->getNum();
		debugPrintf("Current scene is %d\n", curSceneNum);
		debugPrintf("Use %s <num> to change the scene\n", argv[0]);
	} else {
		int num = atoi(argv[1]);
		_vm->changeScene(num);
		debugPrintf("Scene changed to %d\n", num);
	}

	return true;
}

bool Console::cmdTrigger(int argc, const char **argv) {
	if (argc < 2) {
		debugPrintf("Usage: %s <num> <val>\n", argv[0]);
		debugPrintf("%s <num> returns the status of a trigger\n", argv[0]);
		debugPrintf("%s <num> <val> sets the status of a trigger\n", argv[0]);
		return true;
	}

	SDSScene *scene = _vm->getScene();
	int num = atoi(argv[1]);

	if (argc == 2) {
		bool val = scene->isTriggerEnabled(num);
		debugPrintf("Trigger %d is %d\n", num, val);
	} else if (argc == 3) {
		bool enable = atoi(argv[2]);
		scene->enableTrigger(num, enable);
		debugPrintf("Trigger %d set to %d\n", num, enable);
	}

	return true;
}

void Console::printOp(int indent, const char *text) {
	for (int i = 0; i < indent; i++) {
		debugPrintf("\t");
	}
	debugPrintf("%s", text);
}

bool Console::cmdScriptDump(int argc, const char **argv) {
	if (argc < 2) {
		debugPrintf("Usage: %s <filename>\n", argv[0]);
		debugPrintf("  eg: %s CLBATH.ADS\n", argv[0]);
		return true;
	}

	Common::String fname(argv[1]);

	Common::SeekableReadStream *resStream = _vm->getResource(fname, false);
	if (resStream == nullptr) {
		debugPrintf("Resource %s not found\n", fname.c_str());
		return true;
	}

	ADSData adsData;
	ADSInterpreter interp(_vm);
	ADSParser dgds(_vm->getResourceManager(), _vm->getDecompressor());
	dgds.parse(&adsData, fname);

	adsData.scr->seek(0);

	uint8 segno = 0;
	uint8 indent = 1;
	uint16 opcode;
	uint16 tag = 1;
	while (!adsData.scr->eos()) {
		opcode = adsData.scr->readUint16LE();
		switch (opcode) {
		case 0xFFFF:
			printOp(indent, "END\n");
			tag = adsData.scr->readUint16LE();
			segno++;
			// Fall through
		case 0x0001:
			if (adsData._tags.contains(tag))
				debugPrintf("\n%d: %s\n", segno, adsData._tags[tag].c_str());
			continue;
		case 0x0005:
			printOp(indent, "init");
			break;
		case 0x1010:
			printOp(indent++, "WHILE runtype");
			break;
		case 0x1020:
			printOp(indent++, "WHILE not runtype");
			break;
		case 0x1030:
			printOp(indent++, "WHILE NOT_PLAYED");
			break;
		case 0x1040:
			printOp(indent++, "WHILE PLAYED");
			break;
		case 0x1050:
			printOp(indent++, "WHILE FINISHED");
			break;
		case 0x1060:
			printOp(indent++, "WHILE NOT_RUNNING");
			break;
		case 0x1070:
			printOp(indent++, "WHILE RUNNING");
			break;
		case 0x1080:
			printOp(indent++, "WHILE count?");
			break;
		case 0x1090:
			printOp(indent++, "WHILE ??");
			break;
		case 0x1310:
			printOp(indent++, "IF PAUSED");
			break;
		case 0x1320:
			printOp(indent++, "IF NOT_PAUSED");
			break;
		case 0x1330:
			printOp(indent++, "IF NOT_PLAYED");
			break;
		case 0x1340:
			printOp(indent++, "IF PLAYED");
			break;
		case 0x1350:
			printOp(indent++, "IF FINISHED");
			break;
		case 0x1360:
			printOp(indent++, "IF NOT_RUNNING");
			break;
		case 0x1370:
			printOp(indent++, "IF RUNNING");
			break;
		case 0x1380:
			printOp(indent++, "IF DETAIL LEVEL <=");
			break;
		case 0x1390:
			printOp(indent++, "IF DETAIL LEVEL >=");
			break;
		case 0x1500:
			printOp(--indent, "ELSE / Skip to end-if");
			break;
		case 0x1510:
			printOp(--indent, "END IF");
			break;
		case 0x1520:
			printOp(--indent, "END WHILE");
			break;
		case 0x2000:
			printOp(indent, "ADD sequence (restart)");
			break;
		case 0x2005:
			printOp(indent, "ADD sequence (continue)");
			break;
		case 0x2010:
			printOp(indent, "STOP SCENE");
			break;
		case 0x2015:
			printOp(indent, "PAUSE SEQ");
			break;
		case 0x2020:
			printOp(indent, "RESET SEQ");
			break;
		case 0x3020:
			printOp(indent, "RANDOM_NOOP");
			break;
		case 0x3010:
			printOp(indent++, "RANDOM_START");
			break;
		case 0x30FF:
			printOp(--indent, "RANDOM_END");
			break;
		case 0x4000:
			printOp(indent, "MOVE SEQ TO BACK");
			break;
		case 0x4010:
			printOp(indent, "MOVE SEQ TO FRONT");
			break;
		case 0xF000:
			printOp(indent, "SET STATE 2");
			break;
		case 0xF010:
			printOp(indent, "FADE_OUT");
			break;
		case 0xF200:
			printOp(indent, "RUN_SCRIPT");
			break;
		case 0xF210:
			printOp(indent, "RESTART_SCRIPT");
			break;
		case 0x1420:
			printOp(--indent, "AND");
			break;
		case 0x1430:
			printOp(--indent, "OR");
			break;
		case 0xFF10:
			printOp(indent, "UNKNOWN");
			break;
		case 0xFFF0:
			printOp(indent, "END_IF");
			break;
		default:
			printOp(indent, "UNKNOWN");
			debugPrintf("(%04x)", opcode);
			break;
		}
		int numArgs = interp.numArgs(opcode);
		for (int i = 0; i < numArgs; i++) {
			debugPrintf(" %d", adsData.scr->readUint16LE());
		}
		debugPrintf("\n");
	}

	delete resStream;

	return true;
}

} // End of namespace Dgds