Decompiled source of SuperThrow v1.0.0

Mods/SuperThrow.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BoneLib;
using BoneLib.BoneMenu;
using Il2CppSLZ.Marrow;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using SuperThrow;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: MelonInfo(typeof(Mod), "Super Throw", "1.0.0", "Custom Mods", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : System.Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(/*Could not decode attribute arguments.*/)]
	internal sealed class RefSafetyRulesAttribute : System.Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SuperThrow
{
	public sealed class Mod : MelonMod
	{
		private sealed class HandState
		{
			public Rigidbody Body;

			public Vector3 LastPosition;

			public Vector3 Motion;

			public bool Sampled;
		}

		private sealed class Throw
		{
			public Rigidbody Body;

			public Vector3 Motion;

			public int Due;

			public float Multiplier;

			public float Extra;

			public float Cap;
		}

		private MelonPreferences_Category prefs;

		private MelonPreferences_Entry<bool> enabled;

		private MelonPreferences_Entry<float> multiplier;

		private MelonPreferences_Entry<float> extraSpeed;

		private MelonPreferences_Entry<float> maxSpeed;

		private MelonPreferences_Entry<float> threshold;

		private readonly HandState left = new HandState();

		private readonly HandState right = new HandState();

		private readonly List<Throw> pending = new List<Throw>();

		private bool menuReady;

		private int tick;

