Ejemplo n.º 1
0
        private void InjectCommandImpl()
        {
            //Flag to determine whther to color the parent nodes
            var cancel = false;

            //Switches on the injection type (new or existing)
            switch (TabSelectedIndex)
            {
            //Inject new
            case 0:
                //Checks that the name has been provided
                if (string.IsNullOrEmpty(this.Name))
                {
                    MessageBox.Show("A name is required", "Name required", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                //Checks that the member has been provided (if needed)
                if (SelectedInjector.NeedsMember && SelectedMember == null)
                {
                    MessageBox.Show("A type is required", "Type required", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                //Checks that the selected type is valid
                if (SelectedInjector.NeedsMember && !SelectedInjector.MemberFilter(this.SelectedMember))
                {
                    MessageBox.Show("The selected type is not valid for '" + this.SelectedInjector.Name + "'", "Type required", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                //Injects
                this.SelectedInjector.Inject(_node, this.Name, this.SelectedInjector.NeedsMember ? this.SelectedMember : null);

                //Break
                break;

            //Inject existing
            case 1:

                //Checks that there's a selection
                if (ExistingSelectedMember == null)
                {
                    MessageBox.Show("Select a valid member to import in '" + _node.Text.ToString() + "'", "", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                //Cancellation token
                var cts = new CancellationTokenSource();
                var ct  = cts.Token;

                //Starts a new task to perform importing
                var t = new Task(() => {
                    //Options
                    var options = new Existing.MemberImportingOptions()
                    {
                        ImportAsNestedType = ExistingImportAsNestedTypes,
                        CancellationToken  = ct
                    };

                    //Importing session
                    var destType   = !(_node is ModuleTreeNode) ? ((IMemberTreeNode)_node).Member as TypeDefinition : null;
                    var destModule = destType != null ? destType.Module : ((ModuleTreeNode)_node).Module;
                    var destAsm    = Helpers.Tree.GetAssemblyNode(Helpers.Tree.GetModuleNode(destModule)).LoadedAssembly.AssemblyDefinition;
                    var session    = new ILEdit.Injection.Existing.MemberImportingSession(destAsm, destModule, destType, options);

                    //Imports
                    using (var importer = session.CreateImporter(ExistingSelectedMember))
                    {
                        //Performs scanning
                        importer.Scan(options);

                        //Checks whether to show the preview
                        if (ExistingPreview)
                        {
                            //Builds the preview
                            var root = new SharpTreeNode();
                            importer.BuildPreviewNodes(root);

                            //Shows the preview window
                            Application.Current.Dispatcher.Invoke((Action)(() => {
                                cancel = !new Existing.PreviewWindow(root, _node).ShowDialog().GetValueOrDefault(false);
                            }), null);
                        }

                        //Performs importing on the dispatcher thread
                        if (!cancel)
                        {
                            Application.Current.Dispatcher.Invoke((Action)(() => { importer.Import(options, _node); }), null);
                        }
                    }
                }, ct);
                t.Start();
                t.ContinueWith(task => {
                    //Hides the window
                    Application.Current.Dispatcher.Invoke((Action)WaitWindow.Hide, null);

                    //Checks for any exception
                    if (task.Exception != null)
                    {
                        var text = string.Join(Environment.NewLine + Environment.NewLine, task.Exception.InnerExceptions.Select(x => x.Message).ToArray());
                        Application.Current.Dispatcher.BeginInvoke((Action)(() => {
                            MessageBox.Show("An exception occurred:" + Environment.NewLine + Environment.NewLine + text, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }), null);
                    }
                });

                //Shows the wait window
                WaitWindow.ShowDialog("Importing in progress ...", "Please, wait while the importing is in progress ...", cts);

                break;

            //Other: exception
            default:
                throw new ArgumentException("Invalid value: it can be only 0 or 1", "TabSelectedIndex");
            }

            //Colors the parents of the node
            if (!cancel)
            {
                //Colors the ancestors of this node
                var parent = (ICSharpCode.TreeView.SharpTreeNode)_node;
                while (parent != null)
                {
                    if (parent.Foreground != GlobalContainer.NewNodesBrush)
                    {
                        parent.Foreground = GlobalContainer.ModifiedNodesBrush;
                    }
                    parent = parent.Parent;
                }

                //Refreshes the view
                ICSharpCode.ILSpy.MainWindow.Instance.RefreshDecompiledView();

                //Saves the settings
                var settings = GlobalContainer.InjectionSettings.Element("InjectExistingSettings");
                settings.SetAttributeValue("Preview", ExistingPreview);
                if (ExistingImportAsNestedTypesEnabled)
                {
                    settings.SetAttributeValue("ImportAsNestedTypes", ExistingImportAsNestedTypes);
                }
                GlobalContainer.SettingsManager.Instance.Save();

                //Closes the window
                _window.Close();
            }
        }
Ejemplo n.º 2
0
        private void InjectCommandImpl()
        {
            //Flag to determine whther to color the parent nodes
            var cancel = false;

            //Switches on the injection type (new or existing)
            switch (TabSelectedIndex)
            {
                //Inject new
                case 0:
                    //Checks that the name has been provided
                    if (string.IsNullOrEmpty(this.Name))
                    {
                        MessageBox.Show("A name is required", "Name required", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    //Checks that the member has been provided (if needed)
                    if (SelectedInjector.NeedsMember && SelectedMember == null)
                    {
                        MessageBox.Show("A type is required", "Type required", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    //Checks that the selected type is valid
                    if (SelectedInjector.NeedsMember && !SelectedInjector.MemberFilter(this.SelectedMember))
                    {
                        MessageBox.Show("The selected type is not valid for '" + this.SelectedInjector.Name + "'", "Type required", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    //Injects
                    this.SelectedInjector.Inject(_node, this.Name, this.SelectedInjector.NeedsMember ? this.SelectedMember : null);
                    
                    //Break
                    break;
               
                //Inject existing
                case 1:

                    //Checks that there's a selection
                    if (ExistingSelectedMember == null)
                    {
                        MessageBox.Show("Select a valid member to import in '" + _node.Text.ToString() + "'", "", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    //Cancellation token
                    var cts = new CancellationTokenSource();
                    var ct = cts.Token;

                    //Starts a new task to perform importing
                    var t = new Task(() => {

                        //Options
                        var options = new Existing.MemberImportingOptions()
                        {
                            ImportAsNestedType = ExistingImportAsNestedTypes,
                            CancellationToken = ct
                        };

                        //Importing session
                        var destType = !(_node is ModuleTreeNode) ? ((IMemberTreeNode)_node).Member as TypeDefinition : null;
                        var destModule = destType != null ? destType.Module : ((ModuleTreeNode)_node).Module;
                        var destAsm = Helpers.Tree.GetAssemblyNode(Helpers.Tree.GetModuleNode(destModule)).LoadedAssembly.AssemblyDefinition;
                        var session = new ILEdit.Injection.Existing.MemberImportingSession(destAsm, destModule, destType, options);

                        //Imports
                        using (var importer = session.CreateImporter(ExistingSelectedMember))
                        {
                            //Performs scanning
                            importer.Scan(options);

                            //Checks whether to show the preview
                            if (ExistingPreview)
                            {

                                //Builds the preview
                                var root = new SharpTreeNode();
                                importer.BuildPreviewNodes(root);

                                //Shows the preview window
                                Application.Current.Dispatcher.Invoke((Action)(() => {
                                    cancel = !new Existing.PreviewWindow(root, _node).ShowDialog().GetValueOrDefault(false);
                                }), null);
                            }

                            //Performs importing on the dispatcher thread
                            if (!cancel)
                                Application.Current.Dispatcher.Invoke((Action)(() => { importer.Import(options, _node); }), null);

                        }

                    }, ct);
                    t.Start();
                    t.ContinueWith(task => {

                        //Hides the window
                        Application.Current.Dispatcher.Invoke((Action)WaitWindow.Hide, null);

                        //Checks for any exception
                        if (task.Exception != null)
                        {
                            var text = string.Join(Environment.NewLine + Environment.NewLine, task.Exception.InnerExceptions.Select(x => x.Message).ToArray());
                            Application.Current.Dispatcher.BeginInvoke((Action)(() => {
                                MessageBox.Show("An exception occurred:" + Environment.NewLine + Environment.NewLine + text, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            }), null);
                        }

                    });

                    //Shows the wait window
                    WaitWindow.ShowDialog("Importing in progress ...", "Please, wait while the importing is in progress ...", cts);

                    break;
                
                //Other: exception
                default:
                    throw new ArgumentException("Invalid value: it can be only 0 or 1", "TabSelectedIndex");
            }

            //Colors the parents of the node
            if (!cancel)
            {
                //Colors the ancestors of this node
                var parent = (ICSharpCode.TreeView.SharpTreeNode)_node;
                while (parent != null)
                {
                    if (parent.Foreground != GlobalContainer.NewNodesBrush)
                        parent.Foreground = GlobalContainer.ModifiedNodesBrush;
                    parent = parent.Parent;
                }

                //Refreshes the view
                ICSharpCode.ILSpy.MainWindow.Instance.RefreshDecompiledView();

                //Saves the settings
                var settings = GlobalContainer.InjectionSettings.Element("InjectExistingSettings");
                settings.SetAttributeValue("Preview", ExistingPreview);
                if (ExistingImportAsNestedTypesEnabled)
                    settings.SetAttributeValue("ImportAsNestedTypes", ExistingImportAsNestedTypes);
                GlobalContainer.SettingsManager.Instance.Save();

                //Closes the window
                _window.Close();
            }
        }