Send launch properties around as bound constants

Instead of exposing static variable provide launch properties to
dependant classes as bound constants in RuneLiteModule.

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-07-06 11:19:11 +02:00
parent 8909871554
commit 66bfbc5648
6 changed files with 213 additions and 198 deletions

View File

@@ -53,7 +53,6 @@ import net.runelite.client.game.ClanManager;
import net.runelite.client.game.ItemManager;
import net.runelite.client.menus.MenuManager;
import net.runelite.client.plugins.PluginManager;
import net.runelite.client.rs.ClientLoader;
import net.runelite.client.rs.ClientUpdateCheckMode;
import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.DrawManager;
@@ -80,9 +79,6 @@ public class RuneLite
@Getter
private static Injector injector;
@Getter
private static OptionSet options;
@Inject
private PluginManager pluginManager;
@@ -174,15 +170,17 @@ public class RuneLite
});
parser.accepts("help", "Show this text").forHelp();
options = parser.parse(args);
OptionSet options = parser.parse(args);
if (getOptions().has("help"))
if (options.has("help"))
{
parser.printHelpOn(System.out);
System.exit(0);
}
if (RuneLite.getOptions().has("developer-mode") && RuneLiteProperties.getLauncherVersion() == null)
final boolean developerMode = options.has("developer-mode");
if (developerMode && RuneLiteProperties.getLauncherVersion() == null)
{
boolean assertions = false;
assert assertions = true;
@@ -212,8 +210,10 @@ public class RuneLite
}
});
injector = Guice.createInjector(new RuneLiteModule());
injector.getInstance(ClientLoader.class).setUpdateCheckMode(getOptions().valueOf(updateMode));
injector = Guice.createInjector(new RuneLiteModule(
options.valueOf(updateMode),
developerMode));
injector.getInstance(RuneLite.class).start();
}
@@ -298,10 +298,4 @@ public class RuneLite
{
RuneLite.injector = injector;
}
@VisibleForTesting
public static void setOptions(OptionSet options)
{
RuneLite.options = options;
}
}

View File

