Patch_GameScript_Update.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using HarmonyLib;
  2. using GadgetCore.API;
  3. using UnityEngine;
  4. using System.Collections;
  5. using System.Reflection;
  6. using System.Collections.Generic;
  7. namespace RecipeMenuCore.Patches
  8. {
  9. [HarmonyPatch(typeof(GameScript))]
  10. [HarmonyPatch("Update")]
  11. [HarmonyGadget("RecipeMenuCore")]
  12. public static class Patch_GameScript_Update
  13. {
  14. [HarmonyPrefix]
  15. public static bool Prefix(GameScript __instance, int ___craftType, int ___curRecipePage, Item[] ___inventory, Item[] ___craft)
  16. {
  17. bool acted = false;
  18. if (MenuScript.player)
  19. {
  20. if (!GameScript.pausing)
  21. {
  22. if (Input.GetMouseButtonDown(0) && GameScript.inventoryOpen)
  23. {
  24. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  25. RaycastHit hit;
  26. if (Physics.Raycast(ray, out hit, 7f))
  27. {
  28. if (hit.transform.gameObject.layer == 16)
  29. {
  30. if (hit.transform.gameObject.tag == "recipe")
  31. {
  32. var slotID = int.Parse(hit.transform.gameObject.name);
  33. MonoBehaviour.print("hit recipe");
  34. if (___craftType == 0 && (___curRecipePage >= 6 || Core.pageGearForgeInfoListVanilla[___curRecipePage] != null))
  35. {
  36. __instance.StartCoroutine(QuickCraft(__instance, ___curRecipePage, slotID, ___craftType, ___inventory, ___craft));
  37. acted = true;
  38. }
  39. else if (___craftType == 1 && (___curRecipePage >= 2 || Core.pageAlchemyStationInfoListVanilla[___curRecipePage] != null))
  40. {
  41. __instance.StartCoroutine(QuickCraft(__instance, ___curRecipePage, slotID, ___craftType, ___inventory, ___craft));
  42. acted = true;
  43. }
  44. else if (___craftType == 2 && (___curRecipePage >= 6 || Core.pageUltimateForgeInfoListVanilla[___curRecipePage] != null))
  45. {
  46. acted = true;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. if (acted)
  55. return false;
  56. return true;
  57. }
  58. private static IEnumerator QuickCraft(GameScript __instance, int ___curRecipePage, int slot, int ___craftType, Item[] ___inventory, Item[] ___craft)
  59. {
  60. FieldInfo quickCraftingField = typeof(GameScript).GetField("quickCrafting", BindingFlags.NonPublic | BindingFlags.Instance);
  61. FieldInfo holdingItemField = typeof(GameScript).GetField("holdingItem", BindingFlags.NonPublic | BindingFlags.Instance);
  62. if (!(bool)quickCraftingField.GetValue(__instance))
  63. {
  64. quickCraftingField.SetValue(__instance, true);
  65. try
  66. {
  67. int craftingItemID = 0;
  68. int craftingItemAmount = 1;
  69. List<int> price = new List<int>();
  70. List<int> takePlayerInventory = new List<int>();
  71. List<int> takeCraftingInventory = new List<int>();
  72. bool hasAllItems = true;
  73. if (___craftType == 0)
  74. {
  75. var recipePageEntry = ___curRecipePage >= 6 ? Core.pageGearForgeInfoList[___curRecipePage - 6].GetRecipePageEntries()[slot] : Core.pageGearForgeInfoListVanilla[___curRecipePage].GetRecipePageEntries()[slot];
  76. craftingItemID = recipePageEntry.ItemIdBase;
  77. craftingItemAmount = recipePageEntry.MinAmount + Random.Range(0, 1 + recipePageEntry.MaxBonusAmount);
  78. for (int i = 0; i < recipePageEntry.ItemIdExtension.Length; i++)
  79. {
  80. var itemID = recipePageEntry.ItemIdExtension[i];
  81. if (itemID > 0)
  82. price.Add(itemID);
  83. }
  84. }
  85. else if (___craftType == 1)
  86. {
  87. var recipePageEntry = ___curRecipePage >= 2 ? Core.pageAlchemyStationInfoList[___curRecipePage - 2].GetRecipePageEntries()[slot] : Core.pageAlchemyStationInfoListVanilla[___curRecipePage].GetRecipePageEntries()[slot];
  88. craftingItemID = recipePageEntry.ItemIdBase;
  89. craftingItemAmount = recipePageEntry.MinAmount + Random.Range(0, 1 + recipePageEntry.MaxBonusAmount);
  90. for (int i = 0; i < recipePageEntry.ItemIdExtension.Length; i++)
  91. {
  92. var itemID = recipePageEntry.ItemIdExtension[i];
  93. if (itemID > 0)
  94. price.Add(itemID);
  95. }
  96. }
  97. if (craftingItemID == 0 || !__instance.RecipeCraftedAlready(craftingItemID, 0))
  98. {
  99. yield break;
  100. }
  101. for (int i = 0; i < price.Count; i++)
  102. {
  103. int itemSlotPlayerInventory = __instance.ItemExistsSlot(price[i]);
  104. int itemSlotCraftingInventory = __instance.ItemExistsCraft(price[i]);
  105. if (itemSlotPlayerInventory == -1 && itemSlotCraftingInventory == -1)
  106. {
  107. hasAllItems = false;
  108. break;
  109. }
  110. else if (itemSlotPlayerInventory != -1)
  111. takePlayerInventory.Add(itemSlotPlayerInventory);
  112. else if (itemSlotCraftingInventory != -1)
  113. takeCraftingInventory.Add(itemSlotCraftingInventory);
  114. }
  115. if (hasAllItems)
  116. {
  117. bool hasCrafted = false;
  118. Item tempItem = (Item)holdingItemField.GetValue(__instance);
  119. if (tempItem == null || tempItem.id == 0 || tempItem.id == craftingItemID)
  120. {
  121. ItemInfo itemInfo = ItemRegistry.GetItem(craftingItemID);
  122. ItemType slotItemType = itemInfo != null ? (itemInfo.Type & (ItemType.EQUIP_MASK | ItemType.TYPE_MASK)) : ItemRegistry.GetDefaultTypeByID(craftingItemID);
  123. if ((slotItemType & ItemType.NONSTACKING) == ItemType.STACKING)
  124. {
  125. if (tempItem == null || tempItem.id == 0)
  126. {
  127. holdingItemField.SetValue(__instance, new Item(craftingItemID, craftingItemAmount, 0, 0, 0, new int[3], new int[3]));
  128. hasCrafted = true;
  129. }
  130. else if (tempItem.q < 9999)
  131. {
  132. tempItem.q += craftingItemAmount;
  133. if (tempItem.q > 9999)
  134. tempItem.q = 9999;
  135. hasCrafted = true;
  136. }
  137. }
  138. else
  139. {
  140. if (tempItem == null || tempItem.id == 0)
  141. {
  142. holdingItemField.SetValue(__instance, new Item(craftingItemID, 1, 0, __instance.GetRandomTier(), 0, new int[3], new int[3]));
  143. hasCrafted = true;
  144. }
  145. }
  146. }
  147. if (hasCrafted)
  148. {
  149. Object.Instantiate(Resources.Load("clickBurst"), new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, 0f), Quaternion.identity);
  150. __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/create"), Menuu.soundLevel / 10f);
  151. for (int i = 0; i < takePlayerInventory.Count; i++)
  152. {
  153. ___inventory[takePlayerInventory[i]].q--;
  154. __instance.RefreshSlot(takePlayerInventory[i]);
  155. }
  156. for (int i = 0; i < takeCraftingInventory.Count; i++)
  157. {
  158. ___craft[takeCraftingInventory[i]].q--;
  159. __instance.RefreshSlotCraft(takeCraftingInventory[i]);
  160. }
  161. }
  162. __instance.RefreshHoldingSlot();
  163. }
  164. else
  165. __instance.Error(6);
  166. }
  167. catch (System.Exception e) { Core.logger.Log(e); }
  168. finally
  169. {
  170. quickCraftingField.SetValue(__instance, false);
  171. }
  172. }
  173. yield break;
  174. }
  175. }
  176. }