Пример #1
0
		static void Execute(ILSpyTreeNode[] nodes)
		{
			if (!CanExecute(nodes))
				return;

			var ownerNode = nodes[0];
			if (!(ownerNode is TypeTreeNode))
				ownerNode = (ILSpyTreeNode)ownerNode.Parent;
			var typeNode = ownerNode as TypeTreeNode;
			Debug.Assert(typeNode != null);
			if (typeNode == null)
				throw new InvalidOperationException();

			var module = ILSpyTreeNode.GetModule(typeNode);
			Debug.Assert(module != null);
			if (module == null)
				throw new InvalidOperationException();
			var options = TypeDefOptions.Create(UTF8String.Empty, TypeConstants.DEFAULT_TYPE_NAME, module.CorLibTypes.Object.TypeDefOrRef, true);

			var data = new TypeOptionsVM(options, module, MainWindow.Instance.CurrentLanguage, null);
			var win = new TypeOptionsDlg();
			win.Title = "Create Nested Type";
			win.DataContext = data;
			win.Owner = MainWindow.Instance;
			if (win.ShowDialog() != true)
				return;

			UndoCommandManager.Instance.Add(new CreateNestedTypeDefCommand(typeNode, data.CreateTypeDefOptions()));
		}
Пример #2
0
        RenameNamespaceCommand(string newName, NamespaceTreeNode nsNode)
        {
            this.newName        = newName;
            this.origName       = nsNode.Name;
            this.nsNode         = nsNode;
            this.existingNsNode = (NamespaceTreeNode)nsNode.Parent.Children.FirstOrDefault(a => a is NamespaceTreeNode && AssemblyTreeNode.NamespaceStringEqualsComparer.Equals(newName, ((NamespaceTreeNode)a).Name));

            var module = ILSpyTreeNode.GetModule(nsNode);

            Debug.Assert(module != null);
            if (module == null)
            {
                throw new InvalidOperationException();
            }

            this.origParentNode       = (ILSpyTreeNode)nsNode.Parent;
            this.origParentChildIndex = this.origParentNode.Children.IndexOf(nsNode);
            Debug.Assert(this.origParentChildIndex >= 0);
            if (this.origParentChildIndex < 0)
            {
                throw new InvalidOperationException();
            }

            // Make sure the exact same namespace names are restored if we undo. The names are UTF8
            // strings, but not necessarily canonicalized if it's an obfuscated assembly.
            nsNode.EnsureChildrenFiltered();
            this.origChildren   = nsNode.Children.Cast <TypeTreeNode>().ToArray();
            this.typeNamespaces = new UTF8String[nsNode.Children.Count];
            for (int i = 0; i < this.typeNamespaces.Length; i++)
            {
                this.typeNamespaces[i] = origChildren[i].TypeDef.Namespace;
            }

            this.typeRefInfos = GetTypeRefInfos(module, new[] { nsNode });
        }
Пример #3
0
        static void Execute(ILSpyTreeNode[] nodes)
        {
            if (!CanExecute(nodes))
            {
                return;
            }

            var    nsNode = ILSpyTreeNode.GetNode <NamespaceTreeNode>(nodes[0]);
            string ns     = nsNode == null ? string.Empty : nsNode.Name;

            var module = ILSpyTreeNode.GetModule(nodes[0]);

            Debug.Assert(module != null);
            if (module == null)
            {
                throw new InvalidOperationException();
            }
            var options = TypeDefOptions.Create(ns, TypeConstants.DEFAULT_TYPE_NAME, module.CorLibTypes.Object.TypeDefOrRef, false);

            var data = new TypeOptionsVM(options, module, MainWindow.Instance.CurrentLanguage, null);
            var win  = new TypeOptionsDlg();

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

            var cmd = new CreateTypeDefCommand(module.Types, nodes[0], data.CreateTypeDefOptions());

            UndoCommandManager.Instance.Add(cmd);
            MainWindow.Instance.JumpToReference(cmd.typeNode);
        }
