diff options
Diffstat (limited to 'src/cuchaz/enigma/convert/ClassMatcher.java')
| -rw-r--r-- | src/cuchaz/enigma/convert/ClassMatcher.java | 356 |
1 files changed, 153 insertions, 203 deletions
diff --git a/src/cuchaz/enigma/convert/ClassMatcher.java b/src/cuchaz/enigma/convert/ClassMatcher.java index 224d004..f43f5b2 100644 --- a/src/cuchaz/enigma/convert/ClassMatcher.java +++ b/src/cuchaz/enigma/convert/ClassMatcher.java | |||
| @@ -26,9 +26,6 @@ import java.util.Map; | |||
| 26 | import java.util.Set; | 26 | import java.util.Set; |
| 27 | import java.util.jar.JarFile; | 27 | import java.util.jar.JarFile; |
| 28 | 28 | ||
| 29 | import javassist.CtBehavior; | ||
| 30 | import javassist.CtClass; | ||
| 31 | |||
| 32 | import com.google.common.collect.ArrayListMultimap; | 29 | import com.google.common.collect.ArrayListMultimap; |
| 33 | import com.google.common.collect.BiMap; | 30 | import com.google.common.collect.BiMap; |
| 34 | import com.google.common.collect.HashBiMap; | 31 | import com.google.common.collect.HashBiMap; |
| @@ -37,12 +34,10 @@ import com.google.common.collect.Maps; | |||
| 37 | import com.google.common.collect.Multimap; | 34 | import com.google.common.collect.Multimap; |
| 38 | import com.google.common.collect.Sets; | 35 | import com.google.common.collect.Sets; |
| 39 | 36 | ||
| 40 | import cuchaz.enigma.TranslatingTypeLoader; | ||
| 41 | import cuchaz.enigma.analysis.JarIndex; | 37 | import cuchaz.enigma.analysis.JarIndex; |
| 42 | import cuchaz.enigma.convert.ClassNamer.SidedClassNamer; | 38 | import cuchaz.enigma.convert.ClassNamer.SidedClassNamer; |
| 43 | import cuchaz.enigma.mapping.ClassEntry; | 39 | import cuchaz.enigma.mapping.ClassEntry; |
| 44 | import cuchaz.enigma.mapping.ClassMapping; | 40 | import cuchaz.enigma.mapping.ClassMapping; |
| 45 | import cuchaz.enigma.mapping.EntryFactory; | ||
| 46 | import cuchaz.enigma.mapping.MappingParseException; | 41 | import cuchaz.enigma.mapping.MappingParseException; |
| 47 | import cuchaz.enigma.mapping.Mappings; | 42 | import cuchaz.enigma.mapping.Mappings; |
| 48 | import cuchaz.enigma.mapping.MappingsReader; | 43 | import cuchaz.enigma.mapping.MappingsReader; |
| @@ -52,18 +47,23 @@ import cuchaz.enigma.mapping.MethodMapping; | |||
| 52 | 47 | ||
| 53 | public class ClassMatcher { | 48 | public class ClassMatcher { |
| 54 | 49 | ||
| 55 | public static void main(String[] args) throws IOException, MappingParseException { | 50 | public static void main(String[] args) |
| 56 | // TEMP | 51 | throws IOException, MappingParseException { |
| 57 | JarFile sourceJar = new JarFile(new File("input/1.8-pre3.jar")); | 52 | |
| 58 | JarFile destJar = new JarFile(new File("input/1.8.jar")); | 53 | // setup files |
| 59 | File inMappingsFile = new File("../Enigma Mappings/1.8-pre3.mappings"); | 54 | File home = new File(System.getProperty("user.home")); |
| 60 | File outMappingsFile = new File("../Enigma Mappings/1.8.mappings"); | 55 | JarFile sourceJar = new JarFile(new File(home, ".minecraft/versions/1.8/1.8.jar")); |
| 56 | JarFile destJar = new JarFile(new File(home, ".minecraft/versions/1.8.3/1.8.3.jar")); | ||
| 57 | File inMappingsFile = new File("../Enigma Mappings/1.8.mappings"); | ||
| 58 | File outMappingsFile = new File("../Enigma Mappings/1.8.3.mappings"); | ||
| 61 | 59 | ||
| 62 | // define a matching to use when the automated system cannot find a match | 60 | // define a matching to use when the automated system cannot find a match |
| 63 | Map<String,String> fallbackMatching = Maps.newHashMap(); | 61 | Map<String,String> fallbackMatching = Maps.newHashMap(); |
| 62 | /* | ||
| 64 | fallbackMatching.put("none/ayb", "none/ayf"); | 63 | fallbackMatching.put("none/ayb", "none/ayf"); |
| 65 | fallbackMatching.put("none/ayd", "none/ayd"); | 64 | fallbackMatching.put("none/ayd", "none/ayd"); |
| 66 | fallbackMatching.put("none/bgk", "unknown/bgk"); | 65 | fallbackMatching.put("none/bgk", "unknown/bgk"); |
| 66 | */ | ||
| 67 | 67 | ||
| 68 | // do the conversion | 68 | // do the conversion |
| 69 | Mappings mappings = new MappingsReader().read(new FileReader(inMappingsFile)); | 69 | Mappings mappings = new MappingsReader().read(new FileReader(inMappingsFile)); |
| @@ -77,6 +77,7 @@ public class ClassMatcher { | |||
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | private static void convertMappings(JarFile sourceJar, JarFile destJar, Mappings mappings, Map<String,String> fallbackMatching) { | 79 | private static void convertMappings(JarFile sourceJar, JarFile destJar, Mappings mappings, Map<String,String> fallbackMatching) { |
| 80 | |||
| 80 | // index jars | 81 | // index jars |
| 81 | System.out.println("Indexing source jar..."); | 82 | System.out.println("Indexing source jar..."); |
| 82 | JarIndex sourceIndex = new JarIndex(); | 83 | JarIndex sourceIndex = new JarIndex(); |
| @@ -84,48 +85,88 @@ public class ClassMatcher { | |||
| 84 | System.out.println("Indexing dest jar..."); | 85 | System.out.println("Indexing dest jar..."); |
| 85 | JarIndex destIndex = new JarIndex(); | 86 | JarIndex destIndex = new JarIndex(); |
| 86 | destIndex.indexJar(destJar, false); | 87 | destIndex.indexJar(destJar, false); |
| 87 | TranslatingTypeLoader sourceLoader = new TranslatingTypeLoader(sourceJar, sourceIndex); | ||
| 88 | TranslatingTypeLoader destLoader = new TranslatingTypeLoader(destJar, destIndex); | ||
| 89 | 88 | ||
| 90 | // compute the matching | 89 | // compute the matching |
| 91 | ClassMatching matching = computeMatching(sourceIndex, sourceLoader, destIndex, destLoader); | 90 | ClassMatching matching = computeMatching(sourceJar, sourceIndex, destJar, destIndex); |
| 92 | Map<String,Map.Entry<ClassIdentity,List<ClassIdentity>>> matchingIndex = matching.getIndex(); | ||
| 93 | 91 | ||
| 94 | // get all the obf class names used in the mappings | 92 | // get all the obf class names used in the mappings |
| 95 | Set<String> usedClassNames = mappings.getAllObfClassNames(); | 93 | Set<ClassEntry> usedClasses = Sets.newHashSet(); |
| 96 | Set<String> allClassNames = Sets.newHashSet(); | 94 | for (String className : mappings.getAllObfClassNames()) { |
| 97 | for (ClassEntry classEntry : sourceIndex.getObfClassEntries()) { | 95 | usedClasses.add(new ClassEntry(className)); |
| 98 | allClassNames.add(classEntry.getName()); | ||
| 99 | } | 96 | } |
| 100 | usedClassNames.retainAll(allClassNames); | 97 | System.out.println("Mappings reference " + usedClasses.size() + " classes"); |
| 101 | System.out.println("Used " + usedClassNames.size() + " classes in the mappings"); | ||
| 102 | 98 | ||
| 103 | // probabilistically match the non-uniquely-matched source classes | 99 | // see what the used classes map to |
| 104 | for (Map.Entry<ClassIdentity,List<ClassIdentity>> entry : matchingIndex.values()) { | 100 | BiMap<ClassEntry,ClassEntry> uniqueUsedMatches = HashBiMap.create(); |
| 105 | ClassIdentity sourceClass = entry.getKey(); | 101 | Map<ClassEntry,ClassMatch> ambiguousUsedMatches = Maps.newHashMap(); |
| 106 | List<ClassIdentity> destClasses = entry.getValue(); | 102 | Set<ClassEntry> unmatchedUsedClasses = Sets.newHashSet(); |
| 107 | 103 | for (ClassMatch match : matching.matches()) { | |
| 108 | // skip classes that are uniquely matched | 104 | Set<ClassEntry> matchUsedClasses = match.intersectSourceClasses(usedClasses); |
| 109 | if (destClasses.size() == 1) { | 105 | if (matchUsedClasses.isEmpty()) { |
| 110 | continue; | 106 | continue; |
| 111 | } | 107 | } |
| 112 | 108 | ||
| 113 | // skip classes that aren't used in the mappings | 109 | // classify the match |
| 114 | if (!usedClassNames.contains(sourceClass.getClassEntry().getName())) { | 110 | if (!match.isMatched()) { |
| 115 | continue; | 111 | // unmatched |
| 112 | unmatchedUsedClasses.addAll(matchUsedClasses); | ||
| 113 | } else { | ||
| 114 | if (match.isAmbiguous()) { | ||
| 115 | // ambiguously matched | ||
| 116 | for (ClassEntry matchUsedClass : matchUsedClasses) { | ||
| 117 | ambiguousUsedMatches.put(matchUsedClass, match); | ||
| 118 | } | ||
| 119 | } else { | ||
| 120 | // uniquely matched | ||
| 121 | uniqueUsedMatches.put(match.getUniqueSource(), match.getUniqueDest()); | ||
| 122 | } | ||
| 116 | } | 123 | } |
| 124 | } | ||
| 125 | |||
| 126 | // get unmatched dest classes | ||
| 127 | Set<ClassEntry> unmatchedDestClasses = Sets.newHashSet(); | ||
| 128 | for (ClassMatch match : matching.matches()) { | ||
| 129 | if (!match.isMatched()) { | ||
| 130 | unmatchedDestClasses.addAll(match.destClasses); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | // warn about the ambiguous used matches | ||
| 135 | if (ambiguousUsedMatches.size() > 0) { | ||
| 136 | System.out.println(String.format("%d source classes have ambiguous mappings", ambiguousUsedMatches.size())); | ||
| 137 | List<ClassMatch> ambiguousMatchesList = Lists.newArrayList(Sets.newHashSet(ambiguousUsedMatches.values())); | ||
| 138 | Collections.sort(ambiguousMatchesList, new Comparator<ClassMatch>() { | ||
| 139 | @Override | ||
| 140 | public int compare(ClassMatch a, ClassMatch b) { | ||
| 141 | String aName = a.sourceClasses.iterator().next().getName(); | ||
| 142 | String bName = b.sourceClasses.iterator().next().getName(); | ||
| 143 | return aName.compareTo(bName); | ||
| 144 | } | ||
| 145 | }); | ||
| 146 | for (ClassMatch match : ambiguousMatchesList) { | ||
| 147 | System.out.println("Ambiguous matching:"); | ||
| 148 | System.out.println("\tSource: " + getClassNames(match.sourceClasses)); | ||
| 149 | System.out.println("\tDest: " + getClassNames(match.destClasses)); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | // warn about unmatched used classes | ||
| 154 | for (ClassEntry unmatchedUsedClass : unmatchedUsedClasses) { | ||
| 155 | System.out.println("No exact match for source class " + unmatchedUsedClass.getClassEntry()); | ||
| 117 | 156 | ||
| 118 | System.out.println("No exact match for source class " + sourceClass.getClassEntry()); | 157 | // rank all the unmatched dest classes against the used class |
| 119 | 158 | ClassIdentity sourceIdentity = matching.getSourceIdentifier().identify(unmatchedUsedClass); | |
| 120 | // find the closest classes | 159 | Multimap<Integer,ClassEntry> scoredDestClasses = ArrayListMultimap.create(); |
| 121 | Multimap<Integer,ClassIdentity> scoredMatches = ArrayListMultimap.create(); | 160 | for (ClassEntry unmatchedDestClass : unmatchedDestClasses) { |
| 122 | for (ClassIdentity c : destClasses) { | 161 | ClassIdentity destIdentity = matching.getDestIdentifier().identify(unmatchedDestClass); |
| 123 | scoredMatches.put(sourceClass.getMatchScore(c), c); | 162 | scoredDestClasses.put(sourceIdentity.getMatchScore(destIdentity), unmatchedDestClass); |
| 124 | } | 163 | } |
| 125 | List<Integer> scores = new ArrayList<Integer>(scoredMatches.keySet()); | 164 | |
| 165 | List<Integer> scores = new ArrayList<Integer>(scoredDestClasses.keySet()); | ||
| 126 | Collections.sort(scores, Collections.reverseOrder()); | 166 | Collections.sort(scores, Collections.reverseOrder()); |
| 127 | printScoredMatches(sourceClass.getMaxMatchScore(), scores, scoredMatches); | 167 | printScoredMatches(sourceIdentity.getMaxMatchScore(), scores, scoredDestClasses); |
| 128 | 168 | ||
| 169 | /* TODO: re-enable auto-pick logic | ||
| 129 | // does the best match have a non-zero score and the same name? | 170 | // does the best match have a non-zero score and the same name? |
| 130 | int bestScore = scores.get(0); | 171 | int bestScore = scores.get(0); |
| 131 | Collection<ClassIdentity> bestMatches = scoredMatches.get(bestScore); | 172 | Collection<ClassIdentity> bestMatches = scoredMatches.get(bestScore); |
| @@ -138,85 +179,45 @@ public class ClassMatcher { | |||
| 138 | destClasses.add(bestMatch); | 179 | destClasses.add(bestMatch); |
| 139 | } | 180 | } |
| 140 | } | 181 | } |
| 182 | */ | ||
| 141 | } | 183 | } |
| 142 | 184 | ||
| 143 | // group the matching into unique and non-unique matches | 185 | // bail if there were unmatched classes |
| 144 | BiMap<String,String> matchedClassNames = HashBiMap.create(); | 186 | if (!unmatchedUsedClasses.isEmpty()) { |
| 145 | Set<String> unmatchedSourceClassNames = Sets.newHashSet(); | 187 | throw new Error("There were " + unmatchedUsedClasses.size() + " unmatched classes!"); |
| 146 | for (String className : usedClassNames) { | ||
| 147 | // is there a match for this class? | ||
| 148 | Map.Entry<ClassIdentity,List<ClassIdentity>> entry = matchingIndex.get(className); | ||
| 149 | ClassIdentity sourceClass = entry.getKey(); | ||
| 150 | List<ClassIdentity> matches = entry.getValue(); | ||
| 151 | |||
| 152 | if (matches.size() == 1) { | ||
| 153 | // unique match! We're good to go! | ||
| 154 | matchedClassNames.put(sourceClass.getClassEntry().getName(), matches.get(0).getClassEntry().getName()); | ||
| 155 | } else { | ||
| 156 | // no match, check the fallback matching | ||
| 157 | String fallbackMatch = fallbackMatching.get(className); | ||
| 158 | if (fallbackMatch != null) { | ||
| 159 | matchedClassNames.put(sourceClass.getClassEntry().getName(), fallbackMatch); | ||
| 160 | } else { | ||
| 161 | unmatchedSourceClassNames.add(className); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | // report unmatched classes | ||
| 167 | if (!unmatchedSourceClassNames.isEmpty()) { | ||
| 168 | System.err.println("ERROR: there were unmatched classes!"); | ||
| 169 | for (String className : unmatchedSourceClassNames) { | ||
| 170 | System.err.println("\t" + className); | ||
| 171 | } | ||
| 172 | return; | ||
| 173 | } | ||
| 174 | |||
| 175 | // get the class name changes from the matched class names | ||
| 176 | Map<String,String> classChanges = Maps.newHashMap(); | ||
| 177 | for (Map.Entry<String,String> entry : matchedClassNames.entrySet()) { | ||
| 178 | if (!entry.getKey().equals(entry.getValue())) { | ||
| 179 | classChanges.put(entry.getKey(), entry.getValue()); | ||
| 180 | System.out.println(String.format("Class change: %s -> %s", entry.getKey(), entry.getValue())); | ||
| 181 | /* DEBUG | ||
| 182 | System.out.println(String.format("\n%s\n%s", | ||
| 183 | new ClassIdentity(sourceLoader.loadClass(entry.getKey()), null, sourceIndex, false, false), | ||
| 184 | new ClassIdentity( destLoader.loadClass(entry.getValue()), null, destIndex, false, false) | ||
| 185 | )); | ||
| 186 | */ | ||
| 187 | } | ||
| 188 | } | 188 | } |
| 189 | 189 | ||
| 190 | // sort the changes so classes are renamed in the correct order | 190 | // sort the changes so classes are renamed in the correct order |
| 191 | // ie. if we have the mappings a->b, b->c, we have to apply b->c before a->b | 191 | // ie. if we have the mappings a->b, b->c, we have to apply b->c before a->b |
| 192 | LinkedHashMap<String,String> orderedClassChanges = Maps.newLinkedHashMap(); | 192 | BiMap<ClassEntry,ClassEntry> unsortedChanges = HashBiMap.create(uniqueUsedMatches); |
| 193 | int numChangesLeft = classChanges.size(); | 193 | LinkedHashMap<ClassEntry,ClassEntry> sortedChanges = Maps.newLinkedHashMap(); |
| 194 | while (!classChanges.isEmpty()) { | 194 | int numChangesLeft = unsortedChanges.size(); |
| 195 | Iterator<Map.Entry<String,String>> iter = classChanges.entrySet().iterator(); | 195 | while (!unsortedChanges.isEmpty()) { |
| 196 | Iterator<Map.Entry<ClassEntry,ClassEntry>> iter = unsortedChanges.entrySet().iterator(); | ||
| 196 | while (iter.hasNext()) { | 197 | while (iter.hasNext()) { |
| 197 | Map.Entry<String,String> entry = iter.next(); | 198 | Map.Entry<ClassEntry,ClassEntry> change = iter.next(); |
| 198 | if (classChanges.get(entry.getValue()) == null) { | 199 | if (unsortedChanges.containsKey(change.getValue())) { |
| 199 | orderedClassChanges.put(entry.getKey(), entry.getValue()); | 200 | sortedChanges.put(change.getKey(), change.getValue()); |
| 200 | iter.remove(); | 201 | iter.remove(); |
| 201 | } | 202 | } |
| 202 | } | 203 | } |
| 203 | 204 | ||
| 204 | // did we remove any changes? | 205 | // did we remove any changes? |
| 205 | if (numChangesLeft - classChanges.size() > 0) { | 206 | if (numChangesLeft - unsortedChanges.size() > 0) { |
| 206 | // keep going | 207 | // keep going |
| 207 | numChangesLeft = classChanges.size(); | 208 | numChangesLeft = unsortedChanges.size(); |
| 208 | } else { | 209 | } else { |
| 209 | // can't sort anymore. There must be a loop | 210 | // can't sort anymore. There must be a loop |
| 210 | break; | 211 | break; |
| 211 | } | 212 | } |
| 212 | } | 213 | } |
| 213 | if (classChanges.size() > 0) { | 214 | if (!unsortedChanges.isEmpty()) { |
| 214 | throw new Error(String.format("Unable to sort %d/%d class changes!", classChanges.size(), matchedClassNames.size())); | 215 | throw new Error(String.format("Unable to sort %d/%d class changes!", unsortedChanges.size(), uniqueUsedMatches.size())); |
| 215 | } | 216 | } |
| 216 | 217 | ||
| 217 | // convert the mappings in the correct class order | 218 | // convert the mappings in the correct class order |
| 218 | for (Map.Entry<String,String> entry : orderedClassChanges.entrySet()) { | 219 | for (Map.Entry<ClassEntry,ClassEntry> entry : sortedChanges.entrySet()) { |
| 219 | mappings.renameObfClass(entry.getKey(), entry.getValue()); | 220 | mappings.renameObfClass(entry.getKey().getName(), entry.getValue().getName()); |
| 220 | } | 221 | } |
| 221 | 222 | ||
| 222 | // check the method matches | 223 | // check the method matches |
| @@ -238,6 +239,7 @@ public class ClassMatcher { | |||
| 238 | if (!destIndex.containsObfBehavior(methodEntry)) { | 239 | if (!destIndex.containsObfBehavior(methodEntry)) { |
| 239 | System.err.println("WARNING: method doesn't match: " + methodEntry); | 240 | System.err.println("WARNING: method doesn't match: " + methodEntry); |
| 240 | 241 | ||
| 242 | /* TODO: show methods if needed | ||
| 241 | // show the available methods | 243 | // show the available methods |
| 242 | System.err.println("\tAvailable dest methods:"); | 244 | System.err.println("\tAvailable dest methods:"); |
| 243 | CtClass c = destLoader.loadClass(classMapping.getObfFullName()); | 245 | CtClass c = destLoader.loadClass(classMapping.getObfFullName()); |
| @@ -250,6 +252,7 @@ public class ClassMatcher { | |||
| 250 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { | 252 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { |
| 251 | System.err.println("\t\t" + EntryFactory.getBehaviorEntry(behavior)); | 253 | System.err.println("\t\t" + EntryFactory.getBehaviorEntry(behavior)); |
| 252 | } | 254 | } |
| 255 | */ | ||
| 253 | } | 256 | } |
| 254 | } | 257 | } |
| 255 | } | 258 | } |
| @@ -257,125 +260,72 @@ public class ClassMatcher { | |||
| 257 | System.out.println("Done!"); | 260 | System.out.println("Done!"); |
| 258 | } | 261 | } |
| 259 | 262 | ||
| 260 | public static ClassMatching computeMatching(JarIndex sourceIndex, TranslatingTypeLoader sourceLoader, JarIndex destIndex, TranslatingTypeLoader destLoader) { | 263 | public static ClassMatching computeMatching(JarFile sourceJar, JarIndex sourceIndex, JarFile destJar, JarIndex destIndex) { |
| 261 | 264 | ||
| 262 | System.out.println("Matching classes..."); | 265 | System.out.println("Iteratively matching classes..."); |
| 263 | 266 | ||
| 264 | ClassMatching matching = null; | 267 | ClassMatching lastMatching = null; |
| 268 | int round = 0; | ||
| 269 | SidedClassNamer sourceNamer = null; | ||
| 270 | SidedClassNamer destNamer = null; | ||
| 265 | for (boolean useReferences : Arrays.asList(false, true)) { | 271 | for (boolean useReferences : Arrays.asList(false, true)) { |
| 266 | int numMatches = 0; | 272 | |
| 267 | do { | 273 | int numUniqueMatchesLastTime = 0; |
| 268 | SidedClassNamer sourceNamer = null; | 274 | if (lastMatching != null) { |
| 269 | SidedClassNamer destNamer = null; | 275 | numUniqueMatchesLastTime = lastMatching.uniqueMatches().size(); |
| 270 | if (matching != null) { | 276 | } |
| 271 | // build a class namer | 277 | |
| 272 | ClassNamer namer = new ClassNamer(matching.getUniqueMatches()); | 278 | while (true) { |
| 273 | sourceNamer = namer.getSourceNamer(); | 279 | |
| 274 | destNamer = namer.getDestNamer(); | 280 | System.out.println("Round " + (++round) + " ..."); |
| 275 | 281 | ||
| 276 | // note the number of matches | 282 | // init the matching with identity settings |
| 277 | numMatches = matching.getUniqueMatches().size(); | 283 | ClassMatching matching = new ClassMatching( |
| 278 | } | 284 | new ClassIdentifier(sourceJar, sourceIndex, sourceNamer, useReferences), |
| 285 | new ClassIdentifier(destJar, destIndex, destNamer, useReferences) | ||
| 286 | ); | ||
| 279 | 287 | ||
| 280 | // get the entries left to match | 288 | if (lastMatching == null) { |
| 281 | Set<ClassEntry> sourceClassEntries = Sets.newHashSet(); | 289 | // search all classes |
| 282 | Set<ClassEntry> destClassEntries = Sets.newHashSet(); | 290 | matching.match(sourceIndex.getObfClassEntries(), destIndex.getObfClassEntries()); |
| 283 | if (matching == null) { | ||
| 284 | sourceClassEntries.addAll(sourceIndex.getObfClassEntries()); | ||
| 285 | destClassEntries.addAll(destIndex.getObfClassEntries()); | ||
| 286 | matching = new ClassMatching(); | ||
| 287 | } else { | 291 | } else { |
| 288 | for (Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : matching.getAmbiguousMatches().entrySet()) { | 292 | // we already know about these matches |
| 289 | for (ClassIdentity c : entry.getKey()) { | 293 | matching.addKnownMatches(lastMatching.uniqueMatches()); |
| 290 | sourceClassEntries.add(c.getClassEntry()); | 294 | |
| 291 | matching.removeSource(c); | 295 | // search unmatched and ambiguously-matched classes |
| 292 | } | 296 | matching.match(lastMatching.unmatchedSourceClasses(), lastMatching.unmatchedDestClasses()); |
| 293 | for (ClassIdentity c : entry.getValue()) { | 297 | for (ClassMatch match : lastMatching.ambiguousMatches()) { |
| 294 | destClassEntries.add(c.getClassEntry()); | 298 | matching.match(match.sourceClasses, match.destClasses); |
| 295 | matching.removeDest(c); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | for (ClassIdentity c : matching.getUnmatchedSourceClasses()) { | ||
| 299 | sourceClassEntries.add(c.getClassEntry()); | ||
| 300 | matching.removeSource(c); | ||
| 301 | } | ||
| 302 | for (ClassIdentity c : matching.getUnmatchedDestClasses()) { | ||
| 303 | destClassEntries.add(c.getClassEntry()); | ||
| 304 | matching.removeDest(c); | ||
| 305 | } | 299 | } |
| 306 | } | 300 | } |
| 301 | System.out.println(matching); | ||
| 302 | BiMap<ClassEntry,ClassEntry> uniqueMatches = matching.uniqueMatches(); | ||
| 307 | 303 | ||
| 308 | // compute a matching for the classes | 304 | // did we match anything new this time? |
| 309 | for (ClassEntry classEntry : sourceClassEntries) { | 305 | if (uniqueMatches.size() > numUniqueMatchesLastTime) { |
| 310 | CtClass c = sourceLoader.loadClass(classEntry.getName()); | 306 | numUniqueMatchesLastTime = uniqueMatches.size(); |
| 311 | ClassIdentity sourceClass = new ClassIdentity(c, sourceNamer, sourceIndex, useReferences); | 307 | lastMatching = matching; |
| 312 | matching.addSource(sourceClass); | 308 | } else { |
| 313 | } | 309 | break; |
| 314 | for (ClassEntry classEntry : destClassEntries) { | ||
| 315 | CtClass c = destLoader.loadClass(classEntry.getName()); | ||
| 316 | ClassIdentity destClass = new ClassIdentity(c, destNamer, destIndex, useReferences); | ||
| 317 | matching.matchDestClass(destClass); | ||
| 318 | } | 310 | } |
| 319 | 311 | ||
| 320 | // TEMP | 312 | // update the namers |
| 321 | System.out.println(matching); | 313 | ClassNamer namer = new ClassNamer(uniqueMatches); |
| 322 | } while (matching.getUniqueMatches().size() - numMatches > 0); | 314 | sourceNamer = namer.getSourceNamer(); |
| 323 | } | 315 | destNamer = namer.getDestNamer(); |
| 324 | |||
| 325 | // check the class matches | ||
| 326 | System.out.println("Checking class matches..."); | ||
| 327 | ClassNamer namer = new ClassNamer(matching.getUniqueMatches()); | ||
| 328 | SidedClassNamer sourceNamer = namer.getSourceNamer(); | ||
| 329 | SidedClassNamer destNamer = namer.getDestNamer(); | ||
| 330 | for (Map.Entry<ClassIdentity,ClassIdentity> entry : matching.getUniqueMatches().entrySet()) { | ||
| 331 | |||
| 332 | // check source | ||
| 333 | ClassIdentity sourceClass = entry.getKey(); | ||
| 334 | CtClass sourceC = sourceLoader.loadClass(sourceClass.getClassEntry().getName()); | ||
| 335 | assert (sourceC != null) : "Unable to load source class " + sourceClass.getClassEntry(); | ||
| 336 | assert (sourceClass.matches(sourceC)) : "Source " + sourceClass + " doesn't match " + new ClassIdentity(sourceC, sourceNamer, sourceIndex, false); | ||
| 337 | |||
| 338 | // check dest | ||
| 339 | ClassIdentity destClass = entry.getValue(); | ||
| 340 | CtClass destC = destLoader.loadClass(destClass.getClassEntry().getName()); | ||
| 341 | assert (destC != null) : "Unable to load dest class " + destClass.getClassEntry(); | ||
| 342 | assert (destClass.matches(destC)) : "Dest " + destClass + " doesn't match " + new ClassIdentity(destC, destNamer, destIndex, false); | ||
| 343 | } | ||
| 344 | |||
| 345 | // warn about the ambiguous matchings | ||
| 346 | List<Map.Entry<List<ClassIdentity>,List<ClassIdentity>>> ambiguousMatches = new ArrayList<Map.Entry<List<ClassIdentity>,List<ClassIdentity>>>(matching.getAmbiguousMatches().entrySet()); | ||
| 347 | Collections.sort(ambiguousMatches, new Comparator<Map.Entry<List<ClassIdentity>,List<ClassIdentity>>>() { | ||
| 348 | @Override | ||
| 349 | public int compare(Map.Entry<List<ClassIdentity>,List<ClassIdentity>> a, Map.Entry<List<ClassIdentity>,List<ClassIdentity>> b) { | ||
| 350 | String aName = a.getKey().get(0).getClassEntry().getName(); | ||
| 351 | String bName = b.getKey().get(0).getClassEntry().getName(); | ||
| 352 | return aName.compareTo(bName); | ||
| 353 | } | 316 | } |
| 354 | }); | ||
| 355 | for (Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : ambiguousMatches) { | ||
| 356 | System.out.println("Ambiguous matching:"); | ||
| 357 | System.out.println("\tSource: " + getClassNames(entry.getKey())); | ||
| 358 | System.out.println("\tDest: " + getClassNames(entry.getValue())); | ||
| 359 | } | 317 | } |
| 360 | 318 | ||
| 361 | /* DEBUG | 319 | return lastMatching; |
| 362 | Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry = ambiguousMatches.get( 7 ); | ||
| 363 | for (ClassIdentity c : entry.getKey()) { | ||
| 364 | System.out.println(c); | ||
| 365 | } | ||
| 366 | for(ClassIdentity c : entry.getKey()) { | ||
| 367 | System.out.println(decompile(sourceLoader, c.getClassEntry())); | ||
| 368 | } | ||
| 369 | */ | ||
| 370 | |||
| 371 | return matching; | ||
| 372 | } | 320 | } |
| 373 | 321 | ||
| 374 | private static void printScoredMatches(int maxScore, List<Integer> scores, Multimap<Integer,ClassIdentity> scoredMatches) { | 322 | private static void printScoredMatches(int maxScore, List<Integer> scores, Multimap<Integer,ClassEntry> scoredMatches) { |
| 375 | int numScoredMatchesShown = 0; | 323 | int numScoredMatchesShown = 0; |
| 376 | for (int score : scores) { | 324 | for (int score : scores) { |
| 377 | for (ClassIdentity scoredMatch : scoredMatches.get(score)) { | 325 | for (ClassEntry classEntry : scoredMatches.get(score)) { |
| 378 | System.out.println(String.format("\tScore: %3d %3.0f%% %s", score, 100.0 * score / maxScore, scoredMatch.getClassEntry().getName())); | 326 | System.out.println(String.format("\tScore: %3d %3.0f%% %s", |
| 327 | score, 100.0 * score / maxScore, classEntry.getName() | ||
| 328 | )); | ||
| 379 | if (numScoredMatchesShown++ > 10) { | 329 | if (numScoredMatchesShown++ > 10) { |
| 380 | return; | 330 | return; |
| 381 | } | 331 | } |
| @@ -383,10 +333,10 @@ public class ClassMatcher { | |||
| 383 | } | 333 | } |
| 384 | } | 334 | } |
| 385 | 335 | ||
| 386 | private static List<String> getClassNames(Collection<ClassIdentity> classes) { | 336 | private static List<String> getClassNames(Collection<ClassEntry> classes) { |
| 387 | List<String> out = Lists.newArrayList(); | 337 | List<String> out = Lists.newArrayList(); |
| 388 | for (ClassIdentity c : classes) { | 338 | for (ClassEntry c : classes) { |
| 389 | out.add(c.getClassEntry().getName()); | 339 | out.add(c.getName()); |
| 390 | } | 340 | } |
| 391 | Collections.sort(out); | 341 | Collections.sort(out); |
| 392 | return out; | 342 | return out; |