pluginmanager: Load externals only on startup (#1793)

This commit is contained in:
Owain van Brakel
2019-10-18 01:11:07 +02:00
committed by Ganom
parent bdf947775e
commit b52cf1e93b
3 changed files with 17 additions and 127 deletions

View File

@@ -323,20 +323,19 @@ public class RuneLite
}
// Load user configuration
RuneLiteSplashScreen.stage(.57, "Loading user config");
configManager.load();
// Load the session, including saved configuration
sessionManager.loadSession();
RuneLiteSplashScreen.stage(.58, "Loading session data");
// Begin watching for new plugins
pluginManager.watch();
sessionManager.loadSession();
// Tell the plugin manager if client is outdated or not
pluginManager.setOutdated(isOutdated);
// Load external plugins
pluginManager.loadExternalPlugins();
// Load the plugins, but does not start them yet.
// This will initialize configuration
pluginManager.loadCorePlugins();

View File

@@ -30,18 +30,10 @@ import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
import java.util.Objects;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import net.runelite.client.RuneLite;
import net.runelite.client.config.Config;
@@ -50,106 +42,33 @@ import net.runelite.client.config.OpenOSRSConfig;
@Singleton
@Slf4j
public class PluginWatcher extends Thread
public class ExternalPluginLoader
{
private static final File BASE = RuneLite.PLUGIN_DIR;
private final OpenOSRSConfig OpenOSRSConfig;
private final PluginManager pluginManager;
private final WatchService watchService;
private final WatchKey watchKey;
@Inject
private ConfigManager configManager;
@Inject
public PluginWatcher(OpenOSRSConfig OpenOSRSConfig, PluginManager pluginManager) throws IOException
public ExternalPluginLoader(OpenOSRSConfig OpenOSRSConfig, PluginManager pluginManager)
{
this.OpenOSRSConfig = OpenOSRSConfig;
this.pluginManager = pluginManager;
setName("Plugin Watcher");
setDaemon(true);
watchService = FileSystems.getDefault().newWatchService();
BASE.mkdirs();
Path dir = BASE.toPath();
watchKey = dir.register(watchService, ENTRY_MODIFY, ENTRY_DELETE);
}
public void cancel()
public void scanAndLoad()
{
watchKey.cancel();
}
@Override
public void run()
{
if (OpenOSRSConfig.enablePlugins())
if (!OpenOSRSConfig.enablePlugins())
{
scan();
return;
}
for (; ; )
{
try
{
WatchKey key = watchService.take();
Thread.sleep(50);
if (!OpenOSRSConfig.enablePlugins())
{
key.reset();
continue;
}
for (WatchEvent<?> event : key.pollEvents())
{
Kind<?> kind = event.kind();
Path path = (Path) event.context();
File file = new File(BASE, path.toFile().getName());
log.debug("Event {} file {}", kind, file);
if (kind == ENTRY_MODIFY)
{
Plugin existing = findPluginForFile(file);
if (existing != null)
{
log.info("Reloading plugin {}", file);
unload(existing);
}
else
{
log.info("Loading plugin {}", file);
}
load(file);
}
else if (kind == ENTRY_DELETE)
{
Plugin existing = findPluginForFile(file);
if (existing != null)
{
log.info("Unloading plugin {}", file);
unload(existing);
}
}
}
key.reset();
}
catch (InterruptedException ex)
{
log.warn("error polling for plugins", ex);
}
}
}
private void scan()
{
for (File file : BASE.listFiles())
for (File file : Objects.requireNonNull(BASE.listFiles()))
{
if (!file.getName().endsWith(".jar"))
{
@@ -160,18 +79,6 @@ public class PluginWatcher extends Thread
}
}
private Plugin findPluginForFile(File file)
{
for (Plugin plugin : pluginManager.getPlugins())
{
if (plugin.file != null && plugin.file.equals(file))
{
return plugin;
}
}
return null;
}
private void load(File pluginFile)
{
PluginClassLoader loader;
@@ -242,22 +149,6 @@ public class PluginWatcher extends Thread
pluginManager.add(plugin);
}
private void unload(Plugin plugin)
{
try
{
pluginManager.stopPlugin(plugin);
}
catch (PluginInstantiationException ex)
{
log.warn("unable to stop plugin", ex);
}
pluginManager.remove(plugin); // remove it regardless
close(plugin.loader);
}
private void close(URLClassLoader classLoader)
{
try

View File

@@ -99,7 +99,7 @@ public class PluginManager
.getAnnotation(ConfigGroup.class).value();
@Inject
PluginWatcher pluginWatcher;
ExternalPluginLoader externalPluginLoader;
@Setter
boolean isOutdated;
@@ -128,11 +128,6 @@ public class PluginManager
}
}
public void watch()
{
pluginWatcher.start();
}
private void onSessionOpen(SessionOpen event)
{
refreshPlugins();
@@ -210,6 +205,11 @@ public class PluginManager
}
}
public void loadExternalPlugins()
{
externalPluginLoader.scanAndLoad();
}
public void loadCorePlugins() throws IOException
{
plugins.addAll(scanAndInstantiate(getClass().getClassLoader(), PLUGIN_PACKAGE));