clean up and add logging to state persistence

develop
myk002 2022-08-02 11:07:36 -07:00
parent f98015ae55
commit 1dec977476
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 24 additions and 19 deletions

@ -65,11 +65,15 @@ static int get_config_val(int index) {
return -1; return -1;
return config.ival(index); return config.ival(index);
} }
static bool set_config_val(int index, int value) { static bool get_config_bool(int index) {
if (!config.isValid()) return get_config_val(index) == 1;
return false; }
config.ival(index) = value; static void set_config_val(int index, int value) {
return true; if (config.isValid())
config.ival(index) = value;
}
static void set_config_bool(int index, bool value) {
set_config_val(index, value ? 1 : 0);
} }
static bool did_complain = false; // avoids message spam static bool did_complain = false; // avoids message spam
@ -101,23 +105,24 @@ static void autonestbox_cycle(color_ostream &out);
static void init_autonestbox(color_ostream &out) { static void init_autonestbox(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");
config = World::AddPersistentData(CONFIG_KEY); config = World::AddPersistentData(CONFIG_KEY);
set_config_bool(CONFIG_IS_ENABLED, false);
if (get_config_val(CONFIG_IS_ENABLED) == -1) {
set_config_val(CONFIG_IS_ENABLED, 0);
set_config_val(CONFIG_CYCLE_TICKS, 6000); set_config_val(CONFIG_CYCLE_TICKS, 6000);
} }
if (is_enabled) is_enabled = get_config_bool(CONFIG_IS_ENABLED);
set_config_val(CONFIG_IS_ENABLED, 1); DEBUG(status,out).print("loading persisted enabled state: %s\n",
else is_enabled ? "true" : "false");
is_enabled = (get_config_val(CONFIG_IS_ENABLED) == 1);
did_complain = false; did_complain = false;
} }
static void cleanup_autonestbox(color_ostream &out) { static void cleanup_autonestbox(color_ostream &out) {
is_enabled = false; if (is_enabled) {
DEBUG(status,out).print("disabling (not persisting)\n");
is_enabled = false;
}
} }
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) { DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
@ -140,10 +145,10 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
if (enable != is_enabled) { if (enable != is_enabled) {
is_enabled = enable; is_enabled = enable;
if (is_enabled) DEBUG(status,out).print("%s from the API, persisting\n",
init_autonestbox(out); is_enabled ? "enabled" : "disabled");
else set_config_bool(CONFIG_IS_ENABLED, is_enabled);
cleanup_autonestbox(out); init_autonestbox(out);
} }
return CR_OK; return CR_OK;
} }
@ -217,7 +222,7 @@ static command_result df_autonestbox(color_ostream &out, vector<string> &paramet
autonestbox_cycle(out); autonestbox_cycle(out);
} }
else { else {
out << "autonestbox is " << (is_enabled ? "" : "not ") << "running\n"; out << "autonestbox is " << (is_enabled ? "" : "not ") << "running" << endl;
} }
return CR_OK; return CR_OK;
} }