Grasscutter.java 7.67 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;
Melledy's avatar
Melledy committed
7
import java.net.InetSocketAddress;
8
import java.util.Calendar;
Melledy's avatar
Melledy committed
9

Jaida Wu's avatar
Jaida Wu committed
10
import emu.grasscutter.command.CommandMap;
KingRainbow44's avatar
KingRainbow44 committed
11
import emu.grasscutter.plugin.PluginManager;
KingRainbow44's avatar
KingRainbow44 committed
12
import emu.grasscutter.plugin.api.ServerHook;
13
import emu.grasscutter.scripts.ScriptLoader;
KingRainbow44's avatar
KingRainbow44 committed
14
import emu.grasscutter.utils.Utils;
15
16
17
18
19
20
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
21
import org.reflections.Reflections;
Melledy's avatar
Melledy committed
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
import emu.grasscutter.server.dispatch.DispatchServer;
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.Crypto;

KingRainbow44's avatar
KingRainbow44 committed
35
36
public final class Grasscutter {
	private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class);
Melledy's avatar
Melledy committed
37
	private static Config config;
38
	private static LineReader consoleLineReader = null;
方块君's avatar
方块君 committed
39
	private static Language language;
mzfqy's avatar
mzfqy committed
40
	private static CNLanguage cn_language;
41

KingRainbow44's avatar
KingRainbow44 committed
42
43
	private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
	private static final File configFile = new File("./config.json");
Melledy's avatar
Melledy committed
44
	
45
46
	private static int day; // Current day of week
	
Melledy's avatar
Melledy committed
47
48
	private static DispatchServer dispatchServer;
	private static GameServer gameServer;
KingRainbow44's avatar
KingRainbow44 committed
49
	private static PluginManager pluginManager;
Melledy's avatar
Melledy committed
50
	
KingRainbow44's avatar
KingRainbow44 committed
51
	public static final Reflections reflector = new Reflections("emu.grasscutter");
Melledy's avatar
Melledy committed
52
	
KingRainbow44's avatar
KingRainbow44 committed
53
	static {
Melledy's avatar
Melledy committed
54
55
56
57
		// Declare logback configuration.
		System.setProperty("logback.configurationFile", "src/main/resources/logback.xml");
		
		// Load server configuration.
KingRainbow44's avatar
KingRainbow44 committed
58
		Grasscutter.loadConfig();
方块君's avatar
方块君 committed
59
60
61

		// Load Language
		Grasscutter.loadLanguage();
62
		
KingRainbow44's avatar
KingRainbow44 committed
63
64
65
66
		// Check server structure.
		Utils.startupCheck();
	}
	
Melledy's avatar
Melledy committed
67
68
69
70
71
    public static void main(String[] args) throws Exception {
    	Crypto.loadKeys();
    	
		for (String arg : args) {
			switch (arg.toLowerCase()) {
KingRainbow44's avatar
KingRainbow44 committed
72
73
74
				case "-handbook" -> {
					Tools.createGmHandbook(); return;
				}
75
76
77
				case "-gachamap" -> {
					Tools.createGachaMapping(); return;
				}
Melledy's avatar
Melledy committed
78
79
80
			}
		}
		
KingRainbow44's avatar
KingRainbow44 committed
81
		// Initialize server.
方块君's avatar
方块君 committed
82
		Grasscutter.getLogger().info(language.Starting_Grasscutter);
Melledy's avatar
Melledy committed
83
		
KingRainbow44's avatar
KingRainbow44 committed
84
		// Load all resources.
85
		Grasscutter.updateDayOfWeek();
Melledy's avatar
Melledy committed
86
		ResourceLoader.loadAll();
87
		ScriptLoader.init();
88
		
Melledy's avatar
Melledy committed
89
90
		// Database
		DatabaseManager.initialize();
KingRainbow44's avatar
KingRainbow44 committed
91

KingRainbow44's avatar
KingRainbow44 committed
92
93
94
		// Create plugin manager instance.
		pluginManager = new PluginManager();
		
KingRainbow44's avatar
KingRainbow44 committed
95
96
97
		// Create server instances.
		dispatchServer = new DispatchServer();
		gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
KingRainbow44's avatar
KingRainbow44 committed
98
99
		// Create a server hook instance with both servers.
		new ServerHook(gameServer, dispatchServer);
KingRainbow44's avatar
KingRainbow44 committed
100
		
KingRainbow44's avatar
KingRainbow44 committed
101
		// Start servers.
102
		if (getConfig().RunMode == ServerRunMode.HYBRID) {
103
104
			dispatchServer.start();
			gameServer.start();
105
		} else if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
106
			dispatchServer.start();
107
		} else if (getConfig().RunMode == ServerRunMode.GAME_ONLY) {
108
109
			gameServer.start();
		} else {
方块君's avatar
方块君 committed
110
111
112
			getLogger().error(language.Invalid_server_run_mode + " " + getConfig().RunMode);
			getLogger().error(language.Server_run_mode);
			getLogger().error(language.Shutting_down);
113
114
			System.exit(1);
		}
