2022-04-23 09:45:44 +08:00
|
|
|
package emu.grasscutter.plugin;
|
|
|
|
|
|
|
|
import emu.grasscutter.Grasscutter;
|
2022-04-23 13:58:37 +08:00
|
|
|
import emu.grasscutter.server.event.Event;
|
|
|
|
import emu.grasscutter.server.event.EventHandler;
|
|
|
|
import emu.grasscutter.server.event.Listener;
|
2022-04-23 09:45:44 +08:00
|
|
|
import emu.grasscutter.utils.Utils;
|
2022-04-23 13:58:37 +08:00
|
|
|
import org.reflections.Reflections;
|
2022-04-23 09:45:44 +08:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStreamReader;
|
2022-04-23 13:17:35 +08:00
|
|
|
import java.lang.reflect.Method;
|
2022-04-23 09:45:44 +08:00
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
2022-04-23 13:58:37 +08:00
|
|
|
import java.util.*;
|
2022-04-26 20:45:23 +08:00
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
import java.util.jar.JarFile;
|
2022-04-23 09:45:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages the server's plugins & the event system.
|
|
|
|
*/
|
|
|
|
public final class PluginManager {
|
|
|
|
private final Map<String, Plugin> plugins = new HashMap<>();
|
2022-04-23 13:58:37 +08:00
|
|
|
private final Map<Plugin, List<Listener>> listeners = new HashMap<>();
|
2022-04-23 09:45:44 +08:00
|
|
|
|
|
|
|
public PluginManager() {
|
|
|
|
this.loadPlugins(); // Load all plugins from the plugins directory.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads plugins from the config-specified directory.
|
|
|
|
*/
|
|
|
|
private void loadPlugins() {
|
|
|
|
String directory = Grasscutter.getConfig().PLUGINS_FOLDER;
|
|
|
|
File pluginsDir = new File(Utils.toFilePath(directory));
|
|
|
|
if(!pluginsDir.exists() && !pluginsDir.mkdirs()) {
|
|
|
|
Grasscutter.getLogger().error("Failed to create plugins directory: " + pluginsDir.getAbsolutePath());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
File[] files = pluginsDir.listFiles();
|
|
|
|
if(files == null) {
|
|
|
|
// The directory is empty, there aren't any plugins to load.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<File> plugins = Arrays.stream(files)
|
|
|
|
.filter(file -> file.getName().endsWith(".jar"))
|
2022-04-23 13:17:35 +08:00
|
|
|
.toList();
|
2022-04-23 09:45:44 +08:00
|
|
|
|
|
|
|
plugins.forEach(plugin -> {
|
|
|
|
try {
|
|
|
|
URL url = plugin.toURI().toURL();
|
|
|
|
try (URLClassLoader loader = new URLClassLoader(new URL[]{url})) {
|
|
|
|
URL configFile = loader.findResource("plugin.json");
|
|
|
|
InputStreamReader fileReader = new InputStreamReader(configFile.openStream());
|
2022-04-26 20:45:23 +08:00
|
|
|
|
2022-04-23 09:45:44 +08:00
|
|
|
PluginConfig pluginConfig = Grasscutter.getGsonFactory().fromJson(fileReader, PluginConfig.class);
|
|
|
|
if(!pluginConfig.validate()) {
|
|
|
|
Utils.logObject(pluginConfig);
|
|
|
|
Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid config file.");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-26 20:45:23 +08:00
|
|
|
|
|
|
|
JarFile jarFile = new JarFile(plugin);
|
|
|
|
Enumeration<JarEntry> entries = jarFile.entries();
|
|
|
|
while(entries.hasMoreElements()) {
|
|
|
|
JarEntry entry = entries.nextElement();
|
|
|
|
if(entry.isDirectory() || !entry.getName().endsWith(".class")) continue;
|
|
|
|
String className = entry.getName().replace(".class", "").replace("/", ".");
|
|
|
|
Class<?> clazz = loader.loadClass(className);
|
|
|
|
}
|
2022-04-23 09:45:44 +08:00
|
|
|
|
|
|
|
Class<?> pluginClass = loader.loadClass(pluginConfig.mainClass);
|
2022-04-23 13:17:35 +08:00
|
|
|
Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor().newInstance();
|
|
|
|
this.loadPlugin(pluginInstance, PluginIdentifier.fromPluginConfig(pluginConfig));
|
2022-04-23 09:45:44 +08:00
|
|
|
|
|
|
|
fileReader.close(); // Close the file reader.
|
|
|
|
} catch (ClassNotFoundException ignored) {
|
|
|
|
Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid main class.");
|
|
|
|
}
|
|
|
|
} catch (Exception exception) {
|
|
|
|
Grasscutter.getLogger().error("Failed to load plugin: " + plugin.getName(), exception);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the specified plugin.
|
|
|
|
* @param plugin The plugin instance.
|
|
|
|
*/
|
2022-04-23 13:17:35 +08:00
|
|
|
private void loadPlugin(Plugin plugin, PluginIdentifier identifier) {
|
|
|
|
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);
|
|
|
|
}
|
2022-04-23 09:45:44 +08:00
|
|
|
|
|
|
|
// Add the plugin to the list of loaded plugins.
|
2022-04-23 13:17:35 +08:00
|
|
|
this.plugins.put(identifier.name, plugin);
|
2022-04-23 09:45:44 +08:00
|
|
|
// Call the plugin's onLoad method.
|
|
|
|
plugin.onLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables all registered plugins.
|
|
|
|
*/
|
|
|
|
public void enablePlugins() {
|
2022-04-23 13:17:35 +08:00
|
|
|
this.plugins.forEach((name, plugin) -> {
|
|
|
|
Grasscutter.getLogger().info("Enabling plugin: " + name);
|
|
|
|
plugin.onEnable();
|
|
|
|
});
|
2022-04-23 09:45:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables all registered plugins.
|
|
|
|
*/
|
|
|
|
public void disablePlugins() {
|
2022-04-23 13:17:35 +08:00
|
|
|
this.plugins.forEach((name, plugin) -> {
|
|
|
|
Grasscutter.getLogger().info("Disabling plugin: " + name);
|
|
|
|
plugin.onDisable();
|
|
|
|
});
|
2022-04-23 09:45:44 +08:00
|
|
|
}
|
2022-04-23 13:58:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a plugin's event listener.
|
|
|
|
* @param plugin The plugin instance.
|
|
|
|
* @param listener The event listener.
|
|
|
|
*/
|
|
|
|
public void registerListener(Plugin plugin, Listener listener) {
|
|
|
|
this.listeners.computeIfAbsent(plugin, k -> new ArrayList<>()).add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke the provided event on all registered event listeners.
|
|
|
|
* @param event The event to invoke.
|
|
|
|
*/
|
|
|
|
public void invokeEvent(Event event) {
|
|
|
|
this.listeners.values().stream()
|
|
|
|
.flatMap(Collection::stream)
|
|
|
|
.forEach(listener -> this.invokeOnListener(listener, event));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to invoke the event on the provided listener.
|
|
|
|
*/
|
|
|
|
private void invokeOnListener(Listener listener, Event event) {
|
|
|
|
try {
|
|
|
|
Class<?> listenerClass = listener.getClass();
|
|
|
|
Method[] methods = listenerClass.getMethods();
|
|
|
|
for (Method method : methods) {
|
|
|
|
if(!method.isAnnotationPresent(EventHandler.class)) return;
|
|
|
|
if(!method.getParameterTypes()[0].isAssignableFrom(event.getClass())) return;
|
|
|
|
method.invoke(listener, event);
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) { }
|
|
|
|
}
|
2022-04-23 09:45:44 +08:00
|
|
|
}
|