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)
47 meta = new NodeMetadata(ref->m_env->getGameDef());
48 ref->m_env->getMap().setNodeMetadata(ref->m_p, meta);
53 void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
55 // NOTE: This same code is in rollback_interface.cpp
56 // Inform other things that the metadata has changed
57 v3s16 blockpos = getNodeBlockPos(ref->m_p);
59 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
61 ref->m_env->getMap().dispatchEvent(&event);
62 // Set the block to be saved
63 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
65 block->raiseModified(MOD_STATE_WRITE_NEEDED,
66 "NodeMetaRef::reportMetadataChange");
72 int NodeMetaRef::gc_object(lua_State *L) {
73 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
78 // get_string(self, name)
79 int NodeMetaRef::l_get_string(lua_State *L)
81 NodeMetaRef *ref = checkobject(L, 1);
82 std::string name = luaL_checkstring(L, 2);
84 NodeMetadata *meta = getmeta(ref, false);
86 lua_pushlstring(L, "", 0);
89 std::string str = meta->getString(name);
90 lua_pushlstring(L, str.c_str(), str.size());
94 // set_string(self, name, var)
95 int NodeMetaRef::l_set_string(lua_State *L)
97 NodeMetaRef *ref = checkobject(L, 1);
98 std::string name = luaL_checkstring(L, 2);
100 const char *s = lua_tolstring(L, 3, &len);
101 std::string str(s, len);
103 NodeMetadata *meta = getmeta(ref, !str.empty());
104 if(meta == NULL || str == meta->getString(name))
106 meta->setString(name, str);
107 reportMetadataChange(ref);
111 // get_int(self, name)
112 int NodeMetaRef::l_get_int(lua_State *L)
114 NodeMetaRef *ref = checkobject(L, 1);
115 std::string name = lua_tostring(L, 2);
117 NodeMetadata *meta = getmeta(ref, false);
119 lua_pushnumber(L, 0);
122 std::string str = meta->getString(name);
123 lua_pushnumber(L, stoi(str));
127 // set_int(self, name, var)
128 int NodeMetaRef::l_set_int(lua_State *L)
130 NodeMetaRef *ref = checkobject(L, 1);
131 std::string name = lua_tostring(L, 2);
132 int a = lua_tointeger(L, 3);
133 std::string str = itos(a);
135 NodeMetadata *meta = getmeta(ref, true);
136 if(meta == NULL || str == meta->getString(name))
138 meta->setString(name, str);
139 reportMetadataChange(ref);
143 // get_float(self, name)
144 int NodeMetaRef::l_get_float(lua_State *L)
146 NodeMetaRef *ref = checkobject(L, 1);
147 std::string name = lua_tostring(L, 2);
149 NodeMetadata *meta = getmeta(ref, false);
151 lua_pushnumber(L, 0);
154 std::string str = meta->getString(name);
155 lua_pushnumber(L, stof(str));
159 // set_float(self, name, var)
160 int NodeMetaRef::l_set_float(lua_State *L)
162 NodeMetaRef *ref = checkobject(L, 1);
163 std::string name = lua_tostring(L, 2);
164 float a = lua_tonumber(L, 3);
165 std::string str = ftos(a);
167 NodeMetadata *meta = getmeta(ref, true);
168 if(meta == NULL || str == meta->getString(name))
170 meta->setString(name, str);
171 reportMetadataChange(ref);
175 // get_inventory(self)
176 int NodeMetaRef::l_get_inventory(lua_State *L)
178 NodeMetaRef *ref = checkobject(L, 1);
179 getmeta(ref, true); // try to ensure the metadata exists
180 InvRef::createNodeMeta(L, ref->m_p);
185 int NodeMetaRef::l_to_table(lua_State *L)
187 NodeMetaRef *ref = checkobject(L, 1);
189 NodeMetadata *meta = getmeta(ref, true);
198 std::map<std::string, std::string> fields = meta->getStrings();
199 for(std::map<std::string, std::string>::const_iterator
200 i = fields.begin(); i != fields.end(); i++){
201 const std::string &name = i->first;
202 const std::string &value = i->second;
203 lua_pushlstring(L, name.c_str(), name.size());
204 lua_pushlstring(L, value.c_str(), value.size());
208 lua_setfield(L, -2, "fields");
211 Inventory *inv = meta->getInventory();
213 std::vector<const InventoryList*> lists = inv->getLists();
214 for(std::vector<const InventoryList*>::const_iterator
215 i = lists.begin(); i != lists.end(); i++){
216 push_inventory_list(L, inv, (*i)->getName().c_str());
217 lua_setfield(L, -2, (*i)->getName().c_str());
220 lua_setfield(L, -2, "inventory");
224 // from_table(self, table)
225 int NodeMetaRef::l_from_table(lua_State *L)
227 NodeMetaRef *ref = checkobject(L, 1);
230 if(lua_isnil(L, base)){
232 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
233 lua_pushboolean(L, true);
237 // Has metadata; clear old one first
238 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
239 // Create new metadata
240 NodeMetadata *meta = getmeta(ref, true);
242 lua_getfield(L, base, "fields");
243 int fieldstable = lua_gettop(L);
245 while(lua_next(L, fieldstable) != 0){
246 // key at index -2 and value at index -1
247 std::string name = lua_tostring(L, -2);
249 const char *cs = lua_tolstring(L, -1, &cl);
250 std::string value(cs, cl);
251 meta->setString(name, value);
252 lua_pop(L, 1); // removes value, keeps key for next iteration
255 Inventory *inv = meta->getInventory();
256 lua_getfield(L, base, "inventory");
257 int inventorytable = lua_gettop(L);
259 while(lua_next(L, inventorytable) != 0){
260 // key at index -2 and value at index -1
261 std::string name = lua_tostring(L, -2);
262 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
263 lua_pop(L, 1); // removes value, keeps key for next iteration
265 reportMetadataChange(ref);
266 lua_pushboolean(L, true);
271 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
277 NodeMetaRef::~NodeMetaRef()
281 // Creates an NodeMetaRef and leaves it on top of stack
282 // Not callable from Lua; all references are created on the C side.
283 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
285 NodeMetaRef *o = new NodeMetaRef(p, env);
286 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
287 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
288 luaL_getmetatable(L, className);
289 lua_setmetatable(L, -2);
292 void NodeMetaRef::Register(lua_State *L)
295 int methodtable = lua_gettop(L);
296 luaL_newmetatable(L, className);
297 int metatable = lua_gettop(L);
299 lua_pushliteral(L, "__metatable");
300 lua_pushvalue(L, methodtable);
301 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
303 lua_pushliteral(L, "__index");
304 lua_pushvalue(L, methodtable);
305 lua_settable(L, metatable);
307 lua_pushliteral(L, "__gc");
308 lua_pushcfunction(L, gc_object);
309 lua_settable(L, metatable);
311 lua_pop(L, 1); // drop metatable
313 luaL_openlib(L, 0, methods, 0); // fill methodtable
314 lua_pop(L, 1); // drop methodtable
316 // Cannot be created from Lua
317 //lua_register(L, className, create_object);
320 const char NodeMetaRef::className[] = "NodeMetaRef";
321 const luaL_reg NodeMetaRef::methods[] = {
322 luamethod(NodeMetaRef, get_string),
323 luamethod(NodeMetaRef, set_string),
324 luamethod(NodeMetaRef, get_int),
325 luamethod(NodeMetaRef, set_int),
326 luamethod(NodeMetaRef, get_float),
327 luamethod(NodeMetaRef, set_float),
328 luamethod(NodeMetaRef, get_inventory),
329 luamethod(NodeMetaRef, to_table),
330 luamethod(NodeMetaRef, from_table),