Esempio n. 1
0
 public AssemblyProcessorContext(CustomAssemblyResolver assemblyResolver, AssemblyDefinition assembly, PlatformType platform, TextWriter log)
 {
     AssemblyResolver = assemblyResolver;
     Assembly         = assembly;
     Platform         = platform;
     Log = log;
 }
Esempio n. 2
0
        public CustomAssemblyResolver CreateAssemblyResolver()
        {
            var assemblyResolver = new CustomAssemblyResolver();

            assemblyResolver.RemoveSearchDirectory(".");
            foreach (string searchDirectory in SearchDirectories)
            {
                assemblyResolver.AddSearchDirectory(searchDirectory);
            }
            return(assemblyResolver);
        }
Esempio n. 3
0
        public bool Run(string inputFile, string outputFile = null)
        {
            if (inputFile == null)
            {
                throw new ArgumentNullException("inputFile");
            }
            if (outputFile == null)
            {
                outputFile = inputFile;
            }

            CustomAssemblyResolver assemblyResolver   = null;
            AssemblyDefinition     assemblyDefinition = null;

            try
            {
                try
                {
                    assemblyResolver = CreateAssemblyResolver();
                    var readWriteSymbols = UseSymbols;
                    // Double check that
                    var symbolFile = Path.ChangeExtension(inputFile, "pdb");
                    if (!File.Exists(symbolFile))
                    {
                        readWriteSymbols = false;
                    }

                    assemblyDefinition = AssemblyDefinition.ReadAssembly(inputFile, new ReaderParameters {
                        AssemblyResolver = assemblyResolver, ReadSymbols = readWriteSymbols, ReadWrite = true
                    });
                    bool modified;

                    // Check if pdb was actually read
                    readWriteSymbols = assemblyDefinition.MainModule.SymbolReader != null;

                    var symbolWriterProvider = assemblyDefinition.MainModule.SymbolReader?.GetWriterProvider();

                    var result = Run(ref assemblyDefinition, ref readWriteSymbols, out modified);
                    if (modified || inputFile != outputFile)
                    {
                        // Make sure output directory is created
                        var outputDirectory = Path.GetDirectoryName(outputFile);
                        if (!string.IsNullOrEmpty(outputDirectory) && !Directory.Exists(outputDirectory))
                        {
                            Directory.CreateDirectory(outputDirectory);
                        }

                        // Keep the original assembly by adding a .old prefix to the current extension
                        if (KeepOriginal)
                        {
                            var copiedFile = Path.ChangeExtension(inputFile, "old" + Path.GetExtension(inputFile));
                            File.Copy(inputFile, copiedFile, true);
                        }

                        if (assemblyDefinition.MainModule.FileName != outputFile)
                        {
                            // Note: using FileShare.Read otherwise often had access conflict (maybe with antivirus or window ssearch?)
                            assemblyDefinition.MainModule.Write(outputFile, new WriterParameters()
                            {
                                WriteSymbols = readWriteSymbols, SymbolWriterProvider = symbolWriterProvider
                            });
                        }
                        else
                        {
                            assemblyDefinition.MainModule.Write(new WriterParameters()
                            {
                                WriteSymbols = readWriteSymbols
                            });
                        }
                    }

                    return(result);
                }
                finally
                {
                    assemblyResolver?.Dispose();
                    assemblyDefinition?.Dispose();
                }
            }
            catch (Exception e)
            {
                OnErrorAction(null, e);
                if (DeleteOutputOnError)
                {
                    File.Delete(outputFile);
                }
                return(false);
            }
        }