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(const std::string *name)
63 // NOTE: This same code is in rollback_interface.cpp
64 // Inform other things that the metadata has changed
65 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(m_meta);
68 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
70 event.is_private_change = name && meta && meta->isPrivate(*name);
71 m_env->getMap().dispatchEvent(&event);
77 int NodeMetaRef::gc_object(lua_State *L) {
78 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
83 // get_inventory(self)
84 int NodeMetaRef::l_get_inventory(lua_State *L)
88 NodeMetaRef *ref = checkobject(L, 1);
89 ref->getmeta(true); // try to ensure the metadata exists
90 InvRef::createNodeMeta(L, ref->m_p);
94 // mark_as_private(self, <string> or {<string>, <string>, ...})
95 int NodeMetaRef::l_mark_as_private(lua_State *L)
99 NodeMetaRef *ref = checkobject(L, 1);
100 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
103 if (lua_istable(L, 2)) {
105 while (lua_next(L, 2) != 0) {
106 // key at index -2 and value at index -1
107 luaL_checktype(L, -1, LUA_TSTRING);
108 meta->markPrivate(readParam<std::string>(L, -1), true);
109 // removes value, keeps key for next iteration
112 } else if (lua_isstring(L, 2)) {
113 meta->markPrivate(readParam<std::string>(L, 2), true);
115 ref->reportMetadataChange();
120 void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
123 MetaDataRef::handleToTable(L, _meta);
125 NodeMetadata *meta = (NodeMetadata*) _meta;
129 Inventory *inv = meta->getInventory();
131 std::vector<const InventoryList *> lists = inv->getLists();
132 for(std::vector<const InventoryList *>::const_iterator
133 i = lists.begin(); i != lists.end(); ++i) {
134 push_inventory_list(L, inv, (*i)->getName().c_str());
135 lua_setfield(L, -2, (*i)->getName().c_str());
138 lua_setfield(L, -2, "inventory");
141 // from_table(self, table)
142 bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
145 if (!MetaDataRef::handleFromTable(L, table, _meta))
148 NodeMetadata *meta = (NodeMetadata*) _meta;
151 Inventory *inv = meta->getInventory();
152 lua_getfield(L, table, "inventory");
153 if (lua_istable(L, -1)) {
154 int inventorytable = lua_gettop(L);
156 while (lua_next(L, inventorytable) != 0) {
157 // key at index -2 and value at index -1
158 std::string name = luaL_checkstring(L, -2);
159 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
160 lua_pop(L, 1); // Remove value, keep key for next iteration
169 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
175 NodeMetaRef::NodeMetaRef(Metadata *meta):
181 // Creates an NodeMetaRef and leaves it on top of stack
182 // Not callable from Lua; all references are created on the C side.
183 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
185 NodeMetaRef *o = new NodeMetaRef(p, env);
186 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
187 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
188 luaL_getmetatable(L, className);
189 lua_setmetatable(L, -2);
192 // Client-sided version of the above
193 void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
195 NodeMetaRef *o = new NodeMetaRef(meta);
196 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
197 luaL_getmetatable(L, className);
198 lua_setmetatable(L, -2);
201 const char NodeMetaRef::className[] = "NodeMetaRef";
202 void NodeMetaRef::RegisterCommon(lua_State *L)
205 int methodtable = lua_gettop(L);
206 luaL_newmetatable(L, className);
207 int metatable = lua_gettop(L);
209 lua_pushliteral(L, "__metatable");
210 lua_pushvalue(L, methodtable);
211 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
213 lua_pushliteral(L, "metadata_class");
214 lua_pushlstring(L, className, strlen(className));
215 lua_settable(L, metatable);
217 lua_pushliteral(L, "__index");
218 lua_pushvalue(L, methodtable);
219 lua_settable(L, metatable);
221 lua_pushliteral(L, "__gc");
222 lua_pushcfunction(L, gc_object);
223 lua_settable(L, metatable);
225 lua_pushliteral(L, "__eq");
226 lua_pushcfunction(L, l_equals);
227 lua_settable(L, metatable);
229 lua_pop(L, 1); // drop metatable
232 void NodeMetaRef::Register(lua_State *L)
235 luaL_openlib(L, 0, methodsServer, 0); // fill methodtable
236 lua_pop(L, 1); // drop methodtable
240 const luaL_Reg NodeMetaRef::methodsServer[] = {
241 luamethod(MetaDataRef, contains),
242 luamethod(MetaDataRef, get),
243 luamethod(MetaDataRef, get_string),
244 luamethod(MetaDataRef, set_string),
245 luamethod(MetaDataRef, get_int),
246 luamethod(MetaDataRef, set_int),
247 luamethod(MetaDataRef, get_float),
248 luamethod(MetaDataRef, set_float),
249 luamethod(MetaDataRef, to_table),
250 luamethod(MetaDataRef, from_table),
251 luamethod(NodeMetaRef, get_inventory),
252 luamethod(NodeMetaRef, mark_as_private),
253 luamethod(MetaDataRef, equals),
258 void NodeMetaRef::RegisterClient(lua_State *L)
261 luaL_openlib(L, 0, methodsClient, 0); // fill methodtable
262 lua_pop(L, 1); // drop methodtable
266 const luaL_Reg NodeMetaRef::methodsClient[] = {
267 luamethod(MetaDataRef, contains),
268 luamethod(MetaDataRef, get),
269 luamethod(MetaDataRef, get_string),
270 luamethod(MetaDataRef, get_int),
271 luamethod(MetaDataRef, get_float),
272 luamethod(MetaDataRef, to_table),