Add player:get_meta(), deprecate player attributes (#7202)
[oweals/minetest.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 // get_string(self, name)
55 int MetaDataRef::l_get_string(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                 lua_pushlstring(L, "", 0);
65                 return 1;
66         }
67
68         const std::string &str = meta->getString(name);
69         lua_pushlstring(L, str.c_str(), str.size());
70         return 1;
71 }
72
73 // set_string(self, name, var)
74 int MetaDataRef::l_set_string(lua_State *L)
75 {
76         MAP_LOCK_REQUIRED;
77
78         MetaDataRef *ref = checkobject(L, 1);
79         std::string name = luaL_checkstring(L, 2);
80         size_t len = 0;
81         const char *s = lua_tolstring(L, 3, &len);
82         std::string str(s, len);
83
84         Metadata *meta = ref->getmeta(!str.empty());
85         if (meta == NULL || str == meta->getString(name))
86                 return 0;
87
88         meta->setString(name, str);
89         ref->reportMetadataChange();
90         return 0;
91 }
92
93 // get_int(self, name)
94 int MetaDataRef::l_get_int(lua_State *L)
95 {
96         MAP_LOCK_REQUIRED;
97
98         MetaDataRef *ref = checkobject(L, 1);
99         std::string name = luaL_checkstring(L, 2);
100
101         Metadata *meta = ref->getmeta(false);
102         if (meta == NULL) {
103                 lua_pushnumber(L, 0);
104                 return 1;
105         }
106
107         const std::string &str = meta->getString(name);
108         lua_pushnumber(L, stoi(str));
109         return 1;
110 }
111
112 // set_int(self, name, var)
113 int MetaDataRef::l_set_int(lua_State *L)
114 {
115         MAP_LOCK_REQUIRED;
116
117         MetaDataRef *ref = checkobject(L, 1);
118         std::string name = luaL_checkstring(L, 2);
119         int a = luaL_checkint(L, 3);
120         std::string str = itos(a);
121
122         Metadata *meta = ref->getmeta(true);
123         if (meta == NULL || str == meta->getString(name))
124                 return 0;
125
126         meta->setString(name, str);
127         ref->reportMetadataChange();
128         return 0;
129 }
130
131 // get_float(self, name)
132 int MetaDataRef::l_get_float(lua_State *L)
133 {
134         MAP_LOCK_REQUIRED;
135
136         MetaDataRef *ref = checkobject(L, 1);
137         std::string name = luaL_checkstring(L, 2);
138
139         Metadata *meta = ref->getmeta(false);
140         if (meta == NULL) {
141                 lua_pushnumber(L, 0);
142                 return 1;
143         }
144
145         const std::string &str = meta->getString(name);
146         lua_pushnumber(L, stof(str));
147         return 1;
148 }
149
150 // set_float(self, name, var)
151 int MetaDataRef::l_set_float(lua_State *L)
152 {
153         MAP_LOCK_REQUIRED;
154
155         MetaDataRef *ref = checkobject(L, 1);
156         std::string name = luaL_checkstring(L, 2);
157         float a = luaL_checknumber(L, 3);
158         std::string str = ftos(a);
159
160         Metadata *meta = ref->getmeta(true);
161         if (meta == NULL || str == meta->getString(name))
162                 return 0;
163
164         meta->setString(name, str);
165         ref->reportMetadataChange();
166         return 0;
167 }
168
169 // to_table(self)
170 int MetaDataRef::l_to_table(lua_State *L)
171 {
172         MAP_LOCK_REQUIRED;
173
174         MetaDataRef *ref = checkobject(L, 1);
175
176         Metadata *meta = ref->getmeta(true);
177         if (meta == NULL) {
178                 lua_pushnil(L);
179                 return 1;
180         }
181         lua_newtable(L);
182
183         ref->handleToTable(L, meta);
184
185         return 1;
186 }
187
188 // from_table(self, table)
189 int MetaDataRef::l_from_table(lua_State *L)
190 {
191         MAP_LOCK_REQUIRED;
192
193         MetaDataRef *ref = checkobject(L, 1);
194         int base = 2;
195
196         ref->clearMeta();
197
198         if (!lua_istable(L, base)) {
199                 // No metadata
200                 lua_pushboolean(L, true);
201                 return 1;
202         }
203
204         // Create new metadata
205         Metadata *meta = ref->getmeta(true);
206         if (meta == NULL) {
207                 lua_pushboolean(L, false);
208                 return 1;
209         }
210
211         bool was_successful = ref->handleFromTable(L, base, meta);
212         ref->reportMetadataChange();
213         lua_pushboolean(L, was_successful);
214         return 1;
215 }
216
217 void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
218 {
219         lua_newtable(L);
220         {
221                 const StringMap &fields = meta->getStrings();
222                 for (const auto &field : fields) {
223                         const std::string &name = field.first;
224                         const std::string &value = field.second;
225                         lua_pushlstring(L, name.c_str(), name.size());
226                         lua_pushlstring(L, value.c_str(), value.size());
227                         lua_settable(L, -3);
228                 }
229         }
230         lua_setfield(L, -2, "fields");
231 }
232
233 bool MetaDataRef::handleFromTable(lua_State *L, int table, Metadata *meta)
234 {
235         // Set fields
236         lua_getfield(L, table, "fields");
237         if (lua_istable(L, -1)) {
238                 int fieldstable = lua_gettop(L);
239                 lua_pushnil(L);
240                 while (lua_next(L, fieldstable) != 0) {
241                         // key at index -2 and value at index -1
242                         std::string name = lua_tostring(L, -2);
243                         size_t cl;
244                         const char *cs = lua_tolstring(L, -1, &cl);
245                         meta->setString(name, std::string(cs, cl));
246                         lua_pop(L, 1); // Remove value, keep key for next iteration
247                 }
248                 lua_pop(L, 1);
249         }
250
251         return true;
252 }
253
254 // equals(self, other)
255 int MetaDataRef::l_equals(lua_State *L)
256 {
257         MetaDataRef *ref1 = checkobject(L, 1);
258         Metadata *data1 = ref1->getmeta(false);
259         MetaDataRef *ref2 = checkobject(L, 2);
260         Metadata *data2 = ref2->getmeta(false);
261         if (data1 == NULL || data2 == NULL)
262                 lua_pushboolean(L, data1 == data2);
263         else
264                 lua_pushboolean(L, *data1 == *data2);
265         return 1;
266 }