Exemplo n.º 1
0
 public string GetFilePath(IAssemblyDescriptor descriptor)
 {
     return(Path.Combine(DirectoryPath,
                         descriptor.Name,
                         FolderPrefix + descriptor.Version + "__" + descriptor.PublicKeyToken.ToHexString(),
                         descriptor.Name + ".dll"));
 }
Exemplo n.º 2
0
 public static void ValidateAssembly(IAssemblyDescriptor originalDescriptor, IAssemblyDescriptor descriptor)
 {
     Assert.Equal(originalDescriptor.Name, descriptor.Name);
     Assert.Equal(originalDescriptor.Culture, descriptor.Culture);
     Assert.Equal(originalDescriptor.Version, descriptor.Version);
     Assert.Equal(originalDescriptor.PublicKeyToken, descriptor.PublicKeyToken);
 }
Exemplo n.º 3
0
        public AssemblyDefinition ResolveAssembly(IAssemblyDescriptor descriptor)
        {
            if (_cachedAssemblies.TryGetValue(descriptor, out var definition))
            {
                return(definition);
            }

            var path = GetFilePath(descriptor);

            if (!string.IsNullOrEmpty(path))
            {
                var assembly = ReadAssembly(path);
                if (assembly.NetDirectory?.MetadataHeader?.Image != null)
                {
                    definition = assembly.NetDirectory.MetadataHeader.Image.Assembly;
                    _cachedAssemblies.Add(descriptor, definition);
                    return(definition);
                }
            }

            definition = OnAssemblyResolutionFailed(new AssemblyResolutionEventArgs(descriptor));
            if (definition == null)
            {
                if (ThrowOnNotFound)
                {
                    throw new AssemblyResolutionException(descriptor);
                }
            }
            else
            {
                _cachedAssemblies.Add(descriptor, definition);
            }

            return(definition);
        }
Exemplo n.º 4
0
 public static void ValidateAssembly(IAssemblyDescriptor originalDescriptor, IAssemblyDescriptor descriptor)
 {
     Assert.AreEqual(originalDescriptor.Name, descriptor.Name);
     Assert.AreEqual(originalDescriptor.Culture, descriptor.Culture);
     Assert.AreEqual(originalDescriptor.Version, descriptor.Version);
     ValidateByteArrays(originalDescriptor.PublicKeyToken, descriptor.PublicKeyToken);
 }
