]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_nodemeta.cpp
Merge remote-tracking branch 'origin/master'
[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 "cpp_api/scriptapi.h"
21 #include "common/c_converter.h"
22 #include "common/c_content.h"
23 #include "map.h"
24 #include "lua_api/l_nodemeta.h"
25 #include "common/c_internal.h"
26 #include "lua_api/l_inventory.h"
27
28
29 /*
30         NodeMetaRef
31 */
32 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
33 {
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
38 }
39
40 NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create)
41 {
42         NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
43         if(meta == NULL && auto_create)
44         {
45                 meta = new NodeMetadata(ref->m_env->getGameDef());
46                 ref->m_env->getMap().setNodeMetadata(ref->m_p, meta);
47         }
48         return meta;
49 }
50
51 void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
52 {
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);
56         MapEditEvent event;
57         event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
58         event.p = blockpos;
59         ref->m_env->getMap().dispatchEvent(&event);
60         // Set the block to be saved
61         MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
62         if(block)
63                 block->raiseModified(MOD_STATE_WRITE_NEEDED,
64                                 "NodeMetaRef::reportMetadataChange");
65 }
66
67 // Exported functions
68
69 // garbage collector
70 int NodeMetaRef::gc_object(lua_State *L) {
71         NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
72         delete o;
73         return 0;
74 }
75
76 // get_string(self, name)
77 int NodeMetaRef::l_get_string(lua_State *L)
78 {
79         NodeMetaRef *ref = checkobject(L, 1);
80         std::string name = luaL_checkstring(L, 2);
81
82         NodeMetadata *meta = getmeta(ref, false);
83         if(meta == NULL){
84                 lua_pushlstring(L, "", 0);
85                 return 1;
86         }
87         std::string str = meta->getString(name);
88         lua_pushlstring(L, str.c_str(), str.size());
89         return 1;
90 }
91
92 // set_string(self, name, var)
93 int NodeMetaRef::l_set_string(lua_State *L)
94 {
95         NodeMetaRef *ref = checkobject(L, 1);
96         std::string name = luaL_checkstring(L, 2);
97         size_t len = 0;
98         const char *s = lua_tolstring(L, 3, &len);
99         std::string str(s, len);
100
101         NodeMetadata *meta = getmeta(ref, !str.empty());
102         if(meta == NULL || str == meta->getString(name))
103                 return 0;
104         meta->setString(name, str);
105         reportMetadataChange(ref);
106         return 0;
107 }
108
109 // get_int(self, name)
110 int NodeMetaRef::l_get_int(lua_State *L)
111 {
112         NodeMetaRef *ref = checkobject(L, 1);
113         std::string name = lua_tostring(L, 2);
114
115         NodeMetadata *meta = getmeta(ref, false);
116         if(meta == NULL){
117                 lua_pushnumber(L, 0);
118                 return 1;
119         }
120         std::string str = meta->getString(name);
121         lua_pushnumber(L, stoi(str));
122         return 1;
123 }
124
125 // set_int(self, name, var)
126 int NodeMetaRef::l_set_int(lua_State *L)
127 {
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);
132
133         NodeMetadata *meta = getmeta(ref, true);
134         if(meta == NULL || str == meta->getString(name))
135                 return 0;
136         meta->setString(name, str);
137         reportMetadataChange(ref);
138         return 0;
139 }
140
141 // get_float(self, name)
142 int NodeMetaRef::l_get_float(lua_State *L)
143 {
144         NodeMetaRef *ref = checkobject(L, 1);
145         std::string name = lua_tostring(L, 2);
146
147         NodeMetadata *meta = getmeta(ref, false);
148         if(meta == NULL){
149                 lua_pushnumber(L, 0);
150                 return 1;
151         }
152         std::string str = meta->getString(name);
153         lua_pushnumber(L, stof(str));
154         return 1;
155 }
156
157 // set_float(self, name, var)
158 int NodeMetaRef::l_set_float(lua_State *L)
159 {
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);
164
165         NodeMetadata *meta = getmeta(ref, true);
166         if(meta == NULL || str == meta->getString(name))
167                 return 0;
168         meta->setString(name, str);
169         reportMetadataChange(ref);
170         return 0;
171 }
172
173 // get_inventory(self)
174 int NodeMetaRef::l_get_inventory(lua_State *L)
175 {
176         NodeMetaRef *ref = checkobject(L, 1);
177         getmeta(ref, true);  // try to ensure the metadata exists
178         InvRef::createNodeMeta(L, ref->m_p);
179         return 1;
180 }
181
182 // to_table(self)
183 int NodeMetaRef::l_to_table(lua_State *L)
184 {
185         NodeMetaRef *ref = checkobject(L, 1);
186
187         NodeMetadata *meta = getmeta(ref, true);
188         if(meta == NULL){
189                 lua_pushnil(L);
190                 return 1;
191         }
192         lua_newtable(L);
193         // fields
194         lua_newtable(L);
195         {
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());
203                         lua_settable(L, -3);
204                 }
205         }
206         lua_setfield(L, -2, "fields");
207         // inventory
208         lua_newtable(L);
209         Inventory *inv = meta->getInventory();
210         if(inv){
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());
216                 }
217         }
218         lua_setfield(L, -2, "inventory");
219         return 1;
220 }
221
222 // from_table(self, table)
223 int NodeMetaRef::l_from_table(lua_State *L)
224 {
225         NodeMetaRef *ref = checkobject(L, 1);
226         int base = 2;
227
228         if(lua_isnil(L, base)){
229                 // No metadata
230                 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
231                 lua_pushboolean(L, true);
232                 return 1;
233         }
234
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);
239         // Set fields
240         lua_getfield(L, base, "fields");
241         int fieldstable = lua_gettop(L);
242         lua_pushnil(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);
246                 size_t cl;
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
251         }
252         // Set inventory
253         Inventory *inv = meta->getInventory();
254         lua_getfield(L, base, "inventory");
255         int inventorytable = lua_gettop(L);
256         lua_pushnil(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
262         }
263         reportMetadataChange(ref);
264         lua_pushboolean(L, true);
265         return 1;
266 }
267
268
269 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
270         m_p(p),
271         m_env(env)
272 {
273 }
274
275 NodeMetaRef::~NodeMetaRef()
276 {
277 }
278
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)
282 {
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);
288 }
289
290 void NodeMetaRef::Register(lua_State *L)
291 {
292         lua_newtable(L);
293         int methodtable = lua_gettop(L);
294         luaL_newmetatable(L, className);
295         int metatable = lua_gettop(L);
296
297         lua_pushliteral(L, "__metatable");
298         lua_pushvalue(L, methodtable);
299         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
300
301         lua_pushliteral(L, "__index");
302         lua_pushvalue(L, methodtable);
303         lua_settable(L, metatable);
304
305         lua_pushliteral(L, "__gc");
306         lua_pushcfunction(L, gc_object);
307         lua_settable(L, metatable);
308
309         lua_pop(L, 1);  // drop metatable
310
311         luaL_openlib(L, 0, methods, 0);  // fill methodtable
312         lua_pop(L, 1);  // drop methodtable
313
314         // Cannot be created from Lua
315         //lua_register(L, className, create_object);
316 }
317
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),
329         {0,0}
330 };
331
332 REGISTER_LUA_REF(NodeMetaRef);