update sample plugin code

status -> config
don't make cycle ticks configurable. nobody does that
develop
Myk Taylor 2023-01-29 17:35:02 -08:00
parent f4c4d4dcdf
commit 61f3325860
No known key found for this signature in database
1 changed files with 13 additions and 12 deletions

@ -30,7 +30,7 @@ REQUIRE_GLOBAL(world);
// logging levels can be dynamically controlled with the `debugfilter` command. // logging levels can be dynamically controlled with the `debugfilter` command.
namespace DFHack { namespace DFHack {
// for configuration-related logging // for configuration-related logging
DBG_DECLARE(persistent_per_save_example, status, DebugCategory::LINFO); DBG_DECLARE(persistent_per_save_example, cycle, DebugCategory::LINFO);
// for logging during the periodic scan // for logging during the periodic scan
DBG_DECLARE(persistent_per_save_example, cycle, DebugCategory::LINFO); DBG_DECLARE(persistent_per_save_example, cycle, DebugCategory::LINFO);
} }
@ -39,7 +39,7 @@ static const string CONFIG_KEY = string(plugin_name) + "/config";
static PersistentDataItem config; static PersistentDataItem config;
enum ConfigValues { enum ConfigValues {
CONFIG_IS_ENABLED = 0, CONFIG_IS_ENABLED = 0,
CONFIG_CYCLE_TICKS = 1, CONFIG_SOMETHING_ELSE = 1,
}; };
static int get_config_val(int index) { static int get_config_val(int index) {
if (!config.isValid()) if (!config.isValid())
@ -57,13 +57,14 @@ static void set_config_bool(int index, bool value) {
set_config_val(index, value ? 1 : 0); set_config_val(index, value ? 1 : 0);
} }
static const int32_t CYCLE_TICKS = 1200; // one day
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); static void do_cycle(color_ostream &out);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) { DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
DEBUG(status,out).print("initializing %s\n", plugin_name); DEBUG(cycle,out).print("initializing %s\n", plugin_name);
// provide a configuration interface for the plugin // provide a configuration interface for the plugin
commands.push_back(PluginCommand( commands.push_back(PluginCommand(
@ -82,11 +83,11 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
if (enable != is_enabled) { if (enable != is_enabled) {
is_enabled = enable; is_enabled = enable;
DEBUG(status,out).print("%s from the API; persisting\n", DEBUG(cycle,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_IS_ENABLED, is_enabled);
} else { } else {
DEBUG(status,out).print("%s from the API, but already %s; no action\n", DEBUG(cycle,out).print("%s from the API, but already %s; no action\n",
is_enabled ? "enabled" : "disabled", is_enabled ? "enabled" : "disabled",
is_enabled ? "enabled" : "disabled"); is_enabled ? "enabled" : "disabled");
} }
@ -94,7 +95,7 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
} }
DFhackCExport command_result plugin_shutdown (color_ostream &out) { DFhackCExport command_result plugin_shutdown (color_ostream &out) {
DEBUG(status,out).print("shutting down %s\n", plugin_name); DEBUG(cycle,out).print("shutting down %s\n", plugin_name);
return CR_OK; return CR_OK;
} }
@ -103,17 +104,17 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
config = World::GetPersistentData(CONFIG_KEY); config = World::GetPersistentData(CONFIG_KEY);
if (!config.isValid()) { if (!config.isValid()) {
DEBUG(status,out).print("no config found in this save; initializing\n"); DEBUG(cycle,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_IS_ENABLED, is_enabled);
set_config_val(CONFIG_CYCLE_TICKS, 6000); set_config_val(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_IS_ENABLED);
DEBUG(status,out).print("loading persisted enabled state: %s\n", DEBUG(cycle,out).print("loading persisted enabled state: %s\n",
is_enabled ? "true" : "false"); is_enabled ? "true" : "false");
return CR_OK; return CR_OK;
} }
@ -121,7 +122,7 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_WORLD_UNLOADED) { if (event == DFHack::SC_WORLD_UNLOADED) {
if (is_enabled) { if (is_enabled) {
DEBUG(status,out).print("world unloaded; disabling %s\n", DEBUG(cycle,out).print("world unloaded; disabling %s\n",
plugin_name); plugin_name);
is_enabled = false; is_enabled = false;
} }
@ -130,7 +131,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
} }
DFhackCExport command_result plugin_onupdate(color_ostream &out) { DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (is_enabled && world->frame_counter - cycle_timestamp >= get_config_val(CONFIG_CYCLE_TICKS)) if (is_enabled && world->frame_counter - cycle_timestamp >= CYCLE_TICKS)
do_cycle(out); do_cycle(out);
return CR_OK; return CR_OK;
} }
@ -162,5 +163,5 @@ static void do_cycle(color_ostream &out) {
DEBUG(cycle,out).print("running %s cycle\n", plugin_name); DEBUG(cycle,out).print("running %s cycle\n", plugin_name);
// TODO: logic that runs every get_config_val(CONFIG_CYCLE_TICKS) ticks // TODO: logic that runs every CYCLE_TICKS ticks
} }