]> git.lizzy.rs Git - minetest.git/commitdiff
ContentDB: Add overwrite dialog when content is already installed (#10768)
authorrubenwardy <rw@rubenwardy.com>
Mon, 4 Jan 2021 15:18:31 +0000 (15:18 +0000)
committerGitHub <noreply@github.com>
Mon, 4 Jan 2021 15:18:31 +0000 (15:18 +0000)
builtin/mainmenu/dlg_contentstore.lua
doc/menu_lua_api.txt
src/script/lua_api/l_mainmenu.cpp
src/script/lua_api/l_mainmenu.h

index 7f6b4b7e446076c68730b5cdf9237cbd629ad2e3..b5f71e753f171200ddf0581821a240e91bd8c8e6 100644 (file)
@@ -437,12 +437,53 @@ function install_dialog.create(package, raw_deps)
        install_dialog.package = package
        install_dialog.raw_deps = raw_deps
        install_dialog.will_install_deps = true
-       return dialog_create("package_view",
+       return dialog_create("install_dialog",
                        install_dialog.get_formspec,
                        install_dialog.handle_submit,
                        nil)
 end
 
+
+local confirm_overwrite = {}
+function confirm_overwrite.get_formspec()
+       local package = confirm_overwrite.package
+
+       return "size[11.5,4.5,true]" ..
+                       "label[2,2;" ..
+                       fgettext("\"$1\" already exists. Would you like to overwrite it?", package.name) .. "]"..
+                       "style[install;bgcolor=red]" ..
+                       "button[3.25,3.5;2.5,0.5;install;" .. fgettext("Overwrite") .. "]" ..
+                       "button[5.75,3.5;2.5,0.5;cancel;" .. fgettext("Cancel") .. "]"
+end
+
+function confirm_overwrite.handle_submit(this, fields)
+       if fields.cancel then
+               this:delete()
+               return true
+       end
+
+       if fields.install then
+               this:delete()
+               confirm_overwrite.callback()
+               return true
+       end
+
+       return false
+end
+
+function confirm_overwrite.create(package, callback)
+       assert(type(package) == "table")
+       assert(type(callback) == "function")
+
+       confirm_overwrite.package = package
+       confirm_overwrite.callback = callback
+       return dialog_create("confirm_overwrite",
+               confirm_overwrite.get_formspec,
+               confirm_overwrite.handle_submit,
+               nil)
+end
+
+
 local function get_file_extension(path)
        local parts = path:split(".")
        return parts[#parts]
@@ -858,14 +899,37 @@ function store.handle_submit(this, fields)
                assert(package)
 
                if fields["install_" .. i] then
-                       local deps = get_raw_dependencies(package)
-                       if deps and has_hard_deps(deps) then
-                               local dlg = install_dialog.create(package, deps)
+                       local install_parent
+                       if package.type == "mod" then
+                               install_parent = core.get_modpath()
+                       elseif package.type == "game" then
+                               install_parent = core.get_gamepath()
+                       elseif package.type == "txp" then
+                               install_parent = core.get_texturepath()
+                       else
+                               error("Unknown package type: " .. package.type)
+                       end
+
+
+                       local function on_confirm()
+                               local deps = get_raw_dependencies(package)
+                               if deps and has_hard_deps(deps) then
+                                       local dlg = install_dialog.create(package, deps)
+                                       dlg:set_parent(this)
+                                       this:hide()
+                                       dlg:show()
+                               else
+                                       queue_download(package)
+                               end
+                       end
+
+                       if not package.path and core.is_dir(install_parent .. DIR_DELIM .. package.name) then
+                               local dlg = confirm_overwrite.create(package, on_confirm)
                                dlg:set_parent(this)
                                this:hide()
                                dlg:show()
                        else
-                               queue_download(package)
+                               on_confirm()
                        end
 
                        return true
index 8908552d5bab6fca7b633b2e5f307bcd8037facc..1bcf697e99814dd0d756e1e03d6d96ca13eb2f6b 100644 (file)
@@ -67,6 +67,8 @@ core.copy_dir(source,destination,keep_soure) (possible in async calls)
 ^ destination folder
 ^ keep_source DEFAULT true --> if set to false source is deleted after copying
 ^ returns true/false
+core.is_dir(path) (possible in async calls)
+^ returns true if path is a valid dir
 core.extract_zip(zipfile,destination) [unzip within path required]
 ^ zipfile to extract
 ^ destination folder to extract to
index 0b0b2de3b484bce64f39aa71409f58cf2fd8ec11..5070ec7d424908266be182e6917cdf0545ea4d88 100644 (file)
@@ -803,6 +803,15 @@ int ModApiMainMenu::l_copy_dir(lua_State *L)
        return 1;
 }
 
+/******************************************************************************/
+int ModApiMainMenu::l_is_dir(lua_State *L)
+{
+       const char *path = luaL_checkstring(L, 1);
+
+       lua_pushboolean(L, fs::IsDir(path));
+       return 1;
+}
+
 /******************************************************************************/
 int ModApiMainMenu::l_extract_zip(lua_State *L)
 {
@@ -1139,6 +1148,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
        API_FCT(create_dir);
        API_FCT(delete_dir);
        API_FCT(copy_dir);
+       API_FCT(is_dir);
        API_FCT(extract_zip);
        API_FCT(may_modify_path);
        API_FCT(get_mainmenu_path);
@@ -1172,6 +1182,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
        API_FCT(create_dir);
        API_FCT(delete_dir);
        API_FCT(copy_dir);
+       API_FCT(is_dir);
        //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
        API_FCT(may_modify_path);
        API_FCT(download_file);
index faa2bf273c07f5d8b399ae1e6fcd6ba354207043..0b02ed8920d0263d955885a618a53e9dd03ccb00 100644 (file)
@@ -132,6 +132,8 @@ class ModApiMainMenu: public ModApiBase
 
        static int l_copy_dir(lua_State *L);
 
+       static int l_is_dir(lua_State *L);
+
        static int l_extract_zip(lua_State *L);
 
        static int l_may_modify_path(lua_State *L);