Пример #4
0
        static void Execute(ILSpyTreeNode[] nodes)
        {
            if (!CanExecute(nodes))
            {
                return;
            }

            var eventNode = (EventTreeNode)nodes[0];

            var module = ILSpyTreeNode.GetModule(nodes[0]);

            Debug.Assert(module != null);
            if (module == null)
            {
                throw new InvalidOperationException();
            }

            var data = new EventOptionsVM(new EventDefOptions(eventNode.EventDef), module, MainWindow.Instance.CurrentLanguage, eventNode.EventDef.DeclaringType);
            var win  = new EventOptionsDlg();

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

            UndoCommandManager.Instance.Add(new EventDefSettingsCommand(eventNode, data.CreateEventDefOptions()));
        }
Пример #5
0
        static bool TreeView_IsVisible(TextViewContext context)
        {
            ModuleDef module;

            return(context.TreeView == MainWindow.Instance.treeView &&
                   ((module = ILSpyTreeNode.GetModule(context.SelectedTreeNodes)) != null) &&
                   module.EntryPoint is MethodDef);
        }
Пример #6
0
 static ITokenResolver GetResolver(out DecompileTabState tabState)
 {
     tabState = MainWindow.Instance.GetActiveDecompileTabState();
     if (tabState == null)
     {
         return(null);
     }
     return(ILSpyTreeNode.GetModule(tabState.DecompiledNodes) as ITokenResolver);
 }
Пример #7
0
        MoveNamespaceTypesToEmptypNamespaceCommand(ILSpyTreeNode[] nodes)
        {
            var nsNodes = nodes.Where(a => ((NamespaceTreeNode)a).Name != string.Empty).Select(a => (NamespaceTreeNode)a).ToArray();

            Debug.Assert(nsNodes.Length > 0);
            this.nodes        = new DeletableNodes <NamespaceTreeNode>(nsNodes);
            this.nsTarget     = GetTarget();
            this.typeRefInfos = RenameNamespaceCommand.GetTypeRefInfos(ILSpyTreeNode.GetModule(nodes[0]), nsNodes);
        }
Пример #8
0
        static bool TreeView_IsVisible(TextViewContext context)
        {
            ModuleDef module;

            return(context.TreeView == MainWindow.Instance.treeView &&
                   ((module = ILSpyTreeNode.GetModule(context.SelectedTreeNodes)) != null) &&
                   module.GlobalType != null &&
                   module.GlobalType.FindStaticConstructor() != null);
        }
 static ModuleDefMD GetModule(out DecompileTabState tabState)
 {
     tabState = MainWindow.Instance.GetActiveDecompileTabState();
     if (tabState == null)
     {
         return(null);
     }
     return(ILSpyTreeNode.GetModule(tabState.DecompiledNodes) as ModuleDefMD);
 }
Пример #10
0
        internal static ModuleDef GetModule()
        {
            var tabState = MainWindow.Instance.GetActiveDecompileTabState();

            if (tabState == null)
            {
                return(null);
            }
            return(ILSpyTreeNode.GetModule(tabState.DecompiledNodes) as ModuleDef);
        }
Пример #11
0
        static AddressReference GetAddressReference(ContextMenuEntryContext context)
        {
            if (ShowAddressReferenceInHexEditorCommand.IsVisibleInternal(context))
            {
                return(null);
            }
            if (ShowILRangeInHexEditorCommand.IsVisibleInternal(context))
            {
                return(null);
            }

            if (context.SelectedTreeNodes == null || context.SelectedTreeNodes.Length != 1)
            {
                return(null);
            }
            if (!(context.SelectedTreeNodes[0] is HexTreeNode))
            {
                return(null);
            }

            var mod = ILSpyTreeNode.GetModule(context.SelectedTreeNodes[0]) as ModuleDefMD;

            if (mod == null)
            {
                return(null);
            }
            var pe = mod.MetaData.PEImage;

            var sectNode = context.SelectedTreeNodes[0] as ImageSectionHeaderTreeNode;

            if (sectNode != null)
            {
                if (sectNode.SectionNumber >= pe.ImageSectionHeaders.Count)
                {
                    return(null);
                }
                var sect = pe.ImageSectionHeaders[sectNode.SectionNumber];
                return(new AddressReference(mod.Location, false, sect.PointerToRawData, sect.SizeOfRawData));
            }

            var stgNode = context.SelectedTreeNodes[0] as StorageStreamTreeNode;

            if (stgNode != null)
            {
                if (stgNode.StreamNumber >= mod.MetaData.MetaDataHeader.StreamHeaders.Count)
                {
                    return(null);
                }
                var sh = mod.MetaData.MetaDataHeader.StreamHeaders[stgNode.StreamNumber];

                return(new AddressReference(mod.Location, false, (ulong)mod.MetaData.MetaDataHeader.StartOffset + sh.Offset, sh.StreamSize));
            }

            return(null);
        }
