mirror of https://github.com/icsharpcode/ILSpy.git
19 changed files with 604 additions and 174 deletions
@ -0,0 +1,55 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using Mono.Cecil; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes.Analyzer |
||||||
|
{ |
||||||
|
class AnalyzedMethodTreeNode : AnalyzerTreeNode |
||||||
|
{ |
||||||
|
MethodDefinition analyzedMethod; |
||||||
|
|
||||||
|
public AnalyzedMethodTreeNode(MethodDefinition analyzedMethod) |
||||||
|
{ |
||||||
|
if (analyzedMethod == null) |
||||||
|
throw new ArgumentNullException("analyzedMethod"); |
||||||
|
this.analyzedMethod = analyzedMethod; |
||||||
|
this.LazyLoading = true; |
||||||
|
} |
||||||
|
|
||||||
|
public override object Icon { |
||||||
|
get { return MethodTreeNode.GetIcon(analyzedMethod); } |
||||||
|
} |
||||||
|
|
||||||
|
public override object Text { |
||||||
|
get { return Language.TypeToString(analyzedMethod.DeclaringType, true) + "." + MethodTreeNode.GetText(analyzedMethod, Language); } |
||||||
|
} |
||||||
|
|
||||||
|
public override void ActivateItem(System.Windows.RoutedEventArgs e) |
||||||
|
{ |
||||||
|
e.Handled = true; |
||||||
|
MainWindow.Instance.JumpToReference(analyzedMethod); |
||||||
|
} |
||||||
|
|
||||||
|
protected override void LoadChildren() |
||||||
|
{ |
||||||
|
this.Children.Add(new AnalyzedMethodUsedByTreeNode(analyzedMethod)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,103 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using ICSharpCode.NRefactory.Utils; |
||||||
|
using ICSharpCode.TreeView; |
||||||
|
using Mono.Cecil; |
||||||
|
using Mono.Cecil.Cil; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes.Analyzer |
||||||
|
{ |
||||||
|
class AnalyzedMethodUsedByTreeNode : AnalyzerTreeNode |
||||||
|
{ |
||||||
|
MethodDefinition analyzedMethod; |
||||||
|
ThreadingSupport threading; |
||||||
|
|
||||||
|
public AnalyzedMethodUsedByTreeNode(MethodDefinition analyzedMethod) |
||||||
|
{ |
||||||
|
if (analyzedMethod == null) |
||||||
|
throw new ArgumentNullException("analyzedMethod"); |
||||||
|
|
||||||
|
this.analyzedMethod = analyzedMethod; |
||||||
|
this.threading = new ThreadingSupport(); |
||||||
|
this.LazyLoading = true; |
||||||
|
} |
||||||
|
|
||||||
|
public override object Text { |
||||||
|
get { return "Used By"; } |
||||||
|
} |
||||||
|
|
||||||
|
public override object Icon { |
||||||
|
get { return Images.Search; } |
||||||
|
} |
||||||
|
|
||||||
|
protected override void LoadChildren() |
||||||
|
{ |
||||||
|
threading.LoadChildren(this, FetchChildren); |
||||||
|
} |
||||||
|
|
||||||
|
protected override void OnCollapsing() |
||||||
|
{ |
||||||
|
if (threading.IsRunning) { |
||||||
|
this.LazyLoading = true; |
||||||
|
threading.Cancel(); |
||||||
|
this.Children.Clear(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
IEnumerable<SharpTreeNode> FetchChildren(CancellationToken ct) |
||||||
|
{ |
||||||
|
return FindReferences(MainWindow.Instance.AssemblyList.GetAssemblies(), ct); |
||||||
|
} |
||||||
|
|
||||||
|
IEnumerable<SharpTreeNode> FindReferences(LoadedAssembly[] assemblies, CancellationToken ct) |
||||||
|
{ |
||||||
|
// use parallelism only on the assembly level (avoid locks within Cecil)
|
||||||
|
return assemblies.AsParallel().WithCancellation(ct).SelectMany((LoadedAssembly asm) => FindReferences(asm, ct)); |
||||||
|
} |
||||||
|
|
||||||
|
IEnumerable<SharpTreeNode> FindReferences(LoadedAssembly asm, CancellationToken ct) |
||||||
|
{ |
||||||
|
string name = analyzedMethod.Name; |
||||||
|
string declTypeName = analyzedMethod.DeclaringType.FullName; |
||||||
|
foreach (TypeDefinition type in TreeTraversal.PreOrder(asm.AssemblyDefinition.MainModule.Types, t => t.NestedTypes)) { |
||||||
|
ct.ThrowIfCancellationRequested(); |
||||||
|
foreach (MethodDefinition method in type.Methods) { |
||||||
|
ct.ThrowIfCancellationRequested(); |
||||||
|
bool found = false; |
||||||
|
if (!method.HasBody) |
||||||
|
continue; |
||||||
|
foreach (Instruction instr in method.Body.Instructions) { |
||||||
|
MethodReference mr = instr.Operand as MethodReference; |
||||||
|
if (mr != null && mr.Name == name && mr.DeclaringType.FullName == declTypeName && mr.Resolve() == analyzedMethod) { |
||||||
|
found = true; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
if (found) |
||||||
|
yield return new AnalyzedMethodTreeNode(method); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Linq; |
||||||
|
using ICSharpCode.TreeView; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes.Analyzer |
||||||
|
{ |
||||||
|
class AnalyzerTreeNode : SharpTreeNode |
||||||
|
{ |
||||||
|
Language language; |
||||||
|
|
||||||
|
public Language Language { |
||||||
|
get { return language; } |
||||||
|
set { |
||||||
|
if (language != value) { |
||||||
|
language = value; |
||||||
|
foreach (var child in this.Children.OfType<AnalyzerTreeNode>()) |
||||||
|
child.Language = value; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override bool CanDelete() |
||||||
|
{ |
||||||
|
return Parent != null && Parent.IsRoot; |
||||||
|
} |
||||||
|
|
||||||
|
public override void DeleteCore() |
||||||
|
{ |
||||||
|
Parent.Children.Remove(this); |
||||||
|
} |
||||||
|
|
||||||
|
public override void Delete() |
||||||
|
{ |
||||||
|
DeleteCore(); |
||||||
|
} |
||||||
|
|
||||||
|
protected override void OnChildrenChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
||||||
|
{ |
||||||
|
if (e.NewItems != null) { |
||||||
|
foreach (AnalyzerTreeNode a in e.NewItems.OfType<AnalyzerTreeNode>()) |
||||||
|
a.Language = this.Language; |
||||||
|
} |
||||||
|
base.OnChildrenChanged(e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,105 +0,0 @@ |
|||||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
|
||||||
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Collections.Generic; |
|
||||||
using System.Threading; |
|
||||||
using System.Threading.Tasks; |
|
||||||
using System.Windows.Threading; |
|
||||||
using ICSharpCode.Decompiler; |
|
||||||
|
|
||||||
namespace ICSharpCode.ILSpy.TreeNodes |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// Node that is lazy-loaded and loads its children on a background thread.
|
|
||||||
/// </summary>
|
|
||||||
abstract class ThreadedTreeNode : ILSpyTreeNode |
|
||||||
{ |
|
||||||
Task<List<ILSpyTreeNode>> loadChildrenTask; |
|
||||||
|
|
||||||
public ThreadedTreeNode() |
|
||||||
{ |
|
||||||
this.LazyLoading = true; |
|
||||||
} |
|
||||||
|
|
||||||
public void Invalidate() |
|
||||||
{ |
|
||||||
this.LazyLoading = true; |
|
||||||
this.Children.Clear(); |
|
||||||
loadChildrenTask = null; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// FetchChildren() runs on the main thread; but the enumerator is consumed on a background thread
|
|
||||||
/// </summary>
|
|
||||||
protected abstract IEnumerable<ILSpyTreeNode> FetchChildren(CancellationToken ct); |
|
||||||
|
|
||||||
protected override sealed void LoadChildren() |
|
||||||
{ |
|
||||||
this.Children.Add(new LoadingTreeNode()); |
|
||||||
|
|
||||||
CancellationToken ct = CancellationToken.None; |
|
||||||
|
|
||||||
var fetchChildrenEnumerable = FetchChildren(ct); |
|
||||||
Task<List<ILSpyTreeNode>> thisTask = null; |
|
||||||
thisTask = new Task<List<ILSpyTreeNode>>( |
|
||||||
delegate { |
|
||||||
List<ILSpyTreeNode> result = new List<ILSpyTreeNode>(); |
|
||||||
foreach (ILSpyTreeNode child in fetchChildrenEnumerable) { |
|
||||||
ct.ThrowIfCancellationRequested(); |
|
||||||
result.Add(child); |
|
||||||
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action<ILSpyTreeNode>( |
|
||||||
delegate (ILSpyTreeNode newChild) { |
|
||||||
// don't access "child" here the
|
|
||||||
// background thread might already be running the next loop iteration
|
|
||||||
if (loadChildrenTask == thisTask) { |
|
||||||
this.Children.Insert(this.Children.Count - 1, newChild); |
|
||||||
} |
|
||||||
}), child); |
|
||||||
} |
|
||||||
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action( |
|
||||||
delegate { |
|
||||||
if (loadChildrenTask == thisTask) { |
|
||||||
this.Children.RemoveAt(this.Children.Count - 1); // remove 'Loading...'
|
|
||||||
} |
|
||||||
})); |
|
||||||
return result; |
|
||||||
}, ct); |
|
||||||
loadChildrenTask = thisTask; |
|
||||||
thisTask.Start(); |
|
||||||
// Give the task a bit time to complete before we return to WPF - this keeps "Loading..."
|
|
||||||
// from showing up for very short waits.
|
|
||||||
thisTask.Wait(TimeSpan.FromMilliseconds(200)); |
|
||||||
} |
|
||||||
|
|
||||||
public override void Decompile(Language language, ITextOutput output, DecompilationOptions options) |
|
||||||
{ |
|
||||||
var loadChildrenTask = this.loadChildrenTask; |
|
||||||
if (loadChildrenTask == null) { |
|
||||||
App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(EnsureLazyChildren)); |
|
||||||
loadChildrenTask = this.loadChildrenTask; |
|
||||||
} |
|
||||||
if (loadChildrenTask != null) { |
|
||||||
foreach (var child in loadChildrenTask.Result) { |
|
||||||
child.Decompile(language, output, options); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
sealed class LoadingTreeNode : ILSpyTreeNode |
|
||||||
{ |
|
||||||
public override object Text { |
|
||||||
get { return "Loading..."; } |
|
||||||
} |
|
||||||
|
|
||||||
public override FilterResult Filter(FilterSettings settings) |
|
||||||
{ |
|
||||||
return FilterResult.Match; |
|
||||||
} |
|
||||||
|
|
||||||
public override void Decompile(Language language, ICSharpCode.Decompiler.ITextOutput output, DecompilationOptions options) |
|
||||||
{ |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,139 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using System.Windows.Threading; |
||||||
|
using ICSharpCode.Decompiler; |
||||||
|
using ICSharpCode.TreeView; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Adds threading support to nodes
|
||||||
|
/// </summary>
|
||||||
|
class ThreadingSupport |
||||||
|
{ |
||||||
|
Task<List<SharpTreeNode>> loadChildrenTask; |
||||||
|
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); |
||||||
|
|
||||||
|
public bool IsRunning { |
||||||
|
get { return !loadChildrenTask.IsCompleted; } |
||||||
|
} |
||||||
|
|
||||||
|
public void Cancel() |
||||||
|
{ |
||||||
|
cancellationTokenSource.Cancel(); |
||||||
|
loadChildrenTask = null; |
||||||
|
cancellationTokenSource = new CancellationTokenSource(); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Starts loading the children of the specified node.
|
||||||
|
/// </summary>
|
||||||
|
public void LoadChildren(SharpTreeNode node, Func<CancellationToken, IEnumerable<SharpTreeNode>> fetchChildren) |
||||||
|
{ |
||||||
|
node.Children.Add(new LoadingTreeNode()); |
||||||
|
|
||||||
|
CancellationToken ct = cancellationTokenSource.Token; |
||||||
|
|
||||||
|
var fetchChildrenEnumerable = fetchChildren(ct); |
||||||
|
Task<List<SharpTreeNode>> thisTask = null; |
||||||
|
thisTask = new Task<List<SharpTreeNode>>( |
||||||
|
delegate { |
||||||
|
List<SharpTreeNode> result = new List<SharpTreeNode>(); |
||||||
|
foreach (SharpTreeNode child in fetchChildrenEnumerable) { |
||||||
|
ct.ThrowIfCancellationRequested(); |
||||||
|
result.Add(child); |
||||||
|
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action<SharpTreeNode>( |
||||||
|
delegate (SharpTreeNode newChild) { |
||||||
|
// don't access "child" here the
|
||||||
|
// background thread might already be running the next loop iteration
|
||||||
|
if (loadChildrenTask == thisTask) { |
||||||
|
node.Children.Insert(node.Children.Count - 1, newChild); |
||||||
|
} |
||||||
|
}), child); |
||||||
|
} |
||||||
|
return result; |
||||||
|
}, ct); |
||||||
|
loadChildrenTask = thisTask; |
||||||
|
thisTask.Start(); |
||||||
|
thisTask.ContinueWith( |
||||||
|
delegate (Task continuation) { |
||||||
|
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action( |
||||||
|
delegate { |
||||||
|
if (loadChildrenTask == thisTask) { |
||||||
|
node.Children.RemoveAt(node.Children.Count - 1); // remove 'Loading...'
|
||||||
|
} |
||||||
|
if (continuation.Exception != null) { // observe exception even when task isn't current
|
||||||
|
if (loadChildrenTask == thisTask) { |
||||||
|
foreach (Exception ex in continuation.Exception.InnerExceptions) { |
||||||
|
node.Children.Add(new ErrorTreeNode(ex.ToString())); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
})); |
||||||
|
}); |
||||||
|
|
||||||
|
// Give the task a bit time to complete before we return to WPF - this keeps "Loading..."
|
||||||
|
// from showing up for very short waits.
|
||||||
|
thisTask.Wait(TimeSpan.FromMilliseconds(200)); |
||||||
|
} |
||||||
|
|
||||||
|
public void Decompile(Language language, ITextOutput output, DecompilationOptions options, Action ensureLazyChildren) |
||||||
|
{ |
||||||
|
var loadChildrenTask = this.loadChildrenTask; |
||||||
|
if (loadChildrenTask == null) { |
||||||
|
App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, ensureLazyChildren); |
||||||
|
loadChildrenTask = this.loadChildrenTask; |
||||||
|
} |
||||||
|
if (loadChildrenTask != null) { |
||||||
|
foreach (ILSpyTreeNode child in loadChildrenTask.Result.Cast<ILSpyTreeNode>()) { |
||||||
|
child.Decompile(language, output, options); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
sealed class LoadingTreeNode : ILSpyTreeNode |
||||||
|
{ |
||||||
|
public override object Text { |
||||||
|
get { return "Loading..."; } |
||||||
|
} |
||||||
|
|
||||||
|
public override FilterResult Filter(FilterSettings settings) |
||||||
|
{ |
||||||
|
return FilterResult.Match; |
||||||
|
} |
||||||
|
|
||||||
|
public override void Decompile(Language language, ICSharpCode.Decompiler.ITextOutput output, DecompilationOptions options) |
||||||
|
{ |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
sealed class ErrorTreeNode : ILSpyTreeNode |
||||||
|
{ |
||||||
|
string text; |
||||||
|
|
||||||
|
public override object Text { |
||||||
|
get { return text; } |
||||||
|
} |
||||||
|
|
||||||
|
public ErrorTreeNode(string text) |
||||||
|
{ |
||||||
|
this.text = text; |
||||||
|
} |
||||||
|
|
||||||
|
public override FilterResult Filter(FilterSettings settings) |
||||||
|
{ |
||||||
|
return FilterResult.Match; |
||||||
|
} |
||||||
|
|
||||||
|
public override void Decompile(Language language, ICSharpCode.Decompiler.ITextOutput output, DecompilationOptions options) |
||||||
|
{ |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue