Decompiled source of Downed v1.0.2

Mods/Downed.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BoneLib;
using BoneLib.BoneMenu;
using Downed;
using Il2CppSLZ.Bonelab;
using Il2CppSLZ.Marrow;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using RagdollPlayer;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Core), "Downed", "1.0.2", "jorink", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("Downed")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+772ef8e4e96584ee1f2cc44fb8ff421524400110")]
[assembly: AssemblyProduct("Downed")]
[assembly: AssemblyTitle("Downed")]
[assembly: NeutralResourcesLanguage("en-US")]
[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 Downed
{
	public class Core : MelonMod
	{
		private MelonPreferences_Category category;

		private MelonPreferences_Entry<bool> EnableModEntry;

		private MelonPreferences_Entry<float> KnockedDurationEntry;

		private bool downed;

		private bool death;

		private float startTime;

		private static float lastTimeInput;

		private static bool ragdollNextButton;

		private const float DoubleTapTimer = 0.32f;

		public override void OnInitializeMelon()
		{
			SetupMelonPreferences();
			SetupBoneMenu();
			Hooking.OnLevelLoaded += OnLevelLoaded;
			Hooking.OnPlayerDamageReceived += OnPlayerDamageReceived;
			Hooking.OnPlayerDeath += OnPlayerDeath;
		}

		public override void OnDeinitializeMelon()
		{
			Hooking.OnLevelLoaded -= OnLevelLoaded;
			Hooking.OnPlayerDamageReceived -= OnPlayerDamageReceived;
			Hooking.OnPlayerDeath -= OnPlayerDeath;
		}

		private void SetupBoneMenu()
		{
			//IL_000a: 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)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			Page obj = Page.Root.CreatePage("Jorink", Color.red, 0, true).CreatePage("Downed", Color.magenta, 0, true);
			obj.CreateBool("Enable Mod", Color.blue, EnableModEntry.Value, (Action<bool>)delegate(bool a)
			{
				EnableModEntry.Value = a;
			});
			obj.CreateFloat("Knocked Duration", Color.yellow, KnockedDurationEntry.Value, 1f, 1f, 10f, (Action<float>)delegate(float a)
			{
				KnockedDurationEntry.Value = a;
			});
			obj.CreateFunction("Save Settings", Color.cyan, (Action)delegate
			{
				MelonPreferences.Save();
			});
		}

		private void SetupMelonPreferences()
		{
			category = MelonPreferences.CreateCategory("Downed");
			EnableModEntry = category.CreateEntry<bool>("Enable Mod", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			KnockedDurationEntry = category.CreateEntry<float>("Knocked Duration", 5f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			MelonPreferences.Save();
			category.SaveToFile(true);
		}

		private void OnLevelLoaded(LevelInfo levelInfo)
		{
			downed = false;
			death = false;
		}

		public override void OnUpdate()
		{
			if (!EnableModEntry.Value)
			{
				return;
			}
			RigManager rigManager = Player.RigManager;
			if (Object.op_Implicit((Object)(object)rigManager) && !Object.op_Implicit((Object)(object)rigManager.activeSeat) && !UIRig.Instance.popUpMenu.m_IsCursorShown && downed)
			{
				PhysicsRig physicsRig = Player.PhysicsRig;
				if (!physicsRig.torso.shutdown && physicsRig.ballLocoEnabled)
				{
					RagdollPlayerMod.RagdollRig(rigManager);
				}
				bool shutdown = physicsRig.shutdown;
				if (death && !shutdown)
				{
					physicsRig.ShutdownRig();
				}
			}
			if (downed && !death && Time.time - startTime >= KnockedDurationEntry.Value)
			{
				downed = false;
				RagdollPlayerMod.UnragdollRig(rigManager);
			}
			BaseController controller = GetController();
			if (Object.op_Implicit((Object)(object)controller) && GetInput(controller))
			{
				PhysicsRig physicsRig2 = Player.PhysicsRig;
				if (physicsRig2.torso.shutdown || !physicsRig2.ballLocoEnabled)
				{
					downed = false;
					death = false;
					RagdollPlayerMod.UnragdollRig(rigManager);
				}
			}
		}

		private static BaseController GetController()
		{
			return Player.RightController;
		}

		private static bool GetInput(BaseController controller)
		{
			bool thumbStickDown = controller.GetThumbStickDown();
			float time = Time.time;
			if (thumbStickDown && ragdollNextButton)
			{
				if (time - lastTimeInput <= 0.32f)
				{
					return true;
				}
				ragdollNextButton = false;
				lastTimeInput = 0f;
			}
			else if (thumbStickDown)
			{
				lastTimeInput = time;
				ragdollNextButton = true;
			}
			else if (time - lastTimeInput > 0.32f)
			{
				ragdollNextButton = false;
				lastTimeInput = 0f;
			}
			return false;
		}

		private void OnPlayerDamageReceived(RigManager rigManager, float damage)
		{
			if (EnableModEntry.Value && !(Player.RigManager.health.curr_Health > 0f))
			{
				if (downed)
				{
					death = true;
					Player.RigManager.health.Dying(5f);
				}
				if (!downed && !death)
				{
					downed = true;
					TryInvokeLifeSavingDamgeDealt(Player.RigManager.health);
					Player.RigManager.health.curr_Health = Player.RigManager.health.max_Health * 0.25f;
					startTime = Time.time;
				}
			}
		}

		private void OnPlayerDeath(RigManager rigManager)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			if (EnableModEntry.Value)
			{
				RigManager rigManager2 = Player.RigManager;
				downed = false;
				death = false;
				RagdollPlayerMod.UnragdollRig(rigManager2);
				Vector3 val = Player.PhysicsRig.feet.transform.position + new Vector3(0f, 0.25f, 0f);
				rigManager2.Teleport(val, true);
			}
		}

		private static bool TryInvokeLifeSavingDamgeDealt(object health)
		{
			if (health == null)
			{
				return false;
			}
			try
			{
				object obj = health;
				if (!(obj is Player_Health))
				{
					Component val = (Component)((health is Component) ? health : null);
					if (val != null)
					{
						obj = val.gameObject.GetComponent<Player_Health>();
						if (obj == null)
						{
							return false;
						}
					}
				}
				MethodInfo method = obj.GetType().GetMethod("LifeSavingDamgeDealt", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method == null)
				{
					return false;
				}
				method.Invoke(obj, null);
				return true;
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("Error invoking LifeSavingDamgeDealt: " + ex.Message);
				return false;
			}
		}
	}
}