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); } } }
/// <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)); }
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); } } }
/// <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)); }
/// <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); } }
public SettingsRemoteState(SettingObject defaultSettings) => RemoteSettings = defaultSettings;
private SettingsFile(string absoluteFilePath, SettingObject templateSettings) : base(absoluteFilePath) { TemplateSettings = templateSettings; }
/// <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));
private SettingSyntaxTree(RootJsonSyntax jsonSyntaxTree, SettingObject settingObject) { JsonSyntaxTree = jsonSyntaxTree; SettingObject = settingObject; }