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

			gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
			gameServer.start();
		} else if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
			dispatchServer = new DispatchServer();
			dispatchServer.start();
		} else if(getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
			gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
			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);
		}


Melledy's avatar
Melledy committed
96
		
KingRainbow44's avatar
KingRainbow44 committed
97
		// Open console.
Melledy's avatar
Melledy committed
98
99
100
101
102
103
		startConsole();
    }
	
	public static void loadConfig() {
		try (FileReader file = new FileReader(configFile)) {
			config = gson.fromJson(file, Config.class);
104
			saveConfig();
Melledy's avatar
Melledy committed
105
		} catch (Exception e) {
106
107
			Grasscutter.config = new Config(); 
			saveConfig();
Melledy's avatar
Melledy committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
		}
	}
	
	public static void saveConfig() {
		try (FileWriter file = new FileWriter(configFile)) {
			file.write(gson.toJson(config));
		} catch (Exception e) {
			Grasscutter.getLogger().error("Config save error");
		}
	}
	
	public static void startConsole() {
		String input;
		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
			while ((input = br.readLine()) != null) {
Melledy's avatar
Melledy committed
123
				try {
124
125
126
127
					if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
						getLogger().error("Commands are not supported in dispatch only mode");
						return;
					}
Melledy's avatar
Melledy committed
128
129
					CommandMap.getInstance().invoke(null, input);
				} catch (Exception e) {
130
131
					Grasscutter.getLogger().error("Command error: ");
					e.printStackTrace();
Melledy's avatar
Melledy committed
132
				}
Melledy's avatar
Melledy committed
133
134
			}
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
135
			Grasscutter.getLogger().error("An error occurred.", e);
Melledy's avatar
Melledy committed
136
137
138
139
140
141
142
143
		}
	}
	
	public enum RunMode {
		BOTH,
		AUTH,
		GAME
	}
KingRainbow44's avatar
KingRainbow44 committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

	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;
	}
Melledy's avatar
Melledy committed
164
}