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"
28 #include "main.h" //required for g_settings, g_settings_path
29 #include "json/json.h"
32 // Writes a line to dstream
33 int ModApiUtil::l_debug(lua_State *L)
36 // Handle multiple parameters to behave like standard lua print()
37 int n = lua_gettop(L);
38 lua_getglobal(L, "tostring");
39 for (int i = 1; i <= n; i++) {
41 Call tostring(i-th argument).
42 This is what print() does, and it behaves a bit
43 differently from directly calling lua_tostring.
45 lua_pushvalue(L, -1); /* function to be called */
46 lua_pushvalue(L, i); /* value to print */
48 const char *s = lua_tostring(L, -1);
60 // Writes a line to the logger.
61 // The one-argument version logs to infostream.
62 // The two-argument version accept a log level: error, action, info, or verbose.
63 int ModApiUtil::l_log(lua_State *L)
67 LogMessageLevel level = LMT_INFO;
68 if (lua_isnone(L, 2)) {
69 text = lua_tostring(L, 1);
72 std::string levelname = luaL_checkstring(L, 1);
73 text = luaL_checkstring(L, 2);
74 if(levelname == "error")
76 else if(levelname == "action")
78 else if(levelname == "verbose")
81 log_printline(level, text);
85 // setting_set(name, value)
86 int ModApiUtil::l_setting_set(lua_State *L)
89 const char *name = luaL_checkstring(L, 1);
90 const char *value = luaL_checkstring(L, 2);
91 g_settings->set(name, value);
96 int ModApiUtil::l_setting_get(lua_State *L)
99 const char *name = luaL_checkstring(L, 1);
101 std::string value = g_settings->get(name);
102 lua_pushstring(L, value.c_str());
103 } catch(SettingNotFoundException &e){
109 // setting_setbool(name)
110 int ModApiUtil::l_setting_setbool(lua_State *L)
112 NO_MAP_LOCK_REQUIRED;
113 const char *name = luaL_checkstring(L, 1);
114 bool value = lua_toboolean(L, 2);
115 g_settings->setBool(name, value);
119 // setting_getbool(name)
120 int ModApiUtil::l_setting_getbool(lua_State *L)
122 NO_MAP_LOCK_REQUIRED;
123 const char *name = luaL_checkstring(L, 1);
125 bool value = g_settings->getBool(name);
126 lua_pushboolean(L, value);
127 } catch(SettingNotFoundException &e){
134 int ModApiUtil::l_setting_save(lua_State *L)
136 NO_MAP_LOCK_REQUIRED;
137 if(g_settings_path != "")
138 g_settings->updateConfigFile(g_settings_path.c_str());
142 // parse_json(str[, nullvalue])
143 int ModApiUtil::l_parse_json(lua_State *L)
145 NO_MAP_LOCK_REQUIRED;
147 const char *jsonstr = luaL_checkstring(L, 1);
149 // Use passed nullvalue or default to nil
151 if (lua_isnone(L, nullindex)) {
153 nullindex = lua_gettop(L);
160 std::istringstream stream(jsonstr);
162 if (!reader.parse(stream, root)) {
163 errorstream << "Failed to parse json data "
164 << reader.getFormattedErrorMessages();
165 errorstream << "data: \"" << jsonstr << "\""
172 if (!push_json_value(L, root, nullindex)) {
173 errorstream << "Failed to parse json data, "
174 << "depth exceeds lua stack limit" << std::endl;
175 errorstream << "data: \"" << jsonstr << "\"" << std::endl;
181 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
182 int ModApiUtil::l_get_dig_params(lua_State *L)
184 NO_MAP_LOCK_REQUIRED;
185 std::map<std::string, int> groups;
186 read_groups(L, 1, groups);
187 ToolCapabilities tp = read_tool_capabilities(L, 2);
188 if(lua_isnoneornil(L, 3))
189 push_dig_params(L, getDigParams(groups, &tp));
191 push_dig_params(L, getDigParams(groups, &tp,
192 luaL_checknumber(L, 3)));
196 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
197 int ModApiUtil::l_get_hit_params(lua_State *L)
199 NO_MAP_LOCK_REQUIRED;
200 std::map<std::string, int> groups;
201 read_groups(L, 1, groups);
202 ToolCapabilities tp = read_tool_capabilities(L, 2);
203 if(lua_isnoneornil(L, 3))
204 push_hit_params(L, getHitParams(groups, &tp));
206 push_hit_params(L, getHitParams(groups, &tp,
207 luaL_checknumber(L, 3)));
211 // get_password_hash(name, raw_password)
212 int ModApiUtil::l_get_password_hash(lua_State *L)
214 NO_MAP_LOCK_REQUIRED;
215 std::string name = luaL_checkstring(L, 1);
216 std::string raw_password = luaL_checkstring(L, 2);
217 std::string hash = translatePassword(name,
218 narrow_to_wide(raw_password));
219 lua_pushstring(L, hash.c_str());
223 void ModApiUtil::Initialize(lua_State *L, int top)
228 API_FCT(setting_set);
229 API_FCT(setting_get);
230 API_FCT(setting_setbool);
231 API_FCT(setting_getbool);
232 API_FCT(setting_save);
236 API_FCT(get_dig_params);
237 API_FCT(get_hit_params);
239 API_FCT(get_password_hash);