Decompiled source of StayDown v1.0.0

Mods/StayDown.dll

Decompiled 4 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BoneLib;
using BoneLib.BoneMenu;
using BoneLib.Notifications;
using HarmonyLib;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Interaction;
using LabFusion.Data;
using LabFusion.Network;
using LabFusion.Patching;
using LabFusion.Player;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using StayDown;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Mod), "StayDown", "1.1.1", "ElectroDynamic", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("StayDown")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("StayDown")]
[assembly: AssemblyTitle("StayDown")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 StayDown
{
	public class Mod : MelonMod
	{
		public static MelonPreferences_Category Cat;

		public static MelonPreferences_Entry<bool> Enabled;

		public static MelonPreferences_Entry<bool> HandControl;

		public static MelonPreferences_Entry<int> StickMode;

		public static bool HeldDown;

		private static bool _handsLive;

		private static bool _prevLeft;

		private static bool _prevRight;

		public static Instance Log { get; private set; }

		public override void OnInitializeMelon()
		{
			Log = ((MelonBase)this).LoggerInstance;
			Cat = MelonPreferences.CreateCategory("StayDown");
			Enabled = Cat.CreateEntry<bool>("Enabled", true, "Stay knocked down until you click a thumbstick", (string)null, false, false, (ValueValidator)null, (string)null);
			HandControl = Cat.CreateEntry<bool>("HandControl", true, "Keep control of your hands while held down", (string)null, false, false, (ValueValidator)null, (string)null);
			StickMode = Cat.CreateEntry<int>("StickMode", 0, "Stand-up stick: 0=either, 1=left, 2=right", (string)null, false, false, (ValueValidator)null, (string)null);
			Cat.SaveToFile(false);
			BuildMenu();
			Hooking.OnLevelLoaded += delegate
			{
				ResetState();
			};
			Log.Msg("StayDown loaded.");
		}

		public override void OnUpdate()
		{
			if (!HeldDown)
			{
				return;
			}
			if (!NetworkInfo.HasServer || !RigData.HasPlayer)
			{
				ResetState();
			}
			else if (!LocalRagdoll.KnockedOut)
			{
				if (HandControl.Value && !_handsLive)
				{
					WakeHands();
				}
				else if (!HandControl.Value && _handsLive)
				{
					SleepHands();
				}
				if (StickClicked())
				{
					StandUp(notify: true);
				}
			}
		}

		private static bool StickClicked()
		{
			bool flag = false;
			bool flag2 = false;
			try
			{
				BaseController leftController = Player.LeftController;
				BaseController rightController = Player.RightController;
				flag = (Object)(object)leftController != (Object)null && leftController.GetThumbStickDown();
				flag2 = (Object)(object)rightController != (Object)null && rightController.GetThumbStickDown();
			}
			catch
			{
			}
			bool flag3 = flag && !_prevLeft;
			bool flag4 = flag2 && !_prevRight;
			_prevLeft = flag;
			_prevRight = flag2;
			return StickMode.Value switch
			{
				1 => flag3, 
				2 => flag4, 
				_ => flag3 || flag4, 
			};
		}

		public static void StandUp(bool notify)
		{
			HeldDown = false;
			_handsLive = false;
			try
			{
				if (RigData.HasPlayer)
				{
					LocalRagdoll.ToggleRagdoll(false);
				}
			}
			catch (Exception ex)
			{
				Log.Warning("Stand up failed: " + ex.Message);
			}
			if (notify)
			{
				Notify("Back on your feet!", (NotificationType)3);
			}
		}

		private static void WakeHands()
		{
			try
			{
				PhysicsRig physicsRig = RigData.Refs.RigManager.physicsRig;
				if (!((Object)(object)physicsRig == (Object)null))
				{
					PhysicsRigPatches.ForceAllowUnragdoll = true;
					physicsRig.TurnOnRig();
					PhysicsRigPatches.ForceAllowUnragdoll = false;
					physicsRig.ResetHands((Handedness)3);
					_handsLive = true;
					Notify("Hands are live — drag yourself around.", (NotificationType)0);
				}
			}
			catch (Exception ex)
			{
				Log.Warning("Hand control failed: " + ex.Message);
				_handsLive = true;
			}
		}

		private static void SleepHands()
		{
			_handsLive = false;
			try
			{
				PhysicsRig physicsRig = RigData.Refs.RigManager.physicsRig;
				if (physicsRig != null)
				{
					physicsRig.ShutdownRig();
				}
			}
			catch
			{
			}
		}

		public static void DropHold()
		{
			ResetState();
		}

		private static void ResetState()
		{
			HeldDown = false;
			_handsLive = false;
		}

		private static void BuildMenu()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			Page obj = Page.Root.CreatePage("StayDown", new Color(1f, 0.35f, 0.25f), 0, true);
			obj.CreateBool("StayDown Active", Color.green, Enabled.Value, (Action<bool>)delegate(bool v)
			{
				Enabled.Value = v;
				Cat.SaveToFile(false);
				if (!v && HeldDown)
				{
					StandUp(notify: false);
				}
				Notify(v ? "StayDown on — knockouts keep you down." : "StayDown off.", (NotificationType)0);
			});
			obj.CreateBool("Hand Control While Down", Color.cyan, HandControl.Value, (Action<bool>)delegate(bool v)
			{
				HandControl.Value = v;
				Cat.SaveToFile(false);
				Notify(v ? "Hands stay live while you're down." : "Hands go limp while you're down.", (NotificationType)0);
			});
			obj.CreateInt("Stand-Up Stick (0=Both 1=L 2=R)", Color.yellow, StickMode.Value, 1, 0, 2, (Action<int>)delegate(int v)
			{
				StickMode.Value = Mathf.Clamp(v, 0, 2);
				Cat.SaveToFile(false);
			});
			obj.CreateFunction("Get Up Now (failsafe)", Color.red, (Action)delegate
			{
				if (HeldDown)
				{
					StandUp(notify: true);
				}
				else
				{
					Notify("You're not being held down.", (NotificationType)0);
				}
			});
			obj.CreateFunction(" - knocked out? stay down, click stick to rise", new Color(0.6f, 0.6f, 0.6f), (Action)delegate
			{
			});
		}

		private static void Notify(string message, NotificationType type)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: 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_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: 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)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			try
			{
				Notifier.Send(new Notification
				{
					Title = NotificationText.op_Implicit("StayDown"),
					Message = NotificationText.op_Implicit(message),
					PopupLength = 2.5f,
					Type = type
				});
			}
			catch
			{
			}
		}
	}
	[HarmonyPatch(typeof(LocalRagdoll), "Knockout", new Type[]
	{
		typeof(float),
		typeof(bool)
	})]
	public static class StayDown_Knockout_Patch
	{
		public static bool Prefix()
		{
			if (Mod.Enabled.Value)
			{
				return !Mod.HeldDown;
			}
			return true;
		}

		public static void Postfix()
		{
			if (Mod.Enabled.Value && RigData.HasPlayer && !Mod.HeldDown)
			{
				Mod.HeldDown = true;
				Mod.Log.Msg("Knocked out — holding you down until a stick click.");
			}
		}
	}
	[HarmonyPatch(typeof(LocalRagdoll), "ToggleRagdoll")]
	public static class StayDown_ToggleRagdoll_Patch
	{
		public static bool Prefix(bool ragdolled)
		{
			if (ragdolled)
			{
				return true;
			}
			if (!Mod.Enabled.Value || !Mod.HeldDown)
			{
				return true;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(PhysicsRig), "UnRagdollRig")]
	public static class StayDown_UnRagdollRig_Patch
	{
		public static void Postfix()
		{
			if (Mod.HeldDown)
			{
				Mod.DropHold();
			}
		}
	}
}