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 "cpp_api/scriptapi.h"
21 #include "common/c_converter.h"
22 #include "common/c_content.h"
24 #include "lua_api/l_nodemeta.h"
25 #include "common/c_internal.h"
26 #include "lua_api/l_inventory.h"
32 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
34 luaL_checktype(L, narg, LUA_TUSERDATA);
35 void *ud = luaL_checkudata(L, narg, className);
36 if(!ud) luaL_typerror(L, narg, className);
37 return *(NodeMetaRef**)ud; // unbox pointer
40 NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create)
42 NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
43 if(meta == NULL && auto_create)
45 meta = new NodeMetadata(ref->m_env->getGameDef());
46 ref->m_env->getMap().setNodeMetadata(ref->m_p, meta);
51 void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
53 // NOTE: This same code is in rollback_interface.cpp
54 // Inform other things that the metadata has changed
55 v3s16 blockpos = getNodeBlockPos(ref->m_p);
57 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
59 ref->m_env->getMap().dispatchEvent(&event);
60 // Set the block to be saved
61 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
63 block->raiseModified(MOD_STATE_WRITE_NEEDED,
64 "NodeMetaRef::reportMetadataChange");
70 int NodeMetaRef::gc_object(lua_State *L) {
71 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
76 // get_string(self, name)
77 int NodeMetaRef::l_get_string(lua_State *L)
79 NodeMetaRef *ref = checkobject(L, 1);
80 std::string name = luaL_checkstring(L, 2);
82 NodeMetadata *meta = getmeta(ref, false);
84 lua_pushlstring(L, "", 0);
87 std::string str = meta->getString(name);
88 lua_pushlstring(L, str.c_str(), str.size());
92 // set_string(self, name, var)
93 int NodeMetaRef::l_set_string(lua_State *L)
95 NodeMetaRef *ref = checkobject(L, 1);
96 std::string name = luaL_checkstring(L, 2);
98 const char *s = lua_tolstring(L, 3, &len);
99 std::string str(s, len);
101 NodeMetadata *meta = getmeta(ref, !str.empty());
102 if(meta == NULL || str == meta->getString(name))
104 meta->setString(name, str);
105 reportMetadataChange(ref);
109 // get_int(self, name)
110 int NodeMetaRef::l_get_int(lua_State *L)
112 NodeMetaRef *ref = checkobject(L, 1);
113 std::string name = lua_tostring(L, 2);
115 NodeMetadata *meta = getmeta(ref, false);
117 lua_pushnumber(L, 0);
120 std::string str = meta->getString(name);
121 lua_pushnumber(L, stoi(str));
125 // set_int(self, name, var)
126 int NodeMetaRef::l_set_int(lua_State *L)
128 NodeMetaRef *ref = checkobject(L, 1);
129 std::string name = lua_tostring(L, 2);
130 int a = lua_tointeger(L, 3);
131 std::string str = itos(a);
133 NodeMetadata *meta = getmeta(ref, true);
134 if(meta == NULL || str == meta->getString(name))
136 meta->setString(name, str);
137 reportMetadataChange(ref);
141 // get_float(self, name)
142 int NodeMetaRef::l_get_float(lua_State *L)
144 NodeMetaRef *ref = checkobject(L, 1);
145 std::string name = lua_tostring(L, 2);
147 NodeMetadata *meta = getmeta(ref, false);
149 lua_pushnumber(L, 0);
152 std::string str = meta->getString(name);
153 lua_pushnumber(L, stof(str));
157 // set_float(self, name, var)
158 int NodeMetaRef::l_set_float(lua_State *L)
160 NodeMetaRef *ref = checkobject(L, 1);
161 std::string name = lua_tostring(L, 2);
162 float a = lua_tonumber(L, 3);
163 std::string str = ftos(a);
165 NodeMetadata *meta = getmeta(ref, true);
166 if(meta == NULL || str == meta->getString(name))
168 meta->setString(name, str);
169 reportMetadataChange(ref);
173 // get_inventory(self)
174 int NodeMetaRef::l_get_inventory(lua_State *L)
176 NodeMetaRef *ref = checkobject(L, 1);
177 getmeta(ref, true); // try to ensure the metadata exists
178 InvRef::createNodeMeta(L, ref->m_p);
183 int NodeMetaRef::l_to_table(lua_State *L)
185 NodeMetaRef *ref = checkobject(L, 1);
187 NodeMetadata *meta = getmeta(ref, true);
196 std::map<std::string, std::string> fields = meta->getStrings();
197 for(std::map<std::string, std::string>::const_iterator
198 i = fields.begin(); i != fields.end(); i++){
199 const std::string &name = i->first;
200 const std::string &value = i->second;
201 lua_pushlstring(L, name.c_str(), name.size());
202 lua_pushlstring(L, value.c_str(), value.size());
206 lua_setfield(L, -2, "fields");
209 Inventory *inv = meta->getInventory();
211 std::vector<const InventoryList*> lists = inv->getLists();
212 for(std::vector<const InventoryList*>::const_iterator
213 i = lists.begin(); i != lists.end(); i++){
214 push_inventory_list(inv, (*i)->getName().c_str(), L);
215 lua_setfield(L, -2, (*i)->getName().c_str());
218 lua_setfield(L, -2, "inventory");
222 // from_table(self, table)
223 int NodeMetaRef::l_from_table(lua_State *L)
225 NodeMetaRef *ref = checkobject(L, 1);
228 if(lua_isnil(L, base)){
230 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
231 lua_pushboolean(L, true);
235 // Has metadata; clear old one first
236 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
237 // Create new metadata
238 NodeMetadata *meta = getmeta(ref, true);
240 lua_getfield(L, base, "fields");
241 int fieldstable = lua_gettop(L);
243 while(lua_next(L, fieldstable) != 0){
244 // key at index -2 and value at index -1
245 std::string name = lua_tostring(L, -2);
247 const char *cs = lua_tolstring(L, -1, &cl);
248 std::string value(cs, cl);
249 meta->setString(name, value);
250 lua_pop(L, 1); // removes value, keeps key for next iteration
253 Inventory *inv = meta->getInventory();
254 lua_getfield(L, base, "inventory");
255 int inventorytable = lua_gettop(L);
257 while(lua_next(L, inventorytable) != 0){
258 // key at index -2 and value at index -1
259 std::string name = lua_tostring(L, -2);
260 read_inventory_list(inv, name.c_str(), L, -1,STACK_TO_SERVER(L));
261 lua_pop(L, 1); // removes value, keeps key for next iteration
263 reportMetadataChange(ref);
264 lua_pushboolean(L, true);
269 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
275 NodeMetaRef::~NodeMetaRef()
279 // Creates an NodeMetaRef and leaves it on top of stack
280 // Not callable from Lua; all references are created on the C side.
281 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
283 NodeMetaRef *o = new NodeMetaRef(p, env);
284 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
285 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
286 luaL_getmetatable(L, className);
287 lua_setmetatable(L, -2);
290 void NodeMetaRef::Register(lua_State *L)
293 int methodtable = lua_gettop(L);
294 luaL_newmetatable(L, className);
295 int metatable = lua_gettop(L);
297 lua_pushliteral(L, "__metatable");
298 lua_pushvalue(L, methodtable);
299 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
301 lua_pushliteral(L, "__index");
302 lua_pushvalue(L, methodtable);
303 lua_settable(L, metatable);
305 lua_pushliteral(L, "__gc");
306 lua_pushcfunction(L, gc_object);
307 lua_settable(L, metatable);
309 lua_pop(L, 1); // drop metatable
311 luaL_openlib(L, 0, methods, 0); // fill methodtable
312 lua_pop(L, 1); // drop methodtable
314 // Cannot be created from Lua
315 //lua_register(L, className, create_object);
318 const char NodeMetaRef::className[] = "NodeMetaRef";
319 const luaL_reg NodeMetaRef::methods[] = {
320 luamethod(NodeMetaRef, get_string),
321 luamethod(NodeMetaRef, set_string),
322 luamethod(NodeMetaRef, get_int),
323 luamethod(NodeMetaRef, set_int),
324 luamethod(NodeMetaRef, get_float),
325 luamethod(NodeMetaRef, set_float),
326 luamethod(NodeMetaRef, get_inventory),
327 luamethod(NodeMetaRef, to_table),
328 luamethod(NodeMetaRef, from_table),
332 REGISTER_LUA_REF(NodeMetaRef);