Exemplo n.º 1
0
        static uint?GetOperandMDToken(InstructionOperandVM op)
        {
            switch (op.InstructionOperandType)
            {
            case InstructionOperandType.None:
            case InstructionOperandType.SByte:
            case InstructionOperandType.Byte:
            case InstructionOperandType.Int32:
            case InstructionOperandType.Int64:
            case InstructionOperandType.Single:
            case InstructionOperandType.Double:
            case InstructionOperandType.String:
            case InstructionOperandType.BranchTarget:
            case InstructionOperandType.SwitchTargets:
            case InstructionOperandType.Local:
            case InstructionOperandType.Parameter:
                return(null);

            case InstructionOperandType.Field:
            case InstructionOperandType.Method:
            case InstructionOperandType.Token:
            case InstructionOperandType.Type:
                var token = op.Other as IMDTokenProvider;
                return(token == null ? (uint?)null : token.MDToken.ToUInt32());

            case InstructionOperandType.MethodSig:
                var msig = op.Other as MethodSig;
                return(msig == null ? (uint?)null : msig.OriginalToken);

            default:
                throw new InvalidOperationException();
            }
        }
Exemplo n.º 2
0
 public InstructionVM(ModuleDef ownerModule)
 {
     this.ownerModule = ownerModule;
     this.instructionOperandVM = new InstructionOperandVM();
     this.InstructionOperandVM.PropertyChanged += (a, b) => HasErrorUpdated();
     this.codeVM = new ListVM<Code>(codeList, (a, b) => OnCodeUpdated());
 }
Exemplo n.º 3
0
 public InstructionVM(ModuleDef ownerModule)
 {
     this.ownerModule          = ownerModule;
     this.instructionOperandVM = new InstructionOperandVM();
     this.InstructionOperandVM.PropertyChanged += (a, b) => HasErrorUpdated();
     this.codeVM = new ListVM <Code>(codeList, (a, b) => OnCodeUpdated());
 }
Exemplo n.º 4
0
        void AddType(InstructionOperandVM opvm)
        {
            var picker = new DnlibTypePicker(Window.GetWindow(listBox));
            var type   = picker.GetDnlibType(new FlagsTreeViewNodeFilter(VisibleMembersFlags.TypeDef), opvm.Other as ITypeDefOrRef, cilBodyVM.OwnerModule);

            if (type != null)
            {
                opvm.Other = type;
            }
        }
Exemplo n.º 5
0
        void AddMethodDef(InstructionOperandVM opvm)
        {
            var picker = new DnlibTypePicker(Window.GetWindow(listBox));
            var method = picker.GetDnlibType(new FlagsTreeViewNodeFilter(VisibleMembersFlags.MethodDef), opvm.Other as IMethod, cilBodyVM.OwnerModule);

            if (method != null)
            {
                opvm.Other = method;
            }
        }
Exemplo n.º 6
0
 void OnInstructionOperandVMChanged(InstructionOperandVM oldValue, InstructionOperandVM newValue)
 {
     if (oldValue != null)
     {
         oldValue.PropertyChanged -= instructionOperandVM_PropertyChanged;
     }
     if (newValue != null)
     {
         newValue.PropertyChanged += instructionOperandVM_PropertyChanged;
         InitializeOperandType(newValue);
     }
 }
Exemplo n.º 7
0
        void EditMethodSig(InstructionOperandVM opvm)
        {
            var creator = new CreateMethodPropertySig(Window.GetWindow(listBox));
            var opts    = new MethodSigCreatorOptions(cilBodyVM.TypeSigCreatorOptions.Clone("Create MethodSig"));

            opts.CanHaveSentinel = true;
            var sig = (MethodSig)creator.Create(opts, opvm.Other as MethodSig);

            if (sig != null)
            {
                opvm.Other = sig;
            }
        }
