You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

304 lines
7.5 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.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.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "lua_api/l_metadata.h"
  18. #include "lua_api/l_internal.h"
  19. #include "common/c_content.h"
  20. #include "serverenvironment.h"
  21. #include "map.h"
  22. #include "server.h"
  23. // LUALIB_API
  24. void *luaL_checkudata_is_metadataref(lua_State *L, int ud) {
  25. void *p = lua_touserdata(L, ud);
  26. if (p != NULL && // value is a userdata?
  27. lua_getmetatable(L, ud)) { // does it have a metatable?
  28. lua_getfield(L, -1, "metadata_class");
  29. if (lua_type(L, -1) == LUA_TSTRING) { // does it have a metadata_class field?
  30. return p;
  31. }
  32. }
  33. luaL_typerror(L, ud, "MetaDataRef");
  34. return NULL;
  35. }
  36. MetaDataRef* MetaDataRef::checkobject(lua_State *L, int narg)
  37. {
  38. luaL_checktype(L, narg, LUA_TUSERDATA);
  39. void *ud = luaL_checkudata_is_metadataref(L, narg);
  40. if (!ud)
  41. luaL_typerror(L, narg, "MetaDataRef");
  42. return *(MetaDataRef**)ud; // unbox pointer
  43. }
  44. // Exported functions
  45. // contains(self, name)
  46. int MetaDataRef::l_contains(lua_State *L)
  47. {
  48. MAP_LOCK_REQUIRED;
  49. MetaDataRef *ref = checkobject(L, 1);
  50. std::string name = luaL_checkstring(L, 2);
  51. Metadata *meta = ref->getmeta(false);
  52. if (meta == NULL)
  53. return 0;
  54. lua_pushboolean(L, meta->contains(name));
  55. return 1;
  56. }
  57. // get(self, name)
  58. int MetaDataRef::l_get(lua_State *L)
  59. {
  60. MAP_LOCK_REQUIRED;
  61. MetaDataRef *ref = checkobject(L, 1);
  62. std::string name = luaL_checkstring(L, 2);
  63. Metadata *meta = ref->getmeta(false);
  64. if (meta == NULL)
  65. return 0;
  66. std::string str;
  67. if (meta->getStringToRef(name, str)) {
  68. lua_pushlstring(L, str.c_str(), str.size());
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. // get_string(self, name)
  74. int MetaDataRef::l_get_string(lua_State *L)
  75. {
  76. MAP_LOCK_REQUIRED;
  77. MetaDataRef *ref = checkobject(L, 1);
  78. std::string name = luaL_checkstring(L, 2);
  79. Metadata *meta = ref->getmeta(false);
  80. if (meta == NULL) {
  81. lua_pushlstring(L, "", 0);
  82. return 1;
  83. }
  84. const std::string &str = meta->getString(name);
  85. lua_pushlstring(L, str.c_str(), str.size());
  86. return 1;
  87. }
  88. // set_string(self, name, var)
  89. int MetaDataRef::l_set_string(lua_State *L)
  90. {
  91. MAP_LOCK_REQUIRED;
  92. MetaDataRef *ref = checkobject(L, 1);
  93. std::string name = luaL_checkstring(L, 2);
  94. size_t len = 0;
  95. const char *s = lua_tolstring(L, 3, &len);
  96. std::string str(s, len);
  97. Metadata *meta = ref->getmeta(!str.empty());
  98. if (meta == NULL || str == meta->getString(name))
  99. return 0;
  100. meta->setString(name, str);
  101. ref->reportMetadataChange();
  102. return 0;
  103. }
  104. // get_int(self, name)
  105. int MetaDataRef::l_get_int(lua_State *L)
  106. {
  107. MAP_LOCK_REQUIRED;
  108. MetaDataRef *ref = checkobject(L, 1);
  109. std::string name = luaL_checkstring(L, 2);
  110. Metadata *meta = ref->getmeta(false);
  111. if (meta == NULL) {
  112. lua_pushnumber(L, 0);
  113. return 1;
  114. }
  115. const std::string &str = meta->getString(name);
  116. lua_pushnumber(L, stoi(str));
  117. return 1;
  118. }
  119. // set_int(self, name, var)
  120. int MetaDataRef::l_set_int(lua_State *L)
  121. {
  122. MAP_LOCK_REQUIRED;
  123. MetaDataRef *ref = checkobject(L, 1);
  124. std::string name = luaL_checkstring(L, 2);
  125. int a = luaL_checkint(L, 3);
  126. std::string str = itos(a);
  127. Metadata *meta = ref->getmeta(true);
  128. if (meta == NULL || str == meta->getString(name))
  129. return 0;
  130. meta->setString(name, str);
  131. ref->reportMetadataChange();
  132. return 0;
  133. }
  134. // get_float(self, name)
  135. int MetaDataRef::l_get_float(lua_State *L)
  136. {
  137. MAP_LOCK_REQUIRED;
  138. MetaDataRef *ref = checkobject(L, 1);
  139. std::string name = luaL_checkstring(L, 2);
  140. Metadata *meta = ref->getmeta(false);
  141. if (meta == NULL) {
  142. lua_pushnumber(L, 0);
  143. return 1;
  144. }
  145. const std::string &str = meta->getString(name);
  146. lua_pushnumber(L, stof(str));
  147. return 1;
  148. }
  149. // set_float(self, name, var)
  150. int MetaDataRef::l_set_float(lua_State *L)
  151. {
  152. MAP_LOCK_REQUIRED;
  153. MetaDataRef *ref = checkobject(L, 1);
  154. std::string name = luaL_checkstring(L, 2);
  155. float a = readParam<float>(L, 3);
  156. std::string str = ftos(a);
  157. Metadata *meta = ref->getmeta(true);
  158. if (meta == NULL || str == meta->getString(name))
  159. return 0;
  160. meta->setString(name, str);
  161. ref->reportMetadataChange();
  162. return 0;
  163. }
  164. // to_table(self)
  165. int MetaDataRef::l_to_table(lua_State *L)
  166. {
  167. MAP_LOCK_REQUIRED;
  168. MetaDataRef *ref = checkobject(L, 1);
  169. Metadata *meta = ref->getmeta(true);
  170. if (meta == NULL) {
  171. lua_pushnil(L);
  172. return 1;
  173. }
  174. lua_newtable(L);
  175. ref->handleToTable(L, meta);
  176. return 1;
  177. }
  178. // from_table(self, table)
  179. int MetaDataRef::l_from_table(lua_State *L)
  180. {
  181. MAP_LOCK_REQUIRED;
  182. MetaDataRef *ref = checkobject(L, 1);
  183. int base = 2;
  184. ref->clearMeta();
  185. if (!lua_istable(L, base)) {
  186. // No metadata
  187. lua_pushboolean(L, true);
  188. return 1;
  189. }
  190. // Create new metadata
  191. Metadata *meta = ref->getmeta(true);
  192. if (meta == NULL) {
  193. lua_pushboolean(L, false);
  194. return 1;
  195. }
  196. bool was_successful = ref->handleFromTable(L, base, meta);
  197. ref->reportMetadataChange();
  198. lua_pushboolean(L, was_successful);
  199. return 1;
  200. }
  201. void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
  202. {
  203. lua_newtable(L);
  204. {
  205. const StringMap &fields = meta->getStrings();
  206. for (StringMap::const_iterator
  207. it = fields.begin(); it != fields.end(); ++it) {
  208. const std::string &name = it->first;
  209. const std::string &value = it->second;
  210. lua_pushlstring(L, name.c_str(), name.size());
  211. lua_pushlstring(L, value.c_str(), value.size());
  212. lua_settable(L, -3);
  213. }
  214. }
  215. lua_setfield(L, -2, "fields");
  216. }
  217. bool MetaDataRef::handleFromTable(lua_State *L, int table, Metadata *meta)
  218. {
  219. // Set fields
  220. lua_getfield(L, table, "fields");
  221. if (lua_istable(L, -1)) {
  222. int fieldstable = lua_gettop(L);
  223. lua_pushnil(L);
  224. while (lua_next(L, fieldstable) != 0) {
  225. // key at index -2 and value at index -1
  226. std::string name = readParam<std::string>(L, -2);
  227. size_t cl;
  228. const char *cs = lua_tolstring(L, -1, &cl);
  229. meta->setString(name, std::string(cs, cl));
  230. lua_pop(L, 1); // Remove value, keep key for next iteration
  231. }
  232. lua_pop(L, 1);
  233. }
  234. return true;
  235. }
  236. // equals(self, other)
  237. int MetaDataRef::l_equals(lua_State *L)
  238. {
  239. MetaDataRef *ref1 = checkobject(L, 1);
  240. Metadata *data1 = ref1->getmeta(false);
  241. MetaDataRef *ref2 = checkobject(L, 2);
  242. Metadata *data2 = ref2->getmeta(false);
  243. if (data1 == NULL || data2 == NULL)
  244. lua_pushboolean(L, data1 == data2);
  245. else
  246. lua_pushboolean(L, *data1 == *data2);
  247. return 1;
  248. }