Decompiled source of Better MagFed Shotgun Pump v1.1.0

Sirdoggy.TweakMagFedShotgunLoad.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FistVR;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Sirdoggy.TweakMagFedShotgunLoad.Common;
using Sodalite.ModPanel;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("Sirdoggy2")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Makes shells get picked up from mags regardless of when the pump was locked back")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("Sirdoggy.TweakMagFedShotgunLoad")]
[assembly: AssemblyTitle("Better_MagFed_Shotgun_Pump")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string id = null, string name = null, string version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string id = null, string name = null, string version = null)
		{
		}
	}
}
namespace Sirdoggy.TweakMagFedShotgunLoad
{
	[BepInProcess("h3vr.exe")]
	[BepInPlugin("Sirdoggy.TweakMagFedShotgunLoad", "Better_MagFed_Shotgun_Pump", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ExceptionCatcher ExceptionCatcher;

		public const string Id = "Sirdoggy.TweakMagFedShotgunLoad";

		public static string Name => "Better_MagFed_Shotgun_Pump";

		public static string Version => "1.1.0";

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			ExceptionCatcher = new ExceptionCatcher(Log);
			PluginConfig.Initialize(((BaseUnityPlugin)this).Info);
			ExceptionCatcher.Run(delegate
			{
				Harmony.CreateAndPatchAll(typeof(BoltPatcher), (string)null);
			});
			ExceptionCatcher.Run(delegate
			{
				Harmony.CreateAndPatchAll(typeof(TubeFedShotgunMagGrabTriggerPatcher), (string)null);
			});
		}
	}
	[HarmonyPatch(typeof(TubeFedShotgunBolt))]
	internal static class BoltPatcher
	{
		[HarmonyWrapSafe]
		[HarmonyPrefix]
		[HarmonyPatch("UpdateBolt")]
		private static void UpdateBolt_Prefix(TubeFedShotgunBolt __instance, ref BoltPos? __state)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)__instance == (Object)null))
			{
				__state = __instance.CurPos;
			}
		}

		[HarmonyWrapSafe]
		[HarmonyPostfix]
		[HarmonyPatch("UpdateBolt")]
		private static void UpdateBolt_Postfix(TubeFedShotgunBolt __instance, ref BoltPos? __state)
		{
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || !__state.HasValue)
			{
				return;
			}
			TubeFedShotgun shotgun = __instance.Shotgun;
			if ((Object)(object)shotgun == (Object)null)
			{
				return;
			}
			FVRFireArmMagazine magazine = ((FVRFireArm)shotgun).Magazine;
			FVRFireArmChamber chamber = shotgun.Chamber;
			if ((Object)(object)magazine == (Object)null || (Object)(object)chamber == (Object)null || magazine.IsIntegrated)
			{
				return;
			}
			BoltPos? prevPos = __state;
			BoltPos curPos = __instance.CurPos;
			Plugin.ExceptionCatcher.Run(delegate
			{
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0010: 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_002f: Unknown result type (might be due to invalid IL or missing references)
				if (prevPos >= 2 && curPos < prevPos && magazine.HasARound() && !shotgun.HasExtractedRound() && !chamber.IsFull)
				{
					ExtractRoundOntoUpperTrackImmediately(shotgun);
				}
			});
		}

		[HarmonyWrapSafe]
		[HarmonyPrefix]
		[HarmonyPatch("BoltEvent_ExtractRoundFromMag")]
		private static bool BoltEvent_ExtractRoundFromMag_Prefix(TubeFedShotgunBolt __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return true;
			}
			TubeFedShotgun shotgun = __instance.Shotgun;
			if ((Object)(object)shotgun == (Object)null)
			{
				return true;
			}
			FVRFireArmMagazine magazine = ((FVRFireArm)shotgun).Magazine;
			FVRFireArmChamber chamber = shotgun.Chamber;
			if ((Object)(object)magazine == (Object)null || (Object)(object)chamber == (Object)null || magazine.IsIntegrated)
			{
				return true;
			}
			return false;
		}

		[HarmonyWrapSafe]
		[HarmonyPostfix]
		[HarmonyPatch("BoltEvent_EjectRound")]
		private static void BoltEvent_EjectRound_Postfix(TubeFedShotgunBolt __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			TubeFedShotgun shotgun = __instance.Shotgun;
			if (!((Object)(object)shotgun == (Object)null))
			{
				FVRFireArmMagazine magazine = ((FVRFireArm)shotgun).Magazine;
				FVRFireArmChamber chamber = shotgun.Chamber;
				if (!((Object)(object)magazine == (Object)null) && !((Object)(object)chamber == (Object)null) && !magazine.IsIntegrated && !PluginConfig.PreventRoundPickupOnPumpHitRear.Value)
				{
					ExtractRoundOntoUpperTrackImmediately(shotgun);
				}
			}
		}

		private static void ExtractRoundOntoUpperTrackImmediately(TubeFedShotgun shotgun)
		{
			shotgun.ExtractRound();
			shotgun.TransferShellToUpperTrack();
		}
	}
	[HarmonyPatch(typeof(TubeFedShotgunMagGrabTrigger))]
	internal static class TubeFedShotgunMagGrabTriggerPatcher
	{
		[HarmonyWrapSafe]
		[HarmonyPrefix]
		[HarmonyPatch("IsInteractable")]
		private static bool IsInteractable_Prefix(TubeFedShotgunMagGrabTrigger __instance, ref bool __result)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return true;
			}
			TubeFedShotgun shotgun = __instance.Shotgun;
			if ((Object)(object)shotgun == (Object)null)
			{
				return true;
			}
			FVRFireArmMagazine magazine = ((FVRFireArm)shotgun).Magazine;
			if ((Object)(object)magazine == (Object)null || magazine.IsIntegrated)
			{
				return true;
			}
			__result = true;
			return false;
		}
	}
	internal static class PluginConfig
	{
		public static ConfigEntry<bool> PreventRoundPickupOnPumpHitRear;

		private const string ConfigFileName = "Sirdoggy.Better_MagFed_Shotgun_Pump.cfg";

		private static ConfigFile _configFile;

		public static void Initialize(PluginInfo pluginInfo)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			_configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "Sirdoggy.Better_MagFed_Shotgun_Pump.cfg"), true);
			BindFields();
			UniversalModPanel.RegisterPluginSettings(pluginInfo, _configFile);
		}

		private static void BindFields()
		{
			PreventRoundPickupOnPumpHitRear = _configFile.Bind<bool>("General", "Prevent round pickup when pump hits rear", true, "In vanilla, when using a pump-action shotgun with a detachable mag, whenever the pump hits the rear a round will be immediately extracted from the mag and placed onto the carrier. This setting prevents that, which is more realistic, but can look a bit weird with some guns.");
		}
	}
}
namespace Sirdoggy.TweakMagFedShotgunLoad.Common
{
	internal class ExceptionCatcher
	{
		private readonly ManualLogSource _log;

		public ExceptionCatcher(ManualLogSource log)
		{
			_log = log;
		}

		public void Run(Action action, Action<Exception>? customHandler = null)
		{
			try
			{
				action();
			}
			catch (Exception ex)
			{
				if (customHandler == null)
				{
					_log.LogError((object)$"Caught unhandled exception: {ex}");
				}
				else
				{
					customHandler(ex);
				}
			}
		}
	}
}