aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertiesGroupAdapter.java
blob: 037507935eee2744f5d1d7c14d01797c72f0dc2e (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
package shcm.shsupercm.fabric.citresewn.pack.format;

import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class PropertiesGroupAdapter extends PropertyGroup {
    public static final String EXTENSION = ".properties";

    protected PropertiesGroupAdapter(String packName, Identifier identifier) {
        super(packName, identifier);
    }

    @Override
    public String getExtension() {
        return EXTENSION;
    }

    @Override
    public PropertyGroup load(String packName, Identifier identifier, InputStream is) throws IOException, InvalidIdentifierException {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line;
            int linePos = 0, multilineSkip = 0;
            while ((line = reader.readLine()) != null) {
                linePos++;
                line = line.stripLeading();
                if (line.isEmpty() || line.startsWith("#") || line.startsWith("!"))
                    continue;

                while (line.endsWith("\\")) {
                    String nextLine = reader.readLine();
                    linePos++;
                    multilineSkip++;
                    if (nextLine == null)
                        nextLine = "";
                    nextLine = nextLine.stripLeading();

                    if (nextLine.startsWith("#") || nextLine.startsWith("!"))
                        continue;

                    line = line.substring(0, line.length() - 1) + "\\n" + nextLine;
                }

                line = line.stripTrailing();

                StringBuilder builder = new StringBuilder();

                String key = null, keyMetadata = null;
                final boolean checkColonSeparator = !line.contains("=");

                for (int i = 0; i < line.length(); i++) {
                    char c = line.charAt(i);

                    if (c == '\\') { // escape
                        c = switch (c = line.charAt(++i)) {
                            case 'n' -> '\n';
                            case 'r' -> '\r';
                            case 'f' -> '\f';
                            case 't' -> '\t';
                            case 'u' -> {
                                try {
                                    i += 4;
                                    yield (char) Integer.parseInt(line.substring(i - 3, i + 1), 16);
                                } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
                                    throw new IOException("Malformatted escaped unicode character");
                                }
                            }

                            default -> c;
                        };

                    } else if (key == null && (c == '=' || (checkColonSeparator && c == ':'))) {
                        key = builder.toString().stripTrailing();
                        int metadataIndex = key.indexOf('.');
                        if (metadataIndex >= 0) {
                            keyMetadata = key.substring(metadataIndex + 1);
                            key = key.substring(0, metadataIndex);
                        }

                        builder = new StringBuilder();
                        for (i++; i < line.length() && Character.isWhitespace(line.charAt(i)); i++);
                        i--;
                        continue;
                    }

                    builder.append(c);
                }

                if (key == null)
                    throw new IOException("Missing separator in line " + linePos);

                int pos = linePos - multilineSkip;
                multilineSkip = 0;
                this.put(pos, packName, identifier, key, keyMetadata, PropertySeparator.EQUALS, builder.toString());
            }
        }
        return this;
    }
}