Decompiled source of SimpleZoom v1.2.0

SimpleZoom.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SimpleZoom")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+a4efa521a1c12305b313228719e938e3b5cd98cb")]
[assembly: AssemblyProduct("SimpleZoom")]
[assembly: AssemblyTitle("SimpleZoom")]
[assembly: AssemblyVersion("1.0.0.0")]
[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;
		}
	}
}
[BepInPlugin("simplezoom_1.2.0", "Simple Zoom", "1.2.0")]
public class Plugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		ZoomConfig.Init((BaseUnityPlugin)(object)this);
		new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID).PatchAll();
	}
}
public class ZoomConfig
{
	public static KeyCode ZoomKey { get; private set; }

	public static float MaxZoom { get; private set; }

	public static float ZoomSpeed { get; private set; }

	public static float ScrollStep { get; private set; }

	public static bool FocusClarityEnabled { get; private set; }

	public static float FocusClarityDelay { get; private set; }

	public static float FocusClarityFadeSpeed { get; private set; }

	public static float FocusClarityFogReduction { get; private set; }

	public static int CoolPerkMachine { get; private set; }

	public static void Init(BaseUnityPlugin plugin)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Expected O, but got Unknown
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Expected O, but got Unknown
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_0182: Expected O, but got Unknown
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01be: Expected O, but got Unknown
		ZoomKey = plugin.Config.Bind<KeyCode>("General", "Zoom Key", (KeyCode)103, "Key used to activate zoom").Value;
		MaxZoom = plugin.Config.Bind<float>("General", "Max Zoom", 600f, "Maximum zoom amount").Value;
		ZoomSpeed = plugin.Config.Bind<float>("General", "Zoom Speed", 8f, "Speed of FOV change").Value;
		ScrollStep = plugin.Config.Bind<float>("General", "Scroll Step", 50f, "Step size when adjusting zoom with mouse wheel").Value;
		FocusClarityEnabled = plugin.Config.Bind<bool>("Focus Clarity", "Enabled", true, "Reduce fog after holding zoom for a moment, scaled by zoom depth").Value;
		FocusClarityDelay = plugin.Config.Bind<float>("Focus Clarity", "Delay", 0.75f, new ConfigDescription("Seconds of held zoom before fog reduction starts following zoom depth", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 5f), Array.Empty<object>())).Value;
		FocusClarityFadeSpeed = plugin.Config.Bind<float>("Focus Clarity", "Fade Speed", 2.5f, new ConfigDescription("How quickly fog reduction fades in and out", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 20f), Array.Empty<object>())).Value;
		FocusClarityFogReduction = plugin.Config.Bind<float>("Focus Clarity", "Fog Reduction", 0.65f, new ConfigDescription("Fraction of fog removed at maximum zoom focus, from 0 to 1", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>())).Value;
		CoolPerkMachine = plugin.Config.Bind<int>("General", "Cool Perk Machine", 70, new ConfigDescription("FOV while using Perk Machine or computers (it only for fun and doesn't make sense)", (AcceptableValueBase)(object)new AcceptableValueRange<int>(60, 140), Array.Empty<object>())).Value;
	}
}
[HarmonyPatch]
public static class ZoomPatch
{
	[HarmonyPatch(typeof(UT_CameraTakeover), "Start")]
	public static class CameraTakeoverPatch
	{
		[HarmonyPrefix]
		public static bool CoolPerkMachine(UT_CameraTakeover __instance)
		{
			__instance.fov = ZoomConfig.CoolPerkMachine;
			return true;
		}
	}

	private const float MinFov = 10f;

	private const float MaxFov = 140f;

	private const float MinZoomBase = 10f;

	private const float ZoomSpeedScale = 0.4f;

	private const float ScrollStepScale = 0.2f;

	private const float MinZoomSensitivityScale = 0.25f;

	private static float _zoomOffset;

	private static float _targetZoomOffset;

	private static float _zoomBase;

	private static float _focusHoldTime;

	private static float _focusAmount;

	private static readonly FieldInfo _smoothedFOVField;

	private static readonly FieldInfo _camSpeedField;

	static ZoomPatch()
	{
		_zoomOffset = 0f;
		_targetZoomOffset = 0f;
		_zoomBase = 60f;
		_focusHoldTime = 0f;
		_focusAmount = 0f;
		Type typeFromHandle = typeof(ENT_Player);
		_smoothedFOVField = typeFromHandle.GetField("smoothedFOV", BindingFlags.Instance | BindingFlags.NonPublic);
		_camSpeedField = typeFromHandle.GetField("camSpeed", BindingFlags.Instance | BindingFlags.NonPublic);
	}