@@ -46,6 +46,7 @@ import net.runelite.client.config.RuneLiteConfig;
import net.runelite.client.game.ItemManager;
import net.runelite.client.menus.MenuManager;
import net.runelite.client.plugins.PluginManager;
import net.runelite.client.rs.ClientUpdateCheckMode;
import net.runelite.client.rs.ClientLoader;
import net.runelite.client.task.Scheduler;
import net.runelite.client.util.DeferredEventBus;
@@ -58,9 +59,20 @@ import okhttp3.OkHttpClient;
@Slf4j
public class RuneLiteModule extends AbstractModule
{
private final ClientUpdateCheckMode updateCheckMode;
private final boolean developerMode;
public RuneLiteModule(final ClientUpdateCheckMode updateCheckMode, final boolean developerMode)
{
this.updateCheckMode = updateCheckMode;
this.developerMode = developerMode;
}
@Override
protected void configure()
{
bindConstant().annotatedWith(Names.named("updateCheckMode")).to(updateCheckMode);
bindConstant().annotatedWith(Names.named("developerMode")).to(developerMode);
bind(ScheduledExecutorService.class).toInstance(Executors.newSingleThreadScheduledExecutor());
bind(OkHttpClient.class).toInstance(RuneLiteAPI.CLIENT);
bind(QueryRunner.class);

View File

@@ -24,6 +24,7 @@
*/
package net.runelite.client.plugins;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
@@ -36,7 +37,6 @@ import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;
import com.google.inject.Binder;
import com.google.inject.CreationException;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
@@ -52,6 +52,8 @@ import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.swing.SwingUtilities;
import lombok.Setter;
@@ -78,29 +80,38 @@ public class PluginManager
*/
private static final String PLUGIN_PACKAGE = "net.runelite.client.plugins";
@Inject
EventBus eventBus;
@Inject
Scheduler scheduler;
@Inject
ConfigManager configManager;
@Inject
ScheduledExecutorService executor;
@Inject
SceneTileManager sceneTileManager;
@Setter
boolean isOutdated;
private final boolean developerMode;
private final EventBus eventBus;
private final Scheduler scheduler;
private final ConfigManager configManager;
private final ScheduledExecutorService executor;
private final SceneTileManager sceneTileManager;
private final List<Plugin> plugins = new CopyOnWriteArrayList<>();
private final List<Plugin> activePlugins = new CopyOnWriteArrayList<>();
private final String runeliteGroupName = RuneLiteConfig.class
.getAnnotation(ConfigGroup.class).value();
@Setter
boolean isOutdated;
@Inject
@VisibleForTesting
PluginManager(
@Named("developerMode") final boolean developerMode,
final EventBus eventBus,
final Scheduler scheduler,
final ConfigManager configManager,
final ScheduledExecutorService executor,
final SceneTileManager sceneTileManager)
{
this.developerMode = developerMode;
this.eventBus = eventBus;
this.scheduler = scheduler;
this.configManager = configManager;
this.executor = executor;
this.sceneTileManager = sceneTileManager;
}
@Subscribe
public void onSessionOpen(SessionOpen event)
{
@@ -204,8 +215,6 @@ public class PluginManager
List<Plugin> scanAndInstantiate(ClassLoader classLoader, String packageName) throws IOException
{
boolean developerPlugins = RuneLite.getOptions().has("developer-mode");
MutableGraph<Class<? extends Plugin>> graph = GraphBuilder
.directed()
.build();
@@ -242,7 +251,7 @@ public class PluginManager
continue;
}
if (pluginDescriptor.developerPlugin() && !developerPlugins)
if (pluginDescriptor.developerPlugin() && !developerMode)
{
continue;
}

View File

@@ -30,8 +30,8 @@ import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.updatecheck.UpdateCheckClient;
@@ -41,13 +41,14 @@ public class ClientLoader
{
private final UpdateCheckClient updateCheckClient = new UpdateCheckClient();
private final ClientConfigLoader clientConfigLoader;
@Setter
private ClientUpdateCheckMode updateCheckMode = ClientUpdateCheckMode.AUTO;
private final ClientUpdateCheckMode updateCheckMode;
@Inject
private ClientLoader(final ClientConfigLoader clientConfigLoader)
private ClientLoader(
@Named("updateCheckMode") final ClientUpdateCheckMode updateCheckMode,
final ClientConfigLoader clientConfigLoader)
{
this.updateCheckMode = updateCheckMode;
this.clientConfigLoader = clientConfigLoader;
}

View File

@@ -25,6 +25,7 @@
package net.runelite.client;
import com.google.inject.Guice;
import net.runelite.client.rs.ClientUpdateCheckMode;
import org.junit.Test;
public class RuneLiteModuleTest
@@ -32,8 +33,6 @@ public class RuneLiteModuleTest
@Test
public void testConfigure()
{
Guice.createInjector(new RuneLiteModule());
Guice.createInjector(new RuneLiteModule(ClientUpdateCheckMode.AUTO, true));
}
}

View File

@@ -1,153 +1,153 @@
/*
* Copyright (c) 2016-2017, 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 com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.grapher.graphviz.GraphvizGrapher;
import com.google.inject.grapher.graphviz.GraphvizModule;
import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import java.applet.Applet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import joptsimple.OptionSet;
import net.runelite.api.Client;
import net.runelite.client.RuneLite;
import net.runelite.client.RuneLiteModule;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PluginManagerTest
{
private static final String PLUGIN_PACKAGE = "net.runelite.client.plugins";
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Mock
@Bind
public Applet applet;
@Mock
@Bind
public Client client;
private Set<Class> pluginClasses;
@Before
public void before() throws IOException
{
RuneLite.setOptions(mock(OptionSet.class));
Injector injector = Guice.createInjector(new RuneLiteModule(),
BoundFieldModule.of(this));
RuneLite.setInjector(injector);
// Find plugins we expect to have
pluginClasses = new HashSet<>();
Set<ClassInfo> classes = ClassPath.from(getClass().getClassLoader()).getTopLevelClassesRecursive(PLUGIN_PACKAGE);
for (ClassInfo classInfo : classes)
{
Class<?> clazz = classInfo.load();
PluginDescriptor pluginDescriptor = clazz.getAnnotation(PluginDescriptor.class);
if (pluginDescriptor != null)
{
pluginClasses.add(clazz);
}
}
}
@Test
public void testLoadPlugins() throws Exception
{
PluginManager pluginManager = new PluginManager();
pluginManager.setOutdated(true);
pluginManager.loadCorePlugins();
Collection<Plugin> plugins = pluginManager.getPlugins();
long expected = pluginClasses.stream()
.map(cl -> (PluginDescriptor) cl.getAnnotation(PluginDescriptor.class))
.filter(Objects::nonNull)
.filter(PluginDescriptor::loadWhenOutdated)
.count();
assertEquals(expected, plugins.size());
pluginManager = new PluginManager();
pluginManager.loadCorePlugins();
plugins = pluginManager.getPlugins();
expected = pluginClasses.stream()
.map(cl -> (PluginDescriptor) cl.getAnnotation(PluginDescriptor.class))
.filter(Objects::nonNull)
.filter(pd -> !pd.developerPlugin())
.count();
assertEquals(expected, plugins.size());
}
@Test
public void dumpGraph() throws Exception
{
List<Module> modules = new ArrayList<>();
modules.add(new GraphvizModule());
modules.add(new RuneLiteModule());
PluginManager pluginManager = new PluginManager();
pluginManager.loadCorePlugins();
for (Plugin p : pluginManager.getPlugins())
{
modules.add(p);
}
File file = folder.newFile();
try (PrintWriter out = new PrintWriter(file, "UTF-8"))
{
Injector injector = Guice.createInjector(modules);
GraphvizGrapher grapher = injector.getInstance(GraphvizGrapher.class);
grapher.setOut(out);
grapher.setRankdir("TB");
grapher.graph(injector);
}
}
}
/*
* Copyright (c) 2016-2017, 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 com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.grapher.graphviz.GraphvizGrapher;
import com.google.inject.grapher.graphviz.GraphvizModule;
import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import com.google.inject.util.Modules;
import java.applet.Applet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import net.runelite.api.Client;
import net.runelite.client.RuneLite;
import net.runelite.client.RuneLiteModule;
import net.runelite.client.rs.ClientUpdateCheckMode;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PluginManagerTest
{
private static final String PLUGIN_PACKAGE = "net.runelite.client.plugins";
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Mock
@Bind
public Applet applet;
@Mock
@Bind
public Client client;
private Set<Class> pluginClasses;
@Before
public void before() throws IOException
{
Injector injector = Guice.createInjector(Modules
.override(new RuneLiteModule(ClientUpdateCheckMode.AUTO, true))
.with(BoundFieldModule.of(this)));
RuneLite.setInjector(injector);
// Find plugins we expect to have
pluginClasses = new HashSet<>();
Set<ClassInfo> classes = ClassPath.from(getClass().getClassLoader()).getTopLevelClassesRecursive(PLUGIN_PACKAGE);
for (ClassInfo classInfo : classes)
{
Class<?> clazz = classInfo.load();
PluginDescriptor pluginDescriptor = clazz.getAnnotation(PluginDescriptor.class);
if (pluginDescriptor != null)
{
pluginClasses.add(clazz);
}
}
}
@Test
public void testLoadPlugins() throws Exception
{
PluginManager pluginManager = new PluginManager(false, null, null, null, null, null);
pluginManager.setOutdated(true);
pluginManager.loadCorePlugins();
Collection<Plugin> plugins = pluginManager.getPlugins();
long expected = pluginClasses.stream()
.map(cl -> (PluginDescriptor) cl.getAnnotation(PluginDescriptor.class))
.filter(Objects::nonNull)
.filter(PluginDescriptor::loadWhenOutdated)
.count();
assertEquals(expected, plugins.size());
pluginManager = new PluginManager(false, null, null, null, null, null);
pluginManager.loadCorePlugins();
plugins = pluginManager.getPlugins();
expected = pluginClasses.stream()
.map(cl -> (PluginDescriptor) cl.getAnnotation(PluginDescriptor.class))
.filter(Objects::nonNull)
.filter(pd -> !pd.developerPlugin())
.count();
assertEquals(expected, plugins.size());
}
@Test
public void dumpGraph() throws Exception
{
List<Module> modules = new ArrayList<>();
modules.add(new GraphvizModule());
modules.add(new RuneLiteModule(ClientUpdateCheckMode.AUTO, true));
PluginManager pluginManager = new PluginManager(true, null, null, null, null, null);
pluginManager.loadCorePlugins();
for (Plugin p : pluginManager.getPlugins())
{
modules.add(p);
}
File file = folder.newFile();
try (PrintWriter out = new PrintWriter(file, "UTF-8"))
{
Injector injector = Guice.createInjector(modules);
GraphvizGrapher grapher = injector.getInstance(GraphvizGrapher.class);
grapher.setOut(out);
grapher.setRankdir("TB");
grapher.graph(injector);
}
}
}