Grasscutter.java 3.45 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.
Melledy's avatar
Melledy committed
76
77
78
79
80
81
		dispatchServer = new DispatchServer();
		dispatchServer.start();
		
		gameServer = new GameServer(new InetSocketAddress(getConfig().GameServerIp, getConfig().GameServerPort));
		gameServer.start();
		
KingRainbow44's avatar
KingRainbow44 committed
82
		// Open console.
Melledy's avatar
Melledy committed
83
84
85
86
87
88
89
		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
90
			Grasscutter.config = new Config(); saveConfig();
Melledy's avatar
Melledy committed
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) {
Melledy's avatar
Melledy committed
106
107
108
109
110
				try {
					CommandMap.getInstance().invoke(null, input);
				} catch (Exception e) {
					Grasscutter.getLogger().error("Command error: " + e.getMessage());
				}
Melledy's avatar
Melledy committed
111
112
			}
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
113
			Grasscutter.getLogger().error("An error occurred.", e);
Melledy's avatar
Melledy committed
114
115
116
117
118
119
120
121
		}
	}
	
	public enum RunMode {
		BOTH,
		AUTH,
		GAME
	}
KingRainbow44's avatar
KingRainbow44 committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

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