private void UpdateAssemblyInList(AssemblyPair pair) { if (pair != null && pair.OldInfo != null && pair.NewInfo != null) { for (int i = 0; i < listViewAssemblies.Items.Count; i++) { if (listViewAssemblies.Items[i].Tag.ToString().Equals(pair.OldInfo.FilePath, StringComparison.OrdinalIgnoreCase)) { // Replace with new information. listViewAssemblies.Items[i] = CreateListViewItem(pair.NewInfo); break; } } } }
private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) { e.Result = string.Empty; int progress = 0; int signedFiles = 0; int referenceFixes = 0; var assemblyPaths = e.Argument as IEnumerable<string>; var processedAssemblyPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase); if (assemblyPaths != null) { // We go through assemblies twice and every assembly -1 for reference fixes. double progressMax = (assemblyPaths.Count() + (assemblyPaths.Count() * (assemblyPaths.Count() - 1))) * 2; foreach (var filePath in assemblyPaths) { var assemblyPair = new AssemblyPair(); try { log.AppendFormat("Strong-name signing {0}...", filePath).AppendLine(); assemblyPair.OldInfo = SigningHelper.GetAssemblyInfo(filePath); if (!assemblyPair.OldInfo.IsSigned) { assemblyPair.NewInfo = SigningHelper.SignAssembly(filePath, keyFile, outputPath, password); log.Append("Strong-name signed successfully.").AppendLine(); signedFiles++; } else { assemblyPair.NewInfo = assemblyPair.OldInfo; 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)) { log.Append("Reference was found and fixed.").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); } }
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); } }