Decompiled source of Smooth Look Input v1.0.0

plugins/SmoothLookInput.dll

Decompiled 3 hours 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.Bootstrap;
using BepInEx.Configuration;
using IL.RoR2.CameraModes;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using Rewired;
using RiskOfOptions;
using RiskOfOptions.Options;
using RoR2.CameraModes;
using RoR2BepInExPack.Utilities;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SmoothLookInput")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SmoothLookInput")]
[assembly: AssemblyTitle("SmoothLookInput")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
[module: UnverifiableCode]
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 SmoothLookInput
{
	[Serializable]
	[BepInPlugin("com.brynzananas.smoothlookinput", "Smooth Look Input", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class SmoothLookInputPlugin : BaseUnityPlugin
	{
		public delegate void CameraSmoothDelegate(CameraModeBase cameraModeBase, ref CameraModeContext cameraModeContext, ref float smoothFactor);

		public delegate void CameraInputDelegate(CameraModeBase cameraModeBase, ref CameraModeContext cameraModeContext, ref Vector2 inputVector, bool isController);

		[CompilerGenerated]
		private static class <>O
		{
			public static Manipulator <0>__CameraModePlayerBasic_CollectLookInputInternal;

			public static CameraSmoothDelegate <1>__ModifySmoothFactorWithConfig;

			public static CameraInputDelegate <2>__ApplySmooth;

			public static <>A{00000041}<Vector2, CameraModeBase, CameraModeContext, bool> <3>__Handle;
		}

		public const string ModGuid = "com.brynzananas.smoothlookinput";

		public const string ModName = "Smooth Look Input";

		public const string ModVer = "1.0.0";

		private static FixedConditionalWeakTable<CameraModeBase, SmoothCameraModeBase> keyValuePairs = new FixedConditionalWeakTable<CameraModeBase, SmoothCameraModeBase>();

		public static ConfigEntry<float> SmoothConfig;

		private static bool _hooksSet;

		public static SmoothLookInputPlugin instance { get; private set; }

		public static event CameraSmoothDelegate GetSmoothFactor;

		public static event CameraInputDelegate ModifyLookInput;

		public void Awake()
		{
			instance = this;
			RiskOfOptionsCompatability.enabled = Chainloader.PluginInfos.ContainsKey("com.rune580.riskofoptions");
			if (SmoothConfig == null)
			{
				SmoothConfig = CreateConfig("Main", "Smooth Factor", 1f, "");
			}
			SetHooks();
		}

		public void OnDestroy()
		{
			UnsetHooks();
		}

		public static void SetHooks()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			if (!_hooksSet)
			{
				_hooksSet = true;
				object obj = <>O.<0>__CameraModePlayerBasic_CollectLookInputInternal;
				if (obj == null)
				{
					Manipulator val = CameraModePlayerBasic_CollectLookInputInternal;
					<>O.<0>__CameraModePlayerBasic_CollectLookInputInternal = val;
					obj = (object)val;
				}
				CameraModePlayerBasic.CollectLookInputInternal += (Manipulator)obj;
				GetSmoothFactor += ModifySmoothFactorWithConfig;
				ModifyLookInput += ApplySmooth;
			}
		}

		public static void UnsetHooks()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			if (_hooksSet)
			{
				_hooksSet = false;
				object obj = <>O.<0>__CameraModePlayerBasic_CollectLookInputInternal;
				if (obj == null)
				{
					Manipulator val = CameraModePlayerBasic_CollectLookInputInternal;
					<>O.<0>__CameraModePlayerBasic_CollectLookInputInternal = val;
					obj = (object)val;
				}
				CameraModePlayerBasic.CollectLookInputInternal -= (Manipulator)obj;
				GetSmoothFactor -= ModifySmoothFactorWithConfig;
				ModifyLookInput -= ApplySmooth;
			}
		}

		public static void ModifySmoothFactorWithConfig(CameraModeBase cameraModeBase, ref CameraModeContext cameraModeContext, ref float smoothFactor)
		{
			smoothFactor *= SmoothConfig.Value;
		}

		private static void CameraModePlayerBasic_CollectLookInputInternal(ILContext il)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Expected O, but got Unknown
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: 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_014f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02db: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e7: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(il);
			int id = 0;
			int num4 = default(int);
			int num3 = default(int);
			MethodReference val3 = default(MethodReference);
			if (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[8]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdloca(x, ref id),
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref num4),
				(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 2),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<Player>(x, "GetAxisRaw"),
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref num3),
				(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 3),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<Player>(x, "GetAxisRaw"),
				(Instruction x) => ILPatternMatchingExt.MatchCall(x, ref val3)
			}))
			{
				val.Emit(OpCodes.Ldloca, id);
				val.Emit(OpCodes.Ldarg_0);
				val.Emit(OpCodes.Ldarg_2);
				val.Emit(OpCodes.Ldc_I4_0);
				val.EmitDelegate<<>A{00000041}<Vector2, CameraModeBase, CameraModeContext, bool>>((<>A{00000041}<Vector2, CameraModeBase, CameraModeContext, bool>)Handle);
			}
			else
			{
				((BaseUnityPlugin)instance).Logger.LogError((object)(((MemberReference)il.Method).Name + " IL Hook 1 failed!"));
			}
			int id2 = 0;
			int num2 = default(int);
			int num = default(int);
			MethodReference val2 = default(MethodReference);
			if (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[8]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdloca(x, ref id2),
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref num2),
				(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 16),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<Player>(x, "GetAxisRaw"),
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref num),
				(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, 17),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt<Player>(x, "GetAxisRaw"),
				(Instruction x) => ILPatternMatchingExt.MatchCall(x, ref val2)
			}))
			{
				val.Emit(OpCodes.Ldloca, id);
				val.Emit(OpCodes.Ldarg_0);
				val.Emit(OpCodes.Ldarg_2);
				val.Emit(OpCodes.Ldc_I4_1);
				val.EmitDelegate<<>A{00000041}<Vector2, CameraModeBase, CameraModeContext, bool>>((<>A{00000041}<Vector2, CameraModeBase, CameraModeContext, bool>)Handle);
			}
			else
			{
				((BaseUnityPlugin)instance).Logger.LogError((object)(((MemberReference)il.Method).Name + " IL Hook 2 failed!"));
			}
		}

		public static void Handle(ref Vector2 vector2, CameraModeBase cameraModeBase, ref CameraModeContext cameraModeContext, bool isController)
		{
			SmoothLookInputPlugin.ModifyLookInput(cameraModeBase, ref cameraModeContext, ref vector2, isController);
		}

		private static void ApplySmooth(CameraModeBase cameraModeBase, ref CameraModeContext cameraModeContext, ref Vector2 inputVector, bool isController)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			SmoothCameraModeBase smoothCameraModeBase = GetSmoothCameraModeBase(cameraModeBase, inputVector);
			float smoothFactor = 1f;
			SmoothLookInputPlugin.GetSmoothFactor(cameraModeBase, ref cameraModeContext, ref smoothFactor);
			smoothCameraModeBase.currentMouseDelta = Vector2.Lerp(smoothCameraModeBase.currentMouseDelta, inputVector, 1f / smoothFactor);
			inputVector = smoothCameraModeBase.currentMouseDelta;
		}

		public static SmoothCameraModeBase GetSmoothCameraModeBase(CameraModeBase cameraModeBase, Vector2 vector2)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			SmoothCameraModeBase smoothCameraModeBase = default(SmoothCameraModeBase);
			if (!keyValuePairs.TryGetValue(cameraModeBase, ref smoothCameraModeBase))
			{
				smoothCameraModeBase = new SmoothCameraModeBase
				{
					currentMouseDelta = vector2
				};
				keyValuePairs.Add(cameraModeBase, smoothCameraModeBase);
			}
			return smoothCameraModeBase;
		}

		public static ConfigEntry<T> CreateConfig<T>(string section, string key, T defaultValue, string description)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Expected O, but got Unknown
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			ConfigDefinition val = new ConfigDefinition(section, key);
			ConfigDescription val2 = new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>());
			ConfigEntry<T> val3 = ((BaseUnityPlugin)instance).Config.Bind<T>(val, defaultValue, val2);
			if (RiskOfOptionsCompatability.enabled)
			{
				RiskOfOptionsCompatability.AddConfig<ConfigEntry<T>>(val3);
			}
			return val3;
		}
	}
	public class SmoothCameraModeBase
	{
		public Vector2 currentMouseDelta;
	}
	public static class RiskOfOptionsCompatability
	{
		public static bool enabled;

		public const string GUID = "com.rune580.riskofoptions";

		public static void AddConfig<T>(T config) where T : ConfigEntryBase
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Expected O, but got Unknown
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Expected O, but got Unknown
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Expected O, but got Unknown
			if (config is ConfigEntry<float>)
			{
				ModSettingsManager.AddOption((BaseOption)new FloatFieldOption(config as ConfigEntry<float>));
			}
			else if (config is ConfigEntry<bool>)
			{
				ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(config as ConfigEntry<bool>));
			}
			else if (config is ConfigEntry<int>)
			{
				ModSettingsManager.AddOption((BaseOption)new IntFieldOption(config as ConfigEntry<int>));
			}
			else if (config is ConfigEntry<string>)
			{
				ModSettingsManager.AddOption((BaseOption)new StringInputFieldOption(config as ConfigEntry<string>));
			}
			else
			{
				ModSettingsManager.AddOption((BaseOption)new ChoiceOption((ConfigEntryBase)(object)config));
			}
		}
	}
}