mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-01-26 23:05:27 +08:00
Implement server API for handbook controls (avatar)
This commit is contained in:
parent
62fd82fa54
commit
2bd992592d
@ -1,322 +1,324 @@
|
|||||||
package emu.grasscutter;
|
package emu.grasscutter;
|
||||||
|
|
||||||
import static emu.grasscutter.config.Configuration.SERVER;
|
import static emu.grasscutter.config.Configuration.SERVER;
|
||||||
import static emu.grasscutter.utils.Language.translate;
|
import static emu.grasscutter.utils.Language.translate;
|
||||||
|
|
||||||
import ch.qos.logback.classic.Level;
|
import ch.qos.logback.classic.Level;
|
||||||
import ch.qos.logback.classic.Logger;
|
import ch.qos.logback.classic.Logger;
|
||||||
import emu.grasscutter.auth.AuthenticationSystem;
|
import emu.grasscutter.auth.AuthenticationSystem;
|
||||||
import emu.grasscutter.auth.DefaultAuthentication;
|
import emu.grasscutter.auth.DefaultAuthentication;
|
||||||
import emu.grasscutter.command.CommandMap;
|
import emu.grasscutter.command.CommandMap;
|
||||||
import emu.grasscutter.command.DefaultPermissionHandler;
|
import emu.grasscutter.command.DefaultPermissionHandler;
|
||||||
import emu.grasscutter.command.PermissionHandler;
|
import emu.grasscutter.command.PermissionHandler;
|
||||||
import emu.grasscutter.config.ConfigContainer;
|
import emu.grasscutter.config.ConfigContainer;
|
||||||
import emu.grasscutter.data.ResourceLoader;
|
import emu.grasscutter.data.ResourceLoader;
|
||||||
import emu.grasscutter.database.DatabaseManager;
|
import emu.grasscutter.database.DatabaseManager;
|
||||||
import emu.grasscutter.plugin.PluginManager;
|
import emu.grasscutter.plugin.PluginManager;
|
||||||
import emu.grasscutter.plugin.api.ServerHook;
|
import emu.grasscutter.plugin.api.ServerHook;
|
||||||
import emu.grasscutter.scripts.ScriptLoader;
|
import emu.grasscutter.scripts.ScriptLoader;
|
||||||
import emu.grasscutter.server.game.GameServer;
|
import emu.grasscutter.server.game.GameServer;
|
||||||
import emu.grasscutter.server.http.HttpServer;
|
import emu.grasscutter.server.http.HttpServer;
|
||||||
import emu.grasscutter.server.http.dispatch.DispatchHandler;
|
import emu.grasscutter.server.http.dispatch.DispatchHandler;
|
||||||
import emu.grasscutter.server.http.dispatch.RegionHandler;
|
import emu.grasscutter.server.http.dispatch.RegionHandler;
|
||||||
import emu.grasscutter.server.http.documentation.DocumentationServerHandler;
|
import emu.grasscutter.server.http.documentation.DocumentationServerHandler;
|
||||||
import emu.grasscutter.server.http.handlers.AnnouncementsHandler;
|
import emu.grasscutter.server.http.documentation.HandbookHandler;
|
||||||
import emu.grasscutter.server.http.handlers.GachaHandler;
|
import emu.grasscutter.server.http.handlers.AnnouncementsHandler;
|
||||||
import emu.grasscutter.server.http.handlers.GenericHandler;
|
import emu.grasscutter.server.http.handlers.GachaHandler;
|
||||||
import emu.grasscutter.server.http.handlers.LogHandler;
|
import emu.grasscutter.server.http.handlers.GenericHandler;
|
||||||
import emu.grasscutter.tools.Tools;
|
import emu.grasscutter.server.http.handlers.LogHandler;
|
||||||
import emu.grasscutter.utils.*;
|
import emu.grasscutter.tools.Tools;
|
||||||
import java.io.File;
|
import emu.grasscutter.utils.*;
|
||||||
import java.io.FileWriter;
|
import java.io.File;
|
||||||
import java.io.IOError;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOError;
|
||||||
import java.util.Calendar;
|
import java.io.IOException;
|
||||||
import javax.annotation.Nullable;
|
import java.util.Calendar;
|
||||||
import lombok.Getter;
|
import javax.annotation.Nullable;
|
||||||
import lombok.Setter;
|
import lombok.Getter;
|
||||||
import org.jline.reader.EndOfFileException;
|
import lombok.Setter;
|
||||||
import org.jline.reader.LineReader;
|
import org.jline.reader.EndOfFileException;
|
||||||
import org.jline.reader.LineReaderBuilder;
|
import org.jline.reader.LineReader;
|
||||||
import org.jline.reader.UserInterruptException;
|
import org.jline.reader.LineReaderBuilder;
|
||||||
import org.jline.terminal.Terminal;
|
import org.jline.reader.UserInterruptException;
|
||||||
import org.jline.terminal.TerminalBuilder;
|
import org.jline.terminal.Terminal;
|
||||||
import org.reflections.Reflections;
|
import org.jline.terminal.TerminalBuilder;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.reflections.Reflections;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
public final class Grasscutter {
|
|
||||||
public static final File configFile = new File("./config.json");
|
public final class Grasscutter {
|
||||||
public static final Reflections reflector = new Reflections("emu.grasscutter");
|
public static final File configFile = new File("./config.json");
|
||||||
@Getter private static final Logger logger = (Logger) LoggerFactory.getLogger(Grasscutter.class);
|
public static final Reflections reflector = new Reflections("emu.grasscutter");
|
||||||
|
@Getter private static final Logger logger = (Logger) LoggerFactory.getLogger(Grasscutter.class);
|
||||||
@Getter public static ConfigContainer config;
|
|
||||||
|
@Getter public static ConfigContainer config;
|
||||||
@Getter @Setter private static Language language;
|
|
||||||
@Getter @Setter private static String preferredLanguage;
|
@Getter @Setter private static Language language;
|
||||||
|
@Getter @Setter private static String preferredLanguage;
|
||||||
@Getter private static int currentDayOfWeek;
|
|
||||||
@Setter private static ServerRunMode runModeOverride = null; // Config override for run mode
|
@Getter private static int currentDayOfWeek;
|
||||||
|
@Setter private static ServerRunMode runModeOverride = null; // Config override for run mode
|
||||||
@Getter private static HttpServer httpServer;
|
|
||||||
@Getter private static GameServer gameServer;
|
@Getter private static HttpServer httpServer;
|
||||||
@Getter private static PluginManager pluginManager;
|
@Getter private static GameServer gameServer;
|
||||||
@Getter private static CommandMap commandMap;
|
@Getter private static PluginManager pluginManager;
|
||||||
|
@Getter private static CommandMap commandMap;
|
||||||
@Getter @Setter private static AuthenticationSystem authenticationSystem;
|
|
||||||
@Getter @Setter private static PermissionHandler permissionHandler;
|
@Getter @Setter private static AuthenticationSystem authenticationSystem;
|
||||||
|
@Getter @Setter private static PermissionHandler permissionHandler;
|
||||||
private static LineReader consoleLineReader = null;
|
|
||||||
|
private static LineReader consoleLineReader = null;
|
||||||
static {
|
|
||||||
// Declare logback configuration.
|
static {
|
||||||
System.setProperty("logback.configurationFile", "src/main/resources/logback.xml");
|
// Declare logback configuration.
|
||||||
|
System.setProperty("logback.configurationFile", "src/main/resources/logback.xml");
|
||||||
// Disable the MongoDB logger.
|
|
||||||
var mongoLogger = (Logger) LoggerFactory.getLogger("org.mongodb.driver");
|
// Disable the MongoDB logger.
|
||||||
mongoLogger.setLevel(Level.OFF);
|
var mongoLogger = (Logger) LoggerFactory.getLogger("org.mongodb.driver");
|
||||||
|
mongoLogger.setLevel(Level.OFF);
|
||||||
// Load server configuration.
|
|
||||||
Grasscutter.loadConfig();
|
// Load server configuration.
|
||||||
// Attempt to update configuration.
|
Grasscutter.loadConfig();
|
||||||
ConfigContainer.updateConfig();
|
// Attempt to update configuration.
|
||||||
|
ConfigContainer.updateConfig();
|
||||||
// Load translation files.
|
|
||||||
Grasscutter.loadLanguage();
|
// Load translation files.
|
||||||
|
Grasscutter.loadLanguage();
|
||||||
// Check server structure.
|
|
||||||
Utils.startupCheck();
|
// Check server structure.
|
||||||
}
|
Utils.startupCheck();
|
||||||
|
}
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
Crypto.loadKeys(); // Load keys from buffers.
|
public static void main(String[] args) throws Exception {
|
||||||
|
Crypto.loadKeys(); // Load keys from buffers.
|
||||||
// Parse start-up arguments.
|
|
||||||
if (StartupArguments.parse(args)) {
|
// Parse start-up arguments.
|
||||||
System.exit(0); // Exit early.
|
if (StartupArguments.parse(args)) {
|
||||||
}
|
System.exit(0); // Exit early.
|
||||||
|
}
|
||||||
// Create command map.
|
|
||||||
commandMap = new CommandMap(true);
|
// Create command map.
|
||||||
|
commandMap = new CommandMap(true);
|
||||||
// Initialize server.
|
|
||||||
logger.info(translate("messages.status.starting"));
|
// Initialize server.
|
||||||
logger.info(translate("messages.status.game_version", GameConstants.VERSION));
|
logger.info(translate("messages.status.starting"));
|
||||||
logger.info(translate("messages.status.version", BuildConfig.VERSION, BuildConfig.GIT_HASH));
|
logger.info(translate("messages.status.game_version", GameConstants.VERSION));
|
||||||
|
logger.info(translate("messages.status.version", BuildConfig.VERSION, BuildConfig.GIT_HASH));
|
||||||
// Load all resources.
|
|
||||||
Grasscutter.updateDayOfWeek();
|
// Load all resources.
|
||||||
ResourceLoader.loadAll();
|
Grasscutter.updateDayOfWeek();
|
||||||
ScriptLoader.init();
|
ResourceLoader.loadAll();
|
||||||
|
ScriptLoader.init();
|
||||||
// Generate handbooks.
|
|
||||||
Tools.createGmHandbooks(false);
|
// Generate handbooks.
|
||||||
|
Tools.createGmHandbooks(false);
|
||||||
// Initialize database.
|
|
||||||
DatabaseManager.initialize();
|
// Initialize database.
|
||||||
|
DatabaseManager.initialize();
|
||||||
// Initialize the default systems.
|
|
||||||
authenticationSystem = new DefaultAuthentication();
|
// Initialize the default systems.
|
||||||
permissionHandler = new DefaultPermissionHandler();
|
authenticationSystem = new DefaultAuthentication();
|
||||||
|
permissionHandler = new DefaultPermissionHandler();
|
||||||
// Create server instances.
|
|
||||||
httpServer = new HttpServer();
|
// Create server instances.
|
||||||
gameServer = new GameServer();
|
httpServer = new HttpServer();
|
||||||
// Create a server hook instance with both servers.
|
gameServer = new GameServer();
|
||||||
new ServerHook(gameServer, httpServer);
|
// Create a server hook instance with both servers.
|
||||||
|
new ServerHook(gameServer, httpServer);
|
||||||
// Create plugin manager instance.
|
|
||||||
pluginManager = new PluginManager();
|
// Create plugin manager instance.
|
||||||
// Add HTTP routes after loading plugins.
|
pluginManager = new PluginManager();
|
||||||
httpServer.addRouter(HttpServer.UnhandledRequestRouter.class);
|
// Add HTTP routes after loading plugins.
|
||||||
httpServer.addRouter(HttpServer.DefaultRequestRouter.class);
|
httpServer.addRouter(HttpServer.UnhandledRequestRouter.class);
|
||||||
httpServer.addRouter(RegionHandler.class);
|
httpServer.addRouter(HttpServer.DefaultRequestRouter.class);
|
||||||
httpServer.addRouter(LogHandler.class);
|
httpServer.addRouter(RegionHandler.class);
|
||||||
httpServer.addRouter(GenericHandler.class);
|
httpServer.addRouter(LogHandler.class);
|
||||||
httpServer.addRouter(AnnouncementsHandler.class);
|
httpServer.addRouter(GenericHandler.class);
|
||||||
httpServer.addRouter(DispatchHandler.class);
|
httpServer.addRouter(AnnouncementsHandler.class);
|
||||||
httpServer.addRouter(GachaHandler.class);
|
httpServer.addRouter(DispatchHandler.class);
|
||||||
httpServer.addRouter(DocumentationServerHandler.class);
|
httpServer.addRouter(GachaHandler.class);
|
||||||
|
httpServer.addRouter(DocumentationServerHandler.class);
|
||||||
// Start servers.
|
httpServer.addRouter(HandbookHandler.class);
|
||||||
var runMode = Grasscutter.getRunMode();
|
|
||||||
if (runMode == ServerRunMode.HYBRID) {
|
// Start servers.
|
||||||
httpServer.start();
|
var runMode = Grasscutter.getRunMode();
|
||||||
gameServer.start();
|
if (runMode == ServerRunMode.HYBRID) {
|
||||||
} else if (runMode == ServerRunMode.DISPATCH_ONLY) {
|
httpServer.start();
|
||||||
httpServer.start();
|
gameServer.start();
|
||||||
} else if (runMode == ServerRunMode.GAME_ONLY) {
|
} else if (runMode == ServerRunMode.DISPATCH_ONLY) {
|
||||||
gameServer.start();
|
httpServer.start();
|
||||||
} else {
|
} else if (runMode == ServerRunMode.GAME_ONLY) {
|
||||||
logger.error(translate("messages.status.run_mode_error", runMode));
|
gameServer.start();
|
||||||
logger.error(translate("messages.status.run_mode_help"));
|
} else {
|
||||||
logger.error(translate("messages.status.shutdown"));
|
logger.error(translate("messages.status.run_mode_error", runMode));
|
||||||
System.exit(1);
|
logger.error(translate("messages.status.run_mode_help"));
|
||||||
}
|
logger.error(translate("messages.status.shutdown"));
|
||||||
|
System.exit(1);
|
||||||
// Enable all plugins.
|
}
|
||||||
pluginManager.enablePlugins();
|
|
||||||
|
// Enable all plugins.
|
||||||
// Hook into shutdown event.
|
pluginManager.enablePlugins();
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
|
|
||||||
|
// Hook into shutdown event.
|
||||||
// Open console.
|
Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
|
||||||
Grasscutter.startConsole();
|
|
||||||
}
|
// Open console.
|
||||||
|
Grasscutter.startConsole();
|
||||||
/** Server shutdown event. */
|
}
|
||||||
private static void onShutdown() {
|
|
||||||
// Disable all plugins.
|
/** Server shutdown event. */
|
||||||
if (pluginManager != null) pluginManager.disablePlugins();
|
private static void onShutdown() {
|
||||||
}
|
// Disable all plugins.
|
||||||
|
if (pluginManager != null) pluginManager.disablePlugins();
|
||||||
/*
|
}
|
||||||
* Methods for the language system component.
|
|
||||||
*/
|
/*
|
||||||
|
* Methods for the language system component.
|
||||||
public static void loadLanguage() {
|
*/
|
||||||
var locale = config.language.language;
|
|
||||||
language = Language.getLanguage(Utils.getLanguageCode(locale));
|
public static void loadLanguage() {
|
||||||
}
|
var locale = config.language.language;
|
||||||
|
language = Language.getLanguage(Utils.getLanguageCode(locale));
|
||||||
/*
|
}
|
||||||
* Methods for the configuration system component.
|
|
||||||
*/
|
/*
|
||||||
|
* Methods for the configuration system component.
|
||||||
/** Attempts to load the configuration from a file. */
|
*/
|
||||||
public static void loadConfig() {
|
|
||||||
// Check if config.json exists. If not, we generate a new config.
|
/** Attempts to load the configuration from a file. */
|
||||||
if (!configFile.exists()) {
|
public static void loadConfig() {
|
||||||
getLogger().info("config.json could not be found. Generating a default configuration ...");
|
// Check if config.json exists. If not, we generate a new config.
|
||||||
config = new ConfigContainer();
|
if (!configFile.exists()) {
|
||||||
Grasscutter.saveConfig(config);
|
getLogger().info("config.json could not be found. Generating a default configuration ...");
|
||||||
return;
|
config = new ConfigContainer();
|
||||||
}
|
Grasscutter.saveConfig(config);
|
||||||
|
return;
|
||||||
// If the file already exists, we attempt to load it.
|
}
|
||||||
try {
|
|
||||||
config = JsonUtils.loadToClass(configFile.toPath(), ConfigContainer.class);
|
// If the file already exists, we attempt to load it.
|
||||||
} catch (Exception exception) {
|
try {
|
||||||
getLogger()
|
config = JsonUtils.loadToClass(configFile.toPath(), ConfigContainer.class);
|
||||||
.error(
|
} catch (Exception exception) {
|
||||||
"There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json.");
|
getLogger()
|
||||||
System.exit(1);
|
.error(
|
||||||
}
|
"There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json.");
|
||||||
}
|
System.exit(1);
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* Saves the provided server configuration.
|
|
||||||
*
|
/**
|
||||||
* @param config The configuration to save, or null for a new one.
|
* Saves the provided server configuration.
|
||||||
*/
|
*
|
||||||
public static void saveConfig(@Nullable ConfigContainer config) {
|
* @param config The configuration to save, or null for a new one.
|
||||||
if (config == null) config = new ConfigContainer();
|
*/
|
||||||
|
public static void saveConfig(@Nullable ConfigContainer config) {
|
||||||
try (FileWriter file = new FileWriter(configFile)) {
|
if (config == null) config = new ConfigContainer();
|
||||||
file.write(JsonUtils.encode(config));
|
|
||||||
} catch (IOException ignored) {
|
try (FileWriter file = new FileWriter(configFile)) {
|
||||||
logger.error("Unable to write to config file.");
|
file.write(JsonUtils.encode(config));
|
||||||
} catch (Exception e) {
|
} catch (IOException ignored) {
|
||||||
logger.error("Unable to save config file.", e);
|
logger.error("Unable to write to config file.");
|
||||||
}
|
} catch (Exception e) {
|
||||||
}
|
logger.error("Unable to save config file.", e);
|
||||||
|
}
|
||||||
/*
|
}
|
||||||
* Getters for the various server components.
|
|
||||||
*/
|
/*
|
||||||
|
* Getters for the various server components.
|
||||||
public static Language getLanguage(String langCode) {
|
*/
|
||||||
return Language.getLanguage(langCode);
|
|
||||||
}
|
public static Language getLanguage(String langCode) {
|
||||||
|
return Language.getLanguage(langCode);
|
||||||
public static ServerRunMode getRunMode() {
|
}
|
||||||
return Grasscutter.runModeOverride != null ? Grasscutter.runModeOverride : SERVER.runMode;
|
|
||||||
}
|
public static ServerRunMode getRunMode() {
|
||||||
|
return Grasscutter.runModeOverride != null ? Grasscutter.runModeOverride : SERVER.runMode;
|
||||||
public static LineReader getConsole() {
|
}
|
||||||
if (consoleLineReader == null) {
|
|
||||||
Terminal terminal = null;
|
public static LineReader getConsole() {
|
||||||
try {
|
if (consoleLineReader == null) {
|
||||||
terminal = TerminalBuilder.builder().jna(true).build();
|
Terminal terminal = null;
|
||||||
} catch (Exception e) {
|
try {
|
||||||
try {
|
terminal = TerminalBuilder.builder().jna(true).build();
|
||||||
// Fallback to a dumb jline terminal.
|
} catch (Exception e) {
|
||||||
terminal = TerminalBuilder.builder().dumb(true).build();
|
try {
|
||||||
} catch (Exception ignored) {
|
// Fallback to a dumb jline terminal.
|
||||||
// When dumb is true, build() never throws.
|
terminal = TerminalBuilder.builder().dumb(true).build();
|
||||||
}
|
} catch (Exception ignored) {
|
||||||
}
|
// When dumb is true, build() never throws.
|
||||||
|
}
|
||||||
consoleLineReader = LineReaderBuilder.builder().terminal(terminal).build();
|
}
|
||||||
}
|
|
||||||
|
consoleLineReader = LineReaderBuilder.builder().terminal(terminal).build();
|
||||||
return consoleLineReader;
|
}
|
||||||
}
|
|
||||||
|
return consoleLineReader;
|
||||||
/*
|
}
|
||||||
* Utility methods.
|
|
||||||
*/
|
/*
|
||||||
|
* Utility methods.
|
||||||
public static void updateDayOfWeek() {
|
*/
|
||||||
Calendar calendar = Calendar.getInstance();
|
|
||||||
Grasscutter.currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
|
public static void updateDayOfWeek() {
|
||||||
logger.debug("Set day of week to " + currentDayOfWeek);
|
Calendar calendar = Calendar.getInstance();
|
||||||
}
|
Grasscutter.currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
|
||||||
|
logger.debug("Set day of week to " + currentDayOfWeek);
|
||||||
public static void startConsole() {
|
}
|
||||||
// Console should not start in dispatch only mode.
|
|
||||||
if (SERVER.runMode == ServerRunMode.DISPATCH_ONLY) {
|
public static void startConsole() {
|
||||||
logger.info(translate("messages.dispatch.no_commands_error"));
|
// Console should not start in dispatch only mode.
|
||||||
return;
|
if (SERVER.runMode == ServerRunMode.DISPATCH_ONLY) {
|
||||||
} else {
|
logger.info(translate("messages.dispatch.no_commands_error"));
|
||||||
logger.info(translate("messages.status.done"));
|
return;
|
||||||
}
|
} else {
|
||||||
|
logger.info(translate("messages.status.done"));
|
||||||
String input = null;
|
}
|
||||||
var isLastInterrupted = false;
|
|
||||||
while (config.server.game.enableConsole) {
|
String input = null;
|
||||||
try {
|
var isLastInterrupted = false;
|
||||||
input = consoleLineReader.readLine("> ");
|
while (config.server.game.enableConsole) {
|
||||||
} catch (UserInterruptException e) {
|
try {
|
||||||
if (!isLastInterrupted) {
|
input = consoleLineReader.readLine("> ");
|
||||||
isLastInterrupted = true;
|
} catch (UserInterruptException e) {
|
||||||
logger.info("Press Ctrl-C again to shutdown.");
|
if (!isLastInterrupted) {
|
||||||
continue;
|
isLastInterrupted = true;
|
||||||
} else {
|
logger.info("Press Ctrl-C again to shutdown.");
|
||||||
Runtime.getRuntime().exit(0);
|
continue;
|
||||||
}
|
} else {
|
||||||
} catch (EndOfFileException e) {
|
Runtime.getRuntime().exit(0);
|
||||||
logger.info("EOF detected.");
|
}
|
||||||
continue;
|
} catch (EndOfFileException e) {
|
||||||
} catch (IOError e) {
|
logger.info("EOF detected.");
|
||||||
logger.error("An IO error occurred while trying to read from console.", e);
|
continue;
|
||||||
return;
|
} catch (IOError e) {
|
||||||
}
|
logger.error("An IO error occurred while trying to read from console.", e);
|
||||||
|
return;
|
||||||
isLastInterrupted = false;
|
}
|
||||||
|
|
||||||
try {
|
isLastInterrupted = false;
|
||||||
commandMap.invoke(null, null, input);
|
|
||||||
} catch (Exception e) {
|
try {
|
||||||
logger.error(translate("messages.game.command_error"), e);
|
commandMap.invoke(null, null, input);
|
||||||
}
|
} catch (Exception e) {
|
||||||
}
|
logger.error(translate("messages.game.command_error"), e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/*
|
}
|
||||||
* Enums for the configuration.
|
|
||||||
*/
|
/*
|
||||||
|
* Enums for the configuration.
|
||||||
public enum ServerRunMode {
|
*/
|
||||||
HYBRID,
|
|
||||||
DISPATCH_ONLY,
|
public enum ServerRunMode {
|
||||||
GAME_ONLY
|
HYBRID,
|
||||||
}
|
DISPATCH_ONLY,
|
||||||
|
GAME_ONLY
|
||||||
public enum ServerDebugMode {
|
}
|
||||||
ALL,
|
|
||||||
MISSING,
|
public enum ServerDebugMode {
|
||||||
WHITELIST,
|
ALL,
|
||||||
BLACKLIST,
|
MISSING,
|
||||||
NONE
|
WHITELIST,
|
||||||
}
|
BLACKLIST,
|
||||||
}
|
NONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -227,6 +227,8 @@ public class ConfigContainer {
|
|||||||
public ResinOptions resinOptions = new ResinOptions();
|
public ResinOptions resinOptions = new ResinOptions();
|
||||||
public Rates rates = new Rates();
|
public Rates rates = new Rates();
|
||||||
|
|
||||||
|
public HandbookOptions handbook = new HandbookOptions();
|
||||||
|
|
||||||
public static class InventoryLimits {
|
public static class InventoryLimits {
|
||||||
public int weapons = 2000;
|
public int weapons = 2000;
|
||||||
public int relics = 2000;
|
public int relics = 2000;
|
||||||
@ -251,6 +253,13 @@ public class ConfigContainer {
|
|||||||
public int cap = 160;
|
public int cap = 160;
|
||||||
public int rechargeTime = 480;
|
public int rechargeTime = 480;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class HandbookOptions {
|
||||||
|
public boolean enable = false;
|
||||||
|
public boolean allowCommands = true;
|
||||||
|
public int maxRequests = 10;
|
||||||
|
public int maxEntities = 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class JoinOptions {
|
public static class JoinOptions {
|
||||||
|
@ -0,0 +1,110 @@
|
|||||||
|
package emu.grasscutter.server.http.documentation;
|
||||||
|
|
||||||
|
import emu.grasscutter.Grasscutter;
|
||||||
|
import emu.grasscutter.Grasscutter.ServerRunMode;
|
||||||
|
import emu.grasscutter.data.GameData;
|
||||||
|
import emu.grasscutter.game.avatar.Avatar;
|
||||||
|
import emu.grasscutter.server.http.Router;
|
||||||
|
import emu.grasscutter.utils.FileUtils;
|
||||||
|
import emu.grasscutter.utils.objects.HandbookBody;
|
||||||
|
import io.javalin.Javalin;
|
||||||
|
import io.javalin.http.Context;
|
||||||
|
|
||||||
|
/** Handles requests for the new GM Handbook. */
|
||||||
|
public final class HandbookHandler implements Router {
|
||||||
|
private final byte[] handbook;
|
||||||
|
private final boolean serve;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for the handbook router.
|
||||||
|
* Enables serving the handbook if the handbook file is found.
|
||||||
|
*/
|
||||||
|
public HandbookHandler() {
|
||||||
|
this.handbook = FileUtils.readResource("/handbook.html");
|
||||||
|
this.serve = this.handbook.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyRoutes(Javalin javalin) {
|
||||||
|
// The handbook content. (built from src/handbook)
|
||||||
|
javalin.get("/handbook", this::serveHandbook);
|
||||||
|
|
||||||
|
// Handbook control routes.
|
||||||
|
javalin.post("/handbook/avatar", this::grantAvatar);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return True if the server can execute handbook commands.
|
||||||
|
*/
|
||||||
|
private boolean controlSupported() {
|
||||||
|
return Grasscutter.getRunMode() == ServerRunMode.HYBRID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serves the handbook if it is found.
|
||||||
|
*
|
||||||
|
* @route GET /handbook
|
||||||
|
* @param ctx The Javalin request context.
|
||||||
|
*/
|
||||||
|
private void serveHandbook(Context ctx) {
|
||||||
|
if (!this.serve) {
|
||||||
|
ctx.status(500).result("Handbook not found.");
|
||||||
|
} else {
|
||||||
|
ctx.contentType("text/html").result(this.handbook);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Grants the avatar to the user.
|
||||||
|
*
|
||||||
|
* @route POST /handbook/avatar
|
||||||
|
* @param ctx The Javalin request context.
|
||||||
|
*/
|
||||||
|
private void grantAvatar(Context ctx) {
|
||||||
|
if (!this.controlSupported()) {
|
||||||
|
ctx.status(500).result("Handbook control not supported.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the request body into a class.
|
||||||
|
var request = ctx.bodyAsClass(HandbookBody.GrantAvatar.class);
|
||||||
|
// Validate the request.
|
||||||
|
if (request.getPlayer() == null || request.getAvatar() == null) {
|
||||||
|
ctx.status(400).result("Invalid request.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Parse the requested player.
|
||||||
|
var playerId = Integer.parseInt(request.getPlayer());
|
||||||
|
var player = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
||||||
|
|
||||||
|
// Parse the requested avatar.
|
||||||
|
var avatarId = Integer.parseInt(request.getAvatar());
|
||||||
|
var avatarData = GameData.getAvatarDataMap().get(avatarId);
|
||||||
|
|
||||||
|
// Validate the request.
|
||||||
|
if (player == null || avatarData == null) {
|
||||||
|
ctx.status(400).result("Invalid player UID or avatar ID.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the new avatar.
|
||||||
|
var avatar = new Avatar(avatarData);
|
||||||
|
avatar.setLevel(request.getLevel());
|
||||||
|
avatar.setPromoteLevel(Avatar.getMinPromoteLevel(avatar.getLevel()));
|
||||||
|
avatar.getSkillDepot().getSkillsAndEnergySkill().forEach(id ->
|
||||||
|
avatar.setSkillLevel(id, request.getTalentLevels()));
|
||||||
|
avatar.forceConstellationLevel(request.getConstellations());
|
||||||
|
avatar.recalcStats(true); avatar.save();
|
||||||
|
|
||||||
|
player.addAvatar(avatar); // Add the avatar.
|
||||||
|
ctx.json(HandbookBody.Response.builder()
|
||||||
|
.status(200)
|
||||||
|
.message("Avatar granted.")
|
||||||
|
.build());
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
ctx.status(500).result("Invalid player UID or avatar ID.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,265 +1,265 @@
|
|||||||
package emu.grasscutter.utils;
|
package emu.grasscutter.utils;
|
||||||
|
|
||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.FileSystem;
|
import java.nio.file.FileSystem;
|
||||||
import java.nio.file.FileSystems;
|
import java.nio.file.FileSystems;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
|
|
||||||
public final class FileUtils {
|
public final class FileUtils {
|
||||||
private static final Path DATA_DEFAULT_PATH;
|
private static final Path DATA_DEFAULT_PATH;
|
||||||
private static final Path DATA_USER_PATH = Path.of(Grasscutter.config.folderStructure.data);
|
private static final Path DATA_USER_PATH = Path.of(Grasscutter.config.folderStructure.data);
|
||||||
private static final Path PACKETS_PATH = Path.of(Grasscutter.config.folderStructure.packets);
|
private static final Path PACKETS_PATH = Path.of(Grasscutter.config.folderStructure.packets);
|
||||||
private static final Path PLUGINS_PATH = Path.of(Grasscutter.config.folderStructure.plugins);
|
private static final Path PLUGINS_PATH = Path.of(Grasscutter.config.folderStructure.plugins);
|
||||||
private static final Path RESOURCES_PATH;
|
private static final Path RESOURCES_PATH;
|
||||||
private static final Path SCRIPTS_PATH;
|
private static final Path SCRIPTS_PATH;
|
||||||
private static final String[] TSJ_JSON_TSV = {"tsj", "json", "tsv"};
|
private static final String[] TSJ_JSON_TSV = {"tsj", "json", "tsv"};
|
||||||
|
|
||||||
static {
|
static {
|
||||||
FileSystem fs = null;
|
FileSystem fs = null;
|
||||||
Path path = null;
|
Path path = null;
|
||||||
// Setup access to jar resources
|
// Setup access to jar resources
|
||||||
try {
|
try {
|
||||||
var uri = Grasscutter.class.getResource("/defaults/data").toURI();
|
var uri = Grasscutter.class.getResource("/defaults/data").toURI();
|
||||||
switch (uri.getScheme()) {
|
switch (uri.getScheme()) {
|
||||||
case "jar": // When running normally, as a jar
|
case "jar": // When running normally, as a jar
|
||||||
case "zip": // Honestly I have no idea what setup would result in this, but this should work
|
case "zip": // Honestly I have no idea what setup would result in this, but this should work
|
||||||
// regardless
|
// regardless
|
||||||
fs =
|
fs =
|
||||||
FileSystems.newFileSystem(
|
FileSystems.newFileSystem(
|
||||||
uri,
|
uri,
|
||||||
Map.of()); // Have to mount zip filesystem. This leaks, but we want to keep it
|
Map.of()); // Have to mount zip filesystem. This leaks, but we want to keep it
|
||||||
// forever anyway.
|
// forever anyway.
|
||||||
// Fall-through
|
// Fall-through
|
||||||
case "file": // When running in an IDE
|
case "file": // When running in an IDE
|
||||||
path = Path.of(uri); // Can access directly
|
path = Path.of(uri); // Can access directly
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Grasscutter.getLogger()
|
Grasscutter.getLogger()
|
||||||
.error("Invalid URI scheme for class resources: " + uri.getScheme());
|
.error("Invalid URI scheme for class resources: " + uri.getScheme());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (URISyntaxException | IOException e) {
|
} catch (URISyntaxException | IOException e) {
|
||||||
// Failed to load this jar. How?
|
// Failed to load this jar. How?
|
||||||
Grasscutter.getLogger().error("Failed to load jar?!");
|
Grasscutter.getLogger().error("Failed to load jar?!");
|
||||||
} finally {
|
} finally {
|
||||||
DATA_DEFAULT_PATH = path;
|
DATA_DEFAULT_PATH = path;
|
||||||
Grasscutter.getLogger().debug("Setting path for default data: " + path.toAbsolutePath());
|
Grasscutter.getLogger().debug("Setting path for default data: " + path.toAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup Resources path
|
// Setup Resources path
|
||||||
final String resources = Grasscutter.config.folderStructure.resources;
|
final String resources = Grasscutter.config.folderStructure.resources;
|
||||||
fs = null;
|
fs = null;
|
||||||
path = Path.of(resources);
|
path = Path.of(resources);
|
||||||
if (resources.endsWith(
|
if (resources.endsWith(
|
||||||
".zip")) { // Would be nice to support .tar.gz too at some point, but it doesn't come for
|
".zip")) { // Would be nice to support .tar.gz too at some point, but it doesn't come for
|
||||||
// free in Java
|
// free in Java
|
||||||
try {
|
try {
|
||||||
fs = FileSystems.newFileSystem(path);
|
fs = FileSystems.newFileSystem(path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Grasscutter.getLogger().error("Failed to load resources zip \"" + resources + "\"");
|
Grasscutter.getLogger().error("Failed to load resources zip \"" + resources + "\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fs != null) {
|
if (fs != null) {
|
||||||
var root = fs.getPath("");
|
var root = fs.getPath("");
|
||||||
try (Stream<Path> pathStream =
|
try (Stream<Path> pathStream =
|
||||||
Files.find(
|
Files.find(
|
||||||
root,
|
root,
|
||||||
3,
|
3,
|
||||||
(p, a) -> {
|
(p, a) -> {
|
||||||
var filename = p.getFileName();
|
var filename = p.getFileName();
|
||||||
if (filename == null) return false;
|
if (filename == null) return false;
|
||||||
return filename.toString().equals("ExcelBinOutput");
|
return filename.toString().equals("ExcelBinOutput");
|
||||||
})) {
|
})) {
|
||||||
var excelBinOutput = pathStream.findFirst();
|
var excelBinOutput = pathStream.findFirst();
|
||||||
if (excelBinOutput.isPresent()) {
|
if (excelBinOutput.isPresent()) {
|
||||||
path = excelBinOutput.get().getParent();
|
path = excelBinOutput.get().getParent();
|
||||||
if (path == null) path = root;
|
if (path == null) path = root;
|
||||||
Grasscutter.getLogger()
|
Grasscutter.getLogger()
|
||||||
.debug("Resources will be loaded from \"" + resources + "/" + path + "\"");
|
.debug("Resources will be loaded from \"" + resources + "/" + path + "\"");
|
||||||
} else {
|
} else {
|
||||||
Grasscutter.getLogger()
|
Grasscutter.getLogger()
|
||||||
.error("Failed to find ExcelBinOutput in resources zip \"" + resources + "\"");
|
.error("Failed to find ExcelBinOutput in resources zip \"" + resources + "\"");
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Grasscutter.getLogger().error("Failed to scan resources zip \"" + resources + "\"");
|
Grasscutter.getLogger().error("Failed to scan resources zip \"" + resources + "\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RESOURCES_PATH = path;
|
RESOURCES_PATH = path;
|
||||||
|
|
||||||
// Setup Scripts path
|
// Setup Scripts path
|
||||||
final String scripts = Grasscutter.config.folderStructure.scripts;
|
final String scripts = Grasscutter.config.folderStructure.scripts;
|
||||||
SCRIPTS_PATH =
|
SCRIPTS_PATH =
|
||||||
(scripts.startsWith("resources:"))
|
(scripts.startsWith("resources:"))
|
||||||
? RESOURCES_PATH.resolve(scripts.substring("resources:".length()))
|
? RESOURCES_PATH.resolve(scripts.substring("resources:".length()))
|
||||||
: Path.of(scripts);
|
: Path.of(scripts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Apply after initialization. */
|
/* Apply after initialization. */
|
||||||
private static final Path[] DATA_PATHS = {DATA_USER_PATH, DATA_DEFAULT_PATH};
|
private static final Path[] DATA_PATHS = {DATA_USER_PATH, DATA_DEFAULT_PATH};
|
||||||
|
|
||||||
public static Path getDataPathTsjJsonTsv(String filename) {
|
public static Path getDataPathTsjJsonTsv(String filename) {
|
||||||
return getDataPathTsjJsonTsv(filename, true);
|
return getDataPathTsjJsonTsv(filename, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getDataPathTsjJsonTsv(String filename, boolean fallback) {
|
public static Path getDataPathTsjJsonTsv(String filename, boolean fallback) {
|
||||||
val name = getFilenameWithoutExtension(filename);
|
val name = getFilenameWithoutExtension(filename);
|
||||||
for (val data_path : DATA_PATHS) {
|
for (val data_path : DATA_PATHS) {
|
||||||
for (val ext : TSJ_JSON_TSV) {
|
for (val ext : TSJ_JSON_TSV) {
|
||||||
val path = data_path.resolve(name + "." + ext);
|
val path = data_path.resolve(name + "." + ext);
|
||||||
if (Files.exists(path)) return path;
|
if (Files.exists(path)) return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fallback
|
return fallback
|
||||||
? DATA_USER_PATH.resolve(name + ".tsj")
|
? DATA_USER_PATH.resolve(name + ".tsj")
|
||||||
: null; // Maybe they want to write to a new file
|
: null; // Maybe they want to write to a new file
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getDataPath(String path) {
|
public static Path getDataPath(String path) {
|
||||||
Path userPath = DATA_USER_PATH.resolve(path);
|
Path userPath = DATA_USER_PATH.resolve(path);
|
||||||
if (Files.exists(userPath)) return userPath;
|
if (Files.exists(userPath)) return userPath;
|
||||||
Path defaultPath = DATA_DEFAULT_PATH.resolve(path);
|
Path defaultPath = DATA_DEFAULT_PATH.resolve(path);
|
||||||
if (Files.exists(defaultPath)) return defaultPath;
|
if (Files.exists(defaultPath)) return defaultPath;
|
||||||
return userPath; // Maybe they want to write to a new file
|
return userPath; // Maybe they want to write to a new file
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getDataUserPath(String path) {
|
public static Path getDataUserPath(String path) {
|
||||||
return DATA_USER_PATH.resolve(path);
|
return DATA_USER_PATH.resolve(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getPacketPath(String path) {
|
public static Path getPacketPath(String path) {
|
||||||
return PACKETS_PATH.resolve(path);
|
return PACKETS_PATH.resolve(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getPluginPath(String path) {
|
public static Path getPluginPath(String path) {
|
||||||
return PLUGINS_PATH.resolve(path);
|
return PLUGINS_PATH.resolve(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getResourcePath(String path) {
|
public static Path getResourcePath(String path) {
|
||||||
return RESOURCES_PATH.resolve(path);
|
return RESOURCES_PATH.resolve(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getExcelPath(String filename) {
|
public static Path getExcelPath(String filename) {
|
||||||
return getTsjJsonTsv(RESOURCES_PATH.resolve("ExcelBinOutput"), filename);
|
return getTsjJsonTsv(RESOURCES_PATH.resolve("ExcelBinOutput"), filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets path of a resource.
|
// Gets path of a resource.
|
||||||
// If multiple formats of it exist, priority is TSJ > JSON > TSV
|
// If multiple formats of it exist, priority is TSJ > JSON > TSV
|
||||||
// If none exist, return the TSJ path, in case it wants to create a file
|
// If none exist, return the TSJ path, in case it wants to create a file
|
||||||
public static Path getTsjJsonTsv(Path root, String filename) {
|
public static Path getTsjJsonTsv(Path root, String filename) {
|
||||||
val name = getFilenameWithoutExtension(filename);
|
val name = getFilenameWithoutExtension(filename);
|
||||||
for (val ext : TSJ_JSON_TSV) {
|
for (val ext : TSJ_JSON_TSV) {
|
||||||
val path = root.resolve(name + "." + ext);
|
val path = root.resolve(name + "." + ext);
|
||||||
if (Files.exists(path)) return path;
|
if (Files.exists(path)) return path;
|
||||||
}
|
}
|
||||||
return root.resolve(name + ".tsj");
|
return root.resolve(name + ".tsj");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getScriptPath(String path) {
|
public static Path getScriptPath(String path) {
|
||||||
return SCRIPTS_PATH.resolve(path);
|
return SCRIPTS_PATH.resolve(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void write(String dest, byte[] bytes) {
|
public static void write(String dest, byte[] bytes) {
|
||||||
Path path = Path.of(dest);
|
Path path = Path.of(dest);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Files.write(path, bytes);
|
Files.write(path, bytes);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Grasscutter.getLogger().warn("Failed to write file: " + dest);
|
Grasscutter.getLogger().warn("Failed to write file: " + dest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] read(String dest) {
|
public static byte[] read(String dest) {
|
||||||
return read(Path.of(dest));
|
return read(Path.of(dest));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] read(Path path) {
|
public static byte[] read(Path path) {
|
||||||
try {
|
try {
|
||||||
return Files.readAllBytes(path);
|
return Files.readAllBytes(path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Grasscutter.getLogger().warn("Failed to read file: " + path);
|
Grasscutter.getLogger().warn("Failed to read file: " + path);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new byte[0];
|
return new byte[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static InputStream readResourceAsStream(String resourcePath) {
|
public static InputStream readResourceAsStream(String resourcePath) {
|
||||||
return Grasscutter.class.getResourceAsStream(resourcePath);
|
return Grasscutter.class.getResourceAsStream(resourcePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] readResource(String resourcePath) {
|
public static byte[] readResource(String resourcePath) {
|
||||||
try (InputStream is = Grasscutter.class.getResourceAsStream(resourcePath)) {
|
try (InputStream is = Grasscutter.class.getResourceAsStream(resourcePath)) {
|
||||||
return is.readAllBytes();
|
return is.readAllBytes();
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
Grasscutter.getLogger().warn("Failed to read resource: " + resourcePath);
|
Grasscutter.getLogger().warn("Failed to read resource: " + resourcePath);
|
||||||
exception.printStackTrace();
|
Grasscutter.getLogger().debug("Failed to load resource: " + resourcePath, exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new byte[0];
|
return new byte[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] read(File file) {
|
public static byte[] read(File file) {
|
||||||
return read(file.getPath());
|
return read(file.getPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void copyResource(String resourcePath, String destination) {
|
public static void copyResource(String resourcePath, String destination) {
|
||||||
try {
|
try {
|
||||||
byte[] resource = FileUtils.readResource(resourcePath);
|
byte[] resource = FileUtils.readResource(resourcePath);
|
||||||
FileUtils.write(destination, resource);
|
FileUtils.write(destination, resource);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
Grasscutter.getLogger().warn("Failed to copy resource: " + resourcePath + "\n" + exception);
|
Grasscutter.getLogger().warn("Failed to copy resource: " + resourcePath + "\n" + exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated // Misnamed legacy function
|
@Deprecated // Misnamed legacy function
|
||||||
public static String getFilenameWithoutPath(String filename) {
|
public static String getFilenameWithoutPath(String filename) {
|
||||||
return getFilenameWithoutExtension(filename);
|
return getFilenameWithoutExtension(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getFilenameWithoutExtension(String filename) {
|
public static String getFilenameWithoutExtension(String filename) {
|
||||||
int i = filename.lastIndexOf(".");
|
int i = filename.lastIndexOf(".");
|
||||||
return (i < 0) ? filename : filename.substring(0, i);
|
return (i < 0) ? filename : filename.substring(0, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getFileExtension(Path path) {
|
public static String getFileExtension(Path path) {
|
||||||
val filename = path.toString();
|
val filename = path.toString();
|
||||||
int i = filename.lastIndexOf(".");
|
int i = filename.lastIndexOf(".");
|
||||||
return (i < 0) ? "" : filename.substring(i + 1);
|
return (i < 0) ? "" : filename.substring(i + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Path> getPathsFromResource(String folder) throws URISyntaxException {
|
public static List<Path> getPathsFromResource(String folder) throws URISyntaxException {
|
||||||
try {
|
try {
|
||||||
// file walks JAR
|
// file walks JAR
|
||||||
return Files.walk(Path.of(Grasscutter.class.getResource(folder).toURI()))
|
return Files.walk(Path.of(Grasscutter.class.getResource(folder).toURI()))
|
||||||
.filter(Files::isRegularFile)
|
.filter(Files::isRegularFile)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Eclipse puts resources in its bin folder
|
// Eclipse puts resources in its bin folder
|
||||||
try {
|
try {
|
||||||
return Files.walk(Path.of(System.getProperty("user.dir"), folder))
|
return Files.walk(Path.of(System.getProperty("user.dir"), folder))
|
||||||
.filter(Files::isRegularFile)
|
.filter(Files::isRegularFile)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
} catch (IOException ignored) {
|
} catch (IOException ignored) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
public static String readToString(InputStream file) throws IOException {
|
public static String readToString(InputStream file) throws IOException {
|
||||||
byte[] content = file.readAllBytes();
|
byte[] content = file.readAllBytes();
|
||||||
|
|
||||||
return new String(content, StandardCharsets.UTF_8);
|
return new String(content, StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package emu.grasscutter.utils.objects;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/** HTTP request object for handbook controls. */
|
||||||
|
public interface HandbookBody {
|
||||||
|
@Builder
|
||||||
|
class Response {
|
||||||
|
private int status;
|
||||||
|
private String message;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
class GrantAvatar {
|
||||||
|
private String player; // Parse into online player ID.
|
||||||
|
private String avatar; // Parse into avatar ID.
|
||||||
|
|
||||||
|
private int level = 90; // Range between 1 - 90.
|
||||||
|
private int constellations = 6; // Range between 0 - 6.
|
||||||
|
private int talentLevels = 10; // Range between 1 - 15.
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user