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

235 lines
6.2 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 java.util.Calendar;
2022-04-17 12:43:07 +00:00
import emu.grasscutter.command.CommandMap;
2022-04-23 05:17:35 +00:00
import emu.grasscutter.plugin.PluginManager;
2022-05-03 01:20:24 +00:00
import emu.grasscutter.plugin.api.ServerHook;
import emu.grasscutter.scripts.ScriptLoader;
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;
private static Language language;
2022-04-17 12:43:07 +00:00
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
private static int day; // Current day of week
2022-04-17 12:43:07 +00:00
private static DispatchServer dispatchServer;
private static GameServer gameServer;
2022-04-23 05:17:35 +00:00
private static PluginManager pluginManager;
2022-04-17 12:43:07 +00:00
2022-04-26 04:39:05 +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();
// Load Language
Grasscutter.loadLanguage();
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()) {
2022-04-23 05:17:35 +00:00
case "-handbook" -> {
Tools.createGmHandbook(); return;
}
case "-gachamap" -> {
Tools.createGachaMapping(); return;
}
2022-04-17 12:43:07 +00:00
}
}
2022-04-18 05:11:27 +00:00
// Initialize server.
Grasscutter.getLogger().info(language.Starting_Grasscutter);
2022-04-17 12:43:07 +00:00
2022-04-18 05:11:27 +00:00
// Load all resources.
Grasscutter.updateDayOfWeek();
2022-04-17 12:43:07 +00:00
ResourceLoader.loadAll();
ScriptLoader.init();
2022-04-17 12:43:07 +00:00
// Database
DatabaseManager.initialize();
2022-04-23 05:17:35 +00:00
2022-04-26 06:07:00 +00:00
// Create plugin manager instance.
pluginManager = new PluginManager();
2022-04-23 05:17:35 +00:00
// Create server instances.
dispatchServer = new DispatchServer();
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
2022-05-03 01:20:24 +00:00
// Create a server hook instance with both servers.
new ServerHook(gameServer, dispatchServer);
2022-04-23 05:17:35 +00:00
2022-04-18 05:11:27 +00:00
// Start servers.
2022-05-01 05:52:09 +00:00
if (getConfig().RunMode == ServerRunMode.HYBRID) {
dispatchServer.start();
gameServer.start();
2022-05-01 05:52:09 +00:00
} else if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
dispatchServer.start();
2022-05-01 05:52:09 +00:00
} else if (getConfig().RunMode == ServerRunMode.GAME_ONLY) {
gameServer.start();
} else {
getLogger().error(language.Invalid_server_run_mode + " " + getConfig().RunMode);
getLogger().error(language.Server_run_mode);
getLogger().error(language.Shutting_down);
System.exit(1);
}
2022-04-23 05:17:35 +00:00
// Enable all plugins.
pluginManager.enablePlugins();
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();
2022-04-23 05:17:35 +00:00
// Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
2022-04-17 12:43:07 +00:00
}
2022-04-23 05:17:35 +00:00
/**
* Server shutdown event.
*/
private static void onShutdown() {
// Disable all plugins.
pluginManager.disablePlugins();
}
2022-04-17 12:43:07 +00:00
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 loadLanguage() {
2022-05-03 10:19:43 +00:00
try (FileReader file = new FileReader(String.format(getConfig().LANGUAGE_FOLDER + "%s.json", Grasscutter.config.Language))) {
language = gson.fromJson(file, Language.class);
} catch (Exception e) {
Grasscutter.language = new Language();
Grasscutter.config.Language = "en_us";
saveConfig();
try {
File folder = new File("./language");
if (!folder.exists() && !folder.isDirectory()) {
//noinspection ResultOfMethodCallIgnored
folder.mkdirs();
}
} catch (Exception ee) {
Grasscutter.getLogger().error("Unable to create language folder.");
}
try (FileWriter file = new FileWriter("./language/en_us.json")) {
file.write(gson.toJson(language));
} catch (Exception ee) {
Grasscutter.getLogger().error("Unable to create language file.");
}
}
}
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) {
2022-04-23 05:17:35 +00:00
Grasscutter.getLogger().error("Unable to save config file.");
2022-04-17 12:43:07 +00:00
}
}
public static void startConsole() {
String input;
getLogger().info(language.Start_done);
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 {
2022-05-01 05:52:09 +00:00
if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
getLogger().error(language.Dispatch_mode_not_support_command);
return;
}
2022-04-23 05:17:35 +00:00
2022-04-19 04:35:01 +00:00
CommandMap.getInstance().invoke(null, input);
} catch (Exception e) {
Grasscutter.getLogger().error(language.Command_error, e);
2022-04-19 04:35:01 +00:00
}
2022-04-17 12:43:07 +00:00
}
} catch (Exception e) {
2022-05-03 10:19:43 +00:00
Grasscutter.getLogger().error(language.Error, e);
2022-04-17 12:43:07 +00:00
}
}
2022-04-18 05:11:27 +00:00
public static Config getConfig() {
return config;
}
public static Language getLanguage() {
return language;
}
2022-04-18 05:11:27 +00:00
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-23 05:17:35 +00:00
public static PluginManager getPluginManager() {
return pluginManager;
}
public static void updateDayOfWeek() {
Calendar calendar = Calendar.getInstance();
day = calendar.get(Calendar.DAY_OF_WEEK);
}
public static int getCurrentDayOfWeek() {
return day;
}
2022-05-01 05:52:09 +00:00
public enum ServerRunMode {
HYBRID, DISPATCH_ONLY, GAME_ONLY
}
public enum ServerDebugMode {
ALL, MISSING, NONE
}
2022-04-17 12:43:07 +00:00
}