summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/mapping/Mappings.java
blob: f3b8fad103ff9ea4b08b8e7d42a8a38448f8d828 (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
/*******************************************************************************
 * 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.Map;
import java.util.zip.GZIPInputStream;

import com.google.common.collect.Maps;

import cuchaz.enigma.Util;
import cuchaz.enigma.analysis.Ancestries;
import cuchaz.enigma.analysis.DeobfuscatedAncestries;

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();
	}
}