From c8ab6a1e05bf25c332ff56d0a8004f5cf964a883 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 6 Feb 2011 12:27:13 +0100 Subject: [PATCH] Handle failed assembly loads. --- ILSpy/ILSpy.csproj | 2 ++ ILSpy/Images/AssemblyList.png | Bin 0 -> 448 bytes ILSpy/Images/AssemblyWarning.png | Bin 0 -> 666 bytes ILSpy/Images/Images.cs | 1 + ILSpy/TextView/DecompilerTextView.cs | 11 ++++-- ILSpy/TreeNodes/AssemblyTreeNode.cs | 52 +++++++++++++++++++-------- 6 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 ILSpy/Images/AssemblyList.png create mode 100644 ILSpy/Images/AssemblyWarning.png diff --git a/ILSpy/ILSpy.csproj b/ILSpy/ILSpy.csproj index b966307e5..309936e65 100644 --- a/ILSpy/ILSpy.csproj +++ b/ILSpy/ILSpy.csproj @@ -99,6 +99,8 @@ Code + + diff --git a/ILSpy/Images/AssemblyList.png b/ILSpy/Images/AssemblyList.png new file mode 100644 index 0000000000000000000000000000000000000000..f3fd11cea47b43768dad0a220cb51439ffc460a2 GIT binary patch literal 448 zcmV;x0YCnUP)004R>004l5008;`004mK004C`008P>0026e000+ooVrmw00009 za7bBm000ia000ia0czHX2><{98FWQhbW?9;ba!ELWdKlNX>N2bPDNB8b~7$DE;i7E zty%y80TD?=K~y+TV_<-SJNKU(Y&&q#VC~K`%xzOv^}l)d@&D(q-~PV_(hnZL`hWG- zlm8obo%-*eSoI&K2ZcU;@$Ua+n~wdTy<%^BMdyO!kDtAUXnykg|CL*h|DU{Y=l{|T z2mgD;mSHzw_0|*j+orC)KV{)|>&&VNOK#kK{Qt(Cr~l7hzW4vYiL3v&9XS7g+T!j1 zJ)%pn8-VPb_`>$UiF4O|%&VXNKdrp~e_TPw|A_47|6v&o|DD4MHevMwx?n-$jFj|> zi3PFw?FB%M1%8Q@1#VGA1waGT2pZ5ocVpnGi?=@>JA3{A-lG@)Z{Bm_|B6lf|Ib{y q?SFD{7j_qn#sX%zj>ZBV^8x_sBC9m{_jL{c0000004R=004l4008;_004mL004C`008P>0026e000+nl3&F}00009 za7bBm000tl000tl0bXTW;{X5v8FWQhbW?9;ba!ELWdKlNX>N2bPDNB8b~7$DE;i7E zty%y80qaRbK~y+T?UCI}lW`QspJufYrQOsMMs$&Pk={fQU3BAB(1u7at0*aN(hHR? zie|73Y@};oH?ERcL|(*vol!)iE*3SGreoRKtoBSd^JS&mnl{>}Jr7xkJ^ck8IPg1g zzMu2`og)bULv;f;91m%^*^AkBpNrS-($Hk5#?;2ChN~PtYsPTegsRxq(Tg0}Y?gFp zfw^Rw*)Q`*6El2}KI0EY@eYr%&rq`*5}(koPR{RgcXnOi`{KXFva3A@su5z72|T_a z&c{CD(H&3^OCsVJ@f}4Z&G?&w{B{5b1;|I zVkj@8$FY@MvIwi?IM;8vR$OE0B8me!V#B2;lHs)w*I>D{8);Anv+MCc6DhB#SFHXm z74i6xQ9CtQ9Qq;UMPmZq{vvV_0X`Oxn=Hh7Ss$vazpYih`S{EIRjD^?AXoq)fg=YC z>24QcG!KbxU}QkVXuMLT3W&H%y1#lfq+YEe?GtFO-A=$&mahd;UJc*!S$(oLtO|Jd zXm6?Aa%j56REeC{#!6a$z2&<3{899Jvr7%|6J08!N(fl#&j0`b07*qoM6N<$f|VsA Ai~s-t literal 0 HcmV?d00001 diff --git a/ILSpy/Images/Images.cs b/ILSpy/Images/Images.cs index dfc427401..56313f3f7 100644 --- a/ILSpy/Images/Images.cs +++ b/ILSpy/Images/Images.cs @@ -16,6 +16,7 @@ namespace ICSharpCode.ILSpy } public static readonly BitmapImage Assembly = LoadBitmap("Assembly"); + public static readonly BitmapImage AssemblyWarning = LoadBitmap("AssemblyWarning"); public static readonly BitmapImage Library = LoadBitmap("Library"); public static readonly BitmapImage Namespace = LoadBitmap("NameSpace"); diff --git a/ILSpy/TextView/DecompilerTextView.cs b/ILSpy/TextView/DecompilerTextView.cs index 5907898b8..1d89ff7b5 100644 --- a/ILSpy/TextView/DecompilerTextView.cs +++ b/ILSpy/TextView/DecompilerTextView.cs @@ -20,6 +20,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; @@ -27,7 +28,6 @@ using System.Windows.Controls; using System.Windows.Media.Animation; using System.Windows.Threading; using System.Xml; - using ICSharpCode.AvalonEdit.Folding; using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting.Xshd; @@ -98,11 +98,16 @@ namespace ICSharpCode.ILSpy.TextView textEditor.SyntaxHighlighting = language.SyntaxHighlighting; textEditor.Text = textOutput.ToString(); foldingManager.UpdateFoldings(textOutput.Foldings.OrderBy(f => f.StartOffset), -1); - } catch (AggregateException ex) { + } catch (AggregateException aggregateException) { textEditor.SyntaxHighlighting = null; referenceElementGenerator.References = null; definitionLookup = null; - textEditor.Text = string.Join(Environment.NewLine, ex.InnerExceptions.Select(ie => ie.ToString())); + // Unpack aggregate exceptions as long as there's only a single exception: + // (assembly load errors might produce nested aggregate exceptions) + Exception ex = aggregateException; + while (ex is AggregateException && (ex as AggregateException).InnerExceptions.Count == 1) + ex = ex.InnerException; + textEditor.Text = ex.ToString(); } } else { try { diff --git a/ILSpy/TreeNodes/AssemblyTreeNode.cs b/ILSpy/TreeNodes/AssemblyTreeNode.cs index 1a6d642b5..6477df93f 100644 --- a/ILSpy/TreeNodes/AssemblyTreeNode.cs +++ b/ILSpy/TreeNodes/AssemblyTreeNode.cs @@ -43,7 +43,6 @@ namespace ICSharpCode.ILSpy.TreeNodes readonly Task assemblyTask; readonly List classes = new List(); readonly Dictionary namespaces = new Dictionary(); - readonly SynchronizationContext syncContext; public AssemblyTreeNode(string fileName, AssemblyList assemblyList) { @@ -54,7 +53,8 @@ namespace ICSharpCode.ILSpy.TreeNodes this.assemblyList = assemblyList; this.assemblyTask = Task.Factory.StartNew(LoadAssembly); // requires that this.fileName is set this.shortName = Path.GetFileNameWithoutExtension(fileName); - this.syncContext = SynchronizationContext.Current; + + assemblyTask.ContinueWith(OnAssemblyLoaded, TaskScheduler.FromCurrentSynchronizationContext()); this.LazyLoading = true; } @@ -64,7 +64,13 @@ namespace ICSharpCode.ILSpy.TreeNodes } public AssemblyDefinition AssemblyDefinition { - get { return assemblyTask.Result; } + get { + try { + return assemblyTask.Result; + } catch { + return null; + } + } } public override object Text { @@ -72,7 +78,11 @@ namespace ICSharpCode.ILSpy.TreeNodes } public override object Icon { - get { return Images.Assembly; } + get { return assemblyTask.IsFaulted ? Images.AssemblyWarning : Images.Assembly; } + } + + public override bool ShowExpander { + get { return !assemblyTask.IsFaulted; } } AssemblyDefinition LoadAssembly() @@ -86,17 +96,25 @@ namespace ICSharpCode.ILSpy.TreeNodes classes.Add(node); assemblyList.RegisterTypeNode(node); } - syncContext.Post( - delegate { - if (shortName != assembly.Name.Name) { - shortName = assembly.Name.Name; - RaisePropertyChanged("Text"); - } - }, null); return assembly; } + void OnAssemblyLoaded(Task assemblyTask) + { + if (assemblyTask.IsFaulted) { + RaisePropertyChanged("Icon"); + RaisePropertyChanged("ExpandedIcon"); + RaisePropertyChanged("ShowExpander"); + } else { + AssemblyDefinition assembly = assemblyTask.Result; + if (shortName != assembly.Name.Name) { + shortName = assembly.Name.Name; + RaisePropertyChanged("Text"); + } + } + } + sealed class MyAssemblyResolver : IAssemblyResolver { readonly AssemblyTreeNode parent; @@ -133,7 +151,11 @@ namespace ICSharpCode.ILSpy.TreeNodes protected override void LoadChildren() { - assemblyTask.Wait(); + try { + assemblyTask.Wait(); + } catch (AggregateException ex) { + return; + } ModuleDefinition mainModule = assemblyTask.Result.MainModule; this.Children.Add(new ReferenceFolderTreeNode(mainModule, this)); if (mainModule.HasResources) @@ -187,7 +209,7 @@ namespace ICSharpCode.ILSpy.TreeNodes public AssemblyTreeNode LookupReferencedAssembly(string fullName) { foreach (AssemblyTreeNode node in assemblyList.Assemblies) { - if (fullName.Equals(node.AssemblyDefinition.FullName, StringComparison.OrdinalIgnoreCase)) + if (node.AssemblyDefinition != null && fullName.Equals(node.AssemblyDefinition.FullName, StringComparison.OrdinalIgnoreCase)) return node; } @@ -209,8 +231,7 @@ namespace ICSharpCode.ILSpy.TreeNodes public override FilterResult Filter(FilterSettings settings) { - // avoid accessing this.AssemblyDefinition (waiting for background thread) if settings.SearchTerm == null - if (settings.SearchTerm == null || settings.SearchTermMatches(this.AssemblyDefinition.Name.Name)) + if (settings.SearchTermMatches(shortName)) return FilterResult.Match; else return FilterResult.Recurse; @@ -218,6 +239,7 @@ namespace ICSharpCode.ILSpy.TreeNodes public override void Decompile(Language language, ITextOutput output, DecompilationOptions options) { + // use assemblyTask.Result instead of this.AssemblyDefinition so that load errors are passed on to the caller language.DecompileAssembly(assemblyTask.Result, fileName, output, options); } }