Decompiled source of EnhancedControls v1.1.0

EnhancedControls.dll

Decompiled 5 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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 = "")]
[assembly: AssemblyCompany("EnhancedControls")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("EnhancedControls")]
[assembly: AssemblyTitle("EnhancedControls")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace EnhancedControls
{
	[BepInPlugin("Mhz.EnhancedControls", "EnhancedControls", "1.1.0")]
	public class EnhancedControlsPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(PlayerController), "Update")]
		public class PlayerControllerUpdatePatch
		{
			private static bool _wasSprintingLastFrame;

			private static float _crouchDuration;

			private const float MinCrouchTime = 0.4f;

			private static bool _jumpedFromCrouch;

			private static bool _sprintKeyPressedDuringCrouch;

			private static FieldInfo? _jumpImpulseField;

			private static FieldInfo? _toggleCrouchField;

			private static FieldInfo? _toggleSprintField;

			private static MethodInfo? _checkBlockedMethod;

			private static Type? _playerCollisionStandType;

			private static FieldInfo? _playerCollisionStandInstanceField;

			private static bool _reflectionInitialized;

			private static void InitializeReflection(PlayerController playerController)
			{
				if (_reflectionInitialized)
				{
					return;
				}
				try
				{
					_jumpImpulseField = ((object)playerController).GetType().GetField("JumpImpulse", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					_toggleCrouchField = ((object)playerController).GetType().GetField("toggleCrouch", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					_toggleSprintField = ((object)playerController).GetType().GetField("toggleSprint", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					_playerCollisionStandType = Type.GetType("PlayerCollisionStand, Assembly-CSharp");
					if (_playerCollisionStandType != null)
					{
						_playerCollisionStandInstanceField = _playerCollisionStandType.GetField("instance", BindingFlags.Static | BindingFlags.Public);
						_checkBlockedMethod = _playerCollisionStandType.GetMethod("CheckBlocked", BindingFlags.Instance | BindingFlags.Public);
					}
					_reflectionInitialized = true;
				}
				catch (Exception ex)
				{
					ManualLogSource? logger = _logger;
					if (logger != null)
					{
						logger.LogError((object)("初始化反射缓存失败 | Failed to initialize reflection cache: " + ex.Message));
					}
				}
			}

			private static bool CheckCanStandUp()
			{
				if (!_reflectionInitialized || _playerCollisionStandInstanceField == null || _checkBlockedMethod == null)
				{
					return true;
				}
				try
				{
					object value = _playerCollisionStandInstanceField.GetValue(null);
					if (value == null)
					{
						return true;
					}
					return !(bool)_checkBlockedMethod.Invoke(value, null);
				}
				catch (Exception ex)
				{
					ManualLogSource? logger = _logger;
					if (logger != null)
					{
						logger.LogError((object)("检查头顶障碍物失败 | Failed to check overhead obstacles: " + ex.Message));
					}
					return true;
				}
			}

			private static bool IsJumping(PlayerController playerController)
			{
				if (!_reflectionInitialized || _jumpImpulseField == null)
				{
					return false;
				}
				try
				{
					return (bool)_jumpImpulseField.GetValue(playerController);
				}
				catch (Exception ex)
				{
					ManualLogSource? logger = _logger;
					if (logger != null)
					{
						logger.LogError((object)("获取JumpImpulse失败 | Failed to get JumpImpulse: " + ex.Message));
					}
					return false;
				}
			}

			private static void SetToggleCrouch(PlayerController playerController, bool value)
			{
				if (!_reflectionInitialized || _toggleCrouchField == null)
				{
					return;
				}
				try
				{
					_toggleCrouchField.SetValue(playerController, value);
				}
				catch (Exception ex)
				{
					ManualLogSource? logger = _logger;
					if (logger != null)
					{
						logger.LogError((object)("设置toggleCrouch失败 | Failed to set toggleCrouch: " + ex.Message));
					}
				}
			}

			private static void SetToggleSprint(PlayerController playerController, bool value)
			{
				if (!_reflectionInitialized || _toggleSprintField == null)
				{
					return;
				}
				try
				{
					_toggleSprintField.SetValue(playerController, value);
				}
				catch (Exception ex)
				{
					ManualLogSource? logger = _logger;
					if (logger != null)
					{
						logger.LogError((object)("设置toggleSprint失败 | Failed to set toggleSprint: " + ex.Message));
					}
				}
			}

			private static bool GetToggleSprint(PlayerController playerController)
			{
				if (!_reflectionInitialized || _toggleSprintField == null)
				{
					return false;
				}
				try
				{
					return (bool)_toggleSprintField.GetValue(playerController);
				}
				catch (Exception ex)
				{
					ManualLogSource? logger = _logger;
					if (logger != null)
					{
						logger.LogError((object)("获取toggleSprint失败 | Failed to get toggleSprint: " + ex.Message));
					}
					return false;
				}
			}

			[HarmonyPostfix]
			public static void Postfix(PlayerController __instance)
			{
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0023: Invalid comparison between Unknown and I4
				if (!_reflectionInitialized)
				{
					InitializeReflection(__instance);
				}
				if ((int)GameDirector.instance.currentState != 2)
				{
					return;
				}
				ConfigEntry<bool> val = Instance?._enableResetSprintOnStop;
				if (val != null && val.Value && InputManager.instance.InputToggleGet((InputKey)15))
				{
					bool toggleSprint = GetToggleSprint(__instance);
					bool flag = ((Vector3)(ref __instance.InputDirection)).magnitude <= 0.1f;
					if (toggleSprint && flag)
					{
						SetToggleSprint(__instance, value: false);
					}
				}
				if (!InputManager.instance.InputToggleGet((InputKey)12))
				{
					return;
				}
				bool flag2 = SemiFunc.InputHold((InputKey)15);
				bool flag3 = SemiFunc.InputDown((InputKey)12);
				bool flag4 = SemiFunc.InputDown((InputKey)1);
				bool grounded = __instance.CollisionController.Grounded;
				bool flag5 = IsJumping(__instance);
				val = Instance?._enableCrouchJump;
				if (val != null && val.Value)
				{
					if (__instance.Crouching && flag4)
					{
						_jumpedFromCrouch = true;
					}
					if (_jumpedFromCrouch && grounded && !flag5)
					{
						if (CheckCanStandUp())
						{
							__instance.Crouching = false;
							SetToggleCrouch(__instance, value: false);
						}
						_jumpedFromCrouch = false;
					}
					if (flag3)
					{
						_jumpedFromCrouch = false;
					}
				}
				val = Instance?._enableCrouchSprint;
				if (val == null || !val.Value)
				{
					return;
				}
				if (__instance.sprinting && flag3)
				{
					_wasSprintingLastFrame = true;
					return;
				}
				if (_wasSprintingLastFrame && __instance.Crouching)
				{
					if (!flag2)
					{
						_wasSprintingLastFrame = false;
					}
					return;
				}
				_wasSprintingLastFrame = __instance.sprinting;
				if (__instance.Crouching)
				{
					_crouchDuration += Time.deltaTime;
				}
				else
				{
					_crouchDuration = 0f;
					_sprintKeyPressedDuringCrouch = false;
				}
				if (__instance.Crouching && flag2 && _crouchDuration < 0.4f)
				{
					_sprintKeyPressedDuringCrouch = true;
				}
				if (__instance.Crouching && _sprintKeyPressedDuringCrouch && _crouchDuration >= 0.4f && CheckCanStandUp())
				{
					__instance.Crouching = false;
					SetToggleCrouch(__instance, value: false);
					__instance.sprinting = true;
					_crouchDuration = 0f;
					_sprintKeyPressedDuringCrouch = false;
				}
				if (__instance.Crouching && flag2 && _crouchDuration >= 0.4f && CheckCanStandUp())
				{
					__instance.Crouching = false;
					SetToggleCrouch(__instance, value: false);
					__instance.sprinting = true;
					_crouchDuration = 0f;
				}
			}
		}

		private static ManualLogSource? _logger;

		private Harmony? _harmony;

		private ConfigEntry<bool>? _enableCrouchSprint;

		private ConfigEntry<bool>? _enableCrouchJump;

		private ConfigEntry<bool>? _enableResetSprintOnStop;

		public static EnhancedControlsPlugin? Instance { get; private set; }

		private void Awake()
		{
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			Instance = this;
			_logger = ((BaseUnityPlugin)this).Logger;
			_enableCrouchSprint = ((BaseUnityPlugin)this).Config.Bind<bool>("蹲下奔跑 | Crouch Sprint", "启用蹲下自动站起奔跑 | Enable Crouch-to-Sprint", true, "按奔跑键时自动站起并奔跑 | Auto stand up when pressing sprint key");
			_enableCrouchJump = ((BaseUnityPlugin)this).Config.Bind<bool>("蹲下跳跃 | Crouch Jump", "启用蹲下跳跃后站立 | Enable Stand After Crouch Jump", true, "蹲下状态跳跃后不再自动蹲下 | Don't auto crouch after jumping from crouch");
			_enableResetSprintOnStop = ((BaseUnityPlugin)this).Config.Bind<bool>("切换跑步 | Toggle Sprint", "停止移动自动取消跑步 | Auto Cancel Sprint on Stop", true, "切换跑步模式下停止移动时自动恢复走路状态 | Auto return to walking when stopped in toggle sprint mode");
			_harmony = new Harmony("Mhz.EnhancedControls");
			_harmony.PatchAll();
			_logger.LogInfo((object)"增强控制MOD已启用 | Enhanced Controls MOD Enabled");
		}
	}
}