From cf619a519eb031531609604fd29a6e60a0893691 Mon Sep 17 00:00:00 2001 From: expwnent Date: Fri, 14 Dec 2012 22:14:38 -0500 Subject: [PATCH] EventManager: made event handlers unregister when plugins are unloaded. Also changed PluginManager so that plugins can call core.getPluginManager() during plugin_init. --- library/Core.cpp | 3 +++ library/PluginManager.cpp | 30 ++++++++++++++++++------------ library/include/PluginManager.h | 1 + plugins/eventExample.cpp | 6 ++++-- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index a9164128f..5c397dd05 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -905,6 +905,7 @@ bool Core::Init() cerr << "Initializing Plugins.\n"; // create plugin manager plug_mgr = new PluginManager(this); + plug_mgr->init(this); IODATA *temp = new IODATA; temp->core = this; temp->plug_mgr = plug_mgr; @@ -1254,6 +1255,8 @@ void Core::onUpdate(color_ostream &out) void Core::onStateChange(color_ostream &out, state_change_event event) { + EventManager::onStateChange(out, event); + buildings_onStateChange(out, event); plug_mgr->OnStateChange(out, event); diff --git a/library/PluginManager.cpp b/library/PluginManager.cpp index 0c80639b4..86bab66cd 100644 --- a/library/PluginManager.cpp +++ b/library/PluginManager.cpp @@ -22,6 +22,7 @@ must not be misrepresented as being the original software. distribution. */ +#include "modules/EventManager.h" #include "Internal.h" #include "Core.h" #include "MemAccess.h" @@ -270,6 +271,7 @@ bool Plugin::unload(color_ostream &con) // if we are actually loaded if(state == PS_LOADED) { + EventManager::unregisterAll(this); // notify the plugin about an attempt to shutdown if (plugin_onstatechange && plugin_onstatechange(con, SC_BEGIN_UNLOAD) == CR_NOT_FOUND) @@ -598,6 +600,22 @@ void Plugin::push_function(lua_State *state, LuaFunction *fn) } PluginManager::PluginManager(Core * core) +{ + cmdlist_mutex = new mutex(); + eval_ruby = NULL; +} + +PluginManager::~PluginManager() +{ + for(size_t i = 0; i < all_plugins.size();i++) + { + delete all_plugins[i]; + } + all_plugins.clear(); + delete cmdlist_mutex; +} + +void PluginManager::init(Core * core) { #ifdef LINUX_BUILD string path = core->getHackPath() + "plugins/"; @@ -606,8 +624,6 @@ PluginManager::PluginManager(Core * core) string path = core->getHackPath() + "plugins\\"; const string searchstr = ".plug.dll"; #endif - cmdlist_mutex = new mutex(); - eval_ruby = NULL; vector filez; getdir(path, filez); for(size_t i = 0; i < filez.size();i++) @@ -622,16 +638,6 @@ PluginManager::PluginManager(Core * core) } } -PluginManager::~PluginManager() -{ - for(size_t i = 0; i < all_plugins.size();i++) - { - delete all_plugins[i]; - } - all_plugins.clear(); - delete cmdlist_mutex; -} - Plugin *PluginManager::getPluginByName (const std::string & name) { for(size_t i = 0; i < all_plugins.size(); i++) diff --git a/library/include/PluginManager.h b/library/include/PluginManager.h index 9ef16703a..62a195867 100644 --- a/library/include/PluginManager.h +++ b/library/include/PluginManager.h @@ -205,6 +205,7 @@ namespace DFHack friend class Plugin; PluginManager(Core * core); ~PluginManager(); + void init(Core* core); void OnUpdate(color_ostream &out); void OnStateChange(color_ostream &out, state_change_event event); void registerCommands( Plugin * p ); diff --git a/plugins/eventExample.cpp b/plugins/eventExample.cpp index ea8e2cfec..3b898ae7b 100644 --- a/plugins/eventExample.cpp +++ b/plugins/eventExample.cpp @@ -2,6 +2,7 @@ #include "Console.h" #include "Core.h" #include "Export.h" +#include "PluginManager.h" #include "modules/EventManager.h" #include "DataDefs.h" @@ -15,9 +16,10 @@ void jobCompleted(color_ostream& out, void* job); DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) { EventManager::EventHandler initiateHandler(jobInitiated); EventManager::EventHandler completeHandler(jobCompleted); + Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("eventExample"); - EventManager::registerListener(EventManager::EventType::JOB_INITIATED, initiateHandler, NULL); - EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, completeHandler, NULL); + EventManager::registerListener(EventManager::EventType::JOB_INITIATED, initiateHandler, me); + EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, completeHandler, me); return CR_OK; }