3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "lua_api/l_metadata.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_content.h"
23 #include "serverenvironment.h"
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?
37 luaL_typerror(L, ud, "MetaDataRef");
41 MetaDataRef* MetaDataRef::checkobject(lua_State *L, int narg)
43 luaL_checktype(L, narg, LUA_TUSERDATA);
44 void *ud = luaL_checkudata_is_metadataref(L, narg);
46 luaL_typerror(L, narg, "MetaDataRef");
48 return *(MetaDataRef**)ud; // unbox pointer
53 // get_string(self, name)
54 int MetaDataRef::l_get_string(lua_State *L)
58 MetaDataRef *ref = checkobject(L, 1);
59 std::string name = luaL_checkstring(L, 2);
61 Metadata *meta = ref->getmeta(false);
63 lua_pushlstring(L, "", 0);
67 const std::string &str = meta->getString(name);
68 lua_pushlstring(L, str.c_str(), str.size());
72 // set_string(self, name, var)
73 int MetaDataRef::l_set_string(lua_State *L)
77 MetaDataRef *ref = checkobject(L, 1);
78 std::string name = luaL_checkstring(L, 2);
80 const char *s = lua_tolstring(L, 3, &len);
81 std::string str(s, len);
83 Metadata *meta = ref->getmeta(!str.empty());
84 if (meta == NULL || str == meta->getString(name))
87 meta->setString(name, str);
88 ref->reportMetadataChange();
92 // get_int(self, name)
93 int MetaDataRef::l_get_int(lua_State *L)
97 MetaDataRef *ref = checkobject(L, 1);
98 std::string name = lua_tostring(L, 2);
100 Metadata *meta = ref->getmeta(false);
102 lua_pushnumber(L, 0);
106 const std::string &str = meta->getString(name);
107 lua_pushnumber(L, stoi(str));
111 // set_int(self, name, var)
112 int MetaDataRef::l_set_int(lua_State *L)
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);
121 Metadata *meta = ref->getmeta(true);
122 if (meta == NULL || str == meta->getString(name))
125 meta->setString(name, str);
126 ref->reportMetadataChange();
130 // get_float(self, name)
131 int MetaDataRef::l_get_float(lua_State *L)
135 MetaDataRef *ref = checkobject(L, 1);
136 std::string name = lua_tostring(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, stof(str));
149 // set_float(self, name, var)
150 int MetaDataRef::l_set_float(lua_State *L)
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);
159 Metadata *meta = ref->getmeta(true);
160 if (meta == NULL || str == meta->getString(name))
163 meta->setString(name, str);
164 ref->reportMetadataChange();
169 int MetaDataRef::l_to_table(lua_State *L)
173 MetaDataRef *ref = checkobject(L, 1);
175 Metadata *meta = ref->getmeta(true);
182 ref->handleToTable(L, meta);
187 // from_table(self, table)
188 int MetaDataRef::l_from_table(lua_State *L)
192 MetaDataRef *ref = checkobject(L, 1);
197 if (!lua_istable(L, base)) {
199 lua_pushboolean(L, true);
203 // Create new metadata
204 Metadata *meta = ref->getmeta(true);
206 lua_pushboolean(L, false);
210 bool was_successful = ref->handleFromTable(L, base, meta);
211 ref->reportMetadataChange();
212 lua_pushboolean(L, was_successful);
216 void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
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());
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