Patch_GameScript_Update.cs 7.6 KB

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