Decompiled source of MW2HeartbeatSensor v0.1.1

MW2HeartbeatSensor.dll

Decompiled 19 hours ago
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using FistVR;
using HarmonyLib;
using OtherLoader;
using UnityEngine;

[assembly: Debuggable(DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
namespace MW2HeartbeatSensor
{
	public class HeartbeatDisplay : MonoBehaviour
	{
		private class Ping
		{
			public Vector3 Pos;

			public float Time;

			public bool Friendly;
		}

		private class Tracked
		{
			public Component Thing;

			public bool Done;
		}

		[Header("Screen (local metres: +X right, +Y up, seen from -Z)")]
		public float Width = 0.0556f;

		public float Height = 0.0411f;

		[Tooltip("Whose forward is up on the screen: the sensor (on a gun, the gun)")]
		public Transform Reference;

		public MeshFilter Overlay;

		public MeshRenderer OverlayRenderer;

		[Tooltip("The glass under the display, dimmed when the screen is off")]
		public Renderer Glass;

		public float GlassOff = 0.35f;

		[Header("MW2 motionTracker dvars (iw4sp.exe defaults, inches as metres)")]
		public float Range = 40.64f;

		public float SweepInterval = 3f;

		public float SweepSpeed = 50.8f;

		public float SweepAngle = 90f;

		public float PingFadeTime = 3f;

		public float PingSize = 0.2f;

		public Vector2 Center = new Vector2(0f, -0.37f);

		public float PitchBase = 1f;

		public float PitchNearby = 2f;

		public float PitchAddPerEnemy = 0f;

		[Tooltip("MW2 fades the screen in and out over 500 ms")]
		public float PowerFadeTime = 0.5f;

		[Header("Sounds")]
		[Tooltip("MW2 only plays the tracker for the weapon in hand: set by HeartbeatSensor while it, or its gun, is held")]
		public bool Audible;

		public AudioEvent AudEvent_Ping = new AudioEvent();

		public AudioEvent AudEvent_Pong = new AudioEvent();

		private readonly List<Ping> m_pings = new List<Ping>();

		private readonly List<Tracked> m_tracked = new List<Tracked>();

		private readonly Stack<Tracked> m_trackedPool = new Stack<Tracked>();

		private bool m_powered;

		private float m_power;

		private float m_sweepTime;

		private bool m_sweeping;

		private Mesh m_mesh;

		private readonly List<Vector3> m_v = new List<Vector3>();

		private readonly List<Vector2> m_uv = new List<Vector2>();

		private readonly List<Color> m_c = new List<Color>();

		private readonly List<int>[] m_tris = new List<int>[4]
		{
			new List<int>(),
			new List<int>(),
			new List<int>(),
			new List<int>()
		};

		private MaterialPropertyBlock m_block;

		private static int s_brightnessId = -1;

		private const int SubBackground = 0;

		private const int SubSweep = 1;

		private const int SubEnemy = 2;

		private const int SubFriendly = 3;

		public bool IsPowered => m_powered;

		private void Awake()
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			Mesh val = new Mesh();
			((Object)val).name = "HeartbeatDisplay";
			m_mesh = val;
			m_mesh.MarkDynamic();
			Overlay.sharedMesh = m_mesh;
			m_block = new MaterialPropertyBlock();
			if (s_brightnessId < 0)
			{
				s_brightnessId = Shader.PropertyToID("_Brightness");
			}
			SetGlass(0f);
			((Renderer)OverlayRenderer).enabled = false;
		}

		public void SetPowered(bool on, bool instant)
		{
			if (on && !m_powered)
			{
				m_sweepTime = 0f;
				m_sweeping = false;
				m_pings.Clear();
			}
			m_powered = on;
			if (instant)
			{
				m_power = ((!on) ? 0f : 1f);
			}
		}

		private void Update()
		{
			float deltaTime = Time.deltaTime;
			m_power = Mathf.MoveTowards(m_power, (!m_powered) ? 0f : 1f, deltaTime / PowerFadeTime);
			SetGlass(m_power);
			if (m_power <= 0f && !m_powered)
			{
				if (((Renderer)OverlayRenderer).enabled)
				{
					((Renderer)OverlayRenderer).enabled = false;
				}
				return;
			}
			if (!((Renderer)OverlayRenderer).enabled)
			{
				((Renderer)OverlayRenderer).enabled = true;
			}
			if (m_powered)
			{
				m_sweepTime += deltaTime;
				if (!m_sweeping || m_sweepTime >= SweepInterval)
				{
					m_sweepTime = ((!m_sweeping) ? 0f : (m_sweepTime % SweepInterval));
					StartSweep();
				}
				Detect(m_sweepTime * SweepSpeed);
			}
			for (int num = m_pings.Count - 1; num >= 0; num--)
			{
				if (Time.time - m_pings[num].Time > PingFadeTime)
				{
					m_pings.RemoveAt(num);
				}
			}
			Draw();
		}

		private void StartSweep()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			m_sweeping = true;
			if (Audible)
			{
				SM.PlayCoreSound((FVRPooledAudioType)10, AudEvent_Ping, ((Component)this).transform.position);
			}
			foreach (Tracked item in m_tracked)
			{
				m_trackedPool.Push(item);
			}
			m_tracked.Clear();
			foreach (Component item2 in HeartbeatTargets.Scan())
			{
				Tracked tracked = ((m_trackedPool.Count <= 0) ? new Tracked() : m_trackedPool.Pop());
				tracked.Thing = item2;
				tracked.Done = false;
				m_tracked.Add(tracked);
			}
		}

		private void Detect(float radius)
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: 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)
			int num = -1;
			foreach (Tracked item in m_tracked)
			{
				if (item.Done)
				{
					continue;
				}
				if (!HeartbeatTargets.IsAlive(item.Thing))
				{
					item.Done = true;
					continue;
				}
				Vector3 val = HeartbeatTargets.Position(item.Thing);
				float dist;
				float angle;
				Vector2 s = Project(val, out dist, out angle);
				if (dist > radius)
				{
					continue;
				}
				item.Done = true;
				if (!OnScreen(s) || Mathf.Abs(angle) > SweepAngle)
				{
					continue;
				}
				bool flag = HeartbeatTargets.IsFriendly(item.Thing);
				m_pings.Add(new Ping
				{
					Pos = val,
					Time = Time.time,
					Friendly = flag
				});
				if (!flag && Audible)
				{
					if (num < 0)
					{
						num = CountEnemiesShown();
					}
					Pong(dist, num);
				}
			}
		}

		private void Pong(float dist, int enemies)
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			float num = Width / Height;
			float num2 = (0.5f + Mathf.Abs(Center.x)) * num;
			float num3 = 0.5f + Mathf.Abs(Center.y);
			float num4 = Mathf.Clamp01(dist / (Mathf.Sqrt(num2 * num2 + num3 * num3) * Range));
			float num5 = Mathf.Lerp(PitchNearby, PitchBase, num4) * (1f + (float)Mathf.Max(0, enemies - 1) * PitchAddPerEnemy);
			SM.PlayCoreSoundOverrides((FVRPooledAudioType)10, AudEvent_Pong, ((Component)this).transform.position, AudEvent_Pong.VolumeRange, AudEvent_Pong.PitchRange * num5);
		}

		private int CountEnemiesShown()
		{
			int num = 0;
			foreach (Ping ping in m_pings)
			{
				if (!ping.Friendly)
				{
					num++;
				}
			}
			return num + 1;
		}

		private Vector2 Project(Vector3 world, out float dist, out float angle)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: 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_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: 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)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = Reference.forward;
			val.y = 0f;
			if (((Vector3)(ref val)).sqrMagnitude < 0.01f)
			{
				val = ((!(Reference.forward.y > 0f)) ? Reference.up : (-Reference.up));
				val.y = 0f;
			}
			((Vector3)(ref val)).Normalize();
			Vector3 val2 = Vector3.Cross(Vector3.up, val);
			Vector3 val3 = world - Reference.position;
			float num = Vector3.Dot(val3, val2);
			float num2 = Vector3.Dot(val3, val);
			dist = Mathf.Sqrt(num * num + num2 * num2);
			angle = Mathf.Atan2(num, num2) * 57.29578f;
			float num3 = Width / Height;
			return new Vector2(Center.x + num / (Range * num3), Center.y + num2 / Range);
		}

		private static bool OnScreen(Vector2 s)
		{
			return s.x >= -0.5f && s.x <= 0.5f && s.y >= -0.5f && s.y <= 0.5f;
		}

		private void Draw()
		{
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_0275: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			m_v.Clear();
			m_uv.Clear();
			m_c.Clear();
			List<int>[] tris = m_tris;
			foreach (List<int> list in tris)
			{
				list.Clear();
			}
			Color c = default(Color);
			((Color)(ref c))..ctor(1f, 1f, 1f, m_power);
			Quad(0, new Rect(-0.5f, -0.5f, 1f, 1f), c);
			if (m_sweeping)
			{
				float num = m_sweepTime * SweepSpeed / Range;
				float num2 = Width / Height;
				Quad(1, new Rect(Center.x - num / num2, Center.y, 2f * num / num2, num), c);
			}
			float pingSize = PingSize;
			float num3 = pingSize * Height / Width;
			foreach (Ping ping in m_pings)
			{
				float dist;
				float angle;
				Vector2 val = Project(ping.Pos, out dist, out angle);
				if (!(Mathf.Abs(angle) > SweepAngle))
				{
					float num4 = (1f - (Time.time - ping.Time) / PingFadeTime) * m_power;
					Quad((!ping.Friendly) ? 2 : 3, new Rect(val.x - num3 / 2f, val.y - pingSize / 2f, num3, pingSize), new Color(1f, 1f, 1f, num4));
				}
			}
			m_mesh.Clear();
			m_mesh.SetVertices(m_v);
			m_mesh.SetUVs(0, m_uv);
			m_mesh.SetColors(m_c);
			m_mesh.subMeshCount = m_tris.Length;
			for (int j = 0; j < m_tris.Length; j++)
			{
				m_mesh.SetTriangles(m_tris[j], j, false);
			}
			m_mesh.bounds = new Bounds(Vector3.zero, new Vector3(Width, Height, 0.002f));
		}

		private void Quad(int sub, Rect r, Color c)
		{
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
			float num = Mathf.Max(((Rect)(ref r)).xMin, -0.5f);
			float num2 = Mathf.Min(((Rect)(ref r)).xMax, 0.5f);
			float num3 = Mathf.Max(((Rect)(ref r)).yMin, -0.5f);
			float num4 = Mathf.Min(((Rect)(ref r)).yMax, 0.5f);
			if (!(num2 <= num) && !(num4 <= num3) && !(c.a <= 0f))
			{
				float num5 = (num - ((Rect)(ref r)).xMin) / ((Rect)(ref r)).width;
				float num6 = (num2 - ((Rect)(ref r)).xMin) / ((Rect)(ref r)).width;
				float num7 = (num3 - ((Rect)(ref r)).yMin) / ((Rect)(ref r)).height;
				float num8 = (num4 - ((Rect)(ref r)).yMin) / ((Rect)(ref r)).height;
				int count = m_v.Count;
				m_v.Add(new Vector3(num * Width, num3 * Height, 0f));
				m_v.Add(new Vector3(num * Width, num4 * Height, 0f));
				m_v.Add(new Vector3(num2 * Width, num4 * Height, 0f));
				m_v.Add(new Vector3(num2 * Width, num3 * Height, 0f));
				m_uv.Add(new Vector2(num5, num7));
				m_uv.Add(new Vector2(num5, num8));
				m_uv.Add(new Vector2(num6, num8));
				m_uv.Add(new Vector2(num6, num7));
				for (int i = 0; i < 4; i++)
				{
					m_c.Add(c);
				}
				List<int> list = m_tris[sub];
				list.Add(count);
				list.Add(count + 1);
				list.Add(count + 2);
				list.Add(count);
				list.Add(count + 2);
				list.Add(count + 3);
			}
		}

		private void SetGlass(float power)
		{
			if (!((Object)(object)Glass == (Object)null) && m_block != null)
			{
				Glass.GetPropertyBlock(m_block);
				m_block.SetFloat(s_brightnessId, Mathf.Lerp(GlassOff, 1f, power));
				Glass.SetPropertyBlock(m_block);
			}
		}

		public void PreviewPings(Vector2[] enemies, Vector2[] friends, float sweepTime)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: 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_00a9: 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_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: 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_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)m_mesh == (Object)null)
			{
				Awake();
			}
			m_powered = true;
			m_power = 1f;
			m_sweeping = true;
			m_sweepTime = sweepTime;
			m_pings.Clear();
			Vector3 forward = Reference.forward;
			forward.y = 0f;
			((Vector3)(ref forward)).Normalize();
			Vector3 val = Vector3.Cross(Vector3.up, forward);
			for (int i = 0; i < enemies.Length; i++)
			{
				Vector2 val2 = enemies[i];
				m_pings.Add(new Ping
				{
					Pos = Reference.position + val * val2.x + forward * val2.y,
					Time = Time.time,
					Friendly = false
				});
			}
			for (int j = 0; j < friends.Length; j++)
			{
				Vector2 val3 = friends[j];
				m_pings.Add(new Ping
				{
					Pos = Reference.position + val * val3.x + forward * val3.y,
					Time = Time.time,
					Friendly = true
				});
			}
			SetGlass(1f);
			((Renderer)OverlayRenderer).enabled = true;
			Draw();
		}
	}
	public class HeartbeatSensor : FVRFireArmAttachment
	{
		[Header("Heartbeat Sensor")]
		public Transform RotY;

		public Transform RotZ;

		public Transform Head;

		public HeartbeatDisplay Display;

		public bool IsOpen = true;

		[Header("MW2's fold animations, from their first moving frame (s, MW2 degrees)")]
		public float[] OpenTimes = new float[0];

		public float[] OpenRotY = new float[0];

		public float[] OpenRotZ = new float[0];

		public float[] CloseTimes = new float[0];

		public float[] CloseRotY = new float[0];

		public float[] CloseRotZ = new float[0];

		[Tooltip("The screen lights once the head has swung out (s into the open animation)")]
		public float PowerOnTime = 0.167f;

		public AudioEvent AudEvent_Open = new AudioEvent();

		public AudioEvent AudEvent_Close = new AudioEvent();

		[Header("Grab zones fitted to the pose")]
		public BoxCollider GrabBox;

		public BoxCollider InterfaceBox;

		[Tooltip("Clamp and arm, in root space")]
		public Bounds BaseBounds;

		[Tooltip("The head, in Head's space")]
		public Bounds HeadBounds;

		public float GrabPadding = 0.008f;

		public const string AttachSoundDonor = "MagnifierGamepoint3x";

		private float m_time = -1f;

		private float m_y;

		private float m_z;

		private float m_fromY;

		private float m_fromZ;

		private float m_blendStart;

		private bool m_powerPending;

		private const float BlendTime = 0.08f;

		private static bool s_lookedForDonor;

		private static AudioEvent s_attach;

		private static AudioEvent s_detach;

		public override void Awake()
		{
			((FVRFireArmAttachment)this).Awake();
			BorrowAttachSounds();
			if (IsOpen)
			{
				SetPose(OpenRotY[OpenRotY.Length - 1], OpenRotZ[OpenRotZ.Length - 1]);
			}
			else
			{
				SetPose(CloseRotY[CloseRotY.Length - 1], CloseRotZ[CloseRotZ.Length - 1]);
			}
			if ((Object)(object)Display != (Object)null)
			{
				Display.SetPowered(IsOpen, instant: true);
			}
		}

		public override void UpdateInteraction(FVRViveHand hand)
		{
			((FVRFireArmAttachment)this).UpdateInteraction(hand);
			if (FoldPressed(hand))
			{
				ToggleFold();
			}
		}

		public static bool FoldPressed(FVRViveHand hand)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			if (hand.IsInStreamlinedMode)
			{
				return hand.Input.BYButtonDown;
			}
			Vector2 touchpadAxes = hand.Input.TouchpadAxes;
			return hand.Input.TouchpadDown && ((Vector2)(ref touchpadAxes)).magnitude > 0.25f && Vector2.Angle(touchpadAxes, Vector2.up) <= 45f;
		}

		public void ToggleFold()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			IsOpen = !IsOpen;
			SM.PlayCoreSound((FVRPooledAudioType)10, (!IsOpen) ? AudEvent_Close : AudEvent_Open, Head.position);
			float[] array = ((!IsOpen) ? CloseTimes : OpenTimes);
			float[] values = ((!IsOpen) ? CloseRotY : OpenRotY);
			float[] values2 = ((!IsOpen) ? CloseRotZ : OpenRotZ);
			float num = 0f;
			float num2 = float.MaxValue;
			float num3 = array[^1];
			for (int i = 0; i <= 60; i++)
			{
				float num4 = num3 * (float)i / 60f;
				float num5 = Mathf.Abs(Sample(array, values, num4) - m_y) + Mathf.Abs(Sample(array, values2, num4) - m_z);
				if (num5 < num2 - 0.01f)
				{
					num2 = num5;
					num = num4;
				}
			}
			m_time = num;
			m_fromY = m_y;
			m_fromZ = m_z;
			m_blendStart = num;
			m_powerPending = IsOpen;
			if (!IsOpen && (Object)(object)Display != (Object)null)
			{
				Display.SetPowered(on: false, instant: false);
			}
		}

		public override void FVRUpdate()
		{
			((FVRPhysicalObject)this).FVRUpdate();
			if ((Object)(object)Display != (Object)null)
			{
				Display.Audible = IsInUse();
			}
			if (m_time < 0f)
			{
				return;
			}
			m_time += Time.deltaTime;
			float[] array = ((!IsOpen) ? CloseTimes : OpenTimes);
			float num = Sample(array, (!IsOpen) ? CloseRotY : OpenRotY, m_time);
			float num2 = Sample(array, (!IsOpen) ? CloseRotZ : OpenRotZ, m_time);
			float num3 = Mathf.Clamp01((m_time - m_blendStart) / 0.08f);
			SetPose(Mathf.Lerp(m_fromY, num, num3), Mathf.Lerp(m_fromZ, num2, num3));
			if (m_powerPending && m_time >= PowerOnTime)
			{
				m_powerPending = false;
				if ((Object)(object)Display != (Object)null)
				{
					Display.SetPowered(on: true, instant: false);
				}
			}
			if (m_time >= array[^1])
			{
				m_time = -1f;
			}
		}

		public bool IsInUse()
		{
			if (((FVRInteractiveObject)this).IsHeld)
			{
				return true;
			}
			if ((Object)(object)base.curMount == (Object)null)
			{
				return false;
			}
			FVRPhysicalObject rootObject = ((FVRFireArmAttachment)this).GetRootObject();
			return (Object)(object)rootObject != (Object)null && ((FVRInteractiveObject)rootObject).IsHeld;
		}

		private static float Sample(float[] times, float[] values, float t)
		{
			if (t <= times[0])
			{
				return values[0];
			}
			for (int i = 1; i < times.Length; i++)
			{
				if (t <= times[i])
				{
					return Mathf.Lerp(values[i - 1], values[i], Mathf.InverseLerp(times[i - 1], times[i], t));
				}
			}
			return values[^1];
		}

		public void SetPose(float rotY, float rotZ)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			m_y = rotY;
			m_z = rotZ;
			RotY.localRotation = Quaternion.Euler(0f, 0f - rotY, 0f);
			RotZ.localRotation = Quaternion.Euler(0f - rotZ, 0f, 0f);
			FitGrabZones();
		}

		private void FitGrabZones()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: 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_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Head == (Object)null)
			{
				return;
			}
			Bounds baseBounds = BaseBounds;
			Vector3 center = ((Bounds)(ref HeadBounds)).center;
			Vector3 extents = ((Bounds)(ref HeadBounds)).extents;
			Vector3 val = default(Vector3);
			for (int i = 0; i < 8; i++)
			{
				((Vector3)(ref val))..ctor((i % 2 != 0) ? extents.x : (0f - extents.x), (i / 2 % 2 != 0) ? extents.y : (0f - extents.y), (i / 4 != 0) ? extents.z : (0f - extents.z));
				((Bounds)(ref baseBounds)).Encapsulate(((Component)this).transform.InverseTransformPoint(Head.TransformPoint(center + val)));
			}
			((Bounds)(ref baseBounds)).Expand(GrabPadding * 2f);
			BoxCollider[] array = (BoxCollider[])(object)new BoxCollider[2] { GrabBox, InterfaceBox };
			foreach (BoxCollider val2 in array)
			{
				if (!((Object)(object)val2 == (Object)null))
				{
					val2.center = ((Bounds)(ref baseBounds)).center;
					val2.size = ((Bounds)(ref baseBounds)).size;
				}
			}
		}

		private void BorrowAttachSounds()
		{
			if (!s_lookedForDonor)
			{
				s_lookedForDonor = true;
				if (IM.OD != null && IM.OD.TryGetValue("MagnifierGamepoint3x", out var value) && (Object)(object)value != (Object)null)
				{
					GameObject gameObject = ((AnvilAsset)value).GetGameObject();
					FVRFireArmAttachment val = ((!((Object)(object)gameObject != (Object)null)) ? null : gameObject.GetComponent<FVRFireArmAttachment>());
					if ((Object)(object)val != (Object)null)
					{
						s_attach = val.AudClipAttach;
						s_detach = val.AudClipDettach;
					}
				}
				if (s_attach == null)
				{
					Debug.LogWarning((object)"[MW2HeartbeatSensor] MagnifierGamepoint3x not found: the rail clicks are MW2's fold sounds");
				}
			}
			base.AudClipAttach = ((!HasClips(s_attach)) ? AudEvent_Close : s_attach);
			base.AudClipDettach = ((!HasClips(s_detach)) ? AudEvent_Open : s_detach);
		}

		private static bool HasClips(AudioEvent e)
		{
			return e != null && e.Clips != null && e.Clips.Count > 0;
		}
	}
	public class HeartbeatSensorInterface : FVRFireArmAttachmentInterface
	{
		public override void UpdateInteraction(FVRViveHand hand)
		{
			HeartbeatSensor heartbeatSensor = base.Attachment as HeartbeatSensor;
			if ((Object)(object)heartbeatSensor != (Object)null && HeartbeatSensor.FoldPressed(hand))
			{
				heartbeatSensor.ToggleFold();
			}
			((FVRFireArmAttachmentInterface)this).UpdateInteraction(hand);
		}
	}
	public static class HeartbeatTargets
	{
		private static int s_frame = -1;

		private static readonly List<Component> s_things = new List<Component>();

		public static List<Component> Scan()
		{
			if (s_frame == Time.frameCount)
			{
				return s_things;
			}
			s_frame = Time.frameCount;
			s_things.Clear();
			Sosig[] array = Object.FindObjectsOfType<Sosig>();
			foreach (Sosig val in array)
			{
				if (IsAlive((Component)(object)val))
				{
					s_things.Add((Component)(object)val);
				}
			}
			MeatCrab[] array2 = Object.FindObjectsOfType<MeatCrab>();
			foreach (MeatCrab val2 in array2)
			{
				if (IsAlive((Component)(object)val2))
				{
					s_things.Add((Component)(object)val2);
				}
			}
			return s_things;
		}

		public static bool IsAlive(Component c)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Invalid comparison between Unknown and I4
			if ((Object)(object)c == (Object)null || !c.gameObject.activeInHierarchy)
			{
				return false;
			}
			Sosig val = (Sosig)(object)((c is Sosig) ? c : null);
			if ((Object)(object)val != (Object)null)
			{
				return (int)val.BodyState != 3;
			}
			return true;
		}

		public static Vector3 Position(Component c)
		{
			//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)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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)
			Sosig val = (Sosig)(object)((c is Sosig) ? c : null);
			if ((Object)(object)val != (Object)null && (Object)(object)val.CoreRB != (Object)null)
			{
				return val.CoreRB.position;
			}
			return c.transform.position;
		}

		public static bool IsFriendly(Component c)
		{
			int num = (((Object)(object)GM.CurrentPlayerBody != (Object)null) ? GM.CurrentPlayerBody.GetPlayerIFF() : 0);
			Sosig val = (Sosig)(object)((c is Sosig) ? c : null);
			if ((Object)(object)val != (Object)null)
			{
				return val.GetIFF() == num;
			}
			MeatCrab val2 = (MeatCrab)(object)((c is MeatCrab) ? c : null);
			if ((Object)(object)val2 != (Object)null)
			{
				return (Object)(object)val2.E != (Object)null && val2.E.IFFCode == num;
			}
			return false;
		}
	}
}
namespace LJIndustries.MW2HeartbeatSensor
{
	[BepInPlugin("LJIndustries.MW2HeartbeatSensor", "MW2HeartbeatSensor", "0.1.1")]
	[BepInProcess("h3vr.exe")]
	[Description("Built with MeatKit")]
	[BepInDependency("h3vr.otherloader", "1.3.0")]
	public class MW2HeartbeatSensorPlugin : BaseUnityPlugin
	{
		private static readonly string BasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

		internal static ManualLogSource Logger;

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			LoadAssets();
		}

		private void LoadAssets()
		{
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "LJIndustries.MW2HeartbeatSensor");
			OtherLoader.RegisterDirectLoad(BasePath, "LJIndustries.MW2HeartbeatSensor", "", "", "mw2_heartbeat_sensor", "");
		}
	}
}