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