Exemplo n.º 8
0
 public void InitializeFrom(InstructionOperandVM other)
 {
     this.InstructionOperandType = other.InstructionOperandType;
     this.SByte.StringValue      = other.SByte.StringValue;
     this.Byte.StringValue       = other.Byte.StringValue;
     this.Int32.StringValue      = other.Int32.StringValue;
     this.Int64.StringValue      = other.Int64.StringValue;
     this.Single.StringValue     = other.Single.StringValue;
     this.Double.StringValue     = other.Double.StringValue;
     this.String.StringValue     = other.String.StringValue;
     this.Other           = other.Other;
     this.OperandListItem = other.OperandListItem;
 }
Exemplo n.º 9
0
        void AddTypeSpec(InstructionOperandVM opvm)
        {
            var  creator = new TypeSigCreator(Window.GetWindow(listBox));
            var  opts    = cilBodyVM.TypeSigCreatorOptions.Clone("Create a TypeSpec");
            bool canceled;
            var  newSig = creator.Create(opts, (opvm.Other as ITypeDefOrRef).ToTypeSig(), out canceled);

            if (canceled)
            {
                return;
            }

            opvm.Other = newSig.ToTypeDefOrRef();
        }
Exemplo n.º 10
0
        void EditSwitchOperand(InstructionOperandVM opvm)
        {
            var data = new SwitchOperandVM(cilBodyVM.InstructionsListVM, opvm.Other as InstructionVM[]);
            var win  = new SwitchOperandDlg();

            win.DataContext = data;
            win.Owner       = Window.GetWindow(listBox) ?? MainWindow.Instance;
            if (win.ShowDialog() != true)
            {
                return;
            }

            opvm.Other = data.GetSwitchList();
        }
Exemplo n.º 11
0
        void AddMemberRef(InstructionOperandVM opvm, MemberRef mr, bool isField)
        {
            var opts    = mr == null ? new MemberRefOptions() : new MemberRefOptions(mr);
            var vm      = new MemberRefVM(opts, cilBodyVM.TypeSigCreatorOptions, isField);
            var creator = new EditMemberRef(Window.GetWindow(listBox));
            var title   = isField ? "Edit Field MemberRef" : "Edit Method MemberRef";

            vm = creator.Edit(title, vm);
            if (vm == null)
            {
                return;
            }

            opvm.Other = vm.CreateMemberRefOptions().Create(cilBodyVM.OwnerModule);
        }
Exemplo n.º 12
0
        void AddFieldMemberRef(InstructionOperandVM opvm)
        {
            MemberRef mr = opvm.Other as MemberRef;
            var       fd = opvm.Other as FieldDef;

            if (fd != null)
            {
                mr = cilBodyVM.OwnerModule.Import(fd);
            }
            if (mr != null && mr.FieldSig == null)
            {
                mr = null;
            }
            AddMemberRef(opvm, mr, true);
        }
Exemplo n.º 13
0
        void AddMethodSpec(InstructionOperandVM opvm)
        {
            var ms      = opvm.Other as MethodSpec;
            var opts    = ms == null ? new MethodSpecOptions() : new MethodSpecOptions(ms);
            var vm      = new MethodSpecVM(opts, cilBodyVM.TypeSigCreatorOptions);
            var creator = new EditMethodSpec(Window.GetWindow(listBox));

            vm = creator.Edit("Edit MethodSpec", vm);
            if (vm == null)
            {
                return;
            }

            opvm.Other = vm.CreateMethodSpecOptions().Create(cilBodyVM.OwnerModule);
        }
Exemplo n.º 14
0
        void AddMethodMemberRef(InstructionOperandVM opvm)
        {
            MemberRef mr = opvm.Other as MemberRef;
            var       md = opvm.Other as MethodDef;
            var       ms = opvm.Other as MethodSpec;

            if (ms != null)
            {
                mr = ms.Method as MemberRef;
                md = ms.Method as MethodDef;
            }
            if (md != null)
            {
                mr = cilBodyVM.OwnerModule.Import(md);
            }
            if (mr != null && mr.MethodSig == null)
            {
                mr = null;
            }
            AddMemberRef(opvm, mr, false);
        }
