From b813092cfeda27f1e8d6350d438eea68c0f166cb Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 26 Jan 2013 23:46:45 +0100 Subject: [PATCH] Add support for opening .netmodules. --- ICSharpCode.Decompiler/Ast/AstBuilder.cs | 23 ++++++++++----- ILSpy/Languages/CSharpLanguage.cs | 18 ++++++------ ILSpy/Languages/ILLanguage.cs | 9 +++--- ILSpy/Languages/Language.cs | 12 +++++--- ILSpy/LoadedAssembly.cs | 29 +++++++++++++------ ILSpy/MainWindow.xaml.cs | 8 +++-- ILSpy/SearchPane.cs | 6 ++-- .../Analyzer/AnalyzedAssemblyTreeNode.cs | 6 ++-- .../AnalyzedAttributeAppliedToTreeNode.cs | 23 +++++++-------- .../Analyzer/AnalyzerEntityTreeNode.cs | 2 +- ILSpy/TreeNodes/AssemblyListTreeNode.cs | 12 ++++++++ ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs | 6 ++-- ILSpy/TreeNodes/AssemblyTreeNode.cs | 19 ++++++------ ILSpy/TreeNodes/DerivedTypesEntryNode.cs | 4 +-- ILSpy/TreeNodes/DerivedTypesTreeNode.cs | 8 ++--- ILSpy/VB/VBLanguage.cs | 26 ++++++++--------- TestPlugin/ContextMenuCommand.cs | 2 +- 17 files changed, 124 insertions(+), 89 deletions(-) diff --git a/ICSharpCode.Decompiler/Ast/AstBuilder.cs b/ICSharpCode.Decompiler/Ast/AstBuilder.cs index b915fc504..11fb65fd0 100644 --- a/ICSharpCode.Decompiler/Ast/AstBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstBuilder.cs @@ -167,7 +167,12 @@ namespace ICSharpCode.Decompiler.Ast public void AddAssembly(AssemblyDefinition assemblyDefinition, bool onlyAssemblyLevel = false) { - if (assemblyDefinition.Name.Version != null) { + AddAssembly(assemblyDefinition.MainModule, onlyAssemblyLevel); + } + + public void AddAssembly(ModuleDefinition moduleDefinition, bool onlyAssemblyLevel = false) + { + if (moduleDefinition.Assembly != null && moduleDefinition.Assembly.Name.Version != null) { syntaxTree.AddChild( new AttributeSection { AttributeTarget = "assembly", @@ -176,22 +181,24 @@ namespace ICSharpCode.Decompiler.Ast Type = new SimpleType("AssemblyVersion") .WithAnnotation(new TypeReference( "System.Reflection", "AssemblyVersionAttribute", - assemblyDefinition.MainModule, assemblyDefinition.MainModule.TypeSystem.Corlib)), + moduleDefinition, moduleDefinition.TypeSystem.Corlib)), Arguments = { - new PrimitiveExpression(assemblyDefinition.Name.Version.ToString()) + new PrimitiveExpression(moduleDefinition.Assembly.Name.Version.ToString()) } } } }, EntityDeclaration.AttributeRole); } - ConvertCustomAttributes(syntaxTree, assemblyDefinition, "assembly"); - ConvertSecurityAttributes(syntaxTree, assemblyDefinition, "assembly"); - ConvertCustomAttributes(syntaxTree, assemblyDefinition.MainModule, "module"); - AddTypeForwarderAttributes(syntaxTree, assemblyDefinition.MainModule, "assembly"); + if (moduleDefinition.Assembly != null) { + ConvertCustomAttributes(syntaxTree, moduleDefinition.Assembly, "assembly"); + ConvertSecurityAttributes(syntaxTree, moduleDefinition.Assembly, "assembly"); + } + ConvertCustomAttributes(syntaxTree, moduleDefinition, "module"); + AddTypeForwarderAttributes(syntaxTree, moduleDefinition, "assembly"); if (!onlyAssemblyLevel) { - foreach (TypeDefinition typeDef in assemblyDefinition.MainModule.Types) { + foreach (TypeDefinition typeDef in moduleDefinition.Types) { // Skip the class if (typeDef.Name == "") continue; // Skip any hidden types diff --git a/ILSpy/Languages/CSharpLanguage.cs b/ILSpy/Languages/CSharpLanguage.cs index 4c9b58d43..86985264e 100644 --- a/ILSpy/Languages/CSharpLanguage.cs +++ b/ILSpy/Languages/CSharpLanguage.cs @@ -262,13 +262,13 @@ namespace ICSharpCode.ILSpy { if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { HashSet directories = new HashSet(StringComparer.OrdinalIgnoreCase); - var files = WriteCodeFilesInProject(assembly.AssemblyDefinition, options, directories).ToList(); + var files = WriteCodeFilesInProject(assembly.ModuleDefinition, options, directories).ToList(); files.AddRange(WriteResourceFilesInProject(assembly, options, directories)); - WriteProjectFile(new TextOutputWriter(output), files, assembly.AssemblyDefinition.MainModule); + WriteProjectFile(new TextOutputWriter(output), files, assembly.ModuleDefinition); } else { base.DecompileAssembly(assembly, output, options); output.WriteLine(); - ModuleDefinition mainModule = assembly.AssemblyDefinition.MainModule; + ModuleDefinition mainModule = assembly.ModuleDefinition; if (mainModule.EntryPoint != null) { output.Write("// Entry point: "); output.WriteReference(mainModule.EntryPoint.DeclaringType.FullName + "." + mainModule.EntryPoint.Name, mainModule.EntryPoint); @@ -296,8 +296,8 @@ namespace ICSharpCode.ILSpy // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { - AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.AssemblyDefinition.MainModule); - codeDomBuilder.AddAssembly(assembly.AssemblyDefinition, onlyAssemblyLevel: !options.FullDecompilation); + AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.ModuleDefinition); + codeDomBuilder.AddAssembly(assembly.ModuleDefinition, onlyAssemblyLevel: !options.FullDecompilation); codeDomBuilder.RunTransformations(transformAbortCondition); codeDomBuilder.GenerateCode(output); } @@ -423,9 +423,9 @@ namespace ICSharpCode.ILSpy return true; } - IEnumerable> WriteCodeFilesInProject(AssemblyDefinition assembly, DecompilationOptions options, HashSet directories) + IEnumerable> WriteCodeFilesInProject(ModuleDefinition module, DecompilationOptions options, HashSet directories) { - var files = assembly.MainModule.Types.Where(t => IncludeTypeWhenDecompilingProject(t, options)).GroupBy( + var files = module.Types.Where(t => IncludeTypeWhenDecompilingProject(t, options)).GroupBy( delegate(TypeDefinition type) { string file = TextView.DecompilerTextView.CleanUpName(type.Name) + this.FileExtension; if (string.IsNullOrEmpty(type.Namespace)) { @@ -443,7 +443,7 @@ namespace ICSharpCode.ILSpy new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(IGrouping file) { using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) { - AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.MainModule); + AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module); foreach (TypeDefinition type in file) { codeDomBuilder.AddType(type); } @@ -461,7 +461,7 @@ namespace ICSharpCode.ILSpy { //AppDomain bamlDecompilerAppDomain = null; //try { - foreach (EmbeddedResource r in assembly.AssemblyDefinition.MainModule.Resources.OfType()) { + foreach (EmbeddedResource r in assembly.ModuleDefinition.Resources.OfType()) { string fileName; Stream s = r.GetResourceStream(); s.Position = 0; diff --git a/ILSpy/Languages/ILLanguage.cs b/ILSpy/Languages/ILLanguage.cs index 51faf139b..88528f1ec 100644 --- a/ILSpy/Languages/ILLanguage.cs +++ b/ILSpy/Languages/ILLanguage.cs @@ -114,14 +114,15 @@ namespace ICSharpCode.ILSpy ReflectionDisassembler rd = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken); if (options.FullDecompilation) - rd.WriteAssemblyReferences(assembly.AssemblyDefinition.MainModule); - rd.WriteAssemblyHeader(assembly.AssemblyDefinition); + rd.WriteAssemblyReferences(assembly.ModuleDefinition); + if (assembly.AssemblyDefinition != null) + rd.WriteAssemblyHeader(assembly.AssemblyDefinition); output.WriteLine(); - rd.WriteModuleHeader(assembly.AssemblyDefinition.MainModule); + rd.WriteModuleHeader(assembly.ModuleDefinition); if (options.FullDecompilation) { output.WriteLine(); output.WriteLine(); - rd.WriteModuleContents(assembly.AssemblyDefinition.MainModule); + rd.WriteModuleContents(assembly.ModuleDefinition); } } diff --git a/ILSpy/Languages/Language.cs b/ILSpy/Languages/Language.cs index 901ca8af5..750dd8186 100644 --- a/ILSpy/Languages/Language.cs +++ b/ILSpy/Languages/Language.cs @@ -88,11 +88,15 @@ namespace ICSharpCode.ILSpy public virtual void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) { WriteCommentLine(output, assembly.FileName); - var name = assembly.AssemblyDefinition.Name; - if (name.IsWindowsRuntime) { - WriteCommentLine(output, name.Name + " [WinRT]"); + if (assembly.AssemblyDefinition != null) { + var name = assembly.AssemblyDefinition.Name; + if (name.IsWindowsRuntime) { + WriteCommentLine(output, name.Name + " [WinRT]"); + } else { + WriteCommentLine(output, name.FullName); + } } else { - WriteCommentLine(output, name.FullName); + WriteCommentLine(output, assembly.ModuleDefinition.Name); } } diff --git a/ILSpy/LoadedAssembly.cs b/ILSpy/LoadedAssembly.cs index 30a98ded0..43f2e12b2 100644 --- a/ILSpy/LoadedAssembly.cs +++ b/ILSpy/LoadedAssembly.cs @@ -30,7 +30,7 @@ namespace ICSharpCode.ILSpy /// public sealed class LoadedAssembly { - readonly Task assemblyTask; + readonly Task assemblyTask; readonly AssemblyList assemblyList; readonly string fileName; readonly string shortName; @@ -44,15 +44,15 @@ namespace ICSharpCode.ILSpy this.assemblyList = assemblyList; this.fileName = fileName; - this.assemblyTask = Task.Factory.StartNew(LoadAssembly); // requires that this.fileName is set + this.assemblyTask = Task.Factory.StartNew(LoadAssembly); // requires that this.fileName is set this.shortName = Path.GetFileNameWithoutExtension(fileName); } /// - /// Gets the Cecil AssemblyDefinition. + /// Gets the Cecil ModuleDefinition. /// Can be null when there was a load error. /// - public AssemblyDefinition AssemblyDefinition { + public ModuleDefinition ModuleDefinition { get { try { return assemblyTask.Result; @@ -62,6 +62,17 @@ namespace ICSharpCode.ILSpy } } + /// + /// Gets the Cecil AssemblyDefinition. + /// Is null when there was a load error; or when opening a netmodule. + /// + public AssemblyDefinition AssemblyDefinition { + get { + var module = this.ModuleDefinition; + return module != null ? module.Assembly : null; + } + } + public AssemblyList AssemblyList { get { return assemblyList; } } @@ -82,22 +93,22 @@ namespace ICSharpCode.ILSpy get { return assemblyTask.IsFaulted; } } - AssemblyDefinition LoadAssembly() + ModuleDefinition LoadAssembly() { // runs on background thread ReaderParameters p = new ReaderParameters(); p.AssemblyResolver = new MyAssemblyResolver(this); - AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(fileName, p); + ModuleDefinition module = ModuleDefinition.ReadModule(fileName, p); if (DecompilerSettingsPanel.CurrentDecompilerSettings.UseDebugSymbols) { try { - LoadSymbols(asm.MainModule); + LoadSymbols(module); } catch (IOException) { } catch (UnauthorizedAccessException) { } catch (InvalidOperationException) { // ignore any errors during symbol loading } } - return asm; + return module; } private void LoadSymbols(ModuleDefinition module) @@ -244,7 +255,7 @@ namespace ICSharpCode.ILSpy } } - public Task ContinueWhenLoaded(Action> onAssemblyLoaded, TaskScheduler taskScheduler) + public Task ContinueWhenLoaded(Action> onAssemblyLoaded, TaskScheduler taskScheduler) { return this.assemblyTask.ContinueWith(onAssemblyLoaded, taskScheduler); } diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 39ba0ad81..298297ecc 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -275,9 +275,9 @@ namespace ICSharpCode.ILSpy } } else { foreach (LoadedAssembly asm in commandLineLoadedAssemblies) { - AssemblyDefinition def = asm.AssemblyDefinition; + ModuleDefinition def = asm.ModuleDefinition; if (def != null) { - MemberReference mr = XmlDocKeyProvider.FindMemberByKey(def.MainModule, args.NavigateTo); + MemberReference mr = XmlDocKeyProvider.FindMemberByKey(def, args.NavigateTo); if (mr != null) { found = true; JumpToReference(mr); @@ -294,7 +294,7 @@ namespace ICSharpCode.ILSpy } else if (commandLineLoadedAssemblies.Count == 1) { // NavigateTo == null and an assembly was given on the command-line: // Select the newly loaded assembly - JumpToReference(commandLineLoadedAssemblies[0].AssemblyDefinition); + JumpToReference(commandLineLoadedAssemblies[0].ModuleDefinition); } commandLineLoadedAssemblies.Clear(); // clear references once we don't need them anymore } @@ -550,6 +550,8 @@ namespace ICSharpCode.ILSpy return assemblyListTreeNode.FindEventNode(((EventReference)reference).Resolve()); } else if (reference is AssemblyDefinition) { return assemblyListTreeNode.FindAssemblyNode((AssemblyDefinition)reference); + } else if (reference is ModuleDefinition) { + return assemblyListTreeNode.FindAssemblyNode((ModuleDefinition)reference); } else { return null; } diff --git a/ILSpy/SearchPane.cs b/ILSpy/SearchPane.cs index 4a171fb74..3c17287a8 100644 --- a/ILSpy/SearchPane.cs +++ b/ILSpy/SearchPane.cs @@ -244,11 +244,11 @@ namespace ICSharpCode.ILSpy } foreach (var loadedAssembly in assemblies) { - AssemblyDefinition asm = loadedAssembly.AssemblyDefinition; - if (asm == null) + ModuleDefinition module = loadedAssembly.ModuleDefinition; + if (module == null) continue; CancellationToken cancellationToken = cts.Token; - foreach (TypeDefinition type in asm.MainModule.Types) { + foreach (TypeDefinition type in module.Types) { cancellationToken.ThrowIfCancellationRequested(); PerformSearch(type); } diff --git a/ILSpy/TreeNodes/Analyzer/AnalyzedAssemblyTreeNode.cs b/ILSpy/TreeNodes/Analyzer/AnalyzedAssemblyTreeNode.cs index ce3476a29..88cafe6ee 100644 --- a/ILSpy/TreeNodes/Analyzer/AnalyzedAssemblyTreeNode.cs +++ b/ILSpy/TreeNodes/Analyzer/AnalyzedAssemblyTreeNode.cs @@ -23,9 +23,9 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer { internal class AnalyzedAssemblyTreeNode : AnalyzerEntityTreeNode { - private readonly AssemblyDefinition analyzedAssembly; + private readonly ModuleDefinition analyzedAssembly; - public AnalyzedAssemblyTreeNode(AssemblyDefinition analyzedAssembly) + public AnalyzedAssemblyTreeNode(ModuleDefinition analyzedAssembly) { if (analyzedAssembly == null) throw new ArgumentNullException("analyzedAssembly"); @@ -42,7 +42,7 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer { get { - return analyzedAssembly.Name.Name; + return analyzedAssembly.Name; } } diff --git a/ILSpy/TreeNodes/Analyzer/AnalyzedAttributeAppliedToTreeNode.cs b/ILSpy/TreeNodes/Analyzer/AnalyzedAttributeAppliedToTreeNode.cs index 2849b1d4e..45af68e1e 100644 --- a/ILSpy/TreeNodes/Analyzer/AnalyzedAttributeAppliedToTreeNode.cs +++ b/ILSpy/TreeNodes/Analyzer/AnalyzedAttributeAppliedToTreeNode.cs @@ -94,7 +94,7 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer var currentAssembly = analyzedType.Module.Assembly; var assemblies = analyzedType.IsPublic ? GetReferencingAssemblies(currentAssembly, ct) : GetAssemblyAndAnyFriends(currentAssembly, ct); - var results = assemblies.AsParallel().WithCancellation(ct).SelectMany(a => FindReferencesInAssembly(a.Item1, a.Item2, ct)); + var results = assemblies.AsParallel().WithCancellation(ct).SelectMany(a => FindReferencesInAssembly(a.Item1.MainModule, a.Item2, ct)); foreach (var result in results.OrderBy(n => n.Text)) { yield return result; @@ -105,13 +105,14 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer #region standard custom attributes - private IEnumerable FindReferencesInAssembly(AssemblyDefinition asm, TypeReference tr, CancellationToken ct) + private IEnumerable FindReferencesInAssembly(ModuleDefinition module, TypeReference tr, CancellationToken ct) { //since we do not display modules as separate entities, coalesce the assembly and module searches bool foundInAssyOrModule = false; if ((usage & AttributeTargets.Assembly) != 0) { - if (asm.HasCustomAttributes) { + AssemblyDefinition asm = module.Assembly; + if (asm != null && asm.HasCustomAttributes) { foreach (var attribute in asm.CustomAttributes) { if (attribute.AttributeType == tr) { foundInAssyOrModule = true; @@ -126,13 +127,11 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer //search module if ((usage & AttributeTargets.Module) != 0) { - foreach (var module in asm.Modules) { - if (module.HasCustomAttributes) { - foreach (var attribute in module.CustomAttributes) { - if (attribute.AttributeType == tr) { - foundInAssyOrModule = true; - break; - } + if (module.HasCustomAttributes) { + foreach (var attribute in module.CustomAttributes) { + if (attribute.AttributeType == tr) { + foundInAssyOrModule = true; + break; } } } @@ -141,12 +140,12 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer } if (foundInAssyOrModule) { - yield return new AnalyzedAssemblyTreeNode(asm); + yield return new AnalyzedAssemblyTreeNode(module); } ct.ThrowIfCancellationRequested(); - foreach (TypeDefinition type in TreeTraversal.PreOrder(asm.MainModule.Types, t => t.NestedTypes).OrderBy(t => t.FullName)) { + foreach (TypeDefinition type in TreeTraversal.PreOrder(module.Types, t => t.NestedTypes).OrderBy(t => t.FullName)) { ct.ThrowIfCancellationRequested(); foreach (var result in FindReferencesWithinInType(type, tr)) { ct.ThrowIfCancellationRequested(); diff --git a/ILSpy/TreeNodes/Analyzer/AnalyzerEntityTreeNode.cs b/ILSpy/TreeNodes/Analyzer/AnalyzerEntityTreeNode.cs index 184caa0fa..d9e3013ac 100644 --- a/ILSpy/TreeNodes/Analyzer/AnalyzerEntityTreeNode.cs +++ b/ILSpy/TreeNodes/Analyzer/AnalyzerEntityTreeNode.cs @@ -39,7 +39,7 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer public override bool HandleAssemblyListChanged(ICollection removedAssemblies, ICollection addedAssemblies) { foreach (LoadedAssembly asm in removedAssemblies) { - if (this.Member.Module.Assembly == asm.AssemblyDefinition) + if (this.Member.Module == asm.ModuleDefinition) return false; // remove this node } this.Children.RemoveAll( diff --git a/ILSpy/TreeNodes/AssemblyListTreeNode.cs b/ILSpy/TreeNodes/AssemblyListTreeNode.cs index e3725208a..e797e462e 100644 --- a/ILSpy/TreeNodes/AssemblyListTreeNode.cs +++ b/ILSpy/TreeNodes/AssemblyListTreeNode.cs @@ -132,6 +132,18 @@ namespace ICSharpCode.ILSpy.TreeNodes #region Find*Node + public AssemblyTreeNode FindAssemblyNode(ModuleDefinition module) + { + if (module == null) + return null; + App.Current.Dispatcher.VerifyAccess(); + foreach (AssemblyTreeNode node in this.Children) { + if (node.LoadedAssembly.IsLoaded && node.LoadedAssembly.ModuleDefinition == module) + return node; + } + return null; + } + public AssemblyTreeNode FindAssemblyNode(AssemblyDefinition asm) { if (asm == null) diff --git a/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs b/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs index c9596d47a..860ce9951 100644 --- a/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs +++ b/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs @@ -72,9 +72,9 @@ namespace ICSharpCode.ILSpy.TreeNodes if (assemblyListNode != null) { var refNode = assemblyListNode.FindAssemblyNode(parentAssembly.LoadedAssembly.LookupReferencedAssembly(r)); if (refNode != null) { - AssemblyDefinition asm = refNode.LoadedAssembly.AssemblyDefinition; - if (asm != null) { - foreach (var childRef in asm.MainModule.AssemblyReferences) + ModuleDefinition module = refNode.LoadedAssembly.ModuleDefinition; + if (module != null) { + foreach (var childRef in module.AssemblyReferences) this.Children.Add(new AssemblyReferenceTreeNode(childRef, refNode)); } } diff --git a/ILSpy/TreeNodes/AssemblyTreeNode.cs b/ILSpy/TreeNodes/AssemblyTreeNode.cs index ea9ac9af9..7913da096 100644 --- a/ILSpy/TreeNodes/AssemblyTreeNode.cs +++ b/ILSpy/TreeNodes/AssemblyTreeNode.cs @@ -83,15 +83,15 @@ namespace ICSharpCode.ILSpy.TreeNodes get { return !assembly.HasLoadError; } } - void OnAssemblyLoaded(Task assemblyTask) + void OnAssemblyLoaded(Task moduleTask) { // change from "Loading" icon to final icon RaisePropertyChanged("Icon"); RaisePropertyChanged("ExpandedIcon"); - if (assemblyTask.IsFaulted) { + if (moduleTask.IsFaulted) { RaisePropertyChanged("ShowExpander"); // cannot expand assemblies with load error // observe the exception so that the Task's finalizer doesn't re-throw it - try { assemblyTask.Wait(); } + try { moduleTask.Wait(); } catch (AggregateException) { } } else { RaisePropertyChanged("Text"); // shortname might have changed @@ -102,20 +102,19 @@ namespace ICSharpCode.ILSpy.TreeNodes protected override void LoadChildren() { - AssemblyDefinition assemblyDefinition = assembly.AssemblyDefinition; - if (assemblyDefinition == null) { + ModuleDefinition moduleDefinition = assembly.ModuleDefinition; + if (moduleDefinition == null) { // if we crashed on loading, then we don't have any children return; } - ModuleDefinition mainModule = assemblyDefinition.MainModule; - this.Children.Add(new ReferenceFolderTreeNode(mainModule, this)); - if (mainModule.HasResources) - this.Children.Add(new ResourceListTreeNode(mainModule)); + this.Children.Add(new ReferenceFolderTreeNode(moduleDefinition, this)); + if (moduleDefinition.HasResources) + this.Children.Add(new ResourceListTreeNode(moduleDefinition)); foreach (NamespaceTreeNode ns in namespaces.Values) { ns.Children.Clear(); } - foreach (TypeDefinition type in mainModule.Types.OrderBy(t => t.FullName)) { + foreach (TypeDefinition type in moduleDefinition.Types.OrderBy(t => t.FullName)) { NamespaceTreeNode ns; if (!namespaces.TryGetValue(type.Namespace, out ns)) { ns = new NamespaceTreeNode(type.Namespace); diff --git a/ILSpy/TreeNodes/DerivedTypesEntryNode.cs b/ILSpy/TreeNodes/DerivedTypesEntryNode.cs index 437f66986..8b42ee234 100644 --- a/ILSpy/TreeNodes/DerivedTypesEntryNode.cs +++ b/ILSpy/TreeNodes/DerivedTypesEntryNode.cs @@ -27,10 +27,10 @@ namespace ICSharpCode.ILSpy.TreeNodes class DerivedTypesEntryNode : ILSpyTreeNode, IMemberTreeNode { private readonly TypeDefinition type; - private readonly AssemblyDefinition[] assemblies; + private readonly ModuleDefinition[] assemblies; private readonly ThreadingSupport threading; - public DerivedTypesEntryNode(TypeDefinition type, AssemblyDefinition[] assemblies) + public DerivedTypesEntryNode(TypeDefinition type, ModuleDefinition[] assemblies) { this.type = type; this.assemblies = assemblies; diff --git a/ILSpy/TreeNodes/DerivedTypesTreeNode.cs b/ILSpy/TreeNodes/DerivedTypesTreeNode.cs index 78fab7c44..87279e2b0 100644 --- a/ILSpy/TreeNodes/DerivedTypesTreeNode.cs +++ b/ILSpy/TreeNodes/DerivedTypesTreeNode.cs @@ -61,14 +61,14 @@ namespace ICSharpCode.ILSpy.TreeNodes IEnumerable FetchChildren(CancellationToken cancellationToken) { // FetchChildren() runs on the main thread; but the enumerator will be consumed on a background thread - var assemblies = list.GetAssemblies().Select(node => node.AssemblyDefinition).Where(asm => asm != null).ToArray(); + var assemblies = list.GetAssemblies().Select(node => node.ModuleDefinition).Where(asm => asm != null).ToArray(); return FindDerivedTypes(type, assemblies, cancellationToken); } - internal static IEnumerable FindDerivedTypes(TypeDefinition type, AssemblyDefinition[] assemblies, CancellationToken cancellationToken) + internal static IEnumerable FindDerivedTypes(TypeDefinition type, ModuleDefinition[] assemblies, CancellationToken cancellationToken) { - foreach (AssemblyDefinition asm in assemblies) { - foreach (TypeDefinition td in TreeTraversal.PreOrder(asm.MainModule.Types, t => t.NestedTypes)) { + foreach (ModuleDefinition module in assemblies) { + foreach (TypeDefinition td in TreeTraversal.PreOrder(module.Types, t => t.NestedTypes)) { cancellationToken.ThrowIfCancellationRequested(); if (type.IsInterface && td.HasInterfaces) { foreach (TypeReference typeRef in td.Interfaces) { diff --git a/ILSpy/VB/VBLanguage.cs b/ILSpy/VB/VBLanguage.cs index 1a5af8b9a..78cdcbd6a 100644 --- a/ILSpy/VB/VBLanguage.cs +++ b/ILSpy/VB/VBLanguage.cs @@ -72,13 +72,13 @@ namespace ICSharpCode.ILSpy.VB { if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { HashSet directories = new HashSet(StringComparer.OrdinalIgnoreCase); - var files = WriteCodeFilesInProject(assembly.AssemblyDefinition, options, directories).ToList(); + var files = WriteCodeFilesInProject(assembly.ModuleDefinition, options, directories).ToList(); files.AddRange(WriteResourceFilesInProject(assembly, options, directories)); - WriteProjectFile(new TextOutputWriter(output), files, assembly.AssemblyDefinition.MainModule); + WriteProjectFile(new TextOutputWriter(output), files, assembly.ModuleDefinition); } else { base.DecompileAssembly(assembly, output, options); output.WriteLine(); - ModuleDefinition mainModule = assembly.AssemblyDefinition.MainModule; + ModuleDefinition mainModule = assembly.ModuleDefinition; if (mainModule.EntryPoint != null) { output.Write("' Entry point: "); output.WriteReference(mainModule.EntryPoint.DeclaringType.FullName + "." + mainModule.EntryPoint.Name, mainModule.EntryPoint); @@ -106,9 +106,9 @@ namespace ICSharpCode.ILSpy.VB // don't automatically load additional assemblies when an assembly node is selected in the tree view using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { - AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.AssemblyDefinition.MainModule); - codeDomBuilder.AddAssembly(assembly.AssemblyDefinition, onlyAssemblyLevel: !options.FullDecompilation); - RunTransformsAndGenerateCode(codeDomBuilder, output, options, assembly.AssemblyDefinition.MainModule); + AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.ModuleDefinition); + codeDomBuilder.AddAssembly(assembly.ModuleDefinition, onlyAssemblyLevel: !options.FullDecompilation); + RunTransformsAndGenerateCode(codeDomBuilder, output, options, assembly.ModuleDefinition); } } } @@ -248,9 +248,9 @@ namespace ICSharpCode.ILSpy.VB return true; } - IEnumerable> WriteCodeFilesInProject(AssemblyDefinition assembly, DecompilationOptions options, HashSet directories) + IEnumerable> WriteCodeFilesInProject(ModuleDefinition module, DecompilationOptions options, HashSet directories) { - var files = assembly.MainModule.Types.Where(t => IncludeTypeWhenDecompilingProject(t, options)).GroupBy( + var files = module.Types.Where(t => IncludeTypeWhenDecompilingProject(t, options)).GroupBy( delegate(TypeDefinition type) { string file = TextView.DecompilerTextView.CleanUpName(type.Name) + this.FileExtension; if (string.IsNullOrEmpty(type.Namespace)) { @@ -268,11 +268,11 @@ namespace ICSharpCode.ILSpy.VB new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(IGrouping file) { using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) { - AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.MainModule); + AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module); foreach (TypeDefinition type in file) { codeDomBuilder.AddType(type); } - RunTransformsAndGenerateCode(codeDomBuilder, new PlainTextOutput(w), options, assembly.MainModule); + RunTransformsAndGenerateCode(codeDomBuilder, new PlainTextOutput(w), options, module); } }); AstMethodBodyBuilder.PrintNumberOfUnhandledOpcodes(); @@ -285,7 +285,7 @@ namespace ICSharpCode.ILSpy.VB { //AppDomain bamlDecompilerAppDomain = null; //try { - foreach (EmbeddedResource r in assembly.AssemblyDefinition.MainModule.Resources.OfType()) { + foreach (EmbeddedResource r in assembly.ModuleDefinition.Resources.OfType()) { string fileName; Stream s = r.GetResourceStream(); s.Position = 0; @@ -306,8 +306,8 @@ namespace ICSharpCode.ILSpy.VB Stream entryStream = (Stream)pair.Value; entryStream.Position = 0; if (fileName.EndsWith(".baml", StringComparison.OrdinalIgnoreCase)) { - MemoryStream ms = new MemoryStream(); - entryStream.CopyTo(ms); + //MemoryStream ms = new MemoryStream(); + //entryStream.CopyTo(ms); // TODO implement extension point // var decompiler = Baml.BamlResourceEntryNode.CreateBamlDecompilerInAppDomain(ref bamlDecompilerAppDomain, assembly.FileName); // string xaml = null; diff --git a/TestPlugin/ContextMenuCommand.cs b/TestPlugin/ContextMenuCommand.cs index f19000aed..50c98b498 100644 --- a/TestPlugin/ContextMenuCommand.cs +++ b/TestPlugin/ContextMenuCommand.cs @@ -29,7 +29,7 @@ namespace TestPlugin if (context.SelectedTreeNodes == null) return; AssemblyTreeNode node = (AssemblyTreeNode)context.SelectedTreeNodes[0]; - AssemblyDefinition asm = node.LoadedAssembly.AssemblyDefinition as AssemblyDefinition; + AssemblyDefinition asm = node.LoadedAssembly.AssemblyDefinition; if (asm != null) { SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = node.LoadedAssembly.FileName;