Пример #12
0
 public void Execute(TextViewContext context)
 {
     if (TreeView_IsVisible(context))
     {
         MainWindow.Instance.JumpToReference(ILSpyTreeNode.GetModule(context.SelectedTreeNodes).GlobalType.FindStaticConstructor());
     }
     else if (TextEditor_IsVisible(context))
     {
         MainWindow.Instance.JumpToReference(GoToEntryPointCommand.GetModule().GlobalType.FindStaticConstructor());
     }
 }
Пример #13
0
        static void Execute(ILSpyTreeNode[] nodes)
        {
            if (!CanExecute(nodes))
            {
                return;
            }

            var ownerNode = nodes[0];

            if (!(ownerNode is TypeTreeNode))
            {
                ownerNode = (ILSpyTreeNode)ownerNode.Parent;
            }
            var typeNode = ownerNode as TypeTreeNode;

            Debug.Assert(typeNode != null);
            if (typeNode == null)
            {
                throw new InvalidOperationException();
            }

            var module = ILSpyTreeNode.GetModule(typeNode);

            Debug.Assert(module != null);
            if (module == null)
            {
                throw new InvalidOperationException();
            }

            bool isInstance = !(typeNode.TypeDef.IsAbstract && typeNode.TypeDef.IsSealed);
            var  sig        = isInstance ? MethodSig.CreateInstance(module.CorLibTypes.Void) : MethodSig.CreateStatic(module.CorLibTypes.Void);
            var  options    = MethodDefOptions.Create("MyMethod", sig);

            if (typeNode.TypeDef.IsInterface)
            {
                options.Attributes |= MethodAttributes.Abstract | MethodAttributes.Virtual | MethodAttributes.NewSlot;
            }

            var data = new MethodOptionsVM(options, module, MainWindow.Instance.CurrentLanguage, typeNode.TypeDef, null);
            var win  = new MethodOptionsDlg();

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

            var cmd = new CreateMethodDefCommand(typeNode, data.CreateMethodDefOptions());

            UndoCommandManager.Instance.Add(cmd);
            MainWindow.Instance.JumpToReference(cmd.methodNode);
        }
Пример #14
0
 public void Execute(TextViewContext context)
 {
     if (TreeView_IsVisible(context))
     {
         MainWindow.Instance.JumpToReference(ILSpyTreeNode.GetModule(context.SelectedTreeNodes).EntryPoint);
     }
     else if (TextEditor_IsVisible(context))
     {
         MainWindow.Instance.JumpToReference(GetModule().EntryPoint);
     }
 }
Пример #15
0
        static void Execute(ILSpyTreeNode[] nodes)
        {
            if (!CanExecute(nodes))
            {
                return;
            }

            var ownerNode = nodes[0];

            if (!(ownerNode is TypeTreeNode))
            {
                ownerNode = (ILSpyTreeNode)ownerNode.Parent;
            }
            var typeNode = ownerNode as TypeTreeNode;

            Debug.Assert(typeNode != null);
            if (typeNode == null)
            {
                throw new InvalidOperationException();
            }

            var module = ILSpyTreeNode.GetModule(typeNode);

            Debug.Assert(module != null);
            if (module == null)
            {
                throw new InvalidOperationException();
            }

            bool isInstance = !(typeNode.TypeDefinition.IsAbstract && typeNode.TypeDefinition.IsSealed);
            var  options    = PropertyDefOptions.Create(module, "MyProperty", isInstance);

            var data = new PropertyOptionsVM(options, module, MainWindow.Instance.CurrentLanguage, typeNode.TypeDefinition);
            var win  = new PropertyOptionsDlg();

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

            var cmd = new CreatePropertyDefCommand(typeNode, data.CreatePropertyDefOptions());

            UndoCommandManager.Instance.Add(cmd);
            MainWindow.Instance.JumpToReference(cmd.propNode);
        }
