summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/gui/GuiController.java
blob: 7d37febf1f04243e3ec6f64141a5c5f96137454e (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * 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.gui;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import jsyntaxpane.Token;

import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;

import cuchaz.enigma.ClassFile;
import cuchaz.enigma.Deobfuscator;
import cuchaz.enigma.analysis.Analyzer;
import cuchaz.enigma.analysis.SourceIndex;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.Entry;
import cuchaz.enigma.mapping.EntryPair;
import cuchaz.enigma.mapping.MappingsReader;
import cuchaz.enigma.mapping.MappingsWriter;
import cuchaz.enigma.mapping.TranslationDirection;
import cuchaz.enigma.mapping.Translator;

public class GuiController
{
	private Deobfuscator m_deobfuscator;
	private Gui m_gui;
	private SourceIndex m_index;
	private ClassFile m_currentFile;
	private boolean m_isDirty;
	
	public GuiController( Gui gui )
	{
		m_gui = gui;
		m_deobfuscator = null;
		m_index = null;
		m_currentFile = null;
		m_isDirty = false;
	}
	
	public boolean isDirty( )
	{
		return m_isDirty;
	}
	
	public void openJar( File file )
	throws IOException
	{
		m_deobfuscator = new Deobfuscator( file );
		m_gui.onOpenJar( m_deobfuscator.getJarName() );
		refreshClasses();
	}
	
	public void closeJar( )
	{
		m_deobfuscator = null;
		m_gui.onCloseJar();
	}
	
	public void openMappings( File file )
	throws IOException
	{
		FileReader in = new FileReader( file );
		m_deobfuscator.setMappings( new MappingsReader().read( in ) );
		in.close();
		m_isDirty = false;
		m_gui.setMappingsFile( file );
		refreshClasses();
		refreshOpenFiles();
	}

	public void saveMappings( File file )
	throws IOException
	{
		FileWriter out = new FileWriter( file );
		new MappingsWriter().write( out, m_deobfuscator.getMappings() );
		out.close();
		m_isDirty = false;
	}

	public void closeMappings( )
	{
		m_deobfuscator.setMappings( null );
		m_gui.setMappingsFile( null );
		refreshClasses();
		refreshOpenFiles();
	}
	
	public void deobfuscateClass( ClassFile classFile )
	{
		m_currentFile = classFile;
		deobfuscate( m_currentFile );
	}
	
	public EntryPair<Entry> getEntryPair( int pos )
	{
		if( m_index == null )
		{
			return null;
		}
		
		Map.Entry<Entry,Token> deobfEntryAndToken = m_index.getEntry( pos );
		if( deobfEntryAndToken == null )
		{
			return null;
		}
		Entry deobfEntry = deobfEntryAndToken.getKey();
		Token token = deobfEntryAndToken.getValue();
		return new EntryPair<Entry>( m_deobfuscator.obfuscateEntry( deobfEntry ), deobfEntry, token );
	}
	
	public boolean entryHasMapping( int pos )
	{
		EntryPair<Entry> pair = getEntryPair( pos );
		if( pair == null || pair.obf == null )
		{
			return false;
		}
		return m_deobfuscator.hasMapping( pair.obf );
	}
	
	public ClassInheritanceTreeNode getClassInheritance( ClassEntry classEntry )
	{
		Translator deobfuscatingTranslator = m_deobfuscator.getTranslator( TranslationDirection.Deobfuscating );
		
		// create a node for this class
		ClassInheritanceTreeNode thisNode = new ClassInheritanceTreeNode( deobfuscatingTranslator, classEntry.getName() );
		
		// expand all children recursively
		thisNode.load( m_deobfuscator.getAncestries(), true );
		
		// get the ancestors too
		ClassInheritanceTreeNode node = thisNode;
		for( String superclassName : m_deobfuscator.getAncestries().getAncestry( classEntry.getName() ) )
		{
			// add the parent node
			ClassInheritanceTreeNode parentNode = new ClassInheritanceTreeNode( deobfuscatingTranslator, superclassName );
			parentNode.add( node );
			node = parentNode;
		}
		
		return thisNode;
	}
	
	public void rename( Entry obfsEntry, String newName, int lineNum )
	{
		m_deobfuscator.rename( obfsEntry, newName );
		m_isDirty = true;
		refreshClasses();
		refreshOpenFiles( lineNum );
	}
	
	private void refreshClasses( )
	{
		List<ClassFile> obfClasses = Lists.newArrayList();
		Map<ClassFile,String> deobfClasses = Maps.newHashMap();
		m_deobfuscator.getSeparatedClasses( obfClasses, deobfClasses );
		m_gui.setObfClasses( obfClasses );
		m_gui.setDeobfClasses( deobfClasses );
	}

	private void refreshOpenFiles( )
	{
		refreshOpenFiles( 0 );
	}
	
	private void refreshOpenFiles( int lineNum )
	{
		if( m_currentFile != null )
		{
			deobfuscate( m_currentFile, lineNum );
		}
	}

	private void deobfuscate( final ClassFile classFile )
	{
		deobfuscate( classFile, 0 );
	}
	
	private void deobfuscate( final ClassFile classFile, final int lineNum )
	{
		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,decompile the bytecode
				String source = m_deobfuscator.getSource( classFile );
				m_gui.setSource( source, lineNum );
				
				// index the source file
				m_index = Analyzer.analyze( classFile.getName(), source );
				
				// set the highlighted tokens
				List<Token> obfuscatedTokens = Lists.newArrayList();
				List<Token> deobfuscatedTokens = Lists.newArrayList();
				for( Token token : m_index.tokens() )
				{
					if( entryHasMapping( token.start ) )
					{
						deobfuscatedTokens.add( token );
					}
					else
					{
						obfuscatedTokens.add( token );
					}
				}
				m_gui.setHighlightedTokens( obfuscatedTokens, deobfuscatedTokens );
			}
		}.start();
	}
}