From 1370b999fe4e7e6cd00f54833608a8a3a2d6254e Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 7 Jan 2024 18:02:08 +0100 Subject: [PATCH] Fix #2893: Add option to disable automatic assembly loading. 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. --- ICSharpCode.Decompiler/DecompilerSettings.cs | 15 +++++++++++++++ ILSpy/Commands/GeneratePdbContextMenuEntry.cs | 2 +- ILSpy/Languages/CSharpLanguage.cs | 6 +++--- ILSpy/Properties/Resources.Designer.cs | 9 +++++++++ ILSpy/Properties/Resources.resx | 3 +++ ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs | 2 +- 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/ICSharpCode.Decompiler/DecompilerSettings.cs b/ICSharpCode.Decompiler/DecompilerSettings.cs index e0da5f5ce..1032093b3 100644 --- a/ICSharpCode.Decompiler/DecompilerSettings.cs +++ b/ICSharpCode.Decompiler/DecompilerSettings.cs @@ -1959,6 +1959,21 @@ namespace ICSharpCode.Decompiler } } + 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; diff --git a/ILSpy/Commands/GeneratePdbContextMenuEntry.cs b/ILSpy/Commands/GeneratePdbContextMenuEntry.cs index 84bafc423..64e8acfef 100644 --- a/ILSpy/Commands/GeneratePdbContextMenuEntry.cs +++ b/ILSpy/Commands/GeneratePdbContextMenuEntry.cs @@ -80,7 +80,7 @@ namespace ICSharpCode.ILSpy { 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); } diff --git a/ILSpy/Languages/CSharpLanguage.cs b/ILSpy/Languages/CSharpLanguage.cs index b098c8c4a..fbf2a1ce0 100644 --- a/ILSpy/Languages/CSharpLanguage.cs +++ b/ILSpy/Languages/CSharpLanguage.cs @@ -122,7 +122,7 @@ namespace ICSharpCode.ILSpy 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) @@ -416,7 +416,7 @@ namespace ICSharpCode.ILSpy 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) @@ -507,7 +507,7 @@ namespace ICSharpCode.ILSpy 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; diff --git a/ILSpy/Properties/Resources.Designer.cs b/ILSpy/Properties/Resources.Designer.cs index 07d6d8832..1866a7b29 100644 --- a/ILSpy/Properties/Resources.Designer.cs +++ b/ILSpy/Properties/Resources.Designer.cs @@ -774,6 +774,15 @@ namespace ICSharpCode.ILSpy.Properties { } } + /// + /// Looks up a localized string similar to Automatically load assembly references. + /// + public static string DecompilerSettings_AutoLoadAssemblyReferences { + get { + return ResourceManager.GetString("DecompilerSettings.AutoLoadAssemblyReferences", resourceCulture); + } + } + /// /// Looks up a localized string similar to User-defined checked operators. /// diff --git a/ILSpy/Properties/Resources.resx b/ILSpy/Properties/Resources.resx index e7bc5c4e7..cc08fca35 100644 --- a/ILSpy/Properties/Resources.resx +++ b/ILSpy/Properties/Resources.resx @@ -282,6 +282,9 @@ Are you sure you want to continue? Decompile async IAsyncEnumerator methods + + Automatically load assembly references + User-defined checked operators diff --git a/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs b/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs index d2a5b138f..ad003ffcc 100644 --- a/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs +++ b/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs @@ -75,7 +75,7 @@ namespace ICSharpCode.ILSpy.TreeNodes 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) {