runelite-client: set default config by default

This commit is contained in:
Adam
2017-05-20 10:58:38 -04:00
parent ade6a2ae97
commit 9e8bf482cb
6 changed files with 115 additions and 36 deletions

View File

@@ -77,7 +77,7 @@ public class RuneLite
private WSClient wsclient; private WSClient wsclient;
private AccountSession accountSession; private AccountSession accountSession;
private ConfigManager configManager = new ConfigManager(eventBus); private final ConfigManager configManager = new ConfigManager(eventBus);
static static
{ {
@@ -121,6 +121,10 @@ public class RuneLite
pluginManager = new PluginManager(this); pluginManager = new PluginManager(this);
pluginManager.loadAll(); pluginManager.loadAll();
// Plugins have registered their config, so set default config
// to main settings
configManager.loadDefault();
renderer = new OverlayRenderer(); renderer = new OverlayRenderer();
loadSession(); loadSession();
@@ -221,8 +225,7 @@ public class RuneLite
{ {
// Initialize config for new session // Initialize config for new session
// If the session isn't logged in yet, don't switch to the new config // If the session isn't logged in yet, don't switch to the new config
configManager = new ConfigManager(eventBus, session); configManager.switchSession(session);
configManager.load();
} }
eventBus.post(new SessionOpen()); eventBus.post(new SessionOpen());
@@ -246,8 +249,7 @@ public class RuneLite
accountSession = null; // No more account accountSession = null; // No more account
// Restore config // Restore config
configManager = new ConfigManager(eventBus); configManager.switchSession(null);
configManager.load();
eventBus.post(new SessionClose()); eventBus.post(new SessionClose());
} }

View File

@@ -80,17 +80,13 @@ class ConfigInvocationHandler implements InvocationHandler
// Convert value to return type // Convert value to return type
Class<?> returnType = method.getReturnType(); Class<?> returnType = method.getReturnType();
if (returnType == value.getClass()) Object objectValue = ConfigManager.stringToObject(value, returnType);
if (!objectValue.getClass().equals(returnType))
{ {
return value; logger.warn("Unable to convert return type for configuration item {}.{}: {}", group.keyName(), item.keyName(), returnType);
} }
if (returnType == Boolean.class)
{
return Boolean.valueOf(value);
}
logger.warn("Unknown return type for configuration item {}.{}: {}", group.keyName(), item.keyName(), returnType);
return null; return null;
} }
else else
@@ -121,7 +117,7 @@ class ConfigInvocationHandler implements InvocationHandler
} }
} }
private Object callDefaultMethod(Object proxy, Method method, Object[] args) throws Throwable static Object callDefaultMethod(Object proxy, Method method, Object[] args) throws Throwable
{ {
// Call the default method implementation - https://rmannibucau.wordpress.com/2014/03/27/java-8-default-interface-methods-and-jdk-dynamic-proxies/ // Call the default method implementation - https://rmannibucau.wordpress.com/2014/03/27/java-8-default-interface-methods-and-jdk-dynamic-proxies/
Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class); Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);

View File

