diff options
author | faketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2011-10-03 20:41:19 +0200 |
---|---|---|
committer | faketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2011-10-03 20:41:19 +0200 |
commit | 386d58b5862d8b76925c6523721594887606e82a (patch) | |
tree | ef073e7a843f4b75a4008d4b7383f7cdf08ceee5 /source/cLuaCommandBinder.cpp | |
parent | Visual Studio 2010 solution and project files (diff) | |
download | cuberite-386d58b5862d8b76925c6523721594887606e82a.tar cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.gz cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.bz2 cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.lz cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.xz cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.zst cuberite-386d58b5862d8b76925c6523721594887606e82a.zip |
Diffstat (limited to '')
-rw-r--r-- | source/cLuaCommandBinder.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/source/cLuaCommandBinder.cpp b/source/cLuaCommandBinder.cpp new file mode 100644 index 000000000..a800513e1 --- /dev/null +++ b/source/cLuaCommandBinder.cpp @@ -0,0 +1,106 @@ +#include "cLuaCommandBinder.h"
+#include "cMCLogger.h"
+#include "cPlayer.h"
+#include "cPlugin_Lua.h"
+
+#include "tolua++.h"
+
+extern std::vector<std::string> StringSplit(std::string str, std::string delim);
+extern bool report_errors(lua_State* lua, int status);
+
+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( 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 )
+{
+ std::vector<std::string> Split = StringSplit( a_Command, " ");
+ 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;
+ }
+ }
+
+ // For enabling 'self' in the function, it's kind of a hack I'm not sure this is the way to go
+ lua_pushvalue(func.LuaState, LUA_GLOBALSINDEX);
+ lua_pushstring(func.LuaState, "self");
+ tolua_pushusertype( func.LuaState, func.Plugin, "cPlugin" );
+ lua_rawset(func.LuaState, -3);
+ lua_pop(func.LuaState, 1);
+
+ LOGINFO("1. Stack size: %i", lua_gettop(func.LuaState) );
+ lua_rawgeti( func.LuaState, LUA_REGISTRYINDEX, func.Reference); // same as lua_getref()
+
+ // Push the split
+ LOGINFO("2. Stack size: %i", lua_gettop(func.LuaState) );
+ 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;
+ }
+ LOGINFO("3. Stack size: %i", lua_gettop(func.LuaState) );
+ // Push player
+ tolua_pushusertype( func.LuaState, a_Player, "cPlayer" );
+ LOGINFO("Calling bound function! :D");
+ int s = lua_pcall(func.LuaState, 2, 1, 0);
+ if( report_errors( func.LuaState, s ) )
+ {
+ LOGINFO("error. Stack size: %i", lua_gettop(func.LuaState) );
+ return false;
+ }
+ bool RetVal = (tolua_toboolean(func.LuaState, -1, 0) > 0);
+ lua_pop(func.LuaState, 1); // Pop return value
+ LOGINFO("ok. Stack size: %i", lua_gettop(func.LuaState) );
+ return RetVal;
+ }
+ return false;
+}
|