From 0bf427c689985ff9bd852219f146d259b78b85fc Mon Sep 17 00:00:00 2001 From: expwnent Date: Sat, 19 Oct 2013 21:19:39 -0400 Subject: [PATCH] Added enable/disable functionality to new plugins. --- plugins/autoSyndrome.cpp | 2 +- plugins/digFlood.cpp | 24 ++++++++++++++++++------ plugins/treefarm.cpp | 17 ++++++++++++++--- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/plugins/autoSyndrome.cpp b/plugins/autoSyndrome.cpp index 314ffb066..0f388790a 100644 --- a/plugins/autoSyndrome.cpp +++ b/plugins/autoSyndrome.cpp @@ -74,7 +74,7 @@ DFhackCExport command_result plugin_shutdown(color_ostream& out) { return CR_OK; }*/ -DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) +DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) { if (enabled == enable) return CR_OK; diff --git a/plugins/digFlood.cpp b/plugins/digFlood.cpp index 9893fa46c..0d3a8f7a2 100644 --- a/plugins/digFlood.cpp +++ b/plugins/digFlood.cpp @@ -29,10 +29,24 @@ void onDig(color_ostream& out, void* ptr); void maybeExplore(color_ostream& out, MapExtras::MapCache& cache, df::coord pt, set& jobLocations); EventManager::EventHandler digHandler(onDig, 0); -bool enabled = false; +//bool enabled = false; +DFHACK_PLUGIN_IS_ENABLED(enabled); bool digAll = false; set autodigMaterials; +DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) { + if (enabled == enable) + return CR_OK; + + enabled = enable; + if ( enabled ) { + EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, digHandler, plugin_self); + } else { + EventManager::unregisterAll(plugin_self); + } + return CR_OK; +} + DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) { commands.push_back(PluginCommand( @@ -46,7 +60,7 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector & paramet for ( size_t a = 0; a < parameters.size(); a++ ) { int32_t i = (int32_t)strtol(parameters[a].c_str(), NULL, 0); if ( i == 0 && parameters[a] == "0" ) { - EventManager::unregisterAll(plugin_self); + plugin_enable(out, false); adding = false; continue; } else if ( i == 1 ) { - EventManager::unregisterAll(plugin_self); - EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, digHandler, plugin_self); + plugin_enable(out, true); adding = true; continue; } diff --git a/plugins/treefarm.cpp b/plugins/treefarm.cpp index f10efa856..7f619dbdc 100644 --- a/plugins/treefarm.cpp +++ b/plugins/treefarm.cpp @@ -27,6 +27,7 @@ command_result treefarm (color_ostream &out, std::vector & paramet EventManager::EventHandler handler(&checkFarms, -1); int32_t frequency = 1200*30; +DFHACK_PLUGIN_IS_ENABLED(enabled); DFHACK_PLUGIN("treefarm"); DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) @@ -48,6 +49,11 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector & paramet if ( parameters.size() == 1 ) { int32_t i = atoi(parameters[0].c_str()); if ( i < 1 ) { + plugin_enable(out, false); out.print("treefarm disabled\n"); return CR_OK; } + plugin_enable(out, true); frequency = i; } - EventManager::registerTick(handler, 1, plugin_self); - - out.print("treefarm enabled with update frequency %d ticks\n", frequency); + if ( enabled ) { + EventManager::registerTick(handler, 1, plugin_self); + out.print("treefarm enabled with update frequency %d ticks\n", frequency); + } return CR_OK; }