Browse Source

Move more of the settings infrastructure to ILSpyX for reuse.

pull/3505/head
Siegfried Pammer 2 weeks ago
parent
commit
b6dc9fcfa9
  1. 6
      ICSharpCode.ILSpyX/Settings/DecompilerSettings.cs
  2. 75
      ICSharpCode.ILSpyX/Settings/SettingsServiceBase.cs
  3. 2
      ILSpy.ReadyToRun/ReadyToRunOptions.cs
  4. 2
      ILSpy/DecompilationOptions.cs
  5. 1
      ILSpy/LanguageSettings.cs
  6. 1
      ILSpy/Options/DecompilerSettingsViewModel.cs
  7. 2
      ILSpy/Options/DisplaySettings.cs
  8. 1
      ILSpy/SessionSettings.cs
  9. 2
      ILSpy/Updates/UpdateSettings.cs
  10. 57
      ILSpy/Util/SettingsService.cs
  11. 1
      TestPlugin/CustomOptionPage.xaml.cs

6
ILSpy/Options/DecompilerSettings.cs → 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 // 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 // software and associated documentation files (the "Software"), to deal in the Software
@ -21,9 +21,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Xml.Linq; using System.Xml.Linq;
#nullable enable namespace ICSharpCode.ILSpyX.Settings
namespace ICSharpCode.ILSpy.Options
{ {
public class DecompilerSettings : Decompiler.DecompilerSettings, ISettingsSection public class DecompilerSettings : Decompiler.DecompilerSettings, ISettingsSection
{ {

75
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<Type, ISettingsSection> sections = new();
protected ISettingsProvider SpySettings { get; set; } = spySettings;
public T GetSettings<T>() 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)
{
}
}
}

2
ILSpy.ReadyToRun/ReadyToRunOptions.cs

@ -18,7 +18,7 @@
using System.Xml.Linq; using System.Xml.Linq;
using ICSharpCode.ILSpy.Util; using ICSharpCode.ILSpyX.Settings;
using TomsToolbox.Wpf; using TomsToolbox.Wpf;

2
ILSpy/DecompilationOptions.cs

@ -23,7 +23,7 @@ using ICSharpCode.Decompiler;
using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpy.Options;
using ICSharpCode.ILSpyX; using ICSharpCode.ILSpyX;
using DecompilerSettings = ICSharpCode.ILSpy.Options.DecompilerSettings; using DecompilerSettings = ICSharpCode.ILSpyX.Settings.DecompilerSettings;
namespace ICSharpCode.ILSpy namespace ICSharpCode.ILSpy
{ {

1
ILSpy/LanguageSettings.cs

@ -20,6 +20,7 @@ using System.Collections.Generic;
using System.Xml.Linq; using System.Xml.Linq;
using ICSharpCode.ILSpyX; using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.Settings;
using TomsToolbox.Wpf; using TomsToolbox.Wpf;

1
ILSpy/Options/DecompilerSettingsViewModel.cs

@ -24,6 +24,7 @@ using System.Reflection;
using ICSharpCode.ILSpy.Properties; using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.TreeNodes; using ICSharpCode.ILSpy.TreeNodes;
using ICSharpCode.ILSpyX.Settings;
using TomsToolbox.Wpf; using TomsToolbox.Wpf;

2
ILSpy/Options/DisplaySettings.cs

@ -19,6 +19,8 @@
using System.Windows.Media; using System.Windows.Media;
using System.Xml.Linq; using System.Xml.Linq;
using ICSharpCode.ILSpyX.Settings;
using TomsToolbox.Wpf; using TomsToolbox.Wpf;
namespace ICSharpCode.ILSpy.Options namespace ICSharpCode.ILSpy.Options

1
ILSpy/SessionSettings.cs

@ -30,6 +30,7 @@ using System.Xml.Linq;
using ICSharpCode.ILSpy.Docking; using ICSharpCode.ILSpy.Docking;
using ICSharpCode.ILSpy.Themes; using ICSharpCode.ILSpy.Themes;
using ICSharpCode.ILSpyX.Search; using ICSharpCode.ILSpyX.Search;
using ICSharpCode.ILSpyX.Settings;
namespace ICSharpCode.ILSpy namespace ICSharpCode.ILSpy
{ {

2
ILSpy/Updates/UpdateSettings.cs

@ -19,6 +19,8 @@
using System; using System;
using System.Xml.Linq; using System.Xml.Linq;
using ICSharpCode.ILSpyX.Settings;
using TomsToolbox.Wpf; using TomsToolbox.Wpf;
namespace ICSharpCode.ILSpy.Updates namespace ICSharpCode.ILSpy.Updates

57
ILSpy/Util/SettingsService.cs

@ -16,71 +16,18 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Concurrent;
using System.ComponentModel; using System.ComponentModel;
using System.Xml.Linq;
using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpy.Options;
using ICSharpCode.ILSpyX; using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.Settings; using ICSharpCode.ILSpyX.Settings;
using DecompilerSettings = ICSharpCode.ILSpy.Options.DecompilerSettings; using DecompilerSettings = ICSharpCode.ILSpyX.Settings.DecompilerSettings;
#nullable enable #nullable enable
namespace ICSharpCode.ILSpy.Util 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<Type, ISettingsSection> sections = new();
protected ISettingsProvider SpySettings { get; set; } = spySettings;
public T GetSettings<T>() 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 class SettingsSnapshot(SettingsService parent, ISettingsProvider spySettings) : SettingsServiceBase(spySettings)
{ {
public void Save() public void Save()
@ -173,7 +120,7 @@ namespace ICSharpCode.ILSpy.Util
SpySettings.Update(root => { SpySettings.Update(root => {
SaveSection(section, root); SaveSection(section, root);
}); });
}; }
} }
if (sender is DecompilerSettings decompilerSettings && assemblyListManager != null) if (sender is DecompilerSettings decompilerSettings && assemblyListManager != null)

1
TestPlugin/CustomOptionPage.xaml.cs

@ -6,6 +6,7 @@ using System.Xml.Linq;
using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpy.Options;
using ICSharpCode.ILSpy.Util; using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpyX.Settings;
using TomsToolbox.Wpf; using TomsToolbox.Wpf;
using TomsToolbox.Wpf.Composition.AttributedModel; using TomsToolbox.Wpf.Composition.AttributedModel;

Loading…
Cancel
Save