/*
Copyright (C) 2014-2019 de4dot@gmail.com
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see .
*/
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using dnlib.DotNet;
using dnSpy.AsmEditor.Commands;
using dnSpy.AsmEditor.Properties;
using dnSpy.AsmEditor.UndoRedo;
using dnSpy.Contracts.Controls;
using dnSpy.Contracts.Documents.Tabs;
using dnSpy.Contracts.Documents.TreeView;
using dnSpy.Contracts.Extension;
using dnSpy.Contracts.Images;
using dnSpy.Contracts.Menus;
using dnSpy.Contracts.Utilities;
namespace dnSpy.AsmEditor.Event {
[ExportAutoLoaded]
sealed class CommandLoader : IAutoLoaded {
[ImportingConstructor]
CommandLoader(IWpfCommandService wpfCommandService, IDocumentTabService documentTabService, DeleteEventDefCommand.EditMenuCommand removeCmd, DeleteEventDefCommand.CodeCommand removeCmd2, EventDefSettingsCommand.EditMenuCommand settingsCmd, EventDefSettingsCommand.CodeCommand settingsCmd2) {
wpfCommandService.AddRemoveCommand(removeCmd);
wpfCommandService.AddRemoveCommand(removeCmd2, documentTabService);
wpfCommandService.AddSettingsCommand(documentTabService, settingsCmd, settingsCmd2);
}
}
[DebuggerDisplay("{Description}")]
sealed class DeleteEventDefCommand : IUndoCommand {
[ExportMenuItem(Header = "res:DeleteEventCommand", Icon = DsImagesAttribute.Cancel, InputGestureText = "res:DeleteCommandKey", Group = MenuConstants.GROUP_CTX_DOCUMENTS_ASMED_DELETE, Order = 60)]
sealed class DocumentsCommand : DocumentsContextMenuHandler {
readonly Lazy undoCommandService;
[ImportingConstructor]
DocumentsCommand(Lazy undoCommandService) => this.undoCommandService = undoCommandService;
public override bool IsVisible(AsmEditorContext context) => DeleteEventDefCommand.CanExecute(context.Nodes);
public override void Execute(AsmEditorContext context) => DeleteEventDefCommand.Execute(undoCommandService, context.Nodes);
public override string? GetHeader(AsmEditorContext context) => DeleteEventDefCommand.GetHeader(context.Nodes);
}
[Export, ExportMenuItem(OwnerGuid = MenuConstants.APP_MENU_EDIT_GUID, Header = "res:DeleteEventCommand", Icon = DsImagesAttribute.Cancel, InputGestureText = "res:DeleteCommandKey", Group = MenuConstants.GROUP_APP_MENU_EDIT_ASMED_DELETE, Order = 60)]
internal sealed class EditMenuCommand : EditMenuHandler {
readonly Lazy undoCommandService;
[ImportingConstructor]
EditMenuCommand(Lazy undoCommandService, IDocumentTreeView documentTreeView)
: base(documentTreeView) => this.undoCommandService = undoCommandService;
public override bool IsVisible(AsmEditorContext context) => DeleteEventDefCommand.CanExecute(context.Nodes);
public override void Execute(AsmEditorContext context) => DeleteEventDefCommand.Execute(undoCommandService, context.Nodes);
public override string? GetHeader(AsmEditorContext context) => DeleteEventDefCommand.GetHeader(context.Nodes);
}
[Export, ExportMenuItem(Header = "res:DeleteEventCommand", Icon = DsImagesAttribute.Cancel, InputGestureText = "res:DeleteCommandKey", Group = MenuConstants.GROUP_CTX_DOCVIEWER_ASMED_DELETE, Order = 60)]
internal sealed class CodeCommand : CodeContextMenuHandler {
readonly Lazy undoCommandService;
[ImportingConstructor]
CodeCommand(Lazy undoCommandService, IDocumentTreeView documentTreeView)
: base(documentTreeView) => this.undoCommandService = undoCommandService;
public override bool IsEnabled(CodeContext context) => context.IsDefinition && DeleteEventDefCommand.CanExecute(context.Nodes);
public override void Execute(CodeContext context) => DeleteEventDefCommand.Execute(undoCommandService, context.Nodes);
public override string? GetHeader(CodeContext context) => DeleteEventDefCommand.GetHeader(context.Nodes);
}
static string GetHeader(DocumentTreeNodeData[] nodes) {
if (nodes.Length == 1)
return string.Format(dnSpy_AsmEditor_Resources.DeleteX, UIUtilities.EscapeMenuItemHeader(nodes[0].ToString()));
return string.Format(dnSpy_AsmEditor_Resources.DeleteEvents, nodes.Length);
}
static bool CanExecute(DocumentTreeNodeData[] nodes) => nodes.Length > 0 && nodes.All(n => n is EventNode);
static void Execute(Lazy undoCommandService, DocumentTreeNodeData[] nodes) {
if (!CanExecute(nodes))
return;
var eventNodes = nodes.Cast().ToArray();
undoCommandService.Value.Add(new DeleteEventDefCommand(eventNodes));
}
struct DeleteModelNodes {
ModelInfo[]? infos;
readonly struct ModelInfo {
public readonly TypeDef OwnerType;
public readonly int EventIndex;
public readonly int[] MethodIndexes;
public readonly MethodDef[] Methods;
public ModelInfo(EventDef evt) {
OwnerType = evt.DeclaringType;
EventIndex = OwnerType.Events.IndexOf(evt);
Debug.Assert(EventIndex >= 0);
Methods = new HashSet(GetMethods(evt)).ToArray();
MethodIndexes = new int[Methods.Length];
}
static IEnumerable GetMethods(EventDef evt) {
if (evt.AddMethod is not null)
yield return evt.AddMethod;
if (evt.InvokeMethod is not null)
yield return evt.InvokeMethod;
if (evt.RemoveMethod is not null)
yield return evt.RemoveMethod;
foreach (var m in evt.OtherMethods)
yield return m;
}
}
public void Delete(EventNode[] nodes) {
Debug2.Assert(infos is null);
if (infos is not null)
throw new InvalidOperationException();
infos = new ModelInfo[nodes.Length];
for (int i = 0; i < infos.Length; i++) {
var node = nodes[i];
var info = new ModelInfo(node.EventDef);
infos[i] = info;
info.OwnerType.Events.RemoveAt(info.EventIndex);
for (int j = 0; j < info.Methods.Length; j++) {
int index = info.OwnerType.Methods.IndexOf(info.Methods[j]);
Debug.Assert(index >= 0);
if (index < 0)
throw new InvalidOperationException();
info.OwnerType.Methods.RemoveAt(index);
info.MethodIndexes[j] = index;
}
}
}
public void Restore(EventNode[] nodes) {
Debug2.Assert(infos is not null);
if (infos is null)
throw new InvalidOperationException();
Debug.Assert(infos.Length == nodes.Length);
if (infos.Length != nodes.Length)
throw new InvalidOperationException();
for (int i = infos.Length - 1; i >= 0; i--) {
var node = nodes[i];
ref readonly var info = ref infos[i];
info.OwnerType.Events.Insert(info.EventIndex, node.EventDef);
for (int j = info.Methods.Length - 1; j >= 0; j--)
info.OwnerType.Methods.Insert(info.MethodIndexes[j], info.Methods[j]);
}
infos = null;
}
}
DeletableNodes nodes;
DeleteModelNodes modelNodes;
DeleteEventDefCommand(EventNode[] eventNodes) => nodes = new DeletableNodes(eventNodes);
public string Description => dnSpy_AsmEditor_Resources.DeleteEventCommand;
public void Execute() {
nodes.Delete();
modelNodes.Delete(nodes.Nodes);
}
public void Undo() {
modelNodes.Restore(nodes.Nodes);
nodes.Restore();
}
public IEnumerable