		public override void OnInitializeMelon()
		{
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Expected O, but got Unknown
			prefs = MelonPreferences.CreateCategory("SuperThrow");
			enabled = prefs.CreateEntry<bool>("Enabled", false, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			multiplier = prefs.CreateEntry<float>("ThrowMultiplier", 5f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			extraSpeed = prefs.CreateEntry<float>("ExtraSpeed", 10f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			maxSpeed = prefs.CreateEntry<float>("MaxSpeed", 100f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			threshold = prefs.CreateEntry<float>("MinimumHandSpeed", 1f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			Hooking.OnGrabObject += OnGrab;
			Hooking.OnReleaseObject += OnRelease;
			Hooking.OnLevelUnloaded += new Action(Clear);
			((MelonBase)this).LoggerInstance.Msg("Loaded. Open BoneMenu > Super Throw.");
		}

		private void Save()
		{
			prefs.SaveToFile(false);
		}

		public override void OnUpdate()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			if (!menuReady && Page.Root != null)
			{
				Page val = Page.Root.CreatePage("Super Throw", Color.cyan, 0, true);
				val.CreateBool("Enabled", Color.green, enabled.Value, (Action<bool>)([CompilerGenerated] (bool v) =>
				{
					enabled.Value = v;
					pending.Clear();
					Save();
				}));
				val.CreateFloat("Throw strength (multiplier)", Color.white, Mathf.Clamp(multiplier.Value, 1f, 30f), 0.5f, 1f, 30f, (Action<float>)([CompilerGenerated] (float v) =>
				{
					multiplier.Value = v;
					Save();
				}));
				val.CreateFloat("Extra launch speed (m/s)", Color.white, Mathf.Clamp(extraSpeed.Value, 0f, 100f), 5f, 0f, 100f, (Action<float>)([CompilerGenerated] (float v) =>
				{
					extraSpeed.Value = v;
					Save();
				}));
				val.CreateFloat("Speed limit (m/s)", Color.yellow, Mathf.Clamp(maxSpeed.Value, 5f, 200f), 5f, 5f, 200f, (Action<float>)([CompilerGenerated] (float v) =>
				{
					maxSpeed.Value = v;
					Save();
				}));
				val.CreateFloat("Minimum throw motion (m/s)", Color.white, Mathf.Clamp(threshold.Value, 0.25f, 5f), 0.25f, 0.25f, 5f, (Action<float>)([CompilerGenerated] (float v) =>
				{
					threshold.Value = v;
					Save();
				}));
				menuReady = true;
			}
		}

		public override void OnLateUpdate()
		{
			if (!Player.HandsExist || (Object)(object)Player.Head == (Object)null)
			{
				Clear();
				return;
			}
			Sample(left, Player.LeftHand);
			Sample(right, Player.RightHand);
		}

		private static void Sample(HandState state, Hand hand)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: 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_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: 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_0044: 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_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_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = ((Component)hand).transform.position - Player.Head.position;
			float deltaTime = Time.deltaTime;
			if (state.Sampled && deltaTime > 0.0001f && deltaTime < 0.1f)
			{
				Vector3 val2 = (val - state.LastPosition) / deltaTime;
				state.Motion = ((((Vector3)(ref val2)).magnitude < 25f) ? Vector3.Lerp(state.Motion, val2, 0.65f) : Vector3.zero);
			}
			else
			{
				state.Motion = Vector3.zero;
			}
			state.LastPosition = val;
			state.Sampled = true;
			state.Body = Player.GetComponentInHand<Rigidbody>(hand);
		}

		private HandState State(Hand hand)
		{
			if ((Object)(object)hand == (Object)(object)Player.LeftHand)
			{
				return left;
			}
			if ((Object)(object)hand == (Object)(object)Player.RightHand)
			{
				return right;
			}
			return null;
		}

		private void OnGrab(GameObject obj, Hand hand)
		{
			HandState handState = State(hand);
			if (handState != null && !((Object)(object)obj == (Object)null))
			{
				handState.Body = obj.GetComponentInParent<Rigidbody>();
				if ((Object)(object)handState.Body == (Object)null)
				{
					handState.Body = obj.GetComponentInChildren<Rigidbody>();
				}
			}
		}

		private void OnRelease(Hand hand)
		{
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			HandState handState = State(hand);
			if (handState == null)
			{
				return;
			}
			Rigidbody body = handState.Body;
			handState.Body = null;
			if (!enabled.Value || (Object)(object)body == (Object)null || body.isKinematic || ((Vector3)(ref handState.Motion)).magnitude < Mathf.Clamp(threshold.Value, 0.25f, 5f) || IsHeld(body) || IsPlayerBody(body))
			{
				return;
			}
			Enumerator<Throw> enumerator = pending.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Throw current = enumerator.Current;
					if ((Object)(object)current.Body == (Object)(object)body)
					{
						return;
					}
				}
			}
			finally
			{
				((System.IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
			pending.Add(new Throw
			{
				Body = body,
				Motion = handState.Motion,
				Due = tick + 2,
				Multiplier = Mathf.Clamp(multiplier.Value, 1f, 30f),
				Extra = Mathf.Clamp(extraSpeed.Value, 0f, 100f),
				Cap = Mathf.Clamp(maxSpeed.Value, 5f, 200f)
			});
		}

		private static bool IsHeld(Rigidbody body)
		{
			return (Object)(object)Player.GetComponentInHand<Rigidbody>(Player.LeftHand) == (Object)(object)body || (Object)(object)Player.GetComponentInHand<Rigidbody>(Player.RightHand) == (Object)(object)body;
		}

		private static bool IsPlayerBody(Rigidbody body)
		{
			return (Object)(object)Player.PhysicsRig != (Object)null && ((Component)body).transform.IsChildOf(((Component)Player.PhysicsRig).transform);
		}

		public override void OnFixedUpdate()
		{
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: 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_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			tick++;
			for (int num = pending.Count - 1; num >= 0; num--)
			{
				Throw obj = pending[num];
				if (tick >= obj.Due)
				{
					pending.RemoveAt(num);
					Rigidbody body = obj.Body;
					if (enabled.Value && !((Object)(object)body == (Object)null) && !body.isKinematic && !IsHeld(body) && !IsPlayerBody(body))
					{
						Vector3 val = body.velocity;
						if (((Vector3)(ref val)).sqrMagnitude < 0.25f)
						{
							val = obj.Motion;
						}
						Vector3 val2 = val * obj.Multiplier + ((Vector3)(ref val)).normalized * obj.Extra;
						if (!float.IsNaN(((Vector3)(ref val2)).sqrMagnitude) && !float.IsInfinity(((Vector3)(ref val2)).sqrMagnitude))
						{
							body.velocity = Vector3.ClampMagnitude(val2, obj.Cap);
							body.WakeUp();
						}
					}
				}
			}
		}

		private void Clear()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			pending.Clear();
			left.Body = (right.Body = null);
			left.Sampled = (right.Sampled = false);
			left.Motion = (right.Motion = Vector3.zero);
		}

		public override void OnDeinitializeMelon()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			Hooking.OnGrabObject -= OnGrab;
			Hooking.OnReleaseObject -= OnRelease;
			Hooking.OnLevelUnloaded -= new Action(Clear);
			Clear();
		}
	}
}