Decompiled source of CS16 GoldSrc Movement v1.3.0

CS16Movement.dll

Decompiled 2 weeks 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.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;

[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("CS16Movement")]
[assembly: AssemblyConfiguration("release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CS16Movement")]
[assembly: AssemblyTitle("CS16Movement")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 CS16Movement
{
	[BepInPlugin("com.koji.cs16movement", "CS 1.6 GoldSrc Movement", "1.3.0")]
	public class CS16MovementPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.koji.cs16movement";

		public const string PluginName = "CS 1.6 GoldSrc Movement";

		public const string PluginVersion = "1.3.0";

		internal static ManualLogSource Log;

		public static ConfigEntry<float> SvGravity;

		public static ConfigEntry<float> SvFriction;

		public static ConfigEntry<float> SvAccelerate;

		public static ConfigEntry<float> SvAirAccelerate;

		public static ConfigEntry<float> SvWalkSpeed;

		public static ConfigEntry<float> SvSprintSpeed;

		public static ConfigEntry<float> SvStopSpeed;

		public static ConfigEntry<float> AirSpeedCap;

		public static ConfigEntry<float> JumpImpulse;

		public static ConfigEntry<bool> AutoBhop;

		public static ConfigEntry<bool> InfiniteStamina;

		private void Awake()
		{
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			SvGravity = ((BaseUnityPlugin)this).Config.Bind<float>("1. GoldSrc v1.2", "sv_gravity", 19.5f, "Гравитация (в масштабе LC ~800 юнитов CS 1.6)");
			SvFriction = ((BaseUnityPlugin)this).Config.Bind<float>("1. GoldSrc v1.2", "sv_friction", 5.2f, "Трение о землю (резкая остановка как в CS 1.6)");
			SvAccelerate = ((BaseUnityPlugin)this).Config.Bind<float>("1. GoldSrc v1.2", "sv_accelerate", 6.5f, "Ускорение на земле");
			SvAirAccelerate = ((BaseUnityPlugin)this).Config.Bind<float>("1. GoldSrc v1.2", "sv_airaccelerate", 15f, "Воздушное ускорение для плавных аэро-стрейфов");
			SvWalkSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("1. GoldSrc v1.2", "sv_walkspeed", 4f, "Обычная скорость шага");
			SvSprintSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("1. GoldSrc v1.2", "sv_maxspeed", 5.8f, "Скорость бега на Shift");
			SvStopSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("1. GoldSrc v1.2", "sv_stopspeed", 1.8f, "Порог торможения");
			AirSpeedCap = ((BaseUnityPlugin)this).Config.Bind<float>("1. GoldSrc v1.2", "AirWishSpeedCap", 0.65f, "Лимит проекции скорости в воздухе (аналог 30 из GoldSrc)");
			JumpImpulse = ((BaseUnityPlugin)this).Config.Bind<float>("2. Bunnyhop v1.2", "JumpVelocity", 5.4f, "Высота прыжка");
			AutoBhop = ((BaseUnityPlugin)this).Config.Bind<bool>("2. Bunnyhop v1.2", "AutoBunnyhop", true, "Зажатие пробела для бхопа");
			InfiniteStamina = ((BaseUnityPlugin)this).Config.Bind<bool>("2. Bunnyhop v1.2", "NoStaminaDrain", true, "Бесконечная стамина");
			new Harmony("com.koji.cs16movement").PatchAll(typeof(PlayerPhysicsPatch));
			Log.LogInfo((object)"CS 1.6 GoldSrc Movement v1.3 loaded successfully!");
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	public static class PlayerPhysicsPatch
	{
		private static Vector3 _velocity = Vector3.zero;

		private static Vector3 _preUpdatePosition = Vector3.zero;

		private static bool _wasGrounded = true;

		private static bool _jumpQueued = false;

		private static bool _teleportedThisFrame = false;

		private static readonly FieldInfo FallValueField = AccessTools.Field(typeof(PlayerControllerB), "fallValue");

		private static readonly FieldInfo FallValueUncappedField = AccessTools.Field(typeof(PlayerControllerB), "fallValueUncapped");

		private static readonly FieldInfo IsJumpingField = AccessTools.Field(typeof(PlayerControllerB), "isJumping");

		private static readonly FieldInfo PlayerSlidingTimerField = AccessTools.Field(typeof(PlayerControllerB), "playerSlidingTimer");

		private static bool ShouldRunCustomPhysics(PlayerControllerB instance)
		{
			if ((Object)(object)instance == (Object)null || !((NetworkBehaviour)instance).IsOwner || !instance.isPlayerControlled || instance.isPlayerDead)
			{
				return false;
			}
			if (instance.isClimbingLadder || instance.jetpackControls || instance.inSpecialInteractAnimation)
			{
				return false;
			}
			if ((Object)(object)instance.thisController == (Object)null || !((Collider)instance.thisController).enabled)
			{
				return false;
			}
			return true;
		}

		[HarmonyPatch("TeleportPlayer")]
		[HarmonyPostfix]
		public static void OnTeleportPlayer(PlayerControllerB __instance, Vector3 pos)
		{
			//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)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance != (Object)null && ((NetworkBehaviour)__instance).IsOwner)
			{
				_preUpdatePosition = pos;
				_velocity = Vector3.zero;
				_teleportedThisFrame = true;
			}
		}

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		public static void PreUpdateSavePosition(PlayerControllerB __instance)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: 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)
			_teleportedThisFrame = false;
			if (!ShouldRunCustomPhysics(__instance))
			{
				_velocity = Vector3.zero;
				return;
			}
			_preUpdatePosition = ((Component)__instance).transform.position;
			_wasGrounded = __instance.thisController.isGrounded;
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		public static void GoldSrcMoveUpdate(PlayerControllerB __instance)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: 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_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_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: 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_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: 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_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0318: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0406: Unknown result type (might be due to invalid IL or missing references)
			//IL_0408: Unknown result type (might be due to invalid IL or missing references)
			//IL_040d: Unknown result type (might be due to invalid IL or missing references)
			//IL_040f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0425: Unknown result type (might be due to invalid IL or missing references)
			//IL_0427: Unknown result type (might be due to invalid IL or missing references)
			//IL_044b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0456: Unknown result type (might be due to invalid IL or missing references)
			//IL_046b: Unknown result type (might be due to invalid IL or missing references)
			//IL_047f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0490: Unknown result type (might be due to invalid IL or missing references)
			if (!ShouldRunCustomPhysics(__instance))
			{
				return;
			}
			float num = Vector3.Distance(((Component)__instance).transform.position, _preUpdatePosition);
			if (_teleportedThisFrame || num > 2.5f)
			{
				_preUpdatePosition = ((Component)__instance).transform.position;
				return;
			}
			((Collider)__instance.thisController).enabled = false;
			((Component)__instance).transform.position = _preUpdatePosition;
			((Collider)__instance.thisController).enabled = true;
			if (CS16MovementPlugin.InfiniteStamina.Value)
			{
				__instance.sprintMeter = 1f;
				__instance.isExhausted = false;
			}
			bool num2 = __instance.inTerminalMenu || __instance.isTypingChat || ((Object)(object)__instance.quickMenuManager != (Object)null && __instance.quickMenuManager.isMenuOpen);
			InputActionAsset actions = IngamePlayerSettings.Instance.playerInput.actions;
			InputAction val = actions.FindAction("Jump", false);
			InputAction val2 = actions.FindAction("Sprint", false);
			InputAction val3 = actions.FindAction("Move", false);
			bool flag = !num2 && val != null && val.IsPressed();
			bool flag2 = !num2 && val != null && val.WasPressedThisFrame();
			bool flag3 = !num2 && val2 != null && val2.IsPressed();
			if (CS16MovementPlugin.AutoBhop.Value ? flag : flag2)
			{
				_jumpQueued = true;
			}
			else if (!flag)
			{
				_jumpQueued = false;
			}
			Vector2 val4 = ((!num2 && val3 != null) ? val3.ReadValue<Vector2>() : Vector2.zero);
			Transform obj = (((Object)(object)__instance.gameplayCamera != (Object)null) ? ((Component)__instance.gameplayCamera).transform : ((Component)__instance).transform);
			Vector3 val5 = Vector3.ProjectOnPlane(obj.forward, Vector3.up);
			Vector3 normalized = ((Vector3)(ref val5)).normalized;
			val5 = Vector3.ProjectOnPlane(obj.right, Vector3.up);
			Vector3 normalized2 = ((Vector3)(ref val5)).normalized;
			Vector3 val6 = normalized * val4.y + normalized2 * val4.x;
			val6.y = 0f;
			Vector3 wishdir = ((((Vector3)(ref val6)).sqrMagnitude > 0.0001f) ? ((Vector3)(ref val6)).normalized : Vector3.zero);
			float num3 = (flag3 ? CS16MovementPlugin.SvSprintSpeed.Value : CS16MovementPlugin.SvWalkSpeed.Value);
			float num4 = Mathf.Clamp01(((Vector3)(ref val6)).magnitude) * num3;
			if (__instance.isCrouching)
			{
				num4 *= 0.45f;
			}
			float num5 = Mathf.Clamp(__instance.carryWeight - 1f, 0f, 1.5f) * 0.25f;
			num4 *= Mathf.Clamp(1f - num5, 0.55f, 1f);
			bool flag4 = _wasGrounded;
			if (flag4 && _jumpQueued)
			{
				flag4 = false;
				_jumpQueued = false;
				_velocity.y = CS16MovementPlugin.JumpImpulse.Value;
				if ((Object)(object)__instance.movementAudio != (Object)null && (Object)(object)StartOfRound.Instance != (Object)null)
				{
					__instance.movementAudio.PlayOneShot(StartOfRound.Instance.playerJumpSFX);
				}
				PM_AirAccelerate(wishdir, num4, CS16MovementPlugin.SvAirAccelerate.Value);
			}
			else if (flag4)
			{
				PM_Friction();
				PM_Accelerate(wishdir, num4, CS16MovementPlugin.SvAccelerate.Value);
				_velocity.y = -2.5f;
			}
			else
			{
				PM_AirAccelerate(wishdir, num4, CS16MovementPlugin.SvAirAccelerate.Value);
				_velocity.y -= CS16MovementPlugin.SvGravity.Value * Time.deltaTime;
			}
			PlayerSlidingTimerField?.SetValue(__instance, 0f);
			FallValueField?.SetValue(__instance, _velocity.y);
			FallValueUncappedField?.SetValue(__instance, _velocity.y);
			IsJumpingField?.SetValue(__instance, !flag4);
			__instance.isFallingNoJump = false;
			__instance.isFallingFromJump = !flag4;
			Vector3 val7 = new Vector3(_velocity.x, _velocity.y, _velocity.z) * Time.deltaTime;
			CollisionFlags val8 = __instance.thisController.Move(val7);
			_wasGrounded = (val8 & 4) != 0 || __instance.thisController.isGrounded;
			if ((val8 & 2) != 0 && _velocity.y > 0f)
			{
				_velocity.y = -0.5f;
			}
			if ((val8 & 1) != 0)
			{
				Vector3 val9 = default(Vector3);
				((Vector3)(ref val9))..ctor(__instance.thisController.velocity.x, 0f, __instance.thisController.velocity.z);
				_velocity.x = val9.x;
				_velocity.z = val9.z;
			}
		}

		private static void PM_Friction()
		{
			Vector3 val = default(Vector3);
			((Vector3)(ref val))..ctor(_velocity.x, 0f, _velocity.z);
			float magnitude = ((Vector3)(ref val)).magnitude;
			if (magnitude < 0.01f)
			{
				_velocity.x = 0f;
				_velocity.z = 0f;
				return;
			}
			float value = CS16MovementPlugin.SvStopSpeed.Value;
			float value2 = CS16MovementPlugin.SvFriction.Value;
			float num = ((magnitude < value) ? value : magnitude) * value2 * Time.deltaTime;
			float num2 = magnitude - num;
			if (num2 < 0f)
			{
				num2 = 0f;
			}
			num2 /= magnitude;
			_velocity.x *= num2;
			_velocity.z *= num2;
		}

		private static void PM_Accelerate(Vector3 wishdir, float wishspeed, float accel)
		{
			//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_0058: 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)
			if (wishspeed <= 0.001f)
			{
				return;
			}
			float num = Vector3.Dot(new Vector3(_velocity.x, 0f, _velocity.z), wishdir);
			float num2 = wishspeed - num;
			if (!(num2 <= 0f))
			{
				float num3 = accel * Time.deltaTime * wishspeed;
				if (num3 > num2)
				{
					num3 = num2;
				}
				_velocity.x += num3 * wishdir.x;
				_velocity.z += num3 * wishdir.z;
			}
		}

		private static void PM_AirAccelerate(Vector3 wishdir, float wishspeed, float accel)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: 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)
			if (wishspeed <= 0.001f)
			{
				return;
			}
			float num = wishspeed;
			if (num > CS16MovementPlugin.AirSpeedCap.Value)
			{
				num = CS16MovementPlugin.AirSpeedCap.Value;
			}
			float num2 = Vector3.Dot(new Vector3(_velocity.x, 0f, _velocity.z), wishdir);
			float num3 = num - num2;
			if (!(num3 <= 0f))
			{
				float num4 = accel * wishspeed * Time.deltaTime;
				if (num4 > num3)
				{
					num4 = num3;
				}
				_velocity.x += num4 * wishdir.x;
				_velocity.z += num4 * wishdir.z;
			}
		}
	}
}