Esempio n. 1
0
        private IEnumerable <SettingProperty> ChangedProperties(SettingObject newSettings)
        {
            PValueEqualityComparer eq = PValueEqualityComparer.Instance;

            foreach (var property in Settings.Schema.AllProperties)
            {
                // Change if added, updated or deleted.
                if (Settings.TryGetRawValue(property, out PValue oldValue))
                {
                    if (newSettings.TryGetRawValue(property, out PValue newValue))
                    {
                        if (!eq.AreEqual(oldValue, newValue))
                        {
                            // Updated.
                            yield return(property);
                        }
                    }
                    else
                    {
                        // Deleted.
                        yield return(property);
                    }
                }
                else if (newSettings.TryGetRawValue(property, out _))
                {
                    // Added.
                    yield return(property);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Compares this <see cref="SettingCopy"/> with a <see cref="SettingObject"/> and returns if they are equal.
        /// </summary>
        /// <param name="other">
        /// The <see cref="SettingObject"/> to compare with.
        /// </param>
        /// <returns>
        /// true if both <see cref="SettingObject"/> instances are equal; otherwise false.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="other"/> is null.
        /// </exception>
        /// <remarks>
        /// This is not the same as complete equality, in particular this method returns true from the following expression:
        /// <code>
        /// workingCopy.Commit().EqualTo(workingCopy)
        /// </code>
        /// where workingCopy is a <see cref="SettingCopy"/>. Or even:
        /// <code>
        /// workingCopy.Commit().CreateWorkingCopy().EqualTo(workingCopy)
        /// </code>
        /// </remarks>
        public bool EqualTo(SettingObject other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            return(ToPMap().EqualTo(other.Map));
        }
Esempio n. 3
0
        protected override void OnFileUpdated(EventArgs e)
        {
            base.OnFileUpdated(e);

            SettingObject newSettings = ReadSettingObject(LoadedText);

            foreach (var property in ChangedProperties(newSettings))
            {
                // Update settings if at least one property changed.
                Settings = newSettings;

                if (settingsChangedEvents.TryGetValue(property.Name, out WeakEvent <object, EventArgs> keyedEvent))
                {
                    keyedEvent.Raise(this, EventArgs.Empty);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Generates the text to save to the setting file from the current values in <paramref name="settings"/>.
        /// </summary>
        /// <param name="settings">
        /// The settings to write.
        /// </param>
        /// <param name="options">
        /// Specifies options for writing the settings.
        /// </param>
        /// <returns>
        /// The text to save.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="settings"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="settings"/> has an unexpected schema.
        /// </exception>
        public string GenerateJson(SettingObject settings, SettingWriterOptions options)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (settings.Schema != TemplateSettings.Schema)
            {
                throw new ArgumentException(
                          $"{nameof(settings)} has an unexpected schema",
                          nameof(settings));
            }

            return(SettingWriter.ConvertToJson(
                       settings.Map,
                       schema: settings.Schema,
                       options: options));
        }
Esempio n. 5
0
        /// <summary>
        /// Reverts to the state of a <see cref="SettingObject"/>.
        /// </summary>
        /// <param name="settingObject">
        /// The <see cref="SettingObject"/> to revert to.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="settingObject"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="settingObject"/> does not have the same schema.
        /// </exception>
        public void Revert(SettingObject settingObject)
        {
            if (settingObject == null)
            {
                throw new ArgumentNullException(nameof(settingObject));
            }
            if (settingObject.Schema != Schema)
            {
                throw new ArgumentException($"Cannot revert to a {nameof(SettingObject)} with a different schema.");
            }

            // Clear out the mapping before copying key-value pairs.
            KeyValueMapping.Clear();

            // No need to copy values if they can be assumed read-only or are structs.
            foreach (var kv in settingObject.Map)
            {
                KeyValueMapping.Add(kv.Key, kv.Value);
            }
        }
Esempio n. 6
0
 public SettingsRemoteState(SettingObject defaultSettings) => RemoteSettings = defaultSettings;
Esempio n. 7
0
 private SettingsFile(string absoluteFilePath, SettingObject templateSettings)
     : base(absoluteFilePath)
 {
     TemplateSettings = templateSettings;
 }
Esempio n. 8
0
 /// <summary>
 /// Attempts to overwrite the setting file with the current values in <paramref name="settings"/>.
 /// </summary>
 /// <param name="settings">
 /// The settings to write.
 /// </param>
 /// <param name="options">
 /// Specifies options for writing the settings.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="settings"/> is null.
 /// </exception>
 /// <exception cref="ArgumentException">
 /// <paramref name="settings"/> has an unexpected schema.
 /// </exception>
 public void WriteToFile(SettingObject settings, SettingWriterOptions options)
 => Save(GenerateJson(settings, options));
Esempio n. 9
0
 private SettingSyntaxTree(RootJsonSyntax jsonSyntaxTree, SettingObject settingObject)
 {
     JsonSyntaxTree = jsonSyntaxTree;
     SettingObject  = settingObject;
 }