From e9c3869d54778ff8562c777eafa855c9050f5b2b Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 16 Feb 2018 20:42:20 -0500 Subject: [PATCH] runelite-client: add plugin dependencies --- .../client/plugins/PluginDependencies.java | 39 +++++++ .../client/plugins/PluginDependency.java | 41 +++++++ .../plugins/PluginInstantiationException.java | 5 + .../client/plugins/PluginManager.java | 104 +++++++++++++++++- 4 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/PluginDependencies.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/PluginDependency.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/PluginDependencies.java b/runelite-client/src/main/java/net/runelite/client/plugins/PluginDependencies.java new file mode 100644 index 0000000000..af866efefc --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/PluginDependencies.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Documented +public @interface PluginDependencies +{ + PluginDependency[] value(); +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/PluginDependency.java b/runelite-client/src/main/java/net/runelite/client/plugins/PluginDependency.java new file mode 100644 index 0000000000..d1d2b3bbc2 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/PluginDependency.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Documented +@Repeatable(PluginDependencies.class) +public @interface PluginDependency +{ + Class value(); +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/PluginInstantiationException.java b/runelite-client/src/main/java/net/runelite/client/plugins/PluginInstantiationException.java index f4f9033392..46f443028b 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/PluginInstantiationException.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/PluginInstantiationException.java @@ -26,6 +26,11 @@ package net.runelite.client.plugins; public class PluginInstantiationException extends Exception { + public PluginInstantiationException(String message) + { + super(message); + } + public PluginInstantiationException(Throwable cause) { super(cause); diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/PluginManager.java b/runelite-client/src/main/java/net/runelite/client/plugins/PluginManager.java index a554c9d090..cca8fb1c30 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/PluginManager.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/PluginManager.java @@ -25,8 +25,13 @@ package net.runelite.client.plugins; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; +import com.google.common.graph.Graph; +import com.google.common.graph.GraphBuilder; +import com.google.common.graph.Graphs; +import com.google.common.graph.MutableGraph; import com.google.common.reflect.ClassPath; import com.google.common.reflect.ClassPath.ClassInfo; import com.google.inject.Binder; @@ -40,9 +45,13 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ScheduledExecutorService; +import java.util.stream.Collectors; import javax.inject.Singleton; import javax.swing.SwingUtilities; import lombok.Setter; @@ -210,6 +219,10 @@ public class PluginManager { boolean developerPlugins = RuneLite.getOptions().has("developer-mode"); + MutableGraph> graph = GraphBuilder + .directed() + .build(); + List scannedPlugins = new ArrayList<>(); ClassPath classPath = ClassPath.from(classLoader); @@ -225,7 +238,7 @@ public class PluginManager if (clazz.getSuperclass() == Plugin.class) { log.warn("Class {} is a plugin, but has no plugin descriptor", - clazz); + clazz); } continue; } @@ -233,7 +246,7 @@ public class PluginManager if (clazz.getSuperclass() != Plugin.class) { log.warn("Class {} has plugin descriptor, but is not a plugin", - clazz); + clazz); continue; } @@ -247,10 +260,35 @@ public class PluginManager continue; } + Class pluginClass = (Class) clazz; + graph.addNode(pluginClass); + } + + // Build plugin graph + for (Class pluginClazz : graph.nodes()) + { + PluginDependency[] pluginDependencies = pluginClazz.getAnnotationsByType(PluginDependency.class); + + for (PluginDependency pluginDependency : pluginDependencies) + { + graph.putEdge(pluginClazz, pluginDependency.value()); + } + } + + if (Graphs.hasCycle(graph)) + { + throw new RuntimeException("Plugin dependency graph contains a cycle!"); + } + + List> sortedPlugins = topologicalSort(graph); + sortedPlugins = Lists.reverse(sortedPlugins); + + for (Class pluginClazz : sortedPlugins) + { Plugin plugin; try { - plugin = instantiate((Class) clazz); + plugin = instantiate(scannedPlugins, (Class) pluginClazz); } catch (PluginInstantiationException ex) { @@ -354,8 +392,20 @@ public class PluginManager return Boolean.valueOf(value); } - private Plugin instantiate(Class clazz) throws PluginInstantiationException + private Plugin instantiate(List scannedPlugins, Class clazz) throws PluginInstantiationException { + PluginDependency[] pluginDependencies = clazz.getAnnotationsByType(PluginDependency.class); + List deps = new ArrayList<>(); + for (PluginDependency pluginDependency : pluginDependencies) + { + Optional dependency = scannedPlugins.stream().filter(p -> p.getClass() == pluginDependency.value()).findFirst(); + if (!dependency.isPresent()) + { + throw new PluginInstantiationException("Unmet dependency for " + clazz.getSimpleName() + ": " + pluginDependency.value().getSimpleName()); + } + deps.add(dependency.get()); + } + Plugin plugin; try { @@ -372,6 +422,15 @@ public class PluginManager { binder.bind(clazz).toInstance(plugin); binder.install(plugin); + for (Plugin p : deps) + { + Module p2 = (Binder binder2) -> + { + binder2.bind((Class) p.getClass()).toInstance(p); + binder2.install(p); + }; + binder.install(p2); + } }; Injector pluginInjector = RuneLite.getInjector().createChildInjector(pluginModule); pluginInjector.injectMembers(plugin); @@ -434,4 +493,41 @@ public class PluginManager scheduler.removeScheduledMethod(method); } } + + /** + * Topologically sort a graph. Uses Kahn's algorithm. + * @param graph + * @param + * @return + */ + private List topologicalSort(Graph graph) + { + MutableGraph graphCopy = Graphs.copyOf(graph); + List l = new ArrayList<>(); + Set s = graphCopy.nodes().stream() + .filter(node -> graphCopy.inDegree(node) == 0) + .collect(Collectors.toSet()); + while (!s.isEmpty()) + { + Iterator it = s.iterator(); + T n = it.next(); + it.remove(); + + l.add(n); + + for (T m : graphCopy.successors(n)) + { + graphCopy.removeEdge(n, m); + if (graphCopy.inDegree(m) == 0) + { + s.add(m); + } + } + } + if (!graphCopy.edges().isEmpty()) + { + throw new RuntimeException("Graph has at least one cycle"); + } + return l; + } }