Configuration.java 5.52 KB
Newer Older
1
package emu.grasscutter.config;
KingRainbow44's avatar
KingRainbow44 committed
2
3

import java.util.Locale;
4
import java.util.stream.Stream;
5

6
7
8
9
10
11
12
import emu.grasscutter.Grasscutter;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
13
import java.nio.file.Paths;
KingRainbow44's avatar
KingRainbow44 committed
14

15
16
import static emu.grasscutter.Grasscutter.config;

17

KingRainbow44's avatar
KingRainbow44 committed
18
19
/**
 * A data container for the server's configuration.
github-actions's avatar
github-actions committed
20
 *
21
22
 * Use `import static emu.grasscutter.Configuration.*;`
 * to import all configuration constants.
KingRainbow44's avatar
KingRainbow44 committed
23
 */
24
public final class Configuration extends ConfigContainer {
github-actions's avatar
github-actions committed
25

26
27
28
    /*
     * Constants
     */
github-actions's avatar
github-actions committed
29

30
    // 'c' is short for 'config' and makes code look 'cleaner'.
31
    public static final ConfigContainer c = config;
github-actions's avatar
github-actions committed
32

33
34
    public static final Locale LANGUAGE = config.language.language;
    public static final Locale FALLBACK_LANGUAGE = config.language.fallback;
xtaodada's avatar
xtaodada committed
35
    public static final String DOCUMENT_LANGUAGE = config.language.document;
36
37
38
39
40
    private static final String DATA_FOLDER = config.folderStructure.data;
    private static final String RESOURCES_FOLDER = config.folderStructure.resources;
    private static final String PLUGINS_FOLDER = config.folderStructure.plugins;
    private static final String SCRIPTS_FOLDER = config.folderStructure.scripts;
    private static final String PACKETS_FOLDER = config.folderStructure.packets;
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
    private static final FileSystem RESOURCES_FILE_SYSTEM;  // Not sure about lifetime rules on this one, might be safe to remove
    private static final Path RESOURCES_PATH;
    static {
        FileSystem fs = null;
        Path path = Path.of(RESOURCES_FOLDER);
        if (RESOURCES_FOLDER.endsWith(".zip")) {  // Would be nice to support .tar.gz too at some point, but it doesn't come for free in Java
            try {
                fs = FileSystems.newFileSystem(path);
            } catch (IOException e) {
                Grasscutter.getLogger().error("Failed to load resources zip \"" + RESOURCES_FOLDER + "\"");
            }
        }

        if (fs != null) {
            var root = fs.getPath("");
            try (Stream<Path> pathStream = java.nio.file.Files.find(root, 3, (p, a) -> {
                        var filename = p.getFileName();
                        if (filename == null) return false;
                        return filename.toString().equals("ExcelBinOutput");
            })) {
                var excelBinOutput = pathStream.findFirst();
                if (excelBinOutput.isPresent()) {
                    path = excelBinOutput.get().getParent();
                    if (path == null)
                        path = root;
                    Grasscutter.getLogger().debug("Resources will be loaded from \"" + RESOURCES_FOLDER + "/" + path.toString() + "\"");
                } else {
                    Grasscutter.getLogger().error("Failed to find ExcelBinOutput in resources zip \"" + RESOURCES_FOLDER + "\"");
                }
            } catch (IOException e) {
                Grasscutter.getLogger().error("Failed to scan resources zip \"" + RESOURCES_FOLDER + "\"");
            }
        }
        RESOURCES_FILE_SYSTEM = fs;
        RESOURCES_PATH = path;
    };
github-actions's avatar
github-actions committed
77

78
79
80
    public static final Server SERVER = config.server;
    public static final Database DATABASE = config.databaseInfo;
    public static final Account ACCOUNT = config.account;
github-actions's avatar
github-actions committed
81

82
    public static final HTTP HTTP_INFO = config.server.http;
83
    public static final Game GAME_INFO = config.server.game;
84
    public static final Dispatch DISPATCH_INFO = config.server.dispatch;
github-actions's avatar
github-actions committed
85

86
87
    public static final Encryption HTTP_ENCRYPTION = config.server.http.encryption;
    public static final Policies HTTP_POLICIES = config.server.http.policies;
KingRainbow44's avatar
KingRainbow44 committed
88
    public static final Files HTTP_STATIC_FILES = config.server.http.files;
github-actions's avatar
github-actions committed
89

90
91
    public static final GameOptions GAME_OPTIONS = config.server.game.gameOptions;
    public static final GameOptions.InventoryLimits INVENTORY_LIMITS = config.server.game.gameOptions.inventoryLimits;
github-actions's avatar
github-actions committed
92

93
94
95
    /*
     * Utilities
     */
96
97
98
99
    public static String DATA() {
        return DATA_FOLDER;
    }

100
    public static String DATA(String path) {
101
102
103
104
105
        return Path.of(DATA_FOLDER, path).toString();
    }

    public static Path getResourcePath(String path) {
        return RESOURCES_PATH.resolve(path);
106
    }
github-actions's avatar
github-actions committed
107

108
    public static String RESOURCE(String path) {
109
        return getResourcePath(path).toString();
110
    }
github-actions's avatar
github-actions committed
111

112
113
114
115
116
    public static String PLUGIN() {
        return PLUGINS_FOLDER;
    }

    public static String PLUGIN(String path) {
117
        return Path.of(PLUGINS_FOLDER, path).toString();
118
119
    }

120
    public static String SCRIPT(String path) {
121
        return Path.of(SCRIPTS_FOLDER, path).toString();
122
123
124
    }

    public static String PACKET(String path) {
125
        return Path.of(PACKETS_FOLDER, path).toString();
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
    }

    /**
     * Fallback method.
     * @param left Attempt to use.
     * @param right Use if left is undefined.
     * @return Left or right.
     */
    public static <T> T lr(T left, T right) {
        return left == null ? right : left;
    }

    /**
     * {@link Configuration#lr(Object, Object)} for {@link String}s.
     * @param left Attempt to use.
     * @param right Use if left is empty.
     * @return Left or right.
     */
    public static String lr(String left, String right) {
        return left.isEmpty() ? right : left;
    }

    /**
     * {@link Configuration#lr(Object, Object)} for {@link Integer}s.
     * @param left Attempt to use.
     * @param right Use if left is 0.
     * @return Left or right.
     */
    public static int lr(int left, int right) {
        return left == 0 ? right : left;
    }
157
}