Grasscutter.java 3.1 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
26
public final class Grasscutter {
	private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class);
Melledy's avatar
Melledy committed
27
28
	private static Config config;
	
KingRainbow44's avatar
KingRainbow44 committed
29
30
	private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
	private static final File configFile = new File("./config.json");
Melledy's avatar
Melledy committed
31
32
33
34
35
	
	public static RunMode MODE = RunMode.BOTH;
	private static DispatchServer dispatchServer;
	private static GameServer gameServer;
	
KingRainbow44's avatar
KingRainbow44 committed
36
37
38
39
40
41
42
	static {
		// Load configuration.
		Grasscutter.loadConfig();
		// Check server structure.
		Utils.startupCheck();
	}
	
Melledy's avatar
Melledy committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    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
60
61
		// Initialize server.
		Grasscutter.getLogger().info("Starting Grasscutter...");
Melledy's avatar
Melledy committed
62
		
KingRainbow44's avatar
KingRainbow44 committed
63
		// Load all resources.
Melledy's avatar
Melledy committed
64
65
66
67
		ResourceLoader.loadAll();
		// Database
		DatabaseManager.initialize();
		
KingRainbow44's avatar
KingRainbow44 committed
68
		// Start servers.
Melledy's avatar
Melledy committed
69
70
71
72
73
74
		dispatchServer = new DispatchServer();
		dispatchServer.start();
		
		gameServer = new GameServer(new InetSocketAddress(getConfig().GameServerIp, getConfig().GameServerPort));
		gameServer.start();
		
KingRainbow44's avatar
KingRainbow44 committed
75
		// Open console.
Melledy's avatar
Melledy committed
76
77
78
79
80
81
82
		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
83
			Grasscutter.config = new Config(); saveConfig();
Melledy's avatar
Melledy committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
		}
	}
	
	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
102
			Grasscutter.getLogger().error("An error occurred.", e);
Melledy's avatar
Melledy committed
103
104
105
106
107
108
109
110
		}
	}
	
	public enum RunMode {
		BOTH,
		AUTH,
		GAME
	}
KingRainbow44's avatar
KingRainbow44 committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

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