public void FullLoad()
 {
     if (this.HasImage && this.ReadingMode == ReadingMode.Deferred)
     {
         ImmediateModuleReader.ReadModule(this);
         this.ReadingMode = ReadingMode.Immediate;
     }
 }
 protected override void ReadModule()
 {
     this.module.Read <ModuleDefinition, ModuleDefinition>(this.module, (ModuleDefinition module, MetadataReader reader) => {
         base.ReadModuleManifest(reader);
         ImmediateModuleReader.ReadModule(module);
         return(module);
     });
 }
Example #3
0
        public static void WriteModuleTo(ModuleDefinition module, Stream stream, WriterParameters parameters)
        {
            AssemblyNameDefinition name;

            if ((int)(module.Attributes & ModuleAttributes.ILOnly) == 0)
            {
                throw new NotSupportedException("Writing mixed-mode assemblies is not supported");
            }
            if (module.HasImage && module.ReadingMode == ReadingMode.Deferred)
            {
                ImmediateModuleReader.ReadModule(module);
            }
            module.MetadataSystem.Clear();
            if (module.assembly != null)
            {
                name = module.assembly.Name;
            }
            else
            {
                name = null;
            }
            AssemblyNameDefinition publicKey           = name;
            string fullyQualifiedName                  = stream.GetFullyQualifiedName();
            ISymbolWriterProvider symbolWriterProvider = parameters.SymbolWriterProvider;

            if (symbolWriterProvider == null && parameters.WriteSymbols)
            {
                symbolWriterProvider = SymbolProvider.GetPlatformWriterProvider();
            }
            ISymbolWriter symbolWriter = ModuleWriter.GetSymbolWriter(module, fullyQualifiedName, symbolWriterProvider);

            if (parameters.StrongNameKeyPair != null && publicKey != null)
            {
                publicKey.PublicKey = parameters.StrongNameKeyPair.PublicKey;
                ModuleDefinition attributes = module;
                attributes.Attributes = attributes.Attributes | ModuleAttributes.StrongNameSigned;
            }
            MetadataBuilder metadataBuilder = new MetadataBuilder(module, fullyQualifiedName, symbolWriterProvider, symbolWriter);

            ModuleWriter.BuildMetadata(module, metadataBuilder);
            if (module.symbol_reader != null)
            {
                module.symbol_reader.Dispose();
            }
            ImageWriter imageWriter = ImageWriter.CreateWriter(module, metadataBuilder, stream);

            imageWriter.WriteImage();
            if (parameters.StrongNameKeyPair != null)
            {
                CryptoService.StrongName(stream, imageWriter, parameters.StrongNameKeyPair);
            }
            if (symbolWriter != null)
            {
                symbolWriter.Dispose();
            }
        }
Example #4
0
        public void ImmediateRead()
        {
            if (!HasImage)
            {
                return;
            }
            ReadingMode = ReadingMode.Immediate;
            var moduleReader = new ImmediateModuleReader(Image);

            moduleReader.ReadModule(this, resolve_attributes: true);
        }
Example #5
0
        public static void WriteModuleTo(ModuleDefinition module, Stream stream, WriterParameters parameters)
        {
            if ((module.Attributes & ModuleAttributes.ILOnly) == 0)
            {
                throw new NotSupportedException("Writing mixed-mode assemblies is not supported");
            }
            if (module.HasImage && (module.ReadingMode == ReadingMode.Deferred))
            {
                ImmediateModuleReader.ReadModule(module);
            }
            module.MetadataSystem.Clear();
            AssemblyNameDefinition name = module.assembly?.Name;
            string fullyQualifiedName   = stream.GetFullyQualifiedName();
            ISymbolWriterProvider symbolWriterProvider = parameters.SymbolWriterProvider;

            if ((symbolWriterProvider == null) && parameters.WriteSymbols)
            {
                symbolWriterProvider = SymbolProvider.GetPlatformWriterProvider();
            }
            ISymbolWriter writer = GetSymbolWriter(module, fullyQualifiedName, symbolWriterProvider);

            if ((parameters.StrongNameKeyPair != null) && (name != null))
            {
                name.PublicKey     = parameters.StrongNameKeyPair.PublicKey;
                module.Attributes |= ModuleAttributes.StrongNameSigned;
            }
            MetadataBuilder metadata = new MetadataBuilder(module, fullyQualifiedName, symbolWriterProvider, writer);

            BuildMetadata(module, metadata);
            if (module.symbol_reader != null)
            {
                module.symbol_reader.Dispose();
            }
            ImageWriter writer2 = ImageWriter.CreateWriter(module, metadata, stream);

            writer2.WriteImage();
            if (parameters.StrongNameKeyPair != null)
            {
                CryptoService.StrongName(stream, writer2, parameters.StrongNameKeyPair);
            }
            if (writer != null)
            {
                writer.Dispose();
            }
        }
Example #6
0
        private static void Write(ModuleDefinition module, Disposable <Stream> stream, WriterParameters parameters)
        {
            if ((module.Attributes & ModuleAttributes.ILOnly) == (ModuleAttributes)0)
            {
                throw new NotSupportedException("Writing mixed-mode assemblies is not supported");
            }
            if (module.HasImage && module.ReadingMode == ReadingMode.Deferred)
            {
                ImmediateModuleReader immediateModuleReader = new ImmediateModuleReader(module.Image);
                immediateModuleReader.ReadModule(module, false);
                immediateModuleReader.ReadSymbols(module);
            }
            module.MetadataSystem.Clear();
            if (module.symbol_reader != null)
            {
                module.symbol_reader.Dispose();
            }
            AssemblyNameDefinition assemblyNameDefinition = (module.assembly != null) ? module.assembly.Name : null;
            string fileName  = stream.value.GetFileName();
            uint   timestamp = parameters.Timestamp ?? module.timestamp;
            ISymbolWriterProvider symbolWriterProvider = parameters.SymbolWriterProvider;

            if (symbolWriterProvider == null && parameters.WriteSymbols)
            {
                symbolWriterProvider = new DefaultSymbolWriterProvider();
            }
            if (parameters.StrongNameKeyPair != null && assemblyNameDefinition != null)
            {
                assemblyNameDefinition.PublicKey = parameters.StrongNameKeyPair.PublicKey;
                module.Attributes |= ModuleAttributes.StrongNameSigned;
            }
            using (ISymbolWriter symbol_writer = GetSymbolWriter(module, fileName, symbolWriterProvider, parameters))
            {
                MetadataBuilder metadata = new MetadataBuilder(module, fileName, timestamp, symbolWriterProvider, symbol_writer);
                BuildMetadata(module, metadata);
                ImageWriter imageWriter = ImageWriter.CreateWriter(module, metadata, stream);
                stream.value.SetLength(0L);
                imageWriter.WriteImage();
                if (parameters.StrongNameKeyPair != null)
                {
                    CryptoService.StrongName(stream.value, imageWriter, parameters.StrongNameKeyPair);
                }
            }
        }