Merge pull request #2828 from myk002/myk_best_practices

update auto plugin example, use new best practices
develop
Myk 2023-02-06 09:16:05 -08:00 committed by GitHub
commit 1e03582fee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 27 deletions

@ -143,6 +143,8 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
DEBUG(status,out).print("%s from the API; persisting\n", DEBUG(status,out).print("%s from the API; persisting\n",
is_enabled ? "enabled" : "disabled"); is_enabled ? "enabled" : "disabled");
set_config_bool(config, CONFIG_IS_ENABLED, is_enabled); set_config_bool(config, CONFIG_IS_ENABLED, is_enabled);
if (enable)
do_cycle(out, true);
} else { } else {
DEBUG(status,out).print("%s from the API, but already %s; no action\n", DEBUG(status,out).print("%s from the API, but already %s; no action\n",
is_enabled ? "enabled" : "disabled", is_enabled ? "enabled" : "disabled",

@ -90,6 +90,8 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
is_enabled = enable; is_enabled = enable;
DEBUG(status, out).print("%s from the API; persisting\n", is_enabled ? "enabled" : "disabled"); DEBUG(status, out).print("%s from the API; persisting\n", is_enabled ? "enabled" : "disabled");
set_config_bool(CONFIG_IS_ENABLED, is_enabled); set_config_bool(CONFIG_IS_ENABLED, is_enabled);
if (enable)
do_cycle(out);
} }
else else
{ {

@ -6,6 +6,7 @@
// savegame that had this plugin enabled is loaded. // savegame that had this plugin enabled is loaded.
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
#include "df/world.h" #include "df/world.h"
@ -18,6 +19,7 @@
#include "modules/World.h" #include "modules/World.h"
using std::string; using std::string;
using std::unordered_map;
using std::vector; using std::vector;
using namespace DFHack; using namespace DFHack;
@ -38,25 +40,50 @@ namespace DFHack {
} }
static const string CONFIG_KEY = string(plugin_name) + "/config"; static const string CONFIG_KEY = string(plugin_name) + "/config";
static const string ELEM_CONFIG_KEY_PREFIX = string(plugin_name) + "/elem/";
static PersistentDataItem config; static PersistentDataItem config;
static unordered_map<int, PersistentDataItem> elems;
enum ConfigValues { enum ConfigValues {
CONFIG_IS_ENABLED = 0, CONFIG_IS_ENABLED = 0,
CONFIG_SOMETHING_ELSE = 1, CONFIG_SOMETHING_ELSE = 1,
}; };
static int get_config_val(int index) {
if (!config.isValid()) enum ElemConfigValues {
ELEM_CONFIG_ID = 0,
ELEM_CONFIG_SOMETHING_ELSE = 1,
};
static int get_config_val(PersistentDataItem &c, int index) {
if (!c.isValid())
return -1; return -1;
return config.ival(index); return c.ival(index);
} }
static bool get_config_bool(int index) { static bool get_config_bool(PersistentDataItem &c, int index) {
return get_config_val(index) == 1; return get_config_val(c, index) == 1;
} }
static void set_config_val(int index, int value) { static void set_config_val(PersistentDataItem &c, int index, int value) {
if (config.isValid()) if (c.isValid())
config.ival(index) = value; c.ival(index) = value;
} }
static void set_config_bool(int index, bool value) { static void set_config_bool(PersistentDataItem &c, int index, bool value) {
set_config_val(index, value ? 1 : 0); set_config_val(c, index, value ? 1 : 0);
}
static PersistentDataItem & ensure_elem_config(color_ostream &out, int id) {
if (elems.count(id))
return elems[id];
string keyname = ELEM_CONFIG_KEY_PREFIX + int_to_string(id);
DEBUG(config,out).print("creating new persistent key for elem id %d\n", id);
elems.emplace(id, World::GetPersistentData(keyname, NULL));
return elems[id];
}
static void remove_elem_config(color_ostream &out, int id) {
if (!elems.count(id))
return;
DEBUG(config,out).print("removing persistent key for elem id %d\n", id);
World::DeletePersistentData(elems[id]);
elems.erase(id);
} }
static const int32_t CYCLE_TICKS = 1200; // one day static const int32_t CYCLE_TICKS = 1200; // one day
@ -87,7 +114,9 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
is_enabled = enable; is_enabled = enable;
DEBUG(config,out).print("%s from the API; persisting\n", DEBUG(config,out).print("%s from the API; persisting\n",
is_enabled ? "enabled" : "disabled"); is_enabled ? "enabled" : "disabled");
set_config_bool(CONFIG_IS_ENABLED, is_enabled); set_config_bool(config, CONFIG_IS_ENABLED, is_enabled);
if (enable)
do_cycle(out);
} else { } else {
DEBUG(config,out).print("%s from the API, but already %s; no action\n", DEBUG(config,out).print("%s from the API, but already %s; no action\n",
is_enabled ? "enabled" : "disabled", is_enabled ? "enabled" : "disabled",
@ -109,16 +138,27 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
if (!config.isValid()) { if (!config.isValid()) {
DEBUG(config,out).print("no config found in this save; initializing\n"); DEBUG(config,out).print("no config found in this save; initializing\n");
config = World::AddPersistentData(CONFIG_KEY); config = World::AddPersistentData(CONFIG_KEY);
set_config_bool(CONFIG_IS_ENABLED, is_enabled); set_config_bool(config, CONFIG_IS_ENABLED, is_enabled);
set_config_val(CONFIG_SOMETHING_ELSE, 6000); set_config_val(config, CONFIG_SOMETHING_ELSE, 6000);
} }
// we have to copy our enabled flag into the global plugin variable, but // we have to copy our enabled flag into the global plugin variable, but
// all the other state we can directly read/modify from the persistent // all the other state we can directly read/modify from the persistent
// data structure. // data structure.
is_enabled = get_config_bool(CONFIG_IS_ENABLED); is_enabled = get_config_bool(config, CONFIG_IS_ENABLED);
DEBUG(config,out).print("loading persisted enabled state: %s\n", DEBUG(config,out).print("loading persisted enabled state: %s\n",
is_enabled ? "true" : "false"); is_enabled ? "true" : "false");
// load other config elements, if applicable
elems.clear();
vector<PersistentDataItem> elem_configs;
World::GetPersistentData(&elem_configs, ELEM_CONFIG_KEY_PREFIX, true);
const size_t num_elem_configs = elem_configs.size();
for (size_t idx = 0; idx < num_elem_configs; ++idx) {
auto &c = elem_configs[idx];
elems.emplace(get_config_val(c, ELEM_CONFIG_ID), c);
}
return CR_OK; return CR_OK;
} }
@ -150,8 +190,8 @@ static command_result do_command(color_ostream &out, vector<string> &parameters)
// TODO: configuration logic // TODO: configuration logic
// simple commandline parsing can be done in C++, but there are lua libraries // simple commandline parsing can be done in C++, but there are lua libraries
// that can easily handle more complex commandlines. see the blueprint plugin // that can easily handle more complex commandlines. see the seedwatch plugin
// for an example. // for a simple example.
return CR_OK; return CR_OK;
} }

