Decompiled source of Smooth Manual Mag Insertion v1.0.0

SmoothMags.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FistVR;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("SmoothMags")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SmoothMags")]
[assembly: AssemblyTitle("SmoothMags")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SmoothMags;

[BepInPlugin("motomo.smmi", "Smooth Manual Mag Insertion (SMMI)", "1.0.0")]
public class SmoothMagsPlugin : BaseUnityPlugin
{
	public const string Guid = "motomo.smmi";

	public const string Name = "Smooth Manual Mag Insertion (SMMI)";

	public const string Version = "1.0.0";

	public static ManualLogSource Log;

	public static ConfigEntry<int> SnapDepthPercent;

	public static bool AllowLoad;

	private void Awake()
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Expected O, but got Unknown
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		SnapDepthPercent = ((BaseUnityPlugin)this).Config.Bind<int>("General", "SnapDepthPercent", 100, new ConfigDescription("How far the mag slides into the gun before snapping into place, in percent. 10 = 10% of the way in, 100 = fully seated.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(10, 100), new object[0]));
		new Harmony("motomo.smmi").PatchAll();
		Log.LogInfo((object)"Smooth Manual Mag Insertion (SMMI) 1.0.0 loaded!");
	}
}
[HarmonyPatch(typeof(FVRFireArmMagazine), "Load", new Type[] { typeof(FVRFireArm) })]
internal static class MagLoadPatch
{
	private static bool Prefix(FVRFireArmMagazine __instance, FVRFireArm fireArm)
	{
		if (SmoothMagsPlugin.AllowLoad)
		{
			return true;
		}
		if (!(fireArm is Handgun))
		{
			return true;
		}
		if (!((FVRInteractiveObject)__instance).IsHeld)
		{
			return true;
		}
		if ((Object)(object)fireArm.MagazineMountPos == (Object)null)
		{
			return true;
		}
		if ((Object)(object)((Component)__instance).GetComponent<MagSlider>() != (Object)null)
		{
			return false;
		}
		if (Time.time < MagSlider.CooldownUntil)
		{
			return false;
		}
		MagSlider.Begin(__instance, fireArm);
		return false;
	}
}
public class MagSlider : MonoBehaviour
{
	public static float CooldownUntil;

	private FVRFireArmMagazine mag;

	private FVRFireArm gun;

	private Rigidbody rb;

	private bool rbWasKinematic;

	private bool rbDetectCollisions;

	private float startT;

	private float handStartProj;

	private const float MaxLength = 0.12f;

	private const float DefaultLength = 0.06f;

	private const float CancelDistance = 0.03f;

	public static void Begin(FVRFireArmMagazine mag, FVRFireArm gun)
	{
		MagSlider magSlider = ((Component)mag).gameObject.AddComponent<MagSlider>();
		magSlider.mag = mag;
		magSlider.gun = gun;
		magSlider.Init();
	}

	private void Init()
	{
		//IL_0071: 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_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: 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_0096: 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)
		rb = ((FVRPhysicalObject)mag).RootRigidbody;
		if ((Object)(object)rb != (Object)null)
		{
			rbWasKinematic = rb.isKinematic;
			rbDetectCollisions = rb.detectCollisions;
			rb.isKinematic = true;
			rb.detectCollisions = false;
		}
		GetPath(out var entrance, out var axis, out var length);
		startT = Mathf.Clamp01(Vector3.Dot(((Component)mag).transform.position - entrance, axis) / length);
		handStartProj = Vector3.Dot(GetHandPos() - entrance, axis);
		SmoothMagsPlugin.Log.LogDebug((object)("Started sliding mag into " + ((Object)gun).name));
	}

	private void GetPath(out Vector3 entrance, out Vector3 axis, out float length)
	{
		//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_005c: 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_0068: 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_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: 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_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: 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_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		Transform magazineMountPos = gun.MagazineMountPos;
		Vector3 position = magazineMountPos.position;
		if ((Object)(object)gun.MagazineEjectPos != (Object)null && Vector3.Distance(position, gun.MagazineEjectPos.position) > 0.02f)
		{
			entrance = gun.MagazineEjectPos.position;
		}
		else
		{
			entrance = position - magazineMountPos.up * 0.06f;
		}
		Vector3 val = position - entrance;
		length = ((Vector3)(ref val)).magnitude;
		axis = val / length;
		if (length > 0.12f)
		{
			length = 0.12f;
			entrance = position - axis * 0.12f;
		}
	}

	private Vector3 GetHandPos()
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)((FVRInteractiveObject)mag).m_hand != (Object)null))
		{
			return ((Component)mag).transform.position;
		}
		return ((Component)((FVRInteractiveObject)mag).m_hand).transform.position;
	}

	private void LateUpdate()
	{
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: 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_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)mag == (Object)null || (Object)(object)gun == (Object)null)
		{
			Object.Destroy((Object)(object)this);
			return;
		}
		if (!((FVRInteractiveObject)mag).IsHeld || (Object)(object)gun.Magazine != (Object)null)
		{
			Cancel();
			return;
		}
		GetPath(out var entrance, out var axis, out var length);
		float num = Vector3.Dot(GetHandPos() - entrance, axis);
		float num2 = startT + (num - handStartProj) / length;
		if (num2 >= (float)SmoothMagsPlugin.SnapDepthPercent.Value / 100f)
		{
			Finish();
			return;
		}
		if (num2 < -0.03f / length)
		{
			Cancel();
			return;
		}
		num2 = Mathf.Clamp01(num2);
		((Component)mag).transform.position = Vector3.Lerp(entrance, entrance + axis * length, num2);
		((Component)mag).transform.rotation = gun.MagazineMountPos.rotation;
	}

	private void RestoreRigidbody()
	{
		if ((Object)(object)rb != (Object)null)
		{
			rb.isKinematic = rbWasKinematic;
			rb.detectCollisions = rbDetectCollisions;
		}
	}

	private void Finish()
	{
		RestoreRigidbody();
		SmoothMagsPlugin.AllowLoad = true;
		try
		{
			mag.Load(gun);
		}
		finally
		{
			SmoothMagsPlugin.AllowLoad = false;
		}
		SmoothMagsPlugin.Log.LogDebug((object)("Mag seated in " + ((Object)gun).name));
		Object.Destroy((Object)(object)this);
	}

	private void Cancel()
	{
		RestoreRigidbody();
		CooldownUntil = Time.time + 0.3f;
		SmoothMagsPlugin.Log.LogDebug((object)"Slide cancelled");
		Object.Destroy((Object)(object)this);
	}
}