Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Settings to ILSpyX #2869

Merged
merged 5 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions ICSharpCode.ILSpyX/AssemblyListManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,10 @@
using System.Xml.Linq;

using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.ILSpyX.Settings;

namespace ICSharpCode.ILSpyX
{
public interface ISettingsProvider
{
XElement this[XName section] { get; }

void Update(Action<XElement> action);
ISettingsProvider Load();
}

/// <summary>
/// Manages the available assembly lists.
///
Expand Down
40 changes: 40 additions & 0 deletions ICSharpCode.ILSpyX/Settings/DefaultSettingsFilePathProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2022 AlphaSierraPapa for the SharpDevelop Team
//
// 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;

namespace ICSharpCode.ILSpyX.Settings
{
/// <summary>
/// Used in scenarios where the user passes a path and that is to be used, eg ilspycmd parameter
/// </summary>
public class DefaultSettingsFilePathProvider : ISettingsFilePathProvider
{
private readonly string _providedPath;

public DefaultSettingsFilePathProvider(string providedPath)
{
_providedPath = providedPath;
}

public string GetSettingsFilePath()
{
return _providedPath;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@
using System.Xml;
using System.Xml.Linq;

using ICSharpCode.ILSpyX;

namespace ICSharpCode.ILSpy
namespace ICSharpCode.ILSpyX.Settings
{
/// <summary>
/// Manages IL Spy settings.
/// </summary>
public class ILSpySettings : ISettingsProvider
{
/// <summary>
/// This settings file path provider determines where to load settings file from, includes filename
/// </summary>
public static ISettingsFilePathProvider? SettingsFilePathProvider { get; set; }

readonly XElement root;

ILSpySettings()
Expand Down Expand Up @@ -62,6 +65,11 @@ public static ILSpySettings Load()
try
{
XDocument doc = LoadWithoutCheckingCharacters(GetConfigFile());
if (null == doc.Root)
{
return new ILSpySettings();
}

return new ILSpySettings(doc.Root);
}
catch (IOException)
Expand All @@ -87,7 +95,7 @@ public static void SaveSettings(XElement section)
{
Update(
delegate (XElement root) {
XElement existingElement = root.Element(section.Name);
XElement? existingElement = root.Element(section.Name);
if (existingElement != null)
existingElement.ReplaceWith(section);
else
Expand All @@ -113,14 +121,14 @@ public static void Update(Action<XElement> action)
catch (IOException)
{
// ensure the directory exists
Directory.CreateDirectory(Path.GetDirectoryName(config));
Directory.CreateDirectory(Path.GetDirectoryName(config)!);
doc = new XDocument(new XElement("ILSpy"));
}
catch (XmlException)
{
doc = new XDocument(new XElement("ILSpy"));
}
doc.Root.SetAttributeValue("version", DecompilerVersionInfo.Major + "." + DecompilerVersionInfo.Minor + "." + DecompilerVersionInfo.Build + "." + DecompilerVersionInfo.Revision);
doc.Root!.SetAttributeValue("version", DecompilerVersionInfo.Major + "." + DecompilerVersionInfo.Minor + "." + DecompilerVersionInfo.Build + "." + DecompilerVersionInfo.Revision);
action(doc.Root);
doc.Save(config, SaveOptions.None);
}
Expand All @@ -133,12 +141,11 @@ void ISettingsProvider.Update(Action<XElement> action)

static string GetConfigFile()
{
if (App.CommandLineArguments.ConfigFile != null)
return App.CommandLineArguments.ConfigFile;
string localPath = Path.Combine(Path.GetDirectoryName(typeof(MainWindow).Assembly.Location), "ILSpy.xml");
if (File.Exists(localPath))
return localPath;
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ICSharpCode\\ILSpy.xml");
if (null != SettingsFilePathProvider)
return SettingsFilePathProvider.GetSettingsFilePath();

throw new ArgumentNullException(nameof(SettingsFilePathProvider));
// return "ILSpy.xml";
}

ISettingsProvider ISettingsProvider.Load()
Expand Down
42 changes: 42 additions & 0 deletions ICSharpCode.ILSpyX/Settings/IMiscSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2022 AlphaSierraPapa for the SharpDevelop Team
//
// 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.Xml.Linq;

namespace ICSharpCode.ILSpyX.Settings
{
public interface IMiscSettings
{
public bool AllowMultipleInstances { get; set; }
public bool LoadPreviousAssemblies { get; set; }

public static void Save(XElement root, IMiscSettings miscSettings)
{
var section = new XElement("MiscSettings");
section.SetAttributeValue(nameof(miscSettings.AllowMultipleInstances), miscSettings.AllowMultipleInstances);
section.SetAttributeValue(nameof(miscSettings.LoadPreviousAssemblies), miscSettings.LoadPreviousAssemblies);

XElement? existingElement = root.Element("MiscSettings");
if (existingElement != null)
existingElement.ReplaceWith(section);
else
root.Add(section);
}
}
}
27 changes: 27 additions & 0 deletions ICSharpCode.ILSpyX/Settings/ISettingsFilePathProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2022 AlphaSierraPapa for the SharpDevelop Team
//
// 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;

namespace ICSharpCode.ILSpyX.Settings
{
public interface ISettingsFilePathProvider
{
string GetSettingsFilePath();
}
}
69 changes: 69 additions & 0 deletions ICSharpCode.ILSpyX/Settings/ISettingsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
//
// 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.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;

using static System.Collections.Specialized.BitVector32;

namespace ICSharpCode.ILSpyX.Settings
{
public interface ISettingsProvider
{
XElement this[XName section] { get; }

void Update(Action<XElement> action);
ISettingsProvider Load();

public static ICSharpCode.Decompiler.DecompilerSettings LoadDecompilerSettings(ISettingsProvider settingsProvider)
{
XElement e = settingsProvider["DecompilerSettings"];
var newSettings = new Decompiler.DecompilerSettings();
var properties = typeof(Decompiler.DecompilerSettings).GetProperties()
.Where(p => p.GetCustomAttribute<BrowsableAttribute>()?.Browsable != false);
foreach (var p in properties)
{
var value = (bool?)e.Attribute(p.Name);
if (value.HasValue)
p.SetValue(newSettings, value.Value);
}
return newSettings;
}

public static void SaveDecompilerSettings(XElement root, ICSharpCode.Decompiler.DecompilerSettings newSettings)
{
var properties = typeof(Decompiler.DecompilerSettings).GetProperties()
.Where(p => p.GetCustomAttribute<BrowsableAttribute>()?.Browsable != false);

XElement section = new XElement("DecompilerSettings");
foreach (var p in properties)
{
section.SetAttributeValue(p.Name, p.GetValue(newSettings));
}

XElement? existingElement = root.Element("DecompilerSettings");
if (existingElement != null)
existingElement.ReplaceWith(section);
else
root.Add(section);
}
}
}
32 changes: 32 additions & 0 deletions ICSharpCode.ILSpyX/Settings/ISettingsSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2022 AlphaSierraPapa for the SharpDevelop Team
//
// 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;

namespace ICSharpCode.ILSpyX.Settings
{
public interface ISettingsSection<TSelf>
{
// This should be abstract, but that needs C# 11.0 (see IParseable<TSelf>)
// Keep it to be enabled in the future
public static TSelf Load(ISettingsProvider settingsProvider)
{
throw new NotImplementedException();
}
}
}
43 changes: 43 additions & 0 deletions ICSharpCode.ILSpyX/Settings/MiscSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2022 AlphaSierraPapa for the SharpDevelop Team
//
// 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.Xml.Linq;

namespace ICSharpCode.ILSpyX.Settings
{
public class MiscSettings : IMiscSettings, ISettingsSection<MiscSettings>
{
private MiscSettings()
{
}

public bool AllowMultipleInstances { get; set; }
public bool LoadPreviousAssemblies { get; set; }

public static MiscSettings Load(ISettingsProvider settingsProvider)
{
XElement e = settingsProvider["MiscSettings"];
var s = new MiscSettings();
s.AllowMultipleInstances = (bool?)e.Attribute(nameof(s.AllowMultipleInstances)) ?? false;
s.LoadPreviousAssemblies = (bool?)e.Attribute(nameof(s.LoadPreviousAssemblies)) ?? true;

return s;
}
}
}
1 change: 1 addition & 0 deletions ILSpy.ReadyToRun/ReadyToRunOptionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Xml.Linq;

using ICSharpCode.ILSpy.Options;
using ICSharpCode.ILSpyX.Settings;

namespace ICSharpCode.ILSpy.ReadyToRun
{
Expand Down
1 change: 1 addition & 0 deletions ILSpy.ReadyToRun/ReadyToRunOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Xml.Linq;

using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.Settings;

namespace ICSharpCode.ILSpy.ReadyToRun
{
Expand Down
1 change: 1 addition & 0 deletions ILSpy/AboutPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.TextView;
using ICSharpCode.ILSpy.Themes;
using ICSharpCode.ILSpyX.Settings;

namespace ICSharpCode.ILSpy
{
Expand Down
3 changes: 3 additions & 0 deletions ILSpy/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Windows.Threading;

using ICSharpCode.ILSpy.Options;
using ICSharpCode.ILSpyX.Settings;

using Microsoft.VisualStudio.Composition;

Expand Down Expand Up @@ -59,6 +60,8 @@ internal class ExceptionData

public App()
{
ILSpySettings.SettingsFilePathProvider = new ILSpySettingsFilePathProvider();

var cmdArgs = Environment.GetCommandLineArgs().Skip(1);
App.CommandLineArguments = new CommandLineArguments(cmdArgs);
bool forceSingleInstance = (App.CommandLineArguments.SingleInstance ?? true)
Expand Down
Loading