mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-24 04:48:05 +00:00
Plugin enabling & disabling
This commit is contained in:
parent
ec6a54ebd7
commit
2a6984229e
@ -8,6 +8,7 @@ import java.io.InputStreamReader;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
import emu.grasscutter.command.CommandMap;
|
import emu.grasscutter.command.CommandMap;
|
||||||
|
import emu.grasscutter.plugin.PluginManager;
|
||||||
import emu.grasscutter.utils.Utils;
|
import emu.grasscutter.utils.Utils;
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -33,6 +34,7 @@ public final class Grasscutter {
|
|||||||
public static RunMode MODE = RunMode.BOTH;
|
public static RunMode MODE = RunMode.BOTH;
|
||||||
private static DispatchServer dispatchServer;
|
private static DispatchServer dispatchServer;
|
||||||
private static GameServer gameServer;
|
private static GameServer gameServer;
|
||||||
|
private static PluginManager pluginManager;
|
||||||
|
|
||||||
public static final Reflections reflector = new Reflections();
|
public static final Reflections reflector = new Reflections();
|
||||||
|
|
||||||
@ -52,15 +54,11 @@ public final class Grasscutter {
|
|||||||
|
|
||||||
for (String arg : args) {
|
for (String arg : args) {
|
||||||
switch (arg.toLowerCase()) {
|
switch (arg.toLowerCase()) {
|
||||||
case "-auth":
|
case "-auth" -> MODE = RunMode.AUTH;
|
||||||
MODE = RunMode.AUTH;
|
case "-game" -> MODE = RunMode.GAME;
|
||||||
break;
|
case "-handbook" -> {
|
||||||
case "-game":
|
Tools.createGmHandbook(); return;
|
||||||
MODE = RunMode.GAME;
|
}
|
||||||
break;
|
|
||||||
case "-handbook":
|
|
||||||
Tools.createGmHandbook();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,18 +70,20 @@ public final class Grasscutter {
|
|||||||
// Database
|
// Database
|
||||||
DatabaseManager.initialize();
|
DatabaseManager.initialize();
|
||||||
|
|
||||||
|
// Create server instances.
|
||||||
|
dispatchServer = new DispatchServer();
|
||||||
|
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
|
||||||
|
|
||||||
|
// Create plugin manager instance.
|
||||||
|
pluginManager = new PluginManager();
|
||||||
|
|
||||||
// Start servers.
|
// Start servers.
|
||||||
if(getConfig().RunMode.equalsIgnoreCase("HYBRID")) {
|
if(getConfig().RunMode.equalsIgnoreCase("HYBRID")) {
|
||||||
dispatchServer = new DispatchServer();
|
|
||||||
dispatchServer.start();
|
dispatchServer.start();
|
||||||
|
|
||||||
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
|
|
||||||
gameServer.start();
|
gameServer.start();
|
||||||
} else if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
|
} else if (getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
|
||||||
dispatchServer = new DispatchServer();
|
|
||||||
dispatchServer.start();
|
dispatchServer.start();
|
||||||
} else if(getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
|
} else if (getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
|
||||||
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
|
|
||||||
gameServer.start();
|
gameServer.start();
|
||||||
} else {
|
} else {
|
||||||
getLogger().error("Invalid server run mode. " + getConfig().RunMode);
|
getLogger().error("Invalid server run mode. " + getConfig().RunMode);
|
||||||
@ -92,10 +92,21 @@ public final class Grasscutter {
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enable all plugins.
|
||||||
|
pluginManager.enablePlugins();
|
||||||
|
|
||||||
// Open console.
|
// Open console.
|
||||||
startConsole();
|
startConsole();
|
||||||
|
// Hook into shutdown event.
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server shutdown event.
|
||||||
|
*/
|
||||||
|
private static void onShutdown() {
|
||||||
|
// Disable all plugins.
|
||||||
|
pluginManager.disablePlugins();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadConfig() {
|
public static void loadConfig() {
|
||||||
@ -112,7 +123,7 @@ public final class Grasscutter {
|
|||||||
try (FileWriter file = new FileWriter(configFile)) {
|
try (FileWriter file = new FileWriter(configFile)) {
|
||||||
file.write(gson.toJson(config));
|
file.write(gson.toJson(config));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Grasscutter.getLogger().error("Config save error");
|
Grasscutter.getLogger().error("Unable to save config file.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,13 +133,13 @@ public final class Grasscutter {
|
|||||||
while ((input = br.readLine()) != null) {
|
while ((input = br.readLine()) != null) {
|
||||||
try {
|
try {
|
||||||
if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
|
if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
|
||||||
getLogger().error("Commands are not supported in dispatch only mode");
|
getLogger().error("Commands are not supported in dispatch only mode.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandMap.getInstance().invoke(null, input);
|
CommandMap.getInstance().invoke(null, input);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Grasscutter.getLogger().error("Command error: ");
|
Grasscutter.getLogger().error("Command error:", e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -161,4 +172,8 @@ public final class Grasscutter {
|
|||||||
public static GameServer getGameServer() {
|
public static GameServer getGameServer() {
|
||||||
return gameServer;
|
return gameServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static PluginManager getPluginManager() {
|
||||||
|
return pluginManager;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,22 +7,18 @@ import emu.grasscutter.server.game.GameServer;
|
|||||||
* The base class for all plugins to extend.
|
* The base class for all plugins to extend.
|
||||||
*/
|
*/
|
||||||
public abstract class Plugin {
|
public abstract class Plugin {
|
||||||
private final PluginIdentifier identifier;
|
private PluginIdentifier identifier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Empty constructor for developers.
|
* This method is reflected into.
|
||||||
* Should not be called by users.
|
*
|
||||||
*/
|
* Set plugin variables.
|
||||||
public Plugin() {
|
|
||||||
this(new PluginIdentifier("", "", "", new String[]{}));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor for plugins.
|
|
||||||
* @param identifier The plugin's identifier.
|
* @param identifier The plugin's identifier.
|
||||||
*/
|
*/
|
||||||
public Plugin(PluginIdentifier identifier) {
|
private void initializePlugin(PluginIdentifier identifier) {
|
||||||
|
if(this.identifier == null)
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
|
else Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -5,13 +5,13 @@ import emu.grasscutter.utils.Utils;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages the server's plugins & the event system.
|
* Manages the server's plugins & the event system.
|
||||||
@ -42,7 +42,7 @@ public final class PluginManager {
|
|||||||
|
|
||||||
List<File> plugins = Arrays.stream(files)
|
List<File> plugins = Arrays.stream(files)
|
||||||
.filter(file -> file.getName().endsWith(".jar"))
|
.filter(file -> file.getName().endsWith(".jar"))
|
||||||
.collect(Collectors.toList());
|
.toList();
|
||||||
|
|
||||||
plugins.forEach(plugin -> {
|
plugins.forEach(plugin -> {
|
||||||
try {
|
try {
|
||||||
@ -59,9 +59,8 @@ public final class PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Class<?> pluginClass = loader.loadClass(pluginConfig.mainClass);
|
Class<?> pluginClass = loader.loadClass(pluginConfig.mainClass);
|
||||||
Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor(PluginIdentifier.class)
|
Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor().newInstance();
|
||||||
.newInstance(PluginIdentifier.fromPluginConfig(pluginConfig));
|
this.loadPlugin(pluginInstance, PluginIdentifier.fromPluginConfig(pluginConfig));
|
||||||
this.loadPlugin(pluginInstance);
|
|
||||||
|
|
||||||
fileReader.close(); // Close the file reader.
|
fileReader.close(); // Close the file reader.
|
||||||
} catch (ClassNotFoundException ignored) {
|
} catch (ClassNotFoundException ignored) {
|
||||||
@ -77,11 +76,20 @@ public final class PluginManager {
|
|||||||
* Load the specified plugin.
|
* Load the specified plugin.
|
||||||
* @param plugin The plugin instance.
|
* @param plugin The plugin instance.
|
||||||
*/
|
*/
|
||||||
private void loadPlugin(Plugin plugin) {
|
private void loadPlugin(Plugin plugin, PluginIdentifier identifier) {
|
||||||
Grasscutter.getLogger().info("Loading plugin: " + plugin.getName());
|
Grasscutter.getLogger().info("Loading plugin: " + identifier.name);
|
||||||
|
|
||||||
|
// Add the plugin's identifier.
|
||||||
|
try {
|
||||||
|
Class<Plugin> pluginClass = Plugin.class;
|
||||||
|
Method method = pluginClass.getDeclaredMethod("initializePlugin", PluginIdentifier.class);
|
||||||
|
method.setAccessible(true); method.invoke(plugin, identifier); method.setAccessible(false);
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
Grasscutter.getLogger().warn("Failed to add plugin identifier: " + identifier.name);
|
||||||
|
}
|
||||||
|
|
||||||
// Add the plugin to the list of loaded plugins.
|
// Add the plugin to the list of loaded plugins.
|
||||||
this.plugins.put(plugin.getName(), plugin);
|
this.plugins.put(identifier.name, plugin);
|
||||||
// Call the plugin's onLoad method.
|
// Call the plugin's onLoad method.
|
||||||
plugin.onLoad();
|
plugin.onLoad();
|
||||||
}
|
}
|
||||||
@ -90,13 +98,19 @@ public final class PluginManager {
|
|||||||
* Enables all registered plugins.
|
* Enables all registered plugins.
|
||||||
*/
|
*/
|
||||||
public void enablePlugins() {
|
public void enablePlugins() {
|
||||||
|
this.plugins.forEach((name, plugin) -> {
|
||||||
|
Grasscutter.getLogger().info("Enabling plugin: " + name);
|
||||||
|
plugin.onEnable();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disables all registered plugins.
|
* Disables all registered plugins.
|
||||||
*/
|
*/
|
||||||
public void disablePlugins() {
|
public void disablePlugins() {
|
||||||
|
this.plugins.forEach((name, plugin) -> {
|
||||||
|
Grasscutter.getLogger().info("Disabling plugin: " + name);
|
||||||
|
plugin.onDisable();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user