Patch_GameScript_Update.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using GadgetCore;
  2. using GadgetCore.API;
  3. using HarmonyLib;
  4. using QuickStack.ConfigEnums;
  5. using System;
  6. using System.Collections;
  7. using System.Reflection;
  8. using UnityEngine;
  9. namespace QuickStack.Patches
  10. {
  11. [HarmonyPatch(typeof(GameScript))]
  12. [HarmonyPatch("Update")]
  13. [HarmonyGadget("QuickStack")]
  14. public static class Patch_GameScript_Update
  15. {
  16. [HarmonyPrefix]
  17. public static void Prefix(GameScript __instance, Item[] ___inventory, ref Item[] ___storage, int ___storageLevel, int ___curStoragePage)
  18. {
  19. try
  20. {
  21. if (MenuScript.player)
  22. {
  23. if (!GameScript.pausing && Input.GetMouseButtonDown(0) && GameScript.inventoryOpen)
  24. {
  25. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  26. RaycastHit hit;
  27. if (Physics.Raycast(ray, out hit, 7f))
  28. {
  29. if (hit.transform.gameObject.name.StartsWith("button_quick_stack"))
  30. {
  31. for (int slot = 0; slot < 36; slot++)
  32. {
  33. bool updateSlot = false;
  34. int startPage = Core.settingStackRange == StackRangeEnum.OpenPage ? ___curStoragePage : 0;
  35. int endPage = Core.settingStackRange == StackRangeEnum.OpenPage ? ___curStoragePage + 1 : ___storageLevel + 1;
  36. for (int page = startPage; page < endPage; page++)
  37. {
  38. bool contains = false;
  39. Item item = ___inventory[slot];
  40. for (int slotPage = 0; slotPage < 30; slotPage++)
  41. if (___storage[page * 30 + slotPage].id == item.id)
  42. contains = true;
  43. bool potion = item.id >= 60 && item.id <= 73;
  44. bool shouldTake = true;
  45. if (potion)
  46. {
  47. if (slot < 6 && Core.settingStackPotionsSource == StackSourceInventoryPotionsEnum.OnlyMainInventory || Core.settingStackPotionsSource == StackSourceInventoryPotionsEnum.None)
  48. shouldTake = false;
  49. if (slot > 6 && Core.settingStackPotionsSource == StackSourceInventoryPotionsEnum.OnlyToolbar || Core.settingStackPotionsSource == StackSourceInventoryPotionsEnum.None)
  50. shouldTake = false;
  51. }
  52. else
  53. {
  54. if (slot < 6 && Core.settingStackSource == StackSourceInventoryEnum.OnlyMainInventory)
  55. shouldTake = false;
  56. if (slot > 6 && Core.settingStackSource == StackSourceInventoryEnum.OnlyToolbar)
  57. shouldTake = false;
  58. }
  59. if ((contains || Core.settingStackMode == ConfigEnums.StackModeEnum.FillPageAll) && shouldTake)
  60. {
  61. ItemInfo slotInfo = ItemRegistry.Singleton.GetEntry(item.id);
  62. ItemType slotItemType = slotInfo != null ? (slotInfo.Type & (ItemType.EQUIP_MASK | ItemType.TYPE_MASK)) : ItemRegistry.GetDefaultTypeByID(item.id);
  63. if ((slotItemType & ItemType.NONSTACKING) == ItemType.STACKING)
  64. {
  65. for (int slotPage = 0; slotPage < 30; slotPage++)
  66. if (GadgetCoreAPI.CanItemsStack(___storage[page * 30 + slotPage], item))
  67. {
  68. while (___storage[page * 30 + slotPage].q < 9999 && item.q > 0)
  69. {
  70. ___storage[page * 30 + slotPage].q++;
  71. item.q--;
  72. updateSlot = true;
  73. }
  74. }
  75. if (item.q == 0)
  76. ___inventory[slot] = new Item(0, 0, 0, 0, 0, new int[3], new int[3]);
  77. else if (Core.settingStackMode != ConfigEnums.StackModeEnum.StackingOnly)
  78. {
  79. for (int slotPage = 0; slotPage < 30; slotPage++)
  80. if (___storage[page * 30 + slotPage].id == 0)
  81. {
  82. ___storage[page * 30 + slotPage] = item;
  83. ___inventory[slot] = new Item(0, 0, 0, 0, 0, new int[3], new int[3]);
  84. updateSlot = true;
  85. break;
  86. }
  87. }
  88. }
  89. else if (Core.settingStackMode != ConfigEnums.StackModeEnum.StackingOnly)
  90. {
  91. for (int slotPage = 0; slotPage < 30; slotPage++)
  92. if (___storage[page * 30 + slotPage].id == 0)
  93. {
  94. ___storage[page * 30 + slotPage] = item;
  95. ___inventory[slot] = new Item(0, 0, 0, 0, 0, new int[3], new int[3]);
  96. updateSlot = true;
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. if (updateSlot)
  103. typeof(GameScript).GetMethod("RefreshSlot", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { slot });
  104. }
  105. __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/CLICK3"), Menuu.soundLevel / 10f);
  106. __instance.StartCoroutine((IEnumerator)typeof(GameScript).GetMethod("RefreshStoragePage", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { ___curStoragePage }));
  107. }
  108. }
  109. }
  110. }
  111. }
  112. catch (Exception e) { Core.logger.LogConsole(e.Message); }
  113. }
  114. }
  115. }