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
|
/*******************************************************************************
* 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.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarFile;
import javassist.CtClass;
import cuchaz.enigma.TranslatingTypeLoader;
import cuchaz.enigma.analysis.JarIndex;
import cuchaz.enigma.convert.ClassNamer.SidedClassNamer;
import cuchaz.enigma.mapping.ClassEntry;
public class ClassMapper
{
public static void main( String[] args )
throws IOException
{
// TEMP
JarFile fromJar = new JarFile( new File( "input/1.8-pre1.jar" ) );
JarFile toJar = new JarFile( new File( "input/1.8-pre2.jar" ) );
// compute the matching
ClassMatching matching = ClassMapper.computeMatching( fromJar, toJar );
// TODO: use the matching to convert the mappings
}
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 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 );
matching.addSource( sourceClass );
}
for( ClassEntry classEntry : destClassEntries )
{
CtClass c = destLoader.loadClass( classEntry.getName() );
ClassIdentity destClass = new ClassIdentity( c, destNamer, destIndex, useReferences );
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;
}
/* 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();
}
*/
}
|