From ba7a354efae7d49833c887cf147ac940c975a1fa Mon Sep 17 00:00:00 2001 From: Gegy Date: Wed, 30 Jan 2019 21:05:32 +0200 Subject: 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 --- src/main/java/cuchaz/enigma/gui/CodeReader.java | 94 ++----------------------- 1 file changed, 5 insertions(+), 89 deletions(-) (limited to 'src/main/java/cuchaz/enigma/gui/CodeReader.java') 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 @@ package cuchaz.enigma.gui; -import com.strobel.decompiler.languages.java.ast.CompilationUnit; -import cuchaz.enigma.Deobfuscator; -import cuchaz.enigma.analysis.EntryReference; -import cuchaz.enigma.analysis.SourceIndex; import cuchaz.enigma.analysis.Token; -import cuchaz.enigma.translation.representation.entry.ClassEntry; -import cuchaz.enigma.translation.representation.entry.Entry; -import de.sciss.syntaxpane.DefaultSyntaxKit; import javax.swing.*; import javax.swing.text.BadLocationException; +import javax.swing.text.Document; import javax.swing.text.Highlighter.HighlightPainter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CodeReader extends JEditorPane { - private static final long serialVersionUID = 3673180950485748810L; - private static final Object lock = new Object(); - private SourceIndex sourceIndex; - private SelectionListener selectionListener; - - public CodeReader() { - - setEditable(false); - setContentType("text/java"); - - // turn off token highlighting (it's wrong most of the time anyway...) - DefaultSyntaxKit kit = (DefaultSyntaxKit) getEditorKit(); - kit.toggleComponent(this, "de.sciss.syntaxpane.components.TokenMarker"); - - // hook events - addCaretListener(event -> - { - if (selectionListener != null && sourceIndex != null) { - Token token = sourceIndex.getReferenceToken(event.getDot()); - if (token != null) { - selectionListener.onSelect(sourceIndex.getDeobfReference(token)); - } else { - selectionListener.onSelect(null); - } - } - }); - } - // HACKHACK: someday we can update the main GUI to use this code reader public static void navigateToToken(final JEditorPane editor, final Token token, final HighlightPainter highlightPainter) { // set the caret position to the token - editor.setCaretPosition(token.start); + Document document = editor.getDocument(); + int clampedPosition = Math.min(Math.max(token.start, 0), document.getLength()); + + editor.setCaretPosition(clampedPosition); editor.grabFocus(); try { @@ -101,57 +70,4 @@ public class CodeReader extends JEditorPane { }); timer.start(); } - - public void setSelectionListener(SelectionListener val) { - selectionListener = val; - } - - public void setCode(String code) { - // sadly, the java lexer is not thread safe, so we have to serialize all these calls - synchronized (lock) { - setText(code); - } - } - - public SourceIndex getSourceIndex() { - return sourceIndex; - } - - public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator) { - decompileClass(classEntry, deobfuscator, null); - } - - public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator, Runnable callback) { - decompileClass(classEntry, deobfuscator, null, callback); - } - - public void decompileClass(final ClassEntry classEntry, final Deobfuscator deobfuscator, final Boolean ignoreBadTokens, final Runnable callback) { - - if (classEntry == null) { - setCode(null); - return; - } - - setCode("(decompiling...)"); - - // run decompilation in a separate thread to keep ui responsive - new Thread(() -> - { - - // decompile it - - CompilationUnit sourceTree = deobfuscator.getSourceTree(classEntry.getName()); - String source = deobfuscator.getSource(sourceTree); - setCode(source); - sourceIndex = deobfuscator.getSourceIndex(sourceTree, source, ignoreBadTokens); - - if (callback != null) { - callback.run(); - } - }).start(); - } - - public interface SelectionListener { - void onSelect(EntryReference, Entry> reference); - } } -- cgit v1.2.3