Decompiled source of Endgame Arsenal v1.0.5

ScheduleIEndgameArsenal.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using HarmonyLib;
using Il2Cpp;
using Il2CppFishNet.Connection;
using Il2CppFishNet.Object;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppRootMotion;
using Il2CppRootMotion.FinalIK;
using Il2CppScheduleOne;
using Il2CppScheduleOne.Audio;
using Il2CppScheduleOne.AvatarFramework;
using Il2CppScheduleOne.AvatarFramework.Animation;
using Il2CppScheduleOne.AvatarFramework.Equipping;
using Il2CppScheduleOne.Core.Audio;
using Il2CppScheduleOne.Core.Items.Framework;
using Il2CppScheduleOne.DevUtilities;
using Il2CppScheduleOne.Equipping;
using Il2CppScheduleOne.ItemFramework;
using Il2CppScheduleOne.Levelling;
using Il2CppScheduleOne.NPCs;
using Il2CppScheduleOne.Persistence;
using Il2CppScheduleOne.PlayerScripts;
using Il2CppScheduleOne.Storage;
using Il2CppScheduleOne.UI;
using Il2CppScheduleOne.UI.Items;
using Il2CppScheduleOne.UI.Shop;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using ScheduleIEndgameArsenal;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(ArsenalMod), "Schedule I Endgame Arsenal", "1.0.5", "nacho", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: MelonPlatformDomain(/*Could not decode attribute arguments.*/)]
[assembly: HarmonyDontPatchAll]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("ScheduleIEndgameArsenal")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.5.0")]
[assembly: AssemblyInformationalVersion("1.0.5+c02009702c06e41397470955ed5c61813b6c6995")]
[assembly: AssemblyProduct("ScheduleIEndgameArsenal")]
[assembly: AssemblyTitle("ScheduleIEndgameArsenal")]
[assembly: AssemblyVersion("1.0.5.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ScheduleIEndgameArsenal
{
	public sealed class ArsenalMod : MelonMod
	{
		private Harmony? patches;

		private ArsenalSettings? settings;

		private bool settingsDirty = true;

		private float nextAttempt;

		private int failureCount;

		private bool ready;

		public override void OnInitializeMelon()
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			settings = new ArsenalSettings(delegate
			{
				settingsDirty = true;
				nextAttempt = 0f;
			});
			RegistryAndShop.BindSettings(settings);
			patches = new Harmony("nacho.schedulei.endgamearsenal");
			ArsenalNetworkSession.Initialize(patches, settings);
			RegistryAndShop.Initialize(patches);
			WeaponBehavior.Initialize(patches);
			AvatarWeapons.Initialize(patches);
			HandPoses.Initialize(patches);
			WeaponAnimations.Initialize(patches);
			RemoteWeaponAnimations.Initialize(patches);
			SniperScope.Initialize(patches);
			SniperTargets.Initialize(patches);
			SniperChamber.Initialize(patches);
			((MelonBase)this).LoggerInstance.Msg("Endgame Arsenal 1.0.5 local test: co-op settings and remote weapon animations.");
		}

		public override void OnUpdate()
		{
			ArsenalNetworkSession.Update();
			RemoteWeaponAnimations.Update();
			SniperChamber.Update();
			if (ready)
			{
				SniperScope.Update();
			}
			if ((ready && RegistryAndShop.PortraitsComplete && !settingsDirty) || Time.unscaledTime < nextAttempt)
			{
				return;
			}
			nextAttempt = Time.unscaledTime + 1f;
			try
			{
				if (ready)
				{
					if (settingsDirty)
					{
						settingsDirty = !RegistryAndShop.ApplySettings();
						ArsenalNetworkSession.SettingsApplied(!settingsDirty);
					}
					if (!RegistryAndShop.PortraitsComplete)
					{
						RegistryAndShop.RetryModelPortraits();
					}
					return;
				}
				bool flag = RegistryAndShop.TryRegister();
				LoadManager instance = Singleton<LoadManager>.Instance;
				if ((Object)(object)instance == (Object)null || !instance.IsGameLoaded || instance.IsLoading)
				{
					return;
				}
				if (!flag)
				{
					nextAttempt = Time.unscaledTime + 5f;
					if (failureCount++ < 3)
					{
						((MelonBase)this).LoggerInstance.Warning("Arsenal items could not initialize from the game's M1911 and magazine templates. Retrying in 5 seconds.");
					}
					return;
				}
				if (RegistryAndShop.TryAddToStan() && !ready)
				{
					ready = true;
					settingsDirty = !RegistryAndShop.ApplySettings();
					ArsenalNetworkSession.SettingsApplied(!settingsDirty);
					((MelonBase)this).LoggerInstance.Msg("Stan's shop has the three endgame weapons and their matching rounds. Native rank requirements apply.");
				}
				failureCount = 0;
			}
			catch (Exception ex)
			{
				nextAttempt = Time.unscaledTime + 5f;
				if (failureCount++ < 3)
				{
					((MelonBase)this).LoggerInstance.Warning("Arsenal setup will retry: " + ex.GetType().Name + ": " + ex.Message);
				}
			}
		}

		public override void OnSceneWasLoaded(int buildIndex, string sceneName)
		{
			nextAttempt = 0f;
			ready = false;
			settingsDirty = true;
			failureCount = 0;
			ArsenalNetworkSession.Reset();
			RemoteWeaponAnimations.Reset();
			WeaponBehavior.Reset();
			SniperScope.Reset();
			SniperChamber.Reset();
			HandPoses.Reset();
			WeaponAnimations.Reset();
		}

		public override void OnDeinitializeMelon()
		{
			ArsenalNetworkSession.Reset();
			RemoteWeaponAnimations.Reset();
			SniperChamber.Reset();
			SniperScope.Reset();
			HandPoses.Reset();
			WeaponAnimations.Reset();
			settings?.Dispose();
			Harmony? obj = patches;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	internal enum ArsenalNetKind
	{
		Hello,
		Snapshot
	}
	internal sealed record ArsenalNetMessage(ArsenalNetKind Kind, string ClientNonce, string Fingerprint, string? HostNonce = null, int Revision = 0, WeaponShopSettings[]? Settings = null);
	internal static class ArsenalNetworkProtocol
	{
		internal const string Schema = "EA1";

		internal const int MaximumWireLength = 1024;

		internal const int WeaponCount = 3;

		private static readonly CultureInfo Invariant = CultureInfo.InvariantCulture;

		internal static string Encode(ArsenalNetMessage message)
		{
			if (message == null || !IsIdentity(message.ClientNonce) || !IsIdentity(message.Fingerprint))
			{
				throw new ArgumentException("A session nonce and matching-build fingerprint are required.", "message");
			}
			string text;
			if (message.Kind == ArsenalNetKind.Hello)
			{
				if (message.HostNonce != null || message.Revision != 0 || message.Settings != null)
				{
					throw new ArgumentException("Hello cannot carry host state or settings.", "message");
				}
				text = string.Join('|', "EA1", "H", message.ClientNonce, message.Fingerprint);
				goto IL_01f8;
			}
			if (message.Kind == ArsenalNetKind.Snapshot)
			{
				if (IsIdentity(message.HostNonce) && message.Revision >= 0)
				{
					WeaponShopSettings[]? settings = message.Settings;
					if (settings != null && settings.Length == 3)
					{
						string[] array = new string[9]
						{
							"EA1",
							"S",
							message.ClientNonce,
							message.HostNonce,
							message.Fingerprint,
							message.Revision.ToString(Invariant),
							null,
							null,
							null
						};
						for (int i = 0; i < 3; i++)
						{
							WeaponShopSettings value = message.Settings[i];
							if (!IsValid(value))
							{
								throw new ArgumentException("Snapshot settings are outside supported bounds.", "message");
							}
							array[6 + i] = string.Join(',', value.Rank.ToString(Invariant), value.Tier.ToString(Invariant), value.Price.ToString("R", Invariant), value.AmmoPrice.ToString("R", Invariant));
						}
						text = string.Join('|', array);
						goto IL_01f8;
					}
				}
				throw new ArgumentException("Snapshot requires a host nonce, revision and exactly three weapon settings.", "message");
			}
			throw new ArgumentException("Unsupported Arsenal message kind.", "message");
			IL_01f8:
			if (text.Length > 1024)
			{
				throw new ArgumentException("Arsenal message exceeds the wire limit.", "message");
			}
			return text;
		}

		internal static bool TryDecode(string? wire, out ArsenalNetMessage? message)
		{
			message = null;
			if (string.IsNullOrEmpty(wire) || wire.Length > 1024)
			{
				return false;
			}
			string[] array = wire.Split('|');
			if (array.Length < 2 || array[0] != "EA1")
			{
				return false;
			}
			if (array[1] == "H")
			{
				if (array.Length != 4 || !IsIdentity(array[2]) || !IsIdentity(array[3]))
				{
					return false;
				}
				message = new ArsenalNetMessage(ArsenalNetKind.Hello, array[2], array[3]);
				return true;
			}
			if (array[1] != "S" || array.Length != 9 || !IsIdentity(array[2]) || !IsIdentity(array[3]) || !IsIdentity(array[4]) || !TryInteger(array[5], out var result))
			{
				return false;
			}
			WeaponShopSettings[] array2 = new WeaponShopSettings[3];
			for (int i = 0; i < array2.Length; i++)
			{
				string[] array3 = array[6 + i].Split(',');
				if (array3.Length != 4 || !TryInteger(array3[0], out var result2) || !TryInteger(array3[1], out var result3) || !TryPrice(array3[2], 1000000f, out var result4) || !TryPrice(array3[3], 100000f, out var result5))
				{
					return false;
				}
				array2[i] = new WeaponShopSettings(result2, result3, result4, result5);
				if (!IsValid(array2[i]))
				{
					return false;
				}
			}
			message = new ArsenalNetMessage(ArsenalNetKind.Snapshot, array[2], array[4], array[3], result, array2);
			return true;
		}

		private static bool IsIdentity(string? value)
		{
			if (value == null || value.Length != 32)
			{
				return false;
			}
			for (int i = 0; i < value.Length; i++)
			{
				bool flag;
				switch (value[i])
				{
				default:
					flag = true;
					break;
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case 'a':
				case 'b':
				case 'c':
				case 'd':
				case 'e':
				case 'f':
					flag = false;
					break;
				}
				if (flag)
				{
					return false;
				}
			}
			return true;
		}

		private static bool IsValid(WeaponShopSettings value)
		{
			int rank = value.Rank;
			if (rank >= 0 && rank <= 10)
			{
				rank = value.Tier;
				if (rank >= 1 && rank <= 5 && float.IsFinite(value.Price))
				{
					float price = value.Price;
					if (price >= 0f && price <= 1000000f && float.IsFinite(value.AmmoPrice))
					{
						price = value.AmmoPrice;
						if (price >= 0f)
						{
							return price <= 100000f;
						}
						return false;
					}
				}
			}
			return false;
		}

		private static bool TryInteger(string value, out int result)
		{
			result = 0;
			int length = value.Length;
			if (length > 0 && length <= 10 && (value.Length == 1 || value[0] != '0'))
			{
				return int.TryParse(value, NumberStyles.None, Invariant, out result);
			}
			return false;
		}

		private static bool TryPrice(string value, float maximum, out float result)
		{
			result = 0f;
			if (!IsDecimal(value) || !double.TryParse(value, NumberStyles.Float, Invariant, out var result2) || !double.IsFinite(result2) || result2 < 0.0 || result2 > (double)maximum || !float.TryParse(value, NumberStyles.Float, Invariant, out result) || !float.IsFinite(result))
			{
				return false;
			}
			if (result >= 0f)
			{
				return result <= maximum;
			}
			return false;
		}

		private static bool IsDecimal(string value)
		{
			int length = value.Length;
			if ((length > 40 || length == 0) ? true : false)
			{
				return false;
			}
			int i = ((value[0] == '-') ? 1 : 0);
			if (i == value.Length)
			{
				return false;
			}
			if (value[i] == '0')
			{
				i++;
			}
			else
			{
				char c = value[i];
				if ((c < '1' || c > '9') ? true : false)
				{
					return false;
				}
				for (; i < value.Length; i++)
				{
					c = value[i];
					if (c < '0' || c > '9')
					{
						break;
					}
				}
			}
			if (i < value.Length && value[i] == '.')
			{
				int num = ++i;
				for (; i < value.Length; i++)
				{
					char c = value[i];
					if (c < '0' || c > '9')
					{
						break;
					}
				}
				if (i == num)
				{
					return false;
				}
			}
			bool flag = i < value.Length;
			if (flag)
			{
				char c = value[i];
				bool flag2 = ((c == 'E' || c == 'e') ? true : false);
				flag = flag2;
			}
			if (flag)
			{
				i++;
				flag = i < value.Length;
				if (flag)
				{
					char c = value[i];
					bool flag2 = ((c == '+' || c == '-') ? true : false);
					flag = flag2;
				}
				if (flag)
				{
					i++;
				}
				int num2 = i;
				for (; i < value.Length; i++)
				{
					char c = value[i];
					if (c < '0' || c > '9')
					{
						break;
					}
				}
				if (i == num2)
				{
					return false;
				}
			}
			return i == value.Length;
		}
	}
	internal static class ArsenalNetworkSession
	{
		private sealed record Peer(Player Player, string Nonce);

		private sealed record ReaderScope(Player Player, NetworkConnection Sender, ReaderScope? Previous);

		private const string Key = "nacho.endgamearsenal.protocol1";

		[ThreadStatic]
		private static ReaderScope? reader;

		private static readonly Dictionary<int, Peer> peers = new Dictionary<int, Peer>();

		private static readonly Dictionary<int, (IntPtr Player, long Seen, bool Warned)> waitingPeers = new Dictionary<int, (IntPtr, long, bool)>();

		private static ArsenalSettings? settings;

		private static WeaponShopSettings[] snapshot = Array.Empty<WeaponShopSettings>();

		private static string clientNonce = "";

		private static string hostNonce = "";

		private static string fingerprint = "";

		private static IntPtr localPointer;

		private static int revision;

		private static int acceptedRevision = -1;

		private static int failures;

		private static bool running;

		private static bool server;

		private static bool received;

		private static bool applied;

		private static bool warned;

		private static bool allPeersReady = true;

		private static long nextTick;

		private static long nextHello;

		private static long started;

		public static bool CanPurchase
		{
			get
			{
				Player local = Player.Local;
				if ((Object)(object)local == (Object)null || !((NetworkBehaviour)local).IsSpawned)
				{
					return false;
				}
				if (!((NetworkBehaviour)local).IsServer)
				{
					if (running && !server && received && applied)
					{
						return ((Il2CppObjectBase)local).Pointer == localPointer;
					}
					return false;
				}
				return allPeersReady;
			}
		}

		public static string UnavailableReason => "Waiting for matching Endgame Arsenal on every player and the host's shop settings.";

		public static void Initialize(Harmony harmony, ArsenalSettings value)
		{
			settings = value;
			fingerprint = typeof(ArsenalNetworkSession).Assembly.ManifestModule.ModuleVersionId.ToString("N");
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "RpcReader___Server_SendValue_3589193952", (Type[])null, (Type[])null), Patch("BeforeReader"), (HarmonyMethod)null, (HarmonyMethod)null, Patch("AfterReader"), (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "RpcLogic___SendValue_3589193952", (Type[])null, (Type[])null), Patch("BeforeSendValue"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "RpcLogic___ReceiveValue_3895153758", (Type[])null, (Type[])null), Patch("BeforeReceiveValue"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "ReceiveValue", new Type[2]
			{
				typeof(string),
				typeof(string)
			}, (Type[])null), Patch("BeforeDirectReceiveValue"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static HarmonyMethod Patch(string name)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			return new HarmonyMethod(typeof(ArsenalNetworkSession), name, (Type[])null);
		}

		private static void BeforeReader(Player __instance, NetworkConnection __2, out ReaderScope __state)
		{
			__state = new ReaderScope(__instance, __2, reader);
			reader = __state;
		}

		private static Exception? AfterReader(Exception? __exception, ReaderScope? __state)
		{
			if (__state != null && (object)reader == __state)
			{
				reader = __state.Previous;
			}
			return __exception;
		}

		private static bool BeforeSendValue(Player __instance, string __0, string __1)
		{
			if (__0 != "nacho.endgamearsenal.protocol1")
			{
				return true;
			}
			try
			{
				if (!running || !server || (Object)(object)__instance == (Object)null || !((NetworkBehaviour)__instance).IsServer || !AuthenticatedSender(__instance) || !__instance.PlayerLoaded || !ArsenalNetworkProtocol.TryDecode(__1, out ArsenalNetMessage message) || message.Kind != ArsenalNetKind.Hello)
				{
					return false;
				}
				if (message.Fingerprint != fingerprint)
				{
					return false;
				}
				NetworkConnection owner = ((NetworkBehaviour)__instance).Owner;
				if (owner == (NetworkConnection)null || owner.ClientId < 0)
				{
					return false;
				}
				Peer value;
				bool num = !peers.TryGetValue(owner.ClientId, out value) || (Object)(object)value.Player == (Object)null || ((Il2CppObjectBase)value.Player).Pointer != ((Il2CppObjectBase)__instance).Pointer || value.Nonce != message.ClientNonce;
				peers[owner.ClientId] = new Peer(__instance, message.ClientNonce);
				Reply(__instance, message.ClientNonce);
				if (num)
				{
					AvatarWeapons.ReplayHeldWeapons();
				}
			}
			catch (Exception ex)
			{
				Report(ex);
			}
			return false;
		}

		private static bool AuthenticatedSender(Player player)
		{
			if (!((NetworkBehaviour)player).IsSpawned || ((NetworkBehaviour)player).Owner == (NetworkConnection)null)
			{
				return false;
			}
			if (reader != null)
			{
				if (((Il2CppObjectBase)reader.Player).Pointer == ((Il2CppObjectBase)player).Pointer && reader.Sender != (NetworkConnection)null && reader.Sender.ClientId >= 0 && ((Il2CppObjectBase)reader.Sender).Pointer == ((Il2CppObjectBase)((NetworkBehaviour)player).Owner).Pointer)
				{
					return reader.Sender.ClientId == ((NetworkBehaviour)player).Owner.ClientId;
				}
				return false;
			}
			if (((NetworkBehaviour)player).IsServer && player.IsLocalPlayer)
			{
				Player local = Player.Local;
				return ((local != null) ? new IntPtr?(((Il2CppObjectBase)local).Pointer) : ((IntPtr?)null)) == ((Il2CppObjectBase)player).Pointer;
			}
			return false;
		}

		private static bool BeforeReceiveValue(Player __instance, string __1, string __2)
		{
			if (__1 != "nacho.endgamearsenal.protocol1")
			{
				return true;
			}
			try
			{
				if (!running || server || settings == null || (Object)(object)__instance == (Object)null || !__instance.IsLocalPlayer || ((Il2CppObjectBase)__instance).Pointer != localPointer || !ArsenalNetworkProtocol.TryDecode(__2, out ArsenalNetMessage message) || message.Kind != ArsenalNetKind.Snapshot || message.ClientNonce != clientNonce || message.Fingerprint != fingerprint)
				{
					return false;
				}
				if (received && hostNonce != message.HostNonce)
				{
					return false;
				}
				if (hostNonce == message.HostNonce && message.Revision < acceptedRevision)
				{
					return false;
				}
				if (hostNonce == message.HostNonce && message.Revision == acceptedRevision && received)
				{
					return false;
				}
				bool num = !received;
				hostNonce = message.HostNonce;
				acceptedRevision = message.Revision;
				received = true;
				if (settings.SetHostSettings(message.Settings))
				{
					applied = false;
				}
				if (num)
				{
					AvatarWeapons.ReplayLocalWeapon();
				}
			}
			catch (Exception ex)
			{
				Report(ex);
			}
			return false;
		}

		private static bool BeforeDirectReceiveValue(Player __instance, string __0, string __1)
		{
			return BeforeReceiveValue(__instance, __0, __1);
		}

		public static void SettingsApplied(bool success)
		{
			if (running && !server && received && applied != success)
			{
				applied = success;
				settings?.RequestApply();
			}
		}

		public static void Update()
		{
			long tickCount = Environment.TickCount64;
			if (tickCount < nextTick)
			{
				return;
			}
			nextTick = tickCount + 500;
			try
			{
				LoadManager instance = Singleton<LoadManager>.Instance;
				if ((Object)(object)instance == (Object)null || !instance.IsGameLoaded || instance.IsLoading)
				{
					if (running && ((Object)(object)instance == (Object)null || (!instance.IsGameLoaded && !instance.IsLoading)))
					{
						Reset();
					}
					return;
				}
				Player local = Player.Local;
				if ((Object)(object)local == (Object)null || !((NetworkBehaviour)local).IsSpawned || settings == null)
				{
					return;
				}
				if (!running || ((Il2CppObjectBase)local).Pointer != localPointer || ((NetworkBehaviour)local).IsServer != server)
				{
					Start(local, tickCount);
				}
				if (!server)
				{
					if (tickCount >= nextHello)
					{
						nextHello = tickCount + (received ? 15000 : 2000);
						local.SendValue("nacho.endgamearsenal.protocol1", ArsenalNetworkProtocol.Encode(new ArsenalNetMessage(ArsenalNetKind.Hello, clientNonce, fingerprint)), true);
					}
					if (!received && !warned && tickCount - started >= 15000)
					{
						warned = true;
						MelonLogger.Warning(UnavailableReason);
					}
					return;
				}
				WeaponShopSettings[] array = settings.LocalSnapshot();
				bool flag = !((ReadOnlySpan<WeaponShopSettings>)snapshot).SequenceEqual((ReadOnlySpan<WeaponShopSettings>)array);
				if (flag)
				{
					snapshot = array;
					revision++;
				}
				RefreshPeerReadiness(tickCount);
				int[] array2 = peers.Keys.ToArray();
				foreach (int num in array2)
				{
					Peer peer = peers[num];
					Player player = peer.Player;
					if ((Object)(object)player == (Object)null || !((NetworkBehaviour)player).IsSpawned || ((NetworkBehaviour)player).Owner == (NetworkConnection)null || ((NetworkBehaviour)player).Owner.ClientId != num)
					{
						peers.Remove(num);
					}
					else if (flag)
					{
						Reply(player, peer.Nonce);
					}
				}
			}
			catch (Exception ex)
			{
				Report(ex);
			}
		}

		private static void RefreshPeerReadiness(long now)
		{
			bool flag = allPeersReady;
			allPeersReady = true;
			HashSet<int> hashSet = new HashSet<int>();
			if (Player.PlayerList != null)
			{
				Enumerator<Player> enumerator = Player.PlayerList.GetEnumerator();
				while (enumerator.MoveNext())
				{
					Player current = enumerator.Current;
					if ((Object)(object)current == (Object)null || current.IsLocalPlayer || !((NetworkBehaviour)current).IsSpawned || ((NetworkBehaviour)current).Owner == (NetworkConnection)null)
					{
						continue;
					}
					int clientId = ((NetworkBehaviour)current).Owner.ClientId;
					if (clientId < 0)
					{
						continue;
					}
					hashSet.Add(clientId);
					if (peers.TryGetValue(clientId, out Peer value) && (Object)(object)value.Player != (Object)null && ((Il2CppObjectBase)value.Player).Pointer == ((Il2CppObjectBase)current).Pointer)
					{
						waitingPeers.Remove(clientId);
						continue;
					}
					allPeersReady = false;
					if (!waitingPeers.TryGetValue(clientId, out (IntPtr, long, bool) value2) || value2.Item1 != ((Il2CppObjectBase)current).Pointer)
					{
						waitingPeers[clientId] = (((Il2CppObjectBase)current).Pointer, now, false);
					}
					else if (!value2.Item3 && current.PlayerLoaded && now - value2.Item2 >= 15000)
					{
						waitingPeers[clientId] = (value2.Item1, value2.Item2, true);
						MelonLogger.Warning("A co-op player has not confirmed matching Endgame Arsenal. Install the same build on every player before using its guns or ammo.");
					}
				}
			}
			int[] array = waitingPeers.Keys.ToArray();
			foreach (int num in array)
			{
				if (!hashSet.Contains(num))
				{
					waitingPeers.Remove(num);
				}
			}
			if (flag != allPeersReady)
			{
				settings?.RequestApply();
			}
		}

		private static void Start(Player local, long now)
		{
			Reset();
			localPointer = ((Il2CppObjectBase)local).Pointer;
			server = ((NetworkBehaviour)local).IsServer;
			clientNonce = Guid.NewGuid().ToString("N");
			hostNonce = (server ? Guid.NewGuid().ToString("N") : "");
			snapshot = settings.LocalSnapshot();
			revision = 1;
			started = now;
			running = true;
		}

		private static void Reply(Player player, string nonce)
		{
			player.ReceiveValue(((NetworkBehaviour)player).Owner, "nacho.endgamearsenal.protocol1", ArsenalNetworkProtocol.Encode(new ArsenalNetMessage(ArsenalNetKind.Snapshot, nonce, fingerprint, hostNonce, revision, snapshot)));
		}

		public static void Reset()
		{
			running = (received = (applied = (warned = false)));
			allPeersReady = true;
			server = false;
			localPointer = IntPtr.Zero;
			clientNonce = (hostNonce = "");
			snapshot = Array.Empty<WeaponShopSettings>();
			revision = 0;
			acceptedRevision = -1;
			nextTick = (nextHello = (started = 0L));
			peers.Clear();
			waitingPeers.Clear();
			reader = null;
			settings?.SetHostSettings(null);
		}

		private static void Report(Exception ex)
		{
			if (failures++ < 5)
			{
				MelonLogger.Warning("Endgame Arsenal co-op: " + ex.GetType().Name + ": " + ex.Message);
			}
		}
	}
	internal sealed class ArsenalSettings : IDisposable
	{
		private sealed class WeaponEntries
		{
			private readonly WeaponShopSettings defaults;

			private readonly MelonPreferences_Entry<ArsenalUnlockRank> rank;

			private readonly MelonPreferences_Entry<int> tier;

			private readonly MelonPreferences_Entry<float> price;

			private readonly MelonPreferences_Entry<float> ammoPrice;

			internal WeaponShopSettings Value { get; private set; }

			internal WeaponEntries(ArsenalSettings owner, string prefix, WeaponSpec spec)
			{
				defaults = Defaults(spec);
				string text = spec.Name + ": ";
				rank = owner.Add(prefix + "UnlockRank", (ArsenalUnlockRank)spec.Rank, text + "unlock rank", "Street Rat through Kingpin. Applies to this gun and its matching ammunition at Stan's shop.");
				tier = owner.Add(prefix + "UnlockTier", spec.Tier, text + "unlock tier", "1–5 (I–V) within the chosen rank. Applies to this gun and its matching ammunition.");
				price = owner.Add(prefix + "Price", spec.Price, text + "gun price", "$0–$1,000,000 per gun at Stan's shop. Zero makes the gun free.");
				ammoPrice = owner.Add(prefix + "AmmoPrice", spec.AmmoPrice, text + "ammunition price", $"$0–$100,000 per {spec.Capacity}-round pack at Stan's shop. Zero makes ammunition free.");
			}

			internal void Refresh()
			{
				WeaponShopSettings value = ArsenalSettingsPolicy.Normalize(rank.Value, tier.Value, price.Value, ammoPrice.Value, defaults);
				if (rank.Value != (ArsenalUnlockRank)value.Rank)
				{
					rank.Value = (ArsenalUnlockRank)value.Rank;
				}
				if (tier.Value != value.Tier)
				{
					tier.Value = value.Tier;
				}
				if (price.Value != value.Price)
				{
					price.Value = value.Price;
				}
				if (ammoPrice.Value != value.AmmoPrice)
				{
					ammoPrice.Value = value.AmmoPrice;
				}
				Value = value;
			}
		}

		private readonly MelonPreferences_Category category;

		private readonly Action changed;

		private readonly List<Action> unsubscribe = new List<Action>(12);

		private readonly WeaponEntries automaticPistol;

		private readonly WeaponEntries m4;

		private readonly WeaponEntries sniper;

		private bool refreshing = true;

		private bool disposed;

		private WeaponShopSettings[]? hostSettings;

		internal ArsenalSettings(Action changed)
		{
			this.changed = changed ?? throw new ArgumentNullException("changed");
			category = MelonPreferences.CreateCategory("ScheduleIEndgameArsenal", "Endgame Arsenal");
			automaticPistol = new WeaponEntries(this, "AutomaticPistol", FindDefaults("eg_auto_pistol"));
			m4 = new WeaponEntries(this, "M4", FindDefaults("eg_m4"));
			sniper = new WeaponEntries(this, "Sniper", FindDefaults("eg_sniper"));
			Refresh();
			refreshing = false;
			category.SaveToFile(false);
		}

		internal WeaponShopSettings For(WeaponSpec spec)
		{
			if (hostSettings != null)
			{
				for (int i = 0; i < WeaponCatalog.All.Length; i++)
				{
					if (WeaponCatalog.All[i].Id == spec.Id)
					{
						return hostSettings[i];
					}
				}
			}
			return LocalFor(spec);
		}

		internal WeaponShopSettings[] LocalSnapshot()
		{
			return WeaponCatalog.All.Select(LocalFor).ToArray();
		}

		internal void RequestApply()
		{
			changed();
		}

		internal bool SetHostSettings(WeaponShopSettings[]? values)
		{
			if (values != null && values.Length != WeaponCatalog.All.Length)
			{
				throw new ArgumentException("A host snapshot must contain all three guns.", "values");
			}
			if ((hostSettings == null && values == null) || (hostSettings != null && values != null && ((ReadOnlySpan<WeaponShopSettings>)hostSettings).SequenceEqual((ReadOnlySpan<WeaponShopSettings>)values)))
			{
				return false;
			}
			hostSettings = values?.ToArray();
			changed();
			return true;
		}

		private WeaponShopSettings LocalFor(WeaponSpec spec)
		{
			return spec.Id switch
			{
				"eg_auto_pistol" => automaticPistol.Value, 
				"eg_m4" => m4.Value, 
				"eg_sniper" => sniper.Value, 
				_ => ArsenalSettingsPolicy.Normalize((ArsenalUnlockRank)spec.Rank, spec.Tier, spec.Price, spec.AmmoPrice, Defaults(spec)), 
			};
		}

		internal void Save()
		{
			if (!disposed)
			{
				category.SaveToFile(false);
			}
		}

		public void Dispose()
		{
			if (disposed)
			{
				return;
			}
			disposed = true;
			foreach (Action item in unsubscribe)
			{
				item();
			}
			unsubscribe.Clear();
		}

		private void EntryChanged()
		{
			if (!refreshing && !disposed)
			{
				refreshing = true;
				try
				{
					Refresh();
				}
				finally
				{
					refreshing = false;
				}
				category.SaveToFile(false);
				changed();
			}
		}

		private void Refresh()
		{
			automaticPistol.Refresh();
			m4.Refresh();
			sniper.Refresh();
		}

		private MelonPreferences_Entry<T> Add<T>(string key, T defaultValue, string title, string description)
		{
			MelonPreferences_Entry<T> entry = category.CreateEntry<T>(key, defaultValue, title, description, false, false, (ValueValidator)null, (string)null);
			LemonAction<T, T> handler = delegate
			{
				EntryChanged();
			};
			((MelonEventBase<LemonAction<T, T>>)(object)entry.OnEntryValueChanged).Subscribe(handler, 0, false);
			unsubscribe.Add(delegate
			{
				((MelonEventBase<LemonAction<T, T>>)(object)entry.OnEntryValueChanged).Unsubscribe(handler);
			});
			return entry;
		}

		private static WeaponSpec FindDefaults(string id)
		{
			return WeaponCatalog.Find(id) ?? throw new InvalidOperationException("Missing weapon settings defaults: " + id);
		}

		private static WeaponShopSettings Defaults(WeaponSpec spec)
		{
			return new WeaponShopSettings(spec.Rank, spec.Tier, spec.Price, spec.AmmoPrice);
		}
	}
	public enum ArsenalUnlockRank
	{
		Street_Rat,
		Hoodlum,
		Peddler,
		Hustler,
		Bagman,
		Enforcer,
		Shot_Caller,
		Block_Boss,
		Underlord,
		Baron,
		Kingpin
	}
	internal readonly record struct WeaponShopSettings(int Rank, int Tier, float Price, float AmmoPrice);
	internal static class ArsenalSettingsPolicy
	{
		internal const float MaximumWeaponPrice = 1000000f;

		internal const float MaximumAmmoPrice = 100000f;

		internal static WeaponShopSettings Normalize(ArsenalUnlockRank rank, int tier, float price, float ammoPrice, WeaponShopSettings defaults)
		{
			return new WeaponShopSettings(Math.Clamp((int)rank, 0, 10), Math.Clamp(tier, 1, 5), Price(price, defaults.Price, 1000000f), Price(ammoPrice, defaults.AmmoPrice, 100000f));
		}

		private static float Price(float value, float fallback, float maximum)
		{
			return Math.Clamp(float.IsFinite(value) ? value : fallback, 0f, maximum);
		}
	}
	internal static class ArsenalShopGate
	{
		internal static void Initialize(Harmony harmony)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Expected O, but got Unknown
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Expected O, but got Unknown
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Expected O, but got Unknown
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Expected O, but got Unknown
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Cart), "CanCheckout", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(ArsenalShopGate), "AfterCanCheckout", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Cart), "Buy", (Type[])null, (Type[])null), new HarmonyMethod(typeof(ArsenalShopGate), "BeforeBuy", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Cart), "AddItem", (Type[])null, (Type[])null), new HarmonyMethod(typeof(ArsenalShopGate), "BeforeAddItem", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Cart), "SetItemQuantity", (Type[])null, (Type[])null), new HarmonyMethod(typeof(ArsenalShopGate), "BeforeSetQuantity", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ListingUI), "CanAddToCart", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(ArsenalShopGate), "AfterCanAdd", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ListingUI), "UpdateButtons", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(ArsenalShopGate), "AfterUpdateButtons", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static bool IsBlocked(ShopListing? listing)
		{
			if ((Object)(object)((listing != null) ? listing.Item : null) != (Object)null && RegistryAndShop.IsArsenalId(((BaseItemDefinition)listing.Item).ID))
			{
				return !ArsenalNetworkSession.CanPurchase;
			}
			return false;
		}

		private static bool HasBlockedItems(Cart cart)
		{
			if (ArsenalNetworkSession.CanPurchase || ((cart != null) ? cart.cartDictionary : null) == null)
			{
				return false;
			}
			Enumerator<ShopListing, int> enumerator = cart.cartDictionary.GetEnumerator();
			while (enumerator.MoveNext())
			{
				KeyValuePair<ShopListing, int> current = enumerator.Current;
				if (current.Value > 0 && IsBlocked(current.Key))
				{
					return true;
				}
			}
			return false;
		}

		private static void AfterCanCheckout(Cart __instance, ref string __0, ref bool __result)
		{
			if (HasBlockedItems(__instance))
			{
				__0 = ArsenalNetworkSession.UnavailableReason;
				__result = false;
			}
		}

		private static bool BeforeBuy(Cart __instance)
		{
			if (!HasBlockedItems(__instance))
			{
				return true;
			}
			__instance.UpdateProblem();
			return false;
		}

		private static bool BeforeAddItem(ShopListing __0, int __1)
		{
			if (__1 > 0)
			{
				return !IsBlocked(__0);
			}
			return true;
		}

		private static bool BeforeSetQuantity(Cart __instance, ShopListing __0, int __1)
		{
			if (IsBlocked(__0))
			{
				return __1 <= __instance.GetCartCount(__0);
			}
			return true;
		}

		private static void AfterCanAdd(ListingUI __instance, ref bool __result)
		{
			if (IsBlocked((__instance != null) ? __instance.Listing : null))
			{
				__result = false;
			}
		}

		private static void AfterUpdateButtons(ListingUI __instance)
		{
			if (IsBlocked((__instance != null) ? __instance.Listing : null))
			{
				if ((Object)(object)__instance.BuyButton != (Object)null)
				{
					((Selectable)__instance.BuyButton).interactable = false;
				}
				if ((Object)(object)__instance.AddItemButton != (Object)null)
				{
					((Selectable)__instance.AddItemButton).interactable = false;
				}
			}
		}
	}
	internal static class AvatarWeapons
	{
		public const string PathPrefix = "EndgameArsenal/";

		internal static void ReplayHeldWeapons()
		{
			Player local = Player.Local;
			if ((Object)(object)local == (Object)null || !((NetworkBehaviour)local).IsServer || !((NetworkBehaviour)local).IsSpawned || Player.PlayerList == null)
			{
				return;
			}
			Enumerator<Player> enumerator = Player.PlayerList.GetEnumerator();
			while (enumerator.MoveNext())
			{
				Player current = enumerator.Current;
				if (!((Object)(object)current == (Object)null) && ((NetworkBehaviour)current).IsServer && ((NetworkBehaviour)current).IsSpawned)
				{
					Avatar avatar = current.Avatar;
					object obj;
					if (avatar == null)
					{
						obj = null;
					}
					else
					{
						AvatarEquippable currentEquippable = avatar.CurrentEquippable;
						obj = ((currentEquippable != null) ? currentEquippable.AssetPath : null);
					}
					string text = (string)obj;
					if (!string.IsNullOrEmpty(text) && text.StartsWith("EndgameArsenal/", StringComparison.Ordinal) && !(WeaponCatalog.Find(text.Substring("EndgameArsenal/".Length)) == null))
					{
						current.SetEquippable_Networked(text);
					}
				}
			}
		}

		internal static void ReplayLocalWeapon()
		{
			Player local = Player.Local;
			if (!((Object)(object)local == (Object)null) && ((NetworkBehaviour)local).IsOwner && ((NetworkBehaviour)local).IsSpawned)
			{
				PlayerInventory instance = PlayerSingleton<PlayerInventory>.Instance;
				object weapon;
				if (instance == null)
				{
					weapon = null;
				}
				else
				{
					Equippable equippable = instance.Equippable;
					weapon = ((equippable != null) ? ((Il2CppObjectBase)equippable).TryCast<Equippable_RangedWeapon>() : null);
				}
				WeaponSpec spec = WeaponBehavior.GetSpec((Equippable_RangedWeapon?)weapon);
				if (!(spec == null))
				{
					local.SendEquippable_Networked("EndgameArsenal/" + spec.Id);
					RemoteWeaponAnimations.RefreshLocalReload();
				}
			}
		}

		public static void Initialize(Harmony harmony)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_0049: Expected O, but got Unknown
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Expected O, but got Unknown
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Avatar), "SetEquippable", (Type[])null, (Type[])null), new HarmonyMethod(typeof(AvatarWeapons), "BeforeSet", (Type[])null), new HarmonyMethod(typeof(AvatarWeapons), "AfterSet", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Player), "OnDestroy", (Type[])null, (Type[])null), new HarmonyMethod(typeof(AvatarWeapons), "BeforePlayerDestroy", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static void BeforePlayerDestroy(Player __instance)
		{
			try
			{
				Avatar avatar = __instance.Avatar;
				RemoteWeaponAnimations.ForgetAvatar(avatar);
				HandPoses.ClearAvatar(avatar);
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("Co-op held weapon cleanup could not finish: " + ex.Message);
			}
		}

		private static void BeforeSet(Avatar __instance, ref string assetPath, out WeaponSpec? __state)
		{
			RemoteWeaponAnimations.Clear(__instance);
			HandPoses.ClearAvatar(__instance);
			__state = null;
			if (string.IsNullOrEmpty(assetPath) || !assetPath.StartsWith("EndgameArsenal/", StringComparison.Ordinal))
			{
				return;
			}
			WeaponSpec weaponSpec = WeaponCatalog.Find(assetPath.Substring("EndgameArsenal/".Length));
			if (weaponSpec == null)
			{
				return;
			}
			RegistryAndShop.TryRegister();
			if (string.IsNullOrEmpty(RegistryAndShop.BaseAvatarAssetPath))
			{
				return;
			}
			__state = weaponSpec;
			object obj2;
			if (HandPoses.IsLongGun(weaponSpec))
			{
				GameObject obj = Resources.Load<GameObject>("Avatar/Equippables/PumpShotgun");
				if ((Object)(object)((obj != null) ? obj.GetComponent<AvatarEquippable>() : null) != (Object)null)
				{
					obj2 = "Avatar/Equippables/PumpShotgun";
					goto IL_0092;
				}
			}
			obj2 = RegistryAndShop.BaseAvatarAssetPath;
			goto IL_0092;
			IL_0092:
			assetPath = (string)obj2;
		}

		private static void AfterSet(AvatarEquippable? __result, WeaponSpec? __state)
		{
			if (__state == null || (Object)(object)__result == (Object)null)
			{
				return;
			}
			try
			{
				ApplyAppearance(__result, __state);
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("Unable to create held " + __state.Name + ": " + ex.Message);
			}
		}

		public static void ApplyAppearance(AvatarEquippable avatar, WeaponSpec spec)
		{
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: 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)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			string text = spec.Id + "_OriginalModel";
			if ((Object)(object)((Component)avatar).transform.Find(text) == (Object)null)
			{
				foreach (Renderer componentsInChild in ((Component)avatar).GetComponentsInChildren<Renderer>(true))
				{
					componentsInChild.enabled = false;
				}
				AvatarRangedWeapon val = ((Il2CppObjectBase)avatar).TryCast<AvatarRangedWeapon>();
				GameObject val2 = WeaponVisuals.Build(spec.Id, ((Component)avatar).transform);
				if ((Object)(object)val != (Object)null && (Object)(object)val.MuzzlePoint != (Object)null)
				{
					val2.transform.rotation = val.MuzzlePoint.rotation;
				}
				if ((Object)(object)avatar.AlignmentPoint != (Object)null)
				{
					val2.transform.position = avatar.AlignmentPoint.position;
				}
			}
			avatar.AssetPath = "EndgameArsenal/" + spec.Id;
			AvatarRangedWeapon val3 = ((Il2CppObjectBase)avatar).TryCast<AvatarRangedWeapon>();
			if ((Object)(object)val3 == (Object)null)
			{
				return;
			}
			val3.MagazineSize = spec.Capacity;
			val3.Damage = spec.Damage;
			val3.MaxFireRate = 1f / spec.FireInterval;
			val3.ImpactForce = Math.Max(100f, spec.Damage * 2.5f);
			Transform val4 = ((Component)avatar).transform.Find(text);
			if (!((Object)(object)val4 == (Object)null))
			{
				Transform val5 = val4.Find("ArsenalMuzzle");
				if ((Object)(object)val5 == (Object)null)
				{
					val5 = new GameObject("ArsenalMuzzle").transform;
					val5.SetParent(val4, false);
					Transform val6 = val5;
					string id = spec.Id;
					Vector3 localPosition = ((id == "eg_auto_pistol") ? new Vector3(0f, 0.101f, 0.2f) : ((!(id == "eg_m4")) ? new Vector3(0f, 0.097f, 0.713f) : new Vector3(0f, 0.095f, 0.476f)));
					val6.localPosition = localPosition;
				}
				val3.MuzzlePoint = val5;
				HandPoses.BindAvatar(avatar, spec, val4);
				RemoteWeaponAnimations.Bind(val3, spec, val4);
			}
		}
	}
	internal readonly record struct BallisticsTuning(float BloomPerShot, float HipBloomDegrees, float AimBloomDegrees, float PitchPerShot, float YawPerShot, float AimRecoilScale, float MaxPitch, float MaxYaw, float RecoveryDelay, float BloomRecoveryPerSecond, float RecoilRecoveryPerSecond)
	{
		public static BallisticsTuning For(string id)
		{
			return id switch
			{
				"eg_auto_pistol" => new BallisticsTuning(0.14f, 3.4f, 1.7f, 0.72f, 0.25f, 0.65f, 7f, 1.6f, 0.18f, 1.3f, 6f), 
				"eg_m4" => new BallisticsTuning(0.11f, 2.6f, 1.15f, 0.48f, 0.15f, 0.65f, 5.5f, 1.1f, 0.2f, 1.2f, 5f), 
				"eg_sniper" => new BallisticsTuning(0.3f, 2f, 0.12f, 1.8f, 0.22f, 0.4f, 4f, 0.8f, 0.3f, 1.4f, 5.5f), 
				_ => default(BallisticsTuning), 
			};
		}
	}
	internal readonly record struct BallisticsSample(float PitchDegrees, float YawDegrees, float BloomDegrees);
	internal sealed class BallisticsState
	{
		private double lastUpdate = double.NaN;

		private double lastShot = double.NegativeInfinity;

		public float Bloom { get; private set; }

		public float Pitch { get; private set; }

		public float Yaw { get; private set; }

		public void Advance(double now, BallisticsTuning tuning)
		{
			if (double.IsFinite(now))
			{
				if (double.IsNaN(lastUpdate) || now < lastUpdate)
				{
					float num = (Yaw = 0f);
					float bloom = (Pitch = num);
					Bloom = bloom;
					lastShot = double.NegativeInfinity;
					lastUpdate = now;
				}
				else
				{
					float num4 = (float)Math.Max(0.0, now - Math.Max(lastUpdate, lastShot + (double)tuning.RecoveryDelay));
					Bloom = TowardZero(Bloom, num4 * tuning.BloomRecoveryPerSecond);
					Pitch = TowardZero(Pitch, num4 * tuning.RecoilRecoveryPerSecond);
					Yaw = TowardZero(Yaw, num4 * tuning.RecoilRecoveryPerSecond);
					lastUpdate = now;
				}
			}
		}

		public void RecordShot(double now, float aim, float lateralSample, BallisticsTuning tuning)
		{
			Advance(now, tuning);
			if (double.IsFinite(now))
			{
				float num = 1f + (tuning.AimRecoilScale - 1f) * Unit(aim);
				Bloom = Math.Clamp(Bloom + tuning.BloomPerShot, 0f, 1f);
				Pitch = Math.Clamp(Pitch + tuning.PitchPerShot * num, 0f, tuning.MaxPitch);
				Yaw = Math.Clamp(Yaw + Math.Clamp(Finite(lateralSample), -1f, 1f) * tuning.YawPerShot * num, 0f - tuning.MaxYaw, tuning.MaxYaw);
				lastShot = now;
			}
		}

		public BallisticsSample Sample(float aim, BallisticsTuning tuning)
		{
			float num = tuning.HipBloomDegrees + (tuning.AimBloomDegrees - tuning.HipBloomDegrees) * Unit(aim);
			return new BallisticsSample(Pitch, Yaw, Bloom * num);
		}

		internal static float Unit(float value)
		{
			return Math.Clamp(Finite(value), 0f, 1f);
		}

		private static float Finite(float value)
		{
			if (!float.IsFinite(value))
			{
				return 0f;
			}
			return value;
		}

		private static float TowardZero(float value, float amount)
		{
			return (float)Math.Sign(value) * Math.Max(0f, Math.Abs(value) - Math.Max(0f, amount));
		}
	}
	internal static class BallisticsMath
	{
		private const float ToRadians = (float)Math.PI / 180f;

		public static Vector3 OffsetDirection(Vector3 nativeDirection, Vector3 cameraUp, BallisticsSample sample, float areaSample, float azimuthSample)
		{
			Vector3 vector = NormalizeOr(nativeDirection, Vector3.UnitZ);
			Vector3 vector2 = Right(vector, cameraUp);
			Vector3 vector3 = Vector3.Cross(vector, vector2);
			float x = sample.PitchDegrees * ((float)Math.PI / 180f);
			float x2 = sample.YawDegrees * ((float)Math.PI / 180f);
			Vector3 vector4 = vector * (MathF.Cos(x) * MathF.Cos(x2)) + vector2 * (MathF.Cos(x) * MathF.Sin(x2)) + vector3 * MathF.Sin(x);
			float x3 = Math.Clamp(sample.BloomDegrees, 0f, 45f) * ((float)Math.PI / 180f);
			float num = 1f - BallisticsState.Unit(areaSample) * (1f - MathF.Cos(x3));
			float num2 = MathF.Sqrt(Math.Max(0f, 1f - num * num));
			float x4 = BallisticsState.Unit(azimuthSample) * ((float)Math.PI * 2f);
			Vector3 vector5 = Right(vector4, cameraUp);
			Vector3 vector6 = Vector3.Cross(vector4, vector5);
			return NormalizeOr(vector4 * num + vector5 * (num2 * MathF.Cos(x4)) + vector6 * (num2 * MathF.Sin(x4)), vector);
		}

		private static Vector3 Right(Vector3 forward, Vector3 up)
		{
			Vector3 vector = Vector3.Cross(up, forward);
			if (!Valid(vector) || vector.LengthSquared() < 1E-06f)
			{
				vector = Vector3.Cross((Math.Abs(forward.Y) < 0.99f) ? Vector3.UnitY : Vector3.UnitX, forward);
			}
			return Vector3.Normalize(vector);
		}

		private static Vector3 NormalizeOr(Vector3 vector, Vector3 fallback)
		{
			if (!Valid(vector) || !(vector.LengthSquared() >= 1E-06f))
			{
				return fallback;
			}
			return Vector3.Normalize(vector);
		}

		private static bool Valid(Vector3 vector)
		{
			if (float.IsFinite(vector.X) && float.IsFinite(vector.Y))
			{
				return float.IsFinite(vector.Z);
			}
			return false;
		}
	}
	internal static class HandPoses
	{
		private sealed class SupportState
		{
			private readonly Avatar avatar;

			private readonly Transform model;

			private readonly AvatarEquippable? equippable;

			private readonly IKSolverLimb solver;

			private readonly Transform palm;

			private readonly Transform wristTarget;

			private readonly Transform wrist;

			private readonly Vector3 palmOffset;

			private readonly Transform? oldTarget;

			private readonly Vector3 oldPosition;

			private readonly Quaternion oldRotation;

			private readonly float oldPositionWeight;

			private readonly float oldRotationWeight;

			private bool applied;

			private float blend;

			private SupportState(Avatar avatar, Transform model, WeaponSpec spec, AvatarEquippable? equippable, IKSolverLimb solver)
			{
				//IL_0058: Unknown result type (might be due to invalid IL or missing references)
				//IL_005d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0045: Unknown result type (might be due to invalid IL or missing references)
				//IL_0062: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
				//IL_009f: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
				this.avatar = avatar;
				this.model = model;
				this.equippable = equippable;
				this.solver = solver;
				wrist = ((Point)((IKSolverTrigonometric)solver).bone3).transform;
				palmOffset = (((Object)(object)avatar.LeftHandAlignmentPoint != (Object)null) ? wrist.InverseTransformPoint(avatar.LeftHandAlignmentPoint.position) : Vector3.zero);
				palm = Child(model, "ArsenalSupportPalm");
				palm.localPosition = ((spec.Id == "eg_m4") ? new Vector3(0f, 0.07f, 0.255f) : new Vector3(0f, 0.057f, 0.292f));
				wristTarget = Child(model, "ArsenalSupportWrist");
				oldTarget = ((IKSolverTrigonometric)solver).target;
				oldPosition = ((IKSolver)solver).IKPosition;
				oldRotation = ((IKSolverTrigonometric)solver).IKRotation;
				oldPositionWeight = ((IKSolver)solver).IKPositionWeight;
				oldRotationWeight = ((IKSolverTrigonometric)solver).IKRotationWeight;
			}

			public static SupportState? TryCreate(Avatar avatar, Animator? animator, Transform model, WeaponSpec spec, AvatarEquippable? equippable)
			{
				//IL_0121: Unknown result type (might be due to invalid IL or missing references)
				//IL_0126: Unknown result type (might be due to invalid IL or missing references)
				//IL_012d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0134: Unknown result type (might be due to invalid IL or missing references)
				//IL_0140: Expected O, but got Unknown
				AvatarAnimation animation = avatar.Animation;
				object obj;
				if (animation == null)
				{
					obj = null;
				}
				else
				{
					AvatarIKController ikController = animation._ikController;
					obj = ((ikController != null) ? ikController.BodyIK : null);
				}
				BipedIK val = (BipedIK)obj;
				IKSolverLimb val2 = ((val != null) ? val.GetGoalIK((AvatarIKGoal)2) : null);
				object obj2;
				if (val2 == null)
				{
					obj2 = null;
				}
				else
				{
					TrigonometricBone bone = ((IKSolverTrigonometric)val2).bone1;
					obj2 = ((bone != null) ? ((Point)bone).transform : null);
				}
				if (!((Object)obj2 == (Object)null))
				{
					TrigonometricBone bone2 = ((IKSolverTrigonometric)val2).bone2;
					if (!((Object)(object)((bone2 != null) ? ((Point)bone2).transform : null) == (Object)null))
					{
						TrigonometricBone bone3 = ((IKSolverTrigonometric)val2).bone3;
						if (!((Object)(object)((bone3 != null) ? ((Point)bone3).transform : null) == (Object)null))
						{
							goto IL_015a;
						}
					}
				}
				BipedReferences obj3 = ((val != null) ? val.references : null);
				Transform val3 = ((obj3 != null) ? obj3.leftUpperArm : null);
				Transform val4 = ((obj3 != null) ? obj3.leftForearm : null);
				Transform val5 = ((obj3 != null) ? obj3.leftHand : null);
				if (((Object)(object)val3 == (Object)null || (Object)(object)val4 == (Object)null || (Object)(object)val5 == (Object)null) && (Object)(object)animator != (Object)null && animator.isHuman)
				{
					val3 = animator.GetBoneTransform((HumanBodyBones)13);
					val4 = animator.GetBoneTransform((HumanBodyBones)15);
					val5 = animator.GetBoneTransform((HumanBodyBones)17);
				}
				if ((Object)(object)val3 == (Object)null || (Object)(object)val4 == (Object)null || (Object)(object)val5 == (Object)null)
				{
					return null;
				}
				val2 = new IKSolverLimb
				{
					goal = (AvatarIKGoal)2,
					bendModifier = (BendModifier)0,
					bendModifierWeight = 1f
				};
				if (!((IKSolverTrigonometric)val2).SetChain(val3, val4, val5, ((Component)avatar).transform))
				{
					return null;
				}
				((IKSolverTrigonometric)val2).SetBendPlaneToCurrent();
				goto IL_015a;
				IL_015a:
				if (!((IKSolver)val2).initiated)
				{
					((IKSolver)val2).Initiate(((Component)avatar).transform);
				}
				return new SupportState(avatar, model, spec, equippable, val2);
			}

			public bool Matches(Avatar candidate, Transform candidateModel)
			{
				if ((Object)(object)avatar == (Object)(object)candidate)
				{
					return (Object)(object)model == (Object)(object)candidateModel;
				}
				return false;
			}

			public bool IsEquipped()
			{
				if ((Object)(object)avatar != (Object)null && (Object)(object)model != (Object)null && (Object)(object)equippable != (Object)null && (Object)(object)avatar.CurrentEquippable != (Object)null)
				{
					return ((Il2CppObjectBase)avatar.CurrentEquippable).Pointer == ((Il2CppObjectBase)equippable).Pointer;
				}
				return false;
			}

			public void Apply()
			{
				//IL_00df: Unknown result type (might be due to invalid IL or missing references)
				//IL_00eb: 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_010b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0132: Unknown result type (might be due to invalid IL or missing references)
				//IL_0148: Unknown result type (might be due to invalid IL or missing references)
				AvatarEquippable? obj = equippable;
				AvatarRangedWeapon val = ((obj != null) ? ((Il2CppObjectBase)obj).TryCast<AvatarRangedWeapon>() : null);
				if (!((Object)(object)avatar == (Object)null) && !((Object)(object)model == (Object)null) && !((Object)(object)wrist == (Object)null) && !avatar.Ragdolled && !avatar.IsCulled)
				{
					AvatarAnimation animation = avatar.Animation;
					if (animation == null || !animation.IsSeated)
					{
						AvatarAnimation animation2 = avatar.Animation;
						if ((animation2 == null || !animation2.StandUpAnimationPlaying) && (!((Object)(object)val != (Object)null) || (!val.isReloading && !(val.timeEquipped < val.EquipTime))))
						{
							blend = Mathf.MoveTowards(blend, 1f, Time.deltaTime * 7f);
							wristTarget.position = palm.position - wrist.TransformVector(palmOffset);
							wristTarget.rotation = wrist.rotation;
							((IKSolverTrigonometric)solver).target = wristTarget;
							((IKSolver)solver).IKPosition = wristTarget.position;
							((IKSolverTrigonometric)solver).IKRotation = wristTarget.rotation;
							((IKSolver)solver).IKPositionWeight = blend;
							((IKSolverTrigonometric)solver).IKRotationWeight = blend;
							applied = true;
							((IKSolver)solver).Update();
							return;
						}
					}
				}
				Release();
			}

			public void Release()
			{
				//IL_0054: Unknown result type (might be due to invalid IL or missing references)
				//IL_0065: Unknown result type (might be due to invalid IL or missing references)
				blend = 0f;
				if (applied && solver != null)
				{
					applied = false;
					if (!((Object)(object)((IKSolverTrigonometric)solver).target != (Object)(object)wristTarget))
					{
						((IKSolverTrigonometric)solver).target = oldTarget;
						((IKSolver)solver).IKPosition = oldPosition;
						((IKSolverTrigonometric)solver).IKRotation = oldRotation;
						((IKSolver)solver).IKPositionWeight = oldPositionWeight;
						((IKSolverTrigonometric)solver).IKRotationWeight = oldRotationWeight;
					}
				}
			}

			private static Transform Child(Transform parent, string name)
			{
				//IL_0014: Unknown result type (might be due to invalid IL or missing references)
				Transform val = parent.Find(name);
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
				val = new GameObject(name).transform;
				val.SetParent(parent, false);
				return val;
			}
		}

		private static readonly Dictionary<IntPtr, SupportState> Avatars = new Dictionary<IntPtr, SupportState>();

		private static Equippable_RangedWeapon? firstPersonWeapon;

		private static SupportState? firstPersonState;

		private static int failures;

		public static void Initialize(Harmony harmony)
		{
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Equippable_RangedWeapon), "Equip", (Type[])null, (Type[])null), (HarmonyMethod)null, Patch("AfterEquip"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Equippable_RangedWeapon), "Unequip", (Type[])null, (Type[])null), Patch("BeforeUnequip"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ViewmodelAvatar), "SetBoneTransforms", (Type[])null, (Type[])null), (HarmonyMethod)null, Patch("AfterViewmodelTransforms"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(AvatarAnimation), "LateUpdate", (Type[])null, (Type[])null), (HarmonyMethod)null, Patch("AfterAvatarAnimation"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static HarmonyMethod Patch(string name)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			return new HarmonyMethod(typeof(HandPoses), name, (Type[])null);
		}

		internal static bool IsLongGun(WeaponSpec? spec)
		{
			if (!(spec?.Id == "eg_m4"))
			{
				return spec?.Id == "eg_sniper";
			}
			return true;
		}

		private static void AfterEquip(Equippable_RangedWeapon __instance)
		{
			ClearFirstPerson();
			if (IsLongGun(WeaponBehavior.GetSpec(__instance)))
			{
				firstPersonWeapon = __instance;
			}
		}

		private static void BeforeUnequip(Equippable_RangedWeapon __instance)
		{
			if ((Object)(object)firstPersonWeapon != (Object)null && ((Il2CppObjectBase)firstPersonWeapon).Pointer == ((Il2CppObjectBase)__instance).Pointer)
			{
				ClearFirstPerson();
			}
		}

		private static void AfterViewmodelTransforms(ViewmodelAvatar __instance)
		{
			if ((Object)(object)firstPersonWeapon == (Object)null)
			{
				return;
			}
			try
			{
				Equippable_RangedWeapon val = firstPersonWeapon;
				WeaponSpec spec = WeaponBehavior.GetSpec(val);
				PlayerInventory instance = PlayerSingleton<PlayerInventory>.Instance;
				if (!IsLongGun(spec) || (Object)(object)((instance != null) ? instance.Equippable : null) == (Object)null || ((Il2CppObjectBase)instance.Equippable).Pointer != ((Il2CppObjectBase)val).Pointer)
				{
					ClearFirstPerson();
					return;
				}
				Avatar avatar = __instance.Avatar;
				Transform val2 = ((Component)val).transform.Find(spec.Id + "_OriginalModel");
				if ((Object)(object)avatar == (Object)null || (Object)(object)val2 == (Object)null)
				{
					return;
				}
				if (firstPersonState == null || !firstPersonState.Matches(avatar, val2))
				{
					firstPersonState?.Release();
					firstPersonState = SupportState.TryCreate(avatar, __instance.Animator, val2, spec, null);
				}
				if (firstPersonState != null)
				{
					if (!__instance.IsVisible || !((Behaviour)val).isActiveAndEnabled || !((Equippable_AvatarViewmodel)val).equipAnimDone || val.IsReloading || val.IsCocking)
					{
						firstPersonState.Release();
					}
					else
					{
						firstPersonState.Apply();
					}
				}
			}
			catch (Exception ex)
			{
				ClearFirstPerson();
				Warn(ex);
			}
		}

		internal static void BindAvatar(AvatarEquippable equippable, WeaponSpec spec, Transform model)
		{
			Avatar avatar = equippable.avatar;
			if (!IsLongGun(spec) || (Object)(object)((avatar != null) ? avatar.Animation : null) == (Object)null)
			{
				return;
			}
			IntPtr pointer = ((Il2CppObjectBase)avatar.Animation).Pointer;
			if (Avatars.TryGetValue(pointer, out SupportState value))
			{
				if (value.Matches(avatar, model))
				{
					return;
				}
				value.Release();
			}
			SupportState supportState = SupportState.TryCreate(avatar, avatar.Animation._animator, model, spec, equippable);
			if (supportState != null)
			{
				Avatars[pointer] = supportState;
			}
		}

		internal static void ClearAvatar(Avatar? avatar)
		{
			if (!((Object)(object)((avatar != null) ? avatar.Animation : null) == (Object)null) && Avatars.Remove(((Il2CppObjectBase)avatar.Animation).Pointer, out SupportState value))
			{
				value.Release();
			}
		}

		private static void AfterAvatarAnimation(AvatarAnimation __instance)
		{
			if (!Avatars.TryGetValue(((Il2CppObjectBase)__instance).Pointer, out SupportState value))
			{
				return;
			}
			try
			{
				if (!value.IsEquipped())
				{
					value.Release();
					Avatars.Remove(((Il2CppObjectBase)__instance).Pointer);
				}
				else
				{
					value.Apply();
				}
			}
			catch (Exception ex)
			{
				value.Release();
				Avatars.Remove(((Il2CppObjectBase)__instance).Pointer);
				Warn(ex);
			}
		}

		public static void Reset()
		{
			ClearFirstPerson();
			foreach (SupportState value in Avatars.Values)
			{
				value.Release();
			}
			Avatars.Clear();
			failures = 0;
		}

		private static void ClearFirstPerson()
		{
			firstPersonState?.Release();
			firstPersonState = null;
			firstPersonWeapon = null;
		}

		private static void Warn(Exception ex)
		{
			if (failures++ < 3)
			{
				MelonLogger.Warning("Long-gun support hand could not update: " + ex.GetType().Name + ": " + ex.Message);
			}
		}
	}
	internal static class ModelIcons
	{
		private const int Resolution = 256;

		internal static Sprite Render(string id, bool ammo)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//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)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0407: Unknown result type (might be due to invalid IL or missing references)
			//IL_0436: Unknown result type (might be due to invalid IL or missing references)
			//IL_043d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Expected O, but got Unknown
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0221: Expected O, but got Unknown
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_030d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0325: Unknown result type (might be due to invalid IL or missing references)
			//IL_033d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c3: Unknown result type (might be due to invalid IL or missing references)
			Vector3 previewDirection = RuntimePreviewGenerator.PreviewDirection;
			float padding = RuntimePreviewGenerator.Padding;
			Color backgroundColor = RuntimePreviewGenerator.BackgroundColor;
			bool orthographicMode = RuntimePreviewGenerator.OrthographicMode;
			bool useLocalBounds = RuntimePreviewGenerator.UseLocalBounds;
			float renderSupersampling = RuntimePreviewGenerator.RenderSupersampling;
			bool markTextureNonReadable = RuntimePreviewGenerator.MarkTextureNonReadable;
			Camera previewRenderCamera = RuntimePreviewGenerator.PreviewRenderCamera;
			Camera renderCamera = RuntimePreviewGenerator.renderCamera;
			Vector3 camPos = RuntimePreviewGenerator.CamPos;
			Quaternion camRot = RuntimePreviewGenerator.CamRot;
			GameObject val = null;
			GameObject val2 = null;
			GameObject val3 = null;
			Texture2D val4 = null;
			try
			{
				val = (ammo ? WeaponVisuals.BuildAmmo(id, null) : WeaponVisuals.Build(id, null));
				((Object)val).hideFlags = (HideFlags)61;
				val.transform.rotation = (ammo ? Quaternion.Euler(-28f, 0f, 0f) : Quaternion.Euler(-32f, 0f, 0f));
				val.transform.position = RuntimePreviewGenerator.PREVIEW_POSITION;
				RuntimePreviewGenerator.PreviewDirection = new Vector3(-1f, -0.18f, -0.15f);
				RuntimePreviewGenerator.Padding = 0.12f;
				RuntimePreviewGenerator.BackgroundColor = Color.clear;
				RuntimePreviewGenerator.OrthographicMode = true;
				RuntimePreviewGenerator.UseLocalBounds = false;
				RuntimePreviewGenerator.RenderSupersampling = 2f;
				RuntimePreviewGenerator.MarkTextureNonReadable = false;
				RuntimePreviewGenerator.SetLayerRecursively(val.transform);
				val3 = new GameObject("EndgameArsenal_PortraitCamera");
				((Object)val3).hideFlags = (HideFlags)61;
				Camera val5 = val3.AddComponent<Camera>();
				((Behaviour)val5).enabled = false;
				val5.orthographic = true;
				val5.aspect = 1f;
				val5.cullingMask = 1 << RuntimePreviewGenerator.PREVIEW_LAYER;
				val5.clearFlags = (CameraClearFlags)2;
				val5.backgroundColor = Color.clear;
				val5.nearClipPlane = 0.01f;
				val5.allowHDR = false;
				val5.allowMSAA = false;
				((Component)val5).transform.rotation = Quaternion.LookRotation(RuntimePreviewGenerator.PreviewDirection, Vector3.up);
				Bounds val6 = default(Bounds);
				if (!RuntimePreviewGenerator.CalculateBounds(val.transform, true, ((Component)val5).transform.rotation, ref val6))
				{
					throw new InvalidOperationException("Item portrait model has no visible render bounds.");
				}
				RuntimePreviewGenerator.CalculateCameraPosition(val5, val6, RuntimePreviewGenerator.Padding);
				RuntimePreviewGenerator.PreviewRenderCamera = val5;
				RuntimePreviewGenerator.CamPos = ((Component)val5).transform.position;
				RuntimePreviewGenerator.CamRot = ((Component)val5).transform.rotation;
				ValidateFraming(val5, val.transform);
				val2 = new GameObject("EndgameArsenal_PortraitLighting");
				((Object)val2).hideFlags = (HideFlags)61;
				AddLight(val2.transform, "Key", new Vector3(45f, -45f, 0f), 1.55f);
				AddLight(val2.transform, "Fill", new Vector3(325f, 145f, 0f), 0.65f);
				val4 = RuntimePreviewGenerator.GenerateModelPreview(val.transform, 256, 256, false, true);
				if ((Object)(object)val4 == (Object)null)
				{
					throw new InvalidOperationException("Native item portrait renderer returned no texture.");
				}
				bool flag = false;
				Il2CppStructArray<Color32> pixels = val4.GetPixels32();
				foreach (Color32 item in (Il2CppArrayBase<Color32>)(object)pixels)
				{
					if (item.a > 32)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					throw new InvalidOperationException("Native item portrait renderer returned a blank image for " + id + "; model layer and camera framing were validated.");
				}
				if (((Il2CppArrayBase<Color32>)(object)pixels)[0].a > 8 || ((Il2CppArrayBase<Color32>)(object)pixels)[((Texture)val4).width - 1].a > 8 || ((Il2CppArrayBase<Color32>)(object)pixels)[((Il2CppArrayBase<Color32>)(object)pixels).Length - 1].a > 8)
				{
					throw new InvalidOperationException("Native item portrait renderer did not preserve the transparent background.");
				}
				((Object)val4).name = id + (ammo ? "_ammo" : "_weapon") + "_ModelPortrait";
				((Object)val4).hideFlags = (HideFlags)61;
				((Texture)val4).filterMode = (FilterMode)1;
				((Texture)val4).wrapMode = (TextureWrapMode)1;
				val4.Apply(false, true);
				Sprite obj = Sprite.Create(val4, new Rect(0f, 0f, (float)((Texture)val4).width, (float)((Texture)val4).height), new Vector2(0.5f, 0.5f), 256f);
				((Object)obj).name = ((Object)val4).name + "_Sprite";
				((Object)obj).hideFlags = (HideFlags)61;
				val4 = null;
				return obj;
			}
			finally
			{
				RuntimePreviewGenerator.PreviewDirection = previewDirection;
				RuntimePreviewGenerator.Padding = padding;
				RuntimePreviewGenerator.BackgroundColor = backgroundColor;
				RuntimePreviewGenerator.OrthographicMode = orthographicMode;
				RuntimePreviewGenerator.UseLocalBounds = useLocalBounds;
				RuntimePreviewGenerator.RenderSupersampling = renderSupersampling;
				RuntimePreviewGenerator.MarkTextureNonReadable = markTextureNonReadable;
				RuntimePreviewGenerator.PreviewRenderCamera = previewRenderCamera;
				RuntimePreviewGenerator.renderCamera = renderCamera;
				RuntimePreviewGenerator.CamPos = camPos;
				RuntimePreviewGenerator.CamRot = camRot;
				if ((Object)(object)val3 != (Object)null)
				{
					val3.SetActive(false);
					Object.Destroy((Object)(object)val3);
				}
				if ((Object)(object)val2 != (Object)null)
				{
					val2.SetActive(false);
					Object.Destroy((Object)(object)val2);
				}
				if ((Object)(object)val != (Object)null)
				{
					val.SetActive(false);
					Object.Destroy((Object)(object)val);
				}
				if ((Object)(object)val4 != (Object)null)
				{
					Object.Destroy((Object)(object)val4);
				}
			}
		}

		private static void ValidateFraming(Camera camera, Transform model)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: 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_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			bool flag = false;
			foreach (Renderer componentsInChild in ((Component)model).GetComponentsInChildren<Renderer>(true))
			{
				if (componentsInChild.enabled && ((Component)componentsInChild).gameObject.activeInHierarchy)
				{
					if ((camera.cullingMask & (1 << ((Component)componentsInChild).gameObject.layer)) == 0)
					{
						throw new InvalidOperationException("Item portrait camera excludes a model renderer's layer.");
					}
					Bounds bounds = componentsInChild.bounds;
					Vector3 val = camera.WorldToViewportPoint(((Bounds)(ref bounds)).center);
					if (val.z > camera.nearClipPlane && val.x >= 0f && val.x <= 1f && val.y >= 0f && val.y <= 1f)
					{
						flag = true;
					}
				}
			}
			if (!flag)
			{
				throw new InvalidOperationException("Item portrait model is outside its calculated camera frame.");
			}
		}

		private static void AddLight(Transform parent, string name, Vector3 rotation, float intensity)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject(name);
			val.transform.SetParent(parent, false);
			val.transform.rotation = Quaternion.Euler(rotation);
			Light obj = val.AddComponent<Light>();
			obj.type = (LightType)1;
			obj.color = Color.white;
			obj.intensity = intensity;
			obj.shadows = (LightShadows)0;
			obj.cullingMask = 1 << RuntimePreviewGenerator.PREVIEW_LAYER;
		}
	}
	internal static class RegistryAndShop
	{
		private static readonly Dictionary<string, IntegerItemDefinition> definitions = new Dictionary<string, IntegerItemDefinition>(StringComparer.Ordinal);

		private static readonly HashSet<string> modelPortraits = new HashSet<string>(StringComparer.Ordinal);

		private static readonly HashSet<string> itemIds = new HashSet<string>(WeaponCatalog.All.SelectMany((WeaponSpec spec) => new string[2] { spec.Id, spec.AmmoId }), StringComparer.Ordinal);

		private static readonly Dictionary<string, (FullRank Rank, Unlockable Unlock)> ownedUnlocks = new Dictionary<string, (FullRank, Unlockable)>(StringComparer.Ordinal);

		private static ArsenalSettings? settings;

		private static IntPtr unlockManagerPointer;

		private static GameObject? prototypes;

		private static bool registering;

		private static int hookFailures;

		private static int portraitFailures;

		private static float nextPortraitAttempt;

		public static string BaseAvatarAssetPath { get; private set; } = "Avatar/Equippables/M1911";

		public static bool PortraitsComplete => modelPortraits.Count == WeaponCatalog.All.Length * 2;

		public static void BindSettings(ArsenalSettings value)
		{
			settings = value;
		}

		public static void Initialize(Harmony harmony)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Expected O, but got Unknown
			ArsenalShopGate.Initialize(harmony);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Registry), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(RegistryAndShop), "RegistryAwake", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(Registry), "_GetItem", new Type[2]
			{
				typeof(string),
				typeof(bool)
			}, (Type[])null), new HarmonyMethod(typeof(RegistryAndShop), "BeforeLookup", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static void RegistryAwake(Registry __instance)
		{
			RegisterFromHook(__instance);
		}

		private static void BeforeLookup(Registry __instance, string ID)
		{
			if (IsArsenalId(ID))
			{
				RegisterFromHook(__instance);
			}
		}

		private static void RegisterFromHook(Registry registry)
		{
			try
			{
				TryRegister(registry);
			}
			catch (Exception ex)
			{
				if (hookFailures++ < 3)
				{
					MelonLogger.Warning("Endgame Arsenal registration will retry: " + ex.GetType().Name + ": " + ex.Message);
				}
			}
		}

		public static bool TryRegister()
		{
			return TryRegister(Singleton<Registry>.Instance);
		}

		private static bool TryRegister(Registry? registry)
		{
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Expected O, but got Unknown
			if (registering || (Object)(object)registry == (Object)null || registry.ItemDictionary == null)
			{
				return false;
			}
			registering = true;
			try
			{
				if (definitions.Count == WeaponCatalog.All.Length * 2)
				{
					bool flag = true;
					foreach (IntegerItemDefinition value2 in definitions.Values)
					{
						if ((Object)(object)value2 == (Object)null || !Registry.ItemExists(((BaseItemDefinition)value2).ID))
						{
							flag = false;
							break;
						}
					}
					if (flag)
					{
						return true;
					}
				}
				IntegerItemDefinition val = Resources.Load<IntegerItemDefinition>("Weapons/M1911/M1911");
				if ((Object)(object)val == (Object)null)
				{
					return false;
				}
				Equippable equippable = ((ItemDefinition)val).Equippable;
				Equippable_RangedWeapon val2 = ((equippable != null) ? ((Il2CppObjectBase)equippable).TryCast<Equippable_RangedWeapon>() : null);
				object obj;
				if (val2 == null)
				{
					obj = null;
				}
				else
				{
					StorableItemDefinition magazine = val2.Magazine;
					obj = ((magazine != null) ? ((Il2CppObjectBase)magazine).TryCast<IntegerItemDefinition>() : null);
				}
				IntegerItemDefinition val3 = (IntegerItemDefinition)obj;
				if ((Object)(object)val2 == (Object)null || (Object)(object)val3 == (Object)null || (Object)(object)((StorableItemDefinition)val).StoredItem == (Object)null || (Object)(object)((StorableItemDefinition)val3).StoredItem == (Object)null)
				{
					return false;
				}
				if ((Object)(object)prototypes == (Object)null)
				{
					prototypes = new GameObject("EndgameArsenal_Prototypes");
					prototypes.SetActive(false);
					Object.DontDestroyOnLoad((Object)(object)prototypes);
				}
				if ((Object)(object)((Equippable_Viewmodel)val2).AvatarEquippable != (Object)null && !string.IsNullOrWhiteSpace(((Equippable_Viewmodel)val2).AvatarEquippable.AssetPath))
				{
					BaseAvatarAssetPath = ((Equippable_Viewmodel)val2).AvatarEquippable.AssetPath;
				}
				WeaponSpec[] all = WeaponCatalog.All;
				foreach (WeaponSpec weaponSpec in all)
				{
					if (!definitions.TryGetValue(weaponSpec.Id, out IntegerItemDefinition value) || (Object)(object)value == (Object)null)
					{
						BuildDefinitions(weaponSpec, val, val2, val3);
					}
					AddIfMissing(registry, definitions[weaponSpec.AmmoId]);
					AddIfMissing(registry, definitions[weaponSpec.Id]);
				}
				hookFailures = 0;
				return true;
			}
			finally
			{
				registering = false;
			}
		}

		private static void AddIfMissing(Registry registry, IntegerItemDefinition definition)
		{
			if (!Registry.ItemExists(((BaseItemDefinition)definition).ID))
			{
				registry.AddToRegistry((ItemDefinition)(object)definition);
			}
		}

		private static void BuildDefinitions(WeaponSpec spec, IntegerItemDefinition template, Equippable_RangedWeapon weaponTemplate, IntegerItemDefinition ammoTemplate)
		{
			IntegerItemDefinition val = Object.Instantiate<IntegerItemDefinition>(ammoTemplate);
			ConfigureDefinition(val, spec, ammo: true);
			val.DefaultValue = spec.Capacity;
			((StorableItemDefinition)val).StoredItem = CloneStored(((StorableItemDefinition)ammoTemplate).StoredItem, spec, customGunModel: false);
			if ((Object)(object)((ItemDefinition)ammoTemplate).Equippable != (Object)null)
			{
				GameObject val2 = ClonePrototype(((Component)((ItemDefinition)ammoTemplate).Equippable).gameObject, spec.AmmoId + "_Equippable");
				HideRenderers(val2.transform);
				WeaponVisuals.BuildAmmo(spec.Id, val2.transform);
				((ItemDefinition)val).Equippable = val2.GetComponent<Equippable>();
			}
			IntegerItemDefinition val3 = Object.Instantiate<IntegerItemDefinition>(template);
			ConfigureDefinition(val3, spec, ammo: false);
			val3.DefaultValue = spec.Capacity;
			((StorableItemDefinition)val3).StoredItem = CloneStored(((StorableItemDefinition)template).StoredItem, spec, customGunModel: true);
			GameObject val4 = ClonePrototype(((Component)weaponTemplate).gameObject, spec.Id + "_Equippable");
			Equippable_RangedWeapon component = val4.GetComponent<Equippable_RangedWeapon>();
			if ((Object)(object)component == (Object)null)
			{
				throw new InvalidOperationException("Native M1911 clone has no ranged weapon component.");
			}
			HideRenderers(val4.transform);
			WeaponVisuals.Build(spec.Id, val4.transform);
			WeaponAnimations.PreparePrototype(component, spec);
			if ((Object)(object)((Equippable_Viewmodel)weaponTemplate).AvatarEquippable != (Object)null)
			{
				AvatarEquippable component2 = ClonePrototype(((Component)((Equippable_Viewmodel)weaponTemplate).AvatarEquippable).gameObject, spec.Id + "_AvatarEquippable").GetComponent<AvatarEquippable>();
				component2.AssetPath = "EndgameArsenal/" + spec.Id;
				((Equippable_Viewmodel)component).AvatarEquippable = component2;
			}
			WeaponBehavior.Configure(component, spec, (StorableItemDefinition)(object)val);
			((ItemDefinition)val3).Equippable = (Equippable)(object)component;
			definitions[spec.AmmoId] = val;
			definitions[spec.Id] = val3;
		}

		private static void ConfigureDefinition(IntegerItemDefinition definition, WeaponSpec spec, bool ammo)
		{
			((Object)definition).name = (ammo ? spec.AmmoId : spec.Id);
			((BaseItemDefinition)definition).ID = (ammo ? spec.AmmoId : spec.Id);
			((BaseItemDefinition)definition).Name = (ammo ? spec.AmmoName : spec.Name);
			((BaseItemDefinition)definition).StackLimit = 1;
			ApplyDefinitionSettings(definition, spec, ammo);
			((BaseItemDefinition)definition).UsableInFilters = true;
			((Object)definition).hideFlags = (HideFlags)32;
		}

		private static void ApplyDefinitionSettings(IntegerItemDefinition definition, WeaponSpec spec, bool ammo)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			WeaponShopSettings weaponShopSettings = settings?.For(spec) ?? new WeaponShopSettings(spec.Rank, spec.Tier, spec.Price, spec.AmmoPrice);
			FullRank val = new FullRank
			{
				Rank = (ERank)weaponShopSettings.Rank,
				Tier = weaponShopSettings.Tier
			};
			((StorableItemDefinition)definition).BasePurchasePrice = (ammo ? weaponShopSettings.AmmoPrice : weaponShopSettings.Price);
			((StorableItemDefinition)definition).RequiresLevelToPurchase = true;
			((StorableItemDefinition)definition).RequiredRank = val;
			((BaseItemDefinition)definition).Description = (ammo ? $"A {spec.Capacity}-round magazine of ammunition for the {spec.Name}. Compatible only with this weapon." : $"{spec.Name}. Holds {spec.Capacity} rounds and uses {spec.AmmoName}. Available from Stan at {RankLabel(val)}.");
		}

		private unsafe static string RankLabel(FullRank rank)
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			return ((object)(*(ERank*)(&rank.Rank))/*cast due to .constrained prefix*/).ToString().Replace('_', ' ') + " " + (new string[6] { "", "I", "II", "III", "IV", "V" })[rank.Tier];
		}

		private static GameObject ClonePrototype(GameObject original, string name)
		{
			GameObject obj = Object.Instantiate<GameObject>(original, prototypes.transform, false);
			((Object)obj).name = name;
			obj.SetActive(true);
			return obj;
		}

		private static StoredItem CloneStored(StoredItem original, WeaponSpec spec, bool customGunModel)
		{
			GameObject val = ClonePrototype(((Component)original).gameObject, (customGunModel ? spec.Id : spec.AmmoId) + "_Stored");
			StoredItem component = val.GetComponent<StoredItem>();
			if ((Object)(object)component == (Object)null)
			{
				throw new InvalidOperationException("Native item clone has no storage component.");
			}
			HideRenderers(val.transform);
			if (customGunModel)
			{
				WeaponVisuals.Build(spec.Id, val.transform);
				return component;
			}
			WeaponVisuals.BuildAmmo(spec.Id, val.transform);
			return component;
		}

		private static void HideRenderers(Transform parent)
		{
			foreach (Renderer componentsInChild in ((Component)parent).GetComponentsInChildren<Renderer>(true))
			{
				componentsInChild.enabled = false;
			}
		}

		internal static bool IsArsenalId(string? id)
		{
			if (id != null)
			{
				return itemIds.Contains(id);
			}
			return false;
		}

		public static StorableItemDefinition? GetDefinition(string id)
		{
			if (!definitions.TryGetValue(id, out IntegerItemDefinition value))
			{
				return null;
			}
			return (StorableItemDefinition?)(object)value;
		}

		public static bool TryAddToStan()
		{
			if (!TryRegister())
			{
				return false;
			}
			ShopInterface shop = FindStanShop();
			if ((Object)(object)shop == (Object)null || shop.Listings == null || (Object)(object)shop.ListingContainer == (Object)null)
			{
				return false;
			}
			bool unlocksRegistered = false;
			ShopInitialization.Complete(delegate
			{
				bool flag = false;
				WeaponSpec[] all = WeaponCatalog.All;
				foreach (WeaponSpec weaponSpec in all)
				{
					flag |= AddListing(shop, (StorableItemDefinition)(object)definitions[weaponSpec.Id]);
					flag |= AddListing(shop, (StorableItemDefinition)(object)definitions[weaponSpec.AmmoId]);
				}
				if (flag)
				{
					shop.RefreshUnlockStatus();
					shop.RefreshShownItems();
				}
				unlocksRegistered = RegisterUnlocks();
			}, RefreshModelPortraits, ReportPortraitFailure);
			return unlocksRegistered;
		}

		private static ShopInterface? FindStanShop()
		{
			foreach (Stan item in Resources.FindObjectsOfTypeAll<Stan>())
			{
				if ((Object)(object)item != (Object)null && (Object)(object)item.ShopInterface != (Object)null)
				{
					return item.ShopInterface;
				}
			}
			GameObject obj = GameObject.Find("ArmsDealerInterface");
			if (obj == null)
			{
				return null;
			}
			return obj.GetComponent<ShopInterface>();
		}

		public static bool ApplySettings()
		{
			if (settings == null || definitions.Count != WeaponCatalog.All.Length * 2)
			{
				return false;
			}
			WeaponSpec[] all = WeaponCatalog.All;
			foreach (WeaponSpec weaponSpec in all)
			{
				ApplyDefinitionSettings(definitions[weaponSpec.Id], weaponSpec, ammo: false);
				ApplyDefinitionSettings(definitions[weaponSpec.AmmoId], weaponSpec, ammo: true);
			}
			ShopInterface val = FindStanShop();
			if ((Object)(object)val == (Object)null || val.Listings == null || !RegisterUnlocks())
			{
				return false;
			}
			Cart cart = val.Cart;
			if ((Object)(object)cart != (Object)null)
			{
				Enumerator<ShopListing> enumerator = val.Listings.GetEnumerator();
				while (enumerator.MoveNext())
				{
					ShopListing current = enumerator.Current;
					if ((Object)(object)((current != null) ? current.Item : null) != (Object)null && IsArsenalId(((BaseItemDefinition)current.Item).ID) && cart.GetCartCount(current) > 0 && !current.Item.IsUnlocked)
					{
						cart.RemoveListing(current);
					}
				}
				if (cart.cartEntries != null)
				{
					Enumerator<CartEntry> enumerator2 = cart.cartEntries.GetEnumerator();
					while (enumerator2.MoveNext())
					{
						CartEntry current2 = enumerator2.Current;
						object obj;
						if (current2 == null)
						{
							obj = null;
						}
						else
						{
							ShopListing listing = current2.Listing;
							obj = ((listing != null) ? listing.Item : null);
						}
						if ((Object)obj != (Object)null && IsArsenalId(((BaseItemDefinition)current2.Listing.Item).ID))
						{
							current2.UpdatePrice();
						}
					}
				}
				cart.UpdateTotal();
				cart.UpdateProblem();
			}
			val.RefreshUnlockStatus();
			if (val.listingUI != null)
			{
				Enumerator<ListingUI> enumerator3 = val.listingUI.GetEnumerator();
				while (enumerator3.MoveNext())
				{
					ListingUI current3 = enumerator3.Current;
					object obj2;
					if (current3 == null)
					{
						obj2 = null;
					}
					else
					{
						ShopListing listing2 = current3.Listing;
						obj2 = ((listing2 != null) ? listing2.Item : null);
					}
					if ((Object)obj2 != (Object)null && IsArsenalId(((BaseItemDefinition)current3.Listing.Item).ID))
					{
						current3.UpdatePrice();
						current3.UpdateButtons();
					}
				}
			}
			val.RefreshShownItems();
			ShopInterfaceDetailPanel detailPanel = val.DetailPanel;
			if ((Object)(object)((detailPanel != null) ? detailPanel.Panel : null) != (Object)null && ((Component)detailPanel.Panel).gameObject.activeSelf)
			{
				ListingUI listingUI = detailPanel._listingUI;
				object obj3;
				if (listingUI == null)
				{
					obj3 = null;
				}
				else
				{
					ShopListing listing3 = listingUI.Listing;
					obj3 = ((listing3 != null) ? listing3.Item : null);
				}
				if ((Object)obj3 != (Object)null && IsArsenalId(((BaseItemDefinition)detailPanel._listingUI.Listing.Item).ID))
				{
					detailPanel.Open(detailPanel._listingUI);
				}
			}
			return true;
		}

		public static void RetryModelPortraits()
		{
			ShopInitialization.RefreshOptional(RefreshModelPortraits, ReportPortraitFailure);
		}

		private static void RefreshModelPortraits()
		{
			if (PortraitsComplete || Time.unscaledTime < nextPortraitAttempt)
			{
				return;
			}
			nextPortraitAttempt = Time.unscaledTime + 5f;
			bool changed = false;
			WeaponSpec[] all = WeaponCatalog.All;
			foreach (WeaponSpec spec in all)
			{
				ShopInitialization.RefreshOptional(delegate
				{
					if (!modelPortraits.Contains(spec.Id))
					{
						((BaseItemDefinition)definitions[spec.Id]).Icon = WeaponVisuals.Icon(spec.Id, ammo: false);
						modelPortraits.Add(spec.Id);
						changed = true;
					}
				}, ReportPortraitFailure);
				ShopInitialization.RefreshOptional(delegate
				{
					if (!modelPortraits.Contains(spec.AmmoId))
					{
						((BaseItemDefinition)definitions[spec.AmmoId]).Icon = WeaponVisuals.Icon(spec.Id, ammo: true);
						modelPortraits.Add(spec.AmmoId);
						changed = true;
					}
				}, ReportPortraitFailure);
			}
			if (!changed)
			{
				return;
			}
			foreach (ListingUI item in Resources.FindObjectsOfTypeAll<ListingUI>())
			{
				object obj;
				if (item == null)
				{
					obj = null;
				}
				else
				{
					ShopListing listing = item.Listing;
					obj = ((listing != null) ? listing.Item : null);
				}
				StorableItemDefinition val = (StorableItemDefinition)obj;
				if ((Object)(object)val != (Object)null && IsArsenalId(((BaseItemDefinition)val).ID) && (Object)(object)item.Icon != (Object)null)
				{
					item.Icon.sprite = ((BaseItemDefinition)val).Icon;
				}
			}
			foreach (ItemUI item2 in Resources.FindObjectsOfTypeAll<ItemUI>())
			{
				object obj2;
				if (item2 == null)
				{
					obj2 = null;
				}
				else
				{
					ItemInstance itemInstance = item2.itemInstance;
					obj2 = ((itemInstance != null) ? itemInstance.Definition : null);
				}
				ItemDefinition val2 = (ItemDefinition)obj2;
				if ((Object)(object)val2 != (Object)null && IsArsenalId(((BaseItemDefinition)val2).ID) && (Object)(object)item2.IconImg != (Object)null)
				{
					item2.IconImg.sprite = ((BaseItemDefinition)val2).Icon;
				}
			}
			LevelManager instance = NetworkSingleton<LevelManager>.Instance;
			if ((Object)(object)instance == (Object)null || unlockManagerPointer != ((Il2CppObjectBase)instance).Pointer)
			{
				return;
			}
			foreach (KeyValuePair<string, (FullRank, Unlockable)> ownedUnlock in ownedUnlocks)
			{
				if (definitions.TryGetValue(ownedUnlock.Key, out IntegerItemDefinition value) && (Object)(object)value != (Object)null)
				{
					ownedUnlock.Value.Item2.Icon = ((BaseItemDefinition)value).Icon;
				}
			}
		}

		private static void ReportPortraitFailure(Exception ex)
		{
			if (portraitFailures++ < 3)
			{
				MelonLogger.Warning("Arsenal portrait update will retry; Stan's items remain available: " + ex.GetType().Name + ": " + ex.Message);
			}
		}

		private static bool AddListing(ShopInterface shop, StorableItemDefinition definition)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: 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_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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_0053: Expected O, but got Unknown
			if (shop.GetListing(((BaseItemDefinition)definition).ID) != null)
			{
				return false;
			}
			ShopListing val = new ShopListing
			{
				name = ((BaseItemDefinition)definition).Name,
				Item = definition,
				OverridePrice = false,
				LimitedStock = false,
				DefaultStock = 0,
				RestockRate = (ERestockRate)2,
				CanBeDelivered = false,
				ConditionalVisibility = false
			};
			shop.Listings.Add(val);
			val.Initialize(shop);
			shop.CreateListingUI(val);
			return true;
		}

		private static bool RegisterUnlocks()
		{
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Expected O, but got Unknown
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: 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_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			LevelManager instance = NetworkSingleton<LevelManager>.Instance;
			if ((Object)(object)instance == (Object)null || instance.Unlockables == null)
			{
				return false;
			}
			if (unlockManagerPointer != ((Il2CppObjectBase)instance).Pointer)
			{
				ownedUnlocks.Clear();
				unlockManagerPointer = ((Il2CppObjectBase)instance).Pointer;
			}
			foreach (IntegerItemDefinition value2 in definitions.Values)
			{
				FullRank requiredRank = ((StorableItemDefinition)value2).RequiredRank;
				Unlockable val;
				if (ownedUnlocks.TryGetValue(((BaseItemDefinition)value2).ID, out (FullRank, Unlockable) value))
				{
					if (value.Item1.Rank == requiredRank.Rank && value.Item1.Tier == requiredRank.Tier && ContainsUnlock(instance, requiredRank, value.Item2))
					{
						value.Item2.Icon = ((BaseItemDefinition)value2).Icon;
						continue;
					}
					RemoveOwnedUnlock(instance, value.Item1, value.Item2);
					val = value.Item2;
					val.Rank = requiredRank;
					val.Icon = ((BaseItemDefinition)value2).Icon;
				}
				else
				{
					val = new Unlockable(requiredRank, ((BaseItemDefinition)value2).Name, ((BaseItemDefinition)value2).Icon);
				}
				instance.AddUnlockable(val);
				if (ContainsUnlock(instance, requiredRank, val))
				{
					ownedUnlocks[((BaseItemDefinition)value2).ID] = (requiredRank, val);
				}
				else
				{
					ownedUnlocks.Remove(((BaseItemDefinition)value2).ID);
				}
			}
			return true;
		}

		private static bool ContainsUnlock(LevelManager manager, FullRank rank, Unlockable unlock)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			if (!manager.Unlockables.ContainsKey(rank))
			{
				return false;
			}
			Enumerator<Unlockable> enumerator = manager.Unlockables[rank].GetEnumerator();
			while (enumerator.MoveNext())
			{
				Unlockable current = enumerator.Current;
				if (current != null && ((Il2CppObjectBase)current).Pointer == ((Il2CppObjectBase)unlock).Pointer)
				{
					return true;
				}
			}
			return false;
		}

		private static void RemoveOwnedUnlock(LevelManager manager, FullRank rank, Unlockable unlock)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			if (!manager.Unlockables.ContainsKey(rank))
			{
				return;
			}
			List<Unlockable> val = manager.Unlockables[rank];
			for (int num = val.Count - 1; num >= 0; num--)
			{
				if (val[num] != null && ((Il2CppObjectBase)val[num]).Pointer == ((Il2CppObjectBase)unlock).Pointer)
				{
					val.RemoveAt(num);
				}
			}
		}
	}
	internal static class RemoteWeaponAnimations
	{
		private sealed class ObserverState
		{
			internal readonly AvatarRangedWeapon Weapon;

			internal readonly WeaponSpec Spec;

			internal readonly IntPtr AvatarPointer;

			private readonly Transform model;

			private readonly PartGroup magazine;

			private readonly PartGroup? slide;

			private float shotAt = float.NegativeInfinity;

			private float reloadAt;

			private float duration;

			private bool reloadActive;

			internal int Sequence { get; private set; }

			internal ObserverState(AvatarRangedWeapon weapon, WeaponSpec spec, Transform model, int previousSequence)
			{
				Weapon = weapon;
				Spec = spec;
				Avatar avatar = ((AvatarEquippable)weapon).avatar;
				AvatarPointer = ((avatar != null) ? ((Il2CppObjectBase)avatar).Pointer : IntPtr.Zero);
				this.model = model;
				Sequence = previousSequence;
				magazine = new PartGroup(model, (!(spec.Id == "eg_m