Skip to content

Commit 72b93ec

Browse files
authoredNov 4, 2020
Fix ObjectRef errors due to lua_isnil() (#10564)
Treat 'none' values as 'nil'
1 parent 39213bd commit 72b93ec

File tree

3 files changed

+39
-42
lines changed

3 files changed

+39
-42
lines changed
 

Diff for: ‎src/script/common/helper.cpp

+24-20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#include <cmath>
2222
#include <sstream>
2323
#include <irr_v2d.h>
24+
#include <irr_v3d.h>
2425
#include "c_types.h"
2526
#include "c_internal.h"
2627

@@ -54,17 +55,14 @@ template <> bool LuaHelper::readParam(lua_State *L, int index)
5455
return lua_toboolean(L, index) != 0;
5556
}
5657

57-
template <> bool LuaHelper::readParam(lua_State *L, int index, const bool &default_value)
58+
template <> s16 LuaHelper::readParam(lua_State *L, int index)
5859
{
59-
if (lua_isnil(L, index))
60-
return default_value;
61-
62-
return lua_toboolean(L, index) != 0;
60+
return lua_tonumber(L, index);
6361
}
6462

65-
template <> s16 LuaHelper::readParam(lua_State *L, int index)
63+
template <> int LuaHelper::readParam(lua_State *L, int index)
6664
{
67-
return lua_tonumber(L, index);
65+
return luaL_checkint(L, index);
6866
}
6967

7068
template <> float LuaHelper::readParam(lua_State *L, int index)
@@ -105,6 +103,25 @@ template <> v2f LuaHelper::readParam(lua_State *L, int index)
105103
return p;
106104
}
107105

106+
template <> v3f LuaHelper::readParam(lua_State *L, int index)
107+
{
108+
v3f p;
109+
CHECK_POS_TAB(index);
110+
lua_getfield(L, index, "x");
111+
CHECK_POS_COORD("x");
112+
p.X = readParam<float>(L, -1);
113+
lua_pop(L, 1);
114+
lua_getfield(L, index, "y");
115+
CHECK_POS_COORD("y");
116+
p.Y = readParam<float>(L, -1);
117+
lua_pop(L, 1);
118+
lua_getfield(L, index, "z");
119+
CHECK_POS_COORD("z");
120+
p.Z = readParam<float>(L, -1);
121+
lua_pop(L, 1);
122+
return p;
123+
}
124+
108125
template <> std::string LuaHelper::readParam(lua_State *L, int index)
109126
{
110127
size_t length;
@@ -113,16 +130,3 @@ template <> std::string LuaHelper::readParam(lua_State *L, int index)
113130
result.assign(str, length);
114131
return result;
115132
}
116-
117-
template <>
118-
std::string LuaHelper::readParam(
119-
lua_State *L, int index, const std::string &default_value)
120-
{
121-
std::string result;
122-
const char *str = lua_tostring(L, index);
123-
if (str)
124-
result.append(str);
125-
else
126-
result = default_value;
127-
return result;
128-
}

Diff for: ‎src/script/common/helper.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@ class LuaHelper
5050
* @return read value from Lua or default value if nil
5151
*/
5252
template <typename T>
53-
static T readParam(lua_State *L, int index, const T &default_value);
53+
static inline T readParam(lua_State *L, int index, const T &default_value)
54+
{
55+
return lua_isnoneornil(L, index) ? default_value : readParam<T>(L, index);
56+
}
5457
};

Diff for: ‎src/script/lua_api/l_object.cpp

+11-21
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,9 @@ int ObjectRef::l_punch(lua_State *L)
169169
if (sao == nullptr || puncher == nullptr)
170170
return 0;
171171

172-
float time_from_last_punch = lua_isnil(L, 3) ?
173-
1000000.0f : readParam<float>(L,3);
172+
float time_from_last_punch = readParam<float>(L, 3, 1000000.0f);
174173
ToolCapabilities toolcap = read_tool_capabilities(L, 4);
175-
v3f dir = lua_isnil(L, 5) ?
176-
sao->getBasePosition() - puncher->getBasePosition() : check_v3f(L, 5);
174+
v3f dir = readParam<v3f>(L, 5, sao->getBasePosition() - puncher->getBasePosition());
177175

178176
dir.normalize();
179177
u16 src_original_hp = sao->getHP();
@@ -383,20 +381,12 @@ int ObjectRef::l_set_animation(lua_State *L)
383381
if (sao == nullptr)
384382
return 0;
385383

386-
v2f frames = v2f(1, 1);
387-
if (!lua_isnil(L, 2))
388-
frames = readParam<v2f>(L, 2);
389-
float frame_speed = 15;
390-
if (!lua_isnil(L, 3))
391-
frame_speed = lua_tonumber(L, 3);
392-
float frame_blend = 0;
393-
if (!lua_isnil(L, 4))
394-
frame_blend = lua_tonumber(L, 4);
395-
bool frame_loop = true;
396-
if (lua_isboolean(L, 5))
397-
frame_loop = readParam<bool>(L, 5);
384+
v2f frame_range = readParam<v2f>(L, 2, v2f(1, 1));
385+
float frame_speed = readParam<float>(L, 3, 15.0f);
386+
float frame_blend = readParam<float>(L, 4, 0.0f);
387+
bool frame_loop = readParam<bool>(L, 5, true);
398388

399-
sao->setAnimation(frames, frame_speed, frame_blend, frame_loop);
389+
sao->setAnimation(frame_range, frame_speed, frame_blend, frame_loop);
400390
return 0;
401391
}
402392

@@ -436,7 +426,7 @@ int ObjectRef::l_set_local_animation(lua_State *L)
436426
if (!lua_isnil(L, 2+1))
437427
frames[i] = read_v2s32(L, 2+i);
438428
}
439-
float frame_speed = lua_isnil(L, 6) ? 30 : readParam<float>(L, 6);
429+
float frame_speed = readParam<float>(L, 6, 30.0f);
440430

441431
getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
442432
lua_pushboolean(L, true);
@@ -965,9 +955,9 @@ int ObjectRef::l_set_sprite(lua_State *L)
965955
if (entitysao == nullptr)
966956
return 0;
967957

968-
v2s16 start_frame = lua_isnil(L, 2) ? v2s16(0,0) : readParam<v2s16>(L, 2);
969-
int num_frames = lua_isnil(L, 3) ? 1 : luaL_checkint(L, 3);
970-
float framelength = lua_isnil(L, 4) ? 0.2 : lua_tonumber(L, 4);
958+
v2s16 start_frame = readParam<v2s16>(L, 2, v2s16(0,0));
959+
int num_frames = readParam<int>(L, 3, 1);
960+
float framelength = readParam<float>(L, 4, 0.2f);
971961
bool select_x_by_camera = readParam<bool>(L, 5, false);
972962

973963
entitysao->setSprite(start_frame, num_frames, framelength, select_x_by_camera);

0 commit comments

Comments
 (0)
Please sign in to comment.