From 445b580ad148767f7b623e156f0d9b8b1284b4d9 Mon Sep 17 00:00:00 2001 From: Clayton Hughes Date: Sun, 11 Mar 2012 23:31:29 -0700 Subject: [PATCH 1/5] Added 'script' command to load DFHack scripts from file. Syntax is the same as dfhack.init / interactive. --- library/Core.cpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index 82788aff6..1b8d5388f 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -68,6 +68,10 @@ using namespace tthread; using namespace df::enums; using df::global::init; +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 { Cond() @@ -372,6 +376,7 @@ static void runInteractiveCommand(Core *core, PluginManager *plug_mgr, int &clue " die - Force DF to close immediately\n" " keybinding - Modify bindings of commands to keys\n" " belongs COMMAND - Tell which plugin a command belongs to.\n" + " script FILENAME - Run the commands specified in a file.\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" " unload PLUGIN|all - Unload a plugin or all loaded plugins.\n" @@ -467,6 +472,18 @@ static void runInteractiveCommand(Core *core, PluginManager *plug_mgr, int &clue { _exit(666); } + else if(first == "script") + { + if(parts.size() == 1) + { + loadScriptFile(core, plug_mgr, parts[0]); + } + else + { + con << "Usage:" << endl + << " script " << endl; + } + } else { command_result res = plug_mgr->InvokeCommand(first, parts); @@ -479,15 +496,19 @@ 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) { - ifstream init(fname); - if (init.bad()) + core->con << "Loading script at " << fname << std::endl; + ifstream script(fname); + if (script.bad()) + { + core->con.printerr("Error loading script\n"); return; + } int tmp = 0; string command; - while (getline(init, command)) + while (getline(script, command)) { if (!command.empty()) runInteractiveCommand(core, plug_mgr, tmp, command); @@ -511,7 +532,7 @@ void fIOthread(void * iodata) return; } - loadInitFile(core, plug_mgr, "dfhack.init"); + loadScriptFile(core, plug_mgr, "dfhack.init"); con.print("DFHack is ready. Have a nice day!\n" "Type in '?' or 'help' for general help, 'ls' to see all commands.\n"); From 7dff12ead7651aec8189bc213d1222a5663a682c Mon Sep 17 00:00:00 2001 From: Clayton Hughes Date: Mon, 12 Mar 2012 00:05:22 -0700 Subject: [PATCH 2/5] Added "workflow clear all" command to remove all constraints. --- plugins/workflow.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/plugins/workflow.cpp b/plugins/workflow.cpp index a8ace1b02..9040ffc1a 100644 --- a/plugins/workflow.cpp +++ b/plugins/workflow.cpp @@ -701,6 +701,19 @@ static ItemConstraint *get_constraint(Core *c, const std::string &str, Persisten return nct; } +static void delete_all_constraints(Core *c) +{ + for(std::vector::iterator iter = constraints.begin(); + iter != constraints.end(); + ++iter) + { + c->getWorld()->DeletePersistentData((*iter)->config); + delete (*iter); + } + + constraints.clear(); +} + static void delete_constraint(Core *c, ItemConstraint *cv) { int idx = linear_index(constraints, cv); @@ -1557,6 +1570,15 @@ static command_result workflow_cmd(Core *c, vector & parameters) c->con.printerr("Constraint not found: %s\n", parameters[1].c_str()); return CR_FAILURE; } + else if (cmd == "clear") + { + if(parameters.size() == 1 && parameters[0] == "all") + { + delete_all_constraints(c); + return CR_OK; + } + return CR_WRONG_USAGE; + } else return CR_WRONG_USAGE; } From 708dc6c32d2b874aab3305894345eeefaec91a96 Mon Sep 17 00:00:00 2001 From: Clayton Hughes Date: Mon, 12 Mar 2012 00:10:55 -0700 Subject: [PATCH 3/5] Forgot usage message for "clear all" --- plugins/workflow.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/workflow.cpp b/plugins/workflow.cpp index 9040ffc1a..e32830185 100644 --- a/plugins/workflow.cpp +++ b/plugins/workflow.cpp @@ -83,6 +83,8 @@ DFhackCExport command_result plugin_init (Core *c, std::vector & " Set a constraint. The first form counts each stack as only 1 item.\n" " workflow unlimit \n" " Delete a constraint.\n" + " workflow clear all\n" + " Deletes all constraints. Be sure you want to do this.\n" "Function:\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" From d7f7437ca1a7b056795cd98e92194604e2c17937 Mon Sep 17 00:00:00 2001 From: Clayton Hughes Date: Mon, 12 Mar 2012 00:32:43 -0700 Subject: [PATCH 4/5] "clear all" wasn't working properly - I'm bad at counting. --- plugins/workflow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/workflow.cpp b/plugins/workflow.cpp index e32830185..3f35745d2 100644 --- a/plugins/workflow.cpp +++ b/plugins/workflow.cpp @@ -1573,8 +1573,8 @@ static command_result workflow_cmd(Core *c, vector & parameters) return CR_FAILURE; } else if (cmd == "clear") - { - if(parameters.size() == 1 && parameters[0] == "all") + { + if(parameters.size() == 2 && parameters[1] == "all") { delete_all_constraints(c); return CR_OK; From 4cb8995a0558d0ccf316584ca81f304ad976da37 Mon Sep 17 00:00:00 2001 From: Clayton Hughes Date: Mon, 12 Mar 2012 00:33:59 -0700 Subject: [PATCH 5/5] Fixed script loading improperly checking for errors. Also closed the file for good measure. I couldn't find any documentation that said that ~ifstream() did this. --- library/Core.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index 1b8d5388f..82c8c7933 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -500,19 +500,22 @@ static void loadScriptFile(Core *core, PluginManager *plug_mgr, string fname) { core->con << "Loading script at " << fname << std::endl; ifstream script(fname); - if (script.bad()) + if (script.good()) { - core->con.printerr("Error loading script\n"); - return; + int tmp = 0; + string command; + while (getline(script, command)) + { + if (!command.empty()) + runInteractiveCommand(core, plug_mgr, tmp, command); + } } - - int tmp = 0; - string command; - while (getline(script, command)) + else { - if (!command.empty()) - runInteractiveCommand(core, plug_mgr, tmp, command); + core->con.printerr("Error loading script\n"); } + + script.close(); } // A thread function... for the interactive console.