summaryrefslogtreecommitdiffstats
path: root/source/cPluginManager.cpp
blob: db776f0a7160c830c68211aa84d4678de76fd549 (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
413
414
415
416
417
418
419
420
421
422
423
#include "cPluginManager.h"
#include "cPlugin.h"
#include "cPlugin_Lua.h"
#include "cMCLogger.h"
#include "cWebAdmin.h"
#include "cItem.h"
#include "cRoot.h"
#include "cLuaCommandBinder.h"

#include <stdarg.h>

#include "../iniFile/iniFile.h"

extern std::vector<std::string> StringSplit(std::string str, std::string delim);

typedef std::list< cPlugin_Lua* > LuaPluginList;
typedef std::map< cPluginManager::PluginHook, cPluginManager::PluginList > HookMap;

struct cPluginManager::sPluginManagerState
{
	LuaPluginList LuaPlugins;
	cPluginManager::PluginList Plugins;
	HookMap Hooks;
};

cPluginManager* cPluginManager::GetPluginManager()
{
	LOGWARN("WARNING: Using deprecated function cPluginManager::GetPluginManager() use cRoot::Get()->GetPluginManager() instead!");
	return cRoot::Get()->GetPluginManager();
}

cPluginManager::cPluginManager()
	: m_pState( new sPluginManagerState )
	, m_LuaCommandBinder( new cLuaCommandBinder() )
	, m_bReloadPlugins(false)
{
}

cPluginManager::~cPluginManager()
{
	UnloadPluginsNow();
	delete m_LuaCommandBinder;
	delete m_pState;
}

void cPluginManager::ReloadPlugins()
{
	m_bReloadPlugins = true;
}

void cPluginManager::ReloadPluginsNow()
{
	LOG("--Loading plugins--");
	m_bReloadPlugins = false;
	UnloadPluginsNow();

	cIniFile IniFile("settings.ini");
	if( IniFile.ReadFile() )
	{
		unsigned int KeyNum = IniFile.FindKey("Plugins");
		unsigned int NumPlugins = IniFile.GetNumValues( KeyNum );
		if( NumPlugins > 0 )
		{
			for(unsigned int i = 0; i < NumPlugins; i++)
			{
				std::string ValueName = IniFile.GetValueName(KeyNum, i );
				if( ValueName.compare("Plugin") == 0 )
				{	// It's a plugin
					std::string PluginFile = IniFile.GetValue(KeyNum, i );
					if( PluginFile.compare("") != 0 )
					{
						// allow for comma separated plugin list
						// degrades and works fine for the plugin
						// per line
						std::vector< std::string > split = StringSplit( PluginFile, "," );
						for (unsigned int j = 0; j < split.size(); j++) {
							cPlugin_Lua* Plugin = new cPlugin_Lua( (split[j] + std::string(".lua") ).c_str() );
							if( !AddLuaPlugin( Plugin ) )
							{
								delete Plugin;
							}
						}
					}
				}
			}
		}

		if( GetNumPlugins() == 0 )
		{
			LOG("No plugins loaded");
		}
		else
		{
			LOG("Loaded %i plugin(s)", GetNumPlugins() );
		}
	}
	else
	{
		LOG("WARNING: Can't find settings.ini, so can't load any plugins.");
	}
	LOG("--Done loading plugins--");
}

void cPluginManager::Tick(float a_Dt)
{
	if( m_bReloadPlugins )
	{
		ReloadPluginsNow();
	}


	HookMap::iterator Plugins = m_pState->Hooks.find( E_PLUGIN_TICK );
	if( Plugins != m_pState->Hooks.end() )
	{
		for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
		{
			(*itr)->Tick( a_Dt );
		}
	}
}

bool cPluginManager::CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... )
{
	HookMap::iterator Plugins = m_pState->Hooks.find( a_Hook );

	// Special case for chat hook, since you can also bind commands (bound commands don't use chat hook)
	if( a_Hook == E_PLUGIN_CHAT )
	{
		if( a_NumArgs != 2 ) return false;
		va_list argptr;
		va_start( argptr, a_NumArgs);
		const char* Message = va_arg(argptr, const char* );
		cPlayer* Player = va_arg(argptr, cPlayer* );
		va_end (argptr);

		if( m_LuaCommandBinder->HandleCommand( std::string( Message ), Player ) )
			return true;

		if( Plugins != m_pState->Hooks.end() )
		{
			for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
			{
				if( (*itr)->OnChat( Message, Player ) )
					return true;
			}
		}

		return false;
	}

	if( Plugins != m_pState->Hooks.end() )
	{
		switch( a_Hook )
		{
		case E_PLUGIN_COLLECT_ITEM:
			{
				if( a_NumArgs != 2 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				cPickup* Pickup = va_arg(argptr, cPickup* );
				cPlayer* Player = va_arg(argptr, cPlayer* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					if( (*itr)->OnCollectItem( Pickup, Player ) )
						return true;
				}
			}
			break;
		case E_PLUGIN_BLOCK_DIG:
			{
				if( a_NumArgs != 2 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				cPacket_BlockDig* Packet = va_arg(argptr, cPacket_BlockDig* );
				cPlayer* Player = va_arg(argptr, cPlayer* );
				cItem* Item = va_arg( argptr, cItem* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					if( (*itr)->OnBlockDig( Packet, Player, Item ) )
						return true;
				}
			}
			break;
		case E_PLUGIN_BLOCK_PLACE:
			{
				if( a_NumArgs != 2 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				cPacket_BlockPlace* Packet = va_arg(argptr, cPacket_BlockPlace* );
				cPlayer* Player = va_arg(argptr, cPlayer* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					if( (*itr)->OnBlockPlace( Packet, Player ) )
						return true;
				}
			}
			break;
		case E_PLUGIN_DISCONNECT:
			{
				if( a_NumArgs != 2 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				const char* Reason = va_arg(argptr, const char* );
				cPlayer* Player = va_arg(argptr, cPlayer* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					if( (*itr)->OnDisconnect( Reason, Player ) )
						return true;
				}
			}
			break;
		case E_PLUGIN_LOGIN:
			{
				if( a_NumArgs != 1 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				cPacket_Login* Packet = va_arg(argptr, cPacket_Login* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					if( (*itr)->OnLogin( Packet ) )
						return true;
				}
			}
			break;
		case E_PLUGIN_PLAYER_JOIN:
			{
				if( a_NumArgs != 1 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				cPlayer* Player = va_arg(argptr, cPlayer* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					if( (*itr)->OnPlayerJoin( Player ) )
						return true;
				}
			}
			break;
		case E_PLUGIN_PLAYER_MOVE:
			{
				if( a_NumArgs != 1 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				cPlayer* Player = va_arg(argptr, cPlayer* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					(*itr)->OnPlayerMove( Player );
				}
			}
			break;
		case E_PLUGIN_TAKE_DAMAGE:
			{
				if( a_NumArgs != 2 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				cPawn* Pawn = va_arg(argptr, cPawn* );
				TakeDamageInfo* TDI = va_arg(argptr, TakeDamageInfo* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					(*itr)->OnTakeDamage( Pawn, TDI );
				}
			}
			break;
		case E_PLUGIN_KILLED:
			{
				if( a_NumArgs != 2 ) break;
				va_list argptr;
				va_start( argptr, a_NumArgs);
				cPawn* Killed = va_arg(argptr, cPawn* );
				cEntity* Killer = va_arg(argptr, cEntity* );
				va_end (argptr);
				for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
				{
					if( (*itr)->OnKilled( Killed, Killer ) )
						return true;
				}
			}
			break;
		default:
			LOG("WARNING: Calling Unknown hook: %i", a_Hook );
			break;
		}
	}
	return false;
}

cPlugin* cPluginManager::GetPlugin( std::string a_Plugin )
{
	for( PluginList::iterator itr = m_pState->Plugins.begin(); itr != m_pState->Plugins.end(); ++itr )
	{
		if( (*itr)->GetName().compare( a_Plugin ) == 0 )
		{
			return *itr;
		}
	}
	return 0;
}

const cPluginManager::PluginList & cPluginManager::GetAllPlugins()
{
	return m_pState->Plugins;
}

void cPluginManager::UnloadPluginsNow()
{
	m_pState->Hooks.clear();

	while( m_pState->LuaPlugins.size() > 0 )
	{
		cPlugin_Lua* LuaPlugin = *m_pState->LuaPlugins.begin();
		if( LuaPlugin )
		{
			cWebAdmin* WebAdmin = cRoot::Get()->GetWebAdmin();
			if( WebAdmin ) WebAdmin->RemovePlugin( LuaPlugin->GetLuaState() );
			delete LuaPlugin;
		}
		m_pState->LuaPlugins.remove( LuaPlugin );
	}

	while( m_pState->Plugins.size() > 0 )
	{
		RemovePlugin( *m_pState->Plugins.begin(), true );
	}
}

void cPluginManager::RemovePlugin( cPlugin* a_Plugin, bool a_bDelete /* = false */ )
{
	if( a_bDelete )
	{
		m_LuaCommandBinder->RemoveBindingsForPlugin( a_Plugin );
		m_pState->Plugins.remove( a_Plugin );
		a_Plugin->OnDisable();
		delete a_Plugin;
	}
	else
	{
		for( LuaPluginList::iterator itr = m_pState->LuaPlugins.begin(); itr != m_pState->LuaPlugins.end(); ++itr )
		{
			(*itr)->RemovePlugin( a_Plugin );
		}
	}
}

bool cPluginManager::AddPlugin( cPlugin* a_Plugin )
{
	if( a_Plugin->Initialize() )
	{
		m_pState->Plugins.remove( a_Plugin );
		m_pState->Plugins.push_back( a_Plugin );
		return true;
	}
	return false;
}

bool cPluginManager::AddPlugin( lua_State* a_LuaState, cPlugin* a_Plugin )
{
	cPlugin_Lua* LuaPlugin = GetLuaPlugin( a_LuaState );
	if( LuaPlugin && a_Plugin->Initialize() )
	{
		m_pState->Plugins.remove( a_Plugin );
		m_pState->Plugins.push_back( a_Plugin );
		LuaPlugin->AddPlugin( a_Plugin );
		return true;
	}
	return false;
}

bool cPluginManager::AddLuaPlugin( cPlugin_Lua* a_Plugin )
{
	m_pState->LuaPlugins.push_back( a_Plugin ); // It HAS to be in here before calling Initialize, so it can be found by AddPlugin()
	if(a_Plugin->Initialize() )
	{
		return true;
	}
	LOG(">>>>>>> Could not initialize a plugin! ");
	m_pState->LuaPlugins.remove( a_Plugin );
	return false;
}

void cPluginManager::RemoveLuaPlugin( std::string a_FileName )
{
	for( LuaPluginList::iterator itr = m_pState->LuaPlugins.begin(); itr != m_pState->LuaPlugins.end(); ++itr )
	{
		if( (*itr)->GetFileName() == a_FileName )
		{
			cPlugin_Lua* Plugin = *itr;
			delete Plugin;
			m_pState->LuaPlugins.remove( Plugin );
			return;
		}
	}
}

cPlugin_Lua* cPluginManager::GetLuaPlugin( lua_State* a_State )
{
	for( LuaPluginList::iterator itr = m_pState->LuaPlugins.begin(); itr != m_pState->LuaPlugins.end(); ++itr )
	{
		if( (*itr)->GetLuaState() == a_State )
		{
			return *itr;
		}
	}
	return 0;
}

void cPluginManager::AddHook( cPlugin* a_Plugin, PluginHook a_Hook )
{
	PluginList & Plugins = m_pState->Hooks[ a_Hook ];
	Plugins.remove( a_Plugin );
	Plugins.push_back( a_Plugin );
}

unsigned int cPluginManager::GetNumPlugins()
{
	return m_pState->Plugins.size(); 
}