From caa43771f6baffafc231fa90ad528af5c13f33e1 Mon Sep 17 00:00:00 2001 From: ToMe25 <38815969+ToMe25@users.noreply.github.com> Date: Mon, 27 Jun 2022 19:18:40 +0200 Subject: [PATCH 1/3] Improve import.lua logging --- .../item/FileImportExportCardItemDevice.java | 5 +++++ src/main/scripts/bin/import.lua | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/li/cil/oc2/common/bus/device/rpc/item/FileImportExportCardItemDevice.java b/src/main/java/li/cil/oc2/common/bus/device/rpc/item/FileImportExportCardItemDevice.java index 4411d0a1..4a33dc9b 100644 --- a/src/main/java/li/cil/oc2/common/bus/device/rpc/item/FileImportExportCardItemDevice.java +++ b/src/main/java/li/cil/oc2/common/bus/device/rpc/item/FileImportExportCardItemDevice.java @@ -224,6 +224,11 @@ public final class FileImportExportCardItemDevice extends AbstractItemRPCDevice @Nullable @Callback(name = BEGIN_IMPORT_FILE) public ImportedFileInfo beginImportFile() { + if (state == State.IMPORT_CANCELED) { + reset(); + throw new IllegalStateException("import was canceled"); + } + if (state != State.IMPORT_REQUESTED) { throw new IllegalStateException("invalid state"); } diff --git a/src/main/scripts/bin/import.lua b/src/main/scripts/bin/import.lua index dd911934..ecf5e745 100644 --- a/src/main/scripts/bin/import.lua +++ b/src/main/scripts/bin/import.lua @@ -11,13 +11,24 @@ end device:reset() if not device:requestImportFile() then - io.write("No users present to request file from.\n") + io.stderr:write("No users present to request file from.\n") + os.exit(1) +end + +local function error_handler(err) + if err:match("import was canceled$") then + io.stderr:write("Import was calceled by the user.\n") + else + io.stderr:write(debug.traceback(err, 2)) + io.stderr:write("\n") + end os.exit(1) end local name, size while true do - local info = device:beginImportFile() + local _, info = xpcall(device.beginImportFile, error_handler, device) + if info then name = arg[1] or info.name or "imported" size = info.size @@ -50,7 +61,7 @@ while file_exists(name) do end end -io.write("Importing ") +io.write("Importing " .. name) io.flush() local file = assert(io.open(name, "wb")) From c55e8dc9ebe33fa94014389e56ea67566849194b Mon Sep 17 00:00:00 2001 From: ToMe25 <38815969+ToMe25@users.noreply.github.com> Date: Tue, 28 Jun 2022 18:33:47 +0200 Subject: [PATCH 2/3] Remove import override option for directories --- src/main/scripts/bin/import.lua | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/scripts/bin/import.lua b/src/main/scripts/bin/import.lua index ecf5e745..e4ba1f3e 100644 --- a/src/main/scripts/bin/import.lua +++ b/src/main/scripts/bin/import.lua @@ -46,8 +46,26 @@ local function file_exists(path) end end +local function is_dir(path) + if not file_exists(path) then + return false + end + + local f = io.open(path, "r") + local _, _, code = f:read(1) + f:close() + return code == 21 +end + while file_exists(name) do - io.write("File '" .. name .. "' exists. [O]verwrite/[U]se another name/[C]ancel? ") + io.write("File '" .. name .. "' exists. ") + + local is_dir = is_dir(name) + if not is_dir then + io.write("[O]verwrite/") + end + + io.write("[U]se another name/[C]ancel? ") io.flush() local choice = io.read() if not choice or choice == "" or choice == "c" or choice == "C" then @@ -56,8 +74,11 @@ while file_exists(name) do io.write("Enter new name: ") io.flush() name = io.read() - else + elseif not is_dir and (choice == "o" or choice == "O") then break + else + io.stderr:write("Invalid option: " .. choice .. "\n") + os.exit(1) end end From 32c770fe3a45b935af3bf9f039e1428fb4981bdc Mon Sep 17 00:00:00 2001 From: ToMe25 <38815969+ToMe25@users.noreply.github.com> Date: Wed, 29 Jun 2022 16:51:40 +0200 Subject: [PATCH 3/3] Small import.lua fixes * Make override check more clear by giving var and method a different name * Fix typo in input canceled message --- src/main/scripts/bin/import.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/scripts/bin/import.lua b/src/main/scripts/bin/import.lua index e4ba1f3e..457f17c5 100644 --- a/src/main/scripts/bin/import.lua +++ b/src/main/scripts/bin/import.lua @@ -17,7 +17,7 @@ end local function error_handler(err) if err:match("import was canceled$") then - io.stderr:write("Import was calceled by the user.\n") + io.stderr:write("Import was canceled by the user.\n") else io.stderr:write(debug.traceback(err, 2)) io.stderr:write("\n") @@ -60,8 +60,8 @@ end while file_exists(name) do io.write("File '" .. name .. "' exists. ") - local is_dir = is_dir(name) - if not is_dir then + local dir = is_dir(name) + if not dir then io.write("[O]verwrite/") end @@ -74,7 +74,7 @@ while file_exists(name) do io.write("Enter new name: ") io.flush() name = io.read() - elseif not is_dir and (choice == "o" or choice == "O") then + elseif not dir and (choice == "o" or choice == "O") then break else io.stderr:write("Invalid option: " .. choice .. "\n")