Decompiled source of MagazinePatcher v0.3.3

patchers/OldMagazinePatcherDisabler.dll

Decompiled 2 weeks ago
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
using BepInEx.Logging;
using Mono.Cecil;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("OldMagazinePatcherDisabler")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OldMagazinePatcherDisabler")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4f728f32-4deb-4870-bc9b-67fdd28dbfc4")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace OldMagazinePatcherDisabler;

public static class Disabler
{
	private static readonly ManualLogSource Logger = Logger.CreateLogSource("Deliter");

	public static IEnumerable<string> TargetDLLs => Enumerable.Empty<string>();

	public static void Patch(AssemblyDefinition asm)
	{
		Logger.LogWarning((object)("No DLLs should be patched, but the patch method was called. Assembly: " + (object)asm));
	}

	public static string CombinePaths(params string[] paths)
	{
		if (paths == null)
		{
			return "";
		}
		return paths.Aggregate(Path.Combine);
	}

	public static void Initialize()
	{
		string text = CombinePaths(Paths.ConfigPath, "h3vr.magazinepatcher.cfg");
		Logger.LogInfo((object)("Config file path: " + text));
		string text2 = CombinePaths(Paths.CachePath, "MagazinePatcher");
		if (!Directory.Exists(text2))
		{
			Directory.CreateDirectory(text2);
		}
		string text3 = CombinePaths(text2, "CachedCompatibleMags.json");
		Logger.LogInfo((object)("Cache path: " + text3));
		string[] directories = Directory.GetDirectories(Paths.PluginPath);
		foreach (string text4 in directories)
		{
			Logger.LogInfo((object)("Found directory " + text4));
			if (!text4.Contains("devyndamonster-MagazinePatcher"))
			{
				continue;
			}
			string text5 = CombinePaths(text4, "MagazinePatcher", "MagazinePatcher.dll");
			string text6 = CombinePaths(text4, "MagazinePatcher", "manifest.json");
			string text7 = CombinePaths(text4, "MagazinePatcher", "CachedCompatibleMags.json");
			if (!File.Exists(text) && !File.Exists(text3))
			{
				if (File.Exists(text5 + ".old"))
				{
					if (File.Exists(text7 + ".old"))
					{
						File.Copy(text7 + ".old", text3, overwrite: true);
					}
					else if (File.Exists(text7))
					{
						File.Copy(text7, text3, overwrite: true);
					}
				}
				else if (File.Exists(text5))
				{
					File.Copy(text7, text3, overwrite: true);
				}
			}
			if (File.Exists(text5 + ".bak.old"))
			{
				if (File.Exists(text5))
				{
					File.Delete(text5);
				}
				if (File.Exists(text5 + ".old"))
				{
					File.Delete(text5 + ".old");
				}
				File.Move(text5 + ".bak.old", text5 + ".old");
			}
			if (File.Exists(text6 + ".bak.old"))
			{
				if (File.Exists(text6))
				{
					File.Delete(text6);
				}
				if (File.Exists(text6 + ".old"))
				{
					File.Delete(text6 + ".old");
				}
				File.Move(text6 + ".bak.old", text6 + ".old");
			}
			if (File.Exists(text5 + ".bak"))
			{
				if (File.Exists(text5))
				{
					File.Delete(text5);
				}
				if (File.Exists(text5 + ".old"))
				{
					File.Delete(text5 + ".old");
				}
				File.Move(text5 + ".bak", text5 + ".old");
			}
			if (File.Exists(text6 + ".bak"))
			{
				if (File.Exists(text6))
				{
					File.Delete(text6);
				}
				if (File.Exists(text6 + ".old"))
				{
					File.Delete(text6 + ".old");
				}
				File.Move(text6 + ".bak", text6 + ".old");
			}
			if (File.Exists(text5))
			{
				if (File.Exists(text5 + ".old"))
				{
					File.Delete(text5 + ".old");
				}
				File.Move(text5, text5 + ".old");
				Logger.LogInfo((object)"Disabled original MagazinePatcher install. Re-enable it via disabling and enabling it in r2modman. Will break compatibility with new MagazinePatcher.");
			}
			if (File.Exists(text6))
			{
				if (File.Exists(text6 + ".old"))
				{
					File.Delete(text6 + ".old");
				}
				File.Move(text6, text6 + ".old");
			}
			break;
		}
	}
}

