Grasscutter.java 6.74 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
package emu.grasscutter;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
6
import java.io.IOError;
7
import java.util.Calendar;
Melledy's avatar
Melledy committed
8

Jaida Wu's avatar
Jaida Wu committed
9
import emu.grasscutter.command.CommandMap;
KingRainbow44's avatar
KingRainbow44 committed
10
import emu.grasscutter.plugin.PluginManager;
KingRainbow44's avatar
KingRainbow44 committed
11
import emu.grasscutter.plugin.api.ServerHook;
12
import emu.grasscutter.scripts.ScriptLoader;
KingRainbow44's avatar
KingRainbow44 committed
13
import emu.grasscutter.utils.Utils;
14
15
16
17
18
19
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.UserInterruptException;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
Melledy's avatar
Melledy committed
20
import org.reflections.Reflections;
Melledy's avatar
Melledy committed
21
22
23
24
25
26
27
28
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import ch.qos.logback.classic.Logger;
import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.database.DatabaseManager;
29
import emu.grasscutter.utils.Language;
Melledy's avatar
Melledy committed
30
31
32
33
34
import emu.grasscutter.server.dispatch.DispatchServer;
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.Crypto;

35
36
import static emu.grasscutter.utils.Language.translate;

KingRainbow44's avatar
KingRainbow44 committed
37
38
public final class Grasscutter {
	private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class);
39
	private static LineReader consoleLineReader = null;
40
41

	private static Config config;
方块君's avatar
方块君 committed
42
	private static Language language;
43

KingRainbow44's avatar
KingRainbow44 committed
44
45
	private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
	private static final File configFile = new File("./config.json");
Secretboy's avatar
Secretboy committed
46

47
	private static int day; // Current day of week.
Secretboy's avatar
Secretboy committed
48

Melledy's avatar
Melledy committed
49
50
	private static DispatchServer dispatchServer;
	private static GameServer gameServer;
KingRainbow44's avatar
KingRainbow44 committed
51
	private static PluginManager pluginManager;
Secretboy's avatar
Secretboy committed
52

KingRainbow44's avatar
KingRainbow44 committed
53
	public static final Reflections reflector = new Reflections("emu.grasscutter");
Secretboy's avatar
Secretboy committed
54

KingRainbow44's avatar
KingRainbow44 committed
55
	static {
Melledy's avatar
Melledy committed
56
57
		// Declare logback configuration.
		System.setProperty("logback.configurationFile", "src/main/resources/logback.xml");
Secretboy's avatar
Secretboy committed
58

Melledy's avatar
Melledy committed
59
		// Load server configuration.
KingRainbow44's avatar
KingRainbow44 committed
60
		Grasscutter.loadConfig();
方块君's avatar
方块君 committed
61

62
		// Load translation files.
方块君's avatar
方块君 committed
63
		Grasscutter.loadLanguage();
Secretboy's avatar
Secretboy committed
64

KingRainbow44's avatar
KingRainbow44 committed
65
66
67
		// Check server structure.
		Utils.startupCheck();
	}
Secretboy's avatar
Secretboy committed
68

Melledy's avatar
Melledy committed
69
    public static void main(String[] args) throws Exception {
70
    	Crypto.loadKeys(); // Load keys from buffers.
Secretboy's avatar
Secretboy committed
71

72
		// Parse arguments.
Melledy's avatar
Melledy committed
73
74
		for (String arg : args) {
			switch (arg.toLowerCase()) {
75
				case "-handbook" -> Tools.createGmHandbook();
76
				case "-gachamap" -> Tools.createGachaMapping("./gacha-mapping.js");
Melledy's avatar
Melledy committed
77
78
			}
		}
Secretboy's avatar
Secretboy committed
79

KingRainbow44's avatar
KingRainbow44 committed
80
		// Initialize server.
81
		Grasscutter.getLogger().info(translate("messages.status.starting"));
Secretboy's avatar
Secretboy committed
82

KingRainbow44's avatar
KingRainbow44 committed
83
		// Load all resources.
84
		Grasscutter.updateDayOfWeek();
Melledy's avatar
Melledy committed
85
		ResourceLoader.loadAll();
86
		ScriptLoader.init();
Secretboy's avatar
Secretboy committed
87

Melledy's avatar
Melledy committed
88
89
		// Database
		DatabaseManager.initialize();
KingRainbow44's avatar
KingRainbow44 committed
90

KingRainbow44's avatar
KingRainbow44 committed
91
92
		// Create plugin manager instance.
		pluginManager = new PluginManager();
Secretboy's avatar
Secretboy committed
93

KingRainbow44's avatar
KingRainbow44 committed
94
95
		// Create server instances.
		dispatchServer = new DispatchServer();
96
		gameServer = new GameServer();
KingRainbow44's avatar
KingRainbow44 committed
97
98
		// Create a server hook instance with both servers.
		new ServerHook(gameServer, dispatchServer);
Secretboy's avatar
Secretboy committed
99

KingRainbow44's avatar
KingRainbow44 committed
100
		// Start servers.
101
		if (getConfig().RunMode == ServerRunMode.HYBRID) {
102
103
			dispatchServer.start();
			gameServer.start();
104
		} else if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
105
			dispatchServer.start();
106
		} else if (getConfig().RunMode == ServerRunMode.GAME_ONLY) {
107
108
			gameServer.start();
		} else {
109
110
111
			getLogger().error(translate("messages.status.run_mode_error", getConfig().RunMode));
			getLogger().error(translate("messages.status.run_mode_help"));
			getLogger().error(translate("messages.status.shutdown"));
112
113
			System.exit(1);
		}