Exemplo n.º 15
0
        void IEditOperand.Edit(object parameter, InstructionOperandVM opvm)
        {
            MenuCommandFlags flags;

            switch (opvm.InstructionOperandType)
            {
            case InstructionOperandType.Field:
                flags = MenuCommandFlags.FieldDef | MenuCommandFlags.FieldMemberRef;
                ShowMenu(parameter, opvm, flags);
                break;

            case InstructionOperandType.Method:
                flags = MenuCommandFlags.MethodDef | MenuCommandFlags.MethodMemberRef | MenuCommandFlags.MethodSpec;
                ShowMenu(parameter, opvm, flags);
                break;

            case InstructionOperandType.Token:
                flags = MenuCommandFlags.FieldDef | MenuCommandFlags.FieldMemberRef |
                        MenuCommandFlags.MethodDef | MenuCommandFlags.MethodMemberRef | MenuCommandFlags.MethodSpec |
                        MenuCommandFlags.TypeDef | MenuCommandFlags.TypeRef | MenuCommandFlags.TypeSpec;
                ShowMenu(parameter, opvm, flags);
                break;

            case InstructionOperandType.Type:
                flags = MenuCommandFlags.TypeDef | MenuCommandFlags.TypeRef | MenuCommandFlags.TypeSpec;
                ShowMenu(parameter, opvm, flags);
                break;

            case InstructionOperandType.MethodSig:
                EditMethodSig(opvm);
                break;

            case InstructionOperandType.SwitchTargets:
                EditSwitchOperand(opvm);
                break;

            default:
                throw new InvalidOperationException();
            }
        }
Exemplo n.º 16
0
        static void Write(this ITextOutput output, InstructionOperandVM opvm)
        {
            switch (opvm.InstructionOperandType)
            {
            case MethodBody.InstructionOperandType.None:
                break;

            case MethodBody.InstructionOperandType.SByte:   output.Write(opvm.SByte.StringValue, TextTokenType.Number); break;

            case MethodBody.InstructionOperandType.Byte:    output.Write(opvm.Byte.StringValue, TextTokenType.Number); break;

            case MethodBody.InstructionOperandType.Int32:   output.Write(opvm.Int32.StringValue, TextTokenType.Number); break;;

            case MethodBody.InstructionOperandType.Int64:   output.Write(opvm.Int64.StringValue, TextTokenType.Number); break;;

            case MethodBody.InstructionOperandType.Single:  output.Write(opvm.Single.StringValue, TextTokenType.Number); break;;

            case MethodBody.InstructionOperandType.Double:  output.Write(opvm.Double.StringValue, TextTokenType.Number); break;;

            case MethodBody.InstructionOperandType.String:  output.Write(opvm.String.StringValue, TextTokenType.String); break;;

            case MethodBody.InstructionOperandType.Field:
            case MethodBody.InstructionOperandType.Method:
            case MethodBody.InstructionOperandType.Token:
            case MethodBody.InstructionOperandType.Type:
            case MethodBody.InstructionOperandType.MethodSig:
            case MethodBody.InstructionOperandType.SwitchTargets:
            case MethodBody.InstructionOperandType.BranchTarget:
            case MethodBody.InstructionOperandType.Local:
            case MethodBody.InstructionOperandType.Parameter:
                WriteObject(output, opvm.Value, WriteObjectFlags.ShortInstruction);
                break;

            default: throw new InvalidOperationException();
            }
        }
Exemplo n.º 17
0
 public void InitializeFrom(InstructionOperandVM other)
 {
     this.InstructionOperandType = other.InstructionOperandType;
     this.SByte.StringValue = other.SByte.StringValue;
     this.Byte.StringValue = other.Byte.StringValue;
     this.Int32.StringValue = other.Int32.StringValue;
     this.Int64.StringValue = other.Int64.StringValue;
     this.Single.StringValue = other.Single.StringValue;
     this.Double.StringValue = other.Double.StringValue;
     this.String.StringValue = other.String.StringValue;
     this.Other = other.Other;
     this.OperandListItem = other.OperandListItem;
 }