Пример #16
0
        static void Execute(ILSpyTreeNode[] nodes)
        {
            if (!CanExecute(nodes))
            {
                return;
            }

            var ownerNode = nodes[0];

            if (!(ownerNode is TypeTreeNode))
            {
                ownerNode = (ILSpyTreeNode)ownerNode.Parent;
            }
            var typeNode = ownerNode as TypeTreeNode;

            Debug.Assert(typeNode != null);
            if (typeNode == null)
            {
                throw new InvalidOperationException();
            }

            var module = ILSpyTreeNode.GetModule(typeNode);

            Debug.Assert(module != null);
            if (module == null)
            {
                throw new InvalidOperationException();
            }
            var options = EventDefOptions.Create("MyEvent", module.CorLibTypes.GetTypeRef("System", "EventHandler"));

            var data = new EventOptionsVM(options, module, MainWindow.Instance.CurrentLanguage, typeNode.TypeDef);
            var win  = new EventOptionsDlg();

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

            var cmd = new CreateEventDefCommand(typeNode, data.CreateEventDefOptions());

            UndoCommandManager.Instance.Add(cmd);
            MainWindow.Instance.JumpToReference(cmd.eventNode);
        }
Пример #17
0
        static TokenReference GetTokenReference2(ContextMenuEntryContext context)
        {
            if (context == null)
            {
                return(null);
            }
            if (context.Reference != null)
            {
                var tokRef = context.Reference.Reference as TokenReference;
                if (tokRef != null)
                {
                    return(tokRef);
                }

                var mr = context.Reference.Reference as IMemberRef;
                if (mr != null)
                {
                    return(CreateTokenReference(mr.Module, mr));
                }

                var p = context.Reference.Reference as Parameter;
                if (p != null)
                {
                    var pd = p.ParamDef;
                    if (pd != null && pd.DeclaringMethod != null)
                    {
                        return(CreateTokenReference(pd.DeclaringMethod.Module, pd));
                    }
                }
            }
            if (context.SelectedTreeNodes != null && context.SelectedTreeNodes.Length == 1)
            {
                var node = context.SelectedTreeNodes[0] as ITokenTreeNode;
                if (node != null && node.MDTokenProvider != null)
                {
                    var mod = ILSpyTreeNode.GetModule((SharpTreeNode)node);
                    if (mod != null)
                    {
                        return(new TokenReference(mod, node.MDTokenProvider.MDToken.Raw));
                    }
                }
            }

            return(null);
        }
Пример #18
0
        FieldDefSettingsCommand(FieldTreeNode fieldNode, FieldDefOptions options)
        {
            this.fieldNode   = fieldNode;
            this.newOptions  = options;
            this.origOptions = new FieldDefOptions(fieldNode.FieldDefinition);

            this.origParentNode       = (ILSpyTreeNode)fieldNode.Parent;
            this.origParentChildIndex = this.origParentNode.Children.IndexOf(fieldNode);
            Debug.Assert(this.origParentChildIndex >= 0);
            if (this.origParentChildIndex < 0)
            {
                throw new InvalidOperationException();
            }

            this.nameChanged = origOptions.Name != newOptions.Name;
            if (this.nameChanged)
            {
                this.memberRefInfos = RefFinder.FindMemberRefsToThisModule(ILSpyTreeNode.GetModule(fieldNode)).Where(a => RefFinder.FieldEqualityComparerInstance.Equals(a, fieldNode.FieldDefinition)).Select(a => new MemberRefInfo(a)).ToArray();
            }
        }
