Decompiled source of CameraTweaks v1.2.4

BepInEx/plugins/CameraTweaks/CameraTweaks.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using ComputerysModdingUtilities;
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using FishNet.Object;
using HarmonyLib;
using ModMenu.Api;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CameraTweaks")]
[assembly: AssemblyDescription("Takes out the camera movement STRAFTAT adds on its own, one switch each.")]
[assembly: StraftatMod(true)]
[assembly: AssemblyVersion("1.2.4.0")]
namespace CameraTweaks;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("dorsth.straftat.cameratweaks", "CameraTweaks", "1.2.4")]
public sealed class Plugin : BaseUnityPlugin
{
	public const string Guid = "dorsth.straftat.cameratweaks";

	public const string Name = "CameraTweaks";

	public const string Version = "1.2.4";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> NoTilt;

	internal static ConfigEntry<bool> NoFallShake;

	internal static ConfigEntry<bool> NoJumpShake;

	internal static ConfigEntry<bool> NoVaultShake;

	internal static ConfigEntry<bool> BetterRecoil;

	internal static ConfigEntry<int> SprintFov;

	[MethodImpl(MethodImplOptions.NoInlining)]
	private static void ModMenuDescription()
	{
		ModMenuCustomisation.SetPluginDescription("Camera without roll, landing and jump shake, or sprint FOV, and recoil that kicks your aim instead of the camera.");
	}

