l_nodemeta.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include "lua_api/l_nodemeta.h"
  17. #include "lua_api/l_internal.h"
  18. #include "lua_api/l_inventory.h"
  19. #include "common/c_content.h"
  20. #include "serverenvironment.h"
  21. #include "map.h"
  22. #include "mapblock.h"
  23. #include "server.h"
  24. /*
  25. NodeMetaRef
  26. */
  27. NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
  28. {
  29. luaL_checktype(L, narg, LUA_TUSERDATA);
  30. void *ud = luaL_checkudata(L, narg, className);
  31. if(!ud) luaL_typerror(L, narg, className);
  32. return *(NodeMetaRef**)ud; // unbox pointer
  33. }
  34. Metadata* NodeMetaRef::getmeta(bool auto_create)
  35. {
  36. if (m_is_local)
  37. return m_local_meta;
  38. NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p);
  39. if (meta == NULL && auto_create) {
  40. meta = new NodeMetadata(m_env->getGameDef()->idef());
  41. if (!m_env->getMap().setNodeMetadata(m_p, meta)) {
  42. delete meta;
  43. return NULL;
  44. }
  45. }
  46. return meta;
  47. }
  48. void NodeMetaRef::clearMeta()
  49. {
  50. SANITY_CHECK(!m_is_local);
  51. m_env->getMap().removeNodeMetadata(m_p);
  52. }
  53. void NodeMetaRef::reportMetadataChange(const std::string *name)
  54. {
  55. SANITY_CHECK(!m_is_local);
  56. // Inform other things that the metadata has changed
  57. NodeMetadata *meta = dynamic_cast<NodeMetadata*>(getmeta(false));
  58. // If the metadata is now empty, get rid of it
  59. if (meta && meta->empty())
  60. clearMeta();
  61. MapEditEvent event;
  62. event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
  63. event.p = m_p;
  64. event.is_private_change = name && meta && meta->isPrivate(*name);
  65. m_env->getMap().dispatchEvent(event);
  66. }
  67. // Exported functions
  68. // garbage collector
  69. int NodeMetaRef::gc_object(lua_State *L) {
  70. NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
  71. delete o;
  72. return 0;
  73. }
  74. // get_inventory(self)
  75. int NodeMetaRef::l_get_inventory(lua_State *L)
  76. {
  77. MAP_LOCK_REQUIRED;
  78. NodeMetaRef *ref = checkobject(L, 1);
  79. ref->getmeta(true); // try to ensure the metadata exists
  80. InventoryLocation loc;
  81. loc.setNodeMeta(ref->m_p);
  82. InvRef::create(L, loc);
  83. return 1;
  84. }
  85. // mark_as_private(self, <string> or {<string>, <string>, ...})
  86. int NodeMetaRef::l_mark_as_private(lua_State *L)
  87. {
  88. MAP_LOCK_REQUIRED;
  89. NodeMetaRef *ref = checkobject(L, 1);
  90. NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
  91. assert(meta);
  92. if (lua_istable(L, 2)) {
  93. lua_pushnil(L);
  94. while (lua_next(L, 2) != 0) {
  95. // key at index -2 and value at index -1
  96. luaL_checktype(L, -1, LUA_TSTRING);
  97. meta->markPrivate(readParam<std::string>(L, -1), true);
  98. // removes value, keeps key for next iteration
  99. lua_pop(L, 1);
  100. }
  101. } else if (lua_isstring(L, 2)) {
  102. meta->markPrivate(readParam<std::string>(L, 2), true);
  103. }
  104. ref->reportMetadataChange();
  105. return 0;
  106. }
  107. void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
  108. {
  109. // fields
  110. MetaDataRef::handleToTable(L, _meta);
  111. NodeMetadata *meta = (NodeMetadata *) _meta;
  112. // inventory
  113. Inventory *inv = meta->getInventory();
  114. if (inv) {
  115. push_inventory_lists(L, *inv);
  116. } else {
  117. lua_newtable(L);
  118. }
  119. lua_setfield(L, -2, "inventory");
  120. }
  121. // from_table(self, table)
  122. bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
  123. {
  124. // fields
  125. if (!MetaDataRef::handleFromTable(L, table, _meta))
  126. return false;
  127. NodeMetadata *meta = (NodeMetadata*) _meta;
  128. // inventory
  129. Inventory *inv = meta->getInventory();
  130. lua_getfield(L, table, "inventory");
  131. if (lua_istable(L, -1)) {
  132. int inventorytable = lua_gettop(L);
  133. lua_pushnil(L);
  134. while (lua_next(L, inventorytable) != 0) {
  135. // key at index -2 and value at index -1
  136. std::string name = luaL_checkstring(L, -2);
  137. read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
  138. lua_pop(L, 1); // Remove value, keep key for next iteration
  139. }
  140. lua_pop(L, 1);
  141. }
  142. return true;
  143. }
  144. NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
  145. m_p(p),
  146. m_env(env)
  147. {
  148. }
  149. NodeMetaRef::NodeMetaRef(Metadata *meta):
  150. m_is_local(true),
  151. m_local_meta(meta)
  152. {
  153. }
  154. // Creates an NodeMetaRef and leaves it on top of stack
  155. // Not callable from Lua; all references are created on the C side.
  156. void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
  157. {
  158. NodeMetaRef *o = new NodeMetaRef(p, env);
  159. //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
  160. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  161. luaL_getmetatable(L, className);
  162. lua_setmetatable(L, -2);
  163. }
  164. // Client-sided version of the above
  165. void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
  166. {
  167. NodeMetaRef *o = new NodeMetaRef(meta);
  168. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  169. luaL_getmetatable(L, className);
  170. lua_setmetatable(L, -2);
  171. }
  172. const char NodeMetaRef::className[] = "NodeMetaRef";
  173. void NodeMetaRef::RegisterCommon(lua_State *L)
  174. {
  175. lua_newtable(L);
  176. int methodtable = lua_gettop(L);
  177. luaL_newmetatable(L, className);
  178. int metatable = lua_gettop(L);
  179. lua_pushliteral(L, "__metatable");
  180. lua_pushvalue(L, methodtable);
  181. lua_settable(L, metatable); // hide metatable from Lua getmetatable()
  182. lua_pushliteral(L, "metadata_class");
  183. lua_pushlstring(L, className, strlen(className));
  184. lua_settable(L, metatable);
  185. lua_pushliteral(L, "__index");
  186. lua_pushvalue(L, methodtable);
  187. lua_settable(L, metatable);
  188. lua_pushliteral(L, "__gc");
  189. lua_pushcfunction(L, gc_object);
  190. lua_settable(L, metatable);
  191. lua_pushliteral(L, "__eq");
  192. lua_pushcfunction(L, l_equals);
  193. lua_settable(L, metatable);
  194. lua_pop(L, 1); // drop metatable
  195. }
  196. void NodeMetaRef::Register(lua_State *L)
  197. {
  198. RegisterCommon(L);
  199. luaL_register(L, nullptr, methodsServer); // fill methodtable
  200. lua_pop(L, 1); // drop methodtable
  201. }
  202. const luaL_Reg NodeMetaRef::methodsServer[] = {
  203. luamethod(MetaDataRef, contains),
  204. luamethod(MetaDataRef, get),
  205. luamethod(MetaDataRef, get_string),
  206. luamethod(MetaDataRef, set_string),
  207. luamethod(MetaDataRef, get_int),
  208. luamethod(MetaDataRef, set_int),
  209. luamethod(MetaDataRef, get_float),
  210. luamethod(MetaDataRef, set_float),
  211. luamethod(MetaDataRef, to_table),
  212. luamethod(MetaDataRef, from_table),
  213. luamethod(NodeMetaRef, get_inventory),
  214. luamethod(NodeMetaRef, mark_as_private),
  215. luamethod(MetaDataRef, equals),
  216. {0,0}
  217. };
  218. void NodeMetaRef::RegisterClient(lua_State *L)
  219. {
  220. RegisterCommon(L);
  221. luaL_register(L, nullptr, methodsClient); // fill methodtable
  222. lua_pop(L, 1); // drop methodtable
  223. }
  224. const luaL_Reg NodeMetaRef::methodsClient[] = {
  225. luamethod(MetaDataRef, contains),
  226. luamethod(MetaDataRef, get),
  227. luamethod(MetaDataRef, get_string),
  228. luamethod(MetaDataRef, get_int),
  229. luamethod(MetaDataRef, get_float),
  230. luamethod(MetaDataRef, to_table),
  231. {0,0}
  232. };