Decompiled source of Sneak Camera and Collision Height v1.0.2

SneakCameraAndCollision.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
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("SneakCameraAndCollision")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SneakCameraAndCollision")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c2a11acf-fe71-4f51-a922-504d7b45a13b")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
namespace SneakCameraAndCollision;

[HarmonyPatch]
internal class CameraPatches
{
	[HarmonyPatch(typeof(GameCamera), "GetCameraBaseOffset")]
	[HarmonyPostfix]
	private static void GetCameraBaseOffset(Player player, ref Vector3 __result)
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: 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_00cd: 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_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)player != (Object)(object)Player.m_localPlayer) && SneakConfig.LowerCameraWhen.Value != 0 && !(__result != ((Component)((Character)player).m_eye).transform.position - ((Component)player).transform.position) && ((Character)player).IsCrouching() && ((Character)player).IsOnGround())
		{
			bool flag = ((Character)player).IsSneaking();
			if (SneakConfig.LowerCameraWhen.Value == SneakConfig.CrouchPosition.Crouching || (SneakConfig.LowerCameraWhen.Value == SneakConfig.CrouchPosition.CrouchWalking && flag) || (SneakConfig.LowerCameraWhen.Value == SneakConfig.CrouchPosition.CrouchStanding && !flag))
			{
				__result += Vector3.up * (0f - Mathf.Clamp(SneakConfig.CameraHeightReduction.Value, 0f, 1f));
			}
		}
	}
}
[HarmonyPatch]
internal class CollisionPatches
{
	private static float? standingSize;

	private static float? crouchedSize;

	[HarmonyPatch(typeof(Player), "FixedUpdate")]
	[HarmonyPostfix]
	private static void FixedUpdate(Player __instance)
	{
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer || !SneakConfig.ChangeCollisionCrouchHeight.Value)
		{
			return;
		}
		CapsuleCollider collider = ((Character)__instance).GetCollider();
		if (Object.op_Implicit((Object)(object)collider))
		{
			if (!standingSize.HasValue)
			{
				standingSize = collider.height;
				CalculateCrouchedSize();
			}
			bool flag = ((Character)__instance).IsCrouching() && ((Character)__instance).IsOnGround();
			collider.height = (flag ? crouchedSize.Value : standingSize.Value);
			collider.center = new Vector3(0f, collider.height / 2f, 0f);
		}
	}

	internal static void CalculateCrouchedSize()
	{
		if (standingSize.HasValue && SneakConfig.CrouchHeightMultiplier != null)
		{
			crouchedSize = standingSize.Value * Mathf.Clamp(SneakConfig.CrouchHeightMultiplier.Value, 0.75f, 1f);
		}
	}
}
internal sealed class ConfigurationManagerAttributes
{
	public bool? ShowRangeAsPercent;

	public Action<ConfigEntryBase> CustomDrawer;

	public bool? Browsable;

	public string Category;

	public object DefaultValue;

	public bool? HideDefaultButton;

	public bool? HideSettingName;

	public string Description;

	public string DispName;

	public int? Order;

	public bool? ReadOnly;

	public bool? IsAdvanced;

	public Func<object, string> ObjToStr;

	public Func<string, object> StrToObj;

	internal static ConfigDescription ShowAsPercentOverride<T>(bool showRangeAsPercent, AcceptableValueRange<T> acceptableValues, string description = "") where T : IComparable
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Expected O, but got Unknown
		return new ConfigDescription(description, (AcceptableValueBase)(object)acceptableValues, new object[1]
		{
			new ConfigurationManagerAttributes
			{
				ShowRangeAsPercent = showRangeAsPercent
			}
		});
	}
}
[BepInPlugin("goldenrevolver.SneakCameraAndCollision", "Sneak Camera and Collision Height", "1.0.2")]
public class SneakCameraAndCollisionPlugin : BaseUnityPlugin
{
	public const string GUID = "goldenrevolver.SneakCameraAndCollision";

	public const string NAME = "Sneak Camera and Collision Height";

	public const string VERSION = "1.0.2";

	protected void Awake()
	{
		LoadConfig();
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}

	private void LoadConfig()
	{
		string text = "Camera Height";
		SneakConfig.LowerCameraWhen = ((BaseUnityPlugin)this).Config.Bind<SneakConfig.CrouchPosition>(text, "LowerCameraWhen", SneakConfig.CrouchPosition.Crouching, "When to lower the camera height (crouching means both standing and moving)");
		SneakConfig.CameraHeightReduction = ((BaseUnityPlugin)this).Config.Bind<float>(text, "CameraHeightReduction", 0.5f, ConfigurationManagerAttributes.ShowAsPercentOverride<float>(showRangeAsPercent: false, new AcceptableValueRange<float>(0f, 1f), "By how much to lower the camera (in meters)"));
		text = "Collision Height";
		SneakConfig.ChangeCollisionCrouchHeight = ((BaseUnityPlugin)this).Config.Bind<bool>(text, "ChangeCollisionCrouchHeight", true, "Whether to change the player collision height at all");
		SneakConfig.CrouchHeightMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>(text, "CrouchHeightMultiplier", 0.8f, ConfigurationManagerAttributes.ShowAsPercentOverride<float>(showRangeAsPercent: false, new AcceptableValueRange<float>(0.75f, 1f), "By how many percent to reduce the default standing player height (which by default is 1.85 meters)"));
		SneakConfig.CrouchHeightMultiplier.SettingChanged += delegate
		{
			CollisionPatches.CalculateCrouchedSize();
		};
	}
}
internal class SneakConfig
{
	public enum CrouchPosition
	{
		Disabled,
		Crouching,
		CrouchStanding,
		CrouchWalking
	}

	public static ConfigEntry<CrouchPosition> LowerCameraWhen;

	public static ConfigEntry<float> CameraHeightReduction;

	internal const float CameraMin = 0f;

	internal const float CameraMax = 1f;

	public static ConfigEntry<bool> ChangeCollisionCrouchHeight;

	public static ConfigEntry<float> CrouchHeightMultiplier;

	internal const float HeightMultMin = 0.75f;

	internal const float HeightMultMax = 1f;
}