diff options
Diffstat (limited to 'source/ManualBindings.cpp')
-rw-r--r-- | source/ManualBindings.cpp | 140 |
1 files changed, 110 insertions, 30 deletions
diff --git a/source/ManualBindings.cpp b/source/ManualBindings.cpp index a80e186eb..e6605ddb0 100644 --- a/source/ManualBindings.cpp +++ b/source/ManualBindings.cpp @@ -896,6 +896,70 @@ tolua_lerror: +class cLuaWorldTask : + public cWorld::cTask +{ +public: + cLuaWorldTask(cPluginLua & a_Plugin, int a_FnRef) : + m_Plugin(a_Plugin), + m_FnRef(a_FnRef) + { + } + +protected: + cPluginLua & m_Plugin; + int m_FnRef; + + // cWorld::cTask overrides: + virtual void Run(cWorld & a_World) override + { + m_Plugin.Call(m_FnRef, &a_World); + } +} ; + + + + + +static int tolua_cWorld_QueueTask(lua_State * tolua_S) +{ + // Binding for cWorld::QueueTask + // Params: function + + // Retrieve the cPlugin from the LuaState: + cPluginLua * Plugin = GetLuaPlugin(tolua_S); + if (Plugin == NULL) + { + // An error message has been already printed in GetLuaPlugin() + return 0; + } + + // Retrieve the args: + cWorld * self = (cWorld *)tolua_tousertype(tolua_S, 1, 0); + if (self == NULL) + { + return lua_do_error(tolua_S, "Error in function call '#funcname#': Not called on an object instance"); + } + if (!lua_isfunction(tolua_S, 2)) + { + return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a function for parameter #1"); + } + + // Create a reference to the function: + int FnRef = luaL_ref(tolua_S, LUA_REGISTRYINDEX); + if (FnRef == LUA_REFNIL) + { + return lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1"); + } + + self->QueueTask(new cLuaWorldTask(*Plugin, FnRef)); + return 0; +} + + + + + static int tolua_cPluginManager_GetAllPlugins(lua_State * tolua_S) { cPluginManager* self = (cPluginManager*) tolua_tousertype(tolua_S,1,0); @@ -1235,7 +1299,10 @@ static int tolua_cPluginManager_ForEachConsoleCommand(lua_State * tolua_S) static int tolua_cPluginManager_BindCommand(lua_State * L) { - // Function signature: cPluginManager:BindCommand(Command, Permission, Function, HelpString) + /* Function signatures: + cPluginManager:BindCommand(Command, Permission, Function, HelpString) + cPluginManager.BindCommand(Command, Permission, Function, HelpString) -- without the "self" param + */ cPluginLua * Plugin = GetLuaPlugin(L); if (Plugin == NULL) { @@ -1244,26 +1311,30 @@ static int tolua_cPluginManager_BindCommand(lua_State * L) // Read the arguments to this API call: tolua_Error tolua_err; + int idx = 1; + if (tolua_isusertype(L, 1, "cPluginManager", 0, &tolua_err)) + { + idx++; + } if ( - !tolua_isusertype (L, 1, "cPluginManager", 0, &tolua_err) || - !tolua_iscppstring(L, 2, 0, &tolua_err) || - !tolua_iscppstring(L, 3, 0, &tolua_err) || - !tolua_iscppstring(L, 5, 0, &tolua_err) || - !tolua_isnoobj (L, 6, &tolua_err) + !tolua_iscppstring(L, idx, 0, &tolua_err) || + !tolua_iscppstring(L, idx + 1, 0, &tolua_err) || + !tolua_iscppstring(L, idx + 3, 0, &tolua_err) || + !tolua_isnoobj (L, idx + 4, &tolua_err) ) { tolua_error(L, "#ferror in function 'BindCommand'.", &tolua_err); return 0; } - if (!lua_isfunction(L, 4)) + if (!lua_isfunction(L, idx + 2)) { luaL_error(L, "\"BindCommand\" function expects a function as its 3rd parameter. Command-binding aborted."); return 0; } - cPluginManager * self = (cPluginManager *)tolua_tousertype(L, 1, 0); - AString Command (tolua_tocppstring(L, 2, "")); - AString Permission(tolua_tocppstring(L, 3, "")); - AString HelpString(tolua_tocppstring(L, 5, "")); + cPluginManager * self = cPluginManager::Get(); + AString Command (tolua_tocppstring(L, idx, "")); + AString Permission(tolua_tocppstring(L, idx + 1, "")); + AString HelpString(tolua_tocppstring(L, idx + 3, "")); // Store the function reference: lua_pop(L, 1); // Pop the help string off the stack @@ -1290,37 +1361,42 @@ static int tolua_cPluginManager_BindCommand(lua_State * L) static int tolua_cPluginManager_BindConsoleCommand(lua_State * L) { - // Function signature: cPluginManager:BindConsoleCommand(Command, Function, HelpString) + /* Function signatures: + cPluginManager:BindConsoleCommand(Command, Function, HelpString) + cPluginManager.BindConsoleCommand(Command, Function, HelpString) -- without the "self" param + */ // Get the plugin identification out of LuaState: - lua_getglobal(L, LUA_PLUGIN_INSTANCE_VAR_NAME); - if (!lua_islightuserdata(L, -1)) + cPluginLua * Plugin = GetLuaPlugin(L); + if (Plugin == NULL) { - LOGERROR("cPluginManager:BindConsoleCommand() cannot get plugin instance, what have you done to my Lua state? Command-binding aborted."); + return 0; } - cPluginLua * Plugin = (cPluginLua *)lua_topointer(L, -1); - lua_pop(L, 1); // Read the arguments to this API call: tolua_Error tolua_err; + int idx = 1; + if (tolua_isusertype(L, 1, "cPluginManager", 0, &tolua_err)) + { + idx++; + } if ( - !tolua_isusertype (L, 1, "cPluginManager", 0, &tolua_err) || // self - !tolua_iscppstring(L, 2, 0, &tolua_err) || // Command - !tolua_iscppstring(L, 4, 0, &tolua_err) || // HelpString - !tolua_isnoobj (L, 5, &tolua_err) + !tolua_iscppstring(L, idx, 0, &tolua_err) || // Command + !tolua_iscppstring(L, idx + 2, 0, &tolua_err) || // HelpString + !tolua_isnoobj (L, idx + 3, &tolua_err) ) { tolua_error(L, "#ferror in function 'BindConsoleCommand'.", &tolua_err); return 0; } - if (!lua_isfunction(L, 3)) + if (!lua_isfunction(L, idx + 1)) { luaL_error(L, "\"BindConsoleCommand\" function expects a function as its 2nd parameter. Command-binding aborted."); return 0; } - cPluginManager * self = (cPluginManager *)tolua_tousertype(L, 1, 0); - AString Command (tolua_tocppstring(L, 2, "")); - AString HelpString(tolua_tocppstring(L, 4, "")); + cPluginManager * self = cPluginManager::Get(); + AString Command (tolua_tocppstring(L, idx, "")); + AString HelpString(tolua_tocppstring(L, idx + 2, "")); // Store the function reference: lua_pop(L, 1); // Pop the help string off the stack @@ -1489,14 +1565,16 @@ static int tolua_cPluginLua_AddWebTab(lua_State * tolua_S) tolua_Error tolua_err; tolua_err.array = 0; - tolua_err.index = 0; - tolua_err.type = 0; + tolua_err.index = 3; + tolua_err.type = "function"; std::string Title = ""; int Reference = LUA_REFNIL; - if( tolua_isstring( tolua_S, 2, 0, &tolua_err ) && - lua_isfunction( tolua_S, 3 ) ) + if ( + tolua_isstring(tolua_S, 2, 0, &tolua_err ) && + lua_isfunction(tolua_S, 3 ) + ) { Reference = luaL_ref(tolua_S, LUA_REGISTRYINDEX); Title = ((std::string) tolua_tocppstring(tolua_S,2,0)); @@ -2018,6 +2096,8 @@ void ManualBindings::Bind(lua_State * tolua_S) tolua_function(tolua_S, "ForEachPlayer", tolua_ForEach< cWorld, cPlayer, &cWorld::ForEachPlayer>); tolua_function(tolua_S, "GetBlockInfo", tolua_cWorld_GetBlockInfo); tolua_function(tolua_S, "GetBlockTypeMeta", tolua_cWorld_GetBlockTypeMeta); + tolua_function(tolua_S, "GetSignLines", tolua_cWorld_GetSignLines); + tolua_function(tolua_S, "QueueTask", tolua_cWorld_QueueTask); tolua_function(tolua_S, "SetSignLines", tolua_cWorld_SetSignLines); tolua_function(tolua_S, "TryGetHeight", tolua_cWorld_TryGetHeight); tolua_function(tolua_S, "UpdateSign", tolua_cWorld_SetSignLines); @@ -2028,12 +2108,12 @@ void ManualBindings::Bind(lua_State * tolua_S) tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S, "cPluginManager"); + tolua_function(tolua_S, "AddHook", tolua_cPluginManager_AddHook); tolua_function(tolua_S, "BindCommand", tolua_cPluginManager_BindCommand); tolua_function(tolua_S, "BindConsoleCommand", tolua_cPluginManager_BindConsoleCommand); tolua_function(tolua_S, "ForEachCommand", tolua_cPluginManager_ForEachCommand); tolua_function(tolua_S, "ForEachConsoleCommand", tolua_cPluginManager_ForEachConsoleCommand); tolua_function(tolua_S, "GetAllPlugins", tolua_cPluginManager_GetAllPlugins); - tolua_function(tolua_S, "AddHook", tolua_cPluginManager_AddHook); tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S, "cPlayer"); |