Beispiel #1
0
        static void Execute(Lazy <IUndoCommandService> undoCommandService, IAppService appService, DocumentTreeNodeData[] nodes)
        {
            if (!AddNetModuleToAssemblyCommand.CanExecute(nodes))
            {
                return;
            }

            var asmNode = (DsDocumentNode)nodes[0];

            if (asmNode is ModuleDocumentNode)
            {
                asmNode = (AssemblyDocumentNode)asmNode.TreeNode.Parent.Data;
            }

            var win  = new NetModuleOptionsDlg();
            var data = new NetModuleOptionsVM(asmNode.Document.ModuleDef);

            win.DataContext = data;
            win.Owner       = appService.MainWindow;
            if (win.ShowDialog() != true)
            {
                return;
            }

            var options    = data.CreateNetModuleOptions();
            var newModule  = ModuleUtils.CreateNetModule(options.Name, options.Mvid, options.ClrVersion);
            var newFile    = DsDotNetDocument.CreateModule(DsDocumentInfo.CreateDocument(string.Empty), newModule, true);
            var newModNode = asmNode.Context.DocumentTreeView.CreateModule(newFile);
            var cmd        = new AddNewNetModuleToAssemblyCommand(undoCommandService.Value, (DsDocumentNode)nodes[0], newModNode);

            undoCommandService.Value.Add(cmd);
            appService.DocumentTabService.FollowReference(cmd.modNode);
        }
Beispiel #2
0
        CreateNetModuleCommand(Lazy <IUndoCommandService> undoCommandService, IDocumentTreeView documentTreeView, NetModuleOptions options)
        {
            this.undoCommandService = undoCommandService;
            var module = ModuleUtils.CreateNetModule(options.Name, options.Mvid, options.ClrVersion);
            var file   = DsDotNetDocument.CreateModule(DsDocumentInfo.CreateDocument(string.Empty), module, true);

            fileNodeCreator = RootDocumentNodeCreator.CreateModule(documentTreeView, file);
        }
Beispiel #3
0
        CreateAssemblyCommand(IUndoCommandService undoCommandService, IDocumentTreeView documentTreeView, ModuleDef newModule, AssemblyOptions options)
        {
            this.undoCommandService = undoCommandService;
            var module = Module.ModuleUtils.CreateModule(options.Name, Guid.NewGuid(), options.ClrVersion, ModuleKind.Dll, newModule);

            options.CreateAssemblyDef(module).Modules.Add(module);
            var file = DsDotNetDocument.CreateAssembly(DsDocumentInfo.CreateDocument(string.Empty), module, true);

            fileNodeCreator = RootDocumentNodeCreator.CreateAssembly(documentTreeView, file);
        }
        DocumentTabReferenceResult CreateMemberRefResult(IDocumentTabService documentTabService, object @ref)
        {
            var resolvedRef = ResolveMemberDef(@ref);

            if (!IsSupportedReference(resolvedRef))
            {
                return(null);
            }
            var newRef = GetReference(@ref);
            var node   = documentTabService.DocumentTreeView.FindNode(newRef);

            if (node == null)
            {
                // If it's eg. a TypeDef, its assembly has been removed from the document list or it
                // was never inserted because adding an assembly had been temporarily disabled.
                // Add the assembly to the list again. Next time the user clicks on the link,
                // FindNode() above will succeed.
                var def = @ref as IMemberDef ?? (@ref as ParamDef)?.DeclaringMethod;
                if (def != null)
                {
                    DsDocument document = null;
                    var        mod      = def.Module;
                    if (mod != null && mod.Assembly != null)
                    {
                        document = DsDotNetDocument.CreateAssembly(DsDocumentInfo.CreateDocument(mod.Location), mod, false);
                    }
                    else if (mod != null)
                    {
                        document = DsDotNetDocument.CreateModule(DsDocumentInfo.CreateDocument(mod.Location), mod, false);
                    }
                    if (document != null)
                    {
                        var existingDocument = documentTabService.DocumentTreeView.DocumentService.GetOrAdd(document);
                        if (existingDocument != document)
                        {
                            documentTabService.DocumentTreeView.DocumentService.ForceAdd(document, true, null);
                        }
                    }
                }

                return(null);
            }

            var content = decompileDocumentTabContentFactory.Create(new IDocumentTreeNodeData[] { node });

            return(new DocumentTabReferenceResult(content, null, a => {
                if (a.Success && !a.HasMovedCaret)
                {
                    GoToReference(content, resolvedRef);
                    a.HasMovedCaret = true;
                }
            }));
        }
