This repository was archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
50 lines (37 loc) · 1.33 KB
/
Program.cs
File metadata and controls
50 lines (37 loc) · 1.33 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
using Mono.Cecil;
using Mono.Cecil.Cil;
if (Environment.GetCommandLineArgs().Length <= 1)
{
Console.WriteLine("No target specified");
Console.ReadLine();
}
string target = Environment.GetCommandLineArgs()[1];
var asm = AssemblyDefinition.ReadAssembly(target);
foreach (TypeDefinition type in asm.MainModule.Types)
ProcessType(type);
void ProcessType(TypeDefinition type)
{
if (type.HasNestedTypes)
foreach (TypeDefinition tp in type.NestedTypes)
ProcessType(tp);
if (type.HasMethods)
foreach (MethodDefinition method in type.Methods)
ProcessMethod(method);
}
void ProcessMethod(MethodDefinition method)
{
if (method?.Body is null) return;
var il = method.Body.GetILProcessor();
var first = method.Body.Instructions[0];
Instruction start = Instruction.Create(OpCodes.Ldc_I4_0);
il.InsertBefore(first, start);
Instruction invalidDestination;
if (method.ReturnType.FullName == "System.Void")
il.InsertBefore(first, invalidDestination = Instruction.Create(OpCodes.Unaligned, (byte)0));
else invalidDestination = method.Body.Instructions[^1];
il.InsertAfter(start, il.Create(OpCodes.Switch, new Instruction[] { first, invalidDestination }));
}
MemoryStream mem = new();
asm.Write(mem);
asm.Dispose();
File.WriteAllBytes(target, mem.ToArray());