]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_nodemeta.cpp
Environment & IGameDef code refactoring (#4985)
[dragonfireclient.git] / src / script / lua_api / l_nodemeta.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
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"
25 #include "map.h"
26 #include "server.h"
27
28 /*
29         NodeMetaRef
30 */
31 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
32 {
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
37 }
38
39 NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create)
40 {
41         NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
42         if(meta == NULL && auto_create) {
43                 meta = new NodeMetadata(ref->m_env->getGameDef()->idef());
44                 if(!ref->m_env->getMap().setNodeMetadata(ref->m_p, meta)) {
45                         delete meta;
46                         return NULL;
47                 }
48         }
49         return meta;
50 }
51
52 void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
53 {
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);
57         MapEditEvent event;
58         event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
59         event.p = blockpos;
60         ref->m_env->getMap().dispatchEvent(&event);
61         // Set the block to be saved
62         MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
63         if (block) {
64                 block->raiseModified(MOD_STATE_WRITE_NEEDED,
65                         MOD_REASON_REPORT_META_CHANGE);
66         }
67 }
68
69 // Exported functions
70
71 // garbage collector
72 int NodeMetaRef::gc_object(lua_State *L) {
73         NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
74         delete o;
75         return 0;
76 }
77
78 // get_string(self, name)
79 int NodeMetaRef::l_get_string(lua_State *L)
80 {
81         MAP_LOCK_REQUIRED;
82
83         NodeMetaRef *ref = checkobject(L, 1);
84         std::string name = luaL_checkstring(L, 2);
85
86         NodeMetadata *meta = getmeta(ref, false);
87         if(meta == NULL){
88                 lua_pushlstring(L, "", 0);
89                 return 1;
90         }
91         std::string str = meta->getString(name);
92         lua_pushlstring(L, str.c_str(), str.size());
93         return 1;
94 }
95
96 // set_string(self, name, var)
97 int NodeMetaRef::l_set_string(lua_State *L)
98 {
99         MAP_LOCK_REQUIRED;
100
101         NodeMetaRef *ref = checkobject(L, 1);
102         std::string name = luaL_checkstring(L, 2);
103         size_t len = 0;
104         const char *s = lua_tolstring(L, 3, &len);
105         std::string str(s, len);
106
107         NodeMetadata *meta = getmeta(ref, !str.empty());
108         if(meta == NULL || str == meta->getString(name))
109                 return 0;
110         meta->setString(name, str);
111         reportMetadataChange(ref);
112         return 0;
113 }
114
115 // get_int(self, name)
116 int NodeMetaRef::l_get_int(lua_State *L)
117 {
118         MAP_LOCK_REQUIRED;
119
120         NodeMetaRef *ref = checkobject(L, 1);
121         std::string name = lua_tostring(L, 2);
122
123         NodeMetadata *meta = getmeta(ref, false);
124         if(meta == NULL){
125                 lua_pushnumber(L, 0);
126                 return 1;
127         }
128         std::string str = meta->getString(name);
129         lua_pushnumber(L, stoi(str));
130         return 1;
131 }
132
133 // set_int(self, name, var)
134 int NodeMetaRef::l_set_int(lua_State *L)
135 {
136         MAP_LOCK_REQUIRED;
137
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);
142
143         NodeMetadata *meta = getmeta(ref, true);
144         if(meta == NULL || str == meta->getString(name))
145                 return 0;
146         meta->setString(name, str);
147         reportMetadataChange(ref);
148         return 0;
149 }
150
151 // get_float(self, name)
152 int NodeMetaRef::l_get_float(lua_State *L)
153 {
154         MAP_LOCK_REQUIRED;
155
156         NodeMetaRef *ref = checkobject(L, 1);
157         std::string name = lua_tostring(L, 2);
158
159         NodeMetadata *meta = getmeta(ref, false);
160         if(meta == NULL){
161                 lua_pushnumber(L, 0);
162                 return 1;
163         }
164         std::string str = meta->getString(name);
165         lua_pushnumber(L, stof(str));
166         return 1;
167 }
168
169 // set_float(self, name, var)
170 int NodeMetaRef::l_set_float(lua_State *L)
171 {
172         MAP_LOCK_REQUIRED;
173
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);
178
179         NodeMetadata *meta = getmeta(ref, true);
180         if(meta == NULL || str == meta->getString(name))
181                 return 0;
182         meta->setString(name, str);
183         reportMetadataChange(ref);
184         return 0;
185 }
186
187 // get_inventory(self)
188 int NodeMetaRef::l_get_inventory(lua_State *L)
189 {
190         MAP_LOCK_REQUIRED;
191
192         NodeMetaRef *ref = checkobject(L, 1);
193         getmeta(ref, true);  // try to ensure the metadata exists
194         InvRef::createNodeMeta(L, ref->m_p);
195         return 1;
196 }
197
198 // to_table(self)
199 int NodeMetaRef::l_to_table(lua_State *L)
200 {
201         MAP_LOCK_REQUIRED;
202
203         NodeMetaRef *ref = checkobject(L, 1);
204
205         NodeMetadata *meta = getmeta(ref, true);
206         if (meta == NULL) {
207                 lua_pushnil(L);
208                 return 1;
209         }
210         lua_newtable(L);
211
212         // fields
213         lua_newtable(L);
214         {
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());
222                         lua_settable(L, -3);
223                 }
224         }
225         lua_setfield(L, -2, "fields");
226
227         // inventory
228         lua_newtable(L);
229         Inventory *inv = meta->getInventory();
230         if (inv) {
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());
236                 }
237         }
238         lua_setfield(L, -2, "inventory");
239         return 1;
240 }
241
242 // from_table(self, table)
243 int NodeMetaRef::l_from_table(lua_State *L)
244 {
245         MAP_LOCK_REQUIRED;
246
247         NodeMetaRef *ref = checkobject(L, 1);
248         int base = 2;
249
250         // clear old metadata first
251         ref->m_env->getMap().removeNodeMetadata(ref->m_p);
252
253         if(lua_isnil(L, base)){
254                 // No metadata
255                 lua_pushboolean(L, true);
256                 return 1;
257         }
258
259         // Create new metadata
260         NodeMetadata *meta = getmeta(ref, true);
261         if(meta == NULL){
262                 lua_pushboolean(L, false);
263                 return 1;
264         }
265         // Set fields
266         lua_getfield(L, base, "fields");
267         int fieldstable = lua_gettop(L);
268         lua_pushnil(L);
269         while(lua_next(L, fieldstable) != 0){
270                 // key at index -2 and value at index -1
271                 std::string name = lua_tostring(L, -2);
272                 size_t cl;
273                 const char *cs = lua_tolstring(L, -1, &cl);
274                 std::string value(cs, cl);
275                 meta->setString(name, value);
276                 lua_pop(L, 1); // removes value, keeps key for next iteration
277         }
278         // Set inventory
279         Inventory *inv = meta->getInventory();
280         lua_getfield(L, base, "inventory");
281         int inventorytable = lua_gettop(L);
282         lua_pushnil(L);
283         while(lua_next(L, inventorytable) != 0){
284                 // key at index -2 and value at index -1
285                 std::string name = lua_tostring(L, -2);
286                 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
287                 lua_pop(L, 1); // removes value, keeps key for next iteration
288         }
289         reportMetadataChange(ref);
290         lua_pushboolean(L, true);
291         return 1;
292 }
293
294
295 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
296         m_p(p),
297         m_env(env)
298 {
299 }
300
301 NodeMetaRef::~NodeMetaRef()
302 {
303 }
304
305 // Creates an NodeMetaRef and leaves it on top of stack
306 // Not callable from Lua; all references are created on the C side.
307 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
308 {
309         NodeMetaRef *o = new NodeMetaRef(p, env);
310         //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
311         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
312         luaL_getmetatable(L, className);
313         lua_setmetatable(L, -2);
314 }
315
316 void NodeMetaRef::Register(lua_State *L)
317 {
318         lua_newtable(L);
319         int methodtable = lua_gettop(L);
320         luaL_newmetatable(L, className);
321         int metatable = lua_gettop(L);
322
323         lua_pushliteral(L, "__metatable");
324         lua_pushvalue(L, methodtable);
325         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
326
327         lua_pushliteral(L, "__index");
328         lua_pushvalue(L, methodtable);
329         lua_settable(L, metatable);
330
331         lua_pushliteral(L, "__gc");
332         lua_pushcfunction(L, gc_object);
333         lua_settable(L, metatable);
334
335         lua_pop(L, 1);  // drop metatable
336
337         luaL_openlib(L, 0, methods, 0);  // fill methodtable
338         lua_pop(L, 1);  // drop methodtable
339
340         // Cannot be created from Lua
341         //lua_register(L, className, create_object);
342 }
343
344 const char NodeMetaRef::className[] = "NodeMetaRef";
345 const luaL_reg NodeMetaRef::methods[] = {
346         luamethod(NodeMetaRef, get_string),
347         luamethod(NodeMetaRef, set_string),
348         luamethod(NodeMetaRef, get_int),
349         luamethod(NodeMetaRef, set_int),
350         luamethod(NodeMetaRef, get_float),
351         luamethod(NodeMetaRef, set_float),
352         luamethod(NodeMetaRef, get_inventory),
353         luamethod(NodeMetaRef, to_table),
354         luamethod(NodeMetaRef, from_table),
355         {0,0}
356 };