public static ModuleDefinition ReferencedMscorlib(this ModuleDefinition self) { if (self == null) { throw new ArgumentNullException("Module definition is null."); } if (self.Assembly.Name.Name == "mscorlib") { return(self); } AssemblyNameReference assemblyRef = self.ReferencedMscorlibRef(); IAssemblyResolver resolver = self.AssemblyResolver; SpecialTypeAssembly special = self.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; AssemblyDefinition a = resolver.Resolve(assemblyRef, "", self.GetModuleArchitecture(), special); if (a != null) { return(a.MainModule); } return(null); }
/*Telerik Authorship*/ public string ResolveAssemblyPath(string strongName, SpecialTypeAssembly special) { AssemblyNameReference nameRef = AssemblyNameReference.Parse(strongName); TargetArchitecture architecture = GetArchitectureFromStrongName(strongName); AssemblyName assemblyName = new AssemblyName(nameRef.Name, nameRef.FullName, nameRef.Version, nameRef.PublicKeyToken) { TargetArchitecture = architecture }; AssemblyStrongNameExtended assemblyKey = new AssemblyStrongNameExtended(assemblyName.FullName, architecture, special); IEnumerable <string> files = assemblyPathResolver.GetAssemblyPaths(assemblyName, assemblyKey); foreach (string file in files) { if (GetAssemblyDefinition(file).main_module.GetModuleArchitecture().CanReference(architecture)) { return(file); } } return(string.Empty); }
private AssemblyStrongNameExtended GetExtendedStrongName(ModuleDefinition moduleDefinition) { AssemblyNameReference assemblyNameReference = moduleDefinition.Assembly.Name; SpecialTypeAssembly special = moduleDefinition.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; return(new AssemblyStrongNameExtended(assemblyNameReference.FullName, moduleDefinition.GetModuleArchitecture(), special)); }
private Dictionary <string, List <string> > GetModuleCollisionTypesData(ModuleDefinition module, ILanguage language) { Dictionary <string, List <string> > collisionTypesData = new Dictionary <string, List <string> >(language.IdentifierComparer); Dictionary <string, string> typeNamesFirstOccurrence = new Dictionary <string, string>(language.IdentifierComparer); UpdateCollisionTypesDataWithTypes(collisionTypesData, typeNamesFirstOccurrence, module.Types); Dictionary <AssemblyNameReference, List <TypeReference> > dependingOnAssembliesToUsedTypesMap = GetModuleDependsOnAnalysis(module); SpecialTypeAssembly special = module.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; foreach (AssemblyNameReference assemblyNameReference in dependingOnAssembliesToUsedTypesMap.Keys) { AssemblyDefinition referencedAssembly = module.AssemblyResolver.Resolve(assemblyNameReference, "", module.GetModuleArchitecture(), special); if (referencedAssembly != null) { foreach (ModuleDefinition referencedModule in referencedAssembly.Modules) { UpdateCollisionTypesDataWithTypes(collisionTypesData, typeNamesFirstOccurrence, referencedModule.Types); } } else { UpdateCollisionTypesDataWithTypes(collisionTypesData, typeNamesFirstOccurrence, dependingOnAssembliesToUsedTypesMap[assemblyNameReference]); } } return(collisionTypesData); }
private Dictionary <string, HashSet <string> > GetModuleNamespaceHierarchy(ModuleDefinition module, ILanguage language) { Dictionary <string, HashSet <string> > namespaceHierarchy = new Dictionary <string, HashSet <string> >(language.IdentifierComparer); UpdateNamespaceHiearchyDataWithTypes(namespaceHierarchy, module.Types); Dictionary <AssemblyNameReference, List <TypeReference> > dependingOnAssembliesToUsedTypesMap = GetModuleDependsOnAnalysis(module); SpecialTypeAssembly special = module.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; foreach (AssemblyNameReference assemblyNameReference in dependingOnAssembliesToUsedTypesMap.Keys) { AssemblyDefinition referencedAssembly = module.AssemblyResolver.Resolve(assemblyNameReference, "", module.GetModuleArchitecture(), special); if (referencedAssembly != null) { UpdateNamespaceHiearchyDataWithTypes(namespaceHierarchy, referencedAssembly.MainModule.Types); } else { UpdateNamespaceHiearchyDataWithTypes(namespaceHierarchy, dependingOnAssembliesToUsedTypesMap[assemblyNameReference]); } } return(namespaceHierarchy); }
public bool CheckFileExistence(AssemblyName assemblyName, string searchPattern, bool caching, bool checkForBaseDir, bool checkForArchitectPlatfrom = true) { AssemblyName assemblyNameFromStorage; if (TryGetAssemblyNameDefinition(searchPattern, caching, assemblyName.TargetArchitecture, out assemblyNameFromStorage, checkForArchitectPlatfrom)) { var areEquals = AreVersionEquals(assemblyNameFromStorage.Version, assemblyName.Version) && ArePublicKeyEquals(assemblyNameFromStorage.PublicKeyToken, assemblyName.PublicKeyToken) && assemblyName.TargetArchitecture.CanReference(assemblyNameFromStorage.TargetArchitecture) && (!checkForBaseDir || AreDefaultDirEqual(assemblyName, assemblyNameFromStorage)); if (areEquals && caching) { /*Telerik Authorship*/ ModuleDefinition module = AssemblyDefinition.ReadAssembly(searchPattern, readerParameters).MainModule; SpecialTypeAssembly special = module.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; AssemblyStrongNameExtended assemblyKey = new AssemblyStrongNameExtended(assemblyName.FullName, assemblyName.TargetArchitecture, special); pathRepository.AssemblyPathName.Add(assemblyKey, searchPattern); if (!pathRepository.AssemblyPathArchitecture.ContainsKey(searchPattern)) { TargetArchitecture architecture = module.GetModuleArchitecture(); pathRepository.AssemblyPathArchitecture.Add(new KeyValuePair <string, TargetArchitecture>(searchPattern, architecture)); } } return(areEquals); } return(false); }
/*Telerik Authorship*/ private AssemblyStrongNameExtended GetAssemblyKey(AssemblyDefinition assemblyDefinition) { ModuleDefinition moduleDefinition = assemblyDefinition.MainModule; SpecialTypeAssembly special = moduleDefinition.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; return(new AssemblyStrongNameExtended(assemblyDefinition.FullName, moduleDefinition.GetModuleArchitecture(), special)); }
protected virtual void CreateProjectReferenceInternal(ModuleDefinition module, AssemblyNameReference reference, ref int assemblyReferenceIndex, SpecialTypeAssembly special, string referencesPath, string copiedReferencesSubfolder) { AssemblyName assemblyName = new AssemblyName(reference.Name, reference.FullName, reference.Version, reference.PublicKeyToken); AssemblyStrongNameExtended assemblyKey = new AssemblyStrongNameExtended(assemblyName.FullName, module.Architecture, special); string currentReferenceInitialLocation = this.currentAssemblyResolver.FindAssemblyPath(assemblyName, null, assemblyKey); AssemblyDefinition referencedAssembly = this.currentAssemblyResolver.Resolve(reference, "", assembly.MainModule.GetModuleArchitecture(), special); #if NET35 if (!currentReferenceInitialLocation.IsNullOrWhiteSpace()) #else if (!string.IsNullOrWhiteSpace(currentReferenceInitialLocation)) #endif { if (this.IsInReferenceAssemblies(referencedAssembly)) { //TODO: Consider doing additional check, to see if the assembly is resolved because it was pointed by the used/already in the tree // In this case, it might be better to copy it. this.projectFileManager.AddReferenceProjectItem(assemblyReferenceIndex, reference.Name); } else // Copy the referenced assembly { if (!Directory.Exists(referencesPath)) { Directory.CreateDirectory(referencesPath); } string currentReferenceFileName = Path.GetFileName(currentReferenceInitialLocation); string currentReferenceFinalLocation = Path.Combine(referencesPath, currentReferenceFileName); File.Copy(currentReferenceInitialLocation, currentReferenceFinalLocation, true); // set to normal for testing purposes- to allow the test project to delete the coppied file between test runs File.SetAttributes(currentReferenceFinalLocation, FileAttributes.Normal); string relativePath = Path.Combine(".", copiedReferencesSubfolder); relativePath = Path.Combine(relativePath, currentReferenceFileName); this.projectFileManager.AddReferenceProjectItem( assemblyReferenceIndex, Path.GetFileNameWithoutExtension(currentReferenceFinalLocation), relativePath); } } else { this.projectFileManager.AddReferenceProjectItem(assemblyReferenceIndex, reference.FullName); } }
internal void AddToAssemblyPathNameCache(AssemblyName assemblyName, string filePath) { /*Telerik Authorship*/ ModuleDefinition module = AssemblyDefinition.ReadAssembly(filePath, readerParameters).MainModule; SpecialTypeAssembly special = module.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; AssemblyStrongNameExtended assemblyKey = new AssemblyStrongNameExtended(assemblyName.FullName, assemblyName.TargetArchitecture, special); pathRepository.AssemblyPathName.Add(assemblyKey, filePath); if (!pathRepository.AssemblyPathArchitecture.ContainsKey(filePath)) { pathRepository.AssemblyPathArchitecture.Add(new KeyValuePair <string, TargetArchitecture>(filePath, assemblyName.TargetArchitecture)); } }
/*Telerik Authorship*/ public virtual void AddToAssemblyCache(string filePath, TargetArchitecture platform, bool storeAssemblyDefInCahce = false) { assemblyPathResolver.AddToAssemblyCache(filePath, platform); AddSearchDirectory(Path.GetDirectoryName(filePath)); if (storeAssemblyDefInCahce && !filePathToAssemblyDefinitionCache.ContainsKey(filePath)) { AssemblyDefinition assemblyDef = LoadAssemblyDefinition(filePath, new ReaderParameters(this), loadPdb: true); if (assemblyDef != null) { string strongName = assemblyDef.MainModule.Name; TargetArchitecture architecture = assemblyDef.MainModule.Architecture; SpecialTypeAssembly special = assemblyDef.MainModule.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; AssemblyStrongNameExtended assemblyKey = new AssemblyStrongNameExtended(strongName, architecture, special); AddToResolvedAssemblies(assemblyDef); assemblyPathResolver.RemoveFromUnresolvedCache(assemblyKey); } } }
protected virtual void CreateProjectReferences(ModuleDefinition module) { ICollection <AssemblyNameReference> dependingOnAssemblies = GetAssembliesDependingOn(module); this.projectFileManager.CreateReferencesProjectItem(dependingOnAssemblies.Count); string assemblyName = module.IsMain ? module.Assembly.Name.Name : Utilities.GetNetmoduleName(module); string copiedReferencesSubfolder = assemblyName + "References"; string referencesPath = TargetPath.Remove(TargetPath.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar + copiedReferencesSubfolder; ICollection <AssemblyNameReference> filteredDependingOnAssemblies = FilterDependingOnAssemblies(dependingOnAssemblies); int assemblyReferenceIndex = 0; SpecialTypeAssembly special = module.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; foreach (AssemblyNameReference reference in filteredDependingOnAssemblies) { this.CreateProjectReferenceInternal(module, reference, ref assemblyReferenceIndex, special, referencesPath, copiedReferencesSubfolder); assemblyReferenceIndex++; } }
public void AddToAssemblyCache(string filePath, TargetArchitecture architecture) { AssemblyName assemblyName; if (TryGetAssemblyNameDefinition(filePath, true, architecture, out assemblyName)) { TargetPlatform platform = GetTargetPlatform(filePath); if (!pathRepository.AssemblyParts.ContainsKey(filePath)) { pathRepository.AssemblyParts.Add(filePath, platform); } /*Telerik Authorship*/ ModuleDefinition module = AssemblyDefinition.ReadAssembly(filePath, readerParameters).MainModule; SpecialTypeAssembly special = module.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; AssemblyStrongNameExtended assemblyKey = new AssemblyStrongNameExtended(assemblyName.FullName, assemblyName.TargetArchitecture, special); if (!pathRepository.AssemblyPathName.ContainsKey(assemblyKey)) { CheckFileExistence(assemblyName, filePath, true, false); RemoveFromUnresolvedCache(assemblyKey); } } }
public override AssemblyDefinition Resolve(AssemblyNameReference name, string path, TargetArchitecture architecture, SpecialTypeAssembly special, bool addToFailedCache, bool bubbleToUserIfFailed = true) { return(base.Resolve(name, path, architecture, special, addToFailedCache, bubbleToUserIfFailed: false)); }
public override AssemblyDefinition Resolve(string fullName, ReaderParameters parameters, TargetArchitecture platform, SpecialTypeAssembly special, bool bubbleToUserIfFailed = true) { return(base.Resolve(fullName, parameters, platform, special, bubbleToUserIfFailed: false)); }
protected override void CreateProjectReferenceInternal(ModuleDefinition module, AssemblyNameReference reference, ref int assemblyReferenceIndex, SpecialTypeAssembly special, string referencesPath, string copiedReferencesSubfolder) { AssemblyName assemblyName = new AssemblyName(reference.Name, reference.FullName, reference.Version, reference.PublicKeyToken); AssemblyStrongNameExtended assemblyKey = new AssemblyStrongNameExtended(assemblyName.FullName, module.Architecture, special); string currentReferenceInitialLocation = this.currentAssemblyResolver.FindAssemblyPath(assemblyName, null, assemblyKey); AssemblyDefinition referencedAssembly = this.currentAssemblyResolver.Resolve(reference, "", assembly.MainModule.GetModuleArchitecture(), special); #if NET35 if (!currentReferenceInitialLocation.IsNullOrWhiteSpace()) #else if (!string.IsNullOrWhiteSpace(currentReferenceInitialLocation)) #endif { if (!this.IsInDotNetAssemblies(referencedAssembly)) { if (!Directory.Exists(referencesPath)) { Directory.CreateDirectory(referencesPath); } string currentReferenceFileName = Path.GetFileName(currentReferenceInitialLocation); string currentReferenceFinalLocation = Path.Combine(referencesPath, currentReferenceFileName); File.Copy(currentReferenceInitialLocation, currentReferenceFinalLocation, true); //set to normal for testing purposes-to allow the test project to delete the coppied file between test runs File.SetAttributes(currentReferenceFinalLocation, FileAttributes.Normal); string relativePath = Path.Combine(".", copiedReferencesSubfolder); relativePath = Path.Combine(relativePath, currentReferenceFileName); this.ProjectFileManager.AddReferenceProjectItem( assemblyReferenceIndex, Path.GetFileNameWithoutExtension(currentReferenceFinalLocation), relativePath); } } else { if (this.IsAspNetAssembly(reference)) { this.ProjectFileManager.AddPackageReferenceProjectItem(reference.Name, reference.Version.ToString()); } else { this.ProjectFileManager.AddReferenceProjectItem(assemblyReferenceIndex, reference.FullName); } } }
/*Telerik Authorship*/ internal TypeDefinition Resolve(TypeReference type, ICollection <string> visitedDlls) { if (type == null) { throw new ArgumentNullException("type"); } // TODO: The following code must be uncommented when bug 284860 (the one with the Resolver) is fixed. //if (type is ArrayType) //{ // type = type.Module.TypeSystem.LookupType("System", "Array"); //} //else //{ type = type.GetElementType(); //} var scope = type.Scope; if (scope == null) { return(null); } switch (scope.MetadataScopeType) { case MetadataScopeType.AssemblyNameReference: /*Telerik Authorship*/ TargetArchitecture architecture = type.Module.GetModuleArchitecture(); SpecialTypeAssembly special = type.Module.IsReferenceAssembly() ? SpecialTypeAssembly.Reference : SpecialTypeAssembly.None; var assembly = assembly_resolver.Resolve((AssemblyNameReference)scope, type.Module.ModuleDirectoryPath, architecture, special); if (assembly == null) { return(null); } /*Telerik Authorship*/ if (visitedDlls.Contains(assembly.MainModule.FilePath)) { return(null); } visitedDlls.Add(assembly.MainModule.FilePath); return(GetType(assembly.MainModule, type, visitedDlls)); case MetadataScopeType.ModuleDefinition: /*Telerik Authorship*/ ModuleDefinition theModule = (ModuleDefinition)scope; if (visitedDlls.Contains(theModule.FilePath)) { return(null); } visitedDlls.Add(theModule.FilePath); return(GetType(theModule, type, visitedDlls)); case MetadataScopeType.ModuleReference: var modules = type.Module.Assembly.Modules; var module_ref = (ModuleReference)scope; for (int i = 0; i < modules.Count; i++) { var netmodule = modules[i]; if (netmodule.Name == module_ref.Name) { /*Telerik Authorship*/ if (visitedDlls.Contains(netmodule.FilePath)) { return(null); } visitedDlls.Add(netmodule.FilePath); return(GetType(netmodule, type, visitedDlls)); } } break; } throw new NotSupportedException(); }
/*Telerik Authorship*/ private AssemblyDefinition GetFromResolvedAssemblies(AssemblyName assemblyName, SpecialTypeAssembly special) { foreach (string architectureString in GetReferencableArchitectures(assemblyName)) { List <AssemblyDefinition> assemblyList; AssemblyStrongNameExtended extendedStrongName = new AssemblyStrongNameExtended(assemblyName.FullName, (TargetArchitecture)Enum.Parse(typeof(TargetArchitecture), architectureString), special); if (TryGetResolvedAssembly(extendedStrongName, out assemblyList)) { return(assemblyList[0]); } } return(null); }
/*Telerik Authorship*/ private AssemblyDefinition Resolve(AssemblyNameReference name, string defaultPath, ReaderParameters parameters, TargetArchitecture architecture, SpecialTypeAssembly special, bool bubbleToUserIfFailed, bool addToFailedCache = true) { if (!string.IsNullOrEmpty(defaultPath)) { this.AddSearchDirectory(defaultPath); } if (name == null) { throw new ArgumentNullException("name"); } if (parameters == null) { parameters = new ReaderParameters(this); } /*Telerik Authorship*/ AssemblyStrongNameExtended assemblyKey = new AssemblyStrongNameExtended(name.FullName, architecture, special); AssemblyName assemblyName = new AssemblyName(name.Name, name.FullName, name.Version, name.PublicKey) { TargetArchitecture = architecture }; AssemblyDefinition assembly = null; /*Telerik Authorship*/ bool isResolved = DoWithReadLock(this.resolveLock, () => { if (assemblyPathResolver.IsFailedAssembly(assemblyKey)) { assembly = null; return(true); } assembly = GetFromResolvedAssemblies(assemblyName, special); return(assembly != null); }); if (isResolved) { return(assembly); } /*Telerik Authorship*/ return(DoWithWriteLock(this.resolveLock, () => { // Double checks to ensure that the entry is not added in the meantime. if (assemblyPathResolver.IsFailedAssembly(assemblyKey)) { return null; } assembly = GetFromResolvedAssemblies(assemblyName, special); if (assembly != null) { return assembly; } /*Telerik Authorship*/ // This code has been added by Mono.Cecil 0.9.6. It has been commented, because retargetable references should be further // researched and handled appropriately across the application. TP item N. 323383 //if (name.IsRetargetable) //{ // // if the reference is retargetable, zero it // name = new AssemblyNameReference(name.Name, new Version(0, 0, 0, 0)) // { // PublicKeyToken = Empty<byte>.Array, // }; //} assembly = SearchDirectory(name, parameters, architecture, defaultPath) ?? TryGetTargetAssembly(name, parameters, architecture, assemblyKey); if (assembly != null) { if (!filePathToAssemblyDefinitionCache.ContainsKey(assembly.MainModule.FilePath)) { AddToResolvedAssemblies(assembly); } return assembly; } assembly = GetTargetAssembly(name, parameters, architecture); if (assembly != null) { if (!filePathToAssemblyDefinitionCache.ContainsKey(assembly.MainModule.FilePath)) { AddToResolvedAssemblies(assembly); } return assembly; } if (bubbleToUserIfFailed) { return UserSpecifiedAssembly(name, architecture, assemblyKey); } else if (addToFailedCache) { assemblyPathResolver.AddToUnresolvedCache(assemblyKey); } return null; })); }
/*Telerik Authorship*/ public virtual AssemblyDefinition Resolve(AssemblyNameReference name, string path, TargetArchitecture architecture, SpecialTypeAssembly special, bool addToFailedCache, bool bubbleToUserIfFailed = true) { AssemblyDefinition assemblyDefinition = Resolve(name, path, new ReaderParameters(this), architecture, special, bubbleToUserIfFailed, addToFailedCache); return(assemblyDefinition); }
/*Telerik Authorship*/ public virtual AssemblyDefinition Resolve(AssemblyNameReference name, string path, TargetArchitecture architecture, SpecialTypeAssembly special, bool bubbleToUserIfFailed = true) { lock (Locker) { this.AddSearchDirectory(path); AssemblyDefinition assemblyDefinition = Resolve(name, path, new ReaderParameters(this), architecture, special, bubbleToUserIfFailed); return(assemblyDefinition); } }
/*Telerik Authorship*/ public virtual AssemblyDefinition Resolve(string fullName, ReaderParameters parameters, TargetArchitecture platform, SpecialTypeAssembly special, bool bubbleToUserIfFailed = true) { if (fullName == null) { throw new ArgumentNullException("fullName"); } return(Resolve(AssemblyNameReference.Parse(fullName), string.Empty, parameters, platform, special, bubbleToUserIfFailed)); }
public AssemblyStrongNameExtended(string strongName, TargetArchitecture architecture, SpecialTypeAssembly special) { this.StrongName = strongName; this.Architecture = architecture; this.Special = special; }