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_util.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "lua_api/l_async_events.h"
29 #include "main.h" //required for g_settings, g_settings_path
30 #include "json/json.h"
33 // Writes a line to dstream
34 int ModApiUtil::l_debug(lua_State *L)
37 // Handle multiple parameters to behave like standard lua print()
38 int n = lua_gettop(L);
39 lua_getglobal(L, "tostring");
40 for (int i = 1; i <= n; i++) {
42 Call tostring(i-th argument).
43 This is what print() does, and it behaves a bit
44 differently from directly calling lua_tostring.
46 lua_pushvalue(L, -1); /* function to be called */
47 lua_pushvalue(L, i); /* value to print */
49 const char *s = lua_tostring(L, -1);
61 // Writes a line to the logger.
62 // The one-argument version logs to infostream.
63 // The two-argument version accept a log level: error, action, info, or verbose.
64 int ModApiUtil::l_log(lua_State *L)
68 LogMessageLevel level = LMT_INFO;
69 if (lua_isnone(L, 2)) {
70 text = lua_tostring(L, 1);
73 std::string levelname = luaL_checkstring(L, 1);
74 text = luaL_checkstring(L, 2);
75 if(levelname == "error")
77 else if(levelname == "action")
79 else if(levelname == "verbose")
82 log_printline(level, text);
86 // setting_set(name, value)
87 int ModApiUtil::l_setting_set(lua_State *L)
90 const char *name = luaL_checkstring(L, 1);
91 const char *value = luaL_checkstring(L, 2);
92 g_settings->set(name, value);
97 int ModApiUtil::l_setting_get(lua_State *L)
100 const char *name = luaL_checkstring(L, 1);
102 std::string value = g_settings->get(name);
103 lua_pushstring(L, value.c_str());
104 } catch(SettingNotFoundException &e){
110 // setting_setbool(name)
111 int ModApiUtil::l_setting_setbool(lua_State *L)
113 NO_MAP_LOCK_REQUIRED;
114 const char *name = luaL_checkstring(L, 1);
115 bool value = lua_toboolean(L, 2);
116 g_settings->setBool(name, value);
120 // setting_getbool(name)
121 int ModApiUtil::l_setting_getbool(lua_State *L)
123 NO_MAP_LOCK_REQUIRED;
124 const char *name = luaL_checkstring(L, 1);
126 bool value = g_settings->getBool(name);
127 lua_pushboolean(L, value);
128 } catch(SettingNotFoundException &e){
135 int ModApiUtil::l_setting_save(lua_State *L)
137 NO_MAP_LOCK_REQUIRED;
138 if(g_settings_path != "")
139 g_settings->updateConfigFile(g_settings_path.c_str());
143 // parse_json(str[, nullvalue])
144 int ModApiUtil::l_parse_json(lua_State *L)
146 NO_MAP_LOCK_REQUIRED;
148 const char *jsonstr = luaL_checkstring(L, 1);
150 // Use passed nullvalue or default to nil
152 if (lua_isnone(L, nullindex)) {
154 nullindex = lua_gettop(L);
161 std::istringstream stream(jsonstr);
163 if (!reader.parse(stream, root)) {
164 errorstream << "Failed to parse json data "
165 << reader.getFormattedErrorMessages();
166 errorstream << "data: \"" << jsonstr << "\""
173 if (!push_json_value(L, root, nullindex)) {
174 errorstream << "Failed to parse json data, "
175 << "depth exceeds lua stack limit" << std::endl;
176 errorstream << "data: \"" << jsonstr << "\"" << std::endl;
182 // write_json(data[, styled]) -> string or nil and error message
183 int ModApiUtil::l_write_json(lua_State *L)
185 NO_MAP_LOCK_REQUIRED;
188 if (!lua_isnone(L, 2)) {
189 styled = lua_toboolean(L, 2);
195 read_json_value(L, root, 1);
196 } catch (SerializationError &e) {
198 lua_pushstring(L, e.what());
204 Json::StyledWriter writer;
205 out = writer.write(root);
207 Json::FastWriter writer;
208 out = writer.write(root);
210 lua_pushlstring(L, out.c_str(), out.size());
214 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
215 int ModApiUtil::l_get_dig_params(lua_State *L)
217 NO_MAP_LOCK_REQUIRED;
218 std::map<std::string, int> groups;
219 read_groups(L, 1, groups);
220 ToolCapabilities tp = read_tool_capabilities(L, 2);
221 if(lua_isnoneornil(L, 3))
222 push_dig_params(L, getDigParams(groups, &tp));
224 push_dig_params(L, getDigParams(groups, &tp,
225 luaL_checknumber(L, 3)));
229 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
230 int ModApiUtil::l_get_hit_params(lua_State *L)
232 NO_MAP_LOCK_REQUIRED;
233 std::map<std::string, int> groups;
234 read_groups(L, 1, groups);
235 ToolCapabilities tp = read_tool_capabilities(L, 2);
236 if(lua_isnoneornil(L, 3))
237 push_hit_params(L, getHitParams(groups, &tp));
239 push_hit_params(L, getHitParams(groups, &tp,
240 luaL_checknumber(L, 3)));
244 // get_password_hash(name, raw_password)
245 int ModApiUtil::l_get_password_hash(lua_State *L)
247 NO_MAP_LOCK_REQUIRED;
248 std::string name = luaL_checkstring(L, 1);
249 std::string raw_password = luaL_checkstring(L, 2);
250 std::string hash = translatePassword(name,
251 narrow_to_wide(raw_password));
252 lua_pushstring(L, hash.c_str());
257 int ModApiUtil::l_is_yes(lua_State *L)
259 NO_MAP_LOCK_REQUIRED;
261 lua_getglobal(L, "tostring"); // function to be called
262 lua_pushvalue(L, 1); // 1st argument
263 lua_call(L, 1, 1); // execute function
264 std::string str(lua_tostring(L, -1)); // get result
267 bool yes = is_yes(str);
268 lua_pushboolean(L, yes);
272 void ModApiUtil::Initialize(lua_State *L, int top)
277 API_FCT(setting_set);
278 API_FCT(setting_get);
279 API_FCT(setting_setbool);
280 API_FCT(setting_getbool);
281 API_FCT(setting_save);
286 API_FCT(get_dig_params);
287 API_FCT(get_hit_params);
289 API_FCT(get_password_hash);
294 void ModApiUtil::InitializeAsync(AsyncEngine& engine)
296 ASYNC_API_FCT(debug);
299 //ASYNC_API_FCT(setting_set);
300 ASYNC_API_FCT(setting_get);
301 //ASYNC_API_FCT(setting_setbool);
302 ASYNC_API_FCT(setting_getbool);
303 //ASYNC_API_FCT(setting_save);
305 ASYNC_API_FCT(parse_json);
307 ASYNC_API_FCT(is_yes);