summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/convert/ClassMapper.java
blob: fd6ab9220b433cdf53513b788a9a6d8ce13bd519 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*******************************************************************************
 * 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.convert;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.jar.JarFile;

import javassist.CtClass;

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

import cuchaz.enigma.Constants;
import cuchaz.enigma.TranslatingTypeLoader;
import cuchaz.enigma.analysis.JarIndex;
import cuchaz.enigma.convert.ClassNamer.SidedClassNamer;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.ClassMapping;
import cuchaz.enigma.mapping.MappingParseException;
import cuchaz.enigma.mapping.Mappings;
import cuchaz.enigma.mapping.MappingsReader;
import cuchaz.enigma.mapping.MappingsWriter;

public class ClassMapper
{
	public static void main( String[] args )
	throws IOException, MappingParseException
	{
		// TEMP
		JarFile fromJar = new JarFile( new File( "input/1.8-pre1.jar" ) );
		JarFile toJar = new JarFile( new File( "input/1.8-pre3.jar" ) );
		File inMappingsFile = new File( "../minecraft-mappings/1.8-pre.mappings" );
		File outMappingsFile = new File( "../minecraft-mappings/1.8-pre3.mappings" );
		
		// compute the matching
		ClassMatching matching = ClassMapper.computeMatching( fromJar, toJar );
		
		// use the matching to convert the mappings
		Mappings mappings = new MappingsReader().read( new FileReader( inMappingsFile ) );
		Map<String,Map.Entry<ClassIdentity,List<ClassIdentity>>> conversionMap = matching.getConversionMap();
		Map<String,String> finalConversion = Maps.newHashMap();
		Set<String> unmatchedSourceClasses = Sets.newHashSet();
		for( ClassMapping classMapping : mappings.classes() )
		{
			// is there a match for this class?
			Map.Entry<ClassIdentity,List<ClassIdentity>> entry = conversionMap.get( classMapping.getObfName() );
			ClassIdentity sourceClass = entry.getKey();
			List<ClassIdentity> matches = entry.getValue();
			
			if( matches.isEmpty() )
			{
				// no match! =(
				System.out.println( "No exact match for source class " + classMapping.getObfName() );
				
				// find the closest classes
				TreeMap<Integer,ClassIdentity> scoredMatches = Maps.newTreeMap( Collections.reverseOrder() );
				for( ClassIdentity c : matching.getUnmatchedDestClasses() )
				{
					scoredMatches.put( sourceClass.getMatchScore( c ), c );
				}
				Iterator<Map.Entry<Integer,ClassIdentity>> iter = scoredMatches.entrySet().iterator();
				for( int i=0; i<10 && iter.hasNext(); i++ )
				{
					Map.Entry<Integer,ClassIdentity> score = iter.next();
					System.out.println( String.format( "\tScore: %3d   %s", score.getKey(), score.getValue().getClassEntry().getName() ) );
				}
				
				// does the best match have a non-zero score and the same name?
				Map.Entry<Integer,ClassIdentity> bestMatch = scoredMatches.firstEntry();
				if( bestMatch.getKey() > 0 && bestMatch.getValue().getClassEntry().equals( sourceClass.getClassEntry() ) )
				{
					// use it
					System.out.println( "\tAutomatically choosing likely match: " + bestMatch.getValue().getClassEntry().getName() );
					addFinalConversion( finalConversion, sourceClass, bestMatch.getValue() );
				}
				else
				{
					unmatchedSourceClasses.add( classMapping.getObfName() );
				}
			}
			if( matches.size() == 1 )
			{
				// unique match! We're good to go!
				addFinalConversion( finalConversion, sourceClass, matches.get( 0 ) );
			}
			else if( matches.size() > 1 )
			{
				// too many matches! =(
				unmatchedSourceClasses.add( classMapping.getObfName() );
			}
		}
		
		// remove (and warn about) unmatched classes
		if( !unmatchedSourceClasses.isEmpty() )
		{
			System.err.println( "WARNING: there were unmatched classes!" );
			for( String className : unmatchedSourceClasses )
			{
				System.err.println( "\t" + className );
				mappings.removeClassByObfName( className );
			}
			System.err.println( "Mappings for these classes have been removed." );
		}
		
		// show the class name changes
		for( Map.Entry<String,String> entry : finalConversion.entrySet() )
		{
			if( !entry.getKey().equals( entry.getValue() ) )
			{
				System.out.println( String.format( "Class change: %s -> %s", entry.getKey(), entry.getValue() ) );
			}
		}
		
		// do the final conversion
		mappings.renameObfClasses( finalConversion );
		FileWriter writer = new FileWriter( outMappingsFile );
		new MappingsWriter().write( writer, mappings );
		writer.close();
		System.out.println( "Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath() );
	}
	
	public static ClassMatching computeMatching( JarFile sourceJar, JarFile destJar )
	{
		// index jars
		System.out.println( "Indexing source jar..." );
		JarIndex sourceIndex = new JarIndex();
		sourceIndex.indexJar( sourceJar );
		System.out.println( "Indexing dest jar..." );
		JarIndex destIndex = new JarIndex();
		destIndex.indexJar( destJar );
		
		System.out.println( "Computing matching..." );
		
		TranslatingTypeLoader sourceLoader = new TranslatingTypeLoader( sourceJar, sourceIndex );
		TranslatingTypeLoader destLoader = new TranslatingTypeLoader( destJar, destIndex );

		ClassMatching matching = null;
		for( boolean useRawNames : Arrays.asList( false, true ) )
		{
			for( boolean useReferences : Arrays.asList( false, true ) )
			{
				int numMatches = 0;
				do
				{
					SidedClassNamer sourceNamer = null;
					SidedClassNamer destNamer = null;
					if( matching != null )
					{
						// build a class namer
						ClassNamer namer = new ClassNamer( matching.getUniqueMatches() );
						sourceNamer = namer.getSourceNamer();
						destNamer = namer.getDestNamer();
						
						// note the number of matches
						numMatches = matching.getUniqueMatches().size();
					}
					
					// get the entries left to match
					Set<ClassEntry> sourceClassEntries = sourceIndex.getObfClassEntries();
					Set<ClassEntry> destClassEntries = destIndex.getObfClassEntries();
					if( matching != null )
					{
						sourceClassEntries.clear();
						destClassEntries.clear();
						for( Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : matching.getAmbiguousMatches().entrySet() )
						{
							for( ClassIdentity c : entry.getKey() )
							{
								sourceClassEntries.add( c.getClassEntry() );
								matching.removeSource( c );
							}
							for( ClassIdentity c : entry.getValue() )
							{
								destClassEntries.add( c.getClassEntry() );
								matching.removeDest( c );
							}
						}
						for( ClassIdentity c : matching.getUnmatchedSourceClasses() )
						{
							sourceClassEntries.add( c.getClassEntry() );
							matching.removeSource( c );
						}
						for( ClassIdentity c : matching.getUnmatchedDestClasses() )
						{
							destClassEntries.add( c.getClassEntry() );
							matching.removeDest( c );
						}
					}
					else
					{
						matching = new ClassMatching();
					}
					
					// compute a matching for the classes
					for( ClassEntry classEntry : sourceClassEntries )
					{
						CtClass c = sourceLoader.loadClass( classEntry.getName() );
						ClassIdentity sourceClass = new ClassIdentity( c, sourceNamer, sourceIndex, useReferences, useRawNames );
						matching.addSource( sourceClass );
					}
					for( ClassEntry classEntry : destClassEntries )
					{
						CtClass c = destLoader.loadClass( classEntry.getName() );
						ClassIdentity destClass = new ClassIdentity( c, destNamer, destIndex, useReferences, useRawNames );
						matching.matchDestClass( destClass );
					}
					
					// TEMP
					System.out.println( matching );
				}
				while( matching.getUniqueMatches().size() - numMatches > 0 );
			}
		}
		
		/* DEBUG: show some ambiguous matches
		List<Map.Entry<List<ClassIdentity>,List<ClassIdentity>>> ambiguousMatches = new ArrayList<Map.Entry<List<ClassIdentity>,List<ClassIdentity>>>( matching.getAmbiguousMatches().entrySet() );
		Collections.sort( ambiguousMatches, new Comparator<Map.Entry<List<ClassIdentity>,List<ClassIdentity>>>( )
		{
			@Override
			public int compare( Map.Entry<List<ClassIdentity>,List<ClassIdentity>> a, Map.Entry<List<ClassIdentity>,List<ClassIdentity>> b )
			{
				String aName = a.getKey().get( 0 ).getClassEntry().getName();
				String bName = b.getKey().get( 0 ).getClassEntry().getName();
				return aName.compareTo( bName );
			}
		} );
		for( Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : ambiguousMatches )
		{
			for( ClassIdentity c : entry.getKey() )
			{
				System.out.print( c.getClassEntry().getName() + " " );
			}
			System.out.println();
		}
		Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry = ambiguousMatches.get( 7 );
		for( ClassIdentity c : entry.getKey() )
		{
			System.out.println( c );
		}
		for( ClassIdentity c : entry.getKey() )
		{
			System.out.println( decompile( sourceLoader, c.getClassEntry() ) );
		}
		*/
		
		return matching;
	}
	
	private static void addFinalConversion( Map<String,String> finalConversion, ClassIdentity sourceClass, ClassIdentity destClass )
	{
		// flatten inner classes since these are all obf classes in the none package
		String sourceClassName = sourceClass.getClassEntry().getName();
		if( sourceClass.getClassEntry().isInnerClass() )
		{
			sourceClassName = Constants.NonePackage + "/" + sourceClass.getClassEntry().getInnerClassName();
		}
		
		String destClassName = destClass.getClassEntry().getName();
		if( destClass.getClassEntry().isInnerClass() )
		{
			destClassName = Constants.NonePackage + "/" + destClass.getClassEntry().getInnerClassName();
		}
		
		finalConversion.put( sourceClassName, destClassName );
	}
	
	/* DEBUG
	private static String decompile( TranslatingTypeLoader loader, ClassEntry classEntry )
	{
		PlainTextOutput output = new PlainTextOutput();
		DecompilerSettings settings = DecompilerSettings.javaDefaults();
		settings.setForceExplicitImports( true );
		settings.setShowSyntheticMembers( true );
		settings.setTypeLoader( loader );
		Decompiler.decompile( classEntry.getName(), output, settings );
		return output.toString();
	}
	*/
}