forked from GabeHasWon/VerdantMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ILHelper.cs
76 lines (65 loc) · 1.99 KB
/
ILHelper.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
using Microsoft.Xna.Framework;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using ReLogic.OS;
using System;
using System.IO;
using Terraria;
namespace Verdant
{
public static class ILHelper
{
public static void InGameDebug<T>(this ILCursor cursor)
{
cursor.Emit(OpCodes.Dup);
cursor.EmitDelegate((T v) => { Main.NewText(v); });
}
//Code adapted from https://github.com/absoluteAquarian
public static void CompleteLog(ILCursor c, bool beforeEdit = false)
{
int index = 0;
//Get the method name
string method = c.Method.Name;
if (!method.Contains("ctor"))
method = method[(method.LastIndexOf(':') + 1)..];
else
method = method[method.LastIndexOf('.')..];
if (beforeEdit)
method += " - Before";
else
method += " - After";
//And the storage path
string path = Directory.GetCurrentDirectory();
path = Path.Combine(path, "ILLogs");
Directory.CreateDirectory(path);
//Get the class name
string type = c.Method.Name;
type = type[..type.IndexOf(':')];
type = type[(type.LastIndexOf('.') + 1)..];
FileStream file = File.Open(Path.Combine(path, $"{type}.{method}.txt"), FileMode.Create);
using (StreamWriter writer = new StreamWriter(file))
{
writer.WriteLine(DateTime.Now.ToString("'['ddMMMyyyy '-' HH:mm:ss']'"));
writer.WriteLine($"// ILCursor: {c.Method}");
do
{
PrepareInstruction(c.Instrs[index], out string offset, out string opcode, out string operand);
writer.WriteLine($"{offset,-10}{opcode,-12} {operand}");
index++;
} while (index < c.Instrs.Count);
}
}
//Also adapted from https://github.com/absoluteAquarian
private static void PrepareInstruction(Instruction instr, out string offset, out string opcode, out string operand)
{
offset = $"IL_{instr.Offset:X4}:";
opcode = instr.OpCode.Name;
if (instr.Operand is null)
operand = "";
else if (instr.Operand is ILLabel label)
operand = $"IL_{label.Target.Offset:X4}";
else
operand = instr.Operand.ToString();
}
}
}