From cc0050f155e815ce5d4de90e5383beb07b53473b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Sun, 5 Nov 2023 20:00:02 +0100 Subject: [PATCH] Added #2113 Commandline - allow `-onerror abort` argument on `-importScript` --- CHANGELOG.md | 2 + .../flash/importers/AS2ScriptImporter.java | 54 ++++++++++++++----- .../flash/importers/AS3ScriptImporter.java | 3 ++ .../ScriptImporterProgressListener.java | 2 + .../console/CommandLineArgumentParser.java | 21 ++++++-- .../ConsoleAbortRetryIgnoreHandler.java | 6 ++- .../jpexs/decompiler/flash/gui/MainPanel.java | 9 ++++ 7 files changed, 79 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 259fc1360..14dc0e5cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ All notable changes to this project will be documented in this file. advance value (add to the font advance + calculated letterspacing) - DefineEditText display - letterspacing, kerning, indent, relative font size - FLA export - DefineEditText autokern attribute +- [#2113] Commandline - allow `-onerror abort` argument on `-importScript` ### Fixed - [#1306], [#1768] Maximizing window on other than main monitor @@ -3250,6 +3251,7 @@ Major version of SWF to XML export changed to 2. [#2005]: https://www.free-decompiler.com/flash/issues/2005 [#2105]: https://www.free-decompiler.com/flash/issues/2105 [#1799]: https://www.free-decompiler.com/flash/issues/1799 +[#2113]: https://www.free-decompiler.com/flash/issues/2113 [#1306]: https://www.free-decompiler.com/flash/issues/1306 [#1768]: https://www.free-decompiler.com/flash/issues/1768 [#2013]: https://www.free-decompiler.com/flash/issues/2013 diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/AS2ScriptImporter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/AS2ScriptImporter.java index 228d518e3..2f5b994f8 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/AS2ScriptImporter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/AS2ScriptImporter.java @@ -83,26 +83,38 @@ public class AS2ScriptImporter { String txt = Helper.readTextFile(fileName); ActionScript2Parser par = new ActionScript2Parser(asm.getSwf(), asm); + boolean errored = false; try { asm.setActions(par.actionsFromString(txt, asm.getSwf().getCharset())); } catch (ValueTooLargeException ex) { logger.log(Level.SEVERE, "Script or some of its functions are too large, file: {0}", fileName); + errored = true; } catch (ActionParseException ex) { logger.log(Level.SEVERE, "%error% on line %line%, file: %file%".replace("%error%", ex.text).replace("%line%", Long.toString(ex.line)).replace("%file%", fileName), ex); + errored = true; } catch (CompilationException ex) { logger.log(Level.SEVERE, "%error% on line %line%, file: %file%".replace("%error%", ex.text).replace("%line%", Long.toString(ex.line)).replace("%file%", fileName), ex); + errored = true; } catch (IOException ex) { logger.log(Level.SEVERE, "error during script import, file: %file%".replace("%file%", fileName), ex); + errored = true; } catch (InterruptedException ex) { return importCount; } catch (Exception ex) { logger.log(Level.SEVERE, "error during script import, file: %file%".replace("%file%", fileName), ex); + errored = true; } - asm.setModified(); - importCount++; - if (listener != null) { - listener.scriptImported(); + if (!errored) { + asm.setModified(); + importCount++; + if (listener != null) { + listener.scriptImported(); + } + } else { + if (listener != null) { + listener.scriptImportError(); + } } } @@ -111,18 +123,27 @@ public class AS2ScriptImporter { asm.getSwf().informListeners("importing_as", fileName); String txt = Helper.readTextFile(fileName); + boolean errored = false; try { asm.setActions(ASMParser.parse(0, true, txt, asm.getSwf().version, false, asm.getSwf().getCharset())); } catch (IOException ex) { logger.log(Level.SEVERE, "error during script import, file: %file%".replace("%file%", fileName), ex); + errored = true; } catch (ActionParseException ex) { logger.log(Level.SEVERE, "%error% on line %line%, file: %file%".replace("%error%", ex.text).replace("%line%", Long.toString(ex.line)).replace("%file%", fileName), ex); + errored = true; } - asm.setModified(); - importCount++; - if (listener != null) { - listener.scriptImported(); + if (!errored) { + asm.setModified(); + importCount++; + if (listener != null) { + listener.scriptImported(); + } + } else { + if (listener != null) { + listener.scriptImportError(); + } } } @@ -144,15 +165,24 @@ public class AS2ScriptImporter { asm.getSwf().informListeners("importing_as", fileName); String txt = Helper.readTextFile(fileName); + boolean errored = false; try { asm.setConstantPools(Helper.getConstantPoolsFromText(txt)); } catch (ConstantPoolTooBigException ex) { logger.log(Level.SEVERE, null, ex); + errored = true; } - asm.setModified(); - importCount++; - if (listener != null) { - listener.scriptImported(); + + if (!errored) { + asm.setModified(); + importCount++; + if (listener != null) { + listener.scriptImported(); + } + } else { + if (listener != null) { + listener.scriptImportError(); + } } } } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/AS3ScriptImporter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/AS3ScriptImporter.java index f4269bc4e..8e1d9b9c6 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/AS3ScriptImporter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/AS3ScriptImporter.java @@ -69,6 +69,9 @@ public class AS3ScriptImporter { for (As3ScriptReplaceExceptionItem item : asre.getExceptionItems()) { logger.log(Level.SEVERE, "%error% on line %line%, column %col%, file: %file%".replace("%error%", item.getMessage()).replace("%line%", Long.toString(item.getLine())).replace("%file%", fileName).replace("%col%", "" + item.getCol())); } + if (listener != null) { + listener.scriptImportError(); + } } catch (InterruptedException ex) { return importCount; } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/ScriptImporterProgressListener.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/ScriptImporterProgressListener.java index f6c29b79a..eb57dc539 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/ScriptImporterProgressListener.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/ScriptImporterProgressListener.java @@ -23,4 +23,6 @@ package com.jpexs.decompiler.flash.importers; public interface ScriptImporterProgressListener { public void scriptImported(); + + public void scriptImportError(); } diff --git a/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java b/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java index 5eb6612b6..fa0c9cc17 100644 --- a/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java +++ b/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java @@ -122,6 +122,7 @@ import com.jpexs.decompiler.flash.importers.FontImporter; import com.jpexs.decompiler.flash.importers.ImageImporter; import com.jpexs.decompiler.flash.importers.MorphShapeImporter; import com.jpexs.decompiler.flash.importers.MovieImporter; +import com.jpexs.decompiler.flash.importers.ScriptImporterProgressListener; import com.jpexs.decompiler.flash.importers.ShapeImporter; import com.jpexs.decompiler.flash.importers.SoundImporter; import com.jpexs.decompiler.flash.importers.SpriteImporter; @@ -1140,7 +1141,7 @@ public class CommandLineArgumentParser { parseImportText(args, charset); System.exit(0); } else if (command.equals("importscript")) { - parseImportScript(args, charset, air); + parseImportScript(args, charset, air, handler); System.exit(0); } else if (command.equals("as3compiler")) { ActionScript3Parser.compile(null /*?*/, args.pop(), args.pop(), 0, 0); @@ -4202,7 +4203,7 @@ public class CommandLineArgumentParser { } } - private static void parseImportScript(Stack args, String charset, boolean air) { + private static void parseImportScript(Stack args, String charset, boolean air, AbortRetryIgnoreHandler errorHandler) { String flexLocation = Configuration.flexSdkLocation.get(); if (Configuration.useFlexAs3Compiler.get() && (flexLocation.isEmpty() || (!new File(flexLocation).exists()))) { @@ -4226,8 +4227,20 @@ public class CommandLineArgumentParser { } else { scriptsFolder = baseFolder; } - new AS2ScriptImporter().importScripts(scriptsFolder, swf.getASMs(true)); - new AS3ScriptImporter().importScripts(As3ScriptReplacerFactory.createByConfig(air), scriptsFolder, swf.getAS3Packs()); + ScriptImporterProgressListener listener = new ScriptImporterProgressListener() { + @Override + public void scriptImported() { + } + + @Override + public void scriptImportError() { + if (errorHandler != null && ((ConsoleAbortRetryIgnoreHandler)errorHandler).errorMode == AbortRetryIgnoreHandler.ABORT) { + System.exit(1); + } + } + }; + new AS2ScriptImporter().importScripts(scriptsFolder, swf.getASMs(true), listener); + new AS3ScriptImporter().importScripts(As3ScriptReplacerFactory.createByConfig(air), scriptsFolder, swf.getAS3Packs(), listener); try { try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(outFile))) { diff --git a/src/com/jpexs/decompiler/flash/console/ConsoleAbortRetryIgnoreHandler.java b/src/com/jpexs/decompiler/flash/console/ConsoleAbortRetryIgnoreHandler.java index 30d6058d2..90f68e8ed 100644 --- a/src/com/jpexs/decompiler/flash/console/ConsoleAbortRetryIgnoreHandler.java +++ b/src/com/jpexs/decompiler/flash/console/ConsoleAbortRetryIgnoreHandler.java @@ -53,8 +53,10 @@ public class ConsoleAbortRetryIgnoreHandler implements AbortRetryIgnoreHandler { return result; } Scanner sc = new Scanner(System.in); - Logger.getLogger(ConsoleAbortRetryIgnoreHandler.class.getName()).log(Level.SEVERE, "Error occured", thrown); - System.out.println("Error occured: " + thrown.getLocalizedMessage()); + if (thrown != null) { + Logger.getLogger(ConsoleAbortRetryIgnoreHandler.class.getName()).log(Level.SEVERE, "Error occured", thrown); + System.out.println("Error occured: " + thrown.getLocalizedMessage()); + } do { System.out.print("Select action: (A)bort, (R)Retry, (I)Ignore:"); String n = sc.nextLine(); diff --git a/src/com/jpexs/decompiler/flash/gui/MainPanel.java b/src/com/jpexs/decompiler/flash/gui/MainPanel.java index df22a3808..297f95d21 100644 --- a/src/com/jpexs/decompiler/flash/gui/MainPanel.java +++ b/src/com/jpexs/decompiler/flash/gui/MainPanel.java @@ -3774,6 +3774,11 @@ public final class MainPanel extends JPanel implements TreeSelectionListener, Se public void scriptImported() { countAs2++; } + + @Override + public void scriptImportError() { + + } }); List packs; @@ -3791,6 +3796,10 @@ public final class MainPanel extends JPanel implements TreeSelectionListener, Se public void scriptImported() { countAs3++; } + + @Override + public void scriptImportError() { + } } );