]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_metadata.cpp
b54005bacdcf1c41a90a67c55d10b257af0faa05
[dragonfireclient.git] / src / script / lua_api / l_metadata.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_metadata.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_content.h"
23 #include "serverenvironment.h"
24 #include "map.h"
25 #include "server.h"
26
27 // LUALIB_API
28 void *luaL_checkudata_is_metadataref(lua_State *L, int ud) {
29         void *p = lua_touserdata(L, ud);
30         if (p != NULL &&  // value is a userdata?
31                         lua_getmetatable(L, ud)) {  // does it have a metatable?
32                 lua_getfield(L, -1, "metadata_class");
33                 if (lua_type(L, -1) == LUA_TSTRING) { // does it have a metadata_class field?
34                         return p;
35                 }
36         }
37         luaL_typerror(L, ud, "MetaDataRef");
38         return NULL;
39 }
40
41 MetaDataRef* MetaDataRef::checkobject(lua_State *L, int narg)
42 {
43         luaL_checktype(L, narg, LUA_TUSERDATA);
44         void *ud = luaL_checkudata_is_metadataref(L, narg);
45         if (!ud)
46                 luaL_typerror(L, narg, "MetaDataRef");
47
48         return *(MetaDataRef**)ud;  // unbox pointer
49 }
50
51 // Exported functions
52
53 // get_string(self, name)
54 int MetaDataRef::l_get_string(lua_State *L)
55 {
56         MAP_LOCK_REQUIRED;
57
58         MetaDataRef *ref = checkobject(L, 1);
59         std::string name = luaL_checkstring(L, 2);
60
61         Metadata *meta = ref->getmeta(false);
62         if (meta == NULL) {
63                 lua_pushlstring(L, "", 0);
64                 return 1;
65         }
66
67         const std::string &str = meta->getString(name);
68         lua_pushlstring(L, str.c_str(), str.size());
69         return 1;
70 }
71
72 // set_string(self, name, var)
73 int MetaDataRef::l_set_string(lua_State *L)
74 {
75         MAP_LOCK_REQUIRED;
76
77         MetaDataRef *ref = checkobject(L, 1);
78         std::string name = luaL_checkstring(L, 2);
79         size_t len = 0;
80         const char *s = lua_tolstring(L, 3, &len);
81         std::string str(s, len);
82
83         Metadata *meta = ref->getmeta(!str.empty());
84         if (meta == NULL || str == meta->getString(name))
85                 return 0;
86
87         meta->setString(name, str);
88         ref->reportMetadataChange();
89         return 0;
90 }
91
92 // get_int(self, name)
93 int MetaDataRef::l_get_int(lua_State *L)
94 {
95         MAP_LOCK_REQUIRED;
96
97         MetaDataRef *ref = checkobject(L, 1);
98         std::string name = lua_tostring(L, 2);
99
100         Metadata *meta = ref->getmeta(false);
101         if (meta == NULL) {
102                 lua_pushnumber(L, 0);
103                 return 1;
104         }
105
106         const std::string &str = meta->getString(name);
107         lua_pushnumber(L, stoi(str));
108         return 1;
109 }
110
111 // set_int(self, name, var)
112 int MetaDataRef::l_set_int(lua_State *L)
113 {
114         MAP_LOCK_REQUIRED;
115
116         MetaDataRef *ref = checkobject(L, 1);
117         std::string name = lua_tostring(L, 2);
118         int a = lua_tointeger(L, 3);
119         std::string str = itos(a);
120
121         Metadata *meta = ref->getmeta(true);
122         if (meta == NULL || str == meta->getString(name))
123                 return 0;
124
125         meta->setString(name, str);
126         ref->reportMetadataChange();
127         return 0;
128 }
129
130 // get_float(self, name)
131 int MetaDataRef::l_get_float(lua_State *L)
132 {
133         MAP_LOCK_REQUIRED;
134
135         MetaDataRef *ref = checkobject(L, 1);
136         std::string name = lua_tostring(L, 2);
137
138         Metadata *meta = ref->getmeta(false);
139         if (meta == NULL) {
140                 lua_pushnumber(L, 0);
141                 return 1;
142         }
143
144         const std::string &str = meta->getString(name);
145         lua_pushnumber(L, stof(str));
146         return 1;
147 }
148
149 // set_float(self, name, var)
150 int MetaDataRef::l_set_float(lua_State *L)
151 {
152         MAP_LOCK_REQUIRED;
153
154         MetaDataRef *ref = checkobject(L, 1);
155         std::string name = lua_tostring(L, 2);
156         float a = lua_tonumber(L, 3);
157         std::string str = ftos(a);
158
159         Metadata *meta = ref->getmeta(true);
160         if (meta == NULL || str == meta->getString(name))
161                 return 0;
162
163         meta->setString(name, str);
164         ref->reportMetadataChange();
165         return 0;
166 }
167
168 // to_table(self)
169 int MetaDataRef::l_to_table(lua_State *L)
170 {
171         MAP_LOCK_REQUIRED;
172
173         MetaDataRef *ref = checkobject(L, 1);
174
175         Metadata *meta = ref->getmeta(true);
176         if (meta == NULL) {
177                 lua_pushnil(L);
178                 return 1;
179         }
180         lua_newtable(L);
181
182         ref->handleToTable(L, meta);
183
184         return 1;
185 }
186
187 // from_table(self, table)
188 int MetaDataRef::l_from_table(lua_State *L)
189 {
190         MAP_LOCK_REQUIRED;
191
192         MetaDataRef *ref = checkobject(L, 1);
193         int base = 2;
194
195         ref->clearMeta();
196
197         if (!lua_istable(L, base)) {
198                 // No metadata
199                 lua_pushboolean(L, true);
200                 return 1;
201         }
202
203         // Create new metadata
204         Metadata *meta = ref->getmeta(true);
205         if (meta == NULL) {
206                 lua_pushboolean(L, false);
207                 return 1;
208         }
209
210         bool was_successful = ref->handleFromTable(L, base, meta);
211         ref->reportMetadataChange();
212         lua_pushboolean(L, was_successful);
213         return 1;
214 }
215
216 void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
217 {
218         lua_newtable(L);
219         {
220                 const StringMap &fields = meta->getStrings();
221                 for (StringMap::const_iterator
222                                 it = fields.begin(); it != fields.end(); ++it) {
223                         const std::string &name = it->first;
224                         const std::string &value = it->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 }