summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/mapping/MappingsWriter.java
blob: bfd606388408b2ab5facd865ec9ed8b62e4e4bf3 (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
/*******************************************************************************
 * 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.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cuchaz.enigma.json.*;

public class MappingsWriter {

    public void write(File file, Mappings mappings) throws IOException {
        if (!file.isDirectory()) {
            return;
        }

        String[] entries = file.list();
        for (String s : entries) {
            File currentFile = new File(file.getPath(), s);
            currentFile.delete();
        }

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        for (ClassMapping classMapping : sorted(mappings.classes())) {
            if (classMapping.getDeobfName() != null && !classMapping.getDeobfName().equalsIgnoreCase("") && !classMapping.getDeobfName().equalsIgnoreCase("null")) {
                JsonClass jsonClass = new JsonClass(classMapping.getObfSimpleName(), classMapping.getDeobfName());
                write(jsonClass, classMapping);

                File f = new File(file, jsonClass.getName() + ".json");
                f.getParentFile().mkdirs();
                f.createNewFile();
                FileWriter writer = new FileWriter(f);
                writer.write(gson.toJson(jsonClass));
                writer.close();
            }
        }
    }

    private void write(JsonClass jsonClass, ClassMapping classMapping) throws IOException {
        for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) {
            JsonClass innerClass = new JsonClass(classMapping.getObfSimpleName() + "$" + innerClassMapping.getObfSimpleName().replace("nome/", ""), innerClassMapping.getDeobfName());
            write(innerClass, innerClassMapping);
            jsonClass.addInnerClass(innerClass);
        }

        for (FieldMapping fieldMapping : sorted(classMapping.fields())) {
            jsonClass.addField(new JsonField(fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfType().toString()));
        }

        for (MethodMapping methodMapping : sorted(classMapping.methods())) {
            List<JsonArgument> args = new ArrayList<>();
            for (ArgumentMapping argumentMapping : sorted(methodMapping.arguments())) {
                args.add(new JsonArgument(argumentMapping.getIndex(), argumentMapping.getName()));
            }
            if (methodMapping.getObfName().contains("<init>") || methodMapping.getObfName().contains("<clinit>")) {
                jsonClass.addConstructor(new JsonConstructor(methodMapping.getObfSignature().toString(), args, methodMapping.getObfName().contains("<clinit>")));
            } else {
                jsonClass.addMethod(new JsonMethod(methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfSignature().toString(), args));
            }
        }
    }

    private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) {
        List<T> out = new ArrayList<>();
        for (T t : classes) {
            out.add(t);
        }
        Collections.sort(out);
        return out;
    }
}