ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

console.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/myst3/console.h"
00024 
00025 #include "engines/myst3/archive.h"
00026 #include "engines/myst3/database.h"
00027 #include "engines/myst3/effects.h"
00028 #include "engines/myst3/inventory.h"
00029 #include "engines/myst3/script.h"
00030 #include "engines/myst3/state.h"
00031 
00032 namespace Myst3 {
00033 
00034 Console::Console(Myst3Engine *vm) : GUI::Debugger(), _vm(vm) {
00035     registerCmd("infos",                WRAP_METHOD(Console, Cmd_Infos));
00036     registerCmd("lookAt",               WRAP_METHOD(Console, Cmd_LookAt));
00037     registerCmd("initScript",           WRAP_METHOD(Console, Cmd_InitScript));
00038     registerCmd("var",              WRAP_METHOD(Console, Cmd_Var));
00039     registerCmd("listNodes",            WRAP_METHOD(Console, Cmd_ListNodes));
00040     registerCmd("run",              WRAP_METHOD(Console, Cmd_Run));
00041     registerCmd("runOp",                WRAP_METHOD(Console, Cmd_RunOp));
00042     registerCmd("go",               WRAP_METHOD(Console, Cmd_Go));
00043     registerCmd("extract",              WRAP_METHOD(Console, Cmd_Extract));
00044     registerCmd("fillInventory",            WRAP_METHOD(Console, Cmd_FillInventory));
00045     registerCmd("dumpArchive",          WRAP_METHOD(Console, Cmd_DumpArchive));
00046     registerCmd("dumpMasks",            WRAP_METHOD(Console, Cmd_DumpMasks));
00047 }
00048 
00049 Console::~Console() {
00050 }
00051 
00052 void Console::describeScript(const Common::Array<Opcode> &script) {
00053     for (uint j = 0; j < script.size(); j++) {
00054         debugPrintf("%s", _vm->_scriptEngine->describeOpcode(script[j]).c_str());
00055     }
00056 }
00057 
00058 bool Console::Cmd_Infos(int argc, const char **argv) {
00059     uint16 nodeId = _vm->_state->getLocationNode();
00060     uint32 roomId = _vm->_state->getLocationRoom();
00061     uint32 ageID = _vm->_state->getLocationAge();
00062 
00063     if (argc >= 2) {
00064         nodeId = atoi(argv[1]);
00065     }
00066 
00067     if (argc >= 3) {
00068         RoomKey roomKey = _vm->_db->getRoomKey(argv[2]);
00069         if (roomKey.roomID == 0 || roomKey.ageID == 0) {
00070             debugPrintf("Unknown room name %s\n", argv[2]);
00071             return true;
00072         }
00073 
00074         roomId = roomKey.roomID;
00075         ageID = roomKey.ageID;
00076     }
00077 
00078     NodePtr nodeData = _vm->_db->getNodeData(nodeId, roomId, ageID);
00079 
00080     if (!nodeData) {
00081         debugPrintf("No node with id %d\n", nodeId);
00082         return true;
00083     }
00084 
00085     Common::String roomName = _vm->_db->getRoomName(roomId, ageID);
00086 
00087     debugPrintf("node: %s %d    ", roomName.c_str(), nodeId);
00088 
00089     for (uint i = 0; i < nodeData->scripts.size(); i++) {
00090         debugPrintf("\ninit %d > %s (%s)\n", i,
00091                 _vm->_state->describeCondition(nodeData->scripts[i].condition).c_str(),
00092                 _vm->_state->evaluate(nodeData->scripts[i].condition) ? "true" : "false");
00093 
00094         describeScript(nodeData->scripts[i].script);
00095     }
00096 
00097     for (uint i = 0; i < nodeData->hotspots.size(); i++) {
00098         debugPrintf("\nhotspot %d > %s (%s)\n", i,
00099                 _vm->_state->describeCondition(nodeData->hotspots[i].condition).c_str(),
00100                 _vm->_state->evaluate(nodeData->hotspots[i].condition) ? "true" : "false");
00101 
00102         for(uint j = 0; j < nodeData->hotspots[i].rects.size(); j++) {
00103             PolarRect &rect = nodeData->hotspots[i].rects[j];
00104 
00105             debugPrintf("    rect > pitch: %d heading: %d width: %d height: %d\n",
00106                     rect.centerPitch, rect.centerHeading, rect.width, rect.height);
00107         }
00108 
00109         describeScript(nodeData->hotspots[i].script);
00110     }
00111 
00112     for (uint i = 0; i < nodeData->soundScripts.size(); i++) {
00113         debugPrintf("\nsound %d > %s (%s)\n", i,
00114                 _vm->_state->describeCondition(nodeData->soundScripts[i].condition).c_str(),
00115                 _vm->_state->evaluate(nodeData->soundScripts[i].condition) ? "true" : "false");
00116 
00117         describeScript(nodeData->soundScripts[i].script);
00118     }
00119 
00120     for (uint i = 0; i < nodeData->backgroundSoundScripts.size(); i++) {
00121         debugPrintf("\nbackground sound %d > %s (%s)\n", i,
00122                 _vm->_state->describeCondition(nodeData->backgroundSoundScripts[i].condition).c_str(),
00123                 _vm->_state->evaluate(nodeData->backgroundSoundScripts[i].condition) ? "true" : "false");
00124 
00125         describeScript(nodeData->backgroundSoundScripts[i].script);
00126     }
00127 
00128     return true;
00129 }
00130 
00131 bool Console::Cmd_LookAt(int argc, const char **argv) {
00132     if (argc != 1 && argc != 3) {
00133         debugPrintf("Usage :\n");
00134         debugPrintf("lookAt pitch heading\n");
00135         return true;
00136     }
00137 
00138     float pitch = _vm->_state->getLookAtPitch();
00139     float heading = _vm->_state->getLookAtHeading();
00140 
00141     debugPrintf("pitch: %d heading: %d\n", (int)pitch, (int)heading);
00142 
00143     if (argc >= 3){
00144         _vm->_state->lookAt(atof(argv[1]), atof(argv[2]));
00145         return false;
00146     }
00147 
00148     return true;
00149 }
00150 
00151 bool Console::Cmd_InitScript(int argc, const char **argv) {
00152     describeScript(_vm->_db->getNodeInitScript());
00153 
00154     return true;
00155 }
00156 
00157 
00158 bool Console::Cmd_Var(int argc, const char **argv) {
00159 
00160     if (argc != 2 && argc != 3) {
00161         debugPrintf("Usage :\n");
00162         debugPrintf("var variable : Display var value\n");
00163         debugPrintf("var variable value : Change var value\n");
00164         return true;
00165     }
00166 
00167     uint16 var = atoi(argv[1]);
00168     if (var < 1 || var > 2047) {
00169         debugPrintf("Variable out of range %d\n", var);
00170         return true;
00171     }   
00172     uint32 value = _vm->_state->getVar(var);
00173 
00174     if (argc == 3) {
00175         value = atoi(argv[2]);
00176         _vm->_state->setVar(var, value);
00177     }
00178 
00179     debugPrintf("%s: %d\n", _vm->_state->describeVar(var).c_str(), value);
00180 
00181     return true;
00182 }
00183 
00184 bool Console::Cmd_ListNodes(int argc, const char **argv) {
00185     uint32 roomID = _vm->_state->getLocationRoom();
00186     uint32 ageID = _vm->_state->getLocationAge();
00187 
00188     if (argc == 2) {
00189         RoomKey roomKey = _vm->_db->getRoomKey(argv[1]);
00190         if (roomKey.roomID == 0 || roomKey.ageID == 0) {
00191             debugPrintf("Unknown room name %s\n", argv[1]);
00192             return true;
00193         }
00194 
00195         roomID = roomKey.roomID;
00196         ageID = roomKey.ageID;
00197     }
00198 
00199     debugPrintf("Nodes:\n");
00200 
00201     Common::Array<uint16> list = _vm->_db->listRoomNodes(roomID, ageID);
00202     for (uint i = 0; i < list.size(); i++) {
00203         debugPrintf("%d\n", list[i]);
00204     }
00205 
00206     return true;
00207 }
00208 
00209 bool Console::Cmd_Run(int argc, const char **argv) {
00210     uint16 nodeId = _vm->_state->getLocationNode();
00211     uint32 roomId = _vm->_state->getLocationRoom();
00212     uint32 ageId = _vm->_state->getLocationAge();
00213 
00214     if (argc >= 2) {
00215         nodeId = atoi(argv[1]);
00216     }
00217 
00218     if (argc >= 3) {
00219         RoomKey roomKey = _vm->_db->getRoomKey(argv[2]);
00220 
00221         if (roomKey.roomID == 0 || roomKey.ageID == 0) {
00222             debugPrintf("Unknown room name %s\n", argv[2]);
00223             return true;
00224         }
00225 
00226         roomId = roomKey.roomID;
00227         ageId = roomKey.ageID;
00228     }
00229 
00230     _vm->runScriptsFromNode(nodeId, roomId, ageId);
00231 
00232     return false;
00233 }
00234 
00235 bool Console::Cmd_RunOp(int argc, const char **argv) {
00236     if (argc < 2) {
00237         debugPrintf("Usage :\n");
00238         debugPrintf("runOp [opcode] [argument 1] [argument 2] ... : Run specified command\n");
00239         return true;
00240     }
00241 
00242     Opcode op;
00243     op.op = atoi(argv[1]);
00244 
00245     for (int i = 2; i < argc; i++) {
00246         op.args.push_back(atoi(argv[i]));
00247     }
00248 
00249     debugPrintf("Running opcode :\n");
00250     debugPrintf("%s\n", _vm->_scriptEngine->describeOpcode(op).c_str());
00251 
00252     _vm->_scriptEngine->runSingleOp(op);
00253 
00254     return false;
00255 }
00256 
00257 bool Console::Cmd_Go(int argc, const char **argv) {
00258     if (argc != 3) {
00259         debugPrintf("Usage :\n");
00260         debugPrintf("go [room name] [node id] : Go to node\n");
00261         return true;
00262     }
00263 
00264     RoomKey roomKey = _vm->_db->getRoomKey(argv[1]);
00265     if (roomKey.roomID == 0 || roomKey.ageID == 0) {
00266         debugPrintf("Unknown room name %s\n", argv[1]);
00267         return true;
00268     }
00269 
00270     uint16 nodeId = atoi(argv[2]);
00271 
00272     _vm->_state->setLocationNextAge(roomKey.ageID);
00273     _vm->_state->setLocationNextRoom(roomKey.roomID);
00274     _vm->_state->setLocationNextNode(nodeId);
00275 
00276     _vm->goToNode(0, kTransitionFade);
00277 
00278     return false;
00279 }
00280 
00281 bool Console::Cmd_Extract(int argc, const char **argv) {
00282     if (argc != 5) {
00283         debugPrintf("Extract a file from the game's archives\n");
00284         debugPrintf("Usage :\n");
00285         debugPrintf("extract [room] [node id] [face number] [object type]\n");
00286         return true;
00287     }
00288 
00289     // Room names are uppercase
00290     Common::String room = Common::String(argv[1]);
00291     room.toUppercase();
00292 
00293     uint16 id = atoi(argv[2]);
00294     uint16 face = atoi(argv[3]);
00295     Archive::ResourceType type = (Archive::ResourceType) atoi(argv[4]);
00296 
00297     ResourceDescription desc = _vm->getFileDescription(room, id, face, type);
00298 
00299     if (!desc.isValid()) {
00300         debugPrintf("File with room %s, id %d, face %d and type %d does not exist\n", room.c_str(), id, face, type);
00301         return true;
00302     }
00303 
00304     Common::SeekableReadStream *s = desc.getData();
00305     Common::String filename = Common::String::format("node%s_%d_face%d.%d", room.c_str(), id, face, type);
00306     Common::DumpFile f;
00307     f.open(filename);
00308 
00309     uint8 *buf = new uint8[s->size()];
00310 
00311     s->read(buf, s->size());
00312     f.write(buf, s->size());
00313 
00314     delete[] buf;
00315 
00316     f.close();
00317 
00318     delete s;
00319 
00320     debugPrintf("File '%s' successfully written\n", filename.c_str());
00321 
00322     return true;
00323 }
00324 
00325 bool Console::Cmd_FillInventory(int argc, const char **argv) {
00326     _vm->_inventory->addAll();
00327     return false;
00328 }
00329 
00330 class DumpingArchiveVisitor : public ArchiveVisitor {
00331 public:
00332     DumpingArchiveVisitor() :
00333             _archive(nullptr),
00334             _currentDirectoryEntry(nullptr) {
00335     }
00336 
00337     void visitArchive(Archive &archive) override {
00338         _archive = &archive;
00339     }
00340 
00341     void visitDirectoryEntry(Archive::DirectoryEntry &directoryEntry) override {
00342         _currentDirectoryEntry = &directoryEntry;
00343     }
00344 
00345     void visitDirectorySubEntry(Archive::DirectorySubEntry &directorySubEntry) override {
00346         assert(_currentDirectoryEntry);
00347 
00348         Common::String fileName;
00349         switch (directorySubEntry.type) {
00350         case Archive::kNumMetadata:
00351         case Archive::kTextMetadata:
00352             return; // These types are pure metadata and can't be extracted
00353         case Archive::kCubeFace:
00354         case Archive::kSpotItem:
00355         case Archive::kLocalizedSpotItem:
00356         case Archive::kFrame:
00357             fileName = Common::String::format("dump/%s-%d-%d.jpg", _currentDirectoryEntry->roomName.c_str(), _currentDirectoryEntry->index, directorySubEntry.face);
00358             break;
00359         case Archive::kWaterEffectMask:
00360             fileName = Common::String::format("dump/%s-%d-%d.mask", _currentDirectoryEntry->roomName.c_str(), _currentDirectoryEntry->index, directorySubEntry.face);
00361             break;
00362         case Archive::kMovie:
00363         case Archive::kStillMovie:
00364         case Archive::kDialogMovie:
00365         case Archive::kMultitrackMovie:
00366             fileName = Common::String::format("dump/%s-%d.bik", _currentDirectoryEntry->roomName.c_str(), _currentDirectoryEntry->index);
00367             break;
00368         default:
00369             fileName = Common::String::format("dump/%s-%d-%d.%d", _currentDirectoryEntry->roomName.c_str(), _currentDirectoryEntry->index, directorySubEntry.face, directorySubEntry.type);
00370             break;
00371         }
00372 
00373         debug("Extracted %s", fileName.c_str());
00374 
00375         Common::DumpFile outFile;
00376         if (!outFile.open(fileName, true))
00377             error("Unable to open file '%s' for writing", fileName.c_str());
00378 
00379         _archive->copyTo(directorySubEntry.offset, directorySubEntry.size, outFile);
00380         outFile.close();
00381     }
00382 
00383 private:
00384     Archive *_archive;
00385     const Archive::DirectoryEntry *_currentDirectoryEntry;
00386 };
00387 
00388 bool Console::Cmd_DumpArchive(int argc, const char **argv) {
00389     if (argc != 2) {
00390         debugPrintf("Extract all the files from a game archive.\n");
00391         debugPrintf("The destination folder, named 'dump', must exist.\n");
00392         debugPrintf("Usage :\n");
00393         debugPrintf("dumpArchive [file name]\n");
00394         return true;
00395     }
00396 
00397     // Is the archive multi-room
00398     Common::String temp = Common::String(argv[1]);
00399     temp.toUppercase();
00400 
00401     bool multiRoom = !temp.hasSuffix(".M3A");
00402     if (!multiRoom) {
00403         temp = Common::String(argv[1], 4);
00404         temp.toUppercase();
00405     }
00406 
00407     Archive archive;
00408     if (!archive.open(argv[1], multiRoom ? nullptr : temp.c_str())) {
00409         debugPrintf("Can't open archive with name '%s'\n", argv[1]);
00410         return true;
00411     }
00412 
00413     DumpingArchiveVisitor dumper;
00414     archive.visit(dumper);
00415 
00416     archive.close();
00417 
00418     return true;
00419 }
00420 
00421 bool Console::Cmd_DumpMasks(int argc, const char **argv) {
00422     if (argc != 1 && argc != 2) {
00423         debugPrintf("Extract the masks of the faces of a cube node.\n");
00424         debugPrintf("The destination folder, named 'dump', must exist.\n");
00425         debugPrintf("Usage :\n");
00426         debugPrintf("dumpMasks [node]\n");
00427         return true;
00428     }
00429 
00430     uint16 nodeId = _vm->_state->getLocationNode();
00431 
00432     if (argc >= 2) {
00433         nodeId = atoi(argv[1]);
00434     }
00435 
00436     debugPrintf("Extracting masks for node %d:\n", nodeId);
00437 
00438     for (uint i = 0; i < 6; i++) {
00439         bool water = dumpFaceMask(nodeId, i, Archive::kWaterEffectMask);
00440         if (water)
00441             debugPrintf("Face %d: water OK\n", i);
00442 
00443         bool effect2 = dumpFaceMask(nodeId, i, Archive::kLavaEffectMask);
00444         if (effect2)
00445             debugPrintf("Face %d: effect 2 OK\n", i);
00446 
00447         bool magnet = dumpFaceMask(nodeId, i, Archive::kMagneticEffectMask);
00448         if (magnet)
00449             debugPrintf("Face %d: magnet OK\n", i);
00450 
00451         if (!water && !effect2 && !magnet)
00452             debugPrintf("Face %d: No mask found\n", i);
00453     }
00454 
00455     return true;
00456 }
00457 
00458 bool Console::dumpFaceMask(uint16 index, int face, Archive::ResourceType type) {
00459     ResourceDescription maskDesc = _vm->getFileDescription("", index, face, type);
00460 
00461     if (!maskDesc.isValid())
00462         return false;
00463 
00464     Common::SeekableReadStream *maskStream = maskDesc.getData();
00465 
00466     Effect::FaceMask *mask = Effect::loadMask(maskStream);
00467 
00468     delete maskStream;
00469 
00470     Common::DumpFile outFile;
00471     outFile.open(Common::String::format("dump/%d-%d.masku_%d", index, face, type));
00472     outFile.write(mask->surface->getPixels(), mask->surface->pitch * mask->surface->h);
00473     outFile.close();
00474 
00475     delete mask;
00476 
00477     return true;
00478 }
00479 
00480 } // End of namespace Myst3


Generated on Sat Aug 1 2020 05:01:19 for ResidualVM by doxygen 1.7.1
curved edge   curved edge