Decompiled source of DynamicCamera v1.0.4

BepInEx/plugins/DynamicCamera/DynamicCamera.dll

Decompiled 10 months ago
using System;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using Reptile;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("DynamicCamera")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Makes the field of view of the game camera dynamic, to increase the feeling of speed.")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3+b89f498df1e4632eb612ff762ef0abbca372e9a1")]
[assembly: AssemblyProduct("DynamicCamera")]
[assembly: AssemblyTitle("DynamicCamera")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.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 DynamicCamera
{
	[BepInPlugin("DynamicCamera", "DynamicCamera", "1.0.3")]
	public class Plugin : BaseUnityPlugin
	{
		private float _currentFOV;

		private float _currentFOVT;

		private float _fovTangent;

		private readonly AnimationCurve _easeIn = new AnimationCurve((Keyframe[])(object)new Keyframe[2]
		{
			new Keyframe(0f, 0f, 0f, 0f),
			new Keyframe(1f, 1f, 2f, 0f)
		});

		public static Plugin Instance { get; private set; }

		public float DefaultFOV { get; private set; }

		public float MaxSpeed { get; private set; }

		public ConfigEntry<bool> UseTotalSpeed { get; private set; }

		public ConfigEntry<bool> EnableDolly { get; private set; }

		public ConfigEntry<bool> UseCustomMinFOV { get; private set; }

		public ConfigEntry<float> MinFOV { get; private set; }

		public ConfigEntry<float> MaxFOV { get; private set; }

		public ConfigEntry<float> MaxSpeedKmh { get; private set; }

		public ConfigEntry<float> AdjustmentSpeed { get; private set; }

		private void Awake()
		{
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin DynamicCamera is loaded!");
			EnableDolly = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Enable Dolly Zoom Effect", true, "Makes the FOV change while the visual position of the camera doesn't.");
			UseCustomMinFOV = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Use Custom Minimum FOV", false, "Allows overriding the minimum FOV with a custom value.");
			MinFOV = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "Custom Minimum FOV", 64f, new ConfigDescription("The FOV that will be reached at no speed.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(30f, 90f), Array.Empty<object>()));
			MaxFOV = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "Maximum FOV", 80f, new ConfigDescription("The FOV that will be reached at max speed.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(30f, 90f), Array.Empty<object>()));
			AdjustmentSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "Adjustment Speed", 3.5f, "How fast the FOV adjusts in degrees per second.");
			UseTotalSpeed = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Use Total Speed", true, "Whether to use the total speed or only the lateral (horizontal) speed.");
			MaxSpeedKmh = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "Maximum Speed (km/h)", 60f, (ConfigDescription)null);
			MaxSpeed = MaxSpeedKmh.Value / 3.6f;
			Instance = this;
			Harmony val = new Harmony("sgiygas.fastCamera");
			val.PatchAll();
		}

		public void Initialize(float defaultFOV)
		{
			if (UseCustomMinFOV.Value)
			{
				defaultFOV = MinFOV.Value;
			}
			DefaultFOV = defaultFOV;
			_currentFOV = defaultFOV;
			_currentFOVT = 0f;
			if (EnableDolly.Value)
			{
				_fovTangent = Mathf.Tan(defaultFOV * 0.5f * ((float)Math.PI / 180f));
			}
		}

		public float UpdateFOV(float playerSpeedNormalized)
		{
			float num = AdjustmentSpeed.Value * Core.dt;
			if (_currentFOVT < playerSpeedNormalized)
			{
				_currentFOVT += num;
				_currentFOVT = Mathf.Min(_currentFOVT, playerSpeedNormalized);
			}
			else if (_currentFOVT > playerSpeedNormalized)
			{
				_currentFOVT -= num;
				_currentFOVT = Mathf.Max(_currentFOVT, playerSpeedNormalized);
			}
			float num2 = _easeIn.Evaluate(_currentFOVT);
			_currentFOV = Mathf.Lerp(DefaultFOV, MaxFOV.Value, num2);
			return _currentFOV;
		}

		public float UpdateDistance(float targetDistance)
		{
			return GetDistanceRelativeToFOV(targetDistance, _currentFOV);
		}

		private float GetDistanceRelativeToFOV(float targetDistance, float fieldOfView)
		{
			float num = 2f * targetDistance * _fovTangent;
			return num / (2f * Mathf.Tan(0.5f * fieldOfView * ((float)Math.PI / 180f)));
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "DynamicCamera";

		public const string PLUGIN_NAME = "DynamicCamera";

		public const string PLUGIN_VERSION = "1.0.3";
	}
}
namespace DynamicCamera.Patches
{
	[HarmonyPatch(typeof(GameplayCamera), "Awake")]
	public class GameplayCameraAwakePatch
	{
		public static void Postfix(Camera ___cam)
		{
			Plugin.Instance.Initialize(___cam.fieldOfView);
		}
	}
	[HarmonyPatch(typeof(GameplayCamera), "UpdateCamera")]
	public class GameplayCameraUpdatePatch
	{
		public static void Postfix(GameplayCamera __instance, Camera ___cam, Transform ___realTf, Player ___player, CameraMode ___cameraMode, CameraMode ___cameraModePrev, float ___cameraModeFade)
		{
			//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)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: 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)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//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_00a3: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: 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_00be: 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)
			Plugin instance = Plugin.Instance;
			float num = (instance.UseTotalSpeed.Value ? ___player.GetTotalSpeed() : ___player.GetForwardSpeed());
			float playerSpeedNormalized = num / instance.MaxSpeed;
			___cam.fieldOfView = instance.UpdateFOV(playerSpeedNormalized);
			if (instance.EnableDolly.Value)
			{
				Vector3 val;
				if (___cameraModePrev != null)
				{
					float num2 = FadeOutCubic(___cameraModeFade);
					val = Vector3.Lerp(___cameraModePrev.lookAtPos, ___cameraMode.lookAtPos, num2);
				}
				else
				{
					val = ___cameraMode.lookAtPos;
				}
				Vector3 val2 = ___realTf.position - val;
				float magnitude = ((Vector3)(ref val2)).magnitude;
				Vector3 val3 = val2 / magnitude;
				Vector3 val4 = val3 * instance.UpdateDistance(magnitude);
				___realTf.position = val + val4;
			}
		}

		private static float FadeOutCubic(float f)
		{
			f = 1f - f;
			return 1f - f * f * f;
		}
	}
}