From 13a8e5f822a9a77c5bee1d7f60adfdb32decfb1d Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Thu, 17 Mar 2016 22:29:16 -0400 Subject: TITANIC: Implement debugger with dump and room commands --- engines/titanic/core/tree_item.cpp | 4 +- engines/titanic/debugger.cpp | 175 +++++++++++++++++++++++++++++++++++++ engines/titanic/debugger.h | 91 +++++++++++++++++++ engines/titanic/module.mk | 1 + engines/titanic/titanic.cpp | 25 +++++- engines/titanic/titanic.h | 2 + 6 files changed, 293 insertions(+), 5 deletions(-) create mode 100644 engines/titanic/debugger.cpp create mode 100644 engines/titanic/debugger.h (limited to 'engines/titanic') diff --git a/engines/titanic/core/tree_item.cpp b/engines/titanic/core/tree_item.cpp index b476777d90..39c3fe1f10 100644 --- a/engines/titanic/core/tree_item.cpp +++ b/engines/titanic/core/tree_item.cpp @@ -238,7 +238,7 @@ CNamedItem *CTreeItem::findByName(const CString &name, int maxLen) { CString nameLower = name; nameLower.toLowercase(); - for (CTreeItem *treeItem = this; treeItem; treeItem = scan(treeItem)) { + for (CTreeItem *treeItem = this; treeItem; treeItem = treeItem->scan(this)) { CString nodeName = treeItem->getName(); nodeName.toLowercase(); @@ -246,7 +246,7 @@ CNamedItem *CTreeItem::findByName(const CString &name, int maxLen) { if (nodeName.left(maxLen).compareTo(nameLower)) return dynamic_cast(treeItem); } else { - if (nodeName.compareTo(nameLower)) + if (!nodeName.compareTo(nameLower)) return dynamic_cast(treeItem); } } diff --git a/engines/titanic/debugger.cpp b/engines/titanic/debugger.cpp new file mode 100644 index 0000000000..b759b12846 --- /dev/null +++ b/engines/titanic/debugger.cpp @@ -0,0 +1,175 @@ +/* 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "titanic/debugger.h" +#include "titanic/titanic.h" +#include "titanic/core/tree_item.h" + +namespace Titanic { + +Debugger::Debugger(TitanicEngine *vm) : GUI::Debugger(), _vm(vm) { + registerCmd("continue", WRAP_METHOD(Debugger, cmdExit)); + registerCmd("dump", WRAP_METHOD(Debugger, cmdDump)); + registerCmd("room", WRAP_METHOD(Debugger, cmdRoom)); +} + +int Debugger::strToInt(const char *s) { + if (!*s) + // No string at all + return 0; + else if (toupper(s[strlen(s) - 1]) != 'H') + // Standard decimal string + return atoi(s); + + // Hexadecimal string + uint tmp = 0; + int read = sscanf(s, "%xh", &tmp); + if (read < 1) + error("strToInt failed on string \"%s\"", s); + return (int)tmp; +} + +CRoomItem *Debugger::findRoom(const char *name) { + CTreeItem *root = g_vm->_window->_gameManager->_project; + CRoomItem *roomItem = dynamic_cast(root->findByName(name)); + if (roomItem) + return roomItem; + + int roomNumber = strToInt(name); + for (CTreeItem *treeItem = root; treeItem; treeItem = treeItem->scan(root)) { + roomItem = dynamic_cast(treeItem); + if (roomItem && roomItem->_roomNumber == roomNumber) + return roomItem; + } + + return nullptr; +} + +CNodeItem *Debugger::findNode(CRoomItem *room, const char *name) { + CNodeItem *nodeItem = dynamic_cast(room->findByName(name)); + if (nodeItem) + return nodeItem; + + int nodeNumber = strToInt(name); + nodeItem = dynamic_cast(room->findChildInstanceOf(CNodeItem::_type)); + while (nodeItem) { + if (nodeItem->_nodeNumber == nodeNumber) + return nodeItem; + + nodeItem = dynamic_cast(room->findNextInstanceOf(CNodeItem::_type, nodeItem)); + } + + return nullptr; +} + +CViewItem *Debugger::findView(CNodeItem *node, const char *name) { + CViewItem *viewItem = dynamic_cast(node->findByName(name)); + if (viewItem) + return viewItem; + + int viewNumber = strToInt(name); + viewItem = dynamic_cast(node->findChildInstanceOf(CViewItem::_type)); + while (viewItem) { + if (viewItem->_viewNumber == viewNumber) + return viewItem; + + viewItem = dynamic_cast(node->findNextInstanceOf(CViewItem::_type, viewItem)); + } + + return nullptr; +} + +void Debugger::listRooms() { + CTreeItem *root = g_vm->_window->_gameManager->_project; + + for (CTreeItem *treeItem = root; treeItem; treeItem = treeItem->scan(root)) { + CRoomItem *roomItem = dynamic_cast(treeItem); + if (roomItem) + debugPrintf("%s\n", roomItem->_name.c_str()); + } +} + +void Debugger::listRoom(CRoomItem *room) { + for (CTreeItem *treeItem = room; treeItem; treeItem = treeItem->scan(room)) { + CNodeItem *nodeItem = dynamic_cast(treeItem); + if (nodeItem) + debugPrintf("%s\n", nodeItem->_name.c_str()); + } +} + +void Debugger::listNode(CNodeItem *node) { + for (CTreeItem *treeItem = node; treeItem; treeItem = treeItem->scan(node)) { + CViewItem *viewItem = dynamic_cast(treeItem); + if (viewItem) + debugPrintf("%s\n", viewItem->_name.c_str()); + } +} + +bool Debugger::cmdDump(int argc, const char **argv) { + // Get the starting node + CTreeItem *root = g_vm->_window->_gameManager->_project; + if (argc == 2) + root = root->findByName(argv[1]); + + if (root == nullptr) { + debugPrintf("Could not find item\n"); + } else { + root->dump(0); + debugPrintf("Item and it's content were dumped to stdout\n"); + } + + return true; +} + +bool Debugger::cmdRoom(int argc, const char **argv) { + if (argc == 1) { + listRooms(); + } else if (argc >= 2) { + CRoomItem *roomItem = findRoom(argv[1]); + + if (!roomItem) + debugPrintf("Could not find room - %s\n", argv[1]); + else if (argc == 2) + listRoom(roomItem); + else { + CNodeItem *nodeItem = findNode(roomItem, argv[2]); + + if (!nodeItem) + debugPrintf("Could not find node - %s\n", argv[2]); + else if (argc == 3) + listNode(nodeItem); + else { + CViewItem *viewItem = findView(nodeItem, argv[3]); + + if (!viewItem) + debugPrintf("Could not find view - %s\n", argv[3]); + else { + debugPrintf("Found view. TODO: Jump to it\n"); + } + } + } + } + + return true; +} + +} // End of namespace Titanic diff --git a/engines/titanic/debugger.h b/engines/titanic/debugger.h new file mode 100644 index 0000000000..2f3bb91a46 --- /dev/null +++ b/engines/titanic/debugger.h @@ -0,0 +1,91 @@ +/* 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef TITANIC_DEBUGGER_H +#define TITANIC_DEBUGGER_H + +#include "common/scummsys.h" +#include "gui/debugger.h" +#include "titanic/core/room_item.h" +#include "titanic/core/node_item.h" +#include "titanic/core/view_item.h" + +namespace Titanic { + +class TitanicEngine; + +class Debugger : public GUI::Debugger { +private: + /** + * Converts a decimal or hexadecimal string into a number + */ + int strToInt(const char *s); + + /** + * Find a room by name or number + */ + CRoomItem *findRoom(const char *name); + + /** + * Find a node within a room by name or number + */ + CNodeItem *findNode(CRoomItem *room, const char *name); + + /** + * Find a view within a room node by name or number + */ + CViewItem *findView(CNodeItem *node, const char *name); + + /** + * List all the rooms in the game + */ + void listRooms(); + + /** + * List the nodes within a room + */ + void listRoom(CRoomItem *room); + + /** + * List the views within a room node + */ + void listNode(CNodeItem *node); + + /** + * Dump a portion of the game project + */ + bool cmdDump(int argc, const char **argv); + + /** + * List room details, or jump to a specific view + */ + bool cmdRoom(int argc, const char **argv); +protected: + TitanicEngine *_vm; +public: + Debugger(TitanicEngine *vm); + virtual ~Debugger() {} +}; + +} // End of namespace Titanic + +#endif /* TITANIC_DEBUGGER_H */ diff --git a/engines/titanic/module.mk b/engines/titanic/module.mk index 23ac75056d..7f9adaa0b4 100644 --- a/engines/titanic/module.mk +++ b/engines/titanic/module.mk @@ -1,6 +1,7 @@ MODULE := engines/titanic MODULE_OBJS := \ + debugger.o \ detection.o \ direct_draw.o \ files_manager.o \ diff --git a/engines/titanic/titanic.cpp b/engines/titanic/titanic.cpp index e7bbd84c34..e5417c1e7d 100644 --- a/engines/titanic/titanic.cpp +++ b/engines/titanic/titanic.cpp @@ -29,6 +29,7 @@ #include "graphics/scaler.h" #include "graphics/thumbnail.h" #include "titanic/titanic.h" +#include "titanic/debugger.h" #include "titanic/carry/hose.h" #include "titanic/core/saveable_object.h" #include "titanic/game/get_lift_eye2.h" @@ -47,11 +48,13 @@ TitanicEngine::TitanicEngine(OSystem *syst, const TitanicGameDescription *gameDe : _gameDescription(gameDesc), Engine(syst), _randomSource("Titanic"), _ticksCount(0), _frameCounter(0) { g_vm = this; + _debugger = nullptr; _window = nullptr; _screenManager = nullptr; } TitanicEngine::~TitanicEngine() { + delete _debugger; delete _window; delete _screenManager; CSaveableObject::freeClassList(); @@ -79,6 +82,7 @@ void TitanicEngine::initialize() { CExitPellerator::init(); CEnterExitSecClassMiniLift::init(); + _debugger = new Debugger(this); _screenManager = new OSScreenManager(this); _window = new CMainGameWindow(this); _window->applicationStarting(); @@ -108,9 +112,24 @@ Common::Error TitanicEngine::run() { } void TitanicEngine::processEvents() { - Common::Event evt; - g_system->getEventManager()->pollEvent(evt); - + Common::Event event; + g_system->getEventManager()->pollEvent(event); + + // Give time to the debugger + _debugger->onFrame(); + + switch (event.type) { + case Common::EVENT_KEYDOWN: + if (event.kbd.keycode == Common::KEYCODE_d && (event.kbd.flags & Common::KBD_CTRL)) { + // Attach to the debugger + _debugger->attach(); + _debugger->onFrame(); + } + break; + + default: + break; + } } } // End of namespace Titanic diff --git a/engines/titanic/titanic.h b/engines/titanic/titanic.h index c5c642629f..c2a66fbc03 100644 --- a/engines/titanic/titanic.h +++ b/engines/titanic/titanic.h @@ -29,6 +29,7 @@ #include "common/serializer.h" #include "engines/advancedDetector.h" #include "engines/engine.h" +#include "titanic/debugger.h" #include "titanic/files_manager.h" #include "titanic/screen_manager.h" #include "titanic/main_game_window.h" @@ -97,6 +98,7 @@ protected: virtual bool hasFeature(EngineFeature f) const; public: CFilesManager _filesManager; + Debugger *_debugger; OSScreenManager *_screenManager; CMainGameWindow *_window; Common::RandomSource _randomSource; -- cgit v1.2.3