Exemplo n.º 1
0
        /// <summary>
        /// Loads a settings profile from the given file.
        /// </summary>
        /// <param name="filePath">The path of the file from which to load settings.</param>
        /// <param name="setAsCurrent">If <c>true</c>, the loaded profile will also be set as <see cref="CurrentProfile"/>.</param>
        /// <param name="parent">The profile to use as parent for the loaded profile. If <c>null</c>, a default profile will be used.</param>
        /// <returns><c>true</c> if settings were correctly loaded, <c>false</c> otherwise.</returns>
        public SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent = null)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }

            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                return(null);
            }

            SettingsProfile profile;

            try
            {
                SettingsFile settingsFile;
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    settingsFile = (SettingsFile)YamlSerializer.Deserialize(stream);
                }
                profile = new SettingsProfile(this, parent ?? defaultProfile)
                {
                    FilePath = filePath
                };

                foreach (var settings in settingsFile.Settings)
                {
                    SettingsKey key;
                    var         value      = settings.Value;
                    object      finalValue = value;
                    if (settingsKeys.TryGetValue(settings.Key, out key))
                    {
                        finalValue = key.ConvertValue(value);
                    }
                    profile.SetValue(settings.Key, finalValue);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
                return(null);
            }

            profileList.Add(profile);
            if (setAsCurrent)
            {
                CurrentProfile = profile;
            }

            var handler = SettingsFileLoaded;

            if (handler != null)
            {
                SettingsFileLoaded(null, new SettingsFileLoadedEventArgs(filePath));
            }
            return(profile);
        }
Exemplo n.º 2
0
 internal void DecodeSettings(SettingsDictionary settingsDictionary, SettingsProfile profile)
 {
     foreach (var settings in settingsDictionary)
     {
         SettingsKey key;
         var         value      = settings.Value;
         object      finalValue = value;
         if (settingsKeys.TryGetValue(settings.Key, out key))
         {
             finalValue = key.ConvertValue(value);
         }
         profile.SetValue(settings.Key, finalValue);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Copies the values of this profile into another profile.
        /// </summary>
        /// <param name="profile">The profile in which to copy the values.</param>
        /// <param name="overrideValues">If <c>false</c>, the values already present in the targt profile won't be overriden.</param>
        public void CopyTo(SettingsProfile profile, bool overrideValues)
        {
            lock (SettingsContainer.SettingsLock)
            {
                foreach (var setting in Settings)
                {
                    if (!overrideValues && profile.Settings.ContainsKey(setting.Key))
                    {
                        continue;
                    }

                    profile.SetValue(setting.Key, setting.Value.Value);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reloads a profile from its file, updating the value that have changed.
        /// </summary>
        /// <param name="profile">The profile to reload.</param>
        public void ReloadSettingsProfile(SettingsProfile profile)
        {
            var filePath = profile.FilePath;

            if (filePath == null)
            {
                throw new ArgumentException("profile");
            }
            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                throw new ArgumentException("profile");
            }

            try
            {
                SettingsFile settingsFile;
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    settingsFile = (SettingsFile)YamlSerializer.Deserialize(stream);
                }

                foreach (var settings in settingsFile.Settings)
                {
                    SettingsKey key;
                    var         value      = settings.Value;
                    object      finalValue = value;
                    if (settingsKeys.TryGetValue(settings.Key, out key))
                    {
                        finalValue = key.ConvertValue(value);
                    }
                    profile.SetValue(settings.Key, finalValue);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
            }

            var handler = SettingsFileLoaded;

            if (handler != null)
            {
                SettingsFileLoaded(null, new SettingsFileLoadedEventArgs(filePath));
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Sets the value of this settings key in the given profile.
 /// </summary>
 /// <param name="value">The new value to set.</param>
 /// <param name="profile">The profile in which to set the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 public void SetValue(T value, SettingsProfile profile = null)
 {
     profile = profile ?? Group.CurrentProfile;
     profile.SetValue(Name, value);
 }
Exemplo n.º 6
0
 internal void DecodeSettings(SettingsDictionary settingsDictionary, SettingsProfile profile)
 {
     foreach (var settings in settingsDictionary)
     {
         SettingsKey key;
         var value = settings.Value;
         object finalValue = value;
         if (settingsKeys.TryGetValue(settings.Key, out key))
         {
             finalValue = key.ConvertValue(value);
         }
         profile.SetValue(settings.Key, finalValue);
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Sets the value of this settings key in the given profile.
 /// </summary>
 /// <param name="value">The new value to set.</param>
 /// <param name="profile">The profile in which to set the value. If <c>null</c>, it will look in the <see cref="SettingsGroup.CurrentProfile"/>.</param>
 public void SetValue(object value, SettingsProfile profile = null)
 {
     profile = profile ?? Group.CurrentProfile;
     profile.SetValue(Name, value);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Loads a settings profile from the given file.
        /// </summary>
        /// <param name="filePath">The path of the file from which to load settings.</param>
        /// <param name="setAsCurrent">If <c>true</c>, the loaded profile will also be set as <see cref="CurrentProfile"/>.</param>
        /// <param name="parent">The profile to use as parent for the loaded profile. If <c>null</c>, a default profile will be used.</param>
        /// <returns><c>true</c> if settings were correctly loaded, <c>false</c> otherwise.</returns>
        public SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent = null)
        {
            if (filePath == null) throw new ArgumentNullException("filePath");

            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                return null;
            }

            SettingsProfile profile;
            try
            {
                SettingsFile settingsFile;
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    settingsFile = (SettingsFile)YamlSerializer.Deserialize(stream);
                }
                profile = new SettingsProfile(this, parent ?? defaultProfile) { FilePath = filePath };

                foreach (var settings in settingsFile.Settings)
                {
                    SettingsKey key;
                    var value = settings.Value;
                    object finalValue = value;
                    if (settingsKeys.TryGetValue(settings.Key, out key))
                    {
                        finalValue = key.ConvertValue(value);
                    }
                    profile.SetValue(settings.Key, finalValue);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
                return null;
            }

            profileList.Add(profile);
            if (setAsCurrent)
            {
                CurrentProfile = profile;
            }
            
            var handler = SettingsFileLoaded;
            if (handler != null)
            {
                SettingsFileLoaded(null, new SettingsFileLoadedEventArgs(filePath));
            }
            return profile;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Reloads a profile from its file, updating the value that have changed.
        /// </summary>
        /// <param name="profile">The profile to reload.</param>
        public void ReloadSettingsProfile(SettingsProfile profile)
        {
            var filePath = profile.FilePath;
            if (filePath == null) throw new ArgumentException("profile");
            if (!File.Exists(filePath))
            {
                Logger.Error("Settings file [{0}] was not found", filePath);
                throw new ArgumentException("profile");
            }

            try
            {
                SettingsFile settingsFile;
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    settingsFile = (SettingsFile)YamlSerializer.Deserialize(stream);
                }

                foreach (var settings in settingsFile.Settings)
                {
                    SettingsKey key;
                    var value = settings.Value;
                    object finalValue = value;
                    if (settingsKeys.TryGetValue(settings.Key, out key))
                    {
                        finalValue = key.ConvertValue(value);
                    }
                    profile.SetValue(settings.Key, finalValue);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport());
            }

            var handler = SettingsFileLoaded;
            if (handler != null)
            {
                SettingsFileLoaded(null, new SettingsFileLoadedEventArgs(filePath));
            }
        }