Example #1
0
        /// <inheritdoc />
        protected override AssemblyDefinition ResolveImpl(AssemblyDescriptor assembly)
        {
            string path = null;

            if (assembly.GetPublicKeyToken() != null)
            {
                path = ProbeGlobalAssemblyCache(assembly);
            }
            if (string.IsNullOrEmpty(path))
            {
                path = ProbeSearchDirectories(assembly);
            }

            AssemblyDefinition assemblyDef = null;

            try
            {
                assemblyDef = LoadAssemblyFromFile(path);
            }
            catch
            {
                // ignore any errors.
            }

            return(assemblyDef);
        }
Example #2
0
        /// <inheritdoc />
        protected override AssemblyDefinition ResolveImpl(AssemblyDescriptor assembly)
        {
            string path = null;

            var token = assembly.GetPublicKeyToken();

            if (token != null && !string.IsNullOrEmpty(_installationDirectory))
            {
                path = ProbeDirectory(assembly, _installationDirectory);
            }
            if (string.IsNullOrEmpty(path))
            {
                path = ProbeSearchDirectories(assembly);
            }

            AssemblyDefinition assemblyDef = null;

            try
            {
                assemblyDef = LoadAssemblyFromFile(path);
            }
            catch
            {
                // ignore any errors.
            }

            return(assemblyDef);
        }
        /// <summary>
        /// Probes a directory for the provided assembly.
        /// </summary>
        /// <param name="assembly">The assembly descriptor to search.</param>
        /// <param name="directory">The path to the directory to probe.</param>
        /// <returns>The path to the assembly, or <c>null</c> if none was found.</returns>
        protected static string ProbeDirectory(AssemblyDescriptor assembly, string directory)
        {
            string path = string.IsNullOrEmpty(assembly.Culture)
                ? Path.Combine(directory, assembly.Name)
                : Path.Combine(directory, assembly.Culture, assembly.Name);

            path = ProbeFileFromFilePathWithoutExtension(path)
                   ?? ProbeFileFromFilePathWithoutExtension(Path.Combine(path, assembly.Name));
            return(path);
        }
 /// <summary>
 /// Creates a new assembly reference, and copies over all properties of another assembly descriptor.
 /// </summary>
 /// <param name="descriptor">The assembly to base the reference on.</param>
 public AssemblyReference(AssemblyDescriptor descriptor)
     : this(new MetadataToken(TableIndex.AssemblyRef, 0))
 {
     Name             = descriptor.Name;
     Version          = descriptor.Version;
     Attributes       = descriptor.Attributes;
     HasPublicKey     = false;
     PublicKeyOrToken = descriptor.GetPublicKeyToken();
     Culture          = descriptor.Culture;
 }
Example #5
0
        /// <inheritdoc />
        protected override string ProbeRuntimeDirectories(AssemblyDescriptor assembly)
        {
            for (int i = 0; i < GacDirectories.Count; i++)
            {
                string path = GacDirectories[i].Probe(assembly);
                if (!string.IsNullOrEmpty(path))
                {
                    return(path);
                }
            }

            return(null);
        }
Example #6
0
        private string ProbeGlobalAssemblyCache(AssemblyDescriptor assembly)
        {
            foreach (var directory in GacDirectories)
            {
                string path = directory.Probe(assembly);
                if (path != null)
                {
                    return(path);
                }
            }

            return(null);
        }
        /// <inheritdoc />
        public void AddToCache(AssemblyDescriptor descriptor, AssemblyDefinition definition)
        {
            if (_cache.ContainsKey(descriptor))
            {
                throw new ArgumentException($"The cache already contains an entry of assembly {descriptor.FullName}.", nameof(descriptor));
            }

            if (!_signatureComparer.Equals(descriptor, definition))
            {
                throw new ArgumentException("Assembly descriptor and definition do not refer to the same assembly.");
            }

            _cache.Add(descriptor, definition);
        }
        /// <summary>
        /// Probes all search directories in <see cref="SearchDirectories"/> for the provided assembly.
        /// </summary>
        /// <param name="assembly">The assembly descriptor to search.</param>
        /// <returns>The path to the assembly, or <c>null</c> if none was found.</returns>
        protected string ProbeSearchDirectories(AssemblyDescriptor assembly)
        {
            foreach (string directory in SearchDirectories)
            {
                string path = ProbeDirectory(assembly, directory);

                if (!string.IsNullOrEmpty(path))
                {
                    return(path);
                }
            }

            return(null);
        }
        /// <inheritdoc />
        public AssemblyDefinition Resolve(AssemblyDescriptor assembly)
        {
            if (_cache.TryGetValue(assembly, out var assemblyDef))
            {
                return(assemblyDef);
            }

            assemblyDef = ResolveImpl(assembly);
            if (assemblyDef != null)
            {
                _cache.Add(assembly, assemblyDef);
            }

            return(assemblyDef);
        }
Example #10
0
        /// <summary>
        /// Probes the global assembly cache for an assembly.
        /// </summary>
        /// <param name="assembly">The assembly to lookup.</param>
        /// <returns>The path to the assembly, or <c>null</c> if none was found.</returns>
        public string Probe(AssemblyDescriptor assembly)
        {
            string fullPath = Path.Combine(_basePath, assembly.Name);

            if (Directory.Exists(fullPath))
            {
                string pubKeyTokenString = string.Join(string.Empty,
                                                       assembly.GetPublicKeyToken().Select(x => x.ToString("x2")));
                string directoryName = $"{assembly.Version}__{pubKeyTokenString}";
                if (IsPrefixed)
                {
                    directoryName = _prefix + directoryName;
                }

                string filePath = Path.Combine(fullPath, directoryName, assembly.Name);
                filePath = AssemblyResolverBase.ProbeFileFromFilePathWithoutExtension(filePath);
                if (!string.IsNullOrEmpty(filePath))
                {
                    return(filePath);
                }
            }

            return(null);
        }
 /// <summary>
 /// Resolves a new unseen reference to an assembly.
 /// </summary>
 /// <param name="assembly">The assembly to resolve.</param>
 /// <returns>The resolved assembly, or <c>null</c> if the resolution failed.</returns>
 /// <remarks>
 /// This method should not implement caching of resolved assemblies. The caller of this method already implements
 /// this.
 /// </remarks>
 protected abstract AssemblyDefinition ResolveImpl(AssemblyDescriptor assembly);
 /// <inheritdoc />
 public bool HasCached(AssemblyDescriptor descriptor) => _cache.ContainsKey(descriptor);
 /// <inheritdoc />
 public bool RemoveFromCache(AssemblyDescriptor descriptor) => _cache.Remove(descriptor);