From 2bd470c57402088c176fc580fcb6aa7476cb3017 Mon Sep 17 00:00:00 2001 From: muhammadeko Date: Tue, 26 Apr 2022 19:45:23 +0700 Subject: [PATCH] load all classes from plugin --- .../java/emu/grasscutter/plugin/PluginManager.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index 7b54f460f..2c6b9f23d 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -13,6 +13,8 @@ import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.util.*; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; /** * Manages the server's plugins & the event system. @@ -52,13 +54,22 @@ public final class PluginManager { try (URLClassLoader loader = new URLClassLoader(new URL[]{url})) { URL configFile = loader.findResource("plugin.json"); InputStreamReader fileReader = new InputStreamReader(configFile.openStream()); - + 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; } + + JarFile jarFile = new JarFile(plugin); + Enumeration 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); + } Class pluginClass = loader.loadClass(pluginConfig.mainClass); Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor().newInstance();