-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGamePatch.cs
More file actions
513 lines (419 loc) · 18.7 KB
/
GamePatch.cs
File metadata and controls
513 lines (419 loc) · 18.7 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
using HarmonyLib;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
namespace PocketCartPlus
{
//for adding UI hint to deposit items
[HarmonyPatch(typeof(PhysGrabCart), nameof(PhysGrabCart.StateMessages))]
public class CartMessagePatch
{
internal static Color hintColor = new(220f / 255f, 204f / 255f, 188f / 255f);
public static void Postfix(PhysGrabCart __instance)
{
if(!PlayerAvatar.instance.physGrabber.grabbed && HintUI.instance.grabHint)
HintUI.instance.grabHint = false;
if (!__instance.physGrabObject.grabbedLocal || !ModConfig.AllowDeposit.Value)
return;
if (!GetPocketCarts.AllSmallCarts.Contains(__instance))
return;
if (!HostValues.KeepNoUpgrade.Value && !UpgradeManager.LocalItemsUpgrade)
return;
if (!HostValues.KeepNoUpgrade.Value && HostValues.KeepItemsLevels.Value && UpgradeManager.CartItemsUpgradeLevel <= CartManager.CartsStoringItems)
return;
if (__instance.currentState != PhysGrabCart.State.Handled && __instance.currentState != PhysGrabCart.State.Dragged)
return;
if (SemiFunc.RunIsShop())
return;
if(HintUI.instance.transform.parent != HintUI.instance.normalParent)
HintUI.instance.transform.SetParent(HintUI.instance.normalParent);
HintUI.instance.grabHint = true;
HintUI.instance.ShowInfo("Hold <color=#f0bf30>[ALT]</color> to deposit items", HintUI.instance.textColor, 12f);
}
}
[HarmonyPatch(typeof(ItemInfoExtraUI), nameof(ItemInfoExtraUI.Start))]
public class CreateHintUI
{
internal static GameObject Hinter = null!;
public static void Postfix(ItemInfoExtraUI __instance)
{
if (!SpawnPlayerStuff.AreWeInGame())
return;
if (HintUI.instance != null || __instance.Text == null)
return;
Plugin.Spam("Creating Hinter!");
Hinter = new("Hinter");
TextMeshProUGUI text = Hinter.AddComponent<TextMeshProUGUI>();
text.font = __instance.Text.font;
text.fontMaterial = __instance.Text.fontMaterial;
text.fontSharedMaterial = __instance.Text.fontSharedMaterial;
text.fontSharedMaterials = __instance.Text.fontSharedMaterials;
text.spriteAsset = __instance.Text.spriteAsset;
text.alignment = TextAlignmentOptions.Midline;
text.fontSize = 12f;
text.fontStyle = FontStyles.SmallCaps;
//Hinter.AddComponent<RectTransform>();
Hinter.transform.SetParent(__instance.gameObject.transform);
Hinter.AddComponent<HintUI>();
}
}
[HarmonyPatch(typeof(SemiFunc), nameof(SemiFunc.OnLevelGenDone))]
public class PocketDimension
{
internal static GameObject ThePocket = null!;
internal static VoidRef voidRef = null!;
public static void Postfix()
{
if (!SpawnPlayerStuff.AreWeInGame())
return;
if(ThePocket == null)
{
ThePocket = GameObject.Instantiate(Plugin.PocketDimension, new Vector3(999f, 0, 0), new Quaternion(), LevelGenerator.Instance.LevelParent.transform);
}
if(voidRef == null)
{
voidRef = ThePocket.GetComponentInChildren<VoidRef>();
Plugin.Spam($"voidRef is null - {voidRef == null}");
}
}
}
[HarmonyPatch(typeof(PlayerAvatar), nameof(PlayerAvatar.SpawnRPC))]
public class SpawnPlayerStuff
{
public static void Postfix(PlayerAvatar __instance)
{
if (!AreWeInGame() || !__instance.photonView.IsMine)
return;
//Reset CartsStoringItems amount (since they have just spawned and cant possibly be storing items)
Plugin.Message("Player Spawned: CartsStoringItems set to 0");
CartManager.CartsStoringItems = 0;
}
internal static bool AreWeInGame()
{
if (RunManager.instance.levelCurrent == RunManager.instance.levelLobbyMenu)
return false;
if (RunManager.instance.levelCurrent == RunManager.instance.levelMainMenu)
return false;
return true;
}
}
[HarmonyPatch(typeof(StatsManager), nameof(StatsManager.LoadGame))]
public class StatsManagerLoad
{
public static void Postfix()
{
PocketCartUpgradeItems.LoadStart();
}
}
[HarmonyPatch(typeof(RunManagerPUN), nameof(RunManagerPUN.Start))]
public class NetworkingInstance
{
public static void Postfix()
{
if (RunManager.instance.runManagerPUN.gameObject.GetComponent<GlobalNetworking>() == null)
RunManager.instance.runManagerPUN.gameObject.AddComponent<GlobalNetworking>();
Plugin.Spam("RunManagerPUN.Start");
HostConfigCheck();
}
internal static void HostConfigCheck()
{
if (!HostConfigBase.HostConfigInit && IsValidState() && !InvalidScene())
HostValues.StartGame();
}
private static bool IsValidState()
{
if (GameManager.Multiplayer())
{
return PhotonNetwork.MasterClient != null;
}
return true;
}
private static bool InvalidScene()
{
if (!SemiFunc.IsCurrentLevel(RunManager.instance.levelCurrent, RunManager.instance.levelMainMenu))
{
return SemiFunc.IsCurrentLevel(RunManager.instance.levelCurrent, RunManager.instance.levelSplashScreen);
}
return true;
}
}
[HarmonyPatch(typeof(StatsManager), nameof(StatsManager.Start))]
public class StatsManagerStart
{
public static void Postfix(StatsManager __instance)
{
PocketCartUpgradeItems.InitDictionary();
Plugin.Spam("Updating statsmanager with our save keys!");
__instance.dictionaryOfDictionaries.TryAdd("playerUpgradePocketcartKeepItems", PocketCartUpgradeItems.ClientsUpgradeDictionary);
}
}
[HarmonyPatch(typeof(SemiFunc), nameof(SemiFunc.OnSceneSwitch))]
public class LeaveToMainReset
{
public static void Postfix(bool _gameOver, bool _leaveGame)
{
if(_gameOver || _leaveGame)
{
PocketCartUpgradeItems.ResetProgress();
}
Plugin.Spam("SemiFunc.OnSceneSwitch");
if(!SemiFunc.RunIsLobby() && !SemiFunc.RunIsLobbyMenu()) //this hook is too early to determine who the master client is
NetworkingInstance.HostConfigCheck();
if (HostConfigBase.HostConfigInit) //only reset hostconfiginit if we are leaving to main menu
HostConfigBase.HostConfigInit = !_leaveGame;
}
}
[HarmonyPatch(typeof(ShopManager), nameof(ShopManager.GetAllItemsFromStatsManager))]
public class ModifyItemRarity
{
public static void Postfix(ShopManager __instance)
{
if (!SemiFunc.IsMasterClientOrSingleplayer())
return;
//prices
Plugin.Log.LogDebug("Value reference patches");
PocketCartUpgradeItems.ValueRef();
PocketCartUpgradeSize.ValueRef();
VoidController.ValueRef();
//add-on rarities
Plugin.Log.LogDebug("Add-on rarity patches");
ShopPatch(HostValues.KeepItemsRarity.Value, "Item PocketCart Items", PocketCartUpgradeItems.valuePreset, ref __instance.potentialItemUpgrades);
ShopPatch(HostValues.PlusCartRarity.Value, "Item PCartPlus", PocketCartUpgradeSize.valuePreset, ref __instance.potentialItems);
ShopPatch(HostValues.VRRarity.Value, "Item VoidRemote", VoidController.valuePreset, ref __instance.potentialSecretItems);
}
private static void ShopPatch(int config, string prefabName, Value valuePreset, ref List<Item> RefItemList)
{
bool shouldAdd = false;
Item itemName = RefItemList.FirstOrDefault(i => i.prefab.prefabName == prefabName);
if (itemName == null)
{
Plugin.Spam($"Item [{prefabName}] not found!");
return;
}
if (config >= Plugin.Rand.Next(0, 100))
shouldAdd = true;
if (!shouldAdd)
{
int CountToReplace = RefItemList.Count(i => i.itemName == itemName.itemName);
Plugin.Spam($"Add-on rarity has determined {itemName.itemName} should be removed from the store! Original contains {CountToReplace} of this item");
RefItemList.RemoveAll(i => i.itemName == itemName.itemName);
if (CountToReplace > 0 && RefItemList.Count > 0)
{
for (int i = 0; i < CountToReplace; i++)
{
RefItemList.Add(RefItemList[Plugin.Rand.Next(0, RefItemList.Count)]);
Plugin.Spam("Replaced item with another random item of same type");
}
}
}
Plugin.Spam($"Rarity determined Item [{prefabName}] can be added in the shop {shouldAdd}");
itemName.value = valuePreset;
Plugin.Spam($"Value preset set for Item [{prefabName}]");
}
private static void ShopPatch(int config, string prefabName, Value valuePreset, ref Dictionary<SemiFunc.itemSecretShopType, List<Item>> secretShopDict)
{
bool shouldAdd = false;
Item itemName = null!;
SemiFunc.itemSecretShopType secretType = SemiFunc.itemSecretShopType.none;
foreach(var type in secretShopDict.Keys)
{
List<Item> list = secretShopDict[type];
itemName = list.FirstOrDefault(x => x.prefab.prefabName == prefabName);
if (itemName != null)
{
secretType = type;
break;
}
}
if (itemName == null)
{
Plugin.Spam($"Item [{prefabName}] not found in secret shop!");
return;
}
int rand = Plugin.Rand.Next(0, 100);
if (config >= rand)
shouldAdd = true;
else
Plugin.Spam($"Config {config} is less than {rand}");
if (!shouldAdd)
{
int CountToReplace = secretShopDict[secretType].Count(i => i.itemName == itemName.itemName);
Plugin.Spam($"Add-on rarity has determined {itemName.itemName} should be removed from the store! Original contains {CountToReplace} of this item");
secretShopDict[secretType].RemoveAll(i => i.itemName == itemName.itemName);
if (CountToReplace > 0 && secretShopDict.Count > 0)
{
for (int i = 0; i < CountToReplace; i++)
{
secretShopDict[secretType].Add(secretShopDict[secretType][Plugin.Rand.Next(0, secretShopDict.Count)]);
Plugin.Spam("Replaced secret shop item with another random valid secret shop item");
}
}
}
Plugin.Spam($"Rarity determined Item [{prefabName}] can be added in the secret shop {shouldAdd}");
itemName.value = valuePreset;
Plugin.Spam($"Value preset set for Item [{prefabName}]");
}
}
[HarmonyPatch(typeof(PhysGrabCart), nameof(PhysGrabCart.Start))]
public class GetPocketCarts
{
internal static List<PhysGrabCart> AllSmallCarts = [];
public static void Postfix(PhysGrabCart __instance)
{
if (!__instance.isSmallCart)
return;
CartManager cartManager = __instance.gameObject.GetComponent<CartManager>() ?? __instance.gameObject.AddComponent<CartManager>();
AllSmallCarts.RemoveAll(c => c == null);
AllSmallCarts.Add(__instance);
}
}
//for pocketcart upgraded size
[HarmonyPatch(typeof(ItemEquippable), nameof(ItemEquippable.AnimateUnequip))]
public class FixScaleofPlus
{
public static void Postfix(ItemEquippable __instance)
{
PocketCartUpgradeSize upgrade = __instance.gameObject.GetComponent<PocketCartUpgradeSize>();
if (upgrade != null)
{
Plugin.Spam("Returning pocketcart plus to original scale!");
upgrade.ReturnScale();
}
}
}
//need to patch here to parent transform early enough
[HarmonyPatch(typeof(ItemEquippable), nameof(ItemEquippable.RequestEquip))]
public class EquipPatch
{
internal static List<CartItem> AllCartItems = [];
public static void Postfix(ItemEquippable __instance)
{
if (!UpgradeManager.LocalItemsUpgrade && !HostValues.KeepNoUpgrade.Value)
return;
Plugin.Spam("Checking item being equipped");
GetPocketCarts.AllSmallCarts.RemoveAll(c => c == null);
if (!GetPocketCarts.AllSmallCarts.Any(e => e.itemEquippable == __instance))
return;
PhysGrabCart cart = GetPocketCarts.AllSmallCarts.FirstOrDefault(c => c.itemEquippable == __instance);
if (cart == null)
return;
if (cart.itemsInCart.Count == 0)
return;
CartManager cartManager = cart.GetComponent<CartManager>();
if(cartManager == null)
{
Plugin.ERROR("Unable to get cartManager component!");
return;
}
if (cartManager.isShowingItems)
{
Plugin.WARNING("cart is currently showing items!");
return;
}
if (!HostValues.KeepNoUpgrade.Value && HostValues.KeepItemsLevels.Value)
{
//compare local upgrade level to amount of carts storing items
if (UpgradeManager.CartItemsUpgradeLevel <= CartManager.CartsStoringItems)
{
Plugin.Message($"Unable to store items with this cart, already storing items in [ {CartManager.CartsStoringItems} ] carts!");
return;
}
}
if(Keyboard.current.altKey.isPressed && ModConfig.AllowDeposit.Value)
{
Plugin.Message($"Deposit key being pressed, not storing items in cart!");
return;
}
if (SemiFunc.IsMultiplayer())
cartManager.photonView.RPC("HideCartItems", RpcTarget.AllBuffered, PlayerAvatar.instance.steamID);
else
cartManager.HideCartItems(PlayerAvatar.instance.steamID);
Plugin.Spam("Pocket cart equip detected!\nHiding all cart items with cart!");
}
internal static IEnumerator ChangeSize(float duration, Vector3 targetScale, Vector3 originalScale, Transform transform)
{
float elapsed = 0f;
while (elapsed < duration)
{
float t = elapsed / duration;
transform.localScale = Vector3.Lerp(originalScale, targetScale, t);
elapsed += Time.deltaTime;
yield return null;
}
transform.localScale = targetScale;
Plugin.Spam($"All items size set to targetscale [ {targetScale} ] in transform!");
}
}
[HarmonyPatch(typeof(ItemEquippable), nameof(ItemEquippable.UpdateVisuals))]
public class UpdateVisualsPatch
{
public static void Postfix(ItemEquippable __instance)
{
if (__instance.currentState != ItemEquippable.ItemState.Unequipping)
return;
if (!UpgradeManager.LocalItemsUpgrade && !HostValues.KeepNoUpgrade.Value)
return;
Plugin.Spam("Unequip detected! Checking if this item is a cart we care about");
GetPocketCarts.AllSmallCarts.RemoveAll(c => c == null);
if (!GetPocketCarts.AllSmallCarts.Any(e => e.itemEquippable == __instance))
{
Plugin.Spam("We don't care about this equippable");
return;
}
PhysGrabCart cart = GetPocketCarts.AllSmallCarts.FirstOrDefault(c => c.itemEquippable == __instance);
//we only care about one of our carts that has stored items
if (cart == null)
{
Plugin.Spam("Cart is null");
return;
}
CartManager cartManager = cart.GetComponent<CartManager>();
if (cartManager == null)
{
Plugin.ERROR("Unable to get cartManager component!");
return;
}
if (!cartManager.HasItems())
return;
if (SemiFunc.IsMultiplayer())
cartManager.photonView.RPC("ShowCartItems", RpcTarget.AllBuffered);
else
cartManager.ShowCartItems();
}
internal static IEnumerator WaitToDisplay(CartManager cartManager)
{
if (cartManager.isShowingItems)
yield break;
cartManager.isShowingItems = true;
PhysGrabCart cart = cartManager.MyCart;
Plugin.Spam($"Waiting to Display object group for cart!");
yield return null;
//wait for resize to complete
while (cart.transform.localScale != Vector3.one)
{
yield return null;
if (cartManager.isStoringItems)
{
Plugin.WARNING("Ending display early! Cart is equiping!");
cartManager.isShowingItems = false;
yield break;
}
}
Plugin.Spam($"AllCartItems count - {EquipPatch.AllCartItems.Count}\nRemoving null items!");
EquipPatch.AllCartItems.RemoveAll(c => c == null || c.grabObj == null);
Plugin.Spam($"AllCartItems count - {EquipPatch.AllCartItems.Count}\nRestoring items in cart!");
EquipPatch.AllCartItems.DoIf(x => x.IsStored && x.MyCart == cart, x =>
{
cart.StartCoroutine(x.RestoreItem(cart));
});
yield return null;
cartManager.isShowingItems = false;
if(cartManager.storedBy == PlayerAvatar.instance.steamID)
CartManager.CartsStoringItems = Mathf.Clamp(CartManager.CartsStoringItems - 1, 0, 99);
}
}
}