-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTwaila.cs
67 lines (53 loc) · 2.08 KB
/
Twaila.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
using Microsoft.Xna.Framework;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using Terraria;
using Terraria.ModLoader;
using Twaila.ObjectData;
namespace Twaila;
public class Twaila : Mod
{
public static Twaila Instance => ModContent.GetInstance<Twaila>();
public override void Load()
{
ExtraObjectData.Initialize();
IL_Main.DrawMap += IL_Main_DrawMap;
}
private void IL_Main_DrawMap(MonoMod.Cil.ILContext il)
{
var cursor = new ILCursor(il);
int mapXLocalIndex = -1;
int mapYLocalIndex = -1;
int flag2LocalIndex = -1;
cursor.DefineLabel();
bool preSuccess = cursor.TryGotoNext(MoveType.After,
x => x.MatchStloc(out flag2LocalIndex), // stloc.s flag2
x => x.MatchLdloc(flag2LocalIndex), // ldloc.s flag2
x => x.MatchBrtrue(out _)); // brtrue
bool postSuccess = cursor.TryGotoNext(MoveType.Before,
x => x.MatchLdsfld(typeof(Main), nameof(Main.Map)), // ldsfld Main::Map
x => x.MatchLdloc(out mapXLocalIndex), // ldloc.s num89
x => x.MatchLdloc(out mapYLocalIndex)); // ldloc.s num90
bool validIndexes = mapXLocalIndex != -1 && mapYLocalIndex != -1 && flag2LocalIndex != -1;
if (!preSuccess || !postSuccess || !validIndexes)
{
Logger.Warn("Failed to IL edit Main.DrawMap");
return;
}
MonoModHooks.DumpIL(this, il);
cursor.Emit(OpCodes.Ldloc, mapXLocalIndex); // ldloc.s num89
cursor.Emit(OpCodes.Ldloc, mapYLocalIndex); // ldloc.s num90
cursor.EmitDelegate(StealMapPointFromVanilla);
}
public static void StealMapPointFromVanilla(int x, int y)
{
if (Main.LocalPlayer.TryGetModPlayer<TwailaPlayer>(out var player))
{
player.MapTilePos = new Point(x, y);
}
}
public override void Unload()
{
ExtraObjectData.Unload();
}
}