Decompiled source of HelmetCamByDoctorplague v1.0.3

Mods/HelmetCam.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BoneLib.BoneMenu;
using HelmetCam;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Main), "HelmetCam", "1.0.0", "you", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("HelmetCam")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("HelmetCam")]
[assembly: AssemblyTitle("HelmetCam")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 HelmetCam
{
	public class Main : MelonMod
	{
		private MelonPreferences_Category _cat;

		private MelonPreferences_Entry<bool> _enabled;

		private MelonPreferences_Entry<float> _fov;

		private MelonPreferences_Entry<float> _offX;

		private MelonPreferences_Entry<float> _offY;

		private MelonPreferences_Entry<float> _offZ;

		private MelonPreferences_Entry<float> _smoothing;

		private Camera _head;

		private Camera _spectator;

		private float _nextScan;

		private Quaternion _smoothedRot;

		private bool _hasRot;

		private float _originalFov = -1f;

		public override void OnInitializeMelon()
		{
			_cat = MelonPreferences.CreateCategory("HelmetCam");
			_enabled = _cat.CreateEntry<bool>("Enabled", true, "Helmet cam on", (string)null, false, false, (ValueValidator)null, (string)null);
			_fov = _cat.CreateEntry<float>("FOV", 90f, "Spectator FOV (30-140)", (string)null, false, false, (ValueValidator)null, (string)null);
			_offX = _cat.CreateEntry<float>("OffsetX", 0f, "Right/left offset (m)", (string)null, false, false, (ValueValidator)null, (string)null);
			_offY = _cat.CreateEntry<float>("OffsetY", 0.08f, "Up offset (m) - above eyes like a helmet mount", (string)null, false, false, (ValueValidator)null, (string)null);
			_offZ = _cat.CreateEntry<float>("OffsetZ", 0.1f, "Forward offset (m)", (string)null, false, false, (ValueValidator)null, (string)null);
			_smoothing = _cat.CreateEntry<float>("Smoothing", 0f, "Rotation smoothing (0 = rigid, try 12 for gentle)", (string)null, false, false, (ValueValidator)null, (string)null);
			try
			{
				SetupBoneMenu();
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Error("BoneMenu unavailable (is BoneLib installed?). Hotkeys still work. " + ex.Message);
			}
			((MelonBase)this).LoggerInstance.Msg("HelmetCam loaded. BoneMenu > HelmetCam, or F8 toggle | F9/F10 FOV -/+ | Arrows+PgUp/PgDn nudge offset | F7 reset");
		}

		private void SetupBoneMenu()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Page obj = Page.Root.CreatePage("HelmetCam", Color.cyan, 0, true);
				obj.CreateBool("Enabled", Color.white, _enabled.Value, (Action<bool>)delegate(bool v)
				{
					_enabled.Value = v;
					MelonPreferences.Save();
				});
				obj.CreateFloat("FOV", Color.yellow, _fov.Value, 5f, 30f, 140f, (Action<float>)delegate(float v)
				{
					_fov.Value = v;
					MelonPreferences.Save();
				});
				obj.CreateFloat("Offset Up/Down", Color.white, _offY.Value, 0.01f, -0.5f, 0.5f, (Action<float>)delegate(float v)
				{
					_offY.Value = v;
					MelonPreferences.Save();
				});
				obj.CreateFloat("Offset Forward/Back", Color.white, _offZ.Value, 0.01f, -0.5f, 0.5f, (Action<float>)delegate(float v)
				{
					_offZ.Value = v;
					MelonPreferences.Save();
				});
				obj.CreateFloat("Offset Left/Right", Color.white, _offX.Value, 0.01f, -0.5f, 0.5f, (Action<float>)delegate(float v)
				{
					_offX.Value = v;
					MelonPreferences.Save();
				});
				obj.CreateFloat("Smoothing", Color.white, _smoothing.Value, 1f, 0f, 30f, (Action<float>)delegate(float v)
				{
					_smoothing.Value = v;
					MelonPreferences.Save();
				});
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Error("BoneMenu setup failed (hotkeys still work): " + ex);
			}
		}

		public override void OnSceneWasInitialized(int buildIndex, string sceneName)
		{
			_head = null;
			_spectator = null;
			_originalFov = -1f;
			_hasRot = false;
			_nextScan = 0f;
		}

		public override void OnUpdate()
		{
			HandleHotkeys();
		}

		public override void OnLateUpdate()
		{
			if (!_enabled.Value)
			{
				RestoreFov();
				return;
			}
			if ((Object)(object)_head == (Object)null || (Object)(object)_spectator == (Object)null || !Object.op_Implicit((Object)(object)_head) || !Object.op_Implicit((Object)(object)_spectator))
			{
				if (Time.unscaledTime < _nextScan)
				{
					return;
				}
				_nextScan = Time.unscaledTime + 2f;
				FindCameras();
				if ((Object)(object)_head == (Object)null || (Object)(object)_spectator == (Object)null)
				{
					return;
				}
			}
			ApplyPose();
		}

		private void FindCameras()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Invalid comparison between Unknown and I4
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			_head = null;
			_spectator = null;
			Il2CppReferenceArray<Camera> allCameras = Camera.allCameras;
			foreach (Camera item in (Il2CppArrayBase<Camera>)(object)allCameras)
			{
				if (!((Object)(object)item == (Object)null))
				{
					bool flag = (int)item.stereoTargetEye > 0;
					if (((((Object)item).name != null) ? ((Object)item).name.ToLowerInvariant() : "").Contains("spectator"))
					{
						_spectator = item;
					}
					else if (flag && (Object)(object)_head == (Object)null)
					{
						_head = item;
					}
				}
			}
			if ((Object)(object)_spectator == (Object)null)
			{
				foreach (Camera item2 in (Il2CppArrayBase<Camera>)(object)allCameras)
				{
					if (!((Object)(object)item2 == (Object)null) && !((Object)(object)item2 == (Object)(object)_head) && (int)item2.stereoTargetEye == 0 && (Object)(object)item2.targetTexture == (Object)null)
					{
						_spectator = item2;
						break;
					}
				}
			}
			if ((Object)(object)_spectator != (Object)null)
			{
				if (_originalFov < 0f)
				{
					_originalFov = _spectator.fieldOfView;
				}
				((MelonBase)this).LoggerInstance.Msg($"Spectator cam: '{((Object)_spectator).name}' | head cam: '{(((Object)(object)_head != (Object)null) ? ((Object)_head).name : "none")}'");
			}
		}

		private void ApplyPose()
		{
			//IL_0039: 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)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: 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_0074: 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_0097: 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_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			Transform transform = ((Component)_head).transform;
			Transform transform2 = ((Component)_spectator).transform;
			Vector3 val = transform.TransformPoint(new Vector3(_offX.Value, _offY.Value, _offZ.Value));
			Quaternion val2 = transform.rotation;
			if (_smoothing.Value > 0.01f)
			{
				if (!_hasRot)
				{
					_smoothedRot = val2;
					_hasRot = true;
				}
				_smoothedRot = Quaternion.Slerp(_smoothedRot, val2, 1f - Mathf.Exp((0f - _smoothing.Value) * Time.unscaledDeltaTime));
				val2 = _smoothedRot;
			}
			else
			{
				_hasRot = false;
			}
			transform2.SetPositionAndRotation(val, val2);
			_spectator.fieldOfView = Mathf.Clamp(_fov.Value, 30f, 140f);
		}

		private void RestoreFov()
		{
			if ((Object)(object)_spectator != (Object)null && Object.op_Implicit((Object)(object)_spectator) && _originalFov > 0f)
			{
				_spectator.fieldOfView = _originalFov;
			}
		}

		private void HandleHotkeys()
		{
			if (Input.GetKeyDown((KeyCode)289))
			{
				_enabled.Value = !_enabled.Value;
				MelonPreferences.Save();
				((MelonBase)this).LoggerInstance.Msg("Helmet cam " + (_enabled.Value ? "ON" : "OFF"));
			}
			if (Input.GetKey((KeyCode)290))
			{
				_fov.Value = Mathf.Clamp(_fov.Value - 30f * Time.unscaledDeltaTime, 30f, 140f);
			}
			if (Input.GetKey((KeyCode)291))
			{
				_fov.Value = Mathf.Clamp(_fov.Value + 30f * Time.unscaledDeltaTime, 30f, 140f);
			}
			float num = 0.15f * Time.unscaledDeltaTime;
			if (Input.GetKey((KeyCode)275))
			{
				MelonPreferences_Entry<float> offX = _offX;
				offX.Value += num;
			}
			if (Input.GetKey((KeyCode)276))
			{
				MelonPreferences_Entry<float> offX2 = _offX;
				offX2.Value -= num;
			}
			if (Input.GetKey((KeyCode)280))
			{
				MelonPreferences_Entry<float> offY = _offY;
				offY.Value += num;
			}
			if (Input.GetKey((KeyCode)281))
			{
				MelonPreferences_Entry<float> offY2 = _offY;
				offY2.Value -= num;
			}
			if (Input.GetKey((KeyCode)273))
			{
				MelonPreferences_Entry<float> offZ = _offZ;
				offZ.Value += num;
			}
			if (Input.GetKey((KeyCode)274))
			{
				MelonPreferences_Entry<float> offZ2 = _offZ;
				offZ2.Value -= num;
			}
			if (Input.GetKeyDown((KeyCode)288))
			{
				_offX.Value = 0f;
				_offY.Value = 0.08f;
				_offZ.Value = 0.1f;
				_fov.Value = 90f;
			}
			if (Input.GetKeyUp((KeyCode)290) || Input.GetKeyUp((KeyCode)291) || Input.GetKeyUp((KeyCode)280) || Input.GetKeyUp((KeyCode)281) || Input.GetKeyUp((KeyCode)276) || Input.GetKeyUp((KeyCode)275) || Input.GetKeyUp((KeyCode)273) || Input.GetKeyUp((KeyCode)274))
			{
				MelonPreferences.Save();
			}
		}
	}
}