summaryrefslogtreecommitdiff
path: root/core/main.cpp
blob: ae1dbf08d68944ea65412a06b776eb0aa9b93fe7 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <windows.h>
#include <iostream>
//#include <tchar.h>

#include <list>

#include <pluginapi.h>
#include "plugin.h"
#include "service.h"

using namespace std;

list<plugin*> plugins;
list<service*> services;

int LoadModules();
INT_PTR CallService(const char *name, WPARAM w, LPARAM l);
HANDLE CreateServiceFunction(const char *name, SERVICE pService);
int ServiceExists(const char *name);
SERVICE GetPluginInfoList(WPARAM, LPARAM);
SERVICE Test(WPARAM, LPARAM);

PLUGINLINK link = {&CreateServiceFunction, &CallService, &ServiceExists};

int main(int argc, char *argv[])
{
	if(LoadModules())
		return 1; //something wrong
	for(list<plugin*>::iterator p = plugins.begin(); p != plugins.end(); p++) //initializing plugins
	{
		(*p)->getFuncs().load(&link);
	}
	CreateServiceFunction("GetPluginInfoList", (SERVICE)GetPluginInfoList);
	CreateServiceFunction("Test", (SERVICE)Test);
	for(list<plugin*>::iterator p = plugins.begin(); p != plugins.end(); p++)
	{
		if((*p)->getFuncs().loaded)
		(*p)->getFuncs().loaded();
	}
	CallService("GetPluginInfoList", 0, 0);
	for(;;)
		Sleep(1000);
	return 0;
}

int LoadModules()
{
	WIN32_FIND_DATAA findFileData;
	HANDLE hFile = 0;
	if(!(hFile = FindFirstFileA(".\\modules\\*", &findFileData)))
		return 1; //failed to find any plugins in directory
	HMODULE hPlugin = 0;
	char tmp[MAX_PATH] = {0};
	plugin::exported_funcs_s funcs;
	while(hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_NO_MORE_FILES)
	{
		strcpy(tmp, ".\\modules\\");
		strcat(tmp, findFileData.cFileName);
		hPlugin = LoadLibraryA(tmp);
		funcs.info = (SetPluginInfo)GetProcAddress(hPlugin, "SetPluginInfo");
		funcs.load = (Load)GetProcAddress(hPlugin, "Load");
		funcs.loaded = (OnModulesLoaded)GetProcAddress(hPlugin, "OnModulesLoaded");
		if(funcs.info && funcs.load)
		{
			PLUGININFO *pi = funcs.info();
			plugins.push_back(new plugin(hPlugin, funcs, pi->shortName));
		}
		else
			FreeLibrary(hPlugin);
		FindNextFileA(hFile, &findFileData);
	}
	return 0;
}

const HMODULE plugin::getHmodule()
{
	return hModule;
}

int plugin::setHandle(const HMODULE &hMod)
{
	if(!hMod)
		return 1;
	hModule = hMod;
	return 0;
}

int plugin::setName(const char *name)
{
	if(strlen(name)< 2)
		return 1;
	szPluginName = new char [strlen(name)+1];
	strcpy(szPluginName, name);
	return 0;
}

const char* plugin::getName()
{
	return szPluginName;
}
const plugin::exported_funcs_s plugin::getFuncs()
{
	return funcs;
}
const PLUGININFO plugin::getPluginInfo()
{
	return pluginInfo;
}
plugin::plugin(const HMODULE hMod, const exported_funcs_s fnct, const char *name)
{
	hModule = hMod;
	funcs = fnct;
	szPluginName = new char [strlen(name)+1];
	strcpy(szPluginName, name);
}
plugin::~plugin()
{
	FreeLibrary(hModule);
	free(szPluginName);
}
service::service(const char *name, SERVICE service)
{
	szName = new char [strlen(name)+1];
	strcpy(szName, name);
	pService = service;
}
const char *service::getName()
{
	return szName;
}
const SERVICE service::getService()
{
	return pService;
}
INT_PTR CallService(const char *name, WPARAM w, LPARAM l)
{
	for(list<service*>::iterator p = services.begin(); p != services.end(); p++)
	{
		if(!strcmp((*p)->getName(), name))
		{
			SERVICE s = *(*p)->getService();
			return s(w, l);
		}
	}
	return 0;
}
HANDLE CreateServiceFunction(const char *name, SERVICE pService)
{
	services.push_back(new service(name, pService));
}
int ServiceExists(const char *name)
{
	for(list<service*>::iterator p = services.begin(); p != services.end(); p++)
		if(!strcmp((*p)->getName(), name))
			return 1;
	return 0;
}
SERVICE GetPluginInfoList(WPARAM, LPARAM)
{
	list<PLUGININFO> *pluginInfoList = new list<PLUGININFO>;
	for(list<plugin*>::iterator p = plugins.begin(); p != plugins.end(); p++)
		pluginInfoList->push_back((*p)->getPluginInfo());
	return (SERVICE)pluginInfoList; //is it right ?
}

SERVICE Test(WPARAM, LPARAM)
{
	MessageBoxA(0, "Test service working", "INFO", MB_OK);
	return 0;
}