Fix problem with running interactive commands from hotkeys.

develop
Petr Mrázek 2011-08-06 04:37:29 +02:00
parent 24bdc538e9
commit cb93b5542e
9 changed files with 31 additions and 30 deletions

@ -118,7 +118,11 @@ void fHKthread(void * iodata)
if(!stuff.empty())
{
vector <string> 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");
}
}
}
}

@ -262,7 +262,7 @@ bool Plugin::reload()
return true;
}
command_result Plugin::invoke( std::string & command, std::vector <std::string> & parameters)
command_result Plugin::invoke( std::string & command, std::vector <std::string> & 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 <std::string>
{
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 <std::string> & parameters)
command_result PluginManager::InvokeCommand( std::string & command, std::vector <std::string> & 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 <string, Plugin *>::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;

@ -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 <std::string> &)
command_result (*function_)(Core *, std::vector <std::string> &),
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 <std::string> &);
bool interactive;
};
class Plugin
{
@ -83,7 +91,7 @@ namespace DFHack
bool load();
bool unload();
bool reload();
command_result invoke( std::string & command, std::vector <std::string> & parameters );
command_result invoke( std::string & command, std::vector <std::string> & 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 <std::string> & parameters );
command_result InvokeCommand( std::string & command, std::vector <std::string> & parameters, bool interactive = true );
Plugin* operator[] (std::size_t index)
{
if(index >= all_plugins.size())

@ -31,7 +31,7 @@ DFhackCExport const char * plugin_name ( void )
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &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 <string> & 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();

@ -29,7 +29,7 @@ DFhackCExport const char * plugin_name ( void )
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &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;

@ -150,7 +150,7 @@ DFhackCExport const char * plugin_name ( void )
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &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;
}

@ -22,7 +22,7 @@ DFhackCExport const char * plugin_name ( void )
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &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;
}

@ -208,7 +208,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
{
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;
}

@ -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;