@@ -28,36 +28,59 @@ private static void HackTileMapGet()
2828
2929 /// <summary>
3030 /// 对 Tilemap 的 get_Item 方法进行 IL(中间语言)修改的方法。
31- /// 根据 WorldData.nowGenerating 的值决定返回不同的 Tile 数据。
31+ /// 根据 WorldData.nowGenerating 的值以及调用者是否为 WorldGen 类,决定返回不同的 Tile 数据。
3232 /// </summary>
3333 /// <param name="il">IL 上下文对象,用于操作中间语言指令。</param>
3434 private static void ILTileMapGet ( ILContext il )
3535 {
3636 // 创建一个 ILCursor 实例,用于在 IL 代码中移动和插入指令
3737 var cursor = new ILCursor ( il ) ;
38- cursor . EmitNop ( ) ;
39- var label_now = cursor . MarkLabel ( ) ;
40- cursor . GotoPrev ( MoveType . Before , i => i . MatchNop ( ) ) ;
4138
4239 // 发射一个委托调用指令,执行委托方法并将返回值压入栈中
43- // 该委托检查 WorldData.nowGenerating 是否等于 0
40+ // 该委托检查是否由 WorldGen 类调用,如果是,则返回 true,否则返回 false
4441 cursor . EmitDelegate ( ( ) =>
4542 {
4643 if ( WorldData . nowGenerating == 0 )
4744 {
4845 return false ;
4946 }
47+ // throw new Exception("Invalid nowGenerating");
48+ // 获取调用栈信息,跳过当前方法和调用当前方法的方法
5049 var stacktrace = new StackTrace ( 2 , false ) ;
51- return stacktrace . GetFrame ( 0 ) . GetType ( ) == typeof ( WorldGen ) ;
50+ // 检查调用者的方法所属的类型是否为 WorldGen 类
51+ return stacktrace . GetFrame ( 0 ) . GetMethod ( ) . DeclaringType == typeof ( WorldGen ) ;
5252 } ) ;
53- cursor . EmitBrfalse ( label_now ) ;
5453
54+ // 保存当前光标下一条指令的引用,后续用于跳转回来
55+ var tmp = cursor . Next ;
56+
57+ // 将光标移动到原有代码之后
58+ cursor . Goto ( cursor . Instrs . Count - 1 , MoveType . After ) ;
59+
60+ // 插入一个空操作指令(NOP),用于标记位置
61+ cursor . EmitNop ( ) ;
62+
63+ // 将光标移动到上一个 NOP 指令之前
64+ cursor . GotoPrev ( MoveType . Before , i => i . MatchNop ( ) ) ;
65+
66+ // 标记当前位置,创建一个标签,指示WorldGen相关代码的起始位置
67+ var label_now = cursor . MarkLabel ( ) ;
68+
69+ // 将光标移动到else分支里面,原有代码的前面
70+ cursor . Goto ( tmp , MoveType . Before ) ;
71+
72+ // 如果是WorldGen的调用,则跳转到WorldGen相关的代码
73+ cursor . EmitBrtrue ( label_now ) ;
74+
75+ // 移动到原有代码的后面,即新的WorldGen逻辑开头
76+ cursor . Goto ( cursor . Instrs . Count - 1 , MoveType . After ) ;
77+
78+ // 接下来是WorldGen相关的新逻辑
5579 // 加载方法的 x 坐标到栈上
5680 cursor . EmitLdarg1 ( ) ;
5781 // 加载方法的 y 坐标到栈上
5882 cursor . EmitLdarg2 ( ) ;
5983
60- // 发射一个委托调用指令,执行委托方法并将返回值压入栈中
6184 // 该委托根据 WorldData.nowGenerating、x 和 y 坐标从 WorldData.MapData 中获取 Tile 数据
6285 cursor . EmitDelegate ( ( int x , int y ) =>
6386 {
@@ -70,4 +93,4 @@ private static void ILTileMapGet(ILContext il)
7093 // throw new Exception(il.ToString());
7194 }
7295 }
73- }
96+ }
0 commit comments