Conflicts:
	library/Core.cpp
	plugins/workflow.cpp

Just had to fix a few minor things.
develop
Petr Mrázek 2012-03-14 01:52:58 +01:00
commit d75908b95c
2 changed files with 64 additions and 13 deletions

@ -67,6 +67,12 @@ using namespace tthread;
using namespace df::enums; using namespace df::enums;
using df::global::init; using df::global::init;
// FIXME: A lot of code in one file, all doing different things... there's something fishy about it.
static void loadScriptFile(Core *core, PluginManager *plug_mgr, string fname);
static void runInteractiveCommand(Core *core, PluginManager *plug_mgr, int &clueless_counter, const string &command);
static bool parseKeySpec(std::string keyspec, int *psym, int *pmod);
struct Core::Cond struct Core::Cond
{ {
Cond() Cond()
@ -371,6 +377,7 @@ static void runInteractiveCommand(Core *core, PluginManager *plug_mgr, int &clue
" fpause - Force DF to pause.\n" " fpause - Force DF to pause.\n"
" die - Force DF to close immediately\n" " die - Force DF to close immediately\n"
" keybinding - Modify bindings of commands to keys\n" " keybinding - Modify bindings of commands to keys\n"
" script FILENAME - Run the commands specified in a file.\n"
" plug [PLUGIN|v] - List plugin state and detailed description.\n" " plug [PLUGIN|v] - List plugin state and detailed description.\n"
" load PLUGIN|all - Load a plugin by name or load all possible plugins.\n" " load PLUGIN|all - Load a plugin by name or load all possible plugins.\n"
" unload PLUGIN|all - Unload a plugin or all loaded plugins.\n" " unload PLUGIN|all - Unload a plugin or all loaded plugins.\n"
@ -466,6 +473,18 @@ static void runInteractiveCommand(Core *core, PluginManager *plug_mgr, int &clue
{ {
_exit(666); _exit(666);
} }
else if(first == "script")
{
if(parts.size() == 1)
{
loadScriptFile(core, plug_mgr, parts[0]);
}
else
{
con << "Usage:" << endl
<< " script <filename>" << endl;
}
}
else else
{ {
command_result res = plug_mgr->InvokeCommand(con, first, parts); command_result res = plug_mgr->InvokeCommand(con, first, parts);
@ -478,20 +497,27 @@ static void runInteractiveCommand(Core *core, PluginManager *plug_mgr, int &clue
} }
} }
static void loadInitFile(Core *core, PluginManager *plug_mgr, string fname) static void loadScriptFile(Core *core, PluginManager *plug_mgr, string fname)
{
core->getConsole() << "Loading script at " << fname << std::endl;
ifstream script(fname);
if (script.good())
{ {
ifstream init(fname);
if (init.bad())
return;
int tmp = 0; int tmp = 0;
string command; string command;
while (getline(init, command)) while (getline(script, command))
{ {
if (!command.empty()) if (!command.empty())
runInteractiveCommand(core, plug_mgr, tmp, command); runInteractiveCommand(core, plug_mgr, tmp, command);
} }
} }
else
{
core->getConsole().printerr("Error loading script\n");
}
script.close();
}
// A thread function... for the interactive console. // A thread function... for the interactive console.
void fIOthread(void * iodata) void fIOthread(void * iodata)
@ -510,7 +536,7 @@ void fIOthread(void * iodata)
return; return;
} }
loadInitFile(core, plug_mgr, "dfhack.init"); loadScriptFile(core, plug_mgr, "dfhack.init");
con.print("DFHack is ready. Have a nice day!\n" con.print("DFHack is ready. Have a nice day!\n"
"Type in '?' or 'help' for general help, 'ls' to see all commands.\n"); "Type in '?' or 'help' for general help, 'ls' to see all commands.\n");

@ -83,6 +83,8 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector <Plugi
" Set a constraint. The first form counts each stack as only 1 item.\n" " Set a constraint. The first form counts each stack as only 1 item.\n"
" workflow unlimit <constraint-spec>\n" " workflow unlimit <constraint-spec>\n"
" Delete a constraint.\n" " Delete a constraint.\n"
" workflow clear all\n"
" Deletes all constraints. Be sure you want to do this.\n"
"Function:\n" "Function:\n"
" - When the plugin is enabled, it protects all repeat jobs from removal.\n" " - When the plugin is enabled, it protects all repeat jobs from removal.\n"
" If they do disappear due to any cause, they are immediately re-added\n" " If they do disappear due to any cause, they are immediately re-added\n"
@ -705,7 +707,21 @@ static ItemConstraint *get_constraint(color_ostream &out, const std::string &str
return nct; return nct;
} }
static void delete_constraint(color_ostream &out, ItemConstraint *cv) static void delete_all_constraints()
{
DFHack::World * w = Core::getInstance().getWorld();
for(std::vector<ItemConstraint*>::iterator iter = constraints.begin();
iter != constraints.end();
++iter)
{
w->DeletePersistentData((*iter)->config);
delete (*iter);
}
constraints.clear();
}
static void delete_constraint(ItemConstraint *cv)
{ {
int idx = linear_index(constraints, cv); int idx = linear_index(constraints, cv);
if (idx >= 0) if (idx >= 0)
@ -1554,13 +1570,22 @@ static command_result workflow_cmd(color_ostream &out, vector <string> & paramet
if (constraints[i]->config.val() != parameters[1]) if (constraints[i]->config.val() != parameters[1])
continue; continue;
delete_constraint(out, constraints[i]); delete_constraint(constraints[i]);
return CR_OK; return CR_OK;
} }
out.printerr("Constraint not found: %s\n", parameters[1].c_str()); out.printerr("Constraint not found: %s\n", parameters[1].c_str());
return CR_FAILURE; return CR_FAILURE;
} }
else if (cmd == "clear")
{
if(parameters.size() == 2 && parameters[1] == "all")
{
delete_all_constraints();
return CR_OK;
}
return CR_WRONG_USAGE;
}
else else
return CR_WRONG_USAGE; return CR_WRONG_USAGE;
} }