	private void Awake()
	{
		//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fb: Expected O, but got Unknown
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_0112: Expected O, but got Unknown
		//IL_0165: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Expected O, but got Unknown
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Expected O, but got Unknown
		//IL_0250: Unknown result type (might be due to invalid IL or missing references)
		//IL_0255: Unknown result type (might be due to invalid IL or missing references)
		//IL_0265: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		if (Chainloader.PluginInfos.ContainsKey("kestrel.straftat.modmenu"))
		{
			try
			{
				ModMenuDescription();
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Mod Menu description: " + ex.Message));
			}
		}
		SceneManager.sceneLoaded += delegate
		{
			Recoil.Forget();
		};
		NoTilt = ((BaseUnityPlugin)this).Config.Bind<bool>("Camera", "NoTilt", true, "On: the camera stops rolling to the side when you strafe, slide, slide along a wall or move your mouse sideways.");
		NoFallShake = ((BaseUnityPlugin)this).Config.Bind<bool>("Camera", "NoFallShake", true, "On: no camera shake or dip when you land.");
		NoJumpShake = ((BaseUnityPlugin)this).Config.Bind<bool>("Camera", "NoJumpShake", true, "On: no camera shake when you jump out of a slide, wall jump or hit a ceiling.");
		NoVaultShake = ((BaseUnityPlugin)this).Config.Bind<bool>("Camera", "NoVaultShake", false, "On: no camera shake when you vault over something.");
		SprintFov = ((BaseUnityPlugin)this).Config.Bind<int>("Camera", "SprintFovIncrease", 0, new ConfigDescription("How many degrees the FOV widens when you sprint. Sliding widens it by the same share as in the game. 0: it stays what you set in the game.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 30), Array.Empty<object>()));
		SprintFov.SettingChanged += delegate
		{
			SpeedFov.ApplyAll();
		};
		BetterRecoil = ((BaseUnityPlugin)this).Config.Bind<bool>("Camera", "BetterRecoil", true, "On: the AR, AK and warden kick your aim instead of the camera. When you let go it comes back down by the kick you didn't pull down yourself, so taps and bursts come back to where you aimed and a pulled down spray doesn't swing below it.");
		Harmony val = new Harmony("dorsth.straftat.cameratweaks");
		Shakes.Patch(val);
		try
		{
			val.Patch((MethodBase)AccessTools.Method(typeof(FirstPersonController), "Start", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(SpeedFov), "Started", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		catch (Exception ex2)
		{
			Log.LogWarning((object)("The sprint and slide FOV could not be switched off (game update?): " + ex2.Message));
		}
		val.Patch((MethodBase)AccessTools.Method(typeof(FirstPersonController), "HandleCameraController", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Roll), "Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		string[] array = new string[2] { "CameraRevolverAnimation", "AltCameraRevolverAnimation" };
		foreach (string text in array)
		{
			MethodInfo methodInfo = AccessTools.Method(typeof(Weapon), text, (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Recoil), "Prefix", (Type[])null)
				{
					priority = 0
				}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"CameraTweaks 1.2.4 loaded. Dorsth was here.");
		((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
	}

	private void Update()
	{
		Recoil.Tick();
	}
}
internal static class SpeedFov
{
	private static FieldRef<FirstPersonController, float> Run;

	private static FieldRef<FirstPersonController, float> Slide;

	private static FieldRef<FirstPersonController, float> RunSlide;

	private static bool failed;

	private static bool known;

	private static float run;

	private static float slide;

	private static float runSlide;

	internal static void ApplyAll()
	{
		if (known)
		{
			FirstPersonController[] array = Object.FindObjectsOfType<FirstPersonController>();
			for (int i = 0; i < array.Length; i++)
			{
				Set(array[i]);
			}
		}
	}

	private static void Set(FirstPersonController p)
	{
		float num = ((run > 0.01f) ? ((float)Mathf.Clamp(Plugin.SprintFov.Value, 0, 30) / run) : 0f);
		Run.Invoke(p) = run * num;
		Slide.Invoke(p) = slide * num;
		RunSlide.Invoke(p) = runSlide * num;
	}

	internal static void Started(FirstPersonController __instance)
	{
		if (failed || (Object)(object)__instance == (Object)null)
		{
			return;
		}
		try
		{
			if (Run == null)
			{
				Run = AccessTools.FieldRefAccess<FirstPersonController, float>("distToRunFov");
				Slide = AccessTools.FieldRefAccess<FirstPersonController, float>("distToSlideFov");
				RunSlide = AccessTools.FieldRefAccess<FirstPersonController, float>("distToRunSlideFov");
			}
			if (!known)
			{
				known = true;
				run = Run.Invoke(__instance);
				slide = Slide.Invoke(__instance);
				runSlide = RunSlide.Invoke(__instance);
			}
			Set(__instance);
		}
		catch (Exception ex)
		{
			failed = true;
			Plugin.Log.LogWarning((object)("The sprint and slide FOV could not be switched off (game update?): " + ex.Message));
		}
	}
}
internal static class Roll
{
	private static readonly FieldRef<FirstPersonController, float> Tilt = AccessTools.FieldRefAccess<FirstPersonController, float>("tiltAmount");

	private static readonly FieldRef<FirstPersonController, float> SlideTilt = AccessTools.FieldRefAccess<FirstPersonController, float>("slideTiltAmount");

	private static readonly FieldRef<FirstPersonController, float> Mouse = AccessTools.FieldRefAccess<FirstPersonController, float>("rotationAmount");

	private static bool known;

	private static float tilt;

	private static float slideTilt;

	private static float mouse;

	public static void Prefix(FirstPersonController __instance)
	{
		if (((NetworkBehaviour)__instance).IsOwner)
		{
			if (!known)
			{
				known = true;
				tilt = Tilt.Invoke(__instance);
				slideTilt = SlideTilt.Invoke(__instance);
				mouse = Mouse.Invoke(__instance);
			}
			Tilt.Invoke(__instance) = ((!Plugin.NoTilt.Value) ? tilt : 0f);
			SlideTilt.Invoke(__instance) = ((!Plugin.NoTilt.Value) ? slideTilt : 0f);
			Mouse.Invoke(__instance) = ((!Plugin.NoTilt.Value) ? mouse : 0f);
			if (Plugin.NoTilt.Value)
			{
				__instance.wallSlideLean = 0f;
			}
		}
	}
}
internal static class Shakes
{
	private static readonly MethodInfo Punch = AccessTools.Method(typeof(ShortcutExtensions), "DOPunchRotation", new Type[5]
	{
		typeof(Transform),
		typeof(Vector3),
		typeof(float),
		typeof(int),
		typeof(float)
	}, (Type[])null);

	private static readonly MethodInfo LocalRotate = AccessTools.Method(typeof(ShortcutExtensions), "DOLocalRotate", new Type[4]
	{
		typeof(Transform),
		typeof(Vector3),
		typeof(float),
		typeof(RotateMode)
	}, (Type[])null);

	private static readonly Dictionary<string, string[]> Punches = new Dictionary<string, string[]>
	{
		{
			"OnControllerColliderHit",
			new string[2] { "JumpShake", "JumpShake" }
		},
		{
			"Jump",
			new string[4] { "JumpShake", "JumpShake", "JumpShake", "JumpShake" }
		},
		{
			"ApplyFinalMovements",
			new string[2] { "JumpShake", "FallShake" }
		},
		{
			"CheckForVault",
			new string[1] { "VaultShake" }
		}
	};

	public static void Patch(Harmony h)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		HarmonyMethod val = new HarmonyMethod(typeof(Shakes), "Transpiler", (Type[])null);
		foreach (string key in Punches.Keys)
		{
			MethodInfo methodInfo = AccessTools.Method(typeof(FirstPersonController), key, (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Plugin.Log.LogWarning((object)(key + " not found, its camera shake stays"));
			}
			else
			{
				h.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}
	}

	private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase __originalMethod)
	{
		List<CodeInstruction> list = instructions.ToList();
		string name = __originalMethod.Name;
		string[] array = Punches[name];
		List<CodeInstruction> list2 = list.Where((CodeInstruction c) => CodeInstructionExtensions.Calls(c, Punch)).ToList();
		if (list2.Count != array.Length)
		{
			Plugin.Log.LogWarning((object)(name + ": " + list2.Count + " camera shakes where " + array.Length + " were expected, left as they are"));
			return list;
		}
		for (int num = 0; num < list2.Count; num++)
		{
			list2[num].opcode = OpCodes.Call;
			list2[num].operand = AccessTools.Method(typeof(Shakes), array[num], (Type[])null, (Type[])null);
		}
		if (name == "ApplyFinalMovements")
		{
			List<CodeInstruction> list3 = list.Where((CodeInstruction c) => CodeInstructionExtensions.Calls(c, LocalRotate)).ToList();
			if (list3.Count == 1)
			{
				list3[0].opcode = OpCodes.Call;
				list3[0].operand = AccessTools.Method(typeof(Shakes), "HighFall", (Type[])null, (Type[])null);
			}
			else
			{
				Plugin.Log.LogWarning((object)"ApplyFinalMovements: the high fall dip wasn't where it was expected, left as it is");
			}
		}
		return list;
	}

	private static Tweener Gate(bool on, Transform t, Vector3 p, float d, int v, float e)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		if (!on)
		{
			return null;
		}
		return ShortcutExtensions.DOPunchRotation(t, p, d, v, e);
	}

	public static Tweener JumpShake(Transform t, Vector3 p, float d, int v, float e)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		return Gate(!Plugin.NoJumpShake.Value, t, p, d, v, e);
	}

	public static Tweener FallShake(Transform t, Vector3 p, float d, int v, float e)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		return Gate(!Plugin.NoFallShake.Value, t, p, d, v, e);
	}

	public static Tweener VaultShake(Transform t, Vector3 p, float d, int v, float e)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		return Gate(!Plugin.NoVaultShake.Value, t, p, d, v, e);
	}

	public static TweenerCore<Quaternion, Vector3, QuaternionOptions> HighFall(Transform t, Vector3 end, float d, RotateMode mode)
	{
		//IL_000f: 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)
		if (Plugin.NoFallShake.Value)
		{
			return null;
		}
		return ShortcutExtensions.DOLocalRotate(t, end, d, mode);
	}
}
internal static class Recoil
{
	private static readonly FieldRef<Weapon, Vector3> Kick = AccessTools.FieldRefAccess<Weapon, Vector3>("recoil");

	private static FirstPersonController player;

	private static Weapon weapon;

	private static Vector2 left;

	private static Vector2 owed;

	private static float time = 0.1f;

	private static float lastPitch;

	private static float lastYaw;

	private static bool haveLast;

	internal static void Forget()
	{
		//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_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		player = null;
		weapon = null;
		left = (owed = Vector2.zero);
		haveLast = false;
	}

	public static bool Prefix(Weapon __instance, MethodBase __originalMethod, bool __runOriginal)
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: 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_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: 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_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: 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 (!__runOriginal || !Plugin.BetterRecoil.Value || !__instance.akAnim || !Swings(__instance))
		{
			return true;
		}
		FirstPersonController playerController = __instance.playerController;
		if ((Object)(object)playerController == (Object)null || !((NetworkBehaviour)playerController).IsOwner)
		{
			return true;
		}
		Vector3 val = Kick.Invoke(__instance) * ((__originalMethod.Name == "AltCameraRevolverAnimation") ? 3f : 1f);
		if ((Object)(object)player != (Object)(object)playerController)
		{
			player = playerController;
			left = (owed = Vector2.zero);
			haveLast = false;
		}
		weapon = __instance;
		Vector2 val2 = default(Vector2);
		((Vector2)(ref val2))..ctor(0f - val.x, 0f - val.y);
		left += val2;
		owed += val2;
		time = Mathf.Max(0.01f, __instance.duration);
		return false;
	}

	private static bool Swings(Weapon w)
	{
		ItemBehaviour val = (((Object)(object)w.behaviour != (Object)null) ? w.behaviour : ((Component)w).GetComponent<ItemBehaviour>());
		switch (((Object)(object)val != (Object)null && val.weaponName != null) ? val.weaponName.Trim().ToLowerInvariant() : "")
		{
		case "ar":
		case "ak":
		case "warden":
			return true;
		default:
		{
			string name = ((Object)((Component)w).gameObject).name;
			if (!name.StartsWith("AR15", StringComparison.Ordinal) && !name.StartsWith("AK-K", StringComparison.Ordinal))
			{
				return name.StartsWith("Warden", StringComparison.Ordinal);
			}
			return true;
		}
		}
	}

	public static void Tick()
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: 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)
		//IL_0018: 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_00ef: 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_00c5: 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_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0248: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_020b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0215: Unknown result type (might be due to invalid IL or missing references)
		//IL_0220: Unknown result type (might be due to invalid IL or missing references)
		//IL_0225: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_017f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0184: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		//IL_0191: Unknown result type (might be due to invalid IL or missing references)
		//IL_0193: Unknown result type (might be due to invalid IL or missing references)
		//IL_0198: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			left = (owed = Vector2.zero);
			haveLast = false;
			return;
		}
		float rotationX = player.rotationX;
		float y = ((Component)player).transform.eulerAngles.y;
		if (haveLast)
		{
			owed.x = Against(owed.x, rotationX - lastPitch);
			owed.y = Against(owed.y, Mathf.DeltaAngle(lastYaw, y));
		}
		Vector2 val = Vector2.zero;
		if (((Vector2)(ref left)).sqrMagnitude > 1E-08f)
		{
			float num = 1f - Mathf.Exp((0f - Time.deltaTime) * 3f / time);
			Vector2 val2 = left * num;
			left -= val2;
			val += val2;
		}
		else
		{
			left = Vector2.zero;
		}
		CameraShakeConstrains val3 = (((Object)(object)weapon != (Object)null) ? weapon.camAnimScript : null);
		if ((!((Object)(object)val3 != (Object)null) || val3.rotateBack) && ((Vector2)(ref owed)).sqrMagnitude > 1E-08f)
		{
			float num2 = (((Object)(object)val3 != (Object)null && val3.baseSpeed > 0f) ? val3.baseSpeed : 3f);
			Vector2 val4 = owed * Mathf.Min(1f, num2 * Time.deltaTime);
			owed -= val4;
			val -= val4;
		}
		else if (((Vector2)(ref owed)).sqrMagnitude <= 1E-08f)
		{
			owed = Vector2.zero;
		}
		if (((Vector2)(ref val)).sqrMagnitude > 1E-12f)
		{
			player.rotationX = Mathf.Clamp(player.rotationX + val.x, -90f, 90f);
			if (Mathf.Abs(val.y) > 1E-05f)
			{
				Transform transform = ((Component)player).transform;
				transform.rotation *= Quaternion.Euler(0f, val.y, 0f);
			}
		}
		lastPitch = player.rotationX;
		lastYaw = ((Component)player).transform.eulerAngles.y;
		haveLast = true;
	}

	private static float Against(float owed, float moved)
	{
		if (owed < 0f && moved > 0f)
		{
			return Mathf.Min(0f, owed + moved);
		}
		if (owed > 0f && moved < 0f)
		{
			return Mathf.Max(0f, owed + moved);
		}
		return owed;
	}
}