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());
90 // get_string(self, name)
91 int MetaDataRef::l_get_string(lua_State *L)
95 MetaDataRef *ref = checkobject(L, 1);
96 std::string name = luaL_checkstring(L, 2);
98 Metadata *meta = ref->getmeta(false);
100 lua_pushlstring(L, "", 0);
104 const std::string &str = meta->getString(name);
105 lua_pushlstring(L, str.c_str(), str.size());
109 // set_string(self, name, var)
110 int MetaDataRef::l_set_string(lua_State *L)
114 MetaDataRef *ref = checkobject(L, 1);
115 std::string name = luaL_checkstring(L, 2);
117 const char *s = lua_tolstring(L, 3, &len);
118 std::string str(s, len);
120 Metadata *meta = ref->getmeta(!str.empty());
121 if (meta == NULL || str == meta->getString(name))
124 meta->setString(name, str);
125 ref->reportMetadataChange(&name);
129 // get_int(self, name)
130 int MetaDataRef::l_get_int(lua_State *L)
134 MetaDataRef *ref = checkobject(L, 1);
135 std::string name = luaL_checkstring(L, 2);
137 Metadata *meta = ref->getmeta(false);
139 lua_pushnumber(L, 0);
143 const std::string &str = meta->getString(name);
144 lua_pushnumber(L, stoi(str));
148 // set_int(self, name, var)
149 int MetaDataRef::l_set_int(lua_State *L)
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);
158 Metadata *meta = ref->getmeta(true);
159 if (meta == NULL || str == meta->getString(name))
162 meta->setString(name, str);
163 ref->reportMetadataChange(&name);
167 // get_float(self, name)
168 int MetaDataRef::l_get_float(lua_State *L)
172 MetaDataRef *ref = checkobject(L, 1);
173 std::string name = luaL_checkstring(L, 2);
175 Metadata *meta = ref->getmeta(false);
177 lua_pushnumber(L, 0);
181 const std::string &str = meta->getString(name);
182 lua_pushnumber(L, stof(str));
186 // set_float(self, name, var)
187 int MetaDataRef::l_set_float(lua_State *L)
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);
196 Metadata *meta = ref->getmeta(true);
197 if (meta == NULL || str == meta->getString(name))
200 meta->setString(name, str);
201 ref->reportMetadataChange(&name);
206 int MetaDataRef::l_to_table(lua_State *L)
210 MetaDataRef *ref = checkobject(L, 1);
212 Metadata *meta = ref->getmeta(true);
219 ref->handleToTable(L, meta);
224 // from_table(self, table)
225 int MetaDataRef::l_from_table(lua_State *L)
229 MetaDataRef *ref = checkobject(L, 1);
234 if (!lua_istable(L, base)) {
236 lua_pushboolean(L, true);
240 // Create new metadata
241 Metadata *meta = ref->getmeta(true);
243 lua_pushboolean(L, false);
247 bool was_successful = ref->handleFromTable(L, base, meta);
248 ref->reportMetadataChange();
249 lua_pushboolean(L, was_successful);
253 void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
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());
266 lua_setfield(L, -2, "fields");
269 bool MetaDataRef::handleFromTable(lua_State *L, int table, Metadata *meta)
272 lua_getfield(L, table, "fields");
273 if (lua_istable(L, -1)) {
274 int fieldstable = lua_gettop(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);
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
290 // equals(self, other)
291 int MetaDataRef::l_equals(lua_State *L)
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);
300 lua_pushboolean(L, *data1 == *data2);