3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
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.
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.
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.
21 #include "lua_api/l_metadata.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_content.h"
24 #include "serverenvironment.h"
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?
38 luaL_typerror(L, ud, "MetaDataRef");
42 MetaDataRef* MetaDataRef::checkobject(lua_State *L, int narg)
44 luaL_checktype(L, narg, LUA_TUSERDATA);
45 void *ud = luaL_checkudata_is_metadataref(L, narg);
47 luaL_typerror(L, narg, "MetaDataRef");
49 return *(MetaDataRef**)ud; // unbox pointer
54 // contains(self, name)
55 int MetaDataRef::l_contains(lua_State *L)
59 MetaDataRef *ref = checkobject(L, 1);
60 std::string name = luaL_checkstring(L, 2);
62 Metadata *meta = ref->getmeta(false);
66 lua_pushboolean(L, meta->contains(name));
71 int MetaDataRef::l_get(lua_State *L)
75 MetaDataRef *ref = checkobject(L, 1);
76 std::string name = luaL_checkstring(L, 2);
78 Metadata *meta = ref->getmeta(false);
83 if (meta->getStringToRef(name, str)) {
84 lua_pushlstring(L, str.c_str(), str.size());
91 // get_string(self, name)
92 int MetaDataRef::l_get_string(lua_State *L)
96 MetaDataRef *ref = checkobject(L, 1);
97 std::string name = luaL_checkstring(L, 2);
99 Metadata *meta = ref->getmeta(false);
101 lua_pushlstring(L, "", 0);
105 const std::string &str = meta->getString(name);
106 lua_pushlstring(L, str.c_str(), str.size());
110 // set_string(self, name, var)
111 int MetaDataRef::l_set_string(lua_State *L)
115 MetaDataRef *ref = checkobject(L, 1);
116 std::string name = luaL_checkstring(L, 2);
118 const char *s = lua_tolstring(L, 3, &len);
119 std::string str(s, len);
121 Metadata *meta = ref->getmeta(!str.empty());
122 if (meta == NULL || str == meta->getString(name))
125 meta->setString(name, str);
126 ref->reportMetadataChange(&name);
130 // get_int(self, name)
131 int MetaDataRef::l_get_int(lua_State *L)
135 MetaDataRef *ref = checkobject(L, 1);
136 std::string name = luaL_checkstring(L, 2);
138 Metadata *meta = ref->getmeta(false);
140 lua_pushnumber(L, 0);
144 const std::string &str = meta->getString(name);
145 lua_pushnumber(L, stoi(str));
149 // set_int(self, name, var)
150 int MetaDataRef::l_set_int(lua_State *L)
154 MetaDataRef *ref = checkobject(L, 1);
155 std::string name = luaL_checkstring(L, 2);
156 int a = luaL_checkint(L, 3);
157 std::string str = itos(a);
159 Metadata *meta = ref->getmeta(true);
160 if (meta == NULL || str == meta->getString(name))
163 meta->setString(name, str);
164 ref->reportMetadataChange(&name);
168 // get_float(self, name)
169 int MetaDataRef::l_get_float(lua_State *L)
173 MetaDataRef *ref = checkobject(L, 1);
174 std::string name = luaL_checkstring(L, 2);
176 Metadata *meta = ref->getmeta(false);
178 lua_pushnumber(L, 0);
182 const std::string &str = meta->getString(name);
183 lua_pushnumber(L, stof(str));
187 // set_float(self, name, var)
188 int MetaDataRef::l_set_float(lua_State *L)
192 MetaDataRef *ref = checkobject(L, 1);
193 std::string name = luaL_checkstring(L, 2);
194 float a = readParam<float>(L, 3);
195 std::string str = ftos(a);
197 Metadata *meta = ref->getmeta(true);
198 if (meta == NULL || str == meta->getString(name))
201 meta->setString(name, str);
202 ref->reportMetadataChange(&name);
207 int MetaDataRef::l_to_table(lua_State *L)
211 MetaDataRef *ref = checkobject(L, 1);
213 Metadata *meta = ref->getmeta(true);
220 ref->handleToTable(L, meta);
225 // from_table(self, table)
226 int MetaDataRef::l_from_table(lua_State *L)
230 MetaDataRef *ref = checkobject(L, 1);
235 if (!lua_istable(L, base)) {
237 lua_pushboolean(L, true);
241 // Create new metadata
242 Metadata *meta = ref->getmeta(true);
244 lua_pushboolean(L, false);
248 bool was_successful = ref->handleFromTable(L, base, meta);
249 ref->reportMetadataChange();
250 lua_pushboolean(L, was_successful);
254 void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
258 const StringMap &fields = meta->getStrings();
259 for (const auto &field : fields) {
260 const std::string &name = field.first;
261 const std::string &value = field.second;
262 lua_pushlstring(L, name.c_str(), name.size());
263 lua_pushlstring(L, value.c_str(), value.size());
267 lua_setfield(L, -2, "fields");
270 bool MetaDataRef::handleFromTable(lua_State *L, int table, Metadata *meta)
273 lua_getfield(L, table, "fields");
274 if (lua_istable(L, -1)) {
275 int fieldstable = lua_gettop(L);
277 while (lua_next(L, fieldstable) != 0) {
278 // key at index -2 and value at index -1
279 std::string name = readParam<std::string>(L, -2);
281 const char *cs = lua_tolstring(L, -1, &cl);
282 meta->setString(name, std::string(cs, cl));
283 lua_pop(L, 1); // Remove value, keep key for next iteration
291 // equals(self, other)
292 int MetaDataRef::l_equals(lua_State *L)
294 MetaDataRef *ref1 = checkobject(L, 1);
295 Metadata *data1 = ref1->getmeta(false);
296 MetaDataRef *ref2 = checkobject(L, 2);
297 Metadata *data2 = ref2->getmeta(false);
298 if (data1 == NULL || data2 == NULL)
299 lua_pushboolean(L, data1 == data2);
301 lua_pushboolean(L, *data1 == *data2);