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_converter.h"
24 #include "common/c_content.h"
25 #include "environment.h"
27 #include "nodemetadata.h"
34 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
36 luaL_checktype(L, narg, LUA_TUSERDATA);
37 void *ud = luaL_checkudata(L, narg, className);
38 if(!ud) luaL_typerror(L, narg, className);
39 return *(NodeMetaRef**)ud; // unbox pointer
42 NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create)
44 NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
45 if(meta == NULL && auto_create) {
46 meta = new NodeMetadata(ref->m_env->getGameDef());
47 if(!ref->m_env->getMap().setNodeMetadata(ref->m_p, meta)) {
55 void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
57 // NOTE: This same code is in rollback_interface.cpp
58 // Inform other things that the metadata has changed
59 v3s16 blockpos = getNodeBlockPos(ref->m_p);
61 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
63 ref->m_env->getMap().dispatchEvent(&event);
64 // Set the block to be saved
65 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
67 block->raiseModified(MOD_STATE_WRITE_NEEDED,
68 MOD_REASON_REPORT_META_CHANGE);
75 int NodeMetaRef::gc_object(lua_State *L) {
76 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
81 // get_string(self, name)
82 int NodeMetaRef::l_get_string(lua_State *L)
84 NodeMetaRef *ref = checkobject(L, 1);
85 std::string name = luaL_checkstring(L, 2);
87 NodeMetadata *meta = getmeta(ref, false);
89 lua_pushlstring(L, "", 0);
92 std::string str = meta->getString(name);
93 lua_pushlstring(L, str.c_str(), str.size());
97 // set_string(self, name, var)
98 int NodeMetaRef::l_set_string(lua_State *L)
100 NodeMetaRef *ref = checkobject(L, 1);
101 std::string name = luaL_checkstring(L, 2);
103 const char *s = lua_tolstring(L, 3, &len);
104 std::string str(s, len);
106 NodeMetadata *meta = getmeta(ref, !str.empty());
107 if(meta == NULL || str == meta->getString(name))
109 meta->setString(name, str);
110 reportMetadataChange(ref);
114 // get_int(self, name)
115 int NodeMetaRef::l_get_int(lua_State *L)
117 NodeMetaRef *ref = checkobject(L, 1);
118 std::string name = lua_tostring(L, 2);
120 NodeMetadata *meta = getmeta(ref, false);
122 lua_pushnumber(L, 0);
125 std::string str = meta->getString(name);
126 lua_pushnumber(L, stoi(str));
130 // set_int(self, name, var)
131 int NodeMetaRef::l_set_int(lua_State *L)
133 NodeMetaRef *ref = checkobject(L, 1);
134 std::string name = lua_tostring(L, 2);
135 int a = lua_tointeger(L, 3);
136 std::string str = itos(a);
138 NodeMetadata *meta = getmeta(ref, true);
139 if(meta == NULL || str == meta->getString(name))
141 meta->setString(name, str);
142 reportMetadataChange(ref);
146 // get_float(self, name)
147 int NodeMetaRef::l_get_float(lua_State *L)
149 NodeMetaRef *ref = checkobject(L, 1);
150 std::string name = lua_tostring(L, 2);
152 NodeMetadata *meta = getmeta(ref, false);
154 lua_pushnumber(L, 0);
157 std::string str = meta->getString(name);
158 lua_pushnumber(L, stof(str));
162 // set_float(self, name, var)
163 int NodeMetaRef::l_set_float(lua_State *L)
165 NodeMetaRef *ref = checkobject(L, 1);
166 std::string name = lua_tostring(L, 2);
167 float a = lua_tonumber(L, 3);
168 std::string str = ftos(a);
170 NodeMetadata *meta = getmeta(ref, true);
171 if(meta == NULL || str == meta->getString(name))
173 meta->setString(name, str);
174 reportMetadataChange(ref);
178 // get_inventory(self)
179 int NodeMetaRef::l_get_inventory(lua_State *L)
181 NodeMetaRef *ref = checkobject(L, 1);
182 getmeta(ref, true); // try to ensure the metadata exists
183 InvRef::createNodeMeta(L, ref->m_p);
188 int NodeMetaRef::l_to_table(lua_State *L)
190 NodeMetaRef *ref = checkobject(L, 1);
192 NodeMetadata *meta = getmeta(ref, true);
202 StringMap fields = meta->getStrings();
203 for (StringMap::const_iterator
204 it = fields.begin(); it != fields.end(); ++it) {
205 const std::string &name = it->first;
206 const std::string &value = it->second;
207 lua_pushlstring(L, name.c_str(), name.size());
208 lua_pushlstring(L, value.c_str(), value.size());
212 lua_setfield(L, -2, "fields");
216 Inventory *inv = meta->getInventory();
218 std::vector<const InventoryList *> lists = inv->getLists();
219 for(std::vector<const InventoryList *>::const_iterator
220 i = lists.begin(); i != lists.end(); i++) {
221 push_inventory_list(L, inv, (*i)->getName().c_str());
222 lua_setfield(L, -2, (*i)->getName().c_str());
225 lua_setfield(L, -2, "inventory");
229 // from_table(self, table)
230 int NodeMetaRef::l_from_table(lua_State *L)
232 NodeMetaRef *ref = checkobject(L, 1);
235 // clear old metadata first
236 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
238 if(lua_isnil(L, base)){
240 lua_pushboolean(L, true);
244 // Create new metadata
245 NodeMetadata *meta = getmeta(ref, true);
247 lua_pushboolean(L, false);
251 lua_getfield(L, base, "fields");
252 int fieldstable = lua_gettop(L);
254 while(lua_next(L, fieldstable) != 0){
255 // key at index -2 and value at index -1
256 std::string name = lua_tostring(L, -2);
258 const char *cs = lua_tolstring(L, -1, &cl);
259 std::string value(cs, cl);
260 meta->setString(name, value);
261 lua_pop(L, 1); // removes value, keeps key for next iteration
264 Inventory *inv = meta->getInventory();
265 lua_getfield(L, base, "inventory");
266 int inventorytable = lua_gettop(L);
268 while(lua_next(L, inventorytable) != 0){
269 // key at index -2 and value at index -1
270 std::string name = lua_tostring(L, -2);
271 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
272 lua_pop(L, 1); // removes value, keeps key for next iteration
274 reportMetadataChange(ref);
275 lua_pushboolean(L, true);
280 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
286 NodeMetaRef::~NodeMetaRef()
290 // Creates an NodeMetaRef and leaves it on top of stack
291 // Not callable from Lua; all references are created on the C side.
292 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
294 NodeMetaRef *o = new NodeMetaRef(p, env);
295 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
296 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
297 luaL_getmetatable(L, className);
298 lua_setmetatable(L, -2);
301 void NodeMetaRef::Register(lua_State *L)
304 int methodtable = lua_gettop(L);
305 luaL_newmetatable(L, className);
306 int metatable = lua_gettop(L);
308 lua_pushliteral(L, "__metatable");
309 lua_pushvalue(L, methodtable);
310 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
312 lua_pushliteral(L, "__index");
313 lua_pushvalue(L, methodtable);
314 lua_settable(L, metatable);
316 lua_pushliteral(L, "__gc");
317 lua_pushcfunction(L, gc_object);
318 lua_settable(L, metatable);
320 lua_pop(L, 1); // drop metatable
322 luaL_openlib(L, 0, methods, 0); // fill methodtable
323 lua_pop(L, 1); // drop methodtable
325 // Cannot be created from Lua
326 //lua_register(L, className, create_object);
329 const char NodeMetaRef::className[] = "NodeMetaRef";
330 const luaL_reg NodeMetaRef::methods[] = {
331 luamethod(NodeMetaRef, get_string),
332 luamethod(NodeMetaRef, set_string),
333 luamethod(NodeMetaRef, get_int),
334 luamethod(NodeMetaRef, set_int),
335 luamethod(NodeMetaRef, get_float),
336 luamethod(NodeMetaRef, set_float),
337 luamethod(NodeMetaRef, get_inventory),
338 luamethod(NodeMetaRef, to_table),
339 luamethod(NodeMetaRef, from_table),