summaryrefslogtreecommitdiff
path: root/src/common/plugins.c
blob: 614ac3c3af3b56e89156cd4c14de60880b88c7eb (plain) (blame)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder

#include "../common/mmo.h"
#include "../common/core.h"
#include "../common/timer.h"
#include "../common/utils.h" // findfile()
#include "../common/socket.h"
#include "../common/malloc.h"
#include "../common/version.h"
#include "../common/showmsg.h"
#include "plugins.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <unistd.h>
#endif

//////////////////////////////////////////////

typedef struct _Plugin_Event {
	struct _Plugin_Event* next;
	Plugin_Event_Func* func;
} Plugin_Event;

typedef struct _Plugin_Event_List {
	struct _Plugin_Event_List* next;
	char* name;
	struct _Plugin_Event* events;
} Plugin_Event_List;

static int auto_search = 0;
static int load_priority = 0;
Plugin_Event_List* event_head = NULL;
Plugin* plugin_head = NULL;

static Plugin_Info default_info = { "Unknown", PLUGIN_ALL, "0", PLUGIN_VERSION, "Unknown" };

void** plugin_call_table;
static size_t call_table_size	= 0;
static size_t max_call_table	= 0;

////// Plugin Events Functions //////////////////

int register_plugin_func(char* name)
{
	Plugin_Event_List* evl;
	if( name ){
		//ShowDebug("register_plugin_func(%s)\n", name);
		CREATE(evl, Plugin_Event_List, 1);
		evl->next = event_head;
		evl->name = aStrdup(name);
		evl->events = NULL;
		event_head = evl;
	}
	return 0;
}

static Plugin_Event_List* search_plugin_func(char* name)
{
	Plugin_Event_List* evl = event_head;
	while( evl ){
		if( strcmpi(evl->name,name) == 0 )
			return evl;
		evl = evl->next;
	}
	return NULL;
}

int register_plugin_event(Plugin_Event_Func* func, char* name)
{
	Plugin_Event_List* evl = search_plugin_func(name);
	//ShowDebug("register_plugin_event(0x%x, %s)\n", func, name);
	if( !evl )
	{// event does not exist, register
		register_plugin_func(name);
		// get the new event list
		evl = search_plugin_func(name);
	}
	if( evl ){
		Plugin_Event* ev;

		CREATE(ev, Plugin_Event, 1);
		ev->func = func;
		ev->next = NULL;

		// insert event at the end of the linked list
		if( evl->events == NULL )
			evl->events = ev;
		else {
			Plugin_Event* last_ev = evl->events;
			while( last_ev ){
				if( last_ev->next == NULL ){
					last_ev->next = ev;
					break;
				}
				last_ev = last_ev->next;
			}
		}
	}
	return 0;
}

int plugin_event_trigger(char* name)
{
	int c = 0;
	Plugin_Event_List* evl = search_plugin_func(name);
	//ShowDebug("plugin_event_trigger(%s)\n", name);
	if( evl ){
		Plugin_Event* ev = evl->events;
		while( ev ){
			ev->func();
			//ShowDebug("plugin_event_trigger: Executing function 0x%x.\n", ev->func);
			ev = ev->next;
			++c;
		}
	}
	return c;
}

////// Plugins Call Table Functions /////////

int export_symbol(void* var, size_t offset)
{
	//ShowDebug("export_symbol(0x%x,%d)\n", var,offset);

	// add to the end of the list
	if( offset < 0 )
		offset = call_table_size;

	if( offset >= max_call_table )
	{// realloc if not large enough  
		max_call_table = 1 + offset;
		RECREATE(plugin_call_table, void*, max_call_table);

		// clear the new alloced block
		memset(plugin_call_table + call_table_size, 0, (max_call_table-call_table_size)*sizeof(void*));
	}

	// the new table size is delimited by the new element at the end
	if( offset >= call_table_size )
		call_table_size = offset+1;

	// put the pointer at the selected place
	plugin_call_table[offset] = var;
	return 0;
}

////// Plugins Core /////////////////////////

static int plugin_iscompatible(char* version)
{
	int req_major = 0;
	int req_minor = 0;
	int major = 0;
	int minor = 0;

	if( version == NULL )
		return 0;
	sscanf(version, "%d.%d", &req_major, &req_minor);
	sscanf(PLUGIN_VERSION, "%d.%d", &major, &minor);
	return ( req_major == major && req_minor <= minor );
}

