1
0

Got rid of sPluginManagerState

/pluginlist now also shows the number of active plugins
Fixed crash caused by lingering bound Lua commands http://forum.mc-server.org/showthread.php?tid=212&pid=2541#pid2541
Added error messages when binding commands to an invalid plugin

git-svn-id: http://mc-server.googlecode.com/svn/trunk@440 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
faketruth 2012-03-28 18:11:06 +00:00
parent fcb9b281e0
commit 091d958b0c
6 changed files with 91 additions and 71 deletions

View File

@ -7,7 +7,7 @@ function HandlePluginListCommand( Split, Player )
table.insert(PluginTable, Plugin:GetName() ) table.insert(PluginTable, Plugin:GetName() )
end end
Player:SendMessage( cChatColor.Green .. "Loaded plugins:" ) Player:SendMessage( cChatColor.Green .. "Loaded plugins: (" .. #PluginTable .. ")" )
Player:SendMessage( cChatColor.Gold .. table.concat(PluginTable, cChatColor.Gold.." ") ) Player:SendMessage( cChatColor.Gold .. table.concat(PluginTable, cChatColor.Gold.." ") )
return true return true
end end

View File

@ -3,6 +3,7 @@
#include "cLuaCommandBinder.h" #include "cLuaCommandBinder.h"
#include "cPlayer.h" #include "cPlayer.h"
#include "cPlugin.h"
#include "cPlugin_Lua.h" #include "cPlugin_Lua.h"
#include "tolua++.h" #include "tolua++.h"
@ -45,6 +46,11 @@ void cLuaCommandBinder::RemoveBindingsForPlugin( cPlugin* a_Plugin )
bool cLuaCommandBinder::BindCommand( const std::string & a_Command, const std::string & a_Permission, cPlugin* a_Plugin, lua_State * a_LuaState, int a_FunctionReference ) bool cLuaCommandBinder::BindCommand( const std::string & a_Command, const std::string & a_Permission, cPlugin* a_Plugin, lua_State * a_LuaState, int a_FunctionReference )
{ {
if( !a_Plugin->CanBindCommands() )
{
LOGERROR("ERROR: Trying to bind command \"%s\" to a plugin that is not initialized.", a_Command.c_str() );
return false;
}
if( m_BoundCommands.find( a_Command ) != m_BoundCommands.end() ) if( m_BoundCommands.find( a_Command ) != m_BoundCommands.end() )
{ {
LOGERROR("ERROR: Trying to bind command \"%s\" that has already been bound.", a_Command.c_str() ); LOGERROR("ERROR: Trying to bind command \"%s\" that has already been bound.", a_Command.c_str() );

View File

@ -10,6 +10,7 @@
cPlugin::cPlugin() cPlugin::cPlugin()
: m_Version( 0 ) : m_Version( 0 )
, m_Language( E_CPP ) , m_Language( E_CPP )
, m_bCanBindCommands( false )
{ {
} }

View File

@ -80,7 +80,11 @@ public:
PluginLanguage GetLanguage() { return m_Language; } PluginLanguage GetLanguage() { return m_Language; }
void SetLanguage( PluginLanguage a_Language ) { m_Language = a_Language; } void SetLanguage( PluginLanguage a_Language ) { m_Language = a_Language; }
bool CanBindCommands() { return m_bCanBindCommands; }
private: private:
friend class cPluginManager;
bool m_bCanBindCommands; // Only changed by cPluginManager
PluginLanguage m_Language; PluginLanguage m_Language;
std::vector< CommandStruct > m_Commands; std::vector< CommandStruct > m_Commands;
std::string m_Name; std::string m_Name;

View File

@ -10,6 +10,7 @@
#include "cRoot.h" #include "cRoot.h"
#include "cLuaCommandBinder.h" #include "cLuaCommandBinder.h"
#include "../iniFile/iniFile.h" #include "../iniFile/iniFile.h"
#include "tolua++.h"
#include "SquirrelBindings.h" #include "SquirrelBindings.h"
#if USE_SQUIRREL #if USE_SQUIRREL
@ -22,24 +23,6 @@
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() cPluginManager* cPluginManager::GetPluginManager()
{ {
LOGWARN("WARNING: Using deprecated function cPluginManager::GetPluginManager() use cRoot::Get()->GetPluginManager() instead!"); LOGWARN("WARNING: Using deprecated function cPluginManager::GetPluginManager() use cRoot::Get()->GetPluginManager() instead!");
@ -51,8 +34,7 @@ cPluginManager* cPluginManager::GetPluginManager()
cPluginManager::cPluginManager() cPluginManager::cPluginManager()
: m_pState( new sPluginManagerState ) : m_LuaCommandBinder( new cLuaCommandBinder() )
, m_LuaCommandBinder( new cLuaCommandBinder() )
, m_bReloadPlugins(false) , m_bReloadPlugins(false)
{ {
} }
@ -66,7 +48,6 @@ cPluginManager::~cPluginManager()
UnloadPluginsNow(); UnloadPluginsNow();
delete m_LuaCommandBinder; delete m_LuaCommandBinder;
delete m_pState;
} }
@ -192,8 +173,8 @@ void cPluginManager::Tick(float a_Dt)
ReloadPluginsNow(); ReloadPluginsNow();
} }
HookMap::iterator Plugins = m_pState->Hooks.find( E_PLUGIN_TICK ); HookMap::iterator Plugins = m_Hooks.find( E_PLUGIN_TICK );
if( Plugins != m_pState->Hooks.end() ) if( Plugins != m_Hooks.end() )
{ {
for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr ) for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
{ {
@ -208,7 +189,7 @@ void cPluginManager::Tick(float a_Dt)
bool cPluginManager::CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... ) bool cPluginManager::CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... )
{ {
HookMap::iterator Plugins = m_pState->Hooks.find( a_Hook ); HookMap::iterator Plugins = m_Hooks.find( a_Hook );
// Special case for chat hook, since you can also bind commands (bound commands don't use chat 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_Hook == E_PLUGIN_CHAT )
@ -223,7 +204,7 @@ bool cPluginManager::CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... )
if( m_LuaCommandBinder->HandleCommand( std::string( Message ), Player ) ) if( m_LuaCommandBinder->HandleCommand( std::string( Message ), Player ) )
return true; return true;
if( Plugins != m_pState->Hooks.end() ) if( Plugins != m_Hooks.end() )
{ {
for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr ) for( PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr )
{ {
@ -235,7 +216,7 @@ bool cPluginManager::CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... )
return false; return false;
} }
if( Plugins == m_pState->Hooks.end() ) if( Plugins == m_Hooks.end() )
{ {
return false; return false;
} }
@ -395,9 +376,9 @@ bool cPluginManager::CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... )
cPlugin* cPluginManager::GetPlugin( const char* a_Plugin ) cPlugin* cPluginManager::GetPlugin( const char* a_Plugin ) const
{ {
for( PluginList::iterator itr = m_pState->Plugins.begin(); itr != m_pState->Plugins.end(); ++itr ) for( PluginList::const_iterator itr = m_Plugins.begin(); itr != m_Plugins.end(); ++itr )
{ {
if (strcmp((*itr)->GetName(), a_Plugin) == 0 ) if (strcmp((*itr)->GetName(), a_Plugin) == 0 )
{ {
@ -411,9 +392,9 @@ cPlugin* cPluginManager::GetPlugin( const char* a_Plugin )
const cPluginManager::PluginList & cPluginManager::GetAllPlugins() const cPluginManager::PluginList & cPluginManager::GetAllPlugins() const
{ {
return m_pState->Plugins; return m_Plugins;
} }
@ -422,23 +403,23 @@ const cPluginManager::PluginList & cPluginManager::GetAllPlugins()
void cPluginManager::UnloadPluginsNow() void cPluginManager::UnloadPluginsNow()
{ {
m_pState->Hooks.clear(); m_Hooks.clear();
while( m_pState->LuaPlugins.size() > 0 ) while( m_LuaPlugins.size() > 0 )
{ {
cPlugin_Lua* LuaPlugin = *m_pState->LuaPlugins.begin(); cPlugin_Lua* LuaPlugin = *m_LuaPlugins.begin();
if( LuaPlugin ) if( LuaPlugin )
{ {
cWebAdmin* WebAdmin = cRoot::Get()->GetWebAdmin(); cWebAdmin* WebAdmin = cRoot::Get()->GetWebAdmin();
if( WebAdmin ) WebAdmin->RemovePlugin( LuaPlugin->GetLuaState() ); if( WebAdmin ) WebAdmin->RemovePlugin( LuaPlugin->GetLuaState() );
delete LuaPlugin; delete LuaPlugin;
} }
m_pState->LuaPlugins.remove( LuaPlugin ); m_LuaPlugins.remove( LuaPlugin );
} }
while( m_pState->Plugins.size() > 0 ) while( m_Plugins.size() > 0 )
{ {
RemovePlugin( *m_pState->Plugins.begin(), true ); RemovePlugin( *m_Plugins.begin(), true );
} }
//SquirrelVM::Shutdown(); // This breaks shit //SquirrelVM::Shutdown(); // This breaks shit
@ -450,19 +431,19 @@ void cPluginManager::UnloadPluginsNow()
void cPluginManager::RemoveHooks( cPlugin* a_Plugin ) void cPluginManager::RemoveHooks( cPlugin* a_Plugin )
{ {
m_pState->Hooks[ E_PLUGIN_TICK].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_TICK].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_CHAT].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_CHAT].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_COLLECT_ITEM].remove( a_Plugin ); m_Hooks[ E_PLUGIN_COLLECT_ITEM].remove( a_Plugin );
m_pState->Hooks[ E_PLUGIN_BLOCK_DIG].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_BLOCK_DIG].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_BLOCK_PLACE].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_BLOCK_PLACE].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_DISCONNECT].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_DISCONNECT].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_HANDSHAKE].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_HANDSHAKE].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_LOGIN].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_LOGIN].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_PLAYER_SPAWN].remove( a_Plugin ); m_Hooks[ E_PLUGIN_PLAYER_SPAWN].remove( a_Plugin );
m_pState->Hooks[ E_PLUGIN_PLAYER_JOIN].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_PLAYER_JOIN].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_PLAYER_MOVE].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_PLAYER_MOVE].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_TAKE_DAMAGE].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_TAKE_DAMAGE].remove ( a_Plugin );
m_pState->Hooks[ E_PLUGIN_KILLED].remove ( a_Plugin ); m_Hooks[ E_PLUGIN_KILLED].remove ( a_Plugin );
} }
@ -474,7 +455,7 @@ void cPluginManager::RemovePlugin( cPlugin* a_Plugin, bool a_bDelete /* = false
if( a_bDelete ) if( a_bDelete )
{ {
m_LuaCommandBinder->RemoveBindingsForPlugin( a_Plugin ); m_LuaCommandBinder->RemoveBindingsForPlugin( a_Plugin );
m_pState->Plugins.remove( a_Plugin ); m_Plugins.remove( a_Plugin );
RemoveHooks( a_Plugin ); RemoveHooks( a_Plugin );
a_Plugin->OnDisable(); a_Plugin->OnDisable();
if( a_Plugin->GetLanguage() != cPlugin::E_SQUIRREL ) // Squirrel needs to clean it up himself! if( a_Plugin->GetLanguage() != cPlugin::E_SQUIRREL ) // Squirrel needs to clean it up himself!
@ -484,7 +465,7 @@ void cPluginManager::RemovePlugin( cPlugin* a_Plugin, bool a_bDelete /* = false
} }
else else
{ {
for( LuaPluginList::iterator itr = m_pState->LuaPlugins.begin(); itr != m_pState->LuaPlugins.end(); ++itr ) for( LuaPluginList::iterator itr = m_LuaPlugins.begin(); itr != m_LuaPlugins.end(); ++itr )
{ {
(*itr)->RemovePlugin( a_Plugin ); (*itr)->RemovePlugin( a_Plugin );
} }
@ -497,13 +478,15 @@ void cPluginManager::RemovePlugin( cPlugin* a_Plugin, bool a_bDelete /* = false
bool cPluginManager::AddPlugin( cPlugin* a_Plugin ) bool cPluginManager::AddPlugin( cPlugin* a_Plugin )
{ {
a_Plugin->m_bCanBindCommands = true;
if( a_Plugin->Initialize() ) if( a_Plugin->Initialize() )
{ {
m_pState->Plugins.remove( a_Plugin ); m_Plugins.remove( a_Plugin );
m_pState->Plugins.push_back( a_Plugin ); m_Plugins.push_back( a_Plugin );
return true; return true;
} }
a_Plugin->m_bCanBindCommands = false;
RemoveHooks( a_Plugin ); // Undo any damage the Initialize() might have done RemoveHooks( a_Plugin ); // Undo any damage the Initialize() might have done
return false; return false;
} }
@ -516,13 +499,23 @@ bool cPluginManager::AddPlugin( lua_State* a_LuaState, cPlugin* a_Plugin )
{ {
a_Plugin->SetLanguage( cPlugin::E_LUA ); a_Plugin->SetLanguage( cPlugin::E_LUA );
cPlugin_Lua* LuaPlugin = GetLuaPlugin( a_LuaState ); cPlugin_Lua* LuaPlugin = GetLuaPlugin( a_LuaState );
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;
if( LuaPlugin && a_Plugin->Initialize() ) if( LuaPlugin && a_Plugin->Initialize() )
{ {
m_pState->Plugins.remove( a_Plugin ); m_Plugins.remove( a_Plugin );
m_pState->Plugins.push_back( a_Plugin ); m_Plugins.push_back( a_Plugin );
LuaPlugin->AddPlugin( a_Plugin ); LuaPlugin->AddPlugin( a_Plugin );
return true; return true;
} }
a_Plugin->m_bCanBindCommands = false;
return false; return false;
} }
@ -532,13 +525,13 @@ bool cPluginManager::AddPlugin( lua_State* a_LuaState, cPlugin* a_Plugin )
bool cPluginManager::AddLuaPlugin( cPlugin_Lua* a_Plugin ) 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() m_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() ) if(a_Plugin->Initialize() )
{ {
return true; return true;
} }
LOG(">>>>>>> Could not initialize a plugin! "); LOG(">>>>>>> Could not initialize a plugin! ");
m_pState->LuaPlugins.remove( a_Plugin ); m_LuaPlugins.remove( a_Plugin );
return false; return false;
} }
@ -548,12 +541,12 @@ bool cPluginManager::AddLuaPlugin( cPlugin_Lua* a_Plugin )
void cPluginManager::RemoveLuaPlugin( std::string a_FileName ) void cPluginManager::RemoveLuaPlugin( std::string a_FileName )
{ {
for( LuaPluginList::iterator itr = m_pState->LuaPlugins.begin(); itr != m_pState->LuaPlugins.end(); ++itr ) for( LuaPluginList::iterator itr = m_LuaPlugins.begin(); itr != m_LuaPlugins.end(); ++itr )
{ {
if( (*itr)->GetFileName() == a_FileName ) if( (*itr)->GetFileName() == a_FileName )
{ {
cPlugin_Lua* Plugin = *itr; cPlugin_Lua* Plugin = *itr;
m_pState->LuaPlugins.remove( Plugin ); m_LuaPlugins.remove( Plugin );
delete Plugin; delete Plugin;
return; return;
} }
@ -566,14 +559,14 @@ void cPluginManager::RemoveLuaPlugin( std::string a_FileName )
cPlugin_Lua* cPluginManager::GetLuaPlugin( lua_State* a_State ) cPlugin_Lua* cPluginManager::GetLuaPlugin( lua_State* a_State )
{ {
for( LuaPluginList::iterator itr = m_pState->LuaPlugins.begin(); itr != m_pState->LuaPlugins.end(); ++itr ) for( LuaPluginList::iterator itr = m_LuaPlugins.begin(); itr != m_LuaPlugins.end(); ++itr )
{ {
if( (*itr)->GetLuaState() == a_State ) if( (*itr)->GetLuaState() == a_State )
{ {
return *itr; return *itr;
} }
} }
return 0; return NULL;
} }
@ -587,7 +580,7 @@ void cPluginManager::AddHook( cPlugin* a_Plugin, PluginHook a_Hook )
LOGWARN("Called cPluginManager::AddHook while a_Plugin is NULL"); LOGWARN("Called cPluginManager::AddHook while a_Plugin is NULL");
return; return;
} }
PluginList & Plugins = m_pState->Hooks[ a_Hook ]; PluginList & Plugins = m_Hooks[ a_Hook ];
Plugins.remove( a_Plugin ); Plugins.remove( a_Plugin );
Plugins.push_back( a_Plugin ); Plugins.push_back( a_Plugin );
} }
@ -596,11 +589,21 @@ void cPluginManager::AddHook( cPlugin* a_Plugin, PluginHook a_Hook )
unsigned int cPluginManager::GetNumPlugins() unsigned int cPluginManager::GetNumPlugins() const
{ {
return m_pState->Plugins.size(); return m_Plugins.size();
} }
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;
}

View File

@ -32,8 +32,8 @@ public: //tolua_export
static cPluginManager * GetPluginManager(); //tolua_export static cPluginManager * GetPluginManager(); //tolua_export
typedef std::list< cPlugin* > PluginList; typedef std::list< cPlugin* > PluginList;
cPlugin* GetPlugin( const char* a_Plugin ); //tolua_export cPlugin* GetPlugin( const char* a_Plugin ) const; //tolua_export
const PluginList & GetAllPlugins(); // >> EXPORTED IN MANUALBINDINGS << const PluginList & GetAllPlugins() const; // >> EXPORTED IN MANUALBINDINGS <<
void ReloadPlugins(); //tolua_export void ReloadPlugins(); //tolua_export
bool AddPlugin( cPlugin* a_Plugin ); bool AddPlugin( cPlugin* a_Plugin );
@ -41,7 +41,7 @@ public: //tolua_export
bool AddLuaPlugin( cPlugin_Lua* a_Plugin ); bool AddLuaPlugin( cPlugin_Lua* a_Plugin );
void AddHook( cPlugin* a_Plugin, PluginHook a_Hook ); //tolua_export void AddHook( cPlugin* a_Plugin, PluginHook a_Hook ); //tolua_export
unsigned int GetNumPlugins(); //tolua_export unsigned int GetNumPlugins() const; //tolua_export
bool CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... ); bool CallHook( PluginHook a_Hook, unsigned int a_NumArgs, ... );
@ -50,14 +50,20 @@ public: //tolua_export
void RemoveLuaPlugin( std::string a_FileName ); //tolua_export void RemoveLuaPlugin( std::string a_FileName ); //tolua_export
cPlugin_Lua* GetLuaPlugin( lua_State* a_State ); //tolua_export cPlugin_Lua* GetLuaPlugin( lua_State* a_State ); //tolua_export
cLuaCommandBinder* GetLuaCommandBinder() { return m_LuaCommandBinder; } cLuaCommandBinder* GetLuaCommandBinder() const { return m_LuaCommandBinder; }
bool HasPlugin( cPlugin* a_Plugin ) const;
private: private:
friend class cRoot; friend class cRoot;
cPluginManager(); cPluginManager();
~cPluginManager(); ~cPluginManager();
struct sPluginManagerState; typedef std::list< cPlugin_Lua* > LuaPluginList;
sPluginManagerState* m_pState; typedef std::map< cPluginManager::PluginHook, cPluginManager::PluginList > HookMap;
LuaPluginList m_LuaPlugins;
PluginList m_Plugins;
HookMap m_Hooks;
void ReloadPluginsNow(); void ReloadPluginsNow();
void UnloadPluginsNow(); void UnloadPluginsNow();