Decompiled source of ReloadUnlocked v4.3.0

plugins/ReloadUnlocked/ReloadUnlocked.dll

Decompiled 20 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using Agents;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Gear;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Player;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace ReloadControl;

internal enum KeepAimStrategy
{
	Trigger,
	ChangeState,
	Both
}
internal enum AnimResetMode
{
	Off,
	RebindAll
}
[BepInPlugin("ReloadUnlocked", "ReloadUnlocked", "4.3.0")]
public class Plugin : BasePlugin
{
	public const string GUID = "ReloadUnlocked";

	public const string NAME = "ReloadUnlocked";

	public const string VERSION = "4.3.0";

	internal static ConfigEntry<bool> FireCancels;

	internal static ConfigEntry<bool> AimCancels;

	internal static ConfigEntry<bool> KeepAiming;

	internal static ConfigEntry<KeepAimStrategy> KeepAimingStrategy;

	internal static ConfigEntry<float> AimWeightNudge;

	internal static ConfigEntry<float> SettleSeconds;

	internal static ConfigEntry<bool> OnlyBulletWeapons;

	internal static ConfigEntry<AnimResetMode> AnimReset;

	internal static ConfigEntry<bool> LogEnabled;

	internal static ConfigEntry<bool> Verbose;

	internal static ConfigEntry<bool> MeasurePerformance;

	internal static ConfigEntry<float> PerfReportSeconds;

	internal static ManualLogSource L;

	private Harmony _harmony;

