Skip to content

Commit bf5249b

Browse files
Add --ilspy-settingsfile and -ds|--decompiler-setting name=value options
1 parent b6dc9fc commit bf5249b

File tree

2 files changed

+94
-9
lines changed

2 files changed

+94
-9
lines changed

ICSharpCode.ILSpyCmd/IlspyCmdProgram.cs

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class ILSpyCmdProgram
9191
public (bool IsSet, string Value) InputPDBFile { get; }
9292

9393
[Option("-l|--list <entity-type(s)>", "Lists all entities of the specified type(s). Valid types: c(lass), i(nterface), s(truct), d(elegate), e(num)", CommandOptionType.MultipleValue)]
94-
public string[] EntityTypes { get; } = new string[0];
94+
public string[] EntityTypes { get; } = Array.Empty<string>();
9595

9696
public string DecompilerVersion => "ilspycmd: " + typeof(ILSpyCmdProgram).Assembly.GetName().Version.ToString() +
9797
Environment.NewLine
@@ -100,9 +100,16 @@ class ILSpyCmdProgram
100100

101101
[Option("-lv|--languageversion <version>", "C# Language version: CSharp1, CSharp2, CSharp3, " +
102102
"CSharp4, CSharp5, CSharp6, CSharp7, CSharp7_1, CSharp7_2, CSharp7_3, CSharp8_0, CSharp9_0, " +
103-
"CSharp10_0, Preview or Latest", CommandOptionType.SingleValue)]
103+
"CSharp10_0, CSharp11_0, CSharp12_0, CSharp13_0, Preview or Latest", CommandOptionType.SingleValue)]
104104
public LanguageVersion LanguageVersion { get; } = LanguageVersion.Latest;
105105

106+
[FileExists]
107+
[Option("--ilspy-settingsfile <path>", "Path to an ILSpy settings file.", CommandOptionType.SingleValue)]
108+
public string ILSpySettingsFile { get; }
109+
110+
[Option("-ds|--decompiler-setting <name>=<value>", "Set a decompiler setting. Use multiple times to set multiple settings.", CommandOptionType.MultipleValue)]
111+
public string[] DecompilerSettingOverrides { get; set; } = Array.Empty<string>();
112+
106113
[DirectoryExists]
107114
[Option("-r|--referencepath <path>", "Path to a directory containing dependencies of the assembly that is being decompiled.", CommandOptionType.MultipleValue)]
108115
public string[] ReferencePaths { get; }
@@ -325,13 +332,76 @@ private static string ResolveOutputDirectory(string outputDirectory)
325332

326333
DecompilerSettings GetSettings(PEFile module)
327334
{
328-
return new DecompilerSettings(LanguageVersion) {
329-
ThrowOnAssemblyResolveErrors = false,
330-
RemoveDeadCode = RemoveDeadCode,
331-
RemoveDeadStores = RemoveDeadStores,
332-
UseSdkStyleProjectFormat = WholeProjectDecompiler.CanUseSdkStyleProjectFormat(module),
333-
UseNestedDirectoriesForNamespaces = NestedDirectories,
334-
};
335+
DecompilerSettings decompilerSettings = null;
336+
337+
if (ILSpySettingsFile != null)
338+
{
339+
try
340+
{
341+
ILSpyX.Settings.ILSpySettings.SettingsFilePathProvider = new ILSpyX.Settings.DefaultSettingsFilePathProvider(ILSpySettingsFile);
342+
var settingsService = new ILSpyX.Settings.SettingsServiceBase(ILSpyX.Settings.ILSpySettings.Load());
343+
decompilerSettings = settingsService.GetSettings<ILSpyX.Settings.DecompilerSettings>();
344+
}
345+
catch (Exception ex)
346+
{
347+
Console.Error.WriteLine($"Error loading ILSpy settings file '{ILSpySettingsFile}': {ex.Message}");
348+
}
349+
}
350+
351+
if (decompilerSettings == null)
352+
{
353+
decompilerSettings = new DecompilerSettings(LanguageVersion) {
354+
ThrowOnAssemblyResolveErrors = false,
355+
RemoveDeadCode = RemoveDeadCode,
356+
RemoveDeadStores = RemoveDeadStores,
357+
UseSdkStyleProjectFormat = WholeProjectDecompiler.CanUseSdkStyleProjectFormat(module),
358+
UseNestedDirectoriesForNamespaces = NestedDirectories,
359+
};
360+
}
361+
362+
if (DecompilerSettingOverrides is { Length: > 0 })
363+
{
364+
foreach (var entry in DecompilerSettingOverrides)
365+
{
366+
int equals = entry.IndexOf('=');
367+
if (equals <= 0)
368+
{
369+
Console.Error.WriteLine($"Decompiler setting '{entry}' is invalid; use '<Name>=<Value>'");
370+
continue;
371+
}
372+
373+
string name = entry[..equals].Trim();
374+
string value = entry[(equals + 1)..].Trim();
375+
376+
if (!ILSpyX.Settings.DecompilerSettings.IsKnownOption(name, out var property))
377+
{
378+
Console.Error.WriteLine($"Decompiler setting '{name}' is unknown.");
379+
continue;
380+
}
381+
382+
object typedValue;
383+
384+
try
385+
{
386+
typedValue = Convert.ChangeType(value, property.PropertyType);
387+
}
388+
catch (Exception)
389+
{
390+
Console.Error.WriteLine($"Decompiler setting '{name}': Value '{value}' could not be converted to '{property.PropertyType.FullName}'.");
391+
continue;
392+
}
393+
394+
if (typedValue == null && property.PropertyType.IsValueType)
395+
{
396+
Console.Error.WriteLine($"Decompiler setting '{name}': Value '{value}' could not be converted to '{property.PropertyType.FullName}'.");
397+
continue;
398+
}
399+
400+
property.SetValue(decompilerSettings, typedValue);
401+
}
402+
}
403+
404+
return decompilerSettings;
335405
}
336406

337407
CSharpDecompiler GetDecompiler(string assemblyFileName)

ICSharpCode.ILSpyX/Settings/DecompilerSettings.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// DEALINGS IN THE SOFTWARE.
1818

1919
using System.ComponentModel;
20+
using System.Diagnostics.CodeAnalysis;
2021
using System.Linq;
2122
using System.Reflection;
2223
using System.Xml.Linq;
@@ -57,5 +58,19 @@ public override DecompilerSettings Clone()
5758
{
5859
return (DecompilerSettings)base.Clone();
5960
}
61+
62+
public static bool IsKnownOption(string name, [NotNullWhen(true)] out PropertyInfo? property)
63+
{
64+
property = null;
65+
foreach (var item in properties)
66+
{
67+
if (item.Name != name)
68+
continue;
69+
property = item;
70+
return true;
71+
}
72+
73+
return false;
74+
}
6075
}
6176
}

0 commit comments

Comments
 (0)