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"
31 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
33 luaL_checktype(L, narg, LUA_TUSERDATA);
34 void *ud = luaL_checkudata(L, narg, className);
35 if(!ud) luaL_typerror(L, narg, className);
36 return *(NodeMetaRef**)ud; // unbox pointer
39 Metadata* NodeMetaRef::getmeta(bool auto_create)
44 NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p);
45 if (meta == NULL && auto_create) {
46 meta = new NodeMetadata(m_env->getGameDef()->idef());
47 if (!m_env->getMap().setNodeMetadata(m_p, meta)) {
55 void NodeMetaRef::clearMeta()
57 m_env->getMap().removeNodeMetadata(m_p);
60 void NodeMetaRef::reportMetadataChange()
62 // NOTE: This same code is in rollback_interface.cpp
63 // Inform other things that the metadata has changed
64 v3s16 blockpos = getNodeBlockPos(m_p);
66 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
68 m_env->getMap().dispatchEvent(&event);
69 // Set the block to be saved
70 MapBlock *block = m_env->getMap().getBlockNoCreateNoEx(blockpos);
72 block->raiseModified(MOD_STATE_WRITE_NEEDED,
73 MOD_REASON_REPORT_META_CHANGE);
80 int NodeMetaRef::gc_object(lua_State *L) {
81 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
86 // get_inventory(self)
87 int NodeMetaRef::l_get_inventory(lua_State *L)
91 NodeMetaRef *ref = checkobject(L, 1);
92 ref->getmeta(true); // try to ensure the metadata exists
93 InvRef::createNodeMeta(L, ref->m_p);
97 void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
100 MetaDataRef::handleToTable(L, _meta);
102 NodeMetadata *meta = (NodeMetadata*) _meta;
106 Inventory *inv = meta->getInventory();
108 std::vector<const InventoryList *> lists = inv->getLists();
109 for(std::vector<const InventoryList *>::const_iterator
110 i = lists.begin(); i != lists.end(); i++) {
111 push_inventory_list(L, inv, (*i)->getName().c_str());
112 lua_setfield(L, -2, (*i)->getName().c_str());
115 lua_setfield(L, -2, "inventory");
118 // from_table(self, table)
119 bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
122 if (!MetaDataRef::handleFromTable(L, table, _meta))
125 NodeMetadata *meta = (NodeMetadata*) _meta;
128 Inventory *inv = meta->getInventory();
129 lua_getfield(L, table, "inventory");
130 if (lua_istable(L, -1)) {
131 int inventorytable = lua_gettop(L);
133 while (lua_next(L, inventorytable) != 0) {
134 // key at index -2 and value at index -1
135 std::string name = lua_tostring(L, -2);
136 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
137 lua_pop(L, 1); // Remove value, keep key for next iteration
146 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
153 NodeMetaRef::NodeMetaRef(Metadata *meta):
159 NodeMetaRef::~NodeMetaRef()
163 // Creates an NodeMetaRef and leaves it on top of stack
164 // Not callable from Lua; all references are created on the C side.
165 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
167 NodeMetaRef *o = new NodeMetaRef(p, env);
168 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
169 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
170 luaL_getmetatable(L, className);
171 lua_setmetatable(L, -2);
174 // Client-sided version of the above
175 void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
177 NodeMetaRef *o = new NodeMetaRef(meta);
178 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
179 luaL_getmetatable(L, className);
180 lua_setmetatable(L, -2);
183 const char NodeMetaRef::className[] = "NodeMetaRef";
184 void NodeMetaRef::RegisterCommon(lua_State *L)
187 int methodtable = lua_gettop(L);
188 luaL_newmetatable(L, className);
189 int metatable = lua_gettop(L);
191 lua_pushliteral(L, "__metatable");
192 lua_pushvalue(L, methodtable);
193 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
195 lua_pushliteral(L, "metadata_class");
196 lua_pushlstring(L, className, strlen(className));
197 lua_settable(L, metatable);
199 lua_pushliteral(L, "__index");
200 lua_pushvalue(L, methodtable);
201 lua_settable(L, metatable);
203 lua_pushliteral(L, "__gc");
204 lua_pushcfunction(L, gc_object);
205 lua_settable(L, metatable);
207 lua_pop(L, 1); // drop metatable
210 void NodeMetaRef::Register(lua_State *L)
213 luaL_openlib(L, 0, methodsServer, 0); // fill methodtable
214 lua_pop(L, 1); // drop methodtable
218 const luaL_reg NodeMetaRef::methodsServer[] = {
219 luamethod(MetaDataRef, get_string),
220 luamethod(MetaDataRef, set_string),
221 luamethod(MetaDataRef, get_int),
222 luamethod(MetaDataRef, set_int),
223 luamethod(MetaDataRef, get_float),
224 luamethod(MetaDataRef, set_float),
225 luamethod(MetaDataRef, to_table),
226 luamethod(MetaDataRef, from_table),
227 luamethod(NodeMetaRef, get_inventory),
232 void NodeMetaRef::RegisterClient(lua_State *L)
235 luaL_openlib(L, 0, methodsClient, 0); // fill methodtable
236 lua_pop(L, 1); // drop methodtable
240 const luaL_reg NodeMetaRef::methodsClient[] = {
241 luamethod(MetaDataRef, get_string),
242 luamethod(MetaDataRef, get_int),
243 luamethod(MetaDataRef, get_float),
244 luamethod(MetaDataRef, to_table),