aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/zwirn/EnrichSeargeWithMCP.java
blob: d91e424aa3f7b17a6565b7573bde5530964f7da7 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package moe.nea.zwirn;

import de.siegmar.fastcsv.reader.CsvReader;
import net.fabricmc.stitch.commands.tinyv2.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class EnrichSeargeWithMCP {
    private final TinyFile searge;
    private final List<MCPField> fields;
    private final List<MCPMethod> methods;
    private final List<MCPParam> params;
    private final Map<String, MCPField> fieldMap;
    private final Map<Integer, List<MCPParam>> paramMap;
    private final Map<String, MCPMethod> methodMap;

    // TODO: parse joined.exc for constructor indexes parameters
    public EnrichSeargeWithMCP(@NotNull TinyFile searge, Path fields, Path methods, Path params) throws IOException {
        this.searge = searge;
        this.fields = readFields(fields);
        this.methods = readMethods(methods);
        this.params = readParams(params);
        this.fieldMap = this.fields.stream().collect(Collectors.toMap(MCPField::searge, Function.identity()));
        this.methodMap = this.methods.stream().collect(Collectors.toMap(MCPMethod::searge, Function.identity()));
        this.paramMap = this.params.stream().filter(it -> it.methodId() != null).collect(Collectors.groupingBy(MCPParam::methodId, Collectors.toList()));
    }

    record MCPParam(
            String searge,
            String name
    ) {
        public Integer methodId() {
            var matcher = SEARGE_PARAM_ID_PATTERN.matcher(searge);
            if (!matcher.matches())
                return -1;
            return Integer.parseInt(matcher.group(1));
        }

        public Integer lvIndexHeuristic() {
            var matcher = SEARGE_PARAM_ID_PATTERN.matcher(searge);
            if (!matcher.matches())
                return -1;
            return Integer.parseInt(matcher.group(2));
        }

        static final Pattern SEARGE_PARAM_ID_PATTERN = Pattern.compile("^p_([0-9]+)_([0-9]+)_$");

    }

    record MCPField(String searge, String name, String desc) {
    }

    record MCPMethod(String searge, String name, String desc) {
        public int methodId() {
            var matcher = SEARGE_METHOD_ID_PATTERN.matcher(searge);
            if (!matcher.matches())
                throw new IllegalStateException("Searge name does not contain method id");
            return Integer.parseInt(matcher.group(1));
        }

        static final Pattern SEARGE_METHOD_ID_PATTERN = Pattern.compile("^func_([0-9]+)_.+$");
    }


    private static List<MCPParam> readParams(Path params) throws IOException {
        try (var csvReader = CsvReader.builder().ofNamedCsvRecord(params)
                .stream()) {
            // Header: param,name,side
            return csvReader.map(
                    it -> new MCPParam(it.getField("param"), it.getField("name"))
            ).collect(Collectors.toList());
        }
    }

    private static List<MCPField> readFields(Path fields) throws IOException {
        try (var csvReader = CsvReader.builder().ofNamedCsvRecord(fields)
                .stream()) {
            // Header: searge,name,side,desc
            return csvReader.map(
                    it -> new MCPField(it.getField("searge"), it.getField("name"), it.getField("desc").replace("\\n", "\n"))
            ).collect(Collectors.toList());
        }
    }

    private static List<MCPMethod> readMethods(Path methods) throws IOException {
        try (var csvReader = CsvReader.builder().ofNamedCsvRecord(methods)
                .stream()) {
            // Header: searge,name,side,desc
            return csvReader.map(
                    it -> new MCPMethod(it.getField("searge"), it.getField("name"), it.getField("desc").replace("\\n", "\n"))
            ).collect(Collectors.toList());
        }
    }

    public TinyFile mergeTinyFile() {
        return new TinyFile(
                new TinyHeader(
                        Arrays.asList("notch", "searge", "mcp"),
                        2, 0, new HashMap<>()
                ),
                searge.getClassEntries()
                        .stream().map(this::mergeClass)
                        .collect(Collectors.toList())
        );
    }

    private TinyClass mergeClass(TinyClass tinyClass) {
        return new TinyClass(
                Arrays.asList(
                        tinyClass.getClassNames().get(0), tinyClass.getClassNames().get(1),
                        tinyClass.getClassNames().get(1) // MCP does not handle class names. those are done in searge
                ),
                tinyClass.getMethods().stream().map(this::mergeMethod).collect(Collectors.toList()),
                tinyClass.getFields().stream().map(this::mergeField).collect(Collectors.toList()),
                Arrays.asList() // Searge doesn't have comments
        );
    }

    private TinyField mergeField(TinyField tinyField) {
        var srg = tinyField.getFieldNames().get(1);
        var mcpField = fieldMap.get(srg);
        return new TinyField(
                tinyField.getFieldDescriptorInFirstNamespace(),
                Arrays.asList(tinyField.getFieldNames().get(0), srg, mcpField == null ? srg : mcpField.name()),
                mcpField == null ? Arrays.asList() : Arrays.asList(mcpField.desc)// TODO: handle empty comment
        );
    }

    private TinyMethod mergeMethod(TinyMethod tinyMethod) {
        var srg = tinyMethod.getMethodNames().get(1);
        var mcpMethod = methodMap.get(srg);
        List<TinyMethodParameter> params = new ArrayList<>();
        if (mcpMethod != null) {
            var mcpParams = paramMap.get(mcpMethod.methodId());
            if (mcpParams != null) for (var param : mcpParams) {
                params.add(new TinyMethodParameter(
                        param.lvIndexHeuristic(),
                        Arrays.asList("p" + param.lvIndexHeuristic(), param.searge(), param.name()),
                        Arrays.asList()
                ));
            }
        }
        return new TinyMethod(
                tinyMethod.getMethodDescriptorInFirstNamespace(),
                Arrays.asList(tinyMethod.getMethodNames().get(0), srg, mcpMethod == null ? srg : mcpMethod.name()),
                params,
                Arrays.asList(),
                mcpMethod == null ? Arrays.asList() : Arrays.asList(mcpMethod.desc) // TODO: handle empty comment
        );
    }
}