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 SANITY_CHECK(!m_is_local);
59 m_env->getMap().removeNodeMetadata(m_p);
62 void NodeMetaRef::reportMetadataChange(const std::string *name)
64 SANITY_CHECK(!m_is_local);
65 // NOTE: This same code is in rollback_interface.cpp
66 // Inform other things that the metadata has changed
67 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(m_meta);
70 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
72 event.is_private_change = name && meta && meta->isPrivate(*name);
73 m_env->getMap().dispatchEvent(event);
79 int NodeMetaRef::gc_object(lua_State *L) {
80 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
85 // get_inventory(self)
86 int NodeMetaRef::l_get_inventory(lua_State *L)
90 NodeMetaRef *ref = checkobject(L, 1);
91 ref->getmeta(true); // try to ensure the metadata exists
92 InvRef::createNodeMeta(L, ref->m_p);
96 // mark_as_private(self, <string> or {<string>, <string>, ...})
97 int NodeMetaRef::l_mark_as_private(lua_State *L)
101 NodeMetaRef *ref = checkobject(L, 1);
102 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
105 if (lua_istable(L, 2)) {
107 while (lua_next(L, 2) != 0) {
108 // key at index -2 and value at index -1
109 luaL_checktype(L, -1, LUA_TSTRING);
110 meta->markPrivate(readParam<std::string>(L, -1), true);
111 // removes value, keeps key for next iteration
114 } else if (lua_isstring(L, 2)) {
115 meta->markPrivate(readParam<std::string>(L, 2), true);
117 ref->reportMetadataChange();
122 void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
125 MetaDataRef::handleToTable(L, _meta);
127 NodeMetadata *meta = (NodeMetadata*) _meta;
131 Inventory *inv = meta->getInventory();
133 std::vector<const InventoryList *> lists = inv->getLists();
134 for(std::vector<const InventoryList *>::const_iterator
135 i = lists.begin(); i != lists.end(); ++i) {
136 push_inventory_list(L, inv, (*i)->getName().c_str());
137 lua_setfield(L, -2, (*i)->getName().c_str());
140 lua_setfield(L, -2, "inventory");
143 // from_table(self, table)
144 bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
147 if (!MetaDataRef::handleFromTable(L, table, _meta))
150 NodeMetadata *meta = (NodeMetadata*) _meta;
153 Inventory *inv = meta->getInventory();
154 lua_getfield(L, table, "inventory");
155 if (lua_istable(L, -1)) {
156 int inventorytable = lua_gettop(L);
158 while (lua_next(L, inventorytable) != 0) {
159 // key at index -2 and value at index -1
160 std::string name = luaL_checkstring(L, -2);
161 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
162 lua_pop(L, 1); // Remove value, keep key for next iteration
171 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
177 NodeMetaRef::NodeMetaRef(Metadata *meta):
183 // Creates an NodeMetaRef and leaves it on top of stack
184 // Not callable from Lua; all references are created on the C side.
185 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
187 NodeMetaRef *o = new NodeMetaRef(p, env);
188 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
189 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
190 luaL_getmetatable(L, className);
191 lua_setmetatable(L, -2);
194 // Client-sided version of the above
195 void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
197 NodeMetaRef *o = new NodeMetaRef(meta);
198 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
199 luaL_getmetatable(L, className);
200 lua_setmetatable(L, -2);
203 const char NodeMetaRef::className[] = "NodeMetaRef";
204 void NodeMetaRef::RegisterCommon(lua_State *L)
207 int methodtable = lua_gettop(L);
208 luaL_newmetatable(L, className);
209 int metatable = lua_gettop(L);
211 lua_pushliteral(L, "__metatable");
212 lua_pushvalue(L, methodtable);
213 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
215 lua_pushliteral(L, "metadata_class");
216 lua_pushlstring(L, className, strlen(className));
217 lua_settable(L, metatable);
219 lua_pushliteral(L, "__index");
220 lua_pushvalue(L, methodtable);
221 lua_settable(L, metatable);
223 lua_pushliteral(L, "__gc");
224 lua_pushcfunction(L, gc_object);
225 lua_settable(L, metatable);
227 lua_pushliteral(L, "__eq");
228 lua_pushcfunction(L, l_equals);
229 lua_settable(L, metatable);
231 lua_pop(L, 1); // drop metatable
234 void NodeMetaRef::Register(lua_State *L)
237 luaL_openlib(L, 0, methodsServer, 0); // fill methodtable
238 lua_pop(L, 1); // drop methodtable
242 const luaL_Reg NodeMetaRef::methodsServer[] = {
243 luamethod(MetaDataRef, contains),
244 luamethod(MetaDataRef, get),
245 luamethod(MetaDataRef, get_string),
246 luamethod(MetaDataRef, set_string),
247 luamethod(MetaDataRef, get_int),
248 luamethod(MetaDataRef, set_int),
249 luamethod(MetaDataRef, get_float),
250 luamethod(MetaDataRef, set_float),
251 luamethod(MetaDataRef, to_table),
252 luamethod(MetaDataRef, from_table),
253 luamethod(NodeMetaRef, get_inventory),
254 luamethod(NodeMetaRef, mark_as_private),
255 luamethod(MetaDataRef, equals),
260 void NodeMetaRef::RegisterClient(lua_State *L)
263 luaL_openlib(L, 0, methodsClient, 0); // fill methodtable
264 lua_pop(L, 1); // drop methodtable
268 const luaL_Reg NodeMetaRef::methodsClient[] = {
269 luamethod(MetaDataRef, contains),
270 luamethod(MetaDataRef, get),
271 luamethod(MetaDataRef, get_string),
272 luamethod(MetaDataRef, get_int),
273 luamethod(MetaDataRef, get_float),
274 luamethod(MetaDataRef, to_table),