diff options
| author | 2014-07-27 22:33:21 -0400 | |
|---|---|---|
| committer | 2014-07-27 22:33:21 -0400 | |
| commit | d7321b5b0d38c575e54c770f7aa18dacbacab3c8 (patch) | |
| tree | ef4b3e0f83b1fe89125c2674fec023871e70c0d8 /src/cuchaz/enigma/mapping/TranslationMappings.java | |
| parent | made gui responsive to caret position and show identifier info (diff) | |
| download | enigma-fork-d7321b5b0d38c575e54c770f7aa18dacbacab3c8.tar.gz enigma-fork-d7321b5b0d38c575e54c770f7aa18dacbacab3c8.tar.xz enigma-fork-d7321b5b0d38c575e54c770f7aa18dacbacab3c8.zip | |
added identifier renaming capability
copied some code over from M3L to handle the heavy bytecode magic.
It's ok... M3L will eventually depend on Enigma.
Completely restructured the mappings though. This way is better. =)
Diffstat (limited to 'src/cuchaz/enigma/mapping/TranslationMappings.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/TranslationMappings.java | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/TranslationMappings.java b/src/cuchaz/enigma/mapping/TranslationMappings.java new file mode 100644 index 0000000..d6cd449 --- /dev/null +++ b/src/cuchaz/enigma/mapping/TranslationMappings.java | |||
| @@ -0,0 +1,187 @@ | |||
| 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.mapping; | ||
| 12 | |||
| 13 | import java.io.IOException; | ||
| 14 | import java.io.InputStream; | ||
| 15 | import java.io.ObjectInputStream; | ||
| 16 | import java.io.ObjectOutputStream; | ||
| 17 | import java.io.OutputStream; | ||
| 18 | import java.io.Serializable; | ||
| 19 | import java.util.Map; | ||
| 20 | import java.util.zip.GZIPInputStream; | ||
| 21 | import java.util.zip.GZIPOutputStream; | ||
| 22 | |||
| 23 | import com.beust.jcommander.internal.Maps; | ||
| 24 | |||
| 25 | import cuchaz.enigma.Util; | ||
| 26 | |||
| 27 | public class TranslationMappings implements Serializable | ||
| 28 | { | ||
| 29 | private static final long serialVersionUID = 4649790259460259026L; | ||
| 30 | |||
| 31 | private Map<String,ClassIndex> m_classesByObf; | ||
| 32 | private Map<String,ClassIndex> m_classesByDeobf; | ||
| 33 | private Ancestries m_ancestries; | ||
| 34 | |||
| 35 | public TranslationMappings( Ancestries ancestries ) | ||
| 36 | { | ||
| 37 | m_classesByObf = Maps.newHashMap(); | ||
| 38 | m_classesByDeobf = Maps.newHashMap(); | ||
| 39 | m_ancestries = ancestries; | ||
| 40 | } | ||
| 41 | |||
| 42 | public static TranslationMappings newFromResource( String resource ) | ||
| 43 | throws IOException | ||
| 44 | { | ||
| 45 | InputStream in = null; | ||
| 46 | try | ||
| 47 | { | ||
| 48 | in = TranslationMappings.class.getResourceAsStream( resource ); | ||
| 49 | return newFromStream( in ); | ||
| 50 | } | ||
| 51 | finally | ||
| 52 | { | ||
| 53 | Util.closeQuietly( in ); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | public Translator getTranslator( TranslationDirection direction ) | ||
| 58 | { | ||
| 59 | return new Translator( | ||
| 60 | direction, | ||
| 61 | direction.choose( m_classesByObf, m_classesByDeobf ), | ||
| 62 | direction.choose( m_ancestries, new DeobfuscatedAncestries( m_ancestries, m_classesByObf, m_classesByDeobf ) ) | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | public void setClassName( ClassEntry obf, String deobfName ) | ||
| 67 | { | ||
| 68 | ClassIndex classIndex = m_classesByObf.get( obf.getName() ); | ||
| 69 | if( classIndex == null ) | ||
| 70 | { | ||
| 71 | classIndex = createClassIndex( obf ); | ||
| 72 | } | ||
| 73 | |||
| 74 | m_classesByDeobf.remove( classIndex.getDeobfName() ); | ||
| 75 | classIndex.setDeobfName( deobfName ); | ||
| 76 | m_classesByDeobf.put( deobfName, classIndex ); | ||
| 77 | |||
| 78 | updateDeobfMethodSignatures(); | ||
| 79 | |||
| 80 | // TEMP | ||
| 81 | String translatedName = getTranslator( TranslationDirection.Deobfuscating ).translate( obf ); | ||
| 82 | assert( translatedName != null && translatedName.equals( deobfName ) ); | ||
| 83 | } | ||
| 84 | |||
| 85 | public void setFieldName( FieldEntry obf, String deobfName ) | ||
| 86 | { | ||
| 87 | ClassIndex classIndex = m_classesByObf.get( obf.getClassName() ); | ||
| 88 | if( classIndex == null ) | ||
| 89 | { | ||
| 90 | classIndex = createClassIndex( obf.getClassEntry() ); | ||
| 91 | } | ||
| 92 | |||
| 93 | classIndex.setFieldName( obf.getName(), deobfName ); | ||
| 94 | |||
| 95 | // TEMP | ||
| 96 | System.out.println( classIndex ); | ||
| 97 | String translatedName = getTranslator( TranslationDirection.Deobfuscating ).translate( obf ); | ||
| 98 | assert( translatedName != null && translatedName.equals( deobfName ) ); | ||
| 99 | } | ||
| 100 | |||
| 101 | public void setMethodName( MethodEntry obf, String deobfName ) | ||
| 102 | { | ||
| 103 | ClassIndex classIndex = m_classesByObf.get( obf.getClassName() ); | ||
| 104 | if( classIndex == null ) | ||
| 105 | { | ||
| 106 | classIndex = createClassIndex( obf.getClassEntry() ); | ||
| 107 | } | ||
| 108 | |||
| 109 | String deobfSignature = getTranslator( TranslationDirection.Deobfuscating ).translateSignature( obf.getSignature() ); | ||
| 110 | classIndex.setMethodNameAndSignature( obf.getName(), obf.getSignature(), deobfName, deobfSignature ); | ||
| 111 | |||
| 112 | // TODO: update ancestor/descendant methods in other classes in the inheritance hierarchy too | ||
| 113 | |||
| 114 | // TEMP | ||
| 115 | System.out.println( classIndex ); | ||
| 116 | String translatedName = getTranslator( TranslationDirection.Deobfuscating ).translate( obf ); | ||
| 117 | assert( translatedName != null && translatedName.equals( deobfName ) ); | ||
| 118 | } | ||
| 119 | |||
| 120 | public void setArgumentName( ArgumentEntry obf, String deobfName ) | ||
| 121 | { | ||
| 122 | ClassIndex classIndex = m_classesByObf.get( obf.getClassName() ); | ||
| 123 | if( classIndex == null ) | ||
| 124 | { | ||
| 125 | classIndex = createClassIndex( obf.getClassEntry() ); | ||
| 126 | } | ||
| 127 | |||
| 128 | classIndex.setArgumentName( obf.getMethodName(), obf.getMethodSignature(), obf.getIndex(), obf.getName(), deobfName ); | ||
| 129 | |||
| 130 | // TEMP | ||
| 131 | System.out.println( classIndex ); | ||
| 132 | String translatedName = getTranslator( TranslationDirection.Deobfuscating ).translate( obf ); | ||
| 133 | assert( translatedName != null && translatedName.equals( deobfName ) ); | ||
| 134 | } | ||
| 135 | |||
| 136 | public void write( OutputStream out ) | ||
| 137 | throws IOException | ||
| 138 | { | ||
| 139 | // TEMP: just use the object output for now. We can find a more efficient storage format later | ||
| 140 | GZIPOutputStream gzipout = new GZIPOutputStream( out ); | ||
| 141 | ObjectOutputStream oout = new ObjectOutputStream( gzipout ); | ||
| 142 | oout.writeObject( this ); | ||
| 143 | gzipout.finish(); | ||
| 144 | } | ||
| 145 | |||
| 146 | public static TranslationMappings newFromStream( InputStream in ) | ||
| 147 | throws IOException | ||
| 148 | { | ||
| 149 | try | ||
| 150 | { | ||
| 151 | return (TranslationMappings)new ObjectInputStream( new GZIPInputStream( in ) ).readObject(); | ||
| 152 | } | ||
| 153 | catch( ClassNotFoundException ex ) | ||
| 154 | { | ||
| 155 | throw new Error( ex ); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | private ClassIndex createClassIndex( ClassEntry obf ) | ||
| 160 | { | ||
| 161 | ClassIndex classIndex = new ClassIndex( obf.getName(), obf.getName() ); | ||
| 162 | m_classesByObf.put( classIndex.getObfName(), classIndex ); | ||
| 163 | m_classesByDeobf.put( classIndex.getDeobfName(), classIndex ); | ||
| 164 | return classIndex; | ||
| 165 | } | ||
| 166 | |||
| 167 | private void updateDeobfMethodSignatures( ) | ||
| 168 | { | ||
| 169 | Translator translator = getTranslator( TranslationDirection.Deobfuscating ); | ||
| 170 | for( ClassIndex classIndex : m_classesByObf.values() ) | ||
| 171 | { | ||
| 172 | classIndex.updateDeobfMethodSignatures( translator ); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | @Override | ||
| 177 | public String toString( ) | ||
| 178 | { | ||
| 179 | StringBuilder buf = new StringBuilder(); | ||
| 180 | for( ClassIndex classIndex : m_classesByObf.values() ) | ||
| 181 | { | ||
| 182 | buf.append( classIndex.toString() ); | ||
| 183 | buf.append( "\n" ); | ||
| 184 | } | ||
| 185 | return buf.toString(); | ||
| 186 | } | ||
| 187 | } | ||