autogems: load blacklist from autogems.json

Closes #1027
develop
lethosor 2018-05-05 12:46:10 -04:00
parent 60d1c270c2
commit 9d7feaf39d
3 changed files with 56 additions and 2 deletions

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

@ -4,9 +4,12 @@
* For best effect, include "enable autogems" in your dfhack.init configuration.
*/
#include <fstream>
#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<std::string>&) {
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 <PluginCommand> &commands) {
commands.push_back(PluginCommand(
"autogems-reload",
"Reload autogems config file",
cmd_reload_config,
false,
"Reload autogems config file"
));
return CR_OK;
}

@ -1 +1 @@
Subproject commit 53abecc969936ad20c372737e1187dac4a54ef9a
Subproject commit 65b102ed6e0fe5b56b20f0c157f5a000e750432b