diff --git a/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java b/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java index 69b4f1390..748bdfe35 100644 --- a/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java +++ b/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java @@ -23,11 +23,20 @@ import com.jpexs.decompiler.flash.SWF; import com.jpexs.decompiler.flash.SWFBundle; import com.jpexs.decompiler.flash.SWFSourceInfo; import com.jpexs.decompiler.flash.SearchMode; +import com.jpexs.decompiler.flash.abc.ABC; +import com.jpexs.decompiler.flash.abc.ABCInputStream; import com.jpexs.decompiler.flash.abc.ClassPath; import com.jpexs.decompiler.flash.abc.RenameType; import com.jpexs.decompiler.flash.abc.ScriptPack; +import com.jpexs.decompiler.flash.abc.avm2.AVM2Code; +import com.jpexs.decompiler.flash.abc.avm2.UnknownInstructionCode; +import com.jpexs.decompiler.flash.abc.avm2.parser.pcode.ASM3Parser; +import com.jpexs.decompiler.flash.abc.avm2.parser.pcode.MissingSymbolHandler; import com.jpexs.decompiler.flash.abc.avm2.parser.script.ActionScriptParser; +import com.jpexs.decompiler.flash.abc.types.MethodBody; +import com.jpexs.decompiler.flash.abc.types.traits.Trait; import com.jpexs.decompiler.flash.action.parser.ParseException; +import com.jpexs.decompiler.flash.action.parser.pcode.ASMParser; import com.jpexs.decompiler.flash.configuration.Configuration; import com.jpexs.decompiler.flash.configuration.ConfigurationItem; import com.jpexs.decompiler.flash.exporters.BinaryDataExporter; @@ -59,6 +68,7 @@ import com.jpexs.decompiler.flash.exporters.settings.ShapeExportSettings; import com.jpexs.decompiler.flash.exporters.settings.SoundExportSettings; import com.jpexs.decompiler.flash.exporters.settings.TextExportSettings; import com.jpexs.decompiler.flash.gui.Main; +import com.jpexs.decompiler.flash.gui.abc.ASMSourceEditorPane; import com.jpexs.decompiler.flash.helpers.collections.MyEntry; import com.jpexs.decompiler.flash.importers.BinaryDataImporter; import com.jpexs.decompiler.flash.importers.ImageImporter; @@ -76,6 +86,7 @@ import com.jpexs.decompiler.flash.types.sound.SoundFormat; import com.jpexs.decompiler.flash.xfl.FLAVersion; import com.jpexs.decompiler.graph.CompilationException; import com.jpexs.helpers.Helper; +import com.jpexs.helpers.MemoryInputStream; import com.jpexs.helpers.Path; import com.jpexs.helpers.streams.SeekableInputStream; import com.sun.jna.Platform; @@ -98,6 +109,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; +import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -1352,51 +1364,31 @@ public class CommandLineArgumentParser { if (asms.containsKey(objectToReplace)) { found = true; // replace AS1/2 - System.out.println("Replace AS1/2"); - System.out.println("Warning: This feature is EXPERIMENTAL"); String repFile = args.remove(); - String as = Helper.readTextFile(repFile); + String repText = Helper.readTextFile(repFile); ASMSource src = asms.get(objectToReplace); - com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser par = new com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser(swf.version); - try { - src.setActions(par.actionsFromString(as)); - } catch (ParseException ex) { - System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); - System.exit(1); - } catch (CompilationException ex) { - System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); - System.exit(1); + if (Path.getExtension(repFile).equals(".as")) { + replaceAS2(repText, src); + } else { + replaceAS2PCode(repText, src); } - src.setModified(); } else { List> packs = swf.getAS3Packs(); for (MyEntry entry : packs) { if (entry.getKey().toString().equals(objectToReplace)) { found = true; // replace AS3 - System.out.println("Replace AS3"); - System.out.println("Warning: This feature is EXPERIMENTAL"); - File swc = Configuration.getPlayerSWC(); - if(swc == null) { - final String adobePage = "http://www.adobe.com/support/flashplayer/downloads.html"; - System.err.println("For ActionScript 3 direct editation, a library called \"PlayerGlobal.swc\" needs to be downloaded from Adobe homepage:"); - System.err.println(adobePage); - System.err.println("Download the library called PlayerGlobal(.swc), and place it to directory"); - System.err.println(Configuration.getFlashLibPath().getAbsolutePath()); - System.exit(1); - } - String repFile = args.remove(); - String as = Helper.readTextFile(repFile); + String repText = Helper.readTextFile(repFile); ScriptPack pack = entry.getValue(); - try { - pack.abc.replaceSciptPack(pack, as); - } catch (com.jpexs.decompiler.flash.abc.avm2.parser.ParseException ex) { - System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); - System.exit(1); - } catch (CompilationException ex) { - System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); - System.exit(1); + if (Path.getExtension(repFile).equals(".as")) { + replaceAS3(repText, pack); + } else { + // todo: get Ids + int bodyIndex = 0; + int classIndex = 0; + int traitId = 0; + replaceAS3PCode(repText, pack.abc, bodyIndex, classIndex, traitId); } } } @@ -1428,6 +1420,110 @@ public class CommandLineArgumentParser { } } + private static void replaceAS2PCode(String text, ASMSource src) throws IOException, InterruptedException { + System.out.println("Replace AS1/2 PCode"); + if (text.trim().startsWith("#hexdata")) { + src.setActionBytes(Helper.getBytesFromHexaText(text)); + } else { + try { + src.setActions(ASMParser.parse(0, true, text, src.getSwf().version, false)); + } catch (ParseException ex) { + System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); + System.exit(1); + } + } + src.setModified(); + } + + private static void replaceAS2(String as, ASMSource src) throws IOException, InterruptedException { + System.out.println("Replace AS1/2"); + System.out.println("Warning: This feature is EXPERIMENTAL"); + com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser par = new com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser(src.getSwf().version); + try { + src.setActions(par.actionsFromString(as)); + } catch (ParseException ex) { + System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); + System.exit(1); + } catch (CompilationException ex) { + System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); + System.exit(1); + } + src.setModified(); + } + + private static void replaceAS3PCode(String text, ABC abc, int bodyIndex, int classIndex, int traitId) throws IOException, InterruptedException { + System.out.println("Replace AS3 PCode"); + if (text.trim().startsWith("#hexdata")) { + byte[] data = Helper.getBytesFromHexaText(text); + MethodBody mb = abc.bodies.get(bodyIndex); + mb.codeBytes = data; + try { + ABCInputStream ais = new ABCInputStream(new MemoryInputStream(mb.codeBytes)); + mb.code = new AVM2Code(ais); + } catch (UnknownInstructionCode re) { + mb.code = new AVM2Code(); + Logger.getLogger(CommandLineArgumentParser.class.getName()).log(Level.SEVERE, null, re); + } + mb.code.compact(); + } else { + Trait trait = abc.findTraitByTraitId(classIndex, traitId); + try { + AVM2Code acode = ASM3Parser.parse(new StringReader(text), abc.constants, trait, new MissingSymbolHandler() { + //no longer ask for adding new constants + @Override + public boolean missingString(String value) { + return true; + } + + @Override + public boolean missingInt(long value) { + return true; + } + + @Override + public boolean missingUInt(long value) { + return true; + } + + @Override + public boolean missingDouble(double value) { + return true; + } + }, abc.bodies.get(bodyIndex), abc.method_info.get(abc.bodies.get(bodyIndex).method_info)); + acode.getBytes(abc.bodies.get(bodyIndex).codeBytes); + abc.bodies.get(bodyIndex).code = acode; + } catch (com.jpexs.decompiler.flash.abc.avm2.parser.ParseException ex) { + System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); + System.exit(1); + } + } + ((Tag) abc.parentTag).setModified(true); + } + + private static void replaceAS3(String as, ScriptPack pack) throws IOException, InterruptedException { + System.out.println("Replace AS3"); + System.out.println("Warning: This feature is EXPERIMENTAL"); + File swc = Configuration.getPlayerSWC(); + if(swc == null) { + final String adobePage = "http://www.adobe.com/support/flashplayer/downloads.html"; + System.err.println("For ActionScript 3 direct editation, a library called \"PlayerGlobal.swc\" needs to be downloaded from Adobe homepage:"); + System.err.println(adobePage); + System.err.println("Download the library called PlayerGlobal(.swc), and place it to directory"); + System.err.println(Configuration.getFlashLibPath().getAbsolutePath()); + System.exit(1); + } + + try { + pack.abc.replaceSciptPack(pack, as); + } catch (com.jpexs.decompiler.flash.abc.avm2.parser.ParseException ex) { + System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); + System.exit(1); + } catch (CompilationException ex) { + System.err.println("%error% on line %line%".replace("%error%", ex.text).replace("%line%", "" + ex.line)); + System.exit(1); + } + } + private static void parseDumpSwf(Queue args) { if (args.isEmpty()) { badArguments(); @@ -1473,7 +1569,7 @@ public class CommandLineArgumentParser { SWF swf = new SWF(is, Configuration.parallelSpeedUp.get()); List> packs = swf.getAS3Packs(); for (MyEntry entry : packs) { - System.out.println(entry.getKey().toString()); + System.out.println(entry.getKey().toString() + " " + entry.getValue().scriptIndex); } } } catch (IOException | InterruptedException e) { diff --git a/src/com/jpexs/decompiler/flash/gui/abc/ASMSourceEditorPane.java b/src/com/jpexs/decompiler/flash/gui/abc/ASMSourceEditorPane.java index 99a2e673b..67b05175b 100644 --- a/src/com/jpexs/decompiler/flash/gui/abc/ASMSourceEditorPane.java +++ b/src/com/jpexs/decompiler/flash/gui/abc/ASMSourceEditorPane.java @@ -187,7 +187,7 @@ public class ASMSourceEditorPane extends LineMarkedEditorPane implements CaretLi View.showMessageDialog(this, "Returned object:" + o.toString()); } - public boolean save(ConstantPool constants) { + public boolean save() { try { String text = getText(); if (text.trim().startsWith("#hexdata")) { @@ -203,7 +203,7 @@ public class ASMSourceEditorPane extends LineMarkedEditorPane implements CaretLi } mb.code.compact(); } else { - AVM2Code acode = ASM3Parser.parse(new StringReader(text), constants, trait, new MissingSymbolHandler() { + AVM2Code acode = ASM3Parser.parse(new StringReader(text), abc.constants, trait, new MissingSymbolHandler() { //no longer ask for adding new constants @Override public boolean missingString(String value) { diff --git a/src/com/jpexs/decompiler/flash/gui/abc/MethodCodePanel.java b/src/com/jpexs/decompiler/flash/gui/abc/MethodCodePanel.java index f5accf41a..a17f47cb9 100644 --- a/src/com/jpexs/decompiler/flash/gui/abc/MethodCodePanel.java +++ b/src/com/jpexs/decompiler/flash/gui/abc/MethodCodePanel.java @@ -85,8 +85,8 @@ public class MethodCodePanel extends JPanel implements ActionListener { sourceTextArea.clear(); } - public boolean save(ConstantPool constants) { - return sourceTextArea.save(constants); + public boolean save() { + return sourceTextArea.save(); } public MethodCodePanel(DecompiledEditorPane decompiledEditor) { diff --git a/src/com/jpexs/decompiler/flash/gui/abc/MethodTraitDetailPanel.java b/src/com/jpexs/decompiler/flash/gui/abc/MethodTraitDetailPanel.java index 7924d5943..c1ca55bcb 100644 --- a/src/com/jpexs/decompiler/flash/gui/abc/MethodTraitDetailPanel.java +++ b/src/com/jpexs/decompiler/flash/gui/abc/MethodTraitDetailPanel.java @@ -1,57 +1,57 @@ -/* - * Copyright (C) 2010-2014 JPEXS - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.jpexs.decompiler.flash.gui.abc; - -import java.awt.BorderLayout; -import javax.swing.JPanel; - -/** - * - * @author JPEXS - */ -public class MethodTraitDetailPanel extends JPanel implements TraitDetail { - - public MethodCodePanel methodCodePanel; - public ABCPanel abcPanel; - - public MethodTraitDetailPanel(ABCPanel abcPanel) { - this.abcPanel = abcPanel; - methodCodePanel = new MethodCodePanel(abcPanel.decompiledTextArea); - setLayout(new BorderLayout()); - add(methodCodePanel, BorderLayout.CENTER); - } - - @Override - public boolean save() { - if (!methodCodePanel.save(abcPanel.abc.constants)) { - return false; - } - - return true; - } - - @Override - public void setEditMode(boolean val) { - methodCodePanel.setEditMode(val); - } - private boolean active = false; - - @Override - public void setActive(boolean val) { - this.active = val; - } -} +/* + * Copyright (C) 2010-2014 JPEXS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.jpexs.decompiler.flash.gui.abc; + +import java.awt.BorderLayout; +import javax.swing.JPanel; + +/** + * + * @author JPEXS + */ +public class MethodTraitDetailPanel extends JPanel implements TraitDetail { + + public MethodCodePanel methodCodePanel; + public ABCPanel abcPanel; + + public MethodTraitDetailPanel(ABCPanel abcPanel) { + this.abcPanel = abcPanel; + methodCodePanel = new MethodCodePanel(abcPanel.decompiledTextArea); + setLayout(new BorderLayout()); + add(methodCodePanel, BorderLayout.CENTER); + } + + @Override + public boolean save() { + if (!methodCodePanel.save()) { + return false; + } + + return true; + } + + @Override + public void setEditMode(boolean val) { + methodCodePanel.setEditMode(val); + } + private boolean active = false; + + @Override + public void setActive(boolean val) { + this.active = val; + } +}