2012-06-14 06:06:06 -07:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 15:09:57 -07:00
|
|
|
#include "LuaCommandBinder.h"
|
|
|
|
#include "Player.h"
|
|
|
|
#include "Plugin.h"
|
2012-06-14 06:06:06 -07:00
|
|
|
|
|
|
|
#include "tolua++.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-13 16:34:47 -07:00
|
|
|
bool report_errors(lua_State* lua, int status)
|
|
|
|
{
|
|
|
|
if ( status!=0 )
|
|
|
|
{
|
|
|
|
std::string s = lua_tostring(lua, -1);
|
|
|
|
LOGERROR("-- %s", s.c_str() );
|
|
|
|
lua_pop(lua, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-14 06:06:06 -07:00
|
|
|
|
|
|
|
cLuaCommandBinder::cLuaCommandBinder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cLuaCommandBinder::~cLuaCommandBinder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cLuaCommandBinder::ClearBindings()
|
|
|
|
{
|
|
|
|
m_BoundCommands.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cLuaCommandBinder::RemoveBindingsForPlugin( cPlugin* a_Plugin )
|
|
|
|
{
|
|
|
|
for( CommandMap::iterator itr = m_BoundCommands.begin(); itr != m_BoundCommands.end(); )
|
|
|
|
{
|
|
|
|
if( itr->second.Plugin == a_Plugin )
|
|
|
|
{
|
|
|
|
LOGINFO("Unbinding %s ", itr->first.c_str( ) );
|
|
|
|
luaL_unref( itr->second.LuaState, LUA_REGISTRYINDEX, itr->second.Reference ); // unreference
|
|
|
|
CommandMap::iterator eraseme = itr;
|
|
|
|
++itr;
|
|
|
|
m_BoundCommands.erase( eraseme );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() )
|
|
|
|
{
|
|
|
|
LOGERROR("ERROR: Trying to bind command \"%s\" that has already been bound.", a_Command.c_str() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOGINFO("Binding %s (%s)", a_Command.c_str(), a_Permission.c_str() );
|
|
|
|
m_BoundCommands[ a_Command ] = BoundFunction( a_Plugin, a_LuaState, a_FunctionReference, a_Permission );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cLuaCommandBinder::HandleCommand( const std::string & a_Command, cPlayer* a_Player )
|
|
|
|
{
|
|
|
|
AStringVector Split = StringSplit(a_Command, " ");
|
|
|
|
if (Split.size() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandMap::iterator FoundCommand = m_BoundCommands.find( Split[0] );
|
|
|
|
if( FoundCommand != m_BoundCommands.end() )
|
|
|
|
{
|
|
|
|
const BoundFunction & func = FoundCommand->second;
|
|
|
|
if( func.Permission.size() > 0 )
|
|
|
|
{
|
|
|
|
if( !a_Player->HasPermission( func.Permission.c_str() ) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-10 12:25:16 -07:00
|
|
|
LOGD("1. Stack size: %i", lua_gettop(func.LuaState) );
|
2012-06-14 06:06:06 -07:00
|
|
|
lua_rawgeti( func.LuaState, LUA_REGISTRYINDEX, func.Reference); // same as lua_getref()
|
|
|
|
|
|
|
|
// Push the split
|
2012-10-10 12:25:16 -07:00
|
|
|
LOGD("2. Stack size: %i", lua_gettop(func.LuaState) );
|
2012-06-14 06:06:06 -07:00
|
|
|
lua_createtable(func.LuaState, Split.size(), 0);
|
|
|
|
int newTable = lua_gettop(func.LuaState);
|
|
|
|
int index = 1;
|
|
|
|
std::vector<std::string>::const_iterator iter = Split.begin();
|
|
|
|
while(iter != Split.end()) {
|
|
|
|
tolua_pushstring( func.LuaState, (*iter).c_str() );
|
|
|
|
lua_rawseti(func.LuaState, newTable, index);
|
|
|
|
++iter;
|
|
|
|
++index;
|
|
|
|
}
|
2012-10-10 12:25:16 -07:00
|
|
|
LOGD("3. Stack size: %i", lua_gettop(func.LuaState) );
|
2012-06-14 06:06:06 -07:00
|
|
|
// Push player
|
|
|
|
tolua_pushusertype( func.LuaState, a_Player, "cPlayer" );
|
2012-10-10 12:25:16 -07:00
|
|
|
LOGD("Calling bound function! :D");
|
2012-06-14 06:06:06 -07:00
|
|
|
int s = lua_pcall(func.LuaState, 2, 1, 0);
|
|
|
|
if( report_errors( func.LuaState, s ) )
|
|
|
|
{
|
2012-10-10 12:25:16 -07:00
|
|
|
LOGERROR("error. Stack size: %i", lua_gettop(func.LuaState) );
|
2012-06-14 06:06:06 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool RetVal = (tolua_toboolean(func.LuaState, -1, 0) > 0);
|
|
|
|
lua_pop(func.LuaState, 1); // Pop return value
|
2012-10-10 12:25:16 -07:00
|
|
|
LOGD("ok. Stack size: %i", lua_gettop(func.LuaState) );
|
2012-06-14 06:06:06 -07:00
|
|
|
return RetVal;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|