-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathItemGrabMenuPatches.cs
232 lines (207 loc) · 7.83 KB
/
ItemGrabMenuPatches.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using HarmonyLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nanoray.Shrike;
using Nanoray.Shrike.Harmony;
using Shockah.Kokoro;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Menus;
using StardewValley.Objects;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
namespace Shockah.JunimoWarp;
internal class ItemGrabMenuPatches
{
private const int WarpButtonID = 1567601; // {NexusID}01
private static ModEntry Instance
=> ModEntry.Instance;
private static readonly ConditionalWeakTable<ItemGrabMenu, ClickableTextureComponent> WarpButtons = [];
internal static void Apply(Harmony harmony)
{
harmony.TryPatch(
monitor: Instance.Monitor,
original: () => AccessTools.Method(typeof(ItemGrabMenu), nameof(ItemGrabMenu.RepositionSideButtons)),
transpiler: new HarmonyMethod(typeof(ItemGrabMenuPatches), nameof(RepositionSideButtons_Transpiler))
);
harmony.TryPatch(
monitor: Instance.Monitor,
original: () => AccessTools.Method(typeof(ItemGrabMenu), nameof(ItemGrabMenu.draw), [typeof(SpriteBatch)]),
transpiler: new HarmonyMethod(typeof(ItemGrabMenuPatches), nameof(draw_Transpiler))
);
harmony.TryPatch(
monitor: Instance.Monitor,
original: () => AccessTools.Method(typeof(ItemGrabMenu), nameof(ItemGrabMenu.performHoverAction)),
postfix: new HarmonyMethod(typeof(ItemGrabMenuPatches), nameof(performHoverAction_Postfix)),
transpiler: new HarmonyMethod(typeof(ItemGrabMenuPatches), nameof(performHoverAction_Transpiler))
);
harmony.TryPatch(
monitor: Instance.Monitor,
original: () => AccessTools.Method(typeof(ItemGrabMenu), nameof(ItemGrabMenu.receiveLeftClick)),
postfix: new HarmonyMethod(typeof(ItemGrabMenuPatches), nameof(receiveLeftClick_Postfix))
);
harmony.TryPatch(
monitor: Instance.Monitor,
original: () => AccessTools.Method(typeof(IClickableMenu), nameof(IClickableMenu.populateClickableComponentList)),
postfix: new HarmonyMethod(typeof(ItemGrabMenuPatches), nameof(IClickableMenu_populateClickableComponentList_Postfix))
);
}
public static ClickableTextureComponent ObtainWarpButton(ItemGrabMenu menu)
{
if (!WarpButtons.TryGetValue(menu, out var button))
{
button = new ClickableTextureComponent("", new Rectangle(menu.xPositionOnScreen + menu.width, menu.yPositionOnScreen + menu.height / 3 - 64, 64, 64), "", Instance.Helper.Translation.Get("junimoWarp.tooltip"), Game1.mouseCursors, new Rectangle(108, 491, 16, 16), 4f)
{
myID = WarpButtonID,
leftNeighborID = 53912,
region = 15923
};
WarpButtons.AddOrUpdate(menu, button);
}
return button;
}
private static IEnumerable<CodeInstruction> RepositionSideButtons_Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase originalMethod)
{
try
{
return new SequenceBlockMatcher<CodeInstruction>(instructions)
.Find(
ILMatches.Newobj(AccessTools.DeclaredConstructor(typeof(List<ClickableComponent>), [])),
ILMatches.Stloc<List<ClickableComponent>>(originalMethod).CreateLdlocInstruction(out var ldlocSideButtons)
)
.Insert(
SequenceMatcherPastBoundsDirection.After, SequenceMatcherInsertionResultingBounds.JustInsertion,
new CodeInstruction(OpCodes.Ldarg_0),
ldlocSideButtons,
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ItemGrabMenuPatches), nameof(RepositionSideButtons_Transpiler_ModifySideButtons)))
)
.AllElements();
}
catch (Exception ex)
{
Instance.Monitor.Log($"Could not patch methods - {Instance.ModManifest.Name} probably won't work.\nReason: {ex}", LogLevel.Error);
return instructions;
}
}
public static void RepositionSideButtons_Transpiler_ModifySideButtons(ItemGrabMenu menu, List<ClickableComponent> sideButtons)
{
if (menu.context is not Chest chest)
return;
if (chest.SpecialChestType != Chest.SpecialChestTypes.JunimoChest)
return;
sideButtons.Add(ObtainWarpButton(menu));
}
private static IEnumerable<CodeInstruction> draw_Transpiler(IEnumerable<CodeInstruction> instructions)
{
try
{
return new SequenceBlockMatcher<CodeInstruction>(instructions)
.Find(
ILMatches.Ldarg(0),
ILMatches.Ldfld(AccessTools.Field(typeof(MenuWithInventory), nameof(MenuWithInventory.hoverText))),
ILMatches.Brfalse
)
.PointerMatcher(SequenceMatcherRelativeElement.First)
.ExtractLabels(out var labels)
.Insert(
SequenceMatcherPastBoundsDirection.Before, SequenceMatcherInsertionResultingBounds.JustInsertion,
new CodeInstruction(OpCodes.Ldarg_0).WithLabels(labels),
new CodeInstruction(OpCodes.Ldarg_1),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ItemGrabMenuPatches), nameof(draw_Transpiler_DrawWarpButton)))
)
.AllElements();
}
catch (Exception ex)
{
Instance.Monitor.Log($"Could not patch methods - {Instance.ModManifest.Name} probably won't work.\nReason: {ex}", LogLevel.Error);
return instructions;
}
}
public static void draw_Transpiler_DrawWarpButton(ItemGrabMenu menu, SpriteBatch b)
{
if (menu.context is not Chest chest)
return;
if (chest.SpecialChestType != Chest.SpecialChestTypes.JunimoChest)
return;
ObtainWarpButton(menu).draw(b);
}
private static IEnumerable<CodeInstruction> performHoverAction_Transpiler(IEnumerable<CodeInstruction> instructions)
{
try
{
return new SequenceBlockMatcher<CodeInstruction>(instructions)
.Find(
ILMatches.Ldarg(0),
ILMatches.Ldarg(1),
ILMatches.Ldarg(2),
ILMatches.Call("performHoverAction")
)
.Insert(
SequenceMatcherPastBoundsDirection.After, SequenceMatcherInsertionResultingBounds.JustInsertion,
new CodeInstruction(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Ldarg_1),
new CodeInstruction(OpCodes.Ldarg_2),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ItemGrabMenuPatches), nameof(performHoverAction_Transpiler_TryHoverWarpButton)))
)
.AllElements();
}
catch (Exception ex)
{
Instance.Monitor.Log($"Could not patch methods - {Instance.ModManifest.Name} probably won't work.\nReason: {ex}", LogLevel.Error);
return instructions;
}
}
public static void performHoverAction_Transpiler_TryHoverWarpButton(ItemGrabMenu menu, int x, int y)
{
if (menu.context is not Chest chest)
return;
if (chest.SpecialChestType != Chest.SpecialChestTypes.JunimoChest)
return;
ObtainWarpButton(menu).tryHover(x, y, 0.25f);
}
private static void performHoverAction_Postfix(ItemGrabMenu __instance, int x, int y)
{
if (__instance.context is not Chest chest)
return;
if (chest.SpecialChestType != Chest.SpecialChestTypes.JunimoChest)
return;
var button = ObtainWarpButton(__instance);
button.tryHover(x, y);
if (button.containsPoint(x, y))
__instance.hoverText = button.hoverText;
}
private static void receiveLeftClick_Postfix(ItemGrabMenu __instance, int x, int y)
{
if (__instance.context is not Chest chest)
return;
if (chest.SpecialChestType != Chest.SpecialChestTypes.JunimoChest)
return;
var button = ObtainWarpButton(__instance);
if (button.containsPoint(x, y))
{
Game1.exitActiveMenu();
if (Instance.Config.RequiredEmptyChest)
{
foreach (var item in __instance.ItemsToGrabMenu.actualInventory)
if (item is not null && item.Stack > 0)
{
Kokoro.ModEntry.Instance.QueueObjectDialogue(Instance.Helper.Translation.Get("junimoWarp.notEmpty.message"));
return;
}
}
Instance.RequestNextWarp(Game1.player.currentLocation, new((int)chest.TileLocation.X, (int)chest.TileLocation.Y), (warpLocation, warpPoint) =>
{
ModEntry.AnimatePlayerWarp(warpLocation, warpPoint);
});
}
}
private static void IClickableMenu_populateClickableComponentList_Postfix(IClickableMenu __instance)
{
if (__instance is not ItemGrabMenu menu)
return;
menu.allClickableComponents.Add(ObtainWarpButton(menu));
}
}