Patch_GameScript_Update.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using GadgetCore;
  2. using GadgetCore.API;
  3. using HarmonyLib;
  4. using System;
  5. using UnityEngine;
  6. namespace VanitySlots.Patches
  7. {
  8. [HarmonyPatch(typeof(GameScript))]
  9. [HarmonyPatch("Update")]
  10. [HarmonyGadget("VanitySlots")]
  11. public static class Patch_GameScript_Update
  12. {
  13. [HarmonyPrefix]
  14. public static void Prefix(GameScript __instance, ref Item ___holdingItem, ref int ___curhoveringstat, ref bool ___holdingCombatChip, bool ___exitingcombatmode)
  15. {
  16. try
  17. {
  18. if (MenuScript.player)
  19. {
  20. if (!GameScript.pausing && Input.GetMouseButtonDown(0) && GameScript.inventoryOpen)
  21. {
  22. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  23. RaycastHit hit;
  24. if (Physics.Raycast(ray, out hit, 7f))
  25. {
  26. if (hit.transform.gameObject.name.StartsWith("vanity_slot_"))
  27. {
  28. var slot = 2;
  29. if (hit.transform.gameObject.name.StartsWith("vanity_slot_"))
  30. slot = int.Parse(hit.transform.gameObject.name.Substring("vanity_slot_".Length));
  31. if (slot >= 0 && slot < Core.itemStore.Items.Length)
  32. {
  33. if (___holdingItem.id != 0)
  34. {
  35. if (Core.itemStore.CanUse(___holdingItem, slot))
  36. {
  37. if (Core.itemStore.Items[slot].id == 0)
  38. {
  39. Place(__instance, slot, ref ___holdingItem);
  40. }
  41. else
  42. {
  43. Swap(__instance, slot, ref ___holdingItem);
  44. }
  45. }
  46. }
  47. else if (Core.itemStore.Items[slot].id != 0)
  48. {
  49. Select(__instance, slot, ref ___holdingItem);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. catch (Exception e) { Core.logger.LogConsole(e.Message); }
  58. }
  59. [HarmonyPostfix]
  60. public static void Postfix()
  61. {
  62. if (MenuScript.player)
  63. {
  64. if (GameScript.inventoryOpen)
  65. {
  66. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  67. RaycastHit hit;
  68. if (Physics.Raycast(ray, out hit, 7f))
  69. {
  70. if (hit.transform.gameObject.name.StartsWith("vanity_slot_"))
  71. {
  72. var slot = 2;
  73. if (hit.transform.gameObject.name.StartsWith("vanity_slot_"))
  74. slot = int.Parse(hit.transform.gameObject.name.Substring("vanity_slot_".Length));
  75. if ((Core.itemStore.Items[slot]?.id ?? 0) != 0)
  76. PatchMethods.HoverItem(Core.itemStore.Items[slot]);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. private static void Swap(GameScript __instance, int slot, ref Item ___holdingItem)
  83. {
  84. __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/CLICK1"), Menuu.soundLevel / 10f);
  85. Item item = ___holdingItem;
  86. ___holdingItem = Core.itemStore.Items[slot];
  87. Core.itemStore.Items[slot] = item;
  88. Core.itemStore.UpdateUI();
  89. __instance.RefreshHoldingSlot();
  90. }
  91. private static void Select(GameScript __instance, int slot, ref Item ___holdingItem)
  92. {
  93. __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/CLICK1"), Menuu.soundLevel / 10f);
  94. ___holdingItem = Core.itemStore.Items[slot];
  95. Core.itemStore.Items[slot] = __instance.EmptyItem();
  96. Core.itemStore.UpdateUI();
  97. __instance.RefreshHoldingSlot();
  98. }
  99. private static void Place(GameScript __instance, int slot, ref Item ___holdingItem)
  100. {
  101. __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/CLICK1"), Menuu.soundLevel / 10f);
  102. Core.itemStore.Items[slot] = ___holdingItem;
  103. ___holdingItem = __instance.EmptyItem();
  104. Core.itemStore.UpdateUI();
  105. __instance.RefreshHoldingSlot();
  106. }
  107. }
  108. }