Ignore:
Timestamp:
9/27/2008 9:00:44 AM (6 years ago)
Author:
lowjoel
Message:

Redesigned the Manager.Settings class.
-The plugin settings are no longer kept in a single dictionary; instead, they are distributed to be dealt with by the implementer of the Settings class. The GetSettings? and SetSettings? functions now are overloaded internally such that these values along with the plugins' GUID are sent together to the implementer class. The implementer class then stores it directly. This prevents unnecessary resource utilisation for large plugins and for plugins which are disabled
-This new design also allows for the serialisation and unserialisation of plugin settings which are non-primitive types, since the settings are loaded only on the initialisation of the plugin, not the initialisation of the settings object
-Created a SettingsChanged? event for classes to register callback functions to. This is useful when classes need to know when a setting is changed. Currently unimplemented though.
-Load is now a protected internal function which implementer classes must implement, this allows for the refreshing of settings whenever the program is running.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Eraser/Program.cs

    r367 r412  
    9999    { 
    100100        public Settings() 
     101        { 
     102            Load(); 
     103        } 
     104 
     105        protected override void Load() 
    101106        { 
    102107            RegistryKey key = Application.UserAppDataRegistry; 
     
    140145                } 
    141146 
    142             //Load the plugin settings. 
    143             byte[] pluginSettings = (byte[])key.GetValue("PluginSettings", new byte[0]); 
    144             if (pluginSettings.Length != 0) 
    145                 using (MemoryStream stream = new MemoryStream(pluginSettings)) 
    146                 { 
    147                     try 
    148                     { 
    149                         this.pluginSettings = (Dictionary<Guid, Dictionary<string, object>>) 
    150                             new BinaryFormatter().Deserialize(stream); 
    151                     } 
    152                     catch (Exception) 
    153                     { 
    154                         key.DeleteValue("PluginSettings"); 
    155                         MessageBox.Show(S._("Could not load plugin settings. All settings " + 
    156                             "have been lost."), S._("Eraser"), MessageBoxButtons.OK, 
    157                             MessageBoxIcon.Error); 
    158                     } 
    159                 } 
     147            base.Load(); 
    160148        } 
    161149 
     
    179167            using (MemoryStream stream = new MemoryStream()) 
    180168            { 
    181                 new BinaryFormatter().Serialize(stream, pluginSettings); 
    182                 key.SetValue("PluginSettings", stream.ToArray(), RegistryValueKind.Binary); 
    183             } 
    184  
    185             using (MemoryStream stream = new MemoryStream()) 
    186             { 
    187169                new BinaryFormatter().Serialize(stream, PlausibleDeniabilityFiles); 
    188170                key.SetValue("PlausibleDeniabilityFiles", stream.ToArray(), RegistryValueKind.Binary); 
    189171            } 
    190172        } 
     173 
     174        protected override Dictionary<string, object> GetSettings(Guid guid) 
     175        { 
     176            //Open the registry key containing the settings 
     177            RegistryKey pluginsKey = Application.UserAppDataRegistry.OpenSubKey( 
     178                "Plugins\\" + guid.ToString(), true); 
     179            Dictionary<string, object> result = new Dictionary<string, object>(); 
     180 
     181            //Load every key/value pair into the dictionary 
     182            if (pluginsKey != null) 
     183                foreach (string key in pluginsKey.GetValueNames()) 
     184                { 
     185                    byte[] currentSetting = (byte[])pluginsKey.GetValue(key, null); 
     186                    if (currentSetting.Length != 0) 
     187                        using (MemoryStream stream = new MemoryStream(currentSetting)) 
     188                        { 
     189                            try 
     190                            { 
     191                                result[key] = new BinaryFormatter().Deserialize(stream); 
     192                            } 
     193                            catch (Exception) 
     194                            { 
     195                                pluginsKey.DeleteValue(key); 
     196                                MessageBox.Show(string.Format(S._("Could not load the setting {0} for plugin {1}." + 
     197                                    "The setting has been lost."), key, guid.ToString()), 
     198                                    S._("Eraser"), MessageBoxButtons.OK, MessageBoxIcon.Error); 
     199                            } 
     200                        } 
     201                    else 
     202                        result[key] = null; 
     203                } 
     204 
     205            //We're done! 
     206            return result; 
     207        } 
     208 
     209        protected override void SetSettings(Guid guid, Dictionary<string, object> settings) 
     210        { 
     211            //Open the registry key containing the settings 
     212            RegistryKey pluginKey = Application.UserAppDataRegistry.OpenSubKey( 
     213                "Plugins\\" + guid.ToString(), true); 
     214            if (pluginKey == null) 
     215                pluginKey = Application.UserAppDataRegistry.CreateSubKey("Plugins\\" + guid.ToString()); 
     216 
     217            foreach (string key in settings.Keys) 
     218            { 
     219                using (MemoryStream stream = new MemoryStream()) 
     220                { 
     221                    new BinaryFormatter().Serialize(stream, settings[key]); 
     222                    pluginKey.SetValue(key, stream.ToArray(), RegistryValueKind.Binary); 
     223                } 
     224            } 
     225        } 
    191226    } 
    192227} 
Note: See TracChangeset for help on using the changeset viewer.