Example #1
0
        private static bool RemoveInvalidFriendAssemblyReferences(string assemblyPath, string keyFile, string keyFilePassword, params string[] probingPaths)
        {
            try
            {
                PrintMessage(null, LogLevel.Verbose);
                PrintMessage(string.Format("Removing invalid friend references from '{0}'...", assemblyPath), LogLevel.Verbose);

                if (SigningHelper.RemoveInvalidFriendAssemblies(assemblyPath, keyFile, keyFilePassword, probingPaths))
                {
                    PrintMessageColor(string.Format("Invalid friend assemblies removed successfully from '{0}'.", assemblyPath), LogLevel.Changes, ConsoleColor.Green);

                    return(true);
                }
                else
                {
                    PrintMessage("No friend references to fix...", LogLevel.Verbose);
                }
            }
            catch (BadImageFormatException bife)
            {
                PrintMessageColor(string.Format("Warning: {0}", bife.Message), LogLevel.Silent, ConsoleColor.Yellow);
            }
            catch (Exception ex)
            {
                PrintMessageColor(string.Format("Error: {0}", ex.Message), LogLevel.Silent, ConsoleColor.Red);
            }

            return(false);
        }
Example #2
0
        private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = string.Empty;

            int progress               = 0;
            int signedFiles            = 0;
            int referenceFixes         = 0;
            var processedAssemblyPaths = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            var signedAssemblyPaths    = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            if (e.Argument is IEnumerable <string> assemblyPaths)
            {
                var probingPaths = assemblyPaths.Select(f => Path.GetDirectoryName(f)).Distinct().ToArray();

                // We go through assemblies three times and every assembly -1 for reference fixes.
                double progressMax = (assemblyPaths.Count() + (assemblyPaths.Count() * (assemblyPaths.Count() - 1))) * 3;

                foreach (var filePath in assemblyPaths)
                {
                    var assemblyPair = new AssemblyPair();

                    try
                    {
                        log.AppendFormat("Strong-name signing {0}...", filePath).AppendLine();

                        assemblyPair.OldInfo = SigningHelper.GetAssemblyInfo(filePath);
                        assemblyPair.NewInfo = SigningHelper.SignAssembly(filePath, keyFile, outputPath, password, probingPaths);

                        if (assemblyPair.NewInfo.SigningType == StrongNameType.DelaySigned)
                        {
                            log.Append("Delay-signed assembly signing is not supported yet...").AppendLine();
                        }
                        else if (!assemblyPair.OldInfo.IsSigned && assemblyPair.NewInfo.IsSigned)
                        {
                            log.Append("Strong-name signed successfully.").AppendLine();
                            signedAssemblyPaths.Add(assemblyPair.NewInfo.FilePath);
                            signedFiles++;
                        }
                        else
                        {
                            log.Append("Already strong-name signed...").AppendLine();
                        }

                        processedAssemblyPaths.Add(assemblyPair.NewInfo.FilePath);
                    }
                    catch (Exception ex)
                    {
                        log.AppendFormat("Error strong-name signing {0}: {1}", filePath, ex.Message).AppendLine();
                    }

                    backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100), assemblyPair);

                    if (backgroundWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }
                }

                var referencesToFix = new HashSet <string>(processedAssemblyPaths, StringComparer.OrdinalIgnoreCase);
                foreach (var filePath in processedAssemblyPaths)
                {
                    // Go through all the references excluding the file we are working on.
                    foreach (var reference in referencesToFix.Where(r => !r.Equals(filePath)))
                    {
                        backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100));

                        if (backgroundWorker.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }

                        log.AppendFormat("Fixing references to {1} in {0}...", filePath, reference).AppendLine();
                        if (SigningHelper.FixAssemblyReference(filePath, reference, keyFile, password, probingPaths))
                        {
                            log.Append("Reference was found and fixed.").AppendLine();
                            referenceFixes++;
                        }
                        else
                        {
                            log.Append("Nothing to fix.").AppendLine();
                        }
                    }
                }

                // Go through all processed assemblies and remove invalid friend references.
                foreach (var filePath in signedAssemblyPaths)
                {
                    backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100));

                    if (backgroundWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }

                    log.AppendFormat("Removing invalid friend references from '{0}'...", filePath).AppendLine();
                    if (SigningHelper.RemoveInvalidFriendAssemblies(filePath, keyFile, password, probingPaths))
                    {
                        log.Append("Invalid friend assemblies removed.").AppendLine();
                        referenceFixes++;
                    }
                    else
                    {
                        log.Append("Nothing to fix.").AppendLine();
                    }
                }

                e.Result = string.Format(CultureInfo.CurrentCulture, "{0} out of {1} assemblies were strong-name signed and {2} references were fixed.", signedFiles, assemblyPaths.Count(), referenceFixes);
            }
        }