KingRainbow44's avatar
KingRainbow44 committed
115
116
117
		
		// Enable all plugins.
		pluginManager.enablePlugins();
118

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

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

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

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

	public static void loadLanguage() {
145
		try (FileReader file = new FileReader(String.format("%s%s.json", getConfig().LANGUAGE_FOLDER, Grasscutter.config.Language))) {
方块君's avatar
方块君 committed
146
147
148
			language = gson.fromJson(file, Language.class);
		} catch (Exception e) {
			Grasscutter.language = new Language();
mzfqy's avatar
mzfqy committed
149
			Grasscutter.cn_language = new CNLanguage();
方块君's avatar
方块君 committed
150
151
152
153
			Grasscutter.config.Language = "en_us";
			saveConfig();

			try {
mzfqy's avatar
mzfqy committed
154
				File folder = new File("./languages");
方块君's avatar
方块君 committed
155
156
157
158
159
160
161
				if (!folder.exists() && !folder.isDirectory()) {
					//noinspection ResultOfMethodCallIgnored
					folder.mkdirs();
				}
			} catch (Exception ee) {
				Grasscutter.getLogger().error("Unable to create language folder.");
			}
mzfqy's avatar
mzfqy committed
162
			try (FileWriter file = new FileWriter("./languages/en_us.json")) {
方块君's avatar
方块君 committed
163
164
165
166
				file.write(gson.toJson(language));
			} catch (Exception ee) {
				Grasscutter.getLogger().error("Unable to create language file.");
			}
mzfqy's avatar
mzfqy committed
167
168
169
170
171
			try (FileWriter file = new FileWriter("./languages/zh_cn.json")) {
				file.write(gson.toJson(cn_language));
			} catch (Exception ee) {
				Grasscutter.getLogger().error("无法创建中文语言文件。");
			}
方块君's avatar
方块君 committed
172
173
		}
	}
Melledy's avatar
Melledy committed
174
175
176
177
178
	
	public static void saveConfig() {
		try (FileWriter file = new FileWriter(configFile)) {
			file.write(gson.toJson(config));
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
179
			Grasscutter.getLogger().error("Unable to save config file.");
Melledy's avatar
Melledy committed
180
181
182
183
		}
	}
	
	public static void startConsole() {
184
185
186
187
188
189
		// Console should not start in dispatch only mode.
		if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
			getLogger().info(language.Dispatch_mode_not_support_command);
			return;
		}

方块君's avatar
方块君 committed
190
		getLogger().info(language.Start_done);
191
192
193
194
195
196
197
198
199
200
201
202
		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
203
				}
204
205
206
207
208
209
210
211
212
213
214
215
216
			} catch (EndOfFileException e) {
				Grasscutter.getLogger().info("EOF detected.");
				continue;
			} catch (IOError e) {
				Grasscutter.getLogger().error("An IO error occurred.", e);
				continue;
			}

			isLastInterrupted = false;
			try {
				CommandMap.getInstance().invoke(null, input);
			} catch (Exception e) {
				Grasscutter.getLogger().error(language.Command_error, e);
Melledy's avatar
Melledy committed
217
218
219
			}
		}
	}
KingRainbow44's avatar
KingRainbow44 committed
220
221
222
223
224

	public static Config getConfig() {
		return config;
	}

方块君's avatar
方块君 committed
225
226
227
228
	public static Language getLanguage() {
		return language;
	}

KingRainbow44's avatar
KingRainbow44 committed
229
230
231
232
	public static Logger getLogger() {
		return log;
	}

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
	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
253
254
255
256
257
258
259
260
261
262
263
	public static Gson getGsonFactory() {
		return gson;
	}

	public static DispatchServer getDispatchServer() {
		return dispatchServer;
	}

	public static GameServer getGameServer() {
		return gameServer;
	}
KingRainbow44's avatar
KingRainbow44 committed
264
265
266
267
	
	public static PluginManager getPluginManager() {
		return pluginManager;
	}
268
269
270
271
272
273
274
275
276
	
	public static void updateDayOfWeek() {
		Calendar calendar = Calendar.getInstance();
		day = calendar.get(Calendar.DAY_OF_WEEK); 
	}

	public static int getCurrentDayOfWeek() {
		return day;
	}
277
278
279
280
281
282
283
284
	
	public enum ServerRunMode {
		HYBRID, DISPATCH_ONLY, GAME_ONLY
	}
	
	public enum ServerDebugMode {
		ALL, MISSING, NONE
	}
Melledy's avatar
Melledy committed
285
}