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 "NodeMetaRef::reportMetadataChange");
74 int NodeMetaRef::gc_object(lua_State *L) {
75 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
80 // get_string(self, name)
81 int NodeMetaRef::l_get_string(lua_State *L)
83 NodeMetaRef *ref = checkobject(L, 1);
84 std::string name = luaL_checkstring(L, 2);
86 NodeMetadata *meta = getmeta(ref, false);
88 lua_pushlstring(L, "", 0);
91 std::string str = meta->getString(name);
92 lua_pushlstring(L, str.c_str(), str.size());
96 // set_string(self, name, var)
97 int NodeMetaRef::l_set_string(lua_State *L)
99 NodeMetaRef *ref = checkobject(L, 1);
100 std::string name = luaL_checkstring(L, 2);
102 const char *s = lua_tolstring(L, 3, &len);
103 std::string str(s, len);
105 NodeMetadata *meta = getmeta(ref, !str.empty());
106 if(meta == NULL || str == meta->getString(name))
108 meta->setString(name, str);
109 reportMetadataChange(ref);
113 // get_int(self, name)
114 int NodeMetaRef::l_get_int(lua_State *L)
116 NodeMetaRef *ref = checkobject(L, 1);
117 std::string name = lua_tostring(L, 2);
119 NodeMetadata *meta = getmeta(ref, false);
121 lua_pushnumber(L, 0);
124 std::string str = meta->getString(name);
125 lua_pushnumber(L, stoi(str));
129 // set_int(self, name, var)
130 int NodeMetaRef::l_set_int(lua_State *L)
132 NodeMetaRef *ref = checkobject(L, 1);
133 std::string name = lua_tostring(L, 2);
134 int a = lua_tointeger(L, 3);
135 std::string str = itos(a);
137 NodeMetadata *meta = getmeta(ref, true);
138 if(meta == NULL || str == meta->getString(name))
140 meta->setString(name, str);
141 reportMetadataChange(ref);
145 // get_float(self, name)
146 int NodeMetaRef::l_get_float(lua_State *L)
148 NodeMetaRef *ref = checkobject(L, 1);
149 std::string name = lua_tostring(L, 2);
151 NodeMetadata *meta = getmeta(ref, false);
153 lua_pushnumber(L, 0);
156 std::string str = meta->getString(name);
157 lua_pushnumber(L, stof(str));
161 // set_float(self, name, var)
162 int NodeMetaRef::l_set_float(lua_State *L)
164 NodeMetaRef *ref = checkobject(L, 1);
165 std::string name = lua_tostring(L, 2);
166 float a = lua_tonumber(L, 3);
167 std::string str = ftos(a);
169 NodeMetadata *meta = getmeta(ref, true);
170 if(meta == NULL || str == meta->getString(name))
172 meta->setString(name, str);
173 reportMetadataChange(ref);
177 // get_inventory(self)
178 int NodeMetaRef::l_get_inventory(lua_State *L)
180 NodeMetaRef *ref = checkobject(L, 1);
181 getmeta(ref, true); // try to ensure the metadata exists
182 InvRef::createNodeMeta(L, ref->m_p);
187 int NodeMetaRef::l_to_table(lua_State *L)
189 NodeMetaRef *ref = checkobject(L, 1);
191 NodeMetadata *meta = getmeta(ref, true);
200 std::map<std::string, std::string> fields = meta->getStrings();
201 for(std::map<std::string, std::string>::const_iterator
202 i = fields.begin(); i != fields.end(); i++){
203 const std::string &name = i->first;
204 const std::string &value = i->second;
205 lua_pushlstring(L, name.c_str(), name.size());
206 lua_pushlstring(L, value.c_str(), value.size());
210 lua_setfield(L, -2, "fields");
213 Inventory *inv = meta->getInventory();
215 std::vector<const InventoryList*> lists = inv->getLists();
216 for(std::vector<const InventoryList*>::const_iterator
217 i = lists.begin(); i != lists.end(); i++){
218 push_inventory_list(L, inv, (*i)->getName().c_str());
219 lua_setfield(L, -2, (*i)->getName().c_str());
222 lua_setfield(L, -2, "inventory");
226 // from_table(self, table)
227 int NodeMetaRef::l_from_table(lua_State *L)
229 NodeMetaRef *ref = checkobject(L, 1);
232 // clear old metadata first
233 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
235 if(lua_isnil(L, base)){
237 lua_pushboolean(L, true);
241 // Create new metadata
242 NodeMetadata *meta = getmeta(ref, true);
244 lua_pushboolean(L, false);
248 lua_getfield(L, base, "fields");
249 int fieldstable = lua_gettop(L);
251 while(lua_next(L, fieldstable) != 0){
252 // key at index -2 and value at index -1
253 std::string name = lua_tostring(L, -2);
255 const char *cs = lua_tolstring(L, -1, &cl);
256 std::string value(cs, cl);
257 meta->setString(name, value);
258 lua_pop(L, 1); // removes value, keeps key for next iteration
261 Inventory *inv = meta->getInventory();
262 lua_getfield(L, base, "inventory");
263 int inventorytable = lua_gettop(L);
265 while(lua_next(L, inventorytable) != 0){
266 // key at index -2 and value at index -1
267 std::string name = lua_tostring(L, -2);
268 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
269 lua_pop(L, 1); // removes value, keeps key for next iteration
271 reportMetadataChange(ref);
272 lua_pushboolean(L, true);
277 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
283 NodeMetaRef::~NodeMetaRef()
287 // Creates an NodeMetaRef and leaves it on top of stack
288 // Not callable from Lua; all references are created on the C side.
289 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
291 NodeMetaRef *o = new NodeMetaRef(p, env);
292 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
293 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
294 luaL_getmetatable(L, className);
295 lua_setmetatable(L, -2);
298 void NodeMetaRef::Register(lua_State *L)
301 int methodtable = lua_gettop(L);
302 luaL_newmetatable(L, className);
303 int metatable = lua_gettop(L);
305 lua_pushliteral(L, "__metatable");
306 lua_pushvalue(L, methodtable);
307 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
309 lua_pushliteral(L, "__index");
310 lua_pushvalue(L, methodtable);
311 lua_settable(L, metatable);
313 lua_pushliteral(L, "__gc");
314 lua_pushcfunction(L, gc_object);
315 lua_settable(L, metatable);
317 lua_pop(L, 1); // drop metatable
319 luaL_openlib(L, 0, methods, 0); // fill methodtable
320 lua_pop(L, 1); // drop methodtable
322 // Cannot be created from Lua
323 //lua_register(L, className, create_object);
326 const char NodeMetaRef::className[] = "NodeMetaRef";
327 const luaL_reg NodeMetaRef::methods[] = {
328 luamethod(NodeMetaRef, get_string),
329 luamethod(NodeMetaRef, set_string),
330 luamethod(NodeMetaRef, get_int),
331 luamethod(NodeMetaRef, set_int),
332 luamethod(NodeMetaRef, get_float),
333 luamethod(NodeMetaRef, set_float),
334 luamethod(NodeMetaRef, get_inventory),
335 luamethod(NodeMetaRef, to_table),
336 luamethod(NodeMetaRef, from_table),