Skip to content

Commit

Permalink
Merge remote-tracking branch 'myk002/buildingplan_persist_global_sett…
Browse files Browse the repository at this point in the history
…ings' into develop
  • Loading branch information
lethosor committed Nov 12, 2020
2 parents 9c13b49 + 72b6ac7 commit 923b1b1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
63 changes: 61 additions & 2 deletions plugins/buildingplan-planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -535,7 +594,7 @@ void Planner::reset()
planned_buildings.clear();
tasks.clear();

init_global_settings(global_settings);
config = init_global_settings(global_settings);

migrateV1ToV2();

Expand Down
1 change: 1 addition & 0 deletions plugins/buildingplan-planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Planner
void doCycle();

private:
DFHack::PersistentDataItem config;
std::map<std::string, bool> global_settings;
std::unordered_map<BuildingTypeKey,
std::vector<ItemFilter>,
Expand Down
4 changes: 3 additions & 1 deletion plugins/buildingplan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "modules/Maps.h"
#include "modules/World.h"

#include "Core.h"
#include "LuaTools.h"
#include "PluginManager.h"

Expand Down Expand Up @@ -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) ||
Expand Down

0 comments on commit 923b1b1

Please sign in to comment.