Decompiled source of SteadySigns v1.2.1

DeliberateInteract.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("Swiftfingers")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyInformationalVersion("1.2.1+781b304b1da83125f6c9e0d7c25f04eae9ef6832")]
[assembly: AssemblyProduct("DeliberateInteract")]
[assembly: AssemblyTitle("DeliberateInteract")]
[assembly: AssemblyVersion("1.2.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DeliberateInteract
{
	[BepInPlugin("com.swiftfingers.deliberateinteract", "Steady Signs", "1.2.1")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private sealed class PendingMount
		{
			public Player Player;

			public MonoBehaviour Stand;

			public ItemData Item;

			public int Index;

			public string Button;

			public float StartedAt;
		}

		[HarmonyPatch]
		private static class EditInteraction
		{
			private static IEnumerable<MethodBase> TargetMethods()
			{
				yield return AccessTools.Method(typeof(Sign), "Interact", (Type[])null, (Type[])null);
				yield return AccessTools.Method(typeof(TeleportWorld), "Interact", (Type[])null, (Type[])null);
			}

			private static bool Prefix(bool hold, bool alt)
			{
				return !hold && alt;
			}
		}

		[HarmonyPatch(typeof(Sign), "GetHoverText")]
		private static class SignHint
		{
			private static void Postfix(ref string __result)
			{
				ShowAlternateUse(ref __result, "$piece_use");
			}
		}

		[HarmonyPatch(typeof(TeleportWorld), "GetHoverText")]
		private static class PortalHint
		{
			private static void Postfix(ref string __result)
			{
				ShowAlternateUse(ref __result, "$piece_portal_settag");
			}
		}

		[HarmonyPatch(typeof(Player), "UseHotbarItem")]
		private static class StartMountHold
		{
			private static bool Prefix(Player __instance, int index)
			{
				if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer || index < 1 || index > 8)
				{
					return true;
				}
				string text = $"Hotbar{index}";
				if (!ZInput.GetButtonDown(text))
				{
					text += "Alt";
					if (!ZInput.GetButtonDown(text))
					{
						return true;
					}
				}
				MonoBehaviour mountTarget = GetMountTarget(((Humanoid)__instance).GetHoverObject());
				if (!Object.op_Implicit((Object)(object)mountTarget))
				{
					return true;
				}
				ItemData itemAt = ((Humanoid)__instance).GetInventory().GetItemAt(index - 1, 0);
				pendingMount = ((itemAt == null) ? null : new PendingMount
				{
					Player = __instance,
					Stand = mountTarget,
					Item = itemAt,
					Index = index,
					Button = text,
					StartedAt = Time.unscaledTime
				});
				return false;
			}
		}

		[HarmonyPatch(typeof(Player), "Update")]
		private static class FinishMountHold
		{
			private static void Postfix(Player __instance)
			{
				//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
				PendingMount pendingMount = Plugin.pendingMount;
				if (pendingMount == null || (Object)(object)__instance != (Object)(object)Player.m_localPlayer)
				{
					return;
				}
				GameObject hoverObject = ((Humanoid)__instance).GetHoverObject();
				if ((Object)(object)pendingMount.Player != (Object)(object)__instance || !Object.op_Implicit((Object)(object)pendingMount.Stand) || !ZInput.GetButton(pendingMount.Button) || !CanTakeInput(__instance) || Hud.InRadial() || (Object)(object)GetMountTarget(hoverObject) != (Object)(object)pendingMount.Stand || ((Humanoid)__instance).GetInventory().GetItemAt(pendingMount.Index - 1, 0) != pendingMount.Item)
				{
					Plugin.pendingMount = null;
				}
				else if (!(Time.unscaledTime - pendingMount.StartedAt < 0.2f))
				{
					Plugin.pendingMount = null;
					if ((!(pendingMount.Stand is ItemStand) || PrivateArea.CheckAccess(((Component)pendingMount.Stand).transform.position, 0f, true, false)) && ((Interactable)pendingMount.Stand).UseItem((Humanoid)(object)__instance, pendingMount.Item))
					{
						AnimateUse((Humanoid)(object)__instance, hoverObject);
					}
				}
			}
		}

		[HarmonyPatch(typeof(ItemStand), "GetHoverText")]
		private static class ItemStandMountHint
		{
			private static void Postfix(ItemStand __instance, ref string __result)
			{
				if ((Object)(object)__instance.m_guardianPower == (Object)null)
				{
					ShowHoldHotbar(ref __result);
				}
			}
		}

		[HarmonyPatch(typeof(Switch), "Interact")]
		private static class ArmorRemoval
		{
			private static bool Prefix(Switch __instance, ref bool hold)
			{
				if (!IsArmorSlot(__instance))
				{
					return true;
				}
				if (!hold)
				{
					return false;
				}
				hold = false;
				return true;
			}
		}

		[HarmonyPatch(typeof(Switch), "GetHoverText")]
		private static class ArmorRemovalHint
		{
			private static void Postfix(Switch __instance, ref string __result)
			{
				if (IsArmorSlot(__instance))
				{
					ShowHoldHotbar(ref __result);
					string binding = (ZInput.IsGamepadActive() ? "<b>$ui_hold $KEY_Use</b>" : "[<color=yellow><b>$ui_hold $KEY_Use</b></color>]");
					ReplaceUseHint(ref __result, "$piece_itemstand_take", binding);
				}
			}
		}

		private const string Id = "com.swiftfingers.deliberateinteract";

		private const string Version = "1.2.1";

		private const float MountHoldSeconds = 0.2f;

		private static PendingMount pendingMount;

		private static readonly Func<Player, bool> CanTakeInput = AccessTools.MethodDelegate<Func<Player, bool>>(AccessTools.Method(typeof(Player), "TakeInput", (Type[])null, (Type[])null), (object)null, false);

		private static readonly Action<Humanoid, GameObject> AnimateUse = AccessTools.MethodDelegate<Action<Humanoid, GameObject>>(AccessTools.Method(typeof(Humanoid), "DoInteractAnimation", (Type[])null, (Type[])null), (object)null, true);

		private void Awake()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			if (!Application.isBatchMode)
			{
				new Harmony("com.swiftfingers.deliberateinteract").PatchAll(typeof(Plugin).Assembly);
			}
		}

		private static void ShowAlternateUse(ref string text, string action)
		{
			string text2 = ((ZInput.IsNonClassicFunctionality() && ZInput.IsGamepadActive()) ? "$KEY_AltKeys" : "$KEY_AltPlace");
			ReplaceUseHint(ref text, action, "[<color=yellow><b>" + text2 + " + $KEY_Use</b></color>]");
		}

		private static void ReplaceUseHint(ref string text, string action, string binding)
		{
			string text2 = Localization.instance.Localize("\n[<color=yellow><b>$KEY_Use</b></color>] " + action);
			if (text.EndsWith(text2, StringComparison.Ordinal))
			{
				text = text.Substring(0, text.Length - text2.Length) + Localization.instance.Localize("\n" + binding + " " + action);
			}
		}

		private static bool IsArmorSlot(Switch control)
		{
			object? obj = ((Delegate)(object)control.m_onUse)?.Target;
			ArmorStand val = (ArmorStand)(((obj is ArmorStand) ? obj : null) ?? ((Component)control).GetComponentInParent<ArmorStand>());
			if ((Object)(object)val == (Object)null || (Object)(object)control == (Object)(object)val.m_changePoseSwitch)
			{
				return false;
			}
			foreach (ArmorStandSlot slot in val.m_slots)
			{
				if ((Object)(object)slot.m_switch == (Object)(object)control)
				{
					return true;
				}
			}
			return false;
		}

		private static MonoBehaviour GetMountTarget(GameObject hovered)
		{
			if (!Object.op_Implicit((Object)(object)hovered) || hovered.GetComponentInParent<Hoverable>() == null)
			{
				return null;
			}
			Interactable componentInParent = hovered.GetComponentInParent<Interactable>();
			ItemStand val = (ItemStand)(object)((componentInParent is ItemStand) ? componentInParent : null);
			if (val != null && (Object)(object)val.m_guardianPower == (Object)null)
			{
				return (MonoBehaviour)(object)val;
			}
			Switch val2 = (Switch)(object)((componentInParent is Switch) ? componentInParent : null);
			if (val2 == null || !IsArmorSlot(val2))
			{
				return null;
			}
			return (MonoBehaviour)(object)val2;
		}

		private static void ShowHoldHotbar(ref string text)
		{
			if (!ZInput.IsGamepadActive())
			{
				text = text.Replace(Localization.instance.Localize("[<color=yellow><b>$KEY_HotbarUse</b></color>] $piece_itemstand_attach"), Localization.instance.Localize("[<color=yellow><b>$KEY_HotbarUse</b></color>] $piece_itemstand_attach".Replace("$KEY_HotbarUse", "$ui_hold $KEY_HotbarUse")));
			}
		}
	}
}