diff options
| author | 2016-06-30 00:49:21 +1000 | |
|---|---|---|
| committer | 2016-06-30 00:49:21 +1000 | |
| commit | 4be005617b3b8c3578cca07c5d085d12916f0d1d (patch) | |
| tree | db163431f38703e26da417ef05eaea2b27a498b9 /src/main/java/cuchaz/enigma/MainFormatConverter.java | |
| parent | Some small changes to fix idea importing (diff) | |
| download | enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.gz enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.xz enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.zip | |
Json format (#2)
* Added new format
* Fixed bug
* Updated Version
Diffstat (limited to 'src/main/java/cuchaz/enigma/MainFormatConverter.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/MainFormatConverter.java | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/MainFormatConverter.java b/src/main/java/cuchaz/enigma/MainFormatConverter.java new file mode 100644 index 0000000..29e334e --- /dev/null +++ b/src/main/java/cuchaz/enigma/MainFormatConverter.java | |||
| @@ -0,0 +1,121 @@ | |||
| 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 | * <p> | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma; | ||
| 12 | |||
| 13 | import com.google.common.collect.Maps; | ||
| 14 | |||
| 15 | import java.io.File; | ||
| 16 | import java.io.FileReader; | ||
| 17 | import java.io.FileWriter; | ||
| 18 | import java.lang.reflect.Field; | ||
| 19 | import java.util.Map; | ||
| 20 | import java.util.jar.JarFile; | ||
| 21 | |||
| 22 | import cuchaz.enigma.analysis.JarClassIterator; | ||
| 23 | import cuchaz.enigma.mapping.*; | ||
| 24 | import javassist.CtClass; | ||
| 25 | import javassist.CtField; | ||
| 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 = EntryFactory.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 obf, String deobf, String type) { | ||
| 50 | // assume the void type for now | ||
| 51 | return new FieldMapping(obf, new Type("V"), deobf); | ||
| 52 | } | ||
| 53 | }; | ||
| 54 | Mappings mappings = mappingsReader.read(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 | //TODO Fix | ||
| 65 | // try (FileWriter writer = new FileWriter(fileMappings)) { | ||
| 66 | // new MappingsWriter().write(writer, mappings); | ||
| 67 | // } | ||
| 68 | |||
| 69 | System.out.println("Done!"); | ||
| 70 | } | ||
| 71 | |||
| 72 | private static Type moveClasssesOutOfDefaultPackage(Type type) { | ||
| 73 | return new Type(type, new ClassNameReplacer() { | ||
| 74 | @Override | ||
| 75 | public String replace(String className) { | ||
| 76 | ClassEntry entry = new ClassEntry(className); | ||
| 77 | if (entry.isInDefaultPackage()) { | ||
| 78 | return Constants.NonePackage + "/" + className; | ||
| 79 | } | ||
| 80 | return null; | ||
| 81 | } | ||
| 82 | }); | ||
| 83 | } | ||
| 84 | |||
| 85 | private static void updateFieldsInClass(Map<String, Type> fieldTypes, ClassMapping classMapping) | ||
| 86 | throws Exception { | ||
| 87 | |||
| 88 | // update the fields | ||
| 89 | for (FieldMapping fieldMapping : classMapping.fields()) { | ||
| 90 | setFieldType(fieldTypes, classMapping, fieldMapping); | ||
| 91 | } | ||
| 92 | |||
| 93 | // recurse | ||
| 94 | for (ClassMapping innerClassMapping : classMapping.innerClasses()) { | ||
| 95 | updateFieldsInClass(fieldTypes, innerClassMapping); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | private static void setFieldType(Map<String, Type> fieldTypes, ClassMapping classMapping, FieldMapping fieldMapping) | ||
| 100 | throws Exception { | ||
| 101 | |||
| 102 | // get the new type | ||
| 103 | Type newType = fieldTypes.get(getFieldKey(classMapping, fieldMapping)); | ||
| 104 | if (newType == null) { | ||
| 105 | throw new Error("Can't find type for field: " + getFieldKey(classMapping, fieldMapping)); | ||
| 106 | } | ||
| 107 | |||
| 108 | // hack in the new field type | ||
| 109 | Field field = fieldMapping.getClass().getDeclaredField("m_obfType"); | ||
| 110 | field.setAccessible(true); | ||
| 111 | field.set(fieldMapping, newType); | ||
| 112 | } | ||
| 113 | |||
| 114 | private static Object getFieldKey(ClassMapping classMapping, FieldMapping fieldMapping) { | ||
| 115 | return classMapping.getObfSimpleName() + "." + fieldMapping.getObfName(); | ||
| 116 | } | ||
| 117 | |||
| 118 | private static String getFieldKey(FieldEntry obfFieldEntry) { | ||
| 119 | return obfFieldEntry.getClassEntry().getSimpleName() + "." + obfFieldEntry.getName(); | ||
| 120 | } | ||
| 121 | } | ||