Example #1
0
        //private readonly IAddinPathResolver resolver;

        //public Rewriter(IAddinPathResolver resolver)
        //{
        //	this.resolver = resolver;
        //}

        public void Rewrite(Options options)
        {
            var source    = options.Source.FullName;
            var pdbPath   = Path.ChangeExtension(source, ".pdb");
            var pdbExists = File.Exists(pdbPath);

            var readerParameters = new ReaderParameters
            {
                AssemblyResolver = new AssemblyReferenceResolver(source, options.References),
                InMemory         = true,
                ReadingMode      = ReadingMode.Immediate
            };

            var writerParameters = new Mono.Cecil.WriterParameters
            {
#if CAN_SIGN
                StrongNameKeyPair = options.SignAssembly && options.KeyFile != null
                                                                                        ? new StrongNameKeyPair(File.ReadAllBytes(options.KeyFile.FullName))
                                                                                        : null
#endif
            };

            ApplySymbolProviders(options, readerParameters, writerParameters);

            using (var module = Mono.Cecil.ModuleDefinition.ReadModule(source, readerParameters))
            {
                var instance = ResolveRewriterInstance(options.Rewriter.FullName);
                SetProperties(instance, options.Properties);

                instance.Execute(module);

                var target = (options.Target ?? options.Source).FullName;
                if (writerParameters.SymbolWriterProvider == null)
                {
                    try { File.Delete(Path.ChangeExtension(target, ".pdb")); }
                    catch { }
                }

                module.Assembly.AddAttr <AssemblyMetadataAttribute>(module, "Rewriter", DateTime.Now.ToString());

                module.Write(target, writerParameters);
                log.Info($"Saved module as {target}");
            }
        }
 public void Write(string fileName, WriterParameters parameters)
 {
     using (var stream = GetFileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) {
         Write(stream, parameters);
     }
 }
 public void Write(string fileName, WriterParameters parameters)
 {
     main_module.Write(fileName, parameters);
 }
 public void Write(Stream stream, WriterParameters parameters)
 {
     main_module.Write(stream, parameters);
 }
Example #5
0
 public void Write(WriterParameters parameters)
 {
     main_module.Write(parameters);
 }