Example #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);
        }
Example #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);
        }
        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;
                }
            }));
        }
Example #4
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);
            }
Example #5
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));
        }