Пример #19
0
        static AddressReference GetAddressReference(ContextMenuEntryContext context)
        {
            if (context.SelectedTreeNodes == null || context.SelectedTreeNodes.Length != 1)
            {
                return(null);
            }

            var rsrc = context.SelectedTreeNodes[0] as IResourceNode;

            if (rsrc != null && rsrc.FileOffset != 0)
            {
                var mod = ILSpyTreeNode.GetModule((ILSpyTreeNode)rsrc);
                if (mod != null && File.Exists(mod.Location))
                {
                    return(new AddressReference(mod.Location, false, rsrc.FileOffset, rsrc.Length));
                }
            }

            return(null);
        }
Пример #20
0
        public static void WriteOffsetComment(this ITextOutput output, IResourceNode node)
        {
            if (!DecompilerSettingsPanel.CurrentDecompilerSettings.ShowTokenAndRvaComments)
            {
                return;
            }

            ulong fo = node.FileOffset;

            if (fo == 0)
            {
                return;
            }

            var mod      = ILSpyTreeNode.GetModule((SharpTreeNode)node);
            var filename = mod == null ? null : mod.Location;

            output.WriteReference(string.Format("0x{0:X8}", fo), new AddressReference(filename, false, fo, node.Length), TextTokenType.Comment);
            output.Write(": ", TextTokenType.Comment);
        }
