Decompiled source of forcethrow v1.7.0

Mods\forcethrow.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BoneLib;
using BoneLib.BoneMenu;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using MelonLoader;
using Microsoft.CodeAnalysis;
using UnityEngine;
using forcethrow;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(Main), "forcethrow", "1.7.0", "thegbus", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace forcethrow
{
	public class Main : MelonMod
	{
		private struct PhysicsState
		{
			public float mass;

			public bool useGravity;
		}

		[CompilerGenerated]
		private sealed class <DelayedThrow>d__18 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public Rigidbody rb;

			public Vector3 dir;

			public float speed;

			public Main <>4__this;

			private Transform <root>5__1;

			private Il2CppArrayBase<Rigidbody> <allRbs>5__2;

			private IEnumerator<Rigidbody> <>s__3;

			private Rigidbody <childRb>5__4;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <DelayedThrow>d__18(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<root>5__1 = null;
				<allRbs>5__2 = null;
				<>s__3 = null;
				<childRb>5__4 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0036: Unknown result type (might be due to invalid IL or missing references)
				//IL_0040: Expected O, but got Unknown
				//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00db: Unknown result type (might be due to invalid IL or missing references)
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					if (_throwDelay > 0f)
					{
						<>2__current = (object)new WaitForSeconds(_throwDelay);
						<>1__state = 1;
						return true;
					}
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if ((Object)(object)rb != (Object)null && !_currentlyGrabbed.Contains(rb))
				{
					<root>5__1 = ((Component)rb).transform.root;
					<allRbs>5__2 = ((Component)<root>5__1).GetComponentsInChildren<Rigidbody>();
					<>s__3 = <allRbs>5__2.GetEnumerator();
					try
					{
						while (<>s__3.MoveNext())
						{
							<childRb>5__4 = <>s__3.Current;
							<childRb>5__4.velocity = dir * speed;
							<childRb>5__4 = null;
						}
					}
					finally
					{
						if (<>s__3 != null)
						{
							<>s__3.Dispose();
						}
					}
					<>s__3 = null;
					<root>5__1 = null;
					<allRbs>5__2 = null;
				}
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private static bool _forceThrowActive = true;

		private static float _forceThrowMultiplier = 10f;

		private static float _forceThreshold = 0.5f;

		private static float _throwDelay = 0.1f;

		private static bool _weightlessGrab = false;

		private static Dictionary<Rigidbody, PhysicsState> _originalPhysics = new Dictionary<Rigidbody, PhysicsState>();

		private static HashSet<Rigidbody> _currentlyGrabbed = new HashSet<Rigidbody>();

		private static HashSet<Rigidbody> _lastGrabbedItems = new HashSet<Rigidbody>();

		private static Page? _mainPage;

		public override void OnInitializeMelon()
		{
			SetupMenu();
		}

		public override void OnLateUpdate()
		{
			if (_forceThrowActive || _weightlessGrab)
			{
				UpdateForceThrow();
			}
		}

		private void SetupMenu()
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: 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_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			if (_mainPage == null)
			{
				_mainPage = Page.Root.CreatePage("forcethrow", Color.yellow, 0, true);
			}
			_mainPage.RemoveAll();
			_mainPage.CreateBool("Enabled", Color.green, _forceThrowActive, (Action<bool>)delegate(bool v)
			{
				_forceThrowActive = v;
			});
			_mainPage.CreateBool("Weightless Grab", Color.cyan, _weightlessGrab, (Action<bool>)delegate(bool v)
			{
				_weightlessGrab = v;
			});
			_mainPage.CreateFunction($"Current Extra Force: {_forceThrowMultiplier:F1}", Color.white, (Action)null);
			Page val = _mainPage.CreatePage("Adjust Force Strength", Color.yellow, 0, true);
			val.CreateFunction("+100", Color.green, (Action)delegate
			{
				_forceThrowMultiplier += 100f;
				SetupMenu();
			});
			val.CreateFunction("+10", Color.green, (Action)delegate
			{
				_forceThrowMultiplier += 10f;
				SetupMenu();
			});
			val.CreateFunction("+1", Color.green, (Action)delegate
			{
				_forceThrowMultiplier += 1f;
				SetupMenu();
			});
			val.CreateFunction("+0.5", Color.green, (Action)delegate
			{
				_forceThrowMultiplier += 0.5f;
				SetupMenu();
			});
			val.CreateFunction("-0.5", Color.red, (Action)delegate
			{
				_forceThrowMultiplier = Math.Max(0f, _forceThrowMultiplier - 0.5f);
				SetupMenu();
			});
			val.CreateFunction("-1", Color.red, (Action)delegate
			{
				_forceThrowMultiplier = Math.Max(0f, _forceThrowMultiplier - 1f);
				SetupMenu();
			});
			val.CreateFunction("-10", Color.red, (Action)delegate
			{
				_forceThrowMultiplier = Math.Max(0f, _forceThrowMultiplier - 10f);
				SetupMenu();
			});
			val.CreateFunction("-100", Color.red, (Action)delegate
			{
				_forceThrowMultiplier = Math.Max(0f, _forceThrowMultiplier - 100f);
				SetupMenu();
			});
			_mainPage.CreateFloat("Release Delay", Color.white, _throwDelay, 0.05f, 0f, 1f, (Action<float>)delegate(float v)
			{
				_throwDelay = v;
			});
			_mainPage.CreateFunction("Reset Defaults", Color.red, (Action)delegate
			{
				_forceThrowMultiplier = 10f;
				_throwDelay = 0.1f;
				_weightlessGrab = false;
				SetupMenu();
			});
		}

		private void UpdateForceThrow()
		{
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)Player.RigManager == (Object)null || (Object)(object)Player.RigManager.physicsRig == (Object)null)
				{
					return;
				}
				_currentlyGrabbed.Clear();
				CheckHandForItems(Player.RigManager.physicsRig.leftHand);
				CheckHandForItems(Player.RigManager.physicsRig.rightHand);
				if (_weightlessGrab)
				{
					foreach (Rigidbody item in _currentlyGrabbed)
					{
						Transform root = ((Component)item).transform.root;
						Il2CppArrayBase<Rigidbody> componentsInChildren = ((Component)root).GetComponentsInChildren<Rigidbody>();
						foreach (Rigidbody item2 in componentsInChildren)
						{
							if (!_originalPhysics.ContainsKey(item2))
							{
								_originalPhysics[item2] = new PhysicsState
								{
									mass = item2.mass,
									useGravity = item2.useGravity
								};
								item2.mass = 0.0001f;
								item2.useGravity = false;
							}
						}
					}
				}
				foreach (Rigidbody lastGrabbedItem in _lastGrabbedItems)
				{
					if (!((Object)(object)lastGrabbedItem != (Object)null) || _currentlyGrabbed.Contains(lastGrabbedItem))
					{
						continue;
					}
					RestoreRootPhysics(lastGrabbedItem);
					if (_forceThrowActive)
					{
						Vector3 velocity = lastGrabbedItem.velocity;
						if (((Vector3)(ref velocity)).sqrMagnitude > _forceThreshold)
						{
							velocity = lastGrabbedItem.velocity;
							Vector3 normalized = ((Vector3)(ref velocity)).normalized;
							velocity = lastGrabbedItem.velocity;
							float speed = ((Vector3)(ref velocity)).magnitude + _forceThrowMultiplier;
							MelonCoroutines.Start(DelayedThrow(lastGrabbedItem, normalized, speed));
						}
					}
				}
				if (!_weightlessGrab && _originalPhysics.Count > 0)
				{
					RestoreAllPhysics();
				}
				_lastGrabbedItems.Clear();
				foreach (Rigidbody item3 in _currentlyGrabbed)
				{
					_lastGrabbedItems.Add(item3);
				}
				if (Time.frameCount % 300 == 0)
				{
					CleanupDeadReferences();
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Error("ForceThrow Error: " + ex.Message);
			}
		}

		private void CheckHandForItems(Hand hand)
		{
			if (!((Object)(object)hand == (Object)null) && !((Object)(object)hand.m_CurrentAttachedGO == (Object)null))
			{
				Rigidbody componentInParent = hand.m_CurrentAttachedGO.GetComponentInParent<Rigidbody>();
				if ((Object)(object)componentInParent != (Object)null && (Object)(object)((Component)componentInParent).transform.root != (Object)(object)((Component)Player.RigManager).transform.root)
				{
					_currentlyGrabbed.Add(componentInParent);
				}
			}
		}

		private void RestoreRootPhysics(Rigidbody rb)
		{
			if ((Object)(object)rb == (Object)null)
			{
				return;
			}
			Transform root = ((Component)rb).transform.root;
			Il2CppArrayBase<Rigidbody> componentsInChildren = ((Component)root).GetComponentsInChildren<Rigidbody>();
			foreach (Rigidbody item in componentsInChildren)
			{
				if (_originalPhysics.TryGetValue(item, out var value))
				{
					item.mass = value.mass;
					item.useGravity = value.useGravity;
					_originalPhysics.Remove(item);
				}
			}
		}

		private void RestoreAllPhysics()
		{
			foreach (KeyValuePair<Rigidbody, PhysicsState> originalPhysic in _originalPhysics)
			{
				if ((Object)(object)originalPhysic.Key != (Object)null)
				{
					originalPhysic.Key.mass = originalPhysic.Value.mass;
					originalPhysic.Key.useGravity = originalPhysic.Value.useGravity;
				}
			}
			_originalPhysics.Clear();
		}

		private void CleanupDeadReferences()
		{
			List<Rigidbody> list = _originalPhysics.Keys.ToList();
			foreach (Rigidbody item in list)
			{
				if ((Object)(object)item == (Object)null)
				{
					_originalPhysics.Remove(item);
				}
			}
		}

		[IteratorStateMachine(typeof(<DelayedThrow>d__18))]
		private IEnumerator DelayedThrow(Rigidbody rb, Vector3 dir, float speed)
		{
			//IL_0015: 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)
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <DelayedThrow>d__18(0)
			{
				<>4__this = this,
				rb = rb,
				dir = dir,
				speed = speed
			};
		}
	}
}