runelite-client: set default config by default
This commit is contained in:
@@ -77,7 +77,7 @@ public class RuneLite
|
||||
private WSClient wsclient;
|
||||
|
||||
private AccountSession accountSession;
|
||||
private ConfigManager configManager = new ConfigManager(eventBus);
|
||||
private final ConfigManager configManager = new ConfigManager(eventBus);
|
||||
|
||||
static
|
||||
{
|
||||
@@ -121,6 +121,10 @@ public class RuneLite
|
||||
pluginManager = new PluginManager(this);
|
||||
pluginManager.loadAll();
|
||||
|
||||
// Plugins have registered their config, so set default config
|
||||
// to main settings
|
||||
configManager.loadDefault();
|
||||
|
||||
renderer = new OverlayRenderer();
|
||||
|
||||
loadSession();
|
||||
@@ -221,8 +225,7 @@ public class RuneLite
|
||||
{
|
||||
// Initialize config for new session
|
||||
// If the session isn't logged in yet, don't switch to the new config
|
||||
configManager = new ConfigManager(eventBus, session);
|
||||
configManager.load();
|
||||
configManager.switchSession(session);
|
||||
}
|
||||
|
||||
eventBus.post(new SessionOpen());
|
||||
@@ -246,8 +249,7 @@ public class RuneLite
|
||||
accountSession = null; // No more account
|
||||
|
||||
// Restore config
|
||||
configManager = new ConfigManager(eventBus);
|
||||
configManager.load();
|
||||
configManager.switchSession(null);
|
||||
|
||||
eventBus.post(new SessionClose());
|
||||
}
|
||||
|
||||
@@ -80,17 +80,13 @@ class ConfigInvocationHandler implements InvocationHandler
|
||||
|
||||
// Convert value to return type
|
||||
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;
|
||||
}
|
||||
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/
|
||||
Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
|
||||
|
||||
@@ -30,9 +30,13 @@ import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -54,11 +58,13 @@ public class ConfigManager
|
||||
private final EventBus eventBus;
|
||||
private AccountSession session;
|
||||
private ConfigClient client;
|
||||
private File propertiesFile;
|
||||
|
||||
private final File propertiesFile;
|
||||
private final ConfigInvocationHandler handler = new ConfigInvocationHandler(this);
|
||||
private final Properties properties = new Properties();
|
||||
|
||||
private final List<Object> configProxies = new ArrayList<>();
|
||||
|
||||
public ConfigManager(EventBus eventBus)
|
||||
{
|
||||
this.eventBus = eventBus;
|
||||
@@ -68,10 +74,39 @@ public class ConfigManager
|
||||
public ConfigManager(EventBus eventBus, AccountSession session)
|
||||
{
|
||||
this.eventBus = eventBus;
|
||||
this.session = session;
|
||||
// if session username is null then dont..
|
||||
this.client = new ConfigClient(session.getUuid());
|
||||
switchSession(session);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
load(); // load profile specific config
|
||||
loadDefault(); // set defaults over anything not set
|
||||
}
|
||||
|
||||
private File getPropertiesFile()
|
||||
@@ -120,6 +155,8 @@ public class ConfigManager
|
||||
|
||||
for (ConfigEntry entry : configuration.getConfig())
|
||||
{
|
||||
logger.debug("Loading configuration value from client {}: {}", entry.getKey(), entry.getValue());
|
||||
|
||||
properties.setProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
@@ -137,6 +174,8 @@ public class ConfigManager
|
||||
|
||||
private void loadFromFile()
|
||||
{
|
||||
properties.clear();
|
||||
|
||||
try (FileInputStream in = new FileInputStream(propertiesFile))
|
||||
{
|
||||
properties.load(in);
|
||||
@@ -168,10 +207,14 @@ public class ConfigManager
|
||||
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
|
||||
}, handler);
|
||||
|
||||
configProxies.add(t);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
public String getConfiguration(String groupName, String key)
|
||||
@@ -269,4 +312,54 @@ public class ConfigManager
|
||||
.collect(Collectors.toList());
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,11 +44,6 @@ public abstract class Plugin extends AbstractIdleService
|
||||
return overlay != null ? Arrays.asList(overlay) : Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
public Object getConfig()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override AbstractIdleService's default executor to instead execute in
|
||||
* the main thread. Prevents plugins from all being initialized from
|
||||
|
||||
@@ -39,8 +39,6 @@ import net.runelite.client.RuneLite;
|
||||
import net.runelite.client.config.ConfigDescriptor;
|
||||
import net.runelite.client.config.ConfigItemDescriptor;
|
||||
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 static net.runelite.client.ui.PluginPanel.PANEL_HEIGHT;
|
||||
import static net.runelite.client.ui.PluginPanel.PANEL_WIDTH;
|
||||
@@ -76,16 +74,12 @@ public class ConfigPanel extends PluginPanel
|
||||
private Collection<ConfigDescriptor> getConfig()
|
||||
{
|
||||
List<ConfigDescriptor> list = new ArrayList<>();
|
||||
PluginManager pm = runelite.getPluginManager();
|
||||
for (Plugin plugin : pm.getPlugins())
|
||||
for (Object config : runelite.getConfigManager().getConfigProxies())
|
||||
{
|
||||
Object config = plugin.getConfig();
|
||||
if (config != null)
|
||||
{
|
||||
ConfigManager configManager = runelite.getConfigManager();
|
||||
ConfigDescriptor configDescriptor = configManager.getConfigDescriptor(config);
|
||||
list.add(configDescriptor);
|
||||
}
|
||||
ConfigManager configManager = runelite.getConfigManager();
|
||||
ConfigDescriptor configDescriptor = configManager.getConfigDescriptor(config);
|
||||
|
||||
list.add(configDescriptor);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ public class OpponentInfo extends Plugin
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpponentConfig getConfig()
|
||||
{
|
||||
return config;
|
||||
|
||||
Reference in New Issue
Block a user