Browse Source

Assembly search kind, default to short name

pull/1708/head
Jan Kučera 6 years ago
parent
commit
b342dd6b11
  1. 11
      ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs
  2. 60
      ILSpy/Search/AssemblySearchStrategy.cs
  3. 25
      ILSpy/Search/SearchPane.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))}, " +

60
ILSpy/Search/AssemblySearchStrategy.cs

@ -3,6 +3,7 @@ using System.Collections.Concurrent; @@ -3,6 +3,7 @@ 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;
@ -16,23 +17,62 @@ namespace ICSharpCode.ILSpy.Search @@ -16,23 +17,62 @@ namespace ICSharpCode.ILSpy.Search
{
class AssemblySearchStrategy : AbstractSearchStrategy
{
public AssemblySearchStrategy(IProducerConsumerCollection<SearchResult> resultQueue, string term)
: this(resultQueue, new[] { term })
readonly AssemblySearchKind searchKind;
public AssemblySearchStrategy(string term, IProducerConsumerCollection<SearchResult> resultQueue, AssemblySearchKind searchKind)
: this(resultQueue, new[] { term }, searchKind)
{
}
public AssemblySearchStrategy(IProducerConsumerCollection<SearchResult> resultQueue, string[] terms)
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 (IsMatch(module.FullName))
string name = GetNameToMatch(module);
if (IsMatch(name))
OnFoundResult(module);
}
string GetNameToMatch(PEFile module)
{
switch (searchKind) {
case AssemblySearchKind.FullName:
return module.FullName;
case AssemblySearchKind.Name:
return module.Name;
case AssemblySearchKind.FileName:
return module.FileName;
}
if (!module.IsAssembly)
return null;
var metadata = module.Metadata;
var definition = module.Metadata.GetAssemblyDefinition();
switch (searchKind) {
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 {
@ -46,4 +86,16 @@ namespace ICSharpCode.ILSpy.Search @@ -46,4 +86,16 @@ namespace ICSharpCode.ILSpy.Search
OnFoundResult(result);
}
}
enum AssemblySearchKind
{
Name,
FullName,
FileName,
Culture,
Version,
PublicKey,
HashAlgorithm,
Flags
}
}

25
ILSpy/Search/SearchPane.cs

@ -348,7 +348,28 @@ namespace ICSharpCode.ILSpy @@ -348,7 +348,28 @@ namespace ICSharpCode.ILSpy
return new ResourceSearchStrategy(apiVisibility, resultQueue, searchTerm[0].Substring(2));
if (searchTerm[0].StartsWith("a:", StringComparison.Ordinal))
return new AssemblySearchStrategy(resultQueue, searchTerm[0].Substring(2));
return new AssemblySearchStrategy(searchTerm[0].Substring(2), resultQueue, AssemblySearchKind.Name);
if (searchTerm[0].StartsWith("afn:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(4), resultQueue, AssemblySearchKind.FullName);
if (searchTerm[0].StartsWith("af:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(3), resultQueue, AssemblySearchKind.FileName);
if (searchTerm[0].StartsWith("ac:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(3), resultQueue, AssemblySearchKind.Culture);
if (searchTerm[0].StartsWith("av:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(3), resultQueue, AssemblySearchKind.Version);
if (searchTerm[0].StartsWith("apk:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(4), resultQueue, AssemblySearchKind.PublicKey);
if (searchTerm[0].StartsWith("aha:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(4), resultQueue, AssemblySearchKind.HashAlgorithm);
if (searchTerm[0].StartsWith("afl:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(4), resultQueue, AssemblySearchKind.Flags);
}
switch (searchMode)
@ -374,7 +395,7 @@ namespace ICSharpCode.ILSpy @@ -374,7 +395,7 @@ namespace ICSharpCode.ILSpy
case SearchMode.Resource:
return new ResourceSearchStrategy(apiVisibility, resultQueue, searchTerm);
case SearchMode.Assembly:
return new AssemblySearchStrategy(resultQueue, searchTerm);
return new AssemblySearchStrategy(resultQueue, searchTerm, AssemblySearchKind.Name);
}
return null;

Loading…
Cancel
Save