Decompiled source of AimAndCrouchToggles v1.1.2

BepInEx/plugins/AimAndCrouchToggles.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Pigeon.Movement;
using UnityEngine;
using UnityEngine.InputSystem;

[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("Sparroh")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1")]
[assembly: AssemblyProduct("AimAndCrouchToggles")]
[assembly: AssemblyTitle("AimAndCrouchToggles")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.1.0")]
[module: UnverifiableCode]
[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;
		}
	}
}
public static class ConfigManager
{
	private static ConfigFile config;

	private static ManualLogSource logger;

	private static FileSystemWatcher configWatcher;

	private static volatile bool reloadPending;

	private static float lastReloadTime;

	private const float DebounceSeconds = 0.25f;

	public static ConfigEntry<bool> ToggleAim { get; private set; }

	public static ConfigEntry<bool> ToggleCrouch { get; private set; }

	public static bool ToggleAimEnabled => ToggleAim.Value;

	public static bool ToggleCrouchEnabled => ToggleCrouch.Value;

	public static void Initialize(ConfigFile configFile, ManualLogSource log)
	{
		config = configFile;
		logger = log;
		ToggleAim = config.Bind<bool>("General", "Toggle Aim", true, "If true, aim becomes a toggle (press to enter/exit) instead of hold.");
		ToggleCrouch = config.Bind<bool>("General", "Toggle Crouch", true, "If true, enables toggle crouch functionality (hold crouch by pressing slide button).");
		ToggleAim.SettingChanged += OnToggleAimChanged;
		ToggleCrouch.SettingChanged += OnToggleCrouchChanged;
		try
		{
			SetupFileWatcher();
		}
		catch (Exception ex)
		{
			logger.LogError((object)("Error setting up config file watcher: " + ex.Message));
		}
	}

	public static void Tick()
	{
		if (!reloadPending || Time.unscaledTime - lastReloadTime < 0.25f)
		{
			return;
		}
		reloadPending = false;
		lastReloadTime = Time.unscaledTime;
		try
		{
			config.Reload();
			logger.LogInfo((object)"Config reloaded from disk.");
		}
		catch (Exception ex)
		{
			logger.LogError((object)("Error reloading config: " + ex.Message));
		}
	}

	public static void Dispose()
	{
		if (ToggleAim != null)
		{
			ToggleAim.SettingChanged -= OnToggleAimChanged;
		}
		if (ToggleCrouch != null)
		{
			ToggleCrouch.SettingChanged -= OnToggleCrouchChanged;
		}
		if (configWatcher != null)
		{
			configWatcher.EnableRaisingEvents = false;
			configWatcher.Changed -= OnConfigFileChanged;
			configWatcher.Created -= OnConfigFileChanged;
			configWatcher.Renamed -= OnConfigFileChanged;
			configWatcher.Dispose();
			configWatcher = null;
		}
	}

	private static void SetupFileWatcher()
	{
		configWatcher = new FileSystemWatcher(Paths.ConfigPath, "sparroh.aimandcrouchtoggles.cfg");
		configWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
		configWatcher.Changed += OnConfigFileChanged;
		configWatcher.Created += OnConfigFileChanged;
		configWatcher.Renamed += OnConfigFileChanged;
		configWatcher.EnableRaisingEvents = true;
	}

	private static void OnConfigFileChanged(object sender, FileSystemEventArgs e)
	{
		reloadPending = true;
	}

	private static void OnToggleAimChanged(object sender, EventArgs e)
	{
		ToggleAimPatches.ConfigureSubscription();
	}

