3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_nodemeta.h"
21 #include "lua_api/l_internal.h"
22 #include "lua_api/l_inventory.h"
23 #include "common/c_content.h"
24 #include "serverenvironment.h"
32 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
34 luaL_checktype(L, narg, LUA_TUSERDATA);
35 void *ud = luaL_checkudata(L, narg, className);
36 if(!ud) luaL_typerror(L, narg, className);
37 return *(NodeMetaRef**)ud; // unbox pointer
40 Metadata* NodeMetaRef::getmeta(bool auto_create)
45 NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p);
46 if (meta == NULL && auto_create) {
47 meta = new NodeMetadata(m_env->getGameDef()->idef());
48 if (!m_env->getMap().setNodeMetadata(m_p, meta)) {
56 void NodeMetaRef::clearMeta()
58 m_env->getMap().removeNodeMetadata(m_p);
61 void NodeMetaRef::reportMetadataChange()
63 // NOTE: This same code is in rollback_interface.cpp
64 // Inform other things that the metadata has changed
65 v3s16 blockpos = getNodeBlockPos(m_p);
67 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
69 m_env->getMap().dispatchEvent(&event);
70 // Set the block to be saved
71 MapBlock *block = m_env->getMap().getBlockNoCreateNoEx(blockpos);
73 block->raiseModified(MOD_STATE_WRITE_NEEDED,
74 MOD_REASON_REPORT_META_CHANGE);
81 int NodeMetaRef::gc_object(lua_State *L) {
82 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
87 // get_inventory(self)
88 int NodeMetaRef::l_get_inventory(lua_State *L)
92 NodeMetaRef *ref = checkobject(L, 1);
93 ref->getmeta(true); // try to ensure the metadata exists
94 InvRef::createNodeMeta(L, ref->m_p);
98 // mark_as_private(self, <string> or {<string>, <string>, ...})
99 int NodeMetaRef::l_mark_as_private(lua_State *L)
103 NodeMetaRef *ref = checkobject(L, 1);
104 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
107 if (lua_istable(L, 2)) {
109 while (lua_next(L, 2) != 0) {
110 // key at index -2 and value at index -1
111 luaL_checktype(L, -1, LUA_TSTRING);
112 meta->markPrivate(readParam<std::string>(L, -1), true);
113 // removes value, keeps key for next iteration
116 } else if (lua_isstring(L, 2)) {
117 meta->markPrivate(readParam<std::string>(L, 2), true);
119 ref->reportMetadataChange();
124 void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
127 MetaDataRef::handleToTable(L, _meta);
129 NodeMetadata *meta = (NodeMetadata*) _meta;
133 Inventory *inv = meta->getInventory();
135 std::vector<const InventoryList *> lists = inv->getLists();
136 for(std::vector<const InventoryList *>::const_iterator
137 i = lists.begin(); i != lists.end(); ++i) {
138 push_inventory_list(L, inv, (*i)->getName().c_str());
139 lua_setfield(L, -2, (*i)->getName().c_str());
142 lua_setfield(L, -2, "inventory");
145 // from_table(self, table)
146 bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
149 if (!MetaDataRef::handleFromTable(L, table, _meta))
152 NodeMetadata *meta = (NodeMetadata*) _meta;
155 Inventory *inv = meta->getInventory();
156 lua_getfield(L, table, "inventory");
157 if (lua_istable(L, -1)) {
158 int inventorytable = lua_gettop(L);
160 while (lua_next(L, inventorytable) != 0) {
161 // key at index -2 and value at index -1
162 std::string name = luaL_checkstring(L, -2);
163 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
164 lua_pop(L, 1); // Remove value, keep key for next iteration
173 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
179 NodeMetaRef::NodeMetaRef(Metadata *meta):
185 // Creates an NodeMetaRef and leaves it on top of stack
186 // Not callable from Lua; all references are created on the C side.
187 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
189 NodeMetaRef *o = new NodeMetaRef(p, env);
190 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
191 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
192 luaL_getmetatable(L, className);
193 lua_setmetatable(L, -2);
196 // Client-sided version of the above
197 void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
199 NodeMetaRef *o = new NodeMetaRef(meta);
200 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
201 luaL_getmetatable(L, className);
202 lua_setmetatable(L, -2);
205 const char NodeMetaRef::className[] = "NodeMetaRef";
206 void NodeMetaRef::RegisterCommon(lua_State *L)
209 int methodtable = lua_gettop(L);
210 luaL_newmetatable(L, className);
211 int metatable = lua_gettop(L);
213 lua_pushliteral(L, "__metatable");
214 lua_pushvalue(L, methodtable);
215 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
217 lua_pushliteral(L, "metadata_class");
218 lua_pushlstring(L, className, strlen(className));
219 lua_settable(L, metatable);
221 lua_pushliteral(L, "__index");
222 lua_pushvalue(L, methodtable);
223 lua_settable(L, metatable);
225 lua_pushliteral(L, "__gc");
226 lua_pushcfunction(L, gc_object);
227 lua_settable(L, metatable);
229 lua_pushliteral(L, "__eq");
230 lua_pushcfunction(L, l_equals);
231 lua_settable(L, metatable);
233 lua_pop(L, 1); // drop metatable
236 void NodeMetaRef::Register(lua_State *L)
239 luaL_openlib(L, 0, methodsServer, 0); // fill methodtable
240 lua_pop(L, 1); // drop methodtable
244 const luaL_Reg NodeMetaRef::methodsServer[] = {
245 luamethod(MetaDataRef, contains),
246 luamethod(MetaDataRef, get),
247 luamethod(MetaDataRef, get_string),
248 luamethod(MetaDataRef, set_string),
249 luamethod(MetaDataRef, get_int),
250 luamethod(MetaDataRef, set_int),
251 luamethod(MetaDataRef, get_float),
252 luamethod(MetaDataRef, set_float),
253 luamethod(MetaDataRef, to_table),
254 luamethod(MetaDataRef, from_table),
255 luamethod(NodeMetaRef, get_inventory),
256 luamethod(NodeMetaRef, mark_as_private),
257 luamethod(MetaDataRef, equals),
262 void NodeMetaRef::RegisterClient(lua_State *L)
265 luaL_openlib(L, 0, methodsClient, 0); // fill methodtable
266 lua_pop(L, 1); // drop methodtable
270 const luaL_Reg NodeMetaRef::methodsClient[] = {
271 luamethod(MetaDataRef, contains),
272 luamethod(MetaDataRef, get),
273 luamethod(MetaDataRef, get_string),
274 luamethod(MetaDataRef, get_int),
275 luamethod(MetaDataRef, get_float),
276 luamethod(MetaDataRef, to_table),