From 80637af4711f6e13604062b538ec9746b63e8138 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Mon, 20 Feb 2023 12:59:55 +0530 Subject: [PATCH] Moved narrateHoveredItemInInventory to separate class --- stardew-access/Patches/DonationMenuPatches.cs | 52 +++--- stardew-access/Patches/GameMenuPatches.cs | 172 +----------------- stardew-access/Patches/InventoryUtils.cs | 171 +++++++++++++++++ stardew-access/Patches/MenuPatches.cs | 5 +- 4 files changed, 206 insertions(+), 194 deletions(-) create mode 100644 stardew-access/Patches/InventoryUtils.cs diff --git a/stardew-access/Patches/DonationMenuPatches.cs b/stardew-access/Patches/DonationMenuPatches.cs index f4969ca..a49be83 100644 --- a/stardew-access/Patches/DonationMenuPatches.cs +++ b/stardew-access/Patches/DonationMenuPatches.cs @@ -303,31 +303,33 @@ namespace stardew_access.Patches } else { - for (int i = 0; i < __instance.inventory.inventory.Count; i++) - { - if (!__instance.inventory.inventory[i].containsPoint(x, y)) - continue; - - if (__instance.inventory.actualInventory[i] == null) - toSpeak = "Empty slot"; - else - { - toSpeak = $"{__instance.inventory.actualInventory[i].Stack} {__instance.inventory.actualInventory[i].DisplayName}"; - - if (!__instance.inventory.highlightMethod(__instance.inventory.actualInventory[i])) - { - toSpeak = $"{toSpeak} not usable here"; - } - } - - if (fieldOfficeMenuQuery != $"{toSpeak}:{i}") - { - fieldOfficeMenuQuery = $"{toSpeak}:{i}"; - MainClass.ScreenReader.Say(toSpeak, true); - } - + if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) return; - } + // for (int i = 0; i < __instance.inventory.inventory.Count; i++) + // { + // if (!__instance.inventory.inventory[i].containsPoint(x, y)) + // continue; + // + // if (__instance.inventory.actualInventory[i] == null) + // toSpeak = "Empty slot"; + // else + // { + // toSpeak = $"{__instance.inventory.actualInventory[i].Stack} {__instance.inventory.actualInventory[i].DisplayName}"; + // + // if (!__instance.inventory.highlightMethod(__instance.inventory.actualInventory[i])) + // { + // toSpeak = $"{toSpeak} not usable here"; + // } + // } + // + // if (fieldOfficeMenuQuery != $"{toSpeak}:{i}") + // { + // fieldOfficeMenuQuery = $"{toSpeak}:{i}"; + // MainClass.ScreenReader.Say(toSpeak, true); + // } + // + // return; + // } for (int i = 0; i < __instance.pieceHolders.Count; i++) { @@ -414,4 +416,4 @@ namespace stardew_access.Patches #endregion } -} \ No newline at end of file +} diff --git a/stardew-access/Patches/GameMenuPatches.cs b/stardew-access/Patches/GameMenuPatches.cs index 655d51b..a3018a8 100644 --- a/stardew-access/Patches/GameMenuPatches.cs +++ b/stardew-access/Patches/GameMenuPatches.cs @@ -20,7 +20,6 @@ namespace stardew_access.Patches internal static string profilePageQuery = ""; internal static int currentSelectedCraftingRecipe = -1; internal static bool isSelectingRecipe = false; - internal static int prevSlotIndex = -999; internal static void CollectionsPagePatch(CollectionsPage __instance) { @@ -241,7 +240,7 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, hoverPrice: __instance.hoverPrice)) + if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, hoverPrice: __instance.hoverPrice)) { shopMenuQueryKey = ""; return; @@ -396,7 +395,7 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) + if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) geodeMenuQueryKey = ""; #endregion } @@ -573,14 +572,14 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) + if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) { gameMenuQueryKey = ""; itemGrabMenuQueryKey = ""; return; } - if (narrateHoveredItemInInventory(__instance.ItemsToGrabMenu, __instance.ItemsToGrabMenu.inventory, __instance.ItemsToGrabMenu.actualInventory, x, y, true)) + if (InventoryUtils.narrateHoveredItemInInventory(__instance.ItemsToGrabMenu, __instance.ItemsToGrabMenu.inventory, __instance.ItemsToGrabMenu.actualInventory, x, y, true)) { gameMenuQueryKey = ""; itemGrabMenuQueryKey = ""; @@ -855,7 +854,7 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) + if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) { gameMenuQueryKey = ""; craftingPageQueryKey = ""; @@ -1066,7 +1065,7 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) + if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) { gameMenuQueryKey = ""; inventoryPageQueryKey = ""; @@ -1193,164 +1192,5 @@ namespace stardew_access.Patches MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}"); } } - - internal static bool narrateHoveredItemInInventory(InventoryMenu inventoryMenu, List inventory, IList actualInventory, int x, int y, bool giveExtraDetails = false, int hoverPrice = -1, int extraItemToShowIndex = -1, int extraItemToShowAmount = -1) - { - for (int i = 0; i < inventory.Count; i++) - { - if (!inventory[i].containsPoint(x, y)) continue; - - if ((i + 1) > actualInventory.Count || actualInventory[i] == null) - { - // For empty slot - checkAndSpeak("Empty Slot", i); - prevSlotIndex = i; - return true; - } - - string toSpeak = ""; - string name = $"{actualInventory[i].DisplayName}{handleUnHighlightedItem(inventoryMenu.highlightMethod(actualInventory[i]), i)}"; - int stack = actualInventory[i].Stack; - string quality = getQualityFromItem(actualInventory[i]); - string healthNStamine = getHealthNStaminaFromItem(actualInventory[i]); - string buffs = getBuffsFromItem(actualInventory[i]); - string description = actualInventory[i].getDescription(); - string price = getPrice(hoverPrice); - string requirements = getExtraItemInfo(extraItemToShowIndex, extraItemToShowAmount); - - if (giveExtraDetails) - { - if (stack > 1) - toSpeak = $"{stack} {name} {quality}, \n{requirements}, \n{price}, \n{description}, \n{healthNStamine}, \n{buffs}"; - else - toSpeak = $"{name} {quality}, \n{requirements}, \n{price}, \n{description}, \n{healthNStamine}, \n{buffs}"; - } - else - { - if (stack > 1) - toSpeak = $"{stack} {name} {quality}, \n{requirements}, \n{price}"; - else - toSpeak = $"{name} {quality}, \n{requirements}, \n{price}"; - } - - - checkAndSpeak(toSpeak, i); - prevSlotIndex = i; - return true; - } - - // If no slot is hovered - return false; - } - - private static void checkAndSpeak(String toSpeak, int hoveredInventoryIndex) - { - if (hoveredItemQueryKey == $"{toSpeak}:{hoveredInventoryIndex}") return; - - hoveredItemQueryKey = $"{toSpeak}:{hoveredInventoryIndex}"; - MainClass.ScreenReader.Say(toSpeak, true); - } - - private static String getQualityFromItem(Item item) - { - if (item is not StardewValley.Object || ((StardewValley.Object)item).Quality <= 0) - return ""; - - int qualityIndex = ((StardewValley.Object)item).Quality; - if (qualityIndex == 1) - { - return "Silver quality"; - } - else if (qualityIndex == 2 || qualityIndex == 3) - { - return "Gold quality"; - } - else if (qualityIndex >= 4) - { - return "Iridium quality"; - } - - return ""; - } - - private static String getHealthNStaminaFromItem(Item item) - { - if (item is not StardewValley.Object || ((StardewValley.Object)item).Edibility == -300) - return ""; - - String toReturn = ""; - int stamina_recovery = ((StardewValley.Object)item).staminaRecoveredOnConsumption(); - toReturn += $"{stamina_recovery} Energy"; - - if (stamina_recovery < 0) return toReturn; - - int health_recovery = ((StardewValley.Object)item).healthRecoveredOnConsumption(); - toReturn += $"\n\t{health_recovery} Health"; - - return toReturn; - } - - private static String getBuffsFromItem(Item item) - { - if (item == null) return ""; - if (item is not StardewValley.Object) return ""; - if (((StardewValley.Object)item) == null) return ""; - - // These variables are taken from the game's code itself (IClickableMenu.cs -> 1016 line) - bool edibleItem = (int)((StardewValley.Object)item).Edibility != -300; - string[]? buffIconsToDisplay = (edibleItem && Game1.objectInformation[((StardewValley.Object)item).ParentSheetIndex].Split('/').Length > 7) - ? item.ModifyItemBuffs(Game1.objectInformation[((StardewValley.Object)item).ParentSheetIndex].Split('/')[7].Split(' ')) - : null; - - if (buffIconsToDisplay == null) - return ""; - - String toReturn = ""; - for (int j = 0; j < buffIconsToDisplay.Length; j++) - { - string buffName = ((Convert.ToInt32(buffIconsToDisplay[j]) > 0) ? "+" : "") + buffIconsToDisplay[j] + " "; - if (j <= 11) - { - buffName = Game1.content.LoadString("Strings\\UI:ItemHover_Buff" + j, buffName); - } - try - { - int count = int.Parse(buffName.Substring(0, buffName.IndexOf(' '))); - if (count != 0) - toReturn += $"{buffName}\n"; - } - catch (Exception) { } - } - return toReturn; - } - - private static String getExtraItemInfo(int itemIndex, int itemAmount) - { - if (itemIndex == -1) return ""; - - string itemName = Game1.objectInformation[itemIndex].Split('/')[0]; - - if (itemAmount != -1) - return $"Required: {itemAmount} {itemName}"; - else - return $"Required: {itemName}"; - } - - private static String getPrice(int price) - { - if (price == -1) return ""; - - return $"Sell Price: {price} g"; - } - - private static String handleUnHighlightedItem(bool isHighlighted, int hoveredInventoryIndex) - { - if (isHighlighted) return ""; - - if (prevSlotIndex != hoveredInventoryIndex) - Game1.playSound("invalid-selection"); - - return " not usable here"; - } } } diff --git a/stardew-access/Patches/InventoryUtils.cs b/stardew-access/Patches/InventoryUtils.cs new file mode 100644 index 0000000..3362e4c --- /dev/null +++ b/stardew-access/Patches/InventoryUtils.cs @@ -0,0 +1,171 @@ + +using StardewValley; +using StardewValley.Menus; + +namespace stardew_access.Patches +{ + internal class InventoryUtils + { + internal static string hoveredItemQueryKey = ""; + internal static int prevSlotIndex = -999; + + internal static bool narrateHoveredItemInInventory(InventoryMenu inventoryMenu, List inventory, IList actualInventory, int x, int y, bool giveExtraDetails = false, int hoverPrice = -1, int extraItemToShowIndex = -1, int extraItemToShowAmount = -1) + { + for (int i = 0; i < inventory.Count; i++) + { + if (!inventory[i].containsPoint(x, y)) continue; + + if ((i + 1) > actualInventory.Count || actualInventory[i] == null) + { + // For empty slot + checkAndSpeak("Empty Slot", i); + prevSlotIndex = i; + return true; + } + + string toSpeak = ""; + string name = $"{actualInventory[i].DisplayName}{handleUnHighlightedItem(inventoryMenu.highlightMethod(actualInventory[i]), i)}"; + int stack = actualInventory[i].Stack; + string quality = getQualityFromItem(actualInventory[i]); + string healthNStamine = getHealthNStaminaFromItem(actualInventory[i]); + string buffs = getBuffsFromItem(actualInventory[i]); + string description = actualInventory[i].getDescription(); + string price = getPrice(hoverPrice); + string requirements = getExtraItemInfo(extraItemToShowIndex, extraItemToShowAmount); + + if (giveExtraDetails) + { + if (stack > 1) + toSpeak = $"{stack} {name} {quality}, \n{requirements}, \n{price}, \n{description}, \n{healthNStamine}, \n{buffs}"; + else + toSpeak = $"{name} {quality}, \n{requirements}, \n{price}, \n{description}, \n{healthNStamine}, \n{buffs}"; + } + else + { + if (stack > 1) + toSpeak = $"{stack} {name} {quality}, \n{requirements}, \n{price}"; + else + toSpeak = $"{name} {quality}, \n{requirements}, \n{price}"; + } + + + checkAndSpeak(toSpeak, i); + prevSlotIndex = i; + return true; + } + + // If no slot is hovered + return false; + } + + private static void checkAndSpeak(String toSpeak, int hoveredInventoryIndex) + { + if (hoveredItemQueryKey == $"{toSpeak}:{hoveredInventoryIndex}") return; + + hoveredItemQueryKey = $"{toSpeak}:{hoveredInventoryIndex}"; + MainClass.ScreenReader.Say(toSpeak, true); + } + + private static String getQualityFromItem(Item item) + { + if (item is not StardewValley.Object || ((StardewValley.Object)item).Quality <= 0) + return ""; + + int qualityIndex = ((StardewValley.Object)item).Quality; + if (qualityIndex == 1) + { + return "Silver quality"; + } + else if (qualityIndex == 2 || qualityIndex == 3) + { + return "Gold quality"; + } + else if (qualityIndex >= 4) + { + return "Iridium quality"; + } + + return ""; + } + + private static String getHealthNStaminaFromItem(Item item) + { + if (item is not StardewValley.Object || ((StardewValley.Object)item).Edibility == -300) + return ""; + + String toReturn = ""; + int stamina_recovery = ((StardewValley.Object)item).staminaRecoveredOnConsumption(); + toReturn += $"{stamina_recovery} Energy"; + + if (stamina_recovery < 0) return toReturn; + + int health_recovery = ((StardewValley.Object)item).healthRecoveredOnConsumption(); + toReturn += $"\n\t{health_recovery} Health"; + + return toReturn; + } + + private static String getBuffsFromItem(Item item) + { + if (item == null) return ""; + if (item is not StardewValley.Object) return ""; + if (((StardewValley.Object)item) == null) return ""; + + // These variables are taken from the game's code itself (IClickableMenu.cs -> 1016 line) + bool edibleItem = (int)((StardewValley.Object)item).Edibility != -300; + string[]? buffIconsToDisplay = (edibleItem && Game1.objectInformation[((StardewValley.Object)item).ParentSheetIndex].Split('/').Length > 7) + ? item.ModifyItemBuffs(Game1.objectInformation[((StardewValley.Object)item).ParentSheetIndex].Split('/')[7].Split(' ')) + : null; + + if (buffIconsToDisplay == null) + return ""; + + String toReturn = ""; + for (int j = 0; j < buffIconsToDisplay.Length; j++) + { + string buffName = ((Convert.ToInt32(buffIconsToDisplay[j]) > 0) ? "+" : "") + buffIconsToDisplay[j] + " "; + if (j <= 11) + { + buffName = Game1.content.LoadString("Strings\\UI:ItemHover_Buff" + j, buffName); + } + try + { + int count = int.Parse(buffName.Substring(0, buffName.IndexOf(' '))); + if (count != 0) + toReturn += $"{buffName}\n"; + } + catch (Exception) { } + } + return toReturn; + } + + private static String getExtraItemInfo(int itemIndex, int itemAmount) + { + if (itemIndex == -1) return ""; + + string itemName = Game1.objectInformation[itemIndex].Split('/')[0]; + + if (itemAmount != -1) + return $"Required: {itemAmount} {itemName}"; + else + return $"Required: {itemName}"; + } + + private static String getPrice(int price) + { + if (price == -1) return ""; + + return $"Sell Price: {price} g"; + } + + private static String handleUnHighlightedItem(bool isHighlighted, int hoveredInventoryIndex) + { + if (isHighlighted) return ""; + + if (prevSlotIndex != hoveredInventoryIndex) + Game1.playSound("invalid-selection"); + + return " not usable here"; + } + } +} diff --git a/stardew-access/Patches/MenuPatches.cs b/stardew-access/Patches/MenuPatches.cs index ca05742..739f83e 100644 --- a/stardew-access/Patches/MenuPatches.cs +++ b/stardew-access/Patches/MenuPatches.cs @@ -148,7 +148,6 @@ namespace stardew_access.Patches if (prevSlotIndex != i) { prevSlotIndex = i; - MainClass.DebugLog("here"); Game1.playSound("invalid-selection"); } } @@ -322,7 +321,6 @@ namespace stardew_access.Patches if (prevSlotIndex != i) { prevSlotIndex = i; - MainClass.DebugLog("here"); Game1.playSound("invalid-selection"); } } @@ -803,7 +801,8 @@ namespace stardew_access.Patches pondQueryMenuQuery = " "; } - GameMenuPatches.hoveredItemQueryKey = ""; + InventoryUtils.hoveredItemQueryKey = ""; + InventoryUtils.prevSlotIndex = -999; } #endregion