From db4ea4658c58772ee447ff0eff8bb39b692081ec Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Tue, 15 Apr 2014 13:30:46 -0400 Subject: Only push the Lua error handler once --- src/script/cpp_api/s_base.cpp | 27 +++++++------ src/script/cpp_api/s_base.h | 3 ++ src/script/cpp_api/s_entity.cpp | 75 ++++++++++++++++-------------------- src/script/cpp_api/s_inventory.cpp | 55 +++++++++------------------ src/script/cpp_api/s_item.cpp | 58 +++++++++++----------------- src/script/cpp_api/s_mainmenu.cpp | 27 +++++-------- src/script/cpp_api/s_node.cpp | 78 ++++++++++---------------------------- src/script/cpp_api/s_nodemeta.cpp | 66 ++++++++++---------------------- src/script/cpp_api/s_server.cpp | 44 ++++++++------------- 9 files changed, 154 insertions(+), 279 deletions(-) (limited to 'src/script') diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index 932cc5015..774b3f51d 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -72,6 +72,10 @@ ScriptApiBase::ScriptApiBase() m_luastack = luaL_newstate(); assert(m_luastack); + // Add and save an error handler + lua_pushcfunction(m_luastack, script_error_handler); + m_errorhandler = lua_gettop(m_luastack); + // Make the ScriptApiBase* accessible to ModApiBase lua_pushlightuserdata(m_luastack, this); lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi"); @@ -125,23 +129,18 @@ bool ScriptApiBase::loadScript(const std::string &scriptpath) lua_State *L = getStack(); - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - - int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, errorhandler); - if(ret){ - errorstream<<"========== ERROR FROM LUA ==========="<weight); lua_getfield(L, -1, "collisionbox"); - if(lua_istable(L, -1)) + if (lua_istable(L, -1)) prop->collisionbox = read_aabb3f(L, -1, 1.0); lua_pop(L, 1); @@ -199,9 +194,6 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - //infostream<<"scriptapi_luaentity_step: id="< items; - for(u32 i=0; igetSize(); i++) + for (u32 i = 0; i < old_craft_grid->getSize(); i++) { items.push_back(old_craft_grid->getItem(i)); + } push_items(L, items); InvRef::create(L, craft_inv); - if(lua_pcall(L, 4, 1, errorhandler)) + if (lua_pcall(L, 4, 1, m_errorhandler)) scriptError(); - if(!lua_isnil(L, -1)) { + if (!lua_isnil(L, -1)) { try { item = read_item(L,-1, getServer()); } catch (LuaError &e) { throw LuaError(std::string(e.what()) + ". item=" + item.name); } } - lua_pop(L, 2); // Pop item and error handler + lua_pop(L, 1); // Pop item return true; } @@ -154,9 +143,6 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - lua_getglobal(L, "minetest"); lua_getfield(L, -1, "craft_predict"); LuaItemStack::create(L, item); @@ -164,21 +150,22 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user, //Push inventory list std::vector items; - for(u32 i=0; igetSize(); i++) + for (u32 i = 0; i < old_craft_grid->getSize(); i++) { items.push_back(old_craft_grid->getItem(i)); + } push_items(L, items); InvRef::create(L, craft_inv); - if(lua_pcall(L, 4, 1, errorhandler)) + if (lua_pcall(L, 4, 1, m_errorhandler)) scriptError(); - if(!lua_isnil(L, -1)) { + if (!lua_isnil(L, -1)) { try { item = read_item(L,-1, getServer()); } catch (LuaError &e) { throw LuaError(std::string(e.what()) + ". item=" + item.name); } } - lua_pop(L, 2); // Pop item and error handler + lua_pop(L, 1); // Pop item return true; } @@ -252,4 +239,3 @@ void ScriptApiItem::pushPointedThing(const PointedThing& pointed) } } - diff --git a/src/script/cpp_api/s_mainmenu.cpp b/src/script/cpp_api/s_mainmenu.cpp index 5c54f7368..62baeb406 100644 --- a/src/script/cpp_api/s_mainmenu.cpp +++ b/src/script/cpp_api/s_mainmenu.cpp @@ -37,14 +37,11 @@ void ScriptApiMainMenu::handleMainMenuEvent(std::string text) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - // Get handler function lua_getglobal(L, "engine"); lua_getfield(L, -1, "event_handler"); lua_remove(L, -2); // Remove engine - if(lua_isnil(L, -1)) { + if (lua_isnil(L, -1)) { lua_pop(L, 1); // Pop event_handler return; } @@ -52,41 +49,37 @@ void ScriptApiMainMenu::handleMainMenuEvent(std::string text) // Call it lua_pushstring(L, text.c_str()); - if(lua_pcall(L, 1, 0, errorhandler)) + if (lua_pcall(L, 1, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } void ScriptApiMainMenu::handleMainMenuButtons(std::map fields) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - // Get handler function lua_getglobal(L, "engine"); lua_getfield(L, -1, "button_handler"); lua_remove(L, -2); // Remove engine - if(lua_isnil(L, -1)) { + if (lua_isnil(L, -1)) { lua_pop(L, 1); // Pop button handler return; } luaL_checktype(L, -1, LUA_TFUNCTION); - // Convert fields to lua table + // Convert fields to a Lua table lua_newtable(L); - for(std::map::const_iterator - i = fields.begin(); i != fields.end(); i++){ - const std::string &name = i->first; - const std::string &value = i->second; + std::map::const_iterator it; + for (it = fields.begin(); it != fields.end(); it++){ + const std::string &name = it->first; + const std::string &value = it->second; lua_pushstring(L, name.c_str()); lua_pushlstring(L, value.c_str(), value.size()); lua_settable(L, -3); } // Call it - if(lua_pcall(L, 1, 0, errorhandler)) + if (lua_pcall(L, 1, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } + diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp index 55db43584..5d51ebdd4 100644 --- a/src/script/cpp_api/s_node.cpp +++ b/src/script/cpp_api/s_node.cpp @@ -92,13 +92,10 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // Push callback function on stack - if(!getItemCallback(ndef->get(node).name.c_str(), "on_punch")) + if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch")) return false; // Call function @@ -106,9 +103,8 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node, pushnode(L, node, ndef); objectrefGetOrCreate(puncher); pushPointedThing(pointed); - if(lua_pcall(L, 4, 0, errorhandler)) + if (lua_pcall(L, 4, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler return true; } @@ -117,22 +113,18 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // Push callback function on stack - if(!getItemCallback(ndef->get(node).name.c_str(), "on_dig")) + if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig")) return false; // Call function push_v3s16(L, p); pushnode(L, node, ndef); objectrefGetOrCreate(digger); - if(lua_pcall(L, 3, 0, errorhandler)) + if (lua_pcall(L, 3, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler return true; } @@ -140,82 +132,66 @@ void ScriptApiNode::node_on_construct(v3s16 p, MapNode node) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // Push callback function on stack - if(!getItemCallback(ndef->get(node).name.c_str(), "on_construct")) + if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct")) return; // Call function push_v3s16(L, p); - if(lua_pcall(L, 1, 0, errorhandler)) + if (lua_pcall(L, 1, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // Push callback function on stack - if(!getItemCallback(ndef->get(node).name.c_str(), "on_destruct")) + if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct")) return; // Call function push_v3s16(L, p); - if(lua_pcall(L, 1, 0, errorhandler)) + if (lua_pcall(L, 1, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // Push callback function on stack - if(!getItemCallback(ndef->get(node).name.c_str(), "after_destruct")) + if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct")) return; // Call function push_v3s16(L, p); pushnode(L, node, ndef); - if(lua_pcall(L, 2, 0, errorhandler)) + if (lua_pcall(L, 2, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // Push callback function on stack - if(!getItemCallback(ndef->get(node).name.c_str(), "on_timer")) + if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer")) return false; // Call function push_v3s16(L, p); lua_pushnumber(L,dtime); - if(lua_pcall(L, 2, 1, errorhandler)) + if (lua_pcall(L, 2, 1, m_errorhandler)) scriptError(); - lua_remove(L, errorhandler); // Remove error handler return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true; } @@ -226,63 +202,51 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // If node doesn't exist, we don't know what callback to call MapNode node = getEnv()->getMap().getNodeNoEx(p); - if(node.getContent() == CONTENT_IGNORE) + if (node.getContent() == CONTENT_IGNORE) return; // Push callback function on stack - if(!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields")) + if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields")) return; // Call function push_v3s16(L, p); // pos lua_pushstring(L, formname.c_str()); // formname lua_newtable(L); // fields - for(std::map::const_iterator - i = fields.begin(); i != fields.end(); i++){ - const std::string &name = i->first; - const std::string &value = i->second; + std::map::const_iterator it; + for (it = fields.begin(); it != fields.end(); it++){ + const std::string &name = it->first; + const std::string &value = it->second; lua_pushstring(L, name.c_str()); lua_pushlstring(L, value.c_str(), value.size()); lua_settable(L, -3); } objectrefGetOrCreate(sender); // player - if(lua_pcall(L, 4, 0, errorhandler)) + if (lua_pcall(L, 4, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } void ScriptApiNode::node_falling_update(v3s16 p) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - lua_getglobal(L, "nodeupdate"); push_v3s16(L, p); - if(lua_pcall(L, 1, 0, errorhandler)) + if (lua_pcall(L, 1, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } void ScriptApiNode::node_falling_update_single(v3s16 p) { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - lua_getglobal(L, "nodeupdate_single"); push_v3s16(L, p); - if(lua_pcall(L, 1, 0, errorhandler)) + if (lua_pcall(L, 1, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } diff --git a/src/script/cpp_api/s_nodemeta.cpp b/src/script/cpp_api/s_nodemeta.cpp index 2fb683943..bf6a4f73b 100644 --- a/src/script/cpp_api/s_nodemeta.cpp +++ b/src/script/cpp_api/s_nodemeta.cpp @@ -34,19 +34,16 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // If node doesn't exist, we don't know what callback to call MapNode node = getEnv()->getMap().getNodeNoEx(p); - if(node.getContent() == CONTENT_IGNORE) + if (node.getContent() == CONTENT_IGNORE) return 0; // Push callback function on stack std::string nodename = ndef->get(node).name; - if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_move")) + if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_move")) return count; // function(pos, from_list, from_index, to_list, to_index, count, player) @@ -57,10 +54,9 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p, lua_pushinteger(L, to_index + 1); // to_index lua_pushinteger(L, count); // count objectrefGetOrCreate(player); // player - if(lua_pcall(L, 7, 1, errorhandler)) + if (lua_pcall(L, 7, 1, m_errorhandler)) scriptError(); - lua_remove(L, errorhandler); // Remove error handler - if(!lua_isnumber(L, -1)) + if (!lua_isnumber(L, -1)) throw LuaError("allow_metadata_inventory_move should" " return a number, guilty node: " + nodename); int num = luaL_checkinteger(L, -1); @@ -75,19 +71,16 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // If node doesn't exist, we don't know what callback to call MapNode node = getEnv()->getMap().getNodeNoEx(p); - if(node.getContent() == CONTENT_IGNORE) + if (node.getContent() == CONTENT_IGNORE) return 0; // Push callback function on stack std::string nodename = ndef->get(node).name; - if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put")) + if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put")) return stack.count; // Call function(pos, listname, index, stack, player) @@ -96,9 +89,8 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p, lua_pushinteger(L, index + 1); // index LuaItemStack::create(L, stack); // stack objectrefGetOrCreate(player); // player - if(lua_pcall(L, 5, 1, errorhandler)) + if (lua_pcall(L, 5, 1, m_errorhandler)) scriptError(); - lua_remove(L, errorhandler); // Remove error handler if(!lua_isnumber(L, -1)) throw LuaError("allow_metadata_inventory_put should" " return a number, guilty node: " + nodename); @@ -114,19 +106,16 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // If node doesn't exist, we don't know what callback to call MapNode node = getEnv()->getMap().getNodeNoEx(p); - if(node.getContent() == CONTENT_IGNORE) + if (node.getContent() == CONTENT_IGNORE) return 0; // Push callback function on stack std::string nodename = ndef->get(node).name; - if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take")) + if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take")) return stack.count; // Call function(pos, listname, index, count, player) @@ -135,10 +124,9 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p, lua_pushinteger(L, index + 1); // index LuaItemStack::create(L, stack); // stack objectrefGetOrCreate(player); // player - if(lua_pcall(L, 5, 1, errorhandler)) + if (lua_pcall(L, 5, 1, m_errorhandler)) scriptError(); - lua_remove(L, errorhandler); // Remove error handler - if(!lua_isnumber(L, -1)) + if (!lua_isnumber(L, -1)) throw LuaError("allow_metadata_inventory_take should" " return a number, guilty node: " + nodename); int num = luaL_checkinteger(L, -1); @@ -154,19 +142,16 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // If node doesn't exist, we don't know what callback to call MapNode node = getEnv()->getMap().getNodeNoEx(p); - if(node.getContent() == CONTENT_IGNORE) + if (node.getContent() == CONTENT_IGNORE) return; // Push callback function on stack std::string nodename = ndef->get(node).name; - if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_move")) + if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_move")) return; // function(pos, from_list, from_index, to_list, to_index, count, player) @@ -177,9 +162,8 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p, lua_pushinteger(L, to_index + 1); // to_index lua_pushinteger(L, count); // count objectrefGetOrCreate(player); // player - if(lua_pcall(L, 7, 0, errorhandler)) + if (lua_pcall(L, 7, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } // Report put items @@ -189,19 +173,16 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // If node doesn't exist, we don't know what callback to call MapNode node = getEnv()->getMap().getNodeNoEx(p); - if(node.getContent() == CONTENT_IGNORE) + if (node.getContent() == CONTENT_IGNORE) return; // Push callback function on stack std::string nodename = ndef->get(node).name; - if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_put")) + if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_put")) return; // Call function(pos, listname, index, stack, player) @@ -210,9 +191,8 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p, lua_pushinteger(L, index + 1); // index LuaItemStack::create(L, stack); // stack objectrefGetOrCreate(player); // player - if(lua_pcall(L, 5, 0, errorhandler)) + if (lua_pcall(L, 5, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } // Report taken items @@ -222,19 +202,16 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - INodeDefManager *ndef = getServer()->ndef(); // If node doesn't exist, we don't know what callback to call MapNode node = getEnv()->getMap().getNodeNoEx(p); - if(node.getContent() == CONTENT_IGNORE) + if (node.getContent() == CONTENT_IGNORE) return; // Push callback function on stack std::string nodename = ndef->get(node).name; - if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_take")) + if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_take")) return; // Call function(pos, listname, index, stack, player) @@ -243,9 +220,8 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p, lua_pushinteger(L, index + 1); // index LuaItemStack::create(L, stack); // stack objectrefGetOrCreate(player); // player - if(lua_pcall(L, 5, 0, errorhandler)) + if (lua_pcall(L, 5, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } ScriptApiNodemeta::ScriptApiNodemeta() { @@ -254,5 +230,3 @@ ScriptApiNodemeta::ScriptApiNodemeta() { ScriptApiNodemeta::~ScriptApiNodemeta() { } - - diff --git a/src/script/cpp_api/s_server.cpp b/src/script/cpp_api/s_server.cpp index bffd4d3fa..7d21f968b 100644 --- a/src/script/cpp_api/s_server.cpp +++ b/src/script/cpp_api/s_server.cpp @@ -27,35 +27,31 @@ bool ScriptApiServer::getAuth(const std::string &playername, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - getAuthHandler(); lua_getfield(L, -1, "get_auth"); - if(lua_type(L, -1) != LUA_TFUNCTION) + if (lua_type(L, -1) != LUA_TFUNCTION) throw LuaError("Authentication handler missing get_auth"); lua_pushstring(L, playername.c_str()); - if(lua_pcall(L, 1, 1, errorhandler)) + if (lua_pcall(L, 1, 1, m_errorhandler)) scriptError(); lua_remove(L, -2); // Remove auth handler - lua_remove(L, errorhandler); // Remove error handler // nil = login not allowed - if(lua_isnil(L, -1)) + if (lua_isnil(L, -1)) return false; luaL_checktype(L, -1, LUA_TTABLE); std::string password; bool found = getstringfield(L, -1, "password", password); - if(!found) + if (!found) throw LuaError("Authentication handler didn't return password"); - if(dst_password) + if (dst_password) *dst_password = password; lua_getfield(L, -1, "privileges"); - if(!lua_istable(L, -1)) + if (!lua_istable(L, -1)) throw LuaError("Authentication handler didn't return privilege table"); - if(dst_privs) + if (dst_privs) readPrivileges(-1, *dst_privs); lua_pop(L, 1); @@ -68,12 +64,12 @@ void ScriptApiServer::getAuthHandler() lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_auth_handler"); - if(lua_isnil(L, -1)){ + if (lua_isnil(L, -1)){ lua_pop(L, 1); lua_getfield(L, -1, "builtin_auth_handler"); } lua_remove(L, -2); // Remove minetest - if(lua_type(L, -1) != LUA_TTABLE) + if (lua_type(L, -1) != LUA_TTABLE) throw LuaError("Authentication handler table not valid"); } @@ -83,13 +79,13 @@ void ScriptApiServer::readPrivileges(int index, std::set &result) result.clear(); lua_pushnil(L); - if(index < 0) + if (index < 0) index -= 1; - while(lua_next(L, index) != 0){ + while (lua_next(L, index) != 0) { // key at index -2 and value at index -1 std::string key = luaL_checkstring(L, -2); bool value = lua_toboolean(L, -1); - if(value) + if (value) result.insert(key); // removes value, keeps key for next iteration lua_pop(L, 1); @@ -101,19 +97,15 @@ void ScriptApiServer::createAuth(const std::string &playername, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - getAuthHandler(); lua_getfield(L, -1, "create_auth"); lua_remove(L, -2); // Remove auth handler - if(lua_type(L, -1) != LUA_TFUNCTION) + if (lua_type(L, -1) != LUA_TFUNCTION) throw LuaError("Authentication handler missing create_auth"); lua_pushstring(L, playername.c_str()); lua_pushstring(L, password.c_str()); - if(lua_pcall(L, 2, 0, errorhandler)) + if (lua_pcall(L, 2, 0, m_errorhandler)) scriptError(); - lua_pop(L, 1); // Pop error handler } bool ScriptApiServer::setPassword(const std::string &playername, @@ -121,19 +113,15 @@ bool ScriptApiServer::setPassword(const std::string &playername, { SCRIPTAPI_PRECHECKHEADER - lua_pushcfunction(L, script_error_handler); - int errorhandler = lua_gettop(L); - getAuthHandler(); lua_getfield(L, -1, "set_password"); lua_remove(L, -2); // Remove auth handler - if(lua_type(L, -1) != LUA_TFUNCTION) + if (lua_type(L, -1) != LUA_TFUNCTION) throw LuaError("Authentication handler missing set_password"); lua_pushstring(L, playername.c_str()); lua_pushstring(L, password.c_str()); - if(lua_pcall(L, 2, 1, errorhandler)) + if (lua_pcall(L, 2, 1, m_errorhandler)) scriptError(); - lua_remove(L, -2); // Remove error handler return lua_toboolean(L, -1); } -- cgit v1.2.3