Patch_GameScript_Update.cs 4.2 KB

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