summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/mapping/MappingsReader.java
blob: 7aedc5b16f441fa60c25ffc08e740e5897606081 (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
/*******************************************************************************
 * 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.mapping;

import com.google.common.io.Files;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import cuchaz.enigma.json.JsonClass;

public class MappingsReader {

    public Mappings read(File in) throws IOException, MappingParseException {
        Mappings mappings = new Mappings();
        readDirectory(mappings, in);
        return mappings;
    }

    public void readDirectory(Mappings mappings, File in) throws IOException, MappingParseException {

        File[] fList = in.listFiles();
        if (fList != null) {
            for (File file : fList) {
                if (file.isFile() && Files.getFileExtension(file.getName()).equalsIgnoreCase("json")) {
                    readFile(mappings, new BufferedReader(new FileReader(file)));
                } else if (file.isDirectory()) {
                    readDirectory(mappings, file.getAbsoluteFile());
                }
            }
        }
    }

    public void readFile(Mappings mappings, BufferedReader in) throws IOException, MappingParseException {

        StringBuilder buf = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) {
            buf.append(line);
        }

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonClass jsonClass = gson.fromJson(buf.toString(), JsonClass.class);
        load(null, jsonClass, mappings);
        in.close();
    }

    public void load(ClassMapping parent, JsonClass jsonClass, Mappings mappings) {
        ClassMapping classMapping = readClass(jsonClass.getObf(), jsonClass.getName());
        if (parent != null) {
            parent.addInnerClassMapping(classMapping);
        } else {
            mappings.addClassMapping(classMapping);
        }
        jsonClass.getField().forEach(jsonField -> classMapping.addFieldMapping(readField(jsonField.getObf(), jsonField.getName(), jsonField.getType())));

        jsonClass.getConstructors().forEach(jsonConstructor -> {
            MethodMapping methodMapping = readMethod(jsonConstructor.isStatics() ? "<clinit>" : "<init>", null, jsonConstructor.getSignature());
            jsonConstructor.getArgs().forEach(jsonArgument -> methodMapping.addArgumentMapping(readArgument(jsonArgument.getIndex(), jsonArgument.getName())));
            classMapping.addMethodMapping(methodMapping);
        });

        jsonClass.getMethod().forEach(jsonMethod -> {
            MethodMapping methodMapping = readMethod(jsonMethod.getObf(), jsonMethod.getName(), jsonMethod.getSignature());
            jsonMethod.getArgs().forEach(jsonArgument -> methodMapping.addArgumentMapping(readArgument(jsonArgument.getIndex(), jsonArgument.getName())));
            classMapping.addMethodMapping(methodMapping);
        });

        jsonClass.getInnerClass().forEach(jsonInnerClasses -> load(classMapping, jsonInnerClasses, mappings));
    }

    private ArgumentMapping readArgument(int index, String name) {
        return new ArgumentMapping(index, name);
    }

    private ClassMapping readClass(String obf, String deobf) {
        return new ClassMapping("none/" + obf, deobf);
    }

    /* TEMP */
    protected FieldMapping readField(String obf, String deobf, String sig) {
        return new FieldMapping(obf, new Type(sig), deobf);
    }

    private MethodMapping readMethod(String obf, String deobf, String sig) {
        return new MethodMapping(obf, new Signature(sig), deobf);
    }
}