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