-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventPatches.cs
More file actions
272 lines (255 loc) · 8.41 KB
/
Copy pathEventPatches.cs
File metadata and controls
272 lines (255 loc) · 8.41 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
272
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using HarmonyLib;
using MegaCrit.Sts2.Core.Commands;
using MegaCrit.Sts2.Core.Entities.Cards;
using MegaCrit.Sts2.Core.Entities.Creatures;
using MegaCrit.Sts2.Core.Entities.Players;
using MegaCrit.Sts2.Core.Entities.Powers;
using MegaCrit.Sts2.Core.Hooks;
using MegaCrit.Sts2.Core.Models;
using MegaCrit.Sts2.Core.Nodes.CommonUi;
using MegaCrit.Sts2.Core.Platform;
using MegaCrit.Sts2.Core.Runs;
using Sts2Agent.Utilities;
namespace Sts2Agent;
[HarmonyPatch(typeof(Hook), nameof(Hook.AfterDamageReceived))]
public static class DamageReceivedPatch
{
[HarmonyPostfix]
public static void Postfix(Creature target, DamageResult result, Creature? dealer)
{
try
{
var targetName = TextHelper.StripBBCode(target.Name);
var dealerName = dealer != null ? TextHelper.StripBBCode(dealer.Name) : "unknown";
var blocked = result.BlockedDamage;
var hpLoss = result.UnblockedDamage;
var total = result.TotalDamage;
string message;
if (dealer != null)
message = $"{dealerName} dealt {total} damage to {targetName} ({blocked} blocked, {hpLoss} HP lost)";
else
message = $"{targetName} took {total} damage ({blocked} blocked, {hpLoss} HP lost)";
EventLog.Add("damage_received", message, new Dictionary<string, object>
{
["target"] = targetName,
["dealer"] = dealerName,
["total_damage"] = total,
["blocked"] = blocked,
["hp_loss"] = hpLoss,
["was_kill"] = result.WasTargetKilled
});
}
catch (Exception e)
{
Plugin.LogError($"Error in DamageReceivedPatch: {e}");
}
}
}
[HarmonyPatch(typeof(Hook), nameof(Hook.AfterDeath))]
public static class DeathPatch
{
[HarmonyPostfix]
public static void Postfix(Creature creature)
{
try
{
var name = TextHelper.StripBBCode(creature.Name);
var isEnemy = creature.IsMonster;
EventLog.Add("creature_died", $"{name} died", new Dictionary<string, object>
{
["creature"] = name,
["is_enemy"] = isEnemy
});
}
catch (Exception e)
{
Plugin.LogError($"Error in DeathPatch: {e}");
}
}
}
[HarmonyPatch(typeof(Hook), nameof(Hook.BeforeCardAutoPlayed))]
public static class CardAutoPlayedPatch
{
[HarmonyPostfix]
public static void Postfix(CardModel card, Creature? target, AutoPlayType type)
{
try
{
var cardName = TextHelper.StripBBCode(card.Title);
var targetName = target != null ? TextHelper.StripBBCode(target.Name) : null;
var message = targetName != null
? $"{cardName} auto-played on {targetName}"
: $"{cardName} auto-played";
EventLog.Add("card_auto_played", message, new Dictionary<string, object>
{
["card"] = cardName,
["target"] = targetName ?? "",
["auto_play_type"] = type.ToString()
});
}
catch (Exception e)
{
Plugin.LogError($"Error in CardAutoPlayedPatch: {e}");
}
}
}
[HarmonyPatch(typeof(Hook), nameof(Hook.AfterPowerAmountChanged))]
public static class PowerChangedPatch
{
[HarmonyPostfix]
public static void Postfix(PowerModel power, decimal amount, Creature? applier)
{
try
{
var ownerName = TextHelper.StripBBCode(power.Owner.Name);
var powerName = TextHelper.SafeLocString(() => power.Title);
var applierName = applier != null ? TextHelper.StripBBCode(applier.Name) : null;
var powerType = power.Type;
var intAmount = (int)amount;
string message;
if (applierName != null)
message = $"{applierName} applied {intAmount} {powerName} to {ownerName}";
else
message = $"{ownerName} gained {intAmount} {powerName}";
EventLog.Add("power_changed", message, new Dictionary<string, object>
{
["owner"] = ownerName,
["power"] = powerName,
["amount"] = intAmount,
["new_amount"] = power.Amount,
["applier"] = applierName ?? "",
["type"] = powerType.ToString()
});
}
catch (Exception e)
{
Plugin.LogError($"Error in PowerChangedPatch: {e}");
}
}
}
[HarmonyPatch(typeof(CardCmd), nameof(CardCmd.Upgrade), typeof(IEnumerable<CardModel>), typeof(CardPreviewStyle))]
public static class CardUpgradePatch
{
[HarmonyPrefix]
public static void Prefix(IEnumerable<CardModel> cards, out List<(CardModel card, string oldName)>? __state)
{
try
{
__state = new List<(CardModel, string)>();
foreach (var card in cards)
{
if (card.IsUpgradable)
__state.Add((card, TextHelper.StripBBCode(card.Title)));
}
}
catch (Exception e)
{
__state = null;
Plugin.LogError($"Error in CardUpgradePatch.Prefix: {e}");
}
}
[HarmonyPostfix]
public static void Postfix(List<(CardModel card, string oldName)>? __state)
{
try
{
if (__state == null) return;
foreach (var (card, oldName) in __state)
{
var newName = TextHelper.StripBBCode(card.Title);
EventLog.Add("card_upgraded", $"{oldName} upgraded to {newName}", new Dictionary<string, object>
{
["old_name"] = oldName,
["new_name"] = newName
});
}
}
catch (Exception e)
{
Plugin.LogError($"Error in CardUpgradePatch.Postfix: {e}");
}
}
}
[HarmonyPatch(typeof(Hook), nameof(Hook.BeforeCardRemoved))]
public static class CardRemovedPatch
{
[HarmonyPostfix]
public static void Postfix(CardModel card)
{
try
{
var cardName = TextHelper.StripBBCode(card.Title);
EventLog.Add("card_removed", $"{cardName} removed from deck", new Dictionary<string, object>
{
["card"] = cardName
});
}
catch (Exception e)
{
Plugin.LogError($"Error in CardRemovedPatch: {e}");
}
}
}
[HarmonyPatch(typeof(RelicModel), nameof(RelicModel.Flash), new Type[0])]
public static class RelicActivatedPatch
{
[HarmonyPostfix]
public static void Postfix(RelicModel __instance)
{
try
{
var relicName = TextHelper.SafeLocString(() => __instance.Title);
EventLog.Add("relic_activated", $"{relicName} activated", new Dictionary<string, object>
{
["relic"] = relicName
});
}
catch (Exception e)
{
Plugin.LogError($"Error in RelicActivatedPatch: {e}");
}
}
}
[HarmonyPatch(typeof(RelicCmd), nameof(RelicCmd.Obtain), typeof(RelicModel), typeof(Player), typeof(int))]
public static class RelicObtainedPatch
{
[HarmonyPostfix]
public static void Postfix(RelicModel relic)
{
try
{
var relicName = TextHelper.SafeLocString(() => relic.Title);
EventLog.Add("relic_obtained", $"{TextHelper.StripBBCode(PlatformUtil.GetPlayerNameRaw(RunManager.Instance.NetService.Platform, relic.Owner.NetId))} Obtained relic: {relicName}", new Dictionary<string, object>
{
["relic"] = relicName,
});
}
catch (Exception e)
{
Plugin.LogError($"Error in RelicObtainedPatch: {e}");
}
}
}
[HarmonyPatch(typeof(RelicCmd), nameof(RelicCmd.Remove))]
public static class RelicRemovedPatch
{
[HarmonyPostfix]
public static void Postfix(RelicModel relic)
{
try
{
var relicName = TextHelper.SafeLocString(() => relic.Title);
EventLog.Add("relic_removed", $"Lost relic: {relicName}", new Dictionary<string, object>
{
["relic"] = relicName
});
}
catch (Exception e)
{
Plugin.LogError($"Error in RelicRemovedPatch: {e}");
}
}
}