public LoadedAssembly LookupReferencedAssembly(IAssembly asmRef, ModuleDef sourceModule = null, bool delay = false) { FrameworkRedirect.ApplyFrameworkRedirect(ref asmRef, sourceModule); if (asmRef == null) { throw new ArgumentNullException("name"); } if (asmRef.IsContentTypeWindowsRuntime) { return(assemblyList.winRTMetadataLookupCache.GetOrAdd(asmRef.Name, n => LookupWinRTMetadata(n, delay))); } else { // WinMD files have a reference to mscorlib but its version is always 255.255.255.255 // since mscorlib isn't really loaded. The resolver only loads exact versions, so // we must change the version or the resolve will fail. if (asmRef.Name == "mscorlib" && asmRef.Version == invalidMscorlibVersion) { asmRef = new AssemblyNameInfo(asmRef) { Version = newMscorlibVersion } } ; return(LookupReferencedAssembly2(asmRef, sourceModule, delay)); } }
public IDsDocument Resolve(IAssembly assembly, ModuleDef sourceModule = null) { var tempAsm = assembly; FrameworkRedirect.ApplyFrameworkRedirect(ref tempAsm, sourceModule); // OK : System.Runtime 4.0.20.0 => 4.0.0.0 // BAD: System 4.0.0.0 => 2.0.0.0 if (tempAsm.Version.Major >= assembly.Version.Major) { assembly = tempAsm; } // Most people don't have the old mscrolib 1.x files, but there could still be references // to them, eg. some of the older VS SDK Interop assemblies have refs to them. if (assembly.Version.Major == 1 && assembly.Name == mscorlibName && PublicKeyBase.TokenEquals(assembly.PublicKeyOrToken, mscorlibPublicKeyToken)) { assembly = mscorlibRef40; } if (assembly.IsContentTypeWindowsRuntime) { if (failedAssemblyResolveCache.IsFailed(assembly)) { return(null); } var document = ResolveWinMD(assembly, sourceModule); if (document == null) { failedAssemblyResolveCache.MarkFailed(assembly); } return(document); } else { // WinMD files have a reference to mscorlib but its version is always 255.255.255.255 // since mscorlib isn't really loaded. The resolver only loads exact versions, so // we must change the version or the resolve will fail. if (assembly.Name == mscorlibName && assembly.Version == invalidMscorlibVersion) { assembly = new AssemblyNameInfo(assembly) { Version = newMscorlibVersion } } ; if (failedAssemblyResolveCache.IsFailed(assembly)) { return(null); } var document = ResolveNormal(assembly, sourceModule); if (document == null) { failedAssemblyResolveCache.MarkFailed(assembly); } return(document); } }
public IDnSpyFile Resolve(IAssembly assembly, ModuleDef sourceModule = null) { var tempAsm = assembly; FrameworkRedirect.ApplyFrameworkRedirect(ref tempAsm, sourceModule); // OK : System.Runtime 4.0.20.0 => 4.0.0.0 // BAD: System 4.0.0.0 => 2.0.0.0 if (tempAsm.Version.Major >= assembly.Version.Major) { assembly = tempAsm; } if (assembly.IsContentTypeWindowsRuntime) { if (failedAssemblyResolveCache.IsFailed(assembly)) { return(null); } var file = ResolveWinMD(assembly, sourceModule); if (file == null) { failedAssemblyResolveCache.MarkFailed(assembly); } return(file); } else { // WinMD files have a reference to mscorlib but its version is always 255.255.255.255 // since mscorlib isn't really loaded. The resolver only loads exact versions, so // we must change the version or the resolve will fail. if (assembly.Name == mscorlibName && assembly.Version == invalidMscorlibVersion) { assembly = new AssemblyNameInfo(assembly) { Version = newMscorlibVersion } } ; if (failedAssemblyResolveCache.IsFailed(assembly)) { return(null); } var file = ResolveNormal(assembly, sourceModule); if (file == null) { failedAssemblyResolveCache.MarkFailed(assembly); } return(file); } }
public LoadedAssembly LookupReferencedAssembly(IAssembly name, ModuleDef sourceModule = null, bool delay = false) { FrameworkRedirect.ApplyFrameworkRedirect(ref name, sourceModule); if (name == null) { throw new ArgumentNullException("name"); } if (name.IsContentTypeWindowsRuntime) { return(assemblyList.winRTMetadataLookupCache.GetOrAdd(name.Name, n => LookupWinRTMetadata(n, delay))); } else { return(LookupReferencedAssembly(name.FullName, sourceModule, delay)); } }
IDsDocument ResolveNormal(IAssembly assembly, ModuleDef sourceModule) { var fwkKind = GetFrameworkKind(sourceModule, out var netCoreVersion, out var sourceModuleDirectoryHint); IDsDocument document; switch (fwkKind) { case FrameworkKind.Unknown: case FrameworkKind.DotNetFramework2: case FrameworkKind.DotNetFramework4: var tempAsm = assembly; int gacVersion; if (!GacInfo.HasGAC2) { fwkKind = FrameworkKind.DotNetFramework4; } if (fwkKind == FrameworkKind.DotNetFramework4) { FrameworkRedirect.ApplyFrameworkRedirectV4(ref assembly); gacVersion = 4; } else if (fwkKind == FrameworkKind.DotNetFramework2) { FrameworkRedirect.ApplyFrameworkRedirectV2(ref assembly); gacVersion = 2; } else { Debug.Assert(fwkKind == FrameworkKind.Unknown); FrameworkRedirect.ApplyFrameworkRedirect(ref tempAsm, sourceModule); // OK : System.Runtime 4.0.20.0 => 4.0.0.0 // KO : System 4.0.0.0 => 2.0.0.0 if (tempAsm.Version.Major >= assembly.Version.Major) { assembly = tempAsm; } gacVersion = -1; } var existingDocument = documentService.FindAssembly(assembly); if (existingDocument != null) { return(existingDocument); } document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } var gacFile = GacInfo.FindInGac(assembly, gacVersion); if (gacFile != null) { return(documentService.TryGetOrCreateInternal(DsDocumentInfo.CreateDocument(gacFile), true, true)); } foreach (var gacPath in GacInfo.OtherGacPaths) { if (gacVersion == 4) { if (gacPath.Version != GacVersion.V4) { continue; } } else if (gacVersion == 2) { if (gacPath.Version != GacVersion.V2) { continue; } } else { Debug.Assert(gacVersion == -1); } document = TryLoadFromDir(assembly, checkVersion: true, checkPublicKeyToken: true, gacPath.Path); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } } break; case FrameworkKind.DotNetCore: case FrameworkKind.Unity: document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } break; default: throw new InvalidOperationException(); } return(null); }
IDsDocument ResolveNormal(IAssembly assembly, ModuleDef sourceModule) { var fwkKind = GetFrameworkKind(sourceModule, out var netCoreVersion, out var sourceModuleDirectoryHint); if (fwkKind == FrameworkKind.DotNetCore && !dotNetCorePathProvider.HasDotNetCore) fwkKind = FrameworkKind.DotNetFramework4; IDsDocument document; switch (fwkKind) { case FrameworkKind.Unknown: case FrameworkKind.DotNetFramework2: case FrameworkKind.DotNetFramework4: var tempAsm = assembly; int gacVersion; if (!GacInfo.HasGAC2) fwkKind = FrameworkKind.DotNetFramework4; if (fwkKind == FrameworkKind.DotNetFramework4) { FrameworkRedirect.ApplyFrameworkRedirectV4(ref assembly); gacVersion = 4; } else if (fwkKind == FrameworkKind.DotNetFramework2) { FrameworkRedirect.ApplyFrameworkRedirectV2(ref assembly); gacVersion = 2; } else { Debug.Assert(fwkKind == FrameworkKind.Unknown); FrameworkRedirect.ApplyFrameworkRedirect(ref tempAsm, sourceModule); // OK : System.Runtime 4.0.20.0 => 4.0.0.0 // KO : System 4.0.0.0 => 2.0.0.0 if (tempAsm.Version.Major >= assembly.Version.Major) assembly = tempAsm; gacVersion = -1; } var existingDocument = documentService.FindAssembly(assembly); if (existingDocument != null) return existingDocument; document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) return documentService.GetOrAddCanDispose(document, assembly); var gacFile = GacInfo.FindInGac(assembly, gacVersion); if (gacFile != null) return documentService.TryGetOrCreateInternal(DsDocumentInfo.CreateDocument(gacFile), true, true); foreach (var gacPath in GacInfo.OtherGacPaths) { if (gacVersion == 4) { if (gacPath.Version != GacVersion.V4) continue; } else if (gacVersion == 2) { if (gacPath.Version != GacVersion.V2) continue; } else Debug.Assert(gacVersion == -1); document = TryLoadFromDir(assembly, checkVersion: true, checkPublicKeyToken: true, gacPath.Path); if (document != null) return documentService.GetOrAddCanDispose(document, assembly); } break; case FrameworkKind.DotNetCore: case FrameworkKind.Unity: case FrameworkKind.SelfContainedDotNetCore: // If it's a self-contained .NET Core app, we don't need the version since we must only search // the current directory. Debug.Assert(fwkKind == FrameworkKind.DotNetCore || netCoreVersion == null); document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) return documentService.GetOrAddCanDispose(document, assembly); break; default: throw new InvalidOperationException(); } return null; }
IDsDocument ResolveNormal(IAssembly assembly, ModuleDef sourceModule) { var fwkKind = GetFrameworkKind(sourceModule, out var netCoreVersion, out var sourceModuleDirectoryHint); if (fwkKind == FrameworkKind.DotNetCore && !dotNetCorePathProvider.HasDotNetCore) { fwkKind = FrameworkKind.DotNetFramework4; } IDsDocument document; IDsDocument existingDocument; FindAssemblyOptions options; switch (fwkKind) { case FrameworkKind.Unknown: case FrameworkKind.DotNetFramework2: case FrameworkKind.DotNetFramework4: int gacVersion; if (!GacInfo.HasGAC2) { fwkKind = FrameworkKind.DotNetFramework4; } bool redirected; IAssembly tempAsm; if (fwkKind == FrameworkKind.DotNetFramework4) { redirected = FrameworkRedirect.TryApplyFrameworkRedirectV4(assembly, out tempAsm); if (redirected) { assembly = tempAsm; } gacVersion = 4; } else if (fwkKind == FrameworkKind.DotNetFramework2) { redirected = FrameworkRedirect.TryApplyFrameworkRedirectV2(assembly, out tempAsm); if (redirected) { assembly = tempAsm; } gacVersion = 2; } else { Debug.Assert(fwkKind == FrameworkKind.Unknown); redirected = FrameworkRedirect.TryApplyFrameworkRedirect(assembly, sourceModule, out tempAsm); // OK : System.Runtime 4.0.20.0 => 4.0.0.0 // KO : System 4.0.0.0 => 2.0.0.0 if (redirected && tempAsm.Version.Major >= assembly.Version.Major) { assembly = tempAsm; } else { redirected = false; } gacVersion = -1; } options = DsDocumentService.DefaultOptions; // If the assembly was redirected, always compare the version number. This prevents resolving // mscorlib 2.0 when a .NET 4 app references a .NET 2.0-3.5 dll. We should get mscorlib 4.0. if (redirected) { options |= FindAssemblyOptions.Version; } existingDocument = documentService.FindAssembly(assembly, options); if (existingDocument != null) { return(existingDocument); } document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } var gacFile = GacInfo.FindInGac(assembly, gacVersion); if (gacFile != null) { return(documentService.TryGetOrCreateInternal(DsDocumentInfo.CreateDocument(gacFile), true, true)); } foreach (var gacPath in GacInfo.OtherGacPaths) { if (gacVersion == 4) { if (gacPath.Version != GacVersion.V4) { continue; } } else if (gacVersion == 2) { if (gacPath.Version != GacVersion.V2) { continue; } } else { Debug.Assert(gacVersion == -1); } document = TryLoadFromDir(assembly, checkVersion: true, checkPublicKeyToken: true, gacPath.Path); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } } break; case FrameworkKind.DotNetCore: case FrameworkKind.Unity: case FrameworkKind.SelfContainedDotNetCore: case FrameworkKind.WindowsUniversal: // If it's a self-contained .NET Core app, we don't need the version since we must only search // the current directory. Debug.Assert(fwkKind == FrameworkKind.DotNetCore || netCoreVersion == null); document = LookupFromSearchPaths(assembly, sourceModule, sourceModuleDirectoryHint, netCoreVersion); if (document != null) { return(documentService.GetOrAddCanDispose(document, assembly)); } // If it already exists in assembly explorer, use it options = DsDocumentService.DefaultOptions; if (IgnorePublicKey(fwkKind)) { options &= ~FindAssemblyOptions.PublicKeyToken; } existingDocument = documentService.FindAssembly(assembly, options); if (existingDocument != null) { return(existingDocument); } break; default: throw new InvalidOperationException(); } return(null); }
public IDnSpyFile Resolve(IAssembly assembly, ModuleDef sourceModule = null) { FrameworkRedirect.ApplyFrameworkRedirect(ref assembly, sourceModule); if (assembly.IsContentTypeWindowsRuntime) { return(ResolveWinMD(assembly, sourceModule)); } // WinMD files have a reference to mscorlib but its version is always 255.255.255.255 // since mscorlib isn't really loaded. The resolver only loads exact versions, so // we must change the version or the resolve will fail. if (assembly.Name == "mscorlib" && assembly.Version == invalidMscorlibVersion) { assembly = new AssemblyNameInfo(assembly) { Version = newMscorlibVersion } } ; return(ResolveNormal(assembly, sourceModule)); } IDnSpyFile ResolveNormal(IAssembly assembly, ModuleDef sourceModule) { var existingFile = fileManager.FindAssembly(assembly); if (existingFile != null) { return(existingFile); } var file = LookupFromSearchPaths(assembly, sourceModule, true); if (file != null) { return(fileManager.GetOrAddCanDispose(file)); } if (fileManager.Settings.UseGAC) { var gacFile = GacInfo.FindInGac(assembly); if (gacFile != null) { return(fileManager.TryGetOrCreate(DnSpyFileInfo.CreateFile(gacFile), true)); } foreach (var path in GacInfo.OtherGacPaths) { file = TryLoadFromDir(assembly, true, path); if (file != null) { return(fileManager.GetOrAddCanDispose(file)); } } } file = LookupFromSearchPaths(assembly, sourceModule, false); if (file != null) { return(fileManager.GetOrAddCanDispose(file)); } return(null); } IDnSpyFile LookupFromSearchPaths(IAssembly asmName, ModuleDef sourceModule, bool exactCheck) { IDnSpyFile file; string sourceModuleDir = null; if (sourceModule != null && File.Exists(sourceModule.Location)) { sourceModuleDir = Path.GetDirectoryName(sourceModule.Location); file = TryLoadFromDir(asmName, exactCheck, sourceModuleDir); if (file != null) { return(file); } } var ary = asmSearchPathsArray; foreach (var path in ary) { file = TryLoadFromDir(asmName, exactCheck, path); if (file != null) { return(file); } } return(null); } IDnSpyFile TryLoadFromDir(IAssembly asmName, bool exactCheck, string dirPath) { string baseName; try { baseName = Path.Combine(dirPath, asmName.Name); } catch (ArgumentException) { // eg. invalid chars in asmName.Name return(null); } return(TryLoadFromDir2(asmName, exactCheck, baseName + ".dll") ?? TryLoadFromDir2(asmName, exactCheck, baseName + ".exe")); } IDnSpyFile TryLoadFromDir2(IAssembly asmName, bool exactCheck, string filename) { if (!File.Exists(filename)) { return(null); } IDnSpyFile file = null; bool error = true; try { file = fileManager.TryCreateDnSpyFile(DnSpyFileInfo.CreateFile(filename)); if (file == null) { return(null); } file.IsAutoLoaded = true; var asm = file.AssemblyDef; if (asm == null) { return(null); } bool b = exactCheck ? AssemblyNameComparer.CompareAll.Equals(asmName, asm) : AssemblyNameComparer.NameAndPublicKeyTokenOnly.Equals(asmName, asm); if (!b) { return(null); } error = false; return(file); } finally { if (error) { if (file is IDisposable) { ((IDisposable)file).Dispose(); } } } } IDnSpyFile ResolveWinMD(IAssembly assembly, ModuleDef sourceModule) { var existingFile = fileManager.FindAssembly(assembly); if (existingFile != null) { return(existingFile); } foreach (var winmdPath in GacInfo.WinmdPaths) { string file; try { file = Path.Combine(winmdPath, assembly.Name + ".winmd"); } catch (ArgumentException) { continue; } if (File.Exists(file)) { return(fileManager.TryGetOrCreate(DnSpyFileInfo.CreateFile(file), true)); } } return(null); } }