Exemplo n.º 1
0
        public bool PatchAssembly()
        {
            try
            {
                MessageIntegration.Info("CSCli is searching for calls to replace. Assemblyname: " + _assembly.FullName);

                var module = _assembly.MainModule;
                foreach (var type in module.Types)
                {
                    ProcessType(type);
                }

                MessageIntegration.Info(String.Format("CSCli processed {0} types.", module.Types.Count));
                MessageIntegration.Info(String.Format("CSCli replaced {0} method-calls by Calli-instruction.", _replacedCallsCount));
                MessageIntegration.Info(String.Format("Cleaning up assembly. {0} types to remove from assembly.", _typesToRemove.Count));

                foreach (var typeToRemove in _typesToRemove)
                {
                    module.Types.Remove(typeToRemove);
                }
            }
            catch (Exception ex)
            {
                MessageIntegration.WriteError("Unknown error: " + ex.ToString());
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        private void AddTypeToRemove(TypeDefinition type)
        {
            var attributes = GetAttributes(type);

            if (!attributes.Contains(_removeTypeAttributeName))
            {
                string error = String.Format("Can't remove type \"{0}\", because it is not marked with \"{1}\" attribute.",
                                             type.FullName, _removeTypeAttributeName);
                MessageIntegration.WriteError(error);
                throw new Exception(error);
            }

            if (!_typesToRemove.Contains(type))
            {
                _typesToRemove.Add(type);
            }

            foreach (var nestedType in type.NestedTypes)
            {
                AddTypeToRemove(nestedType);
            }
        }
Exemplo n.º 3
0
        private static void Main(string[] args)
        {
            string calliAttributeName      = "CalliAttribute";
            string removeTypeAttributeName = "RemoveTypeAttribute";
            string filename = null;
            string pdbfile  = null;

            MessageIntegration.Info("CSCli started.");
            MessageIntegration.Info("CSCli was copied from http://www.codeproject.com/Articles/644130/NET-COM-Interop-using-Postbuild.");
            MessageIntegration.Info(String.Empty); //new line

            MessageIntegration.Info(String.Format("CSCli-Argments: [{0}].", String.Concat(args.Select(x => x + " ")).Trim()));

            /*
             * Parse parameters
             */
            foreach (var a in args)
            {
                if (a.StartsWith("-file:"))
                {
                    filename = a.Substring("-file:".Length);
                    MessageIntegration.Info("Filename: " + filename);
                }
                else if (a.StartsWith("-c:"))
                {
                    calliAttributeName = a.Substring("-c:".Length);
                }
                else if (a.StartsWith("-r:"))
                {
                    removeTypeAttributeName = a.Substring("-r:".Length);
                }
                else if (a.StartsWith("-pdb:"))
                {
                    pdbfile = a.Substring("-pdb:".Length);
                }
                else
                {
                    MessageIntegration.WriteWarning(String.Format("Unknown parameter: \"{0}\".", a));
                }
            }

            /*
             * Load and process assembly
             */
            if (!File.Exists(filename))
            {
                MessageIntegration.WriteError(String.Format("Could not find file \"{0}\".", filename));
                Environment.Exit(-1);
            }

            WriterParameters wp = new WriterParameters();
            ReaderParameters rp = new ReaderParameters();

            var strongNameKey = Path.ChangeExtension(filename, "snk");

            if (File.Exists(strongNameKey))
            {
                MessageIntegration.Info("Signing with Key : " + strongNameKey);
                wp.StrongNameKeyPair = new StrongNameKeyPair(File.OpenRead(strongNameKey));
            }

            //check whether the pdbfile has been passed through application parameters
            if (pdbfile == null)
            {
                //if not use the default pdbfilepath by changing the extension of the assembly to .pdb
                pdbfile = Path.ChangeExtension(filename, "pdb");
            }

            //check whether the original pdb-file exists
            bool generatePdb = File.Exists(pdbfile);

            //if the original pdb-file exists -> prepare for rewriting the symbols file
            wp.WriteSymbols = generatePdb;
            rp.ReadSymbols  = generatePdb;

            if (rp.ReadSymbols)
            {
                rp.SymbolReaderProvider = new PdbReaderProvider();
            }

            MessageIntegration.Info("Generating pdb: " + generatePdb.ToString());

            //open assembly
            var assembly = AssemblyDefinition.ReadAssembly(filename, rp);

            //add the directory assembly directory as search directory to resolve referenced assemblies
            ((BaseAssemblyResolver)assembly.MainModule.AssemblyResolver).AddSearchDirectory(Path.GetDirectoryName(filename));

            //path the assembly
            AssemblyPatcher patcher = new AssemblyPatcher(assembly, calliAttributeName, removeTypeAttributeName);

            if (patcher.PatchAssembly())
            {
                try
                {
                    //if the assembly was patched successfully -> replace the old assembly file with the new, patched assembly file
                    //the symbols file will be created automatically
                    File.Delete(filename);
                    assembly.Write(filename, wp);
                }
                catch (Exception ex)
                {
                    MessageIntegration.WriteError("Creating new assembly failed: " + ex.ToString());
                    Environment.Exit(-1);
                }

                MessageIntegration.Info(String.Format("CSCli patched assembly \"{0}\" successfully.", Path.GetFileName(filename)));
                Environment.Exit(0);
            }
            else
            {
                MessageIntegration.WriteError(String.Format("\"{0}\" could not be patched.", filename));
                Environment.Exit(-1);
            }
        }