From cb93b5542ec20a8660800f98f1b090af3623e100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 6 Aug 2011 04:37:29 +0200 Subject: [PATCH] Fix problem with running interactive commands from hotkeys. --- library/Core.cpp | 6 +++++- library/PluginManager.cpp | 12 ++++++++---- library/include/dfhack/PluginManager.h | 14 +++++++++++--- plugins/cleanowned.cpp | 17 +---------------- plugins/kittens.cpp | 2 +- plugins/liquids.cpp | 2 +- plugins/mode.cpp | 2 +- plugins/tiles.cpp | 2 +- plugins/tiletypes.cpp | 4 ++-- 9 files changed, 31 insertions(+), 30 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index e3f0263a7..02ef3fbfb 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -118,7 +118,11 @@ void fHKthread(void * iodata) if(!stuff.empty()) { vector crap; - plug_mgr->InvokeCommand(stuff, crap); + command_result cr = plug_mgr->InvokeCommand(stuff, crap, false); + if(cr == CR_WOULD_BREAK) + { + core->con.printerr("It isn't possible to run an interactive command outside the console.\n"); + } } } } diff --git a/library/PluginManager.cpp b/library/PluginManager.cpp index 9324c20db..0a553ae63 100644 --- a/library/PluginManager.cpp +++ b/library/PluginManager.cpp @@ -262,7 +262,7 @@ bool Plugin::reload() return true; } -command_result Plugin::invoke( std::string & command, std::vector & parameters) +command_result Plugin::invoke( std::string & command, std::vector & parameters, bool interactive_) { Core & c = Core::getInstance(); command_result cr = CR_NOT_IMPLEMENTED; @@ -273,7 +273,11 @@ command_result Plugin::invoke( std::string & command, std::vector { if(commands[i].name == command) { - cr = commands[i].function(&c, parameters); + // running interactive things from some other source than the console would break it + if(!interactive_ && commands[i].interactive) + cr = CR_WOULD_BREAK; + else + cr = commands[i].function(&c, parameters); break; } } @@ -345,7 +349,7 @@ Plugin *PluginManager::getPluginByName (const std::string & name) } // FIXME: handle name collisions... -command_result PluginManager::InvokeCommand( std::string & command, std::vector & parameters) +command_result PluginManager::InvokeCommand( std::string & command, std::vector & parameters, bool interactive) { command_result cr = CR_NOT_IMPLEMENTED; Core * c = &Core::getInstance(); @@ -353,7 +357,7 @@ command_result PluginManager::InvokeCommand( std::string & command, std::vector map ::iterator iter = belongs.find(command); if(iter != belongs.end()) { - cr = iter->second->invoke(command, parameters); + cr = iter->second->invoke(command, parameters, interactive); } cmdlist_mutex->unlock(); return cr; diff --git a/library/include/dfhack/PluginManager.h b/library/include/dfhack/PluginManager.h index 660675bd9..fef1aea50 100644 --- a/library/include/dfhack/PluginManager.h +++ b/library/include/dfhack/PluginManager.h @@ -41,30 +41,38 @@ namespace DFHack class PluginManager; enum command_result { + CR_WOULD_BREAK = -2, CR_NOT_IMPLEMENTED = -1, CR_FAILURE = 0, CR_OK = 1 }; struct PluginCommand { + /// create a command with a name, description, function pointer to its code + /// and saying if it needs an interactive terminal + /// Most commands shouldn't require an interactive terminal! PluginCommand(const char * _name, const char * _description, - command_result (*function_)(Core *, std::vector &) + command_result (*function_)(Core *, std::vector &), + bool interactive_ = false ) { name = _name; description = _description; function = function_; + interactive = interactive_; } PluginCommand (const PluginCommand & rhs) { name = rhs.name; description = rhs.description; function = rhs.function; + interactive = rhs.interactive; } std::string name; std::string description; command_result (*function)(Core *, std::vector &); + bool interactive; }; class Plugin { @@ -83,7 +91,7 @@ namespace DFHack bool load(); bool unload(); bool reload(); - command_result invoke( std::string & command, std::vector & parameters ); + command_result invoke( std::string & command, std::vector & parameters, bool interactive ); plugin_state getState () const; const PluginCommand& operator[] (std::size_t index) const { @@ -123,7 +131,7 @@ namespace DFHack // PUBLIC METHODS public: Plugin *getPluginByName (const std::string & name); - command_result InvokeCommand( std::string & command, std::vector & parameters ); + command_result InvokeCommand( std::string & command, std::vector & parameters, bool interactive = true ); Plugin* operator[] (std::size_t index) { if(index >= all_plugins.size()) diff --git a/plugins/cleanowned.cpp b/plugins/cleanowned.cpp index a4e367917..eea6d3af9 100644 --- a/plugins/cleanowned.cpp +++ b/plugins/cleanowned.cpp @@ -31,7 +31,7 @@ DFhackCExport const char * plugin_name ( void ) DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) { commands.clear(); - commands.push_back(PluginCommand("vlub", + commands.push_back(PluginCommand("cleanowned", "Confiscates and dumps garbage owned by dwarfs.", df_cleanowned)); return CR_OK; @@ -195,23 +195,8 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete c->con.print("(unsuccessfully) "); if (dump) itm.base->flags.dump = 1; - - // NO-OP really - //Items->writeItem(itm); } c->con.print("\n"); -/* - printf( - "%5d: %08x %08x (%d,%d,%d) #%08x [%d] %s - %s %s\n", - i, itm.origin, itm.base.flags.whole, - itm.base.x, itm.base.y, itm.base.z, - itm.base.vtable, - itm.wear_level, - Items->getItemClass(itm.matdesc.itemType).c_str(), - Items->getItemDescription(itm, Materials).c_str(), - info.c_str() - ); - */ } } c->Resume(); diff --git a/plugins/kittens.cpp b/plugins/kittens.cpp index 80e2f412b..6becd3763 100644 --- a/plugins/kittens.cpp +++ b/plugins/kittens.cpp @@ -29,7 +29,7 @@ DFhackCExport const char * plugin_name ( void ) DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) { commands.clear(); - commands.push_back(PluginCommand("nyan","NYAN CAT INVASION!",kittens)); + commands.push_back(PluginCommand("nyan","NYAN CAT INVASION!",kittens, true)); commands.push_back(PluginCommand("ktimer","Measure time between game updates and console lag.",ktimer)); commands.push_back(PluginCommand("blockflags","Look up block flags",bflags)); return CR_OK; diff --git a/plugins/liquids.cpp b/plugins/liquids.cpp index bf796eb65..7acfc1b00 100644 --- a/plugins/liquids.cpp +++ b/plugins/liquids.cpp @@ -150,7 +150,7 @@ DFhackCExport const char * plugin_name ( void ) DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) { commands.clear(); - commands.push_back(PluginCommand("liquids", "Place magma, water or obsidian.", df_liquids)); + commands.push_back(PluginCommand("liquids", "Place magma, water or obsidian.", df_liquids, true)); return CR_OK; } diff --git a/plugins/mode.cpp b/plugins/mode.cpp index 0a442be89..1ae336b93 100644 --- a/plugins/mode.cpp +++ b/plugins/mode.cpp @@ -22,7 +22,7 @@ DFhackCExport const char * plugin_name ( void ) DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) { commands.clear(); - commands.push_back(PluginCommand("mode","View, change and track game mode.",mode)); + commands.push_back(PluginCommand("mode","View, change and track game mode.", mode, true)); return CR_OK; } diff --git a/plugins/tiles.cpp b/plugins/tiles.cpp index 4e51270bb..fc83f1891 100644 --- a/plugins/tiles.cpp +++ b/plugins/tiles.cpp @@ -208,7 +208,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector { commands.clear(); commands.push_back(PluginCommand("tiles", "A tile painter. See 'tile help' for details.", df_tiles)); - commands.push_back(PluginCommand("paint", "Paint with the last used brush.", df_paint)); + commands.push_back(PluginCommand("paint", "Paint with the current tiles settings.", df_paint)); return CR_OK; } diff --git a/plugins/tiletypes.cpp b/plugins/tiletypes.cpp index 7b031f34c..43607958c 100644 --- a/plugins/tiletypes.cpp +++ b/plugins/tiletypes.cpp @@ -124,7 +124,7 @@ bool processTileType(TileType &paint, const std::string &option, const std::stri if (option == "shape" || option == "sh" || option == "s") { - if (valInt >= -1 && valInt << DFHack::tileshape_count) + if (valInt >= -1 && valInt < DFHack::tileshape_count) { paint.shape = (DFHack::TileShape) valInt; found = true; @@ -174,7 +174,7 @@ bool processTileType(TileType &paint, const std::string &option, const std::stri } else if (option == "special" || option == "sp") { - if (valInt >= -1 && valInt << DFHack::tilespecial_count) + if (valInt >= -1 && valInt < DFHack::tilespecial_count) { paint.special = (DFHack::TileSpecial) valInt; found = true;