Plugin* plugin_open(const char* filename)
{
	Plugin* plugin;
	Plugin_Info* info;
	Plugin_Event_Table* events;
	void** procs;
	int init_flag = 1;

	//ShowDebug("plugin_open(%s)\n", filename);
	
	// Check if the plugin has been loaded before
	plugin = plugin_head;
	while (plugin) {
		// returns handle to the already loaded plugin
		if( plugin->state && strcmpi(plugin->filename, filename) == 0 ){
			ShowWarning("plugin_open: not loaded (duplicate) : '"CL_WHITE"%s"CL_RESET"'\n", filename);
			return plugin;
		}
		plugin = plugin->next;
	}

	CREATE(plugin, Plugin, 1);
	plugin->state = -1;	// not loaded

	plugin->dll = DLL_OPEN(filename);
	if( !plugin->dll ){
		ShowWarning("plugin_open: not loaded (invalid file) : '"CL_WHITE"%s"CL_RESET"'\n", filename);
		plugin_unload(plugin);
		return NULL;
	}

	// Retrieve plugin information
	plugin->state = 0;	// initialising
	info = (Plugin_Info*)DLL_SYM(plugin->dll, "plugin_info");
	// For high priority plugins (those that are explicitly loaded from the conf file)
	// we'll ignore them even (could be a 3rd party dll file)
	if( !info )
	{// foreign plugin
		//ShowDebug("plugin_open: plugin_info not found\n");
		if( load_priority == 0 )
		{// not requested
			//ShowDebug("plugin_open: not loaded (not requested) : '"CL_WHITE"%s"CL_RESET"'\n", filename);
			plugin_unload(plugin);
			return NULL;
		}
	} else if( !plugin_iscompatible(info->req_version) )
	{// incompatible version
		ShowWarning("plugin_open: not loaded (incompatible version '%s' -> '%s') : '"CL_WHITE"%s"CL_RESET"'\n", info->req_version, PLUGIN_VERSION, filename);
		plugin_unload(plugin);
		return NULL;
	} else if( (info->type != PLUGIN_ALL && info->type != PLUGIN_CORE && info->type != SERVER_TYPE) ||
		(info->type == PLUGIN_CORE && SERVER_TYPE != PLUGIN_LOGIN && SERVER_TYPE != PLUGIN_CHAR && SERVER_TYPE != PLUGIN_MAP) )
	{// not for this server
		//ShowDebug("plugin_open: not loaded (incompatible) : '"CL_WHITE"%s"CL_RESET"'\n", filename);
		plugin_unload(plugin);
		return NULL;
	}

	plugin->info = ( info != NULL ? info : &default_info );
	plugin->filename = aStrdup(filename);

	// Initialise plugin call table (For exporting procedures)
	procs = (void**)DLL_SYM(plugin->dll, "plugin_call_table");
	if( procs )
		*procs = plugin_call_table;
	//else ShowDebug("plugin_open: plugin_call_table not found\n");

	// Register plugin events
	events = (Plugin_Event_Table*)DLL_SYM(plugin->dll, "plugin_event_table");
	if( events ){
		int i = 0;
		//ShowDebug("plugin_open: parsing plugin_event_table\n");
		while( events[i].func_name ){
			if( strcmpi(events[i].event_name, EVENT_PLUGIN_TEST) == 0 ){
				Plugin_Test_Func* test_func;
				test_func = (Plugin_Test_Func*)DLL_SYM(plugin->dll, events[i].func_name);
				//ShowDebug("plugin_open: invoking "EVENT_PLUGIN_TEST" with %s()\n", events[i].func_name);
				if( test_func && test_func() == 0 ){
					// plugin has failed test, disabling
					//ShowDebug("plugin_open: disabled (failed test) : %s\n", filename);
					init_flag = 0;
				}
			} else {
				Plugin_Event_Func* func;
				func = (Plugin_Event_Func*)DLL_SYM(plugin->dll, events[i].func_name);
				if (func)
					register_plugin_event(func, events[i].event_name);
			}
			i++;
		}
	}
	//else ShowDebug("plugin_open: plugin_event_table not found\n");

	plugin->next = plugin_head;
	plugin_head = plugin;

	plugin->state = init_flag;	// fully loaded
	ShowStatus("Done loading plugin '"CL_WHITE"%s"CL_RESET"'\n", (info) ? plugin->info->name : filename);

	return plugin;
}

void plugin_load(const char* filename)
{
	plugin_open(filename);
}

void plugin_unload(Plugin* plugin)
{
	if( plugin == NULL )
		return;
	if( plugin->filename )
		aFree(plugin->filename);
	if( plugin->dll )
		DLL_CLOSE(plugin->dll);
	aFree(plugin);
}

#ifdef WIN32
char *DLL_ERROR(void)
{
	static char dllbuf[80];
	DWORD dw = GetLastError();
	FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, 0, dllbuf, 80, NULL);
	return dllbuf;
}
#endif

