diff --git a/trunk/src/com/jpexs/decompiler/flash/SWF.java b/trunk/src/com/jpexs/decompiler/flash/SWF.java index 8424f483d..eb7fc9b28 100644 --- a/trunk/src/com/jpexs/decompiler/flash/SWF.java +++ b/trunk/src/com/jpexs/decompiler/flash/SWF.java @@ -1743,7 +1743,7 @@ public class SWF { SymbolClassTag sc = (SymbolClassTag) tag; for (int i = 0; i < sc.classNames.length; i++) { String newname = deobfuscateNameWithPackage(sc.classNames[i], renameType, deobfuscated); - if(newname!=null){ + if (newname != null) { sc.classNames[i] = newname; } } diff --git a/trunk/src/com/jpexs/decompiler/flash/SWFInputStream.java b/trunk/src/com/jpexs/decompiler/flash/SWFInputStream.java index 80bffcbad..e3b62ee8a 100644 --- a/trunk/src/com/jpexs/decompiler/flash/SWFInputStream.java +++ b/trunk/src/com/jpexs/decompiler/flash/SWFInputStream.java @@ -1126,7 +1126,7 @@ public class SWFInputStream extends InputStream { StringBuilder sb = new StringBuilder(); sb.append(Helper.formatHex((int) tag.getPos(), 8)); sb.append(": "); - sb.append(Helper.indent(level, "")); + sb.append(Helper.indent(level, "", " ")); sb.append(Helper.format(tag.toString(), 25 - 2 * level)); sb.append(" tagId="); sb.append(Helper.formatInt(tag.getId(), 3)); diff --git a/trunk/src/com/jpexs/decompiler/flash/TagNode.java b/trunk/src/com/jpexs/decompiler/flash/TagNode.java index 0d4335b52..df0a65195 100644 --- a/trunk/src/com/jpexs/decompiler/flash/TagNode.java +++ b/trunk/src/com/jpexs/decompiler/flash/TagNode.java @@ -17,6 +17,7 @@ package com.jpexs.decompiler.flash; import com.jpexs.decompiler.flash.action.Action; +import com.jpexs.decompiler.flash.graph.Graph; import com.jpexs.decompiler.flash.helpers.Helper; import com.jpexs.decompiler.flash.helpers.Highlighting; import com.jpexs.decompiler.flash.tags.DefineBitsJPEG2Tag; @@ -271,11 +272,11 @@ public class TagNode { String res; ASMSource asm = ((ASMSource) node.tag); if (isPcode) { - res = asm.getActionSourcePrefix() + Highlighting.stripHilights(asm.getASMSource(SWF.DEFAULT_VERSION, false)) + asm.getActionSourceSuffix(); + res = asm.getActionSourcePrefix() + Helper.indentRows(asm.getActionSourceIndent(), Highlighting.stripHilights(asm.getASMSource(SWF.DEFAULT_VERSION, false)), Graph.INDENT_STRING) + asm.getActionSourceSuffix(); } else { List as = asm.getActions(SWF.DEFAULT_VERSION); Action.setActionsAddresses(as, 0, SWF.DEFAULT_VERSION); - res = asm.getActionSourcePrefix() + Highlighting.stripHilights(Action.actionsToSource(as, SWF.DEFAULT_VERSION)) + asm.getActionSourceSuffix(); + res = asm.getActionSourcePrefix() + Helper.indentRows(asm.getActionSourceIndent(), Highlighting.stripHilights(Action.actionsToSource(as, SWF.DEFAULT_VERSION)), Graph.INDENT_STRING) + asm.getActionSourceSuffix(); } try (FileOutputStream fos = new FileOutputStream(f)) { fos.write(res.getBytes("utf-8")); diff --git a/trunk/src/com/jpexs/decompiler/flash/graph/Graph.java b/trunk/src/com/jpexs/decompiler/flash/graph/Graph.java index 0c9f900f8..1acd1f6ed 100644 --- a/trunk/src/com/jpexs/decompiler/flash/graph/Graph.java +++ b/trunk/src/com/jpexs/decompiler/flash/graph/Graph.java @@ -2174,7 +2174,7 @@ public class Graph { * String used to unindent line when converting to string */ public static final String INDENTCLOSE = "INDENTCLOSE"; - private static final String INDENT_STRING = " "; + public static final String INDENT_STRING = " "; private static String tabString(int len) { String ret = ""; diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/abc/DecompiledEditorPane.java b/trunk/src/com/jpexs/decompiler/flash/gui/abc/DecompiledEditorPane.java index 2b567c0f0..b1dc1b102 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/abc/DecompiledEditorPane.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/abc/DecompiledEditorPane.java @@ -359,7 +359,7 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements CaretL this.abc = abc; this.abcList = abcList; this.script = scriptLeaf; - + String hilightedCode = ""; cacheScriptPack(scriptLeaf, abcList); CachedDecompilation cd = getCached(scriptLeaf); @@ -367,7 +367,7 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements CaretL highlights = cd.getHighlights(); traitHighlights = cd.getTraitHighlights(); methodHighlights = cd.getMethodHighlights(); - classHighlights = cd.getClassHighlights(); + classHighlights = cd.getClassHighlights(); setText(hilightedCode); } diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/abc/LineMarkedEditorPane.java b/trunk/src/com/jpexs/decompiler/flash/gui/abc/LineMarkedEditorPane.java index 7bdb5bf55..0895ae761 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/abc/LineMarkedEditorPane.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/abc/LineMarkedEditorPane.java @@ -23,6 +23,7 @@ import javax.swing.JEditorPane; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.Element; +import jsyntaxpane.actions.ActionUtils; /** * @@ -32,6 +33,14 @@ public class LineMarkedEditorPane extends JEditorPane { int lastLine = -1; + public int getLine() { + return lastLine; + } + + public void gotoLine(int line) { + setCaretPosition(ActionUtils.getDocumentPosition(this, line, 0)); + } + public LineMarkedEditorPane() { setOpaque(false); addCaretListener(new CaretListener() { 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 e482afca6..02ec63dd8 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java @@ -1,16 +1,16 @@ /* * Copyright (C) 2010-2013 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 . */ @@ -27,6 +27,7 @@ import com.jpexs.decompiler.flash.action.parser.pcode.ASMParser; import com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser; import com.jpexs.decompiler.flash.action.swf4.ActionPush; import com.jpexs.decompiler.flash.action.swf4.ConstantIndex; +import com.jpexs.decompiler.flash.graph.Graph; import com.jpexs.decompiler.flash.graph.GraphTargetItem; import static com.jpexs.decompiler.flash.gui.AppStrings.translate; import com.jpexs.decompiler.flash.gui.GraphFrame; @@ -296,7 +297,7 @@ public class ActionPanel extends JPanel implements ActionListener { lastDecompiled = sc.text; lastASM = asm; stripped = lastDecompiled; - decompiledEditor.setText(asm.getActionSourcePrefix() + lastDecompiled + asm.getActionSourceSuffix()); + decompiledEditor.setText(asm.getActionSourcePrefix() + Helper.indentRows(asm.getActionSourceIndent(), lastDecompiled, Graph.INDENT_STRING) + asm.getActionSourceSuffix()); } setEditMode(false); setDecompiledEditMode(false); @@ -542,13 +543,15 @@ public class ActionPanel extends JPanel implements ActionListener { public void setDecompiledEditMode(boolean val) { String pref = lastASM.getActionSourcePrefix(); int lastPos = decompiledEditor.getCaretPosition(); + int lastLine = decompiledEditor.getLine(); + int prefLines = Helper.getLineCount(pref); if (val) { String newText = lastDecompiled; decompiledEditor.setText(newText); - if (lastPos > -1) { + if (lastLine > -1) { int newpos = lastPos - pref.length(); - if (newpos < newText.length() && newpos >= 0) { - decompiledEditor.setCaretPosition(newpos); + if (lastLine - prefLines >= 0) { + decompiledEditor.gotoLine(lastLine - prefLines + 1); } } decompiledEditor.setEditable(true); @@ -559,13 +562,10 @@ public class ActionPanel extends JPanel implements ActionListener { decompiledEditor.getCaret().setVisible(true); decLabel.setIcon(View.getIcon("editing16")); } else { - String newText = pref + lastDecompiled + lastASM.getActionSourceSuffix(); + String newText = pref + Helper.indentRows(lastASM.getActionSourceIndent(), lastDecompiled, Graph.INDENT_STRING) + lastASM.getActionSourceSuffix(); decompiledEditor.setText(newText); - if (lastPos > -1) { - int newpos = lastPos + pref.length(); - if (newpos < newText.length()) { - decompiledEditor.setCaretPosition(newpos); - } + if (lastLine > -1) { + decompiledEditor.gotoLine(lastLine + prefLines + 1); } decompiledEditor.setEditable(false); saveDecompiledButton.setVisible(false); diff --git a/trunk/src/com/jpexs/decompiler/flash/helpers/Helper.java b/trunk/src/com/jpexs/decompiler/flash/helpers/Helper.java index 68c1d5db3..b4b56daaf 100644 --- a/trunk/src/com/jpexs/decompiler/flash/helpers/Helper.java +++ b/trunk/src/com/jpexs/decompiler/flash/helpers/Helper.java @@ -175,15 +175,43 @@ public class Helper { return sb.toString(); } - public static String indent(int level, String ss) { + public static String indent(int level, String ss, String indentStr) { StringBuilder sb = new StringBuilder(); - for (int ii = 0; ii < level * 2; ii++) { - sb.append(' '); + for (int ii = 0; ii < level; ii++) { + sb.append(indentStr); } sb.append(ss); return sb.toString(); } + public static String indentRows(int level, String ss, String indentStr) { + StringBuilder sb = new StringBuilder(); + for (int ii = 0; ii < level; ii++) { + sb.append(indentStr); + } + ss = ss.replaceAll("(\r\n|\r|\n)", "\r\n"); + ss = "\r\n" + ss; + String repl = "\r\n" + sb.toString(); + ss = ss.replace("\r\n", repl); + if (ss.endsWith(repl)) { + ss = ss.substring(0, ss.length() - sb.toString().length()); + } + ss = ss.substring(2); + return ss; + } + + public static int getLineCount(String s) { + if (s.endsWith("\r\n")) { + s = s.substring(0, s.length() - 2); + } else if (s.endsWith("\r")) { + s = s.substring(0, s.length() - 1); + } else if (s.endsWith("\n")) { + s = s.substring(0, s.length() - 1); + } + String parts[] = s.split("(\r\n|\r|\n)"); + return parts.length; + } + public static String padZeros(long number, int length) { String ret = "" + number; while (ret.length() < length) { diff --git a/trunk/src/com/jpexs/decompiler/flash/tags/DefineButtonTag.java b/trunk/src/com/jpexs/decompiler/flash/tags/DefineButtonTag.java index 407e1df6e..2fb9f4000 100644 --- a/trunk/src/com/jpexs/decompiler/flash/tags/DefineButtonTag.java +++ b/trunk/src/com/jpexs/decompiler/flash/tags/DefineButtonTag.java @@ -304,4 +304,9 @@ public class DefineButtonTag extends CharacterTag implements ASMSource, BoundedT public String getActionSourceSuffix() { return ""; } + + @Override + public int getActionSourceIndent() { + return 0; + } } diff --git a/trunk/src/com/jpexs/decompiler/flash/tags/DoActionTag.java b/trunk/src/com/jpexs/decompiler/flash/tags/DoActionTag.java index c7bd04df8..6f63924f7 100644 --- a/trunk/src/com/jpexs/decompiler/flash/tags/DoActionTag.java +++ b/trunk/src/com/jpexs/decompiler/flash/tags/DoActionTag.java @@ -160,4 +160,9 @@ public class DoActionTag extends Tag implements ASMSource { public String getActionSourceSuffix() { return ""; } + + @Override + public int getActionSourceIndent() { + return 0; + } } diff --git a/trunk/src/com/jpexs/decompiler/flash/tags/DoInitActionTag.java b/trunk/src/com/jpexs/decompiler/flash/tags/DoInitActionTag.java index 44649d666..4b2993c29 100644 --- a/trunk/src/com/jpexs/decompiler/flash/tags/DoInitActionTag.java +++ b/trunk/src/com/jpexs/decompiler/flash/tags/DoInitActionTag.java @@ -203,4 +203,9 @@ public class DoInitActionTag extends CharacterIdTag implements ASMSource { public String getActionSourceSuffix() { return ""; } + + @Override + public int getActionSourceIndent() { + return 0; + } } 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 12167e867..de5d70416 100644 --- a/trunk/src/com/jpexs/decompiler/flash/tags/base/ASMSource.java +++ b/trunk/src/com/jpexs/decompiler/flash/tags/base/ASMSource.java @@ -72,4 +72,6 @@ public interface ASMSource { public String getActionSourcePrefix(); public String getActionSourceSuffix(); + + public int getActionSourceIndent(); } diff --git a/trunk/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java b/trunk/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java index 3733530ad..f5e4639b6 100644 --- a/trunk/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java +++ b/trunk/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java @@ -261,4 +261,9 @@ public class BUTTONCONDACTION implements ASMSource, Exportable { public String getExportFileName() { 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 5d34b1f6a..9e90d2cb5 100644 --- a/trunk/src/com/jpexs/decompiler/flash/types/CLIPACTIONRECORD.java +++ b/trunk/src/com/jpexs/decompiler/flash/types/CLIPACTIONRECORD.java @@ -218,4 +218,9 @@ public class CLIPACTIONRECORD implements ASMSource, Exportable { public String getExportFileName() { return eventFlags.getHeader(keyCode, true); } + + @Override + public int getActionSourceIndent() { + return 1; + } }