Beispiel #5
0
        public void Execute()
        {
            Debug.Assert(!hasExecuted);
            if (hasExecuted)
            {
                throw new InvalidOperationException();
            }
            for (int i = 0; i < nodes.Length; i++)
            {
                var node       = nodes[i];
                var savedState = savedStates[i];

                var  module = node.Document.ModuleDef;
                bool b      = module != null && module.Assembly == null;
                Debug.Assert(b);
                if (!b)
                {
                    throw new InvalidOperationException();
                }

                savedState.ModuleKind = module.Kind;
                ModuleUtils.AddToNewAssemblyDef(module, ModuleKind.Dll, out savedState.Characteristics);

                if (savedState.AssemblyFileNode == null)
                {
                    var asmFile = DsDotNetDocument.CreateAssembly(node.Document);
                    savedState.AssemblyFileNode = node.Context.DocumentTreeView.CreateAssembly(asmFile);
                }

                Debug.Assert(savedState.AssemblyFileNode.Document.Children.Count == 1);
                savedState.AssemblyFileNode.Document.Children.Remove(node.Document);
                Debug.Assert(savedState.AssemblyFileNode.Document.Children.Count == 0);
                savedState.AssemblyFileNode.TreeNode.EnsureChildrenLoaded();
                Debug.Assert(savedState.AssemblyFileNode.TreeNode.Children.Count == 0);
                savedState.AssemblyFileNode.Document.Children.Add(node.Document);

                int index = node.Context.DocumentTreeView.TreeView.Root.DataChildren.ToList().IndexOf(node);
                b = index >= 0;
                Debug.Assert(b);
                if (!b)
                {
                    throw new InvalidOperationException();
                }
                node.Context.DocumentTreeView.TreeView.Root.Children[index] = savedState.AssemblyFileNode.TreeNode;
                savedState.AssemblyFileNode.TreeNode.AddChild(node.TreeNode);
            }
            hasExecuted = true;
        }
Beispiel #6
0
        DocumentTabReferenceResult?CreateMemberRefResult(IDocumentTabService documentTabService, object? @ref)
        {
            var resolvedRef = ResolveMemberDef(@ref);

            if (!IsSupportedReference(resolvedRef))
            {
                return(null);
            }
            if (resolvedRef is MethodDef method && StateMachineHelpers.TryGetKickoffMethod(method, out var kickoffMethod))
            {
                @ref        = kickoffMethod;
                resolvedRef = kickoffMethod;
            }
            var newRef = GetReference(@ref);
            var node   = documentTabService.DocumentTreeView.FindNode(newRef);

            if (node is null)
            {
                // If it's eg. a TypeDef, its assembly has been removed from the document list or it
                // was never inserted because adding an assembly had been temporarily disabled.
                // Add the assembly to the list again. Next time the user clicks on the link,
                // FindNode() above will succeed.
                var def = @ref as IMemberDef ?? (@ref as ParamDef)?.DeclaringMethod;
                if (def is not null)
                {
                    DsDocument?document = null;
                    var        mod      = def.Module;
                    if (mod is not null && mod.Assembly is not null)
                    {
                        document = DsDotNetDocument.CreateAssembly(DsDocumentInfo.CreateDocument(mod.Location), mod, false);
                    }
                    else if (mod is not null)
                    {
                        document = DsDotNetDocument.CreateModule(DsDocumentInfo.CreateDocument(mod.Location), mod, false);
                    }
                    if (document is not null)
                    {
                        var existingDocument = documentTabService.DocumentTreeView.DocumentService.GetOrAdd(document);
                        if (existingDocument != document)
                        {
                            documentTabService.DocumentTreeView.DocumentService.ForceAdd(document, true, null);
                        }
                    }
                }

                return(null);
            }
Beispiel #7
0
        public IDsDocument CreateDocument(DsDocumentInfo documentInfo, string filename, bool isModule)
        {
            try {
                // Quick check to prevent exceptions from being thrown
                if (!File.Exists(filename))
                {
                    return(new DsUnknownDocument(filename));
                }

                IPEImage peImage;

                if (Settings.UseMemoryMappedIO)
                {
                    peImage = new PEImage(filename);
                }
                else
                {
                    peImage = new PEImage(File.ReadAllBytes(filename), filename);
                }

                var  dotNetDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14];
                bool isDotNet  = dotNetDir.VirtualAddress != 0 && dotNetDir.Size >= 0x48;
                if (isDotNet)
                {
                    try {
                        var options = new ModuleCreationOptions(DsDotNetDocumentBase.CreateModuleContext(AssemblyResolver));
                        if (isModule)
                        {
                            return(DsDotNetDocument.CreateModule(documentInfo, ModuleDefMD.Load(peImage, options), true));
                        }
                        return(DsDotNetDocument.CreateAssembly(documentInfo, ModuleDefMD.Load(peImage, options), true));
                    }
                    catch {
                    }
                }

                return(new DsPEDocument(peImage));
            }
            catch {
            }

            return(new DsUnknownDocument(filename));
        }