Add inheritance map writing

New --write-inheritance/-H command-line option, iterates all classes
in mapping and writes inheritance (interfaces and superclasses) to file.

Example usage, generating inheritance map from MCPC+:

java -cp target/SpecialSource-1.3-SNAPSHOT-shaded.jar:mcpc-plus-1.4.7-R0.2-SNAPSHOT.jar net.md_5.specialsource.SpecialSource --srg-in ~/minecraft/1.4.x/jars/1.4.7/cb2obf.csrg --live --write-inheritance /tmp/h
This commit is contained in:
Agaricus 2013-01-27 13:10:54 -08:00
parent d23c72ae7a
commit a64fb14abd
2 changed files with 74 additions and 12 deletions

View File

@ -0,0 +1,48 @@
package net.md_5.specialsource;
import com.google.common.base.Joiner;
import java.io.File;
import java.io.PrintWriter;
import java.util.*;
public class InheritanceMap {
private final Map<String, ArrayList<String>> inheritanceMap = new HashMap<String, ArrayList<String>>();
public void generate(List<IInheritanceProvider> inheritanceProviders, Collection<String> classes) {
for (String className : classes) {
ArrayList<String> parents = getParents(inheritanceProviders, className);
if (parents == null) {
System.out.println("No inheritance information found for "+className);
} else {
inheritanceMap.put(className, parents);
}
}
}
private ArrayList<String> getParents(List<IInheritanceProvider> inheritanceProviders, String className) {
for (IInheritanceProvider inheritanceProvider : inheritanceProviders) {
// // ask each provider for inheritance information on the class, until one responds
// TODO: refactor with JarRemapper tryClimb?
List<String> parents = inheritanceProvider.getParents(className);
if (parents != null) {
return (ArrayList<String>) parents;
}
}
return null;
}
public void save(PrintWriter writer) {
for (String className : inheritanceMap.keySet()) {
writer.print(className);
writer.print(' ');
List<String> parents = inheritanceMap.get(className);
writer.println(Joiner.on(' ').join(parents));
}
}
}

View File

@ -28,10 +28,7 @@
*/
package net.md_5.specialsource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -85,6 +82,10 @@ public class SpecialSource {
acceptsAll(asList("l", "live"), "Enable runtime inheritance lookup");
acceptsAll(asList("L", "live-remapped"), "Enable runtime inheritance lookup through a mapping");
acceptsAll(asList("H", "write-inheritance"), "Write inheritance map to file")
.withRequiredArg()
.ofType(File.class);
acceptsAll(asList("q", "quiet"), "Quiet mode");
}
};
@ -143,6 +144,17 @@ public class SpecialSource {
}
log(jarMapping.classes.size() + " classes, " + jarMapping.fields.size() + " fields, " + jarMapping.methods.size() + " methods");
List<IInheritanceProvider> inheritanceProviders = new ArrayList<IInheritanceProvider>();
if (options.has("live-remapped")) {
inheritanceProviders.add(new RemappedRuntimeInheritanceProvider(ClassLoader.getSystemClassLoader(), !options.has("quiet"), jarMapping));
}
if (options.has("live")) {
inheritanceProviders.add(new RuntimeInheritanceProvider(ClassLoader.getSystemClassLoader(), !options.has("quiet")));
}
if (options.has("in-jar")) {
if (!options.has("out-jar")) {
System.err.println("No output jar given, in-jar requires out-jar");
@ -153,21 +165,23 @@ public class SpecialSource {
log("Remapping final jar");
Jar jar3 = Jar.init((File) options.valueOf("in-jar"));
List<IInheritanceProvider> inheritanceProviders = new ArrayList<IInheritanceProvider>();
inheritanceProviders.add(new JarInheritanceProvider(jar3));
if (options.has("live-remapped")) {
inheritanceProviders.add(new RemappedRuntimeInheritanceProvider(ClassLoader.getSystemClassLoader(), !options.has("quiet"), jarMapping));
}
if (options.has("live")) {
inheritanceProviders.add(new RuntimeInheritanceProvider(ClassLoader.getSystemClassLoader(), !options.has("quiet")));
}
JarRemapper jarRemapper = new JarRemapper(jarMapping, inheritanceProviders);
jarRemapper.remapJar(jar3, (File) options.valueOf("out-jar"));
}
if (options.has("write-inheritance")) {
InheritanceMap inheritanceMap = new InheritanceMap();
inheritanceMap.generate(inheritanceProviders, jarMapping.classes.values());
PrintWriter printWriter = new PrintWriter((File) options.valueOf("write-inheritance"));
inheritanceMap.save(printWriter);
printWriter.close();
}
}
private static void log(String message) {