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

		// Create server instances.
		dispatchServer = new DispatchServer();
		gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
		
		// Create plugin manager instance.
		pluginManager = new PluginManager();
Melledy's avatar
Melledy committed
79
		
KingRainbow44's avatar
KingRainbow44 committed
80
		// Start servers.
81
82
83
		if(getConfig().RunMode.equalsIgnoreCase("HYBRID")) {
			dispatchServer.start();
			gameServer.start();
KingRainbow44's avatar
KingRainbow44 committed
84
		} else if (getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
85
			dispatchServer.start();
KingRainbow44's avatar
KingRainbow44 committed
86
		} else if (getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
87
88
89
90
91
92
93
			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
94
95
96
		
		// Enable all plugins.
		pluginManager.enablePlugins();
Melledy's avatar
Melledy committed
97
		
KingRainbow44's avatar
KingRainbow44 committed
98
		// Open console.
Melledy's avatar
Melledy committed
99
		startConsole();
KingRainbow44's avatar
KingRainbow44 committed
100
101
		// Hook into shutdown event.
		Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
Melledy's avatar
Melledy committed
102
    }
KingRainbow44's avatar
KingRainbow44 committed
103
104
105
106
107
108
109
110

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

	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
176
177
178
179
	
	public static PluginManager getPluginManager() {
		return pluginManager;
	}
Melledy's avatar
Melledy committed
180
}