Decompiled source of 6FingerReload v0.1.0

NGA.6FingerReload.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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 UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyCompany("NGA")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Persistent player progression! Raid, stash loot, and deploy with seemless scene/loadout saving.")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("NGA.6FingerReload")]
[assembly: AssemblyTitle("BepInEx Plugin Title")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[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 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 NGA
{
	[BepInPlugin("NGA.6FingerReload", "6FingerReload", "0.1.0")]
	[BepInDependency("nrgill28.Sodalite", "1.4.1")]
	[BepInProcess("h3vr.exe")]
	public class SixFingerReload : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(FVRFireArmReloadTriggerMag))]
		[HarmonyPatch("OnTriggerEnter")]
		public class FVRFireArmReloadTriggerMagOnTriggerEnter : MonoBehaviour
		{
			private static void Prefix(FVRFireArmReloadTriggerMag __instance, Collider collider)
			{
				if (config_enable.Value && (Object)(object)__instance.Magazine != (Object)null && (Object)(object)__instance.Magazine.FireArm == (Object)null && (Object)(object)((FVRPhysicalObject)__instance.Magazine).QuickbeltSlot == (Object)null && ((Component)collider).gameObject.tag == "FVRFireArmReloadTriggerWell" && (Object)(object)((Component)__instance).gameObject.GetComponent<MagOnRailsTriggerVol>() != (Object)null)
				{
					detectedMag = __instance.Magazine;
					timeMagDetected = Time.time;
					Logger.LogWarning((object)("Detected magwell " + ((Object)((Component)collider).gameObject).name + " at " + timeMagDetected));
				}
			}
		}

		[HarmonyPatch(typeof(FVRFireArmMagazine))]
		[HarmonyPatch("Load")]
		[HarmonyPatch(new Type[] { typeof(FVRFireArm) })]
		public class FVRFireArmMagazineLoad : MonoBehaviour
		{
			private static bool Prefix(FVRFireArmMagazine __instance, FVRFireArm fireArm)
			{
				if (!config_enable.Value)
				{
					return true;
				}
				__instance.m_isVizLerping = false;
				__instance.UsesVizInterp = false;
				float time = Time.time;
				if ((Object)(object)__instance == (Object)(object)detectedMag && Mathf.Abs(timeMagDetected - time) <= magCompareThres.Value)
				{
					acceptedRailedMag = __instance;
					detectedMag = null;
					Logger.LogWarning((object)("Ignored load and accepted mag for " + ((Object)((Component)__instance).gameObject).name + " into " + ((Object)((Component)fireArm).gameObject).name));
					return false;
				}
				Logger.LogWarning((object)("Doing load for " + ((Object)((Component)__instance).gameObject).name + " into " + ((Object)((Component)fireArm).gameObject).name + " comparing " + timeMagDetected + " - " + time + " = " + Mathf.Abs(timeMagDetected - time) + " <= " + magCompareThres.Value));
				return true;
			}

			private static void Postfix(FVRFireArmMagazine __instance, FVRFireArm fireArm)
			{
				if (config_enable.Value)
				{
					__instance.m_isVizLerping = false;
					__instance.UsesVizInterp = false;
				}
			}
		}

		[HarmonyPatch(typeof(FVRFireArmMagazine))]
		[HarmonyPatch("FVRFixedUpdate")]
		private class FVRFireArmMagazineFVRFixedUpdate
		{
			private static void Prefix(FVRFireArmMagazine __instance)
			{
				if (config_enable.Value)
				{
					__instance.m_isVizLerping = false;
					__instance.UsesVizInterp = false;
				}
			}

			private static void Postfix(FVRFireArmMagazine __instance)
			{
				//IL_0079: Unknown result type (might be due to invalid IL or missing references)
				//IL_008b: Unknown result type (might be due to invalid IL or missing references)
				//IL_009d: Unknown result type (might be due to invalid IL or missing references)
				//IL_00af: Unknown result type (might be due to invalid IL or missing references)
				if (!config_enable.Value)
				{
					return;
				}
				if ((Object)(object)__instance.FireArm != (Object)null)
				{
					FVRFireArm fireArm = __instance.FireArm;
					Transform magMountPos = fireArm.GetMagMountPos(__instance.IsBeltBox);
					if ((Object)(object)magMountPos != (Object)null && (Object)(object)fireArm.Magazine == (Object)(object)__instance && (Object)(object)__instance.Viz != (Object)null)
					{
						((Component)__instance).transform.rotation = magMountPos.rotation;
						((Component)__instance).transform.position = magMountPos.position;
						__instance.Viz.position = magMountPos.position;
						__instance.Viz.rotation = magMountPos.rotation;
					}
				}
				__instance.m_isVizLerping = false;
				__instance.UsesVizInterp = false;
			}
		}

		[HarmonyPatch(typeof(FVRFireArmMagazine))]
		[HarmonyPatch("Awake")]
		public class FVRFireArmMagazineAwake : MonoBehaviour
		{
			private static void Prefix(FVRFireArmMagazine __instance)
			{
				if (!config_enable.Value)
				{
					return;
				}
				if (forbiddenNames.Contains(((Object)((Component)__instance).gameObject).name))
				{
					Logger.LogWarning((object)("Skipped 6FingerReload on forbidden mag: " + ((Object)((Component)__instance).gameObject).name));
					return;
				}
				Transform val = FindChildWithFVRFireArmReloadTriggerMag(((Component)__instance).gameObject);
				if ((Object)(object)val == (Object)null)
				{
					Logger.LogError((object)("No child in mag " + ((Object)((Component)__instance).gameObject).name + " with FVRFireArmReloadTriggerMag"));
					return;
				}
				MagOnRailsTriggerVol magOnRailsTriggerVol = ((Component)val).gameObject.AddComponent<MagOnRailsTriggerVol>();
				magOnRailsTriggerVol.SelfMag = __instance;
				Logger.LogWarning((object)("Added railVol to " + ((Object)((Component)__instance).gameObject).name));
			}
		}

		public class MagOnRailsTriggerVol : MonoBehaviour
		{
			public bool isInTriggerWell = false;

			private FVRFireArmReloadTriggerWell wellCompt = null;

			public FVRFireArmMagazine SelfMag;

			public void OnTriggerStay(Collider other)
			{
				if (!((Object)(object)SelfMag == (Object)null) && !(((Component)other).gameObject.tag != "FVRFireArmReloadTriggerWell") && !((Object)(object)SelfMag != (Object)(object)acceptedRailedMag))
				{
					isInTriggerWell = true;
					wellCompt = ((Component)other).gameObject.GetComponent<FVRFireArmReloadTriggerWell>();
				}
			}

			public void OnTriggerExit(Collider other)
			{
				if (((Component)other).gameObject.tag == "FVRFireArmReloadTriggerWell" && (Object)(object)SelfMag == (Object)(object)acceptedRailedMag)
				{
					Logger.LogWarning((object)("Mag exited TriggerWell " + ((Object)((Component)SelfMag).gameObject).name));
					isInTriggerWell = false;
				}
			}

			public void Update()
			{
				//IL_0048: Unknown result type (might be due to invalid IL or missing references)
				//IL_004d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0059: Unknown result type (might be due to invalid IL or missing references)
				//IL_005f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0064: Unknown result type (might be due to invalid IL or missing references)
				//IL_0069: Unknown result type (might be due to invalid IL or missing references)
				//IL_006a: Unknown result type (might be due to invalid IL or missing references)
				//IL_006b: 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_0071: Unknown result type (might be due to invalid IL or missing references)
				//IL_019a: Unknown result type (might be due to invalid IL or missing references)
				//IL_019f: Unknown result type (might be due to invalid IL or missing references)
				//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
				//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
				//IL_01ac: 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_01cd: Unknown result type (might be due to invalid IL or missing references)
				//IL_0246: Unknown result type (might be due to invalid IL or missing references)
				//IL_025c: 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_013c: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)acceptedRailedMag == (Object)null || (Object)(object)wellCompt == (Object)null)
				{
					return;
				}
				FVRFireArm fireArm = wellCompt.FireArm;
				Transform magMountPos = fireArm.GetMagMountPos(SelfMag.IsBeltBox);
				Vector3 up = magMountPos.up;
				Vector3 val = ((Component)SelfMag).transform.position - magMountPos.position;
				Vector3 val2 = Vector3.ProjectOnPlane(val, up);
				float magnitude = ((Vector3)(ref val2)).magnitude;
				float num = DistanceAlongUpAxis(((Component)SelfMag.Viz).transform, magMountPos);
				if (!isInTriggerWell && (magnitude > hoz_railUnlock_dist.Value || num > ver_railUnlock_dist.Value))
				{
					Logger.LogWarning((object)("Disconnected mag too far away: upDist_" + num + " hozDist_" + magnitude));
					wellCompt = null;
					acceptedRailedMag = null;
					SelfMag.Viz.position = ((Component)SelfMag).transform.position;
					SelfMag.Viz.rotation = ((Component)SelfMag).transform.rotation;
					((FVRInteractiveObject)SelfMag).SetAllCollidersToLayer(false, "Default");
					SelfMag.IsNonPhysForLoad = false;
					Logger.LogWarning((object)"Nulled acceptedRailedMag & reset vis pos");
					return;
				}
				SelfMag.IsNonPhysForLoad = true;
				((FVRInteractiveObject)SelfMag).SetAllCollidersToLayer(false, "NoCol");
				Vector3 position = magMountPos.position - up * num;
				SelfMag.Viz.position = position;
				SelfMag.Viz.rotation = magMountPos.rotation;
				if (num <= 0f - mag_trigger_dist.Value)
				{
					Logger.LogWarning((object)("Met conditions for loading: upDist " + num + " of " + ((Object)((Component)acceptedRailedMag).gameObject).name));
					((Component)SelfMag.Viz).gameObject.SetActive(false);
					SelfMag.Viz.localPosition = Vector3.zero;
					SelfMag.Viz.localRotation = Quaternion.identity;
					SelfMag.Load(fireArm);
					((Component)SelfMag.Viz).gameObject.SetActive(true);
					((FVRInteractiveObject)SelfMag).SetAllCollidersToLayer(false, "Default");
					SelfMag.IsNonPhysForLoad = false;
					wellCompt = null;
					acceptedRailedMag = null;
					Logger.LogWarning((object)"Nulled acceptedRailedMag");
				}
			}

			private static float DistanceAlongUpAxis(Transform mag, Transform hole)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_000d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_0014: 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_001b: Unknown result type (might be due to invalid IL or missing references)
				Vector3 val = hole.position - mag.position;
				Vector3 up = hole.up;
				return Vector3.Dot(val, up);
			}
		}

		[HarmonyPatch(typeof(FVRFireArm))]
		[HarmonyPatch("Awake")]
		public class FVRFireArmAwake : MonoBehaviour
		{
			private static void Prefix(FVRFireArm __instance)
			{
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_003e: Unknown result type (might be due to invalid IL or missing references)
				if (config_enable.Value)
				{
					Transform val = FindChildWithReloadTriggerWell(((Component)__instance).gameObject);
					if ((Object)(object)val != (Object)null)
					{
						val.localScale *= magwellTriggerScale.Value;
					}
				}
			}

			public static Transform FindChildWithReloadTriggerWell(GameObject parent)
			{
				//IL_0016: Unknown result type (might be due to invalid IL or missing references)
				//IL_001c: Expected O, but got Unknown
				foreach (Transform item in parent.transform)
				{
					Transform val = item;
					if ((Object)(object)((Component)val).GetComponent<FVRFireArmReloadTriggerWell>() != (Object)null)
					{
						return val;
					}
				}
				return null;
			}
		}

		private static ConfigEntry<bool> config_enable;

		private static ConfigEntry<string> comma_forbids;

		public static ConfigEntry<float> magCompareThres;

		private static ConfigEntry<float> hoz_railUnlock_dist;

		private static ConfigEntry<float> ver_railUnlock_dist;

		private static ConfigEntry<float> mag_trigger_dist;

		private static ConfigEntry<float> magwellTriggerScale;

		public static FVRFireArmMagazine detectedMag;

		public static float timeMagDetected;

		public static FVRFireArmMagazine acceptedRailedMag;

		private static HashSet<string> forbiddenNames = new HashSet<string>
		{
			"P90_Mag(Clone)", "LaserPistol_Magazine(Clone)", "HandCrankFrank_Magazine(Clone)", "Mk19_BeltBox(Clone)", "BerthierCarbine_Magazine(Clone)", "Carcano1891_Magazine(Clone)", "M2Tombstone_BeltBox(Clone)", "KWG_Magazine(Clone)", "SustenanceCrossbow_Battery(Clone)", "Minigun_Box(Clone)",
			"S9R Derringer_Mag(Clone)", "G11_Mag(Clone)"
		};

		internal static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("NGA.6FingerReload");
			Logger.LogMessage((object)"New NGA.6FingerReload harmony");
			SetUpConfigFields();
			Logger.LogMessage((object)"Setted the fields");
			UpdateForbidden();
			Logger.LogMessage((object)"Updated forbidden guns.");
			val.PatchAll();
			Logger.LogMessage((object)"Hello, world! Sent from NGA.6FingerReload");
		}

		private void SetUpConfigFields()
		{
			config_enable = ((BaseUnityPlugin)this).Config.Bind<bool>("Enabled", "ON/OFF", true, "Enable or disable the mod now.");
			comma_forbids = ((BaseUnityPlugin)this).Config.Bind<string>("Excluded Items", "List to Forbid", "", "Use toolbox object info tool to find name of magazine, Comma separated, add (Clone) postfix. Needs game restart. [Mod has builtin excluded guns not shown here.]");
			magCompareThres = ((BaseUnityPlugin)this).Config.Bind<float>("INTERNAL", "Compare Mag Thres", 0.01f, "When to ignore Load magazine call.");
			hoz_railUnlock_dist = ((BaseUnityPlugin)this).Config.Bind<float>("INTERNAL", "Horizontal Dist cancel anim", 0.15f, "(Meters) How far horizontally to cancel unbind mag from anim.");
			ver_railUnlock_dist = ((BaseUnityPlugin)this).Config.Bind<float>("INTERNAL", "Vertical Dist cancel anim", 0.1f, "(Meters) How far vertically to cancel unbind mag from anim.");
			mag_trigger_dist = ((BaseUnityPlugin)this).Config.Bind<float>("INTERNAL", "Autoload dist", 0.001f, "(Meters) How close mag has to be to its fully inserted positino to load");
			magwellTriggerScale = ((BaseUnityPlugin)this).Config.Bind<float>("INTERNAL", "Magwell Scale", 0.8f, "Multiplier for how big trigger volume of guns magwell is");
		}

		private void UpdateForbidden()
		{
			string[] array = comma_forbids.Value.Split(new char[1] { ',' });
			string[] array2 = array;
			foreach (string text in array2)
			{
				forbiddenNames.Add(text.Trim());
			}
			foreach (string forbiddenName in forbiddenNames)
			{
				Logger.LogMessage((object)forbiddenName);
			}
		}

		public static Transform FindChildWithFVRFireArmReloadTriggerMag(GameObject parent)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			foreach (Transform item in parent.transform)
			{
				Transform val = item;
				if ((Object)(object)((Component)val).GetComponent<FVRFireArmReloadTriggerMag>() != (Object)null)
				{
					return val;
				}
			}
			return null;
		}
	}
}