Grasscutter/src/main/java/emu/grasscutter/Grasscutter.java

166 lines
4.4 KiB
Java
Raw Normal View History

2022-04-17 12:43:07 +00:00
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;
import emu.grasscutter.command.CommandMap;
2022-04-18 05:11:27 +00:00
import emu.grasscutter.utils.Utils;
2022-04-19 04:35:01 +00:00
import org.reflections.Reflections;
2022-04-17 12:43:07 +00:00
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;
2022-04-18 05:11:27 +00:00
public final class Grasscutter {
private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class);
2022-04-17 12:43:07 +00:00
private static Config config;
2022-04-18 05:11:27 +00:00
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final File configFile = new File("./config.json");
2022-04-17 12:43:07 +00:00
public static RunMode MODE = RunMode.BOTH;
private static DispatchServer dispatchServer;
private static GameServer gameServer;
2022-04-21 03:56:27 +00:00
public static final Reflections reflector = new Reflections("emu.grasscutter");
2022-04-19 04:35:01 +00:00
2022-04-18 05:11:27 +00:00
static {
2022-04-19 04:35:01 +00:00
// Declare logback configuration.
System.setProperty("logback.configurationFile", "src/main/resources/logback.xml");
// Load server configuration.
2022-04-18 05:11:27 +00:00
Grasscutter.loadConfig();
2022-04-18 05:11:27 +00:00
// Check server structure.
Utils.startupCheck();
}
2022-04-17 12:43:07 +00:00
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;
}
}
2022-04-18 05:11:27 +00:00
// Initialize server.
Grasscutter.getLogger().info("Starting Grasscutter...");
2022-04-17 12:43:07 +00:00
2022-04-18 05:11:27 +00:00
// Load all resources.
2022-04-17 12:43:07 +00:00
ResourceLoader.loadAll();
// Database
DatabaseManager.initialize();
2022-04-18 05:11:27 +00:00
// Start servers.
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);
}
2022-04-17 12:43:07 +00:00
2022-04-18 05:11:27 +00:00
// Open console.
2022-04-17 12:43:07 +00:00
startConsole();
}
public static void loadConfig() {
try (FileReader file = new FileReader(configFile)) {
config = gson.fromJson(file, Config.class);
saveConfig();
2022-04-17 12:43:07 +00:00
} catch (Exception e) {
Grasscutter.config = new Config();
saveConfig();
2022-04-17 12:43:07 +00:00
}
}
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;
getLogger().info("Done! For help, type \"help\"");
2022-04-17 12:43:07 +00:00
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
while ((input = br.readLine()) != null) {
2022-04-19 04:35:01 +00:00
try {
if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
getLogger().error("Commands are not supported in dispatch only mode");
return;
}
2022-04-19 04:35:01 +00:00
CommandMap.getInstance().invoke(null, input);
} catch (Exception e) {
Grasscutter.getLogger().error("Command error: ");
e.printStackTrace();
2022-04-19 04:35:01 +00:00
}
2022-04-17 12:43:07 +00:00
}
} catch (Exception e) {
2022-04-18 05:11:27 +00:00
Grasscutter.getLogger().error("An error occurred.", e);
2022-04-17 12:43:07 +00:00
}
}
public enum RunMode {
BOTH,
AUTH,
GAME
}
2022-04-18 05:11:27 +00:00
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;
}
2022-04-17 12:43:07 +00:00
}