Merge pull request #608 from Adam-/plugindeps

Add plugin dependencies
This commit is contained in:
Adam
2018-02-17 13:17:03 -05:00
committed by GitHub
13 changed files with 367 additions and 25 deletions

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* 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();
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* 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<? extends Plugin> value();
}

View File

@@ -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);

View File

@@ -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<Class<? extends Plugin>> graph = GraphBuilder
.directed()
.build();
List<Plugin> 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<Plugin> pluginClass = (Class<Plugin>) clazz;
graph.addNode(pluginClass);
}
// Build plugin graph
for (Class<? extends Plugin> 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<Class<? extends Plugin>> sortedPlugins = topologicalSort(graph);
sortedPlugins = Lists.reverse(sortedPlugins);
for (Class<? extends Plugin> pluginClazz : sortedPlugins)
{
Plugin plugin;
try
{
plugin = instantiate((Class<Plugin>) clazz);
plugin = instantiate(scannedPlugins, (Class<Plugin>) pluginClazz);
}
catch (PluginInstantiationException ex)
{
@@ -354,8 +392,20 @@ public class PluginManager
return Boolean.valueOf(value);
}
private Plugin instantiate(Class<Plugin> clazz) throws PluginInstantiationException
private Plugin instantiate(List<Plugin> scannedPlugins, Class<Plugin> clazz) throws PluginInstantiationException
{
PluginDependency[] pluginDependencies = clazz.getAnnotationsByType(PluginDependency.class);
List<Plugin> deps = new ArrayList<>();
for (PluginDependency pluginDependency : pluginDependencies)
{
Optional<Plugin> 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<Plugin>) 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 <T>
* @return
*/
private <T> List<T> topologicalSort(Graph<T> graph)
{
MutableGraph<T> graphCopy = Graphs.copyOf(graph);
List<T> l = new ArrayList<>();
Set<T> s = graphCopy.nodes().stream()
.filter(node -> graphCopy.inDegree(node) == 0)
.collect(Collectors.toSet());
while (!s.isEmpty())
{
Iterator<T> 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;
}
}

View File

@@ -30,9 +30,10 @@ import java.awt.Graphics2D;
import java.awt.Point;
import java.time.Duration;
import java.time.Instant;
import javax.annotation.Nullable;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.Skill;
import net.runelite.client.plugins.xptracker.XpTrackerService;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.client.ui.overlay.OverlayPosition;
import net.runelite.client.ui.overlay.components.PanelComponent;
@@ -44,15 +45,18 @@ class FishingOverlay extends Overlay
private final Client client;
private final FishingPlugin plugin;
private final FishingConfig config;
private final XpTrackerService xpTrackerService;
private final PanelComponent panelComponent = new PanelComponent();
@Inject
public FishingOverlay(@Nullable Client client, FishingPlugin plugin, FishingConfig config)
public FishingOverlay(Client client, FishingPlugin plugin, FishingConfig config, XpTrackerService xpTrackerService)
{
setPosition(OverlayPosition.TOP_LEFT);
this.client = client;
this.plugin = plugin;
this.config = config;
this.xpTrackerService = xpTrackerService;
}
@Override
@@ -85,6 +89,23 @@ class FishingOverlay extends Overlay
panelComponent.setTitleColor(Color.RED);
}
int actions = xpTrackerService.getActions(Skill.FISHING);
if (actions > 0)
{
panelComponent.getLines().add(new PanelComponent.Line(
"Caught fish:",
Integer.toString(actions)
));
if (actions > 2)
{
panelComponent.getLines().add(new PanelComponent.Line(
"Fish/hr:",
Integer.toString(xpTrackerService.getActionsHr(Skill.FISHING))
));
}
}
return panelComponent.render(graphics, parent);
}
}

View File

@@ -43,13 +43,16 @@ import net.runelite.api.events.GameTick;
import net.runelite.api.queries.NPCQuery;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDependency;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.xptracker.XpTrackerPlugin;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.client.util.QueryRunner;
@PluginDescriptor(
name = "Fishing"
)
@PluginDependency(XpTrackerPlugin.class)
@Singleton
public class FishingPlugin extends Plugin
{

View File

@@ -25,18 +25,15 @@
package net.runelite.client.plugins.fishing;
import java.time.Instant;
import lombok.Getter;
public class FishingSession
{
@Getter
private Instant lastFishCaught;
public void setLastFishCaught()
{
lastFishCaught = Instant.now();
}
public Instant getLastFishCaught()
{
return lastFishCaught;
}
}

View File

@@ -24,6 +24,14 @@
*/
package net.runelite.client.plugins.woodcutting;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.time.Duration;
import java.time.Instant;
import java.util.stream.IntStream;
import javax.inject.Inject;
import static net.runelite.api.AnimationID.WOODCUTTING_ADAMANT;
import static net.runelite.api.AnimationID.WOODCUTTING_BLACK;
import static net.runelite.api.AnimationID.WOODCUTTING_BRONZE;
@@ -33,16 +41,9 @@ import static net.runelite.api.AnimationID.WOODCUTTING_IRON;
import static net.runelite.api.AnimationID.WOODCUTTING_MITHRIL;
import static net.runelite.api.AnimationID.WOODCUTTING_RUNE;
import static net.runelite.api.AnimationID.WOODCUTTING_STEEL;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.time.Duration;
import java.time.Instant;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.Skill;
import net.runelite.client.plugins.xptracker.XpTrackerService;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.client.ui.overlay.OverlayPosition;
import net.runelite.client.ui.overlay.components.PanelComponent;
@@ -59,15 +60,17 @@ class WoodcuttingOverlay extends Overlay
private final Client client;
private final WoodcuttingPlugin plugin;
private final WoodcuttingConfig config;
private final XpTrackerService xpTrackerService;
private final PanelComponent panelComponent = new PanelComponent();
@Inject
public WoodcuttingOverlay(@Nullable Client client, WoodcuttingPlugin plugin, WoodcuttingConfig config)
public WoodcuttingOverlay(Client client, WoodcuttingPlugin plugin, WoodcuttingConfig config, XpTrackerService xpTrackerService)
{
setPosition(OverlayPosition.TOP_LEFT);
this.client = client;
this.plugin = plugin;
this.config = config;
this.xpTrackerService = xpTrackerService;
}
@Override
@@ -101,6 +104,23 @@ class WoodcuttingOverlay extends Overlay
panelComponent.setTitleColor(Color.RED);
}
int actions = xpTrackerService.getActions(Skill.WOODCUTTING);
if (actions > 0)
{
panelComponent.getLines().add(new PanelComponent.Line(
"Logs cut:",
Integer.toString(actions)
));
if (actions > 2)
{
panelComponent.getLines().add(new PanelComponent.Line(
"Logs/hr:",
Integer.toString(xpTrackerService.getActionsHr(Skill.WOODCUTTING))
));
}
}
return panelComponent.render(graphics, parent);
}
}

