]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_nodemeta.cpp
Fix CSM crash (#5779)
[dragonfireclient.git] / src / script / cpp_api / s_nodemeta.cpp
index e15abd40f34ddbdce91794e174b7298df66f9fb4..d050c0bc9f4ffe77d2bc6b27d4208301cd156ee8 100644 (file)
@@ -34,19 +34,18 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       int error_handler = PUSH_ERROR_HANDLER(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)
@@ -56,15 +55,13 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
        lua_pushstring(L, to_list.c_str());   // to_list
        lua_pushinteger(L, to_index + 1);     // to_index
        lua_pushinteger(L, count);            // count
-       objectrefGetOrCreate(player);         // player
-       if(lua_pcall(L, 7, 1, errorhandler))
-               scriptError();
-       lua_remove(L, errorhandler); // Remove error handler
-       if(!lua_isnumber(L, -1))
-               throw LuaError(NULL, "allow_metadata_inventory_move should"
+       objectrefGetOrCreate(L, player);      // player
+       PCALL_RES(lua_pcall(L, 7, 1, error_handler));
+       if (!lua_isnumber(L, -1))
+               throw LuaError("allow_metadata_inventory_move should"
                                " return a number, guilty node: " + nodename);
        int num = luaL_checkinteger(L, -1);
-       lua_pop(L, 1); // Pop integer
+       lua_pop(L, 2); // Pop integer and error handler
        return num;
 }
 
@@ -75,19 +72,18 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       int error_handler = PUSH_ERROR_HANDLER(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)
@@ -95,15 +91,13 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
        lua_pushstring(L, listname.c_str()); // listname
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 1, errorhandler))
-               scriptError();
-       lua_remove(L, errorhandler); // Remove error handler
+       objectrefGetOrCreate(L, player);     // player
+       PCALL_RES(lua_pcall(L, 5, 1, error_handler));
        if(!lua_isnumber(L, -1))
-               throw LuaError(NULL, "allow_metadata_inventory_put should"
+               throw LuaError("allow_metadata_inventory_put should"
                                " return a number, guilty node: " + nodename);
        int num = luaL_checkinteger(L, -1);
-       lua_pop(L, 1); // Pop integer
+       lua_pop(L, 2); // Pop integer and error handler
        return num;
 }
 
@@ -114,19 +108,18 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       int error_handler = PUSH_ERROR_HANDLER(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)
@@ -134,15 +127,13 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
        lua_pushstring(L, listname.c_str()); // listname
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 1, errorhandler))
-               scriptError();
-       lua_remove(L, errorhandler); // Remove error handler
-       if(!lua_isnumber(L, -1))
-               throw LuaError(NULL, "allow_metadata_inventory_take should"
+       objectrefGetOrCreate(L, player);     // player
+       PCALL_RES(lua_pcall(L, 5, 1, error_handler));
+       if (!lua_isnumber(L, -1))
+               throw LuaError("allow_metadata_inventory_take should"
                                " return a number, guilty node: " + nodename);
        int num = luaL_checkinteger(L, -1);
-       lua_pop(L, 1); // Pop integer
+       lua_pop(L, 2); // Pop integer and error handler
        return num;
 }
 
@@ -154,19 +145,18 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       int error_handler = PUSH_ERROR_HANDLER(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)
@@ -176,10 +166,9 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
        lua_pushstring(L, to_list.c_str());   // to_list
        lua_pushinteger(L, to_index + 1);     // to_index
        lua_pushinteger(L, count);            // count
-       objectrefGetOrCreate(player);         // player
-       if(lua_pcall(L, 7, 0, errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop error handler
+       objectrefGetOrCreate(L, player);      // player
+       PCALL_RES(lua_pcall(L, 7, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 // Report put items
@@ -189,19 +178,18 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       int error_handler = PUSH_ERROR_HANDLER(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)
@@ -209,10 +197,9 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
        lua_pushstring(L, listname.c_str()); // listname
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 0, errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop error handler
+       objectrefGetOrCreate(L, player);     // player
+       PCALL_RES(lua_pcall(L, 5, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 // Report taken items
@@ -222,19 +209,18 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       int error_handler = PUSH_ERROR_HANDLER(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)
@@ -242,17 +228,16 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
        lua_pushstring(L, listname.c_str()); // listname
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 0, errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop error handler
+       objectrefGetOrCreate(L, player);     // player
+       PCALL_RES(lua_pcall(L, 5, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
-ScriptApiNodemeta::ScriptApiNodemeta() {
+ScriptApiNodemeta::ScriptApiNodemeta()
+{
 }
 
-ScriptApiNodemeta::~ScriptApiNodemeta() {
+ScriptApiNodemeta::~ScriptApiNodemeta()
+{
 }
 
-
-