////// Initialize/Finalize ////////////////////

static int plugins_config_read(const char *cfgName)
{
	char line[1024], w1[1024], w2[1024];
	FILE *fp;

	fp = fopen(cfgName, "r");
	if( fp == NULL ){
		ShowError("File not found: %s\n", cfgName);
		return 1;
	}
	while( fgets(line, sizeof(line), fp) )
	{
		if( line[0] == '/' && line[1] == '/' )
			continue;
		if( sscanf(line,"%[^:]: %[^\r\n]",w1,w2) != 2 )
			continue;

		if( strcmpi(w1,"auto_search") == 0 ){
			if( strcmpi(w2,"yes") == 0 )
				auto_search = 1;
			else if( strcmpi(w2,"no") == 0 )
				auto_search = 0;
			else
				auto_search = atoi(w2);
		} else if( strcmpi(w1,"plugin") == 0 ){
			char filename[128];
			snprintf(filename, sizeof filename, "plugins/%s%s", w2, DLL_EXT);
			plugin_load(filename);
		} else if( strcmpi(w1,"import") == 0 )
			plugins_config_read(w2);
	}
	fclose(fp);
	return 0;
}

void plugins_init(void)
{
	// Sugested functionality:
	// add atcommands/script commands (Borf)
	char* PLUGIN_CONF_FILENAME = "conf/plugin_athena.conf";
	//ShowDebug("plugins_init()\n");
	register_plugin_func(EVENT_PLUGIN_INIT);
	register_plugin_func(EVENT_PLUGIN_FINAL);
	register_plugin_func(EVENT_ATHENA_INIT);
	register_plugin_func(EVENT_ATHENA_FINAL);

	// networking
	EXPORT_SYMBOL(RFIFOSKIP,  SYMBOL_RFIFOSKIP);
	EXPORT_SYMBOL(WFIFOSET,   SYMBOL_WFIFOSET);
	EXPORT_SYMBOL(do_close,   SYMBOL_DELETE_SESSION);
	EXPORT_SYMBOL(session,    SYMBOL_SESSION);
	EXPORT_SYMBOL(&fd_max,    SYMBOL_FD_MAX);
	EXPORT_SYMBOL(addr_,      SYMBOL_ADDR);
	// timers
	EXPORT_SYMBOL(get_uptime,              SYMBOL_GET_UPTIME);
	EXPORT_SYMBOL(delete_timer,            SYMBOL_DELETE_TIMER);
	EXPORT_SYMBOL(add_timer_func_list,     SYMBOL_ADD_TIMER_FUNC_LIST);
	EXPORT_SYMBOL(add_timer_interval,      SYMBOL_ADD_TIMER_INTERVAL);
	EXPORT_SYMBOL(add_timer,               SYMBOL_ADD_TIMER);
	EXPORT_SYMBOL((void*)get_svn_revision, SYMBOL_GET_SVN_REVISION);
	EXPORT_SYMBOL(gettick,                 SYMBOL_GETTICK);
	// core
	EXPORT_SYMBOL(parse_console, SYMBOL_PARSE_CONSOLE);
	EXPORT_SYMBOL(&runflag,      SYMBOL_RUNFLAG);
	EXPORT_SYMBOL(arg_v,         SYMBOL_ARG_V);
	EXPORT_SYMBOL(&arg_c,        SYMBOL_ARG_C);
	EXPORT_SYMBOL(SERVER_NAME,   SYMBOL_SERVER_NAME);
	EXPORT_SYMBOL(&SERVER_TYPE,  SYMBOL_SERVER_TYPE);

	load_priority = 1;
	plugins_config_read(PLUGIN_CONF_FILENAME);
	load_priority = 0;

	if( auto_search )
		findfile("plugins", DLL_EXT, plugin_load);

	plugin_event_trigger(EVENT_PLUGIN_INIT);

	return;
}

void plugins_final(void)
{
	Plugin* plugin = plugin_head;
	Plugin* next_plugin;
	Plugin_Event_List* evl = event_head;
	Plugin_Event_List* next_evl;
	Plugin_Event* ev;
	Plugin_Event* next_ev;

	//ShowDebug("plugins_final()\n");
	plugin_event_trigger(EVENT_PLUGIN_FINAL);

	while( plugin ){
		next_plugin = plugin->next;
		plugin_unload(plugin);
		plugin = next_plugin;
	}

	while( evl ){
		ev = evl->events;
		while( ev ){
			next_ev = ev->next;
			aFree(ev);
			ev = next_ev;
		}
		next_evl = evl->next;
		aFree(evl->name);
		aFree(evl);
		evl = next_evl;
	}

	aFree(plugin_call_table);

	return;
}