2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cPluginManager.h"
|
|
|
|
#include "cPlugin.h"
|
|
|
|
#include "cPlugin_Lua.h"
|
2012-01-26 15:39:46 -05:00
|
|
|
#include "cPlugin_NewLua.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cWebAdmin.h"
|
|
|
|
#include "cItem.h"
|
|
|
|
#include "cRoot.h"
|
|
|
|
#include "cLuaCommandBinder.h"
|
2011-11-07 20:25:01 -05:00
|
|
|
#include "../iniFile/iniFile.h"
|
2012-03-28 14:11:06 -04:00
|
|
|
#include "tolua++.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-11-07 20:25:01 -05:00
|
|
|
#include "SquirrelBindings.h"
|
|
|
|
#if USE_SQUIRREL
|
2012-02-01 04:12:54 -05:00
|
|
|
#pragma warning(disable:4100;disable:4127;disable:4510;disable:4610;disable:4244;disable:4512) // Getting A LOT of these warnings from SqPlus
|
|
|
|
#include <sqplus/sqplus.h>
|
|
|
|
#pragma warning(default:4100;default:4127;default:4510;default:4610;default:4244;default:4512)
|
2011-11-07 20:25:01 -05:00
|
|
|
#endif
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cPluginManager* cPluginManager::GetPluginManager()
|
|
|
|
{
|
|
|
|
LOGWARN("WARNING: Using deprecated function cPluginManager::GetPluginManager() use cRoot::Get()->GetPluginManager() instead!");
|
|
|
|
return cRoot::Get()->GetPluginManager();
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cPluginManager::cPluginManager()
|
2012-03-28 14:11:06 -04:00
|
|
|
: m_LuaCommandBinder( new cLuaCommandBinder() )
|
2011-10-03 14:41:19 -04:00
|
|
|
, m_bReloadPlugins(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cPluginManager::~cPluginManager()
|
|
|
|
{
|
|
|
|
UnloadPluginsNow();
|
2011-11-07 20:25:01 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
delete m_LuaCommandBinder;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cPluginManager::ReloadPlugins()
|
|
|
|
{
|
|
|
|
m_bReloadPlugins = true;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cPluginManager::ReloadPluginsNow()
|
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
LOG("Loading plugins");
|
2011-10-03 14:41:19 -04:00
|
|
|
m_bReloadPlugins = false;
|
|
|
|
UnloadPluginsNow();
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
#if USE_SQUIRREL
|
2011-11-07 20:25:01 -05:00
|
|
|
if( !SquirrelBindings::IsBound ) // Can only do this once apparently, or we're making ambiguous calls in the script
|
|
|
|
{
|
|
|
|
SquirrelVM::Init();
|
|
|
|
SquirrelBindings::Bind( SquirrelVM::GetVMPtr() );
|
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
#endif // USE_SQUIRREL
|
2011-11-07 20:25:01 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cIniFile IniFile("settings.ini");
|
2012-02-08 12:38:02 -05:00
|
|
|
if (!IniFile.ReadFile() )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
LOGWARNING("cPluginManager: Can't find settings.ini, so can't load any plugins.");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int KeyNum = IniFile.FindKey("Plugins");
|
|
|
|
unsigned int NumPlugins = IniFile.GetNumValues( KeyNum );
|
|
|
|
if( NumPlugins > 0 )
|
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < NumPlugins; i++)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
AString ValueName = IniFile.GetValueName(KeyNum, i );
|
|
|
|
if( ValueName.compare("Plugin") == 0 ) // It's a Lua plugin
|
|
|
|
{
|
|
|
|
AString PluginFile = IniFile.GetValue(KeyNum, i );
|
|
|
|
if( !PluginFile.empty() )
|
|
|
|
{
|
|
|
|
// allow for comma separated plugin list
|
|
|
|
// degrades and works fine for the plugin
|
|
|
|
// per line
|
|
|
|
AStringVector split = StringSplit( PluginFile, "," );
|
|
|
|
for (unsigned int j = 0; j < split.size(); j++)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
cPlugin_Lua* Plugin = new cPlugin_Lua( (split[j] + AString(".lua") ).c_str() );
|
|
|
|
if( !AddLuaPlugin( Plugin ) )
|
|
|
|
{
|
|
|
|
delete Plugin;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
else if( ValueName.compare("NewPlugin") == 0 ) // New plugin style
|
|
|
|
{
|
|
|
|
AString PluginFile = IniFile.GetValue(KeyNum, i );
|
|
|
|
if( !PluginFile.empty() )
|
2012-01-26 15:39:46 -05:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
cPlugin_NewLua* Plugin = new cPlugin_NewLua( PluginFile.c_str() );
|
|
|
|
if( !AddPlugin( Plugin ) )
|
2012-01-26 15:39:46 -05:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
delete Plugin;
|
2012-01-26 15:39:46 -05:00
|
|
|
}
|
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_SQUIRREL
|
|
|
|
else if( ValueName.compare("Squirrel") == 0 ) // Squirrel plugin
|
|
|
|
{
|
|
|
|
AString PluginFile = IniFile.GetValue(KeyNum, i );
|
|
|
|
if( !PluginFile.empty() )
|
2011-11-07 20:25:01 -05:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
LOGINFO("Loading Squirrel plugin: %s", PluginFile.c_str() );
|
|
|
|
try
|
2011-11-07 20:25:01 -05:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
SquirrelObject SquirrelScript = SquirrelVM::CompileScript( (AString("Plugins/") + PluginFile + ".nut").c_str() );
|
2011-11-07 20:25:01 -05:00
|
|
|
try
|
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
SquirrelVM::RunScript( SquirrelScript );
|
|
|
|
}
|
|
|
|
catch (SquirrelError & e)
|
2011-11-07 20:25:01 -05:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
LOGERROR("SquirrelScript error: %s, %s\n", e.desc, "SquirrelVM::RunScript");
|
2011-11-07 20:25:01 -05:00
|
|
|
}
|
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
catch (SquirrelError & e)
|
|
|
|
{
|
|
|
|
LOGERROR("SquirrelScript error: %s, %s\n", e.desc, "SquirrelVM::CompileScript");
|
|
|
|
}
|
2011-11-07 20:25:01 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
#endif // USE_SQUIRREL
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
if( GetNumPlugins() == 0 )
|
|
|
|
{
|
|
|
|
LOG("No plugins loaded");
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
LOG("Loaded %i plugin(s)", GetNumPlugins() );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-07 20:25:01 -05:00
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
void cPluginManager::Tick(float a_Dt)
|
|
|
|
{
|
|
|
|
if( m_bReloadPlugins )
|
|
|
|
{
|
|
|
|
ReloadPluginsNow();
|
|
|
|
}
|
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
HookMap::iterator Plugins = m_Hooks.find( E_PLUGIN_TICK );
|
|
|
|
if( Plugins != m_Hooks.end() )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
|
|
|
|
{
|
|
|
|
(*itr)->Tick( a_Dt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
bool cPluginManager::CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... )
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
HookMap::iterator Plugins = m_Hooks.find( a_Hook );
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
if( Plugins != m_Hooks.end() )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
|
|
|
|
{
|
|
|
|
if( (*itr)->OnChat( Message, Player ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
if( Plugins == m_Hooks.end() )
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( a_Hook )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
case E_PLUGIN_COLLECT_ITEM:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
if( (*itr)->OnCollectItem( Pickup, Player ) )
|
|
|
|
return true;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
case E_PLUGIN_BLOCK_DIG:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
if( (*itr)->OnBlockDig( Packet, Player, Item ) )
|
|
|
|
return true;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
case E_PLUGIN_BLOCK_PLACE:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
if( (*itr)->OnBlockPlace( Packet, Player ) )
|
|
|
|
return true;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
case E_PLUGIN_DISCONNECT:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
if( (*itr)->OnDisconnect( Reason, Player ) )
|
|
|
|
return true;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
case E_PLUGIN_LOGIN:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
if( (*itr)->OnLogin( Packet ) )
|
|
|
|
return true;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
case E_PLUGIN_PLAYER_JOIN:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
if( (*itr)->OnPlayerJoin( Player ) )
|
|
|
|
return true;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
case E_PLUGIN_PLAYER_MOVE:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
(*itr)->OnPlayerMove( Player );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
case E_PLUGIN_TAKE_DAMAGE:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
(*itr)->OnTakeDamage( Pawn, TDI );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
case E_PLUGIN_KILLED:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
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 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
if( (*itr)->OnKilled( Killed, Killer ) )
|
|
|
|
return true;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
case E_PLUGIN_CHUNK_GENERATED:
|
|
|
|
{
|
|
|
|
if (a_NumArgs != 3)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_list argptr;
|
|
|
|
va_start( argptr, a_NumArgs);
|
|
|
|
cWorld * World = va_arg(argptr, cWorld *);
|
|
|
|
int ChunkX = va_arg(argptr, int);
|
|
|
|
int ChunkZ = va_arg(argptr, int);
|
|
|
|
va_end (argptr);
|
|
|
|
for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
|
|
|
|
{
|
|
|
|
(*itr)->OnChunkGenerated(World, ChunkX, ChunkZ);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
default:
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
|
|
|
LOGWARNING("cPluginManager: Calling Unknown hook: %i", a_Hook );
|
2011-10-03 14:41:19 -04:00
|
|
|
break;
|
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
} // switch (a_Hook)
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
cPlugin* cPluginManager::GetPlugin( const char* a_Plugin ) const
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
for( PluginList::const_iterator itr = m_Plugins.begin(); itr != m_Plugins.end(); ++itr )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-08 12:38:02 -05:00
|
|
|
if (strcmp((*itr)->GetName(), a_Plugin) == 0 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
const cPluginManager::PluginList & cPluginManager::GetAllPlugins() const
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
return m_Plugins;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cPluginManager::UnloadPluginsNow()
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
m_Hooks.clear();
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
while( m_LuaPlugins.size() > 0 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
cPlugin_Lua* LuaPlugin = *m_LuaPlugins.begin();
|
2011-10-03 14:41:19 -04:00
|
|
|
if( LuaPlugin )
|
|
|
|
{
|
|
|
|
cWebAdmin* WebAdmin = cRoot::Get()->GetWebAdmin();
|
|
|
|
if( WebAdmin ) WebAdmin->RemovePlugin( LuaPlugin->GetLuaState() );
|
|
|
|
delete LuaPlugin;
|
|
|
|
}
|
2012-03-28 14:11:06 -04:00
|
|
|
m_LuaPlugins.remove( LuaPlugin );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
while( m_Plugins.size() > 0 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
RemovePlugin( *m_Plugins.begin(), true );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2011-11-07 20:25:01 -05:00
|
|
|
|
|
|
|
//SquirrelVM::Shutdown(); // This breaks shit
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-07 20:25:01 -05:00
|
|
|
void cPluginManager::RemoveHooks( cPlugin* a_Plugin )
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
m_Hooks[ E_PLUGIN_TICK].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_CHAT].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_COLLECT_ITEM].remove( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_BLOCK_DIG].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_BLOCK_PLACE].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_DISCONNECT].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_HANDSHAKE].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_LOGIN].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_PLAYER_SPAWN].remove( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_PLAYER_JOIN].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_PLAYER_MOVE].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_TAKE_DAMAGE].remove ( a_Plugin );
|
|
|
|
m_Hooks[ E_PLUGIN_KILLED].remove ( a_Plugin );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cPluginManager::RemovePlugin( cPlugin* a_Plugin, bool a_bDelete /* = false */ )
|
|
|
|
{
|
|
|
|
if( a_bDelete )
|
|
|
|
{
|
|
|
|
m_LuaCommandBinder->RemoveBindingsForPlugin( a_Plugin );
|
2012-03-28 14:11:06 -04:00
|
|
|
m_Plugins.remove( a_Plugin );
|
2011-11-07 20:25:01 -05:00
|
|
|
RemoveHooks( a_Plugin );
|
2011-10-03 14:41:19 -04:00
|
|
|
a_Plugin->OnDisable();
|
2011-11-07 20:25:01 -05:00
|
|
|
if( a_Plugin->GetLanguage() != cPlugin::E_SQUIRREL ) // Squirrel needs to clean it up himself!
|
2012-02-08 12:38:02 -05:00
|
|
|
{
|
2011-11-07 20:25:01 -05:00
|
|
|
delete a_Plugin;
|
2012-02-08 12:38:02 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
for( LuaPluginList::iterator itr = m_LuaPlugins.begin(); itr != m_LuaPlugins.end(); ++itr )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
(*itr)->RemovePlugin( a_Plugin );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
bool cPluginManager::AddPlugin( cPlugin* a_Plugin )
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
a_Plugin->m_bCanBindCommands = true;
|
2011-10-03 14:41:19 -04:00
|
|
|
if( a_Plugin->Initialize() )
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
m_Plugins.remove( a_Plugin );
|
|
|
|
m_Plugins.push_back( a_Plugin );
|
2011-10-03 14:41:19 -04:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-07 20:25:01 -05:00
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
a_Plugin->m_bCanBindCommands = false;
|
2011-11-07 20:25:01 -05:00
|
|
|
RemoveHooks( a_Plugin ); // Undo any damage the Initialize() might have done
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
bool cPluginManager::AddPlugin( lua_State* a_LuaState, cPlugin* a_Plugin )
|
|
|
|
{
|
2011-11-07 20:25:01 -05:00
|
|
|
a_Plugin->SetLanguage( cPlugin::E_LUA );
|
2011-10-03 14:41:19 -04:00
|
|
|
cPlugin_Lua* LuaPlugin = GetLuaPlugin( a_LuaState );
|
2012-03-28 14:11:06 -04:00
|
|
|
if( LuaPlugin == NULL )
|
|
|
|
{
|
|
|
|
lua_Debug ar;
|
|
|
|
lua_getstack(a_LuaState, 1, &ar);
|
|
|
|
lua_getinfo(a_LuaState, "nSl", &ar);
|
|
|
|
LOGERROR("ERROR: Trying to add an 'old style' plugin from within a 'new style' plugin.\nIn file: %s at line: %i", ar.source, ar.currentline);
|
|
|
|
}
|
|
|
|
a_Plugin->m_bCanBindCommands = true;
|
2011-10-03 14:41:19 -04:00
|
|
|
if( LuaPlugin && a_Plugin->Initialize() )
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
m_Plugins.remove( a_Plugin );
|
|
|
|
m_Plugins.push_back( a_Plugin );
|
2011-10-03 14:41:19 -04:00
|
|
|
LuaPlugin->AddPlugin( a_Plugin );
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-28 14:11:06 -04:00
|
|
|
|
|
|
|
a_Plugin->m_bCanBindCommands = false;
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
bool cPluginManager::AddLuaPlugin( cPlugin_Lua* a_Plugin )
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
m_LuaPlugins.push_back( a_Plugin ); // It HAS to be in here before calling Initialize, so it can be found by AddPlugin()
|
2011-10-03 14:41:19 -04:00
|
|
|
if(a_Plugin->Initialize() )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
LOG(">>>>>>> Could not initialize a plugin! ");
|
2012-03-28 14:11:06 -04:00
|
|
|
m_LuaPlugins.remove( a_Plugin );
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cPluginManager::RemoveLuaPlugin( std::string a_FileName )
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
for( LuaPluginList::iterator itr = m_LuaPlugins.begin(); itr != m_LuaPlugins.end(); ++itr )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
if( (*itr)->GetFileName() == a_FileName )
|
|
|
|
{
|
|
|
|
cPlugin_Lua* Plugin = *itr;
|
2012-03-28 14:11:06 -04:00
|
|
|
m_LuaPlugins.remove( Plugin );
|
2011-11-07 20:25:01 -05:00
|
|
|
delete Plugin;
|
2011-10-03 14:41:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cPlugin_Lua* cPluginManager::GetLuaPlugin( lua_State* a_State )
|
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
for( LuaPluginList::iterator itr = m_LuaPlugins.begin(); itr != m_LuaPlugins.end(); ++itr )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
if( (*itr)->GetLuaState() == a_State )
|
|
|
|
{
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 14:11:06 -04:00
|
|
|
return NULL;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cPluginManager::AddHook( cPlugin* a_Plugin, PluginHook a_Hook )
|
|
|
|
{
|
2012-01-26 15:39:46 -05:00
|
|
|
if( !a_Plugin )
|
|
|
|
{
|
|
|
|
LOGWARN("Called cPluginManager::AddHook while a_Plugin is NULL");
|
|
|
|
return;
|
|
|
|
}
|
2012-03-28 14:11:06 -04:00
|
|
|
PluginList & Plugins = m_Hooks[ a_Hook ];
|
2011-10-03 14:41:19 -04:00
|
|
|
Plugins.remove( a_Plugin );
|
|
|
|
Plugins.push_back( a_Plugin );
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
unsigned int cPluginManager::GetNumPlugins() const
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-03-28 14:11:06 -04:00
|
|
|
return m_Plugins.size();
|
2012-02-04 14:40:48 -05:00
|
|
|
}
|
2012-02-08 12:38:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-28 14:11:06 -04:00
|
|
|
|
|
|
|
bool cPluginManager::HasPlugin( cPlugin* a_Plugin ) const
|
|
|
|
{
|
|
|
|
for( PluginList::const_iterator itr = m_Plugins.begin(); itr != m_Plugins.end(); ++itr )
|
|
|
|
{
|
|
|
|
if( *itr == a_Plugin )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|