Grasscutter.java 5.14 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
7
8
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;
9
import java.util.Calendar;
Melledy's avatar
Melledy committed
10

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

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

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

	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
183
184
185
186
	
	public static PluginManager getPluginManager() {
		return pluginManager;
	}
187
188
189
190
191
192
193
194
195
	
	public static void updateDayOfWeek() {
		Calendar calendar = Calendar.getInstance();
		day = calendar.get(Calendar.DAY_OF_WEEK); 
	}

	public static int getCurrentDayOfWeek() {
		return day;
	}
Melledy's avatar
Melledy committed
196
}