From 38ec145e8623595f057b4969089cc2328081078b Mon Sep 17 00:00:00 2001 From: AhmedAhmedEG Date: Wed, 28 Sep 2022 19:56:05 +0200 Subject: [PATCH] Improved -replace command The replace command suffered from errors if it was gives unicode class named in the terminal, also it was limited with the maximum line length of the terminal, now I made a very simple modification to make only 3 parameters:- 1-InFile 2-OutFile 3-ArgumentsTextfilePath So we create a text file with all the arguments we want, with infinite length, and getting rid of the unicode errors, this is very useful as some flash files have 100+ scripts, and you have to stuff 2 arguments per file with full path in the terminal line length limit, and also lose the ability to specify classes that have unicode characters on it, as FFDEC was parsing them from the terminal as question marks. --- .../console/CommandLineArgumentParser.java | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java b/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java index 4cf43b57a..0cc93c8e9 100644 --- a/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java +++ b/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java @@ -171,6 +171,7 @@ import java.awt.print.PageFormat; import java.awt.print.Paper; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; +import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -207,6 +208,13 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; +import java.io.FileWriter; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Scanner; /** * @@ -2904,18 +2912,37 @@ public class CommandLineArgumentParser { } private static void parseReplace(Stack args) { - if (args.size() < 4) { + if (args.size() == 3) { badArguments("replace"); } - + + System.out.println("Replacing in: " + args.lastElement()); + File inFile = new File(args.pop()); File outFile = new File(args.pop()); + + try { + List lines = Files.readAllLines(Paths.get(args.pop()), StandardCharsets.UTF_8); + Collections.reverse(lines); + + args.clear(); + args.addAll(lines); + + } catch (IOException e) { + e.printStackTrace(System.out); + } + + if (args.isEmpty()) { + System.err.println("Replacments file is empty."); + System.exit(1); + } + try { try (FileInputStream is = new FileInputStream(inFile)) { SWF swf = new SWF(is, Configuration.parallelSpeedUp.get()); while (true) { - String objectToReplace = args.pop(); - + String objectToReplace = args.pop(); + if (objectToReplace.matches("\\d+")) { // replace character tag int characterId = 0; @@ -3004,15 +3031,16 @@ public class CommandLineArgumentParser { } } else { List packs = swf.getAS3Packs(); + for (ScriptPack entry : packs) { - if (entry.getClassPath().toString().equals(objectToReplace)) { + if (entry.getClassPath().toString().equals(objectToReplace)) { found = true; // replace AS3 String repFile = args.pop(); String repText = Helper.readTextFile(repFile); ScriptPack pack = entry; if (Path.getExtension(repFile).equals(".as")) { - replaceAS3(repText, pack); + replaceAS3(repFile, repText, pack); } else { // todo: get traits if (args.isEmpty()) { @@ -3641,8 +3669,8 @@ public class CommandLineArgumentParser { ((Tag) abc.parentTag).setModified(true); } - private static void replaceAS3(String as, ScriptPack pack) throws IOException, InterruptedException { - System.out.println("Replace AS3"); + private static void replaceAS3(String asp, String as, ScriptPack pack) throws IOException, InterruptedException { + System.out.println("Replaceing: " + asp); System.out.println("Warning: This feature is EXPERIMENTAL"); As3ScriptReplacerInterface scriptReplacer = As3ScriptReplacerFactory.createByConfig(); if (!scriptReplacer.isAvailable()) {