mirror of https://github.com/icsharpcode/ILSpy.git
29 changed files with 792 additions and 85 deletions
@ -0,0 +1,56 @@ |
|||||||
|
// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.Solution |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// A container class that holds platform and GUID information about a Visual Studio project.
|
||||||
|
/// </summary>
|
||||||
|
public class ProjectId |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="ProjectId"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="projectPlatform">The project platform.</param>
|
||||||
|
/// <param name="projectGuid">The project GUID.</param>
|
||||||
|
///
|
||||||
|
/// <exception cref="ArgumentException">Thrown when <paramref name="projectFile"/>
|
||||||
|
/// or <paramref name="projectPlatform"/> is null or empty.</exception>
|
||||||
|
public ProjectId(string projectPlatform, Guid projectGuid) |
||||||
|
{ |
||||||
|
if (string.IsNullOrWhiteSpace(projectPlatform)) { |
||||||
|
throw new ArgumentException("The platform cannot be null or empty.", nameof(projectPlatform)); |
||||||
|
} |
||||||
|
|
||||||
|
Guid = projectGuid; |
||||||
|
PlatformName = projectPlatform; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the GUID of this project.
|
||||||
|
/// </summary>
|
||||||
|
public Guid Guid { get; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the platform name of this project. Only single platform per project is supported.
|
||||||
|
/// </summary>
|
||||||
|
public string PlatformName { get; } |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,55 @@ |
|||||||
|
// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.IO; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.Solution |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// A container class that holds information about a Visual Studio project.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProjectItem : ProjectId |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="ProjectItem"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="projectFile">The full path of the project file.</param>
|
||||||
|
/// <param name="projectPlatform">The project platform.</param>
|
||||||
|
/// <param name="projectGuid">The project GUID.</param>
|
||||||
|
///
|
||||||
|
/// <exception cref="ArgumentException">Thrown when <paramref name="projectFile"/>
|
||||||
|
/// or <paramref name="projectPlatform"/> is null or empty.</exception>
|
||||||
|
public ProjectItem(string projectFile, string projectPlatform, Guid projectGuid) |
||||||
|
: base(projectPlatform, projectGuid) |
||||||
|
{ |
||||||
|
ProjectName = Path.GetFileNameWithoutExtension(projectFile); |
||||||
|
FilePath = projectFile; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the name of the project.
|
||||||
|
/// </summary>
|
||||||
|
public string ProjectName { get; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the full path to the project file.
|
||||||
|
/// </summary>
|
||||||
|
public string FilePath { get; } |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,201 @@ |
|||||||
|
// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// 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.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Xml.Linq; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.Solution |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// A helper class that can write a Visual Studio Solution file for the provided projects.
|
||||||
|
/// </summary>
|
||||||
|
public static class SolutionCreator |
||||||
|
{ |
||||||
|
private static readonly XNamespace ProjectFileNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes a solution file to the specified <paramref name="targetFile"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="targetFile">The full path of the file to write.</param>
|
||||||
|
/// <param name="projects">The projects contained in this solution.</param>
|
||||||
|
///
|
||||||
|
/// <exception cref="ArgumentException">Thrown when <paramref name="targetFile"/> is null or empty.</exception>
|
||||||
|
/// <exception cref="ArgumentNullException">Thrown when <paramref name="projects"/> is null.</exception>
|
||||||
|
/// <exception cref="InvalidOperationException">Thrown when <paramref name="projects"/> contains no items.</exception>
|
||||||
|
public static void WriteSolutionFile(string targetFile, IEnumerable<ProjectItem> projects) |
||||||
|
{ |
||||||
|
if (string.IsNullOrWhiteSpace(targetFile)) { |
||||||
|
throw new ArgumentException("The target file cannot be null or empty.", nameof(targetFile)); |
||||||
|
} |
||||||
|
|
||||||
|
if (projects == null) { |
||||||
|
throw new ArgumentNullException(nameof(projects)); |
||||||
|
} |
||||||
|
|
||||||
|
if (!projects.Any()) { |
||||||
|
throw new InvalidOperationException("At least one project is expected."); |
||||||
|
} |
||||||
|
|
||||||
|
using (var writer = new StreamWriter(targetFile)) { |
||||||
|
WriteSolutionFile(writer, projects, targetFile); |
||||||
|
} |
||||||
|
|
||||||
|
FixProjectReferences(projects); |
||||||
|
} |
||||||
|
|
||||||
|
private static void WriteSolutionFile(TextWriter writer, IEnumerable<ProjectItem> projects, string solutionFilePath) |
||||||
|
{ |
||||||
|
WriteHeader(writer); |
||||||
|
WriteProjects(writer, projects, solutionFilePath); |
||||||
|
|
||||||
|
writer.WriteLine("Global"); |
||||||
|
|
||||||
|
var platforms = WriteSolutionConfigurations(writer, projects); |
||||||
|
WriteProjectConfigurations(writer, projects, platforms); |
||||||
|
|
||||||
|
writer.WriteLine("\tGlobalSection(SolutionProperties) = preSolution"); |
||||||
|
writer.WriteLine("\t\tHideSolutionNode = FALSE"); |
||||||
|
writer.WriteLine("\tEndGlobalSection"); |
||||||
|
|
||||||
|
writer.WriteLine("EndGlobal"); |
||||||
|
} |
||||||
|
|
||||||
|
private static void WriteHeader(TextWriter writer) |
||||||
|
{ |
||||||
|
writer.WriteLine("Microsoft Visual Studio Solution File, Format Version 12.00"); |
||||||
|
writer.WriteLine("# Visual Studio 14"); |
||||||
|
writer.WriteLine("VisualStudioVersion = 14.0.24720.0"); |
||||||
|
writer.WriteLine("MinimumVisualStudioVersion = 10.0.40219.1"); |
||||||
|
} |
||||||
|
|
||||||
|
private static void WriteProjects(TextWriter writer, IEnumerable<ProjectItem> projects, string solutionFilePath) |
||||||
|
{ |
||||||
|
var solutionGuid = Guid.NewGuid().ToString("B").ToUpperInvariant(); |
||||||
|
|
||||||
|
foreach (var project in projects) { |
||||||
|
var projectRelativePath = GetRelativePath(solutionFilePath, project.FilePath); |
||||||
|
var projectGuid = project.Guid.ToString("B").ToUpperInvariant(); |
||||||
|
|
||||||
|
writer.WriteLine($"Project(\"{solutionGuid}\") = \"{project.ProjectName}\", \"{projectRelativePath}\", \"{projectGuid}\""); |
||||||
|
writer.WriteLine("EndProject"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static IEnumerable<string> WriteSolutionConfigurations(TextWriter writer, IEnumerable<ProjectItem> projects) |
||||||
|
{ |
||||||
|
var platforms = projects.GroupBy(p => p.PlatformName).Select(g => g.Key).ToList(); |
||||||
|
|
||||||
|
platforms.Sort(); |
||||||
|
|
||||||
|
writer.WriteLine("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution"); |
||||||
|
foreach (var platform in platforms) { |
||||||
|
writer.WriteLine($"\t\tDebug|{platform} = Debug|{platform}"); |
||||||
|
} |
||||||
|
|
||||||
|
foreach (var platform in platforms) { |
||||||
|
writer.WriteLine($"\t\tRelease|{platform} = Release|{platform}"); |
||||||
|
} |
||||||
|
|
||||||
|
writer.WriteLine("\tEndGlobalSection"); |
||||||
|
|
||||||
|
return platforms; |
||||||
|
} |
||||||
|
|
||||||
|
private static void WriteProjectConfigurations( |
||||||
|
TextWriter writer, |
||||||
|
IEnumerable<ProjectItem> projects, |
||||||
|
IEnumerable<string> solutionPlatforms) |
||||||
|
{ |
||||||
|
writer.WriteLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution"); |
||||||
|
|
||||||
|
foreach (var project in projects) { |
||||||
|
var projectGuid = project.Guid.ToString("B").ToUpperInvariant(); |
||||||
|
|
||||||
|
foreach (var platform in solutionPlatforms) { |
||||||
|
writer.WriteLine($"\t\t{projectGuid}.Debug|{platform}.ActiveCfg = Debug|{project.PlatformName}"); |
||||||
|
writer.WriteLine($"\t\t{projectGuid}.Debug|{platform}.Build.0 = Debug|{project.PlatformName}"); |
||||||
|
} |
||||||
|
|
||||||
|
foreach (var platform in solutionPlatforms) { |
||||||
|
writer.WriteLine($"\t\t{projectGuid}.Release|{platform}.ActiveCfg = Release|{project.PlatformName}"); |
||||||
|
writer.WriteLine($"\t\t{projectGuid}.Release|{platform}.Build.0 = Release|{project.PlatformName}"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
writer.WriteLine("\tEndGlobalSection"); |
||||||
|
} |
||||||
|
|
||||||
|
private static void FixProjectReferences(IEnumerable<ProjectItem> projects) |
||||||
|
{ |
||||||
|
var projectsMap = projects.ToDictionary(p => p.ProjectName, p => p); |
||||||
|
|
||||||
|
foreach (var project in projects) { |
||||||
|
XDocument projectDoc = XDocument.Load(project.FilePath); |
||||||
|
|
||||||
|
var referencesItemGroups = projectDoc.Root |
||||||
|
.Elements(ProjectFileNamespace + "ItemGroup") |
||||||
|
.Where(e => e.Elements(ProjectFileNamespace + "Reference").Any()); |
||||||
|
|
||||||
|
foreach (var itemGroup in referencesItemGroups) { |
||||||
|
FixProjectReferences(project.FilePath, itemGroup, projectsMap); |
||||||
|
} |
||||||
|
|
||||||
|
projectDoc.Save(project.FilePath); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static void FixProjectReferences(string projectFilePath, XElement itemGroup, IDictionary<string, ProjectItem> projects) |
||||||
|
{ |
||||||
|
foreach (var item in itemGroup.Elements(ProjectFileNamespace + "Reference").ToList()) { |
||||||
|
var assemblyName = item.Attribute("Include")?.Value; |
||||||
|
if (assemblyName != null && projects.TryGetValue(assemblyName, out var referencedProject)) { |
||||||
|
item.Remove(); |
||||||
|
|
||||||
|
var projectReference = new XElement(ProjectFileNamespace + "ProjectReference", |
||||||
|
new XElement(ProjectFileNamespace + "Project", referencedProject.Guid.ToString("B").ToUpperInvariant()), |
||||||
|
new XElement(ProjectFileNamespace + "Name", referencedProject.ProjectName)); |
||||||
|
projectReference.SetAttributeValue("Include", GetRelativePath(projectFilePath, referencedProject.FilePath)); |
||||||
|
|
||||||
|
itemGroup.Add(projectReference); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static string GetRelativePath(string fromFilePath, string toFilePath) |
||||||
|
{ |
||||||
|
Uri fromUri = new Uri(fromFilePath); |
||||||
|
Uri toUri = new Uri(toFilePath); |
||||||
|
|
||||||
|
if (fromUri.Scheme != toUri.Scheme) { |
||||||
|
return toFilePath; |
||||||
|
} |
||||||
|
|
||||||
|
Uri relativeUri = fromUri.MakeRelativeUri(toUri); |
||||||
|
string relativePath = Uri.UnescapeDataString(relativeUri.ToString()); |
||||||
|
|
||||||
|
if (string.Equals(toUri.Scheme, Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase)) { |
||||||
|
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); |
||||||
|
} |
||||||
|
|
||||||
|
return relativePath; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,133 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// 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.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Windows; |
||||||
|
using System.Windows.Input; |
||||||
|
using ICSharpCode.ILSpy.Properties; |
||||||
|
using ICSharpCode.ILSpy.TreeNodes; |
||||||
|
using ICSharpCode.TreeView; |
||||||
|
using Microsoft.Win32; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TextView |
||||||
|
{ |
||||||
|
[ExportContextMenuEntry(Header = nameof(Resources._SaveCode), Category = nameof(Resources.Save), Icon = "Images/SaveFile.png")] |
||||||
|
sealed class SaveCodeContextMenuEntry : IContextMenuEntry |
||||||
|
{ |
||||||
|
public void Execute(TextViewContext context) |
||||||
|
{ |
||||||
|
Execute(context.SelectedTreeNodes); |
||||||
|
} |
||||||
|
|
||||||
|
public bool IsEnabled(TextViewContext context) => true; |
||||||
|
|
||||||
|
public bool IsVisible(TextViewContext context) |
||||||
|
{ |
||||||
|
return CanExecute(context.SelectedTreeNodes); |
||||||
|
} |
||||||
|
|
||||||
|
public static bool CanExecute(IReadOnlyList<SharpTreeNode> selectedNodes) |
||||||
|
{ |
||||||
|
if (selectedNodes == null || selectedNodes.Any(n => !(n is ILSpyTreeNode))) |
||||||
|
return false; |
||||||
|
return selectedNodes.Count == 1 |
||||||
|
|| (selectedNodes.Count > 1 && (selectedNodes.All(n => n is AssemblyTreeNode) || selectedNodes.All(n => n is IMemberTreeNode))); |
||||||
|
} |
||||||
|
|
||||||
|
public static void Execute(IReadOnlyList<SharpTreeNode> selectedNodes) |
||||||
|
{ |
||||||
|
var currentLanguage = MainWindow.Instance.CurrentLanguage; |
||||||
|
var textView = MainWindow.Instance.TextView; |
||||||
|
if (selectedNodes.Count == 1 && selectedNodes[0] is ILSpyTreeNode singleSelection) { |
||||||
|
// if there's only one treenode selected
|
||||||
|
// we will invoke the custom Save logic
|
||||||
|
if (singleSelection.Save(textView)) |
||||||
|
return; |
||||||
|
} else if (selectedNodes.Count > 1 && selectedNodes.All(n => n is AssemblyTreeNode)) { |
||||||
|
var initialPath = Path.GetDirectoryName(((AssemblyTreeNode)selectedNodes[0]).LoadedAssembly.FileName); |
||||||
|
var selectedPath = SelectSolutionFile(initialPath); |
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(selectedPath)) { |
||||||
|
var assemblies = selectedNodes.OfType<AssemblyTreeNode>() |
||||||
|
.Select(n => n.LoadedAssembly) |
||||||
|
.Where(a => !a.HasLoadError).ToArray(); |
||||||
|
SolutionWriter.CreateSolution(textView, selectedPath, currentLanguage, assemblies); |
||||||
|
} |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// Fallback: if nobody was able to handle the request, use default behavior.
|
||||||
|
// try to save all nodes to disk.
|
||||||
|
var options = new DecompilationOptions() { FullDecompilation = true }; |
||||||
|
textView.SaveToDisk(currentLanguage, selectedNodes.OfType<ILSpyTreeNode>(), options); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shows a File Selection dialog where the user can select the target file for the solution.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">The initial path to show in the dialog. If not specified, the 'Documents' directory
|
||||||
|
/// will be used.</param>
|
||||||
|
///
|
||||||
|
/// <returns>The full path of the selected target file, or <c>null</c> if the user canceled.</returns>
|
||||||
|
static string SelectSolutionFile(string path) |
||||||
|
{ |
||||||
|
const string SolutionExtension = ".sln"; |
||||||
|
const string DefaultSolutionName = "Solution"; |
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(path)) { |
||||||
|
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
||||||
|
} |
||||||
|
|
||||||
|
SaveFileDialog dlg = new SaveFileDialog(); |
||||||
|
dlg.InitialDirectory = path; |
||||||
|
dlg.FileName = Path.Combine(path, DefaultSolutionName + SolutionExtension); |
||||||
|
dlg.Filter = "Visual Studio Solution file|*" + SolutionExtension; |
||||||
|
|
||||||
|
bool targetInvalid; |
||||||
|
do { |
||||||
|
if (dlg.ShowDialog() != true) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
string selectedPath = Path.GetDirectoryName(dlg.FileName); |
||||||
|
try { |
||||||
|
targetInvalid = Directory.EnumerateFileSystemEntries(selectedPath).Any(); |
||||||
|
} catch (Exception e) when (e is IOException || e is UnauthorizedAccessException || e is System.Security.SecurityException) { |
||||||
|
MessageBox.Show( |
||||||
|
"The directory cannot be accessed. Please ensure it exists and you have sufficient rights to access it.", |
||||||
|
"Solution directory not accessible", |
||||||
|
MessageBoxButton.OK, MessageBoxImage.Error); |
||||||
|
targetInvalid = true; |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
if (targetInvalid) { |
||||||
|
MessageBox.Show( |
||||||
|
"The directory is not empty. Please select an empty directory.", |
||||||
|
"Solution directory not empty", |
||||||
|
MessageBoxButton.OK, MessageBoxImage.Warning); |
||||||
|
} |
||||||
|
} while (targetInvalid); |
||||||
|
|
||||||
|
return dlg.FileName; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,182 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// 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.Concurrent; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using ICSharpCode.Decompiler; |
||||||
|
using ICSharpCode.Decompiler.Solution; |
||||||
|
using ICSharpCode.Decompiler.Util; |
||||||
|
using ICSharpCode.ILSpy.TextView; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// An utility class that creates a Visual Studio solution containing projects for the
|
||||||
|
/// decompiled assemblies.
|
||||||
|
/// </summary>
|
||||||
|
internal class SolutionWriter |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Creates a Visual Studio solution that contains projects with decompiled code
|
||||||
|
/// of the specified <paramref name="assemblies"/>. The solution file will be saved
|
||||||
|
/// to the <paramref name="solutionFilePath"/>. The directory of this file must either
|
||||||
|
/// be empty or not exist.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="textView">A reference to the <see cref="DecompilerTextView"/> instance.</param>
|
||||||
|
/// <param name="solutionFilePath">The target file path of the solution file.</param>
|
||||||
|
/// <param name="assemblies">The assembly nodes to decompile.</param>
|
||||||
|
///
|
||||||
|
/// <exception cref="ArgumentException">Thrown when <paramref name="solutionFilePath"/> is null,
|
||||||
|
/// an empty or a whitespace string.</exception>
|
||||||
|
/// <exception cref="ArgumentNullException">Thrown when <paramref name="textView"/>> or
|
||||||
|
/// <paramref name="assemblies"/> is null.</exception>
|
||||||
|
public static void CreateSolution(DecompilerTextView textView, string solutionFilePath, Language language, IEnumerable<LoadedAssembly> assemblies) |
||||||
|
{ |
||||||
|
if (textView == null) { |
||||||
|
throw new ArgumentNullException(nameof(textView)); |
||||||
|
} |
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(solutionFilePath)) { |
||||||
|
throw new ArgumentException("The solution file path cannot be null or empty.", nameof(solutionFilePath)); |
||||||
|
} |
||||||
|
|
||||||
|
if (assemblies == null) { |
||||||
|
throw new ArgumentNullException(nameof(assemblies)); |
||||||
|
} |
||||||
|
|
||||||
|
var writer = new SolutionWriter(solutionFilePath); |
||||||
|
|
||||||
|
textView |
||||||
|
.RunWithCancellation(ct => writer.CreateSolution(assemblies, language, ct)) |
||||||
|
.Then(output => textView.ShowText(output)) |
||||||
|
.HandleExceptions(); |
||||||
|
} |
||||||
|
|
||||||
|
readonly string solutionFilePath; |
||||||
|
readonly string solutionDirectory; |
||||||
|
readonly ConcurrentBag<ProjectItem> projects; |
||||||
|
readonly ConcurrentBag<string> statusOutput; |
||||||
|
|
||||||
|
SolutionWriter(string solutionFilePath) |
||||||
|
{ |
||||||
|
this.solutionFilePath = solutionFilePath; |
||||||
|
solutionDirectory = Path.GetDirectoryName(solutionFilePath); |
||||||
|
statusOutput = new ConcurrentBag<string>(); |
||||||
|
projects = new ConcurrentBag<ProjectItem>(); |
||||||
|
} |
||||||
|
|
||||||
|
async Task<AvalonEditTextOutput> CreateSolution(IEnumerable<LoadedAssembly> assemblies, Language language, CancellationToken ct) |
||||||
|
{ |
||||||
|
var result = new AvalonEditTextOutput(); |
||||||
|
|
||||||
|
var duplicates = new HashSet<string>(); |
||||||
|
if (assemblies.Any(asm => !duplicates.Add(asm.ShortName))) { |
||||||
|
result.WriteLine("Duplicate assembly names selected, cannot generate a solution."); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
Stopwatch stopwatch = Stopwatch.StartNew(); |
||||||
|
|
||||||
|
try { |
||||||
|
await Task.Run(() => Parallel.ForEach(assemblies, n => WriteProject(n, language, solutionDirectory, ct))) |
||||||
|
.ConfigureAwait(false); |
||||||
|
|
||||||
|
await Task.Run(() => SolutionCreator.WriteSolutionFile(solutionFilePath, projects)) |
||||||
|
.ConfigureAwait(false); |
||||||
|
} catch (AggregateException ae) { |
||||||
|
if (ae.Flatten().InnerExceptions.All(e => e is OperationCanceledException)) { |
||||||
|
result.WriteLine(); |
||||||
|
result.WriteLine("Generation was cancelled."); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
result.WriteLine(); |
||||||
|
result.WriteLine("Failed to generate the Visual Studio Solution. Errors:"); |
||||||
|
ae.Handle(e => { |
||||||
|
result.WriteLine(e.Message); |
||||||
|
return true; |
||||||
|
}); |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
foreach (var item in statusOutput) { |
||||||
|
result.WriteLine(item); |
||||||
|
} |
||||||
|
|
||||||
|
if (statusOutput.Count == 0) { |
||||||
|
result.WriteLine("Successfully decompiled the following assemblies into Visual Studio projects:"); |
||||||
|
foreach (var item in assemblies.Select(n => n.Text.ToString())) { |
||||||
|
result.WriteLine(item); |
||||||
|
} |
||||||
|
|
||||||
|
result.WriteLine(); |
||||||
|
|
||||||
|
if (assemblies.Count() == projects.Count) { |
||||||
|
result.WriteLine("Created the Visual Studio Solution file."); |
||||||
|
} |
||||||
|
|
||||||
|
result.WriteLine(); |
||||||
|
result.WriteLine("Elapsed time: " + stopwatch.Elapsed.TotalSeconds.ToString("F1") + " seconds."); |
||||||
|
result.WriteLine(); |
||||||
|
result.AddButton(null, "Open Explorer", delegate { Process.Start("explorer", "/select,\"" + solutionFilePath + "\""); }); |
||||||
|
} |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
void WriteProject(LoadedAssembly loadedAssembly, Language language, string targetDirectory, CancellationToken ct) |
||||||
|
{ |
||||||
|
targetDirectory = Path.Combine(targetDirectory, loadedAssembly.ShortName); |
||||||
|
string projectFileName = Path.Combine(targetDirectory, loadedAssembly.ShortName + language.ProjectFileExtension); |
||||||
|
|
||||||
|
if (!Directory.Exists(targetDirectory)) { |
||||||
|
try { |
||||||
|
Directory.CreateDirectory(targetDirectory); |
||||||
|
} catch (Exception e) { |
||||||
|
statusOutput.Add($"Failed to create a directory '{targetDirectory}':{Environment.NewLine}{e}"); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
using (var projectFileWriter = new StreamWriter(projectFileName)) { |
||||||
|
var projectFileOutput = new PlainTextOutput(projectFileWriter); |
||||||
|
var options = new DecompilationOptions() { |
||||||
|
FullDecompilation = true, |
||||||
|
CancellationToken = ct, |
||||||
|
SaveAsProjectDirectory = targetDirectory |
||||||
|
}; |
||||||
|
|
||||||
|
var projectInfo = language.DecompileAssembly(loadedAssembly, projectFileOutput, options); |
||||||
|
if (projectInfo != null) { |
||||||
|
projects.Add(new ProjectItem(projectFileName, projectInfo.PlatformName, projectInfo.Guid)); |
||||||
|
} |
||||||
|
} |
||||||
|
} catch (Exception e) when (!(e is OperationCanceledException)) { |
||||||
|
statusOutput.Add($"Failed to decompile the assembly '{loadedAssembly.FileName}':{Environment.NewLine}{e}"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue