From 5cc9ecfd91ead78072da6eb1949a42b5631ada98 Mon Sep 17 00:00:00 2001 From: muhammadeko Date: Sat, 7 May 2022 19:48:20 +0700 Subject: [PATCH 1/2] PluginManager: Use the same class loader and add getPlugin method --- .../emu/grasscutter/plugin/PluginManager.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index 7a5e0aa00..4844a698a 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -4,12 +4,12 @@ import emu.grasscutter.Grasscutter; import emu.grasscutter.server.event.Event; import emu.grasscutter.server.event.EventHandler; import emu.grasscutter.server.event.HandlerPriority; -import emu.grasscutter.utils.EventConsumer; import emu.grasscutter.utils.Utils; import java.io.File; import java.io.InputStreamReader; import java.lang.reflect.Method; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.*; @@ -47,12 +47,23 @@ public final class PluginManager { List plugins = Arrays.stream(files) .filter(file -> file.getName().endsWith(".jar")) .toList(); - + + URL[] pluginNames = new URL[plugins.size()]; + plugins.forEach(plugin -> { + try { + pluginNames[plugins.indexOf(plugin)] = plugin.toURI().toURL(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + }); + + URLClassLoader classLoader = new URLClassLoader(pluginNames); + plugins.forEach(plugin -> { try { URL url = plugin.toURI().toURL(); try (URLClassLoader loader = new URLClassLoader(new URL[]{url})) { - URL configFile = loader.findResource("plugin.json"); + URL configFile = loader.findResource("plugin.json"); // Find the plugin.json file for each plugin. InputStreamReader fileReader = new InputStreamReader(configFile.openStream()); PluginConfig pluginConfig = Grasscutter.getGsonFactory().fromJson(fileReader, PluginConfig.class); @@ -68,10 +79,10 @@ public final class PluginManager { JarEntry entry = entries.nextElement(); if(entry.isDirectory() || !entry.getName().endsWith(".class") || entry.getName().contains("module-info")) continue; String className = entry.getName().replace(".class", "").replace("/", "."); - loader.loadClass(className); + classLoader.loadClass(className); //For all plugin we use the same class loader. } - Class pluginClass = loader.loadClass(pluginConfig.mainClass); + Class pluginClass = classLoader.loadClass(pluginConfig.mainClass); Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor().newInstance(); this.loadPlugin(pluginInstance, PluginIdentifier.fromPluginConfig(pluginConfig), loader); @@ -156,6 +167,10 @@ public final class PluginManager { .toList().forEach(handler -> this.invokeHandler(event, handler)); } + public Plugin getPlugin(String name) { + return this.plugins.get(name); + } + /** * Performs logic checks then invokes the provided event handler. * @param event The event passed through to the handler. From f133a8b12351d74a1817f0bc2de0367ff4d1b203 Mon Sep 17 00:00:00 2001 From: Magix <27646710+KingRainbow44@users.noreply.github.com> Date: Sat, 7 May 2022 17:58:18 -0400 Subject: [PATCH 2/2] Update PluginManager.java --- src/main/java/emu/grasscutter/plugin/PluginManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index 4844a698a..bc78d12eb 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -52,8 +52,8 @@ public final class PluginManager { plugins.forEach(plugin -> { try { pluginNames[plugins.indexOf(plugin)] = plugin.toURI().toURL(); - } catch (MalformedURLException e) { - e.printStackTrace(); + } catch (MalformedURLException exception) { + Grasscutter.getLogger().warn("Unable to load plugin.", exception); } }); @@ -79,7 +79,7 @@ public final class PluginManager { JarEntry entry = entries.nextElement(); if(entry.isDirectory() || !entry.getName().endsWith(".class") || entry.getName().contains("module-info")) continue; String className = entry.getName().replace(".class", "").replace("/", "."); - classLoader.loadClass(className); //For all plugin we use the same class loader. + classLoader.loadClass(className); // Use the same class loader for ALL plugins. } Class pluginClass = classLoader.loadClass(pluginConfig.mainClass); @@ -182,4 +182,4 @@ public final class PluginManager { (event.isCanceled() && handler.ignoresCanceled()) ) handler.getCallback().consume((T) event); } -} \ No newline at end of file +}