	public override void Load()
	{
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Expected O, but got Unknown
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Expected O, but got Unknown
		//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bd: Expected O, but got Unknown
		//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f2: Expected O, but got Unknown
		//IL_0209: Unknown result type (might be due to invalid IL or missing references)
		//IL_020f: Expected O, but got Unknown
		L = ((BasePlugin)this).Log;
		FireCancels = ((BasePlugin)this).Config.Bind<bool>("1. Interrupt", "FireCancelsReload", true, "Pressing fire while reloading aborts the reload. The ammo is NOT granted - that is the point: you trade the reload for being able to shoot right away.");
		AimCancels = ((BasePlugin)this).Config.Bind<bool>("1. Interrupt", "AimCancelsReload", true, "Pressing aim (ADS) while reloading aborts the reload.\nSet this to false if you want to press R in hip fire and then aim while the reload keeps going: with KeepAimingWhileReloading the camera then stays zoomed until the reload ends.");
		KeepAiming = ((BasePlugin)this).Config.Bind<bool>("2. Reload while aiming", "KeepAimingWhileReloading", true, "Stay in ADS while reloading: the camera zoom keeps working.\nKnown limitation: the weapon MODEL still plays the hip reload animation, because GTFO ships no aim-pose reload animation to blend to.\nNOTE: BepInEx reads configs only at plugin load. Edit, then RESTART the game.");
		KeepAimingStrategy = ((BasePlugin)this).Config.Bind<KeepAimStrategy>("2. Reload while aiming", "KeepAimingStrategy", KeepAimStrategy.Both, "How to fight the game. Trigger alone leaves the state machine in Hip and the camera unzoomed (measured: fov 82 instead of 41), so change-state forcing is required. Keep this at Both.");
		AimWeightNudge = ((BasePlugin)this).Config.Bind<float>("2. Reload while aiming", "AimWeightNudge", 0.9f, new ConfigDescription("While reloading with aim held, push the aim blend up to at least this value every frame. NOTE: the camera zoom follows the FP item STATE, not this blend (a full ADS and a reloading Hip both read 1.00 while the FOV differs 41 vs 82); the value is kept at the 1.0.0 default. Set 0 to disable.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		SettleSeconds = ((BasePlugin)this).Config.Bind<float>("2. Reload while aiming", "SettleSeconds", 0.25f, new ConfigDescription("Hand-over grace time: after a NORMAL reload end with aim still held, keep forcing Aim for this long so the game's own aim logic takes over without a Hip flicker. 0 = off.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		OnlyBulletWeapons = ((BasePlugin)this).Config.Bind<bool>("3. Compatibility", "OnlyBulletWeapons", true, "Only act on Gear.BulletWeapon (rifles / shotguns). Leaves tools, glue gun and marker gun alone.");
		AnimReset = ((BasePlugin)this).Config.Bind<AnimResetMode>("3. Compatibility", "AnimResetMode", AnimResetMode.RebindAll, "How an aborted reload is cleaned up:\n  Off       = vanilla, the reload animation plays out on its own\n  RebindAll = Rebind() every rig still on a reload clip, all layers (default)\nMeasured: the FP weapon is several animators (main rig, arms rig, per-weapon parts rig) on several layers, and stopping the sequence coroutine only skips the ammo grant - the clips keep playing.");
		LogEnabled = ((BasePlugin)this).Config.Bind<bool>("4. Diagnostics", "Log", true, "Log aborts (throttled to one line per second).");
		MeasurePerformance = ((BasePlugin)this).Config.Bind<bool>("4. Diagnostics", "MeasurePerformance", false, "Log this mod's own per-frame cost (avg/max microseconds + managed allocations). One run is enough.");
		PerfReportSeconds = ((BasePlugin)this).Config.Bind<float>("4. Diagnostics", "PerfReportSeconds", 10f, new ConfigDescription("Seconds between [perf] report lines when MeasurePerformance is on.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 120f), Array.Empty<object>()));
		Verbose = ((BasePlugin)this).Config.Bind<bool>("4. Diagnostics", "Verbose", false, "Log per-abort details (which animators were rebound).");
		_harmony = new Harmony("ReloadUnlocked");
		bool flag = ReloadHook.Apply(_harmony);
		ManualLogSource l = L;
		bool flag2 = default(bool);
		BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(73, 9, ref flag2);
		if (flag2)
		{
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("ReloadUnlocked");
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" ");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("4.3.0");
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" loaded: hook=");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(flag ? "ok" : "FAILED");
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", ");
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral("fire=");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(FireCancels.Value);
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", aim=");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(AimCancels.Value);
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", keepAiming=");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(KeepAiming.Value);
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", ");
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral("strategy=");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<KeepAimStrategy>(KeepAimingStrategy.Value);
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", bulletOnly=");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(OnlyBulletWeapons.Value);
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", ");
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral("reset=");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<AnimResetMode>(AnimReset.Value);
		}
		l.LogInfo(val);
	}

	public override bool Unload()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
		ReloadDriver.Shutdown();
		return true;
	}
}
internal static class ReloadAnims
{
	internal static List<Animator> Collect(FirstPersonItemHolder holder)
	{
		List<Animator> list = new List<Animator>();
		try
		{
			Il2CppArrayBase<Animator> componentsInChildren = ((Component)holder).GetComponentsInChildren<Animator>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (!Contains(list, componentsInChildren[i]))
				{
					list.Add(componentsInChildren[i]);
				}
			}
		}
		catch
		{
		}
		try
		{
			PlayerFPSBody fPSArms = holder.FPSArms;
			if (!IsNull((Il2CppObjectBase)(object)fPSArms))
			{
				Il2CppArrayBase<Animator> componentsInChildren2 = ((Component)fPSArms).GetComponentsInChildren<Animator>(true);
				for (int j = 0; j < componentsInChildren2.Length; j++)
				{
					if (!Contains(list, componentsInChildren2[j]))
					{
						list.Add(componentsInChildren2[j]);
					}
				}
			}
		}
		catch
		{
		}
		if (list.Count == 0)
		{
			try
			{
				Animator fpsItemAnim = holder.m_fpsItemAnim;
				if (!IsNull((Il2CppObjectBase)(object)fpsItemAnim))
				{
					list.Add(fpsItemAnim);
				}
			}
			catch
			{
			}
		}
		return list;
	}

	internal static bool IsPlayingReload(Animator a)
	{
		int num = LayerCount(a);
		for (int i = 0; i < num; i++)
		{
			string text = CurrentClipName(a, i);
			if (text != null && text.IndexOf("reload", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return true;
			}
		}
		return false;
	}

	internal static void RebindReloadRigs(FirstPersonItemHolder holder, bool log)
	{
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Expected O, but got Unknown
		List<Animator> list = Collect(holder);
		int num = 0;
		StringBuilder stringBuilder = (log ? new StringBuilder() : null);
		for (int i = 0; i < list.Count; i++)
		{
			try
			{
				Animator val = list[i];
				if (!IsNull((Il2CppObjectBase)(object)val) && IsPlayingReload(val))
				{
					val.Rebind();
					val.Update(0f);
					num++;
					stringBuilder?.Append(((Object)((Component)val).gameObject).name).Append(' ');
				}
			}
			catch
			{
			}
		}
		if (stringBuilder != null)
		{
			ManualLogSource l = Plugin.L;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(29, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("reload animators rebound: ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(num);
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" (");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<StringBuilder>(stringBuilder);
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(")");
			}
			l.LogInfo(val2);
		}
	}

	internal static string Describe(FirstPersonItemHolder holder)
	{
		StringBuilder stringBuilder = new StringBuilder();
		try
		{
			List<Animator> list = Collect(holder);
			for (int i = 0; i < list.Count; i++)
			{
				Animator val = list[i];
				try
				{
					if (IsNull((Il2CppObjectBase)(object)val))
					{
						continue;
					}
					int num = LayerCount(val);
					stringBuilder.Append(((Object)((Component)val).gameObject).name).Append("(L").Append(num)
						.Append(')');
					for (int j = 0; j < num; j++)
					{
						string text = CurrentClipName(val, j);
						if (text != null)
						{
							stringBuilder.Append('[').Append(text).Append('@')
								.Append(j)
								.Append(']');
						}
					}
					stringBuilder.Append(' ');
				}
				catch
				{
				}
			}
		}
		catch
		{
		}
		return stringBuilder.ToString();
	}

	internal static int LayerCount(Animator a)
	{
		int num;
		try
		{
			num = a.layerCount;
		}
		catch
		{
			num = 1;
		}
		if (num > 0)
		{
			return num;
		}
		return 1;
	}

	internal static string CurrentClipName(Animator a, int layer)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Il2CppStructArray<AnimatorClipInfo> currentAnimatorClipInfo = a.GetCurrentAnimatorClipInfo(layer);
			if (currentAnimatorClipInfo == null || ((Il2CppArrayBase<AnimatorClipInfo>)(object)currentAnimatorClipInfo).Length == 0)
			{
				return null;
			}
			AnimatorClipInfo val = ((Il2CppArrayBase<AnimatorClipInfo>)(object)currentAnimatorClipInfo)[0];
			AnimationClip clip = ((AnimatorClipInfo)(ref val)).clip;
			if (IsNull((Il2CppObjectBase)(object)clip))
			{
				return null;
			}
			return ((Object)clip).name;
		}
		catch
		{
			return null;
		}
	}

	private static bool Contains(List<Animator> list, Animator a)
	{
		for (int i = 0; i < list.Count; i++)
		{
			try
			{
				if (!IsNull((Il2CppObjectBase)(object)list[i]) && !IsNull((Il2CppObjectBase)(object)a) && ((Il2CppObjectBase)list[i]).Pointer == ((Il2CppObjectBase)a).Pointer)
				{
					return true;
				}
			}
			catch
			{
			}
		}
		return false;
	}

	internal static bool IsNull(Il2CppObjectBase o)
	{
		if (o == null || o.Pointer == IntPtr.Zero)
		{
			return true;
		}
		try
		{
			return o.WasCollected;
		}
		catch
		{
			return false;
		}
	}
}
public class ReloadDriver : MonoBehaviour
{
	private static ReloadDriver _instance;

