forked from SignatureBeef/Open-Terraria-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookChestQuickStack.Server.cs
More file actions
271 lines (235 loc) · 11.2 KB
/
HookChestQuickStack.Server.cs
File metadata and controls
271 lines (235 loc) · 11.2 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Copyright (C) 2020 DeathCradle
This file is part of Open Terraria API v3 (OTAPI)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma warning disable CS0436 // Type conflicts with imported type
using ModFramework;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoMod;
using MonoMod.Cil;
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.GameContent;
using Terraria.ID;
/// <summary>
/// @doc Creates Hooks.Chest.QuickStack.
/// </summary>
[MonoModIgnore]
partial class ChestHooks
{
static MethodDefinition PutItemInNearbyChest { get; set; }
static ParameterDefinition PlayerID { get; set; }
[Modification(ModType.PreMerge, "Hooking chest stacking")]
static void HookChestQuickStack(ModFwModder modder)
{
#if TerrariaServer_1450_OrAbove || Terraria__1450_OrAbove || tModLoader_1450_OrAbove
{
// DO NOT use GetILCursor(). The instruction body does not have jumps transformed into labels,
// a process which is required to happen for the edits below to work.
var ctx = new ILContext(modder.GetMethodDefinition(() => QuickStacking.BuildDestinationMetricsAndStackItems(default, default, default)));
// Hooks onto quick stacking into existing slot
ctx.Invoke((ctx) =>
{
var csr = new ILCursor(ctx);
ILLabel endLabel = null!;
csr.GotoNext(
MoveType.After,
i => i.MatchLdarg(1),
i => i.MatchLdcI4(1),
i => i.MatchStfld(typeof(QuickStacking.DestinationHelper), nameof(QuickStacking.DestinationHelper.transferBlocked)),
i => i.MatchBr(out endLabel)
);
// AfterLabel is not the same as After + MoveAfterLabels for some reason
csr.MoveAfterLabels();
// Load player ID (source.slots[0].Player.whoAmI)
// source
csr.Emit(OpCodes.Ldarg_0);
// ^.slots
csr.Emit(OpCodes.Ldfld, modder.GetFieldDefinition(() => default(QuickStacking.SourceInventory).slots));
// ^[0]
csr.Emit(OpCodes.Ldc_I4_0);
csr.Emit(OpCodes.Ldelem_Any, modder.GetDefinition<PlayerItemSlotID.SlotReference>());
// ^.Player
csr.Emit(OpCodes.Ldfld, modder.GetFieldDefinition(() => default(PlayerItemSlotID.SlotReference).Player));
// ^.whoAmI
csr.Emit(OpCodes.Ldfld, modder.GetFieldDefinition(() => default(Player)!.whoAmI));
// Load item
// NOTE: this is very fragile, but I can't think of a way to write it better without significantly bloating the code
csr.Emit(OpCodes.Ldloc_S, (byte)3);
// Load chest index
csr.Emit(OpCodes.Ldarg_S, (byte)1);
// ^.ChestIndex (property get)
csr.Emit(OpCodes.Callvirt, modder.GetDefinition<QuickStacking.DestinationHelper>().Properties.Single(p => p.Name == "ChestIndex")!.GetMethod);
// Call hook
csr.Emit(OpCodes.Call, modder.GetMethodDefinition(() => OTAPI.Hooks.Chest.InvokeQuickStack(default, default!, default)));
// Continue if handled
csr.Emit(OpCodes.Brfalse, endLabel);
});
}
{
// used for the out parameter only
List<int> _blockedChests;
// DO NOT use GetILCursor(). The instruction body does not have jumps transformed into labels,
// a process which is required to happen for the edits below to work.
var ctx = new ILContext(modder.GetMethodDefinition(() => QuickStacking.Transfer(default, default, out _blockedChests, default)));
// Hooks onto quick stacking overflowing into a new stack
ctx.Invoke((ctx) =>
{
var csr = new ILCursor(ctx);
int count = 0;
while (csr.TryGotoNext(
MoveType.Before,
i => i.MatchLdarg(0),
i => i.MatchLdloc(out _),
i => i.MatchCall(typeof(QuickStacking), nameof(QuickStacking.Consolidate))
))
{
if (++count > 2)
{
throw new Exception($"More than two matches of {nameof(QuickStacking.Consolidate)}.");
}
// Both targets are preceded by a jump instruction exiting the loop
var endInstruction = csr.Prev.Operand;
csr.MoveAfterLabels();
// Load player ID (source.slots[0].Player.whoAmI)
// source
csr.Emit(OpCodes.Ldarg_0);
// ^.slots
csr.Emit(OpCodes.Ldfld, modder.GetFieldDefinition(() => default(QuickStacking.SourceInventory).slots));
// ^[0]
csr.Emit(OpCodes.Ldc_I4_0);
csr.Emit(OpCodes.Ldelem_Any, modder.GetDefinition<PlayerItemSlotID.SlotReference>());
// ^.Player
csr.Emit(OpCodes.Ldfld, modder.GetFieldDefinition(() => default(PlayerItemSlotID.SlotReference).Player));
// ^.whoAmI
csr.Emit(OpCodes.Ldfld, modder.GetFieldDefinition(() => default(Player)!.whoAmI));
// Load item
// NOTE: this is very fragile, but I can't think of a way to write it better without significantly bloating the code
csr.Emit(OpCodes.Ldloc_S, (byte)(count == 1 ? 6 : 9));
// Load chest index
// NOTE: this is very fragile, but I can't think of a way to write it better without significantly bloating the code
csr.Emit(OpCodes.Ldloc_S, (byte)(count == 1 ? 7 : 10));
// ^.ChestIndex (property get)
csr.Emit(OpCodes.Callvirt, modder.GetDefinition<QuickStacking.DestinationHelper>().Properties.Single(p => p.Name == "ChestIndex")!.GetMethod);
// Call hook
csr.Emit(OpCodes.Call, modder.GetMethodDefinition(() => OTAPI.Hooks.Chest.InvokeQuickStack(default, default!, default)));
// Continue if handled
csr.Emit(OpCodes.Brfalse, endInstruction);
// Move after Consolidate call
csr.Goto(csr.Index + 3);
}
});
}
#else
var csr = modder.GetILCursor(() => Terraria.Chest.PutItemInNearbyChest(null, default));
PutItemInNearbyChest = csr.Method;
modder.OnRewritingMethodBody += Modder_OnRewritingMethodBody;
// add playerID for the hook to consume, then we need to rewrite all the uses of it to add it in.
PutItemInNearbyChest.Parameters.Add(PlayerID = new ParameterDefinition(
"playerID",
ParameterAttributes.None,
modder.Module.TypeSystem.Int32
));
// inject the hook
{
#if TerrariaServer_1441_OrAbove
var beginInstruction = csr.Method.Body.Instructions.Single(x => x.OpCode == OpCodes.Bge);
#else
var beginInstruction = csr.Method.Body.Instructions.Single(x => x.OpCode == OpCodes.Bge_Un);
#endif
var endInstruction = beginInstruction.Next(x => x.OpCode == OpCodes.Ldc_I4_0);
csr.Goto(beginInstruction, MoveType.After);
csr.EmitAll(
new { OpCodes.Ldarg, Operand = csr.Method.Parameters.Skip(2).SingleOrDefault() },
new { OpCodes.Ldarg, Operand = csr.Method.Parameters.First() },
#if TerrariaServer_1441_OrAbove
new { OpCodes.Ldloc_1 },
#else
new { OpCodes.Ldloc_0 },
#endif
new { OpCodes.Call, Operand = modder.GetMethodDefinition(() => OTAPI.Hooks.Chest.InvokeQuickStack(0, null, 0)) },
new { OpCodes.Brtrue, endInstruction },
new { OpCodes.Br, Operand = (Instruction)beginInstruction.Operand }
);
}
#endif
}
private static void Modder_OnRewritingMethodBody(MonoModder modder, MethodBody body, Instruction instr, int instri)
{
if (instr.Operand is MethodReference methodReference)
{
if (methodReference.DeclaringType.Name == PutItemInNearbyChest.DeclaringType.Name
&& methodReference.Name == PutItemInNearbyChest.Name)
{
if (methodReference.Parameters.Any(x => x.Name == PlayerID.Name))
{
return;
}
methodReference.Parameters.Add(PlayerID);
switch (body.Method.Name)
{
case "ServerPlaceItem":
// add the player id from the first argument
body.GetILProcessor().InsertBefore(instr, new
{
OpCodes.Ldarg_0
});
break;
case "QuickStackAllChests":
body.GetILProcessor().InsertBefore(instr,
new { OpCodes.Ldarg_0, },
#if !tModLoaderServer_V1_3
new { OpCodes.Ldfld, Operand = (FieldReference)modder.GetFieldDefinition(() => (new Terraria.Player()).whoAmI) }
#else
new { OpCodes.Ldfld, Operand = (FieldReference)modder.GetFieldDefinition(() => (new Terraria.Player(true)).whoAmI) }
#endif
);
break;
default:
throw new NotImplementedException($"{body.Method.Name} is not a supported caller for this modification");
}
}
}
}
}
namespace OTAPI
{
public static partial class Hooks
{
public static partial class Chest
{
public class QuickStackEventArgs : EventArgs
{
public HookResult? Result { get; set; }
public int PlayerId { get; set; }
public Terraria.Item Item { get; set; }
public int ChestIndex { get; set; }
}
public static event EventHandler<QuickStackEventArgs> QuickStack;
public static bool InvokeQuickStack(int playerId, Terraria.Item item, int chestIndex)
{
var args = new QuickStackEventArgs()
{
PlayerId = playerId,
Item = item,
ChestIndex = chestIndex,
};
QuickStack?.Invoke(null, args);
return args.Result != HookResult.Cancel;
}
}
}
}