@@ -30,9 +30,13 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -54,11 +58,13 @@ public class ConfigManager
private final EventBus eventBus; private final EventBus eventBus;
private AccountSession session; private AccountSession session;
private ConfigClient client; private ConfigClient client;
private File propertiesFile;
private final File propertiesFile;
private final ConfigInvocationHandler handler = new ConfigInvocationHandler(this); private final ConfigInvocationHandler handler = new ConfigInvocationHandler(this);
private final Properties properties = new Properties(); private final Properties properties = new Properties();
private final List<Object> configProxies = new ArrayList<>();
public ConfigManager(EventBus eventBus) public ConfigManager(EventBus eventBus)
{ {
this.eventBus = eventBus; this.eventBus = eventBus;
@@ -68,10 +74,39 @@ public class ConfigManager
public ConfigManager(EventBus eventBus, AccountSession session) public ConfigManager(EventBus eventBus, AccountSession session)
{ {
this.eventBus = eventBus; this.eventBus = eventBus;
this.session = session; switchSession(session);
// if session username is null then dont.. }
this.client = new ConfigClient(session.getUuid());
public Collection<Object> getConfigProxies()
{
return Collections.unmodifiableCollection(configProxies);
}
public void loadDefault()
{
for (Object config : configProxies)
{
setDefaultConfiguration(config);
}
}
public final void switchSession(AccountSession session)
{
if (session == null)
{
this.session = null;
this.client = null;
}
else
{
this.session = session;
this.client = new ConfigClient(session.getUuid());
}
this.propertiesFile = getPropertiesFile(); this.propertiesFile = getPropertiesFile();
load(); // load profile specific config
loadDefault(); // set defaults over anything not set
} }
private File getPropertiesFile() private File getPropertiesFile()
@@ -120,6 +155,8 @@ public class ConfigManager
for (ConfigEntry entry : configuration.getConfig()) for (ConfigEntry entry : configuration.getConfig())
{ {
logger.debug("Loading configuration value from client {}: {}", entry.getKey(), entry.getValue());
properties.setProperty(entry.getKey(), entry.getValue()); properties.setProperty(entry.getKey(), entry.getValue());
} }
@@ -137,6 +174,8 @@ public class ConfigManager
private void loadFromFile() private void loadFromFile()
{ {
properties.clear();
try (FileInputStream in = new FileInputStream(propertiesFile)) try (FileInputStream in = new FileInputStream(propertiesFile))
{ {
properties.load(in); properties.load(in);
@@ -168,10 +207,14 @@ public class ConfigManager
throw new RuntimeException("Non-public configuration classes can't have default methods invoked"); throw new RuntimeException("Non-public configuration classes can't have default methods invoked");
} }
return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] T t = (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]
{ {
clazz clazz
}, handler); }, handler);
configProxies.add(t);
return t;
} }
public String getConfiguration(String groupName, String key) public String getConfiguration(String groupName, String key)
@@ -269,4 +312,54 @@ public class ConfigManager
.collect(Collectors.toList()); .collect(Collectors.toList());
return new ConfigDescriptor(group, items); return new ConfigDescriptor(group, items);
} }
private void setDefaultConfiguration(Object proxy)
{
Class<?> clazz = proxy.getClass().getInterfaces()[0];
ConfigGroup group = clazz.getAnnotation(ConfigGroup.class);
if (group == null)
{
return;
}
for (Method method : clazz.getDeclaredMethods())
{
ConfigItem item = method.getAnnotation(ConfigItem.class);
if (item == null || !method.isDefault())
{
continue;
}
String current = getConfiguration(group.keyName(), item.keyName());
if (current != null)
{
continue; // something else is already set
}
Object defaultValue;
try
{
defaultValue = ConfigInvocationHandler.callDefaultMethod(proxy, method, null);
}
catch (Throwable ex)
{
logger.warn(null, ex);
continue;
}
logger.debug("Setting default configuration value for {}.{} to {}", group.keyName(), item.keyName(), defaultValue);
setConfiguration(group.keyName(), item.keyName(), defaultValue.toString());
}
}
static Object stringToObject(String str, Class<?> type)
{
if (type == boolean.class)
{
return Boolean.parseBoolean(str);
}
return str;
}
} }

View File

@@ -44,11 +44,6 @@ public abstract class Plugin extends AbstractIdleService
return overlay != null ? Arrays.asList(overlay) : Collections.EMPTY_LIST; return overlay != null ? Arrays.asList(overlay) : Collections.EMPTY_LIST;
} }
public Object getConfig()
{
return null;
}
/** /**
* Override AbstractIdleService's default executor to instead execute in * Override AbstractIdleService's default executor to instead execute in
* the main thread. Prevents plugins from all being initialized from * the main thread. Prevents plugins from all being initialized from

View File

@@ -39,8 +39,6 @@ import net.runelite.client.RuneLite;
import net.runelite.client.config.ConfigDescriptor; import net.runelite.client.config.ConfigDescriptor;
import net.runelite.client.config.ConfigItemDescriptor; import net.runelite.client.config.ConfigItemDescriptor;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginManager;
import net.runelite.client.ui.PluginPanel; import net.runelite.client.ui.PluginPanel;
import static net.runelite.client.ui.PluginPanel.PANEL_HEIGHT; import static net.runelite.client.ui.PluginPanel.PANEL_HEIGHT;
import static net.runelite.client.ui.PluginPanel.PANEL_WIDTH; import static net.runelite.client.ui.PluginPanel.PANEL_WIDTH;
@@ -76,16 +74,12 @@ public class ConfigPanel extends PluginPanel
private Collection<ConfigDescriptor> getConfig() private Collection<ConfigDescriptor> getConfig()
{ {
List<ConfigDescriptor> list = new ArrayList<>(); List<ConfigDescriptor> list = new ArrayList<>();
PluginManager pm = runelite.getPluginManager(); for (Object config : runelite.getConfigManager().getConfigProxies())
for (Plugin plugin : pm.getPlugins())
{ {
Object config = plugin.getConfig(); ConfigManager configManager = runelite.getConfigManager();
if (config != null) ConfigDescriptor configDescriptor = configManager.getConfigDescriptor(config);
{
ConfigManager configManager = runelite.getConfigManager(); list.add(configDescriptor);
ConfigDescriptor configDescriptor = configManager.getConfigDescriptor(config);
list.add(configDescriptor);
}
} }
return list; return list;
} }

View File

@@ -55,7 +55,6 @@ public class OpponentInfo extends Plugin
{ {
} }
@Override
public OpponentConfig getConfig() public OpponentConfig getConfig()
{ {
return config; return config;