summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/gui/CodeReader.java
diff options
context:
space:
mode:
authorGravatar Gegy2019-01-30 21:05:32 +0200
committerGravatar GitHub2019-01-30 21:05:32 +0200
commitba7a354efae7d49833c887cf147ac940c975a1fa (patch)
tree02e14fda81dd5984e24f2df392c57c6e829fc875 /src/main/java/cuchaz/enigma/gui/CodeReader.java
parentRewrite the Jenkinsfile to use the new declarative pipeline syntax, lets hope... (diff)
downloadenigma-fork-ba7a354efae7d49833c887cf147ac940c975a1fa.tar.gz
enigma-fork-ba7a354efae7d49833c887cf147ac940c975a1fa.tar.xz
enigma-fork-ba7a354efae7d49833c887cf147ac940c975a1fa.zip
Remap sources (#106)
* Source remapping beginnings * Fix navigation to remapped classes * Translate identifier info reference * Remap local variables with default names in source * Caching translator * Fix lack of highlighting for first opened class * Fix unicode variable names * Unicode checker shouldn't be checking just alphanumeric * Fix package tree being built from obf names * Don't index `this` as method call for method::reference * Apply proposed names * Fix source export issues * Replace unicode var names at bytecode level uniquely * Drop imports from editor source * Class selector fixes * Delta keep track of base mappings to enable lookup of old names * Optimize source remapping by remapping source with a StringBuffer instead of copying * Bump version
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/CodeReader.java')
-rw-r--r--src/main/java/cuchaz/enigma/gui/CodeReader.java94
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
12package cuchaz.enigma.gui; 12package cuchaz.enigma.gui;
13 13
14import com.strobel.decompiler.languages.java.ast.CompilationUnit;
15import cuchaz.enigma.Deobfuscator;
16import cuchaz.enigma.analysis.EntryReference;
17import cuchaz.enigma.analysis.SourceIndex;
18import cuchaz.enigma.analysis.Token; 14import cuchaz.enigma.analysis.Token;
19import cuchaz.enigma.translation.representation.entry.ClassEntry;
20import cuchaz.enigma.translation.representation.entry.Entry;
21import de.sciss.syntaxpane.DefaultSyntaxKit;
22 15
23import javax.swing.*; 16import javax.swing.*;
24import javax.swing.text.BadLocationException; 17import javax.swing.text.BadLocationException;
18import javax.swing.text.Document;
25import javax.swing.text.Highlighter.HighlightPainter; 19import javax.swing.text.Highlighter.HighlightPainter;
26import java.awt.*; 20import java.awt.*;
27import java.awt.event.ActionEvent; 21import java.awt.event.ActionEvent;
28import java.awt.event.ActionListener; 22import java.awt.event.ActionListener;
29 23
30public class CodeReader extends JEditorPane { 24public 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}