-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
GetDecompiledProjectCmdlet.cs
100 lines (88 loc) · 2.66 KB
/
GetDecompiledProjectCmdlet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.IO;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.CSharp.ProjectDecompiler;
using ICSharpCode.Decompiler.Metadata;
namespace ICSharpCode.Decompiler.PowerShell
{
[Cmdlet(VerbsCommon.Get, "DecompiledProject")]
[OutputType(typeof(string))]
public class GetDecompiledProjectCmdlet : PSCmdlet, IProgress<DecompilationProgress>
{
[Parameter(Position = 0, Mandatory = true)]
public CSharpDecompiler Decompiler { get; set; }
[Parameter(Position = 1, Mandatory = true)]
[Alias("PSPath", "OutputPath")]
[ValidateNotNullOrEmpty]
public string LiteralPath { get; set; }
readonly object syncObject = new object();
int completed;
string fileName;
ProgressRecord progress;
public void Report(DecompilationProgress value)
{
lock (syncObject)
{
completed++;
progress = new ProgressRecord(1, "Decompiling " + fileName, $"Completed {completed} of {value.TotalUnits}: {value.Status}") {
PercentComplete = (int)(completed * 100.0 / value.TotalUnits)
};
}
}
protected override void ProcessRecord()
{
string path = GetUnresolvedProviderPathFromPSPath(LiteralPath);
if (!Directory.Exists(path))
{
WriteObject("Destination directory must exist prior to decompilation");
return;
}
try
{
var task = Task.Run(() => DoDecompile(path));
int timeout = 100;
// Give the decompiler some time to spin up all threads
Thread.Sleep(timeout);
while (!task.IsCompleted)
{
ProgressRecord progress;
lock (syncObject)
{
progress = this.progress;
this.progress = null;
}
if (progress != null)
{
timeout = 100;
WriteProgress(progress);
}
else
{
Thread.Sleep(timeout);
timeout = Math.Min(1000, timeout * 2);
}
}
task.Wait();
WriteProgress(new ProgressRecord(1, "Decompiling " + fileName, "Decompilation finished") { RecordType = ProgressRecordType.Completed });
}
catch (Exception e)
{
WriteVerbose(e.ToString());
WriteError(new ErrorRecord(e, ErrorIds.DecompilationFailed, ErrorCategory.OperationStopped, null));
}
}
private void DoDecompile(string path)
{
MetadataFile module = Decompiler.TypeSystem.MainModule.MetadataFile;
var assemblyResolver = new UniversalAssemblyResolver(module.FileName, false, module.Metadata.DetectTargetFrameworkId());
WholeProjectDecompiler decompiler = new WholeProjectDecompiler(assemblyResolver);
decompiler.ProgressIndicator = this;
fileName = module.FileName;
completed = 0;
decompiler.DecompileProject(module, path);
}
}
}