Decompiled source of FocusGuard v1.0.1

FocusGuard.dll

Decompiled 4 days ago
using System;
using System.Diagnostics;
using System.Reflection;
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("FocusGuard")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("FocusGuard")]
[assembly: AssemblyTitle("FocusGuard")]
[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 FocusGuard
{
	[BepInPlugin("maddcatter.focusguard", "FocusGuard", "1.0.1")]
	public sealed class FocusGuardPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "maddcatter.focusguard";

		public const string PluginName = "FocusGuard";

		public const string PluginVersion = "1.0.1";

		private static ConfigEntry<bool> _enabled;

		private static ConfigEntry<bool> _guardLeftClick;

		private static ConfigEntry<bool> _guardRightClick;

		private static ConfigEntry<bool> _guardAlternateAttack;

		private static ConfigEntry<float> _safetyWindow;

		private static bool _leftGuarded;

		private static bool _rightGuarded;

		private static bool _alternateGuarded;

		private static int _alternateReleaseFrame = -1;

		private static float _safetyUntil;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Expected O, but got Unknown
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Expected O, but got Unknown
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enables protection against mouse clicks used to refocus Valheim.");
			_guardLeftClick = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "GuardLeftClick", true, "Prevents a refocus click from attacking or using a tool.");
			_guardRightClick = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "GuardRightClick", true, "Prevents a refocus click from blocking.");
			_guardAlternateAttack = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "GuardAlternateAttack", true, "Prevents a middle-mouse refocus click from triggering an alternate attack.");
			_safetyWindow = ((BaseUnityPlugin)this).Config.Bind<float>("General", "SafetyWindow", 0.15f, new ConfigDescription("Short fallback window, in seconds, for systems that report the activating click after focus returns.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 0.5f), Array.Empty<object>()));
			_harmony = new Harmony("maddcatter.focusguard");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"FocusGuard 1.0.1 loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void OnApplicationFocus(bool hasFocus)
		{
			if (!hasFocus)
			{
				_leftGuarded = false;
				_rightGuarded = false;
				_alternateGuarded = false;
				_safetyUntil = 0f;
			}
			else if (_enabled.Value)
			{
				_leftGuarded = _guardLeftClick.Value && Input.GetMouseButton(0);
				_rightGuarded = _guardRightClick.Value && Input.GetMouseButton(1);
				_alternateGuarded = _guardAlternateAttack.Value && Input.GetMouseButton(2);
				_safetyUntil = Time.unscaledTime + _safetyWindow.Value;
			}
		}

		private void Update()
		{
			if (!_enabled.Value)
			{
				_leftGuarded = false;
				_rightGuarded = false;
				_alternateGuarded = false;
				_safetyUntil = 0f;
				return;
			}
			bool flag = Time.unscaledTime <= _safetyUntil;
			if (_guardLeftClick.Value)
			{
				if (flag && Input.GetMouseButton(0))
				{
					_leftGuarded = true;
				}
				else if (_leftGuarded && !Input.GetMouseButton(0))
				{
					_leftGuarded = false;
				}
			}
			else
			{
				_leftGuarded = false;
			}
			if (_guardRightClick.Value)
			{
				if (flag && Input.GetMouseButton(1))
				{
					_rightGuarded = true;
				}
				else if (_rightGuarded && !Input.GetMouseButton(1))
				{
					_rightGuarded = false;
				}
			}
			else
			{
				_rightGuarded = false;
			}
			if (_guardAlternateAttack.Value)
			{
				if (flag && Input.GetMouseButton(2))
				{
					_alternateGuarded = true;
				}
				else if (_alternateGuarded && !Input.GetMouseButton(2))
				{
					_alternateReleaseFrame = Time.frameCount;
					_alternateGuarded = false;
				}
			}
			else
			{
				_alternateGuarded = false;
			}
		}

		internal static bool ShouldSuppress(string action)
		{
			if (!_enabled.Value)
			{
				return false;
			}
			bool flag = Time.unscaledTime <= _safetyUntil;
			switch (action)
			{
			case "Attack":
				if (_guardLeftClick.Value)
				{
					return _leftGuarded || flag;
				}
				return false;
			case "Block":
			case "BuildMenu":
				if (_guardRightClick.Value)
				{
					return _rightGuarded || flag;
				}
				return false;
			case "SecondaryAttack":
			case "Remove":
				if (_guardAlternateAttack.Value)
				{
					if (!(_alternateGuarded || flag))
					{
						return Time.frameCount == _alternateReleaseFrame;
					}
					return true;
				}
				return false;
			default:
				return false;
			}
		}
	}
	[HarmonyPatch(typeof(ZInput))]
	internal static class ZInputPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("GetButton")]
		private static void GetButtonPostfix(string name, ref bool __result)
		{
			if (__result && FocusGuardPlugin.ShouldSuppress(name))
			{
				__result = false;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("GetButtonDown")]
		private static void GetButtonDownPostfix(string name, ref bool __result)
		{
			if (__result && FocusGuardPlugin.ShouldSuppress(name))
			{
				__result = false;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("GetButtonUp")]
		private static void GetButtonUpPostfix(string name, ref bool __result)
		{
			if (__result && FocusGuardPlugin.ShouldSuppress(name))
			{
				__result = false;
			}
		}
	}
}