Decompiled source of ReloadTimer v1.5.0

Zose_ReloadTimer.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using CellMenu;
using GameData;
using Gear;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Microsoft.CodeAnalysis;
using Player;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("Zose_ReloadTimer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Zose_ReloadTimer")]
[assembly: AssemblyTitle("Zose_ReloadTimer")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace Zose_ReloadTimer
{
	public static class PluginData
	{
		public const string GUID = "Zose_ReloadTimer";

		public const string Name = "Zose_ReloadTimer";

		public const string Version = "1.5.0";
	}
	[BepInPlugin("Zose_ReloadTimer", "Zose_ReloadTimer", "1.5.0")]
	public class EntryPoint : BasePlugin
	{
		public static EntryPoint? Instance;

		public static ConfigEntry<float>? ConfigOffset;

		public static ConfigEntry<string>? ConfigChargeColor;

		public static ConfigEntry<float>? ConfigLowAmmoThreshold;

		public static ConfigEntry<string>? ConfigLowAmmoColor;

		public static Color ParsedChargeColor = new Color(0f, 1f, 0f, 0.8f);

		public static Color ParsedLowAmmoColor = new Color(1f, 0f, 0f, 0.8f);

		public static bool NeedsConfigReload = false;

		private static Harmony? harmony;

		private static FileSystemWatcher? _configWatcher;

		public override void Load()
		{
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Expected O, but got Unknown
			Instance = this;
			ClassInjector.RegisterTypeInIl2Cpp<Zose_ReloadTimer>();
			ConfigOffset = ((BasePlugin)this).Config.Bind<float>("Settings", "TextOffset", 85f, "The vertical offset of the reload text from the crosshair.");
			ConfigOffset.SettingChanged += delegate
			{
				Zose_ReloadTimer.Instance?.UpdateUIPosition();
			};
			ConfigChargeColor = ((BasePlugin)this).Config.Bind<string>("Settings", "ChargeColor", "#00FF0080", "The color of the reload charge circle in Hex format (#RRGGBBAA).");
			ConfigLowAmmoThreshold = ((BasePlugin)this).Config.Bind<float>("AmmoWarning", "LowAmmoThreshold", 0.35f, "The ammo threshold (percentage 0.0-1.0) for large weapons (>20 clip size).");
			ConfigOffset = ((BasePlugin)this).Config.Bind<float>("Settings", "TextOffset", 85f, "Vertical offset of the reload text from the crosshair.");
			ConfigOffset.SettingChanged += delegate
			{
				Zose_ReloadTimer.Instance?.UpdateUIPosition();
			};
			ConfigChargeColor = ((BasePlugin)this).Config.Bind<string>("Settings", "ChargeColor", "#00FF0080", "Color of the reload charge circle in Hex format (#RRGGBBAA).");
			ConfigLowAmmoThreshold = ((BasePlugin)this).Config.Bind<float>("AmmoWarning", "LowAmmoThreshold", 0.35f, "Ammo threshold (0.0-1.0) for warning.");
			ConfigLowAmmoColor = ((BasePlugin)this).Config.Bind<string>("AmmoWarning", "LowAmmoColor", "#FF000080", "Color of the 'RELOAD' warning text in Hex format (#RRGGBBAA).");
			UpdateParsedColor();
			ConfigChargeColor.SettingChanged += delegate
			{
				UpdateParsedColor();
			};
			ConfigLowAmmoColor.SettingChanged += delegate
			{
				UpdateParsedColor();
			};
			SetupConfigWatcher();
			harmony = new Harmony("Zose_ReloadTimer");
			harmony.PatchAll();
			Zose_ReloadTimer.LoadCache();
			Logger.Info("Zose_ReloadTimer 1.5.0 loaded.");
		}

		public void ReloadConfig()
		{
			((BasePlugin)this).Config.Reload();
			UpdateParsedColor();
			Logger.Info("Config reloaded from disk.");
		}

		private void UpdateParsedColor()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			Color parsedChargeColor = default(Color);
			if (ConfigChargeColor != null && ColorUtility.TryParseHtmlString(ConfigChargeColor.Value, ref parsedChargeColor))
			{
				ParsedChargeColor = parsedChargeColor;
			}
			Color parsedLowAmmoColor = default(Color);
			if (ConfigLowAmmoColor != null && ColorUtility.TryParseHtmlString(ConfigLowAmmoColor.Value, ref parsedLowAmmoColor))
			{
				ParsedLowAmmoColor = parsedLowAmmoColor;
			}
		}

		private void SetupConfigWatcher()
		{
			try
			{
				_configWatcher = new FileSystemWatcher(Path.GetDirectoryName(((BasePlugin)this).Config.ConfigFilePath), Path.GetFileName(((BasePlugin)this).Config.ConfigFilePath));
				_configWatcher.NotifyFilter = NotifyFilters.LastWrite;
				_configWatcher.Changed += delegate
				{
					NeedsConfigReload = true;
				};
				_configWatcher.EnableRaisingEvents = true;
			}
			catch (Exception ex)
			{
				Logger.Error("Failed to setup ConfigWatcher: " + ex.Message);
			}
		}
	}
	internal static class Logger
	{
		private static readonly ManualLogSource _Logger;

		static Logger()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Expected O, but got Unknown
			_Logger = new ManualLogSource("Zose_ReloadTimer");
			Logger.Sources.Add((ILogSource)(object)_Logger);
		}

		public static void Info(string str)
		{
			_Logger.LogMessage((object)str);
		}

		public static void Info(object data)
		{
			_Logger.LogMessage((object)data?.ToString());
		}

		public static void Debug(string str)
		{
			_Logger.LogDebug((object)str);
		}

		public static void Debug(object data)
		{
			_Logger.LogDebug((object)data?.ToString());
		}

		public static void Error(string str)
		{
			_Logger.LogError((object)str);
		}

		public static void Error(object data)
		{
			_Logger.LogError((object)data?.ToString());
		}

		public static void Fatal(string str)
		{
			_Logger.LogFatal((object)str);
		}

		public static void Fatal(object data)
		{
			_Logger.LogFatal((object)data?.ToString());
		}

		public static void Warn(string str)
		{
			_Logger.LogWarning((object)str);
		}

		public static void Warn(object data)
		{
			_Logger.LogWarning((object)data?.ToString());
		}
	}
	public class Zose_ReloadTimer : MonoBehaviour
	{
		private TextMeshProUGUI _textMesh;

		private TextMeshProUGUI _glitchTextMesh2;

		private GameObject _uiRoot;

		public static Zose_ReloadTimer Instance;

		private bool _isReloading;

		private float _reloadStartTime;

		private float _lastReloadDuration;

		private string _currentWeaponName = "";

		private float _lastFireTime = -10f;

		private int _lastClipCount = -1;

		private static Dictionary<string, float> _persistentRatioMap = new Dictionary<string, float>();

		private string _currFingerprint = "";

		private bool _ammoFilledThisReload;

		private bool _isStutteringDuringReload;

		private float _readyEffectStartTime = -1f;

		private bool _hasTriggeredReadyEffect;

		private bool _isAnimatingReady;

		private bool _waitingForNativeReloadToEnd;

		private bool _isChargeWeapon;

		private float _lastGlitchTime;

		private string _currGlitchedText = "";

		private const string GLITCH_CHARS = "#@&!$%01X?";

		private bool _chargeShown;

		private static bool _reflectionChecked = false;

		private Color _originalChargeColor;

		private static MethodInfo? _setChargeUpVisibleAndProgressMethod;

		private static Il2CppObjectBase? _circleTarget;

		private static MethodInfo? _setChargeMethod;

		private static MethodInfo? _showChargeMethod;

		private static MethodInfo? _setChargeColorMethod;

		private static FieldInfo? _chargeUpColOrgField;

		private static FieldInfo? _ammoField;

		private static FieldInfo? _isAimingField;

		private IntPtr _lastWieldPointer = IntPtr.Zero;

		private BulletWeapon? _cachedBulletWeapon;

		private static MethodInfo? _getIdentifierMethod;

		private PlayerAgent? _localPlayer;

		private const float MAX_SWAY_OFFSET = 12f;

		private const float SWAY_SENSITIVITY = 1.2f;

		private const float SWAY_SMOOTHING = 0.12f;

		private const float ADS_SWAY_MULTIPLIER = 0.25f;

		private Vector2 _currentSwayOffset = Vector2.zero;

		private Vector2 _swayVelocity = Vector2.zero;

		private bool _isFirstSwayFrame = true;

		private static string _cachePath = Path.Combine(Paths.ConfigPath, "Zose_ReloadTimer_Cache.txt");

		private static bool _cacheLoaded = false;

		private static string _cachedRundownInfo = "";

		private Vector3 _lastCamForward = Vector3.one;

		public void Awake()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Expected O, but got Unknown
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Expected O, but got Unknown
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Logger.Info("Zose_ReloadTimer INITIALIZED.");
			if (!_cacheLoaded)
			{
				LoadCache();
			}
			_uiRoot = new GameObject("ReloadTimer")
			{
				layer = 5
			};
			_uiRoot.transform.SetParent(((Component)GuiManager.CrosshairLayer.m_circleCrosshair).transform.parent, false);
			UpdateUIPosition();
			GameObject val = new GameObject("MainText")
			{
				layer = 5
			};
			val.transform.SetParent(_uiRoot.transform, false);
			_textMesh = SetupTextMesh(val, new Color(1f, 1f, 1f, 1f));
			new GameObject("GlitchR")
			{
				layer = 5
			}.transform.SetParent(_uiRoot.transform, false);
			GameObject val2 = new GameObject("GlitchC")
			{
				layer = 5
			};
			val2.transform.SetParent(_uiRoot.transform, false);
			_glitchTextMesh2 = SetupTextMesh(val2, new Color(1f, 1f, 1f, 0f));
		}

		public string GetWeaponFingerprint(BulletWeapon weapon)
		{
			if ((Object)(object)weapon == (Object)null)
			{
				return "";
			}
			if (string.IsNullOrEmpty(_cachedRundownInfo) || _cachedRundownInfo == "RD_UNK")
			{
				_cachedRundownInfo = "";
				try
				{
					Type typeFromHandle = typeof(RundownManager);
					BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
					string[] array = new string[3] { "CurrentlySelectedRundownKey", "ActiveRundownData", "m_activeRundownKey" };
					foreach (string name in array)
					{
						try
						{
							PropertyInfo property = typeFromHandle.GetProperty(name, bindingAttr);
							FieldInfo field = typeFromHandle.GetField(name, bindingAttr);
							object obj = property?.GetValue(null) ?? field?.GetValue(null);
							if (obj != null)
							{
								string text2 = ((obj is string text) ? text : obj.ToString());
								if (!string.IsNullOrEmpty(text2) && text2 != "0" && !text2.Contains("DataBlock"))
								{
									_cachedRundownInfo = "RD_" + text2;
									break;
								}
							}
						}
						catch
						{
						}
					}
					if (string.IsNullOrEmpty(_cachedRundownInfo))
					{
						try
						{
							Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
							foreach (Assembly assembly in assemblies)
							{
								string fullName = assembly.FullName;
								if (fullName.Contains("System") || fullName.Contains("Unity") || fullName.Contains("mscorlib") || fullName.Contains("BepInEx"))
								{
									continue;
								}
								Type[] types = assembly.GetTypes();
								foreach (Type type in types)
								{
									if (type.Name.Contains("Config") || type.Name.Contains("Rundown") || type.Name.Contains("Archive") || type.Name.Contains("Global"))
									{
										PropertyInfo[] properties = type.GetProperties(bindingAttr);
										foreach (PropertyInfo propertyInfo in properties)
										{
											if (!(propertyInfo.PropertyType == typeof(string)) || (!propertyInfo.Name.Contains("Rundown") && !propertyInfo.Name.Contains("SelectedKey")))
											{
												continue;
											}
											try
											{
												string text3 = propertyInfo.GetValue(null)?.ToString();
												if (!string.IsNullOrEmpty(text3) && text3.Length > 2 && !text3.Contains(" ") && !text3.Contains("/"))
												{
													_cachedRundownInfo = text3;
													break;
												}
											}
											catch
											{
											}
										}
										if (!string.IsNullOrEmpty(_cachedRundownInfo))
										{
											break;
										}
										FieldInfo[] fields = type.GetFields(bindingAttr);
										foreach (FieldInfo fieldInfo in fields)
										{
											if (!(fieldInfo.FieldType == typeof(string)) || (!fieldInfo.Name.Contains("Rundown") && !fieldInfo.Name.Contains("ActiveKey")))
											{
												continue;
											}
											try
											{
												string text4 = fieldInfo.GetValue(null)?.ToString();
												if (!string.IsNullOrEmpty(text4) && text4.Length > 2 && !text4.Contains(" "))
												{
													_cachedRundownInfo = text4;
													break;
												}
											}
											catch
											{
											}
										}
									}
									if (!string.IsNullOrEmpty(_cachedRundownInfo))
									{
										break;
									}
								}
								if (!string.IsNullOrEmpty(_cachedRundownInfo))
								{
									break;
								}
							}
						}
						catch
						{
						}
					}
					if (string.IsNullOrEmpty(_cachedRundownInfo))
					{
						try
						{
							object obj6 = typeFromHandle.GetMethod("GetActiveRundownData", bindingAttr)?.Invoke(null, null);
							if (obj6 != null)
							{
								_cachedRundownInfo = (obj6.GetType().GetProperty("name") ?? obj6.GetType().GetProperty("Key"))?.GetValue(obj6)?.ToString() ?? "RD_UNK";
							}
						}
						catch
						{
						}
					}
					if (string.IsNullOrEmpty(_cachedRundownInfo))
					{
						_cachedRundownInfo = "RD_UNK";
					}
					if (_cachedRundownInfo != "RD_UNK")
					{
						_cachedRundownInfo = Regex.Replace(_cachedRundownInfo, "[^a-zA-Z0-9_-]", "");
						Logger.Info("Rundown Identified: " + _cachedRundownInfo);
					}
				}
				catch (Exception ex)
				{
					Logger.Error("Rundown Scan Error: " + ex.Message);
				}
			}
			return $"{_cachedRundownInfo}_{((GameDataBlockBase<ArchetypeDataBlock>)(object)((ItemEquippable)weapon).ArchetypeData).persistentID}_{((Item)weapon).PublicName}_{((ItemEquippable)weapon).ArchetypeData.DefaultReloadTime:F2}_{((ItemEquippable)weapon).ClipSize}";
		}

		public static void LoadCache()
		{
			if (string.IsNullOrEmpty(_cachePath))
			{
				return;
			}
			try
			{
				if (!File.Exists(_cachePath))
				{
					Logger.Info("No existing calibration cache found. Starting fresh.");
					_cacheLoaded = true;
					return;
				}
				string[] array = File.ReadAllLines(_cachePath);
				foreach (string text in array)
				{
					if (!string.IsNullOrWhiteSpace(text))
					{
						string[] array2 = text.Split('=');
						if (array2.Length == 2 && float.TryParse(array2[1], out var result))
						{
							_persistentRatioMap[array2[0]] = result;
						}
					}
				}
				_cacheLoaded = true;
				Logger.Info($"SUCCESS: Loaded {_persistentRatioMap.Count} calibrated weapon profiles from disk.");
			}
			catch (Exception ex)
			{
				Logger.Error("Cache Load Error: " + ex.Message);
			}
		}

		private static void SaveCache()
		{
			try
			{
				List<string> list = new List<string>();
				foreach (KeyValuePair<string, float> item in _persistentRatioMap)
				{
					list.Add($"{item.Key}={item.Value:F4}");
				}
				File.WriteAllLines(_cachePath, list);
			}
			catch (Exception ex)
			{
				Logger.Error("Cache Save Error: " + ex.Message);
			}
		}

		private TextMeshProUGUI SetupTextMesh(GameObject go, Color col)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			TextMeshProUGUI obj = go.AddComponent<TextMeshProUGUI>();
			((TMP_Text)obj).alignment = (TextAlignmentOptions)514;
			((Component)obj).GetComponent<RectTransform>().sizeDelta = new Vector2(400f, 100f);
			((TMP_Text)obj).fontSize = 20f;
			((TMP_Text)obj).font = ((TMP_Text)GuiManager.PlayerLayer.m_playerStatus.m_healthText).font;
			((Graphic)obj).color = col;
			((TMP_Text)obj).characterSpacing = 0f;
			((TMP_Text)obj).richText = true;
			return obj;
		}

		public void OnAmmoRefreshed()
		{
			if (!_isReloading || _ammoFilledThisReload)
			{
				return;
			}
			_ammoFilledThisReload = true;
			if (_isStutteringDuringReload)
			{
				Logger.Info("Stutter detected during reload, skipping calibration to maintain accuracy.");
			}
			else
			{
				float num = Time.time - _reloadStartTime;
				if (_lastReloadDuration > 0f && !string.IsNullOrEmpty(_currFingerprint))
				{
					float num2 = Mathf.Clamp(num / _lastReloadDuration, 0.3f, 0.98f);
					if (!_persistentRatioMap.ContainsKey(_currFingerprint))
					{
						_persistentRatioMap[_currFingerprint] = num2;
						SaveCache();
					}
					else
					{
						float num3 = _persistentRatioMap[_currFingerprint];
						if (Mathf.Abs(num2 - num3) > 0.01f)
						{
							float value = (num3 * 4f + num2) / 5f;
							_persistentRatioMap[_currFingerprint] = value;
							SaveCache();
						}
					}
				}
			}
			if (!_hasTriggeredReadyEffect)
			{
				_readyEffectStartTime = Time.time;
				_hasTriggeredReadyEffect = true;
				_isAnimatingReady = true;
			}
		}

		private void Update()
		{
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			if (EntryPoint.NeedsConfigReload)
			{
				EntryPoint.NeedsConfigReload = false;
				EntryPoint.Instance?.ReloadConfig();
				UpdateUIPosition();
			}
			if ((Object)(object)_localPlayer == (Object)null)
			{
				_localPlayer = PlayerManager.GetLocalPlayerAgent();
				if ((Object)(object)_localPlayer == (Object)null)
				{
					return;
				}
			}
			PlayerInventoryBase inventory = _localPlayer.Inventory;
			ItemEquippable val = ((inventory != null) ? inventory.WieldedItem : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			if (((Il2CppObjectBase)val).Pointer != _lastWieldPointer)
			{
				_lastWieldPointer = ((Il2CppObjectBase)val).Pointer;
				_cachedBulletWeapon = ((Il2CppObjectBase)val).TryCast<BulletWeapon>();
				ResetReloadState();
				if ((Object)(object)_cachedBulletWeapon != (Object)null)
				{
					_currentWeaponName = ((Item)_cachedBulletWeapon).PublicName;
					_currFingerprint = GetWeaponFingerprint(_cachedBulletWeapon);
					_lastReloadDuration = ((ItemEquippable)_cachedBulletWeapon).ArchetypeData.DefaultReloadTime * GetWeaponMultiplier(_cachedBulletWeapon);
					_isChargeWeapon = DetectIsChargeWeapon(_cachedBulletWeapon);
				}
				UpdateUIPosition();
			}
			if ((Object)(object)_cachedBulletWeapon == (Object)null)
			{
				return;
			}
			if (!_reflectionChecked && GuiManager.CrosshairLayer != null)
			{
				InitializeReflection();
			}
			bool isReloading = ((ItemEquippable)_cachedBulletWeapon).IsReloading;
			if (isReloading && !_isReloading && !_waitingForNativeReloadToEnd)
			{
				_isReloading = true;
				_ammoFilledThisReload = false;
				_reloadStartTime = Time.time;
				_currGlitchedText = string.Empty;
				_lastReloadDuration = ((ItemEquippable)_cachedBulletWeapon).ArchetypeData.DefaultReloadTime * GetWeaponMultiplier(_cachedBulletWeapon);
				if (!_chargeShown && _circleTarget != null)
				{
					if (_chargeUpColOrgField != null)
					{
						_originalChargeColor = (Color)_chargeUpColOrgField.GetValue(_circleTarget);
					}
					_setChargeColorMethod?.Invoke(_circleTarget, new object[1] { EntryPoint.ParsedChargeColor });
					_chargeUpColOrgField?.SetValue(_circleTarget, EntryPoint.ParsedChargeColor);
					_setChargeUpVisibleAndProgressMethod?.Invoke(GuiManager.CrosshairLayer, new object[2] { true, 0f });
					_showChargeMethod?.Invoke(_circleTarget, new object[1] { true });
					_chargeShown = true;
				}
				_isStutteringDuringReload = false;
			}
			if (_isReloading && !_ammoFilledThisReload && Time.deltaTime > 0.1f)
			{
				_isStutteringDuringReload = true;
			}
			else if (!isReloading)
			{
				_waitingForNativeReloadToEnd = false;
				if (_isReloading)
				{
					if (!_isAnimatingReady)
					{
						ResetReloadState();
					}
					else
					{
						_isReloading = false;
					}
				}
			}
			if (_isAnimatingReady && Time.time - _readyEffectStartTime >= 0.5f)
			{
				ResetReloadState();
			}
			if (isReloading || _isAnimatingReady)
			{
				return;
			}
			int clip = _cachedBulletWeapon.m_clip;
			int clipSize = ((ItemEquippable)_cachedBulletWeapon).ClipSize;
			if (clip < _lastClipCount && clip >= 0)
			{
				_lastFireTime = Time.time;
			}
			_lastClipCount = clip;
			if (isReloading || _isAnimatingReady)
			{
				return;
			}
			float num = ((clipSize <= 20) ? 0.5f : (EntryPoint.ConfigLowAmmoThreshold?.Value ?? 0.35f));
			if ((float)clip <= (float)clipSize * num)
			{
				if (Time.time - _lastGlitchTime > 0.08f)
				{
					_lastGlitchTime = Time.time;
					bool flag = clip == 0;
					string original = "RELOAD";
					_currGlitchedText = GetGlitchedText(original, flag ? 0.4f : 0.25f);
				}
			}
			else
			{
				_currGlitchedText = string.Empty;
			}
		}

		private void LateUpdate()
		{
			//IL_0339: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_0475: Unknown result type (might be due to invalid IL or missing references)
			//IL_0413: Unknown result type (might be due to invalid IL or missing references)
			//IL_0428: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0389: Unknown result type (might be due to invalid IL or missing references)
			//IL_038e: Unknown result type (might be due to invalid IL or missing references)
			//IL_039f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0401: Unknown result type (might be due to invalid IL or missing references)
			//IL_056b: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0623: Unknown result type (might be due to invalid IL or missing references)
			//IL_0646: Unknown result type (might be due to invalid IL or missing references)
			//IL_0667: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_cachedBulletWeapon == (Object)null || (Object)(object)_localPlayer == (Object)null)
			{
				return;
			}
			ApplyHUDSway();
			bool isReloading = ((ItemEquippable)_cachedBulletWeapon).IsReloading;
			bool flag = _isAnimatingReady && Time.time - _readyEffectStartTime < 0.5f;
			bool flag2 = isReloading && !_ammoFilledThisReload;
			if (flag2 || flag || !string.IsNullOrEmpty(_currGlitchedText))
			{
				string text = "";
				if (flag)
				{
					text = "0.00s";
					if (_circleTarget != null)
					{
						float num = Mathf.Clamp01((Time.time - _readyEffectStartTime) / 0.5f);
						float num2 = Mathf.Lerp(1.25f, 1f, num);
						GameObject gameObject = _circleTarget.Cast<Component>().gameObject;
						if ((Object)(object)gameObject != (Object)null)
						{
							gameObject.transform.localScale = new Vector3(num2, num2, 1f);
						}
						_showChargeMethod?.Invoke(_circleTarget, new object[1] { true });
						_setChargeMethod?.Invoke(_circleTarget, new object[1] { 1f });
						_setChargeUpVisibleAndProgressMethod?.Invoke(GuiManager.CrosshairLayer, new object[2] { true, 1f });
					}
				}
				else if (flag2)
				{
					float num3 = Time.time - _reloadStartTime;
					float num4 = 0.9f;
					if (!string.IsNullOrEmpty(_currFingerprint) && _persistentRatioMap.ContainsKey(_currFingerprint))
					{
						num4 = _persistentRatioMap[_currFingerprint];
					}
					else if (((ItemEquippable)_cachedBulletWeapon).ArchetypeData.DefaultClipSize > 10)
					{
						num4 = 0.6f;
					}
					float num5 = _lastReloadDuration * num4;
					float num6 = Mathf.Clamp01(num3 / num5);
					float num7 = Mathf.Max(0f, num5 - num3);
					text = ((num7 > 0f) ? $"{num7:0.00}s" : "");
					if (_circleTarget != null)
					{
						_showChargeMethod?.Invoke(_circleTarget, new object[1] { true });
						_setChargeMethod?.Invoke(_circleTarget, new object[1] { num6 });
						_setChargeUpVisibleAndProgressMethod?.Invoke(GuiManager.CrosshairLayer, new object[2] { true, num6 });
						GameObject gameObject2 = _circleTarget.Cast<Component>().gameObject;
						if ((Object)(object)gameObject2 != (Object)null)
						{
							gameObject2.transform.localScale = Vector3.one;
						}
					}
				}
				else if (!string.IsNullOrEmpty(_currGlitchedText))
				{
					text = _currGlitchedText;
				}
				((TMP_Text)_textMesh).text = text;
				if (flag)
				{
					((Graphic)_textMesh).color = new Color(0f, 1f, 0f, 0.8f);
				}
				else if (!string.IsNullOrEmpty(_currGlitchedText) && !isReloading)
				{
					float a = ((Mathf.Floor(Time.time * 5f) % 2f == 0f) ? 0.85f : 0.75f);
					Color parsedLowAmmoColor = EntryPoint.ParsedLowAmmoColor;
					parsedLowAmmoColor.a = a;
					((Graphic)_textMesh).color = parsedLowAmmoColor;
					float value = Random.value;
					float num8 = 0f;
					if (value < 0.04f)
					{
						num8 = Random.Range(-6f, 6f);
					}
					else if (value < 0.05f)
					{
						num8 = Random.Range(-12f, 12f);
					}
					((Transform)((TMP_Text)_textMesh).rectTransform).localPosition = new Vector3(num8, 0f, 0f);
				}
				else
				{
					((Graphic)_textMesh).color = Color.white;
					((Transform)((TMP_Text)_textMesh).rectTransform).localPosition = Vector3.zero;
				}
			}
			else
			{
				((TMP_Text)_textMesh).text = string.Empty;
			}
			ClearGlitchLayers();
			if (_circleTarget == null)
			{
				return;
			}
			if (flag)
			{
				_setChargeColorMethod?.Invoke(_circleTarget, new object[1] { ((Graphic)_textMesh).color });
			}
			else if (flag2)
			{
				_setChargeColorMethod?.Invoke(_circleTarget, new object[1] { EntryPoint.ParsedChargeColor });
			}
			if (flag2 || flag)
			{
				return;
			}
			if (isReloading)
			{
				_setChargeUpVisibleAndProgressMethod?.Invoke(GuiManager.CrosshairLayer, new object[2] { false, 0f });
				_showChargeMethod?.Invoke(_circleTarget, new object[1] { false });
				_setChargeMethod?.Invoke(_circleTarget, new object[1] { 0f });
				GameObject gameObject3 = _circleTarget.Cast<Component>().gameObject;
				if ((Object)(object)gameObject3 != (Object)null)
				{
					gameObject3.transform.localScale = Vector3.one;
				}
			}
			else if (!_isChargeWeapon)
			{
				_setChargeUpVisibleAndProgressMethod?.Invoke(GuiManager.CrosshairLayer, new object[2] { false, 0f });
				_showChargeMethod?.Invoke(_circleTarget, new object[1] { false });
				_setChargeMethod?.Invoke(_circleTarget, new object[1] { 0f });
				GameObject gameObject4 = _circleTarget.Cast<Component>().gameObject;
				if ((Object)(object)gameObject4 != (Object)null)
				{
					gameObject4.transform.localScale = Vector3.one;
				}
				_setChargeColorMethod?.Invoke(_circleTarget, new object[1] { _originalChargeColor });
				_chargeUpColOrgField?.SetValue(_circleTarget, _originalChargeColor);
			}
		}

		private void ClearGlitchLayers()
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_glitchTextMesh2 != (Object)null)
			{
				((TMP_Text)_glitchTextMesh2).text = "";
				((Graphic)_glitchTextMesh2).color = new Color(0f, 0f, 0f, 0f);
			}
		}

		private void UpdateGlitchArt(Color baseColor)
		{
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_glitchTextMesh2 == (Object)null) && Time.frameCount % 4 == 0)
			{
				((TMP_Text)_glitchTextMesh2).text = GetGlitchedText(((TMP_Text)_textMesh).text, 0.04f);
				float num = Time.time * 15f;
				float num2 = Mathf.Sin(num) * 0.4f;
				float num3 = Mathf.Cos(num * 0.8f) * 0.3f;
				float num4 = ((Random.value > 0.01f) ? 1f : 0.5f);
				float num5 = ((baseColor.r > 0.5f && baseColor.g < 0.5f) ? 0.35f : 0.12f);
				Color color = baseColor;
				color.a = num5 * 0.7f * num4;
				((Transform)((TMP_Text)_glitchTextMesh2).rectTransform).localPosition = new Vector3((0f - num2) * 0.4f, (0f - num3) * 0.4f, 0f);
				((Graphic)_glitchTextMesh2).color = color;
			}
		}

		private string GetGlitchedText(string original, float intensity)
		{
			if (string.IsNullOrEmpty(original) || Random.value > intensity)
			{
				return original;
			}
			char[] array = original.ToCharArray();
			int num = Random.Range(0, array.Length);
			if (array[num] != ' ' && array[num] != '[' && array[num] != ']' && array[num] != '[' && array[num] != ']')
			{
				array[num] = "#@&!$%01X?"[Random.Range(0, "#@&!$%01X?".Length)];
			}
			return new string(array);
		}

		private void ResetReloadState()
		{
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			_isReloading = false;
			_isAnimatingReady = false;
			_hasTriggeredReadyEffect = false;
			_readyEffectStartTime = -1f;
			((TMP_Text)_textMesh).text = string.Empty;
			_currGlitchedText = string.Empty;
			_lastClipCount = -1;
			_waitingForNativeReloadToEnd = (Object)(object)_cachedBulletWeapon != (Object)null && ((ItemEquippable)_cachedBulletWeapon).IsReloading;
			if (_circleTarget != null)
			{
				_showChargeMethod?.Invoke(_circleTarget, new object[1] { false });
				_setChargeMethod?.Invoke(_circleTarget, new object[1] { 0f });
				_setChargeUpVisibleAndProgressMethod?.Invoke(GuiManager.CrosshairLayer, new object[2] { false, 0f });
				if (_chargeShown)
				{
					_setChargeColorMethod?.Invoke(_circleTarget, new object[1] { _originalChargeColor });
					_chargeUpColOrgField?.SetValue(_circleTarget, _originalChargeColor);
					GameObject gameObject = _circleTarget.Cast<Component>().gameObject;
					if ((Object)(object)gameObject != (Object)null)
					{
						gameObject.transform.localScale = Vector3.one;
					}
					_chargeShown = false;
				}
			}
			ClearGlitchLayers();
		}

		private void ApplyHUDSway()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			FPSCamera fPSCamera = _localPlayer.FPSCamera;
			if ((Object)(object)fPSCamera == (Object)null)
			{
				return;
			}
			Vector3 forward = ((Component)fPSCamera).transform.forward;
			if (_isFirstSwayFrame)
			{
				_lastCamForward = forward;
				_isFirstSwayFrame = false;
				return;
			}
			Vector3 val = ((Component)fPSCamera).transform.InverseTransformDirection(forward - _lastCamForward);
			_lastCamForward = forward;
			float num = 1800.0001f;
			bool flag = false;
			try
			{
				if (_isAimingField != null)
				{
					flag = (bool)_isAimingField.GetValue(_cachedBulletWeapon);
				}
			}
			catch
			{
			}
			if ((Object)(object)_cachedBulletWeapon != (Object)null && flag)
			{
				num *= 0.25f;
			}
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor((0f - val.x) * num, val.y * num);
			if (((Vector2)(ref val2)).magnitude > 12f)
			{
				val2 = ((Vector2)(ref val2)).normalized * 12f;
			}
			_currentSwayOffset = Vector2.SmoothDamp(_currentSwayOffset, val2, ref _swayVelocity, 0.12f);
			_currentSwayOffset.x = Mathf.Clamp(_currentSwayOffset.x, -12f, 12f);
			_currentSwayOffset.y = Mathf.Clamp(_currentSwayOffset.y, -12f, 12f);
			float num2 = EntryPoint.ConfigOffset?.Value ?? 85f;
			_uiRoot.transform.localPosition = new Vector3(_currentSwayOffset.x, 0f - num2 + _currentSwayOffset.y, 0f);
		}

		public void UpdateUIPosition()
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_uiRoot == (Object)null))
			{
				float num = EntryPoint.ConfigOffset?.Value ?? 85f;
				_uiRoot.transform.localPosition = new Vector3(0f, 0f - num, 0f);
			}
		}

		private float GetWeaponMultiplier(BulletWeapon weapon)
		{
			try
			{
				GearIDRange gearIDRange = ((ItemEquippable)weapon).GearIDRange;
				if (gearIDRange == null)
				{
					return 1f;
				}
				if (_getIdentifierMethod == null)
				{
					_getIdentifierMethod = ((object)gearIDRange).GetType().GetMethod("GetIdentifier");
				}
				if (_getIdentifierMethod == null)
				{
					return 1f;
				}
				for (int i = 0; i < 64; i++)
				{
					try
					{
						uint num = (uint)_getIdentifierMethod.Invoke(gearIDRange, new object[1] { (object)(InventorySlot)(byte)i });
						if (num != 0)
						{
							GearMagPartDataBlock block = GameDataBlockBase<GearMagPartDataBlock>.GetBlock(num);
							if (block != null)
							{
								return block.ReloadTimeMultiplier;
							}
						}
					}
					catch
					{
					}
				}
			}
			catch
			{
			}
			return 1f;
		}

		private void InitializeReflection()
		{
			_reflectionChecked = true;
			if (GuiManager.CrosshairLayer != null)
			{
				_setChargeUpVisibleAndProgressMethod = ((object)GuiManager.CrosshairLayer).GetType().GetMethod("SetChargeUpVisibleAndProgress");
				CircleCrosshair circleCrosshair = GuiManager.CrosshairLayer.m_circleCrosshair;
				if ((Object)(object)circleCrosshair != (Object)null)
				{
					_circleTarget = (Il2CppObjectBase?)(object)circleCrosshair;
					Type type = ((object)circleCrosshair).GetType();
					_setChargeMethod = type.GetMethod("SetCharge");
					_showChargeMethod = type.GetMethod("ShowCharge");
					_setChargeColorMethod = type.GetMethod("SetChargeUpColor");
					_chargeUpColOrgField = type.GetField("m_chargeUpColOrg", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				}
				Type typeFromHandle = typeof(BulletWeapon);
				if ((object)_ammoField == null)
				{
					_ammoField = typeFromHandle.GetField("m_ammo", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				}
				Type typeFromHandle2 = typeof(ItemEquippable);
				if ((object)_isAimingField == null)
				{
					_isAimingField = typeFromHandle2.GetField("m_isAiming", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				}
			}
		}

		private bool DetectIsChargeWeapon(BulletWeapon weapon)
		{
			if (((weapon != null) ? ((ItemEquippable)weapon).ArchetypeData : null) == null)
			{
				return false;
			}
			try
			{
				ArchetypeDataBlock archetypeData = ((ItemEquippable)weapon).ArchetypeData;
				Type type = ((object)archetypeData).GetType();
				BindingFlags bindingAttr = BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
				PropertyInfo[] properties = type.GetProperties(bindingAttr);
				foreach (PropertyInfo propertyInfo in properties)
				{
					if (propertyInfo.PropertyType == typeof(float) && propertyInfo.Name.IndexOf("charge", StringComparison.OrdinalIgnoreCase) >= 0 && ((float?)propertyInfo.GetValue(archetypeData)).GetValueOrDefault() > 0.001f)
					{
						return true;
					}
				}
				FieldInfo[] fields = type.GetFields(bindingAttr);
				foreach (FieldInfo fieldInfo in fields)
				{
					if (fieldInfo.FieldType == typeof(float) && fieldInfo.Name.IndexOf("charge", StringComparison.OrdinalIgnoreCase) >= 0 && ((float?)fieldInfo.GetValue(archetypeData)).GetValueOrDefault() > 0.001f)
					{
						return true;
					}
				}
			}
			catch
			{
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(PlayerInventoryLocal), "DoReload")]
	internal static class ReloadDonePatch
	{
		public static void Postfix()
		{
			if ((Object)(object)Zose_ReloadTimer.Instance != (Object)null)
			{
				Zose_ReloadTimer.Instance.OnAmmoRefreshed();
			}
		}
	}
}
namespace Zose_ReloadTimer.Patches
{
	[HarmonyPatch(typeof(CM_PageRundown_New), "PlaceRundown")]
	internal static class CM_PageRundown_New_PlaceRundown
	{
		private static bool _injected;

		private static void Postfix()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			if (!_injected)
			{
				_injected = true;
				GameObject val = new GameObject("RT_CompInject");
				val.AddComponent<Zose_ReloadTimer>();
				Object.DontDestroyOnLoad((Object)val);
			}
		}
	}
}