Merge remote-tracking branch 'myk002/buildingplan_persist_global_settings' into develop

develop
lethosor 2020-11-11 23:59:09 -05:00
commit 923b1b14f3
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
3 changed files with 65 additions and 3 deletions

@ -20,6 +20,7 @@
static const std::string planned_building_persistence_key_v1 = "buildingplan/constraints";
static const std::string planned_building_persistence_key_v2 = "buildingplan/constraints2";
static const std::string global_settings_persistence_key = "buildingplan/global";
/*
* ItemFilter
@ -501,13 +502,69 @@ void migrateV1ToV2()
}
}
static void init_global_settings(std::map<std::string, bool> & settings)
// assumes no setting has '=' or '|' characters
static std::string serialize_settings(std::map<std::string, bool> & settings)
{
std::ostringstream ser;
for (auto & entry : settings)
{
ser << entry.first << "=" << (entry.second ? "1" : "0") << "|";
}
return ser.str();
}
static void deserialize_settings(std::map<std::string, bool> & settings,
std::string ser)
{
std::vector<std::string> tokens;
split_string(&tokens, ser, "|");
for (auto token : tokens)
{
if (token.empty())
continue;
std::vector<std::string> parts;
split_string(&parts, token, "=");
if (parts.size() != 2)
{
debug("invalid serialized setting format: '%s'", token.c_str());
continue;
}
std::string key = parts[0];
if (settings.count(key) == 0)
{
debug("unknown serialized setting: '%s", key.c_str());
continue;
}
settings[key] = static_cast<bool>(atoi(parts[1].c_str()));
debug("deserialized setting: %s = %d", key.c_str(), settings[key]);
}
}
static DFHack::PersistentDataItem init_global_settings(
std::map<std::string, bool> & settings)
{
settings.clear();
settings["blocks"] = true;
settings["boulders"] = true;
settings["logs"] = true;
settings["bars"] = false;
// load persistent global settings if they exist; otherwise create them
std::vector<PersistentDataItem> items;
DFHack::World::GetPersistentData(&items, global_settings_persistence_key);
if (items.size() == 1)
{
DFHack::PersistentDataItem & config = items[0];
deserialize_settings(settings, config.val());
return config;
}
debug("initializing persistent global settings");
DFHack::PersistentDataItem config =
DFHack::World::AddPersistentData(global_settings_persistence_key);
config.val() = serialize_settings(settings);
return config;
}
const std::map<std::string, bool> & Planner::getGlobalSettings() const
@ -525,6 +582,8 @@ bool Planner::setGlobalSetting(std::string name, bool value)
debug("global setting '%s' %d -> %d",
name.c_str(), global_settings[name], value);
global_settings[name] = value;
if (config.isValid())
config.val() = serialize_settings(global_settings);
return true;
}
@ -535,7 +594,7 @@ void Planner::reset()
planned_buildings.clear();
tasks.clear();
init_global_settings(global_settings);
config = init_global_settings(global_settings);
migrateV1ToV2();

@ -120,6 +120,7 @@ public:
void doCycle();
private:
DFHack::PersistentDataItem config;
std::map<std::string, bool> global_settings;
std::unordered_map<BuildingTypeKey,
std::vector<ItemFilter>,

@ -8,6 +8,7 @@
#include "modules/Maps.h"
#include "modules/World.h"
#include "Core.h"
#include "LuaTools.h"
#include "PluginManager.h"
@ -927,7 +928,8 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
if (enable != is_enabled)
{
planner.reset();
if (DFHack::Core::getInstance().isMapLoaded())
planner.reset();
if (!INTERPOSE_HOOK(buildingplan_query_hook, feed).apply(enable) ||
!INTERPOSE_HOOK(buildingplan_place_hook, feed).apply(enable) ||