2022-04-17 12:43:07 +00:00
|
|
|
package emu.grasscutter;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FileWriter;
|
2022-05-03 12:44:52 +00:00
|
|
|
import java.io.IOError;
|
2022-04-29 21:29:34 +00:00
|
|
|
import java.util.Calendar;
|
2022-04-17 12:43:07 +00:00
|
|
|
|
2022-04-20 12:21:38 +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;
|
2022-04-29 05:19:14 +00:00
|
|
|
import emu.grasscutter.scripts.ScriptLoader;
|
2022-04-18 05:11:27 +00:00
|
|
|
import emu.grasscutter.utils.Utils;
|
2022-05-03 12:44:52 +00:00
|
|
|
import org.jline.reader.EndOfFileException;
|
|
|
|
import org.jline.reader.LineReader;
|
|
|
|
import org.jline.reader.LineReaderBuilder;
|
|
|
|
import org.jline.reader.UserInterruptException;
|
|
|
|
import org.jline.terminal.Terminal;
|
|
|
|
import org.jline.terminal.TerminalBuilder;
|
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;
|
2022-05-06 03:57:55 +00:00
|
|
|
import emu.grasscutter.utils.Language;
|
2022-04-17 12:43:07 +00:00
|
|
|
import emu.grasscutter.server.dispatch.DispatchServer;
|
|
|
|
import emu.grasscutter.server.game.GameServer;
|
|
|
|
import emu.grasscutter.tools.Tools;
|
|
|
|
import emu.grasscutter.utils.Crypto;
|
|
|
|
|
2022-05-06 03:57:55 +00:00
|
|
|
import static emu.grasscutter.utils.Language.translate;
|
|
|
|
|
2022-04-18 05:11:27 +00:00
|
|
|
public final class Grasscutter {
|
|
|
|
private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class);
|
2022-05-03 12:44:52 +00:00
|
|
|
private static LineReader consoleLineReader = null;
|
2022-05-06 03:57:55 +00:00
|
|
|
|
|
|
|
private static Config config;
|
2022-05-03 06:23:25 +00:00
|
|
|
private static Language language;
|
2022-05-03 12:44:52 +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-05-05 05:24:49 +00:00
|
|
|
|
2022-05-06 03:57:55 +00:00
|
|
|
private static int day; // Current day of week.
|
2022-05-05 05:24:49 +00:00
|
|
|
|
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-05-05 05:24:49 +00:00
|
|
|
|
2022-04-26 04:39:05 +00:00
|
|
|
public static final Reflections reflector = new Reflections("emu.grasscutter");
|
2022-05-05 05:24:49 +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");
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-19 04:35:01 +00:00
|
|
|
// Load server configuration.
|
2022-04-18 05:11:27 +00:00
|
|
|
Grasscutter.loadConfig();
|
2022-05-03 06:23:25 +00:00
|
|
|
|
2022-05-06 03:57:55 +00:00
|
|
|
// Load translation files.
|
2022-05-03 06:23:25 +00:00
|
|
|
Grasscutter.loadLanguage();
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-18 05:11:27 +00:00
|
|
|
// Check server structure.
|
|
|
|
Utils.startupCheck();
|
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
public static void main(String[] args) throws Exception {
|
2022-05-06 03:57:55 +00:00
|
|
|
Crypto.loadKeys(); // Load keys from buffers.
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-05-06 03:57:55 +00:00
|
|
|
// Parse arguments.
|
2022-04-17 12:43:07 +00:00
|
|
|
for (String arg : args) {
|
|
|
|
switch (arg.toLowerCase()) {
|
2022-05-06 03:57:55 +00:00
|
|
|
case "-handbook" -> Tools.createGmHandbook();
|
2022-05-06 23:40:32 +00:00
|
|
|
case "-gachamap" -> Tools.createGachaMapping("./gacha-mapping.js");
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-18 05:11:27 +00:00
|
|
|
// Initialize server.
|
2022-05-06 03:57:55 +00:00
|
|
|
Grasscutter.getLogger().info(translate("messages.status.starting"));
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-18 05:11:27 +00:00
|
|
|
// Load all resources.
|
2022-04-29 21:29:34 +00:00
|
|
|
Grasscutter.updateDayOfWeek();
|
2022-04-17 12:43:07 +00:00
|
|
|
ResourceLoader.loadAll();
|
2022-04-29 05:19:14 +00:00
|
|
|
ScriptLoader.init();
|
2022-05-05 05:24:49 +00:00
|
|
|
|
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-05-05 05:24:49 +00:00
|
|
|
|
2022-04-23 05:17:35 +00:00
|
|
|
// Create server instances.
|
|
|
|
dispatchServer = new DispatchServer();
|
2022-05-06 03:57:55 +00:00
|
|
|
gameServer = new GameServer();
|
2022-05-03 01:20:24 +00:00
|
|
|
// Create a server hook instance with both servers.
|
|
|
|
new ServerHook(gameServer, dispatchServer);
|
2022-05-05 05:24:49 +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) {
|
2022-04-21 10:04:00 +00:00
|
|
|
dispatchServer.start();
|
|
|
|
gameServer.start();
|
2022-05-01 05:52:09 +00:00
|
|
|
} else if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
|
2022-04-21 10:04:00 +00:00
|
|
|
dispatchServer.start();
|
2022-05-01 05:52:09 +00:00
|
|
|
} else if (getConfig().RunMode == ServerRunMode.GAME_ONLY) {
|
2022-04-21 10:04:00 +00:00
|
|
|
gameServer.start();
|
|
|
|
} else {
|
2022-05-06 03:57:55 +00:00
|
|
|
getLogger().error(translate("messages.status.run_mode_error", getConfig().RunMode));
|
|
|
|
getLogger().error(translate("messages.status.run_mode_help"));
|
|
|
|
getLogger().error(translate("messages.status.shutdown"));
|
2022-04-21 10:04:00 +00:00
|
|
|
System.exit(1);
|
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-23 05:17:35 +00:00
|
|
|
// Enable all plugins.
|
|
|
|
pluginManager.enablePlugins();
|
2022-05-03 12:44:52 +00:00
|
|
|
|
2022-04-23 05:17:35 +00:00
|
|
|
// Hook into shutdown event.
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
|
2022-05-03 12:44:52 +00:00
|
|
|
|
|
|
|
// Open console.
|
|
|
|
startConsole();
|
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-05-03 12:44:52 +00:00
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
public static void loadConfig() {
|
|
|
|
try (FileReader file = new FileReader(configFile)) {
|
|
|
|
config = gson.fromJson(file, Config.class);
|
2022-04-21 21:44:55 +00:00
|
|
|
saveConfig();
|
2022-04-17 12:43:07 +00:00
|
|
|
} catch (Exception e) {
|
2022-04-21 21:44:55 +00:00
|
|
|
Grasscutter.config = new Config();
|
|
|
|
saveConfig();
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-03 06:23:25 +00:00
|
|
|
|
|
|
|
public static void loadLanguage() {
|
2022-05-06 03:57:55 +00:00
|
|
|
var locale = config.LocaleLanguage;
|
|
|
|
language = Language.getLanguage(locale.toLanguageTag());
|
2022-05-03 06:23:25 +00:00
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
public static void startConsole() {
|
2022-05-03 12:44:52 +00:00
|
|
|
// Console should not start in dispatch only mode.
|
|
|
|
if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
|
2022-05-06 03:57:55 +00:00
|
|
|
getLogger().info(translate("messages.dispatch.no_commands_error"));
|
2022-05-03 12:44:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-06 03:57:55 +00:00
|
|
|
getLogger().info(translate("messages.status.done"));
|
2022-05-03 12:44:52 +00:00
|
|
|
String input = null;
|
|
|
|
boolean isLastInterrupted = false;
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
input = consoleLineReader.readLine("> ");
|
|
|
|
} catch (UserInterruptException e) {
|
|
|
|
if (!isLastInterrupted) {
|
|
|
|
isLastInterrupted = true;
|
|
|
|
Grasscutter.getLogger().info("Press Ctrl-C again to shutdown.");
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
Runtime.getRuntime().exit(0);
|
2022-04-19 04:35:01 +00:00
|
|
|
}
|
2022-05-03 12:44:52 +00:00
|
|
|
} catch (EndOfFileException e) {
|
|
|
|
Grasscutter.getLogger().info("EOF detected.");
|
|
|
|
continue;
|
|
|
|
} catch (IOError e) {
|
|
|
|
Grasscutter.getLogger().error("An IO error occurred.", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
isLastInterrupted = false;
|
|
|
|
try {
|
2022-05-04 06:32:09 +00:00
|
|
|
CommandMap.getInstance().invoke(null, null, input);
|
2022-05-03 12:44:52 +00:00
|
|
|
} catch (Exception e) {
|
2022-05-06 03:57:55 +00:00
|
|
|
Grasscutter.getLogger().error(translate("messages.game.command_error"), e);
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 05:11:27 +00:00
|
|
|
|
|
|
|
public static Config getConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2022-05-03 06:23:25 +00:00
|
|
|
public static Language getLanguage() {
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
2022-04-18 05:11:27 +00:00
|
|
|
public static Logger getLogger() {
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
2022-05-03 12:44:52 +00:00
|
|
|
public static LineReader getConsole() {
|
|
|
|
if (consoleLineReader == null) {
|
|
|
|
Terminal terminal = null;
|
|
|
|
try {
|
|
|
|
terminal = TerminalBuilder.builder().jna(true).build();
|
|
|
|
} catch (Exception e) {
|
|
|
|
try {
|
|
|
|
// Fallback to a dumb jline terminal.
|
|
|
|
terminal = TerminalBuilder.builder().dumb(true).build();
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
// When dumb is true, build() never throws.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
consoleLineReader = LineReaderBuilder.builder()
|
|
|
|
.terminal(terminal)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
return consoleLineReader;
|
|
|
|
}
|
|
|
|
|
2022-04-18 05:11:27 +00:00
|
|
|
public static Gson getGsonFactory() {
|
|
|
|
return gson;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DispatchServer getDispatchServer() {
|
|
|
|
return dispatchServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static GameServer getGameServer() {
|
|
|
|
return gameServer;
|
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-23 05:17:35 +00:00
|
|
|
public static PluginManager getPluginManager() {
|
|
|
|
return pluginManager;
|
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-04-29 21:29:34 +00:00
|
|
|
public static void updateDayOfWeek() {
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
day = calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getCurrentDayOfWeek() {
|
|
|
|
return day;
|
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-05-01 05:52:09 +00:00
|
|
|
public enum ServerRunMode {
|
|
|
|
HYBRID, DISPATCH_ONLY, GAME_ONLY
|
|
|
|
}
|
2022-05-05 05:24:49 +00:00
|
|
|
|
2022-05-01 05:52:09 +00:00
|
|
|
public enum ServerDebugMode {
|
|
|
|
ALL, MISSING, NONE
|
|
|
|
}
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|