Skip to content

Commit

Permalink
Fix #2893: Add option to disable automatic assembly loading.
Browse files Browse the repository at this point in the history
This setting is honored by all functionality that triggers a decompilation run. It is ignored by features that load assemblies as their primary function. For example, using the "Load Dependencies" feature will still resolve and load assemblies from the file-system. The same happens when you double-click on an assembly reference in the tree view. It will be resolved and loaded.

Note that disabling automatic assembly load will cause the decompiler to potentially not be able to resolve types from references that have not been added manually and the quality of the decompiled code will be inferior as a result.
  • Loading branch information
siegfriedpammer committed Jan 7, 2024
1 parent 075d616 commit 1370b99
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
15 changes: 15 additions & 0 deletions ICSharpCode.Decompiler/DecompilerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,21 @@ public bool ApplyWindowsRuntimeProjections {
}
}

bool autoLoadAssemblyReferences = true;

[Category("DecompilerSettings.Other")]
[Description("DecompilerSettings.AutoLoadAssemblyReferences")]
public bool AutoLoadAssemblyReferences {
get { return autoLoadAssemblyReferences; }
set {
if (autoLoadAssemblyReferences != value)
{
autoLoadAssemblyReferences = value;
OnPropertyChanged();
}
}
}

#endregion

bool forStatement = true;
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/Commands/GeneratePdbContextMenuEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal static void GeneratePdbForAssembly(LoadedAssembly assembly)
{
try
{
var decompiler = new CSharpDecompiler(file, assembly.GetAssemblyResolver(), options.DecompilerSettings);
var decompiler = new CSharpDecompiler(file, assembly.GetAssemblyResolver(options.DecompilerSettings.AutoLoadAssemblyReferences), options.DecompilerSettings);
decompiler.CancellationToken = ct;
PortablePdbWriter.WritePdb(file, decompiler, options.DecompilerSettings, stream, progress: options.Progress);
}
Expand Down
6 changes: 3 additions & 3 deletions ILSpy/Languages/CSharpLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public override IReadOnlyList<LanguageVersion> LanguageVersions {

CSharpDecompiler CreateDecompiler(PEFile module, DecompilationOptions options)
{
CSharpDecompiler decompiler = new CSharpDecompiler(module, module.GetAssemblyResolver(), options.DecompilerSettings);
CSharpDecompiler decompiler = new CSharpDecompiler(module, module.GetAssemblyResolver(options.DecompilerSettings.AutoLoadAssemblyReferences), options.DecompilerSettings);
decompiler.CancellationToken = options.CancellationToken;
decompiler.DebugInfoProvider = module.GetDebugInfoOrNull();
while (decompiler.AstTransforms.Count > transformCount)
Expand Down Expand Up @@ -416,7 +416,7 @@ public override ProjectId DecompileAssembly(LoadedAssembly assembly, ITextOutput
base.DecompileAssembly(assembly, output, options);

// don't automatically load additional assemblies when an assembly node is selected in the tree view
IAssemblyResolver assemblyResolver = assembly.GetAssemblyResolver(loadOnDemand: options.FullDecompilation);
IAssemblyResolver assemblyResolver = assembly.GetAssemblyResolver(loadOnDemand: options.FullDecompilation && options.DecompilerSettings.AutoLoadAssemblyReferences);
var typeSystem = new DecompilerTypeSystem(module, assemblyResolver, options.DecompilerSettings);
var globalType = typeSystem.MainModule.TypeDefinitions.FirstOrDefault();
if (globalType != null)
Expand Down Expand Up @@ -507,7 +507,7 @@ class ILSpyWholeProjectDecompiler : WholeProjectDecompiler
readonly DecompilationOptions options;

public ILSpyWholeProjectDecompiler(LoadedAssembly assembly, DecompilationOptions options)
: base(options.DecompilerSettings, assembly.GetAssemblyResolver(), assembly.GetAssemblyReferenceClassifier(options.DecompilerSettings.ApplyWindowsRuntimeProjections), assembly.GetDebugInfoOrNull())
: base(options.DecompilerSettings, assembly.GetAssemblyResolver(options.DecompilerSettings.AutoLoadAssemblyReferences, options.DecompilerSettings.ApplyWindowsRuntimeProjections), assembly.GetAssemblyReferenceClassifier(options.DecompilerSettings.ApplyWindowsRuntimeProjections), assembly.GetDebugInfoOrNull())
{
this.assembly = assembly;
this.options = options;
Expand Down
9 changes: 9 additions & 0 deletions ILSpy/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ILSpy/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ Are you sure you want to continue?</value>
<data name="DecompilerSettings.AsyncEnumerator" xml:space="preserve">
<value>Decompile async IAsyncEnumerator methods</value>
</data>
<data name="DecompilerSettings.AutoLoadAssemblyReferences" xml:space="preserve">
<value>Automatically load assembly references</value>
</data>
<data name="DecompilerSettings.CheckedOperators" xml:space="preserve">
<value>User-defined checked operators</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override void ActivateItem(System.Windows.RoutedEventArgs e)

protected override void LoadChildren()
{
var resolver = parentAssembly.LoadedAssembly.GetAssemblyResolver();
var resolver = parentAssembly.LoadedAssembly.GetAssemblyResolver(MainWindow.Instance.CurrentDecompilerSettings.AutoLoadAssemblyReferences);
var module = resolver.Resolve(r);
if (module != null)
{
Expand Down

0 comments on commit 1370b99

Please sign in to comment.