aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/zwirn/ConstructorEnrichmentTask.java
blob: 687aeb902491fa249ecf477388fb02f2ef1a1140 (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
package moe.nea.zwirn;

import net.fabricmc.stitch.commands.tinyv2.TinyClass;
import net.fabricmc.stitch.commands.tinyv2.TinyFile;
import net.fabricmc.stitch.commands.tinyv2.TinyMethod;
import net.fabricmc.stitch.commands.tinyv2.TinyMethodParameter;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;

public class ConstructorEnrichmentTask {
    private final TinyFile searge;
    private final List<McpConstructor> mcpConstructors;
    private final Map<String, List<McpConstructor>> indexedConstructors;
    private final SimpleRemapper remapper;

    public ConstructorEnrichmentTask(@NotNull TinyFile searge, Path joinedExc) {
        this.searge = searge;
        this.mcpConstructors = readExc(joinedExc);
        this.indexedConstructors = this.mcpConstructors.stream().collect(Collectors.groupingBy(McpConstructor::owningClass, Collectors.toList()));
        this.remapper = new SimpleRemapper(searge, "right", "left");
    }

    public TinyFile enrich() {
        return new TinyFile(
                searge.getHeader(),
                searge.getClassEntries()
                        .stream().map(this::enrichClass).collect(Collectors.toList())
        );
    }

    private TinyClass enrichClass(TinyClass tinyClass) {
        List<TinyMethod> methods = new ArrayList<>(tinyClass.getMethods());
        List<McpConstructor> constructors = indexedConstructors.getOrDefault(tinyClass.getClassNames().get(1).replace('/', '.'), Collections.emptyList());
        for (McpConstructor constructor : constructors) {
            methods.add(new TinyMethod(
                    remapper.remapMethodDescriptor(constructor.methodDescriptorInSrg()),
                    Arrays.asList("<init>", "<init>"),
                    constructor.getTinyParameters(),
                    Arrays.asList(),
                    Arrays.asList()
            ));
        }
        return new TinyClass(
                tinyClass.getClassNames(),
                methods,
                tinyClass.getFields(),
                tinyClass.getComments()
        );
    }

    record McpConstructor(
            String owningClass,
            String methodDescriptorInSrg,
            List<String> parameterNames
    ) {
        public Collection<TinyMethodParameter> getTinyParameters() {
            List<TinyMethodParameter> params = new ArrayList<>();
            int i = 0;
            for (String parameterName : parameterNames) {
                int lvIndex = ++i;
                params.add(new TinyMethodParameter(lvIndex, Arrays.asList("p" + lvIndex, parameterName), Arrays.asList()));
            }
            return params;
        }
    }

    private static List<McpConstructor> readExc(Path joinedExc) {
        var prop = new Properties();
        try (var reader = Files.newBufferedReader(joinedExc)) {
            prop.load(reader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<McpConstructor> cons = new ArrayList<>();
        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
            var target = (String) entry.getKey();
            var signatureInfo = (String) entry.getValue();
            if (!signatureInfo.contains("|")) continue;
            var targetParts = target.split("\\.");
            assert targetParts.length == 2;
            var className = targetParts[0].replace('/', '.');
            var constructorDescriptor = "(" + targetParts[1].split("\\(")[1];
            if (!targetParts[1].startsWith("<init>"))
                continue;
            int pipeIndex = signatureInfo.indexOf('|');
            var exceptions = signatureInfo.substring(0, pipeIndex);
            var parameterNameString = signatureInfo.substring(pipeIndex + 1);
            var parameterNames = parameterNameString.isBlank()
                    ? Arrays.<String>asList()
                    : Arrays.asList(parameterNameString.split(","));
            cons.add(new McpConstructor(className, constructorDescriptor, parameterNames));
        }
        return cons;
    }
}