l_nodemeta.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. IMetadata* NodeMetaRef::getmeta(bool auto_create)
  28. {
  29. if (m_is_local)
  30. return m_local_meta;
  31. NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p);
  32. if (meta == NULL && auto_create) {
  33. meta = new NodeMetadata(m_env->getGameDef()->idef());
  34. if (!m_env->getMap().setNodeMetadata(m_p, meta)) {
  35. delete meta;
  36. return NULL;
  37. }
  38. }
  39. return meta;
  40. }
  41. void NodeMetaRef::clearMeta()
  42. {
  43. SANITY_CHECK(!m_is_local);
  44. m_env->getMap().removeNodeMetadata(m_p);
  45. }
  46. void NodeMetaRef::reportMetadataChange(const std::string *name)
  47. {
  48. SANITY_CHECK(!m_is_local);
  49. // Inform other things that the metadata has changed
  50. NodeMetadata *meta = dynamic_cast<NodeMetadata*>(getmeta(false));
  51. // If the metadata is now empty, get rid of it
  52. if (meta && meta->empty()) {
  53. clearMeta();
  54. meta = nullptr;
  55. }
  56. MapEditEvent event;
  57. event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
  58. event.setPositionModified(m_p);
  59. event.is_private_change = name && meta && meta->isPrivate(*name);
  60. m_env->getMap().dispatchEvent(event);
  61. }
  62. // Exported functions
  63. // get_inventory(self)
  64. int NodeMetaRef::l_get_inventory(lua_State *L)
  65. {
  66. MAP_LOCK_REQUIRED;
  67. NodeMetaRef *ref = checkObject<NodeMetaRef>(L, 1);
  68. ref->getmeta(true); // try to ensure the metadata exists
  69. InventoryLocation loc;
  70. loc.setNodeMeta(ref->m_p);
  71. InvRef::create(L, loc);
  72. return 1;
  73. }
  74. // mark_as_private(self, <string> or {<string>, <string>, ...})
  75. int NodeMetaRef::l_mark_as_private(lua_State *L)
  76. {
  77. MAP_LOCK_REQUIRED;
  78. NodeMetaRef *ref = checkObject<NodeMetaRef>(L, 1);
  79. NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
  80. assert(meta);
  81. if (lua_istable(L, 2)) {
  82. lua_pushnil(L);
  83. while (lua_next(L, 2) != 0) {
  84. // key at index -2 and value at index -1
  85. luaL_checktype(L, -1, LUA_TSTRING);
  86. meta->markPrivate(readParam<std::string>(L, -1), true);
  87. // removes value, keeps key for next iteration
  88. lua_pop(L, 1);
  89. }
  90. } else if (lua_isstring(L, 2)) {
  91. meta->markPrivate(readParam<std::string>(L, 2), true);
  92. }
  93. ref->reportMetadataChange();
  94. return 0;
  95. }
  96. void NodeMetaRef::handleToTable(lua_State *L, IMetadata *_meta)
  97. {
  98. // fields
  99. MetaDataRef::handleToTable(L, _meta);
  100. NodeMetadata *meta = dynamic_cast<NodeMetadata*>(_meta);
  101. assert(meta);
  102. // inventory
  103. Inventory *inv = meta->getInventory();
  104. if (inv) {
  105. push_inventory_lists(L, *inv);
  106. } else {
  107. lua_newtable(L);
  108. }
  109. lua_setfield(L, -2, "inventory");
  110. }
  111. // from_table(self, table)
  112. bool NodeMetaRef::handleFromTable(lua_State *L, int table, IMetadata *_meta)
  113. {
  114. // fields
  115. if (!MetaDataRef::handleFromTable(L, table, _meta))
  116. return false;
  117. NodeMetadata *meta = dynamic_cast<NodeMetadata*>(_meta);
  118. assert(meta);
  119. // inventory
  120. Inventory *inv = meta->getInventory();
  121. lua_getfield(L, table, "inventory");
  122. if (lua_istable(L, -1)) {
  123. int inventorytable = lua_gettop(L);
  124. lua_pushnil(L);
  125. while (lua_next(L, inventorytable) != 0) {
  126. // key at index -2 and value at index -1
  127. std::string name = luaL_checkstring(L, -2);
  128. read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
  129. lua_pop(L, 1); // Remove value, keep key for next iteration
  130. }
  131. lua_pop(L, 1);
  132. }
  133. return true;
  134. }
  135. NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
  136. m_p(p),
  137. m_env(env)
  138. {
  139. }
  140. NodeMetaRef::NodeMetaRef(IMetadata *meta):
  141. m_is_local(true),
  142. m_local_meta(meta)
  143. {
  144. }
  145. // Creates an NodeMetaRef and leaves it on top of stack
  146. // Not callable from Lua; all references are created on the C side.
  147. void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
  148. {
  149. NodeMetaRef *o = new NodeMetaRef(p, env);
  150. //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
  151. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  152. luaL_getmetatable(L, className);
  153. lua_setmetatable(L, -2);
  154. }
  155. // Client-sided version of the above
  156. void NodeMetaRef::createClient(lua_State *L, IMetadata *meta)
  157. {
  158. NodeMetaRef *o = new NodeMetaRef(meta);
  159. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  160. luaL_getmetatable(L, className);
  161. lua_setmetatable(L, -2);
  162. }
  163. const char NodeMetaRef::className[] = "NodeMetaRef";
  164. void NodeMetaRef::Register(lua_State *L)
  165. {
  166. registerMetadataClass(L, className, methodsServer);
  167. }
  168. const luaL_Reg NodeMetaRef::methodsServer[] = {
  169. luamethod(MetaDataRef, contains),
  170. luamethod(MetaDataRef, get),
  171. luamethod(MetaDataRef, get_string),
  172. luamethod(MetaDataRef, set_string),
  173. luamethod(MetaDataRef, get_int),
  174. luamethod(MetaDataRef, set_int),
  175. luamethod(MetaDataRef, get_float),
  176. luamethod(MetaDataRef, set_float),
  177. luamethod(MetaDataRef, get_keys),
  178. luamethod(MetaDataRef, to_table),
  179. luamethod(MetaDataRef, from_table),
  180. luamethod(NodeMetaRef, get_inventory),
  181. luamethod(NodeMetaRef, mark_as_private),
  182. luamethod(MetaDataRef, equals),
  183. {0,0}
  184. };
  185. void NodeMetaRef::RegisterClient(lua_State *L)
  186. {
  187. registerMetadataClass(L, className, methodsClient);
  188. }
  189. const luaL_Reg NodeMetaRef::methodsClient[] = {
  190. luamethod(MetaDataRef, contains),
  191. luamethod(MetaDataRef, get),
  192. luamethod(MetaDataRef, get_string),
  193. luamethod(MetaDataRef, get_int),
  194. luamethod(MetaDataRef, get_float),
  195. luamethod(MetaDataRef, get_keys),
  196. luamethod(MetaDataRef, to_table),
  197. {0,0}
  198. };