|
| 1 | +package the.bytecode.club.jda; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.net.MalformedURLException; |
| 6 | +import java.net.URL; |
| 7 | +import java.net.URLClassLoader; |
| 8 | + |
| 9 | +public class PluginLoader { |
| 10 | + public static void tryLoadPlugin(File pluginFile) throws MalformedURLException { |
| 11 | + String pluginFileName = pluginFile.getName(); |
| 12 | + try { |
| 13 | + ClassLoader loader = new URLClassLoader(new URL[] { pluginFile.toURI().toURL() }) { |
| 14 | + public URL getResource(String name) { |
| 15 | + if (name.startsWith("\0JDA-hack:")) |
| 16 | + return findResource(name.substring(name.indexOf(':') + 1)); |
| 17 | + else |
| 18 | + return super.getResource(name); |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + InputStream metaInfStream = loader.getResourceAsStream("\0JDA-hack:META-INF/MANIFEST.MF"); |
| 23 | + if (metaInfStream == null) { |
| 24 | + System.out.println("Invalid plugin " + pluginFileName + ": no manifest"); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + String mainClass = parseManifest(metaInfStream); |
| 29 | + if (mainClass == null) { |
| 30 | + System.out.println("Invalid plugin " + pluginFileName + ": unable to parse manifest"); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + Class<?> clazz = Class.forName(mainClass, true, loader); |
| 35 | + Method mainMethod = clazz.getMethod("main", String[].class); |
| 36 | + mainMethod.invoke(null, (Object) new String[0]); |
| 37 | + } catch (ReflectiveOperationException e) { |
| 38 | + System.err.println("Failed to load plugin " + pluginFileName); |
| 39 | + e.printStackTrace(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Read Main-Class attribute from manifest. Return null if invalid |
| 44 | + public static String parseManifest(InputStream manifestStream) { |
| 45 | + BufferedReader reader = new BufferedReader(new InputStreamReader(manifestStream)); |
| 46 | + String line; |
| 47 | + try { |
| 48 | + while ((line = reader.readLine()) != null) { |
| 49 | + String[] parts = line.split(":"); |
| 50 | + if (parts.length < 2) |
| 51 | + return null; |
| 52 | + |
| 53 | + String header = parts[0]; |
| 54 | + if (header.equals("Main-Class")) { |
| 55 | + return parts[1].trim(); |
| 56 | + } |
| 57 | + } |
| 58 | + return null; |
| 59 | + } catch (IOException e) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments