Grasscutter.java 4.9 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
7
8
9
package emu.grasscutter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;

Jaida Wu's avatar
Jaida Wu committed
10
import emu.grasscutter.command.CommandMap;
KingRainbow44's avatar
KingRainbow44 committed
11
import emu.grasscutter.plugin.PluginManager;
12
import emu.grasscutter.plugin.api.ServerHook;
KingRainbow44's avatar
KingRainbow44 committed
13
import emu.grasscutter.utils.Utils;
Melledy's avatar
Melledy committed
14
import org.reflections.Reflections;
Melledy's avatar
Melledy committed
15
16
17
18
19
20
21
22
23
24
25
26
27
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
28
29
public final class Grasscutter {
	private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class);
Melledy's avatar
Melledy committed
30
31
	private static Config config;
	
KingRainbow44's avatar
KingRainbow44 committed
32
33
	private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
	private static final File configFile = new File("./config.json");
Melledy's avatar
Melledy committed
34
35
36
37
	
	public static RunMode MODE = RunMode.BOTH;
	private static DispatchServer dispatchServer;
	private static GameServer gameServer;
KingRainbow44's avatar
KingRainbow44 committed
38
	private static PluginManager pluginManager;
Melledy's avatar
Melledy committed
39
	
KingRainbow44's avatar
KingRainbow44 committed
40
	public static final Reflections reflector = new Reflections("emu.grasscutter");
Melledy's avatar
Melledy committed
41
	
KingRainbow44's avatar
KingRainbow44 committed
42
	static {
Melledy's avatar
Melledy committed
43
44
45
46
		// Declare logback configuration.
		System.setProperty("logback.configurationFile", "src/main/resources/logback.xml");
		
		// Load server configuration.
KingRainbow44's avatar
KingRainbow44 committed
47
		Grasscutter.loadConfig();
48
		
KingRainbow44's avatar
KingRainbow44 committed
49
50
51
52
		// Check server structure.
		Utils.startupCheck();
	}
	
Melledy's avatar
Melledy committed
53
54
55
56
57
    public static void main(String[] args) throws Exception {
    	Crypto.loadKeys();
    	
		for (String arg : args) {
			switch (arg.toLowerCase()) {
KingRainbow44's avatar
KingRainbow44 committed
58
59
60
61
62
				case "-auth" -> MODE = RunMode.AUTH;
				case "-game" -> MODE = RunMode.GAME;
				case "-handbook" -> {
					Tools.createGmHandbook(); return;
				}
Melledy's avatar
Melledy committed
63
64
65
			}
		}
		
KingRainbow44's avatar
KingRainbow44 committed
66
67
		// Initialize server.
		Grasscutter.getLogger().info("Starting Grasscutter...");
Melledy's avatar
Melledy committed
68
		
KingRainbow44's avatar
KingRainbow44 committed
69
		// Load all resources.
Melledy's avatar
Melledy committed
70
71
72
		ResourceLoader.loadAll();
		// Database
		DatabaseManager.initialize();
KingRainbow44's avatar
KingRainbow44 committed
73

KingRainbow44's avatar
KingRainbow44 committed
74
75
76
		// Create plugin manager instance.
		pluginManager = new PluginManager();
		
KingRainbow44's avatar
KingRainbow44 committed
77
78
79
80
		// Create server instances.
		dispatchServer = new DispatchServer();
		gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
		
81
82
83
		// Create server hook instance.
		new ServerHook(gameServer, dispatchServer);
		
KingRainbow44's avatar
KingRainbow44 committed
84
		// Start servers.
85
86
87
		if(getConfig().RunMode.equalsIgnoreCase("HYBRID")) {
			dispatchServer.start();
			gameServer.start();
KingRainbow44's avatar
KingRainbow44 committed
88
		} else if (getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
89
			dispatchServer.start();
KingRainbow44's avatar
KingRainbow44 committed
90
		} else if (getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
91
92
93
94
95
96
97
			gameServer.start();
		} else {
			getLogger().error("Invalid server run mode. " + getConfig().RunMode);
			getLogger().error("Server run mode must be 'HYBRID', 'DISPATCH_ONLY', or 'GAME_ONLY'. Unable to start Grasscutter...");
			getLogger().error("Shutting down...");
			System.exit(1);
		}
KingRainbow44's avatar
KingRainbow44 committed
98
99
100
		
		// Enable all plugins.
		pluginManager.enablePlugins();
Melledy's avatar
Melledy committed
101
		
KingRainbow44's avatar
KingRainbow44 committed
102
		// Open console.
Melledy's avatar
Melledy committed
103
		startConsole();
KingRainbow44's avatar
KingRainbow44 committed
104
105
		// Hook into shutdown event.
		Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
Melledy's avatar
Melledy committed
106
    }
KingRainbow44's avatar
KingRainbow44 committed
107
108
109
110
111
112
113
114

	/**
	 * Server shutdown event.
	 */
	private static void onShutdown() {
		// Disable all plugins.
		pluginManager.disablePlugins();
	}
Melledy's avatar
Melledy committed
115
116
117
118
	
	public static void loadConfig() {
		try (FileReader file = new FileReader(configFile)) {
			config = gson.fromJson(file, Config.class);
119
			saveConfig();
Melledy's avatar
Melledy committed
120
		} catch (Exception e) {
121
122
			Grasscutter.config = new Config(); 
			saveConfig();
Melledy's avatar
Melledy committed
123
124
125
126
127
128
129
		}
	}
	
	public static void saveConfig() {
		try (FileWriter file = new FileWriter(configFile)) {
			file.write(gson.toJson(config));
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
130
			Grasscutter.getLogger().error("Unable to save config file.");
Melledy's avatar
Melledy committed
131
132
133
134
135
		}
	}
	
	public static void startConsole() {
		String input;
Jaida Wu's avatar
Jaida Wu committed
136
		getLogger().info("Done! For help, type \"help\"");
Melledy's avatar
Melledy committed
137
138
		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
			while ((input = br.readLine()) != null) {
Melledy's avatar
Melledy committed
139
				try {
140
					if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
KingRainbow44's avatar
KingRainbow44 committed
141
						getLogger().error("Commands are not supported in dispatch only mode.");
142
143
						return;
					}
KingRainbow44's avatar
KingRainbow44 committed
144
					
Melledy's avatar
Melledy committed
145
146
					CommandMap.getInstance().invoke(null, input);
				} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
147
					Grasscutter.getLogger().error("Command error:", e);
Melledy's avatar
Melledy committed
148
				}
Melledy's avatar
Melledy committed
149
150
			}
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
151
			Grasscutter.getLogger().error("An error occurred.", e);
Melledy's avatar
Melledy committed
152
153
154
155
156
157
158
159
		}
	}
	
	public enum RunMode {
		BOTH,
		AUTH,
		GAME
	}
KingRainbow44's avatar
KingRainbow44 committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

	public static Config getConfig() {
		return config;
	}

	public static Logger getLogger() {
		return log;
	}

	public static Gson getGsonFactory() {
		return gson;
	}

	public static DispatchServer getDispatchServer() {
		return dispatchServer;
	}

	public static GameServer getGameServer() {
		return gameServer;
	}
KingRainbow44's avatar
KingRainbow44 committed
180
181
182
183
	
	public static PluginManager getPluginManager() {
		return pluginManager;
	}
Melledy's avatar
Melledy committed
184
}