This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
XrefUtils.cs
119 lines (111 loc) · 4.02 KB
/
XrefUtils.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Reflection;
using HarmonyLib;
using MelonLoader;
using UnhollowerRuntimeLib.XrefScans;
namespace ReMod.Core
{
public static class XrefUtils
{
/// <summary>
/// Returns if a string is contained within the given method's body.
/// </summary>
/// <param name="method">The method to check</param>
/// <param name="match">The string to check</param>
public static bool CheckMethod(MethodInfo method, string match)
{
try
{
foreach (var instance in XrefScanner.XrefScan(method))
{
if (instance.Type == XrefType.Global && instance.ReadAsObject().ToString().Contains(match))
return true;
}
return false;
}
catch
{
// ignored
}
return false;
}
/// <summary>
/// Returns if the given method is called by the other given method.
/// </summary>
/// <param name="method">The method to check</param>
/// <param name="methodName">The name of the method that uses the given method</param>
/// <param name="type">The type of the method that uses the given method</param>
public static bool CheckUsedBy(MethodInfo method, string methodName, Type type = null)
{
foreach (var instance in XrefScanner.UsedBy(method))
{
if (instance.Type == XrefType.Method)
{
try
{
if ((type == null || instance.TryResolve().DeclaringType == type) && instance.TryResolve().Name.Contains(methodName))
return true;
}
catch
{
// ignored
}
}
}
return false;
}
/// <summary>
/// Returns whether the given method is using another the other given method.
/// </summary>
/// <param name="method">The method to check</param>
/// <param name="methodName">The name of the method that is used by the given method</param>
/// <param name="type">The type of the method that is used by the given method</param>
public static bool CheckUsing(MethodInfo method, string methodName, Type type = null)
{
foreach (var instance in XrefScanner.XrefScan(method))
{
if (instance.Type == XrefType.Method)
{
try
{
if ((type == null || instance.TryResolve().DeclaringType == type) && instance.TryResolve().Name.Contains(methodName))
return true;
}
catch
{
// ignored
}
}
}
return false;
}
public static void DumpXRefs(this Type type)
{
MelonLogger.Msg($"{type.Name} XRefs:");
foreach (var m in AccessTools.GetDeclaredMethods(type))
{
m.DumpXRefs(1);
}
}
public static void DumpXRefs(this MethodInfo method, int depth = 0)
{
var indent = new string('\t', depth);
MelonLogger.Msg($"{indent}{method.Name} XRefs:");
foreach (var x in XrefScanner.XrefScan(method))
{
if (x.Type == XrefType.Global)
{
MelonLogger.Msg($"\tString = {x.ReadAsObject()?.ToString()}");
}
else
{
var resolvedMethod = x.TryResolve();
if (resolvedMethod != null)
{
MelonLogger.Msg($"{indent}\tMethod -> {resolvedMethod.DeclaringType?.Name}.{resolvedMethod.Name}");
}
}
}
}
}
}