Decompiled source of PerspectiveSwitcher v0.1.1

plugins/com.github.nazo-x1.PerspectiveSwitcher.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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using On;
using UnityEngine;
using Zorro.ControllerSupport;
using Zorro.Settings;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("MMHOOK_Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.nazo-x1.PerspectiveSwitcher")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1+7a6193543ce7c7c28e74bb27ade5f7804a81872a")]
[assembly: AssemblyProduct("com.github.nazo-x1.PerspectiveSwitcher")]
[assembly: AssemblyTitle("PerspectiveSwitcher")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace PerspectiveSwitcher
{
	internal enum PerspectiveMode
	{
		FirstPerson,
		SecondPerson,
		ThirdPerson
	}
	internal interface IPerspectiveAdapter
	{
		PerspectiveMode Mode { get; }

		void OnLateUpdate(MainCameraMovement self, float scrollDelta);

		bool OnCharacterCam(orig_CharacterCam orig, MainCameraMovement self);

		bool OnTryToStartWallClimb(orig_TryToStartWallClimb orig, CharacterClimbing self, bool forceAttempt, Vector3 overide, bool botGrab, float raycastDistance);
	}
	internal sealed class FirstPersonAdapter : IPerspectiveAdapter
	{
		public PerspectiveMode Mode => PerspectiveMode.FirstPerson;

		public void OnLateUpdate(MainCameraMovement self, float scrollDelta)
		{
		}

		public bool OnCharacterCam(orig_CharacterCam orig, MainCameraMovement self)
		{
			return false;
		}

		public bool OnTryToStartWallClimb(orig_TryToStartWallClimb orig, CharacterClimbing self, bool forceAttempt, Vector3 overide, bool botGrab, float raycastDistance)
		{
			return false;
		}
	}
	internal sealed class OrbitPerspectiveAdapter : IPerspectiveAdapter
	{
		private readonly Plugin _plugin;

		private readonly PerspectiveMode _mode;

		private readonly int _directionSign;

		private readonly float _height = 1.5f;

		private readonly float _defaultDistance = 3f;

		private float _currentDistance;

		private readonly float _minDistance = 2f;

		private readonly float _maxDistance = 4f;

		private readonly float _zoomSpeed = 1f;

		private readonly float _lerpRate = 5f;

		private readonly float _turnSpeed = 720f;

		private readonly float _clipRadius = 0.06f;

		private readonly float _clipBuffer = 0.03f;

		private readonly LayerMask _clipMask = LayerMask.op_Implicit(LayerMask.GetMask(new string[2] { "Terrain", "Map" }));

		public PerspectiveMode Mode => _mode;

		public OrbitPerspectiveAdapter(Plugin plugin, PerspectiveMode mode, int directionSign)
		{
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			_plugin = plugin;
			_mode = mode;
			_directionSign = directionSign;
			_currentDistance = _defaultDistance;
		}

		public void OnLateUpdate(MainCameraMovement self, float scrollDelta)
		{
			if (Mathf.Abs(scrollDelta) > 0.01f)
			{
				_currentDistance = Mathf.Clamp(_currentDistance - scrollDelta * _zoomSpeed, _minDistance, _maxDistance);
			}
		}

		public bool OnCharacterCam(orig_CharacterCam orig, MainCameraMovement self)
		{
			//IL_004d: 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_0053: 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_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_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_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: 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_00a0: 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_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: 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_00b6: 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_00bd: 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_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: 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)
			//IL_0061: 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)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: 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_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Character.localCharacter == (Object)null)
			{
				return false;
			}
			_plugin.EnsureSettings();
			MainCamera component = ((Component)self).GetComponent<MainCamera>();
			component.cam.fieldOfView = self.GetFov();
			Transform transform = ((Component)Character.localCharacter.GetBodypart((BodypartType)2)).transform;
			Vector3 val = Character.localCharacter.data.lookDirection;
			if (val == Vector3.zero)
			{
				val = transform.forward;
			}
			Vector3 val2 = transform.position + Vector3.up * _height + ((Vector3)(ref val)).normalized * _currentDistance * (float)_directionSign;
			Vector3 val3 = val2 - transform.position;
			Vector3 normalized = ((Vector3)(ref val3)).normalized;
			float num = Vector3.Distance(val2, transform.position);
			RaycastHit val4 = default(RaycastHit);
			if (Physics.SphereCast(transform.position, _clipRadius, normalized, ref val4, num, LayerMask.op_Implicit(_clipMask)))
			{
				val2 = ((RaycastHit)(ref val4)).point - normalized * _clipBuffer;
			}
			((Component)self).transform.position = Vector3.Lerp(((Component)self).transform.position, val2, Time.deltaTime * _lerpRate);
			Vector3 val5 = ((_mode == PerspectiveMode.SecondPerson) ? (transform.position + Vector3.up * 0.5f) : transform.position);
			Quaternion val6 = Quaternion.LookRotation(val5 - ((Component)self).transform.position, Vector3.up);
			float sensitivityMultiplier = _plugin.GetSensitivityMultiplier();
			((Component)self).transform.rotation = Quaternion.RotateTowards(((Component)self).transform.rotation, val6, _turnSpeed * sensitivityMultiplier * Time.deltaTime);
			return true;
		}

		public bool OnTryToStartWallClimb(orig_TryToStartWallClimb orig, CharacterClimbing self, bool forceAttempt, Vector3 overide, bool botGrab, float raycastDistance)
		{
			//IL_001c: 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)
			//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_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: 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_008d: 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_007e: 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_00a8: Unknown result type (might be due to invalid IL or missing references)
			orig_TryToStartWallClimb orig2 = orig;
			CharacterClimbing self2 = self;
			Transform transform = ((Component)self2.character.GetBodypart((BodypartType)2)).transform;
			Vector3 val = self2.character.data.lookDirection;
			if (val == Vector3.zero)
			{
				val = transform.forward;
			}
			Vector3 tempPosition = ((_mode == PerspectiveMode.SecondPerson) ? (transform.position - ((Vector3)(ref val)).normalized * 1f) : transform.position);
			_plugin.WithTemporaryCameraPosition(tempPosition, delegate
			{
				//IL_0013: Unknown result type (might be due to invalid IL or missing references)
				orig2.Invoke(self2, forceAttempt, overide, botGrab, raycastDistance);
			});
			return true;
		}
	}
	[BepInPlugin("com.github.nazo-x1.PerspectiveSwitcher", "PerspectiveSwitcher", "0.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		private ConfigEntry<KeyCode> configSwitchPerspective;

		private bool _settingsInitialized;

		private MouseSensitivitySetting _mouseSensSetting;

		private ControllerSensitivitySetting _controllerSensSetting;

		private PerspectiveMode _currentMode;

		private readonly Dictionary<PerspectiveMode, IPerspectiveAdapter> _adapters = new Dictionary<PerspectiveMode, IPerspectiveAdapter>();

		public const string Id = "com.github.nazo-x1.PerspectiveSwitcher";

		internal static ManualLogSource Log { get; private set; }

		private IPerspectiveAdapter CurrentAdapter => _adapters[_currentMode];

		public static string Name => "PerspectiveSwitcher";

		public static string Version => "0.1.1";

		private void Awake()
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			configSwitchPerspective = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Camera.Toggles", "SwitchPerspective", (KeyCode)118, "Switch the camera perspective (First -> Second -> Third)");
			_adapters[PerspectiveMode.FirstPerson] = new FirstPersonAdapter();
			_adapters[PerspectiveMode.SecondPerson] = new OrbitPerspectiveAdapter(this, PerspectiveMode.SecondPerson, 1);
			_adapters[PerspectiveMode.ThirdPerson] = new OrbitPerspectiveAdapter(this, PerspectiveMode.ThirdPerson, -1);
			MainCameraMovement.LateUpdate += new hook_LateUpdate(MainCameraMovement_LateUpdate);
			MainCameraMovement.CharacterCam += new hook_CharacterCam(MainCameraMovement_CharacterCam);
			CharacterClimbing.TryToStartWallClimb += new hook_TryToStartWallClimb(CharacterClimbing_TryStartWallClimb);
			Log.LogInfo((object)$"Plugin {Name} is loaded! Current mode: {_currentMode}");
		}

		internal void EnsureSettings()
		{
			if (!_settingsInitialized && (Object)(object)GameHandler.Instance != (Object)null && GameHandler.Instance.SettingsHandler != null)
			{
				_mouseSensSetting = GameHandler.Instance.SettingsHandler.GetSetting<MouseSensitivitySetting>();
				_controllerSensSetting = GameHandler.Instance.SettingsHandler.GetSetting<ControllerSensitivitySetting>();
				_settingsInitialized = true;
			}
		}

		internal float GetSensitivityMultiplier()
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Invalid comparison between Unknown and I4
			if (!_settingsInitialized)
			{
				return 1f;
			}
			if ((int)InputHandler.GetCurrentUsedInputScheme() != 1)
			{
				return ((FloatSetting)_mouseSensSetting).Value;
			}
			return ((FloatSetting)_controllerSensSetting).Value;
		}

		internal void WithTemporaryCameraPosition(Vector3 tempPosition, Action action)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			Transform transform = ((Component)MainCamera.instance).transform;
			Vector3 position = transform.position;
			try
			{
				transform.position = tempPosition;
				action();
			}
			finally
			{
				transform.position = position;
			}
		}

		private void SwitchToNextMode()
		{
			_currentMode = _currentMode switch
			{
				PerspectiveMode.FirstPerson => PerspectiveMode.SecondPerson, 
				PerspectiveMode.SecondPerson => PerspectiveMode.ThirdPerson, 
				_ => PerspectiveMode.FirstPerson, 
			};
			Log.LogInfo((object)$"Switched perspective to: {_currentMode}");
		}

		private void MainCameraMovement_LateUpdate(orig_LateUpdate orig, MainCameraMovement self)
		{
			//IL_0006: 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 (Input.GetKeyDown(configSwitchPerspective.Value))
			{
				SwitchToNextMode();
			}
			EnsureSettings();
			float y = Input.mouseScrollDelta.y;
			CurrentAdapter.OnLateUpdate(self, y);
			orig.Invoke(self);
		}

		private void MainCameraMovement_CharacterCam(orig_CharacterCam orig, MainCameraMovement self)
		{
			if (!CurrentAdapter.OnCharacterCam(orig, self))
			{
				orig.Invoke(self);
			}
		}

		private void CharacterClimbing_TryStartWallClimb(orig_TryToStartWallClimb orig, CharacterClimbing self, bool forceAttempt, Vector3 overide, bool botGrab, float raycastDistance)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			if (!CurrentAdapter.OnTryToStartWallClimb(orig, self, forceAttempt, overide, botGrab, raycastDistance))
			{
				orig.Invoke(self, forceAttempt, overide, botGrab, raycastDistance);
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}