summaryrefslogtreecommitdiff
path: root/core/modules.cpp
blob: 4910d7146f469ed3e0480fe3449471d14d53fc8f (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
#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");
	boost::filesystem::path pth(path);
	if(!boost::filesystem::is_directory(pth))
		return;
	boost::filesystem::directory_iterator i(pth), end = boost::filesystem::directory_iterator();
	while(i != end)
	{
		if(boost::filesystem::is_directory((*i).status())) //we not look in subdirectories
			continue;
		if(!boost::filesystem::status_known((*i).status()))
			continue;
		bool is_plugin = true;
		plugin::exported_functions_s *funcs = new plugin::exported_functions_s;
		memset(&funcs,0,sizeof(plugin::exported_functions_s));
		ACE_DLL *dll = new ACE_DLL;
		if(dll->open((*i).path().string().c_str()) != -1)
		{
			if((funcs->Load = (load)dll->symbol("load")) == NULL)
				is_plugin = false;
			if((funcs->OnModulesLoaded = (on_modules_loaded)dll->symbol("on_modules_loaded")) == NULL)
				is_plugin = false;
			if((funcs->Unload = (unload)dll->symbol("unload")) == NULL)
				is_plugin = false;
			if((funcs->SetPluginInfo = (set_plugin_info)dll->symbol("set_plugin_info")) == NULL)
				is_plugin = false;
		}
		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)
        {
            (*i)->get_exported_functions()->Load(&pluglink);
            (*i)->get_exported_functions()->OnModulesLoaded();
        }
    }
}

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