Secretboy's avatar
Secretboy committed
114

KingRainbow44's avatar
KingRainbow44 committed
115
116
		// Enable all plugins.
		pluginManager.enablePlugins();
117

KingRainbow44's avatar
KingRainbow44 committed
118
119
		// Hook into shutdown event.
		Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
120
121
122

		// Open console.
		startConsole();
Melledy's avatar
Melledy committed
123
    }
KingRainbow44's avatar
KingRainbow44 committed
124
125
126
127
128
129
130
131

	/**
	 * Server shutdown event.
	 */
	private static void onShutdown() {
		// Disable all plugins.
		pluginManager.disablePlugins();
	}
132

Melledy's avatar
Melledy committed
133
134
135
	public static void loadConfig() {
		try (FileReader file = new FileReader(configFile)) {
			config = gson.fromJson(file, Config.class);
136
			saveConfig();
Melledy's avatar
Melledy committed
137
		} catch (Exception e) {
138
139
			Grasscutter.config = new Config(); 
			saveConfig();
Melledy's avatar
Melledy committed
140
141
		}
	}
方块君's avatar
方块君 committed
142
143

	public static void loadLanguage() {
144
145
		var locale = config.LocaleLanguage;
		language = Language.getLanguage(locale.toLanguageTag());
方块君's avatar
方块君 committed
146
	}
Secretboy's avatar
Secretboy committed
147

Melledy's avatar
Melledy committed
148
149
150
151
	public static void saveConfig() {
		try (FileWriter file = new FileWriter(configFile)) {
			file.write(gson.toJson(config));
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
152
			Grasscutter.getLogger().error("Unable to save config file.");
Melledy's avatar
Melledy committed
153
154
		}
	}
Secretboy's avatar
Secretboy committed
155

Melledy's avatar
Melledy committed
156
	public static void startConsole() {
157
158
		// Console should not start in dispatch only mode.
		if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
159
			getLogger().info(translate("messages.dispatch.no_commands_error"));
160
161
162
			return;
		}

163
		getLogger().info(translate("messages.status.done"));
164
165
166
167
168
169
170
171
172
173
174
175
		String input = null;
		boolean isLastInterrupted = false;
		while (true) {
			try {
				input = consoleLineReader.readLine("> ");
			} catch (UserInterruptException e) {
				if (!isLastInterrupted) {
					isLastInterrupted = true;
					Grasscutter.getLogger().info("Press Ctrl-C again to shutdown.");
					continue;
				} else {
					Runtime.getRuntime().exit(0);
Melledy's avatar
Melledy committed
176
				}
177
178
179
180
181
182
183
184
185
186
			} catch (EndOfFileException e) {
				Grasscutter.getLogger().info("EOF detected.");
				continue;
			} catch (IOError e) {
				Grasscutter.getLogger().error("An IO error occurred.", e);
				continue;
			}

			isLastInterrupted = false;
			try {
AnimeGitB's avatar
AnimeGitB committed
187
				CommandMap.getInstance().invoke(null, null, input);
188
			} catch (Exception e) {
189
				Grasscutter.getLogger().error(translate("messages.game.command_error"), e);
Melledy's avatar
Melledy committed
190
191
192
			}
		}
	}
KingRainbow44's avatar
KingRainbow44 committed
193
194
195
196
197

	public static Config getConfig() {
		return config;
	}

方块君's avatar
方块君 committed
198
199
200
201
	public static Language getLanguage() {
		return language;
	}

KingRainbow44's avatar
KingRainbow44 committed
202
203
204
205
	public static Logger getLogger() {
		return log;
	}

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
	public static LineReader getConsole() {
		if (consoleLineReader == null) {
			Terminal terminal = null;
			try {
				terminal = TerminalBuilder.builder().jna(true).build();
			} catch (Exception e) {
				try {
					// Fallback to a dumb jline terminal.
					terminal = TerminalBuilder.builder().dumb(true).build();
				} catch (Exception ignored) {
					// When dumb is true, build() never throws.
				}
			}
			consoleLineReader = LineReaderBuilder.builder()
					.terminal(terminal)
					.build();
		}
		return consoleLineReader;
	}

KingRainbow44's avatar
KingRainbow44 committed
226
227
228
229
230
231
232
233
234
235
236
	public static Gson getGsonFactory() {
		return gson;
	}

	public static DispatchServer getDispatchServer() {
		return dispatchServer;
	}

	public static GameServer getGameServer() {
		return gameServer;
	}
Secretboy's avatar
Secretboy committed
237

KingRainbow44's avatar
KingRainbow44 committed
238
239
240
	public static PluginManager getPluginManager() {
		return pluginManager;
	}
Secretboy's avatar
Secretboy committed
241

242
243
244
245
246
247
248
249
	public static void updateDayOfWeek() {
		Calendar calendar = Calendar.getInstance();
		day = calendar.get(Calendar.DAY_OF_WEEK); 
	}

	public static int getCurrentDayOfWeek() {
		return day;
	}
Secretboy's avatar
Secretboy committed
250

251
252
253
	public enum ServerRunMode {
		HYBRID, DISPATCH_ONLY, GAME_ONLY
	}
Secretboy's avatar
Secretboy committed
254

255
256
257
	public enum ServerDebugMode {
		ALL, MISSING, NONE
	}
Melledy's avatar
Melledy committed
258
}