Decompiled source of TrueSens v1.0.4

BepInEx/plugins/TrueSens/TrueSens.dll

Decompiled 9 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ComputerysModdingUtilities;
using FishNet.Object;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("TrueSens")]
[assembly: AssemblyDescription("TrueSens for STRAFTAT. Dorsth was here.")]
[assembly: StraftatMod(true)]
[assembly: AssemblyVersion("1.0.2.0")]
namespace TrueSens;

[BepInPlugin("dorsth.straftat.truesens", "TrueSens", "1.0.2")]
public sealed class Plugin : BaseUnityPlugin
{
	public enum Scaling
	{
		Quake,
		CrosshairOneToOne
	}

	public const string Guid = "dorsth.straftat.truesens";

	public const string Name = "TrueSens";

	public const string Version = "1.0.2";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> On;

	internal static ConfigEntry<int> Dpi;

	internal static ConfigEntry<float> Cm360;

	internal static ConfigEntry<Scaling> Zoom;

	private void Awake()
	{
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Expected O, but got Unknown
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Expected O, but got Unknown
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		On = ((BaseUnityPlugin)this).Config.Bind<bool>("Sensitivity", "Enabled", true, "Set your sensitivity from your DPI and cm/360. Off: the game's own sensitivity settings.");
		Dpi = ((BaseUnityPlugin)this).Config.Bind<int>("Sensitivity", "Mouse DPI", 800, new ConfigDescription("The DPI your mouse is set to.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(100, 32000), Array.Empty<object>()));
		Cm360 = ((BaseUnityPlugin)this).Config.Bind<float>("Sensitivity", "cm/360", 30f, new ConfigDescription("How far you move your mouse, in cm, for a full turn.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 150f), Array.Empty<object>()));
		Zoom = ((BaseUnityPlugin)this).Config.Bind<Scaling>("Sensitivity", "Zoom scaling", Scaling.Quake, "Quake: zoom sensitivity scales with the zoom, like Quake. CrosshairOneToOne: small moves at the crosshair match exactly at every zoom.");
		On.SettingChanged += delegate
		{
			if (!On.Value)
			{
				Restore();
			}
			Report();
		};
		Dpi.SettingChanged += delegate
		{
			Report();
		};
		Cm360.SettingChanged += delegate
		{
			Report();
		};
		new Harmony("dorsth.straftat.truesens").Patch((MethodBase)AccessTools.Method(typeof(FirstPersonController), "HandleMouseLook", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "LookPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"TrueSens 1.0.2 loaded. Dorsth was here.");
		Report();
	}

	internal static float HipSens()
	{
		return 9144f / (Mathf.Max(5f, Cm360.Value) * (float)Mathf.Max(100, Dpi.Value));
	}

	private static void Report()
	{
		if (On.Value)
		{
			Log.LogInfo((object)("Sensitivity " + HipSens().ToString("0.####") + " for " + Dpi.Value + " dpi at " + Cm360.Value.ToString("0.##") + " cm/360"));
		}
	}

	private static float Horizontal(float vertical, float aspect)
	{
		return 2f * Mathf.Atan(Mathf.Tan(vertical * 0.5f * ((float)Math.PI / 180f)) * aspect) * 57.29578f;
	}

	internal static float ZoomFactor(float zoomFov, float hipFov, float aspect)
	{
		if (zoomFov <= 0f || hipFov <= 0f || zoomFov >= hipFov)
		{
			return 1f;
		}
		if (Zoom.Value == Scaling.CrosshairOneToOne)
		{
			return Mathf.Tan(zoomFov * 0.5f * ((float)Math.PI / 180f)) / Mathf.Tan(hipFov * 0.5f * ((float)Math.PI / 180f));
		}
		return Horizontal(zoomFov, aspect) / Horizontal(hipFov, aspect);
	}

	private static void LookPrefix(FirstPersonController __instance)
	{
		if (On.Value && ((NetworkBehaviour)__instance).IsOwner && (!((Object)(object)__instance.pauseManager != (Object)null) || !__instance.pauseManager.gamepad))
		{
			float num = (__instance.lookSpeedY = (__instance.lookSpeedX = HipSens()));
			float aspect = (((Object)(object)__instance.playerCamera != (Object)null) ? __instance.playerCamera.aspect : ((float)Screen.width / (float)Mathf.Max(1, Screen.height)));
			__instance.lookSpeedAim = (__instance.lookSpeedAimNoScope = num * ZoomFactor(__instance.zoomFOV, __instance.defaultFOV, aspect));
		}
	}

	private static void Restore()
	{
		try
		{
			Settings instance = Settings.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			FirstPersonController[] array = Object.FindObjectsOfType<FirstPersonController>();
			foreach (FirstPersonController val in array)
			{
				if (!((Object)(object)val == (Object)null) && ((NetworkBehaviour)val).IsOwner)
				{
					val.lookSpeedX = (instance.toggleTwoAxis ? instance.horizontalSensitivity : instance.mouseSensitivity);
					val.lookSpeedY = (instance.toggleTwoAxis ? instance.verticalSensitivity : instance.mouseSensitivity);
					val.lookSpeedAim = instance.mouseAimScopeSensitivity;
					val.lookSpeedAimNoScope = instance.mouseAimSensitivity;
				}
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Could not restore the game's sensitivity: " + ex.Message));
		}
	}
}