public bool Rewrite(Configuration configuration, MethodDefinition method,
                            IRewriteTargetMatcher rewriteTargetMatcher)
        {
            bool rewritten = false;

            if (method.Body == null)
            {
                return(rewritten);
            }

            ILProcessor processor = method.Body.GetILProcessor();

            // Iterate copy of initial instructions without modifications
            foreach (var instruction in method.Body.Instructions.ToList())
            {
                MethodReference calledMethod;
                if (_instructionHelper.TryGetCall(instruction, out calledMethod))
                {
                    List <IRewriteTarget> targets = rewriteTargetMatcher.GetMatchingTargets(calledMethod).ToList();

                    if (targets.Count == 0)
                    {
                        continue;
                    }

                    RewriteContext context = new RewriteContext(configuration,
                                                                processor, instruction, targets, calledMethod);
                    RewriteInstruction(context);

                    rewritten = true;
                }
            }

            return(rewritten);
        }
        public bool Rewrite(Configuration configuration, MethodDefinition method,
            IRewriteTargetMatcher rewriteTargetMatcher)
        {
            bool rewritten = false;

            if (method.Body == null)
            {
                return rewritten;
            }

            ILProcessor processor = method.Body.GetILProcessor();

            // Iterate copy of initial instructions without modifications
            foreach (var instruction in method.Body.Instructions.ToList())
            {
                MethodReference calledMethod;
                if (_instructionHelper.TryGetCall(instruction, out calledMethod))
                {
                    List<IRewriteTarget> targets = rewriteTargetMatcher.GetMatchingTargets(calledMethod).ToList();

                    if (targets.Count == 0)
                    {
                        continue;
                    }

                    RewriteContext context = new RewriteContext(configuration,
                        processor, instruction, targets, calledMethod);
                    RewriteInstruction(context);

                    rewritten = true;
                }
            }

            return rewritten;
        }
Exemple #3
0
        public IAssemblyLoader Rewrite(string path)
        {
            bool hasSymbols = File.Exists(Path.ChangeExtension(path, ".pdb"));
            bool rewritten  = false;

            var readerParameters = new ReaderParameters
            {
                ReadSymbols          = hasSymbols,
                AssemblyResolver     = _assemblyResolver,
                SymbolReaderProvider = hasSymbols ? new FixedPdbReaderProvider() : null
            };

            using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(path, readerParameters))
            {
                foreach (var module in assembly.Modules)
                {
                    if (_moduleFilter.Accepts(module))
                    {
                        IRewriteTargetMatcher targetCollection = _rewriteTargetCollection.GetMatcher(module);

                        var types = module.GetTypes().ToList();
                        foreach (var type in types)
                        {
                            rewritten |= ProcessType(_configuration, type, targetCollection);
                        }
                    }
                }

                if (!rewritten)
                {
                    // No changes made: we can just load the original assembly.
                    return(new AssemblyLoader(path));
                }

                // Run a number of post-processing operations on the assembly: module mvid generation,
                // assembly attribute filtering, etc.
                foreach (var postProcessor in _postProcessors)
                {
                    postProcessor.Process(assembly);
                }

                string outputPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(path));

                WriterParameters writerParameters = new WriterParameters
                {
                    WriteSymbols = hasSymbols
                };

                assembly.Write(outputPath, writerParameters);

                _rewrittenAssemblies.Add(outputPath);

                return(new AssemblyLoader(outputPath));
            }
        }
        private bool ProcessType(Configuration configuration, TypeDefinition type,
                                 IRewriteTargetMatcher targetMatcher)
        {
            bool rewritten = false;

            foreach (var method in type.Methods)
            {
                rewritten |= _methodRewriter.Rewrite(configuration, method, targetMatcher);
            }

            return(rewritten);
        }
        public IAssemblyLoader Rewrite(string path)
        {
            bool hasSymbols = File.Exists(Path.ChangeExtension(path, ".pdb"));
            bool rewritten  = false;

            ReaderParameters readerParameters = new ReaderParameters {
                ReadSymbols = hasSymbols
            };
            AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(path, readerParameters);

            foreach (var module in assembly.Modules)
            {
                if (_moduleFilter.Accepts(module))
                {
                    IRewriteTargetMatcher targetCollection = _rewriteTargetCollection.GetMatcher(module);

                    var types = module.GetTypes().ToList();
                    foreach (var type in types)
                    {
                        rewritten |= ProcessType(_configuration, type, targetCollection);
                    }
                }
            }

            if (!rewritten)
            {
                // No changes made: we can just load the original assembly.
                return(new AssemblyLoader(path));
            }

            string outputPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(path));

            WriterParameters writerParameters = new WriterParameters
            {
                WriteSymbols = hasSymbols
            };

            foreach (var module in assembly.Modules)
            {
                module.Mvid = Guid.NewGuid();
            }

            assembly.Write(outputPath, writerParameters);

            _rewrittenAssemblies.Add(outputPath);

            return(new AssemblyLoader(outputPath));
        }
        private bool ProcessType(Configuration configuration, TypeDefinition type,
            IRewriteTargetMatcher targetMatcher)
        {
            bool rewritten = false;

            foreach (var method in type.Methods)
            {
                rewritten |= _methodRewriter.Rewrite(configuration, method, targetMatcher);
            }

            return rewritten;
        }