summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/mapping/Mappings.java
blob: 4b47d1607ae681a3204449497bb928bff422cce1 (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
188
189
190
191
192
/*******************************************************************************
 * 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.Serializable;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPInputStream;

import com.beust.jcommander.internal.Sets;
import com.google.common.collect.Maps;

import cuchaz.enigma.Util;
import cuchaz.enigma.analysis.Ancestries;
import cuchaz.enigma.analysis.DeobfuscatedAncestries;
import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater;

public class Mappings implements Serializable
{
	private static final long serialVersionUID = 4649790259460259026L;
	
	protected Map<String,ClassMapping> m_classesByObf;
	protected Map<String,ClassMapping> m_classesByDeobf;
	
	public Mappings( )
	{
		m_classesByObf = Maps.newHashMap();
		m_classesByDeobf = Maps.newHashMap();
	}
	
	public Mappings( Iterable<ClassMapping> classes )
	{
		this();
		
		for( ClassMapping classMapping : classes )
		{
			m_classesByObf.put( classMapping.getObfName(), classMapping );
			m_classesByDeobf.put( classMapping.getDeobfName(), classMapping );
		}
	}
	
	public static Mappings newFromResource( String resource )
	throws IOException
	{
		InputStream in = null;
		try
		{
			in = Mappings.class.getResourceAsStream( resource );
			return newFromStream( in );
		}
		finally
		{
			Util.closeQuietly( in );
		}
	}
	
	public Iterable<ClassMapping> classes( )
	{
		assert( m_classesByObf.size() == m_classesByDeobf.size() );
		return m_classesByObf.values();
	}
	
	protected void addClassMapping( ClassMapping classMapping )
	{
		if( m_classesByObf.containsKey( classMapping.getObfName() ) )
		{
			throw new Error( "Already have mapping for " + classMapping.getObfName() );
		}
		if( m_classesByDeobf.containsKey( classMapping.getDeobfName() ) )
		{
			throw new Error( "Already have mapping for " + classMapping.getDeobfName() );
		}
		m_classesByObf.put( classMapping.getObfName(), classMapping );
		m_classesByDeobf.put( classMapping.getDeobfName(), classMapping );
		assert( m_classesByObf.size() == m_classesByDeobf.size() );
	}
	
	public ClassMapping getClassByObf( ClassEntry entry )
	{
		return getClassByObf( entry.getName() );
	}
	
	public ClassMapping getClassByObf( String obfName )
	{
		return m_classesByObf.get( obfName );
	}
	
	public ClassMapping getClassByDeobf( ClassEntry entry )
	{
		return getClassByObf( entry.getName() );
	}
	
	public ClassMapping getClassByDeobf( String deobfName )
	{
		return m_classesByDeobf.get( deobfName );
	}
	
	public Translator getTranslator( Ancestries ancestries, TranslationDirection direction )
	{
		return new Translator(
			direction,
			direction.choose( m_classesByObf, m_classesByDeobf ),
			direction.choose( ancestries, new DeobfuscatedAncestries( ancestries, m_classesByObf, m_classesByDeobf ) )
		);
	}
	
	public static Mappings newFromStream( InputStream in )
	throws IOException
	{
		try
		{
			return (Mappings)new ObjectInputStream( new GZIPInputStream( in ) ).readObject();
		}
		catch( ClassNotFoundException ex )
		{
			throw new Error( ex );
		}
	}
	
	@Override
	public String toString( )
	{
		StringBuilder buf = new StringBuilder();
		for( ClassMapping classMapping : m_classesByObf.values() )
		{
			buf.append( classMapping.toString() );
			buf.append( "\n" );
		}
		return buf.toString();
	}
	
	public void renameObfClasses( Map<String,String> nameMap )
	{
		for( ClassMapping classMapping : new ArrayList<ClassMapping>( m_classesByObf.values() ) )
		{
			String newName = nameMap.get( classMapping.getObfName() );
			if( newName != null )
			{
				m_classesByObf.remove( classMapping.getObfName() );
				classMapping.renameObfClasses( nameMap );
				m_classesByObf.put( classMapping.getObfName(), classMapping );
			}
		}
	}

	public void removeClassByObfName( String obfName )
	{
		ClassMapping classMapping = m_classesByObf.get( obfName );
		if( classMapping != null )
		{
			m_classesByObf.remove( classMapping.getObfName() );
			m_classesByDeobf.remove( classMapping.getDeobfName() );
		}
	}

	public Set<String> getAllObfClassNames( )
	{
		final Set<String> classNames = Sets.newHashSet();
		for( ClassMapping classMapping : classes() )
		{
			// add the class name
			classNames.add( classMapping.getObfName() );
			
			// add classes from method signatures
			for( MethodMapping methodMapping : classMapping.methods() )
			{
				SignatureUpdater.update( methodMapping.getObfSignature(), new ClassNameUpdater( )
				{
					@Override
					public String update( String className )
					{
						classNames.add( className );
						return className;
					}
				} );
			}
		}
		return classNames;
	}
}