diff --git a/src/Engine/Config/Config.cs b/src/Engine/Config/Config.cs index dc88b90f5..c0c6291d5 100644 --- a/src/Engine/Config/Config.cs +++ b/src/Engine/Config/Config.cs @@ -51,20 +51,25 @@ public class Config : PermanentRemoteObject #endif protected bool m_IsCleanConfig; protected Hashtable m_Preferences = Hashtable.Synchronized(new Hashtable()); + private Dictionary m_Overrides; public Version PreviousVersion { get; private set; } public Version CurrentVersion { get; private set; } - internal Dictionary Overrides { get; private set; } + public event EventHandler Changed; public object this[string key] { get { - // config overrides - lock (Overrides) { - foreach (var @override in Overrides) { - var pattern = @override.Key; - var value = @override.Value; - if (Pattern.IsMatch(key, pattern)) { - return value; + // config overrides hook + if (m_Overrides != null) { + lock (m_Overrides) { + foreach (var @override in m_Overrides) { + var pattern = @override.Key; + var value = @override.Value; + if (Pattern.IsMatch(key, pattern)) { + // honor config overrides by returning the + // overridden value instead + return value; + } } } } @@ -79,15 +84,19 @@ public object this[string key] { return; } - // config overrides - lock (Overrides) { - foreach (var @override in Overrides) { - var pattern = @override.Key; - if (Pattern.IsMatch(key, pattern)) { -#if LOG4NET - _Logger.Debug("set[]: ignoring setting an overridden config key " + key + "."); -#endif - return; + // config overrides hook + if (m_Overrides != null) { + lock (m_Overrides) { + foreach (var @override in m_Overrides) { + var pattern = @override.Key; + if (Pattern.IsMatch(key, pattern)) { + // honor config overrides by ignoring sets (writes) to + // overridden keys + #if LOG4NET + _Logger.Debug("set[]: ignoring setting an overridden config key " + key + "."); + #endif + return; + } } } } @@ -103,6 +112,16 @@ public object this[string key] { } } } + + internal Dictionary Overrides { + get { + var overrides = m_Overrides; + if (overrides == null) { + m_Overrides = new Dictionary(); + } + return m_Overrides; + } + } public bool IsCleanConfig { get { @@ -112,7 +131,6 @@ public bool IsCleanConfig { public Config() { - Overrides = new Dictionary(); #if CONFIG_NINI m_ConfigPath = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), "smuxi"); diff --git a/src/Engine/Engine.cs b/src/Engine/Engine.cs index e6cd9f1b6..60c151ae7 100644 --- a/src/Engine/Engine.cs +++ b/src/Engine/Engine.cs @@ -179,28 +179,31 @@ public static void Init() _Config.Save(); // config overrides - // we only scan all config keys so we can convert the override - // value into the right type - foreach (var kvp in _Config.GetAll()) { - var configKey = kvp.Key; - var configValue = kvp.Value; - - foreach (var @override in ConfigOverrides) { - var keyPattern = @override.Key; + if (ConfigOverrides != null) { + // we only scan all config keys here so we can convert the override + // string value into the actual type + foreach (var kvp in _Config.GetAll()) { + var configKey = kvp.Key; + var configValue = kvp.Value; + + foreach (var @override in ConfigOverrides) { + var keyPattern = @override.Key; + if (!Pattern.IsMatch(configKey, keyPattern)) { + continue; + } - if (!Pattern.IsMatch(configKey, keyPattern)) { - continue; - } + // convert string to actual type + var overridenValue = Convert.ChangeType(@override.Value, + configValue.GetType()); - // convert string to type - var overridenValue = Convert.ChangeType(@override.Value, - configValue.GetType()); - // as the patttern could match many keys we only need one - // override with the right value type - if (_Config.Overrides.ContainsKey(keyPattern)) { - continue; + // as a patttern could match many keys we only need one + // override entry in _Config.Overrides with the right type and value + if (_Config.Overrides.ContainsKey(keyPattern)) { + // config key pattern exist already + continue; + } + _Config.Overrides.Add(keyPattern, overridenValue); } - _Config.Overrides.Add(keyPattern, overridenValue); } }