summaryrefslogtreecommitdiff
path: root/core/modules.cpp
blob: 437c40129bf7184f55a8888662f22dda36c7f31f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "commonheaders.h"

extern std::list<plugin*> plugins;

extern PLUGINLINK pluglink;

void load_modules()
{
	std::string path = boost::filesystem::initial_path().directory_string(); //need some workaround for windows than called indirectly
	path.append("/plugins");
	logger.log(LM_DEBUG, "Plugin directory is %s \n", path.c_str());
	logger.log(LM_DEBUG, "Loading plugins...\n");
	boost::filesystem::path pth(path);
	if(!boost::filesystem::is_directory(pth))
		return;
	boost::filesystem::directory_iterator i(pth), end;
	while(i != end)
	{
		if(boost::filesystem::is_directory(*i)) //we not look in subdirectories
		{
			logger.log(LM_DEBUG, "found subdirectory under plugins directory, skipping.\n");
			++i;
			continue;
		}
		if(!boost::filesystem::status_known((*i).status())) //worng data
		{
			logger.log(LM_DEBUG, "found invalid filesystem object, skipping.\n");
			++i;
			continue;
		}
		bool is_plugin = true;
		plugin::exported_functions_s *funcs = new plugin::exported_functions_s;
		ACE_DLL *dll = new ACE_DLL;
		boost::filesystem::path pth = i->path();
		if(dll->open(pth.string().c_str()) != -1)
		{
			if((funcs->Load = (load)dll->symbol("load")) == NULL)
			{
				is_plugin = false;
				logger.log(LM_DEBUG, "\'load\' not found in library, this is not plugin.\n");
			}
			if((funcs->OnModulesLoaded = (on_modules_loaded)dll->symbol("on_modules_loaded")) == NULL)
			{
				is_plugin = false;
				logger.log(LM_DEBUG, "\'on_modules_loaded\' not found in library, this is not plugin.\n");
			}
			if((funcs->Unload = (unload)dll->symbol("unload")) == NULL)
			{
				is_plugin = false;
				logger.log(LM_DEBUG, "\'unload\' not found in library, this is not plugin.\n");
			}
			if((funcs->SetPluginInfo = (set_plugin_info)dll->symbol("set_plugin_info")) == NULL)
			{
				is_plugin = false;
				logger.log(LM_DEBUG, "\'set_plugin_info\' not found in library, this is not plugin.\n");
			}
		}
		if(!is_plugin)
		{
			delete funcs;
			delete dll;
			continue;
		}
		PLUGININFO *info = funcs->SetPluginInfo();
		plugins.push_back(new plugin(dll, info, funcs));
		++i;		
	}
}
void run_plugins()
{ //now for testing only
	if(!plugins.empty())
	{
		std::list<plugin*>::iterator end = plugins.end();
		for(std::list<plugin*>::iterator i = plugins.begin(); i != end; ++i)
		{
			boost::thread *thr = new boost::thread(boost::bind((*i)->get_exported_functions()->Load, &pluglink));
			if(!thr->timed_join(boost::posix_time::seconds(10)))
				logger.log(LM_DEBUG, "Thread execution timeout, plugin \"%s\" basic initialisation failed.\n", toUTF8((*i)->get_plugininfo()->name).c_str());
			else
				logger.log(LM_DEBUG, "plugin \"%s\" basic initialisation success.\n", toUTF8((*i)->get_plugininfo()->name).c_str());
			delete thr;
		}
		for(std::list<plugin*>::iterator i = plugins.begin(); i != end; ++i)
		{
			boost::thread *thr = new boost::thread(boost::bind((*i)->get_exported_functions()->OnModulesLoaded));
			if(!thr->timed_join(boost::posix_time::seconds(15)))
				logger.log(LM_DEBUG, "Thread execution timeout, plugin \"%s\" main initialisation failed.\n", toUTF8((*i)->get_plugininfo()->name).c_str());
			else
				logger.log(LM_DEBUG, "plugin \"%s\" main initialisation success.\n", toUTF8((*i)->get_plugininfo()->name).c_str());
			delete thr;
		}
	}
}

plugin::plugin(ACE_DLL *lib, PLUGININFO *info, exported_functions_s *funcs)
{
	if(lib)
		plug = lib;
	else
		plug = NULL;
	if(info)
		plugininfo = info;
	else
		plugininfo = NULL;
	if(funcs)
		exported_funcs = funcs;
	else
		exported_funcs = NULL;
}

plugin::~plugin()
{
	if(plug)
		delete plug;
	if(exported_funcs)
		delete exported_funcs;
}

const PLUGININFO* plugin::get_plugininfo()
{
	return plugininfo;
}

const plugin::exported_functions_s* plugin::get_exported_functions()
{
	return exported_funcs;
}