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 // get_string(self, name)
55 int MetaDataRef::l_get_string(lua_State *L)
59 MetaDataRef *ref = checkobject(L, 1);
60 std::string name = luaL_checkstring(L, 2);
62 Metadata *meta = ref->getmeta(false);
64 lua_pushlstring(L, "", 0);
68 const std::string &str = meta->getString(name);
69 lua_pushlstring(L, str.c_str(), str.size());
73 // set_string(self, name, var)
74 int MetaDataRef::l_set_string(lua_State *L)
78 MetaDataRef *ref = checkobject(L, 1);
79 std::string name = luaL_checkstring(L, 2);
81 const char *s = lua_tolstring(L, 3, &len);
82 std::string str(s, len);
84 Metadata *meta = ref->getmeta(!str.empty());
85 if (meta == NULL || str == meta->getString(name))
88 meta->setString(name, str);
89 ref->reportMetadataChange();
93 // get_int(self, name)
94 int MetaDataRef::l_get_int(lua_State *L)
98 MetaDataRef *ref = checkobject(L, 1);
99 std::string name = luaL_checkstring(L, 2);
101 Metadata *meta = ref->getmeta(false);
103 lua_pushnumber(L, 0);
107 const std::string &str = meta->getString(name);
108 lua_pushnumber(L, stoi(str));
112 // set_int(self, name, var)
113 int MetaDataRef::l_set_int(lua_State *L)
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);
122 Metadata *meta = ref->getmeta(true);
123 if (meta == NULL || str == meta->getString(name))
126 meta->setString(name, str);
127 ref->reportMetadataChange();
131 // get_float(self, name)
132 int MetaDataRef::l_get_float(lua_State *L)
136 MetaDataRef *ref = checkobject(L, 1);
137 std::string name = luaL_checkstring(L, 2);
139 Metadata *meta = ref->getmeta(false);
141 lua_pushnumber(L, 0);
145 const std::string &str = meta->getString(name);
146 lua_pushnumber(L, stof(str));
150 // set_float(self, name, var)
151 int MetaDataRef::l_set_float(lua_State *L)
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);
160 Metadata *meta = ref->getmeta(true);
161 if (meta == NULL || str == meta->getString(name))
164 meta->setString(name, str);
165 ref->reportMetadataChange();
170 int MetaDataRef::l_to_table(lua_State *L)
174 MetaDataRef *ref = checkobject(L, 1);
176 Metadata *meta = ref->getmeta(true);
183 ref->handleToTable(L, meta);
188 // from_table(self, table)
189 int MetaDataRef::l_from_table(lua_State *L)
193 MetaDataRef *ref = checkobject(L, 1);
198 if (!lua_istable(L, base)) {
200 lua_pushboolean(L, true);
204 // Create new metadata
205 Metadata *meta = ref->getmeta(true);
207 lua_pushboolean(L, false);
211 bool was_successful = ref->handleFromTable(L, base, meta);
212 ref->reportMetadataChange();
213 lua_pushboolean(L, was_successful);
217 void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
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());
230 lua_setfield(L, -2, "fields");
233 bool MetaDataRef::handleFromTable(lua_State *L, int table, Metadata *meta)
236 lua_getfield(L, table, "fields");
237 if (lua_istable(L, -1)) {
238 int fieldstable = lua_gettop(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);
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
254 // equals(self, other)
255 int MetaDataRef::l_equals(lua_State *L)
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);
264 lua_pushboolean(L, *data1 == *data2);