1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
ILHook ilHook = new ILHook(onMethodinfo, ilBody =>
{
//进行IL编辑
ILCursor ilCursor = new ILCursor(il); //获取IL指针
if(ilCursor.TryGotoNext( //尝试跳转到指定目标位置之后
MoveType.After,
x => x.MatchLdloc0(),
x => x.MatchLdcI4(out _)))
{
//跳转完后还得加1,因为当前指针位置是那个Label也就是跳转
ilCursor.Index++;
//观察IL发现,if语句有两个Lable,那么两个Lable跳转的地方应该是相同的,我们直接copy
ILLabel ilLabel = il.Labels[0];
//将局部变量压入计算栈(索引0)
ilCursor.Emit(OpCodes.Ldloc_0);
//将方法传入的参数压入计算栈(索引1) 由于他是结构体,需要使用 Ldarga
ilCursor.Emit(OpCodes.Ldarga, 1);
ilCursor.Emit(OpCodes.Call, typeof(ReadOnlySpan<GlobalNPC>).GetProperty("Length").GetMethod);
ilCursor.Emit(OpCodes.Bge, ilLabel); //如果第一个值大于或等于第二个值,则将控制转移到目标指令。
}
});
|