highlighting refactored

This commit is contained in:
Honfika
2013-11-01 01:07:19 +01:00
parent 2cf26a4c31
commit d23c2dbe63
242 changed files with 1572 additions and 1075 deletions
@@ -26,6 +26,7 @@ import com.jpexs.decompiler.flash.abc.avm2.parser.ParseException;
import com.jpexs.decompiler.flash.abc.types.traits.Trait;
import com.jpexs.decompiler.flash.gui.GraphFrame;
import com.jpexs.decompiler.flash.gui.View;
import com.jpexs.decompiler.flash.helpers.HilightedText;
import com.jpexs.decompiler.flash.helpers.HilightedTextWriter;
import com.jpexs.decompiler.flash.helpers.hilight.Highlighting;
import com.jpexs.decompiler.graph.ExportMode;
@@ -49,29 +50,43 @@ public class ASMSourceEditorPane extends LineMarkedEditorPane implements CaretLi
private DecompiledEditorPane decompiledEditor;
private boolean ignoreCarret = false;
private String name;
private String textWithHex = "";
private String textNoHex = "";
private boolean hex = false;
private HilightedText textWithHex;
private HilightedText textNoHex;
private HilightedText textHexOnly;
private ExportMode exportMode = ExportMode.PCODE;
private Trait trait;
public boolean isHex() {
return hex;
public ExportMode getExportMode() {
return exportMode;
}
public void switchHex() {
setHex(!hex);
private HilightedText getHilightedText(ExportMode exportMode) {
HilightedTextWriter writer = new HilightedTextWriter(true);
abc.bodies[bodyIndex].code.toASMSource(abc.constants, trait, abc.method_info[abc.bodies[bodyIndex].method_info], abc.bodies[bodyIndex], exportMode, writer);
return new HilightedText(writer);
}
public void setHex(boolean hex) {
if (this.hex == hex) {
public void setHex(ExportMode exportMode, boolean force) {
if (this.exportMode == exportMode & !force) {
return;
}
this.hex = hex;
this.exportMode = exportMode;
long oldOffset = getSelectedOffset();
if (hex) {
if (exportMode == ExportMode.PCODE) {
if (textNoHex == null) {
textNoHex = getHilightedText(exportMode);
}
setText(textNoHex);
} else if (exportMode == ExportMode.PCODEWITHHEX) {
if (textWithHex == null) {
textWithHex = getHilightedText(exportMode);
}
setText(textWithHex);
} else {
setText(textNoHex);
if (textHexOnly == null) {
textHexOnly = new HilightedText(Helper.byteArrToString(abc.bodies[bodyIndex].code.getBytes()));
}
setText(textWithHex);
}
hilighOffset(oldOffset);
}
@@ -135,12 +150,9 @@ public class ASMSourceEditorPane extends LineMarkedEditorPane implements CaretLi
if (bodyIndex == -1) {
return;
}
HilightedTextWriter writer = new HilightedTextWriter(true);
abc.bodies[bodyIndex].code.toASMSource(abc.constants, trait, abc.method_info[abc.bodies[bodyIndex].method_info], abc.bodies[bodyIndex], ExportMode.PCODEWITHHEX, writer);
String textWithHexTags = writer.toString();
textWithHex = Helper.hexToComments(textWithHexTags);
textNoHex = Helper.stripComments(textWithHexTags);
setText(hex ? textWithHex : textNoHex);
textWithHex = null;
textNoHex = null;
setHex(exportMode, true);
}
public void graph() {
@@ -193,13 +205,19 @@ public class ASMSourceEditorPane extends LineMarkedEditorPane implements CaretLi
@Override
public void setText(String t) {
disassembledHilights = Highlighting.getInstrHighlights(t);
specialHilights = Highlighting.getSpecialHighlights(t);
t = Highlighting.stripHilights(t);
disassembledHilights = new ArrayList<>();
specialHilights = new ArrayList<>();
super.setText(t);
setCaretPosition(0);
}
public void setText(HilightedText hilightedText) {
disassembledHilights = hilightedText.instructionHilights;
specialHilights = hilightedText.specialHilights;
super.setText(hilightedText.text);
setCaretPosition(0);
}
public void selectInstruction(int pos) {
String text = getText();
int lineCnt = 1;
@@ -16,6 +16,7 @@
*/
package com.jpexs.decompiler.flash.gui.abc;
import com.jpexs.decompiler.flash.helpers.HilightedText;
import com.jpexs.decompiler.flash.helpers.hilight.Highlighting;
import java.io.Serializable;
import java.util.List;
@@ -27,30 +28,38 @@ import java.util.List;
public class CachedDecompilation implements Serializable {
public String text;
public String hilightedText;
public List<Highlighting> traitHilights;
public List<Highlighting> classHilights;
public List<Highlighting> methodHilights;
public List<Highlighting> instructionHilights;
public List<Highlighting> specialHilights;
public List<Highlighting> getHighlights() {
return Highlighting.getInstrHighlights(hilightedText);
public List<Highlighting> getInstructionHighlights() {
return instructionHilights;
}
public List<Highlighting> getTraitHighlights() {
return Highlighting.getTraitHighlights(hilightedText);
return traitHilights;
}
public List<Highlighting> getMethodHighlights() {
return Highlighting.getMethodHighlights(hilightedText);
return methodHilights;
}
public List<Highlighting> getClassHighlights() {
return Highlighting.getClassHighlights(hilightedText);
return classHilights;
}
public List<Highlighting> getSpecialHighligths() {
return Highlighting.getSpecialHighlights(hilightedText);
return specialHilights;
}
public CachedDecompilation(String hilightedText) {
this.hilightedText = hilightedText;
this.text = Highlighting.stripHilights(hilightedText);
public CachedDecompilation(HilightedText hilightedText) {
this.text = hilightedText.text;
this.traitHilights = hilightedText.traitHilights;
this.classHilights = hilightedText.classHilights;
this.methodHilights = hilightedText.methodHilights;
this.instructionHilights = hilightedText.instructionHilights;
this.specialHilights = hilightedText.specialHilights;
}
}
@@ -28,11 +28,11 @@ import com.jpexs.decompiler.flash.abc.types.traits.TraitMethodGetterSetter;
import com.jpexs.decompiler.flash.abc.types.traits.TraitSlotConst;
import static com.jpexs.decompiler.flash.gui.AppStrings.translate;
import com.jpexs.decompiler.flash.gui.View;
import com.jpexs.decompiler.flash.helpers.HilightedText;
import com.jpexs.decompiler.flash.helpers.HilightedTextWriter;
import com.jpexs.decompiler.flash.helpers.hilight.Highlighting;
import com.jpexs.decompiler.flash.tags.ABCContainerTag;
import com.jpexs.decompiler.graph.ExportMode;
import com.jpexs.decompiler.graph.Graph;
import com.jpexs.helpers.Cache;
import java.util.ArrayList;
import java.util.List;
@@ -343,7 +343,7 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements CaretL
abcPanel.detailPanel.showCard(DetailPanel.SLOT_CONST_TRAIT_CARD, currentTrait);
abcPanel.detailPanel.setEditMode(false);
currentMethodHighlight = null;
Highlighting spec = Highlighting.search(specialHighlights, pos, "type", "special", currentTraitHighlight.startPos, currentTraitHighlight.startPos + currentTraitHighlight.len);
Highlighting spec = Highlighting.search(specialHighlights, pos, null, null, currentTraitHighlight.startPos, currentTraitHighlight.startPos + currentTraitHighlight.len);
if (spec != null) {
abcPanel.detailPanel.slotConstTraitPanel.hilightSpecial(spec);
}
@@ -445,19 +445,21 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements CaretL
public void cacheScriptPack(ScriptPack scriptLeaf, List<ABCContainerTag> abcList) {
int maxCacheSize = 50;
int scriptIndex = scriptLeaf.scriptIndex;
String hilightedCode = "";
HilightedText hilightedCode = null;
ScriptInfo script = null;
ABC abc = scriptLeaf.abc;
if (scriptIndex > -1) {
script = abc.script_info[scriptIndex];
}
if (!cache.contains(scriptLeaf)) {
for (int scriptTraitIndex : scriptLeaf.traitIndices) {
script.traits.traits[scriptTraitIndex].convertPackaged(null, scriptLeaf.getPath().toString(), abcList, abc, false, ExportMode.SOURCE, scriptIndex, -1, new ArrayList<String>(), Configuration.getConfig("parallelSpeedUp", true));
}
HilightedTextWriter writer = new HilightedTextWriter(true);
for (int scriptTraitIndex : scriptLeaf.traitIndices) {
script.traits.traits[scriptTraitIndex].convertPackaged(null, scriptLeaf.getPath().toString(), abcList, abc, false, ExportMode.SOURCE, scriptIndex, -1, writer, new ArrayList<String>(), Configuration.getConfig("parallelSpeedUp", true));
script.traits.traits[scriptTraitIndex].toStringPackaged(null, scriptLeaf.getPath().toString(), abcList, abc, false, ExportMode.SOURCE, scriptIndex, -1, writer, new ArrayList<String>(), Configuration.getConfig("parallelSpeedUp", true));
}
String s = Graph.removeNonRefenrencedLoopLabels(writer.toString(), true);
hilightedCode = s;
hilightedCode = new HilightedText(writer);
cache.put(scriptLeaf, new CachedDecompilation(hilightedCode));
}
}
@@ -487,7 +489,7 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements CaretL
cacheScriptPack(scriptLeaf, abcList);
CachedDecompilation cd = getCached(scriptLeaf);
final String hilightedCode = cd.text;
highlights = cd.getHighlights();
highlights = cd.getInstructionHighlights();
specialHighlights = cd.getSpecialHighligths();
traitHighlights = cd.getTraitHighlights();
methodHighlights = cd.getMethodHighlights();
@@ -22,6 +22,7 @@ import com.jpexs.decompiler.flash.abc.types.traits.Trait;
import static com.jpexs.decompiler.flash.gui.AppStrings.translate;
import com.jpexs.decompiler.flash.gui.Main;
import com.jpexs.decompiler.flash.gui.View;
import com.jpexs.decompiler.graph.ExportMode;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Insets;
@@ -42,6 +43,7 @@ public class MethodCodePanel extends JPanel implements ActionListener {
private ASMSourceEditorPane sourceTextArea;
public JPanel buttonsPanel;
private JToggleButton hexButton;
private JToggleButton hexOnlyButton;
public void focusEditor() {
sourceTextArea.requestFocusInWindow();
@@ -106,9 +108,18 @@ public class MethodCodePanel extends JPanel implements ActionListener {
hexButton.setToolTipText(translate("button.viewhex"));
hexButton.setMargin(new Insets(3, 3, 3, 3));
// todo: find icon, and set visible
hexOnlyButton = new JToggleButton(View.getIcon("hex16"));
hexOnlyButton.setActionCommand("HEXONLY");
hexOnlyButton.addActionListener(this);
hexOnlyButton.setToolTipText(translate("button.viewhex"));
hexOnlyButton.setMargin(new Insets(3, 3, 3, 3));
hexOnlyButton.setVisible(false);
buttonsPanel.add(graphButton);
buttonsPanel.add(hexButton);
buttonsPanel.add(hexOnlyButton);
buttonsPanel.add(new JPanel());
// buttonsPanel.add(saveButton);
// buttonsPan.add(execButton);
@@ -125,17 +136,24 @@ public class MethodCodePanel extends JPanel implements ActionListener {
sourceTextArea.graph();
}
if (e.getActionCommand().equals("HEX")) {
sourceTextArea.setHex(hexButton.isSelected());
if (e.getActionCommand().equals("HEX") || e.getActionCommand().equals("HEXONLY")) {
sourceTextArea.setHex(getExportMode(), false);
}
}
private ExportMode getExportMode() {
ExportMode exportMode = hexOnlyButton.isSelected() ? ExportMode.HEX :
(hexButton.isSelected() ? ExportMode.PCODEWITHHEX : ExportMode.PCODE);
return exportMode;
}
public void setEditMode(boolean val) {
if (val) {
sourceTextArea.setHex(false);
sourceTextArea.setHex(ExportMode.PCODE, false);
} else {
if (hexButton.isSelected()) {
sourceTextArea.setHex(true);
ExportMode exportMode = getExportMode();
if (exportMode != ExportMode.PCODE) {
sourceTextArea.setHex(exportMode, false);
}
}
@@ -102,9 +102,9 @@ public class SlotConstTraitDetailPanel extends JPanel implements TraitDetail {
writer.appendNoHilight(" value ");
writer.hilightSpecial((new ValueKind(trait.value_index, trait.value_kind).toASMString(abc.constants)), "traitvalue");
String s = writer.toString();
specialHilights = Highlighting.getSpecialHighlights(s);
specialHilights = writer.specialHilights;
showWarning = trait.isConst() || isStatic;
slotConstEditor.setText(Highlighting.stripHilights(s));
slotConstEditor.setText(s);
}
@Override
@@ -68,12 +68,14 @@ public class TraitsListItem {
public String toString() {
String s = "";
if ((type != Type.INITIALIZER) && isStatic) {
abc.class_info[classIndex].static_traits.traits[index].convertHeader(null, "", abcTags, abc, true, ExportMode.SOURCE, scriptIndex, classIndex, new ArrayList<String>(), false);
HilightedTextWriter writer = new HilightedTextWriter(false);
abc.class_info[classIndex].static_traits.traits[index].convertHeader(null, "", abcTags, abc, true, ExportMode.SOURCE, scriptIndex, classIndex, writer, new ArrayList<String>(), false);
abc.class_info[classIndex].static_traits.traits[index].toStringHeader(null, "", abcTags, abc, true, ExportMode.SOURCE, scriptIndex, classIndex, writer, new ArrayList<String>(), false);
s = writer.toString();
} else if ((type != Type.INITIALIZER) && (!isStatic)) {
abc.instance_info[classIndex].instance_traits.traits[index].convertHeader(null, "", abcTags, abc, false, ExportMode.SOURCE, scriptIndex, classIndex, new ArrayList<String>(), false);
HilightedTextWriter writer = new HilightedTextWriter(false);
abc.instance_info[classIndex].instance_traits.traits[index].convertHeader(null, "", abcTags, abc, false, ExportMode.SOURCE, scriptIndex, classIndex, writer, new ArrayList<String>(), false);
abc.instance_info[classIndex].instance_traits.traits[index].toStringHeader(null, "", abcTags, abc, false, ExportMode.SOURCE, scriptIndex, classIndex, writer, new ArrayList<String>(), false);
s = writer.toString();
} else if (!isStatic) {
s = STR_INSTANCE_INITIALIZER;
@@ -34,11 +34,13 @@ import com.jpexs.decompiler.flash.gui.Main;
import com.jpexs.decompiler.flash.gui.TagTreeModel;
import com.jpexs.decompiler.flash.gui.View;
import com.jpexs.decompiler.flash.gui.abc.LineMarkedEditorPane;
import com.jpexs.decompiler.flash.helpers.HilightedText;
import com.jpexs.decompiler.flash.helpers.HilightedTextWriter;
import com.jpexs.decompiler.flash.helpers.hilight.Highlighting;
import com.jpexs.decompiler.flash.tags.Tag;
import com.jpexs.decompiler.flash.tags.base.ASMSource;
import com.jpexs.decompiler.graph.ExportMode;
import static com.jpexs.decompiler.graph.ExportMode.HEX;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.helpers.Cache;
import com.jpexs.helpers.Helper;
@@ -96,16 +98,15 @@ public class ActionPanel extends JPanel implements ActionListener {
public JLabel decLabel = new HeaderLabel(translate("panel.decompiled"));
public List<Highlighting> decompiledHilights = new ArrayList<>();
public List<Highlighting> disassembledHilights = new ArrayList<>();
public String lastDisasm = "";
private boolean ignoreCarret = false;
private boolean editMode = false;
private boolean editDecompiledMode = false;
private List<Action> lastCode;
private ASMSource src;
public JPanel topButtonsPan;
private String srcWithHex;
private String srcNoHex;
private String srcHexOnly;
private HilightedText srcWithHex;
private HilightedText srcNoHex;
private HilightedText srcHexOnly;
private String lastDecompiled = "";
private ASMSource lastASM;
public JPanel searchPanel;
@@ -174,9 +175,9 @@ public class ActionPanel extends JPanel implements ActionListener {
if (actions == null) {
actions = src.getActions(SWF.DEFAULT_VERSION);
}
String s = Action.actionsToSource(actions, SWF.DEFAULT_VERSION, src.toString()/*FIXME?*/, true, 0);
List<Highlighting> hilights = Highlighting.getInstrHighlights(s);
String srcNoHex = Highlighting.stripHilights(s);
HilightedText text = Action.actionsToSource(actions, SWF.DEFAULT_VERSION, src.toString()/*FIXME?*/, true, 0);
List<Highlighting> hilights = text.instructionHilights;
String srcNoHex = text.text;
cache.put(src, new CachedScript(srcNoHex, hilights));
}
}
@@ -247,9 +248,9 @@ public class ActionPanel extends JPanel implements ActionListener {
return false;
}
public void setText(String text) {
public void setText(HilightedText text) {
int pos = editor.getCaretPosition();
Highlighting lastH = new Highlighting(0, 0, 0);
Highlighting lastH = null;
for (Highlighting h : disassembledHilights) {
if (pos < h.startPos) {
break;
@@ -258,8 +259,8 @@ public class ActionPanel extends JPanel implements ActionListener {
}
String offset = lastH == null ? "0" : lastH.getPropertyString("offset");
editor.setText("; " + translate("work.gettinghilights") + "...");
disassembledHilights = Highlighting.getInstrHighlights(text);
String stripped = Highlighting.stripHilights(text);
disassembledHilights = text.instructionHilights;
String stripped = text.text;
/*if(stripped.length()>30000){
editor.setContentType("text/plain");
}else{
@@ -275,10 +276,58 @@ public class ActionPanel extends JPanel implements ActionListener {
}
public void setHex(boolean hex, boolean hexOnly) {
setText(hex ? srcWithHex : (hexOnly ? srcHexOnly : srcNoHex));
private HilightedText getHilightedText(ExportMode exportMode) {
ASMSource asm = (ASMSource) src;
DisassemblyListener listener = getDisassemblyListener();
asm.addDisassemblyListener(listener);
HilightedTextWriter writer = new HilightedTextWriter(true);
asm.getASMSource(SWF.DEFAULT_VERSION, exportMode, writer, lastCode);
asm.removeDisassemblyListener(listener);
return new HilightedText(writer);
}
public void setHex(ExportMode exportMode) {
if (exportMode != ExportMode.HEX) {
if (exportMode == exportMode.PCODE) {
if (srcNoHex == null) {
srcNoHex = getHilightedText(exportMode);
}
setText(srcNoHex);
} else {
if (srcWithHex == null) {
srcWithHex = getHilightedText(exportMode);
}
setText(srcWithHex);
}
} else {
if (srcHexOnly == null) {
srcHexOnly = new HilightedText(Helper.byteArrToString(src.getActionBytes()));
}
setText(srcHexOnly);
}
}
private DisassemblyListener getDisassemblyListener() {
DisassemblyListener listener = new DisassemblyListener() {
int percent = 0;
String phase = "";
@Override
public void progress(String phase, long pos, long total) {
if (total < 1) {
return;
}
int newpercent = (int) (pos * 100 / total);
if (((newpercent > percent) || (!this.phase.equals(phase))) && newpercent <= 100) {
percent = newpercent;
this.phase = phase;
editor.setText("; " + translate("work.disassembling") + " - " + phase + " " + percent + "%...");
}
}
};
return listener;
}
public void setSource(final ASMSource src, final boolean useCache) {
this.src = src;
Main.startWork(translate("work.decompiling") + "...");
@@ -290,34 +339,15 @@ public class ActionPanel extends JPanel implements ActionListener {
if (Configuration.getConfig("decompile", true)) {
decompiledEditor.setText("//" + translate("work.waitingfordissasembly") + "...");
}
DisassemblyListener listener = new DisassemblyListener() {
int percent = 0;
String phase = "";
@Override
public void progress(String phase, long pos, long total) {
if (total < 1) {
return;
}
int newpercent = (int) (pos * 100 / total);
if (((newpercent > percent) || (!this.phase.equals(phase))) && newpercent <= 100) {
percent = newpercent;
this.phase = phase;
editor.setText("; " + translate("work.disassembling") + " - " + phase + " " + percent + "%...");
}
}
};
DisassemblyListener listener = getDisassemblyListener();
asm.addDisassemblyListener(listener);
List<Action> actions = asm.getActions(SWF.DEFAULT_VERSION);
lastCode = actions;
HilightedTextWriter writer = new HilightedTextWriter(true);
asm.getASMSource(SWF.DEFAULT_VERSION, ExportMode.PCODEWITHHEX, writer, actions);
lastDisasm = writer.toString();
asm.removeDisassemblyListener(listener);
srcWithHex = Helper.hexToComments(lastDisasm);
srcNoHex = Helper.stripComments(lastDisasm);
srcHexOnly = Helper.byteArrToString(src.getActionBytes());
setHex(hexButton.isSelected(), hexOnlyButton.isSelected());
srcWithHex = null;
srcNoHex = null;
srcHexOnly = null;
setHex(getExportMode());
if (Configuration.getConfig("decompile", true)) {
decompiledEditor.setText("//" + translate("work.decompiling") + "...");
String stripped = "";
@@ -530,6 +560,7 @@ 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"));
@@ -655,10 +686,10 @@ public class ActionPanel extends JPanel implements ActionListener {
} else if (e.getActionCommand().equals("EDITACTION")) {
setEditMode(true);
} else if (e.getActionCommand().equals("HEX")) {
setHex(hexButton.isSelected(), hexOnlyButton.isSelected());
setHex(getExportMode());
} else if (e.getActionCommand().equals("CANCELACTION")) {
setEditMode(false);
setHex(hexButton.isSelected(), hexOnlyButton.isSelected());
setHex(getExportMode());
} else if (e.getActionCommand().equals("SAVEACTION")) {
try {
String text = editor.getText();
@@ -698,6 +729,12 @@ public class ActionPanel extends JPanel implements ActionListener {
}
}
private ExportMode getExportMode() {
ExportMode exportMode = hexOnlyButton.isSelected() ? ExportMode.HEX :
(hexButton.isSelected() ? ExportMode.PCODEWITHHEX : ExportMode.PCODE);
return exportMode;
}
public byte[] getBytesFromHexaText(String text) {
Scanner scanner = new Scanner(text);
scanner.nextLine(); // ignore first line