|
| 1 | +/* |
| 2 | + Copyright (C) 2026 ElektroKill |
| 3 | +
|
| 4 | + This file is part of dnSpy |
| 5 | +
|
| 6 | + dnSpy is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + dnSpy is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with dnSpy. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +using System; |
| 21 | +using System.Collections.Generic; |
| 22 | +using System.ComponentModel.Composition; |
| 23 | +using System.Diagnostics; |
| 24 | +using System.Linq; |
| 25 | +using dnlib.DotNet; |
| 26 | +using dnSpy.AsmEditor.Commands; |
| 27 | +using dnSpy.Contracts.Documents.TreeView; |
| 28 | +using dnSpy.Contracts.Menus; |
| 29 | + |
| 30 | +namespace dnSpy.AsmEditor.Compiler { |
| 31 | + sealed class LoadDependenciesCommands { |
| 32 | + [ExportMenuItem(Header = "res:LoadDependenciesCommand", Group = MenuConstants.GROUP_CTX_DOCUMENTS_ASMED_ILED, Order = 28.999)] |
| 33 | + sealed class DocumentsCommand : DocumentsContextMenuHandler { |
| 34 | + public override bool IsVisible(AsmEditorContext context) => LoadDependenciesCommands.CanExecute(context.Nodes); |
| 35 | + public override void Execute(AsmEditorContext context) => LoadDependenciesCommands.Execute(context.Nodes, false); |
| 36 | + } |
| 37 | + |
| 38 | + [ExportMenuItem(OwnerGuid = MenuConstants.APP_MENU_EDIT_GUID, Header = "res:LoadDependenciesCommand", Group = MenuConstants.GROUP_APP_MENU_EDIT_ASMED_SETTINGS, Order = 58.999)] |
| 39 | + sealed class EditMenuCommand : EditMenuHandler { |
| 40 | + [ImportingConstructor] |
| 41 | + EditMenuCommand(IAppService appService) : base(appService.DocumentTreeView) { } |
| 42 | + |
| 43 | + public override bool IsVisible(AsmEditorContext context) => LoadDependenciesCommands.CanExecute(context.Nodes); |
| 44 | + public override void Execute(AsmEditorContext context) => LoadDependenciesCommands.Execute(context.Nodes, false); |
| 45 | + } |
| 46 | + |
| 47 | + [ExportMenuItem(Header = "res:LoadDependenciesRecursiveCommand", Group = MenuConstants.GROUP_CTX_DOCUMENTS_ASMED_ILED, Order = 29.999)] |
| 48 | + sealed class RecursiveDocumentsCommand : DocumentsContextMenuHandler { |
| 49 | + public override bool IsVisible(AsmEditorContext context) => LoadDependenciesCommands.CanExecute(context.Nodes); |
| 50 | + public override void Execute(AsmEditorContext context) => LoadDependenciesCommands.Execute(context.Nodes, true); |
| 51 | + } |
| 52 | + |
| 53 | + [ExportMenuItem(OwnerGuid = MenuConstants.APP_MENU_EDIT_GUID, Header = "res:LoadDependenciesRecursiveCommand", Group = MenuConstants.GROUP_APP_MENU_EDIT_ASMED_SETTINGS, Order = 59.999)] |
| 54 | + sealed class RecursiveEditMenuCommand : EditMenuHandler { |
| 55 | + [ImportingConstructor] |
| 56 | + RecursiveEditMenuCommand(IAppService appService) : base(appService.DocumentTreeView) { } |
| 57 | + |
| 58 | + public override bool IsVisible(AsmEditorContext context) => LoadDependenciesCommands.CanExecute(context.Nodes); |
| 59 | + public override void Execute(AsmEditorContext context) => LoadDependenciesCommands.Execute(context.Nodes, true); |
| 60 | + } |
| 61 | + |
| 62 | + static bool CanExecute(DocumentTreeNodeData[] nodes) => nodes.Length == 1 && GetModuleNode(nodes[0]) is not null; |
| 63 | + |
| 64 | + static ModuleDocumentNode? GetModuleNode(DocumentTreeNodeData node) { |
| 65 | + if (node is AssemblyDocumentNode asmNode) { |
| 66 | + asmNode.TreeNode.EnsureChildrenLoaded(); |
| 67 | + return asmNode.TreeNode.DataChildren.FirstOrDefault() as ModuleDocumentNode; |
| 68 | + } |
| 69 | + |
| 70 | + return node.GetModuleNode(); |
| 71 | + } |
| 72 | + |
| 73 | + static void Execute(DocumentTreeNodeData[] nodes, bool recursive) { |
| 74 | + if (!CanExecute(nodes)) |
| 75 | + return; |
| 76 | + |
| 77 | + var modNode = GetModuleNode(nodes[0]); |
| 78 | + Debug2.Assert(modNode is not null); |
| 79 | + if (modNode is null) |
| 80 | + return; |
| 81 | + |
| 82 | + var module = modNode.Document.ModuleDef; |
| 83 | + Debug2.Assert(module is not null); |
| 84 | + if (module is null) |
| 85 | + throw new InvalidOperationException(); |
| 86 | + |
| 87 | + var documentService = modNode.Context.DocumentTreeView.DocumentService; |
| 88 | + |
| 89 | + if (!recursive) { |
| 90 | + foreach (var assemblyRef in module.GetAssemblyRefs()) |
| 91 | + documentService.Resolve(assemblyRef, module); |
| 92 | + } |
| 93 | + else { |
| 94 | + var processedModules = new HashSet<ModuleDef>(); |
| 95 | + |
| 96 | + var queue = new Stack<ModuleDef>(); |
| 97 | + queue.Push(module); |
| 98 | + |
| 99 | + while (queue.Count > 0) { |
| 100 | + var mod = queue.Pop(); |
| 101 | + if (!processedModules.Add(mod)) |
| 102 | + continue; |
| 103 | + |
| 104 | + foreach (var assemblyRef in mod.GetAssemblyRefs()) { |
| 105 | + var asm = documentService.Resolve(assemblyRef, mod); |
| 106 | + if (asm?.ModuleDef is null) |
| 107 | + continue; |
| 108 | + queue.Push(asm.ModuleDef); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments