diff options
| author | 2015-02-09 12:17:26 -0500 | |
|---|---|---|
| committer | 2015-02-09 12:17:26 -0500 | |
| commit | 71ec7b53a1b4ecce0623dded1e445818a757b695 (patch) | |
| tree | 335c2c4bb5535d5b4bd51c2d448655297a4e2efd /src/cuchaz/enigma/MainFormatConverter.java | |
| parent | added types to fields (diff) | |
| download | enigma-fork-71ec7b53a1b4ecce0623dded1e445818a757b695.tar.gz enigma-fork-71ec7b53a1b4ecce0623dded1e445818a757b695.tar.xz enigma-fork-71ec7b53a1b4ecce0623dded1e445818a757b695.zip | |
add converter to update old mappings format
fix a few decompiler issues too
Diffstat (limited to 'src/cuchaz/enigma/MainFormatConverter.java')
| -rw-r--r-- | src/cuchaz/enigma/MainFormatConverter.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/MainFormatConverter.java b/src/cuchaz/enigma/MainFormatConverter.java new file mode 100644 index 0000000..1848144 --- /dev/null +++ b/src/cuchaz/enigma/MainFormatConverter.java | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | package cuchaz.enigma; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.io.FileReader; | ||
| 5 | import java.io.FileWriter; | ||
| 6 | import java.lang.reflect.Field; | ||
| 7 | import java.util.Map; | ||
| 8 | import java.util.jar.JarFile; | ||
| 9 | |||
| 10 | import javassist.CtClass; | ||
| 11 | import javassist.CtField; | ||
| 12 | |||
| 13 | import com.google.common.collect.Maps; | ||
| 14 | |||
| 15 | import cuchaz.enigma.analysis.JarClassIterator; | ||
| 16 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 17 | import cuchaz.enigma.mapping.ClassMapping; | ||
| 18 | import cuchaz.enigma.mapping.ClassNameReplacer; | ||
| 19 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 20 | import cuchaz.enigma.mapping.FieldMapping; | ||
| 21 | import cuchaz.enigma.mapping.JavassistUtil; | ||
| 22 | import cuchaz.enigma.mapping.Mappings; | ||
| 23 | import cuchaz.enigma.mapping.MappingsReader; | ||
| 24 | import cuchaz.enigma.mapping.MappingsWriter; | ||
| 25 | import cuchaz.enigma.mapping.Type; | ||
| 26 | |||
| 27 | public class MainFormatConverter { | ||
| 28 | |||
| 29 | public static void main(String[] args) | ||
| 30 | throws Exception { | ||
| 31 | |||
| 32 | System.out.println("Getting field types from jar..."); | ||
| 33 | |||
| 34 | JarFile jar = new JarFile(System.getProperty("user.home") + "/.minecraft/versions/1.8/1.8.jar"); | ||
| 35 | Map<String,Type> fieldTypes = Maps.newHashMap(); | ||
| 36 | for (CtClass c : JarClassIterator.classes(jar)) { | ||
| 37 | for (CtField field : c.getDeclaredFields()) { | ||
| 38 | FieldEntry fieldEntry = JavassistUtil.getFieldEntry(field); | ||
| 39 | fieldTypes.put(getFieldKey(fieldEntry), moveClasssesOutOfDefaultPackage(fieldEntry.getType())); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | System.out.println("Reading mappings..."); | ||
| 44 | |||
| 45 | File fileMappings = new File("../Enigma Mappings/1.8.mappings"); | ||
| 46 | MappingsReader mappingsReader = new MappingsReader() { | ||
| 47 | |||
| 48 | @Override | ||
| 49 | protected FieldMapping readField(String[] parts) { | ||
| 50 | // assume the void type for now | ||
| 51 | return new FieldMapping(parts[1], new Type("V"), parts[2]); | ||
| 52 | } | ||
| 53 | }; | ||
| 54 | Mappings mappings = mappingsReader.read(new FileReader(fileMappings)); | ||
| 55 | |||
| 56 | System.out.println("Updating field types..."); | ||
| 57 | |||
| 58 | for (ClassMapping classMapping : mappings.classes()) { | ||
| 59 | updateFieldsInClass(fieldTypes, classMapping); | ||
| 60 | } | ||
| 61 | |||
| 62 | System.out.println("Saving mappings..."); | ||
| 63 | |||
| 64 | try (FileWriter writer = new FileWriter(fileMappings)) { | ||
| 65 | new MappingsWriter().write(writer, mappings); | ||
| 66 | } | ||
| 67 | |||
| 68 | System.out.println("Done!"); | ||
| 69 | } | ||
| 70 | |||
| 71 | private static Type moveClasssesOutOfDefaultPackage(Type type) { | ||
| 72 | return new Type(type, new ClassNameReplacer() { | ||
| 73 | @Override | ||
| 74 | public String replace(String className) { | ||
| 75 | ClassEntry entry = new ClassEntry(className); | ||
| 76 | if (entry.isInDefaultPackage()) { | ||
| 77 | return Constants.NonePackage + "/" + className; | ||
| 78 | } | ||
| 79 | return null; | ||
| 80 | } | ||
| 81 | }); | ||
| 82 | } | ||
| 83 | |||
| 84 | private static void updateFieldsInClass(Map<String,Type> fieldTypes, ClassMapping classMapping) | ||
| 85 | throws Exception { | ||
| 86 | |||
| 87 | // update the fields | ||
| 88 | for (FieldMapping fieldMapping : classMapping.fields()) { | ||
| 89 | setFieldType(fieldTypes, classMapping, fieldMapping); | ||
| 90 | } | ||
| 91 | |||
| 92 | // recurse | ||
| 93 | for (ClassMapping innerClassMapping : classMapping.innerClasses()) { | ||
| 94 | updateFieldsInClass(fieldTypes, innerClassMapping); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | private static void setFieldType(Map<String,Type> fieldTypes, ClassMapping classMapping, FieldMapping fieldMapping) | ||
| 99 | throws Exception { | ||
| 100 | |||
| 101 | // get the new type | ||
| 102 | Type newType = fieldTypes.get(getFieldKey(classMapping, fieldMapping)); | ||
| 103 | if (newType == null) { | ||
| 104 | throw new Error("Can't find type for field: " + getFieldKey(classMapping, fieldMapping)); | ||
| 105 | } | ||
| 106 | |||
| 107 | // hack in the new field type | ||
| 108 | Field field = fieldMapping.getClass().getDeclaredField("m_obfType"); | ||
| 109 | field.setAccessible(true); | ||
| 110 | field.set(fieldMapping, newType); | ||
| 111 | } | ||
| 112 | |||
| 113 | private static Object getFieldKey(ClassMapping classMapping, FieldMapping fieldMapping) { | ||
| 114 | return new ClassEntry(classMapping.getObfName()).getSimpleName() + "." + fieldMapping.getObfName(); | ||
| 115 | } | ||
| 116 | |||
| 117 | private static String getFieldKey(FieldEntry obfFieldEntry) { | ||
| 118 | return obfFieldEntry.getClassEntry().getSimpleName() + "." + obfFieldEntry.getName(); | ||
| 119 | } | ||
| 120 | } | ||