diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index e85e0aaf4..231ff656d 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -85,7 +85,7 @@ if (BUILD_SUPPORTED) #DFHACK_PLUGIN(advtools advtools.cpp) DFHACK_PLUGIN(autochop autochop.cpp) DFHACK_PLUGIN(autodump autodump.cpp) - DFHACK_PLUGIN(autogems autogems.cpp) + DFHACK_PLUGIN(autogems autogems.cpp LINK_LIBRARIES jsoncpp) DFHACK_PLUGIN(autohauler autohauler.cpp) DFHACK_PLUGIN(autolabor autolabor.cpp) DFHACK_PLUGIN(automaterial automaterial.cpp) diff --git a/plugins/autogems.cpp b/plugins/autogems.cpp index b77c5d249..ee255846d 100644 --- a/plugins/autogems.cpp +++ b/plugins/autogems.cpp @@ -4,9 +4,12 @@ * For best effect, include "enable autogems" in your dfhack.init configuration. */ +#include + #include "uicommon.h" #include "modules/Buildings.h" +#include "modules/Filesystem.h" #include "modules/Gui.h" #include "modules/Job.h" #include "modules/World.h" @@ -19,6 +22,8 @@ #include "df/job_item.h" #include "df/viewscreen_dwarfmodest.h" +#include "jsoncpp-ex.h" + #define CONFIG_KEY "autogems/config" #define DELTA_TICKS 1200 #define MAX_WORKSHOP_JOBS 10 @@ -286,6 +291,44 @@ struct autogem_hook : public df::viewscreen_dwarfmodest { IMPLEMENT_VMETHOD_INTERPOSE(autogem_hook, feed); IMPLEMENT_VMETHOD_INTERPOSE(autogem_hook, render); +bool read_config(color_ostream &out) { + std::string path = "data/save/" + World::ReadWorldFolder() + "/autogems.json"; + if (!Filesystem::isfile(path)) { + // no need to require the config file to exist + return true; + } + + std::ifstream f(path); + Json::Value config; + try { + if (!f.good() || !(f >> config)) { + out.printerr("autogems: failed to read autogems.json\n"); + return false; + } + } + catch (Json::Exception &e) { + out.printerr("autogems: failed to read autogems.json: %s\n", e.what()); + return false; + } + + if (config["blacklist"].isArray()) { + blacklist.clear(); + for (int i = 0; i < int(config["blacklist"].size()); i++) { + Json::Value item = config["blacklist"][i]; + if (item.isInt()) { + blacklist.insert(mat_index(item.asInt())); + } + else { + out.printerr("autogems: illegal item at position %i in blacklist\n", i); + } + } + } + return true; +} + +command_result cmd_reload_config(color_ostream &out, std::vector&) { + return read_config(out) ? CR_OK : CR_FAILURE; +} DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { if (event == DFHack::SC_MAP_LOADED) { @@ -294,6 +337,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan auto config = World::GetPersistentData(CONFIG_KEY); running = config.isValid() && !config.ival(0); last_frame_count = world->frame_counter; + read_config(out); } } else if (event == DFHack::SC_MAP_UNLOADED) { running = false; @@ -313,10 +357,20 @@ DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) { } running = enabled && World::isFortressMode(); + if (running) { + read_config(out); + } return CR_OK; } DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) { + commands.push_back(PluginCommand( + "autogems-reload", + "Reload autogems config file", + cmd_reload_config, + false, + "Reload autogems config file" + )); return CR_OK; } diff --git a/scripts b/scripts index 53abecc96..65b102ed6 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 53abecc969936ad20c372737e1187dac4a54ef9a +Subproject commit 65b102ed6e0fe5b56b20f0c157f5a000e750432b