Browse Source

Merge pull request #1708 from miloush/assembly-search

Add assembly search strategy
pull/1728/head
Siegfried Pammer 6 years ago committed by GitHub
parent
commit
5592c9c729
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs
  2. 1
      ILSpy/ILSpy.csproj
  3. 111
      ILSpy/Search/AssemblySearchStrategy.cs
  4. 15
      ILSpy/Search/SearchPane.cs
  5. 9
      ILSpy/Search/SearchResult.cs

11
ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs

@ -49,7 +49,7 @@ namespace ICSharpCode.Decompiler.Metadata @@ -49,7 +49,7 @@ namespace ICSharpCode.Decompiler.Metadata
return publicKeyTokenBytes.TakeLast(8).Reverse().ToHexString(8);
}
public static string GetFullAssemblyName(this MetadataReader reader)
public static string GetPublicKeyToken(this MetadataReader reader)
{
if (!reader.IsAssembly)
return string.Empty;
@ -59,6 +59,15 @@ namespace ICSharpCode.Decompiler.Metadata @@ -59,6 +59,15 @@ namespace ICSharpCode.Decompiler.Metadata
// AssemblyFlags.PublicKey does not apply to assembly definitions
publicKey = CalculatePublicKeyToken(asm.PublicKey, reader);
}
return publicKey;
}
public static string GetFullAssemblyName(this MetadataReader reader)
{
if (!reader.IsAssembly)
return string.Empty;
var asm = reader.GetAssemblyDefinition();
string publicKey = reader.GetPublicKeyToken();
return $"{reader.GetString(asm.Name)}, " +
$"Version={asm.Version}, " +
$"Culture={(asm.Culture.IsNil ? "neutral" : reader.GetString(asm.Culture))}, " +

1
ILSpy/ILSpy.csproj

@ -172,6 +172,7 @@ @@ -172,6 +172,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Search\AbstractEntitySearchStrategy.cs" />
<Compile Include="Search\AssemblySearchStrategy.cs" />
<Compile Include="Search\LiteralSearchStrategy.cs" />
<Compile Include="LoadedAssembly.cs" />
<Compile Include="LoadedAssemblyExtensions.cs" />

111
ILSpy/Search/AssemblySearchStrategy.cs

@ -0,0 +1,111 @@ @@ -0,0 +1,111 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Reflection.Metadata;
using System.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Util;
using ICSharpCode.ILSpy.TreeNodes;
using ICSharpCode.TreeView;
namespace ICSharpCode.ILSpy.Search
{
class AssemblySearchStrategy : AbstractSearchStrategy
{
readonly AssemblySearchKind searchKind;
public AssemblySearchStrategy(string term, IProducerConsumerCollection<SearchResult> resultQueue, AssemblySearchKind searchKind)
: this(resultQueue, new[] { term }, searchKind)
{
}
public AssemblySearchStrategy(IProducerConsumerCollection<SearchResult> resultQueue, string[] terms, AssemblySearchKind searchKind)
: base(resultQueue, terms)
{
this.searchKind = searchKind;
}
public override void Search(PEFile module, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (searchKind == AssemblySearchKind.NameOrFileName) {
string localName = GetNameToMatch(module, AssemblySearchKind.Name);
string fileName = Path.GetFileName(GetNameToMatch(module, AssemblySearchKind.FilePath));
if (IsMatch(localName) || IsMatch(fileName))
OnFoundResult(module);
return;
}
string name = GetNameToMatch(module, searchKind);
if (IsMatch(name))
OnFoundResult(module);
}
string GetNameToMatch(PEFile module, AssemblySearchKind kind)
{
switch (kind) {
case AssemblySearchKind.FullName:
return module.FullName;
case AssemblySearchKind.Name:
return module.Name;
case AssemblySearchKind.FilePath:
return module.FileName;
}
if (!module.IsAssembly)
return null;
var metadata = module.Metadata;
var definition = module.Metadata.GetAssemblyDefinition();
switch (kind) {
case AssemblySearchKind.Culture:
if (definition.Culture.IsNil)
return "neutral";
return metadata.GetString(definition.Culture);
case AssemblySearchKind.Version:
return definition.Version.ToString();
case AssemblySearchKind.PublicKey:
return module.Metadata.GetPublicKeyToken();
case AssemblySearchKind.HashAlgorithm:
return definition.HashAlgorithm.ToString();
case AssemblySearchKind.Flags:
return definition.Flags.ToString();
}
return null;
}
void OnFoundResult(PEFile module)
{
var result = new AssemblySearchResult {
Module = module,
Fitness = 1.0f / module.Name.Length,
Name = module.Name,
Location = module.FileName,
Assembly = module.FullName,
ToolTip = module.FileName,
};
OnFoundResult(result);
}
}
enum AssemblySearchKind
{
NameOrFileName,
Name,
FullName,
FilePath,
Culture,
Version,
PublicKey,
HashAlgorithm,
Flags
}
}

15
ILSpy/Search/SearchPane.cs

@ -76,6 +76,7 @@ namespace ICSharpCode.ILSpy @@ -76,6 +76,7 @@ namespace ICSharpCode.ILSpy
searchModeComboBox.Items.Add(new { Image = Images.Literal, Name = "Constant" });
searchModeComboBox.Items.Add(new { Image = Images.Library, Name = "Metadata Token" });
searchModeComboBox.Items.Add(new { Image = Images.Resource, Name = "Resource" });
searchModeComboBox.Items.Add(new { Image = Images.Assembly, Name = "Assembly" });
ContextMenuProvider.Add(listBox);
MainWindow.Instance.CurrentAssemblyListChanged += MainWindow_Instance_CurrentAssemblyListChanged;
@ -345,6 +346,15 @@ namespace ICSharpCode.ILSpy @@ -345,6 +346,15 @@ namespace ICSharpCode.ILSpy
if (searchTerm[0].StartsWith("r:", StringComparison.Ordinal))
return new ResourceSearchStrategy(apiVisibility, resultQueue, searchTerm[0].Substring(2));
if (searchTerm[0].StartsWith("a:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(2), resultQueue, AssemblySearchKind.NameOrFileName);
if (searchTerm[0].StartsWith("af:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(3), resultQueue, AssemblySearchKind.FilePath);
if (searchTerm[0].StartsWith("an:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(3), resultQueue, AssemblySearchKind.FullName);
}
switch (searchMode)
@ -369,6 +379,8 @@ namespace ICSharpCode.ILSpy @@ -369,6 +379,8 @@ namespace ICSharpCode.ILSpy
return new MetadataTokenSearchStrategy(language, apiVisibility, resultQueue, searchTerm);
case SearchMode.Resource:
return new ResourceSearchStrategy(apiVisibility, resultQueue, searchTerm);
case SearchMode.Assembly:
return new AssemblySearchStrategy(resultQueue, searchTerm, AssemblySearchKind.NameOrFileName);
}
return null;
@ -400,6 +412,7 @@ namespace ICSharpCode.ILSpy @@ -400,6 +412,7 @@ namespace ICSharpCode.ILSpy
Event,
Literal,
Token,
Resource
Resource,
Assembly
}
}

9
ILSpy/Search/SearchResult.cs

@ -94,4 +94,13 @@ namespace ICSharpCode.ILSpy @@ -94,4 +94,13 @@ namespace ICSharpCode.ILSpy
public Resource Resource { get; set; }
public override object Reference => ValueTuple.Create(Resource, Name);
}
public class AssemblySearchResult : SearchResult
{
public PEFile Module { get; set; }
public override object Reference => Module;
public override ImageSource Image => Images.Assembly;
public override ImageSource LocationImage => Images.Library;
}
}
Loading…
Cancel
Save