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