summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/Controller.java
blob: 3af139e808ee565ad01d7defb565ace9e9c4f826 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*******************************************************************************
 * Copyright (c) 2014 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma;

import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import cuchaz.enigma.analysis.Analyzer;
import cuchaz.enigma.analysis.SourceIndex;
import cuchaz.enigma.gui.ClassSelectionListener;
import cuchaz.enigma.gui.Gui;
import cuchaz.enigma.gui.RenameListener;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.Entry;
import cuchaz.enigma.mapping.EntryPair;

public class Controller implements ClassSelectionListener, CaretListener, RenameListener
{
	private Deobfuscator m_deobfuscator;
	private Gui m_gui;
	private SourceIndex m_index;
	private ClassFile m_currentFile;
	
	public Controller( Deobfuscator deobfuscator, Gui gui )
	{
		m_deobfuscator = deobfuscator;
		m_gui = gui;
		m_index = null;
		m_currentFile = null;
		
		// update GUI
		gui.setTitle( deobfuscator.getJarName() );
		gui.setObfClasses( deobfuscator.getObfuscatedClasses() );
		
		// handle events
		gui.setClassSelectionListener( this );
		gui.setCaretListener( this );
		gui.setRenameListener( this );
	}
	
	@Override
	public void classSelected( ClassFile classFile )
	{
		m_currentFile = classFile;
		deobfuscate( m_currentFile );
	}
	
	@Override
	public void caretUpdate( CaretEvent event )
	{
		if( m_index != null )
		{
			int pos = event.getDot();
			Entry deobfEntry = m_index.getEntry( pos );
			if( deobfEntry != null )
			{
				m_gui.showEntryPair( new EntryPair( m_deobfuscator.obfuscate( deobfEntry ), deobfEntry ) );
			}
			else
			{
				m_gui.clearEntryPair();
			}
		}
	}
	
	@Override
	public void rename( Entry obfsEntry, String newName )
	{
		m_deobfuscator.rename( obfsEntry, newName );
		
		// did we rename the current file?
		if( obfsEntry instanceof ClassEntry )
		{
			ClassEntry classEntry = (ClassEntry)obfsEntry;
			
			// update the current file
			if( classEntry.getName().equals( m_currentFile.getName() ) )
			{
				m_currentFile = new ClassFile( newName );
			}
		}
		
		deobfuscate( m_currentFile );
	}
	
	private void deobfuscate( final ClassFile classFile )
	{
		m_gui.setSource( "(deobfuscating...)" );
		
		// run the deobfuscator in a separate thread so we don't block the GUI event queue
		new Thread( )
		{
			@Override
			public void run( )
			{
				// deobfuscate the bytecode
				String source = m_deobfuscator.getSource( classFile );
				m_gui.setSource( source );
				
				// index the source file
				m_index = Analyzer.analyze( classFile.getName(), source );
				m_gui.highlightTokens( m_index.tokens() );
			}
		}.start();
	}
}