	private static void OnToggleCrouchChanged(object sender, EventArgs e)
	{
		if (!ToggleCrouchEnabled)
		{
			ToggleCrouchPatches.ResetToggle();
		}
	}
}
[HarmonyPatch]
public static class EndCrouchPatches
{
	[HarmonyPatch(typeof(Player), "EndCrouch")]
	[HarmonyPrefix]
	private static bool Prefix()
	{
		if (ConfigManager.ToggleCrouchEnabled)
		{
			return !ToggleCrouchPatches.isToggleOn;
		}
		return true;
	}
}
[HarmonyPatch]
public static class EndSlidePatches
{
	[HarmonyPatch(typeof(Player), "EndSlide")]
	[HarmonyPrefix]
	private static bool Prefix()
	{
		if (ConfigManager.ToggleCrouchEnabled)
		{
			return !ToggleCrouchPatches.isToggleOn;
		}
		return true;
	}
}
[BepInPlugin("sparroh.aimandcrouchtoggles", "AimAndCrouchToggles", "1.1.1")]
[MycoMod(/*Could not decode attribute arguments.*/)]
public class AimAndCrouchTogglesPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "sparroh.aimandcrouchtoggles";

	public const string PluginName = "AimAndCrouchToggles";

	public const string PluginVersion = "1.1.1";

	internal static ManualLogSource Logger;

	private Harmony harmony;

	private void Awake()
	{
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Expected O, but got Unknown
		Logger = ((BaseUnityPlugin)this).Logger;
		ConfigManager.Initialize(((BaseUnityPlugin)this).Config, Logger);
		try
		{
			ToggleAimPatches.Setup();
			ToggleCrouchPatches.Setup();
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error setting up access tools: " + ex.Message));
		}
		try
		{
			harmony = new Harmony("sparroh.aimandcrouchtoggles");
			harmony.PatchAll(typeof(ToggleAimPatches));
			harmony.PatchAll(typeof(ToggleCrouchPatches));
			harmony.PatchAll(typeof(EndCrouchPatches));
			harmony.PatchAll(typeof(EndSlidePatches));
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Error applying patches: " + ex2.Message));
		}
		Logger.LogInfo((object)"AimAndCrouchToggles v1.1.1 loaded successfully.");
	}

	private void Update()
	{
		ConfigManager.Tick();
	}

	private void OnDestroy()
	{
		ConfigManager.Dispose();
		ToggleAimPatches.Cleanup();
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}
}
[HarmonyPatch]
public static class ToggleAimPatches
{
	internal static bool isAimToggled;

	internal static InputAction aimAction;

	private static FieldInfo isAimInputHeldField;

	private static FieldInfo lastPressedAimTimeField;

	private static FieldInfo lastPressedFireTimeField;

	private static FieldInfo playerField;

	private static MethodInfo isAimingGetter;

	private static MethodInfo wantsToFireGetter;

	private static MethodInfo lastFireTimeGetter;

	internal static void Setup()
	{
		isAimInputHeldField = AccessTools.Field(typeof(Gun), "isAimInputHeld");
		lastPressedAimTimeField = AccessTools.Field(typeof(Gun), "lastPressedAimTime");
		lastPressedFireTimeField = AccessTools.Field(typeof(Gun), "lastPressedFireTime");
		playerField = AccessTools.Field(typeof(Gun), "player");
		isAimingGetter = AccessTools.PropertyGetter(typeof(Gun), "IsAiming");
		wantsToFireGetter = AccessTools.PropertyGetter(typeof(Gun), "WantsToFire");
		lastFireTimeGetter = AccessTools.PropertyGetter(typeof(Gun), "LastFireTime");
	}

	internal static void ConfigureSubscription()
	{
		if (aimAction != null)
		{
			aimAction.started -= OnAimStarted;
			if (ConfigManager.ToggleAimEnabled)
			{
				aimAction.started += OnAimStarted;
			}
			else
			{
				isAimToggled = false;
			}
		}
	}

	internal static void Cleanup()
	{
		if (aimAction != null)
		{
			aimAction.started -= OnAimStarted;
		}
	}

	internal static void ResetToggle()
	{
		isAimToggled = false;
	}

