Decompiled source of FirstPersonGB v1.0.0

Mods/FirstPersonGB.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using FirstPersonGB;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using Il2CppSystem.Reflection;
using MelonLoader;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(FirstPersonGBMod), "FirstPersonGB", "1.0.0", "KMK And Zooks", null)]
[assembly: MelonGame("Boneloaf", "Gang Beasts")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("FirstPersonGB")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("First Person for GB")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FirstPersonGB")]
[assembly: AssemblyTitle("FirstPersonGB")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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 FirstPersonGB
{
	public class FirstPersonGBMod : MelonMod
	{
		private bool isFirstPerson;

		private string currentSceneName = "";

		private Transform playerHead;

		private Camera mainCamera;

		private float originalFOV;

		private float originalNearClip;

		private float pitch;

		private float yaw;

		private readonly float mouseSensitivity = 0.15f;

		private bool justToggled;

		private readonly float positionSmoothSpeed = 40f;

		private Dictionary<Renderer, bool> hiddenRenderers = new Dictionary<Renderer, bool>();

		private List<MonoBehaviour> disabledScripts = new List<MonoBehaviour>();

		public override void OnInitializeMelon()
		{
			MelonLogger.Msg("FirstPersonGB v1.0.0 by KMK And Zooks Loaded.");
		}

		public override void OnUpdate()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			Scene activeScene = SceneManager.GetActiveScene();
			string name = ((Scene)(ref activeScene)).name;
			if (currentSceneName != name)
			{
				currentSceneName = name;
				if (isFirstPerson)
				{
					ForceDisableFirstPerson();
				}
			}
			if (Keyboard.current != null)
			{
				if (((ButtonControl)Keyboard.current.kKey).wasPressedThisFrame)
				{
					ToggleFirstPerson();
				}
				if (isFirstPerson && ((Object)(object)playerHead == (Object)null || !((Component)playerHead).gameObject.activeInHierarchy))
				{
					ForceDisableFirstPerson();
				}
			}
		}

		public override void OnFixedUpdate()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			if (isFirstPerson && (Object)(object)mainCamera != (Object)null)
			{
				((Component)mainCamera).transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
			}
		}

		public override void OnLateUpdate()
		{
			if (isFirstPerson)
			{
				UpdateVisualCamera();
			}
		}

		private void ToggleFirstPerson()
		{
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			if (!isFirstPerson)
			{
				mainCamera = Camera.main;
				if ((Object)(object)mainCamera == (Object)null)
				{
					return;
				}
				SmartFindLocalPlayer();
				if (!((Object)(object)playerHead == (Object)null))
				{
					originalFOV = mainCamera.fieldOfView;
					originalNearClip = mainCamera.nearClipPlane;
					mainCamera.fieldOfView = 140f;
					mainCamera.nearClipPlane = 0.01f;
					disabledScripts.Clear();
					Transform val = ((Component)mainCamera).transform;
					while ((Object)(object)val != (Object)null)
					{
						DisableTrackingScripts(((Component)val).gameObject);
						val = val.parent;
					}
					Quaternion rotation = playerHead.rotation;
					Vector3 eulerAngles = ((Quaternion)(ref rotation)).eulerAngles;
					yaw = eulerAngles.y;
					pitch = eulerAngles.x;
					Cursor.lockState = (CursorLockMode)1;
					Cursor.visible = false;
					HideHeadVisuals();
					isFirstPerson = true;
					justToggled = true;
				}
			}
			else
			{
				ForceDisableFirstPerson();
			}
		}

		private void ForceDisableFirstPerson()
		{
			isFirstPerson = false;
			Cursor.lockState = (CursorLockMode)0;
			Cursor.visible = true;
			RestoreHeadVisuals();
			if ((Object)(object)mainCamera != (Object)null)
			{
				mainCamera.fieldOfView = originalFOV;
				mainCamera.nearClipPlane = originalNearClip;
				foreach (MonoBehaviour disabledScript in disabledScripts)
				{
					if ((Object)(object)disabledScript != (Object)null)
					{
						((Behaviour)disabledScript).enabled = true;
					}
				}
			}
			disabledScripts.Clear();
		}

		private void DisableTrackingScripts(GameObject obj)
		{
			MonoBehaviour[] array = Il2CppArrayBase<MonoBehaviour>.op_Implicit(obj.GetComponents<MonoBehaviour>());
			foreach (MonoBehaviour val in array)
			{
				if (!((Object)(object)val == (Object)null))
				{
					string text = ((MemberInfo)((Object)val).GetIl2CppType()).Name.ToLower();
					if (!text.Contains("postprocess") && !text.Contains("volume") && !text.Contains("amplify") && !text.Contains("layer") && !text.Contains("audio") && ((Behaviour)val).enabled)
					{
						((Behaviour)val).enabled = false;
						disabledScripts.Add(val);
					}
				}
			}
		}

		private void SmartFindLocalPlayer()
		{
			playerHead = null;
			Rigidbody[] array = Il2CppArrayBase<Rigidbody>.op_Implicit(Object.FindObjectsOfType<Rigidbody>());
			foreach (Rigidbody val in array)
			{
				if ((Object)(object)val == (Object)null || ((Object)((Component)val).gameObject).name.ToLower() != "actor_head_collider")
				{
					continue;
				}
				Transform root = ((Component)val).transform.root;
				Component val2 = ((Component)root).GetComponent("Actor") ?? ((Component)root).GetComponent("Il2CppFemur.Actor");
				if ((Object)(object)val2 != (Object)null)
				{
					Type type = ((object)val2).GetType();
					PropertyInfo property = type.GetProperty("IsAI");
					if (property != null)
					{
						try
						{
							if ((bool)property.GetValue(val2, null))
							{
								continue;
							}
						}
						catch
						{
						}
					}
					PropertyInfo propertyInfo = type.GetProperty("isLocalPlayer") ?? type.GetProperty("IsLocal");
					if (propertyInfo != null)
					{
						try
						{
							if ((bool)propertyInfo.GetValue(val2, null))
							{
								playerHead = ((Component)val).transform;
								break;
							}
						}
						catch
						{
						}
					}
				}
				if (((Object)root).name.Contains("[PlayerID: 0]") && (Object)(object)playerHead == (Object)null)
				{
					playerHead = ((Component)val).transform;
				}
			}
		}

		private void HideHeadVisuals()
		{
			hiddenRenderers.Clear();
			if ((Object)(object)playerHead == (Object)null)
			{
				return;
			}
			Renderer[] array = Il2CppArrayBase<Renderer>.op_Implicit(((Component)playerHead.root).GetComponentsInChildren<Renderer>());
			foreach (Renderer val in array)
			{
				if (!((Object)(object)val == (Object)null))
				{
					string text = ((Object)((Component)val).gameObject).name.ToLower();
					if (text.Contains("head") || text.Contains("eye") || text.Contains("hat") || text.Contains("glass") || text.Contains("mask") || text.Contains("hair") || text.Contains("helmet") || text.Contains("cap") || text.Contains("hood") || text.Contains("teeth") || text.Contains("jaw") || text.Contains("mouth") || text.Contains("goggle") || text.Contains("beard") || text.Contains("ear") || text.Contains("beanie") || text.Contains("crown") || text.Contains("wig"))
					{
						hiddenRenderers[val] = val.enabled;
						val.enabled = false;
					}
				}
			}
		}

		private void RestoreHeadVisuals()
		{
			foreach (KeyValuePair<Renderer, bool> hiddenRenderer in hiddenRenderers)
			{
				if ((Object)(object)hiddenRenderer.Key != (Object)null)
				{
					hiddenRenderer.Key.enabled = hiddenRenderer.Value;
				}
			}
			hiddenRenderers.Clear();
		}

		private void UpdateVisualCamera()
		{
			//IL_002b: 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_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: 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_00dc: 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_0076: 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_0083: 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_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: 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)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: 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)
			if (isFirstPerson && !((Object)(object)mainCamera == (Object)null) && !((Object)(object)playerHead == (Object)null))
			{
				Vector3 val = playerHead.position + playerHead.up * 0.38f + playerHead.forward * 0.05f;
				if (Mouse.current != null)
				{
					Vector2 val2 = ((InputControl<Vector2>)(object)((Pointer)Mouse.current).delta).ReadValue();
					yaw += val2.x * mouseSensitivity;
					pitch -= val2.y * mouseSensitivity;
					pitch = Mathf.Clamp(pitch, -85f, 85f);
				}
				Quaternion rotation = Quaternion.Euler(pitch, yaw, 0f);
				if (justToggled)
				{
					((Component)mainCamera).transform.position = val;
					((Component)mainCamera).transform.rotation = rotation;
					justToggled = false;
				}
				else
				{
					((Component)mainCamera).transform.position = Vector3.Lerp(((Component)mainCamera).transform.position, val, Time.deltaTime * positionSmoothSpeed);
					((Component)mainCamera).transform.rotation = rotation;
				}
			}
		}
	}
}