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