mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-15 16:34:56 +01:00
Refactor dispatch (now called HTTP) server (pt. 1)
This commit is contained in:
@@ -3,10 +3,18 @@ package emu.grasscutter;
|
||||
import java.io.*;
|
||||
import java.util.Calendar;
|
||||
|
||||
import emu.grasscutter.auth.AuthenticationSystem;
|
||||
import emu.grasscutter.auth.DefaultAuthentication;
|
||||
import emu.grasscutter.command.CommandMap;
|
||||
import emu.grasscutter.plugin.PluginManager;
|
||||
import emu.grasscutter.plugin.api.ServerHook;
|
||||
import emu.grasscutter.scripts.ScriptLoader;
|
||||
import emu.grasscutter.server.http.HttpServer;
|
||||
import emu.grasscutter.server.http.dispatch.DispatchHandler;
|
||||
import emu.grasscutter.server.http.handlers.AnnouncementsHandler;
|
||||
import emu.grasscutter.server.http.handlers.GenericHandler;
|
||||
import emu.grasscutter.server.http.handlers.LogHandler;
|
||||
import emu.grasscutter.server.http.dispatch.RegionHandler;
|
||||
import emu.grasscutter.utils.ConfigContainer;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import org.jline.reader.EndOfFileException;
|
||||
@@ -47,8 +55,10 @@ public final class Grasscutter {
|
||||
private static int day; // Current day of week.
|
||||
|
||||
private static DispatchServer dispatchServer;
|
||||
private static HttpServer httpServer;
|
||||
private static GameServer gameServer;
|
||||
private static PluginManager pluginManager;
|
||||
private static AuthenticationSystem authenticationSystem;
|
||||
|
||||
public static final Reflections reflector = new Reflections("emu.grasscutter");
|
||||
public static ConfigContainer config;
|
||||
@@ -98,14 +108,27 @@ public final class Grasscutter {
|
||||
|
||||
// Initialize database.
|
||||
DatabaseManager.initialize();
|
||||
|
||||
// Initialize the default authentication system.
|
||||
authenticationSystem = new DefaultAuthentication();
|
||||
|
||||
// Create server instances.
|
||||
dispatchServer = new DispatchServer();
|
||||
httpServer = new HttpServer();
|
||||
gameServer = new GameServer();
|
||||
// Create a server hook instance with both servers.
|
||||
new ServerHook(gameServer, dispatchServer);
|
||||
|
||||
// Create plugin manager instance.
|
||||
pluginManager = new PluginManager();
|
||||
// Add HTTP routes after loading plugins.
|
||||
httpServer.addRouter(HttpServer.UnhandledRequestRouter.class);
|
||||
httpServer.addRouter(HttpServer.DefaultRequestRouter.class);
|
||||
httpServer.addRouter(RegionHandler.class);
|
||||
httpServer.addRouter(LogHandler.class);
|
||||
httpServer.addRouter(GenericHandler.class);
|
||||
httpServer.addRouter(AnnouncementsHandler.class);
|
||||
httpServer.addRouter(DispatchHandler.class);
|
||||
|
||||
// Start servers.
|
||||
var runMode = SERVER.runMode;
|
||||
@@ -114,6 +137,7 @@ public final class Grasscutter {
|
||||
gameServer.start();
|
||||
} else if (runMode == ServerRunMode.DISPATCH_ONLY) {
|
||||
dispatchServer.start();
|
||||
httpServer.start();
|
||||
} else if (runMode == ServerRunMode.GAME_ONLY) {
|
||||
gameServer.start();
|
||||
} else {
|
||||
@@ -141,6 +165,19 @@ public final class Grasscutter {
|
||||
pluginManager.disablePlugins();
|
||||
}
|
||||
|
||||
/*
|
||||
* Methods for the language system component.
|
||||
*/
|
||||
|
||||
public static void loadLanguage() {
|
||||
var locale = config.language.language;
|
||||
language = Language.getLanguage(Utils.getLanguageCode(locale));
|
||||
}
|
||||
|
||||
/*
|
||||
* Methods for the configuration system component.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Attempts to load the configuration from a file.
|
||||
*/
|
||||
@@ -157,11 +194,6 @@ public final class Grasscutter {
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadLanguage() {
|
||||
var locale = config.language.language;
|
||||
language = Language.getLanguage(Utils.getLanguageCode(locale));
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the provided server configuration.
|
||||
* @param config The configuration to save, or null for a new one.
|
||||
@@ -178,44 +210,10 @@ public final class Grasscutter {
|
||||
}
|
||||
}
|
||||
|
||||
public static void startConsole() {
|
||||
// Console should not start in dispatch only mode.
|
||||
if (SERVER.runMode == ServerRunMode.DISPATCH_ONLY) {
|
||||
getLogger().info(translate("messages.dispatch.no_commands_error"));
|
||||
return;
|
||||
}
|
||||
|
||||
getLogger().info(translate("messages.status.done"));
|
||||
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);
|
||||
}
|
||||
} catch (EndOfFileException e) {
|
||||
Grasscutter.getLogger().info("EOF detected.");
|
||||
continue;
|
||||
} catch (IOError e) {
|
||||
Grasscutter.getLogger().error("An IO error occurred.", e);
|
||||
continue;
|
||||
}
|
||||
|
||||
isLastInterrupted = false;
|
||||
try {
|
||||
CommandMap.getInstance().invoke(null, null, input);
|
||||
} catch (Exception e) {
|
||||
Grasscutter.getLogger().error(translate("messages.game.command_error"), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Getters for the various server components.
|
||||
*/
|
||||
|
||||
public static ConfigContainer getConfig() {
|
||||
return config;
|
||||
}
|
||||
@@ -271,16 +269,74 @@ public final class Grasscutter {
|
||||
public static PluginManager getPluginManager() {
|
||||
return pluginManager;
|
||||
}
|
||||
|
||||
public static void updateDayOfWeek() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
day = calendar.get(Calendar.DAY_OF_WEEK);
|
||||
|
||||
public static AuthenticationSystem getAuthenticationSystem() {
|
||||
return authenticationSystem;
|
||||
}
|
||||
|
||||
public static int getCurrentDayOfWeek() {
|
||||
return day;
|
||||
}
|
||||
|
||||
/*
|
||||
* Utility methods.
|
||||
*/
|
||||
|
||||
public static void updateDayOfWeek() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
day = calendar.get(Calendar.DAY_OF_WEEK);
|
||||
}
|
||||
|
||||
public static void startConsole() {
|
||||
// Console should not start in dispatch only mode.
|
||||
if (SERVER.runMode == ServerRunMode.DISPATCH_ONLY) {
|
||||
getLogger().info(translate("messages.dispatch.no_commands_error"));
|
||||
return;
|
||||
}
|
||||
|
||||
getLogger().info(translate("messages.status.done"));
|
||||
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);
|
||||
}
|
||||
} catch (EndOfFileException e) {
|
||||
Grasscutter.getLogger().info("EOF detected.");
|
||||
continue;
|
||||
} catch (IOError e) {
|
||||
Grasscutter.getLogger().error("An IO error occurred.", e);
|
||||
continue;
|
||||
}
|
||||
|
||||
isLastInterrupted = false;
|
||||
try {
|
||||
CommandMap.getInstance().invoke(null, null, input);
|
||||
} catch (Exception e) {
|
||||
Grasscutter.getLogger().error(translate("messages.game.command_error"), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the authentication system for the server.
|
||||
* @param authenticationSystem The authentication system to use.
|
||||
*/
|
||||
public static void setAuthenticationSystem(AuthenticationSystem authenticationSystem) {
|
||||
Grasscutter.authenticationSystem = authenticationSystem;
|
||||
}
|
||||
|
||||
/*
|
||||
* Enums for the configuration.
|
||||
*/
|
||||
|
||||
public enum ServerRunMode {
|
||||
HYBRID, DISPATCH_ONLY, GAME_ONLY
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user