summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar jeff2015-02-09 12:17:26 -0500
committerGravatar jeff2015-02-09 12:17:26 -0500
commit71ec7b53a1b4ecce0623dded1e445818a757b695 (patch)
tree335c2c4bb5535d5b4bd51c2d448655297a4e2efd /src
parentadded types to fields (diff)
downloadenigma-71ec7b53a1b4ecce0623dded1e445818a757b695.tar.gz
enigma-71ec7b53a1b4ecce0623dded1e445818a757b695.tar.xz
enigma-71ec7b53a1b4ecce0623dded1e445818a757b695.zip
add converter to update old mappings format
fix a few decompiler issues too
Diffstat (limited to 'src')
-rw-r--r--src/cuchaz/enigma/MainFormatConverter.java120
-rw-r--r--src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java12
-rw-r--r--src/cuchaz/enigma/bytecode/MethodParameterWriter.java8
-rw-r--r--src/cuchaz/enigma/mapping/MappingsReader.java36
4 files changed, 152 insertions, 24 deletions
diff --git a/src/cuchaz/enigma/MainFormatConverter.java b/src/cuchaz/enigma/MainFormatConverter.java
new file mode 100644
index 00000000..1848144d
--- /dev/null
+++ b/src/cuchaz/enigma/MainFormatConverter.java
@@ -0,0 +1,120 @@
1package cuchaz.enigma;
2
3import java.io.File;
4import java.io.FileReader;
5import java.io.FileWriter;
6import java.lang.reflect.Field;
7import java.util.Map;
8import java.util.jar.JarFile;
9
10import javassist.CtClass;
11import javassist.CtField;
12
13import com.google.common.collect.Maps;
14
15import cuchaz.enigma.analysis.JarClassIterator;
16import cuchaz.enigma.mapping.ClassEntry;
17import cuchaz.enigma.mapping.ClassMapping;
18import cuchaz.enigma.mapping.ClassNameReplacer;
19import cuchaz.enigma.mapping.FieldEntry;
20import cuchaz.enigma.mapping.FieldMapping;
21import cuchaz.enigma.mapping.JavassistUtil;
22import cuchaz.enigma.mapping.Mappings;
23import cuchaz.enigma.mapping.MappingsReader;
24import cuchaz.enigma.mapping.MappingsWriter;
25import cuchaz.enigma.mapping.Type;
26
27public 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}
diff --git a/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java
index f15a724b..b4094d9e 100644
--- a/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java
+++ b/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java
@@ -66,11 +66,11 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor {
66 if (ref instanceof MethodReference) { 66 if (ref instanceof MethodReference) {
67 MethodReference methodRef = (MethodReference)ref; 67 MethodReference methodRef = (MethodReference)ref;
68 if (methodRef.isConstructor()) { 68 if (methodRef.isConstructor()) {
69 behaviorEntry = new ConstructorEntry(classEntry, new Signature(ref.getSignature())); 69 behaviorEntry = new ConstructorEntry(classEntry, new Signature(ref.getErasedSignature()));
70 } else if (methodRef.isTypeInitializer()) { 70 } else if (methodRef.isTypeInitializer()) {
71 behaviorEntry = new ConstructorEntry(classEntry); 71 behaviorEntry = new ConstructorEntry(classEntry);
72 } else { 72 } else {
73 behaviorEntry = new MethodEntry(classEntry, ref.getName(), new Signature(ref.getSignature())); 73 behaviorEntry = new MethodEntry(classEntry, ref.getName(), new Signature(ref.getErasedSignature()));
74 } 74 }
75 } 75 }
76 if (behaviorEntry != null) { 76 if (behaviorEntry != null) {
@@ -96,12 +96,12 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor {
96 MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); 96 MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE);
97 if (ref != null) { 97 if (ref != null) {
98 // make sure this is actually a field 98 // make sure this is actually a field
99 if (ref.getSignature().indexOf('(') >= 0) { 99 if (ref.getErasedSignature().indexOf('(') >= 0) {
100 throw new Error("Expected a field here! got " + ref); 100 throw new Error("Expected a field here! got " + ref);
101 } 101 }
102 102
103 ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); 103 ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName());
104 FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName(), new Type(ref.getSignature())); 104 FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName(), new Type(ref.getErasedSignature()));
105 index.addReference(node.getMemberNameToken(), fieldEntry, m_behaviorEntry); 105 index.addReference(node.getMemberNameToken(), fieldEntry, m_behaviorEntry);
106 } 106 }
107 107
@@ -141,7 +141,7 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor {
141 MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); 141 MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE);
142 if (ref != null) { 142 if (ref != null) {
143 ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); 143 ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName());
144 FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName(), new Type(ref.getSignature())); 144 FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName(), new Type(ref.getErasedSignature()));
145 index.addReference(node.getIdentifierToken(), fieldEntry, m_behaviorEntry); 145 index.addReference(node.getIdentifierToken(), fieldEntry, m_behaviorEntry);
146 } 146 }
147 147
@@ -153,7 +153,7 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor {
153 MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); 153 MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE);
154 if (ref != null) { 154 if (ref != null) {
155 ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); 155 ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName());
156 ConstructorEntry constructorEntry = new ConstructorEntry(classEntry, new Signature(ref.getSignature())); 156 ConstructorEntry constructorEntry = new ConstructorEntry(classEntry, new Signature(ref.getErasedSignature()));
157 if (node.getType() instanceof SimpleType) { 157 if (node.getType() instanceof SimpleType) {
158 SimpleType simpleTypeNode = (SimpleType)node.getType(); 158 SimpleType simpleTypeNode = (SimpleType)node.getType();
159 index.addReference(simpleTypeNode.getIdentifierToken(), constructorEntry, m_behaviorEntry); 159 index.addReference(simpleTypeNode.getIdentifierToken(), constructorEntry, m_behaviorEntry);
diff --git a/src/cuchaz/enigma/bytecode/MethodParameterWriter.java b/src/cuchaz/enigma/bytecode/MethodParameterWriter.java
index 5d4ca1ad..853928c7 100644
--- a/src/cuchaz/enigma/bytecode/MethodParameterWriter.java
+++ b/src/cuchaz/enigma/bytecode/MethodParameterWriter.java
@@ -18,6 +18,7 @@ import javassist.CtClass;
18import cuchaz.enigma.mapping.ArgumentEntry; 18import cuchaz.enigma.mapping.ArgumentEntry;
19import cuchaz.enigma.mapping.BehaviorEntry; 19import cuchaz.enigma.mapping.BehaviorEntry;
20import cuchaz.enigma.mapping.BehaviorEntryFactory; 20import cuchaz.enigma.mapping.BehaviorEntryFactory;
21import cuchaz.enigma.mapping.Signature;
21import cuchaz.enigma.mapping.Translator; 22import cuchaz.enigma.mapping.Translator;
22 23
23public class MethodParameterWriter { 24public class MethodParameterWriter {
@@ -35,7 +36,12 @@ public class MethodParameterWriter {
35 BehaviorEntry behaviorEntry = BehaviorEntryFactory.create(behavior); 36 BehaviorEntry behaviorEntry = BehaviorEntryFactory.create(behavior);
36 37
37 // get the number of arguments 38 // get the number of arguments
38 int numParams = behaviorEntry.getSignature().getArgumentTypes().size(); 39 Signature signature = behaviorEntry.getSignature();
40 if (signature == null) {
41 // static initializers have no signatures, or arguments
42 continue;
43 }
44 int numParams = signature.getArgumentTypes().size();
39 if (numParams <= 0) { 45 if (numParams <= 0) {
40 continue; 46 continue;
41 } 47 }
diff --git a/src/cuchaz/enigma/mapping/MappingsReader.java b/src/cuchaz/enigma/mapping/MappingsReader.java
index 1e7b6e3d..bfb27319 100644
--- a/src/cuchaz/enigma/mapping/MappingsReader.java
+++ b/src/cuchaz/enigma/mapping/MappingsReader.java
@@ -19,11 +19,13 @@ import com.google.common.collect.Queues;
19 19
20public class MappingsReader { 20public class MappingsReader {
21 21
22 public Mappings read(Reader in) throws IOException, MappingParseException { 22 public Mappings read(Reader in)
23 throws IOException, MappingParseException {
23 return read(new BufferedReader(in)); 24 return read(new BufferedReader(in));
24 } 25 }
25 26
26 public Mappings read(BufferedReader in) throws IOException, MappingParseException { 27 public Mappings read(BufferedReader in)
28 throws IOException, MappingParseException {
27 Mappings mappings = new Mappings(); 29 Mappings mappings = new Mappings();
28 Deque<Object> mappingStack = Queues.newArrayDeque(); 30 Deque<Object> mappingStack = Queues.newArrayDeque();
29 31
@@ -64,42 +66,41 @@ public class MappingsReader {
64 66
65 if (token.equalsIgnoreCase("CLASS")) { 67 if (token.equalsIgnoreCase("CLASS")) {
66 ClassMapping classMapping; 68 ClassMapping classMapping;
67 if (indent == 0) { 69 if (indent <= 0) {
68 // outer class 70 // outer class
69 classMapping = readClass(parts, false); 71 classMapping = readClass(parts, false);
70 mappings.addClassMapping(classMapping); 72 mappings.addClassMapping(classMapping);
71 } else if (indent == 1) { 73 } else {
74
72 // inner class 75 // inner class
73 if (! (mappingStack.getFirst() instanceof ClassMapping)) { 76 if (!(mappingStack.peek() instanceof ClassMapping)) {
74 throw new MappingParseException(lineNumber, "Unexpected CLASS entry here!"); 77 throw new MappingParseException(lineNumber, "Unexpected CLASS entry here!");
75 } 78 }
76 79
77 classMapping = readClass(parts, true); 80 classMapping = readClass(parts, true);
78 ((ClassMapping)mappingStack.getFirst()).addInnerClassMapping(classMapping); 81 ((ClassMapping)mappingStack.peek()).addInnerClassMapping(classMapping);
79 } else {
80 throw new MappingParseException(lineNumber, "Unexpected CLASS entry nesting!");
81 } 82 }
82 mappingStack.push(classMapping); 83 mappingStack.push(classMapping);
83 } else if (token.equalsIgnoreCase("FIELD")) { 84 } else if (token.equalsIgnoreCase("FIELD")) {
84 if (mappingStack.isEmpty() || ! (mappingStack.getFirst() instanceof ClassMapping)) { 85 if (mappingStack.isEmpty() || ! (mappingStack.peek() instanceof ClassMapping)) {
85 throw new MappingParseException(lineNumber, "Unexpected FIELD entry here!"); 86 throw new MappingParseException(lineNumber, "Unexpected FIELD entry here!");
86 } 87 }
87 ((ClassMapping)mappingStack.getFirst()).addFieldMapping(readField(parts)); 88 ((ClassMapping)mappingStack.peek()).addFieldMapping(readField(parts));
88 } else if (token.equalsIgnoreCase("METHOD")) { 89 } else if (token.equalsIgnoreCase("METHOD")) {
89 if (mappingStack.isEmpty() || ! (mappingStack.getFirst() instanceof ClassMapping)) { 90 if (mappingStack.isEmpty() || ! (mappingStack.peek() instanceof ClassMapping)) {
90 throw new MappingParseException(lineNumber, "Unexpected METHOD entry here!"); 91 throw new MappingParseException(lineNumber, "Unexpected METHOD entry here!");
91 } 92 }
92 MethodMapping methodMapping = readMethod(parts); 93 MethodMapping methodMapping = readMethod(parts);
93 ((ClassMapping)mappingStack.getFirst()).addMethodMapping(methodMapping); 94 ((ClassMapping)mappingStack.peek()).addMethodMapping(methodMapping);
94 mappingStack.push(methodMapping); 95 mappingStack.push(methodMapping);
95 } else if (token.equalsIgnoreCase("ARG")) { 96 } else if (token.equalsIgnoreCase("ARG")) {
96 if (mappingStack.isEmpty() || ! (mappingStack.getFirst() instanceof MethodMapping)) { 97 if (mappingStack.isEmpty() || ! (mappingStack.peek() instanceof MethodMapping)) {
97 throw new MappingParseException(lineNumber, "Unexpected ARG entry here!"); 98 throw new MappingParseException(lineNumber, "Unexpected ARG entry here!");
98 } 99 }
99 ((MethodMapping)mappingStack.getFirst()).addArgumentMapping(readArgument(parts)); 100 ((MethodMapping)mappingStack.peek()).addArgumentMapping(readArgument(parts));
100 } 101 }
101 } catch (ArrayIndexOutOfBoundsException | NumberFormatException ex) { 102 } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) {
102 throw new MappingParseException(lineNumber, "Malformed line!"); 103 throw new MappingParseException(lineNumber, "Malformed line:\n" + line);
103 } 104 }
104 } 105 }
105 106
@@ -118,7 +119,8 @@ public class MappingsReader {
118 } 119 }
119 } 120 }
120 121
121 private FieldMapping readField(String[] parts) { 122 /* TEMP */
123 protected FieldMapping readField(String[] parts) {
122 return new FieldMapping(parts[1], new Type(parts[3]), parts[2]); 124 return new FieldMapping(parts[1], new Type(parts[3]), parts[2]);
123 } 125 }
124 126