Grasscutter.java 4.84 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.scripts.ScriptLoader;
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
		ResourceLoader.loadAll();
71
		ScriptLoader.init();
Melledy's avatar
Melledy committed
72
73
		// Database
		DatabaseManager.initialize();
KingRainbow44's avatar
KingRainbow44 committed
74

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

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

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