示例#1
0
        public bool TrimBinaries(string sourceDir, string outputDir)
        {
            bool fSuccess = true;

            foreach (TrimAssembly trimAssembly in _includeSet.GetAllAssemblies())
            {
                _currentTrimAssembly = trimAssembly;

                try
                {
                    string sourceFile = Path.Combine(sourceDir, trimAssembly.Name + ".dll");
                    string outputFile = Path.Combine(outputDir, trimAssembly.Name + ".dll");

                    Console.WriteLine("loading assembly '" + sourceFile + "'");
                    IModule module = host.LoadUnitFrom(sourceFile) as IModule;

                    if (module == null || module == Dummy.Module || module == Dummy.Assembly)
                    {
                        throw new Exception(sourceFile + " is not a PE file containing a CLR module or assembly, or an error occurred when loading it.");
                    }

                    // Working around bug 
                    DummyTraverser dummyTraverser = new DummyTraverser();

                    PdbReader pdbReader = null;
                    PdbWriter pdbWriter = null;
                    string pdbSourceFile = Path.ChangeExtension(sourceFile, "pdb");
                    string pdbOutputFile = Path.ChangeExtension(outputFile, "pdb");
                    if (File.Exists(pdbSourceFile))
                    {
                        Stream pdbStream = File.OpenRead(pdbSourceFile);
                        pdbReader = new PdbReader(pdbStream, host);
                        pdbWriter = new PdbWriter(Path.GetFullPath(pdbOutputFile), pdbReader);
                    }

                    IAssembly/*?*/ assembly = module as IAssembly;
                    if (assembly != null)
                    {
                        dummyTraverser.Visit(assembly);
                        module = this.Rewrite(assembly);
                    }
                    else
                    {
                        dummyTraverser.Visit(module);
                        module = this.Rewrite(module);
                    }
                    PeWriter.WritePeToStream(module, host, File.Create(outputFile), pdbReader, pdbReader, pdbWriter);
                }
                catch (Exception e)
                {
                    Console.WriteLine(trimAssembly.Key + ": " + e.Message);
                    throw;
                }
            }

            if (!fSuccess)
                Console.Error.WriteLine(String.Format("At least one of the assemblies could not be processed!"));

            return fSuccess;
        }
示例#2
0
        public bool TrimBinaries(string sourceDir, string outputDir)
        {
            bool fSuccess = true;

            foreach (TrimAssembly trimAssembly in _includeSet.GetAllAssemblies())
            {
                _currentTrimAssembly = trimAssembly;

                try
                {
                    string sourceFile = Path.Combine(sourceDir, trimAssembly.Name + ".dll");
                    string outputFile = Path.Combine(outputDir, trimAssembly.Name + ".dll");

                    Console.WriteLine("loading assembly '" + sourceFile + "'");
                    IModule module = host.LoadUnitFrom(sourceFile) as IModule;

                    if (module == null || module == Dummy.Module || module == Dummy.Assembly)
                    {
                        throw new Exception(sourceFile + " is not a PE file containing a CLR module or assembly, or an error occurred when loading it.");
                    }

                    // Working around bug
                    DummyTraverser dummyTraverser = new DummyTraverser();

                    PdbReader pdbReader     = null;
                    PdbWriter pdbWriter     = null;
                    string    pdbSourceFile = Path.ChangeExtension(sourceFile, "pdb");
                    string    pdbOutputFile = Path.ChangeExtension(outputFile, "pdb");
                    if (File.Exists(pdbSourceFile))
                    {
                        Stream pdbStream = File.OpenRead(pdbSourceFile);
                        pdbReader = new PdbReader(pdbStream, host);
                        pdbWriter = new PdbWriter(Path.GetFullPath(pdbOutputFile), pdbReader);
                    }

                    IAssembly /*?*/ assembly = module as IAssembly;
                    if (assembly != null)
                    {
                        dummyTraverser.Visit(assembly);
                        module = this.Rewrite(assembly);
                    }
                    else
                    {
                        dummyTraverser.Visit(module);
                        module = this.Rewrite(module);
                    }
                    PeWriter.WritePeToStream(module, host, File.Create(outputFile), pdbReader, pdbReader, pdbWriter);
                }
                catch (Exception e)
                {
                    Console.WriteLine(trimAssembly.Key + ": " + e.Message);
                    throw;
                }
            }

            if (!fSuccess)
            {
                Console.Error.WriteLine(String.Format("At least one of the assemblies could not be processed!"));
            }

            return(fSuccess);
        }