Exemplo n.º 5
0
 public static void ImportAssemblyInfo(this AssemblyDefinition assembly, IAssemblyDescriptor info)
 {
     assembly.Name      = info.Name;
     assembly.Version   = info.Version;
     assembly.Culture   = info.Culture;
     assembly.PublicKey = info.PublicKeyToken != null ? new DataBlobSignature(info.PublicKeyToken) : null;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the file path to the assembly file that was described by the given assembly descriptor.
        /// </summary>
        /// <param name="descriptor">The assembly to resolve.</param>
        /// <returns>The path to the assembly file.</returns>
        protected virtual string GetFilePath(IAssemblyDescriptor descriptor)
        {
            if (descriptor.PublicKeyToken != null)
            {
                foreach (var gacDirectory in GacDirectories)
                {
                    var filePath = gacDirectory.GetFilePath(descriptor);
                    if (File.Exists(filePath))
                    {
                        return(filePath);
                    }
                }
            }

            foreach (var directory in SearchDirectories)
            {
                var path = Path.Combine(directory, descriptor.Name);
                if (File.Exists(path + ".dll"))
                {
                    return(path + ".dll");
                }
                if (File.Exists(path + ".dll"))
                {
                    return(path + ".dll");
                }
            }

            return(null);
        }
 public AssemblyResolutionEventArgs(IAssemblyDescriptor requestedAssembly, Exception exception)
 {
     if (requestedAssembly == null)
         throw new ArgumentNullException("requestedAssembly");
     RequestedAssembly = requestedAssembly;
     Exception = exception;
 }
Exemplo n.º 8
0
 public AssemblyReference ImportAssembly(IAssemblyDescriptor assemblyInfo)
 {
     var table = _tableStreamBuffer.GetTable<AssemblyReference>();
     var reference = table.FirstOrDefault(x => _signatureComparer.MatchAssemblies(x, assemblyInfo));
     if (reference == null)
         table.Add(reference = new AssemblyReference(assemblyInfo));
     return reference;
 }
Exemplo n.º 9
0
 public AssemblyResolutionEventArgs(IAssemblyDescriptor requestedAssembly)
 {
     if (requestedAssembly == null)
     {
         throw new ArgumentNullException("requestedAssembly");
     }
     RequestedAssembly = requestedAssembly;
 }
Exemplo n.º 10
0
 public AssemblyDefinition(IAssemblyDescriptor info)
     : base(
         null, new MetadataToken(MetadataTokenType.Assembly),
         new MetadataRow <uint, ushort, ushort, ushort, ushort, uint, uint, uint, uint>())
 {
     _name      = new LazyValue <string>(info.Name);
     Version    = info.Version;
     _culture   = new LazyValue <string>(info.Culture);
     _publicKey = new LazyValue <DataBlobSignature>(info.PublicKeyToken == null ? null : new DataBlobSignature(info.PublicKeyToken));
 }
Exemplo n.º 11
0
        public AssemblyReference ImportAssembly(IAssemblyDescriptor assemblyInfo)
        {
            var table     = _tableStreamBuffer.GetTable <AssemblyReference>();
            var reference = table.FirstOrDefault(x => _signatureComparer.MatchAssemblies(x, assemblyInfo));

            if (reference == null)
            {
                table.Add(reference = new AssemblyReference(assemblyInfo));
            }
            return(reference);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Determines whether two assembly descriptors are considered equal according to their signature.
        /// </summary>
        /// <param name="info1">The first assembly to compare.</param>
        /// <param name="info2">The second assembly to compare.</param>
        /// <returns><c>True</c> if the assemblies are considered equal, <c>False</c> otherwise.</returns>
        public bool MatchAssemblies(IAssemblyDescriptor info1, IAssemblyDescriptor info2)
        {
            if (info1 == null && info2 == null)
                return true;
            if (info1 == null || info2 == null)
                return false;

            return info1.Name == info2.Name &&
                   info1.Version == info2.Version &&
                   info1.Culture == info2.Culture &&
                   ByteArrayMatches(info1.PublicKeyToken, info2.PublicKeyToken);
        }
Exemplo n.º 13
0
        /// <inheritdoc />
        public virtual AssemblyReference ImportAssembly(IAssemblyDescriptor assemblyInfo)
        {
            var reference = TargetImage.Assembly.AssemblyReferences.FirstOrDefault(x =>
                                                                                   _signatureComparer.Equals(x, assemblyInfo));

            if (reference == null)
            {
                reference = new AssemblyReference(assemblyInfo);
                TargetImage.Assembly.AssemblyReferences.Add(reference);
            }
            return(reference);
        }
Exemplo n.º 14
0
        public AssemblyReference(IAssemblyDescriptor info)
            : base(new MetadataToken(MetadataTokenType.AssemblyRef))
        {
            _name      = new LazyValue <string>(info.Name);
            Version    = info.Version;
            _culture   = new LazyValue <string>(info.Culture);
            _publicKey = new LazyValue <DataBlobSignature>(info.PublicKeyToken == null ? null : new DataBlobSignature(info.PublicKeyToken));
            _hashValue = new LazyValue <DataBlobSignature>();

            CustomAttributes = new CustomAttributeCollection(this);
            OperatingSystems = new DelegatedMemberCollection <AssemblyReference, AssemblyRefOs>(this, GetOsOwner, SetOsOwner);
            Processors       = new DelegatedMemberCollection <AssemblyReference, AssemblyRefProcessor>(this, GetProcessorOwner, SetProcessorOwner);
        }
Exemplo n.º 15
0
        internal static string GetFullName(this IAssemblyDescriptor info)
        {
            var builder = new StringBuilder();

            builder.Append(info.Name);
            builder.Append(", Version=");
            builder.Append(info.Version);
            builder.Append(", Culture=");
            builder.Append(string.IsNullOrEmpty(info.Culture) ? "neutral" : info.Culture);
            builder.Append(", PublicKeyToken=");
            builder.Append(info.PublicKeyToken != null
                ? info.PublicKeyToken.ToHexString()
                : "null");
            return(builder.ToString());
        }
Exemplo n.º 16
0
 public AssemblyDefinition(IAssemblyDescriptor info)
     : base(new MetadataToken(MetadataTokenType.Assembly))
 {
     _name                = new LazyValue <string>(info.Name);
     Version              = info.Version;
     _culture             = new LazyValue <string>(info.Culture);
     _publicKey           = new LazyValue <DataBlobSignature>(info.PublicKeyToken == null ? null : new DataBlobSignature(info.PublicKeyToken));
     Modules              = new DelegatedMemberCollection <AssemblyDefinition, ModuleDefinition>(this, GetModuleOwner, SetModuleOwner);
     AssemblyReferences   = new Collection <AssemblyReference>();
     ModuleReferences     = new Collection <ModuleReference>();
     SecurityDeclarations = new SecurityDeclarationCollection(this);
     Resources            = new Collection <ManifestResource>();
     Files                = new Collection <FileReference>();
     OperatingSystems     = new DelegatedMemberCollection <AssemblyDefinition, AssemblyOs>(this, GetOsOwner, SetOsOwner);
     Processors           = new DelegatedMemberCollection <AssemblyDefinition, AssemblyProcessor>(this, GetProcessorOwner, SetProcessorOwner);
 }
Exemplo n.º 17
0
        /// <summary>
        /// Determines whether two assembly descriptors are considered equal according to their signature.
        /// </summary>
        /// <param name="info1">The first assembly to compare.</param>
        /// <param name="info2">The second assembly to compare.</param>
        /// <returns><c>True</c> if the assemblies are considered equal, <c>False</c> otherwise.</returns>
        public bool MatchAssemblies(IAssemblyDescriptor info1, IAssemblyDescriptor info2)
        {
            if (info1 == null && info2 == null)
            {
                return(true);
            }
            if (info1 == null || info2 == null)
            {
                return(false);
            }

            return(info1.Name == info2.Name &&
                   info1.Version == info2.Version &&
                   info1.Culture == info2.Culture &&
                   ByteArrayMatches(info1.PublicKeyToken, info2.PublicKeyToken));
        }
Exemplo n.º 18
0
        public AssemblyDefinition ResolveAssembly(IAssemblyDescriptor descriptor)
        {
            AssemblyDefinition definition;

            if (_cachedAssemblies.TryGetValue(descriptor, out definition))
            {
                return(definition);
            }

            var path = GetFilePath(descriptor);

            if (!string.IsNullOrEmpty(path))
            {
                var assembly = ReadAssembly(path);
                if (assembly.NetDirectory != null && assembly.NetDirectory.MetadataHeader != null)
                {
                    var tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>();
                    if (tableStream != null)
                    {
                        var assemblyTable = tableStream.GetTable <AssemblyDefinition>();
                        if (assemblyTable.Count > 0)
                        {
                            definition = assemblyTable[0];
                            _cachedAssemblies.Add(descriptor, definition);
                            return(definition);
                        }
                    }
                }
            }

            definition = OnAssemblyResolutionFailed(new AssemblyResolutionEventArgs(descriptor));
            if (definition == null)
            {
                if (ThrowOnNotFound)
                {
                    throw new AssemblyResolutionException(descriptor);
                }
            }
            else
            {
                _cachedAssemblies.Add(descriptor, definition);
            }

            return(definition);
        }
Exemplo n.º 19
0
        public AssemblyDefinition ResolveAssembly(IAssemblyDescriptor descriptor)
        {
            AssemblyDefinition definition;
            if (_cachedAssemblies.TryGetValue(descriptor, out definition))
                return definition;

            var path = GetFilePath(descriptor);
            if (!string.IsNullOrEmpty(path))
            {
                var assembly = ReadAssembly(path);
                if (assembly.NetDirectory != null && assembly.NetDirectory.MetadataHeader != null)
                {
                    var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
                    if (tableStream != null)
                    {

                        var assemblyTable = tableStream.GetTable<AssemblyDefinition>();
                        if (assemblyTable.Count > 0)
                        {

                            definition = assemblyTable[0];
                            _cachedAssemblies.Add(descriptor, definition);
                            return definition;
                        }
                    }
                }
            }

            definition = OnAssemblyResolutionFailed(new AssemblyResolutionEventArgs(descriptor));
            if (definition == null)
            {
                if (ThrowOnNotFound)
                    throw new AssemblyResolutionException(descriptor);
            }
            else
            {
                _cachedAssemblies.Add(descriptor, definition);
            }

            return definition;
        }
Exemplo n.º 20
0
 public AssemblyResolutionException(IAssemblyDescriptor info)
     : this(string.Format("Assembly {0} could not be resolved.", info.GetFullName()))
 {
 }
Exemplo n.º 21
0
 public string GetFilePath(IAssemblyDescriptor descriptor)
 {
     return Path.Combine(DirectoryPath,
         descriptor.Name,
         FolderPrefix + descriptor.Version + "__" + descriptor.PublicKeyToken.ToHexString(),
         descriptor.Name + ".dll");
 }
 public AssemblyResolutionException(IAssemblyDescriptor info)
     : this(string.Format("Assembly {0} could not be resolved.", info.GetFullName()))
 {
 }
Exemplo n.º 23
0
        /// <summary>
        /// Gets the file path to the assembly file that was described by the given assembly descriptor.
        /// </summary>
        /// <param name="descriptor">The assembly to resolve.</param>
        /// <returns>The path to the assembly file.</returns>
        protected virtual string GetFilePath(IAssemblyDescriptor descriptor)
        {
            if (descriptor.PublicKeyToken != null)
            {
                foreach (var gacDirectory in GacDirectories)
                {
                    var filePath = gacDirectory.GetFilePath(descriptor);
                    if (File.Exists(filePath))
                        return filePath;
                }
            }

            foreach (var directory in SearchDirectories)
            {
                var path = Path.Combine(directory, descriptor.Name);
                if (File.Exists(path + ".dll"))
                    return path + ".dll";
                if (File.Exists(path + ".dll"))
                    return path + ".dll";
            }

            return null;
        }