Exemplo n.º 18
0
 void OnCodeUpdated()
 {
     InstructionOperandVM.UpdateOperandType(Code);
     OnPropertyChanged("Code");
     HasErrorUpdated();
 }
Exemplo n.º 19
0
 void OnInstructionOperandVMChanged(InstructionOperandVM oldValue, InstructionOperandVM newValue)
 {
     if (oldValue != null)
         oldValue.PropertyChanged -= instructionOperandVM_PropertyChanged;
     if (newValue != null) {
         newValue.PropertyChanged += instructionOperandVM_PropertyChanged;
         InitializeOperandType(newValue);
     }
 }
Exemplo n.º 20
0
        void InitializeOperandType(InstructionOperandVM instructionOperandVM)
        {
            switch (instructionOperandVM.InstructionOperandType)
            {
            case InstructionOperandType.None:
                Content = null;
                break;

            case InstructionOperandType.SByte:
            case InstructionOperandType.Byte:
            case InstructionOperandType.Int32:
            case InstructionOperandType.Int64:
            case InstructionOperandType.Single:
            case InstructionOperandType.Double:
            case InstructionOperandType.String:
                // Don't cache the TextBox as a field in this class. The error border disappears when
                // switching from a textbox opcode to a non-textbox opcode and back to the textbox
                // again. Only solution seems to be to create a new textbox.
                var textBox = Content as TextBox;
                if (textBox == null)
                {
                    textBox = new TextBox();
                    var binding = new Binding("InstructionOperandVM.Text.StringValue")
                    {
                        Source = this,
                        ValidatesOnDataErrors = true,
                        ValidatesOnExceptions = true,
                        UpdateSourceTrigger   = UpdateSourceTrigger.PropertyChanged,
                    };
                    textBox.SetBinding(TextBox.TextProperty, binding);
                    binding = new Binding("TextBoxStyle")
                    {
                        Source = this,
                    };
                    textBox.SetBinding(TextBox.StyleProperty, binding);
                    Content = textBox;
                }
                break;

            case InstructionOperandType.Field:
            case InstructionOperandType.Method:
            case InstructionOperandType.Token:
            case InstructionOperandType.Type:
            case InstructionOperandType.MethodSig:
            case InstructionOperandType.SwitchTargets:
                var button = Content as FastClickButton;
                if (button == null)
                {
                    button = new FastClickButton();
                    var binding = new Binding("InstructionOperandVM.Other")
                    {
                        Source                = this,
                        Mode                  = BindingMode.OneWay,
                        Converter             = CilObjectConverter.Instance,
                        ValidatesOnDataErrors = true,
                        ValidatesOnExceptions = true,
                        UpdateSourceTrigger   = UpdateSourceTrigger.PropertyChanged,
                    };
                    button.SetBinding(Button.ContentProperty, binding);
                    binding = new Binding("ButtonStyle")
                    {
                        Source = this,
                    };
                    button.SetBinding(Button.StyleProperty, binding);
                    binding = new Binding("ButtonCommand")
                    {
                        Source = this,
                    };
                    button.SetBinding(Button.CommandProperty, binding);
                    button.CommandParameter = button;
                    Content = button;
                }
                break;

            case InstructionOperandType.BranchTarget:
            case InstructionOperandType.Local:
            case InstructionOperandType.Parameter:
                var comboBox = Content as ComboBox;
                if (comboBox == null)
                {
                    comboBox = new ComboBox();
                    comboBox.ItemTemplate = (DataTemplate)GetValue(ComboBoxItemTemplateProperty);
                    ComboBoxAttachedProps.SetSelectionBoxItemTemplate(comboBox, (DataTemplate)GetValue(ComboBoxSelectionBoxItemTemplateProperty));
                    var binding = new Binding("InstructionOperandVM.OperandListVM.Items")
                    {
                        Source = this,
                    };
                    comboBox.SetBinding(ComboBox.ItemsSourceProperty, binding);
                    binding = new Binding("InstructionOperandVM.OperandListVM.SelectedIndex")
                    {
                        Source = this,
                        ValidatesOnDataErrors = true,
                        ValidatesOnExceptions = true,
                        UpdateSourceTrigger   = UpdateSourceTrigger.PropertyChanged,
                    };
                    comboBox.SetBinding(ComboBox.SelectedIndexProperty, binding);
                    binding = new Binding("ComboBoxStyle")
                    {
                        Source = this,
                    };
                    comboBox.SetBinding(ComboBox.StyleProperty, binding);
                    binding = new Binding("ComboBoxItemTemplate")
                    {
                        Source = this,
                    };
                    comboBox.SetBinding(ComboBox.ItemTemplateProperty, binding);
                    binding = new Binding("ComboBoxSelectionBoxItemTemplate")
                    {
                        Source = this,
                    };
                    comboBox.SetBinding(ComboBoxAttachedProps.SelectionBoxItemTemplateProperty, binding);
                    Content = comboBox;
                }
                break;

            default: throw new InvalidOperationException();
            }
        }
