protected override void LoadChildren() { foreach (var r in module.GetAssemblyRefs().OrderBy(r => r.Name.String)) { this.Children.Add(new AssemblyReferenceTreeNode(r, parentAssembly, dnSpyFileListTreeNode)); } foreach (var r in module.GetModuleRefs().OrderBy(r => r.Name.String)) { this.Children.Add(new ModuleReferenceTreeNode(r)); } }
void Resolve(ModuleDef source, ModState parent) { using (_recursionCounter.Enter()) { var name = source.Location ?? source.Name; if (_recursionCounter.Value > 100) { throw _context.Error("recursive module reference to " + name); } if (!_states.TryGetValue(source, out var state)) { var ev = new NetfuserEvent.ResolveSourceModules(_context, source) { ResolveReferences = true, Treat = ModuleTreat.Merge }; if (!IsInMainDir(source.Location)) { ev.Treat = ModuleTreat.Ignore; ev.ResolveReferences = false; } else if (parent != null && (parent.Treat == ModuleTreat.Copy || parent.Treat == ModuleTreat.Embed)) { ev.Treat = parent.Treat; } if (ev.Treat == ModuleTreat.Merge && source.Assembly.CustomAttributes.Any(a => a.TypeFullName == "System.Reflection.AssemblyMetadataAttribute" && a.HasConstructorArguments && Convert.ToString(a.ConstructorArguments[0].Value) == "NotSupported" && Convert.ToString(a.ConstructorArguments[1].Value) == "True")) // these are ref assemblies { ev.Treat = ModuleTreat.Ignore; } _context.Fire(ev); _states.Add(source, state = new ModState(source) { Treat = ev.Treat, Resolve = ev.ResolveReferences }); _context.Info($"module {name}: {state}"); } if (parent != null) { state.ReferencesToMe.Add(parent); } if (!state.Resolve || state.Resolved) { return; } state.Resolved = true; foreach (var aref in source.GetAssemblyRefs()) { var adef = source.Context.AssemblyResolver.Resolve(aref, source); if (adef != null) { foreach (var mdef in adef.Modules) { Resolve(mdef, state); } } } } }
static Dnr2035Version GetDotNetVersion2035Internal(ModuleDef module) { var ver = Dnr2035Version.V20; foreach (var r in module.GetAssemblyRefs()) { if (dotNet35Asms.Contains(r.FullName)) { return(Dnr2035Version.V35); } if (dotNet30Asms.Contains(r.FullName)) { ver = Dnr2035Version.V30; } } var asm = module.Assembly; if (asm != null && module.IsManifestModule) { if (dotNet35Asms.Contains(asm.FullName)) { return(Dnr2035Version.V35); } if (dotNet30Asms.Contains(asm.FullName)) { ver = Dnr2035Version.V30; } } return(ver); }
static Compiler CalculateCompiler(ModuleDef module) { if (module == null) { return(Compiler.Other); } foreach (var asmRef in module.GetAssemblyRefs()) { if (asmRef.Name == nameAssemblyVisualBasic) { return(Compiler.VisualBasic); } } // The VB runtime can also be embedded, and if so, it seems that "Microsoft.VisualBasic.Embedded" // attribute is added to the assembly's custom attributes. var asm = module.Assembly; if (asm != null && asm.CustomAttributes.IsDefined("Microsoft.VisualBasic.Embedded")) { return(Compiler.VisualBasic); } return(Compiler.Other); }
private void assemblyReferencesAdder(ModuleDef moduleDefMD) { var asmResolver = new AssemblyResolver { EnableTypeDefCache = true }; var modCtx = new ModuleContext(asmResolver); asmResolver.DefaultModuleContext = modCtx; var asmRefs = moduleDefMD.GetAssemblyRefs(); moduleDefMD.Context = modCtx; foreach (var asmRef in asmRefs) { try { if (asmRef == null) { continue; } var asm = asmResolver.Resolve(asmRef.FullName, moduleDefMD); if (asm == null) { continue; } moduleDefMD.Context.AssemblyResolver.AddToCache(asm); } catch {} } }
// Token: 0x06000164 RID: 356 RVA: 0x00019900 File Offset: 0x00017B00 public static void Fix(ModuleDef moduleDef) { AssemblyResolver assemblyResolver = new AssemblyResolver(); ModuleContext moduleContext = new ModuleContext(assemblyResolver); assemblyResolver.DefaultModuleContext = moduleContext; assemblyResolver.EnableTypeDefCache = true; List <AssemblyRef> list = moduleDef.GetAssemblyRefs().ToList <AssemblyRef>(); moduleDef.Context = moduleContext; foreach (AssemblyRef assemblyRef in list) { bool flag = assemblyRef == null; bool flag2 = !flag; if (flag2) { AssemblyDef assemblyDef = assemblyResolver.Resolve(assemblyRef.FullName, moduleDef); bool flag3 = assemblyDef == null; bool flag4 = !flag3; if (flag4) { moduleDef.Context.AssemblyResolver.AddToCache(assemblyDef); } } } }
public void ResolveReferences() { var resolver = new AssemblyResolver(); var moduleContext = new ModuleContext(resolver); resolver.DefaultModuleContext = moduleContext; _module.Context = moduleContext; foreach (var probe in _project.ExtraProbePaths) { resolver.PostSearchPaths.Add(probe); } foreach (var @ref in _module.GetAssemblyRefs()) { if (ResolveRef(@ref, resolver)) { _logger.Success($"Resolved {@ref.Name}"); } else { _logger.Error($"Couldn't resolve {@ref.Name}"); } } }
/// <summary> /// Gets the references for the given module. /// </summary> /// <param name="moduleDefinition">The module definition.</param> /// <param name="assemblyResolver">The assembly resolver.</param> /// <param name="ignoreSystem">if set to <c>true</c> [ignore system].</param> /// <param name="logging">The logging.</param> /// <param name="isMainModule">if set to <c>true</c> [is main module].</param> /// <returns></returns> private static IEnumerable <ModuleDef> GetReferencedModules(this ModuleDef moduleDefinition, IAssemblyResolver assemblyResolver, bool ignoreSystem, ILogging logging, bool isMainModule) { var assemblyDefinitions = moduleDefinition.GetAssemblyRefs() .Where(ar => !ignoreSystem || !IsSystem(ar)) .Select(r => TryResolve(assemblyResolver, r, moduleDefinition, logging, isMainModule)).Where(a => a != null); return(assemblyDefinitions.Select(a => a.GetMainModule())); }
IEnumerable<IAssembly> GetAssemblyRefs(ModuleDef module, IEnumerable<string> extraAssemblyReferences) { foreach (var a in module.GetAssemblyRefs()) yield return a; foreach (var s in extraAssemblyReferences) { var info = new AssemblyNameInfo(s); if (info.Version != null) yield return info; } }
public ResXProjectFile(ModuleDef module, string filename, string typeFullName, EmbeddedResource er) { this.filename = filename; TypeFullName = typeFullName; embeddedResource = er; newToOldAsm = new Dictionary<IAssembly, IAssembly>(new AssemblyNameComparer(AssemblyNameComparerFlags.All & ~AssemblyNameComparerFlags.Version)); foreach (var asmRef in module.GetAssemblyRefs()) newToOldAsm[asmRef] = asmRef; }
public ResXProjectFile(ModuleDef module, string filename, string typeFullName, EmbeddedResource er) { this.filename = filename; TypeFullName = typeFullName; embeddedResource = er; newToOldAsm = new Dictionary <IAssembly, IAssembly>(new AssemblyNameComparer(AssemblyNameComparerFlags.All & ~AssemblyNameComparerFlags.Version)); foreach (var asmRef in module.GetAssemblyRefs()) { newToOldAsm[asmRef] = asmRef; } }
static IEnumerable <ModuleDef> GetModules(ModuleDef module) { yield return(module); foreach (var asmRef in module.GetAssemblyRefs()) { var asm = module.Context.AssemblyResolver.Resolve(asmRef, module); if (asm != null) { yield return(asm.ManifestModule); } } }
IEnumerable <AssemblyDef> GetResolvedContractAssemblies(ModuleDef module) { var nonContractAsms = new HashSet <IAssembly>(AssemblyNameComparer.CompareAll); var stack = new Stack <AssemblyRef>(module.GetAssemblyRefs()); while (stack.Count > 0) { cancellationToken.ThrowIfCancellationRequested(); var asmRef = stack.Pop(); if (!IsPublicKeyToken(contractsPublicKeyTokens, asmRef.PublicKeyOrToken?.Token) && !IsOtherReferenceAssembly(asmRef)) { continue; } if (checkedContractsAssemblies.Contains(asmRef)) { continue; } checkedContractsAssemblies.Add(asmRef); var contractsAsm = module.Context.AssemblyResolver.Resolve(asmRef, module); if (contractsAsm != null) { yield return(contractsAsm); foreach (var m in contractsAsm.Modules) { foreach (var ar in m.GetAssemblyRefs()) { cancellationToken.ThrowIfCancellationRequested(); if (IsPublicKeyToken(contractsPublicKeyTokens, ar.PublicKeyOrToken?.Token) || IsOtherReferenceAssembly(ar)) { stack.Push(ar); } else { nonContractAsms.Add(ar); } } } } } foreach (var asmRef in nonContractAsms) { cancellationToken.ThrowIfCancellationRequested(); var asm = module.Context.AssemblyResolver.Resolve(asmRef, module); if (asm != null) { yield return(asm); } } }
public static void Resolve(ModuleDef module) { var asmResolver = new AssemblyResolver(); var modCtx = new ModuleContext(asmResolver); asmResolver.DefaultModuleContext = modCtx; module.Context = modCtx; asmResolver.EnableTypeDefCache = true; foreach (var asmRef in module.GetAssemblyRefs()) { var def = asmResolver.Resolve(asmRef, module); asmResolver.AddToCache(def); } }
static bool IsNonSupportedAssembly(ModuleDef module, AssemblyDef asm, IAssembly assembly) { if (AssemblyNameComparer.NameAndPublicKeyTokenOnly.Equals(asm, assembly)) { return(true); } foreach (var asmRef in module.GetAssemblyRefs()) { if (AssemblyNameComparer.NameAndPublicKeyTokenOnly.Equals(asmRef, assembly)) { return(true); } } return(false); }
IEnumerable <IAssembly> GetAssemblyRefs(ModuleDef module, IEnumerable <string> extraAssemblyReferences) { foreach (var a in module.GetAssemblyRefs()) { yield return(a); } foreach (var s in extraAssemblyReferences) { var info = new AssemblyNameInfo(s); if (info.Version != null) { yield return(info); } } }
private static bool HasReference(ModuleDef module, string findReference, out string[] fullNames) { fullNames = null; var refs = module.GetAssemblyRefs(); if (_useRegex) { var regex = new Regex(findReference); fullNames = refs.Where(assembly => regex.IsMatch(assembly.Name)).Select(assembly => assembly.FullName).ToArray(); } else { fullNames = new[] { refs.FirstOrDefault(assembly => string.Equals(assembly.Name, findReference, StringComparison.OrdinalIgnoreCase))?.FullName }; } return(!string.IsNullOrEmpty(fullNames.FirstOrDefault())); }
public static bool ReferencesModule(ModuleDef sourceModule, ModuleDef targetModule) { if (targetModule == null) { return(false); } if (sourceModule == targetModule) { return(true); } var targetAssembly = targetModule.Assembly; if (targetAssembly != null) { // Don't compare version, there could be binding redirects var asmComparer = new AssemblyNameComparer(AssemblyNameComparerFlags.Name | AssemblyNameComparerFlags.PublicKeyToken | AssemblyNameComparerFlags.Culture | AssemblyNameComparerFlags.ContentType); foreach (var asmRef in sourceModule.GetAssemblyRefs()) { if (asmComparer.Equals(asmRef, targetAssembly)) { return(true); } } if (targetAssembly == sourceModule.Assembly) { foreach (var modRef in sourceModule.GetModuleRefs()) { if (StringComparer.OrdinalIgnoreCase.Equals(modRef.Name.String, targetModule.Name.String)) { return(true); } } } } return(false); }
static Dnr2035Version GetDotNetVersion2035Internal(ModuleDef module) { var ver = Dnr2035Version.V20; foreach (var r in module.GetAssemblyRefs()) { if (dotNet35Asms.Contains(r.FullName)) return Dnr2035Version.V35; if (dotNet30Asms.Contains(r.FullName)) ver = Dnr2035Version.V30; } var asm = module.Assembly; if (asm != null && module.IsManifestModule) { if (dotNet35Asms.Contains(asm.FullName)) return Dnr2035Version.V35; if (dotNet30Asms.Contains(asm.FullName)) ver = Dnr2035Version.V30; } return ver; }
FrameworkKind GetFrameworkKind_AssemblyRefs(ModuleDef module, string frameworkName, out Version version) { AssemblyRef mscorlibRef = null; AssemblyRef systemRuntimeRef = null; // ASP.NET Core *.Views assemblies don't have a TFM attribute, so grab the .NET Core version from an ASP.NET Core asm ref AssemblyRef aspNetCoreRef = null; foreach (var asmRef in module.GetAssemblyRefs()) { var name = asmRef.Name; if (name == mscorlibName) { if (asmRef.Version != invalidMscorlibVersion) { if (mscorlibRef == null || asmRef.Version > mscorlibRef.Version) { mscorlibRef = asmRef; } } } else if (name == systemRuntimeName) { if (systemRuntimeRef == null || asmRef.Version > systemRuntimeRef.Version) { systemRuntimeRef = asmRef; } } else if (name == netstandardName) { if (!dotNetCorePathProvider.HasDotNetCore) { version = null; return(FrameworkKind.DotNetFramework4); } version = null; return(FrameworkKind.Unknown); } else if (StartsWith(name, aspNetCoreName)) { if (aspNetCoreRef == null || asmRef.Version > aspNetCoreRef.Version) { aspNetCoreRef = asmRef; } } } if (systemRuntimeRef != null) { // - .NET Core: // 1.0: System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.1: System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 2.0: System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 2.1: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 2.2: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 3.0: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // - .NET Standard: // 1.0: System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.1: System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.2: System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.3: System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.4: System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.5: System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 2.0: <it has no System.Runtime ref, just a netstandard.dll ref> if (frameworkName != TFM_netstandard) { if (module.IsClr40Exactly && systemRuntimeRef.Version >= minSystemRuntimeNetCoreVersion) { version = aspNetCoreRef?.Version; return(FrameworkKind.DotNetCore); } } } version = null; if (mscorlibRef != null) { // It can't be Unity since we checked that before this method was called. // It can't be .NET Core since it uses System.Runtime. if (mscorlibRef.Version.Major >= 4) { return(FrameworkKind.DotNetFramework4); } // If it's an exe and it's net20-net35, return that if ((module.Characteristics & Characteristics.Dll) == 0) { return(FrameworkKind.DotNetFramework2); } // It's a net20-net35 dll, but it could be referenced by a net4x asm so we // can't return net20-net35. } return(FrameworkKind.Unknown); }
static bool IsNonSupportedAssembly(ModuleDef module, AssemblyDef asm, IAssembly assembly) { if (AssemblyNameComparer.NameAndPublicKeyTokenOnly.Equals(asm, assembly)) return true; foreach (var asmRef in module.GetAssemblyRefs()) { if (AssemblyNameComparer.NameAndPublicKeyTokenOnly.Equals(asmRef, assembly)) return true; } return false; }
IEnumerable<AssemblyDef> GetResolvedContractAssemblies(ModuleDef module) { var nonContractAsms = new HashSet<IAssembly>(AssemblyNameComparer.CompareAll); var stack = new Stack<AssemblyRef>(module.GetAssemblyRefs()); while (stack.Count > 0) { cancellationToken.ThrowIfCancellationRequested(); var asmRef = stack.Pop(); if (!contractsPublicKeyToken.Equals(asmRef.PublicKeyOrToken?.Token)) continue; if (checkedContractsAssemblies.Contains(asmRef)) continue; checkedContractsAssemblies.Add(asmRef); var contractsAsm = module.Context.AssemblyResolver.Resolve(asmRef, module); if (contractsAsm != null) { yield return contractsAsm; foreach (var m in contractsAsm.Modules) { foreach (var ar in m.GetAssemblyRefs()) { cancellationToken.ThrowIfCancellationRequested(); if (contractsPublicKeyToken.Equals(ar.PublicKeyOrToken)) stack.Push(ar); else nonContractAsms.Add(ar); } } } } foreach (var asmRef in nonContractAsms) { cancellationToken.ThrowIfCancellationRequested(); var asm = module.Context.AssemblyResolver.Resolve(asmRef, module); if (asm != null) yield return asm; } }
FrameworkKind GetFrameworkKind_AssemblyRefs(ModuleDef module, string?frameworkName, out Version?version) { AssemblyRef?mscorlibRef = null; AssemblyRef?systemRuntimeRef = null; // ASP.NET Core *.Views assemblies don't have a TFM attribute, so grab the .NET version from an ASP.NET Core asm ref AssemblyRef?aspNetCoreRef = null; foreach (var asmRef in module.GetAssemblyRefs()) { var name = asmRef.Name; if (name == mscorlibName) { if (IsValidMscorlibVersion(asmRef.Version)) { if (mscorlibRef is null || asmRef.Version > mscorlibRef.Version) { mscorlibRef = asmRef; } } } else if (name == systemRuntimeName) { if (systemRuntimeRef is null || asmRef.Version > systemRuntimeRef.Version) { systemRuntimeRef = asmRef; } } else if (name == netstandardName) { version = asmRef.Version; return(FrameworkKind.DotNetStandard); } else if (StartsWith(name, aspNetCoreName)) { if (aspNetCoreRef is null || asmRef.Version > aspNetCoreRef.Version) { aspNetCoreRef = asmRef; } } } if (systemRuntimeRef is not null) { // - .NET Core: // 1.0: System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.1: System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 2.0: System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 2.1: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 2.2: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 3.0: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 3.1: System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // - .NET Standard: // 1.0: System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.1: System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.2: System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.3: System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.4: System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 1.5: System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 2.0: <it has no System.Runtime ref, just a netstandard.dll ref> // 2.1: <it has no System.Runtime ref, just a netstandard.dll ref> // - .NET: // 5.0: System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // 6.0: System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a if (frameworkName != TFM_netstandard) { if (module.IsClr40Exactly && systemRuntimeRef.Version >= minSystemRuntimeNetCoreVersion) { version = aspNetCoreRef?.Version; if (version is null) { // .NET Core 1.0 or 1.1 if (systemRuntimeRef.Version == version_4_1_0_0) { version = new Version(1, 0, 0, 0); } // .NET Core 2.0 else if (systemRuntimeRef.Version == version_4_2_0_0) { version = new Version(2, 0, 0, 0); } // .NET Core 2.1, 2.2 or 3.0 else if (systemRuntimeRef.Version == version_4_2_1_0) { version = new Version(2, 1, 0, 0); } // .NET Core 3.1 else if (systemRuntimeRef.Version == version_4_2_2_0) { version = new Version(3, 1, 0, 0); } // .NET 5 else if (systemRuntimeRef.Version == version_5_0_0_0) { version = version_5_0_0_0; } // .NET 6 else if (systemRuntimeRef.Version == version_6_0_0_0) { version = version_6_0_0_0; } else { Debug.Fail("Unknown .NET Core version"); } } return(FrameworkKind.DotNet); } } } version = null; if (mscorlibRef is not null) { // It can't be Unity since we checked that before this method was called. // It can't be .NET Core since it uses System.Runtime. if (mscorlibRef.Version.Major >= 4) { return(FrameworkKind.DotNetFramework4); } // If it's an exe and it's net20-net35, return that if ((module.Characteristics & Characteristics.Dll) == 0) { return(FrameworkKind.DotNetFramework2); } // It's a net20-net35 dll, but it could be referenced by a net4x asm so we // can't return net20-net35. } return(FrameworkKind.Unknown); }
static IEnumerable<ModuleDef> GetModules(ModuleDef module) { yield return module; foreach (var asmRef in module.GetAssemblyRefs()) { var asm = module.Context.AssemblyResolver.Resolve(asmRef, module); if (asm != null) yield return asm.ManifestModule; } }