]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Fix ObjectRef errors due to lua_isnil() (#10564)
authorZughy <63455151+Zughy@users.noreply.github.com>
Wed, 4 Nov 2020 20:43:18 +0000 (21:43 +0100)
committerGitHub <noreply@github.com>
Wed, 4 Nov 2020 20:43:18 +0000 (21:43 +0100)
Treat 'none' values as 'nil'

src/script/common/helper.cpp
src/script/common/helper.h
src/script/lua_api/l_object.cpp

index f53a2b7e8086d3a3cd4c4eaabd44c2970ec19167..488144790d479a4d483b1969daef1a292ce95c7c 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <cmath>
 #include <sstream>
 #include <irr_v2d.h>
+#include <irr_v3d.h>
 #include "c_types.h"
 #include "c_internal.h"
 
@@ -54,17 +55,14 @@ template <> bool LuaHelper::readParam(lua_State *L, int index)
        return lua_toboolean(L, index) != 0;
 }
 
-template <> bool LuaHelper::readParam(lua_State *L, int index, const bool &default_value)
+template <> s16 LuaHelper::readParam(lua_State *L, int index)
 {
-       if (lua_isnil(L, index))
-               return default_value;
-
-       return lua_toboolean(L, index) != 0;
+       return lua_tonumber(L, index);
 }
 
-template <> s16 LuaHelper::readParam(lua_State *L, int index)
+template <> int LuaHelper::readParam(lua_State *L, int index)
 {
-       return lua_tonumber(L, index);
+       return luaL_checkint(L, index);
 }
 
 template <> float LuaHelper::readParam(lua_State *L, int index)
@@ -105,6 +103,25 @@ template <> v2f LuaHelper::readParam(lua_State *L, int index)
        return p;
 }
 
+template <> v3f LuaHelper::readParam(lua_State *L, int index)
+{
+       v3f p;
+       CHECK_POS_TAB(index);
+       lua_getfield(L, index, "x");
+       CHECK_POS_COORD("x");
+       p.X = readParam<float>(L, -1);
+       lua_pop(L, 1);
+       lua_getfield(L, index, "y");
+       CHECK_POS_COORD("y");
+       p.Y = readParam<float>(L, -1);
+       lua_pop(L, 1);
+       lua_getfield(L, index, "z");
+       CHECK_POS_COORD("z");
+       p.Z = readParam<float>(L, -1);
+       lua_pop(L, 1);
+       return p;
+}
+
 template <> std::string LuaHelper::readParam(lua_State *L, int index)
 {
        size_t length;
@@ -113,16 +130,3 @@ template <> std::string LuaHelper::readParam(lua_State *L, int index)
        result.assign(str, length);
        return result;
 }
-
-template <>
-std::string LuaHelper::readParam(
-               lua_State *L, int index, const std::string &default_value)
-{
-       std::string result;
-       const char *str = lua_tostring(L, index);
-       if (str)
-               result.append(str);
-       else
-               result = default_value;
-       return result;
-}
index d639d6e1626d4256fbb5e8f83fb51d323b1edc17..7a794dc9ba5741a02b2d62d801d6d162df651205 100644 (file)
@@ -50,5 +50,8 @@ class LuaHelper
         * @return read value from Lua or default value if nil
         */
        template <typename T>
-       static T readParam(lua_State *L, int index, const T &default_value);
+       static inline T readParam(lua_State *L, int index, const T &default_value)
+       {
+               return lua_isnoneornil(L, index) ? default_value : readParam<T>(L, index);
+       }
 };
index beb8dd33939a97c21da6ba3551989f306805de5d..a6a7c05f978f523805e6abbd4ac16b63114691fc 100644 (file)
@@ -169,11 +169,9 @@ int ObjectRef::l_punch(lua_State *L)
        if (sao == nullptr || puncher == nullptr)
                return 0;
 
-       float time_from_last_punch = lua_isnil(L, 3) ?
-               1000000.0f : readParam<float>(L,3);
+       float time_from_last_punch = readParam<float>(L, 3, 1000000.0f);
        ToolCapabilities toolcap = read_tool_capabilities(L, 4);
-       v3f dir = lua_isnil(L, 5) ?
-               sao->getBasePosition() - puncher->getBasePosition() : check_v3f(L, 5);
+       v3f dir = readParam<v3f>(L, 5, sao->getBasePosition() - puncher->getBasePosition());
 
        dir.normalize();
        u16 src_original_hp = sao->getHP();
@@ -383,20 +381,12 @@ int ObjectRef::l_set_animation(lua_State *L)
        if (sao == nullptr)
                return 0;
 
-       v2f frames = v2f(1, 1);
-       if (!lua_isnil(L, 2))
-               frames = readParam<v2f>(L, 2);
-       float frame_speed = 15;
-       if (!lua_isnil(L, 3))
-               frame_speed = lua_tonumber(L, 3);
-       float frame_blend = 0;
-       if (!lua_isnil(L, 4))
-               frame_blend = lua_tonumber(L, 4);
-       bool frame_loop = true;
-       if (lua_isboolean(L, 5))
-               frame_loop = readParam<bool>(L, 5);
+       v2f frame_range   = readParam<v2f>(L,  2, v2f(1, 1));
+       float frame_speed = readParam<float>(L, 3, 15.0f);
+       float frame_blend = readParam<float>(L, 4, 0.0f);
+       bool frame_loop   = readParam<bool>(L, 5, true);
 
-       sao->setAnimation(frames, frame_speed, frame_blend, frame_loop);
+       sao->setAnimation(frame_range, frame_speed, frame_blend, frame_loop);
        return 0;
 }
 
@@ -436,7 +426,7 @@ int ObjectRef::l_set_local_animation(lua_State *L)
                if (!lua_isnil(L, 2+1))
                        frames[i] = read_v2s32(L, 2+i);
        }
-       float frame_speed = lua_isnil(L, 6) ? 30 : readParam<float>(L, 6);
+       float frame_speed = readParam<float>(L, 6, 30.0f);
 
        getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
        lua_pushboolean(L, true);
@@ -965,9 +955,9 @@ int ObjectRef::l_set_sprite(lua_State *L)
        if (entitysao == nullptr)
                return 0;
 
-       v2s16 start_frame       = lua_isnil(L, 2) ? v2s16(0,0) : readParam<v2s16>(L, 2);
-       int num_frames          = lua_isnil(L, 3) ? 1 : luaL_checkint(L, 3);
-       float framelength       = lua_isnil(L, 4) ? 0.2 : lua_tonumber(L, 4);
+       v2s16 start_frame = readParam<v2s16>(L, 2, v2s16(0,0));
+       int num_frames    = readParam<int>(L, 3, 1);
+       float framelength = readParam<float>(L, 4, 0.2f);
        bool select_x_by_camera = readParam<bool>(L, 5, false);
 
        entitysao->setSprite(start_frame, num_frames, framelength, select_x_by_camera);