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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*******************************************************************************
* 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.Stack;
import com.google.common.collect.Lists;
import cuchaz.enigma.Deobfuscator;
import cuchaz.enigma.analysis.ClassInheritanceTreeNode;
import cuchaz.enigma.analysis.FieldCallsTreeNode;
import cuchaz.enigma.analysis.MethodCallsTreeNode;
import cuchaz.enigma.analysis.MethodInheritanceTreeNode;
import cuchaz.enigma.analysis.SourceIndex;
import cuchaz.enigma.analysis.Token;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.ConstructorEntry;
import cuchaz.enigma.mapping.Entry;
import cuchaz.enigma.mapping.EntryPair;
import cuchaz.enigma.mapping.FieldEntry;
import cuchaz.enigma.mapping.MappingParseException;
import cuchaz.enigma.mapping.MappingsReader;
import cuchaz.enigma.mapping.MappingsWriter;
import cuchaz.enigma.mapping.MethodEntry;
import cuchaz.enigma.mapping.TranslationDirection;
public class GuiController
{
private Deobfuscator m_deobfuscator;
private Gui m_gui;
private SourceIndex m_index;
private ClassEntry m_currentClass;
private boolean m_isDirty;
private Stack<Entry> m_entryStack;
public GuiController( Gui gui )
{
m_gui = gui;
m_deobfuscator = null;
m_index = null;
m_currentClass = null;
m_isDirty = false;
m_entryStack = new Stack<Entry>();
}
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, MappingParseException
{
FileReader in = new FileReader( file );
m_deobfuscator.setMappings( new MappingsReader().read( in ) );
in.close();
m_isDirty = false;
m_gui.setMappingsFile( file );
refreshClasses();
refreshCurrentClass();
}
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();
refreshCurrentClass();
}
public Token getToken( int pos )
{
if( m_index == null )
{
return null;
}
return m_index.getToken( pos );
}
public EntryPair<Entry> getEntryPair( Token token )
{
if( m_index == null )
{
return null;
}
Entry deobfEntry = m_index.getEntry( token );
if( deobfEntry == null )
{
return null;
}
return new EntryPair<Entry>( m_deobfuscator.obfuscateEntry( deobfEntry ), deobfEntry );
}
public boolean entryHasMapping( Entry deobfEntry )
{
return m_deobfuscator.hasMapping( m_deobfuscator.obfuscateEntry( deobfEntry ) );
}
public boolean entryIsObfuscatedIdenfitier( Entry deobfEntry )
{
return m_deobfuscator.isObfuscatedIdentifier( m_deobfuscator.obfuscateEntry( deobfEntry ) );
}
public ClassInheritanceTreeNode getClassInheritance( ClassEntry obfClassEntry )
{
ClassInheritanceTreeNode rootNode = m_deobfuscator.getJarIndex().getClassInheritance(
m_deobfuscator.getTranslator( TranslationDirection.Deobfuscating ),
obfClassEntry
);
return ClassInheritanceTreeNode.findNode( rootNode, obfClassEntry );
}
public MethodInheritanceTreeNode getMethodInheritance( MethodEntry obfMethodEntry )
{
MethodInheritanceTreeNode rootNode = m_deobfuscator.getJarIndex().getMethodInheritance(
m_deobfuscator.getTranslator( TranslationDirection.Deobfuscating ),
obfMethodEntry
);
return MethodInheritanceTreeNode.findNode( rootNode, obfMethodEntry );
}
public FieldCallsTreeNode getFieldCalls( FieldEntry obfFieldEntry )
{
FieldCallsTreeNode rootNode = new FieldCallsTreeNode(
m_deobfuscator.getTranslator( TranslationDirection.Deobfuscating ),
obfFieldEntry
);
rootNode.load( m_deobfuscator.getJarIndex(), true );
return rootNode;
}
public MethodCallsTreeNode getMethodCalls( Entry obfEntry )
{
MethodCallsTreeNode rootNode;
if( obfEntry instanceof MethodEntry )
{
rootNode = new MethodCallsTreeNode(
m_deobfuscator.getTranslator( TranslationDirection.Deobfuscating ),
(MethodEntry)obfEntry
);
}
else if( obfEntry instanceof ConstructorEntry )
{
rootNode = new MethodCallsTreeNode(
m_deobfuscator.getTranslator( TranslationDirection.Deobfuscating ),
(ConstructorEntry)obfEntry
);
}
else
{
throw new IllegalArgumentException( "entry must be a MethodEntry or a ConstructorEntry!" );
}
rootNode.load( m_deobfuscator.getJarIndex(), true );
return rootNode;
}
public void rename( Entry obfEntry, String newName )
{
m_deobfuscator.rename( obfEntry, newName );
m_isDirty = true;
refreshClasses();
refreshCurrentClass( obfEntry );
}
public void openEntry( Entry entry )
{
// go to the entry
Entry obfEntry = m_deobfuscator.obfuscateEntry( entry );
if( m_currentClass == null || !m_currentClass.equals( obfEntry.getClassEntry() ) )
{
m_currentClass = new ClassEntry( obfEntry.getClassEntry() );
deobfuscate( m_currentClass, obfEntry );
}
else
{
m_gui.showToken( m_index.getDeclarationToken( m_deobfuscator.deobfuscateEntry( obfEntry ) ) );
}
if( m_entryStack.isEmpty() || !m_entryStack.peek().equals( obfEntry ) )
{
// update the stack
m_entryStack.push( obfEntry );
}
}
public void openPreviousEntry( )
{
if( hasPreviousEntry() )
{
m_entryStack.pop();
openEntry( m_entryStack.peek() );
}
}
public boolean hasPreviousEntry( )
{
return m_entryStack.size() > 1;
}
private void refreshClasses( )
{
List<String> obfClasses = Lists.newArrayList();
List<String> deobfClasses = Lists.newArrayList();
m_deobfuscator.getSeparatedClasses( obfClasses, deobfClasses );
m_gui.setObfClasses( obfClasses );
m_gui.setDeobfClasses( deobfClasses );
}
private void refreshCurrentClass( )
{
refreshCurrentClass( null );
}
private void refreshCurrentClass( Entry obfEntryToShow )
{
if( m_currentClass != null )
{
deobfuscate( m_currentClass, obfEntryToShow );
}
}
private void deobfuscate( final ClassEntry classEntry, final Entry obfEntryToShow )
{
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( )
{
// decompile,deobfuscate the bytecode
m_index = m_deobfuscator.getSource( classEntry.getClassName() );
m_gui.setSource( m_index.getSource() );
if( obfEntryToShow != null )
{
Entry deobfEntryToShow = m_deobfuscator.deobfuscateEntry( obfEntryToShow );
Token token = m_index.getDeclarationToken( deobfEntryToShow );
if( token == null )
{
// TEMP
System.out.println( "WARNING: can't find token for " + obfEntryToShow + " -> " + deobfEntryToShow );
}
m_gui.showToken( token );
}
// set the highlighted tokens
List<Token> obfuscatedTokens = Lists.newArrayList();
List<Token> deobfuscatedTokens = Lists.newArrayList();
for( Token token : m_index.tokens() )
{
Entry entry = m_index.getEntry( token );
if( entryHasMapping( entry ) )
{
deobfuscatedTokens.add( token );
}
else if( entryIsObfuscatedIdenfitier( entry ) )
{
obfuscatedTokens.add( token );
}
}
m_gui.setHighlightedTokens( obfuscatedTokens, deobfuscatedTokens );
}
}.start();
}
}
|