c_converter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. extern "C" {
  17. #include <lua.h>
  18. #include <lauxlib.h>
  19. }
  20. #include "util/numeric.h"
  21. #include "util/serialize.h"
  22. #include "util/string.h"
  23. #include "common/c_converter.h"
  24. #include "common/c_internal.h"
  25. #include "constants.h"
  26. #include <set>
  27. #include <cmath>
  28. #define CHECK_TYPE(index, name, type) do { \
  29. int t = lua_type(L, (index)); \
  30. if (t != (type)) { \
  31. throw LuaError(std::string("Invalid ") + (name) + \
  32. " (expected " + lua_typename(L, (type)) + \
  33. " got " + lua_typename(L, t) + ")."); \
  34. } \
  35. } while(0)
  36. #define CHECK_FLOAT(value, name) do {\
  37. if (std::isnan(value) || std::isinf(value)) { \
  38. throw LuaError("Invalid float value for '" name \
  39. "' (NaN or infinity)"); \
  40. } \
  41. } while (0)
  42. #define CHECK_POS_COORD(index, name) CHECK_TYPE(index, "vector coordinate " name, LUA_TNUMBER)
  43. #define CHECK_POS_TAB(index) CHECK_TYPE(index, "vector", LUA_TTABLE)
  44. /**
  45. * A helper which calls CUSTOM_RIDX_READ_VECTOR with the argument at the given index
  46. */
  47. static void read_v3_aux(lua_State *L, int index)
  48. {
  49. lua_pushvalue(L, index);
  50. lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_VECTOR);
  51. lua_insert(L, -2);
  52. lua_call(L, 1, 3);
  53. }
  54. // Retrieve an integer vector where all components are optional
  55. template<class T>
  56. static bool getv3intfield(lua_State *L, int index,
  57. const char *fieldname, T &result)
  58. {
  59. lua_getfield(L, index, fieldname);
  60. bool got = false;
  61. if (lua_istable(L, -1)) {
  62. got |= getintfield(L, -1, "x", result.X);
  63. got |= getintfield(L, -1, "y", result.Y);
  64. got |= getintfield(L, -1, "z", result.Z);
  65. }
  66. lua_pop(L, 1);
  67. return got;
  68. }
  69. void push_v3f(lua_State *L, v3f p)
  70. {
  71. lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
  72. lua_pushnumber(L, p.X);
  73. lua_pushnumber(L, p.Y);
  74. lua_pushnumber(L, p.Z);
  75. lua_call(L, 3, 1);
  76. }
  77. void push_v2f(lua_State *L, v2f p)
  78. {
  79. lua_createtable(L, 0, 2);
  80. lua_pushnumber(L, p.X);
  81. lua_setfield(L, -2, "x");
  82. lua_pushnumber(L, p.Y);
  83. lua_setfield(L, -2, "y");
  84. }
  85. v2s16 read_v2s16(lua_State *L, int index)
  86. {
  87. v2s16 p;
  88. CHECK_POS_TAB(index);
  89. lua_getfield(L, index, "x");
  90. p.X = lua_tonumber(L, -1);
  91. lua_pop(L, 1);
  92. lua_getfield(L, index, "y");
  93. p.Y = lua_tonumber(L, -1);
  94. lua_pop(L, 1);
  95. return p;
  96. }
  97. void push_v2s16(lua_State *L, v2s16 p)
  98. {
  99. lua_createtable(L, 0, 2);
  100. lua_pushinteger(L, p.X);
  101. lua_setfield(L, -2, "x");
  102. lua_pushinteger(L, p.Y);
  103. lua_setfield(L, -2, "y");
  104. }
  105. void push_v2s32(lua_State *L, v2s32 p)
  106. {
  107. lua_createtable(L, 0, 2);
  108. lua_pushinteger(L, p.X);
  109. lua_setfield(L, -2, "x");
  110. lua_pushinteger(L, p.Y);
  111. lua_setfield(L, -2, "y");
  112. }
  113. v2s32 read_v2s32(lua_State *L, int index)
  114. {
  115. v2s32 p;
  116. CHECK_POS_TAB(index);
  117. lua_getfield(L, index, "x");
  118. p.X = lua_tonumber(L, -1);
  119. lua_pop(L, 1);
  120. lua_getfield(L, index, "y");
  121. p.Y = lua_tonumber(L, -1);
  122. lua_pop(L, 1);
  123. return p;
  124. }
  125. v2f read_v2f(lua_State *L, int index)
  126. {
  127. v2f p;
  128. CHECK_POS_TAB(index);
  129. lua_getfield(L, index, "x");
  130. p.X = lua_tonumber(L, -1);
  131. lua_pop(L, 1);
  132. lua_getfield(L, index, "y");
  133. p.Y = lua_tonumber(L, -1);
  134. lua_pop(L, 1);
  135. return p;
  136. }
  137. v2f check_v2f(lua_State *L, int index)
  138. {
  139. v2f p;
  140. CHECK_POS_TAB(index);
  141. lua_getfield(L, index, "x");
  142. CHECK_POS_COORD(-1, "x");
  143. p.X = lua_tonumber(L, -1);
  144. CHECK_FLOAT(p.X, "x");
  145. lua_pop(L, 1);
  146. lua_getfield(L, index, "y");
  147. CHECK_POS_COORD(-1, "y");
  148. p.Y = lua_tonumber(L, -1);
  149. CHECK_FLOAT(p.Y, "y");
  150. lua_pop(L, 1);
  151. return p;
  152. }
  153. v3f read_v3f(lua_State *L, int index)
  154. {
  155. read_v3_aux(L, index);
  156. float x = lua_tonumber(L, -3);
  157. float y = lua_tonumber(L, -2);
  158. float z = lua_tonumber(L, -1);
  159. lua_pop(L, 3);
  160. return v3f(x, y, z);
  161. }
  162. v3f check_v3f(lua_State *L, int index)
  163. {
  164. read_v3_aux(L, index);
  165. CHECK_POS_COORD(-3, "x");
  166. CHECK_POS_COORD(-2, "y");
  167. CHECK_POS_COORD(-1, "z");
  168. float x = lua_tonumber(L, -3);
  169. float y = lua_tonumber(L, -2);
  170. float z = lua_tonumber(L, -1);
  171. lua_pop(L, 3);
  172. return v3f(x, y, z);
  173. }
  174. v3d read_v3d(lua_State *L, int index)
  175. {
  176. read_v3_aux(L, index);
  177. double x = lua_tonumber(L, -3);
  178. double y = lua_tonumber(L, -2);
  179. double z = lua_tonumber(L, -1);
  180. lua_pop(L, 3);
  181. return v3d(x, y, z);
  182. }
  183. v3d check_v3d(lua_State *L, int index)
  184. {
  185. read_v3_aux(L, index);
  186. CHECK_POS_COORD(-3, "x");
  187. CHECK_POS_COORD(-2, "y");
  188. CHECK_POS_COORD(-1, "z");
  189. double x = lua_tonumber(L, -3);
  190. double y = lua_tonumber(L, -2);
  191. double z = lua_tonumber(L, -1);
  192. lua_pop(L, 3);
  193. return v3d(x, y, z);
  194. }
  195. void push_ARGB8(lua_State *L, video::SColor color)
  196. {
  197. lua_createtable(L, 0, 4);
  198. lua_pushinteger(L, color.getAlpha());
  199. lua_setfield(L, -2, "a");
  200. lua_pushinteger(L, color.getRed());
  201. lua_setfield(L, -2, "r");
  202. lua_pushinteger(L, color.getGreen());
  203. lua_setfield(L, -2, "g");
  204. lua_pushinteger(L, color.getBlue());
  205. lua_setfield(L, -2, "b");
  206. }
  207. void pushFloatPos(lua_State *L, v3f p)
  208. {
  209. p /= BS;
  210. push_v3f(L, p);
  211. }
  212. v3f checkFloatPos(lua_State *L, int index)
  213. {
  214. return check_v3f(L, index) * BS;
  215. }
  216. void push_v3s16(lua_State *L, v3s16 p)
  217. {
  218. lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
  219. lua_pushinteger(L, p.X);
  220. lua_pushinteger(L, p.Y);
  221. lua_pushinteger(L, p.Z);
  222. lua_call(L, 3, 1);
  223. }
  224. v3s16 read_v3s16(lua_State *L, int index)
  225. {
  226. // Correct rounding at <0
  227. v3d pf = read_v3d(L, index);
  228. return doubleToInt(pf, 1.0);
  229. }
  230. v3s16 check_v3s16(lua_State *L, int index)
  231. {
  232. // Correct rounding at <0
  233. v3d pf = check_v3d(L, index);
  234. return doubleToInt(pf, 1.0);
  235. }
  236. bool read_color(lua_State *L, int index, video::SColor *color)
  237. {
  238. if (lua_istable(L, index)) {
  239. *color = read_ARGB8(L, index);
  240. } else if (lua_isnumber(L, index)) {
  241. color->set(lua_tonumber(L, index));
  242. } else if (lua_isstring(L, index)) {
  243. video::SColor parsed_color;
  244. if (!parseColorString(lua_tostring(L, index), parsed_color, true))
  245. return false;
  246. *color = parsed_color;
  247. } else {
  248. return false;
  249. }
  250. return true;
  251. }
  252. video::SColor read_ARGB8(lua_State *L, int index)
  253. {
  254. video::SColor color(0);
  255. CHECK_TYPE(index, "ARGB color", LUA_TTABLE);
  256. lua_getfield(L, index, "a");
  257. color.setAlpha(lua_isnumber(L, -1) ? lua_tonumber(L, -1) : 0xFF);
  258. lua_pop(L, 1);
  259. lua_getfield(L, index, "r");
  260. color.setRed(lua_tonumber(L, -1));
  261. lua_pop(L, 1);
  262. lua_getfield(L, index, "g");
  263. color.setGreen(lua_tonumber(L, -1));
  264. lua_pop(L, 1);
  265. lua_getfield(L, index, "b");
  266. color.setBlue(lua_tonumber(L, -1));
  267. lua_pop(L, 1);
  268. return color;
  269. }
  270. bool is_color_table(lua_State *L, int index)
  271. {
  272. // Check whole table in case of missing ColorSpec keys:
  273. // This check does not remove the checked value from the stack.
  274. // Only update the value if we know we have a valid ColorSpec key pair.
  275. if (!lua_istable(L, index))
  276. return false;
  277. bool is_color_table = false;
  278. lua_getfield(L, index, "r");
  279. if (!is_color_table)
  280. is_color_table = lua_isnumber(L, -1);
  281. lua_getfield(L, index, "g");
  282. if (!is_color_table)
  283. is_color_table = lua_isnumber(L, -1);
  284. lua_getfield(L, index, "b");
  285. if (!is_color_table)
  286. is_color_table = lua_isnumber(L, -1);
  287. lua_pop(L, 3); // b, g, r values
  288. return is_color_table;
  289. }
  290. aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
  291. {
  292. aabb3f box;
  293. if(lua_istable(L, index)){
  294. lua_rawgeti(L, index, 1);
  295. box.MinEdge.X = lua_tonumber(L, -1) * scale;
  296. lua_pop(L, 1);
  297. lua_rawgeti(L, index, 2);
  298. box.MinEdge.Y = lua_tonumber(L, -1) * scale;
  299. lua_pop(L, 1);
  300. lua_rawgeti(L, index, 3);
  301. box.MinEdge.Z = lua_tonumber(L, -1) * scale;
  302. lua_pop(L, 1);
  303. lua_rawgeti(L, index, 4);
  304. box.MaxEdge.X = lua_tonumber(L, -1) * scale;
  305. lua_pop(L, 1);
  306. lua_rawgeti(L, index, 5);
  307. box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
  308. lua_pop(L, 1);
  309. lua_rawgeti(L, index, 6);
  310. box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
  311. lua_pop(L, 1);
  312. }
  313. box.repair();
  314. return box;
  315. }
  316. void push_aabb3f(lua_State *L, aabb3f box)
  317. {
  318. lua_createtable(L, 6, 0);
  319. lua_pushnumber(L, box.MinEdge.X);
  320. lua_rawseti(L, -2, 1);
  321. lua_pushnumber(L, box.MinEdge.Y);
  322. lua_rawseti(L, -2, 2);
  323. lua_pushnumber(L, box.MinEdge.Z);
  324. lua_rawseti(L, -2, 3);
  325. lua_pushnumber(L, box.MaxEdge.X);
  326. lua_rawseti(L, -2, 4);
  327. lua_pushnumber(L, box.MaxEdge.Y);
  328. lua_rawseti(L, -2, 5);
  329. lua_pushnumber(L, box.MaxEdge.Z);
  330. lua_rawseti(L, -2, 6);
  331. }
  332. std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
  333. {
  334. std::vector<aabb3f> boxes;
  335. if(lua_istable(L, index)){
  336. int n = lua_objlen(L, index);
  337. // Check if it's a single box or a list of boxes
  338. bool possibly_single_box = (n == 6);
  339. for(int i = 1; i <= n && possibly_single_box; i++){
  340. lua_rawgeti(L, index, i);
  341. if(!lua_isnumber(L, -1))
  342. possibly_single_box = false;
  343. lua_pop(L, 1);
  344. }
  345. if(possibly_single_box){
  346. // Read a single box
  347. boxes.push_back(read_aabb3f(L, index, scale));
  348. } else {
  349. // Read a list of boxes
  350. for(int i = 1; i <= n; i++){
  351. lua_rawgeti(L, index, i);
  352. boxes.push_back(read_aabb3f(L, -1, scale));
  353. lua_pop(L, 1);
  354. }
  355. }
  356. }
  357. return boxes;
  358. }
  359. size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result)
  360. {
  361. if (index < 0)
  362. index = lua_gettop(L) + 1 + index;
  363. size_t num_strings = 0;
  364. if (lua_istable(L, index)) {
  365. lua_pushnil(L);
  366. while (lua_next(L, index)) {
  367. if (lua_isstring(L, -1)) {
  368. result->push_back(lua_tostring(L, -1));
  369. num_strings++;
  370. }
  371. lua_pop(L, 1);
  372. }
  373. } else if (lua_isstring(L, index)) {
  374. result->push_back(lua_tostring(L, index));
  375. num_strings++;
  376. }
  377. return num_strings;
  378. }
  379. /*
  380. Table field getters
  381. */
  382. bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname)
  383. {
  384. thread_local std::set<u64> warned_msgs;
  385. int t = lua_type(L, index);
  386. if (t == LUA_TNIL)
  387. return false;
  388. if (t == type)
  389. return true;
  390. // Check coercion types
  391. if (type == LUA_TNUMBER) {
  392. if (lua_isnumber(L, index))
  393. return true;
  394. } else if (type == LUA_TSTRING) {
  395. if (lua_isstring(L, index))
  396. return true;
  397. }
  398. // Types mismatch. Log unique line.
  399. std::string backtrace = std::string("Invalid field ") + fieldname +
  400. " (expected " + lua_typename(L, type) +
  401. " got " + lua_typename(L, t) + ").\n" + script_get_backtrace(L);
  402. u64 hash = murmur_hash_64_ua(backtrace.data(), backtrace.length(), 0xBADBABE);
  403. if (warned_msgs.find(hash) == warned_msgs.end()) {
  404. errorstream << backtrace << std::endl;
  405. warned_msgs.insert(hash);
  406. }
  407. return false;
  408. }
  409. bool getstringfield(lua_State *L, int table,
  410. const char *fieldname, std::string &result)
  411. {
  412. lua_getfield(L, table, fieldname);
  413. bool got = false;
  414. if (check_field_or_nil(L, -1, LUA_TSTRING, fieldname)) {
  415. size_t len = 0;
  416. const char *ptr = lua_tolstring(L, -1, &len);
  417. if (ptr) {
  418. result.assign(ptr, len);
  419. got = true;
  420. }
  421. }
  422. lua_pop(L, 1);
  423. return got;
  424. }
  425. bool getfloatfield(lua_State *L, int table,
  426. const char *fieldname, float &result)
  427. {
  428. lua_getfield(L, table, fieldname);
  429. bool got = false;
  430. if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)) {
  431. result = lua_tonumber(L, -1);
  432. got = true;
  433. }
  434. lua_pop(L, 1);
  435. return got;
  436. }
  437. bool getboolfield(lua_State *L, int table,
  438. const char *fieldname, bool &result)
  439. {
  440. lua_getfield(L, table, fieldname);
  441. bool got = false;
  442. if (check_field_or_nil(L, -1, LUA_TBOOLEAN, fieldname)){
  443. result = lua_toboolean(L, -1);
  444. got = true;
  445. }
  446. lua_pop(L, 1);
  447. return got;
  448. }
  449. size_t getstringlistfield(lua_State *L, int table, const char *fieldname,
  450. std::vector<std::string> *result)
  451. {
  452. lua_getfield(L, table, fieldname);
  453. size_t num_strings_read = read_stringlist(L, -1, result);
  454. lua_pop(L, 1);
  455. return num_strings_read;
  456. }
  457. std::string getstringfield_default(lua_State *L, int table,
  458. const char *fieldname, const std::string &default_)
  459. {
  460. std::string result = default_;
  461. getstringfield(L, table, fieldname, result);
  462. return result;
  463. }
  464. int getintfield_default(lua_State *L, int table,
  465. const char *fieldname, int default_)
  466. {
  467. int result = default_;
  468. getintfield(L, table, fieldname, result);
  469. return result;
  470. }
  471. float getfloatfield_default(lua_State *L, int table,
  472. const char *fieldname, float default_)
  473. {
  474. float result = default_;
  475. getfloatfield(L, table, fieldname, result);
  476. return result;
  477. }
  478. bool getboolfield_default(lua_State *L, int table,
  479. const char *fieldname, bool default_)
  480. {
  481. bool result = default_;
  482. getboolfield(L, table, fieldname, result);
  483. return result;
  484. }
  485. v3s16 getv3s16field_default(lua_State *L, int table,
  486. const char *fieldname, v3s16 default_)
  487. {
  488. getv3intfield(L, table, fieldname, default_);
  489. return default_;
  490. }
  491. void setstringfield(lua_State *L, int table,
  492. const char *fieldname, const std::string &value)
  493. {
  494. lua_pushlstring(L, value.c_str(), value.length());
  495. if(table < 0)
  496. table -= 1;
  497. lua_setfield(L, table, fieldname);
  498. }
  499. void setintfield(lua_State *L, int table,
  500. const char *fieldname, int value)
  501. {
  502. lua_pushinteger(L, value);
  503. if(table < 0)
  504. table -= 1;
  505. lua_setfield(L, table, fieldname);
  506. }
  507. void setfloatfield(lua_State *L, int table,
  508. const char *fieldname, float value)
  509. {
  510. lua_pushnumber(L, value);
  511. if(table < 0)
  512. table -= 1;
  513. lua_setfield(L, table, fieldname);
  514. }
  515. void setboolfield(lua_State *L, int table,
  516. const char *fieldname, bool value)
  517. {
  518. lua_pushboolean(L, value);
  519. if(table < 0)
  520. table -= 1;
  521. lua_setfield(L, table, fieldname);
  522. }
  523. ////
  524. //// Array table slices
  525. ////
  526. size_t write_array_slice_float(
  527. lua_State *L,
  528. int table_index,
  529. float *data,
  530. v3u16 data_size,
  531. v3u16 slice_offset,
  532. v3u16 slice_size)
  533. {
  534. v3u16 pmin, pmax(data_size);
  535. if (slice_offset.X > 0) {
  536. slice_offset.X--;
  537. pmin.X = slice_offset.X;
  538. pmax.X = MYMIN(slice_offset.X + slice_size.X, data_size.X);
  539. }
  540. if (slice_offset.Y > 0) {
  541. slice_offset.Y--;
  542. pmin.Y = slice_offset.Y;
  543. pmax.Y = MYMIN(slice_offset.Y + slice_size.Y, data_size.Y);
  544. }
  545. if (slice_offset.Z > 0) {
  546. slice_offset.Z--;
  547. pmin.Z = slice_offset.Z;
  548. pmax.Z = MYMIN(slice_offset.Z + slice_size.Z, data_size.Z);
  549. }
  550. const u32 ystride = data_size.X;
  551. const u32 zstride = data_size.X * data_size.Y;
  552. u32 elem_index = 1;
  553. for (u32 z = pmin.Z; z != pmax.Z; z++)
  554. for (u32 y = pmin.Y; y != pmax.Y; y++)
  555. for (u32 x = pmin.X; x != pmax.X; x++) {
  556. u32 i = z * zstride + y * ystride + x;
  557. lua_pushnumber(L, data[i]);
  558. lua_rawseti(L, table_index, elem_index);
  559. elem_index++;
  560. }
  561. return elem_index - 1;
  562. }