View File

@@ -32,12 +32,15 @@ import net.runelite.api.events.ChatMessage;
import net.runelite.client.Notifier;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDependency;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.xptracker.XpTrackerPlugin;
import net.runelite.client.ui.overlay.Overlay;
@PluginDescriptor(
name = "Woodcutting"
)
@PluginDependency(XpTrackerPlugin.class)
public class WoodcuttingPlugin extends Plugin
{
@Inject

View File

@@ -49,7 +49,7 @@ class XpPanel extends PluginPanel
private final JLabel totalXpGained = new JLabel();
private final JLabel totalXpHr = new JLabel();
XpPanel(Client client, SkillIconManager iconManager)
XpPanel(XpTrackerPlugin xpTrackerPlugin, Client client, SkillIconManager iconManager)
{
super();
@@ -90,7 +90,7 @@ class XpPanel extends PluginPanel
break;
}
infoBoxes.put(skill, new XpInfoBox(client, infoBoxPanel, new SkillXPInfo(skill), iconManager));
infoBoxes.put(skill, new XpInfoBox(client, infoBoxPanel, xpTrackerPlugin.getSkillXpInfo(skill), iconManager));
}
}
catch (IOException e)

View File

@@ -24,10 +24,12 @@
*/
package net.runelite.client.plugins.xptracker;
import static net.runelite.client.plugins.xptracker.XpWorldType.NORMAL;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Binder;
import java.io.IOException;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import javax.imageio.ImageIO;
@@ -36,12 +38,14 @@ import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.Player;
import net.runelite.api.Skill;
import net.runelite.api.events.ExperienceChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.client.game.SkillIconManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import static net.runelite.client.plugins.xptracker.XpWorldType.NORMAL;
import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.NavigationButton;
import net.runelite.http.api.worlds.World;
@@ -70,12 +74,21 @@ public class XpTrackerPlugin extends Plugin
private NavigationButton navButton;
private XpPanel xpPanel;
private final Map<Skill, SkillXPInfo> xpInfos = new HashMap<>();
private WorldResult worlds;
private XpWorldType lastWorldType;
private String lastUsername;
private final XpClient xpClient = new XpClient();
@Override
public void configure(Binder binder)
{
binder.bind(XpTrackerService.class).to(XpTrackerServiceImpl.class);
}
@Override
protected void startUp() throws Exception
{
@@ -90,7 +103,7 @@ public class XpTrackerPlugin extends Plugin
log.warn("Error looking up worlds list", e);
}
xpPanel = new XpPanel(client, skillIconManager);
xpPanel = new XpPanel(this, client, skillIconManager);
navButton = new NavigationButton(
"XP Tracker",
ImageIO.read(getClass().getResourceAsStream("xp.png")),
@@ -170,6 +183,11 @@ public class XpTrackerPlugin extends Plugin
return xpType;
}
public SkillXPInfo getSkillXpInfo(Skill skill)
{
return xpInfos.computeIfAbsent(skill, s -> new SkillXPInfo(s));
}
@Subscribe
public void onXpChanged(ExperienceChanged event)
{

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* 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.xptracker;
import net.runelite.api.Skill;
public interface XpTrackerService
{
/**
* Get the number of actions done
* @param skill
* @return
*/
int getActions(Skill skill);
/**
* Get the number of actions per hour
* @param skill
* @return
*/
int getActionsHr(Skill skill);
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* 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.xptracker;
import javax.inject.Inject;
import javax.inject.Singleton;
import net.runelite.api.Skill;
@Singleton
class XpTrackerServiceImpl implements XpTrackerService
{
private final XpTrackerPlugin plugin;
@Inject
XpTrackerServiceImpl(XpTrackerPlugin plugin)
{
this.plugin = plugin;
}
@Override
public int getActions(Skill skill)
{
SkillXPInfo xpInfo = plugin.getSkillXpInfo(skill);
return xpInfo.getActions();
}
@Override
public int getActionsHr(Skill skill)
{
SkillXPInfo xpInfo = plugin.getSkillXpInfo(skill);
return xpInfo.getActionsHr();
}
}