From ea5cd807bf93a4a8a419b76678f5b686be12d01f Mon Sep 17 00:00:00 2001 From: Abbysssal Date: Mon, 20 Jun 2022 01:39:27 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Redo=20the=20interactable=20obje?= =?UTF-8?q?ct=20highlighting=20patch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the `PlayfieldObject.EnterDetails` patch with a `PlayfieldObject.get_interactable` patch. The former's vanilla method is way too fine-tuned to simply replace it. --- .../Interactions/InteractionModel.cs | 17 ++-- .../Interactions/VanillaInteractions.cs | 5 -- .../Interactions/VanillaInteractions/Altar.cs | 1 - .../VanillaInteractions/ArcadeGame.cs | 1 - .../Interactions/VanillaInteractions/Bars.cs | 1 - .../VanillaInteractions/Television.cs | 1 - .../VanillaInteractions/TrapDoor.cs | 1 - .../VanillaInteractions/Turret.cs | 1 - .../VanillaInteractions/_NoInteractions.cs | 52 ----------- RogueLibsCore/Patches/Patches_Interactions.cs | 87 ++++++------------- 10 files changed, 35 insertions(+), 132 deletions(-) diff --git a/RogueLibsCore/Interactions/InteractionModel.cs b/RogueLibsCore/Interactions/InteractionModel.cs index ecda7fd7f..256fee014 100644 --- a/RogueLibsCore/Interactions/InteractionModel.cs +++ b/RogueLibsCore/Interactions/InteractionModel.cs @@ -35,7 +35,7 @@ public void StopInteraction(bool forced) } shouldStop = false; forcedStop = true; - OriginalStopInteraction(Instance); + if (!fakingInteraction) OriginalStopInteraction(Instance); } protected override void Initialize() { } @@ -184,24 +184,27 @@ private void OnPressedButton2(string buttonName) // refresh the buttons (restarts the cycle) Agent.worldSpaceGUI?.StartCoroutine(Agent.worldSpaceGUI.RefreshObjectButtons2(Object)); } - public bool ShouldBeHighlighted(Agent agent) + + private static string? lastLog; + private bool fakingInteraction; + public bool IsInteractable(Agent agent) { - Agent prevAgent = Instance.interactingAgent; + Agent? prevAgent = Instance.interactingAgent; try { + fakingInteraction = true; RogueLibsPlugin.useModelStopInteraction = true; Instance.interactingAgent = agent; - bool res = ShouldBeHighlighted2(); - // RogueFramework.LogWarning($"{Instance} will{(res ? null : " not")} be highlighted."); - return res; + return IsInteractable2(); } finally { + fakingInteraction = false; RogueLibsPlugin.useModelStopInteraction = false; Instance.interactingAgent = prevAgent; } } - private bool ShouldBeHighlighted2() + private bool IsInteractable2() { // reset state interactions.Clear(); diff --git a/RogueLibsCore/Interactions/VanillaInteractions.cs b/RogueLibsCore/Interactions/VanillaInteractions.cs index 3519ec0d4..133058a73 100644 --- a/RogueLibsCore/Interactions/VanillaInteractions.cs +++ b/RogueLibsCore/Interactions/VanillaInteractions.cs @@ -124,11 +124,6 @@ public static void PatchInteractFar() where T : PlayfieldObject { patcher.Prefix(typeof(T), nameof(PlayfieldObject.InteractFar), nameof(RogueLibsPlugin.InteractFarHook)); } - public static void MakeInteractable() where T : PlayfieldObject - { - patcher.Postfix(typeof(T), "Awake", nameof(RogueLibsPlugin.AwakeInteractableHook)); - patcher.Postfix(typeof(T), nameof(PlayfieldObject.RecycleAwake), nameof(RogueLibsPlugin.RecycleAwakeInteractableHook)); - } [AttributeUsage(AttributeTargets.Method), MeansImplicitUse] internal class IncludeAttribute : Attribute { } diff --git a/RogueLibsCore/Interactions/VanillaInteractions/Altar.cs b/RogueLibsCore/Interactions/VanillaInteractions/Altar.cs index 7a07d1a36..9d279ab15 100644 --- a/RogueLibsCore/Interactions/VanillaInteractions/Altar.cs +++ b/RogueLibsCore/Interactions/VanillaInteractions/Altar.cs @@ -8,7 +8,6 @@ private static void Patch_Altar() Patch(Params2); PatchInteract(); PatchInteractFar(); - MakeInteractable(); RogueInteractions.CreateProvider(static h => { diff --git a/RogueLibsCore/Interactions/VanillaInteractions/ArcadeGame.cs b/RogueLibsCore/Interactions/VanillaInteractions/ArcadeGame.cs index b670c923c..71ec636a6 100644 --- a/RogueLibsCore/Interactions/VanillaInteractions/ArcadeGame.cs +++ b/RogueLibsCore/Interactions/VanillaInteractions/ArcadeGame.cs @@ -8,7 +8,6 @@ private static void Patch_ArcadeGame() Patch(Params1); PatchInteract(); PatchInteractFar(); - MakeInteractable(); RogueInteractions.CreateProvider(static h => { diff --git a/RogueLibsCore/Interactions/VanillaInteractions/Bars.cs b/RogueLibsCore/Interactions/VanillaInteractions/Bars.cs index 1a0725b79..bffc74d98 100644 --- a/RogueLibsCore/Interactions/VanillaInteractions/Bars.cs +++ b/RogueLibsCore/Interactions/VanillaInteractions/Bars.cs @@ -8,7 +8,6 @@ public static partial class VanillaInteractions private static void Patch_Bars() { PatchInteract(); - MakeInteractable(); RogueLibs.CreateCustomName("InteractWithAgent", NameTypes.Interface, new CustomNameInfo { diff --git a/RogueLibsCore/Interactions/VanillaInteractions/Television.cs b/RogueLibsCore/Interactions/VanillaInteractions/Television.cs index d02ec6155..e576a960c 100644 --- a/RogueLibsCore/Interactions/VanillaInteractions/Television.cs +++ b/RogueLibsCore/Interactions/VanillaInteractions/Television.cs @@ -9,7 +9,6 @@ private static void Patch_Television() { Patch(Params1); PatchInteractFar(); - MakeInteractable(); RogueInteractions.CreateProvider(static h => { diff --git a/RogueLibsCore/Interactions/VanillaInteractions/TrapDoor.cs b/RogueLibsCore/Interactions/VanillaInteractions/TrapDoor.cs index 00279a815..d729be5bb 100644 --- a/RogueLibsCore/Interactions/VanillaInteractions/TrapDoor.cs +++ b/RogueLibsCore/Interactions/VanillaInteractions/TrapDoor.cs @@ -8,7 +8,6 @@ private static void Patch_TrapDoor() Patch(Params1); PatchInteract(); PatchInteractFar(); - MakeInteractable(); RogueInteractions.CreateProvider(static h => { diff --git a/RogueLibsCore/Interactions/VanillaInteractions/Turret.cs b/RogueLibsCore/Interactions/VanillaInteractions/Turret.cs index 9e2bdb777..070ea443d 100644 --- a/RogueLibsCore/Interactions/VanillaInteractions/Turret.cs +++ b/RogueLibsCore/Interactions/VanillaInteractions/Turret.cs @@ -8,7 +8,6 @@ private static void Patch_Turret() Patch(Params2); PatchInteract(); PatchInteractFar(); - MakeInteractable(); RogueInteractions.CreateProvider(static h => { diff --git a/RogueLibsCore/Interactions/VanillaInteractions/_NoInteractions.cs b/RogueLibsCore/Interactions/VanillaInteractions/_NoInteractions.cs index e7d234d8d..6b6077f6f 100644 --- a/RogueLibsCore/Interactions/VanillaInteractions/_NoInteractions.cs +++ b/RogueLibsCore/Interactions/VanillaInteractions/_NoInteractions.cs @@ -5,67 +5,15 @@ public static partial class VanillaInteractions [Include] private static void Patch_NoInteractions() { - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - - MakeInteractable(); PatchInteract(); - - MakeInteractable(); - - MakeInteractable(); PatchInteract(); - - MakeInteractable(); PatchInteract(); - - MakeInteractable(); - - MakeInteractable(); PatchInteract(); - - MakeInteractable(); - - MakeInteractable(); - MakeInteractable(); - - MakeInteractable(); - MakeInteractable(); - - MakeInteractable(); PatchInteract(); - - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - - MakeInteractable(); PatchInteract(); - - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - MakeInteractable(); - - MakeInteractable(); PatchInteract(); - - MakeInteractable(); - - MakeInteractable(); PatchInteract(); - MakeInteractable(); - - } } } diff --git a/RogueLibsCore/Patches/Patches_Interactions.cs b/RogueLibsCore/Patches/Patches_Interactions.cs index d58f076b7..ca6a9c519 100644 --- a/RogueLibsCore/Patches/Patches_Interactions.cs +++ b/RogueLibsCore/Patches/Patches_Interactions.cs @@ -20,7 +20,18 @@ public void PatchInteractions() Patcher.Prefix(typeof(PlayfieldObject), nameof(PlayfieldObject.StopInteraction), new Type[1] { typeof(bool) }); - Patcher.Prefix(typeof(InteractionHelper), "EnterDetails"); + Patcher.Prefix(typeof(InteractionHelper), "EnterDetails", nameof(InteractionHelper_EnterDetails_Prefix)); + Patcher.Finalizer(typeof(InteractionHelper), "EnterDetails", nameof(InteractionHelper_EnterDetails_Finalizer)); + Patcher.Prefix(typeof(InteractionHelper), nameof(InteractionHelper.UpdateInteractionHelper), + nameof(InteractionHelper_UpdateInteractionHelper_Prefix)); + Patcher.Finalizer(typeof(InteractionHelper), nameof(InteractionHelper.UpdateInteractionHelper), + nameof(InteractionHelper_UpdateInteractionHelper_Finalizer)); + + MethodInfo interactableGetter = AccessTools.PropertyGetter(typeof(PlayfieldObject), nameof(PlayfieldObject.interactable)); + Harmony harmony = Patcher.GetHarmony(); + harmony.Patch(interactableGetter, new HarmonyMethod(AccessTools.Method(typeof(RogueLibsPlugin), nameof(PlayfieldObject_get_interactable)))); + + Patcher.Prefix(typeof(InteractionHelper), nameof(InteractionHelper.canInteractCounter)); VanillaInteractions.PatchAll(); @@ -149,14 +160,6 @@ public static bool InteractFarHook(PlayfieldObject __instance, Agent agent) __instance.ShowObjectButtons(); return false; } - public static void AwakeInteractableHook(PlayfieldObject __instance) - { - __instance.interactable = true; - } - public static void RecycleAwakeInteractableHook(PlayfieldObject __instance) - { - __instance.interactable = true; - } internal static bool useModelStopInteraction; public static bool PlayfieldObject_StopInteraction(PlayfieldObject __instance) @@ -170,63 +173,23 @@ public static bool PlayfieldObject_StopInteraction(PlayfieldObject __instance) } private static readonly FieldInfo interactionHelperAgentField = AccessTools.Field(typeof(InteractionHelper), "agent"); - public static bool InteractionHelper_EnterDetails(InteractionHelper __instance, Collider2D other) + private static Agent? useModelInteractable; + public static void InteractionHelper_EnterDetails_Prefix(InteractionHelper __instance) + => useModelInteractable = (Agent)interactionHelperAgentField.GetValue(__instance); + public static void InteractionHelper_EnterDetails_Finalizer() => useModelInteractable = null; + public static void InteractionHelper_UpdateInteractionHelper_Prefix(InteractionHelper __instance) + => useModelInteractable = (Agent)interactionHelperAgentField.GetValue(__instance); + public static void InteractionHelper_UpdateInteractionHelper_Finalizer() => useModelInteractable = null; + + public static bool PlayfieldObject_get_interactable(PlayfieldObject __instance, ref bool __result) { - __instance.EnterRange(other.gameObject); - - Agent agent = (Agent)interactionHelperAgentField.GetValue(__instance); - Vector3 pointPos = agent.tr.position; - - float itemDistance = float.MaxValue; - float objectDistance = float.MaxValue; - __instance.closestItem = null; - __instance.closestObject = null; - foreach (GameObject obj in __instance.TriggerList) - { - PlayfieldObject playfieldObj = obj.GetComponent() - ?? obj.GetComponent()?.playFieldObject - ?? obj.transform.parent.GetComponent() - ?? obj.transform.parent.GetComponent().playFieldObject; - InteractionModel model = GetOrCreateModel(playfieldObj); - - float dist = Vector3.Distance(playfieldObj.tr.position, pointPos); - if (obj.CompareTag("AgentSprite") || obj.CompareTag("ObjectRealSprite")) - { - if (dist < objectDistance && model.ShouldBeHighlighted(agent)) - { - objectDistance = dist; - __instance.closestObject = obj; - } - } - else if (obj.CompareTag("ItemImage")) - { - if (dist < itemDistance && model.ShouldBeHighlighted(agent)) - { - itemDistance = dist; - __instance.closestItem = obj; - } - } - } - foreach (GameObject obj in __instance.TriggerList) - { - PlayfieldObject playfieldObj = obj.GetComponent() - ?? obj.GetComponent()?.playFieldObject - ?? obj.transform.parent.GetComponent() - ?? obj.transform.parent.GetComponent().playFieldObject; - - if (obj == __instance.closestItem || obj == __instance.closestObject) - { - try { playfieldObj.objectSprite.SetHighlight("Normal", agent); } catch { } - } - else - { - try { playfieldObj.objectSprite.SetHighlight("Off", agent); } catch { } - } - } - + if (useModelInteractable is null) return true; + InteractionModel model = GetOrCreateModel(__instance); + __result = model.IsInteractable(useModelInteractable); return false; } + public static bool InteractionHelper_canInteractCounter() => true; } }