diff --git a/BepInEx5Plugins.Ash.Alisa.Closet/BepInEx5Plugins.Ash.Alisa.Closet.csproj b/BepInEx5Plugins.Ash.Alisa.Closet/BepInEx5Plugins.Ash.Alisa.Closet.csproj new file mode 100644 index 0000000..20fe7bf --- /dev/null +++ b/BepInEx5Plugins.Ash.Alisa.Closet/BepInEx5Plugins.Ash.Alisa.Closet.csproj @@ -0,0 +1,37 @@ + + + + net35 + BepInEx5Plugins.Ash.Alisa.Closet + Closet + 1.0.0 + true + 9.0 + + + + + + + + + + + + + + + + ..\lib\SteamRelease\Assembly-CSharp.dll + false + + + ..\lib\UnityEngine\UnityEngine.UI.dll + false + + + + + + + diff --git a/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_Awake.cs b/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_Awake.cs new file mode 100644 index 0000000..fb55cf7 --- /dev/null +++ b/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_Awake.cs @@ -0,0 +1,69 @@ +using HarmonyLib; +using System; +using UnityEngine; + +namespace BepInEx5Plugins.Ash.Alisa.Closet.HarmonyPatches +{ + [HarmonyPatch(typeof(ButtonSelected), "Awake")] + public class ButtonSelected_Awake + { + // Forcibly assign null fields to appopriate values if possible. + public static void Postfix(ButtonSelected __instance) + { + if (!__instance.slot_Button01) + { + Console.WriteLine("ButtonSelected_Awake.Postfix: Force initialization of slot_Button01"); + + __instance.slot_Button01 = __instance.buttonSet01?.transform.parent?.Find("SlotButtons")?.Find("Slot01")?.gameObject; + } + + if (!__instance.slot_Button02) + { + Console.WriteLine("ButtonSelected_Awake.Postfix: Force initialization of slot_Button02"); + + __instance.slot_Button02 = __instance.buttonSet02?.transform.parent?.Find("SlotButtons")?.Find("Slot02")?.gameObject; + } + + if (!__instance.slot_Button03) + { + Console.WriteLine("ButtonSelected_Awake.Postfix: Force initialization of slot_Button03"); + + __instance.slot_Button03 = __instance.buttonSet03?.transform.parent?.Find("SlotButtons")?.Find("Slot03")?.gameObject; + } + + if (!__instance.myHighlightedBackground) + { + Console.WriteLine("ButtonSelected_Awake.Postfix: Force initialization of myHighlightedBackground"); + + var target = (GameObject)null; + + if (__instance.slotButton01) + { + target = __instance.buttonSet01; + } + else if (__instance.slotButton02) + { + target = __instance.buttonSet02; + } + else if (__instance.slotButton03) + { + target = __instance.buttonSet03; + } + + if (target) + { + for (var i = 0; i < target.transform.childCount; ++i) + { + var child = target.transform.GetChild(i); + + if (child.name.StartsWith("ClosetSlotBackground")) + { + __instance.myHighlightedBackground = child.gameObject; + break; + } + } + } + } + } + } +} diff --git a/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_OnDeselect.cs b/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_OnDeselect.cs new file mode 100644 index 0000000..adb0c1c --- /dev/null +++ b/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_OnDeselect.cs @@ -0,0 +1,16 @@ +using HarmonyLib; +using System; +using UnityEngine.EventSystems; + +namespace BepInEx5Plugins.Ash.Alisa.Closet.HarmonyPatches +{ + [HarmonyPatch(typeof(ButtonSelected), "OnDeselect", new Type[] { typeof(BaseEventData) })] + public class ButtonSelected_OnDeselect + { + // Enable button selection auto fix. + public static void Prefix(ButtonSelected __instance, BaseEventData eventData) + { + ClosetButtonsSorting_Update.checkPending = true; + } + } +} diff --git a/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_OnSelect.cs b/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_OnSelect.cs new file mode 100644 index 0000000..2dcdefa --- /dev/null +++ b/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ButtonSelected_OnSelect.cs @@ -0,0 +1,16 @@ +using HarmonyLib; +using System; +using UnityEngine.EventSystems; + +namespace BepInEx5Plugins.Ash.Alisa.Closet.HarmonyPatches +{ + [HarmonyPatch(typeof(ButtonSelected), "OnSelect", new Type[] { typeof(BaseEventData) })] + public class ButtonSelected_OnSelect + { + // Disable button selection auto fix. + public static void Prefix(ButtonSelected __instance, BaseEventData eventData) + { + ClosetButtonsSorting_Update.checkPending = false; + } + } +} diff --git a/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ClosetButtonsSorting_Update.cs b/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ClosetButtonsSorting_Update.cs new file mode 100644 index 0000000..fdc5dd1 --- /dev/null +++ b/BepInEx5Plugins.Ash.Alisa.Closet/HarmonyPatches/ClosetButtonsSorting_Update.cs @@ -0,0 +1,120 @@ +using HarmonyLib; +using System; +using System.Reflection; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; +using Object = UnityEngine.Object; + +namespace BepInEx5Plugins.Ash.Alisa.Closet.HarmonyPatches +{ + [HarmonyPatch(typeof(ClosetButtonsSorting), "Update")] + public class ClosetButtonsSorting_Update + { + public static float checkDelay = 1f; + + public static bool checkPending; + + public static float timer; + + private static FieldInfo ButtonSelected_selected; + + public static bool Prepare(MethodBase original) + { + if (original is null) + { + try + { + ButtonSelected_selected = typeof(ButtonSelected).GetField("selected", BindingFlags.NonPublic | BindingFlags.Instance); + } + catch (Exception exception) + { + Console.WriteLine(exception); + return false; + } + } + + return true; + } + + // Auto fix button selection if necessary. + public static void Postfix(ClosetButtonsSorting __instance) + { + if (!checkPending) + { + return; + } + + var equipmentMenu = __instance.GetComponent(); + + if (!equipmentMenu || !equipmentMenu.charlotteIsActive) + { + return; + } + + if (timer >= checkDelay) + { + timer -= checkDelay; + + if (!HasSelection()) + { + SelectionFix(); + } + } + + timer += Time.deltaTime; + } + + public static bool HasSelection() + { + foreach (var buttonSelected in Object.FindObjectsOfType()) + { + if ((bool)ButtonSelected_selected.GetValue(buttonSelected)) + { + return true; + } + } + + return false; + } + + public static void SelectionFix() + { + checkPending = false; + + if (!EventSystem.current) + { + return; + } + + var selectedObject = EventSystem.current.currentSelectedGameObject; + + if (!selectedObject) + { + return; + } + + Console.WriteLine("ClosetButtonsSorting_Update.SelectionFix: Forcibly reset selection to game object " + selectedObject); + + var buttonSelected = selectedObject.GetComponent(); + + if (!buttonSelected) + { + return; + } + + if (buttonSelected.slotButton01 || buttonSelected.itemButtonSlot01) + { + buttonSelected.slot_Button01?.GetComponent