Decompiled source of F5CameraCycle v0.1.0

plugins\F5CameraCycle.dll

Decompiled 41 minutes ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.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 F5CameraCycle
{
	[BepInPlugin("fan.f5cameracycle", "F5 Camera Cycle", "0.1.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private enum ViewMode
		{
			FirstPerson,
			ThirdPersonBack,
			ThirdPersonFront
		}

		[HarmonyPatch(typeof(PlayerController), "Update")]
		private static class PlayerControllerUpdatePatch
		{
			[HarmonyPostfix]
			private static void Postfix()
			{
				if ((Object)(object)Instance != (Object)null)
				{
					Instance.TickInput();
				}
			}
		}

		[HarmonyPatch(typeof(CameraPosition), "Update")]
		private static class CameraPositionUpdatePatch
		{
			[HarmonyPostfix]
			private static void Postfix()
			{
				if ((Object)(object)Instance != (Object)null)
				{
					Instance.TickCamera();
				}
			}
		}

		public const string PluginGuid = "fan.f5cameracycle";

		public const string PluginName = "F5 Camera Cycle";

		public const string PluginVersion = "0.1.0";

		private Harmony _harmony;

		private ViewMode _mode;

		private int _lastInputFrame = -1;

		private int _lastCameraFrame = -1;

		private bool _frontRotationApplied;

		private const float CameraDistance = 3.25f;

		private const float CameraHeight = 1.82f;

		internal static Plugin Instance { get; private set; }

		private void Awake()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected O, but got Unknown
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			Instance = this;
			_harmony = new Harmony("fan.f5cameracycle");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			Camera.onPreCull = (CameraCallback)Delegate.Combine((Delegate?)(object)Camera.onPreCull, (Delegate?)new CameraCallback(OnCameraPreCull));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"F5 Camera Cycle loaded. F5: back -> front -> first person.");
		}

		private void OnDestroy()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			Camera.onPreCull = (CameraCallback)Delegate.Remove((Delegate?)(object)Camera.onPreCull, (Delegate?)new CameraCallback(OnCameraPreCull));
			ResetToFirstPerson();
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
				_harmony = null;
			}
			if ((Object)(object)Instance == (Object)(object)this)
			{
				Instance = null;
			}
		}

		private void OnDisable()
		{
			ResetToFirstPerson();
		}

		internal void TickInput()
		{
			if (_lastInputFrame == Time.frameCount)
			{
				return;
			}
			_lastInputFrame = Time.frameCount;
			if (IsGameplayActive() && Input.GetKeyDown((KeyCode)286))
			{
				if (_frontRotationApplied)
				{
					RestoreFrontCameraRotation();
				}
				_mode = (ViewMode)((int)(_mode + 1) % 3);
				if (_mode == ViewMode.FirstPerson)
				{
					ResetToFirstPerson();
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Camera mode: first person.");
				}
				else
				{
					ShowLocalPlayerModel();
					((BaseUnityPlugin)this).Logger.LogInfo((object)((_mode == ViewMode.ThirdPersonBack) ? "Camera mode: third person (back)." : "Camera mode: third person (front). "));
				}
			}
		}

		internal void TickCamera()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//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_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_007f: 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_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: 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_0078: 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_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_0109: 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_00f3: 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_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: 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_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			if (_lastCameraFrame == Time.frameCount)
			{
				return;
			}
			_lastCameraFrame = Time.frameCount;
			if (_mode == ViewMode.FirstPerson)
			{
				return;
			}
			if (!TryGetCameraContext(out var cameraPosition, out var avatar, out var aimTransform))
			{
				ResetToFirstPerson();
				return;
			}
			Transform transform = ((Component)avatar).transform;
			Vector3 val = transform.position + Vector3.up * 1.82f;
			Vector3 val2;
			if (_mode == ViewMode.ThirdPersonBack)
			{
				Vector3 forward = aimTransform.forward;
				if (((Vector3)(ref forward)).sqrMagnitude < 0.0001f)
				{
					forward = transform.forward;
				}
				val2 = val - ((Vector3)(ref forward)).normalized * 3.25f;
			}
			else
			{
				Vector3 forward2 = (((Object)(object)avatar.playerAvatarVisuals != (Object)null) ? ((Component)avatar.playerAvatarVisuals).transform : transform).forward;
				forward2.y = 0f;
				if (((Vector3)(ref forward2)).sqrMagnitude < 0.0001f)
				{
					forward2 = transform.forward;
				}
				forward2.y = 0f;
				((Vector3)(ref forward2)).Normalize();
				val2 = val + forward2 * 3.25f;
			}
			cameraPosition.OverridePosition(val2, 0.1f);
			((Component)cameraPosition).transform.position = val2;
			ShowLocalPlayerModel();
		}

		private void OnCameraPreCull(Camera camera)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: 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_006c: 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_008d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)camera == (Object)null || (Object)(object)camera != (Object)(object)Camera.main)
			{
				return;
			}
			CameraPosition cameraPosition;
			PlayerAvatar avatar;
			Transform aimTransform;
			if (_mode != ViewMode.ThirdPersonFront)
			{
				if (_frontRotationApplied)
				{
					RestoreFrontCameraRotation();
				}
			}
			else if (TryGetCameraContext(out cameraPosition, out avatar, out aimTransform))
			{
				Vector3 val = ((Component)avatar).transform.position + Vector3.up * 1.82f - ((Component)camera).transform.position;
				if (((Vector3)(ref val)).sqrMagnitude > 0.0001f)
				{
					((Component)camera).transform.rotation = Quaternion.LookRotation(((Vector3)(ref val)).normalized, Vector3.up);
					_frontRotationApplied = true;
				}
			}
		}

		private static bool TryGetCameraContext(out CameraPosition cameraPosition, out PlayerAvatar avatar, out Transform aimTransform)
		{
			cameraPosition = CameraPosition.instance;
			avatar = PlayerAvatar.instance;
			aimTransform = (((Object)(object)CameraAim.Instance != (Object)null) ? ((Component)CameraAim.Instance).transform : null);
			if (IsGameplayActive() && (Object)(object)cameraPosition != (Object)null && (Object)(object)avatar != (Object)null && (Object)(object)aimTransform != (Object)null)
			{
				return (Object)(object)cameraPosition.playerTransform != (Object)null;
			}
			return false;
		}

		private void RestoreFrontCameraRotation()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			Camera main = Camera.main;
			Transform val = (((Object)(object)CameraAim.Instance != (Object)null) ? ((Component)CameraAim.Instance).transform : null);
			if ((Object)(object)main != (Object)null && (Object)(object)val != (Object)null)
			{
				((Component)main).transform.rotation = val.rotation;
			}
			_frontRotationApplied = false;
		}

		private static bool IsGameplayActive()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Invalid comparison between Unknown and I4
			if ((Object)(object)GameDirector.instance == (Object)null)
			{
				return false;
			}
			if ((int)GameDirector.instance.currentState == 2 && (Object)(object)PlayerAvatar.instance != (Object)null && (Object)(object)CameraPosition.instance != (Object)null)
			{
				return (Object)(object)CameraAim.Instance != (Object)null;
			}
			return false;
		}

		private static void ShowLocalPlayerModel()
		{
			PlayerAvatar instance = PlayerAvatar.instance;
			if ((Object)(object)instance != (Object)null && (Object)(object)instance.playerAvatarVisuals != (Object)null)
			{
				instance.playerAvatarVisuals.ShowSelfOverride(0.15f);
			}
		}

		private static void ResetToFirstPerson()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			Plugin instance = Instance;
			if ((Object)(object)instance != (Object)null)
			{
				if (instance._frontRotationApplied)
				{
					instance.RestoreFrontCameraRotation();
				}
				instance._mode = ViewMode.FirstPerson;
			}
			CameraPosition instance2 = CameraPosition.instance;
			if ((Object)(object)instance2 != (Object)null && (Object)(object)instance2.playerTransform != (Object)null)
			{
				instance2.OverridePosition(Vector3.zero, 0f);
				((Component)instance2).transform.localRotation = Quaternion.identity;
			}
			PlayerAvatar instance3 = PlayerAvatar.instance;
			if ((Object)(object)instance3 != (Object)null && (Object)(object)instance3.playerAvatarVisuals != (Object)null)
			{
				instance3.playerAvatarVisuals.ShowSelfOverride(0f);
			}
		}
	}
}