Browse Source

#1663: Add various safe-guards against unintended use to Language and SolutionWriter.

pull/2134/head
Siegfried Pammer 5 years ago
parent
commit
6bd3f5fe74
  1. 6
      ILSpy/Languages/ILLanguage.cs
  2. 4
      ILSpy/Languages/Language.cs
  3. 34
      ILSpy/SolutionWriter.cs

6
ILSpy/Languages/ILLanguage.cs

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
@ -165,6 +166,11 @@ namespace ICSharpCode.ILSpy @@ -165,6 +166,11 @@ namespace ICSharpCode.ILSpy
var metadata = module.Metadata;
var dis = CreateDisassembler(output, options);
if (options.FullDecompilation && options.SaveAsProjectDirectory != null)
{
throw new NotSupportedException($"Language '{Name}' does not support exporting assemblies as projects!");
}
// don't automatically load additional assemblies when an assembly node is selected in the tree view
using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad(assembly.AssemblyList))
{

4
ILSpy/Languages/Language.cs

@ -130,6 +130,10 @@ namespace ICSharpCode.ILSpy @@ -130,6 +130,10 @@ namespace ICSharpCode.ILSpy
var asm = assembly.GetPEFileOrNull();
if (asm == null)
return null;
if (options.FullDecompilation && options.SaveAsProjectDirectory != null)
{
throw new NotSupportedException($"Language '{Name}' does not support exporting assemblies as projects!");
}
var metadata = asm.Metadata;
if (metadata.IsAssembly)
{

34
ILSpy/SolutionWriter.cs

@ -113,8 +113,16 @@ namespace ICSharpCode.ILSpy @@ -113,8 +113,16 @@ namespace ICSharpCode.ILSpy
n => WriteProject(n, language, solutionDirectory, ct)))
.ConfigureAwait(false);
await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects))
.ConfigureAwait(false);
if (projects.Count == 0)
{
result.WriteLine();
result.WriteLine("Solution could not be created, because none of the selected assemblies could be decompiled into a project.");
}
else
{
await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects))
.ConfigureAwait(false);
}
}
catch (AggregateException ae)
{
@ -167,8 +175,23 @@ namespace ICSharpCode.ILSpy @@ -167,8 +175,23 @@ namespace ICSharpCode.ILSpy
void WriteProject(LoadedAssembly loadedAssembly, Language language, string targetDirectory, CancellationToken ct)
{
targetDirectory = Path.Combine(targetDirectory, loadedAssembly.ShortName);
if (language.ProjectFileExtension == null)
{
statusOutput.Add("-------------");
statusOutput.Add($"Language '{language.Name}' does not support exporting assemblies as projects!");
return;
}
string projectFileName = Path.Combine(targetDirectory, loadedAssembly.ShortName + language.ProjectFileExtension);
if (File.Exists(targetDirectory))
{
statusOutput.Add("-------------");
statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}A file with the same name already exists!");
return;
}
if (!Directory.Exists(targetDirectory))
{
try
@ -177,6 +200,7 @@ namespace ICSharpCode.ILSpy @@ -177,6 +200,7 @@ namespace ICSharpCode.ILSpy
}
catch (Exception e)
{
statusOutput.Add("-------------");
statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}{e}");
return;
}
@ -200,8 +224,14 @@ namespace ICSharpCode.ILSpy @@ -200,8 +224,14 @@ namespace ICSharpCode.ILSpy
}
}
}
catch (NotSupportedException e)
{
statusOutput.Add("-------------");
statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e.Message}");
}
catch (Exception e) when (!(e is OperationCanceledException))
{
statusOutput.Add("-------------");
statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e}");
}
}

Loading…
Cancel
Save