-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathBepInExPlugin.cs
386 lines (329 loc) · 15.2 KB
/
BepInExPlugin.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Reflection;
using Debug = UnityEngine.Debug;
namespace EquipMultipleUtilityItems
{
[BepInPlugin("aedenthorn.EquipMultipleUtilityItems", "Equip Multiple Utility Items", "0.6.0")]
public class BepInExPlugin : BaseUnityPlugin
{
private static BepInExPlugin context;
private Harmony harmony;
public static ConfigEntry<bool> modEnabled;
public static ConfigEntry<bool> isDebug;
public static ConfigEntry<int> nexusID;
public static ConfigEntry<int> maxEquippedItems;
public static void Dbgl(string str = "", bool pref = true)
{
if (isDebug.Value)
Debug.Log((pref ? typeof(BepInExPlugin).Namespace + " " : "") + str);
}
private void Awake()
{
context = this;
modEnabled = Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
isDebug = Config.Bind<bool>("General", "IsDebug", true, "Enable debug logs");
nexusID = Config.Bind<int>("General", "NexusID", 1348, "Nexus mod ID for updates");
nexusID.Value = 1348;
maxEquippedItems = Config.Bind<int>("Variables", "MaxEquippedItems", 5, "Maximum number of utility items equipped at once.");
if (!modEnabled.Value)
return;
harmony = new Harmony(Info.Metadata.GUID);
harmony.PatchAll();
}
[HarmonyPatch(typeof(Player), "UpdateMovementModifier")]
static class UpdateMovementModifier_Patch
{
static void Postfix(Player __instance, ref float ___m_equipmentMovementModifier, ItemDrop.ItemData ___m_utilityItem)
{
if (!modEnabled.Value)
return;
try
{
var list = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && i != ___m_utilityItem);
foreach (var item in list)
{
___m_equipmentMovementModifier += item.m_shared.m_movementModifier;
}
}
catch
{
//Dbgl($"Error: {Environment.StackTrace}");
}
}
}
//[HarmonyPatch(typeof(Player), "ApplyArmorDamageMods")]
static class ApplyArmorDamageMods_Patch
{
static void Postfix(Player __instance, ref HitData.DamageModifiers mods, ItemDrop.ItemData ___m_utilityItem)
{
if (!modEnabled.Value)
return;
try
{
var list = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && i != ___m_utilityItem);
foreach (var item in list)
{
mods.Apply(item.m_shared.m_damageModifiers);
}
}
catch
{
//Dbgl($"Error: {Environment.StackTrace}");
}
}
}
//[HarmonyPatch(typeof(Player), "GetBodyArmor")]
static class GetBodyArmor_Patch
{
static void Postfix(Player __instance, ref float __result)
{
if (!modEnabled.Value)
return;
try
{
var list = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility);
foreach (var item in list)
{
__result += item.GetArmor();
}
}
catch
{
//Dbgl($"Error: {Environment.StackTrace}");
}
}
}
[HarmonyPatch(typeof(Humanoid), nameof(Humanoid.IsItemEquiped))]
static class Humanoid_IsItemEquiped_Patch
{
static void Postfix(Humanoid __instance, ItemDrop.ItemData item, ItemDrop.ItemData ___m_utilityItem, ref bool __result)
{
if (!modEnabled.Value || __result)
return;
try
{
__result = item.m_equipped && item.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && item != ___m_utilityItem;
}
catch
{
//Dbgl($"Error: {Environment.StackTrace}");
}
}
}
[HarmonyPatch(typeof(Player), nameof(Player.GetEquipmentEitrRegenModifier))]
static class GetEquipmentEitrRegenModifier_Patch
{
static void Postfix(Player __instance, ItemDrop.ItemData ___m_utilityItem, ref float __result)
{
if (!modEnabled.Value)
return;
try
{
var list = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && i != ___m_utilityItem);
foreach (var item in list)
{
__result += item.m_shared.m_eitrRegenModifier;
}
}
catch
{
//Dbgl($"Error: {Environment.StackTrace}");
}
}
}
[HarmonyPatch(typeof(Player), "QueueEquipAction")]
static class QueueEquipItem_Patch
{
static bool Prefix(Player __instance, ItemDrop.ItemData item)
{
if (!modEnabled.Value || item == null || __instance.IsEquipActionQueued(item) || item.m_shared.m_itemType != ItemDrop.ItemData.ItemType.Utility)
return true;
try
{
var items = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility);
if (items.Exists(i => i.m_shared.m_name == item.m_shared.m_name))
return false;
if (items.Count >= maxEquippedItems.Value)
return false;
}
catch
{
Dbgl($"Error: {Environment.StackTrace}");
}
return true;
}
}
[HarmonyPatch(typeof(Humanoid), "EquipItem")]
static class EquipItem_Patch
{
static bool Prefix(Humanoid __instance, ItemDrop.ItemData item, bool triggerEquipEffects, Inventory ___m_inventory, ref bool __result, ref ItemDrop.ItemData ___m_utilityItem)
{
try
{
//Dbgl($"trying to equip item {item.m_shared.m_name}");
if (!modEnabled.Value || item == null || item.m_shared.m_itemType != ItemDrop.ItemData.ItemType.Utility || !__instance.IsPlayer() || !___m_inventory.ContainsItem(item) || __instance.InAttack() || __instance.InDodge() || (__instance.IsPlayer() && !__instance.IsDead() && __instance.IsSwimming() && !__instance.IsOnGround()) || (item.m_shared.m_useDurability && item.m_durability <= 0f) || (item.m_shared.m_dlc.Length > 0 && !DLCMan.instance.IsDLCInstalled(item.m_shared.m_dlc)))
return true;
//Dbgl($"can equip {item.m_shared.m_name}");
int count = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility).Count;
if (count >= maxEquippedItems.Value)
{
__result = false;
return false;
}
if (___m_utilityItem == null)
{
//Dbgl($"setting as utility item {item.m_shared.m_name}");
___m_utilityItem = item;
}
item.m_equipped = true;
typeof(Humanoid).GetMethod("SetupEquipment", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { });
if (triggerEquipEffects)
{
typeof(Humanoid).GetMethod("TriggerEquipEffect", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { item });
}
__result = true;
//Dbgl($"Equipped {item.m_shared.m_name}");
return false;
}
catch
{
Dbgl($"Error: {Environment.StackTrace}");
}
return true;
}
}
[HarmonyPatch(typeof(Humanoid), "UpdateEquipmentStatusEffects")]
static class UpdateEquipmentStatusEffects_Patch
{
static void Prefix(Humanoid __instance, ItemDrop.ItemData ___m_utilityItem, SEMan ___m_seman)
{
try
{
if (!modEnabled.Value || !__instance.IsPlayer())
return;
var list = __instance.GetInventory().GetAllItems().FindAll(i => !i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && i != ___m_utilityItem && i.m_shared.m_equipStatusEffect);
var list2 = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && i != ___m_utilityItem && i.m_shared.m_equipStatusEffect);
foreach (var item in list)
{
foreach (StatusEffect statusEffect in AccessTools.FieldRefAccess<SEMan, List<StatusEffect>>(___m_seman, "m_statusEffects"))
{
if (statusEffect.name == item.m_shared.m_equipStatusEffect.name && (___m_utilityItem is null || ___m_utilityItem.m_shared.m_equipStatusEffect.name != statusEffect.name) && !list2.Exists(i => i.m_shared.m_equipStatusEffect.name == statusEffect.name))
{
___m_seman.RemoveStatusEffect(statusEffect.NameHash(), false);
}
}
}
}
catch
{
//Dbgl($"Error: {Environment.StackTrace}");
}
}
static void Postfix(Humanoid __instance, ItemDrop.ItemData ___m_utilityItem, SEMan ___m_seman)
{
try
{
if (!modEnabled.Value || !__instance.IsPlayer())
return;
var list = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && i != ___m_utilityItem && i.m_shared.m_equipStatusEffect);
foreach (var item in list)
{
___m_seman.AddStatusEffect(item.m_shared.m_equipStatusEffect, false);
}
//Dbgl($"added {list.Count} effects");
}
catch
{
//Dbgl($"Error: {Environment.StackTrace}");
}
}
}
[HarmonyPatch(typeof(Humanoid), "UnequipAllItems")]
static class UnequipAllItems_Patch
{
static void Postfix(Humanoid __instance, ItemDrop.ItemData ___m_utilityItem)
{
try
{
if (!modEnabled.Value || !__instance.IsPlayer())
return;
var list = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && i != ___m_utilityItem);
foreach (ItemDrop.ItemData item in list)
__instance.UnequipItem(item, false);
}
catch
{
Dbgl($"Error: {Environment.StackTrace}");
}
}
}
[HarmonyPatch(typeof(Player), nameof(Player.UnequipDeathDropItems))]
static class UnequipDeathDropItems_PatchUnequipItem
{
static void Postfix(Player __instance, ItemDrop.ItemData ___m_utilityItem)
{
try
{
if (!modEnabled.Value)
return;
var list = __instance.GetInventory().GetAllItems().FindAll(i => i.m_equipped && i.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Utility && i != ___m_utilityItem);
foreach (ItemDrop.ItemData item in list)
__instance.UnequipItem(item, false);
}
catch
{
Dbgl($"Error: {Environment.StackTrace}");
}
}
}
[HarmonyPatch(typeof(ItemDrop.ItemData), "GetTooltip", new Type[] { typeof(ItemDrop.ItemData), typeof(int), typeof(bool) , typeof(float) })]
static class GetTooltip_Patch
{
static void Postfix(ref ItemDrop.ItemData item, int qualityLevel, float worldLevel, ref string __result)
{
try
{
if (!modEnabled.Value || item.m_shared.m_itemType != ItemDrop.ItemData.ItemType.Utility)
return;
__result += string.Format("\n\n$item_armor: <color=orange>{0}</color>", item.GetArmor(qualityLevel, worldLevel));
if (item.m_shared.m_damageModifiers.Count > 0)
__result += SE_Stats.GetDamageModifiersTooltipString(item.m_shared.m_damageModifiers);
}
catch
{
//Dbgl($"Error: {Environment.StackTrace}");
}
}
}
[HarmonyPatch(typeof(Terminal), "InputText")]
static class InputText_Patch
{
static bool Prefix(Terminal __instance)
{
if (!modEnabled.Value)
return true;
try
{
string text = __instance.m_input.text;
if (text.ToLower().Equals($"{typeof(BepInExPlugin).Namespace.ToLower()} reset"))
{
context.Config.Reload();
context.Config.Save();
__instance.AddString(text);
__instance.AddString($"{context.Info.Metadata.Name} config reloaded");
return false;
}
}
catch
{
Dbgl($"Error: {Environment.StackTrace}");
}
return true;
}
}
}
}