lua_pop(L, 1);
lua_getfield(L, index, "sounds");
- if(lua_istable(L, -1)){
+ if (!lua_isnil(L, -1)) {
+ luaL_checktype(L, -1, LUA_TTABLE);
lua_getfield(L, -1, "place");
read_soundspec(L, -1, def.sound_place);
lua_pop(L, 1);
{
if(index < 0)
index = lua_gettop(L) + 1 + index;
- if(!lua_istable(L, index))
+ if (lua_isnil(L, index))
return;
+ luaL_checktype(L, -1, LUA_TTABLE);
+
int hp_max = 0;
if (getintfield(L, -1, "hp_max", hp_max)) {
prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
{
if(index < 0)
index = lua_gettop(L) + 1 + index;
- if(lua_isnil(L, index)){
- } else if(lua_istable(L, index)){
+ if (lua_isnil(L, index))
+ return;
+
+ if (lua_istable(L, index)) {
getstringfield(L, index, "name", spec.name);
getfloatfield(L, index, "gain", spec.gain);
getfloatfield(L, index, "fade", spec.fade);
getfloatfield(L, index, "pitch", spec.pitch);
- } else if(lua_isstring(L, index)){
+ } else if (lua_isstring(L, index)) {
spec.name = lua_tostring(L, index);
}
}
NodeBox read_nodebox(lua_State *L, int index)
{
NodeBox nodebox;
- if(lua_istable(L, -1)){
- nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
- ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
+ if (lua_isnil(L, -1))
+ return nodebox;
+
+ luaL_checktype(L, -1, LUA_TTABLE);
+
+ nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
+ ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
#define NODEBOXREAD(n, s){ \
lua_getfield(L, index, (s)); \
}
#define NODEBOXREADVEC(n, s) \
- lua_getfield(L, index, (s)); \
- if (lua_istable(L, -1)) \
- (n) = read_aabb3f_vector(L, -1, BS); \
- lua_pop(L, 1);
+ lua_getfield(L, index, (s)); \
+ if (lua_istable(L, -1)) \
+ (n) = read_aabb3f_vector(L, -1, BS); \
+ lua_pop(L, 1);
+
+ NODEBOXREADVEC(nodebox.fixed, "fixed");
+ NODEBOXREAD(nodebox.wall_top, "wall_top");
+ NODEBOXREAD(nodebox.wall_bottom, "wall_bottom");
+ NODEBOXREAD(nodebox.wall_side, "wall_side");
+ NODEBOXREADVEC(nodebox.connect_top, "connect_top");
+ NODEBOXREADVEC(nodebox.connect_bottom, "connect_bottom");
+ NODEBOXREADVEC(nodebox.connect_front, "connect_front");
+ NODEBOXREADVEC(nodebox.connect_left, "connect_left");
+ NODEBOXREADVEC(nodebox.connect_back, "connect_back");
+ NODEBOXREADVEC(nodebox.connect_right, "connect_right");
+ NODEBOXREADVEC(nodebox.disconnected_top, "disconnected_top");
+ NODEBOXREADVEC(nodebox.disconnected_bottom, "disconnected_bottom");
+ NODEBOXREADVEC(nodebox.disconnected_front, "disconnected_front");
+ NODEBOXREADVEC(nodebox.disconnected_left, "disconnected_left");
+ NODEBOXREADVEC(nodebox.disconnected_back, "disconnected_back");
+ NODEBOXREADVEC(nodebox.disconnected_right, "disconnected_right");
+ NODEBOXREADVEC(nodebox.disconnected, "disconnected");
+ NODEBOXREADVEC(nodebox.disconnected_sides, "disconnected_sides");
- NODEBOXREADVEC(nodebox.fixed, "fixed");
- NODEBOXREAD(nodebox.wall_top, "wall_top");
- NODEBOXREAD(nodebox.wall_bottom, "wall_bottom");
- NODEBOXREAD(nodebox.wall_side, "wall_side");
- NODEBOXREADVEC(nodebox.connect_top, "connect_top");
- NODEBOXREADVEC(nodebox.connect_bottom, "connect_bottom");
- NODEBOXREADVEC(nodebox.connect_front, "connect_front");
- NODEBOXREADVEC(nodebox.connect_left, "connect_left");
- NODEBOXREADVEC(nodebox.connect_back, "connect_back");
- NODEBOXREADVEC(nodebox.connect_right, "connect_right");
- NODEBOXREADVEC(nodebox.disconnected_top, "disconnected_top");
- NODEBOXREADVEC(nodebox.disconnected_bottom, "disconnected_bottom");
- NODEBOXREADVEC(nodebox.disconnected_front, "disconnected_front");
- NODEBOXREADVEC(nodebox.disconnected_left, "disconnected_left");
- NODEBOXREADVEC(nodebox.disconnected_back, "disconnected_back");
- NODEBOXREADVEC(nodebox.disconnected_right, "disconnected_right");
- NODEBOXREADVEC(nodebox.disconnected, "disconnected");
- NODEBOXREADVEC(nodebox.disconnected_sides, "disconnected_sides");
- }
return nodebox;
}
/******************************************************************************/
void read_groups(lua_State *L, int index, ItemGroupList &result)
{
- if (!lua_istable(L,index))
+ if (lua_isnil(L, index))
return;
+
+ luaL_checktype(L, index, LUA_TTABLE);
+
result.clear();
lua_pushnil(L);
if (index < 0)
#define CHECK_TYPE(index, name, type) { \
int t = lua_type(L, (index)); \
if (t != (type)) { \
- std::string traceback = script_get_backtrace(L); \
throw LuaError(std::string("Invalid ") + (name) + \
" (expected " + lua_typename(L, (type)) + \
- " got " + lua_typename(L, t) + ").\n" + traceback); \
+ " got " + lua_typename(L, t) + ")."); \
} \
}
#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
Table field getters
*/
+bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname)
+{
+ if (lua_isnil(L, index))
+ return false;
+
+ CHECK_TYPE(index, std::string("field \"") + fieldname + '"', type);
+ return true;
+}
+
bool getstringfield(lua_State *L, int table,
const char *fieldname, std::string &result)
{
lua_getfield(L, table, fieldname);
bool got = false;
- if(lua_isstring(L, -1)){
+
+ if (check_field_or_nil(L, -1, LUA_TSTRING, fieldname)) {
size_t len = 0;
const char *ptr = lua_tolstring(L, -1, &len);
if (ptr) {
{
lua_getfield(L, table, fieldname);
bool got = false;
- if(lua_isnumber(L, -1)){
+
+ if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)) {
result = lua_tonumber(L, -1);
got = true;
}
{
lua_getfield(L, table, fieldname);
bool got = false;
- if(lua_isboolean(L, -1)){
+
+ if (check_field_or_nil(L, -1, LUA_TBOOLEAN, fieldname)){
result = lua_toboolean(L, -1);
got = true;
}
return num_strings_read;
}
-std::string checkstringfield(lua_State *L, int table,
- const char *fieldname)
-{
- lua_getfield(L, table, fieldname);
- CHECK_TYPE(-1, std::string("field \"") + fieldname + '"', LUA_TSTRING);
- size_t len;
- const char *s = lua_tolstring(L, -1, &len);
- lua_pop(L, 1);
- return std::string(s, len);
-}
-
std::string getstringfield_default(lua_State *L, int table,
const char *fieldname, const std::string &default_)
{
int getintfield_default(lua_State *L, int table,
const char *fieldname, int default_);
+bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname);
+
template<typename T>
bool getintfield(lua_State *L, int table,
const char *fieldname, T &result)
{
lua_getfield(L, table, fieldname);
bool got = false;
- if (lua_isnumber(L, -1)){
+ if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)){
result = lua_tointeger(L, -1);
got = true;
}
const char *fieldname, bool &result);
bool getfloatfield(lua_State *L, int table,
const char *fieldname, float &result);
-std::string checkstringfield(lua_State *L, int table,
- const char *fieldname);
void setstringfield(lua_State *L, int table,
const char *fieldname, const std::string &value);