@ -70,6 +70,8 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
DEBUG(config,out).print("%s from the API; persisting\n", DEBUG(config,out).print("%s from the API; persisting\n",
is_enabled ? "enabled" : "disabled"); is_enabled ? "enabled" : "disabled");
set_config_bool(config, CONFIG_IS_ENABLED, is_enabled); set_config_bool(config, CONFIG_IS_ENABLED, is_enabled);
if (enable)
do_cycle(out);
} else { } else {
DEBUG(config,out).print("%s from the API, but already %s; no action\n", DEBUG(config,out).print("%s from the API, but already %s; no action\n",
is_enabled ? "enabled" : "disabled", is_enabled ? "enabled" : "disabled",

@ -99,7 +99,7 @@ static const int32_t CYCLE_TICKS = 1200;
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
static command_result do_command(color_ostream &out, vector<string> &parameters); static command_result do_command(color_ostream &out, vector<string> &parameters);
static void do_cycle(color_ostream &out, int32_t *num_enabled_seeds, int32_t *num_disabled_seeds); static void do_cycle(color_ostream &out, int32_t *num_enabled_seeds = NULL, int32_t *num_disabled_seeds = NULL);
static void seedwatch_setTarget(color_ostream &out, string name, int32_t num); static void seedwatch_setTarget(color_ostream &out, string name, int32_t num);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) { DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
@ -149,7 +149,7 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
is_enabled ? "enabled" : "disabled"); is_enabled ? "enabled" : "disabled");
set_config_bool(config, CONFIG_IS_ENABLED, is_enabled); set_config_bool(config, CONFIG_IS_ENABLED, is_enabled);
if (enable) if (enable)
seedwatch_setTarget(out, "all", DEFAULT_TARGET); do_cycle(out);
} else { } else {
DEBUG(config,out).print("%s from the API, but already %s; no action\n", DEBUG(config,out).print("%s from the API, but already %s; no action\n",
is_enabled ? "enabled" : "disabled", is_enabled ? "enabled" : "disabled",
@ -174,26 +174,27 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
world_plant_ids[plant->id] = i; world_plant_ids[plant->id] = i;
} }
watched_seeds.clear();
vector<PersistentDataItem> seed_configs;
World::GetPersistentData(&seed_configs, SEED_CONFIG_KEY_PREFIX, true);
const size_t num_seed_configs = seed_configs.size();
for (size_t idx = 0; idx < num_seed_configs; ++idx) {
auto &c = seed_configs[idx];
watched_seeds.emplace(get_config_val(c, SEED_CONFIG_ID), c);
}
config = World::GetPersistentData(CONFIG_KEY); config = World::GetPersistentData(CONFIG_KEY);
if (!config.isValid()) { if (!config.isValid()) {
DEBUG(config,out).print("no config found in this save; initializing\n"); DEBUG(config,out).print("no config found in this save; initializing\n");
config = World::AddPersistentData(CONFIG_KEY); config = World::AddPersistentData(CONFIG_KEY);
set_config_bool(config, CONFIG_IS_ENABLED, is_enabled); set_config_bool(config, CONFIG_IS_ENABLED, is_enabled);
seedwatch_setTarget(out, "all", DEFAULT_TARGET);
} }
is_enabled = get_config_bool(config, CONFIG_IS_ENABLED); is_enabled = get_config_bool(config, CONFIG_IS_ENABLED);
DEBUG(config,out).print("loading persisted enabled state: %s\n", DEBUG(config,out).print("loading persisted enabled state: %s\n",
is_enabled ? "true" : "false"); is_enabled ? "true" : "false");
watched_seeds.clear();
vector<PersistentDataItem> seed_configs;
World::GetPersistentData(&seed_configs, SEED_CONFIG_KEY_PREFIX, true);
const size_t num_seed_configs = seed_configs.size();
for (size_t idx = 0; idx < num_seed_configs; ++idx) {
auto &c = seed_configs[idx];
watched_seeds.emplace(get_config_val(c, SEED_CONFIG_ID), c);
}
return CR_OK; return CR_OK;
} }