Patch_GameScript_Update.cs 6.2 KB

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