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_content.h"
24 #include "serverenvironment.h"
31 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
33 luaL_checktype(L, narg, LUA_TUSERDATA);
34 void *ud = luaL_checkudata(L, narg, className);
35 if(!ud) luaL_typerror(L, narg, className);
36 return *(NodeMetaRef**)ud; // unbox pointer
39 NodeMetadata* NodeMetaRef::getmeta(bool auto_create)
41 NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p);
42 if (meta == NULL && auto_create) {
43 meta = new NodeMetadata(m_env->getGameDef()->idef());
44 if (!m_env->getMap().setNodeMetadata(m_p, meta)) {
52 void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
54 // NOTE: This same code is in rollback_interface.cpp
55 // Inform other things that the metadata has changed
56 v3s16 blockpos = getNodeBlockPos(ref->m_p);
58 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
60 ref->m_env->getMap().dispatchEvent(&event);
61 // Set the block to be saved
62 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
64 block->raiseModified(MOD_STATE_WRITE_NEEDED,
65 MOD_REASON_REPORT_META_CHANGE);
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)
83 NodeMetaRef *ref = checkobject(L, 1);
84 std::string name = luaL_checkstring(L, 2);
86 NodeMetadata *meta = ref->getmeta(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)
101 NodeMetaRef *ref = checkobject(L, 1);
102 std::string name = luaL_checkstring(L, 2);
104 const char *s = lua_tolstring(L, 3, &len);
105 std::string str(s, len);
107 NodeMetadata *meta = ref->getmeta(!str.empty());
108 if(meta == NULL || str == meta->getString(name))
110 meta->setString(name, str);
111 reportMetadataChange(ref);
115 // get_int(self, name)
116 int NodeMetaRef::l_get_int(lua_State *L)
120 NodeMetaRef *ref = checkobject(L, 1);
121 std::string name = lua_tostring(L, 2);
123 NodeMetadata *meta = ref->getmeta(false);
125 lua_pushnumber(L, 0);
128 std::string str = meta->getString(name);
129 lua_pushnumber(L, stoi(str));
133 // set_int(self, name, var)
134 int NodeMetaRef::l_set_int(lua_State *L)
138 NodeMetaRef *ref = checkobject(L, 1);
139 std::string name = lua_tostring(L, 2);
140 int a = lua_tointeger(L, 3);
141 std::string str = itos(a);
143 NodeMetadata *meta = ref->getmeta(true);
144 if(meta == NULL || str == meta->getString(name))
146 meta->setString(name, str);
147 reportMetadataChange(ref);
151 // get_float(self, name)
152 int NodeMetaRef::l_get_float(lua_State *L)
156 NodeMetaRef *ref = checkobject(L, 1);
157 std::string name = lua_tostring(L, 2);
159 NodeMetadata *meta = ref->getmeta(false);
161 lua_pushnumber(L, 0);
164 std::string str = meta->getString(name);
165 lua_pushnumber(L, stof(str));
169 // set_float(self, name, var)
170 int NodeMetaRef::l_set_float(lua_State *L)
174 NodeMetaRef *ref = checkobject(L, 1);
175 std::string name = lua_tostring(L, 2);
176 float a = lua_tonumber(L, 3);
177 std::string str = ftos(a);
179 NodeMetadata *meta = ref->getmeta(true);
180 if(meta == NULL || str == meta->getString(name))
182 meta->setString(name, str);
183 reportMetadataChange(ref);
187 // get_inventory(self)
188 int NodeMetaRef::l_get_inventory(lua_State *L)
192 NodeMetaRef *ref = checkobject(L, 1);
193 ref->getmeta(true); // try to ensure the metadata exists
194 InvRef::createNodeMeta(L, ref->m_p);
199 int NodeMetaRef::l_to_table(lua_State *L)
203 NodeMetaRef *ref = checkobject(L, 1);
205 NodeMetadata *meta = ref->getmeta(true);
215 StringMap fields = meta->getStrings();
216 for (StringMap::const_iterator
217 it = fields.begin(); it != fields.end(); ++it) {
218 const std::string &name = it->first;
219 const std::string &value = it->second;
220 lua_pushlstring(L, name.c_str(), name.size());
221 lua_pushlstring(L, value.c_str(), value.size());
225 lua_setfield(L, -2, "fields");
229 Inventory *inv = meta->getInventory();
231 std::vector<const InventoryList *> lists = inv->getLists();
232 for(std::vector<const InventoryList *>::const_iterator
233 i = lists.begin(); i != lists.end(); i++) {
234 push_inventory_list(L, inv, (*i)->getName().c_str());
235 lua_setfield(L, -2, (*i)->getName().c_str());
238 lua_setfield(L, -2, "inventory");
242 // from_table(self, table)
243 int NodeMetaRef::l_from_table(lua_State *L)
247 NodeMetaRef *ref = checkobject(L, 1);
250 // clear old metadata first
251 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
253 if (!lua_istable(L, base)) {
255 lua_pushboolean(L, true);
259 // Create new metadata
260 NodeMetadata *meta = ref->getmeta(true);
262 lua_pushboolean(L, false);
267 lua_getfield(L, base, "fields");
268 if (lua_istable(L, -1)) {
269 int fieldstable = lua_gettop(L);
271 while (lua_next(L, fieldstable) != 0) {
272 // key at index -2 and value at index -1
273 std::string name = lua_tostring(L, -2);
275 const char *cs = lua_tolstring(L, -1, &cl);
276 meta->setString(name, std::string(cs, cl));
277 lua_pop(L, 1); // Remove value, keep key for next iteration
283 Inventory *inv = meta->getInventory();
284 lua_getfield(L, base, "inventory");
285 if (lua_istable(L, -1)) {
286 int inventorytable = lua_gettop(L);
288 while (lua_next(L, inventorytable) != 0) {
289 // key at index -2 and value at index -1
290 std::string name = lua_tostring(L, -2);
291 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
292 lua_pop(L, 1); // Remove value, keep key for next iteration
297 reportMetadataChange(ref);
298 lua_pushboolean(L, true);
303 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
309 NodeMetaRef::~NodeMetaRef()
313 // Creates an NodeMetaRef and leaves it on top of stack
314 // Not callable from Lua; all references are created on the C side.
315 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
317 NodeMetaRef *o = new NodeMetaRef(p, env);
318 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
319 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
320 luaL_getmetatable(L, className);
321 lua_setmetatable(L, -2);
324 void NodeMetaRef::Register(lua_State *L)
327 int methodtable = lua_gettop(L);
328 luaL_newmetatable(L, className);
329 int metatable = lua_gettop(L);
331 lua_pushliteral(L, "__metatable");
332 lua_pushvalue(L, methodtable);
333 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
335 lua_pushliteral(L, "__index");
336 lua_pushvalue(L, methodtable);
337 lua_settable(L, metatable);
339 lua_pushliteral(L, "__gc");
340 lua_pushcfunction(L, gc_object);
341 lua_settable(L, metatable);
343 lua_pop(L, 1); // drop metatable
345 luaL_openlib(L, 0, methods, 0); // fill methodtable
346 lua_pop(L, 1); // drop methodtable
348 // Cannot be created from Lua
349 //lua_register(L, className, create_object);
352 const char NodeMetaRef::className[] = "NodeMetaRef";
353 const luaL_reg NodeMetaRef::methods[] = {
354 luamethod(NodeMetaRef, get_string),
355 luamethod(NodeMetaRef, set_string),
356 luamethod(NodeMetaRef, get_int),
357 luamethod(NodeMetaRef, set_int),
358 luamethod(NodeMetaRef, get_float),
359 luamethod(NodeMetaRef, set_float),
360 luamethod(NodeMetaRef, get_inventory),
361 luamethod(NodeMetaRef, to_table),
362 luamethod(NodeMetaRef, from_table),