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"
33 #define CHECK_TYPE(index, name, type) { \
34 int t = lua_type(L, (index)); \
36 std::string traceback = script_get_backtrace(L); \
37 throw LuaError(std::string("Invalid ") + (name) + \
38 " (expected " + lua_typename(L, (type)) + \
39 " got " + lua_typename(L, t) + ").\n" + traceback); \
42 #define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
43 #define CHECK_FLOAT_RANGE(value, name) \
44 if (value < F1000_MIN || value > F1000_MAX) { \
45 std::ostringstream error_text; \
46 error_text << "Invalid float vector dimension range '" name "' " << \
47 "(expected " << F1000_MIN << " < " name " < " << F1000_MAX << \
48 " got " << value << ")." << std::endl; \
49 throw LuaError(error_text.str()); \
51 #define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)
54 void push_float_string(lua_State *L, float value)
60 lua_pushstring(L, str.c_str());
63 void push_v3f(lua_State *L, v3f p)
66 lua_pushnumber(L, p.X);
67 lua_setfield(L, -2, "x");
68 lua_pushnumber(L, p.Y);
69 lua_setfield(L, -2, "y");
70 lua_pushnumber(L, p.Z);
71 lua_setfield(L, -2, "z");
74 void push_v2f(lua_State *L, v2f p)
77 lua_pushnumber(L, p.X);
78 lua_setfield(L, -2, "x");
79 lua_pushnumber(L, p.Y);
80 lua_setfield(L, -2, "y");
83 void push_v3_float_string(lua_State *L, v3f p)
86 push_float_string(L, p.X);
87 lua_setfield(L, -2, "x");
88 push_float_string(L, p.Y);
89 lua_setfield(L, -2, "y");
90 push_float_string(L, p.Z);
91 lua_setfield(L, -2, "z");
94 void push_v2_float_string(lua_State *L, v2f p)
97 push_float_string(L, p.X);
98 lua_setfield(L, -2, "x");
99 push_float_string(L, p.Y);
100 lua_setfield(L, -2, "y");
103 v2s16 read_v2s16(lua_State *L, int index)
106 CHECK_POS_TAB(index);
107 lua_getfield(L, index, "x");
108 p.X = lua_tonumber(L, -1);
110 lua_getfield(L, index, "y");
111 p.Y = lua_tonumber(L, -1);
116 void push_v2s16(lua_State *L, v2s16 p)
119 lua_pushnumber(L, p.X);
120 lua_setfield(L, -2, "x");
121 lua_pushnumber(L, p.Y);
122 lua_setfield(L, -2, "y");
125 void push_v2s32(lua_State *L, v2s32 p)
128 lua_pushnumber(L, p.X);
129 lua_setfield(L, -2, "x");
130 lua_pushnumber(L, p.Y);
131 lua_setfield(L, -2, "y");
134 v2s32 read_v2s32(lua_State *L, int index)
137 CHECK_POS_TAB(index);
138 lua_getfield(L, index, "x");
139 p.X = lua_tonumber(L, -1);
141 lua_getfield(L, index, "y");
142 p.Y = lua_tonumber(L, -1);
147 v2f read_v2f(lua_State *L, int index)
150 CHECK_POS_TAB(index);
151 lua_getfield(L, index, "x");
152 p.X = lua_tonumber(L, -1);
154 lua_getfield(L, index, "y");
155 p.Y = lua_tonumber(L, -1);
160 v2f check_v2f(lua_State *L, int index)
163 CHECK_POS_TAB(index);
164 lua_getfield(L, index, "x");
165 CHECK_POS_COORD("x");
166 p.X = lua_tonumber(L, -1);
168 lua_getfield(L, index, "y");
169 CHECK_POS_COORD("y");
170 p.Y = lua_tonumber(L, -1);
175 v3f read_v3f(lua_State *L, int index)
178 CHECK_POS_TAB(index);
179 lua_getfield(L, index, "x");
180 pos.X = lua_tonumber(L, -1);
182 lua_getfield(L, index, "y");
183 pos.Y = lua_tonumber(L, -1);
185 lua_getfield(L, index, "z");
186 pos.Z = lua_tonumber(L, -1);
191 v3f check_v3f(lua_State *L, int index)
194 CHECK_POS_TAB(index);
195 lua_getfield(L, index, "x");
196 CHECK_POS_COORD("x");
197 pos.X = lua_tonumber(L, -1);
198 CHECK_FLOAT_RANGE(pos.X, "x")
200 lua_getfield(L, index, "y");
201 CHECK_POS_COORD("y");
202 pos.Y = lua_tonumber(L, -1);
203 CHECK_FLOAT_RANGE(pos.Y, "y")
205 lua_getfield(L, index, "z");
206 CHECK_POS_COORD("z");
207 pos.Z = lua_tonumber(L, -1);
208 CHECK_FLOAT_RANGE(pos.Z, "z")
213 v3d read_v3d(lua_State *L, int index)
216 CHECK_POS_TAB(index);
217 lua_getfield(L, index, "x");
218 pos.X = lua_tonumber(L, -1);
220 lua_getfield(L, index, "y");
221 pos.Y = lua_tonumber(L, -1);
223 lua_getfield(L, index, "z");
224 pos.Z = lua_tonumber(L, -1);
229 v3d check_v3d(lua_State *L, int index)
232 CHECK_POS_TAB(index);
233 lua_getfield(L, index, "x");
234 CHECK_POS_COORD("x");
235 pos.X = lua_tonumber(L, -1);
236 CHECK_FLOAT_RANGE(pos.X, "x")
238 lua_getfield(L, index, "y");
239 CHECK_POS_COORD("y");
240 pos.Y = lua_tonumber(L, -1);
241 CHECK_FLOAT_RANGE(pos.Y, "y")
243 lua_getfield(L, index, "z");
244 CHECK_POS_COORD("z");
245 pos.Z = lua_tonumber(L, -1);
246 CHECK_FLOAT_RANGE(pos.Z, "z")
251 void push_ARGB8(lua_State *L, video::SColor color)
254 lua_pushnumber(L, color.getAlpha());
255 lua_setfield(L, -2, "a");
256 lua_pushnumber(L, color.getRed());
257 lua_setfield(L, -2, "r");
258 lua_pushnumber(L, color.getGreen());
259 lua_setfield(L, -2, "g");
260 lua_pushnumber(L, color.getBlue());
261 lua_setfield(L, -2, "b");
264 void pushFloatPos(lua_State *L, v3f p)
270 v3f checkFloatPos(lua_State *L, int index)
272 return check_v3f(L, index) * BS;
275 void push_v3s16(lua_State *L, v3s16 p)
278 lua_pushnumber(L, p.X);
279 lua_setfield(L, -2, "x");
280 lua_pushnumber(L, p.Y);
281 lua_setfield(L, -2, "y");
282 lua_pushnumber(L, p.Z);
283 lua_setfield(L, -2, "z");
286 v3s16 read_v3s16(lua_State *L, int index)
288 // Correct rounding at <0
289 v3d pf = read_v3d(L, index);
290 return doubleToInt(pf, 1.0);
293 v3s16 check_v3s16(lua_State *L, int index)
295 // Correct rounding at <0
296 v3d pf = check_v3d(L, index);
297 return doubleToInt(pf, 1.0);
300 bool read_color(lua_State *L, int index, video::SColor *color)
302 if (lua_istable(L, index)) {
303 *color = read_ARGB8(L, index);
304 } else if (lua_isnumber(L, index)) {
305 color->set(lua_tonumber(L, index));
306 } else if (lua_isstring(L, index)) {
307 video::SColor parsed_color;
308 if (!parseColorString(lua_tostring(L, index), parsed_color, true))
311 *color = parsed_color;
319 video::SColor read_ARGB8(lua_State *L, int index)
321 video::SColor color(0);
322 CHECK_TYPE(index, "ARGB color", LUA_TTABLE);
323 lua_getfield(L, index, "a");
324 color.setAlpha(lua_isnumber(L, -1) ? lua_tonumber(L, -1) : 0xFF);
326 lua_getfield(L, index, "r");
327 color.setRed(lua_tonumber(L, -1));
329 lua_getfield(L, index, "g");
330 color.setGreen(lua_tonumber(L, -1));
332 lua_getfield(L, index, "b");
333 color.setBlue(lua_tonumber(L, -1));
338 aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
341 if(lua_istable(L, index)){
342 lua_rawgeti(L, index, 1);
343 box.MinEdge.X = lua_tonumber(L, -1) * scale;
345 lua_rawgeti(L, index, 2);
346 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
348 lua_rawgeti(L, index, 3);
349 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
351 lua_rawgeti(L, index, 4);
352 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
354 lua_rawgeti(L, index, 5);
355 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
357 lua_rawgeti(L, index, 6);
358 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
365 void push_aabb3f(lua_State *L, aabb3f box)
368 lua_pushnumber(L, box.MinEdge.X);
369 lua_rawseti(L, -2, 1);
370 lua_pushnumber(L, box.MinEdge.Y);
371 lua_rawseti(L, -2, 2);
372 lua_pushnumber(L, box.MinEdge.Z);
373 lua_rawseti(L, -2, 3);
374 lua_pushnumber(L, box.MaxEdge.X);
375 lua_rawseti(L, -2, 4);
376 lua_pushnumber(L, box.MaxEdge.Y);
377 lua_rawseti(L, -2, 5);
378 lua_pushnumber(L, box.MaxEdge.Z);
379 lua_rawseti(L, -2, 6);
382 std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
384 std::vector<aabb3f> boxes;
385 if(lua_istable(L, index)){
386 int n = lua_objlen(L, index);
387 // Check if it's a single box or a list of boxes
388 bool possibly_single_box = (n == 6);
389 for(int i = 1; i <= n && possibly_single_box; i++){
390 lua_rawgeti(L, index, i);
391 if(!lua_isnumber(L, -1))
392 possibly_single_box = false;
395 if(possibly_single_box){
397 boxes.push_back(read_aabb3f(L, index, scale));
399 // Read a list of boxes
400 for(int i = 1; i <= n; i++){
401 lua_rawgeti(L, index, i);
402 boxes.push_back(read_aabb3f(L, -1, scale));
410 size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result)
413 index = lua_gettop(L) + 1 + index;
415 size_t num_strings = 0;
417 if (lua_istable(L, index)) {
419 while (lua_next(L, index)) {
420 if (lua_isstring(L, -1)) {
421 result->push_back(lua_tostring(L, -1));
426 } else if (lua_isstring(L, index)) {
427 result->push_back(lua_tostring(L, index));
438 bool getstringfield(lua_State *L, int table,
439 const char *fieldname, std::string &result)
441 lua_getfield(L, table, fieldname);
443 if(lua_isstring(L, -1)){
445 const char *ptr = lua_tolstring(L, -1, &len);
447 result.assign(ptr, len);
455 bool getfloatfield(lua_State *L, int table,
456 const char *fieldname, float &result)
458 lua_getfield(L, table, fieldname);
460 if(lua_isnumber(L, -1)){
461 result = lua_tonumber(L, -1);
468 bool getboolfield(lua_State *L, int table,
469 const char *fieldname, bool &result)
471 lua_getfield(L, table, fieldname);
473 if(lua_isboolean(L, -1)){
474 result = lua_toboolean(L, -1);
481 size_t getstringlistfield(lua_State *L, int table, const char *fieldname,
482 std::vector<std::string> *result)
484 lua_getfield(L, table, fieldname);
486 size_t num_strings_read = read_stringlist(L, -1, result);
489 return num_strings_read;
492 std::string checkstringfield(lua_State *L, int table,
493 const char *fieldname)
495 lua_getfield(L, table, fieldname);
496 CHECK_TYPE(-1, std::string("field \"") + fieldname + '"', LUA_TSTRING);
498 const char *s = lua_tolstring(L, -1, &len);
500 return std::string(s, len);
503 std::string getstringfield_default(lua_State *L, int table,
504 const char *fieldname, const std::string &default_)
506 std::string result = default_;
507 getstringfield(L, table, fieldname, result);
511 int getintfield_default(lua_State *L, int table,
512 const char *fieldname, int default_)
514 int result = default_;
515 getintfield(L, table, fieldname, result);
519 float getfloatfield_default(lua_State *L, int table,
520 const char *fieldname, float default_)
522 float result = default_;
523 getfloatfield(L, table, fieldname, result);
527 bool getboolfield_default(lua_State *L, int table,
528 const char *fieldname, bool default_)
530 bool result = default_;
531 getboolfield(L, table, fieldname, result);
535 v3s16 getv3s16field_default(lua_State *L, int table,
536 const char *fieldname, v3s16 default_)
538 getv3intfield(L, table, fieldname, default_);
542 void setstringfield(lua_State *L, int table,
543 const char *fieldname, const char *value)
545 lua_pushstring(L, value);
548 lua_setfield(L, table, fieldname);
551 void setintfield(lua_State *L, int table,
552 const char *fieldname, int value)
554 lua_pushinteger(L, value);
557 lua_setfield(L, table, fieldname);
560 void setfloatfield(lua_State *L, int table,
561 const char *fieldname, float value)
563 lua_pushnumber(L, value);
566 lua_setfield(L, table, fieldname);
569 void setboolfield(lua_State *L, int table,
570 const char *fieldname, bool value)
572 lua_pushboolean(L, value);
575 lua_setfield(L, table, fieldname);
580 //// Array table slices
583 size_t write_array_slice_float(
591 v3u16 pmin, pmax(data_size);
593 if (slice_offset.X > 0) {
595 pmin.X = slice_offset.X;
596 pmax.X = MYMIN(slice_offset.X + slice_size.X, data_size.X);
599 if (slice_offset.Y > 0) {
601 pmin.Y = slice_offset.Y;
602 pmax.Y = MYMIN(slice_offset.Y + slice_size.Y, data_size.Y);
605 if (slice_offset.Z > 0) {
607 pmin.Z = slice_offset.Z;
608 pmax.Z = MYMIN(slice_offset.Z + slice_size.Z, data_size.Z);
611 const u32 ystride = data_size.X;
612 const u32 zstride = data_size.X * data_size.Y;
615 for (u32 z = pmin.Z; z != pmax.Z; z++)
616 for (u32 y = pmin.Y; y != pmax.Y; y++)
617 for (u32 x = pmin.X; x != pmax.X; x++) {
618 u32 i = z * zstride + y * ystride + x;
619 lua_pushnumber(L, data[i]);
620 lua_rawseti(L, table_index, elem_index);
624 return elem_index - 1;
628 size_t write_array_slice_u16(
636 v3u16 pmin, pmax(data_size);
638 if (slice_offset.X > 0) {
640 pmin.X = slice_offset.X;
641 pmax.X = MYMIN(slice_offset.X + slice_size.X, data_size.X);
644 if (slice_offset.Y > 0) {
646 pmin.Y = slice_offset.Y;
647 pmax.Y = MYMIN(slice_offset.Y + slice_size.Y, data_size.Y);
650 if (slice_offset.Z > 0) {
652 pmin.Z = slice_offset.Z;
653 pmax.Z = MYMIN(slice_offset.Z + slice_size.Z, data_size.Z);
656 const u32 ystride = data_size.X;
657 const u32 zstride = data_size.X * data_size.Y;
660 for (u32 z = pmin.Z; z != pmax.Z; z++)
661 for (u32 y = pmin.Y; y != pmax.Y; y++)
662 for (u32 x = pmin.X; x != pmax.X; x++) {
663 u32 i = z * zstride + y * ystride + x;
664 lua_pushinteger(L, data[i]);
665 lua_rawseti(L, table_index, elem_index);
669 return elem_index - 1;