plugins/MagazinePatcher/MagazinePatcher.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FistVR;
using Microsoft.CodeAnalysis;
using OtherLoader;
using Stratum;
using UnityEngine;
using Valve.Newtonsoft.Json;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MagazinePatcher")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MagazinePatcher")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("98995a27-db6e-4095-afcc-a831bf390698")]
[assembly: AssemblyFileVersion("0.3.3.0")]
[assembly: AssemblyVersion("0.3.3.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 MagazinePatcher
{
	public class CompatibleMagazineCache
	{
		public List<string> Firearms;

		public List<string> Magazines;

		public List<string> Clips;

		public List<string> SpeedLoaders;

		public List<string> Bullets;

		public Dictionary<string, MagazineCacheEntry> Entries;

		public Dictionary<string, AmmoObjectDataTemplate> AmmoObjects;

		public Dictionary<FireArmMagazineType, List<AmmoObjectDataTemplate>> MagazineData;

		public Dictionary<FireArmClipType, List<AmmoObjectDataTemplate>> ClipData;

		public Dictionary<FireArmRoundType, List<AmmoObjectDataTemplate>> SpeedLoaderData;

		public Dictionary<FireArmRoundType, List<AmmoObjectDataTemplate>> BulletData;

		public static CompatibleMagazineCache Instance;

		public static Dictionary<string, MagazineBlacklistEntry> BlacklistEntries;

		public CompatibleMagazineCache()
		{
			Firearms = new List<string>();
			Magazines = new List<string>();
			Clips = new List<string>();
			SpeedLoaders = new List<string>();
			Bullets = new List<string>();
			Entries = new Dictionary<string, MagazineCacheEntry>();
			AmmoObjects = new Dictionary<string, AmmoObjectDataTemplate>();
			MagazineData = new Dictionary<FireArmMagazineType, List<AmmoObjectDataTemplate>>();
			ClipData = new Dictionary<FireArmClipType, List<AmmoObjectDataTemplate>>();
			SpeedLoaderData = new Dictionary<FireArmRoundType, List<AmmoObjectDataTemplate>>();
			BulletData = new Dictionary<FireArmRoundType, List<AmmoObjectDataTemplate>>();
			BlacklistEntries = new Dictionary<string, MagazineBlacklistEntry>();
		}

		public void AddMagazineData(FVRFireArmMagazine mag)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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)
			if (!MagazineData.ContainsKey(mag.MagazineType))
			{
				MagazineData.Add(mag.MagazineType, new List<AmmoObjectDataTemplate>());
			}
			MagazineData[mag.MagazineType].Add(new AmmoObjectDataTemplate(mag));
			if (!AmmoObjects.ContainsKey(((FVRPhysicalObject)mag).ObjectWrapper.ItemID))
			{
				AmmoObjects.Add(((FVRPhysicalObject)mag).ObjectWrapper.ItemID, new AmmoObjectDataTemplate(mag));
			}
		}

		public void AddClipData(FVRFireArmClip clip)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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)
			if (!ClipData.ContainsKey(clip.ClipType))
			{
				ClipData.Add(clip.ClipType, new List<AmmoObjectDataTemplate>());
			}
			ClipData[clip.ClipType].Add(new AmmoObjectDataTemplate(clip));
			if (!AmmoObjects.ContainsKey(((FVRPhysicalObject)clip).ObjectWrapper.ItemID))
			{
				AmmoObjects.Add(((FVRPhysicalObject)clip).ObjectWrapper.ItemID, new AmmoObjectDataTemplate(clip));
			}
		}

		public void AddSpeedLoaderData(Speedloader speedloader)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			if (!SpeedLoaderData.ContainsKey(speedloader.Chambers[0].Type))
			{
				SpeedLoaderData.Add(speedloader.Chambers[0].Type, new List<AmmoObjectDataTemplate>());
			}
			SpeedLoaderData[speedloader.Chambers[0].Type].Add(new AmmoObjectDataTemplate(speedloader));
			if (!AmmoObjects.ContainsKey(((FVRPhysicalObject)speedloader).ObjectWrapper.ItemID))
			{
				AmmoObjects.Add(((FVRPhysicalObject)speedloader).ObjectWrapper.ItemID, new AmmoObjectDataTemplate(speedloader));
			}
		}

		public void AddBulletData(FVRFireArmRound bullet)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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)
			if (!BulletData.ContainsKey(bullet.RoundType))
			{
				BulletData.Add(bullet.RoundType, new List<AmmoObjectDataTemplate>());
			}
			BulletData[bullet.RoundType].Add(new AmmoObjectDataTemplate(bullet));
			if (!AmmoObjects.ContainsKey(((FVRPhysicalObject)bullet).ObjectWrapper.ItemID))
			{
				AmmoObjects.Add(((FVRPhysicalObject)bullet).ObjectWrapper.ItemID, new AmmoObjectDataTemplate(bullet));
			}
		}
	}
	public class MagazineCacheEntry
	{
		public string FirearmID;

		public FireArmMagazineType MagType;

		public FireArmClipType ClipType;

		public FireArmRoundType BulletType;

		public bool DoesUseSpeedloader;

		public HashSet<string> CompatibleMagazines;

		public HashSet<string> CompatibleClips;

		public HashSet<string> CompatibleSpeedLoaders;

		public HashSet<string> CompatibleBullets;

		public MagazineCacheEntry()
		{
			CompatibleMagazines = new HashSet<string>();
			CompatibleClips = new HashSet<string>();
			CompatibleSpeedLoaders = new HashSet<string>();
			CompatibleBullets = new HashSet<string>();
		}
	}
	public class AmmoObjectDataTemplate
	{
		public string ObjectID;

		public int Capacity;

		public FireArmMagazineType MagType;

		public FireArmRoundType RoundType;

		public AmmoObjectDataTemplate()
		{
		}

		public AmmoObjectDataTemplate(FVRFireArmMagazine mag)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			ObjectID = ((FVRPhysicalObject)mag).ObjectWrapper.ItemID;
			Capacity = mag.m_capacity;
			MagType = mag.MagazineType;
			RoundType = mag.RoundType;
		}

		public AmmoObjectDataTemplate(FVRFireArmClip clip)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			ObjectID = ((FVRPhysicalObject)clip).ObjectWrapper.ItemID;
			Capacity = clip.m_capacity;
			MagType = (FireArmMagazineType)0;
			RoundType = clip.RoundType;
		}

		public AmmoObjectDataTemplate(Speedloader speedloader)
		{
			//IL_002a: 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_0041: Unknown result type (might be due to invalid IL or missing references)
			ObjectID = ((FVRPhysicalObject)speedloader).ObjectWrapper.ItemID;
			Capacity = speedloader.Chambers.Count;
			MagType = (FireArmMagazineType)0;
			RoundType = speedloader.Chambers[0].Type;
		}

		public AmmoObjectDataTemplate(FVRFireArmRound bullet)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			ObjectID = ((FVRPhysicalObject)bullet).ObjectWrapper.ItemID;
			Capacity = -1;
			MagType = (FireArmMagazineType)0;
			RoundType = bullet.RoundType;
		}
	}
	public class MagazineBlacklistEntry
	{
		public string FirearmID;

		public List<string> MagazineBlacklist = new List<string>();

		public List<string> MagazineWhitelist = new List<string>();

		public List<string> ClipBlacklist = new List<string>();

		public List<string> ClipWhitelist = new List<string>();

		public List<string> SpeedLoaderBlacklist = new List<string>();

		public List<string> SpeedLoaderWhitelist = new List<string>();

		public List<string> RoundBlacklist = new List<string>();

		public List<string> RoundWhitelist = new List<string>();

		public bool IsItemBlacklisted(string itemID)
		{
			if (!MagazineBlacklist.Contains(itemID) && !ClipBlacklist.Contains(itemID) && !RoundBlacklist.Contains(itemID))
			{
				return SpeedLoaderBlacklist.Contains(itemID);
			}
			return true;
		}

		public bool IsMagazineAllowed(string itemID)
		{
			if (MagazineWhitelist.Count > 0 && !MagazineWhitelist.Contains(itemID))
			{
				return false;
			}
			if (MagazineBlacklist.Contains(itemID))
			{
				return false;
			}
			return true;
		}

		public bool IsClipAllowed(string itemID)
		{
			if (ClipWhitelist.Count > 0 && !ClipWhitelist.Contains(itemID))
			{
				return false;
			}
			if (ClipBlacklist.Contains(itemID))
			{
				return false;
			}
			return true;
		}

		public bool IsSpeedloaderAllowed(string itemID)
		{
			if (SpeedLoaderWhitelist.Count > 0 && !SpeedLoaderWhitelist.Contains(itemID))
			{
				return false;
			}
			if (SpeedLoaderBlacklist.Contains(itemID))
			{
				return false;
			}
			return true;
		}

		public bool IsRoundAllowed(string itemID)
		{
			if (RoundWhitelist.Count > 0 && !RoundWhitelist.Contains(itemID))
			{
				return false;
			}
			if (RoundBlacklist.Contains(itemID))
			{
				return false;
			}
			return true;
		}
	}
	internal static class PatchLogger
	{
		public enum LogType
		{
			General,
			Debug
		}

		public static ManualLogSource BepLog;

		public static bool AllowLogging;

		public static bool LogDebug;

		public static void Init()
		{
			BepLog = Logger.CreateLogSource("MagazinePatcher");
		}

		public static void Log(string log, LogType type)
		{
			if (!AllowLogging)
			{
				return;
			}
			switch (type)
			{
			case LogType.General:
				BepLog.LogInfo((object)log);
				break;
			case LogType.Debug:
				if (LogDebug)
				{
					BepLog.LogInfo((object)log);
				}
				break;
			}
		}

		public static void LogWarning(string log)
		{
			BepLog.LogWarning((object)log);
		}

		public static void LogError(string log)
		{
			BepLog.LogError((object)log);
		}
	}
	[BepInPlugin("h3vr.magazinepatcher", "MagazinePatcher", "0.3.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency("stratum", "1.1.0")]
	public class MagazinePatcher : StratumPlugin
	{
		[CompilerGenerated]
		private sealed class <LoadMagazineCacheAsync>d__21 : IEnumerator<object>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private object <>2__current;

			private bool <canCache>5__2;

			private bool <isOtherloaderLoaded>5__3;

			private List<FVRObject> <clips>5__4;

			private List<FVRObject> <speedloaders>5__5;

			private List<FVRObject> <bullets>5__6;

			private List<FVRObject> <firearms>5__7;

			private int <totalObjects>5__8;

			private int <progress>5__9;

			private DateTime <start>5__10;

			private List<string> <skipList>5__11;

			private List<FVRObject>.Enumerator <>7__wrap11;

			private FVRObject <magazine>5__13;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <LoadMagazineCacheAsync>d__21(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				switch (<>1__state)
				{
				case -3:
				case 2:
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
					break;
				case -4:
				case 3:
					try
					{
					}
					finally
					{
						<>m__Finally2();
					}
					break;
				case -5:
				case 4:
					try
					{
					}
					finally
					{
						<>m__Finally3();
					}
					break;
				case -6:
				case 5:
					try
					{
					}
					finally
					{
						<>m__Finally4();
					}
					break;
				case -7:
				case 6:
					try
					{
					}
					finally
					{
						<>m__Finally5();
					}
					break;
				}
				<clips>5__4 = null;
				<speedloaders>5__5 = null;
				<bullets>5__6 = null;
				<firearms>5__7 = null;
				<skipList>5__11 = null;
				<>7__wrap11 = default(List<FVRObject>.Enumerator);
				<magazine>5__13 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0be4: Unknown result type (might be due to invalid IL or missing references)
				//IL_0be9: Unknown result type (might be due to invalid IL or missing references)
				//IL_0bf1: Unknown result type (might be due to invalid IL or missing references)
				//IL_0bf6: Unknown result type (might be due to invalid IL or missing references)
				//IL_0bfe: Unknown result type (might be due to invalid IL or missing references)
				//IL_0c03: Unknown result type (might be due to invalid IL or missing references)
				//IL_0c0c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0c13: Unknown result type (might be due to invalid IL or missing references)
				//IL_0c1a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0c41: Unknown result type (might be due to invalid IL or missing references)
				//IL_0c58: Unknown result type (might be due to invalid IL or missing references)
				//IL_0c5d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0a95: Unknown result type (might be due to invalid IL or missing references)
				//IL_0a9b: Invalid comparison between Unknown and I4
				//IL_0d74: Unknown result type (might be due to invalid IL or missing references)
				//IL_0dea: Unknown result type (might be due to invalid IL or missing references)
				//IL_0d87: Unknown result type (might be due to invalid IL or missing references)
				//IL_0dfd: Unknown result type (might be due to invalid IL or missing references)
				//IL_0d9f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0f02: Unknown result type (might be due to invalid IL or missing references)
				//IL_0e76: Unknown result type (might be due to invalid IL or missing references)
				//IL_0e15: Unknown result type (might be due to invalid IL or missing references)
				//IL_0f1a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0e8e: Unknown result type (might be due to invalid IL or missing references)
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						PatchLogger.Log("Patching has started", PatchLogger.LogType.General);
						<canCache>5__2 = false;
						<isOtherloaderLoaded>5__3 = false;
						try
						{
							PokeOtherLoader();
							<isOtherloaderLoaded>5__3 = true;
							PatchLogger.Log("Otherloader detected!", PatchLogger.LogType.General);
						}
						catch
						{
							PatchLogger.Log("Otherloader not detected!", PatchLogger.LogType.General);
						}
						goto IL_0077;
					case 1:
					{
						<>1__state = -1;
						if (<isOtherloaderLoaded>5__3)
						{
							<canCache>5__2 = GetOtherLoaderProgress() >= 1f;
						}
						if (!<canCache>5__2 & <isOtherloaderLoaded>5__3)
						{
							goto IL_0077;
						}
						PatcherStatus.AppendCacheLog("Checking Cache");
						bool num = LoadFullCache();
						CompatibleMagazineCache.BlacklistEntries = GetMagazineCacheBlacklist();
						if (num)
						{
							break;
						}
						PatchLogger.Log($"[{DateTime.Now:HH:mm:ss}] Caching started!", PatchLogger.LogType.General);
						PatchLogger.Log("Building new magazine cache -- This may take a while!", PatchLogger.LogType.General);
						PatcherStatus.AppendCacheLog("Caching Started -- This may take a while!");
						List<FVRObject> list = ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)2];
						<clips>5__4 = ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)3];
						<speedloaders>5__5 = ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)6];
						<bullets>5__6 = ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)4];
						<firearms>5__7 = ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)1];
						<totalObjects>5__8 = list.Count + <clips>5__4.Count + <bullets>5__6.Count + <speedloaders>5__5.Count + <firearms>5__7.Count;
						<progress>5__9 = 0;
						PatchLogger.Log("Loading all magazines", PatchLogger.LogType.General);
						PatcherStatus.AppendCacheLog("Caching Magazines");
						<start>5__10 = DateTime.Now;
						<>7__wrap11 = list.GetEnumerator();
						<>1__state = -3;
						goto IL_0395;
					}
					case 2:
					{
						<>1__state = -3;
						if ((Object)(object)((AnvilAsset)<magazine>5__13).GetGameObject() == (Object)null)
						{
							PatchLogger.LogWarning("No object was found to use FVRObject! ItemID: " + <magazine>5__13.ItemID);
							goto IL_0395;
						}
						FVRFireArmMagazine component5 = ((AnvilAsset)<magazine>5__13).GetGameObject().GetComponent<FVRFireArmMagazine>();
						if ((Object)(object)component5 != (Object)null)
						{
							if ((Object)(object)((FVRPhysicalObject)component5).ObjectWrapper == (Object)null)
							{
								PatchLogger.LogWarning("Object was found to have no ObjectWrapper assigned! ItemID: " + <magazine>5__13.ItemID);
								((FVRPhysicalObject)component5).ObjectWrapper = <magazine>5__13;
							}
							CompatibleMagazineCache.Instance.AddMagazineData(component5);
						}
						CompatibleMagazineCache.Instance.Magazines.Add(<magazine>5__13.ItemID);
						goto IL_038e;
					}
					case 3:
					{
						<>1__state = -4;
						if ((Object)(object)((AnvilAsset)<magazine>5__13).GetGameObject() == (Object)null)
						{
							PatchLogger.LogWarning("No object was found to use FVRObject! ItemID: " + <magazine>5__13.ItemID);
							goto IL_058b;
						}
						FVRFireArmClip component3 = ((AnvilAsset)<magazine>5__13).GetGameObject().GetComponent<FVRFireArmClip>();
						if ((Object)(object)component3 != (Object)null)
						{
							if ((Object)(object)((FVRPhysicalObject)component3).ObjectWrapper == (Object)null)
							{
								PatchLogger.LogWarning("Object was found to have no ObjectWrapper assigned! ItemID: " + <magazine>5__13.ItemID);
								((FVRPhysicalObject)component3).ObjectWrapper = <magazine>5__13;
							}
							CompatibleMagazineCache.Instance.AddClipData(component3);
						}
						CompatibleMagazineCache.Instance.Clips.Add(<magazine>5__13.ItemID);
						goto IL_0584;
					}
					case 4:
					{
						<>1__state = -5;
						if ((Object)(object)((AnvilAsset)<magazine>5__13).GetGameObject() == (Object)null)
						{
							PatchLogger.LogWarning("No object was found to use FVRObject! ItemID: " + <magazine>5__13.ItemID);
							goto IL_0781;
						}
						Speedloader component6 = ((AnvilAsset)<magazine>5__13).GetGameObject().GetComponent<Speedloader>();
						if ((Object)(object)component6 != (Object)null)
						{
							if ((Object)(object)((FVRPhysicalObject)component6).ObjectWrapper == (Object)null)
							{
								PatchLogger.LogWarning("Object was found to have no ObjectWrapper assigned! ItemID: " + <magazine>5__13.ItemID);
								((FVRPhysicalObject)component6).ObjectWrapper = <magazine>5__13;
							}
							CompatibleMagazineCache.Instance.AddSpeedLoaderData(component6);
						}
						CompatibleMagazineCache.Instance.SpeedLoaders.Add(<magazine>5__13.ItemID);
						goto IL_077a;
					}
					case 5:
					{
						<>1__state = -6;
						if ((Object)(object)((AnvilAsset)<magazine>5__13).GetGameObject() == (Object)null)
						{
							PatchLogger.LogWarning("No object was found to use FVRObject! ItemID: " + <magazine>5__13.ItemID);
							goto IL_0977;
						}
						FVRFireArmRound component4 = ((AnvilAsset)<magazine>5__13).GetGameObject().GetComponent<FVRFireArmRound>();
						if ((Object)(object)component4 != (Object)null)
						{
							if ((Object)(object)((FVRPhysicalObject)component4).ObjectWrapper == (Object)null)
							{
								PatchLogger.LogWarning("Object was found to have no ObjectWrapper assigned! ItemID: " + <magazine>5__13.ItemID);
								((FVRPhysicalObject)component4).ObjectWrapper = <magazine>5__13;
							}
							CompatibleMagazineCache.Instance.AddBulletData(component4);
						}
						CompatibleMagazineCache.Instance.Bullets.Add(<magazine>5__13.ItemID);
						goto IL_0970;
					}
					case 6:
						{
							<>1__state = -7;
							if ((Object)(object)((AnvilAsset)<magazine>5__13).GetGameObject() == (Object)null)
							{
								PatchLogger.LogWarning("No object was found to use FVRObject! ItemID: " + <magazine>5__13.ItemID);
								goto IL_0cde;
							}
							FVRFireArm component = ((AnvilAsset)<magazine>5__13).GetGameObject().GetComponent<FVRFireArm>();
							if ((Object)(object)component != (Object)null)
							{
								if ((Object)(object)((FVRPhysicalObject)component).ObjectWrapper == (Object)null)
								{
									PatchLogger.LogWarning("Object was found to have no ObjectWrapper assigned! ItemID: " + <magazine>5__13.ItemID);
									((FVRPhysicalObject)component).ObjectWrapper = <magazine>5__13;
								}
								MagazineCacheEntry magazineCacheEntry = new MagazineCacheEntry
								{
									FirearmID = <magazine>5__13.ItemID,
									MagType = component.MagazineType,
									ClipType = component.ClipType,
									BulletType = component.RoundType
								};
								if (!ValidFireArm(component.RoundType, component.ClipType, component.MagazineType, <magazine>5__13.MagazineCapacity))
								{
									PatchLogger.Log($"Firearm {<magazine>5__13.DisplayName} being set to round type {InvalidRoundType}!", PatchLogger.LogType.Debug);
									magazineCacheEntry.BulletType = InvalidRoundType;
								}
								Revolver component2 = ((Component)component).gameObject.GetComponent<Revolver>();
								if ((Object)(object)component2 != (Object)null)
								{
									magazineCacheEntry.DoesUseSpeedloader = true;
									IM.OD[magazineCacheEntry.FirearmID].MagazineCapacity = component2.Chambers.Length;
								}
								CompatibleMagazineCache.Instance.Entries.Add(<magazine>5__13.ItemID, magazineCacheEntry);
							}
							CompatibleMagazineCache.Instance.Firearms.Add(<magazine>5__13.ItemID);
							goto IL_0cd7;
						}
						IL_0977:
						if (<>7__wrap11.MoveNext())
						{
							<magazine>5__13 = <>7__wrap11.Current;
							if ((DateTime.Now - <start>5__10).TotalSeconds > 2.0)
							{
								<start>5__10 = DateTime.Now;
								PatchLogger.Log($"-- {(int)((float)<progress>5__9 / (float)<totalObjects>5__8 * 100f)}% --", PatchLogger.LogType.General);
							}
							PatcherStatus.UpdateProgress(Mathf.Min((float)<progress>5__9 / (float)<totalObjects>5__8, 0.999f));
							<progress>5__9++;
							LastTouchedItem = <magazine>5__13.ItemID;
							if (!CompatibleMagazineCache.Instance.Bullets.Contains(<magazine>5__13.ItemID))
							{
								AnvilCallback<GameObject> gameObjectAsync = ((AnvilAsset)<magazine>5__13).GetGameObjectAsync();
								<>2__current = gameObjectAsync;
								<>1__state = 5;
								return true;
							}
							goto IL_0970;
						}
						<>m__Finally4();
						<>7__wrap11 = default(List<FVRObject>.Enumerator);
						PatchLogger.Log("Loading all firearms", PatchLogger.LogType.General);
						PatcherStatus.AppendCacheLog("Caching Firearms");
						<skipList>5__11 = new List<string>();
						<>7__wrap11 = <firearms>5__7.GetEnumerator();
						<>1__state = -7;
						goto IL_0cde;
						IL_0cde:
						while (<>7__wrap11.MoveNext())
						{
							<magazine>5__13 = <>7__wrap11.Current;
							if ((DateTime.Now - <start>5__10).TotalSeconds > 2.0)
							{
								<start>5__10 = DateTime.Now;
								PatchLogger.Log($"-- {(int)((float)<progress>5__9 / (float)<totalObjects>5__8 * 100f)}% --", PatchLogger.LogType.General);
							}
							PatcherStatus.UpdateProgress(Mathf.Min((float)<progress>5__9 / (float)<totalObjects>5__8, 0.999f));
							<progress>5__9++;
							LastTouchedItem = <magazine>5__13.ItemID;
							if (!<magazine>5__13.IsModContent && (int)<magazine>5__13.TagFirearmAction == 8 && <magazine>5__13.TagFirearmFeedOption.Contains((OTagFirearmFeedOption)1))
							{
								<skipList>5__11.Add(<magazine>5__13.ItemID);
							}
							if (!CompatibleMagazineCache.Instance.Firearms.Contains(<magazine>5__13.ItemID))
							{
								if (!IM.OD.ContainsKey(<magazine>5__13.ItemID))
								{
									PatchLogger.LogWarning("Item not found in Object Dictionary! ItemID: " + <magazine>5__13.ItemID);
									continue;
								}
								AnvilCallback<GameObject> gameObjectAsync = ((AnvilAsset)<magazine>5__13).GetGameObjectAsync();
								<>2__current = gameObjectAsync;
								<>1__state = 6;
								return true;
							}
							goto IL_0cd7;
						}
						<>m__Finally5();
						<>7__wrap11 = default(List<FVRObject>.Enumerator);
						PatchLogger.Log("Building Cache Entries", PatchLogger.LogType.General);
						PatcherStatus.AppendCacheLog("Building Cache");
						foreach (MagazineCacheEntry value2 in CompatibleMagazineCache.Instance.Entries.Values)
						{
							if (!IM.OD.ContainsKey(value2.FirearmID) || <skipList>5__11.Contains(value2.FirearmID))
							{
								continue;
							}
							LastTouchedItem = value2.FirearmID;
							if ((int)value2.MagType != 0 && CompatibleMagazineCache.Instance.MagazineData.ContainsKey(value2.MagType))
							{
								foreach (AmmoObjectDataTemplate item in CompatibleMagazineCache.Instance.MagazineData[value2.MagType])
								{
									value2.CompatibleMagazines.Add(item.ObjectID);
								}
							}
							if ((int)value2.ClipType != 0 && CompatibleMagazineCache.Instance.ClipData.ContainsKey(value2.ClipType))
							{
								foreach (AmmoObjectDataTemplate item2 in CompatibleMagazineCache.Instance.ClipData[value2.ClipType])
								{
									value2.CompatibleClips.Add(item2.ObjectID);
								}
							}
							if (value2.DoesUseSpeedloader && CompatibleMagazineCache.Instance.SpeedLoaderData.ContainsKey(value2.BulletType))
							{
								foreach (AmmoObjectDataTemplate item3 in CompatibleMagazineCache.Instance.SpeedLoaderData[value2.BulletType])
								{
									if (IM.OD[value2.FirearmID].MagazineCapacity == item3.Capacity)
									{
										value2.CompatibleSpeedLoaders.Add(item3.ObjectID);
									}
								}
							}
							if (!CompatibleMagazineCache.Instance.BulletData.ContainsKey(value2.BulletType))
							{
								continue;
							}
							foreach (AmmoObjectDataTemplate item4 in CompatibleMagazineCache.Instance.BulletData[value2.BulletType])
							{
								value2.CompatibleBullets.Add(item4.ObjectID);
							}
						}
						PatchLogger.Log("Saving Data", PatchLogger.LogType.General);
						PatcherStatus.AppendCacheLog("Saving");
						using (StreamWriter streamWriter = File.CreateText(FullCachePath))
						{
							string value = JsonConvert.SerializeObject((object)CompatibleMagazineCache.Instance, (Formatting)1);
							streamWriter.WriteLine(value);
							streamWriter.Close();
							PatchLogger.Log($"[{DateTime.Now:HH:mm:ss}] Caching finished!", PatchLogger.LogType.General);
						}
						<clips>5__4 = null;
						<speedloaders>5__5 = null;
						<bullets>5__6 = null;
						<firearms>5__7 = null;
						<skipList>5__11 = null;
						break;
						IL_0077:
						<>2__current = null;
						<>1__state = 1;
						return true;
						IL_0970:
						<magazine>5__13 = null;
						goto IL_0977;
						IL_0395:
						if (<>7__wrap11.MoveNext())
						{
							<magazine>5__13 = <>7__wrap11.Current;
							if ((DateTime.Now - <start>5__10).TotalSeconds > 2.0)
							{
								<start>5__10 = DateTime.Now;
								PatchLogger.Log($"-- {(int)((float)<progress>5__9 / (float)<totalObjects>5__8 * 100f)}% --", PatchLogger.LogType.General);
							}
							PatcherStatus.UpdateProgress(Mathf.Min((float)<progress>5__9 / (float)<totalObjects>5__8, 0.999f));
							<progress>5__9++;
							LastTouchedItem = <magazine>5__13.ItemID;
							if (!CompatibleMagazineCache.Instance.Magazines.Contains(<magazine>5__13.ItemID))
							{
								AnvilCallback<GameObject> gameObjectAsync = ((AnvilAsset)<magazine>5__13).GetGameObjectAsync();
								<>2__current = gameObjectAsync;
								<>1__state = 2;
								return true;
							}
							goto IL_038e;
						}
						<>m__Finally1();
						<>7__wrap11 = default(List<FVRObject>.Enumerator);
						PatchLogger.Log("Loading all clips", PatchLogger.LogType.General);
						PatcherStatus.AppendCacheLog("Caching Clips");
						<>7__wrap11 = <clips>5__4.GetEnumerator();
						<>1__state = -4;
						goto IL_058b;
						IL_077a:
						<magazine>5__13 = null;
						goto IL_0781;
						IL_058b:
						if (<>7__wrap11.MoveNext())
						{
							<magazine>5__13 = <>7__wrap11.Current;
							if ((DateTime.Now - <start>5__10).TotalSeconds > 2.0)
							{
								<start>5__10 = DateTime.Now;
								PatchLogger.Log($"-- {(int)((float)<progress>5__9 / (float)<totalObjects>5__8 * 100f)}% --", PatchLogger.LogType.General);
							}
							PatcherStatus.UpdateProgress(Mathf.Min((float)<progress>5__9 / (float)<totalObjects>5__8, 0.999f));
							<progress>5__9++;
							LastTouchedItem = <magazine>5__13.ItemID;
							if (!CompatibleMagazineCache.Instance.Clips.Contains(<magazine>5__13.ItemID))
							{
								AnvilCallback<GameObject> gameObjectAsync = ((AnvilAsset)<magazine>5__13).GetGameObjectAsync();
								<>2__current = gameObjectAsync;
								<>1__state = 3;
								return true;
							}
							goto IL_0584;
						}
						<>m__Finally2();
						<>7__wrap11 = default(List<FVRObject>.Enumerator);
						PatchLogger.Log("Loading all speedloaders", PatchLogger.LogType.General);
						PatcherStatus.AppendCacheLog("Caching Speedloaders");
						<>7__wrap11 = <speedloaders>5__5.GetEnumerator();
						<>1__state = -5;
						goto IL_0781;
						IL_038e:
						<magazine>5__13 = null;
						goto IL_0395;
						IL_0781:
						if (<>7__wrap11.MoveNext())
						{
							<magazine>5__13 = <>7__wrap11.Current;
							if ((DateTime.Now - <start>5__10).TotalSeconds > 2.0)
							{
								<start>5__10 = DateTime.Now;
								PatchLogger.Log($"-- {(int)((float)<progress>5__9 / (float)<totalObjects>5__8 * 100f)}% --", PatchLogger.LogType.General);
							}
							PatcherStatus.UpdateProgress(Mathf.Min((float)<progress>5__9 / (float)<totalObjects>5__8, 0.999f));
							<progress>5__9++;
							LastTouchedItem = <magazine>5__13.ItemID;
							if (!CompatibleMagazineCache.Instance.SpeedLoaders.Contains(<magazine>5__13.ItemID))
							{
								AnvilCallback<GameObject> gameObjectAsync = ((AnvilAsset)<magazine>5__13).GetGameObjectAsync();
								<>2__current = gameObjectAsync;
								<>1__state = 4;
								return true;
							}
							goto IL_077a;
						}
						<>m__Finally3();
						<>7__wrap11 = default(List<FVRObject>.Enumerator);
						PatchLogger.Log("Loading all bullets", PatchLogger.LogType.General);
						PatcherStatus.AppendCacheLog("Caching Bullets");
						<>7__wrap11 = <bullets>5__6.GetEnumerator();
						<>1__state = -6;
						goto IL_0977;
						IL_0584:
						<magazine>5__13 = null;
						goto IL_058b;
						IL_0cd7:
						<magazine>5__13 = null;
						goto IL_0cde;
					}
					PatchLogger.Log("Applying magazine cache to firearms", PatchLogger.LogType.General);
					ApplyMagazineCache(CompatibleMagazineCache.Instance);
					RemoveBlacklistedMagazines(CompatibleMagazineCache.BlacklistEntries);
					PatcherStatus.UpdateProgress(1f);
					return false;
				}
				catch
				{
					//try-fault
					((IDisposable)this).Dispose();
					throw;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				((IDisposable)<>7__wrap11).Dispose();
			}

			private void <>m__Finally2()
			{
				<>1__state = -1;
				((IDisposable)<>7__wrap11).Dispose();
			}

			private void <>m__Finally3()
			{
				<>1__state = -1;
				((IDisposable)<>7__wrap11).Dispose();
			}

			private void <>m__Finally4()
			{
				<>1__state = -1;
				((IDisposable)<>7__wrap11).Dispose();
			}

			private void <>m__Finally5()
			{
				<>1__state = -1;
				((IDisposable)<>7__wrap11).Dispose();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		[CompilerGenerated]
		private sealed class <OnRuntime>d__16 : IEnumerator<object>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private object <>2__current;

			public MagazinePatcher <>4__this;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <OnRuntime>d__16(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				int num = <>1__state;
				MagazinePatcher magazinePatcher = <>4__this;
				if (num != 0)
				{
					return false;
				}
				<>1__state = -1;
				PatchLogger.Log("MagazinePatcher runtime has started!", PatchLogger.LogType.General);
				((MonoBehaviour)magazinePatcher).StartCoroutine(RunAndCatch(LoadMagazineCacheAsync(), delegate(Exception e)
				{
					PatcherStatus.AppendCacheLog("Something bad happened while caching item: " + LastTouchedItem);
					PatcherStatus.CachingFailed = true;
					PatchLogger.LogError("Something bad happened while caching item: " + LastTouchedItem);
					PatchLogger.LogError(e.ToString());
				}));
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		[CompilerGenerated]
		private sealed class <RunAndCatch>d__28 : IEnumerator<object>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private object <>2__current;

			public IEnumerator routine;

			public Action<Exception> onError;

			private bool <more>5__2;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <RunAndCatch>d__28(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<more>5__2 = true;
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				while (<more>5__2)
				{
					try
					{
						<more>5__2 = routine.MoveNext();
					}
					catch (Exception obj)
					{
						onError?.Invoke(obj);
						return false;
					}
					if (<more>5__2)
					{
						<>2__current = routine.Current;
						<>1__state = 1;
						return true;
					}
				}
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private static ConfigEntry<bool> ResetBasicCacheOnNextStart;

		private static ConfigEntry<bool> ResetXLCacheOnNextStart;

		private static ConfigEntry<bool> DeleteCacheOnNextStart;

		private static ConfigEntry<bool> EnableLogging;

		private static ConfigEntry<bool> LogDebugInfo;

		private static string FullCachePath;

		private static string BlacklistPath;

		private static string BasicCachePath;

		private static string XLCachePath;

		private static string LastTouchedItem;

		private static readonly FireArmRoundType InvalidRoundType = (FireArmRoundType)(-1);

		private void Awake()
		{
			PatchLogger.Init();
			GetPaths();
			LoadConfigFile();
		}

		private void GetPaths()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			FullCachePath = Path.Combine(Path.Combine(Paths.CachePath, "MagazinePatcher"), "CachedCompatibleMags.json");
			PatchLogger.Log("Full cache path: " + FullCachePath, PatchLogger.LogType.Debug);
			BlacklistPath = Path.Combine(new PluginDirectories(new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))).Data.FullName, "MagazineCacheBlacklist.json");
			PatchLogger.Log("Blacklist path: " + BlacklistPath, PatchLogger.LogType.Debug);
			string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "backup");
			BasicCachePath = Path.Combine(path, "CachedCompatibleMags-Basic.json");
			PatchLogger.Log("Backup cache path: " + BasicCachePath, PatchLogger.LogType.Debug);
			XLCachePath = Path.Combine(path, "CachedCompatibleMags-XL.json");
			PatchLogger.Log("Backup cache path: " + BasicCachePath, PatchLogger.LogType.Debug);
		}

		private void LoadConfigFile()
		{
			PatchLogger.Log("Getting config file", PatchLogger.LogType.General);
			ResetBasicCacheOnNextStart = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Reset to Basic Cache on Next Start", false, "If true, resets the cache from a basic starting cache on the next start of H3VR. MagazinePatcher will then cache any items that are missing from it. This setting will always be set back to false after startup.");
			ResetXLCacheOnNextStart = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Reset to XL Cache on Next Start", false, "Experimental. If true, resets the cache from the XL starting cache on the next start of H3VR. This is a large cache made from many available mods. This setting will always be set back to false after startup.");
			DeleteCacheOnNextStart = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Delete Cache on Next Start", false, "Nuclear option. If true, deletes the cache on the next start of H3VR. The cache will be built from scratch. This takes the most time and the most RAM to finish. This setting will always be set back to false after startup.");
			EnableLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "EnableLogging", true, "Set to true to enable logging");
			LogDebugInfo = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "LogDebugInfo", false, "If true, logs extra debugging info");
			((BaseUnityPlugin)this).Config.SettingChanged += ConfigSettingChanged;
			PatchLogger.AllowLogging = EnableLogging.Value;
			PatchLogger.LogDebug = LogDebugInfo.Value;
		}

		private void ConfigSettingChanged(object sender, SettingChangedEventArgs e)
		{
			if (e.ChangedSetting == ResetBasicCacheOnNextStart)
			{
				if (ResetBasicCacheOnNextStart.Value)
				{
					ResetXLCacheOnNextStart.Value = false;
					DeleteCacheOnNextStart.Value = false;
				}
			}
			else if (e.ChangedSetting == ResetXLCacheOnNextStart)
			{
				if (ResetXLCacheOnNextStart.Value)
				{
					ResetBasicCacheOnNextStart.Value = false;
					DeleteCacheOnNextStart.Value = false;
				}
			}
			else if (e.ChangedSetting == DeleteCacheOnNextStart && DeleteCacheOnNextStart.Value)
			{
				ResetBasicCacheOnNextStart.Value = false;
				ResetXLCacheOnNextStart.Value = false;
			}
		}

		public override void OnSetup(IStageContext<Empty> ctx)
		{
		}

		public override IEnumerator OnRuntime(IStageContext<IEnumerator> ctx)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <OnRuntime>d__16(0)
			{
				<>4__this = this
			};
		}

		private static Dictionary<string, MagazineBlacklistEntry> GetMagazineCacheBlacklist()
		{
			try
			{
				if (string.IsNullOrEmpty(BlacklistPath) || !File.Exists(BlacklistPath))
				{
					PatchLogger.LogError("Failed to load magazine blacklist! Creating new one!");
					return CreateNewBlacklist();
				}
				string text = File.ReadAllText(BlacklistPath);
				if (string.IsNullOrEmpty(text))
				{
					return CreateNewBlacklist();
				}
				List<MagazineBlacklistEntry> list = JsonConvert.DeserializeObject<List<MagazineBlacklistEntry>>(text);
				Dictionary<string, MagazineBlacklistEntry> dictionary = new Dictionary<string, MagazineBlacklistEntry>();
				foreach (MagazineBlacklistEntry item in list)
				{
					dictionary.Add(item.FirearmID, item);
				}
				return dictionary;
			}
			catch (Exception ex)
			{
				PatchLogger.LogError("Failed to load magazine blacklist! Creating new one! Stack trace below:");
				PatchLogger.LogError(ex.ToString());
				return CreateNewBlacklist();
			}
		}

		private static Dictionary<string, MagazineBlacklistEntry> CreateNewBlacklist()
		{
			PatchLogger.Log("Blacklist does not exist! Building new one", PatchLogger.LogType.General);
			Dictionary<string, MagazineBlacklistEntry> dictionary = new Dictionary<string, MagazineBlacklistEntry>();
			StreamWriter streamWriter = File.CreateText(BlacklistPath);
			List<MagazineBlacklistEntry> list = new List<MagazineBlacklistEntry>();
			MagazineBlacklistEntry magazineBlacklistEntry = new MagazineBlacklistEntry
			{
				FirearmID = "SKSClassic"
			};
			magazineBlacklistEntry.MagazineWhitelist.Add("None");
			list.Add(magazineBlacklistEntry);
			string value = JsonConvert.SerializeObject((object)list, (Formatting)1);
			streamWriter.WriteLine(value);
			streamWriter.Close();
			foreach (MagazineBlacklistEntry item in list)
			{
				dictionary.Add(item.FirearmID, item);
			}
			return dictionary;
		}

		private static void PokeOtherLoader()
		{
			LoaderStatus.GetLoaderProgress();
		}

		private static float GetOtherLoaderProgress()
		{
			return LoaderStatus.GetLoaderProgress();
		}

		private static IEnumerator LoadMagazineCacheAsync()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LoadMagazineCacheAsync>d__21(0);
		}

		private static bool CheckBackupCache()
		{
			bool value = ResetBasicCacheOnNextStart.Value;
			bool flag = !value && ResetXLCacheOnNextStart.Value;
			bool num = !value && !flag && DeleteCacheOnNextStart.Value;
			ResetBasicCacheOnNextStart.Value = false;
			ResetXLCacheOnNextStart.Value = false;
			DeleteCacheOnNextStart.Value = false;
			if (num)
			{
				PatchLogger.Log("Deleted cache and starting from scratch!", PatchLogger.LogType.General);
				if (File.Exists(FullCachePath))
				{
					File.Delete(FullCachePath);
				}
				return true;
			}
			string text = (flag ? XLCachePath : BasicCachePath);
			if (!File.Exists(text))
			{
				PatchLogger.Log("Starting cache is missing: " + text + "!", PatchLogger.LogType.General);
				return false;
			}
			if (!File.Exists(FullCachePath) || value || flag)
			{
				File.Copy(text, FullCachePath, overwrite: true);
				PatchLogger.Log("Starting cache restored from " + text + "!", PatchLogger.LogType.General);
				return true;
			}
			return false;
		}

		private static bool LoadFullCache()
		{
			bool result = false;
			CheckBackupCache();
			if (!string.IsNullOrEmpty(FullCachePath) && File.Exists(FullCachePath))
			{
				try
				{
					CompatibleMagazineCache.Instance = JsonConvert.DeserializeObject<CompatibleMagazineCache>(File.ReadAllText(FullCachePath));
					result = IsMagazineCacheValid(CompatibleMagazineCache.Instance);
					PatchLogger.Log("Cache file found! Is Valid? " + result, PatchLogger.LogType.General);
				}
				catch (Exception ex)
				{
					CompatibleMagazineCache.Instance = new CompatibleMagazineCache();
					PatchLogger.LogError("Failed to read cache file!");
					PatchLogger.LogError(ex.ToString());
					File.Delete(FullCachePath);
				}
			}
			else
			{
				PatchLogger.Log("Cache file not found! Creating new cache file", PatchLogger.LogType.General);
				CompatibleMagazineCache.Instance = new CompatibleMagazineCache();
			}
			return result;
		}

		public static bool ValidFireArm(FireArmRoundType roundType, FireArmClipType clipType, FireArmMagazineType magazineType, int magazineCapacity)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Invalid comparison between Unknown and I4
			if ((int)roundType == 0 && (int)magazineType == 0 && magazineCapacity == 0)
			{
				return (int)clipType > 0;
			}
			return true;
		}

		private static void ApplyMagazineCache(CompatibleMagazineCache magazineCache)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
			foreach (KeyValuePair<FireArmMagazineType, List<AmmoObjectDataTemplate>> magazineDatum in CompatibleMagazineCache.Instance.MagazineData)
			{
				if (!IM.CompatMags.ContainsKey(magazineDatum.Key))
				{
					IM.CompatMags.Add(magazineDatum.Key, new List<FVRObject>());
				}
				List<FVRObject> list = new List<FVRObject>();
				foreach (AmmoObjectDataTemplate item in magazineDatum.Value)
				{
					if (IM.OD.ContainsKey(item.ObjectID))
					{
						FVRObject val = IM.OD[item.ObjectID];
						val.MagazineType = magazineDatum.Key;
						val.RoundType = item.RoundType;
						val.MagazineCapacity = item.Capacity;
						list.Add(val);
					}
				}
				IM.CompatMags[magazineDatum.Key] = list;
			}
			foreach (MagazineCacheEntry value in magazineCache.Entries.Values)
			{
				if (!IM.OD.ContainsKey(value.FirearmID))
				{
					continue;
				}
				FVRObject val2 = IM.OD[value.FirearmID];
				if (IM.CompatMags.ContainsKey(value.MagType))
				{
					val2.MagazineType = value.MagType;
				}
				val2.RoundType = value.BulletType;
				val2.ClipType = value.ClipType;
				LastTouchedItem = value.FirearmID;
				int num = -1;
				int num2 = -1;
				if ((int)value.MagType != 0)
				{
					foreach (string mag in value.CompatibleMagazines)
					{
						if (IM.OD.ContainsKey(mag) && !val2.CompatibleMagazines.Any((FVRObject o) => (Object)(object)o != (Object)null && o.ItemID == mag))
						{
							FVRObject val3 = IM.OD[mag];
							val2.CompatibleMagazines.Add(val3);
							if (magazineCache.AmmoObjects.ContainsKey(mag))
							{
								val3.MagazineCapacity = magazineCache.AmmoObjects[mag].Capacity;
							}
							if (num < val3.MagazineCapacity)
							{
								num = val3.MagazineCapacity;
							}
							if (num2 == -1)
							{
								num2 = val3.MagazineCapacity;
							}
							else if (num2 > val3.MagazineCapacity)
							{
								num2 = val3.MagazineCapacity;
							}
						}
					}
				}
				if ((int)value.ClipType != 0)
				{
					foreach (string clip in value.CompatibleClips)
					{
						if (IM.OD.ContainsKey(clip) && !val2.CompatibleClips.Any((FVRObject o) => (Object)(object)o != (Object)null && o.ItemID == clip))
						{
							FVRObject val4 = IM.OD[clip];
							val2.CompatibleClips.Add(val4);
							if (magazineCache.AmmoObjects.ContainsKey(clip))
							{
								val4.MagazineCapacity = magazineCache.AmmoObjects[clip].Capacity;
							}
							if (num < val4.MagazineCapacity)
							{
								num = val4.MagazineCapacity;
							}
							if (num2 == -1)
							{
								num2 = val4.MagazineCapacity;
							}
							else if (num2 > val4.MagazineCapacity)
							{
								num2 = val4.MagazineCapacity;
							}
						}
					}
				}
				foreach (string speedloader in value.CompatibleSpeedLoaders)
				{
					if (IM.OD.ContainsKey(speedloader) && !val2.CompatibleSpeedLoaders.Any((FVRObject o) => (Object)(object)o != (Object)null && o.ItemID == speedloader))
					{
						FVRObject val5 = IM.OD[speedloader];
						val2.CompatibleSpeedLoaders.Add(val5);
						if (magazineCache.AmmoObjects.ContainsKey(speedloader))
						{
							val5.MagazineCapacity = magazineCache.AmmoObjects[speedloader].Capacity;
						}
						if (num < val5.MagazineCapacity)
						{
							num = val5.MagazineCapacity;
						}
						if (num2 == -1)
						{
							num2 = val5.MagazineCapacity;
						}
						else if (num2 > val5.MagazineCapacity)
						{
							num2 = val5.MagazineCapacity;
						}
					}
				}
				foreach (string bullet in value.CompatibleBullets)
				{
					if (IM.OD.ContainsKey(bullet) && !val2.CompatibleSingleRounds.Any((FVRObject o) => (Object)(object)o != (Object)null && o.ItemID == bullet))
					{
						val2.CompatibleSingleRounds.Add(IM.OD[bullet]);
					}
				}
				if (num != -1)
				{
					val2.MaxCapacityRelated = num;
				}
				if (num2 != -1)
				{
					val2.MinCapacityRelated = num2;
				}
			}
		}

		private static void RemoveBlacklistedMagazines(Dictionary<string, MagazineBlacklistEntry> blacklist)
		{
			foreach (FVRObject item in ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)1])
			{
				if (!blacklist.ContainsKey(item.ItemID))
				{
					continue;
				}
				for (int num = item.CompatibleMagazines.Count - 1; num >= 0; num--)
				{
					if (!blacklist[item.ItemID].IsMagazineAllowed(item.CompatibleMagazines[num].ItemID))
					{
						item.CompatibleMagazines.RemoveAt(num);
					}
				}
				for (int num2 = item.CompatibleClips.Count - 1; num2 >= 0; num2--)
				{
					if (!blacklist[item.ItemID].IsClipAllowed(item.CompatibleClips[num2].ItemID))
					{
						item.CompatibleClips.RemoveAt(num2);
					}
				}
				for (int num3 = item.CompatibleSingleRounds.Count - 1; num3 >= 0; num3--)
				{
					if (!blacklist[item.ItemID].IsRoundAllowed(item.CompatibleSingleRounds[num3].ItemID))
					{
						item.CompatibleSingleRounds.RemoveAt(num3);
					}
				}
				for (int num4 = item.CompatibleSpeedLoaders.Count - 1; num4 >= 0; num4--)
				{
					if (!blacklist[item.ItemID].IsSpeedloaderAllowed(item.CompatibleSpeedLoaders[num4].ItemID))
					{
						item.CompatibleSpeedLoaders.RemoveAt(num4);
					}
				}
			}
		}

		private static bool IsMagazineCacheValid(CompatibleMagazineCache magazineCache)
		{
			bool flag = true;
			foreach (string item in ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)2].Select((FVRObject f) => f.ItemID))
			{
				if (!magazineCache.Magazines.Contains(item) && flag && (Object)(object)((AnvilAsset)IM.OD[item]).GetGameObject() != (Object)null)
				{
					PatchLogger.LogWarning("Magazine not found in cache: " + item);
					flag = false;
				}
			}
			foreach (string item2 in ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)1].Select((FVRObject f) => f.ItemID))
			{
				if (!magazineCache.Firearms.Contains(item2) && flag && (Object)(object)((AnvilAsset)IM.OD[item2]).GetGameObject() != (Object)null)
				{
					PatchLogger.LogWarning("Firearm not found in cache: " + item2);
					flag = false;
				}
			}
			foreach (string item3 in ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)3].Select((FVRObject f) => f.ItemID))
			{
				if (!magazineCache.Clips.Contains(item3) && flag && (Object)(object)((AnvilAsset)IM.OD[item3]).GetGameObject() != (Object)null)
				{
					PatchLogger.LogWarning("Clip not found in cache: " + item3);
					flag = false;
				}
			}
			foreach (string item4 in ManagerSingleton<IM>.Instance.odicTagCategory[(ObjectCategory)4].Select((FVRObject f) => f.ItemID))
			{
				if (!magazineCache.Bullets.Contains(item4) && flag && (Object)(object)((AnvilAsset)IM.OD[item4]).GetGameObject() != (Object)null)
				{
					PatchLogger.LogWarning("Bullet not found in cache: " + item4);
					flag = false;
				}
			}
			return flag;
		}

		public static IEnumerator RunAndCatch(IEnumerator routine, Action<Exception> onError = null)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <RunAndCatch>d__28(0)
			{
				routine = routine,
				onError = onError
			};
		}
	}
	public static class PatcherStatus
	{
		public static string CacheLog = "";

		private static float patcherProgress = 0f;

		public static bool CachingFailed = false;

		private static List<string> CacheLogList = new List<string>();

		public static float PatcherProgress => patcherProgress;

		public static void UpdateProgress(float progress)
		{
			patcherProgress = progress;
		}

		public static void AppendCacheLog(string log)
		{
			CacheLogList.Add(log);
			if (CacheLogList.Count > 6)
			{
				CacheLogList.RemoveAt(0);
			}
			CacheLog = string.Join("\n", CacheLogList.ToArray());
		}
	}
}