summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/mapping/TranslationMappings.java
blob: d6cd44916f0c702467952d523068e5db69ca0527 (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
/*******************************************************************************
 * 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.mapping;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import com.beust.jcommander.internal.Maps;

import cuchaz.enigma.Util;

public class TranslationMappings implements Serializable
{
	private static final long serialVersionUID = 4649790259460259026L;
	
	private Map<String,ClassIndex> m_classesByObf;
	private Map<String,ClassIndex> m_classesByDeobf;
	private Ancestries m_ancestries;
	
	public TranslationMappings( Ancestries ancestries )
	{
		m_classesByObf = Maps.newHashMap();
		m_classesByDeobf = Maps.newHashMap();
		m_ancestries = ancestries;
	}
	
	public static TranslationMappings newFromResource( String resource )
	throws IOException
	{
		InputStream in = null;
		try
		{
			in = TranslationMappings.class.getResourceAsStream( resource );
			return newFromStream( in );
		}
		finally
		{
			Util.closeQuietly( in );
		}
	}
	
	public Translator getTranslator( TranslationDirection direction )
	{
		return new Translator(
			direction,
			direction.choose( m_classesByObf, m_classesByDeobf ),
			direction.choose( m_ancestries, new DeobfuscatedAncestries( m_ancestries, m_classesByObf, m_classesByDeobf ) )
		);
	}
	
	public void setClassName( ClassEntry obf, String deobfName )
	{
		ClassIndex classIndex = m_classesByObf.get( obf.getName() );
		if( classIndex == null )
		{
			classIndex = createClassIndex( obf );
		}
		
		m_classesByDeobf.remove( classIndex.getDeobfName() );
		classIndex.setDeobfName( deobfName );
		m_classesByDeobf.put( deobfName, classIndex );
		
		updateDeobfMethodSignatures();
		
		// TEMP
		String translatedName = getTranslator( TranslationDirection.Deobfuscating ).translate( obf );
		assert( translatedName != null && translatedName.equals( deobfName ) );
	}
	
	public void setFieldName( FieldEntry obf, String deobfName )
	{
		ClassIndex classIndex = m_classesByObf.get( obf.getClassName() );
		if( classIndex == null )
		{
			classIndex = createClassIndex( obf.getClassEntry() );
		}
		
		classIndex.setFieldName( obf.getName(), deobfName );
		
		// TEMP
		System.out.println( classIndex );
		String translatedName = getTranslator( TranslationDirection.Deobfuscating ).translate( obf );
		assert( translatedName != null && translatedName.equals( deobfName ) );
	}
	
	public void setMethodName( MethodEntry obf, String deobfName )
	{
		ClassIndex classIndex = m_classesByObf.get( obf.getClassName() );
		if( classIndex == null )
		{
			classIndex = createClassIndex( obf.getClassEntry() );
		}
		
		String deobfSignature = getTranslator( TranslationDirection.Deobfuscating ).translateSignature( obf.getSignature() );
		classIndex.setMethodNameAndSignature( obf.getName(), obf.getSignature(), deobfName, deobfSignature );
		
		// TODO: update ancestor/descendant methods in other classes in the inheritance hierarchy too
		
		// TEMP
		System.out.println( classIndex );
		String translatedName = getTranslator( TranslationDirection.Deobfuscating ).translate( obf );
		assert( translatedName != null && translatedName.equals( deobfName ) );
	}
	
	public void setArgumentName( ArgumentEntry obf, String deobfName )
	{
		ClassIndex classIndex = m_classesByObf.get( obf.getClassName() );
		if( classIndex == null )
		{
			classIndex = createClassIndex( obf.getClassEntry() );
		}
		
		classIndex.setArgumentName( obf.getMethodName(), obf.getMethodSignature(), obf.getIndex(), obf.getName(), deobfName );
		
		// TEMP
		System.out.println( classIndex );
		String translatedName = getTranslator( TranslationDirection.Deobfuscating ).translate( obf );
		assert( translatedName != null && translatedName.equals( deobfName ) );
	}
	
	public void write( OutputStream out )
	throws IOException
	{
		// TEMP: just use the object output for now. We can find a more efficient storage format later
		GZIPOutputStream gzipout = new GZIPOutputStream( out );
		ObjectOutputStream oout = new ObjectOutputStream( gzipout );
		oout.writeObject( this );
		gzipout.finish();
	}
	
	public static TranslationMappings newFromStream( InputStream in )
	throws IOException
	{
		try
		{
			return (TranslationMappings)new ObjectInputStream( new GZIPInputStream( in ) ).readObject();
		}
		catch( ClassNotFoundException ex )
		{
			throw new Error( ex );
		}
	}
	
	private ClassIndex createClassIndex( ClassEntry obf )
	{
		ClassIndex classIndex = new ClassIndex( obf.getName(), obf.getName() );
		m_classesByObf.put( classIndex.getObfName(), classIndex );
		m_classesByDeobf.put( classIndex.getDeobfName(), classIndex );
		return classIndex;
	}
	
	private void updateDeobfMethodSignatures( )
	{
		Translator translator = getTranslator( TranslationDirection.Deobfuscating );
		for( ClassIndex classIndex : m_classesByObf.values() )
		{
			classIndex.updateDeobfMethodSignatures( translator );
		}
	}
	
	@Override
	public String toString( )
	{
		StringBuilder buf = new StringBuilder();
		for( ClassIndex classIndex : m_classesByObf.values() )
		{
			buf.append( classIndex.toString() );
			buf.append( "\n" );
		}
		return buf.toString();
	}
}