]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_util.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / script / lua_api / l_util.cpp
index d575eb6037017e7a6ea0d73501fc58d1fb548922..fa749c2e5104ce6174bc7f854e05164d3e94d5ef 100644 (file)
@@ -41,7 +41,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/hex.h"
 #include "util/sha1.h"
 #include "util/png.h"
-#include <algorithm>
 #include <cstdio>
 
 // log([level,] text)
@@ -160,28 +159,33 @@ int ModApiUtil::l_write_json(lua_State *L)
        return 1;
 }
 
-// get_dig_params(groups, tool_capabilities)
+// get_dig_params(groups, tool_capabilities[, wear])
 int ModApiUtil::l_get_dig_params(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        ItemGroupList groups;
        read_groups(L, 1, groups);
        ToolCapabilities tp = read_tool_capabilities(L, 2);
-       push_dig_params(L, getDigParams(groups, &tp));
+       if (lua_isnoneornil(L, 3)) {
+               push_dig_params(L, getDigParams(groups, &tp));
+       } else {
+               u16 wear = readParam<int>(L, 3);
+               push_dig_params(L, getDigParams(groups, &tp, wear));
+       }
        return 1;
 }
 
-// get_hit_params(groups, tool_capabilities[, time_from_last_punch])
+// get_hit_params(groups, tool_capabilities[, time_from_last_punch, [, wear]])
 int ModApiUtil::l_get_hit_params(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        std::unordered_map<std::string, int> groups;
        read_groups(L, 1, groups);
        ToolCapabilities tp = read_tool_capabilities(L, 2);
-       if(lua_isnoneornil(L, 3))
-               push_hit_params(L, getHitParams(groups, &tp));
-       else
-               push_hit_params(L, getHitParams(groups, &tp, readParam<float>(L, 3)));
+       float time_from_last_punch = readParam<float>(L, 3, 1000000);
+       int wear = readParam<int>(L, 4, 0);
+       push_hit_params(L, getHitParams(groups, &tp,
+               time_from_last_punch, wear));
        return 1;
 }
 
@@ -344,6 +348,49 @@ int ModApiUtil::l_mkdir(lua_State *L)
        return 1;
 }
 
+// rmdir(path, recursive)
+int ModApiUtil::l_rmdir(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       const char *path = luaL_checkstring(L, 1);
+       CHECK_SECURE_PATH(L, path, true);
+
+       bool recursive = readParam<bool>(L, 2, false);
+
+       if (recursive)
+               lua_pushboolean(L, fs::RecursiveDelete(path));
+       else
+               lua_pushboolean(L, fs::DeleteSingleFileOrEmptyDirectory(path));
+
+       return 1;
+}
+
+// cpdir(source, destination)
+int ModApiUtil::l_cpdir(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       const char *source = luaL_checkstring(L, 1);
+       const char *destination = luaL_checkstring(L, 2);
+       CHECK_SECURE_PATH(L, source, false);
+       CHECK_SECURE_PATH(L, destination, true);
+
+       lua_pushboolean(L, fs::CopyDir(source, destination));
+       return 1;
+}
+
+// mpdir(source, destination)
+int ModApiUtil::l_mvdir(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       const char *source = luaL_checkstring(L, 1);
+       const char *destination = luaL_checkstring(L, 2);
+       CHECK_SECURE_PATH(L, source, true);
+       CHECK_SECURE_PATH(L, destination, true);
+
+       lua_pushboolean(L, fs::MoveDir(source, destination));
+       return 1;
+}
+
 // get_dir_list(path, is_dir)
 int ModApiUtil::l_get_dir_list(lua_State *L)
 {
@@ -396,36 +443,7 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L)
                return 1;
        }
 
-       // We have to make sure that this function is being called directly by
-       // a mod, otherwise a malicious mod could override this function and
-       // steal its return value.
-       lua_Debug info;
-       // Make sure there's only one item below this function on the stack...
-       if (lua_getstack(L, 2, &info)) {
-               return 0;
-       }
-       FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
-       FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
-       // ...and that that item is the main file scope.
-       if (strcmp(info.what, "main") != 0) {
-               return 0;
-       }
-
-       // Get mod name
-       lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
-       if (!lua_isstring(L, -1)) {
-               return 0;
-       }
-
-       // Check secure.trusted_mods
-       std::string mod_name = readParam<std::string>(L, -1);
-       std::string trusted_mods = g_settings->get("secure.trusted_mods");
-       trusted_mods.erase(std::remove_if(trusted_mods.begin(),
-                       trusted_mods.end(), static_cast<int(*)(int)>(&std::isspace)),
-                       trusted_mods.end());
-       std::vector<std::string> mod_list = str_split(trusted_mods, ',');
-       if (std::find(mod_list.begin(), mod_list.end(), mod_name) ==
-                       mod_list.end()) {
+       if (!ScriptApiSecurity::checkWhitelisted(L, "secure.trusted_mods")) {
                return 0;
        }
 
@@ -583,6 +601,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
        API_FCT(decompress);
 
        API_FCT(mkdir);
+       API_FCT(rmdir);
+       API_FCT(cpdir);
+       API_FCT(mvdir);
        API_FCT(get_dir_list);
        API_FCT(safe_file_write);
 
@@ -651,7 +672,13 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
        API_FCT(decompress);
 
        API_FCT(mkdir);
+       API_FCT(rmdir);
+       API_FCT(cpdir);
+       API_FCT(mvdir);
        API_FCT(get_dir_list);
+       API_FCT(safe_file_write);
+
+       API_FCT(request_insecure_environment);
 
        API_FCT(encode_base64);
        API_FCT(decode_base64);
@@ -661,6 +688,8 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
        API_FCT(colorspec_to_colorstring);
        API_FCT(colorspec_to_bytes);
 
+       API_FCT(encode_png);
+
        API_FCT(get_last_run_mod);
        API_FCT(set_last_run_mod);