From 8b0350497bf7a3d92fa141e86045142f83995a11 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Wed, 22 Feb 2023 17:59:11 +0530 Subject: [PATCH] Refactored code --- stardew-access/HarmonyPatches.cs | 7 +- stardew-access/Patches/IClickableMenuPatch.cs | 123 ++++++++++++++++++ stardew-access/Patches/MenuPatches.cs | 119 +---------------- 3 files changed, 130 insertions(+), 119 deletions(-) create mode 100644 stardew-access/Patches/IClickableMenuPatch.cs diff --git a/stardew-access/HarmonyPatches.cs b/stardew-access/HarmonyPatches.cs index 056999f..eb0fa7e 100644 --- a/stardew-access/HarmonyPatches.cs +++ b/stardew-access/HarmonyPatches.cs @@ -225,7 +225,7 @@ namespace stardew_access #region On Menu CLose Patch harmony.Patch( original: AccessTools.Method(typeof(IClickableMenu), nameof(IClickableMenu.exitThisMenu)), - postfix: new HarmonyMethod(typeof(MenuPatches), nameof(MenuPatches.IClickableMenuOnExitPatch)) + postfix: new HarmonyMethod(typeof(IClickableMenuPatch), nameof(IClickableMenuPatch.ExitThisMenuPatch)) ); harmony.Patch( original: AccessTools.Method(typeof(Game1), nameof(Game1.exitActiveMenu)), @@ -290,6 +290,11 @@ namespace stardew_access original: AccessTools.Method(typeof(TextBox), nameof(TextBox.Draw)), prefix: new HarmonyMethod(typeof(TextBoxPatch), nameof(TextBoxPatch.DrawPatch)) ); + + harmony.Patch( + original: AccessTools.Method(typeof(IClickableMenu), nameof(IClickableMenu.draw)), + prefix: new HarmonyMethod(typeof(IClickableMenuPatch), nameof(IClickableMenuPatch.DrawPatch)) + ); } } } diff --git a/stardew-access/Patches/IClickableMenuPatch.cs b/stardew-access/Patches/IClickableMenuPatch.cs new file mode 100644 index 0000000..1a3992a --- /dev/null +++ b/stardew-access/Patches/IClickableMenuPatch.cs @@ -0,0 +1,123 @@ +using StardewValley.Menus; + +namespace stardew_access.Patches +{ + // These patches are global, i.e. work on every menus + internal class IClickableMenuPatch + { + internal static void ExitThisMenuPatch(IClickableMenu __instance) + { + try + { + Cleanup(__instance); + } + catch (Exception e) + { + MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}"); + } + } + + internal static void Cleanup(IClickableMenu menu) + { + if (menu is LetterViewerMenu) + { + DialoguePatches.currentLetterText = " "; + } + else if (menu is LevelUpMenu) + { + MenuPatches.currentLevelUpTitle = " "; + } + else if (menu is Billboard) + { + QuestPatches.currentDailyQuestText = " "; + } + else if (menu is GameMenu) + { + GameMenuPatches.gameMenuQueryKey = ""; + GameMenuPatches.craftingPageQueryKey = ""; + GameMenuPatches.inventoryPageQueryKey = ""; + GameMenuPatches.exitPageQueryKey = ""; + GameMenuPatches.optionsPageQueryKey = ""; + GameMenuPatches.socialPageQuery = ""; + GameMenuPatches.currentSelectedCraftingRecipe = -1; + GameMenuPatches.isSelectingRecipe = false; + } + else if (menu is JunimoNoteMenu) + { + BundleMenuPatches.currentIngredientListItem = -1; + BundleMenuPatches.currentIngredientInputSlot = -1; + BundleMenuPatches.currentInventorySlot = -1; + BundleMenuPatches.junimoNoteMenuQuery = ""; + } + else if (menu is ShopMenu) + { + GameMenuPatches.shopMenuQueryKey = ""; + } + else if (menu is ItemGrabMenu) + { + GameMenuPatches.itemGrabMenuQueryKey = ""; + } + else if (menu is GeodeMenu) + { + GameMenuPatches.geodeMenuQueryKey = ""; + } + else if (menu is CarpenterMenu) + { + BuildingNAnimalMenuPatches.carpenterMenuQuery = ""; + BuildingNAnimalMenuPatches.isUpgrading = false; + BuildingNAnimalMenuPatches.isDemolishing = false; + BuildingNAnimalMenuPatches.isPainting = false; + BuildingNAnimalMenuPatches.isMoving = false; + BuildingNAnimalMenuPatches.isConstructing = false; + BuildingNAnimalMenuPatches.carpenterMenu = null; + } + else if (menu is PurchaseAnimalsMenu) + { + BuildingNAnimalMenuPatches.purchaseAnimalMenuQuery = ""; + BuildingNAnimalMenuPatches.firstTimeInNamingMenu = true; + BuildingNAnimalMenuPatches.purchaseAnimalsMenu = null; + } + else if (menu is DialogueBox) + { + DialoguePatches.isDialogueAppearingFirstTime = true; + DialoguePatches.currentDialogue = " "; + } + else if (menu is JojaCDMenu) + { + BundleMenuPatches.jojaCDMenuQuery = ""; + } + else if (menu is QuestLog) + { + QuestPatches.questLogQuery = " "; + } + else if (menu is TailoringMenu) + { + MenuPatches.tailoringMenuQuery = " "; + } + else if (menu is ForgeMenu) + { + MenuPatches.forgeMenuQuery = " "; + } + else if (menu is ItemListMenu) + { + MenuPatches.itemListMenuQuery = " "; + } + else if (menu is FieldOfficeMenu) + { + DonationMenuPatches.fieldOfficeMenuQuery = " "; + } + else if (menu is MuseumMenu) + { + DonationMenuPatches.museumQueryKey = " "; + } + else if (menu is PondQueryMenu) + { + MenuPatches.pondQueryMenuQuery = " "; + } + + InventoryUtils.hoveredItemQueryKey = ""; + InventoryUtils.prevSlotIndex = -999; + TextBoxPatch.activeTextBoxes = ""; + } + } +} diff --git a/stardew-access/Patches/MenuPatches.cs b/stardew-access/Patches/MenuPatches.cs index 5625593..2576c40 100644 --- a/stardew-access/Patches/MenuPatches.cs +++ b/stardew-access/Patches/MenuPatches.cs @@ -604,12 +604,11 @@ namespace stardew_access.Patches } } - #region Cleanup on exitting a menu internal static void Game1ExitActiveMenuPatch() { try { - Cleanup(Game1.activeClickableMenu); + IClickableMenuPatch.Cleanup(Game1.activeClickableMenu); } catch (Exception e) { @@ -617,122 +616,6 @@ namespace stardew_access.Patches } } - internal static void IClickableMenuOnExitPatch(IClickableMenu __instance) - { - try - { - Cleanup(__instance); - } - catch (Exception e) - { - MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}"); - } - } - - private static void Cleanup(IClickableMenu menu) - { - if (menu is LetterViewerMenu) - { - DialoguePatches.currentLetterText = " "; - } - else if (menu is LevelUpMenu) - { - currentLevelUpTitle = " "; - } - else if (menu is Billboard) - { - QuestPatches.currentDailyQuestText = " "; - } - else if (menu is GameMenu) - { - GameMenuPatches.gameMenuQueryKey = ""; - GameMenuPatches.craftingPageQueryKey = ""; - GameMenuPatches.inventoryPageQueryKey = ""; - GameMenuPatches.exitPageQueryKey = ""; - GameMenuPatches.optionsPageQueryKey = ""; - GameMenuPatches.socialPageQuery = ""; - GameMenuPatches.currentSelectedCraftingRecipe = -1; - GameMenuPatches.isSelectingRecipe = false; - } - else if (menu is JunimoNoteMenu) - { - BundleMenuPatches.currentIngredientListItem = -1; - BundleMenuPatches.currentIngredientInputSlot = -1; - BundleMenuPatches.currentInventorySlot = -1; - BundleMenuPatches.junimoNoteMenuQuery = ""; - } - else if (menu is ShopMenu) - { - GameMenuPatches.shopMenuQueryKey = ""; - } - else if (menu is ItemGrabMenu) - { - GameMenuPatches.itemGrabMenuQueryKey = ""; - } - else if (menu is GeodeMenu) - { - GameMenuPatches.geodeMenuQueryKey = ""; - } - else if (menu is CarpenterMenu) - { - BuildingNAnimalMenuPatches.carpenterMenuQuery = ""; - BuildingNAnimalMenuPatches.isUpgrading = false; - BuildingNAnimalMenuPatches.isDemolishing = false; - BuildingNAnimalMenuPatches.isPainting = false; - BuildingNAnimalMenuPatches.isMoving = false; - BuildingNAnimalMenuPatches.isConstructing = false; - BuildingNAnimalMenuPatches.carpenterMenu = null; - } - else if (menu is PurchaseAnimalsMenu) - { - BuildingNAnimalMenuPatches.purchaseAnimalMenuQuery = ""; - BuildingNAnimalMenuPatches.firstTimeInNamingMenu = true; - BuildingNAnimalMenuPatches.purchaseAnimalsMenu = null; - } - else if (menu is DialogueBox) - { - DialoguePatches.isDialogueAppearingFirstTime = true; - DialoguePatches.currentDialogue = " "; - } - else if (menu is JojaCDMenu) - { - BundleMenuPatches.jojaCDMenuQuery = ""; - } - else if (menu is QuestLog) - { - QuestPatches.questLogQuery = " "; - } - else if (menu is TailoringMenu) - { - tailoringMenuQuery = " "; - } - else if (menu is ForgeMenu) - { - forgeMenuQuery = " "; - } - else if (menu is ItemListMenu) - { - itemListMenuQuery = " "; - } - else if (menu is FieldOfficeMenu) - { - DonationMenuPatches.fieldOfficeMenuQuery = " "; - } - else if (menu is MuseumMenu) - { - DonationMenuPatches.museumQueryKey = " "; - } - else if (menu is PondQueryMenu) - { - pondQueryMenuQuery = " "; - } - - InventoryUtils.hoveredItemQueryKey = ""; - InventoryUtils.prevSlotIndex = -999; - TextBoxPatch.activeTextBoxes = ""; - } - #endregion - internal static void ExitEventPatch() { if (MainClass.ScreenReader != null)