3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "util/numeric.h"
26 #include "util/serialize.h"
27 #include "util/string.h"
28 #include "common/c_converter.h"
29 #include "common/c_internal.h"
30 #include "constants.h"
35 #define CHECK_TYPE(index, name, type) do { \
36 int t = lua_type(L, (index)); \
38 throw LuaError(std::string("Invalid ") + (name) + \
39 " (expected " + lua_typename(L, (type)) + \
40 " got " + lua_typename(L, t) + ")."); \
44 #define CHECK_FLOAT(value, name) do {\
45 if (std::isnan(value) || std::isinf(value)) { \
46 throw LuaError("Invalid float value for '" name \
47 "' (NaN or infinity)"); \
51 #define CHECK_POS_COORD(index, name) CHECK_TYPE(index, "vector coordinate " name, LUA_TNUMBER)
52 #define CHECK_POS_TAB(index) CHECK_TYPE(index, "vector", LUA_TTABLE)
56 * A helper which calls CUSTOM_RIDX_READ_VECTOR with the argument at the given index
58 static void read_v3_aux(lua_State *L, int index)
60 lua_pushvalue(L, index);
61 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_VECTOR);
66 // Retrieve an integer vector where all components are optional
68 static bool getv3intfield(lua_State *L, int index,
69 const char *fieldname, T &result)
71 lua_getfield(L, index, fieldname);
73 if (lua_istable(L, -1)) {
74 got |= getintfield(L, -1, "x", result.X);
75 got |= getintfield(L, -1, "y", result.Y);
76 got |= getintfield(L, -1, "z", result.Z);
82 void push_v3f(lua_State *L, v3f p)
84 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
85 lua_pushnumber(L, p.X);
86 lua_pushnumber(L, p.Y);
87 lua_pushnumber(L, p.Z);
91 void push_v2f(lua_State *L, v2f p)
93 lua_createtable(L, 0, 2);
94 lua_pushnumber(L, p.X);
95 lua_setfield(L, -2, "x");
96 lua_pushnumber(L, p.Y);
97 lua_setfield(L, -2, "y");
100 v2s16 read_v2s16(lua_State *L, int index)
103 CHECK_POS_TAB(index);
104 lua_getfield(L, index, "x");
105 p.X = lua_tonumber(L, -1);
107 lua_getfield(L, index, "y");
108 p.Y = lua_tonumber(L, -1);
113 void push_v2s16(lua_State *L, v2s16 p)
115 lua_createtable(L, 0, 2);
116 lua_pushinteger(L, p.X);
117 lua_setfield(L, -2, "x");
118 lua_pushinteger(L, p.Y);
119 lua_setfield(L, -2, "y");
122 void push_v2s32(lua_State *L, v2s32 p)
124 lua_createtable(L, 0, 2);
125 lua_pushinteger(L, p.X);
126 lua_setfield(L, -2, "x");
127 lua_pushinteger(L, p.Y);
128 lua_setfield(L, -2, "y");
131 v2s32 read_v2s32(lua_State *L, int index)
134 CHECK_POS_TAB(index);
135 lua_getfield(L, index, "x");
136 p.X = lua_tonumber(L, -1);
138 lua_getfield(L, index, "y");
139 p.Y = lua_tonumber(L, -1);
144 v2f read_v2f(lua_State *L, int index)
147 CHECK_POS_TAB(index);
148 lua_getfield(L, index, "x");
149 p.X = lua_tonumber(L, -1);
151 lua_getfield(L, index, "y");
152 p.Y = lua_tonumber(L, -1);
157 v2f check_v2f(lua_State *L, int index)
160 CHECK_POS_TAB(index);
161 lua_getfield(L, index, "x");
162 CHECK_POS_COORD(-1, "x");
163 p.X = lua_tonumber(L, -1);
164 CHECK_FLOAT(p.X, "x");
166 lua_getfield(L, index, "y");
167 CHECK_POS_COORD(-1, "y");
168 p.Y = lua_tonumber(L, -1);
169 CHECK_FLOAT(p.Y, "y");
174 v3f read_v3f(lua_State *L, int index)
176 read_v3_aux(L, index);
177 float x = lua_tonumber(L, -3);
178 float y = lua_tonumber(L, -2);
179 float z = lua_tonumber(L, -1);
184 v3f check_v3f(lua_State *L, int index)
186 read_v3_aux(L, index);
187 CHECK_POS_COORD(-3, "x");
188 CHECK_POS_COORD(-2, "y");
189 CHECK_POS_COORD(-1, "z");
190 float x = lua_tonumber(L, -3);
191 float y = lua_tonumber(L, -2);
192 float z = lua_tonumber(L, -1);
197 v3d read_v3d(lua_State *L, int index)
199 read_v3_aux(L, index);
200 double x = lua_tonumber(L, -3);
201 double y = lua_tonumber(L, -2);
202 double z = lua_tonumber(L, -1);
207 v3d check_v3d(lua_State *L, int index)
209 read_v3_aux(L, index);
210 CHECK_POS_COORD(-3, "x");
211 CHECK_POS_COORD(-2, "y");
212 CHECK_POS_COORD(-1, "z");
213 double x = lua_tonumber(L, -3);
214 double y = lua_tonumber(L, -2);
215 double z = lua_tonumber(L, -1);
220 void push_ARGB8(lua_State *L, video::SColor color)
222 lua_createtable(L, 0, 4);
223 lua_pushinteger(L, color.getAlpha());
224 lua_setfield(L, -2, "a");
225 lua_pushinteger(L, color.getRed());
226 lua_setfield(L, -2, "r");
227 lua_pushinteger(L, color.getGreen());
228 lua_setfield(L, -2, "g");
229 lua_pushinteger(L, color.getBlue());
230 lua_setfield(L, -2, "b");
233 void pushFloatPos(lua_State *L, v3f p)
239 v3f checkFloatPos(lua_State *L, int index)
241 return check_v3f(L, index) * BS;
244 void push_v3s16(lua_State *L, v3s16 p)
246 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
247 lua_pushinteger(L, p.X);
248 lua_pushinteger(L, p.Y);
249 lua_pushinteger(L, p.Z);
253 v3s16 read_v3s16(lua_State *L, int index)
255 // Correct rounding at <0
256 v3d pf = read_v3d(L, index);
257 return doubleToInt(pf, 1.0);
260 v3s16 check_v3s16(lua_State *L, int index)
262 // Correct rounding at <0
263 v3d pf = check_v3d(L, index);
264 return doubleToInt(pf, 1.0);
267 bool read_color(lua_State *L, int index, video::SColor *color)
269 if (lua_istable(L, index)) {
270 *color = read_ARGB8(L, index);
271 } else if (lua_isnumber(L, index)) {
272 color->set(lua_tonumber(L, index));
273 } else if (lua_isstring(L, index)) {
274 video::SColor parsed_color;
275 if (!parseColorString(lua_tostring(L, index), parsed_color, true))
278 *color = parsed_color;
286 video::SColor read_ARGB8(lua_State *L, int index)
288 video::SColor color(0);
289 CHECK_TYPE(index, "ARGB color", LUA_TTABLE);
290 lua_getfield(L, index, "a");
291 color.setAlpha(lua_isnumber(L, -1) ? lua_tonumber(L, -1) : 0xFF);
293 lua_getfield(L, index, "r");
294 color.setRed(lua_tonumber(L, -1));
296 lua_getfield(L, index, "g");
297 color.setGreen(lua_tonumber(L, -1));
299 lua_getfield(L, index, "b");
300 color.setBlue(lua_tonumber(L, -1));
305 bool is_color_table(lua_State *L, int index)
307 // Check whole table in case of missing ColorSpec keys:
308 // This check does not remove the checked value from the stack.
309 // Only update the value if we know we have a valid ColorSpec key pair.
310 if (!lua_istable(L, index))
313 bool is_color_table = false;
314 lua_getfield(L, index, "r");
316 is_color_table = lua_isnumber(L, -1);
317 lua_getfield(L, index, "g");
319 is_color_table = lua_isnumber(L, -1);
320 lua_getfield(L, index, "b");
322 is_color_table = lua_isnumber(L, -1);
323 lua_pop(L, 3); // b, g, r values
324 return is_color_table;
327 aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
330 if(lua_istable(L, index)){
331 lua_rawgeti(L, index, 1);
332 box.MinEdge.X = lua_tonumber(L, -1) * scale;
334 lua_rawgeti(L, index, 2);
335 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
337 lua_rawgeti(L, index, 3);
338 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
340 lua_rawgeti(L, index, 4);
341 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
343 lua_rawgeti(L, index, 5);
344 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
346 lua_rawgeti(L, index, 6);
347 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
354 void push_aabb3f(lua_State *L, aabb3f box)
356 lua_createtable(L, 6, 0);
357 lua_pushnumber(L, box.MinEdge.X);
358 lua_rawseti(L, -2, 1);
359 lua_pushnumber(L, box.MinEdge.Y);
360 lua_rawseti(L, -2, 2);
361 lua_pushnumber(L, box.MinEdge.Z);
362 lua_rawseti(L, -2, 3);
363 lua_pushnumber(L, box.MaxEdge.X);
364 lua_rawseti(L, -2, 4);
365 lua_pushnumber(L, box.MaxEdge.Y);
366 lua_rawseti(L, -2, 5);
367 lua_pushnumber(L, box.MaxEdge.Z);
368 lua_rawseti(L, -2, 6);
371 std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
373 std::vector<aabb3f> boxes;
374 if(lua_istable(L, index)){
375 int n = lua_objlen(L, index);
376 // Check if it's a single box or a list of boxes
377 bool possibly_single_box = (n == 6);
378 for(int i = 1; i <= n && possibly_single_box; i++){
379 lua_rawgeti(L, index, i);
380 if(!lua_isnumber(L, -1))
381 possibly_single_box = false;
384 if(possibly_single_box){
386 boxes.push_back(read_aabb3f(L, index, scale));
388 // Read a list of boxes
389 for(int i = 1; i <= n; i++){
390 lua_rawgeti(L, index, i);
391 boxes.push_back(read_aabb3f(L, -1, scale));
399 size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result)
402 index = lua_gettop(L) + 1 + index;
404 size_t num_strings = 0;
406 if (lua_istable(L, index)) {
408 while (lua_next(L, index)) {
409 if (lua_isstring(L, -1)) {
410 result->push_back(lua_tostring(L, -1));
415 } else if (lua_isstring(L, index)) {
416 result->push_back(lua_tostring(L, index));
427 bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname)
429 thread_local std::set<u64> warned_msgs;
431 int t = lua_type(L, index);
438 // Check coercion types
439 if (type == LUA_TNUMBER) {
440 if (lua_isnumber(L, index))
442 } else if (type == LUA_TSTRING) {
443 if (lua_isstring(L, index))
447 // Types mismatch. Log unique line.
448 std::string backtrace = std::string("Invalid field ") + fieldname +
449 " (expected " + lua_typename(L, type) +
450 " got " + lua_typename(L, t) + ").\n" + script_get_backtrace(L);
452 u64 hash = murmur_hash_64_ua(backtrace.data(), backtrace.length(), 0xBADBABE);
453 if (warned_msgs.find(hash) == warned_msgs.end()) {
454 errorstream << backtrace << std::endl;
455 warned_msgs.insert(hash);
461 bool getstringfield(lua_State *L, int table,
462 const char *fieldname, std::string &result)
464 lua_getfield(L, table, fieldname);
467 if (check_field_or_nil(L, -1, LUA_TSTRING, fieldname)) {
469 const char *ptr = lua_tolstring(L, -1, &len);
471 result.assign(ptr, len);
479 bool getfloatfield(lua_State *L, int table,
480 const char *fieldname, float &result)
482 lua_getfield(L, table, fieldname);
485 if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)) {
486 result = lua_tonumber(L, -1);
493 bool getboolfield(lua_State *L, int table,
494 const char *fieldname, bool &result)
496 lua_getfield(L, table, fieldname);
499 if (check_field_or_nil(L, -1, LUA_TBOOLEAN, fieldname)){
500 result = lua_toboolean(L, -1);
507 size_t getstringlistfield(lua_State *L, int table, const char *fieldname,
508 std::vector<std::string> *result)
510 lua_getfield(L, table, fieldname);
512 size_t num_strings_read = read_stringlist(L, -1, result);
515 return num_strings_read;
518 std::string getstringfield_default(lua_State *L, int table,
519 const char *fieldname, const std::string &default_)
521 std::string result = default_;
522 getstringfield(L, table, fieldname, result);
526 int getintfield_default(lua_State *L, int table,
527 const char *fieldname, int default_)
529 int result = default_;
530 getintfield(L, table, fieldname, result);
534 float getfloatfield_default(lua_State *L, int table,
535 const char *fieldname, float default_)
537 float result = default_;
538 getfloatfield(L, table, fieldname, result);
542 bool getboolfield_default(lua_State *L, int table,
543 const char *fieldname, bool default_)
545 bool result = default_;
546 getboolfield(L, table, fieldname, result);
550 v3s16 getv3s16field_default(lua_State *L, int table,
551 const char *fieldname, v3s16 default_)
553 getv3intfield(L, table, fieldname, default_);
557 void setstringfield(lua_State *L, int table,
558 const char *fieldname, const std::string &value)
560 lua_pushlstring(L, value.c_str(), value.length());
563 lua_setfield(L, table, fieldname);
566 void setintfield(lua_State *L, int table,
567 const char *fieldname, int value)
569 lua_pushinteger(L, value);
572 lua_setfield(L, table, fieldname);
575 void setfloatfield(lua_State *L, int table,
576 const char *fieldname, float value)
578 lua_pushnumber(L, value);
581 lua_setfield(L, table, fieldname);
584 void setboolfield(lua_State *L, int table,
585 const char *fieldname, bool value)
587 lua_pushboolean(L, value);
590 lua_setfield(L, table, fieldname);
595 //// Array table slices
598 size_t write_array_slice_float(
606 v3u16 pmin, pmax(data_size);
608 if (slice_offset.X > 0) {
610 pmin.X = slice_offset.X;
611 pmax.X = MYMIN(slice_offset.X + slice_size.X, data_size.X);
614 if (slice_offset.Y > 0) {
616 pmin.Y = slice_offset.Y;
617 pmax.Y = MYMIN(slice_offset.Y + slice_size.Y, data_size.Y);
620 if (slice_offset.Z > 0) {
622 pmin.Z = slice_offset.Z;
623 pmax.Z = MYMIN(slice_offset.Z + slice_size.Z, data_size.Z);
626 const u32 ystride = data_size.X;
627 const u32 zstride = data_size.X * data_size.Y;
630 for (u32 z = pmin.Z; z != pmax.Z; z++)
631 for (u32 y = pmin.Y; y != pmax.Y; y++)
632 for (u32 x = pmin.X; x != pmax.X; x++) {
633 u32 i = z * zstride + y * ystride + x;
634 lua_pushnumber(L, data[i]);
635 lua_rawseti(L, table_index, elem_index);
639 return elem_index - 1;