Exemplo n.º 21
0
        void ShowMenu(object parameter, InstructionOperandVM opvm, MenuCommandFlags flags)
        {
            var ctxMenu = new ContextMenu();

            MenuItem menuItem;

            if ((flags & (MenuCommandFlags.TypeDef | MenuCommandFlags.TypeRef)) != 0)
            {
                ctxMenu.Items.Add(menuItem = new MenuItem()
                {
                    Header  = "_Type...",
                    Command = new RelayCommand(a => AddType(opvm)),
                });
                MainWindow.CreateMenuItemImage(menuItem, typeof(MethodBodyControl).Assembly, "Class", BackgroundType.ContextMenuItem, true);
            }
            if ((flags & MenuCommandFlags.TypeSpec) != 0)
            {
                ctxMenu.Items.Add(menuItem = new MenuItem()
                {
                    Header  = "Type_Spec...",
                    Command = new RelayCommand(a => AddTypeSpec(opvm)),
                });
                MainWindow.CreateMenuItemImage(menuItem, typeof(MethodBodyControl).Assembly, "Generic", BackgroundType.ContextMenuItem, true);
            }
            if ((flags & MenuCommandFlags.MethodDef) != 0)
            {
                ctxMenu.Items.Add(menuItem = new MenuItem()
                {
                    Header  = "_Method...",
                    Command = new RelayCommand(a => AddMethodDef(opvm)),
                });
                MainWindow.CreateMenuItemImage(menuItem, typeof(MethodBodyControl).Assembly, "Method", BackgroundType.ContextMenuItem, true);
            }
            if ((flags & MenuCommandFlags.MethodMemberRef) != 0)
            {
                ctxMenu.Items.Add(new MenuItem()
                {
                    Header  = "M_ethod MemberRef...",
                    Command = new RelayCommand(a => AddMethodMemberRef(opvm)),
                });
            }
            if ((flags & MenuCommandFlags.MethodSpec) != 0)
            {
                ctxMenu.Items.Add(new MenuItem()
                {
                    Header  = "Met_hodSpec...",
                    Command = new RelayCommand(a => AddMethodSpec(opvm)),
                });
            }
            if ((flags & MenuCommandFlags.FieldDef) != 0)
            {
                ctxMenu.Items.Add(menuItem = new MenuItem()
                {
                    Header  = "_Field...",
                    Command = new RelayCommand(a => AddFieldDef(opvm)),
                });
                MainWindow.CreateMenuItemImage(menuItem, typeof(MethodBodyControl).Assembly, "Field", BackgroundType.ContextMenuItem, true);
            }
            if ((flags & MenuCommandFlags.FieldMemberRef) != 0)
            {
                ctxMenu.Items.Add(new MenuItem()
                {
                    Header  = "F_ield MemberRef...",
                    Command = new RelayCommand(a => AddFieldMemberRef(opvm)),
                });
            }

            ctxMenu.Placement       = PlacementMode.Bottom;
            ctxMenu.PlacementTarget = parameter as UIElement;
            ctxMenu.IsOpen          = true;
        }
