Skip to content

Commit f9b0411

Browse files
Use List<LoadedAssembly> in ILSpy/SolutionWriter.cs
1 parent fdb0703 commit f9b0411

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

ILSpy/Commands/SaveCodeContextMenuEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static void Execute(IReadOnlyList<SharpTreeNode> selectedNodes, LanguageS
7878
{
7979
var assemblies = selectedNodes.OfType<AssemblyTreeNode>()
8080
.Select(n => n.LoadedAssembly)
81-
.Where(a => a.IsLoadedAsValidAssembly).ToArray();
81+
.Where(a => a.IsLoadedAsValidAssembly).ToList();
8282
SolutionWriter.CreateSolution(tabPage, textView, selectedPath, currentLanguage, assemblies);
8383
}
8484
return;

ILSpy/SolutionWriter.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal class SolutionWriter
5656
/// <exception cref="ArgumentNullException">Thrown when <paramref name="textView"/>> or
5757
/// <paramref name="assemblies"/> is null.</exception>
5858
public static void CreateSolution(TabPageModel tabPage, DecompilerTextView textView, string solutionFilePath,
59-
Language language, IEnumerable<LoadedAssembly> assemblies)
59+
Language language, List<LoadedAssembly> assemblies)
6060
{
6161
if (textView == null)
6262
{
@@ -77,7 +77,7 @@ public static void CreateSolution(TabPageModel tabPage, DecompilerTextView textV
7777

7878
textView
7979
.RunWithCancellation(ct => writer.CreateSolution(tabPage, assemblies, language, ct))
80-
.Then(output => textView.ShowText(output))
80+
.Then(textView.ShowText)
8181
.HandleExceptions();
8282
}
8383

@@ -94,41 +94,29 @@ public static void CreateSolution(TabPageModel tabPage, DecompilerTextView textV
9494
projects = new ConcurrentBag<ProjectItem>();
9595
}
9696

97-
async Task<AvalonEditTextOutput> CreateSolution(TabPageModel tabPage, IEnumerable<LoadedAssembly> assemblies, Language language, CancellationToken ct)
97+
async Task<AvalonEditTextOutput> CreateSolution(TabPageModel tabPage, List<LoadedAssembly> allAssemblies, Language language, CancellationToken ct)
9898
{
9999
var result = new AvalonEditTextOutput();
100100

101-
var assembliesByShortName = assemblies.ToLookup(_ => _.ShortName);
101+
var assembliesByShortName = allAssemblies.GroupBy(_ => _.ShortName).ToDictionary(_ => _.Key, _ => _.ToList());
102102
bool first = true;
103103
bool abort = false;
104104

105-
foreach (var item in assembliesByShortName)
105+
foreach (var (shortName, assemblies) in assembliesByShortName)
106106
{
107-
var enumerator = item.GetEnumerator();
108-
if (!enumerator.MoveNext())
109-
continue;
110-
var firstAssembly = enumerator.Current;
111-
if (!enumerator.MoveNext())
107+
if (assemblies.Count == 1)
108+
{
112109
continue;
110+
}
111+
113112
if (first)
114113
{
115114
result.WriteLine("Duplicate assembly names selected, cannot generate a solution:");
116115
abort = true;
116+
first = false;
117117
}
118118

119-
result.Write("- " + firstAssembly.Text + " conflicts with ");
120-
121-
first = true;
122-
do
123-
{
124-
var asm = enumerator.Current;
125-
if (!first)
126-
result.Write(", ");
127-
result.Write(asm.Text);
128-
first = false;
129-
} while (enumerator.MoveNext());
130-
result.WriteLine();
131-
first = false;
119+
result.WriteLine("- " + assemblies[0].Text + " conflicts with " + string.Join(", ", assemblies.Skip(1)));
132120
}
133121

134122
if (abort)
@@ -141,7 +129,7 @@ async Task<AvalonEditTextOutput> CreateSolution(TabPageModel tabPage, IEnumerabl
141129
// Explicitly create an enumerable partitioner here to avoid Parallel.ForEach's special cases for lists,
142130
// as those seem to use static partitioning which is inefficient if assemblies take differently
143131
// long to decompile.
144-
await Task.Run(() => Parallel.ForEach(Partitioner.Create(assemblies),
132+
await Task.Run(() => Parallel.ForEach(Partitioner.Create(allAssemblies),
145133
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = ct },
146134
item => WriteProject(tabPage, item, language, solutionDirectory, ct)))
147135
.ConfigureAwait(false);
@@ -153,7 +141,7 @@ await Task.Run(() => Parallel.ForEach(Partitioner.Create(assemblies),
153141
}
154142
else
155143
{
156-
await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects.ToList()))
144+
await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects.ToList()), ct)
157145
.ConfigureAwait(false);
158146
}
159147
}
@@ -184,14 +172,14 @@ await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, project
184172
if (statusOutput.Count == 0)
185173
{
186174
result.WriteLine("Successfully decompiled the following assemblies into Visual Studio projects:");
187-
foreach (var item in assemblies.Select(n => n.Text.ToString()))
175+
foreach (var n in allAssemblies)
188176
{
189-
result.WriteLine(item);
177+
result.WriteLine(n.Text.ToString());
190178
}
191179

192180
result.WriteLine();
193181

194-
if (assemblies.Count() == projects.Count)
182+
if (allAssemblies.Count == projects.Count)
195183
{
196184
result.WriteLine("Created the Visual Studio Solution file.");
197185
}

0 commit comments

Comments
 (0)