summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/MainFormatConverter.java
blob: 4dd19ea80906873840e2014235d4957595d27c67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Contributors:
 * Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma;

import com.google.common.collect.Maps;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.jar.JarFile;

import cuchaz.enigma.analysis.JarClassIterator;
import cuchaz.enigma.mapping.*;
import javassist.CtClass;
import javassist.CtField;

public class MainFormatConverter {

    public static void main(String[] args)
            throws Exception {

        System.out.println("Getting field types from jar...");

        JarFile jar = new JarFile(System.getProperty("user.home") + "/.minecraft/versions/1.8/1.8.jar");
        Map<String, Type> fieldTypes = Maps.newHashMap();
        for (CtClass c : JarClassIterator.classes(jar)) {
            for (CtField field : c.getDeclaredFields()) {
                FieldEntry fieldEntry = EntryFactory.getFieldEntry(field);
                fieldTypes.put(getFieldKey(fieldEntry), moveClasssesOutOfDefaultPackage(fieldEntry.getType()));
            }
        }

        System.out.println("Reading mappings...");

        File fileMappings = new File("../Enigma Mappings/1.8.mappings");
        MappingsReader mappingsReader = new MappingsReader() {

            @Override
            protected FieldMapping readField(String obf, String deobf, String type) {
                // assume the void type for now
                return new FieldMapping(obf, new Type("V"), deobf);
            }
        };
        Mappings mappings = mappingsReader.read(fileMappings);

        System.out.println("Updating field types...");

        for (ClassMapping classMapping : mappings.classes()) {
            updateFieldsInClass(fieldTypes, classMapping);
        }

        System.out.println("Saving mappings...");

        new MappingsWriter().write(fileMappings, mappings);

        System.out.println("Done!");
    }

    private static Type moveClasssesOutOfDefaultPackage(Type type) {
        return new Type(type, className -> {
            ClassEntry entry = new ClassEntry(className);
            if (entry.isInDefaultPackage()) {
                return Constants.NONE_PACKAGE + "/" + className;
            }
            return null;
        });
    }

    private static void updateFieldsInClass(Map<String, Type> fieldTypes, ClassMapping classMapping)
            throws Exception {

        // update the fields
        for (FieldMapping fieldMapping : classMapping.fields()) {
            setFieldType(fieldTypes, classMapping, fieldMapping);
        }

        // recurse
        for (ClassMapping innerClassMapping : classMapping.innerClasses()) {
            updateFieldsInClass(fieldTypes, innerClassMapping);
        }
    }

    private static void setFieldType(Map<String, Type> fieldTypes, ClassMapping classMapping, FieldMapping fieldMapping)
            throws Exception {

        // get the new type
        Type newType = fieldTypes.get(getFieldKey(classMapping, fieldMapping));
        if (newType == null) {
            throw new Error("Can't find type for field: " + getFieldKey(classMapping, fieldMapping));
        }

        // hack in the new field type
        Field field = fieldMapping.getClass().getDeclaredField("m_obfType");
        field.setAccessible(true);
        field.set(fieldMapping, newType);
    }

    private static Object getFieldKey(ClassMapping classMapping, FieldMapping fieldMapping) {
        return classMapping.getObfSimpleName() + "." + fieldMapping.getObfName();
    }

    private static String getFieldKey(FieldEntry obfFieldEntry) {
        return obfFieldEntry.getClassEntry().getSimpleName() + "." + obfFieldEntry.getName();
    }
}