diff --git a/Sources/arm/io/ImportArm.hx b/Sources/arm/io/ImportArm.hx index 072d0d24..792b0bf2 100644 --- a/Sources/arm/io/ImportArm.hx +++ b/Sources/arm/io/ImportArm.hx @@ -16,6 +16,7 @@ import arm.ui.UITrait; import arm.ui.UINodes; import arm.ui.UIFiles; import arm.sys.Path; +import arm.sys.File; import arm.util.Lz4; import arm.util.RenderUtil; import arm.util.ViewportUtil; @@ -68,13 +69,7 @@ class ImportArm { // Convert image path from relative to absolute var isAbsolute = file.charAt(0) == "/" || file.charAt(1) == ":"; var abs = isAbsolute ? file : base + file; - #if krom_windows - var exists = Krom.sysCommand('IF EXIST "' + abs + '" EXIT /b 1'); - #else - var exists = 1; - // { test -e file && echo 1 || echo 0 } - #end - if (exists == 0) { + if (!File.exists(abs)) { Log.showError(Strings.error2 + abs); var b = Bytes.alloc(4); b.set(0, 255); @@ -254,13 +249,7 @@ class ImportArm { // Convert image path from relative to absolute var isAbsolute = file.charAt(0) == "/" || file.charAt(1) == ":"; var abs = isAbsolute ? file : base + file; - #if krom_windows - var exists = Krom.sysCommand('IF EXIST "' + abs + '" EXIT /b 1'); - #else - var exists = 1; - // { test -e file && echo 1 || echo 0 } - #end - if (exists == 0) { + if (!File.exists(abs)) { Log.showError(Strings.error2 + abs); var b = Bytes.alloc(4); b.set(0, 255); diff --git a/Sources/arm/sys/File.hx b/Sources/arm/sys/File.hx index ffcaa59c..7262f996 100644 --- a/Sources/arm/sys/File.hx +++ b/Sources/arm/sys/File.hx @@ -8,9 +8,11 @@ class File { #if krom_windows static inline var cmd_dir = "dir /b"; static inline var cmd_copy = "copy"; + static inline var cmd_del = "del /f"; #else static inline var cmd_dir = "ls"; static inline var cmd_copy = "cp"; + static inline var cmd_del = "rm"; #end public static function readDirectory(path:String):Array { @@ -24,6 +26,54 @@ class File { } public static function copy(srcPath:String, dstPath:String) { - Krom.sysCommand(cmd_copy + ' ' + srcPath + ' ' + dstPath); + Krom.sysCommand(cmd_copy + ' "' + srcPath + '" "' + dstPath + '"'); + } + + public static function start(path:String) { + #if krom_windows + Krom.sysCommand('start "" "' + path + '"'); + #elseif krom_linux + Krom.sysCommand('xdg-open "' + path + '"'); + #else + Krom.sysCommand('open "' + path + '"'); + #end + } + + public static function explorer(url:String) { + #if krom_windows + Krom.sysCommand('explorer "' + url + '"'); + #elseif krom_linux + Krom.sysCommand('xdg-open "' + url + '"'); + #else + Krom.sysCommand('open "' + url + '"'); + #end + } + + public static function delete(path:String) { + Krom.sysCommand(cmd_del + ' "' + path + '"'); + } + + public static function exists(path:String):Bool { + #if krom_windows + var exists = Krom.sysCommand('IF EXIST "' + path + '" EXIT /b 1'); + #else + var exists = 1; + // { test -e file && echo 1 || echo 0 } + #end + return exists == 1; + } + + public static function download(url:String, dstPath:String) { + #if krom_windows + Krom.sysCommand('powershell -c "Invoke-WebRequest -Uri ' + url + " -OutFile '" + dstPath + "'"); + #else + Krom.sysCommand('curl ' + url + ' -o ' + dstPath); + #end + } + + public static function downloadBytes(url:String):Bytes { + var save = Path.data() + Path.sep + "download.bin"; + download(url, save); + return Bytes.ofData(Krom.loadBlob(save)); } } diff --git a/Sources/arm/ui/BoxPreferences.hx b/Sources/arm/ui/BoxPreferences.hx index 1ffd4a95..d15888f9 100644 --- a/Sources/arm/ui/BoxPreferences.hx +++ b/Sources/arm/ui/BoxPreferences.hx @@ -8,6 +8,8 @@ import iron.data.Data; import arm.nodes.MaterialParser; import arm.data.LayerSlot; import arm.io.ImportPlugin; +import arm.sys.Path; +import arm.sys.File; using StringTools; class BoxPreferences { @@ -70,14 +72,14 @@ class BoxPreferences { ui.endElement(); ui.row([1/2]); if (ui.button("Restore")) { - // UIMenu.draw(function(ui:Zui) { - // ui.fill(0, 0, ui._w / ui.SCALE(), ui.t.ELEMENT_H * 2, ui.t.SEPARATOR_COL); - // ui.text("Restore defaults?", Right, ui.t.CONTEXT_COL); - // if (ui.button("Confirm", Left)) { + UIMenu.draw(function(ui:Zui) { + ui.fill(0, 0, ui._w / ui.SCALE(), ui.t.ELEMENT_H * 2, ui.t.SEPARATOR_COL); + ui.text("Restore defaults?", Right, ui.t.CONTEXT_COL); + if (ui.button("Confirm", Left)) { Config.restore(); setScale(); - // } - // }); + } + }); } } if (ui.tab(htab, "Usage")) { @@ -220,14 +222,6 @@ class BoxPreferences { if (ui.changed) Config.applyConfig(); } if (ui.tab(htab, "Plugins")) { - #if krom_windows - var sep = "\\"; - var dataPath = Data.dataPath.replace("/", "\\"); - #else - var sep = "/"; - var dataPath = Data.dataPath; - #end - ui.row([1/4, 1/4]); if (ui.button("New")) { var template = @@ -247,7 +241,7 @@ plugin.drawUI = function(ui) { var pluginName = ui.textInput(Id.handle({text: "new_plugin"}), "Name"); if (ui.button("OK") || ui.isReturnDown) { if (!pluginName.endsWith(".js")) pluginName += ".js"; - var path = Krom.getFilesLocation() + sep + dataPath + sep + "plugins" + sep + pluginName; + var path = Path.data() + Path.sep + "plugins" + Path.sep + pluginName; Krom.fileSaveBytes(path, Bytes.ofString(template).getData()); files = null; // Refresh file list UIBox.show = false; @@ -263,25 +257,7 @@ plugin.drawUI = function(ui) { } if (files == null) { - #if krom_windows - var cmd = "dir /b "; - #else - var cmd = "ls "; - #end - #if krom_linux - var save = "/tmp"; - #else - var save = Krom.savePath(); - #end - save += sep + "dir.txt"; - var path = Krom.getFilesLocation() + sep + dataPath + sep + "plugins"; - Krom.sysCommand(cmd + '"' + path + '"' + ' > ' + '"' + save + '"'); - var str = Bytes.ofData(Krom.loadBlob(save)).toString(); - files = str.split("\n"); - files.pop(); - for (i in 0...files.length) { - files[i] = files[i].replace("\r", ""); - } + files = File.readDirectory(Path.data() + Path.sep + "plugins"); } var h = Id.handle({selected: false}); @@ -309,25 +285,14 @@ plugin.drawUI = function(ui) { UIMenu.draw(function(ui:Zui) { ui.fill(0, 0, ui._w / ui.SCALE(), ui.t.ELEMENT_H * 4, ui.t.SEPARATOR_COL); ui.text(f, Right, ui.t.CONTEXT_COL); - var path = Krom.getFilesLocation() + sep + dataPath + sep + "plugins" + sep + f; + var path = Path.data() + Path.sep + "plugins" + Path.sep + f; if (ui.button("Edit", Left)) { - #if krom_windows - Krom.sysCommand('start "" "' + path + '"'); - #elseif krom_linux - Krom.sysCommand('xdg-open "' + path + '"'); - #else - Krom.sysCommand('open "' + path + '"'); - #end + File.start(path); } if (ui.button("Export", Left)) { UIFiles.show("js", true, function(dest:String) { - #if krom_windows - var copy = "copy"; - #else - var copy = "cp"; - #end if (!UIFiles.filename.endsWith(".js")) UIFiles.filename += ".js"; - Krom.sysCommand(copy + ' ' + path + ' ' + dest + sep + UIFiles.filename); + File.copy(path, dest + Path.sep + UIFiles.filename); }); } if (ui.button("Delete", Left)) { @@ -336,18 +301,12 @@ plugin.drawUI = function(ui) { Plugin.stop(f); } files.remove(f); - #if krom_windows - var cmd = "del /f "; - #else - var cmd = "rm "; - #end - Krom.sysCommand(cmd + '"' + path + '"'); + File.delete(path); } }); } } } - ui._w = _w; }, 500, 310); } diff --git a/Sources/arm/ui/UIFiles.hx b/Sources/arm/ui/UIFiles.hx index 55cc540b..814becb6 100644 --- a/Sources/arm/ui/UIFiles.hx +++ b/Sources/arm/ui/UIFiles.hx @@ -25,13 +25,8 @@ class UIFiles { #end path = path.replace("\\\\", "\\"); path = path.replace("\r", ""); - #if krom_windows - var sep = "\\"; - #else - var sep = "/"; - #end - filename = path.substr(path.lastIndexOf(sep) + 1); - if (isSave) path = path.substr(0, path.lastIndexOf(sep)); + filename = path.substr(path.lastIndexOf(Path.sep) + 1); + if (isSave) path = path.substr(0, path.lastIndexOf(Path.sep)); filesDone(path); } releaseKeys(); @@ -49,13 +44,8 @@ class UIFiles { var known = Path.isTexture(path) || Path.isMesh(path) || Path.isProject(path); if (ui.button(isSave ? "Save" : "Open") || known || ui.isReturnDown) { UIBox.show = false; - #if krom_windows - var sep = "\\"; - #else - var sep = "/"; - #end - filesDone((known || isSave) ? path : path + sep + filename); - if (known) pathHandle.text = pathHandle.text.substr(0, pathHandle.text.lastIndexOf(sep)); + filesDone((known || isSave) ? path : path + Path.sep + filename); + if (known) pathHandle.text = pathHandle.text.substr(0, pathHandle.text.lastIndexOf(Path.sep)); } zui.Ext.dataPath = iron.data.Data.dataPath; path = zui.Ext.fileBrowser(ui, pathHandle, false); diff --git a/Sources/arm/ui/UIMenu.hx b/Sources/arm/ui/UIMenu.hx index dfb5f030..6ab1fc9d 100644 --- a/Sources/arm/ui/UIMenu.hx +++ b/Sources/arm/ui/UIMenu.hx @@ -9,6 +9,7 @@ import iron.data.Data; import arm.util.ViewportUtil; import arm.util.BuildMacros; import arm.sys.Path; +import arm.sys.File; using StringTools; class UIMenu { @@ -127,22 +128,10 @@ class UIMenu { } else if (menuCategory == 3) { if (ui.button("Manual", Left)) { - #if krom_windows - Krom.sysCommand('explorer "https://armorpaint.org/manual"'); - #elseif krom_linux - Krom.sysCommand('xdg-open "https://armorpaint.org/manual"'); - #else - Krom.sysCommand('open "https://armorpaint.org/manual"'); - #end + File.explorer("https://armorpaint.org/manual"); } if (ui.button("Issue Tracker", Left)) { - #if krom_windows - Krom.sysCommand('explorer "https://github.com/armory3d/armorpaint/issues"'); - #elseif krom_linux - Krom.sysCommand('xdg-open "https://github.com/armory3d/armorpaint/issues"'); - #else - Krom.sysCommand('open "https://github.com/armory3d/armorpaint/issues"'); - #end + File.explorer("https://github.com/armory3d/armorpaint/issues"); } if (ui.button("Report Bug", Left)) { var ver = App.version; @@ -150,40 +139,25 @@ class UIMenu { sha = sha.substr(1, sha.length - 2); var os = System.systemId; var url = "https://github.com/armory3d/armorpaint/issues/new?labels=bug&template=bug_report.md&body=*ArmorPaint%20" + ver + "-" + sha + ",%20" + os + "*"; - #if krom_windows - Krom.sysCommand('explorer "$url"'); - #elseif krom_linux - Krom.sysCommand('xdg-open $url'); - #else - Krom.sysCommand('open $url'); - #end + File.explorer(url); } - // if (ui.button("Request Feature", Left)) {} if (ui.button("Check for Updates...", Left)) { // Retrieve latest version number - var outFile = Krom.getFilesLocation() + '/' + Data.dataPath + "update.txt"; - var uri = "'https://luboslenco.gitlab.io/armorpaint/index.html'"; - #if krom_windows - Krom.sysCommand('powershell -c "Invoke-WebRequest -Uri ' + uri + " -OutFile '" + outFile + "'"); - #else - Krom.sysCommand('curl ' + uri + ' -o ' + outFile); - #end + var url = "'https://luboslenco.gitlab.io/armorpaint/index.html'"; + var blob = File.downloadBytes(url); // Compare versions - Data.getBlob(outFile, function(blob:Blob) { - var update = Json.parse(blob.toString()); - var updateVersion = Std.int(update.version); - if (updateVersion > 0) { - var date = BuildMacros.date().split(" ")[0].substr(2); // 2019 -> 19 - var dateInt = Std.parseInt(date.replace("-", "")); - if (updateVersion > dateInt) { - UIBox.showMessage("Update", "Update is available!\nPlease visit armorpaint.org to download."); - } - else { - UIBox.showMessage("Update", "You are up to date!"); - } + var update = Json.parse(blob.toString()); + var updateVersion = Std.int(update.version); + if (updateVersion > 0) { + var date = BuildMacros.date().split(" ")[0].substr(2); // 2019 -> 19 + var dateInt = Std.parseInt(date.replace("-", "")); + if (updateVersion > dateInt) { + UIBox.showMessage("Update", "Update is available!\nPlease visit armorpaint.org to download."); } - Data.deleteBlob(outFile); - }); + else { + UIBox.showMessage("Update", "You are up to date!"); + } + } } if (ui.button("About...", Left)) { var sha = BuildMacros.sha();