コード例 #1
0
        private void RewriteAssembly(AssemblyToRewrite assemblyToRewrite, List <AssemblyToRewrite> assembliesToRewrite, ReaderParameters readerParameters)
        {
            if (assemblyToRewrite.Rewritten)
            {
                return;
            }

            using (var assembly = AssemblyDefinition.ReadAssembly(assemblyToRewrite.InputPath, readerParameters))
            {
                var currentName = assembly.Name.Name;
                var newName     = assemblyToRewrite.OutputName;

                Write(currentName, nameof(AssemblyDefinition), $"rewriting {currentName} from {assemblyToRewrite.InputPath}");

                foreach (var moduleDefinition in assembly.Modules)
                {
                    foreach (var assemblyReference in moduleDefinition.AssemblyReferences)
                    {
                        Write(currentName, nameof(AssemblyDefinition),
                              $"{assembly.Name} references {assemblyReference.Name}");

                        var assemblyReferenceToRewrite =
                            assembliesToRewrite.FirstOrDefault(a => a.InputName == assemblyReference.Name);

                        if (assemblyReferenceToRewrite != null)
                        {
                            if (!assemblyReferenceToRewrite.Rewritten)
                            {
                                Write(currentName, nameof(AssemblyNameReference),
                                      $"{assemblyReference.Name} will be rewritten first");
                                RewriteAssembly(assemblyReferenceToRewrite, assembliesToRewrite, readerParameters);
                            }
                            else
                            {
                                Write(currentName, nameof(AssemblyNameReference),
                                      $"{assemblyReference.Name} already rewritten");
                            }

                            foreach (var innerModuleDefinition in assembly.Modules)
                            {
                                RewriteTypeReferences(currentName, innerModuleDefinition.GetTypeReferences(),
                                                      assemblyReferenceToRewrite.InputName,
                                                      assemblyReferenceToRewrite.OutputName);

                                RewriteTypes(currentName, innerModuleDefinition.Types,
                                             assemblyReferenceToRewrite.InputName,
                                             assemblyReferenceToRewrite.OutputName);
                            }

                            assemblyReference.Name = assemblyReferenceToRewrite.OutputName;
                        }
                    }

                    RewriteTypes(currentName, moduleDefinition.Types, currentName, newName);
                    moduleDefinition.Name = moduleDefinition.Name.Replace(currentName, newName);
                }

                RewriteAssemblyTitleAttribute(assembly, currentName, newName);
                assembly.Name.Name = newName;
                assembly.Write(assemblyToRewrite.OutputPath);
                assemblyToRewrite.Rewritten = true;

                Write(currentName, nameof(AssemblyDefinition),
                      $"finished rewriting {currentName} into {assemblyToRewrite.OutputPath}");
            }
        }
コード例 #2
0
        private void RewriteAssembly(AssemblyToRewrite assemblyToRewrite, List <AssemblyToRewrite> assembliesToRewrite,
                                     ReaderParameters readerParameters)
        {
            if (assemblyToRewrite.Rewritten)
            {
                return;
            }

            string tempOutputPath = null;
            string currentName;

            using (var assembly = AssemblyDefinition.ReadAssembly(assemblyToRewrite.InputPath, readerParameters))
            {
                currentName = assembly.Name.Name;
                var newName = assemblyToRewrite.OutputName;

                Write(currentName, nameof(AssemblyDefinition),
                      $"rewriting {currentName} from {assemblyToRewrite.InputPath}");

                foreach (var moduleDefinition in assembly.Modules)
                {
                    foreach (var assemblyReference in moduleDefinition.AssemblyReferences)
                    {
                        Write(currentName, nameof(AssemblyDefinition),
                              $"{assembly.Name} references {assemblyReference.Name}");

                        var assemblyReferenceToRewrite =
                            assembliesToRewrite.FirstOrDefault(a => a.InputName == assemblyReference.Name);

                        if (assemblyReferenceToRewrite != null)
                        {
                            if (!assemblyReferenceToRewrite.Rewritten)
                            {
                                Write(currentName, nameof(AssemblyNameReference),
                                      $"{assemblyReference.Name} will be rewritten first");
                                RewriteAssembly(assemblyReferenceToRewrite, assembliesToRewrite, readerParameters);
                            }
                            else
                            {
                                Write(currentName, nameof(AssemblyNameReference),
                                      $"{assemblyReference.Name} already rewritten");
                            }

                            foreach (var innerModuleDefinition in assembly.Modules)
                            {
                                RewriteTypeReferences(currentName, innerModuleDefinition.GetTypeReferences());
                                RewriteTypes(currentName, innerModuleDefinition.Types);
                            }

                            assemblyReference.Name = assemblyReferenceToRewrite.OutputName;
                        }
                    }

                    RewriteTypes(currentName, moduleDefinition.Types);
                    moduleDefinition.Name = RenameTypeName(moduleDefinition.Name);
                }

                RewriteAssemblyTitleAttribute(assembly, currentName, newName);
                assembly.Name.Name = newName;
                var writerParameters = new WriterParameters();

                if (!string.IsNullOrEmpty(_keyFile) && File.Exists(_keyFile))
                {
                    Write(currentName, nameof(AssemblyDefinition),
                          $"signing {newName} with keyfile {_keyFile}");
                    var fileBytes = File.ReadAllBytes(_keyFile);
                    writerParameters.StrongNameKeyBlob = fileBytes;
                    assembly.Name.Attributes          |= AssemblyAttributes.PublicKey;
                    assembly.MainModule.Attributes    |= ModuleAttributes.StrongNameSigned;
                }

                if (assemblyToRewrite.OutputPath == assemblyToRewrite.InputPath)
                {
                    tempOutputPath = assemblyToRewrite.OutputPath + ".temp";
                    assembly.Write(tempOutputPath, writerParameters);
                    assemblyToRewrite.Rewritten = true;
                    Write(currentName, nameof(AssemblyDefinition),
                          $"finished rewriting {currentName} into {tempOutputPath}");
                }
                else
                {
                    assembly.Write(assemblyToRewrite.OutputPath, writerParameters);
                    assemblyToRewrite.Rewritten = true;
                    Write(currentName, nameof(AssemblyDefinition),
                          $"finished rewriting {currentName} into {assemblyToRewrite.OutputPath}");
                }
            }

            if (!string.IsNullOrWhiteSpace(tempOutputPath))
            {
                File.Delete(assemblyToRewrite.OutputPath);
                File.Move(tempOutputPath, assemblyToRewrite.OutputPath);
                Write(currentName, nameof(AssemblyDefinition),
                      $"Rename {tempOutputPath} back to {assemblyToRewrite.OutputPath}");
            }
        }