Communicate the Plugin pointer to the plugin in a decent sort of way.

develop
Alexander Gavrilov 2013-02-13 14:45:24 +04:00
parent 39dbaf743a
commit a17760af4f
4 changed files with 8 additions and 7 deletions

@ -212,9 +212,10 @@ bool Plugin::load(color_ostream &con)
} }
const char ** plug_name =(const char ** ) LookupPlugin(plug, "name"); const char ** plug_name =(const char ** ) LookupPlugin(plug, "name");
const char ** plug_version =(const char ** ) LookupPlugin(plug, "version"); const char ** plug_version =(const char ** ) LookupPlugin(plug, "version");
if(!plug_name || !plug_version) Plugin **plug_self = (Plugin**)LookupPlugin(plug, "plugin_self");
if(!plug_name || !plug_version || !plug_self)
{ {
con.printerr("Plugin %s has no name or version.\n", filename.c_str()); con.printerr("Plugin %s has no name, version or self pointer.\n", filename.c_str());
ClosePlugin(plug); ClosePlugin(plug);
RefAutolock lock(access); RefAutolock lock(access);
state = PS_BROKEN; state = PS_BROKEN;
@ -229,6 +230,7 @@ bool Plugin::load(color_ostream &con)
state = PS_BROKEN; state = PS_BROKEN;
return false; return false;
} }
*plug_self = this;
RefAutolock lock(access); RefAutolock lock(access);
plugin_init = (command_result (*)(color_ostream &, std::vector <PluginCommand> &)) LookupPlugin(plug, "plugin_init"); plugin_init = (command_result (*)(color_ostream &, std::vector <PluginCommand> &)) LookupPlugin(plug, "plugin_init");
if(!plugin_init) if(!plugin_init)

@ -247,7 +247,8 @@ namespace DFHack
/// You have to have this in every plugin you write - just once. Ideally on top of the main file. /// You have to have this in every plugin you write - just once. Ideally on top of the main file.
#define DFHACK_PLUGIN(plugin_name) \ #define DFHACK_PLUGIN(plugin_name) \
DFhackDataExport const char * version = DFHACK_VERSION;\ DFhackDataExport const char * version = DFHACK_VERSION;\
DFhackDataExport const char * name = plugin_name; DFhackDataExport const char * name = plugin_name;\
DFhackDataExport Plugin *plugin_self = NULL;
#define DFHACK_PLUGIN_LUA_COMMANDS \ #define DFHACK_PLUGIN_LUA_COMMANDS \
DFhackCExport const DFHack::CommandReg plugin_lua_commands[] = DFhackCExport const DFHack::CommandReg plugin_lua_commands[] =

@ -126,9 +126,8 @@ DFhackCExport command_result plugin_init(color_ostream& out, vector<PluginComman
)); ));
Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("autoSyndrome");
EventManager::EventHandler handle(processJob, 5); EventManager::EventHandler handle(processJob, 5);
EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, handle, me); EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, handle, plugin_self);
return CR_OK; return CR_OK;
} }

@ -26,8 +26,7 @@ void syndromeHandler(color_ostream& out, void* ptr);
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands) DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{ {
EventManager::EventHandler syndrome(syndromeHandler, 1); EventManager::EventHandler syndrome(syndromeHandler, 1);
Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("trueTransformation"); EventManager::registerListener(EventManager::EventType::SYNDROME, syndrome, plugin_self);
EventManager::registerListener(EventManager::EventType::SYNDROME, syndrome, me);
return CR_OK; return CR_OK;
} }