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 // Inform other things that the metadata has changed
66 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(getmeta(false));
68 // If the metadata is now empty, get rid of it
69 if (meta && meta->empty()) {
75 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
77 event.is_private_change = name && meta && meta->isPrivate(*name);
78 m_env->getMap().dispatchEvent(event);
84 int NodeMetaRef::gc_object(lua_State *L) {
85 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
90 // get_inventory(self)
91 int NodeMetaRef::l_get_inventory(lua_State *L)
95 NodeMetaRef *ref = checkobject(L, 1);
96 ref->getmeta(true); // try to ensure the metadata exists
98 InventoryLocation loc;
99 loc.setNodeMeta(ref->m_p);
100 InvRef::create(L, loc);
104 // mark_as_private(self, <string> or {<string>, <string>, ...})
105 int NodeMetaRef::l_mark_as_private(lua_State *L)
109 NodeMetaRef *ref = checkobject(L, 1);
110 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
113 if (lua_istable(L, 2)) {
115 while (lua_next(L, 2) != 0) {
116 // key at index -2 and value at index -1
117 luaL_checktype(L, -1, LUA_TSTRING);
118 meta->markPrivate(readParam<std::string>(L, -1), true);
119 // removes value, keeps key for next iteration
122 } else if (lua_isstring(L, 2)) {
123 meta->markPrivate(readParam<std::string>(L, 2), true);
125 ref->reportMetadataChange();
130 void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
133 MetaDataRef::handleToTable(L, _meta);
135 NodeMetadata *meta = (NodeMetadata *) _meta;
138 Inventory *inv = meta->getInventory();
140 push_inventory_lists(L, *inv);
144 lua_setfield(L, -2, "inventory");
147 // from_table(self, table)
148 bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
151 if (!MetaDataRef::handleFromTable(L, table, _meta))
154 NodeMetadata *meta = (NodeMetadata*) _meta;
157 Inventory *inv = meta->getInventory();
158 lua_getfield(L, table, "inventory");
159 if (lua_istable(L, -1)) {
160 int inventorytable = lua_gettop(L);
162 while (lua_next(L, inventorytable) != 0) {
163 // key at index -2 and value at index -1
164 std::string name = luaL_checkstring(L, -2);
165 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
166 lua_pop(L, 1); // Remove value, keep key for next iteration
175 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
181 NodeMetaRef::NodeMetaRef(Metadata *meta):
187 // Creates an NodeMetaRef and leaves it on top of stack
188 // Not callable from Lua; all references are created on the C side.
189 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
191 NodeMetaRef *o = new NodeMetaRef(p, env);
192 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
193 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
194 luaL_getmetatable(L, className);
195 lua_setmetatable(L, -2);
198 // Client-sided version of the above
199 void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
201 NodeMetaRef *o = new NodeMetaRef(meta);
202 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
203 luaL_getmetatable(L, className);
204 lua_setmetatable(L, -2);
207 const char NodeMetaRef::className[] = "NodeMetaRef";
208 void NodeMetaRef::RegisterCommon(lua_State *L)
211 int methodtable = lua_gettop(L);
212 luaL_newmetatable(L, className);
213 int metatable = lua_gettop(L);
215 lua_pushliteral(L, "__metatable");
216 lua_pushvalue(L, methodtable);
217 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
219 lua_pushliteral(L, "metadata_class");
220 lua_pushlstring(L, className, strlen(className));
221 lua_settable(L, metatable);
223 lua_pushliteral(L, "__index");
224 lua_pushvalue(L, methodtable);
225 lua_settable(L, metatable);
227 lua_pushliteral(L, "__gc");
228 lua_pushcfunction(L, gc_object);
229 lua_settable(L, metatable);
231 lua_pushliteral(L, "__eq");
232 lua_pushcfunction(L, l_equals);
233 lua_settable(L, metatable);
235 lua_pop(L, 1); // drop metatable
238 void NodeMetaRef::Register(lua_State *L)
241 luaL_register(L, nullptr, methodsServer); // fill methodtable
242 lua_pop(L, 1); // drop methodtable
246 const luaL_Reg NodeMetaRef::methodsServer[] = {
247 luamethod(MetaDataRef, contains),
248 luamethod(MetaDataRef, get),
249 luamethod(MetaDataRef, get_string),
250 luamethod(MetaDataRef, set_string),
251 luamethod(MetaDataRef, get_int),
252 luamethod(MetaDataRef, set_int),
253 luamethod(MetaDataRef, get_float),
254 luamethod(MetaDataRef, set_float),
255 luamethod(MetaDataRef, to_table),
256 luamethod(MetaDataRef, from_table),
257 luamethod(NodeMetaRef, get_inventory),
258 luamethod(NodeMetaRef, mark_as_private),
259 luamethod(MetaDataRef, equals),
264 void NodeMetaRef::RegisterClient(lua_State *L)
267 luaL_register(L, nullptr, methodsClient); // fill methodtable
268 lua_pop(L, 1); // drop methodtable
272 const luaL_Reg NodeMetaRef::methodsClient[] = {
273 luamethod(MetaDataRef, contains),
274 luamethod(MetaDataRef, get),
275 luamethod(MetaDataRef, get_string),
276 luamethod(MetaDataRef, get_int),
277 luamethod(MetaDataRef, get_float),
278 luamethod(MetaDataRef, to_table),