diff --git a/ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs b/ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs index 561c25704..7d9d2ca5b 100644 --- a/ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs +++ b/ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs @@ -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 // 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))}, " + diff --git a/ILSpy/Search/AssemblySearchStrategy.cs b/ILSpy/Search/AssemblySearchStrategy.cs index fc27fa905..6adaebfb1 100644 --- a/ILSpy/Search/AssemblySearchStrategy.cs +++ b/ILSpy/Search/AssemblySearchStrategy.cs @@ -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 { class AssemblySearchStrategy : AbstractSearchStrategy { - public AssemblySearchStrategy(IProducerConsumerCollection resultQueue, string term) - : this(resultQueue, new[] { term }) + readonly AssemblySearchKind searchKind; + + public AssemblySearchStrategy(string term, IProducerConsumerCollection resultQueue, AssemblySearchKind searchKind) + : this(resultQueue, new[] { term }, searchKind) { } - public AssemblySearchStrategy(IProducerConsumerCollection resultQueue, string[] terms) + public AssemblySearchStrategy(IProducerConsumerCollection 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 OnFoundResult(result); } } + + enum AssemblySearchKind + { + Name, + FullName, + FileName, + Culture, + Version, + PublicKey, + HashAlgorithm, + Flags + } } diff --git a/ILSpy/Search/SearchPane.cs b/ILSpy/Search/SearchPane.cs index e4139836f..1b0aa24ac 100644 --- a/ILSpy/Search/SearchPane.cs +++ b/ILSpy/Search/SearchPane.cs @@ -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 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;