Example #1
0
        protected void SaveSettingsObject(object settingsObject)
        {
            try
            {
                Type t = settingsObject.GetType();
                SettingsFileHandler globalSerializer = new SettingsFileHandler(GetGlobalFilePath(t));
                SettingsFileHandler userSerializer   = new SettingsFileHandler(GetUserFilePath(t));
                foreach (PropertyInfo property in t.GetProperties())
                {
                    SettingAttribute att = GetSettingAttribute(property);
                    if (att == null)
                    {
                        continue;
                    }
                    SettingsFileHandler s = (att.SettingScope == SettingScope.Global ? globalSerializer : userSerializer);
                    object value          = property.GetValue(settingsObject, null);
                    s.SetValue(property.Name, value);
                }
                globalSerializer.Close();
                userSerializer.Close();

                // Send a message that the setting has been changed.
                SettingsManagerMessaging.SendSettingsChangeMessage(t);
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Error("SettingsManager: Error writing settings of type '{0}'... Will clear settings files for this setting", e, settingsObject.GetType().Name);
                RemoveSettingsData(settingsObject.GetType(), true, true);
            }
        }
Example #2
0
        protected object LoadSettingsObject(Type settingsType)
        {
            SettingsFileHandler globalHandler = new SettingsFileHandler(GetGlobalFilePath(settingsType));
            SettingsFileHandler userHandler   = new SettingsFileHandler(GetUserFilePath(settingsType));

            try
            {
                globalHandler.Load();
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Error("SettingsManager: Error loading global settings file for setting type '{0}'... Will clear this settings file.", e, settingsType.Name);
                globalHandler.Clear();
                RemoveSettingsData(settingsType, false, true);
            }
            try
            {
                userHandler.Load();
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Error("SettingsManager: Error loading user settings file for setting type '{0}'... Will clear this settings file.", e, settingsType.Name);
                userHandler.Clear();
                RemoveSettingsData(settingsType, true, false);
            }
            try
            {
                object result = Activator.CreateInstance(settingsType);
                foreach (PropertyInfo property in result.GetType().GetProperties())
                {
                    SettingAttribute att = GetSettingAttribute(property);
                    if (att == null)
                    {
                        continue;
                    }
                    SettingsFileHandler s = (att.SettingScope == SettingScope.Global ? globalHandler : userHandler);
                    try
                    {
                        object value = s.GetValue(property.Name, property.PropertyType);
                        if (value == null)
                        {
                            if (att.HasDefault)
                            {
                                value = att.DefaultValue;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        property.SetValue(result, value, null);
                    }
                    catch (Exception e)
                    {
                        ServiceRegistration.Get <ILogger>().Error("SettingsManager: Error setting property '{0}' in settings of type '{1}'" +
                                                                  (att.HasDefault ? ", using default value" : string.Empty), e, property.Name, settingsType.Name);
                        if (att.HasDefault)
                        {
                            property.SetValue(result, att.DefaultValue, null);
                        }
                    }
                }
                if (!globalHandler.SettingsFileExists && !userHandler.SettingsFileExists)
                {
                    SaveSettingsObject(result);
                }
                return(result);
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Error("SettingsManager: Error loading settings of type '{0}'", e, settingsType.Name);
                return(null);
            }
        }