]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_metadata.cpp
Modernize lua read (part 2 & 3): C++ templating assurance (#7410)
[dragonfireclient.git] / src / script / lua_api / l_metadata.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "lua_api/l_metadata.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_content.h"
24 #include "serverenvironment.h"
25 #include "map.h"
26 #include "server.h"
27
28 // LUALIB_API
29 void *luaL_checkudata_is_metadataref(lua_State *L, int ud) {
30         void *p = lua_touserdata(L, ud);
31         if (p != NULL &&  // value is a userdata?
32                         lua_getmetatable(L, ud)) {  // does it have a metatable?
33                 lua_getfield(L, -1, "metadata_class");
34                 if (lua_type(L, -1) == LUA_TSTRING) { // does it have a metadata_class field?
35                         return p;
36                 }
37         }
38         luaL_typerror(L, ud, "MetaDataRef");
39         return NULL;
40 }
41
42 MetaDataRef* MetaDataRef::checkobject(lua_State *L, int narg)
43 {
44         luaL_checktype(L, narg, LUA_TUSERDATA);
45         void *ud = luaL_checkudata_is_metadataref(L, narg);
46         if (!ud)
47                 luaL_typerror(L, narg, "MetaDataRef");
48
49         return *(MetaDataRef**)ud;  // unbox pointer
50 }
51
52 // Exported functions
53
54 // contains(self, name)
55 int MetaDataRef::l_contains(lua_State *L)
56 {
57         MAP_LOCK_REQUIRED;
58
59         MetaDataRef *ref = checkobject(L, 1);
60         std::string name = luaL_checkstring(L, 2);
61
62         Metadata *meta = ref->getmeta(false);
63         if (meta == NULL)
64                 return 0;
65
66         lua_pushboolean(L, meta->contains(name));
67         return 1;
68 }
69
70 // get(self, name)
71 int MetaDataRef::l_get(lua_State *L)
72 {
73         MAP_LOCK_REQUIRED;
74
75         MetaDataRef *ref = checkobject(L, 1);
76         std::string name = luaL_checkstring(L, 2);
77
78         Metadata *meta = ref->getmeta(false);
79         if (meta == NULL)
80                 return 0;
81
82         std::string str;
83         if (meta->getStringToRef(name, str)) {
84                 lua_pushlstring(L, str.c_str(), str.size());
85                 return 1;
86         }
87         return 0;
88 }
89
90 // get_string(self, name)
91 int MetaDataRef::l_get_string(lua_State *L)
92 {
93         MAP_LOCK_REQUIRED;
94
95         MetaDataRef *ref = checkobject(L, 1);
96         std::string name = luaL_checkstring(L, 2);
97
98         Metadata *meta = ref->getmeta(false);
99         if (meta == NULL) {
100                 lua_pushlstring(L, "", 0);
101                 return 1;
102         }
103
104         const std::string &str = meta->getString(name);
105         lua_pushlstring(L, str.c_str(), str.size());
106         return 1;
107 }
108
109 // set_string(self, name, var)
110 int MetaDataRef::l_set_string(lua_State *L)
111 {
112         MAP_LOCK_REQUIRED;
113
114         MetaDataRef *ref = checkobject(L, 1);
115         std::string name = luaL_checkstring(L, 2);
116         size_t len = 0;
117         const char *s = lua_tolstring(L, 3, &len);
118         std::string str(s, len);
119
120         Metadata *meta = ref->getmeta(!str.empty());
121         if (meta == NULL || str == meta->getString(name))
122                 return 0;
123
124         meta->setString(name, str);
125         ref->reportMetadataChange();
126         return 0;
127 }
128
129 // get_int(self, name)
130 int MetaDataRef::l_get_int(lua_State *L)
131 {
132         MAP_LOCK_REQUIRED;
133
134         MetaDataRef *ref = checkobject(L, 1);
135         std::string name = luaL_checkstring(L, 2);
136
137         Metadata *meta = ref->getmeta(false);
138         if (meta == NULL) {
139                 lua_pushnumber(L, 0);
140                 return 1;
141         }
142
143         const std::string &str = meta->getString(name);
144         lua_pushnumber(L, stoi(str));
145         return 1;
146 }
147
148 // set_int(self, name, var)
149 int MetaDataRef::l_set_int(lua_State *L)
150 {
151         MAP_LOCK_REQUIRED;
152
153         MetaDataRef *ref = checkobject(L, 1);
154         std::string name = luaL_checkstring(L, 2);
155         int a = luaL_checkint(L, 3);
156         std::string str = itos(a);
157
158         Metadata *meta = ref->getmeta(true);
159         if (meta == NULL || str == meta->getString(name))
160                 return 0;
161
162         meta->setString(name, str);
163         ref->reportMetadataChange();
164         return 0;
165 }
166
167 // get_float(self, name)
168 int MetaDataRef::l_get_float(lua_State *L)
169 {
170         MAP_LOCK_REQUIRED;
171
172         MetaDataRef *ref = checkobject(L, 1);
173         std::string name = luaL_checkstring(L, 2);
174
175         Metadata *meta = ref->getmeta(false);
176         if (meta == NULL) {
177                 lua_pushnumber(L, 0);
178                 return 1;
179         }
180
181         const std::string &str = meta->getString(name);
182         lua_pushnumber(L, stof(str));
183         return 1;
184 }
185
186 // set_float(self, name, var)
187 int MetaDataRef::l_set_float(lua_State *L)
188 {
189         MAP_LOCK_REQUIRED;
190
191         MetaDataRef *ref = checkobject(L, 1);
192         std::string name = luaL_checkstring(L, 2);
193         float a = readParam<float>(L, 3);
194         std::string str = ftos(a);
195
196         Metadata *meta = ref->getmeta(true);
197         if (meta == NULL || str == meta->getString(name))
198                 return 0;
199
200         meta->setString(name, str);
201         ref->reportMetadataChange();
202         return 0;
203 }
204
205 // to_table(self)
206 int MetaDataRef::l_to_table(lua_State *L)
207 {
208         MAP_LOCK_REQUIRED;
209
210         MetaDataRef *ref = checkobject(L, 1);
211
212         Metadata *meta = ref->getmeta(true);
213         if (meta == NULL) {
214                 lua_pushnil(L);
215                 return 1;
216         }
217         lua_newtable(L);
218
219         ref->handleToTable(L, meta);
220
221         return 1;
222 }
223
224 // from_table(self, table)
225 int MetaDataRef::l_from_table(lua_State *L)
226 {
227         MAP_LOCK_REQUIRED;
228
229         MetaDataRef *ref = checkobject(L, 1);
230         int base = 2;
231
232         ref->clearMeta();
233
234         if (!lua_istable(L, base)) {
235                 // No metadata
236                 lua_pushboolean(L, true);
237                 return 1;
238         }
239
240         // Create new metadata
241         Metadata *meta = ref->getmeta(true);
242         if (meta == NULL) {
243                 lua_pushboolean(L, false);
244                 return 1;
245         }
246
247         bool was_successful = ref->handleFromTable(L, base, meta);
248         ref->reportMetadataChange();
249         lua_pushboolean(L, was_successful);
250         return 1;
251 }
252
253 void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
254 {
255         lua_newtable(L);
256         {
257                 const StringMap &fields = meta->getStrings();
258                 for (const auto &field : fields) {
259                         const std::string &name = field.first;
260                         const std::string &value = field.second;
261                         lua_pushlstring(L, name.c_str(), name.size());
262                         lua_pushlstring(L, value.c_str(), value.size());
263                         lua_settable(L, -3);
264                 }
265         }
266         lua_setfield(L, -2, "fields");
267 }
268
269 bool MetaDataRef::handleFromTable(lua_State *L, int table, Metadata *meta)
270 {
271         // Set fields
272         lua_getfield(L, table, "fields");
273         if (lua_istable(L, -1)) {
274                 int fieldstable = lua_gettop(L);
275                 lua_pushnil(L);
276                 while (lua_next(L, fieldstable) != 0) {
277                         // key at index -2 and value at index -1
278                         std::string name = readParam<std::string>(L, -2);
279                         size_t cl;
280                         const char *cs = lua_tolstring(L, -1, &cl);
281                         meta->setString(name, std::string(cs, cl));
282                         lua_pop(L, 1); // Remove value, keep key for next iteration
283                 }
284                 lua_pop(L, 1);
285         }
286
287         return true;
288 }
289
290 // equals(self, other)
291 int MetaDataRef::l_equals(lua_State *L)
292 {
293         MetaDataRef *ref1 = checkobject(L, 1);
294         Metadata *data1 = ref1->getmeta(false);
295         MetaDataRef *ref2 = checkobject(L, 2);
296         Metadata *data2 = ref2->getmeta(false);
297         if (data1 == NULL || data2 == NULL)
298                 lua_pushboolean(L, data1 == data2);
299         else
300                 lua_pushboolean(L, *data1 == *data2);
301         return 1;
302 }