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"
27 #include "cpp_api/s_security.h"
28 #include "areastore.h"
34 #include "util/auth.h"
38 // Writes a line to the logger.
39 // The one-argument version logs to infostream.
40 // The two-argument version accepts a log level.
41 // Either the special case "deprecated" for deprecation notices, or any specified in
42 // Logger::stringToLevel(name).
43 int ModApiUtil::l_log(lua_State *L)
47 LogLevel level = LL_NONE;
48 if (lua_isnone(L, 2)) {
49 text = luaL_checkstring(L, 1);
51 std::string name = luaL_checkstring(L, 1);
52 text = luaL_checkstring(L, 2);
53 if (name == "deprecated") {
54 log_deprecated(L, text);
57 level = Logger::stringToLevel(name);
58 if (level == LL_MAX) {
59 warningstream << "Tried to log at unknown level '" << name
60 << "'. Defaulting to \"none\"." << std::endl;
64 g_logger.log(level, text);
69 int ModApiUtil::l_get_us_time(lua_State *L)
72 lua_pushnumber(L, porting::getTimeUs());
76 #define CHECK_SECURE_SETTING(L, name) \
77 if (name.compare(0, 7, "secure.") == 0) {\
78 lua_pushliteral(L, "Attempt to set secure setting.");\
82 // setting_set(name, value)
83 int ModApiUtil::l_setting_set(lua_State *L)
86 std::string name = luaL_checkstring(L, 1);
87 std::string value = luaL_checkstring(L, 2);
88 CHECK_SECURE_SETTING(L, name);
89 g_settings->set(name, value);
94 int ModApiUtil::l_setting_get(lua_State *L)
97 const char *name = luaL_checkstring(L, 1);
99 std::string value = g_settings->get(name);
100 lua_pushstring(L, value.c_str());
101 } catch(SettingNotFoundException &e){
107 // setting_setbool(name)
108 int ModApiUtil::l_setting_setbool(lua_State *L)
110 NO_MAP_LOCK_REQUIRED;
111 std::string name = luaL_checkstring(L, 1);
112 bool value = lua_toboolean(L, 2);
113 CHECK_SECURE_SETTING(L, name);
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 // parse_json(str[, nullvalue])
142 int ModApiUtil::l_parse_json(lua_State *L)
144 NO_MAP_LOCK_REQUIRED;
146 const char *jsonstr = luaL_checkstring(L, 1);
148 // Use passed nullvalue or default to nil
150 if (lua_isnone(L, nullindex)) {
152 nullindex = lua_gettop(L);
159 std::istringstream stream(jsonstr);
161 if (!reader.parse(stream, root)) {
162 errorstream << "Failed to parse json data "
163 << reader.getFormattedErrorMessages();
164 errorstream << "data: \"" << jsonstr << "\""
171 if (!push_json_value(L, root, nullindex)) {
172 errorstream << "Failed to parse json data, "
173 << "depth exceeds lua stack limit" << std::endl;
174 errorstream << "data: \"" << jsonstr << "\"" << std::endl;
180 // write_json(data[, styled]) -> string or nil and error message
181 int ModApiUtil::l_write_json(lua_State *L)
183 NO_MAP_LOCK_REQUIRED;
186 if (!lua_isnone(L, 2)) {
187 styled = lua_toboolean(L, 2);
193 read_json_value(L, root, 1);
194 } catch (SerializationError &e) {
196 lua_pushstring(L, e.what());
202 Json::StyledWriter writer;
203 out = writer.write(root);
205 Json::FastWriter writer;
206 out = writer.write(root);
208 lua_pushlstring(L, out.c_str(), out.size());
212 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
213 int ModApiUtil::l_get_dig_params(lua_State *L)
215 NO_MAP_LOCK_REQUIRED;
216 std::map<std::string, int> groups;
217 read_groups(L, 1, groups);
218 ToolCapabilities tp = read_tool_capabilities(L, 2);
219 if(lua_isnoneornil(L, 3))
220 push_dig_params(L, getDigParams(groups, &tp));
222 push_dig_params(L, getDigParams(groups, &tp,
223 luaL_checknumber(L, 3)));
227 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
228 int ModApiUtil::l_get_hit_params(lua_State *L)
230 NO_MAP_LOCK_REQUIRED;
231 std::map<std::string, int> groups;
232 read_groups(L, 1, groups);
233 ToolCapabilities tp = read_tool_capabilities(L, 2);
234 if(lua_isnoneornil(L, 3))
235 push_hit_params(L, getHitParams(groups, &tp));
237 push_hit_params(L, getHitParams(groups, &tp,
238 luaL_checknumber(L, 3)));
242 // get_password_hash(name, raw_password)
243 int ModApiUtil::l_get_password_hash(lua_State *L)
245 NO_MAP_LOCK_REQUIRED;
246 std::string name = luaL_checkstring(L, 1);
247 std::string raw_password = luaL_checkstring(L, 2);
248 std::string hash = translatePassword(name, raw_password);
249 lua_pushstring(L, hash.c_str());
254 int ModApiUtil::l_is_yes(lua_State *L)
256 NO_MAP_LOCK_REQUIRED;
258 lua_getglobal(L, "tostring"); // function to be called
259 lua_pushvalue(L, 1); // 1st argument
260 lua_call(L, 1, 1); // execute function
261 std::string str(lua_tostring(L, -1)); // get result
264 bool yes = is_yes(str);
265 lua_pushboolean(L, yes);
269 int ModApiUtil::l_get_builtin_path(lua_State *L)
271 NO_MAP_LOCK_REQUIRED;
273 std::string path = porting::path_share + DIR_DELIM + "builtin";
274 lua_pushstring(L, path.c_str());
278 // compress(data, method, level)
279 int ModApiUtil::l_compress(lua_State *L)
281 NO_MAP_LOCK_REQUIRED;
284 const char *data = luaL_checklstring(L, 1, &size);
287 if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
288 level = luaL_checknumber(L, 3);
290 std::ostringstream os;
291 compressZlib(std::string(data, size), os, level);
293 std::string out = os.str();
295 lua_pushlstring(L, out.data(), out.size());
299 // decompress(data, method)
300 int ModApiUtil::l_decompress(lua_State *L)
302 NO_MAP_LOCK_REQUIRED;
305 const char *data = luaL_checklstring(L, 1, &size);
307 std::istringstream is(std::string(data, size));
308 std::ostringstream os;
309 decompressZlib(is, os);
311 std::string out = os.str();
313 lua_pushlstring(L, out.data(), out.size());
318 int ModApiUtil::l_mkdir(lua_State *L)
320 NO_MAP_LOCK_REQUIRED;
321 const char *path = luaL_checkstring(L, 1);
322 CHECK_SECURE_PATH_OPTIONAL(L, path);
323 lua_pushboolean(L, fs::CreateAllDirs(path));
327 // get_dir_list(path, is_dir)
328 int ModApiUtil::l_get_dir_list(lua_State *L)
330 NO_MAP_LOCK_REQUIRED;
331 const char *path = luaL_checkstring(L, 1);
332 short is_dir = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : -1;
334 CHECK_SECURE_PATH_OPTIONAL(L, path);
336 std::vector<fs::DirListNode> list = fs::GetDirListing(path);
341 for (size_t i = 0; i < list.size(); i++) {
342 if (is_dir == -1 || is_dir == list[i].dir) {
343 lua_pushstring(L, list[i].name.c_str());
344 lua_rawseti(L, -2, ++index);
351 int ModApiUtil::l_request_insecure_environment(lua_State *L)
353 NO_MAP_LOCK_REQUIRED;
354 if (!ScriptApiSecurity::isSecure(L)) {
355 lua_getglobal(L, "_G");
358 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
359 if (!lua_isstring(L, -1)) {
363 const char *mod_name = lua_tostring(L, -1);
364 std::string trusted_mods = g_settings->get("secure.trusted_mods");
365 std::vector<std::string> mod_list = str_split(trusted_mods, ',');
366 if (std::find(mod_list.begin(), mod_list.end(), mod_name) == mod_list.end()) {
370 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
375 void ModApiUtil::Initialize(lua_State *L, int top)
379 API_FCT(get_us_time);
381 API_FCT(setting_set);
382 API_FCT(setting_get);
383 API_FCT(setting_setbool);
384 API_FCT(setting_getbool);
385 API_FCT(setting_save);
390 API_FCT(get_dig_params);
391 API_FCT(get_hit_params);
393 API_FCT(get_password_hash);
397 API_FCT(get_builtin_path);
403 API_FCT(get_dir_list);
405 API_FCT(request_insecure_environment);
408 void ModApiUtil::InitializeAsync(AsyncEngine& engine)
412 ASYNC_API_FCT(get_us_time);
414 //ASYNC_API_FCT(setting_set);
415 ASYNC_API_FCT(setting_get);
416 //ASYNC_API_FCT(setting_setbool);
417 ASYNC_API_FCT(setting_getbool);
418 //ASYNC_API_FCT(setting_save);
420 ASYNC_API_FCT(parse_json);
421 ASYNC_API_FCT(write_json);
423 ASYNC_API_FCT(is_yes);
425 ASYNC_API_FCT(get_builtin_path);
427 ASYNC_API_FCT(compress);
428 ASYNC_API_FCT(decompress);
430 ASYNC_API_FCT(mkdir);
431 ASYNC_API_FCT(get_dir_list);