summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/Deobfuscator.java
diff options
context:
space:
mode:
authorGravatar jeff2014-08-23 16:20:15 -0400
committerGravatar jeff2014-08-23 16:20:15 -0400
commita700b403d790c23989da524c934f0185b87c7b32 (patch)
tree10a6b279927efea8cbfd12288edd6ad06f046eb2 /src/cuchaz/enigma/Deobfuscator.java
parentAdded tag v0.3 beta for changeset d90963ed6887 (diff)
downloadenigma-fork-a700b403d790c23989da524c934f0185b87c7b32.tar.gz
enigma-fork-a700b403d790c23989da524c934f0185b87c7b32.tar.xz
enigma-fork-a700b403d790c23989da524c934f0185b87c7b32.zip
added export command with progress bar
Diffstat (limited to 'src/cuchaz/enigma/Deobfuscator.java')
-rw-r--r--src/cuchaz/enigma/Deobfuscator.java78
1 files changed, 67 insertions, 11 deletions
diff --git a/src/cuchaz/enigma/Deobfuscator.java b/src/cuchaz/enigma/Deobfuscator.java
index 38f7af5..5d87ad0 100644
--- a/src/cuchaz/enigma/Deobfuscator.java
+++ b/src/cuchaz/enigma/Deobfuscator.java
@@ -11,6 +11,7 @@
11package cuchaz.enigma; 11package cuchaz.enigma;
12 12
13import java.io.File; 13import java.io.File;
14import java.io.FileWriter;
14import java.io.IOException; 15import java.io.IOException;
15import java.io.StringWriter; 16import java.io.StringWriter;
16import java.util.List; 17import java.util.List;
@@ -44,6 +45,12 @@ import cuchaz.enigma.mapping.Translator;
44 45
45public class Deobfuscator 46public class Deobfuscator
46{ 47{
48 public interface ProgressListener
49 {
50 void init( int totalWork );
51 void onProgress( int numDone, String message );
52 }
53
47 private File m_file; 54 private File m_file;
48 private JarFile m_jar; 55 private JarFile m_jar;
49 private DecompilerSettings m_settings; 56 private DecompilerSettings m_settings;
@@ -137,7 +144,7 @@ public class Deobfuscator
137 } 144 }
138 } 145 }
139 146
140 public SourceIndex getSource( String className ) 147 public CompilationUnit getSourceTree( String className )
141 { 148 {
142 // is this class deobfuscated? 149 // is this class deobfuscated?
143 // we need to tell the decompiler the deobfuscated name so it doesn't get freaked out 150 // we need to tell the decompiler the deobfuscated name so it doesn't get freaked out
@@ -156,19 +163,18 @@ public class Deobfuscator
156 AstBuilder builder = new AstBuilder( context ); 163 AstBuilder builder = new AstBuilder( context );
157 builder.addType( resolvedType ); 164 builder.addType( resolvedType );
158 builder.runTransformations( null ); 165 builder.runTransformations( null );
159 CompilationUnit root = builder.getCompilationUnit(); 166 return builder.getCompilationUnit();
167 }
168
169 public SourceIndex getSourceIndex( CompilationUnit sourceTree, String source )
170 {
171 // build the source index
172 SourceIndex index = new SourceIndex( source );
173 sourceTree.acceptVisitor( new SourceIndexVisitor(), index );
160 174
161 // render the AST into source
162 StringWriter buf = new StringWriter();
163 root.acceptVisitor( new InsertParenthesesVisitor(), null );
164 // DEBUG 175 // DEBUG
165 //root.acceptVisitor( new TreeDumpVisitor( new File( "tree.txt" ) ), null ); 176 //root.acceptVisitor( new TreeDumpVisitor( new File( "tree.txt" ) ), null );
166 root.acceptVisitor( new JavaOutputVisitor( new PlainTextOutput( buf ), m_settings ), null ); 177
167
168 // build the source index
169 SourceIndex index = new SourceIndex( buf.toString() );
170 root.acceptVisitor( new SourceIndexVisitor(), index );
171
172 /* DEBUG 178 /* DEBUG
173 for( Token token : index.referenceTokens() ) 179 for( Token token : index.referenceTokens() )
174 { 180 {
@@ -180,6 +186,56 @@ public class Deobfuscator
180 return index; 186 return index;
181 } 187 }
182 188
189 public String getSource( CompilationUnit sourceTree )
190 {
191 // render the AST into source
192 StringWriter buf = new StringWriter();
193 sourceTree.acceptVisitor( new InsertParenthesesVisitor(), null );
194 sourceTree.acceptVisitor( new JavaOutputVisitor( new PlainTextOutput( buf ), m_settings ), null );
195 return buf.toString();
196 }
197
198 public void writeSources( File dirOut, ProgressListener progress )
199 throws IOException
200 {
201 int numClasses = m_jarIndex.getObfClassNames().size();
202 if( progress != null )
203 {
204 progress.init( numClasses );
205 }
206 int i = 0;
207
208 // DEOBFUSCATE ALL THE THINGS!! @_@
209 for( String obfClassName : m_jarIndex.getObfClassNames() )
210 {
211 // skip inner classes
212 if( m_jarIndex.getOuterClass( obfClassName ) != null )
213 {
214 continue;
215 }
216
217 ClassEntry deobfClassEntry = deobfuscateEntry( new ClassEntry( obfClassName ) );
218 if( progress != null )
219 {
220 progress.onProgress( i++, deobfClassEntry.toString() );
221 }
222
223 // get the source
224 String source = getSource( getSourceTree( obfClassName ) );
225
226 // write the file
227 File file = new File( dirOut, deobfClassEntry.getName().replace( '.', '/' ) + ".java" );
228 file.getParentFile().mkdirs();
229 try( FileWriter out = new FileWriter( file ) )
230 {
231 out.write( source );
232 }
233 }
234
235 // done!
236 progress.onProgress( numClasses, "Done!" );
237 }
238
183 public <T extends Entry> T obfuscateEntry( T deobfEntry ) 239 public <T extends Entry> T obfuscateEntry( T deobfEntry )
184 { 240 {
185 if( deobfEntry == null ) 241 if( deobfEntry == null )