diff options
Diffstat (limited to 'src/cuchaz/enigma/gui/GuiController.java')
| -rw-r--r-- | src/cuchaz/enigma/gui/GuiController.java | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/gui/GuiController.java b/src/cuchaz/enigma/gui/GuiController.java new file mode 100644 index 0000000..5df2d43 --- /dev/null +++ b/src/cuchaz/enigma/gui/GuiController.java | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2014 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Public License v3.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/gpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.gui; | ||
| 12 | |||
| 13 | import java.io.File; | ||
| 14 | import java.io.FileInputStream; | ||
| 15 | import java.io.FileOutputStream; | ||
| 16 | import java.io.IOException; | ||
| 17 | |||
| 18 | import cuchaz.enigma.ClassFile; | ||
| 19 | import cuchaz.enigma.Deobfuscator; | ||
| 20 | import cuchaz.enigma.analysis.Analyzer; | ||
| 21 | import cuchaz.enigma.analysis.SourceIndex; | ||
| 22 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 23 | import cuchaz.enigma.mapping.Entry; | ||
| 24 | import cuchaz.enigma.mapping.EntryPair; | ||
| 25 | import cuchaz.enigma.mapping.TranslationMappings; | ||
| 26 | |||
| 27 | public class GuiController | ||
| 28 | { | ||
| 29 | private Deobfuscator m_deobfuscator; | ||
| 30 | private Gui m_gui; | ||
| 31 | private SourceIndex m_index; | ||
| 32 | private ClassFile m_currentFile; | ||
| 33 | |||
| 34 | public GuiController( Gui gui ) | ||
| 35 | { | ||
| 36 | m_gui = gui; | ||
| 37 | m_deobfuscator = null; | ||
| 38 | m_index = null; | ||
| 39 | m_currentFile = null; | ||
| 40 | } | ||
| 41 | |||
| 42 | public void openJar( File file ) | ||
| 43 | throws IOException | ||
| 44 | { | ||
| 45 | m_deobfuscator = new Deobfuscator( file ); | ||
| 46 | m_gui.onOpenJar( m_deobfuscator.getJarName() ); | ||
| 47 | m_gui.setObfClasses( m_deobfuscator.getObfuscatedClasses() ); | ||
| 48 | } | ||
| 49 | |||
| 50 | public void closeJar( ) | ||
| 51 | { | ||
| 52 | m_deobfuscator = null; | ||
| 53 | m_gui.onCloseJar(); | ||
| 54 | } | ||
| 55 | |||
| 56 | public void openMappings( File file ) | ||
| 57 | throws IOException | ||
| 58 | { | ||
| 59 | FileInputStream in = new FileInputStream( file ); | ||
| 60 | m_deobfuscator.setMappings( TranslationMappings.newFromStream( in ) ); | ||
| 61 | in.close(); | ||
| 62 | refreshOpenFiles(); | ||
| 63 | } | ||
| 64 | |||
| 65 | public void saveMappings( File file ) | ||
| 66 | throws IOException | ||
| 67 | { | ||
| 68 | FileOutputStream out = new FileOutputStream( file ); | ||
| 69 | m_deobfuscator.getMappings().write( out ); | ||
| 70 | out.close(); | ||
| 71 | } | ||
| 72 | |||
| 73 | public void closeMappings( ) | ||
| 74 | { | ||
| 75 | m_deobfuscator.setMappings( null ); | ||
| 76 | refreshOpenFiles(); | ||
| 77 | } | ||
| 78 | |||
| 79 | public void deobfuscateClass( ClassFile classFile ) | ||
| 80 | { | ||
| 81 | m_currentFile = classFile; | ||
| 82 | deobfuscate( m_currentFile ); | ||
| 83 | } | ||
| 84 | |||
| 85 | public EntryPair getEntryPair( int pos ) | ||
| 86 | { | ||
| 87 | if( m_index == null ) | ||
| 88 | { | ||
| 89 | return null; | ||
| 90 | } | ||
| 91 | |||
| 92 | Entry deobfEntry = m_index.getEntry( pos ); | ||
| 93 | if( deobfEntry == null ) | ||
| 94 | { | ||
| 95 | return null; | ||
| 96 | } | ||
| 97 | return new EntryPair( m_deobfuscator.obfuscate( deobfEntry ), deobfEntry ); | ||
| 98 | } | ||
| 99 | |||
| 100 | public void rename( Entry obfsEntry, String newName ) | ||
| 101 | { | ||
| 102 | m_deobfuscator.rename( obfsEntry, newName ); | ||
| 103 | |||
| 104 | // did we rename the current file? | ||
| 105 | if( obfsEntry instanceof ClassEntry ) | ||
| 106 | { | ||
| 107 | ClassEntry classEntry = (ClassEntry)obfsEntry; | ||
| 108 | |||
| 109 | // update the current file | ||
| 110 | if( classEntry.getName().equals( m_currentFile.getName() ) ) | ||
| 111 | { | ||
| 112 | m_currentFile = new ClassFile( newName ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | refreshOpenFiles(); | ||
| 117 | } | ||
| 118 | |||
| 119 | private void refreshOpenFiles( ) | ||
| 120 | { | ||
| 121 | if( m_currentFile != null ) | ||
| 122 | { | ||
| 123 | deobfuscate( m_currentFile ); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | private void deobfuscate( final ClassFile classFile ) | ||
| 128 | { | ||
| 129 | m_gui.setSource( "(deobfuscating...)" ); | ||
| 130 | |||
| 131 | // run the deobfuscator in a separate thread so we don't block the GUI event queue | ||
| 132 | new Thread( ) | ||
| 133 | { | ||
| 134 | @Override | ||
| 135 | public void run( ) | ||
| 136 | { | ||
| 137 | // deobfuscate the bytecode | ||
| 138 | String source = m_deobfuscator.getSource( classFile ); | ||
| 139 | m_gui.setSource( source ); | ||
| 140 | |||
| 141 | // index the source file | ||
| 142 | m_index = Analyzer.analyze( classFile.getName(), source ); | ||
| 143 | m_gui.setHighlightedTokens( m_index.tokens() ); | ||
| 144 | } | ||
| 145 | }.start(); | ||
| 146 | } | ||
| 147 | } | ||