Patch_GameScript_Update.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. else if (___craftType == MenuRegistry.Singleton["Gadget Core:Crafter Menu"].GetID())
  49. {
  50. __instance.StartCoroutine(QuickCraft(__instance, ___curRecipePage, slotID, ___craftType, ___inventory, ___craft));
  51. acted = true;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. if (acted)
  60. return false;
  61. return true;
  62. }
  63. private static IEnumerator QuickCraft(GameScript __instance, int ___curRecipePage, int slot, int ___craftType, Item[] ___inventory, Item[] ___craft)
  64. {
  65. FieldInfo quickCraftingField = typeof(GameScript).GetField("quickCrafting", BindingFlags.NonPublic | BindingFlags.Instance);
  66. FieldInfo holdingItemField = typeof(GameScript).GetField("holdingItem", BindingFlags.NonPublic | BindingFlags.Instance);
  67. if (!(bool)quickCraftingField.GetValue(__instance))
  68. {
  69. quickCraftingField.SetValue(__instance, true);
  70. try
  71. {
  72. int craftingItemID = 0;
  73. int craftingItemAmount = 1;
  74. List<int> price = new List<int>();
  75. List<int> takePlayerInventory = new List<int>();
  76. List<int> takeCraftingInventory = new List<int>();
  77. bool hasAllItems = true;
  78. if (___craftType == 0)
  79. {
  80. var recipePageEntry = ___curRecipePage >= 6 ? Core.pageGearForgeInfoList[___curRecipePage - 6].GetRecipePageEntries()[slot] : Core.pageGearForgeInfoListVanilla[___curRecipePage].GetRecipePageEntries()[slot];
  81. craftingItemID = recipePageEntry.ItemIdBase;
  82. craftingItemAmount = recipePageEntry.MinAmount + Random.Range(0, 1 + recipePageEntry.MaxBonusAmount);
  83. for (int i = 0; i < recipePageEntry.ItemIdExtension.Length; i++)
  84. {
  85. var itemID = recipePageEntry.ItemIdExtension[i];
  86. if (itemID > 0)
  87. price.Add(itemID);
  88. }
  89. }
  90. else if (___craftType == 1)
  91. {
  92. var recipePageEntry = ___curRecipePage >= 2 ? Core.pageAlchemyStationInfoList[___curRecipePage - 2].GetRecipePageEntries()[slot] : Core.pageAlchemyStationInfoListVanilla[___curRecipePage].GetRecipePageEntries()[slot];
  93. craftingItemID = recipePageEntry.ItemIdBase;
  94. craftingItemAmount = recipePageEntry.MinAmount + Random.Range(0, 1 + recipePageEntry.MaxBonusAmount);
  95. for (int i = 0; i < recipePageEntry.ItemIdExtension.Length; i++)
  96. {
  97. var itemID = recipePageEntry.ItemIdExtension[i];
  98. if (itemID > 0)
  99. price.Add(itemID);
  100. }
  101. }
  102. else if (___craftType == MenuRegistry.Singleton["Gadget Core:Crafter Menu"].GetID())
  103. {
  104. var recipePageEntry = Core.pageUniversalCrafterInfoList[___curRecipePage].GetRecipePageEntries()[slot];
  105. craftingItemID = recipePageEntry.ItemIdBase;
  106. craftingItemAmount = recipePageEntry.MinAmount + Random.Range(0, 1 + recipePageEntry.MaxBonusAmount);
  107. for (int i = 0; i < recipePageEntry.ItemIdExtension.Length; i++)
  108. {
  109. var itemID = recipePageEntry.ItemIdExtension[i];
  110. if (itemID > 0)
  111. price.Add(itemID);
  112. }
  113. }
  114. if (craftingItemID == 0
  115. || !(___craftType == MenuRegistry.Singleton["Gadget Core:Crafter Menu"].GetID()
  116. ? ((CraftMenuInfo)MenuRegistry.Singleton["Gadget Core:Crafter Menu"]).IsRecipeUnlocked(craftingItemID)
  117. : __instance.RecipeCraftedAlready(craftingItemID, 0) ))
  118. {
  119. yield break;
  120. }
  121. for (int i = 0; i < price.Count; i++)
  122. {
  123. int itemSlotPlayerInventory = __instance.ItemExistsSlot(price[i]);
  124. int itemSlotCraftingInventory = __instance.ItemExistsCraft(price[i]);
  125. if (itemSlotPlayerInventory == -1 && itemSlotCraftingInventory == -1)
  126. {
  127. hasAllItems = false;
  128. break;
  129. }
  130. else if (itemSlotPlayerInventory != -1)
  131. takePlayerInventory.Add(itemSlotPlayerInventory);
  132. else if (itemSlotCraftingInventory != -1)
  133. takeCraftingInventory.Add(itemSlotCraftingInventory);
  134. }
  135. if (hasAllItems)
  136. {
  137. bool hasCrafted = false;
  138. Item tempItem = (Item)holdingItemField.GetValue(__instance);
  139. if (tempItem == null || tempItem.id == 0 || tempItem.id == craftingItemID)
  140. {
  141. ItemInfo itemInfo = ItemRegistry.GetItem(craftingItemID);
  142. ItemType slotItemType = itemInfo != null ? (itemInfo.Type & (ItemType.EQUIP_MASK | ItemType.TYPE_MASK)) : ItemRegistry.GetDefaultTypeByID(craftingItemID);
  143. if ((slotItemType & ItemType.NONSTACKING) == ItemType.STACKING)
  144. {
  145. if (tempItem == null || tempItem.id == 0)
  146. {
  147. holdingItemField.SetValue(__instance, new Item(craftingItemID, craftingItemAmount, 0, 0, 0, new int[3], new int[3]));
  148. hasCrafted = true;
  149. }
  150. else if (tempItem.q < 9999)
  151. {
  152. tempItem.q += craftingItemAmount;
  153. if (tempItem.q > 9999)
  154. tempItem.q = 9999;
  155. hasCrafted = true;
  156. }
  157. }
  158. else
  159. {
  160. if (tempItem == null || tempItem.id == 0)
  161. {
  162. holdingItemField.SetValue(__instance, new Item(craftingItemID, 1, 0, __instance.GetRandomTier(), 0, new int[3], new int[3]));
  163. hasCrafted = true;
  164. }
  165. }
  166. }
  167. if (hasCrafted)
  168. {
  169. Object.Instantiate(Resources.Load("clickBurst"), new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, 0f), Quaternion.identity);
  170. __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/create"), Menuu.soundLevel / 10f);
  171. for (int i = 0; i < takePlayerInventory.Count; i++)
  172. {
  173. ___inventory[takePlayerInventory[i]].q--;
  174. __instance.RefreshSlot(takePlayerInventory[i]);
  175. }
  176. for (int i = 0; i < takeCraftingInventory.Count; i++)
  177. {
  178. ___craft[takeCraftingInventory[i]].q--;
  179. __instance.RefreshSlotCraft(takeCraftingInventory[i]);
  180. }
  181. }
  182. __instance.RefreshHoldingSlot();
  183. }
  184. else
  185. __instance.Error(6);
  186. }
  187. catch (System.Exception e) { Core.logger.Log(e); }
  188. finally
  189. {
  190. quickCraftingField.SetValue(__instance, false);
  191. }
  192. }
  193. yield break;
  194. }
  195. }
  196. }