-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodDefinitionExtensions.cs
More file actions
63 lines (55 loc) · 1.75 KB
/
MethodDefinitionExtensions.cs
File metadata and controls
63 lines (55 loc) · 1.75 KB
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
namespace Pluton.Patcher
{
using System;
using System.Linq;
using Mono.Cecil;
static class MethodDefinitionExtensions
{
public static string GetSigniture(this MethodDefinition self)
{
return $"{self.Name}({String.Join(",", (from param in self.Parameters select param.ParameterType.Name))})";
}
public static MethodDefinition SetPublic(this MethodDefinition self, bool value)
{
if (self == null) {
throw new ArgumentNullException(nameof(self));
}
self.IsPublic = value;
self.IsPrivate = !value;
return self;
}
public static string Print(this MethodDefinition self)
{
return self.PrintIL() + Environment.NewLine + self.PrintCSharp();
}
public static string PrintIL(this MethodDefinition self)
{
try {
var textoutput = new ICSharpCode.Decompiler.PlainTextOutput();
var disasm = new ICSharpCode.Decompiler.Disassembler.ReflectionDisassembler(textoutput, true, new System.Threading.CancellationToken());
disasm.DisassembleMethod(self);
return textoutput.ToString();
} catch (Exception ex) {
MainClass.LogException(ex);
return ex.ToString();
}
}
public static string PrintCSharp(this MethodDefinition self)
{
try {
var textoutput = new ICSharpCode.Decompiler.PlainTextOutput();
var builder = new ICSharpCode.Decompiler.Ast.AstBuilder(new ICSharpCode.Decompiler.DecompilerContext(self.DeclaringType.Module) {
CancellationToken = new System.Threading.CancellationToken(),
CurrentType = self.DeclaringType,
Settings = new ICSharpCode.Decompiler.DecompilerSettings()
});
builder.AddMethod(self);
builder.GenerateCode(textoutput);
return textoutput.ToString();
} catch (Exception ex) {
MainClass.LogException(ex);
return ex.ToString();
}
}
}
}