diff --git a/trunk/src/com/jpexs/decompiler/flash/TagNode.java b/trunk/src/com/jpexs/decompiler/flash/TagNode.java index 2cd17b356..66681cc6a 100644 --- a/trunk/src/com/jpexs/decompiler/flash/TagNode.java +++ b/trunk/src/com/jpexs/decompiler/flash/TagNode.java @@ -299,18 +299,25 @@ public class TagNode { String res; ASMSource asm = ((ASMSource) node.tag); if (exportMode == ExportMode.HEX) { - HilightedTextWriter writer = new HilightedTextWriter(false, asm.getActionSourceIndent()); + HilightedTextWriter writer = new HilightedTextWriter(false); + asm.getActionSourcePrefix(writer); asm.getActionBytesAsHex(writer); - res = asm.getActionSourcePrefix() + writer.toString() + asm.getActionSourceSuffix(); + asm.getActionSourceSuffix(writer); + res = writer.toString(); } else if (exportMode != ExportMode.SOURCE) { - HilightedTextWriter writer = new HilightedTextWriter(false, asm.getActionSourceIndent()); + HilightedTextWriter writer = new HilightedTextWriter(false); + asm.getActionSourcePrefix(writer); asm.getASMSource(SWF.DEFAULT_VERSION, exportMode, writer, null); - String str = writer.toString(); - res = asm.getActionSourcePrefix() + str + asm.getActionSourceSuffix(); + asm.getActionSourceSuffix(writer); + res = writer.toString(); } else { List as = asm.getActions(SWF.DEFAULT_VERSION); Action.setActionsAddresses(as, 0, SWF.DEFAULT_VERSION); - res = asm.getActionSourcePrefix() + Action.actionsToSource(as, SWF.DEFAULT_VERSION, ""/*FIXME*/, false, asm.getActionSourceIndent()) + asm.getActionSourceSuffix(); + HilightedTextWriter writer = new HilightedTextWriter(false); + asm.getActionSourcePrefix(writer); + Action.actionsToSource(as, SWF.DEFAULT_VERSION, ""/*FIXME*/, writer); + asm.getActionSourceSuffix(writer); + res = writer.toString(); } try (FileOutputStream fos = new FileOutputStream(f)) { fos.write(res.getBytes("utf-8")); diff --git a/trunk/src/com/jpexs/decompiler/flash/action/Action.java b/trunk/src/com/jpexs/decompiler/flash/action/Action.java index cb06bdfba..7a9fec799 100644 --- a/trunk/src/com/jpexs/decompiler/flash/action/Action.java +++ b/trunk/src/com/jpexs/decompiler/flash/action/Action.java @@ -48,7 +48,6 @@ import com.jpexs.decompiler.flash.action.swf5.*; import com.jpexs.decompiler.flash.action.swf7.ActionDefineFunction2; import com.jpexs.decompiler.flash.ecma.Null; import com.jpexs.decompiler.flash.helpers.GraphTextWriter; -import com.jpexs.decompiler.flash.helpers.HilightedText; import com.jpexs.decompiler.flash.helpers.HilightedTextWriter; import com.jpexs.decompiler.flash.helpers.NulWriter; import com.jpexs.decompiler.flash.helpers.collections.MyEntry; @@ -698,7 +697,7 @@ public class Action implements GraphSourceItem { * @param path * @return String with Source code */ - public static HilightedText actionsToSource(final List actions, final int version, final String path, boolean highlight, int indent) { + public static void actionsToSource(final List actions, final int version, final String path, HilightedTextWriter writer) { List tree = null; Throwable convertException = null; int timeout = Configuration.getConfig("decompilationTimeoutSingleMethod", 60); @@ -720,7 +719,6 @@ public class Action implements GraphSourceItem { } } - HilightedTextWriter writer = new HilightedTextWriter(highlight, indent); if (convertException == null) { Graph.graphToString(tree, writer, new LocalData()); } else if (convertException instanceof TimeoutException) { @@ -739,7 +737,6 @@ public class Action implements GraphSourceItem { writer.appendNoHilight(" */").newLine(); writer.appendNoHilight("throw new IllegalOperationError(\"Not decompiled due to error\");").newLine(); } - return new HilightedText(writer); } /** diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java b/trunk/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java index d669a1e3b..249c841a1 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java @@ -174,9 +174,12 @@ public class ActionPanel extends JPanel implements ActionListener { if (actions == null) { actions = src.getActions(SWF.DEFAULT_VERSION); } - HilightedText text = Action.actionsToSource(actions, SWF.DEFAULT_VERSION, src.toString()/*FIXME?*/, true, 0); - List hilights = text.instructionHilights; - String srcNoHex = text.text; + HilightedTextWriter writer = new HilightedTextWriter(true); + src.getActionSourcePrefix(writer); + Action.actionsToSource(actions, SWF.DEFAULT_VERSION, src.toString()/*FIXME?*/, writer); + src.getActionSourceSuffix(writer); + List hilights = writer.instructionHilights; + String srcNoHex = writer.toString(); cache.put(src, new CachedScript(srcNoHex, hilights)); } } @@ -349,7 +352,6 @@ public class ActionPanel extends JPanel implements ActionListener { setHex(getExportMode()); if (Configuration.getConfig("decompile", true)) { decompiledEditor.setText("//" + translate("work.decompiling") + "..."); - String stripped = ""; if (!useCache) { uncache(asm); } @@ -358,8 +360,7 @@ public class ActionPanel extends JPanel implements ActionListener { decompiledHilights = sc.hilights; lastDecompiled = sc.text; lastASM = asm; - stripped = lastDecompiled; - decompiledEditor.setText(asm.getActionSourcePrefix() + Helper.indentRows(asm.getActionSourceIndent(), lastDecompiled, HilightedTextWriter.INDENT_STRING) + asm.getActionSourceSuffix()); + decompiledEditor.setText(lastDecompiled); } setEditMode(false); setDecompiledEditMode(false); @@ -559,7 +560,6 @@ public class ActionPanel extends JPanel implements ActionListener { } decompiledEditor.getCaret().setVisible(true); int pos = decompiledEditor.getCaretPosition(); - System.out.println("pos: " + pos); Highlighting h = Highlighting.search(decompiledHilights, pos); if (h != null) { Highlighting h2 = Highlighting.search(disassembledHilights, "offset", h.getPropertyString("offset")); @@ -620,15 +620,12 @@ public class ActionPanel extends JPanel implements ActionListener { return; } - String pref = lastASM.getActionSourcePrefix(); - int lastPos = decompiledEditor.getCaretPosition(); int lastLine = decompiledEditor.getLine(); - int prefLines = Helper.getLineCount(pref); + int prefLines = lastASM.getPrefixLineCount(); if (val) { - String newText = lastDecompiled; + String newText = lastASM.removePrefixAndSuffix(lastDecompiled); decompiledEditor.setText(newText); if (lastLine > -1) { - int newpos = lastPos - pref.length(); if (lastLine - prefLines >= 0) { decompiledEditor.gotoLine(lastLine - prefLines + 1); } @@ -641,7 +638,7 @@ public class ActionPanel extends JPanel implements ActionListener { decompiledEditor.getCaret().setVisible(true); decLabel.setIcon(View.getIcon("editing16")); } else { - String newText = pref + Helper.indentRows(lastASM.getActionSourceIndent(), lastDecompiled, HilightedTextWriter.INDENT_STRING) + lastASM.getActionSourceSuffix(); + String newText = lastDecompiled; decompiledEditor.setText(newText); if (lastLine > -1) { decompiledEditor.gotoLine(lastLine + prefLines + 1); diff --git a/trunk/src/com/jpexs/decompiler/flash/helpers/HilightedTextWriter.java b/trunk/src/com/jpexs/decompiler/flash/helpers/HilightedTextWriter.java index 843e40728..5a3db523f 100644 --- a/trunk/src/com/jpexs/decompiler/flash/helpers/HilightedTextWriter.java +++ b/trunk/src/com/jpexs/decompiler/flash/helpers/HilightedTextWriter.java @@ -36,6 +36,7 @@ import java.util.Stack; public class HilightedTextWriter extends GraphTextWriter { public static final String INDENT_STRING = " "; + public static final String NEW_LINE = "\r\n"; private StringBuilder sb = new StringBuilder(); private boolean hilight; private boolean newLine = true; @@ -221,7 +222,7 @@ public class HilightedTextWriter extends GraphTextWriter { @Override public HilightedTextWriter newLine() { - appendToSb("\r\n"); + appendToSb(NEW_LINE); newLine = true; newLineCount++; return this; diff --git a/trunk/src/com/jpexs/decompiler/flash/tags/DefineButtonTag.java b/trunk/src/com/jpexs/decompiler/flash/tags/DefineButtonTag.java index 9af76378c..a14f6bd5d 100644 --- a/trunk/src/com/jpexs/decompiler/flash/tags/DefineButtonTag.java +++ b/trunk/src/com/jpexs/decompiler/flash/tags/DefineButtonTag.java @@ -304,17 +304,22 @@ public class DefineButtonTag extends CharacterTag implements ASMSource, BoundedT } @Override - public String getActionSourcePrefix() { - return ""; + public GraphTextWriter getActionSourcePrefix(GraphTextWriter writer) { + return writer; } @Override - public String getActionSourceSuffix() { - return ""; + public GraphTextWriter getActionSourceSuffix(GraphTextWriter writer) { + return writer; } @Override - public int getActionSourceIndent() { + public int getPrefixLineCount() { return 0; } + + @Override + public String removePrefixAndSuffix(String source) { + return source; + } } diff --git a/trunk/src/com/jpexs/decompiler/flash/tags/DoActionTag.java b/trunk/src/com/jpexs/decompiler/flash/tags/DoActionTag.java index 49a411e8c..e6c346ce1 100644 --- a/trunk/src/com/jpexs/decompiler/flash/tags/DoActionTag.java +++ b/trunk/src/com/jpexs/decompiler/flash/tags/DoActionTag.java @@ -161,17 +161,22 @@ public class DoActionTag extends Tag implements ASMSource { } @Override - public String getActionSourcePrefix() { - return ""; + public GraphTextWriter getActionSourcePrefix(GraphTextWriter writer) { + return writer; } @Override - public String getActionSourceSuffix() { - return ""; + public GraphTextWriter getActionSourceSuffix(GraphTextWriter writer) { + return writer; } @Override - public int getActionSourceIndent() { + public int getPrefixLineCount() { return 0; } + + @Override + public String removePrefixAndSuffix(String source) { + return source; + } } diff --git a/trunk/src/com/jpexs/decompiler/flash/tags/DoInitActionTag.java b/trunk/src/com/jpexs/decompiler/flash/tags/DoInitActionTag.java index b599d8e40..161b9236c 100644 --- a/trunk/src/com/jpexs/decompiler/flash/tags/DoInitActionTag.java +++ b/trunk/src/com/jpexs/decompiler/flash/tags/DoInitActionTag.java @@ -204,17 +204,22 @@ public class DoInitActionTag extends CharacterIdTag implements ASMSource { } @Override - public String getActionSourcePrefix() { - return ""; + public GraphTextWriter getActionSourcePrefix(GraphTextWriter writer) { + return writer; } @Override - public String getActionSourceSuffix() { - return ""; + public GraphTextWriter getActionSourceSuffix(GraphTextWriter writer) { + return writer; } @Override - public int getActionSourceIndent() { + public int getPrefixLineCount() { return 0; } + + @Override + public String removePrefixAndSuffix(String source) { + return source; + } } diff --git a/trunk/src/com/jpexs/decompiler/flash/tags/base/ASMSource.java b/trunk/src/com/jpexs/decompiler/flash/tags/base/ASMSource.java index 88bba9850..8b44efa11 100644 --- a/trunk/src/com/jpexs/decompiler/flash/tags/base/ASMSource.java +++ b/trunk/src/com/jpexs/decompiler/flash/tags/base/ASMSource.java @@ -73,9 +73,11 @@ public interface ASMSource { public void removeDisassemblyListener(DisassemblyListener listener); - public String getActionSourcePrefix(); + public GraphTextWriter getActionSourcePrefix(GraphTextWriter writer); - public String getActionSourceSuffix(); + public GraphTextWriter getActionSourceSuffix(GraphTextWriter writer); - public int getActionSourceIndent(); + public int getPrefixLineCount(); + + public String removePrefixAndSuffix(String source); } diff --git a/trunk/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java b/trunk/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java index 06c04a09e..7e8ba3859 100644 --- a/trunk/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java +++ b/trunk/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java @@ -257,22 +257,30 @@ public class BUTTONCONDACTION implements ASMSource, Exportable, ContainerItem { } @Override - public String getActionSourcePrefix() { - return getHeader(false) + "{\r\n"; + public GraphTextWriter getActionSourcePrefix(GraphTextWriter writer) { + writer.appendNoHilight(getHeader(false)); + writer.appendNoHilight("{").newLine(); + return writer.indent(); } @Override - public String getActionSourceSuffix() { - return "}\r\n"; + public GraphTextWriter getActionSourceSuffix(GraphTextWriter writer) { + writer.unindent(); + return writer.appendNoHilight("}").newLine(); + } + + @Override + public int getPrefixLineCount() { + return 1; + } + + @Override + public String removePrefixAndSuffix(String source) { + return Helper.unindentRows(1, 1, source); } @Override public String getExportFileName(List tags) { return getHeader(true); } - - @Override - public int getActionSourceIndent() { - return 1; - } } diff --git a/trunk/src/com/jpexs/decompiler/flash/types/CLIPACTIONRECORD.java b/trunk/src/com/jpexs/decompiler/flash/types/CLIPACTIONRECORD.java index 6305c030d..954d93c2e 100644 --- a/trunk/src/com/jpexs/decompiler/flash/types/CLIPACTIONRECORD.java +++ b/trunk/src/com/jpexs/decompiler/flash/types/CLIPACTIONRECORD.java @@ -215,22 +215,30 @@ public class CLIPACTIONRECORD implements ASMSource, Exportable, ContainerItem { } @Override - public String getActionSourcePrefix() { - return eventFlags.getHeader(keyCode, false) + "{\r\n"; + public GraphTextWriter getActionSourcePrefix(GraphTextWriter writer) { + writer.appendNoHilight(eventFlags.getHeader(keyCode, false)); + writer.appendNoHilight("{").newLine(); + return writer.indent(); } @Override - public String getActionSourceSuffix() { - return "}\r\n"; + public GraphTextWriter getActionSourceSuffix(GraphTextWriter writer) { + writer.unindent(); + return writer.appendNoHilight("}").newLine(); + } + + @Override + public int getPrefixLineCount() { + return 1; + } + + @Override + public String removePrefixAndSuffix(String source) { + return Helper.unindentRows(1, 1, source); } @Override public String getExportFileName(List tags) { return eventFlags.getHeader(keyCode, true); } - - @Override - public int getActionSourceIndent() { - return 1; - } } diff --git a/trunk/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java b/trunk/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java index 85beb25df..9a90cbc8d 100644 --- a/trunk/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java +++ b/trunk/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java @@ -22,7 +22,7 @@ import com.jpexs.decompiler.flash.RunnableIOEx; import com.jpexs.decompiler.flash.SWF; import com.jpexs.decompiler.flash.SWFInputStream; import com.jpexs.decompiler.flash.action.Action; -import com.jpexs.decompiler.flash.helpers.HilightedText; +import com.jpexs.decompiler.flash.helpers.HilightedTextWriter; import com.jpexs.decompiler.flash.tags.CSMTextSettingsTag; import com.jpexs.decompiler.flash.tags.DefineButton2Tag; import com.jpexs.decompiler.flash.tags.DefineButtonCxformTag; @@ -1112,8 +1112,11 @@ public class XFLConverter { } private static String convertActionScript(ASMSource as) { - HilightedText decompiledAS = Action.actionsToSource(as.getActions(SWF.DEFAULT_VERSION), SWF.DEFAULT_VERSION, as.toString(), false, as.getActionSourceIndent()); - return as.getActionSourcePrefix() + decompiledAS.text + as.getActionSourceSuffix(); + HilightedTextWriter writer = new HilightedTextWriter(false); + as.getActionSourcePrefix(writer); + Action.actionsToSource(as.getActions(SWF.DEFAULT_VERSION), SWF.DEFAULT_VERSION, as.toString(), writer); + as.getActionSourceSuffix(writer); + return writer.toString(); } private static long getTimestamp() { diff --git a/trunk/src/com/jpexs/helpers/Helper.java b/trunk/src/com/jpexs/helpers/Helper.java index 579092fe1..86e098f33 100644 --- a/trunk/src/com/jpexs/helpers/Helper.java +++ b/trunk/src/com/jpexs/helpers/Helper.java @@ -37,6 +37,7 @@ import java.util.ArrayList; import java.util.BitSet; import java.util.Collection; import java.util.List; +import java.util.Scanner; import java.util.Stack; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @@ -217,6 +218,28 @@ public class Helper { return ss; } + public static String unindentRows(int prefixLineCount, int level, String text) { + StringBuilder sb = new StringBuilder(); + Scanner scanner = new Scanner(text); + String indentStr = ""; + for (int i = 0; i < level; i++) { + indentStr += HilightedTextWriter.INDENT_STRING; + } + int indentLength = indentStr.length(); + for (int i = 0; i < prefixLineCount; i++) { + scanner.nextLine(); // ignore line + } + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if (line.startsWith(indentStr)) { + sb.append(line.substring(indentLength)).append(HilightedTextWriter.NEW_LINE); + } else { + return sb.toString(); + } + } + return sb.toString(); + } + public static int getLineCount(String s) { if (s.endsWith("\r\n")) { s = s.substring(0, s.length() - 2);