]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_nodemeta.cpp
add LUA_FCT
[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 "mapblock.h"
27 #include "server.h"
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 Metadata* NodeMetaRef::getmeta(bool auto_create)
41 {
42         if (m_is_local)
43                 return m_meta;
44
45         NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p);
46         if (meta == NULL && auto_create) {
47                 meta = new NodeMetadata(m_env->getGameDef()->idef());
48                 if (!m_env->getMap().setNodeMetadata(m_p, meta)) {
49                         delete meta;
50                         return NULL;
51                 }
52         }
53         return meta;
54 }
55
56 void NodeMetaRef::clearMeta()
57 {
58         SANITY_CHECK(!m_is_local);
59         m_env->getMap().removeNodeMetadata(m_p);
60 }
61
62 void NodeMetaRef::reportMetadataChange(const std::string *name)
63 {
64         SANITY_CHECK(!m_is_local);
65         // NOTE: This same code is in rollback_interface.cpp
66         // Inform other things that the metadata has changed
67         NodeMetadata *meta = dynamic_cast<NodeMetadata*>(m_meta);
68
69         MapEditEvent event;
70         event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
71         event.p = m_p;
72         event.is_private_change = name && meta && meta->isPrivate(*name);
73         m_env->getMap().dispatchEvent(event);
74 }
75
76 // Exported functions
77
78 // garbage collector
79 int NodeMetaRef::gc_object(lua_State *L) {
80         NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
81         delete o;
82         return 0;
83 }
84
85 // get_inventory(self)
86 int NodeMetaRef::l_get_inventory(lua_State *L)
87 {
88         MAP_LOCK_REQUIRED;
89
90         NodeMetaRef *ref = checkobject(L, 1);
91         ref->getmeta(true);  // try to ensure the metadata exists
92         InvRef::createNodeMeta(L, ref->m_p);
93         return 1;
94 }
95
96 // mark_as_private(self, <string> or {<string>, <string>, ...})
97 int NodeMetaRef::l_mark_as_private(lua_State *L)
98 {
99         MAP_LOCK_REQUIRED;
100
101         NodeMetaRef *ref = checkobject(L, 1);
102         NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
103         assert(meta);
104
105         if (lua_istable(L, 2)) {
106                 lua_pushnil(L);
107                 while (lua_next(L, 2) != 0) {
108                         // key at index -2 and value at index -1
109                         luaL_checktype(L, -1, LUA_TSTRING);
110                         meta->markPrivate(readParam<std::string>(L, -1), true);
111                         // removes value, keeps key for next iteration
112                         lua_pop(L, 1);
113                 }
114         } else if (lua_isstring(L, 2)) {
115                 meta->markPrivate(readParam<std::string>(L, 2), true);
116         }
117         ref->reportMetadataChange();
118
119         return 0;
120 }
121
122 void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
123 {
124         // fields
125         MetaDataRef::handleToTable(L, _meta);
126
127         NodeMetadata *meta = (NodeMetadata*) _meta;
128
129         // inventory
130         lua_newtable(L);
131         Inventory *inv = meta->getInventory();
132         if (inv) {
133                 std::vector<const InventoryList *> lists = inv->getLists();
134                 for(std::vector<const InventoryList *>::const_iterator
135                                 i = lists.begin(); i != lists.end(); ++i) {
136                         push_inventory_list(L, inv, (*i)->getName().c_str());
137                         lua_setfield(L, -2, (*i)->getName().c_str());
138                 }
139         }
140         lua_setfield(L, -2, "inventory");
141 }
142
143 // from_table(self, table)
144 bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
145 {
146         // fields
147         if (!MetaDataRef::handleFromTable(L, table, _meta))
148                 return false;
149
150         NodeMetadata *meta = (NodeMetadata*) _meta;
151
152         // inventory
153         Inventory *inv = meta->getInventory();
154         lua_getfield(L, table, "inventory");
155         if (lua_istable(L, -1)) {
156                 int inventorytable = lua_gettop(L);
157                 lua_pushnil(L);
158                 while (lua_next(L, inventorytable) != 0) {
159                         // key at index -2 and value at index -1
160                         std::string name = luaL_checkstring(L, -2);
161                         read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
162                         lua_pop(L, 1); // Remove value, keep key for next iteration
163                 }
164                 lua_pop(L, 1);
165         }
166
167         return true;
168 }
169
170
171 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
172         m_p(p),
173         m_env(env)
174 {
175 }
176
177 NodeMetaRef::NodeMetaRef(Metadata *meta):
178         m_meta(meta),
179         m_is_local(true)
180 {
181 }
182
183 // Creates an NodeMetaRef and leaves it on top of stack
184 // Not callable from Lua; all references are created on the C side.
185 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
186 {
187         NodeMetaRef *o = new NodeMetaRef(p, env);
188         //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
189         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
190         luaL_getmetatable(L, className);
191         lua_setmetatable(L, -2);
192 }
193
194 // Client-sided version of the above
195 void NodeMetaRef::createClient(lua_State *L, Metadata *meta)
196 {
197         NodeMetaRef *o = new NodeMetaRef(meta);
198         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
199         luaL_getmetatable(L, className);
200         lua_setmetatable(L, -2);
201 }
202
203 const char NodeMetaRef::className[] = "NodeMetaRef";
204 void NodeMetaRef::RegisterCommon(lua_State *L)
205 {
206         lua_newtable(L);
207         int methodtable = lua_gettop(L);
208         luaL_newmetatable(L, className);
209         int metatable = lua_gettop(L);
210
211         lua_pushliteral(L, "__metatable");
212         lua_pushvalue(L, methodtable);
213         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
214
215         lua_pushliteral(L, "metadata_class");
216         lua_pushlstring(L, className, strlen(className));
217         lua_settable(L, metatable);
218
219         lua_pushliteral(L, "__index");
220         lua_pushvalue(L, methodtable);
221         lua_settable(L, metatable);
222
223         lua_pushliteral(L, "__gc");
224         lua_pushcfunction(L, gc_object);
225         lua_settable(L, metatable);
226
227         lua_pushliteral(L, "__eq");
228         lua_pushcfunction(L, l_equals);
229         lua_settable(L, metatable);
230
231         lua_pop(L, 1);  // drop metatable
232 }
233
234 void NodeMetaRef::Register(lua_State *L)
235 {
236         RegisterCommon(L);
237         luaL_openlib(L, 0, methodsServer, 0);  // fill methodtable
238         lua_pop(L, 1);  // drop methodtable
239 }
240
241
242 const luaL_Reg NodeMetaRef::methodsServer[] = {
243         luamethod(MetaDataRef, contains),
244         luamethod(MetaDataRef, get),
245         luamethod(MetaDataRef, get_string),
246         luamethod(MetaDataRef, set_string),
247         luamethod(MetaDataRef, get_int),
248         luamethod(MetaDataRef, set_int),
249         luamethod(MetaDataRef, get_float),
250         luamethod(MetaDataRef, set_float),
251         luamethod(MetaDataRef, to_table),
252         luamethod(MetaDataRef, from_table),
253         luamethod(NodeMetaRef, get_inventory),
254         luamethod(NodeMetaRef, mark_as_private),
255         luamethod(MetaDataRef, equals),
256         {0,0}
257 };
258
259
260 void NodeMetaRef::RegisterClient(lua_State *L)
261 {
262         RegisterCommon(L);
263         luaL_openlib(L, 0, methodsClient, 0);  // fill methodtable
264         lua_pop(L, 1);  // drop methodtable
265 }
266
267
268 const luaL_Reg NodeMetaRef::methodsClient[] = {
269         luamethod(MetaDataRef, contains),
270         luamethod(MetaDataRef, get),
271         luamethod(MetaDataRef, get_string),
272         luamethod(MetaDataRef, get_int),
273         luamethod(MetaDataRef, get_float),
274         luamethod(MetaDataRef, to_table),
275         {0,0}
276 };