Decompiled source of Double Tap Forsaken Power v1.0.1

BepInEx/plugins/Double_Tap_Forsaken_Power/Double_Tap_Forsaken_Power.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Double_Tap_Forsaken_Power")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Client-only double tap for Valheim's configured guardian power keyboard shortcut.")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("Double_Tap_Forsaken_Power")]
[assembly: AssemblyTitle("Double_Tap_Forsaken_Power")]
[assembly: AssemblyVersion("1.0.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 Double_Tap_Forsaken_Power
{
	internal sealed class DoubleTapGate
	{
		private bool armed;

		private double firstPressTime;

		private int lastPressFrame = -1;

		internal bool Filter(bool pressed, double now, int frame, double window)
		{
			if (!pressed || frame == lastPressFrame)
			{
				return false;
			}
			lastPressFrame = frame;
			if (armed && now >= firstPressTime && now - firstPressTime <= window)
			{
				armed = false;
				return true;
			}
			armed = true;
			firstPressTime = now;
			return false;
		}

		internal void Reset()
		{
			armed = false;
		}
	}
	[BepInPlugin("xitrine.valheim.Double_Tap_Forsaken_Power", "Double_Tap_Forsaken_Power", "1.0.1")]
	[BepInProcess("valheim.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Player), "Update")]
		private static class PowerInputPatch
		{
			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				return PowerInputTranspiler.Rewrite(instructions, AccessTools.Method(typeof(ZInput), "GetButtonDown", new Type[1] { typeof(string) }, (Type[])null), AccessTools.Method(typeof(Plugin), "FilterPowerInput", (Type[])null, (Type[])null));
			}
		}

		[HarmonyPatch(typeof(Player), "TakeInput")]
		private static class InputContextPatch
		{
			private static void Postfix(Player __instance, bool __result)
			{
				Plugin instance = Plugin.instance;
				if (!((Object)(object)instance == (Object)null) && __instance == Player.m_localPlayer && (!__result || !Application.isFocused || Hud.InRadial() || Hud.IsPieceSelectionVisible()))
				{
					instance.gate.Reset();
				}
			}
		}

		public const string Guid = "xitrine.valheim.Double_Tap_Forsaken_Power";

		private static Plugin instance;

		private readonly DoubleTapGate gate = new DoubleTapGate();

		private ConfigEntry<bool> enabledSetting;

		private ConfigEntry<float> windowSetting;

		private Player inputPlayer;

		private Harmony harmony;

		private bool ready;

		private void Awake()
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Expected O, but got Unknown
			instance = this;
			enabledSetting = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Require two quick presses of the Forsaken Power shortcut configured in Valheim.");
			windowSetting = ((BaseUnityPlugin)this).Config.Bind<float>("General", "DoubleTapWindow", 0.4f, new ConfigDescription("Maximum interval between the two presses, in seconds.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.15f, 1f), Array.Empty<object>()));
			enabledSetting.SettingChanged += ResetSettings;
			windowSetting.SettingChanged += ResetSettings;
			harmony = new Harmony("xitrine.valheim.Double_Tap_Forsaken_Power");
			try
			{
				harmony.PatchAll(typeof(Plugin).Assembly);
				ready = true;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Double_Tap_Forsaken_Power 1.0.1 active: double tap the game's GP binding (" + windowSetting.Value + " s). Keyboard/mouse only; native activation preserved."));
			}
			catch (Exception ex)
			{
				harmony.UnpatchSelf();
				((BaseUnityPlugin)this).Logger.LogError((object)("Double_Tap_Forsaken_Power inactive: incompatible game version or mod conflict. " + ex));
			}
		}

		private void ResetSettings(object sender, EventArgs args)
		{
			gate.Reset();
		}

		private void OnApplicationFocus(bool focused)
		{
			if (!focused)
			{
				gate.Reset();
			}
		}

		private void OnApplicationPause(bool paused)
		{
			if (paused)
			{
				gate.Reset();
			}
		}

		private void OnDisable()
		{
			gate.Reset();
		}

		private void OnDestroy()
		{
			ready = false;
			if (enabledSetting != null)
			{
				enabledSetting.SettingChanged -= ResetSettings;
			}
			if (windowSetting != null)
			{
				windowSetting.SettingChanged -= ResetSettings;
			}
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			if (instance == this)
			{
				instance = null;
			}
		}

		internal static bool FilterPowerInput(bool pressed, Player player)
		{
			Plugin plugin = instance;
			if ((Object)(object)plugin == (Object)null || !plugin.ready || !((Behaviour)plugin).isActiveAndEnabled || !plugin.enabledSetting.Value)
			{
				return pressed;
			}
			if (player != Player.m_localPlayer)
			{
				return pressed;
			}
			if (!Application.isFocused)
			{
				plugin.gate.Reset();
				return false;
			}
			if (plugin.inputPlayer != player)
			{
				plugin.inputPlayer = player;
				plugin.gate.Reset();
			}
			return plugin.gate.Filter(pressed, Time.realtimeSinceStartupAsDouble, Time.frameCount, plugin.windowSetting.Value);
		}
	}
	internal static class PowerInputTranspiler
	{
		internal static IEnumerable<CodeInstruction> Rewrite(IEnumerable<CodeInstruction> instructions, MethodInfo getButtonDown, MethodInfo filter)
		{
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Expected O, but got Unknown
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			List<int> list2 = new List<int>();
			for (int i = 0; i + 1 < list.Count; i++)
			{
				if (list[i].opcode == OpCodes.Ldstr && object.Equals(list[i].operand, "GP") && CodeInstructionExtensions.Calls(list[i + 1], getButtonDown))
				{
					list2.Add(i + 1);
				}
			}
			if (list2.Count != 1)
			{
				throw new InvalidOperationException("Double_Tap_Forsaken_Power: expected exactly one GP input query in Player.Update; found " + list2.Count);
			}
			int num = list2[0];
			if (list[num].blocks.Count != 0)
			{
				throw new InvalidOperationException("Double_Tap_Forsaken_Power: unexpected exception boundary at GP query.");
			}
			list.InsertRange(num + 1, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[2]
			{
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Call, (object)filter)
			});
			return list;
		}
	}
}