ConfigContainer.java 9.14 KB
Newer Older
1
package emu.grasscutter.config;
2
3
4

import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;
5
6
import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.Grasscutter.ServerRunMode;
7

8
import java.nio.charset.StandardCharsets;
9
import java.util.Set;
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.FileReader;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Locale;

import static emu.grasscutter.Grasscutter.config;

/**
 * *when your JVM fails*
 */
public class ConfigContainer {
    private static int version() {
KingRainbow44's avatar
KingRainbow44 committed
22
        return 3;
23
24
25
    }

    /**
26
     * Attempts to update the server's existing configuration to the latest
27
28
29
30
     */
    public static void updateConfig() {
        try { // Check if the server is using a legacy config.
            JsonObject configObject = Grasscutter.getGsonFactory()
31
                    .fromJson(new FileReader(Grasscutter.configFile, StandardCharsets.UTF_8), JsonObject.class);
github-actions's avatar
github-actions committed
32
            if (!configObject.has("version")) {
33
34
35
36
37
38
39
40
                Grasscutter.getLogger().info("Updating legacy ..");
                Grasscutter.saveConfig(null);
            }
        } catch (Exception ignored) { }

        var existing = config.version;
        var latest = version();

github-actions's avatar
github-actions committed
41
        if (existing == latest)
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
            return;

        // Create a new configuration instance.
        ConfigContainer updated = new ConfigContainer();
        // Update all configuration fields.
        Field[] fields = ConfigContainer.class.getDeclaredFields();
        Arrays.stream(fields).forEach(field -> {
            try {
                field.set(updated, field.get(config));
            } catch (Exception exception) {
                Grasscutter.getLogger().error("Failed to update a configuration field.", exception);
            }
        }); updated.version = version();

        try { // Save configuration & reload.
            Grasscutter.saveConfig(updated);
            Grasscutter.loadConfig();
        } catch (Exception exception) {
            Grasscutter.getLogger().warn("Failed to inject the updated ", exception);
        }
    }
63

64
65
66
67
68
69
70
71
72
73
74
75
    public Structure folderStructure = new Structure();
    public Database databaseInfo = new Database();
    public Language language = new Language();
    public Account account = new Account();
    public Server server = new Server();

    // DO NOT. TOUCH. THE VERSION NUMBER.
    public int version = version();

    /* Option containers. */

    public static class Database {
76
77
        public DataStore server = new DataStore();
        public DataStore game = new DataStore();
78

79
80
81
82
        public static class DataStore {
            public String connectionUri = "mongodb://localhost:27017";
            public String collection = "grasscutter";
        }
83
84
85
86
87
88
    }

    public static class Structure {
        public String resources = "./resources/";
        public String data = "./data/";
        public String packets = "./packets/";
89
        public String scripts = "./resources/Scripts/";
90
91
92
93
94
95
96
        public String plugins = "./plugins/";

        // UNUSED (potentially added later?)
        // public String dumps = "./dumps/";
    }

    public static class Server {
97
98
        public Set<Integer> debugWhitelist = Set.of();
        public Set<Integer> debugBlacklist = Set.of();
99
        public ServerRunMode runMode = ServerRunMode.HYBRID;
100

101
        public HTTP http = new HTTP();
102
        public Game game = new Game();
103

104
        public Dispatch dispatch = new Dispatch();
105
106
107
108
109
    }

    public static class Language {
        public Locale language = Locale.getDefault();
        public Locale fallback = Locale.US;
xtaodada's avatar
xtaodada committed
110
        public String document = "EN";
111
112
113
114
    }

    public static class Account {
        public boolean autoCreate = false;
115
        public boolean EXPERIMENTAL_RealPassword = false;
116
        public String[] defaultPermissions = {};
117
        public int maxPlayer = -1;
118
119
120
    }

    /* Server options. */
121

122
    public static class HTTP {
123
        public String bindAddress = "0.0.0.0";
Melledy's avatar
Melledy committed
124
        public int bindPort = 443;
125

126
127
128
        /* This is the address used in URLs. */
        public String accessAddress = "127.0.0.1";
        /* This is the port used in URLs. */
129
        public int accessPort = 0;
130

131
132
        public Encryption encryption = new Encryption();
        public Policies policies = new Policies();
KingRainbow44's avatar
KingRainbow44 committed
133
        public Files files = new Files();
134
135
136
137
    }

