diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/CodeReader.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/gui/CodeReader.java | 94 |
1 files changed, 5 insertions, 89 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/CodeReader.java b/src/main/java/cuchaz/enigma/gui/CodeReader.java index 0810043..e119640 100644 --- a/src/main/java/cuchaz/enigma/gui/CodeReader.java +++ b/src/main/java/cuchaz/enigma/gui/CodeReader.java | |||
| @@ -11,58 +11,27 @@ | |||
| 11 | 11 | ||
| 12 | package cuchaz.enigma.gui; | 12 | package cuchaz.enigma.gui; |
| 13 | 13 | ||
| 14 | import com.strobel.decompiler.languages.java.ast.CompilationUnit; | ||
| 15 | import cuchaz.enigma.Deobfuscator; | ||
| 16 | import cuchaz.enigma.analysis.EntryReference; | ||
| 17 | import cuchaz.enigma.analysis.SourceIndex; | ||
| 18 | import cuchaz.enigma.analysis.Token; | 14 | import cuchaz.enigma.analysis.Token; |
| 19 | import cuchaz.enigma.translation.representation.entry.ClassEntry; | ||
| 20 | import cuchaz.enigma.translation.representation.entry.Entry; | ||
| 21 | import de.sciss.syntaxpane.DefaultSyntaxKit; | ||
| 22 | 15 | ||
| 23 | import javax.swing.*; | 16 | import javax.swing.*; |
| 24 | import javax.swing.text.BadLocationException; | 17 | import javax.swing.text.BadLocationException; |
| 18 | import javax.swing.text.Document; | ||
| 25 | import javax.swing.text.Highlighter.HighlightPainter; | 19 | import javax.swing.text.Highlighter.HighlightPainter; |
| 26 | import java.awt.*; | 20 | import java.awt.*; |
| 27 | import java.awt.event.ActionEvent; | 21 | import java.awt.event.ActionEvent; |
| 28 | import java.awt.event.ActionListener; | 22 | import java.awt.event.ActionListener; |
| 29 | 23 | ||
| 30 | public class CodeReader extends JEditorPane { | 24 | public class CodeReader extends JEditorPane { |
| 31 | |||
| 32 | private static final long serialVersionUID = 3673180950485748810L; | 25 | private static final long serialVersionUID = 3673180950485748810L; |
| 33 | 26 | ||
| 34 | private static final Object lock = new Object(); | ||
| 35 | private SourceIndex sourceIndex; | ||
| 36 | private SelectionListener selectionListener; | ||
| 37 | |||
| 38 | public CodeReader() { | ||
| 39 | |||
| 40 | setEditable(false); | ||
| 41 | setContentType("text/java"); | ||
| 42 | |||
| 43 | // turn off token highlighting (it's wrong most of the time anyway...) | ||
| 44 | DefaultSyntaxKit kit = (DefaultSyntaxKit) getEditorKit(); | ||
| 45 | kit.toggleComponent(this, "de.sciss.syntaxpane.components.TokenMarker"); | ||
| 46 | |||
| 47 | // hook events | ||
| 48 | addCaretListener(event -> | ||
| 49 | { | ||
| 50 | if (selectionListener != null && sourceIndex != null) { | ||
| 51 | Token token = sourceIndex.getReferenceToken(event.getDot()); | ||
| 52 | if (token != null) { | ||
| 53 | selectionListener.onSelect(sourceIndex.getDeobfReference(token)); | ||
| 54 | } else { | ||
| 55 | selectionListener.onSelect(null); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | }); | ||
| 59 | } | ||
| 60 | |||
| 61 | // HACKHACK: someday we can update the main GUI to use this code reader | 27 | // HACKHACK: someday we can update the main GUI to use this code reader |
| 62 | public static void navigateToToken(final JEditorPane editor, final Token token, final HighlightPainter highlightPainter) { | 28 | public static void navigateToToken(final JEditorPane editor, final Token token, final HighlightPainter highlightPainter) { |
| 63 | 29 | ||
| 64 | // set the caret position to the token | 30 | // set the caret position to the token |
| 65 | editor.setCaretPosition(token.start); | 31 | Document document = editor.getDocument(); |
| 32 | int clampedPosition = Math.min(Math.max(token.start, 0), document.getLength()); | ||
| 33 | |||
| 34 | editor.setCaretPosition(clampedPosition); | ||
| 66 | editor.grabFocus(); | 35 | editor.grabFocus(); |
| 67 | 36 | ||
| 68 | try { | 37 | try { |
| @@ -101,57 +70,4 @@ public class CodeReader extends JEditorPane { | |||
| 101 | }); | 70 | }); |
| 102 | timer.start(); | 71 | timer.start(); |
| 103 | } | 72 | } |
| 104 | |||
| 105 | public void setSelectionListener(SelectionListener val) { | ||
| 106 | selectionListener = val; | ||
| 107 | } | ||
| 108 | |||
| 109 | public void setCode(String code) { | ||
| 110 | // sadly, the java lexer is not thread safe, so we have to serialize all these calls | ||
| 111 | synchronized (lock) { | ||
| 112 | setText(code); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | public SourceIndex getSourceIndex() { | ||
| 117 | return sourceIndex; | ||
| 118 | } | ||
| 119 | |||
| 120 | public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator) { | ||
| 121 | decompileClass(classEntry, deobfuscator, null); | ||
| 122 | } | ||
| 123 | |||
| 124 | public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator, Runnable callback) { | ||
| 125 | decompileClass(classEntry, deobfuscator, null, callback); | ||
| 126 | } | ||
| 127 | |||
| 128 | public void decompileClass(final ClassEntry classEntry, final Deobfuscator deobfuscator, final Boolean ignoreBadTokens, final Runnable callback) { | ||
| 129 | |||
| 130 | if (classEntry == null) { | ||
| 131 | setCode(null); | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | setCode("(decompiling...)"); | ||
| 136 | |||
| 137 | // run decompilation in a separate thread to keep ui responsive | ||
| 138 | new Thread(() -> | ||
| 139 | { | ||
| 140 | |||
| 141 | // decompile it | ||
| 142 | |||
| 143 | CompilationUnit sourceTree = deobfuscator.getSourceTree(classEntry.getName()); | ||
| 144 | String source = deobfuscator.getSource(sourceTree); | ||
| 145 | setCode(source); | ||
| 146 | sourceIndex = deobfuscator.getSourceIndex(sourceTree, source, ignoreBadTokens); | ||
| 147 | |||
| 148 | if (callback != null) { | ||
| 149 | callback.run(); | ||
| 150 | } | ||
| 151 | }).start(); | ||
| 152 | } | ||
| 153 | |||
| 154 | public interface SelectionListener { | ||
| 155 | void onSelect(EntryReference<Entry<?>, Entry<?>> reference); | ||
| 156 | } | ||
| 157 | } | 73 | } |