]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/lua_api/l_util.cpp
Refactor Script API's log_deprecated
[minetest.git] / src / script / lua_api / l_util.cpp
index ef4df8182c87881d66e44cf76f304594b70a7c71..ae3e5df3d2ae70d64b447f0f9c474bf5211c4fdb 100644 (file)
@@ -59,7 +59,7 @@ int ModApiUtil::l_log(lua_State *L)
                std::string name = luaL_checkstring(L, 1);
                text = luaL_checkstring(L, 2);
                if (name == "deprecated") {
-                       log_deprecated(L, text);
+                       log_deprecated(L, text, 2);
                        return 0;
                }
                level = Logger::stringToLevel(name);
@@ -135,7 +135,7 @@ int ModApiUtil::l_write_json(lua_State *L)
 
        bool styled = false;
        if (!lua_isnone(L, 2)) {
-               styled = lua_toboolean(L, 2);
+               styled = readParam<bool>(L, 2);
                lua_pop(L, 1);
        }
 
@@ -158,18 +158,14 @@ int ModApiUtil::l_write_json(lua_State *L)
        return 1;
 }
 
-// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
+// get_dig_params(groups, tool_capabilities)
 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);
-       if(lua_isnoneornil(L, 3))
-               push_dig_params(L, getDigParams(groups, &tp));
-       else
-               push_dig_params(L, getDigParams(groups, &tp,
-                                       luaL_checknumber(L, 3)));
+       push_dig_params(L, getDigParams(groups, &tp));
        return 1;
 }
 
@@ -183,8 +179,7 @@ int ModApiUtil::l_get_hit_params(lua_State *L)
        if(lua_isnoneornil(L, 3))
                push_hit_params(L, getHitParams(groups, &tp));
        else
-               push_hit_params(L, getHitParams(groups, &tp,
-                                       luaL_checknumber(L, 3)));
+               push_hit_params(L, getHitParams(groups, &tp, readParam<float>(L, 3)));
        return 1;
 }
 
@@ -236,7 +231,7 @@ int ModApiUtil::l_is_yes(lua_State *L)
        lua_getglobal(L, "tostring"); // function to be called
        lua_pushvalue(L, 1); // 1st argument
        lua_call(L, 1, 1); // execute function
-       std::string str(lua_tostring(L, -1)); // get result
+       std::string str = readParam<std::string>(L, -1); // get result
        lua_pop(L, 1);
 
        bool yes = is_yes(str);
@@ -244,6 +239,15 @@ int ModApiUtil::l_is_yes(lua_State *L)
        return 1;
 }
 
+// is_nan(arg)
+int ModApiUtil::l_is_nan(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+
+       lua_pushboolean(L, isNaN(L, 1));
+       return 1;
+}
+
 // get_builtin_path()
 int ModApiUtil::l_get_builtin_path(lua_State *L)
 {
@@ -265,7 +269,7 @@ int ModApiUtil::l_compress(lua_State *L)
 
        int level = -1;
        if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
-               level = luaL_checknumber(L, 3);
+               level = readParam<float>(L, 3);
 
        std::ostringstream os;
        compressZlib(std::string(data, size), os, level);
@@ -338,7 +342,7 @@ int ModApiUtil::l_get_dir_list(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        const char *path = luaL_checkstring(L, 1);
        bool list_all = !lua_isboolean(L, 2); // if its not a boolean list all
-       bool list_dirs = lua_toboolean(L, 2); // true: list dirs, false: list files
+       bool list_dirs = readParam<bool>(L, 2); // true: list dirs, false: list files
 
        CHECK_SECURE_PATH(L, path, false);
 
@@ -357,6 +361,23 @@ int ModApiUtil::l_get_dir_list(lua_State *L)
        return 1;
 }
 
+// safe_file_write(path, content)
+int ModApiUtil::l_safe_file_write(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       const char *path = luaL_checkstring(L, 1);
+       size_t size;
+       const char *content = luaL_checklstring(L, 2, &size);
+
+       CHECK_SECURE_PATH(L, path, true);
+
+       bool ret = fs::safeWriteToFile(path, std::string(content, size));
+       lua_pushboolean(L, ret);
+
+       return 1;
+}
+
+// request_insecure_environment()
 int ModApiUtil::l_request_insecure_environment(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
@@ -389,7 +410,7 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L)
        }
 
        // Check secure.trusted_mods
-       const char *mod_name = lua_tostring(L, -1);
+       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)),
@@ -430,7 +451,7 @@ int ModApiUtil::l_sha1(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        size_t size;
        const char *data = luaL_checklstring(L, 1, &size);
-       bool hex = !lua_isboolean(L, 2) || !lua_toboolean(L, 2);
+       bool hex = !lua_isboolean(L, 2) || !readParam<bool>(L, 2);
 
        // Compute actual checksum of data
        std::string data_sha1;
@@ -468,6 +489,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
        API_FCT(get_password_hash);
 
        API_FCT(is_yes);
+       API_FCT(is_nan);
 
        API_FCT(get_builtin_path);
 
@@ -476,6 +498,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
 
        API_FCT(mkdir);
        API_FCT(get_dir_list);
+       API_FCT(safe_file_write);
 
        API_FCT(request_insecure_environment);
 
@@ -499,6 +522,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
        API_FCT(write_json);
 
        API_FCT(is_yes);
+       API_FCT(is_nan);
 
        API_FCT(compress);
        API_FCT(decompress);