Пример #21
0
        public static bool IsInSameModule(this ILSpyTreeNode[] nodes)
        {
            if (nodes == null || nodes.Length == 0)
            {
                return(false);
            }
            var module = ILSpyTreeNode.GetModule(nodes[0]);

            if (module == null)
            {
                return(false);
            }
            for (int i = 0; i < nodes.Length; i++)
            {
                if (module != ILSpyTreeNode.GetModule(nodes[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #22
0
		static void Execute(ILSpyTreeNode[] nodes)
		{
			if (!CanExecute(nodes))
				return;

			var typeNode = (TypeTreeNode)nodes[0];

			var module = ILSpyTreeNode.GetModule(nodes[0]);
			Debug.Assert(module != null);
			if (module == null)
				throw new InvalidOperationException();

			var data = new TypeOptionsVM(new TypeDefOptions(typeNode.TypeDefinition), module, MainWindow.Instance.CurrentLanguage, typeNode.TypeDefinition);
			var win = new TypeOptionsDlg();
			win.DataContext = data;
			win.Owner = MainWindow.Instance;
			if (win.ShowDialog() != true)
				return;

			UndoCommandManager.Instance.Add(new TypeDefSettingsCommand(module, typeNode, data.CreateTypeDefOptions()));
		}
Пример #23
0
            public void Delete(NamespaceTreeNode[] nodes, ILSpyTreeNode[] parents)
            {
                Debug.Assert(parents != null && nodes.Length == parents.Length);
                Debug.Assert(infos == null);
                if (infos != null)
                {
                    throw new InvalidOperationException();
                }

                infos = new ModuleInfo[nodes.Length];

                for (int i = 0; i < infos.Length; i++)
                {
                    var node   = nodes[i];
                    var module = ILSpyTreeNode.GetModule(parents[i]);
                    Debug.Assert(module != null);
                    if (module == null)
                    {
                        throw new InvalidOperationException();
                    }

                    var info = new ModuleInfo(module, node.Children.Count);
                    infos[i] = info;

                    for (int j = 0; j < node.Children.Count; j++)
                    {
                        var typeNode = (TypeTreeNode)node.Children[j];
                        int index    = module.Types.IndexOf(typeNode.TypeDef);
                        Debug.Assert(index >= 0);
                        if (index < 0)
                        {
                            throw new InvalidOperationException();
                        }
                        module.Types.RemoveAt(index);
                        info.Types[j]   = typeNode.TypeDef;
                        info.Indexes[j] = index;
                    }
                }
            }
Пример #24
0
        ObservableCollection <TabInfo> CreateCollection()
        {
            var list = new List <TabInfo>();

            foreach (var tabState in MainWindow.Instance.GetTabStateInOrder())
            {
                var info = new TabInfo();
                info.TabState = tabState;
                var module = ILSpyTreeNode.GetModule(tabState.DecompiledNodes);
                if (module != null)
                {
                    info.FirstModuleName     = module.Name;
                    info.FirstModuleFullName = module.Location;
                }
                else
                {
                    info.FirstModuleName     = string.Empty;
                    info.FirstModuleFullName = string.Empty;
                }
                list.Add(info);
            }
            list.Sort((a, b) => StringComparer.InvariantCultureIgnoreCase.Compare(a.TabState.ShortHeader, b.TabState.ShortHeader));
            return(new ObservableCollection <TabInfo>(list));
        }
Пример #25
0
        internal static void Execute(ILSpyTreeNode[] nodes, uint[] offsets = null)
        {
            if (!CanExecute(nodes))
            {
                return;
            }

            var methodNode = (MethodTreeNode)nodes[0];

            var module = ILSpyTreeNode.GetModule(nodes[0]);

            Debug.Assert(module != null);
            if (module == null)
            {
                throw new InvalidOperationException();
            }

            var data = new MethodBodyVM(new MethodBodyOptions(methodNode.MethodDefinition), module, MainWindow.Instance.CurrentLanguage, methodNode.MethodDefinition.DeclaringType, methodNode.MethodDefinition);
            var win  = new MethodBodyDlg();

            win.DataContext = data;
            win.Owner       = MainWindow.Instance;
            win.Title       = string.Format("{0} - {1}", win.Title, methodNode.ToString());

            if (data.IsCilBody && offsets != null)
            {
                data.CilBodyVM.Select(offsets);
            }

            if (win.ShowDialog() != true)
            {
                return;
            }

            UndoCommandManager.Instance.Add(new MethodBodySettingsCommand(methodNode, data.CreateMethodBodyOptions()));
        }
Пример #26
0
        static void Execute(ILSpyTreeNode[] nodes)
        {
            if (!CanExecute(nodes))
            {
                return;
            }

            var ownerNode = nodes[0];

            if (!(ownerNode is TypeTreeNode))
            {
                ownerNode = (ILSpyTreeNode)ownerNode.Parent;
            }
            var typeNode = ownerNode as TypeTreeNode;

            Debug.Assert(typeNode != null);
            if (typeNode == null)
            {
                throw new InvalidOperationException();
            }

            var module = ILSpyTreeNode.GetModule(typeNode);

            Debug.Assert(module != null);
            if (module == null)
            {
                throw new InvalidOperationException();
            }

            FieldDefOptions options;
            var             type = typeNode.TypeDefinition;

            if (type.IsEnum)
            {
                var ts = type.GetEnumUnderlyingType();
                if (ts != null)
                {
                    options             = FieldDefOptions.Create("MyField", new FieldSig(new ValueTypeSig(typeNode.TypeDefinition)));
                    options.Constant    = module.UpdateRowId(new ConstantUser(ModelUtils.GetDefaultValue(ts), ts.RemovePinnedAndModifiers().GetElementType()));
                    options.Attributes |= FieldAttributes.Literal | FieldAttributes.Static | FieldAttributes.HasDefault;
                }
                else
                {
                    options             = FieldDefOptions.Create("value__", new FieldSig(module.CorLibTypes.Int32));
                    options.Attributes |= FieldAttributes.SpecialName | FieldAttributes.RTSpecialName;
                }
            }
            else if (type.IsAbstract && type.IsSealed)
            {
                options             = FieldDefOptions.Create("MyField", new FieldSig(module.CorLibTypes.Int32));
                options.Attributes |= FieldAttributes.Static;
            }
            else
            {
                options = FieldDefOptions.Create("MyField", new FieldSig(module.CorLibTypes.Int32));
            }

            var data = new FieldOptionsVM(options, module, MainWindow.Instance.CurrentLanguage, type);
            var win  = new FieldOptionsDlg();

            win.Title       = "Create Field";
            win.DataContext = data;
            win.Owner       = MainWindow.Instance;
            if (win.ShowDialog() != true)
            {
                return;
            }

            UndoCommandManager.Instance.Add(new CreateFieldDefCommand(typeNode, data.CreateFieldDefOptions()));
        }