	private static void OnAimStarted(CallbackContext context)
	{
		if (ConfigManager.ToggleAimEnabled)
		{
			isAimToggled = !isAimToggled;
		}
	}

	[HarmonyPatch(typeof(PlayerInput), "Initialize")]
	[HarmonyPostfix]
	public static void PlayerInputInitializePostfix()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		PlayerControls controls = PlayerInput.Controls;
		object obj;
		if (controls == null)
		{
			obj = null;
		}
		else
		{
			PlayerActions player = controls.Player;
			obj = ((PlayerActions)(ref player)).Aim;
		}
		aimAction = (InputAction)obj;
		ConfigureSubscription();
	}

	[HarmonyPatch(typeof(Gun), "OnAimInputPerformed")]
	[HarmonyPrefix]
	public static bool SkipPrefix()
	{
		return !ConfigManager.ToggleAimEnabled;
	}

	[HarmonyPatch(typeof(Gun), "OnAimInputCancelled")]
	[HarmonyPrefix]
	public static bool SkipPrefixCancelled()
	{
		return !ConfigManager.ToggleAimEnabled;
	}

	[HarmonyPatch(typeof(Gun), "HandleAim")]
	[HarmonyPrefix]
	public static void HandleAimPrefix(Gun __instance)
	{
		if (ConfigManager.ToggleAimEnabled && __instance.IsAimEnabled)
		{
			isAimInputHeldField.SetValue(__instance, isAimToggled);
			if (isAimToggled)
			{
				lastPressedAimTimeField.SetValue(__instance, Time.time);
			}
		}
	}

	[HarmonyPatch(typeof(Gun), "Update")]
	[HarmonyPostfix]
	public static void UpdatePostfix(Gun __instance)
	{
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Expected O, but got Unknown
		if (ConfigManager.ToggleAimEnabled && __instance.IsAimEnabled)
		{
			bool flag = (bool)isAimingGetter.Invoke(__instance, null);
			bool flag2 = (bool)wantsToFireGetter.Invoke(__instance, null);
			float num = (float)lastFireTimeGetter.Invoke(__instance, null);
			float num2 = (float)lastPressedFireTimeField.GetValue(__instance);
			Player val = (Player)playerField.GetValue(__instance);
			if ((Object)(object)val != (Object)null && !flag && !flag2 && Time.time - Mathf.Max(num, num2) > 0.5f)
			{
				val.ResumeSprint();
			}
		}
	}

	[HarmonyPatch(typeof(Player), "Resurrect_ClientRpc")]
	[HarmonyPostfix]
	public static void ResetTogglePostfix()
	{
		ResetToggle();
	}
}
[HarmonyPatch]
public static class ToggleCrouchPatches
{
	public static bool isToggleOn;

	private static PropertyInfo crouchingProp;

	private static PropertyInfo slidingProp;

	internal static void Setup()
	{
		crouchingProp = typeof(Player).GetProperty("Crouching");
		slidingProp = typeof(Player).GetProperty("Sliding");
	}

	internal static void ResetToggle()
	{
		isToggleOn = false;
	}

	[HarmonyPatch(typeof(Player), "Update")]
	[HarmonyPrefix]
	public static void Prefix(Player __instance)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		if (!ConfigManager.ToggleCrouchEnabled)
		{
			return;
		}
		PlayerActions player = PlayerInput.Controls.Player;
		if (((PlayerActions)(ref player)).Slide.WasPressedThisFrame())
		{
			bool num = (bool)crouchingProp.GetValue(__instance);
			bool flag = (bool)slidingProp.GetValue(__instance);
			if (num || flag || isToggleOn)
			{
				isToggleOn = false;
			}
			else
			{
				isToggleOn = true;
			}
		}
	}
}
namespace AimAndCrouchToggles
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "AimAndCrouchToggles";

		public const string PLUGIN_NAME = "AimAndCrouchToggles";

		public const string PLUGIN_VERSION = "1.1.1";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}