Decompiled source of Ragdoll Player v1.3.0

Mods/RagdollPlayer.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BoneLib;
using BoneLib.BoneMenu;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Bonelab;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Audio;
using MelonLoader;
using MelonLoader.Preferences;
using RagdollPlayer;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(RagdollPlayerMod), "Ragdoll Player", "1.3.0", "Lakatrazz", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: AssemblyDescription("A BONELAB mod that allows you to ragdoll your player with an input.")]
[assembly: AssemblyTitle("Ragdoll Player")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.0.0")]
[module: UnverifiableCode]
namespace RagdollPlayer;

[HarmonyPatch(typeof(FootstepSFX))]
public static class FootstepSFXPatches
{
	[HarmonyPrefix]
	[HarmonyPatch("PlayStep")]
	public static bool PlayStep(FootstepSFX __instance)
	{
		if (RagdollPlayerMod.DisableFootstep(__instance))
		{
			return false;
		}
		return true;
	}
}
public class RagdollPlayerMod : MelonMod
{
	public enum RagdollMode
	{
		LIMP,
		ARM_CONTROL
	}

	public enum RagdollBinding
	{
		THUMBSTICK_PRESS,
		DOUBLE_TAP_B
	}

	public enum RagdollHand
	{
		RIGHT_HAND,
		LEFT_HAND
	}

	public const string Version = "1.3.0";

	private const float DoubleTapTimer = 0.32f;

	private static float _lastTimeInput;

	private static bool _ragdollNextButton;

	private static bool _preferencesSetup = false;

	private static List<FootstepSFX> _footsteps = new List<FootstepSFX>();

	public static MelonPreferences_Category MelonPrefCategory { get; private set; }

	public static MelonPreferences_Entry<bool> MelonPrefEnabled { get; private set; }

	public static MelonPreferences_Entry<RagdollBinding> MelonPrefBinding { get; private set; }

	public static MelonPreferences_Entry<RagdollHand> MelonPrefHand { get; private set; }

	public static MelonPreferences_Entry<RagdollMode> MelonPrefMode { get; private set; }

	public static bool IsEnabled { get; private set; }

	public static RagdollBinding Binding { get; private set; }

	public static RagdollHand Hand { get; private set; }

	public static RagdollMode Mode { get; private set; }

	public static Page MainPage { get; private set; }

	public static BoolElement EnabledElement { get; private set; }

	public static EnumElement BindingElement { get; private set; }

	public static EnumElement HandElement { get; private set; }

	public static EnumElement ModeElement { get; private set; }

	public override void OnInitializeMelon()
	{
		Hooking.OnLevelLoaded += OnLevelLoaded;
		SetupMelonPrefs();
		SetupBoneMenu();
	}

	public static bool DisableFootstep(FootstepSFX sfx)
	{
		if (!IsEnabled)
		{
			return false;
		}
		if (!_footsteps.Contains(sfx))
		{
			return false;
		}
		PhysicsRig physicsRig = Player.PhysicsRig;
		if ((Object)(object)physicsRig == (Object)null)
		{
			return false;
		}
		if (!physicsRig.torso.shutdown)
		{
			return !physicsRig.ballLocoEnabled;
		}
		return true;
	}

	private void OnLevelLoaded(LevelInfo info)
	{
		_footsteps = new List<FootstepSFX>();
		Il2CppReferenceArray<Footstep> footsteps = Player.RigManager.animationRig.body.locomotion.footsteps;
		_footsteps.Add(((Il2CppArrayBase<Footstep>)(object)footsteps)[0].stepSfx);
		_footsteps.Add(((Il2CppArrayBase<Footstep>)(object)footsteps)[1].stepSfx);
	}

	public static void SetupMelonPrefs()
	{
		MelonPrefCategory = MelonPreferences.CreateCategory("Ragdoll Player");
		MelonPrefEnabled = MelonPrefCategory.CreateEntry<bool>("IsEnabled", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		MelonPrefBinding = MelonPrefCategory.CreateEntry<RagdollBinding>("Binding", RagdollBinding.THUMBSTICK_PRESS, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		MelonPrefHand = MelonPrefCategory.CreateEntry<RagdollHand>("Hand", RagdollHand.RIGHT_HAND, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		MelonPrefMode = MelonPrefCategory.CreateEntry<RagdollMode>("Mode", RagdollMode.LIMP, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		IsEnabled = MelonPrefEnabled.Value;
		Binding = MelonPrefBinding.Value;
		Hand = MelonPrefHand.Value;
		Mode = MelonPrefMode.Value;
		_preferencesSetup = true;
	}

	public static void SetupBoneMenu()
	{
		//IL_000a: 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_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		MainPage = Page.Root.CreatePage("Ragdoll Player", Color.green, 0, true);
		EnabledElement = MainPage.CreateBool("Enabled", Color.yellow, IsEnabled, (Action<bool>)OnSetEnabled);
		BindingElement = MainPage.CreateEnum("Binding", Color.red, (Enum)Binding, (Action<Enum>)OnSetBinding);
		ModeElement = MainPage.CreateEnum("Mode", Color.cyan, (Enum)Mode, (Action<Enum>)OnSetMode);
		HandElement = MainPage.CreateEnum("Hand", Color.green, (Enum)Hand, (Action<Enum>)OnSetHand);
	}

	private static void OnSetEnabled(bool value)
	{
		IsEnabled = value;
		MelonPrefEnabled.Value = value;
		MelonPrefCategory.SaveToFile(false);
	}

	private static void OnSetBinding(Enum value)
	{
		RagdollBinding value2 = (Binding = (RagdollBinding)(object)value);
		MelonPrefBinding.Value = value2;
		MelonPrefCategory.SaveToFile(false);
	}

	private static void OnSetHand(Enum value)
	{
		RagdollHand value2 = (Hand = (RagdollHand)(object)value);
		MelonPrefHand.Value = value2;
		MelonPrefCategory.SaveToFile(false);
	}

	private static void OnSetMode(Enum value)
	{
		RagdollMode value2 = (Mode = (RagdollMode)(object)value);
		MelonPrefMode.Value = value2;
		MelonPrefCategory.SaveToFile(false);
	}

	public override void OnPreferencesLoaded()
	{
		if (_preferencesSetup)
		{
			IsEnabled = MelonPrefEnabled.Value;
			Binding = MelonPrefBinding.Value;
			Hand = MelonPrefHand.Value;
			Mode = MelonPrefMode.Value;
			EnabledElement.Value = IsEnabled;
			BindingElement.Value = Binding;
			HandElement.Value = Hand;
			ModeElement.Value = Mode;
		}
	}

	public override void OnUpdate()
	{
		if (!IsEnabled)
		{
			return;
		}
		RigManager rigManager = Player.RigManager;
		if (Object.op_Implicit((Object)(object)rigManager) && !Object.op_Implicit((Object)(object)rigManager.activeSeat) && !UIRig.Instance.popUpMenu.m_IsCursorShown && GetInput(GetController()))
		{
			PhysicsRig physicsRig = Player.PhysicsRig;
			if (!physicsRig.torso.shutdown && physicsRig.ballLocoEnabled)
			{
				RagdollRig(rigManager);
			}
			else
			{
				UnragdollRig(rigManager);
			}
		}
	}

	public static void RagdollRig(RigManager rig)
	{
		PhysicsRig physicsRig = rig.physicsRig;
		if (Mode == RagdollMode.LIMP)
		{
			physicsRig.ShutdownRig();
		}
		physicsRig.RagdollRig();
		if (Mode == RagdollMode.ARM_CONTROL)
		{
			physicsRig.DisableBallLoco();
			physicsRig.PhysicalLegs();
			physicsRig.legLf.ShutdownLimb();
			physicsRig.legRt.ShutdownLimb();
		}
	}

	public static void UnragdollRig(RigManager rig)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: 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_004f: Unknown result type (might be due to invalid IL or missing references)
		PhysicsRig physicsRig = rig.physicsRig;
		Transform transform = physicsRig.feet.transform;
		Transform transform2 = physicsRig.knee.transform;
		Transform transform3 = ((Component)((Rig)physicsRig).m_pelvis).transform;
		physicsRig.TurnOnRig();
		physicsRig.UnRagdollRig();
		Vector3 position = transform3.position;
		Quaternion rotation = transform3.rotation;
		transform2.SetPositionAndRotation(position, rotation);
		transform.SetPositionAndRotation(position, rotation);
	}

	private static BaseController GetController()
	{
		if (Hand == RagdollHand.LEFT_HAND)
		{
			return Player.LeftController;
		}
		return Player.RightController;
	}

	private static bool GetInput(BaseController controller)
	{
		RagdollBinding binding = Binding;
		if (binding == RagdollBinding.THUMBSTICK_PRESS || binding != RagdollBinding.DOUBLE_TAP_B)
		{
			_lastTimeInput = 0f;
			_ragdollNextButton = false;
			return controller.GetThumbStickDown();
		}
		bool bButtonDown = controller.GetBButtonDown();
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		if (bButtonDown && _ragdollNextButton)
		{
			if (realtimeSinceStartup - _lastTimeInput <= 0.32f)
			{
				return true;
			}
			_ragdollNextButton = false;
			_lastTimeInput = 0f;
		}
		else if (bButtonDown)
		{
			_lastTimeInput = realtimeSinceStartup;
			_ragdollNextButton = true;
		}
		else if (realtimeSinceStartup - _lastTimeInput > 0.32f)
		{
			_ragdollNextButton = false;
			_lastTimeInput = 0f;
		}
		return false;
	}
}