Decompiled source of UltimateTelekinesis2 v0.2.7

Mods/UltimateTelekinesis2.dll

Decompiled 16 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using MelonLoader;
using Microsoft.CodeAnalysis;
using UltimateTelekinesis2;
using UnityEngine;
using UnityEngine.XR;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(UltimateTelekinesisMod), "Ultimate Telekinesis 2.0", "0.2.7", "GravWarden", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("0.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 UltimateTelekinesis2
{
	public sealed class UltimateTelekinesisMod : MelonMod
	{
		private const float MaxRange = 18f;

		private const float HoldDistance = 3.5f;

		private const float FollowStrength = 12f;

		private const float MaxFollowSpeed = 12f;

		private const float ThrowSpeed = 12f;

		private const float CastRadius = 0.08f;

		private InputDevice _rightController;

		private bool _previousTrigger;

		private bool _previousGrip;

		private Rigidbody _heldBody;

		private bool _originalUseGravity;

		private bool _loggedControllerWarning;

		private Transform _cameraTransform;

		public override void OnInitializeMelon()
		{
			MelonLogger.Msg("Initialized. Right trigger grabs/releases; right grip throws.");
		}

		public override void OnUpdate()
		{
			RefreshController();
			if (!((InputDevice)(ref _rightController)).isValid)
			{
				if (!_loggedControllerWarning)
				{
					MelonLogger.Warning("Right XR controller not detected. Check headset/controller tracking.");
					_loggedControllerWarning = true;
				}
				return;
			}
			bool flag = default(bool);
			((InputDevice)(ref _rightController)).TryGetFeatureValue(CommonUsages.triggerButton, ref flag);
			bool flag2 = default(bool);
			((InputDevice)(ref _rightController)).TryGetFeatureValue(CommonUsages.gripButton, ref flag2);
			if (flag && !_previousTrigger)
			{
				if ((Object)(object)_heldBody == (Object)null)
				{
					TryGrab();
				}
				else
				{
					ReleaseHeld();
				}
			}
			if (flag2 && !_previousGrip && (Object)(object)_heldBody != (Object)null)
			{
				ThrowHeld();
			}
			_previousTrigger = flag;
			_previousGrip = flag2;
			if ((Object)(object)_heldBody != (Object)null)
			{
				UpdateHeldObject();
			}
		}

		private void RefreshController()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			if (!((InputDevice)(ref _rightController)).isValid)
			{
				_rightController = InputDevices.GetDeviceAtXRNode((XRNode)5);
			}
			Camera main = Camera.main;
			if ((Object)(object)main != (Object)null)
			{
				_cameraTransform = ((Component)main).transform;
			}
		}

		private bool TryGetHandPose(out Vector3 worldPosition, out Quaternion worldRotation)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			worldPosition = Vector3.zero;
			worldRotation = Quaternion.identity;
			if (!((InputDevice)(ref _rightController)).isValid)
			{
				return false;
			}
			Vector3 val = default(Vector3);
			if (!((InputDevice)(ref _rightController)).TryGetFeatureValue(CommonUsages.devicePosition, ref val))
			{
				return false;
			}
			Quaternion val2 = default(Quaternion);
			if (!((InputDevice)(ref _rightController)).TryGetFeatureValue(CommonUsages.deviceRotation, ref val2))
			{
				return false;
			}
			Transform val3 = (((Object)(object)_cameraTransform != (Object)null && (Object)(object)_cameraTransform.parent != (Object)null) ? _cameraTransform.parent : (((Object)(object)_cameraTransform != (Object)null) ? _cameraTransform : null));
			if ((Object)(object)val3 == (Object)null)
			{
				return false;
			}
			worldPosition = val3.TransformPoint(val);
			worldRotation = val3.rotation * val2;
			return true;
		}

		private void TryGrab()
		{
			//IL_0017: 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_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			if (!TryGetHandPose(out var worldPosition, out var worldRotation))
			{
				MelonLogger.Warning("Could not read right-hand pose.");
				return;
			}
			Vector3 val = worldRotation * Vector3.forward;
			RaycastHit val2 = default(RaycastHit);
			if (!Physics.SphereCast(worldPosition, 0.08f, val, ref val2, 18f, -5, (QueryTriggerInteraction)1))
			{
				MelonLogger.Msg("No object hit.");
				return;
			}
			Rigidbody rigidbody = ((RaycastHit)(ref val2)).rigidbody;
			if ((Object)(object)rigidbody == (Object)null || rigidbody.isKinematic)
			{
				MelonLogger.Msg("Target has no movable Rigidbody.");
				return;
			}
			_heldBody = rigidbody;
			_originalUseGravity = rigidbody.useGravity;
			rigidbody.useGravity = false;
			rigidbody.velocity = Vector3.zero;
			rigidbody.angularVelocity = Vector3.zero;
			MelonLogger.Msg("Grabbed: " + ((Object)rigidbody).name);
		}

		private void UpdateHeldObject()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: 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_0032: 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_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: 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_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_heldBody == (Object)null) && TryGetHandPose(out var worldPosition, out var worldRotation))
			{
				Vector3 val = Vector3.ClampMagnitude((worldPosition + worldRotation * Vector3.forward * 3.5f - _heldBody.worldCenterOfMass) * 12f, 12f);
				_heldBody.velocity = Vector3.Lerp(_heldBody.velocity, val, Mathf.Clamp01(Time.deltaTime * 10f));
				_heldBody.angularVelocity = Vector3.Lerp(_heldBody.angularVelocity, Vector3.zero, Mathf.Clamp01(Time.deltaTime * 4f));
			}
		}

		private void ThrowHeld()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_heldBody == (Object)null))
			{
				if (TryGetHandPose(out var _, out var worldRotation))
				{
					_heldBody.velocity = worldRotation * Vector3.forward * 12f;
				}
				RestoreGravityAndClear();
				MelonLogger.Msg("Thrown.");
			}
		}

		private void ReleaseHeld()
		{
			RestoreGravityAndClear();
			MelonLogger.Msg("Released.");
		}

		private void RestoreGravityAndClear()
		{
			if ((Object)(object)_heldBody != (Object)null)
			{
				_heldBody.useGravity = _originalUseGravity;
			}
			_heldBody = null;
		}

		public override void OnDeinitializeMelon()
		{
			RestoreGravityAndClear();
		}
	}
}