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"
25 #include "serialization.h"
26 #include "json/json.h"
35 // Writes a line to dstream
36 int ModApiUtil::l_debug(lua_State *L)
39 // Handle multiple parameters to behave like standard lua print()
40 int n = lua_gettop(L);
41 lua_getglobal(L, "tostring");
42 for (int i = 1; i <= n; i++) {
44 Call tostring(i-th argument).
45 This is what print() does, and it behaves a bit
46 differently from directly calling lua_tostring.
48 lua_pushvalue(L, -1); /* function to be called */
49 lua_pushvalue(L, i); /* value to print */
52 const char *s = lua_tolstring(L, -1, &len);
56 dstream << std::string(s, len);
64 // Writes a line to the logger.
65 // The one-argument version logs to infostream.
66 // The two-argument version accept a log level: error, action, info, or verbose.
67 int ModApiUtil::l_log(lua_State *L)
71 LogMessageLevel level = LMT_INFO;
72 if (lua_isnone(L, 2)) {
73 text = lua_tostring(L, 1);
76 std::string levelname = luaL_checkstring(L, 1);
77 text = luaL_checkstring(L, 2);
78 if(levelname == "error")
80 else if(levelname == "action")
82 else if(levelname == "verbose")
84 else if (levelname == "deprecated") {
85 log_deprecated(L,text);
90 log_printline(level, text);
94 // setting_set(name, value)
95 int ModApiUtil::l_setting_set(lua_State *L)
98 const char *name = luaL_checkstring(L, 1);
99 const char *value = luaL_checkstring(L, 2);
100 g_settings->set(name, value);
105 int ModApiUtil::l_setting_get(lua_State *L)
107 NO_MAP_LOCK_REQUIRED;
108 const char *name = luaL_checkstring(L, 1);
110 std::string value = g_settings->get(name);
111 lua_pushstring(L, value.c_str());
112 } catch(SettingNotFoundException &e){
118 // setting_setbool(name)
119 int ModApiUtil::l_setting_setbool(lua_State *L)
121 NO_MAP_LOCK_REQUIRED;
122 const char *name = luaL_checkstring(L, 1);
123 bool value = lua_toboolean(L, 2);
124 g_settings->setBool(name, value);
128 // setting_getbool(name)
129 int ModApiUtil::l_setting_getbool(lua_State *L)
131 NO_MAP_LOCK_REQUIRED;
132 const char *name = luaL_checkstring(L, 1);
134 bool value = g_settings->getBool(name);
135 lua_pushboolean(L, value);
136 } catch(SettingNotFoundException &e){
143 int ModApiUtil::l_setting_save(lua_State *L)
145 NO_MAP_LOCK_REQUIRED;
146 if(g_settings_path != "")
147 g_settings->updateConfigFile(g_settings_path.c_str());
151 // parse_json(str[, nullvalue])
152 int ModApiUtil::l_parse_json(lua_State *L)
154 NO_MAP_LOCK_REQUIRED;
156 const char *jsonstr = luaL_checkstring(L, 1);
158 // Use passed nullvalue or default to nil
160 if (lua_isnone(L, nullindex)) {
162 nullindex = lua_gettop(L);
169 std::istringstream stream(jsonstr);
171 if (!reader.parse(stream, root)) {
172 errorstream << "Failed to parse json data "
173 << reader.getFormattedErrorMessages();
174 errorstream << "data: \"" << jsonstr << "\""
181 if (!push_json_value(L, root, nullindex)) {
182 errorstream << "Failed to parse json data, "
183 << "depth exceeds lua stack limit" << std::endl;
184 errorstream << "data: \"" << jsonstr << "\"" << std::endl;
190 // write_json(data[, styled]) -> string or nil and error message
191 int ModApiUtil::l_write_json(lua_State *L)
193 NO_MAP_LOCK_REQUIRED;
196 if (!lua_isnone(L, 2)) {
197 styled = lua_toboolean(L, 2);
203 read_json_value(L, root, 1);
204 } catch (SerializationError &e) {
206 lua_pushstring(L, e.what());
212 Json::StyledWriter writer;
213 out = writer.write(root);
215 Json::FastWriter writer;
216 out = writer.write(root);
218 lua_pushlstring(L, out.c_str(), out.size());
222 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
223 int ModApiUtil::l_get_dig_params(lua_State *L)
225 NO_MAP_LOCK_REQUIRED;
226 std::map<std::string, int> groups;
227 read_groups(L, 1, groups);
228 ToolCapabilities tp = read_tool_capabilities(L, 2);
229 if(lua_isnoneornil(L, 3))
230 push_dig_params(L, getDigParams(groups, &tp));
232 push_dig_params(L, getDigParams(groups, &tp,
233 luaL_checknumber(L, 3)));
237 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
238 int ModApiUtil::l_get_hit_params(lua_State *L)
240 NO_MAP_LOCK_REQUIRED;
241 std::map<std::string, int> groups;
242 read_groups(L, 1, groups);
243 ToolCapabilities tp = read_tool_capabilities(L, 2);
244 if(lua_isnoneornil(L, 3))
245 push_hit_params(L, getHitParams(groups, &tp));
247 push_hit_params(L, getHitParams(groups, &tp,
248 luaL_checknumber(L, 3)));
252 // get_password_hash(name, raw_password)
253 int ModApiUtil::l_get_password_hash(lua_State *L)
255 NO_MAP_LOCK_REQUIRED;
256 std::string name = luaL_checkstring(L, 1);
257 std::string raw_password = luaL_checkstring(L, 2);
258 std::string hash = translatePassword(name, 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());
286 // compress(data, method, level)
287 int ModApiUtil::l_compress(lua_State *L)
290 const char *data = luaL_checklstring(L, 1, &size);
293 if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
294 level = luaL_checknumber(L, 3);
296 std::ostringstream os;
297 compressZlib(std::string(data, size), os, level);
299 std::string out = os.str();
301 lua_pushlstring(L, out.data(), out.size());
305 // decompress(data, method)
306 int ModApiUtil::l_decompress(lua_State *L)
309 const char * data = luaL_checklstring(L, 1, &size);
311 std::istringstream is(std::string(data, size));
312 std::ostringstream os;
313 decompressZlib(is, os);
315 std::string out = os.str();
317 lua_pushlstring(L, out.data(), out.size());
321 void ModApiUtil::Initialize(lua_State *L, int top)
326 API_FCT(setting_set);
327 API_FCT(setting_get);
328 API_FCT(setting_setbool);
329 API_FCT(setting_getbool);
330 API_FCT(setting_save);
335 API_FCT(get_dig_params);
336 API_FCT(get_hit_params);
338 API_FCT(get_password_hash);
342 API_FCT(get_builtin_path);
348 void ModApiUtil::InitializeAsync(AsyncEngine& engine)
350 ASYNC_API_FCT(debug);
353 //ASYNC_API_FCT(setting_set);
354 ASYNC_API_FCT(setting_get);
355 //ASYNC_API_FCT(setting_setbool);
356 ASYNC_API_FCT(setting_getbool);
357 //ASYNC_API_FCT(setting_save);
359 ASYNC_API_FCT(parse_json);
360 ASYNC_API_FCT(write_json);
362 ASYNC_API_FCT(is_yes);
364 ASYNC_API_FCT(get_builtin_path);
366 ASYNC_API_FCT(compress);
367 ASYNC_API_FCT(decompress);