as2 export fix, as2 edit fix (prefix, suffix problem)

This commit is contained in:
Honfika
2013-11-01 15:01:34 +01:00
parent 9b5acbcaee
commit 6d264576e4
12 changed files with 124 additions and 63 deletions

View File

@@ -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<Action> 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"));

View File

@@ -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<Action> actions, final int version, final String path, boolean highlight, int indent) {
public static void actionsToSource(final List<Action> actions, final int version, final String path, HilightedTextWriter writer) {
List<GraphTargetItem> 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);
}
/**

View File

@@ -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<Highlighting> 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<Highlighting> 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);

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -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<Tag> tags) {
return getHeader(true);
}
@Override
public int getActionSourceIndent() {
return 1;
}
}

View File

@@ -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<Tag> tags) {
return eventFlags.getHeader(keyCode, true);
}
@Override
public int getActionSourceIndent() {
return 1;
}
}

View File

@@ -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() {

View File

@@ -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);