Exemplo n.º 22
0
        void InitializeOperandType(InstructionOperandVM instructionOperandVM)
        {
            switch (instructionOperandVM.InstructionOperandType) {
            case InstructionOperandType.None:
                Content = null;
                break;

            case InstructionOperandType.SByte:
            case InstructionOperandType.Byte:
            case InstructionOperandType.Int32:
            case InstructionOperandType.Int64:
            case InstructionOperandType.Single:
            case InstructionOperandType.Double:
            case InstructionOperandType.String:
                // Don't cache the TextBox as a field in this class. The error border disappears when
                // switching from a textbox opcode to a non-textbox opcode and back to the textbox
                // again. Only solution seems to be to create a new textbox.
                var textBox = Content as TextBox;
                if (textBox == null) {
                    textBox = new TextBox();
                    var binding = new Binding("InstructionOperandVM.Text.StringValue") {
                        Source = this,
                        ValidatesOnDataErrors = true,
                        ValidatesOnExceptions = true,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                    };
                    textBox.SetBinding(TextBox.TextProperty, binding);
                    binding = new Binding("TextBoxStyle") {
                        Source = this,
                    };
                    textBox.SetBinding(TextBox.StyleProperty, binding);
                    Content = textBox;
                }
                break;

            case InstructionOperandType.Field:
            case InstructionOperandType.Method:
            case InstructionOperandType.Token:
            case InstructionOperandType.Type:
            case InstructionOperandType.MethodSig:
            case InstructionOperandType.SwitchTargets:
                var button = Content as FastClickButton;
                if (button == null) {
                    button = new FastClickButton();
                    var binding = new Binding("InstructionOperandVM.Other") {
                        Source = this,
                        Mode = BindingMode.OneWay,
                        Converter = CilObjectConverter.Instance,
                        ValidatesOnDataErrors = true,
                        ValidatesOnExceptions = true,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                    };
                    button.SetBinding(Button.ContentProperty, binding);
                    binding = new Binding("ButtonStyle") {
                        Source = this,
                    };
                    button.SetBinding(Button.StyleProperty, binding);
                    binding = new Binding("ButtonCommand") {
                        Source = this,
                    };
                    button.SetBinding(Button.CommandProperty, binding);
                    button.CommandParameter = button;
                    Content = button;
                }
                break;

            case InstructionOperandType.BranchTarget:
            case InstructionOperandType.Local:
            case InstructionOperandType.Parameter:
                var comboBox = Content as ComboBox;
                if (comboBox == null) {
                    comboBox = new ComboBox();
                    comboBox.ItemTemplate = (DataTemplate)GetValue(ComboBoxItemTemplateProperty);
                    ComboBoxAttachedProps.SetSelectionBoxItemTemplate(comboBox, (DataTemplate)GetValue(ComboBoxSelectionBoxItemTemplateProperty));
                    var binding = new Binding("InstructionOperandVM.OperandListVM.Items") {
                        Source = this,
                    };
                    comboBox.SetBinding(ComboBox.ItemsSourceProperty, binding);
                    binding = new Binding("InstructionOperandVM.OperandListVM.SelectedIndex") {
                        Source = this,
                        ValidatesOnDataErrors = true,
                        ValidatesOnExceptions = true,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                    };
                    comboBox.SetBinding(ComboBox.SelectedIndexProperty, binding);
                    binding = new Binding("ComboBoxStyle") {
                        Source = this,
                    };
                    comboBox.SetBinding(ComboBox.StyleProperty, binding);
                    binding = new Binding("ComboBoxItemTemplate") {
                        Source = this,
                    };
                    comboBox.SetBinding(ComboBox.ItemTemplateProperty, binding);
                    binding = new Binding("ComboBoxSelectionBoxItemTemplate") {
                        Source = this,
                    };
                    comboBox.SetBinding(ComboBoxAttachedProps.SelectionBoxItemTemplateProperty, binding);
                    Content = comboBox;
                }
                break;

            default: throw new InvalidOperationException();
            }
        }