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.
 
 
 
 
 
 

283 lines
7.8 KiB

  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_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. m_env->getMap().removeNodeMetadata(m_p);
  51. }
  52. void NodeMetaRef::reportMetadataChange()
  53. {
  54. // NOTE: This same code is in rollback_interface.cpp
  55. // Inform other things that the metadata has changed
  56. v3s16 blockpos = getNodeBlockPos(m_p);
  57. MapEditEvent event;
  58. event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
  59. event.p = blockpos;
  60. m_env->getMap().dispatchEvent(&event);
  61. // Set the block to be saved
  62. MapBlock *block = m_env->getMap().getBlockNoCreateNoEx(blockpos);
  63. if (block) {
  64. block->raiseModified(MOD_STATE_WRITE_NEEDED,
  65. MOD_REASON_REPORT_META_CHANGE);
  66. }
  67. }
  68. // Exported functions
  69. // garbage collector
  70. int NodeMetaRef::gc_object(lua_State *L) {
  71. NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
  72. delete o;
  73. return 0;
  74. }
  75. // get_inventory(self)
  76. int NodeMetaRef::l_get_inventory(lua_State *L)
  77. {
  78. MAP_LOCK_REQUIRED;
  79. NodeMetaRef *ref = checkobject(L, 1);
  80. ref->getmeta(true); // try to ensure the metadata exists
  81. InvRef::createNodeMeta(L, ref->m_p);
  82. return 1;
  83. }
  84. // mark_as_private(self, <string> or {<string>, <string>, ...})
  85. int NodeMetaRef::l_mark_as_private(lua_State *L)
  86. {
  87. MAP_LOCK_REQUIRED;
  88. NodeMetaRef *ref = checkobject(L, 1);
  89. NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
  90. assert(meta);
  91. if (lua_istable(L, 2)) {
  92. lua_pushnil(L);
  93. while (lua_next(L, 2) != 0) {
  94. // key at index -2 and value at index -1
  95. luaL_checktype(L, -1, LUA_TSTRING);
  96. meta->markPrivate(readParam<std::string>(L, -1), true);
  97. // removes value, keeps key for next iteration
  98. lua_pop(L, 1);
  99. }
  100. } else if (lua_isstring(L, 2)) {
  101. meta->markPrivate(readParam<std::string>(L, 2), true);
  102. }
  103. ref->reportMetadataChange();
  104. return 0;
  105. }
  106. void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
  107. {
  108. // fields
  109. MetaDataRef::handleToTable(L, _meta);
  110. NodeMetadata *meta = (NodeMetadata*) _meta;
  111. // inventory
  112. lua_newtable(L);
  113. Inventory *inv = meta->getInventory();
  114. if (inv) {
  115. std::vector<const InventoryList *> lists = inv->getLists();
  116. for(std::vector<const InventoryList *>::const_iterator
  117. i = lists.begin(); i != lists.end(); ++i) {
  118. push_inventory_list(L, inv, (*i)->getName().c_str());
  119. lua_setfield(L, -2, (*i)->getName().c_str());
  120. }
  121. }
  122. lua_setfield(L, -2, "inventory");
  123. }
  124. // from_table(self, table)
  125. bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
  126. {
  127. // fields
  128. if (!MetaDataRef::handleFromTable(L, table, _meta))
  129. return false;
  130. NodeMetadata *meta = (NodeMetadata*) _meta;
  131. // inventory
  132. Inventory *inv = meta->getInventory();
  133. lua_getfield(L, table, "inventory");
  134. if (lua_istable(L, -1)) {
  135. int inventorytable = lua_gettop(L);
  136. lua_pushnil(L);
  137. while (lua_next(L, inventorytable) != 0) {
  138. // key at index -2 and value at index -1
  139. std::string name = luaL_checkstring(L, -2);
  140. read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
  141. lua_pop(L, 1); // Remove value, keep key for next iteration
  142. }
  143. lua_pop(L, 1);
  144. }
  145. return true;
  146. }
  147. NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
  148. m_p(p),
  149. m_env(env)
  150. {
  151. }
  152. NodeMetaRef::NodeMetaRef(Metadata *meta):
  153. m_meta(meta),
  154. m_is_local(true)
  155. {
  156. }
  157. NodeMetaRef::~NodeMetaRef()
  158. {
  159. }
  160. // Creates an NodeMetaRef and leaves it on top of stack
  161. // Not callable from Lua; all references are created on the C side.
  162. void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
  163. {
  164. NodeMetaRef *o = new NodeMetaRef(p, env);
  165. //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
  166. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  167. luaL_getmetatable(L, className);
  168. lua_setmetatable(L, -2);
  169. }
  170. // Client-sided version of the above
  171. void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
  172. {
  173. NodeMetaRef *o = new NodeMetaRef(meta);
  174. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  175. luaL_getmetatable(L, className);
  176. lua_setmetatable(L, -2);
  177. }
  178. const char NodeMetaRef::className[] = "NodeMetaRef";
  179. void NodeMetaRef::RegisterCommon(lua_State *L)
  180. {
  181. lua_newtable(L);
  182. int methodtable = lua_gettop(L);
  183. luaL_newmetatable(L, className);
  184. int metatable = lua_gettop(L);
  185. lua_pushliteral(L, "__metatable");
  186. lua_pushvalue(L, methodtable);
  187. lua_settable(L, metatable); // hide metatable from Lua getmetatable()
  188. lua_pushliteral(L, "metadata_class");
  189. lua_pushlstring(L, className, strlen(className));
  190. lua_settable(L, metatable);
  191. lua_pushliteral(L, "__index");
  192. lua_pushvalue(L, methodtable);
  193. lua_settable(L, metatable);
  194. lua_pushliteral(L, "__gc");
  195. lua_pushcfunction(L, gc_object);
  196. lua_settable(L, metatable);
  197. lua_pushliteral(L, "__eq");
  198. lua_pushcfunction(L, l_equals);
  199. lua_settable(L, metatable);
  200. lua_pop(L, 1); // drop metatable
  201. }
  202. void NodeMetaRef::Register(lua_State *L)
  203. {
  204. RegisterCommon(L);
  205. luaL_openlib(L, 0, methodsServer, 0); // fill methodtable
  206. lua_pop(L, 1); // drop methodtable
  207. }
  208. const luaL_Reg NodeMetaRef::methodsServer[] = {
  209. luamethod(MetaDataRef, contains),
  210. luamethod(MetaDataRef, get),
  211. luamethod(MetaDataRef, get_string),
  212. luamethod(MetaDataRef, set_string),
  213. luamethod(MetaDataRef, get_int),
  214. luamethod(MetaDataRef, set_int),
  215. luamethod(MetaDataRef, get_float),
  216. luamethod(MetaDataRef, set_float),
  217. luamethod(MetaDataRef, to_table),
  218. luamethod(MetaDataRef, from_table),
  219. luamethod(NodeMetaRef, get_inventory),
  220. luamethod(NodeMetaRef, mark_as_private),
  221. luamethod(MetaDataRef, equals),
  222. {0,0}
  223. };
  224. void NodeMetaRef::RegisterClient(lua_State *L)
  225. {
  226. RegisterCommon(L);
  227. luaL_openlib(L, 0, methodsClient, 0); // fill methodtable
  228. lua_pop(L, 1); // drop methodtable
  229. }
  230. const luaL_Reg NodeMetaRef::methodsClient[] = {
  231. luamethod(MetaDataRef, contains),
  232. luamethod(MetaDataRef, get),
  233. luamethod(MetaDataRef, get_string),
  234. luamethod(MetaDataRef, get_int),
  235. luamethod(MetaDataRef, get_float),
  236. luamethod(MetaDataRef, to_table),
  237. {0,0}
  238. };