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 "cpp_api/s_async.h"
30 #include "main.h" //required for g_settings, g_settings_path
31 #include "json/json.h"
34 // Writes a line to dstream
35 int ModApiUtil::l_debug(lua_State *L)
38 // Handle multiple parameters to behave like standard lua print()
39 int n = lua_gettop(L);
40 lua_getglobal(L, "tostring");
41 for (int i = 1; i <= n; i++) {
43 Call tostring(i-th argument).
44 This is what print() does, and it behaves a bit
45 differently from directly calling lua_tostring.
47 lua_pushvalue(L, -1); /* function to be called */
48 lua_pushvalue(L, i); /* value to print */
51 const char *s = lua_tolstring(L, -1, &len);
55 dstream << std::string(s, len);
63 // Writes a line to the logger.
64 // The one-argument version logs to infostream.
65 // The two-argument version accept a log level: error, action, info, or verbose.
66 int ModApiUtil::l_log(lua_State *L)
70 LogMessageLevel level = LMT_INFO;
71 if (lua_isnone(L, 2)) {
72 text = lua_tostring(L, 1);
75 std::string levelname = luaL_checkstring(L, 1);
76 text = luaL_checkstring(L, 2);
77 if(levelname == "error")
79 else if(levelname == "action")
81 else if(levelname == "verbose")
83 else if (levelname == "deprecated") {
84 log_deprecated(L,text);
89 log_printline(level, text);
93 // setting_set(name, value)
94 int ModApiUtil::l_setting_set(lua_State *L)
97 const char *name = luaL_checkstring(L, 1);
98 const char *value = luaL_checkstring(L, 2);
99 g_settings->set(name, value);
104 int ModApiUtil::l_setting_get(lua_State *L)
106 NO_MAP_LOCK_REQUIRED;
107 const char *name = luaL_checkstring(L, 1);
109 std::string value = g_settings->get(name);
110 lua_pushstring(L, value.c_str());
111 } catch(SettingNotFoundException &e){
117 // setting_setbool(name)
118 int ModApiUtil::l_setting_setbool(lua_State *L)
120 NO_MAP_LOCK_REQUIRED;
121 const char *name = luaL_checkstring(L, 1);
122 bool value = lua_toboolean(L, 2);
123 g_settings->setBool(name, value);
127 // setting_getbool(name)
128 int ModApiUtil::l_setting_getbool(lua_State *L)
130 NO_MAP_LOCK_REQUIRED;
131 const char *name = luaL_checkstring(L, 1);
133 bool value = g_settings->getBool(name);
134 lua_pushboolean(L, value);
135 } catch(SettingNotFoundException &e){
142 int ModApiUtil::l_setting_save(lua_State *L)
144 NO_MAP_LOCK_REQUIRED;
145 if(g_settings_path != "")
146 g_settings->updateConfigFile(g_settings_path.c_str());
150 // parse_json(str[, nullvalue])
151 int ModApiUtil::l_parse_json(lua_State *L)
153 NO_MAP_LOCK_REQUIRED;
155 const char *jsonstr = luaL_checkstring(L, 1);
157 // Use passed nullvalue or default to nil
159 if (lua_isnone(L, nullindex)) {
161 nullindex = lua_gettop(L);
168 std::istringstream stream(jsonstr);
170 if (!reader.parse(stream, root)) {
171 errorstream << "Failed to parse json data "
172 << reader.getFormattedErrorMessages();
173 errorstream << "data: \"" << jsonstr << "\""
180 if (!push_json_value(L, root, nullindex)) {
181 errorstream << "Failed to parse json data, "
182 << "depth exceeds lua stack limit" << std::endl;
183 errorstream << "data: \"" << jsonstr << "\"" << std::endl;
189 // write_json(data[, styled]) -> string or nil and error message
190 int ModApiUtil::l_write_json(lua_State *L)
192 NO_MAP_LOCK_REQUIRED;
195 if (!lua_isnone(L, 2)) {
196 styled = lua_toboolean(L, 2);
202 read_json_value(L, root, 1);
203 } catch (SerializationError &e) {
205 lua_pushstring(L, e.what());
211 Json::StyledWriter writer;
212 out = writer.write(root);
214 Json::FastWriter writer;
215 out = writer.write(root);
217 lua_pushlstring(L, out.c_str(), out.size());
221 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
222 int ModApiUtil::l_get_dig_params(lua_State *L)
224 NO_MAP_LOCK_REQUIRED;
225 std::map<std::string, int> groups;
226 read_groups(L, 1, groups);
227 ToolCapabilities tp = read_tool_capabilities(L, 2);
228 if(lua_isnoneornil(L, 3))
229 push_dig_params(L, getDigParams(groups, &tp));
231 push_dig_params(L, getDigParams(groups, &tp,
232 luaL_checknumber(L, 3)));
236 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
237 int ModApiUtil::l_get_hit_params(lua_State *L)
239 NO_MAP_LOCK_REQUIRED;
240 std::map<std::string, int> groups;
241 read_groups(L, 1, groups);
242 ToolCapabilities tp = read_tool_capabilities(L, 2);
243 if(lua_isnoneornil(L, 3))
244 push_hit_params(L, getHitParams(groups, &tp));
246 push_hit_params(L, getHitParams(groups, &tp,
247 luaL_checknumber(L, 3)));
251 // get_password_hash(name, raw_password)
252 int ModApiUtil::l_get_password_hash(lua_State *L)
254 NO_MAP_LOCK_REQUIRED;
255 std::string name = luaL_checkstring(L, 1);
256 std::string raw_password = luaL_checkstring(L, 2);
257 std::string hash = translatePassword(name,
258 narrow_to_wide(raw_password));
259 lua_pushstring(L, hash.c_str());
264 int ModApiUtil::l_is_yes(lua_State *L)
266 NO_MAP_LOCK_REQUIRED;
268 lua_getglobal(L, "tostring"); // function to be called
269 lua_pushvalue(L, 1); // 1st argument
270 lua_call(L, 1, 1); // execute function
271 std::string str(lua_tostring(L, -1)); // get result
274 bool yes = is_yes(str);
275 lua_pushboolean(L, yes);
279 int ModApiUtil::l_get_builtin_path(lua_State *L)
281 std::string path = porting::path_share + DIR_DELIM + "builtin";
282 lua_pushstring(L, path.c_str());
287 void ModApiUtil::Initialize(lua_State *L, int top)
292 API_FCT(setting_set);
293 API_FCT(setting_get);
294 API_FCT(setting_setbool);
295 API_FCT(setting_getbool);
296 API_FCT(setting_save);
301 API_FCT(get_dig_params);
302 API_FCT(get_hit_params);
304 API_FCT(get_password_hash);
308 API_FCT(get_builtin_path);
311 void ModApiUtil::InitializeAsync(AsyncEngine& engine)
313 ASYNC_API_FCT(debug);
316 //ASYNC_API_FCT(setting_set);
317 ASYNC_API_FCT(setting_get);
318 //ASYNC_API_FCT(setting_setbool);
319 ASYNC_API_FCT(setting_getbool);
320 //ASYNC_API_FCT(setting_save);
322 ASYNC_API_FCT(parse_json);
323 ASYNC_API_FCT(write_json);
325 ASYNC_API_FCT(is_yes);
327 ASYNC_API_FCT(get_builtin_path);