Exemplo n.º 1
0
        /// <summary>
        /// Fills the given node with the nodes necessary for the preview of the importing. This action requires a call to Scan()
        /// </summary>
        /// <param name="root"></param>
        public void BuildPreviewNodes(SharpTreeNode root)
        {
            //Checks that the root node isn't null
            if (root == null)
            {
                throw new ArgumentNullException("root");
            }

            //Checks if Scan() has already been called
            if (!Scanned)
            {
                throw new InvalidOperationException("Cannot build preview nodes before a call to Scan()");
            }

            //Gets the members needed by the import list and by this instance
            var members = GetMembersForPreview().ToArray();

            //Checks that there's at least one element
            if (members.Length == 0)
            {
                return;
            }

            //Builds up to the type nodes
            var typeNodes = BuildTypeNodes(members);

            //Lists of the assembly and module nodes (needed for preview of assembly references)
            var asmNodes    = new List <ILEditTreeNode>();
            var moduleNodes = new List <ILEditTreeNode>();

            //Groups by assembly, module and then namespace
            var grouped =
                typeNodes.GroupBy(x => x.Member.Module)
                .OrderBy(x => x.Key.Name)
                .Select(x => x.OrderBy(y => y.Text.ToString()).GroupBy(y => ((TypeDefinition)y.Member).Namespace).OrderBy(y => y.Key).GroupBy(y => x.Key).ElementAt(0))
                .GroupBy(x => x.Key.Assembly)
                .OrderBy(x => x.Key.Name.Name)
                .Select(x =>
            {
                //Assembly node
                var asmNode = new ILEditTreeNode(x.Key, true)
                {
                    IsExpanded = true, Foreground = GlobalContainer.NormalNodesBrush
                };
                foreach (var m in x)
                {
                    //Module node
                    var moduleNode = new ILEditTreeNode(m.Key, true)
                    {
                        IsExpanded = true, Foreground = GlobalContainer.NormalNodesBrush
                    };
                    foreach (var n in m)
                    {
                        //Namespace node
                        var namespaceNode = new NamespaceTreeNode(n.Key)
                        {
                            IsExpanded = true, Foreground = GlobalContainer.NormalNodesBrush
                        };
                        foreach (var t in n)
                        {
                            namespaceNode.Children.Add(t);
                        }
                        moduleNode.Children.Add(namespaceNode);
                    }
                    moduleNodes.Add(moduleNode);
                    asmNode.Children.Add(moduleNode);
                }
                asmNodes.Add(asmNode);
                return(asmNode);
            });

            //Clears the root
            root.Children.Clear();

            //Adds the nodes to the root
            foreach (var x in grouped)
            {
                root.Children.Add(x);
            }

            //Groups the references by module
            var references =
                members.OfType <Importers.AssemblyReferenceImporter>()
                .GroupBy(
                    x => (ModuleDefinition)x.Destination,
                    x => (AssemblyNameReference)x.Member
                    );

            //Creates the references nodes
            foreach (var refs in references)
            {
                //Creates and populates the references node
                var refFolder = new ILEditTreeNode.ReferenceFolderNode();
                foreach (var r in refs)
                {
                    refFolder.Children.Add(new ILEditTreeNode(r, true)
                    {
                        IsExpanded = true,
                        Foreground = GlobalContainer.ModifiedNodesBrush
                    });
                }

                //Finds the module node to add to
                var moduleNode = moduleNodes.FirstOrDefault(x => (ModuleDefinition)x.TokenProvider == refs.Key);
                if (moduleNode != null)
                {
                    //Adds the references to the module node
                    moduleNode.Children.Insert(0, refFolder);
                }
                else
                {
                    //Finds or creates the assembly node
                    var asmNode = asmNodes.FirstOrDefault(x => (AssemblyDefinition)x.TokenProvider == refs.Key.Assembly);
                    if (asmNode != null)
                    {
                        //Adds a module node to the assembly
                        asmNode.Children.Add(new ILEditTreeNode(refs.Key, true)
                        {
                            IsExpanded = true,
                            Foreground = GlobalContainer.NormalNodesBrush,
                            Children   = { refFolder }
                        });
                    }
                    else
                    {
                        //Creates the nodes and adds it to the root
                        root.Children.Add(new ILEditTreeNode(refs.Key.Assembly, true)
                        {
                            IsExpanded = true,
                            Foreground = GlobalContainer.NormalNodesBrush,
                            Children   =
                            {
                                new ILEditTreeNode(refs.Key, true)
                                {
                                    IsExpanded = true,
                                    Foreground = GlobalContainer.NormalNodesBrush,
                                    Children   = { refFolder }
                                }
                            }
                        });
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Fills the given node with the nodes necessary for the preview of the importing. This action requires a call to Scan()
        /// </summary>
        /// <param name="root"></param>
        public void BuildPreviewNodes(SharpTreeNode root)
        {
            //Checks that the root node isn't null
            if (root == null)
                throw new ArgumentNullException("root");

            //Checks if Scan() has already been called
            if (!Scanned)
                throw new InvalidOperationException("Cannot build preview nodes before a call to Scan()");

            //Gets the members needed by the import list and by this instance
            var members = GetMembersForPreview().ToArray();

            //Checks that there's at least one element
            if (members.Length == 0)
                return;

            //Builds up to the type nodes
            var typeNodes = BuildTypeNodes(members);

            //Lists of the assembly and module nodes (needed for preview of assembly references)
            var asmNodes = new List<ILEditTreeNode>();
            var moduleNodes = new List<ILEditTreeNode>();

            //Groups by assembly, module and then namespace
            var grouped =
                typeNodes.GroupBy(x => x.Member.Module)
                .OrderBy(x => x.Key.Name)
                .Select(x => x.OrderBy(y => y.Text.ToString()).GroupBy(y => ((TypeDefinition)y.Member).Namespace).OrderBy(y => y.Key).GroupBy(y => x.Key).ElementAt(0))
                .GroupBy(x => x.Key.Assembly)
                .OrderBy(x => x.Key.Name.Name)
                .Select(x =>
                {
                    //Assembly node
                    var asmNode = new ILEditTreeNode(x.Key, true) { IsExpanded = true, Foreground = GlobalContainer.NormalNodesBrush };
                    foreach (var m in x)
                    {
                        //Module node
                        var moduleNode = new ILEditTreeNode(m.Key, true) { IsExpanded = true, Foreground = GlobalContainer.NormalNodesBrush };
                        foreach (var n in m)
                        {
                            //Namespace node
                            var namespaceNode = new NamespaceTreeNode(n.Key) { IsExpanded = true, Foreground = GlobalContainer.NormalNodesBrush };
                            foreach (var t in n)
                                namespaceNode.Children.Add(t);
                            moduleNode.Children.Add(namespaceNode);
                        }
                        moduleNodes.Add(moduleNode);
                        asmNode.Children.Add(moduleNode);
                    }
                    asmNodes.Add(asmNode);
                    return asmNode;
                });

            //Clears the root
            root.Children.Clear();

            //Adds the nodes to the root
            foreach (var x in grouped)
                root.Children.Add(x);

            //Groups the references by module
            var references =
                members.OfType<Importers.AssemblyReferenceImporter>()
                .GroupBy(
                    x => (ModuleDefinition)x.Destination,
                    x => (AssemblyNameReference)x.Member
                );

            //Creates the references nodes
            foreach(var refs in references)
            {
                //Creates and populates the references node
                var refFolder = new ILEditTreeNode.ReferenceFolderNode();
                foreach (var r in refs)
                    refFolder.Children.Add(new ILEditTreeNode(r, true) { 
                        IsExpanded = true, 
                        Foreground = GlobalContainer.ModifiedNodesBrush 
                    });
                
                //Finds the module node to add to
                var moduleNode = moduleNodes.FirstOrDefault(x => (ModuleDefinition)x.TokenProvider == refs.Key);
                if (moduleNode != null)
                {
                    //Adds the references to the module node
                    moduleNode.Children.Insert(0, refFolder);
                }
                else
                {
                    //Finds or creates the assembly node
                    var asmNode = asmNodes.FirstOrDefault(x => (AssemblyDefinition)x.TokenProvider == refs.Key.Assembly);
                    if (asmNode != null)
                    {
                        //Adds a module node to the assembly
                        asmNode.Children.Add(new ILEditTreeNode(refs.Key, true) { 
                            IsExpanded = true, 
                            Foreground = GlobalContainer.NormalNodesBrush, 
                            Children = { refFolder } 
                        });
                    }
                    else
                    {
                        //Creates the nodes and adds it to the root
                        root.Children.Add(new ILEditTreeNode(refs.Key.Assembly, true) {
                            IsExpanded = true,
                            Foreground = GlobalContainer.NormalNodesBrush,
                            Children = {
                                new ILEditTreeNode(refs.Key, true) {
                                IsExpanded = true,
                                Foreground = GlobalContainer.NormalNodesBrush,
                                    Children = { refFolder }
                                }
                            }
                        });
                    }
                }
            }
        }