2022-08-02 19:41:03 -06:00
|
|
|
// This template is appropriate for plugins that periodically check game state
|
|
|
|
// and make some sort of automated change. These types of plugins typically
|
|
|
|
// provide a command that can be used to configure the plugin behavior and
|
|
|
|
// require a world to be loaded before they can function. This kind of plugin
|
|
|
|
// should persist its state in the savegame and auto-re-enable itself when a
|
|
|
|
// savegame that had this plugin enabled is loaded.
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "df/world.h"
|
|
|
|
|
|
|
|
#include "Core.h"
|
|
|
|
#include "Debug.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
|
|
|
|
#include "modules/Persistence.h"
|
|
|
|
#include "modules/World.h"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
DFHACK_PLUGIN("persistent_per_save_example");
|
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
|
|
|
|
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
|
|
|
|
// logging levels can be dynamically controlled with the `debugfilter` command.
|
2023-01-29 18:39:48 -07:00
|
|
|
// the names "config" and "cycle" are arbitrary and are just used to categorize
|
|
|
|
// your log messages.
|
2022-08-02 19:41:03 -06:00
|
|
|
namespace DFHack {
|
|
|
|
// for configuration-related logging
|
2023-01-29 18:39:48 -07:00
|
|
|
DBG_DECLARE(persistent_per_save_example, config, DebugCategory::LINFO);
|
2022-08-02 19:41:03 -06:00
|
|
|
// for logging during the periodic scan
|
|
|
|
DBG_DECLARE(persistent_per_save_example, cycle, DebugCategory::LINFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const string CONFIG_KEY = string(plugin_name) + "/config";
|
|
|
|
static PersistentDataItem config;
|
|
|
|
enum ConfigValues {
|
|
|
|
CONFIG_IS_ENABLED = 0,
|
2023-01-29 18:35:02 -07:00
|
|
|
CONFIG_SOMETHING_ELSE = 1,
|
2022-08-02 19:41:03 -06:00
|
|
|
};
|
|
|
|
static int get_config_val(int index) {
|
|
|
|
if (!config.isValid())
|
|
|
|
return -1;
|
|
|
|
return config.ival(index);
|
|
|
|
}
|
|
|
|
static bool get_config_bool(int index) {
|
|
|
|
return get_config_val(index) == 1;
|
|
|
|
}
|
|
|
|
static void set_config_val(int index, int value) {
|
|
|
|
if (config.isValid())
|
|
|
|
config.ival(index) = value;
|
|
|
|
}
|
|
|
|
static void set_config_bool(int index, bool value) {
|
|
|
|
set_config_val(index, value ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:35:02 -07:00
|
|
|
static const int32_t CYCLE_TICKS = 1200; // one day
|
2022-08-02 19:41:03 -06:00
|
|
|
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
|
|
|
|
|
|
|
|
static command_result do_command(color_ostream &out, vector<string> ¶meters);
|
|
|
|
static void do_cycle(color_ostream &out);
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
|
2023-01-29 18:39:48 -07:00
|
|
|
DEBUG(config,out).print("initializing %s\n", plugin_name);
|
2022-08-02 19:41:03 -06:00
|
|
|
|
|
|
|
// provide a configuration interface for the plugin
|
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
plugin_name,
|
|
|
|
"Short (~54 character) description of command.",
|
|
|
|
do_command));
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
|
|
|
|
if (!Core::getInstance().isWorldLoaded()) {
|
|
|
|
out.printerr("Cannot enable %s without a loaded world.\n", plugin_name);
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable != is_enabled) {
|
|
|
|
is_enabled = enable;
|
2023-01-29 18:39:48 -07:00
|
|
|
DEBUG(config,out).print("%s from the API; persisting\n",
|
2022-08-02 19:41:03 -06:00
|
|
|
is_enabled ? "enabled" : "disabled");
|
|
|
|
set_config_bool(CONFIG_IS_ENABLED, is_enabled);
|
|
|
|
} else {
|
2023-01-29 18:39:48 -07:00
|
|
|
DEBUG(config,out).print("%s from the API, but already %s; no action\n",
|
2022-08-02 19:41:03 -06:00
|
|
|
is_enabled ? "enabled" : "disabled",
|
|
|
|
is_enabled ? "enabled" : "disabled");
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown (color_ostream &out) {
|
2023-01-29 18:39:48 -07:00
|
|
|
DEBUG(config,out).print("shutting down %s\n", plugin_name);
|
2022-08-02 19:41:03 -06:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_load_data (color_ostream &out) {
|
|
|
|
config = World::GetPersistentData(CONFIG_KEY);
|
|
|
|
|
|
|
|
if (!config.isValid()) {
|
2023-01-29 18:39:48 -07:00
|
|
|
DEBUG(config,out).print("no config found in this save; initializing\n");
|
2022-08-02 19:41:03 -06:00
|
|
|
config = World::AddPersistentData(CONFIG_KEY);
|
|
|
|
set_config_bool(CONFIG_IS_ENABLED, is_enabled);
|
2023-01-29 18:35:02 -07:00
|
|
|
set_config_val(CONFIG_SOMETHING_ELSE, 6000);
|
2022-08-02 19:41:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// data structure.
|
|
|
|
is_enabled = get_config_bool(CONFIG_IS_ENABLED);
|
2023-01-29 18:39:48 -07:00
|
|
|
DEBUG(config,out).print("loading persisted enabled state: %s\n",
|
2022-08-02 19:41:03 -06:00
|
|
|
is_enabled ? "true" : "false");
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
|
2022-08-03 17:18:22 -06:00
|
|
|
if (event == DFHack::SC_WORLD_UNLOADED) {
|
2022-08-02 19:41:03 -06:00
|
|
|
if (is_enabled) {
|
2023-01-29 18:39:48 -07:00
|
|
|
DEBUG(config,out).print("world unloaded; disabling %s\n",
|
2022-08-02 19:41:03 -06:00
|
|
|
plugin_name);
|
|
|
|
is_enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
|
2023-01-29 18:35:02 -07:00
|
|
|
if (is_enabled && world->frame_counter - cycle_timestamp >= CYCLE_TICKS)
|
2022-08-02 19:41:03 -06:00
|
|
|
do_cycle(out);
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static command_result do_command(color_ostream &out, vector<string> ¶meters) {
|
|
|
|
// be sure to suspend the core if any DF state is read or modified
|
|
|
|
CoreSuspender suspend;
|
|
|
|
|
|
|
|
if (!Core::getInstance().isWorldLoaded()) {
|
|
|
|
out.printerr("Cannot run %s without a loaded world.\n", plugin_name);
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-08-03 01:11:18 -06:00
|
|
|
// TODO: configuration logic
|
|
|
|
// simple commandline parsing can be done in C++, but there are lua libraries
|
|
|
|
// that can easily handle more complex commandlines. see the blueprint plugin
|
|
|
|
// for an example.
|
2022-08-02 19:41:03 -06:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
// cycle logic
|
|
|
|
//
|
|
|
|
|
|
|
|
static void do_cycle(color_ostream &out) {
|
|
|
|
// mark that we have recently run
|
|
|
|
cycle_timestamp = world->frame_counter;
|
|
|
|
|
|
|
|
DEBUG(cycle,out).print("running %s cycle\n", plugin_name);
|
|
|
|
|
2023-01-29 18:35:02 -07:00
|
|
|
// TODO: logic that runs every CYCLE_TICKS ticks
|
2022-08-02 19:41:03 -06:00
|
|
|
}
|