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
31 // Writes a line to dstream
32 int ModApiUtil::l_debug(lua_State *L)
35 // Handle multiple parameters to behave like standard lua print()
36 int n = lua_gettop(L);
37 lua_getglobal(L, "tostring");
38 for (int i = 1; i <= n; i++) {
40 Call tostring(i-th argument).
41 This is what print() does, and it behaves a bit
42 differently from directly calling lua_tostring.
44 lua_pushvalue(L, -1); /* function to be called */
45 lua_pushvalue(L, i); /* value to print */
47 const char *s = lua_tostring(L, -1);
59 // Writes a line to the logger.
60 // The one-argument version logs to infostream.
61 // The two-argument version accept a log level: error, action, info, or verbose.
62 int ModApiUtil::l_log(lua_State *L)
66 LogMessageLevel level = LMT_INFO;
67 if (lua_isnone(L, 2)) {
68 text = lua_tostring(L, 1);
71 std::string levelname = luaL_checkstring(L, 1);
72 text = luaL_checkstring(L, 2);
73 if(levelname == "error")
75 else if(levelname == "action")
77 else if(levelname == "verbose")
80 log_printline(level, text);
84 // setting_set(name, value)
85 int ModApiUtil::l_setting_set(lua_State *L)
88 const char *name = luaL_checkstring(L, 1);
89 const char *value = luaL_checkstring(L, 2);
90 g_settings->set(name, value);
95 int ModApiUtil::l_setting_get(lua_State *L)
98 const char *name = luaL_checkstring(L, 1);
100 std::string value = g_settings->get(name);
101 lua_pushstring(L, value.c_str());
102 } catch(SettingNotFoundException &e){
108 // setting_setbool(name)
109 int ModApiUtil::l_setting_setbool(lua_State *L)
111 NO_MAP_LOCK_REQUIRED;
112 const char *name = luaL_checkstring(L, 1);
113 bool value = lua_toboolean(L, 2);
114 g_settings->setBool(name, value);
118 // setting_getbool(name)
119 int ModApiUtil::l_setting_getbool(lua_State *L)
121 NO_MAP_LOCK_REQUIRED;
122 const char *name = luaL_checkstring(L, 1);
124 bool value = g_settings->getBool(name);
125 lua_pushboolean(L, value);
126 } catch(SettingNotFoundException &e){
133 int ModApiUtil::l_setting_save(lua_State *L)
135 NO_MAP_LOCK_REQUIRED;
136 if(g_settings_path != "")
137 g_settings->updateConfigFile(g_settings_path.c_str());
141 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
142 int ModApiUtil::l_get_dig_params(lua_State *L)
144 NO_MAP_LOCK_REQUIRED;
145 std::map<std::string, int> groups;
146 read_groups(L, 1, groups);
147 ToolCapabilities tp = read_tool_capabilities(L, 2);
148 if(lua_isnoneornil(L, 3))
149 push_dig_params(L, getDigParams(groups, &tp));
151 push_dig_params(L, getDigParams(groups, &tp,
152 luaL_checknumber(L, 3)));
156 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
157 int ModApiUtil::l_get_hit_params(lua_State *L)
159 NO_MAP_LOCK_REQUIRED;
160 std::map<std::string, int> groups;
161 read_groups(L, 1, groups);
162 ToolCapabilities tp = read_tool_capabilities(L, 2);
163 if(lua_isnoneornil(L, 3))
164 push_hit_params(L, getHitParams(groups, &tp));
166 push_hit_params(L, getHitParams(groups, &tp,
167 luaL_checknumber(L, 3)));
171 // get_password_hash(name, raw_password)
172 int ModApiUtil::l_get_password_hash(lua_State *L)
174 NO_MAP_LOCK_REQUIRED;
175 std::string name = luaL_checkstring(L, 1);
176 std::string raw_password = luaL_checkstring(L, 2);
177 std::string hash = translatePassword(name,
178 narrow_to_wide(raw_password));
179 lua_pushstring(L, hash.c_str());
183 void ModApiUtil::Initialize(lua_State *L, int top)
188 API_FCT(setting_set);
189 API_FCT(setting_get);
190 API_FCT(setting_setbool);
191 API_FCT(setting_getbool);
192 API_FCT(setting_save);
194 API_FCT(get_dig_params);
195 API_FCT(get_hit_params);
197 API_FCT(get_password_hash);