From b6dc9fcfa994934a1ad241ac67ddd87968840f79 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 6 Jul 2025 09:52:32 +0200 Subject: [PATCH] Move more of the settings infrastructure to ILSpyX for reuse. --- .../Settings}/DecompilerSettings.cs | 6 +- .../Settings/SettingsServiceBase.cs | 75 +++++++++++++++++++ ILSpy.ReadyToRun/ReadyToRunOptions.cs | 2 +- ILSpy/DecompilationOptions.cs | 2 +- ILSpy/LanguageSettings.cs | 1 + ILSpy/Options/DecompilerSettingsViewModel.cs | 1 + ILSpy/Options/DisplaySettings.cs | 2 + ILSpy/SessionSettings.cs | 1 + ILSpy/Updates/UpdateSettings.cs | 2 + ILSpy/Util/SettingsService.cs | 57 +------------- TestPlugin/CustomOptionPage.xaml.cs | 1 + 11 files changed, 89 insertions(+), 61 deletions(-) rename {ILSpy/Options => ICSharpCode.ILSpyX/Settings}/DecompilerSettings.cs (94%) create mode 100644 ICSharpCode.ILSpyX/Settings/SettingsServiceBase.cs diff --git a/ILSpy/Options/DecompilerSettings.cs b/ICSharpCode.ILSpyX/Settings/DecompilerSettings.cs similarity index 94% rename from ILSpy/Options/DecompilerSettings.cs rename to ICSharpCode.ILSpyX/Settings/DecompilerSettings.cs index 4281b6f0f..817f002a0 100644 --- a/ILSpy/Options/DecompilerSettings.cs +++ b/ICSharpCode.ILSpyX/Settings/DecompilerSettings.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2024 Tom Englert for the SharpDevelop Team +// Copyright (c) 2024 Tom Englert // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -21,9 +21,7 @@ using System.Linq; using System.Reflection; using System.Xml.Linq; -#nullable enable - -namespace ICSharpCode.ILSpy.Options +namespace ICSharpCode.ILSpyX.Settings { public class DecompilerSettings : Decompiler.DecompilerSettings, ISettingsSection { diff --git a/ICSharpCode.ILSpyX/Settings/SettingsServiceBase.cs b/ICSharpCode.ILSpyX/Settings/SettingsServiceBase.cs new file mode 100644 index 000000000..96ce572c0 --- /dev/null +++ b/ICSharpCode.ILSpyX/Settings/SettingsServiceBase.cs @@ -0,0 +1,75 @@ +// Copyright (c) 2024 Tom Englert +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; +using System.Collections.Concurrent; +using System.ComponentModel; +using System.Xml.Linq; + +namespace ICSharpCode.ILSpyX.Settings +{ + public interface IChildSettings + { + ISettingsSection Parent { get; } + } + + public interface ISettingsSection : INotifyPropertyChanged + { + XName SectionName { get; } + + void LoadFromXml(XElement section); + + XElement SaveToXml(); + } + + public class SettingsServiceBase(ISettingsProvider spySettings) + { + protected readonly ConcurrentDictionary sections = new(); + + protected ISettingsProvider SpySettings { get; set; } = spySettings; + + public T GetSettings() where T : ISettingsSection, new() + { + return (T)sections.GetOrAdd(typeof(T), _ => { + T section = new T(); + + var sectionElement = SpySettings[section.SectionName]; + + section.LoadFromXml(sectionElement); + section.PropertyChanged += Section_PropertyChanged; + + return section; + }); + } + + protected static void SaveSection(ISettingsSection section, XElement root) + { + var element = section.SaveToXml(); + + var existingElement = root.Element(section.SectionName); + if (existingElement != null) + existingElement.ReplaceWith(element); + else + root.Add(element); + } + + protected virtual void Section_PropertyChanged(object? sender, PropertyChangedEventArgs e) + { + } + } +} diff --git a/ILSpy.ReadyToRun/ReadyToRunOptions.cs b/ILSpy.ReadyToRun/ReadyToRunOptions.cs index 9b13f7ba7..79ecb5bf0 100644 --- a/ILSpy.ReadyToRun/ReadyToRunOptions.cs +++ b/ILSpy.ReadyToRun/ReadyToRunOptions.cs @@ -18,7 +18,7 @@ using System.Xml.Linq; -using ICSharpCode.ILSpy.Util; +using ICSharpCode.ILSpyX.Settings; using TomsToolbox.Wpf; diff --git a/ILSpy/DecompilationOptions.cs b/ILSpy/DecompilationOptions.cs index c7c87322d..77078fa97 100644 --- a/ILSpy/DecompilationOptions.cs +++ b/ILSpy/DecompilationOptions.cs @@ -23,7 +23,7 @@ using ICSharpCode.Decompiler; using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpyX; -using DecompilerSettings = ICSharpCode.ILSpy.Options.DecompilerSettings; +using DecompilerSettings = ICSharpCode.ILSpyX.Settings.DecompilerSettings; namespace ICSharpCode.ILSpy { diff --git a/ILSpy/LanguageSettings.cs b/ILSpy/LanguageSettings.cs index c07b993ec..12fc1d2d5 100644 --- a/ILSpy/LanguageSettings.cs +++ b/ILSpy/LanguageSettings.cs @@ -20,6 +20,7 @@ using System.Collections.Generic; using System.Xml.Linq; using ICSharpCode.ILSpyX; +using ICSharpCode.ILSpyX.Settings; using TomsToolbox.Wpf; diff --git a/ILSpy/Options/DecompilerSettingsViewModel.cs b/ILSpy/Options/DecompilerSettingsViewModel.cs index a01fa1587..4d3999f7a 100644 --- a/ILSpy/Options/DecompilerSettingsViewModel.cs +++ b/ILSpy/Options/DecompilerSettingsViewModel.cs @@ -24,6 +24,7 @@ using System.Reflection; using ICSharpCode.ILSpy.Properties; using ICSharpCode.ILSpy.TreeNodes; +using ICSharpCode.ILSpyX.Settings; using TomsToolbox.Wpf; diff --git a/ILSpy/Options/DisplaySettings.cs b/ILSpy/Options/DisplaySettings.cs index 8b21987bb..9ff525959 100644 --- a/ILSpy/Options/DisplaySettings.cs +++ b/ILSpy/Options/DisplaySettings.cs @@ -19,6 +19,8 @@ using System.Windows.Media; using System.Xml.Linq; +using ICSharpCode.ILSpyX.Settings; + using TomsToolbox.Wpf; namespace ICSharpCode.ILSpy.Options diff --git a/ILSpy/SessionSettings.cs b/ILSpy/SessionSettings.cs index 59a939887..b11380c8b 100644 --- a/ILSpy/SessionSettings.cs +++ b/ILSpy/SessionSettings.cs @@ -30,6 +30,7 @@ using System.Xml.Linq; using ICSharpCode.ILSpy.Docking; using ICSharpCode.ILSpy.Themes; using ICSharpCode.ILSpyX.Search; +using ICSharpCode.ILSpyX.Settings; namespace ICSharpCode.ILSpy { diff --git a/ILSpy/Updates/UpdateSettings.cs b/ILSpy/Updates/UpdateSettings.cs index 4fd3a883a..bd7cee5d0 100644 --- a/ILSpy/Updates/UpdateSettings.cs +++ b/ILSpy/Updates/UpdateSettings.cs @@ -19,6 +19,8 @@ using System; using System.Xml.Linq; +using ICSharpCode.ILSpyX.Settings; + using TomsToolbox.Wpf; namespace ICSharpCode.ILSpy.Updates diff --git a/ILSpy/Util/SettingsService.cs b/ILSpy/Util/SettingsService.cs index 80b7dc6e0..1a87e6a7e 100644 --- a/ILSpy/Util/SettingsService.cs +++ b/ILSpy/Util/SettingsService.cs @@ -16,71 +16,18 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -using System; -using System.Collections.Concurrent; using System.ComponentModel; -using System.Xml.Linq; using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpyX; using ICSharpCode.ILSpyX.Settings; -using DecompilerSettings = ICSharpCode.ILSpy.Options.DecompilerSettings; +using DecompilerSettings = ICSharpCode.ILSpyX.Settings.DecompilerSettings; #nullable enable namespace ICSharpCode.ILSpy.Util { - public interface IChildSettings - { - ISettingsSection Parent { get; } - } - - public interface ISettingsSection : INotifyPropertyChanged - { - XName SectionName { get; } - - void LoadFromXml(XElement section); - - XElement SaveToXml(); - } - - public abstract class SettingsServiceBase(ISettingsProvider spySettings) - { - protected readonly ConcurrentDictionary sections = new(); - - protected ISettingsProvider SpySettings { get; set; } = spySettings; - - public T GetSettings() where T : ISettingsSection, new() - { - return (T)sections.GetOrAdd(typeof(T), _ => { - T section = new T(); - - var sectionElement = SpySettings[section.SectionName]; - - section.LoadFromXml(sectionElement); - section.PropertyChanged += Section_PropertyChanged; - - return section; - }); - } - - protected static void SaveSection(ISettingsSection section, XElement root) - { - var element = section.SaveToXml(); - - var existingElement = root.Element(section.SectionName); - if (existingElement != null) - existingElement.ReplaceWith(element); - else - root.Add(element); - } - - protected virtual void Section_PropertyChanged(object? sender, PropertyChangedEventArgs e) - { - } - } - public class SettingsSnapshot(SettingsService parent, ISettingsProvider spySettings) : SettingsServiceBase(spySettings) { public void Save() @@ -173,7 +120,7 @@ namespace ICSharpCode.ILSpy.Util SpySettings.Update(root => { SaveSection(section, root); }); - }; + } } if (sender is DecompilerSettings decompilerSettings && assemblyListManager != null) diff --git a/TestPlugin/CustomOptionPage.xaml.cs b/TestPlugin/CustomOptionPage.xaml.cs index 95116a743..e28e5ec44 100644 --- a/TestPlugin/CustomOptionPage.xaml.cs +++ b/TestPlugin/CustomOptionPage.xaml.cs @@ -6,6 +6,7 @@ using System.Xml.Linq; using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpy.Util; +using ICSharpCode.ILSpyX.Settings; using TomsToolbox.Wpf; using TomsToolbox.Wpf.Composition.AttributedModel;