Decompiled source of CrouchAndCrawlSprint v1.0.0

CrouchAndCrawlSprint.dll

Decompiled 2 weeks ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CrouchAndCrawlSprint")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP")]
[assembly: AssemblyProduct("CrouchAndCrawlSprint")]
[assembly: AssemblyCopyright("Copyright © HP 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e9e10d94-5f30-40b8-af64-4f4133769cba")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CrouchAndCrawlSprint;

[BepInPlugin("TechedCrayon413.CrouchAndCrawlSprint", "Crouch And Crawl Sprint", "1.0.0")]
public class CrouchAndCrawlSprint : BaseUnityPlugin
{
	public static ConfigEntry<KeyCode> CrawlKey;

	public static ConfigEntry<bool> ToggleMode;

	private void Awake()
	{
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Expected O, but got Unknown
		CrawlKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "Crawl Key", (KeyCode)308, "Key used to crawl");
		ToggleMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Controls", "Crawl Key Toggle Mode", true, "If true then press crawl key to toggle, if false = hold crawl key to crawl");
		Harmony val = new Harmony("TechedCrayon413.CrouchAndCrawlSprint");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogMessage((object)"This is a mod that is the successor to the mod, Small Crouch Mode");
		((BaseUnityPlugin)this).Logger.LogMessage((object)"This mod adds the ability to sprint while crawilng or crouching wich is something that the SmallCrouchMode mod didnt do, but this mod still has the crawling feature from SmallCrouchMode");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin CrouchAndCrawlSprint v1.0.0 loaded successfully");
	}
}
public static class CrawlState
{
	public static bool IsCrawling = false;

	public static bool AttemptToSprint = false;

	public static bool SprintLock = false;

	public static float StaminaOverride = -1f;

	public static Dictionary<int, float> DefaultSpeed = new Dictionary<int, float>();
}
[HarmonyPatch(typeof(PlayerController), "Update")]
public static class Patch_PlayerController
{
	public static void Postfix(PlayerController __instance)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		KeyCode value = CrouchAndCrawlSprint.CrawlKey.Value;
		if (CrouchAndCrawlSprint.ToggleMode.Value)
		{
			if (Input.GetKeyDown(value))
			{
				CrawlState.IsCrawling = !CrawlState.IsCrawling;
			}
		}
		else
		{
			CrawlState.IsCrawling = Input.GetKey(value);
		}
		if (CrawlState.IsCrawling)
		{
			__instance.SetCrawl();
		}
	}
}
[HarmonyPatch(typeof(PlayerController), "FixedUpdate")]
public static class PlayerController_FixedUpdate_Patch
{
	private const float AreYouPressingSprintKey = 0.01f;

	private const float SprintSpeedScaler = 1.6f;

	public static bool CheckIfWalking(PlayerController player)
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		Traverse val = Traverse.Create((object)player);
		bool value = val.Field<bool>("moving").Value;
		Vector3 value2 = val.Field<Vector3>("InputDirection").Value;
		bool flag = ((Vector3)(ref value2)).sqrMagnitude > 0.01f;
		return value || flag;
	}

	public static void Prefix(PlayerController __instance)
	{
		Traverse val = Traverse.Create((object)__instance);
		int instanceID = ((Object)__instance).GetInstanceID();
		if (!CrawlState.DefaultSpeed.ContainsKey(instanceID))
		{
			CrawlState.DefaultSpeed[instanceID] = val.Field<float>("CrouchSpeed").Value;
		}
		bool value = val.Field<bool>("Crouching").Value;
		bool isCrawling = CrawlState.IsCrawling;
		CrawlState.StaminaOverride = -1f;
		CrawlState.AttemptToSprint = false;
		if (value || isCrawling)
		{
			bool value2 = val.Field<bool>("toggleSprint").Value;
			bool flag = CheckIfWalking(__instance);
			float value3 = val.Field<float>("EnergyCurrent").Value;
			bool value4 = val.Field<bool>("DebugEnergy").Value;
			float value5 = val.Field<float>("crawlTimer").Value;
			bool flag2 = SemiFunc.InputHold((InputKey)15) || value2;
			CrawlState.AttemptToSprint = flag2 && flag;
			if (!flag2)
			{
				CrawlState.SprintLock = false;
			}
			val.Field<float>("CrouchSpeed").Value = CrawlState.DefaultSpeed[instanceID];
			if (!CrawlState.AttemptToSprint)
			{
				return;
			}
			if (!CrawlState.SprintLock && (value3 > 0f || value4))
			{
				float value6 = val.Field<float>("SprintSpeed").Value;
				float value7 = val.Field<float>("SprintSpeedUpgrades").Value;
				float num = value6 + value7;
				if (value5 > 0f || isCrawling)
				{
					num /= 1.6f;
				}
				val.Field<float>("CrouchSpeed").Value = num;
				if (value4)
				{
					return;
				}
				float value8 = val.Field<float>("EnergySprintDrain").Value;
				value3 -= value8 * Time.fixedDeltaTime;
				if (value3 <= 0f)
				{
					value3 = 0f;
					CrawlState.SprintLock = true;
					if (value2)
					{
						val.Field<bool>("toggleSprint").Value = false;
					}
				}
				val.Field<float>("EnergyCurrent").Value = value3;
				CrawlState.StaminaOverride = value3;
			}
			else if (CrawlState.SprintLock)
			{
				CrawlState.StaminaOverride = 0f;
				if (value2)
				{
					val.Field<bool>("toggleSprint").Value = false;
				}
			}
		}
		else
		{
			val.Field<float>("CrouchSpeed").Value = CrawlState.DefaultSpeed[instanceID];
		}
	}

	public static void Postfix(PlayerController __instance)
	{
		if (CrawlState.StaminaOverride >= 0f)
		{
			Traverse.Create((object)__instance).Field<float>("EnergyCurrent").Value = CrawlState.StaminaOverride;
		}
	}
}
[HarmonyPatch(typeof(PlayerAvatar), "CrouchRestUpgrade")]
public static class PlayerAvatar_CrouchRestUpgrade_Patch
{
	public static bool Prefix(PlayerAvatar __instance)
	{
		if (CrawlState.AttemptToSprint)
		{
			Traverse.Create((object)__instance).Field<bool>("upgradeCrouchRestActive").Value = false;
			return false;
		}
		return true;
	}
}