File: console.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (480 lines) | stat: -rw-r--r-- 13,736 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
/* 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 "engines/myst3/console.h"

#include "engines/myst3/archive.h"
#include "engines/myst3/database.h"
#include "engines/myst3/effects.h"
#include "engines/myst3/inventory.h"
#include "engines/myst3/script.h"
#include "engines/myst3/state.h"

namespace Myst3 {

Console::Console(Myst3Engine *vm) : GUI::Debugger(), _vm(vm) {
	registerCmd("infos",				WRAP_METHOD(Console, Cmd_Infos));
	registerCmd("lookAt",				WRAP_METHOD(Console, Cmd_LookAt));
	registerCmd("initScript",			WRAP_METHOD(Console, Cmd_InitScript));
	registerCmd("var",				WRAP_METHOD(Console, Cmd_Var));
	registerCmd("listNodes",			WRAP_METHOD(Console, Cmd_ListNodes));
	registerCmd("run",				WRAP_METHOD(Console, Cmd_Run));
	registerCmd("runOp",				WRAP_METHOD(Console, Cmd_RunOp));
	registerCmd("go",				WRAP_METHOD(Console, Cmd_Go));
	registerCmd("extract",				WRAP_METHOD(Console, Cmd_Extract));
	registerCmd("fillInventory",			WRAP_METHOD(Console, Cmd_FillInventory));
	registerCmd("dumpArchive",			WRAP_METHOD(Console, Cmd_DumpArchive));
	registerCmd("dumpMasks",			WRAP_METHOD(Console, Cmd_DumpMasks));
}

Console::~Console() {
}

void Console::describeScript(const Common::Array<Opcode> &script) {
	for (uint j = 0; j < script.size(); j++) {
		debugPrintf("%s", _vm->_scriptEngine->describeOpcode(script[j]).c_str());
	}
}

bool Console::Cmd_Infos(int argc, const char **argv) {
	uint16 nodeId = _vm->_state->getLocationNode();
	uint32 roomId = _vm->_state->getLocationRoom();
	uint32 ageID = _vm->_state->getLocationAge();

	if (argc >= 2) {
		nodeId = atoi(argv[1]);
	}

	if (argc >= 3) {
		RoomKey roomKey = _vm->_db->getRoomKey(argv[2]);
		if (roomKey.roomID == 0 || roomKey.ageID == 0) {
			debugPrintf("Unknown room name %s\n", argv[2]);
			return true;
		}

		roomId = roomKey.roomID;
		ageID = roomKey.ageID;
	}

	NodePtr nodeData = _vm->_db->getNodeData(nodeId, roomId, ageID);

	if (!nodeData) {
		debugPrintf("No node with id %d\n", nodeId);
		return true;
	}

	Common::String roomName = _vm->_db->getRoomName(roomId, ageID);

	debugPrintf("node: %s %d    ", roomName.c_str(), nodeId);

	for (uint i = 0; i < nodeData->scripts.size(); i++) {
		debugPrintf("\ninit %d > %s (%s)\n", i,
				_vm->_state->describeCondition(nodeData->scripts[i].condition).c_str(),
				_vm->_state->evaluate(nodeData->scripts[i].condition) ? "true" : "false");

		describeScript(nodeData->scripts[i].script);
	}

	for (uint i = 0; i < nodeData->hotspots.size(); i++) {
		debugPrintf("\nhotspot %d > %s (%s)\n", i,
				_vm->_state->describeCondition(nodeData->hotspots[i].condition).c_str(),
				_vm->_state->evaluate(nodeData->hotspots[i].condition) ? "true" : "false");

		for(uint j = 0; j < nodeData->hotspots[i].rects.size(); j++) {
			PolarRect &rect = nodeData->hotspots[i].rects[j];

			debugPrintf("    rect > pitch: %d heading: %d width: %d height: %d\n",
					rect.centerPitch, rect.centerHeading, rect.width, rect.height);
		}

		describeScript(nodeData->hotspots[i].script);
	}

	for (uint i = 0; i < nodeData->soundScripts.size(); i++) {
		debugPrintf("\nsound %d > %s (%s)\n", i,
				_vm->_state->describeCondition(nodeData->soundScripts[i].condition).c_str(),
				_vm->_state->evaluate(nodeData->soundScripts[i].condition) ? "true" : "false");

		describeScript(nodeData->soundScripts[i].script);
	}

	for (uint i = 0; i < nodeData->backgroundSoundScripts.size(); i++) {
		debugPrintf("\nbackground sound %d > %s (%s)\n", i,
				_vm->_state->describeCondition(nodeData->backgroundSoundScripts[i].condition).c_str(),
				_vm->_state->evaluate(nodeData->backgroundSoundScripts[i].condition) ? "true" : "false");

		describeScript(nodeData->backgroundSoundScripts[i].script);
	}

	return true;
}

bool Console::Cmd_LookAt(int argc, const char **argv) {
	if (argc != 1 && argc != 3) {
		debugPrintf("Usage :\n");
		debugPrintf("lookAt pitch heading\n");
		return true;
	}

	float pitch = _vm->_state->getLookAtPitch();
	float heading = _vm->_state->getLookAtHeading();

	debugPrintf("pitch: %d heading: %d\n", (int)pitch, (int)heading);

	if (argc >= 3){
		_vm->_state->lookAt(atof(argv[1]), atof(argv[2]));
		return false;
	}

	return true;
}

bool Console::Cmd_InitScript(int argc, const char **argv) {
	describeScript(_vm->_db->getNodeInitScript());

	return true;
}


bool Console::Cmd_Var(int argc, const char **argv) {

	if (argc != 2 && argc != 3) {
		debugPrintf("Usage :\n");
		debugPrintf("var variable : Display var value\n");
		debugPrintf("var variable value : Change var value\n");
		return true;
	}

	uint16 var = atoi(argv[1]);
	if (var < 1 || var > 2047) {
		debugPrintf("Variable out of range %d\n", var);
		return true;
	}
	uint32 value = _vm->_state->getVar(var);

	if (argc == 3) {
		value = atoi(argv[2]);
		_vm->_state->setVar(var, value);
	}

	debugPrintf("%s: %d\n", _vm->_state->describeVar(var).c_str(), value);

	return true;
}

bool Console::Cmd_ListNodes(int argc, const char **argv) {
	uint32 roomID = _vm->_state->getLocationRoom();
	uint32 ageID = _vm->_state->getLocationAge();

	if (argc == 2) {
		RoomKey roomKey = _vm->_db->getRoomKey(argv[1]);
		if (roomKey.roomID == 0 || roomKey.ageID == 0) {
			debugPrintf("Unknown room name %s\n", argv[1]);
			return true;
		}

		roomID = roomKey.roomID;
		ageID = roomKey.ageID;
	}

	debugPrintf("Nodes:\n");

	Common::Array<uint16> list = _vm->_db->listRoomNodes(roomID, ageID);
	for (uint i = 0; i < list.size(); i++) {
		debugPrintf("%d\n", list[i]);
	}

	return true;
}

bool Console::Cmd_Run(int argc, const char **argv) {
	uint16 nodeId = _vm->_state->getLocationNode();
	uint32 roomId = _vm->_state->getLocationRoom();
	uint32 ageId = _vm->_state->getLocationAge();

	if (argc >= 2) {
		nodeId = atoi(argv[1]);
	}

	if (argc >= 3) {
		RoomKey roomKey = _vm->_db->getRoomKey(argv[2]);

		if (roomKey.roomID == 0 || roomKey.ageID == 0) {
			debugPrintf("Unknown room name %s\n", argv[2]);
			return true;
		}

		roomId = roomKey.roomID;
		ageId = roomKey.ageID;
	}

	_vm->runScriptsFromNode(nodeId, roomId, ageId);

	return false;
}

bool Console::Cmd_RunOp(int argc, const char **argv) {
	if (argc < 2) {
		debugPrintf("Usage :\n");
		debugPrintf("runOp [opcode] [argument 1] [argument 2] ... : Run specified command\n");
		return true;
	}

	Opcode op;
	op.op = atoi(argv[1]);

	for (int i = 2; i < argc; i++) {
		op.args.push_back(atoi(argv[i]));
	}

	debugPrintf("Running opcode :\n");
	debugPrintf("%s\n", _vm->_scriptEngine->describeOpcode(op).c_str());

	_vm->_scriptEngine->runSingleOp(op);

	return false;
}

bool Console::Cmd_Go(int argc, const char **argv) {
	if (argc != 3) {
		debugPrintf("Usage :\n");
		debugPrintf("go [room name] [node id] : Go to node\n");
		return true;
	}

	RoomKey roomKey = _vm->_db->getRoomKey(argv[1]);
	if (roomKey.roomID == 0 || roomKey.ageID == 0) {
		debugPrintf("Unknown room name %s\n", argv[1]);
		return true;
	}

	uint16 nodeId = atoi(argv[2]);

	_vm->_state->setLocationNextAge(roomKey.ageID);
	_vm->_state->setLocationNextRoom(roomKey.roomID);
	_vm->_state->setLocationNextNode(nodeId);

	_vm->goToNode(0, kTransitionFade);

	return false;
}

bool Console::Cmd_Extract(int argc, const char **argv) {
	if (argc != 5) {
		debugPrintf("Extract a file from the game's archives\n");
		debugPrintf("Usage :\n");
		debugPrintf("extract [room] [node id] [face number] [object type]\n");
		return true;
	}

	// Room names are uppercase
	Common::String room = Common::String(argv[1]);
	room.toUppercase();

	uint16 id = atoi(argv[2]);
	uint16 face = atoi(argv[3]);
	Archive::ResourceType type = (Archive::ResourceType) atoi(argv[4]);

	ResourceDescription desc = _vm->getFileDescription(room, id, face, type);

	if (!desc.isValid()) {
		debugPrintf("File with room %s, id %d, face %d and type %d does not exist\n", room.c_str(), id, face, type);
		return true;
	}

	Common::SeekableReadStream *s = desc.getData();
	Common::Path filename(Common::String::format("node%s_%d_face%d.%d", room.c_str(), id, face, type));
	Common::DumpFile f;
	f.open(filename);

	uint8 *buf = new uint8[s->size()];

	s->read(buf, s->size());
	f.write(buf, s->size());

	delete[] buf;

	f.close();

	delete s;

	debugPrintf("File '%s' successfully written\n", filename.toString(Common::Path::kNativeSeparator).c_str());

	return true;
}

bool Console::Cmd_FillInventory(int argc, const char **argv) {
	_vm->_inventory->addAll();
	return false;
}

class DumpingArchiveVisitor : public ArchiveVisitor {
public:
	DumpingArchiveVisitor() :
			_archive(nullptr),
			_currentDirectoryEntry(nullptr) {
	}

	void visitArchive(Archive &archive) override {
		_archive = &archive;
	}

	void visitDirectoryEntry(Archive::DirectoryEntry &directoryEntry) override {
		_currentDirectoryEntry = &directoryEntry;
	}

	void visitDirectorySubEntry(Archive::DirectorySubEntry &directorySubEntry) override {
		assert(_currentDirectoryEntry);

		Common::Path fileName;
		switch (directorySubEntry.type) {
		case Archive::kNumMetadata:
		case Archive::kTextMetadata:
			return; // These types are pure metadata and can't be extracted
		case Archive::kCubeFace:
		case Archive::kSpotItem:
		case Archive::kLocalizedSpotItem:
		case Archive::kFrame:
			fileName = Common::Path(Common::String::format("dump/%s-%d-%d.jpg", _currentDirectoryEntry->roomName.c_str(), _currentDirectoryEntry->index, directorySubEntry.face));
			break;
		case Archive::kWaterEffectMask:
			fileName = Common::Path(Common::String::format("dump/%s-%d-%d.mask", _currentDirectoryEntry->roomName.c_str(), _currentDirectoryEntry->index, directorySubEntry.face));
			break;
		case Archive::kMovie:
		case Archive::kStillMovie:
		case Archive::kDialogMovie:
		case Archive::kMultitrackMovie:
			fileName = Common::Path(Common::String::format("dump/%s-%d.bik", _currentDirectoryEntry->roomName.c_str(), _currentDirectoryEntry->index));
			break;
		default:
			fileName = Common::Path(Common::String::format("dump/%s-%d-%d.%d", _currentDirectoryEntry->roomName.c_str(), _currentDirectoryEntry->index, directorySubEntry.face, directorySubEntry.type));
			break;
		}

		debug("Extracted %s", fileName.toString(Common::Path::kNativeSeparator).c_str());

		Common::DumpFile outFile;
		if (!outFile.open(fileName, true))
			error("Unable to open file '%s' for writing", fileName.toString(Common::Path::kNativeSeparator).c_str());

		_archive->copyTo(directorySubEntry.offset, directorySubEntry.size, outFile);
		outFile.close();
	}

private:
	Archive *_archive;
	const Archive::DirectoryEntry *_currentDirectoryEntry;
};

bool Console::Cmd_DumpArchive(int argc, const char **argv) {
	if (argc != 2) {
		debugPrintf("Extract all the files from a game archive.\n");
		debugPrintf("The destination folder, named 'dump', must exist.\n");
		debugPrintf("Usage :\n");
		debugPrintf("dumpArchive [file name]\n");
		return true;
	}

	// Is the archive multi-room
	Common::String temp = Common::String(argv[1]);
	temp.toUppercase();

	bool multiRoom = !temp.hasSuffix(".M3A");
	if (!multiRoom) {
		temp = Common::String(argv[1], 4);
		temp.toUppercase();
	}

	Archive archive;
	if (!archive.open(argv[1], multiRoom ? nullptr : temp.c_str())) {
		debugPrintf("Can't open archive with name '%s'\n", argv[1]);
		return true;
	}

	DumpingArchiveVisitor dumper;
	archive.visit(dumper);

	archive.close();

	return true;
}

bool Console::Cmd_DumpMasks(int argc, const char **argv) {
	if (argc != 1 && argc != 2) {
		debugPrintf("Extract the masks of the faces of a cube node.\n");
		debugPrintf("The destination folder, named 'dump', must exist.\n");
		debugPrintf("Usage :\n");
		debugPrintf("dumpMasks [node]\n");
		return true;
	}

	uint16 nodeId = _vm->_state->getLocationNode();

	if (argc >= 2) {
		nodeId = atoi(argv[1]);
	}

	debugPrintf("Extracting masks for node %d:\n", nodeId);

	for (uint i = 0; i < 6; i++) {
		bool water = dumpFaceMask(nodeId, i, Archive::kWaterEffectMask);
		if (water)
			debugPrintf("Face %d: water OK\n", i);

		bool effect2 = dumpFaceMask(nodeId, i, Archive::kLavaEffectMask);
		if (effect2)
			debugPrintf("Face %d: effect 2 OK\n", i);

		bool magnet = dumpFaceMask(nodeId, i, Archive::kMagneticEffectMask);
		if (magnet)
			debugPrintf("Face %d: magnet OK\n", i);

		if (!water && !effect2 && !magnet)
			debugPrintf("Face %d: No mask found\n", i);
	}

	return true;
}

bool Console::dumpFaceMask(uint16 index, int face, Archive::ResourceType type) {
	ResourceDescription maskDesc = _vm->getFileDescription("", index, face, type);

	if (!maskDesc.isValid())
		return false;

	Common::SeekableReadStream *maskStream = maskDesc.getData();

	Effect::FaceMask *mask = Effect::loadMask(maskStream);

	delete maskStream;

	Common::DumpFile outFile;
	Common::Path fileName(Common::String::format("dump/%d-%d.masku_%d", index, face, type));
	outFile.open(fileName);
	outFile.write(mask->surface->getPixels(), mask->surface->pitch * mask->surface->h);
	outFile.close();

	delete mask;

	return true;
}

} // End of namespace Myst3