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"
28 #include "nodemetadata.h"
35 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
37 luaL_checktype(L, narg, LUA_TUSERDATA);
38 void *ud = luaL_checkudata(L, narg, className);
39 if(!ud) luaL_typerror(L, narg, className);
40 return *(NodeMetaRef**)ud; // unbox pointer
43 NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create)
45 NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
46 if(meta == NULL && auto_create) {
47 meta = new NodeMetadata(ref->m_env->getGameDef()->idef());
48 if(!ref->m_env->getMap().setNodeMetadata(ref->m_p, meta)) {
56 void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
58 // NOTE: This same code is in rollback_interface.cpp
59 // Inform other things that the metadata has changed
60 v3s16 blockpos = getNodeBlockPos(ref->m_p);
62 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
64 ref->m_env->getMap().dispatchEvent(&event);
65 // Set the block to be saved
66 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
68 block->raiseModified(MOD_STATE_WRITE_NEEDED,
69 MOD_REASON_REPORT_META_CHANGE);
76 int NodeMetaRef::gc_object(lua_State *L) {
77 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
82 // get_string(self, name)
83 int NodeMetaRef::l_get_string(lua_State *L)
87 NodeMetaRef *ref = checkobject(L, 1);
88 std::string name = luaL_checkstring(L, 2);
90 NodeMetadata *meta = getmeta(ref, false);
92 lua_pushlstring(L, "", 0);
95 std::string str = meta->getString(name);
96 lua_pushlstring(L, str.c_str(), str.size());
100 // set_string(self, name, var)
101 int NodeMetaRef::l_set_string(lua_State *L)
105 NodeMetaRef *ref = checkobject(L, 1);
106 std::string name = luaL_checkstring(L, 2);
108 const char *s = lua_tolstring(L, 3, &len);
109 std::string str(s, len);
111 NodeMetadata *meta = getmeta(ref, !str.empty());
112 if(meta == NULL || str == meta->getString(name))
114 meta->setString(name, str);
115 reportMetadataChange(ref);
119 // get_int(self, name)
120 int NodeMetaRef::l_get_int(lua_State *L)
124 NodeMetaRef *ref = checkobject(L, 1);
125 std::string name = lua_tostring(L, 2);
127 NodeMetadata *meta = getmeta(ref, false);
129 lua_pushnumber(L, 0);
132 std::string str = meta->getString(name);
133 lua_pushnumber(L, stoi(str));
137 // set_int(self, name, var)
138 int NodeMetaRef::l_set_int(lua_State *L)
142 NodeMetaRef *ref = checkobject(L, 1);
143 std::string name = lua_tostring(L, 2);
144 int a = lua_tointeger(L, 3);
145 std::string str = itos(a);
147 NodeMetadata *meta = getmeta(ref, true);
148 if(meta == NULL || str == meta->getString(name))
150 meta->setString(name, str);
151 reportMetadataChange(ref);
155 // get_float(self, name)
156 int NodeMetaRef::l_get_float(lua_State *L)
160 NodeMetaRef *ref = checkobject(L, 1);
161 std::string name = lua_tostring(L, 2);
163 NodeMetadata *meta = getmeta(ref, false);
165 lua_pushnumber(L, 0);
168 std::string str = meta->getString(name);
169 lua_pushnumber(L, stof(str));
173 // set_float(self, name, var)
174 int NodeMetaRef::l_set_float(lua_State *L)
178 NodeMetaRef *ref = checkobject(L, 1);
179 std::string name = lua_tostring(L, 2);
180 float a = lua_tonumber(L, 3);
181 std::string str = ftos(a);
183 NodeMetadata *meta = getmeta(ref, true);
184 if(meta == NULL || str == meta->getString(name))
186 meta->setString(name, str);
187 reportMetadataChange(ref);
191 // get_inventory(self)
192 int NodeMetaRef::l_get_inventory(lua_State *L)
196 NodeMetaRef *ref = checkobject(L, 1);
197 getmeta(ref, true); // try to ensure the metadata exists
198 InvRef::createNodeMeta(L, ref->m_p);
203 int NodeMetaRef::l_to_table(lua_State *L)
207 NodeMetaRef *ref = checkobject(L, 1);
209 NodeMetadata *meta = getmeta(ref, true);
219 StringMap fields = meta->getStrings();
220 for (StringMap::const_iterator
221 it = fields.begin(); it != fields.end(); ++it) {
222 const std::string &name = it->first;
223 const std::string &value = it->second;
224 lua_pushlstring(L, name.c_str(), name.size());
225 lua_pushlstring(L, value.c_str(), value.size());
229 lua_setfield(L, -2, "fields");
233 Inventory *inv = meta->getInventory();
235 std::vector<const InventoryList *> lists = inv->getLists();
236 for(std::vector<const InventoryList *>::const_iterator
237 i = lists.begin(); i != lists.end(); i++) {
238 push_inventory_list(L, inv, (*i)->getName().c_str());
239 lua_setfield(L, -2, (*i)->getName().c_str());
242 lua_setfield(L, -2, "inventory");
246 // from_table(self, table)
247 int NodeMetaRef::l_from_table(lua_State *L)
251 NodeMetaRef *ref = checkobject(L, 1);
254 // clear old metadata first
255 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
257 if(lua_isnil(L, base)){
259 lua_pushboolean(L, true);
263 // Create new metadata
264 NodeMetadata *meta = getmeta(ref, true);
266 lua_pushboolean(L, false);
270 lua_getfield(L, base, "fields");
271 int fieldstable = lua_gettop(L);
273 while(lua_next(L, fieldstable) != 0){
274 // key at index -2 and value at index -1
275 std::string name = lua_tostring(L, -2);
277 const char *cs = lua_tolstring(L, -1, &cl);
278 std::string value(cs, cl);
279 meta->setString(name, value);
280 lua_pop(L, 1); // removes value, keeps key for next iteration
283 Inventory *inv = meta->getInventory();
284 lua_getfield(L, base, "inventory");
285 int inventorytable = lua_gettop(L);
287 while(lua_next(L, inventorytable) != 0){
288 // key at index -2 and value at index -1
289 std::string name = lua_tostring(L, -2);
290 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
291 lua_pop(L, 1); // removes value, keeps key for next iteration
293 reportMetadataChange(ref);
294 lua_pushboolean(L, true);
299 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
305 NodeMetaRef::~NodeMetaRef()
309 // Creates an NodeMetaRef and leaves it on top of stack
310 // Not callable from Lua; all references are created on the C side.
311 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
313 NodeMetaRef *o = new NodeMetaRef(p, env);
314 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
315 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
316 luaL_getmetatable(L, className);
317 lua_setmetatable(L, -2);
320 void NodeMetaRef::Register(lua_State *L)
323 int methodtable = lua_gettop(L);
324 luaL_newmetatable(L, className);
325 int metatable = lua_gettop(L);
327 lua_pushliteral(L, "__metatable");
328 lua_pushvalue(L, methodtable);
329 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
331 lua_pushliteral(L, "__index");
332 lua_pushvalue(L, methodtable);
333 lua_settable(L, metatable);
335 lua_pushliteral(L, "__gc");
336 lua_pushcfunction(L, gc_object);
337 lua_settable(L, metatable);
339 lua_pop(L, 1); // drop metatable
341 luaL_openlib(L, 0, methods, 0); // fill methodtable
342 lua_pop(L, 1); // drop methodtable
344 // Cannot be created from Lua
345 //lua_register(L, className, create_object);
348 const char NodeMetaRef::className[] = "NodeMetaRef";
349 const luaL_reg NodeMetaRef::methods[] = {
350 luamethod(NodeMetaRef, get_string),
351 luamethod(NodeMetaRef, set_string),
352 luamethod(NodeMetaRef, get_int),
353 luamethod(NodeMetaRef, set_int),
354 luamethod(NodeMetaRef, get_float),
355 luamethod(NodeMetaRef, set_float),
356 luamethod(NodeMetaRef, get_inventory),
357 luamethod(NodeMetaRef, to_table),
358 luamethod(NodeMetaRef, from_table),