diff options
Diffstat (limited to 'src/cuchaz/enigma/MainFormatConverter.java')
| -rw-r--r-- | src/cuchaz/enigma/MainFormatConverter.java | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/MainFormatConverter.java b/src/cuchaz/enigma/MainFormatConverter.java new file mode 100644 index 0000000..73ee41f --- /dev/null +++ b/src/cuchaz/enigma/MainFormatConverter.java | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma; | ||
| 12 | |||
| 13 | import java.io.File; | ||
| 14 | import java.io.FileReader; | ||
| 15 | import java.io.FileWriter; | ||
| 16 | import java.lang.reflect.Field; | ||
| 17 | import java.util.Map; | ||
| 18 | import java.util.jar.JarFile; | ||
| 19 | |||
| 20 | import javassist.CtClass; | ||
| 21 | import javassist.CtField; | ||
| 22 | |||
| 23 | import com.google.common.collect.Maps; | ||
| 24 | |||
| 25 | import cuchaz.enigma.analysis.JarClassIterator; | ||
| 26 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 27 | import cuchaz.enigma.mapping.ClassMapping; | ||
| 28 | import cuchaz.enigma.mapping.ClassNameReplacer; | ||
| 29 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 30 | import cuchaz.enigma.mapping.FieldMapping; | ||
| 31 | import cuchaz.enigma.mapping.EntryFactory; | ||
| 32 | import cuchaz.enigma.mapping.Mappings; | ||
| 33 | import cuchaz.enigma.mapping.MappingsReader; | ||
| 34 | import cuchaz.enigma.mapping.MappingsWriter; | ||
| 35 | import cuchaz.enigma.mapping.Type; | ||
| 36 | |||
| 37 | public class MainFormatConverter { | ||
| 38 | |||
| 39 | public static void main(String[] args) | ||
| 40 | throws Exception { | ||
| 41 | |||
| 42 | System.out.println("Getting field types from jar..."); | ||
| 43 | |||
| 44 | JarFile jar = new JarFile(System.getProperty("user.home") + "/.minecraft/versions/1.8/1.8.jar"); | ||
| 45 | Map<String,Type> fieldTypes = Maps.newHashMap(); | ||
| 46 | for (CtClass c : JarClassIterator.classes(jar)) { | ||
| 47 | for (CtField field : c.getDeclaredFields()) { | ||
| 48 | FieldEntry fieldEntry = EntryFactory.getFieldEntry(field); | ||
| 49 | fieldTypes.put(getFieldKey(fieldEntry), moveClasssesOutOfDefaultPackage(fieldEntry.getType())); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | System.out.println("Reading mappings..."); | ||
| 54 | |||
| 55 | File fileMappings = new File("../Enigma Mappings/1.8.mappings"); | ||
| 56 | MappingsReader mappingsReader = new MappingsReader() { | ||
| 57 | |||
| 58 | @Override | ||
| 59 | protected FieldMapping readField(String[] parts) { | ||
| 60 | // assume the void type for now | ||
| 61 | return new FieldMapping(parts[1], new Type("V"), parts[2]); | ||
| 62 | } | ||
| 63 | }; | ||
| 64 | Mappings mappings = mappingsReader.read(new FileReader(fileMappings)); | ||
| 65 | |||
| 66 | System.out.println("Updating field types..."); | ||
| 67 | |||
| 68 | for (ClassMapping classMapping : mappings.classes()) { | ||
| 69 | updateFieldsInClass(fieldTypes, classMapping); | ||
| 70 | } | ||
| 71 | |||
| 72 | System.out.println("Saving mappings..."); | ||
| 73 | |||
| 74 | try (FileWriter writer = new FileWriter(fileMappings)) { | ||
| 75 | new MappingsWriter().write(writer, mappings); | ||
| 76 | } | ||
| 77 | |||
| 78 | System.out.println("Done!"); | ||
| 79 | } | ||
| 80 | |||
| 81 | private static Type moveClasssesOutOfDefaultPackage(Type type) { | ||
| 82 | return new Type(type, new ClassNameReplacer() { | ||
| 83 | @Override | ||
| 84 | public String replace(String className) { | ||
| 85 | ClassEntry entry = new ClassEntry(className); | ||
| 86 | if (entry.isInDefaultPackage()) { | ||
| 87 | return Constants.NonePackage + "/" + className; | ||
| 88 | } | ||
| 89 | return null; | ||
| 90 | } | ||
| 91 | }); | ||
| 92 | } | ||
| 93 | |||
| 94 | private static void updateFieldsInClass(Map<String,Type> fieldTypes, ClassMapping classMapping) | ||
| 95 | throws Exception { | ||
| 96 | |||
| 97 | // update the fields | ||
| 98 | for (FieldMapping fieldMapping : classMapping.fields()) { | ||
| 99 | setFieldType(fieldTypes, classMapping, fieldMapping); | ||
| 100 | } | ||
| 101 | |||
| 102 | // recurse | ||
| 103 | for (ClassMapping innerClassMapping : classMapping.innerClasses()) { | ||
| 104 | updateFieldsInClass(fieldTypes, innerClassMapping); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | private static void setFieldType(Map<String,Type> fieldTypes, ClassMapping classMapping, FieldMapping fieldMapping) | ||
| 109 | throws Exception { | ||
| 110 | |||
| 111 | // get the new type | ||
| 112 | Type newType = fieldTypes.get(getFieldKey(classMapping, fieldMapping)); | ||
| 113 | if (newType == null) { | ||
| 114 | throw new Error("Can't find type for field: " + getFieldKey(classMapping, fieldMapping)); | ||
| 115 | } | ||
| 116 | |||
| 117 | // hack in the new field type | ||
| 118 | Field field = fieldMapping.getClass().getDeclaredField("m_obfType"); | ||
| 119 | field.setAccessible(true); | ||
| 120 | field.set(fieldMapping, newType); | ||
| 121 | } | ||
| 122 | |||
| 123 | private static Object getFieldKey(ClassMapping classMapping, FieldMapping fieldMapping) { | ||
| 124 | return classMapping.getObfSimpleName() + "." + fieldMapping.getObfName(); | ||
| 125 | } | ||
| 126 | |||
| 127 | private static String getFieldKey(FieldEntry obfFieldEntry) { | ||
| 128 | return obfFieldEntry.getClassEntry().getSimpleName() + "." + obfFieldEntry.getName(); | ||
| 129 | } | ||
| 130 | } | ||