	[HarmonyPatch(typeof(ENT_Player), "LateUpdate")]
	[HarmonyPrefix]
	public static void LateUpdatePrefix(ENT_Player __instance)
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.cam == (Object)null)
		{
			return;
		}
		bool flag = false;
		if (!__instance.IsLocked() && !__instance.IsInputLocked())
		{
			float maxZoomBase = GetMaxZoomBase(__instance);
			float y = Input.mouseScrollDelta.y;
			if (Mathf.Abs(y) > 0.01f)
			{
				_zoomBase = Mathf.Clamp(_zoomBase + y * ZoomConfig.ScrollStep * 0.2f, 10f, maxZoomBase);
			}
			else
			{
				_zoomBase = Mathf.Clamp(_zoomBase, 10f, maxZoomBase);
			}
			flag = Input.GetKey(ZoomConfig.ZoomKey);
			_targetZoomOffset = (flag ? (0f - _zoomBase) : 0f);
		}
		else
		{
			_targetZoomOffset = 0f;
		}
		float num = 1f - Mathf.Exp((0f - ZoomConfig.ZoomSpeed) * 0.4f * Time.deltaTime);
		_zoomOffset = Mathf.Lerp(_zoomOffset, _targetZoomOffset, num);
		_zoomOffset = Mathf.Clamp(_zoomOffset, 0f - GetMaxZoomBase(__instance), 0f);
		UpdateFocusClarity(flag, __instance);
	}

	[HarmonyPatch(typeof(ENT_Player), "LateUpdate")]
	[HarmonyPostfix]
	public static void LateUpdatePostfix(ENT_Player __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.cam == (Object)null) && !__instance.IsLocked() && !(Mathf.Abs(_zoomOffset) < 0.001f))
		{
			float baseFov = GetBaseFov(__instance);
			__instance.cam.fieldOfView = Mathf.Clamp(baseFov + _zoomOffset, 10f, 140f);
		}
	}

	[HarmonyPatch(typeof(ENT_Player), "MouseLook")]
	[HarmonyPrefix]
	public static void MouseLookPrefix(ENT_Player __instance, ref float __state)
	{
		__state = float.NaN;
		if (!((Object)(object)__instance == (Object)null) && !(_camSpeedField == null) && !(Mathf.Abs(_zoomOffset) < 0.001f))
		{
			float num = (__state = (float)_camSpeedField.GetValue(__instance));
			_camSpeedField.SetValue(__instance, num * GetZoomSensitivityScale(__instance));
		}
	}

	[HarmonyPatch(typeof(ENT_Player), "MouseLook")]
	[HarmonyPostfix]
	public static void MouseLookPostfix(ENT_Player __instance, float __state)
	{
		if (!((Object)(object)__instance == (Object)null) && !(_camSpeedField == null) && !float.IsNaN(__state))
		{
			_camSpeedField.SetValue(__instance, __state);
		}
	}

	[HarmonyPatch(typeof(FXManager), "CameraFX")]
	[HarmonyPostfix]
	public static void CameraFXPostfix(Camera camera)
	{
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		if (ZoomConfig.FocusClarityEnabled && !(_focusAmount <= 0.001f) && !((Object)(object)camera == (Object)null) && FXManager.showFog && FXManager.fxData != null)
		{
			ENT_Player player = ENT_Player.GetPlayer();
			if (!((Object)(object)player == (Object)null) && !((Object)(object)player.cam != (Object)(object)camera))
			{
				float num = Mathf.Clamp01(ZoomConfig.FocusClarityFogReduction) * _focusAmount;
				float num2 = Mathf.Max(0f, FXManager.fxData.fogMultiplier * (1f - num));
				Vector4 val = default(Vector4);
				((Vector4)(ref val))..ctor(FXManager.fxData.fog.r, FXManager.fxData.fog.g, FXManager.fxData.fog.b, num2);
				Shader.SetGlobalVector("_FOG", val);
				Shader.SetGlobalFloat("_FOGMULT", num2);
				Shader.SetGlobalFloat("_FOGDITHERAMOUNT", Mathf.Lerp(FXManager.fxData.fogDitherAmount, 0f, num));
			}
		}
	}

	private static void UpdateFocusClarity(bool zoomHeld, ENT_Player player)
	{
		float num = 0f;
		if (ZoomConfig.FocusClarityEnabled && zoomHeld)
		{
			_focusHoldTime += Time.deltaTime;
			if (_focusHoldTime >= ZoomConfig.FocusClarityDelay)
			{
				num = GetZoomDepth(player);
			}
		}
		else
		{
			_focusHoldTime = 0f;
		}
		float num2 = Mathf.Max(0.1f, ZoomConfig.FocusClarityFadeSpeed);
		float num3 = 1f - Mathf.Exp((0f - num2) * Time.deltaTime);
		_focusAmount = Mathf.Lerp(_focusAmount, num, num3);
	}

	private static float GetZoomDepth(ENT_Player player)
	{
		float maxZoomBase = GetMaxZoomBase(player);
		if (maxZoomBase <= 0f)
		{
			return 0f;
		}
		return Mathf.Clamp01((0f - _zoomOffset) / maxZoomBase);
	}

	private static float GetBaseFov(ENT_Player player)
	{
		float num = player.cam.fieldOfView;
		if (_smoothedFOVField?.GetValue(player) is float num2)
		{
			num = num2;
		}
		return Mathf.Clamp(num, 10f, 140f);
	}

	private static float GetZoomSensitivityScale(ENT_Player player)
	{
		float baseFov = GetBaseFov(player);
		float num = Mathf.Clamp(baseFov + _zoomOffset, 10f, 140f);
		float num2 = Mathf.Tan(baseFov * (MathF.PI / 180f) * 0.5f);
		float num3 = Mathf.Tan(num * (MathF.PI / 180f) * 0.5f);
		if (num2 <= 0f)
		{
			return 1f;
		}
		return Mathf.Clamp(num3 / num2, 0.25f, 1f);
	}

	private static float GetMaxZoomBase(ENT_Player player)
	{
		float num = Mathf.Max(10f, GetBaseFov(player) - 10f);
		return Mathf.Max(10f, Mathf.Min(ZoomConfig.MaxZoom, num));
	}
}