Grasscutter.java 3.2 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;

KingRainbow44's avatar
KingRainbow44 committed
10
import emu.grasscutter.utils.Utils;
Melledy's avatar
Melledy committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import ch.qos.logback.classic.Logger;
import emu.grasscutter.commands.ServerCommands;
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
25
public final class Grasscutter {
Melledy's avatar
Melledy committed
26
27
28
29
	static { 
		System.setProperty("logback.configurationFile", "src/main/resources/logback.xml");
	}
	
KingRainbow44's avatar
KingRainbow44 committed
30
	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
38
39
	
	public static RunMode MODE = RunMode.BOTH;
	private static DispatchServer dispatchServer;
	private static GameServer gameServer;
	
KingRainbow44's avatar
KingRainbow44 committed
40
41
42
43
44
45
46
	static {
		// Load configuration.
		Grasscutter.loadConfig();
		// Check server structure.
		Utils.startupCheck();
	}
	
Melledy's avatar
Melledy committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    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
64
65
		// Initialize server.
		Grasscutter.getLogger().info("Starting Grasscutter...");
Melledy's avatar
Melledy committed
66
		
KingRainbow44's avatar
KingRainbow44 committed
67
		// Load all resources.
Melledy's avatar
Melledy committed
68
69
70
71
		ResourceLoader.loadAll();
		// Database
		DatabaseManager.initialize();
		
KingRainbow44's avatar
KingRainbow44 committed
72
		// Start servers.
Melledy's avatar
Melledy committed
73
74
75
76
77
78
		dispatchServer = new DispatchServer();
		dispatchServer.start();
		
		gameServer = new GameServer(new InetSocketAddress(getConfig().GameServerIp, getConfig().GameServerPort));
		gameServer.start();
		
KingRainbow44's avatar
KingRainbow44 committed
79
		// Open console.
Melledy's avatar
Melledy committed
80
81
82
83
84
85
86
		startConsole();
    }
	
	public static void loadConfig() {
		try (FileReader file = new FileReader(configFile)) {
			config = gson.fromJson(file, Config.class);
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
87
			Grasscutter.config = new Config(); saveConfig();
Melledy's avatar
Melledy committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
		}
	}
	
	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) {
				ServerCommands.handle(input);
			}
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
106
			Grasscutter.getLogger().error("An error occurred.", e);
Melledy's avatar
Melledy committed
107
108
109
110
111
112
113
114
		}
	}
	
	public enum RunMode {
		BOTH,
		AUTH,
		GAME
	}
KingRainbow44's avatar
KingRainbow44 committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

	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
135
}