	private static bool _createFailed;

	private static int _createAttempts;

	private static bool _instanceAlive;

	private static int _aliveCheckCountdown;

	private ItemEquippable _item;

	private bool _itemIsBulletWeapon;

	private int _failures;

	private FirstPersonItemHolder _holder;

	private bool _keepAimingActive;

	private FirstPersonItemHolder _settleHolder;

	private float _settleUntil;

	private float _nextAbortLog;

	private int _frame;

	private bool _wieldedKnown = true;

	private int _nextWieldCheck;

	private static long _updSum;

	private static long _updMax;

	private static long _lateSum;

	private static long _lateMax;

	private static int _perfFrames;

	private static float _nextPerf;

	private static long _allocSum;

	public ReloadDriver(IntPtr ptr)
		: base(ptr)
	{
	}

	internal static void EnsureCreated()
	{
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Expected O, but got Unknown
		if (_createFailed)
		{
			return;
		}
		if (_instance != null)
		{
			if (_instanceAlive && --_aliveCheckCountdown > 0)
			{
				return;
			}
			_aliveCheckCountdown = 30;
			_instanceAlive = !IsNull((Il2CppObjectBase)(object)_instance);
			if (_instanceAlive)
			{
				return;
			}
		}
		_instance = null;
		_instanceAlive = false;
		try
		{
			Create();
		}
		catch (Exception ex)
		{
			_createFailed = true;
			ManualLogSource l = Plugin.L;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(32, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("early driver creation failed: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.GetType().Name);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
			}
			l.LogError(val);
		}
	}

	internal static void Shutdown()
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Expected O, but got Unknown
		ReloadDriver instance = _instance;
		_instance = null;
		_instanceAlive = false;
		if (instance == null || IsNull((Il2CppObjectBase)(object)instance))
		{
			return;
		}
		try
		{
			Object.Destroy((Object)(object)((Component)instance).gameObject);
		}
		catch (Exception ex)
		{
			try
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(19, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("driver shutdown: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.GetType().Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				l.LogWarning(val);
			}
			catch
			{
			}
		}
	}

	internal static void NotifyReloadStarted(ItemEquippable item)
	{
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Expected O, but got Unknown
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Expected O, but got Unknown
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Expected O, but got Unknown
		if (_createFailed || IsNull((Il2CppObjectBase)(object)item))
		{
			return;
		}
		bool flag = default(bool);
		try
		{
			PlayerAgent owner = ((Item)item).Owner;
			if (IsNull((Il2CppObjectBase)(object)owner) || !((Agent)owner).IsLocallyOwned)
			{
				return;
			}
			if (_instance == null)
			{
				Create();
				if (_instance == null)
				{
					return;
				}
			}
			try
			{
				_instance._item = item;
				BulletWeapon o = ((Il2CppObjectBase)item).TryCast<BulletWeapon>();
				_instance._itemIsBulletWeapon = !IsNull((Il2CppObjectBase)(object)o);
			}
			catch (Exception ex)
			{
				_instance = null;
				_instanceAlive = false;
				if (_createAttempts >= 3)
				{
					_createFailed = true;
					ManualLogSource l = Plugin.L;
					BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(44, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("driver dead after ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(_createAttempts);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" creations, mod inactive: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.GetType().Name);
					}
					l.LogError(val);
				}
				else
				{
					ManualLogSource l2 = Plugin.L;
					BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(43, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("driver reference was stale, will recreate: ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.GetType().Name);
					}
					l2.LogWarning(val2);
				}
			}
		}
		catch (Exception ex2)
		{
			_createFailed = true;
			ManualLogSource l3 = Plugin.L;
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(38, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("driver notify failed, mod inactive: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex2.GetType().Name);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex2.Message);
			}
			l3.LogError(val);
		}
	}

	private static void Create()
	{
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Expected O, but got Unknown
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Expected O, but got Unknown
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Expected O, but got Unknown
		bool flag = default(bool);
		try
		{
			ClassInjector.RegisterTypeInIl2Cpp<ReloadDriver>();
		}
		catch (Exception ex)
		{
			if (ex.Message != null && ex.Message.IndexOf("already", StringComparison.OrdinalIgnoreCase) < 0)
			{
				ManualLogSource l = Plugin.L;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(28, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("driver type registration: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.GetType().Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				l.LogWarning(val);
			}
		}
		_createAttempts++;
		try
		{
			GameObject val2 = new GameObject("ReloadUnlocked");
			Object.DontDestroyOnLoad((Object)val2);
			_instance = val2.AddComponent<ReloadDriver>();
			_instanceAlive = true;
			_aliveCheckCountdown = 30;
			Plugin.L.LogInfo((object)"reload driver created");
		}
		catch (Exception ex2)
		{
			_createFailed = true;
			ManualLogSource l2 = Plugin.L;
			BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(40, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("driver creation failed, mod inactive: ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex2.GetType().Name);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex2.Message);
			}
			l2.LogError(val3);
		}
	}

	private void Update()
	{
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Expected O, but got Unknown
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		if (_failures >= 5)
		{
			return;
		}
		bool value = Plugin.MeasurePerformance.Value;
		long num = (value ? Stopwatch.GetTimestamp() : 0);
		long num2 = (value ? GC.GetAllocatedBytesForCurrentThread() : 0);
		try
		{
			Tick();
		}
		catch (Exception ex)
		{
			_failures++;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val;
			if (_failures >= 5)
			{
				ManualLogSource l = Plugin.L;
				val = new BepInExErrorLogInterpolatedStringHandler(42, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("driver disabled after 5 failures; last: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.GetType().Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				l.LogError(val);
				return;
			}
			ManualLogSource l2 = Plugin.L;
			val = new BepInExErrorLogInterpolatedStringHandler(27, 3, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("driver tick failed (");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(_failures);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("/5): ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.GetType().Name);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
			}
			l2.LogError(val);
		}
		finally
		{
			if (value)
			{
				RecordPerf(ref _updSum, ref _updMax, Stopwatch.GetTimestamp() - num);
				_allocSum += GC.GetAllocatedBytesForCurrentThread() - num2;
				MaybeReportPerf();
			}
		}
	}

	private void Tick()
	{
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Expected O, but got Unknown
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_015a: Expected O, but got Unknown
		//IL_0197: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01af: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
		_frame++;
		ItemEquippable item = _item;
		if (IsNull((Il2CppObjectBase)(object)item))
		{
			Clear();
			return;
		}
		if (!item.IsReloading)
		{
			BeginSettle(item);
			Clear();
			return;
		}
		if (Plugin.OnlyBulletWeapons.Value && !_itemIsBulletWeapon)
		{
			Clear();
			return;
		}
		PlayerAgent owner = ((Item)item).Owner;
		if (IsNull((Il2CppObjectBase)(object)owner) || !((Agent)owner).IsLocallyOwned)
		{
			Clear();
			return;
		}
		FirstPersonItemHolder fPItemHolder;
		try
		{
			fPItemHolder = owner.FPItemHolder;
		}
		catch
		{
			return;
		}
		if (IsNull((Il2CppObjectBase)(object)fPItemHolder))
		{
			return;
		}
		if (_frame >= _nextWieldCheck)
		{
			_nextWieldCheck = _frame + 3;
			_wieldedKnown = IsWielded(fPItemHolder, item);
		}
		if (!_wieldedKnown)
		{
			if (Plugin.Verbose.Value)
			{
				string text = "?";
				try
				{
					PlayerInventoryBase inventoryLocal = (PlayerInventoryBase)(object)fPItemHolder.m_inventoryLocal;
					if (!IsNull((Il2CppObjectBase)(object)inventoryLocal))
					{
						ItemEquippable wieldedItem = inventoryLocal.WieldedItem;
						if (!IsNull((Il2CppObjectBase)(object)wieldedItem))
						{
							text = ((Object)((Component)wieldedItem).gameObject).name;
						}
					}
				}
				catch
				{
				}
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(74, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[swap] reload interrupted by a weapon swap -> '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("', resetting lingering rigs");
				}
				l.LogInfo(val);
				ManualLogSource l2 = Plugin.L;
				val = new BepInExInfoLogInterpolatedStringHandler(13, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[swap] rigs: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ReloadAnims.Describe(fPItemHolder));
				}
				l2.LogInfo(val);
			}
			ReloadAnims.RebindReloadRigs(fPItemHolder, Plugin.Verbose.Value);
			Clear();
		}
		else
		{
			eFocusState inputFilter;
			try
			{
				inputFilter = owner.InputFilter;
			}
			catch
			{
				return;
			}
			if (Plugin.FireCancels.Value && ButtonDown((InputAction)8, inputFilter))
			{
				Cancel(item, "fire");
			}
			else if (Plugin.AimCancels.Value && ButtonDown((InputAction)7, inputFilter))
			{
				Cancel(item, "aim");
			}
			else
			{
				KeepAiming(owner, inputFilter);
			}
		}
	}

	private void KeepAiming(PlayerAgent owner, eFocusState filter)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		if (!Plugin.KeepAiming.Value || !ButtonHeld((InputAction)7, filter))
		{
			_keepAimingActive = false;
			return;
		}
		FirstPersonItemHolder fPItemHolder = owner.FPItemHolder;
		if (!IsNull((Il2CppObjectBase)(object)fPItemHolder))
		{
			_holder = fPItemHolder;
			_keepAimingActive = true;
			KeepAimStrategy keepAimStrategy;
			try
			{
				keepAimStrategy = Plugin.KeepAimingStrategy.Value;
			}
			catch
			{
				keepAimStrategy = KeepAimStrategy.Trigger;
			}
			if (keepAimStrategy != KeepAimStrategy.ChangeState)
			{
				fPItemHolder.m_itemAimTrigger = true;
			}
			if (keepAimStrategy != KeepAimStrategy.Trigger)
			{
				ForceAimState(fPItemHolder);
			}
			ApplyNudge(fPItemHolder);
		}
	}

	private void ForceAimState(FirstPersonItemHolder holder)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Invalid comparison between Unknown and I4
		try
		{
			if ((int)holder.m_currentState != 3)
			{
				holder.ChangeState((FPItemStateName)3);
			}
		}
		catch
		{
		}
	}

	private void LateUpdate()
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Invalid comparison between Unknown and I4
		if (_failures >= 5)
		{
			return;
		}
		if (_settleUntil > 0f)
		{
			if (Time.time <= _settleUntil)
			{
				try
				{
					FirstPersonItemHolder settleHolder = _settleHolder;
					if (!IsNull((Il2CppObjectBase)(object)settleHolder) && (int)settleHolder.m_currentState != 3)
					{
						settleHolder.ChangeState((FPItemStateName)3);
					}
				}
				catch
				{
				}
			}
			else
			{
				_settleUntil = 0f;
				_settleHolder = null;
			}
		}
		if (!_keepAimingActive || !Plugin.KeepAiming.Value)
		{
			return;
		}
		bool value = Plugin.MeasurePerformance.Value;
		long num = (value ? Stopwatch.GetTimestamp() : 0);
		try
		{
			FirstPersonItemHolder holder = _holder;
			if (IsNull((Il2CppObjectBase)(object)holder))
			{
				_keepAimingActive = false;
				_holder = null;
				return;
			}
			ApplyNudge(holder);
			if (Plugin.KeepAimingStrategy.Value != KeepAimStrategy.Trigger)
			{
				ForceAimState(holder);
			}
		}
		catch
		{
			_keepAimingActive = false;
		}
		finally
		{
			if (value)
			{
				RecordPerf(ref _lateSum, ref _lateMax, Stopwatch.GetTimestamp() - num);
			}
		}
	}

	private static void ApplyNudge(FirstPersonItemHolder holder)
	{
		float value = Plugin.AimWeightNudge.Value;
		if (value <= 0f)
		{
			return;
		}
		try
		{
			if (holder.m_aimWeight < value)
			{
				holder.m_aimWeight = value;
			}
		}
		catch
		{
		}
	}

	private static bool IsWielded(FirstPersonItemHolder holder, ItemEquippable item)
	{
		try
		{
			PlayerInventoryBase inventoryLocal = (PlayerInventoryBase)(object)holder.m_inventoryLocal;
			if (IsNull((Il2CppObjectBase)(object)inventoryLocal))
			{
				return true;
			}
			ItemEquippable wieldedItem = inventoryLocal.WieldedItem;
			if (IsNull((Il2CppObjectBase)(object)wieldedItem))
			{
				return true;
			}
			return ((Il2CppObjectBase)wieldedItem).Pointer == ((Il2CppObjectBase)item).Pointer;
		}
		catch
		{
			return true;
		}
	}

	private void BeginSettle(ItemEquippable item)
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		if (!_keepAimingActive)
		{
			return;
		}
		float value = Plugin.SettleSeconds.Value;
		if (value <= 0f)
		{
			return;
		}
		try
		{
			PlayerAgent owner = ((Item)item).Owner;
			if (IsNull((Il2CppObjectBase)(object)owner))
			{
				return;
			}
			eFocusState inputFilter = owner.InputFilter;
			if (ButtonHeld((InputAction)7, inputFilter))
			{
				FirstPersonItemHolder fPItemHolder = owner.FPItemHolder;
				if (!IsNull((Il2CppObjectBase)(object)fPItemHolder) && IsWielded(fPItemHolder, item))
				{
					_settleHolder = fPItemHolder;
					_settleUntil = Time.time + value;
				}
			}
		}
		catch
		{
		}
	}

	private void Clear()
	{
		_item = null;
		_itemIsBulletWeapon = false;
		_holder = null;
		_keepAimingActive = false;
		_wieldedKnown = true;
		_nextWieldCheck = 0;
	}

	private void Cancel(ItemEquippable item, string reason)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Expected O, but got Unknown
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Expected O, but got Unknown
		bool flag = default(bool);
		try
		{
			item.StopAnimationSequenceCoroutine();
			item.IsReloading = false;
		}
		catch (Exception ex)
		{
			ManualLogSource l = Plugin.L;
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(22, 3, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("cancel via ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(reason);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" failed: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.GetType().Name);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
			}
			l.LogError(val);
			return;
		}
		FirstPersonItemHolder val2 = null;
		try
		{
			PlayerAgent owner = ((Item)item).Owner;
			if (!IsNull((Il2CppObjectBase)(object)owner))
			{
				val2 = owner.FPItemHolder;
			}
		}
		catch
		{
		}
		if (!IsNull((Il2CppObjectBase)(object)val2))
		{
			ReloadAnims.RebindReloadRigs(val2, Plugin.Verbose.Value);
		}
		Clear();
		if (Plugin.LogEnabled.Value && Time.time >= _nextAbortLog)
		{
			_nextAbortLog = Time.time + 1f;
			ManualLogSource l2 = Plugin.L;
			BepInExInfoLogInterpolatedStringHandler val3 = new BepInExInfoLogInterpolatedStringHandler(18, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("reload aborted by ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(reason);
			}
			l2.LogInfo(val3);
		}
	}

	private static void RecordPerf(ref long sum, ref long max, long delta)
	{
		sum += delta;
		if (delta > max)
		{
			max = delta;
		}
	}

	private static void MaybeReportPerf()
	{
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Expected O, but got Unknown
		_perfFrames++;
		float value = Plugin.PerfReportSeconds.Value;
		float time = Time.time;
		if (_nextPerf <= 0f)
		{
			_nextPerf = time + value;
		}
		else if (!(time < _nextPerf))
		{
			double num = 1000000.0 / (double)Stopwatch.Frequency;
			long allocSum = _allocSum;
			int perfFrames = _perfFrames;
			ManualLogSource l = Plugin.L;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(65, 6, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[perf] frames=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(perfFrames);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" ");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("update avg=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<double>((double)_updSum * num / (double)perfFrames, "0.000");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("us max=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<double>((double)_updMax * num, "0.00");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("us ");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("late avg=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<double>((double)_lateSum * num / (double)perfFrames, "0.000");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("us max=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<double>((double)_lateMax * num, "0.00");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("us ");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("ownAlloc=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<long>(allocSum);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("B");
			}
			l.LogInfo(val);
			_updSum = 0L;
			_updMax = 0L;
			_lateSum = 0L;
			_lateMax = 0L;
			_perfFrames = 0;
			_allocSum = 0L;
			_nextPerf = time + value;
		}
	}

	private static bool ButtonDown(InputAction action, eFocusState filter)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			DelGetButton getButtonDown = InputMapper.GetButtonDown;
			return getButtonDown != null && getButtonDown.Invoke(action, filter);
		}
		catch
		{
			return false;
		}
	}

	private static bool ButtonHeld(InputAction action, eFocusState filter)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			DelGetButton getButton = InputMapper.GetButton;
			return getButton != null && getButton.Invoke(action, filter);
		}
		catch
		{
			return false;
		}
	}

	private static bool IsNull(Il2CppObjectBase o)
	{
		if (o == null || o.Pointer == IntPtr.Zero)
		{
			return true;
		}
		try
		{
			return o.WasCollected;
		}
		catch
		{
			return false;
		}
	}
}
internal static class ReloadHook
{
	internal static bool Apply(Harmony harmony)
	{
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Expected O, but got Unknown
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Expected O, but got Unknown
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Expected O, but got Unknown
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Expected O, but got Unknown
		bool flag = default(bool);
		try
		{
			MethodInfo methodInfo = AccessTools.Method(typeof(ItemEquippable), "TryTriggerReloadAnimationSequence", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Plugin.L.LogError((object)"ItemEquippable.TryTriggerReloadAnimationSequence not found.");
				return false;
			}
			harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(ReloadHook), "OnReloadStarted", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Plugin.L.LogInfo((object)"hooked ItemEquippable.TryTriggerReloadAnimationSequence");
			try
			{
				MethodInfo methodInfo2 = AccessTools.Method(typeof(FirstPersonItemHolder), "ChangeState", (Type[])null, (Type[])null);
				if (methodInfo2 != null)
				{
					harmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(AccessTools.Method(typeof(ReloadHook), "OnHolderChangeState", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					Plugin.L.LogInfo((object)"hooked FirstPersonItemHolder.ChangeState (early driver)");
				}
			}
			catch (Exception ex)
			{
				ManualLogSource l = Plugin.L;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(31, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("early-creation hook skipped: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.GetType().Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				l.LogWarning(val);
			}
			return true;
		}
		catch (Exception ex2)
		{
			ManualLogSource l2 = Plugin.L;
			BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(53, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("patching TryTriggerReloadAnimationSequence failed: ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex2.GetType().Name);
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex2.Message);
			}
			l2.LogError(val2);
			return false;
		}
	}

	public static void OnHolderChangeState(FPItemStateName __0)
	{
		ReloadDriver.EnsureCreated();
	}

	public static void OnReloadStarted(ItemEquippable __instance, bool __result)
	{
		if (__result)
		{
			ReloadDriver.NotifyReloadStarted(__instance);
		}
	}
}