Browse Source

Use List<LoadedAssembly> in ILSpy/SolutionWriter.cs

pull/3502/head
Siegfried Pammer 2 weeks ago
parent
commit
f9b0411a96
  1. 2
      ILSpy/Commands/SaveCodeContextMenuEntry.cs
  2. 44
      ILSpy/SolutionWriter.cs

2
ILSpy/Commands/SaveCodeContextMenuEntry.cs

@ -78,7 +78,7 @@ namespace ICSharpCode.ILSpy.TextView
{ {
var assemblies = selectedNodes.OfType<AssemblyTreeNode>() var assemblies = selectedNodes.OfType<AssemblyTreeNode>()
.Select(n => n.LoadedAssembly) .Select(n => n.LoadedAssembly)
.Where(a => a.IsLoadedAsValidAssembly).ToArray(); .Where(a => a.IsLoadedAsValidAssembly).ToList();
SolutionWriter.CreateSolution(tabPage, textView, selectedPath, currentLanguage, assemblies); SolutionWriter.CreateSolution(tabPage, textView, selectedPath, currentLanguage, assemblies);
} }
return; return;

44
ILSpy/SolutionWriter.cs

@ -56,7 +56,7 @@ namespace ICSharpCode.ILSpy
/// <exception cref="ArgumentNullException">Thrown when <paramref name="textView"/>> or /// <exception cref="ArgumentNullException">Thrown when <paramref name="textView"/>> or
/// <paramref name="assemblies"/> is null.</exception> /// <paramref name="assemblies"/> is null.</exception>
public static void CreateSolution(TabPageModel tabPage, DecompilerTextView textView, string solutionFilePath, public static void CreateSolution(TabPageModel tabPage, DecompilerTextView textView, string solutionFilePath,
Language language, IEnumerable<LoadedAssembly> assemblies) Language language, List<LoadedAssembly> assemblies)
{ {
if (textView == null) if (textView == null)
{ {
@ -77,7 +77,7 @@ namespace ICSharpCode.ILSpy
textView textView
.RunWithCancellation(ct => writer.CreateSolution(tabPage, assemblies, language, ct)) .RunWithCancellation(ct => writer.CreateSolution(tabPage, assemblies, language, ct))
.Then(output => textView.ShowText(output)) .Then(textView.ShowText)
.HandleExceptions(); .HandleExceptions();
} }
@ -94,41 +94,29 @@ namespace ICSharpCode.ILSpy
projects = new ConcurrentBag<ProjectItem>(); projects = new ConcurrentBag<ProjectItem>();
} }
async Task<AvalonEditTextOutput> CreateSolution(TabPageModel tabPage, IEnumerable<LoadedAssembly> assemblies, Language language, CancellationToken ct) async Task<AvalonEditTextOutput> CreateSolution(TabPageModel tabPage, List<LoadedAssembly> allAssemblies, Language language, CancellationToken ct)
{ {
var result = new AvalonEditTextOutput(); var result = new AvalonEditTextOutput();
var assembliesByShortName = assemblies.ToLookup(_ => _.ShortName); var assembliesByShortName = allAssemblies.GroupBy(_ => _.ShortName).ToDictionary(_ => _.Key, _ => _.ToList());
bool first = true; bool first = true;
bool abort = false; bool abort = false;
foreach (var item in assembliesByShortName) foreach (var (shortName, assemblies) in assembliesByShortName)
{ {
var enumerator = item.GetEnumerator(); if (assemblies.Count == 1)
if (!enumerator.MoveNext()) {
continue;
var firstAssembly = enumerator.Current;
if (!enumerator.MoveNext())
continue; continue;
}
if (first) if (first)
{ {
result.WriteLine("Duplicate assembly names selected, cannot generate a solution:"); result.WriteLine("Duplicate assembly names selected, cannot generate a solution:");
abort = true; abort = true;
first = false;
} }
result.Write("- " + firstAssembly.Text + " conflicts with "); result.WriteLine("- " + assemblies[0].Text + " conflicts with " + string.Join(", ", assemblies.Skip(1)));
first = true;
do
{
var asm = enumerator.Current;
if (!first)
result.Write(", ");
result.Write(asm.Text);
first = false;
} while (enumerator.MoveNext());
result.WriteLine();
first = false;
} }
if (abort) if (abort)
@ -141,7 +129,7 @@ namespace ICSharpCode.ILSpy
// Explicitly create an enumerable partitioner here to avoid Parallel.ForEach's special cases for lists, // Explicitly create an enumerable partitioner here to avoid Parallel.ForEach's special cases for lists,
// as those seem to use static partitioning which is inefficient if assemblies take differently // as those seem to use static partitioning which is inefficient if assemblies take differently
// long to decompile. // long to decompile.
await Task.Run(() => Parallel.ForEach(Partitioner.Create(assemblies), await Task.Run(() => Parallel.ForEach(Partitioner.Create(allAssemblies),
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = ct }, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = ct },
item => WriteProject(tabPage, item, language, solutionDirectory, ct))) item => WriteProject(tabPage, item, language, solutionDirectory, ct)))
.ConfigureAwait(false); .ConfigureAwait(false);
@ -153,7 +141,7 @@ namespace ICSharpCode.ILSpy
} }
else else
{ {
await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects.ToList())) await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects.ToList()), ct)
.ConfigureAwait(false); .ConfigureAwait(false);
} }
} }
@ -184,14 +172,14 @@ namespace ICSharpCode.ILSpy
if (statusOutput.Count == 0) if (statusOutput.Count == 0)
{ {
result.WriteLine("Successfully decompiled the following assemblies into Visual Studio projects:"); result.WriteLine("Successfully decompiled the following assemblies into Visual Studio projects:");
foreach (var item in assemblies.Select(n => n.Text.ToString())) foreach (var n in allAssemblies)
{ {
result.WriteLine(item); result.WriteLine(n.Text.ToString());
} }
result.WriteLine(); result.WriteLine();
if (assemblies.Count() == projects.Count) if (allAssemblies.Count == projects.Count)
{ {
result.WriteLine("Created the Visual Studio Solution file."); result.WriteLine("Created the Visual Studio Solution file.");
} }

Loading…
Cancel
Save