    public static class Game {
        public String bindAddress = "0.0.0.0";
138
        public int bindPort = 22102;
github-actions's avatar
github-actions committed
139

140
141
142
        /* This is the address used in the default region. */
        public String accessAddress = "127.0.0.1";
        /* This is the port used in the default region. */
143
        public int accessPort = 0;
github-actions's avatar
github-actions committed
144

145
146
        /* Entities within a certain range will be loaded for the player */
        public int loadEntitiesForPlayerRange = 100;
Akka's avatar
Akka committed
147
        public boolean enableScriptInBigWorld = false;
148
        public boolean enableConsole = true;
149

Melledy's avatar
Melledy committed
150
151
        /* Kcp internal work interval (milliseconds) */
        public int kcpInterval = 20;
152
153
        /* Controls whether packets should be logged in console or not */
        public ServerDebugMode logPackets = ServerDebugMode.NONE;
github-actions's avatar
github-actions committed
154

155
156
157
158
159
160
161
        public GameOptions gameOptions = new GameOptions();
        public JoinOptions joinOptions = new JoinOptions();
        public ConsoleAccount serverAccount = new ConsoleAccount();
    }

    /* Data containers. */

162
163
164
165
    public static class Dispatch {
        public Region[] regions = {};

        public String defaultName = "Grasscutter";
github-actions's avatar
github-actions committed
166

167
        public ServerDebugMode logRequests = ServerDebugMode.NONE;
168
169
    }

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    public static class Encryption {
        public boolean useEncryption = true;
        /* Should 'https' be appended to URLs? */
        public boolean useInRouting = true;
        public String keystore = "./keystore.p12";
        public String keystorePassword = "123456";
    }

    public static class Policies {
        public Policies.CORS cors = new Policies.CORS();

        public static class CORS {
            public boolean enabled = false;
            public String[] allowedOrigins = new String[]{"*"};
        }
    }

    public static class GameOptions {
188
189
        public InventoryLimits inventoryLimits = new InventoryLimits();
        public AvatarLimits avatarLimits = new AvatarLimits();
190
        public int sceneEntityLimit = 1000; // Unenforced. TODO: Implement.
191
192
193
194

        public boolean watchGachaConfig = false;
        public boolean enableShopItems = true;
        public boolean staminaUsage = true;
195
        public boolean energyUsage = false;
ImmuState's avatar
ImmuState committed
196
        public ResinOptions resinOptions = new ResinOptions();
197
        public Rates rates = new Rates();
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

        public static class InventoryLimits {
            public int weapons = 2000;
            public int relics = 2000;
            public int materials = 2000;
            public int furniture = 2000;
            public int all = 30000;
        }

        public static class AvatarLimits {
            public int singlePlayerTeam = 4;
            public int multiplayerTeam = 4;
        }

        public static class Rates {
            public float adventureExp = 1.0f;
            public float mora = 1.0f;
            public float leyLines = 1.0f;
        }
ImmuState's avatar
ImmuState committed
217
218
219
220
221
222

        public static class ResinOptions {
            public boolean resinUsage = false;
            public int cap = 160;
            public int rechargeTime = 480;
        }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    }

    public static class JoinOptions {
        public int[] welcomeEmotes = {2007, 1002, 4010};
        public String welcomeMessage = "Welcome to a Grasscutter server.";
        public JoinOptions.Mail welcomeMail = new JoinOptions.Mail();

        public static class Mail {
            public String title = "Welcome to Grasscutter!";
            public String content = """
                    Hi there!\r
                    First of all, welcome to Grasscutter. If you have any issues, please let us know so that Lawnmower can help you! \r
                    \r
                    Check out our:\r
                    <type="browser" text="Discord" href="https://discord.gg/T5vZU6UyeG"/>
                    """;
            public String sender = "Lawnmower";
            public emu.grasscutter.game.mail.Mail.MailItem[] items = {
                    new emu.grasscutter.game.mail.Mail.MailItem(13509, 1, 1),
                    new emu.grasscutter.game.mail.Mail.MailItem(201, 99999, 1)
            };
        }
    }

    public static class ConsoleAccount {
        public int avatarId = 10000007;
        public int nameCardId = 210001;
        public int adventureRank = 1;
        public int worldLevel = 0;

        public String nickName = "Server";
        public String signature = "Welcome to Grasscutter!";
    }
256

KingRainbow44's avatar
KingRainbow44 committed
257
258
259
260
    public static class Files {
        public String indexFile = "./index.html";
        public String errorFile = "./404.html";
    }
261
262
263
264

    /* Objects. */

    public static class Region {
265
        public Region() { }
266

267
268
269
270
271
272
273
274
275
        public Region(
                String name, String title,
                String address, int port
        ) {
            this.Name = name;
            this.Title = title;
            this.Ip = address;
            this.Port  = port;
        }
276

277
278
279
280
281
        public String Name = "os_usa";
        public String Title = "Grasscutter";
        public String Ip = "127.0.0.1";
        public int Port = 22102;
    }
github-actions's avatar
github-actions committed
282
}