Decompiled source of PaintingVRControllerSmoother v1.0.3

mods/PaintingVRControllerMotionSmoothing.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using MelonLoader;
using PaintingVR.Controller.MotionSmoothing;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(MotionSmoother), "Painting VR Controller Smoother", "1.5.2", "Nitric Acid", null)]
[assembly: MelonGame("Oisoi", "PaintingVr")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("PaintingVRControllerMotionSmoothing")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PaintingVRControllerMotionSmoothing")]
[assembly: AssemblyTitle("PaintingVRControllerMotionSmoothing")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PaintingVR.Controller.MotionSmoothing;

public class MotionSmoother : MelonMod
{
	[CompilerGenerated]
	private sealed class <FindControllerAndHands>d__22 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public MotionSmoother <>4__this;

		private int <attempts>5__1;

		private Transform <trackingSpace>5__2;

		private int <i>5__3;

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

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

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

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<trackingSpace>5__2 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: 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_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: 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_01db: Unknown result type (might be due to invalid IL or missing references)
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<attempts>5__1 = 0;
				break;
			case 1:
			{
				<>1__state = -1;
				GameObject obj = GameObject.Find("TrackingSpace");
				<trackingSpace>5__2 = ((obj != null) ? obj.transform : null);
				if ((Object)(object)<trackingSpace>5__2 != (Object)null)
				{
					<>4__this.leftController = <trackingSpace>5__2.Find("LeftHandAnchor");
					<>4__this.rightController = <trackingSpace>5__2.Find("RightHandAnchor");
					if ((Object)(object)<>4__this.leftController != (Object)null && (Object)(object)<>4__this.rightController != (Object)null)
					{
						<i>5__3 = 0;
						while (<i>5__3 < 5)
						{
							<>4__this.leftPosBuffer[<i>5__3] = <>4__this.leftController.position;
							<>4__this.rightPosBuffer[<i>5__3] = <>4__this.rightController.position;
							<>4__this.leftRotBuffer[<i>5__3] = <>4__this.leftController.rotation;
							<>4__this.rightRotBuffer[<i>5__3] = <>4__this.rightController.rotation;
							<i>5__3++;
						}
						<>4__this.lastLeftPos = <>4__this.leftController.position;
						<>4__this.lastRightPos = <>4__this.rightController.position;
						MelonLogger.Msg("[Painting VR Controller Smoother] Initialization successful.");
						return false;
					}
				}
				<attempts>5__1++;
				<trackingSpace>5__2 = null;
				break;
			}
			}
			if (<attempts>5__1 < 10)
			{
				<>2__current = (object)new WaitForSeconds(0.5f);
				<>1__state = 1;
				return true;
			}
			MelonLogger.Error("[Painting VR Controller Smoother] Failed to initialize after multiple attempts.");
			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 bool isVisible = true;

	private float smoothingValue = 0.8667f;

	private float deadzoneThreshold = 0.005f;

	private Rect windowRect = new Rect(20f, 20f, 300f, 220f);

	private bool isDragging = false;

	private bool useFullRange = false;

	private Transform leftController;

	private Transform rightController;

	private Vector3[] leftPosBuffer = (Vector3[])(object)new Vector3[5];

	private Vector3[] rightPosBuffer = (Vector3[])(object)new Vector3[5];

	private Quaternion[] leftRotBuffer = (Quaternion[])(object)new Quaternion[5];

	private Quaternion[] rightRotBuffer = (Quaternion[])(object)new Quaternion[5];

	private int bufferIndex;

	private Vector3 lastLeftPos;

	private Vector3 lastRightPos;

	private bool leftIsMoving = false;

	private bool rightIsMoving = false;

	private float leftStillTime = 0f;

	private float rightStillTime = 0f;

	private const float STILL_THRESHOLD_TIME = 0.5f;

	private const float STOP_THRESHOLD_SCALE = 0.1f;

	public override void OnSceneWasInitialized(int buildIndex, string sceneName)
	{
		MelonCoroutines.Start(FindControllerAndHands());
	}

	[IteratorStateMachine(typeof(<FindControllerAndHands>d__22))]
	private IEnumerator FindControllerAndHands()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <FindControllerAndHands>d__22(0)
		{
			<>4__this = this
		};
	}

	public override void OnUpdate()
	{
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: 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_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: 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_008c: 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_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_0218: Unknown result type (might be due to invalid IL or missing references)
		//IL_021d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0201: Unknown result type (might be due to invalid IL or missing references)
		//IL_0202: Unknown result type (might be due to invalid IL or missing references)
		//IL_025a: Unknown result type (might be due to invalid IL or missing references)
		//IL_025f: Unknown result type (might be due to invalid IL or missing references)
		//IL_023c: Unknown result type (might be due to invalid IL or missing references)
		//IL_023d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0243: Unknown result type (might be due to invalid IL or missing references)
		//IL_0244: Unknown result type (might be due to invalid IL or missing references)
		//IL_0277: Unknown result type (might be due to invalid IL or missing references)
		//IL_027c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0293: Unknown result type (might be due to invalid IL or missing references)
		//IL_0298: Unknown result type (might be due to invalid IL or missing references)
		//IL_029d: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_0368: Unknown result type (might be due to invalid IL or missing references)
		//IL_036f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0374: Unknown result type (might be due to invalid IL or missing references)
		//IL_0376: Unknown result type (might be due to invalid IL or missing references)
		//IL_037d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0382: Unknown result type (might be due to invalid IL or missing references)
		//IL_031c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0326: Unknown result type (might be due to invalid IL or missing references)
		//IL_0330: Unknown result type (might be due to invalid IL or missing references)
		//IL_0335: Unknown result type (might be due to invalid IL or missing references)
		//IL_0337: Unknown result type (might be due to invalid IL or missing references)
		//IL_0341: Unknown result type (might be due to invalid IL or missing references)
		//IL_034b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0350: Unknown result type (might be due to invalid IL or missing references)
		//IL_0302: Unknown result type (might be due to invalid IL or missing references)
		//IL_0307: Unknown result type (might be due to invalid IL or missing references)
		//IL_0311: Unknown result type (might be due to invalid IL or missing references)
		//IL_0316: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03da: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_0411: Unknown result type (might be due to invalid IL or missing references)
		//IL_0416: Unknown result type (might be due to invalid IL or missing references)
		//IL_041a: Unknown result type (might be due to invalid IL or missing references)
		if (Input.GetKeyDown((KeyCode)289))
		{
			isVisible = !isVisible;
		}
		if ((Object)(object)leftController == (Object)null || (Object)(object)rightController == (Object)null)
		{
			MelonLogger.Warning("[Painting VR Controller Smoother] Controller not found, skipping update.");
			return;
		}
		Vector3 position = leftController.position;
		Vector3 position2 = rightController.position;
		float deltaTime = Time.deltaTime;
		Vector3 val = position - lastLeftPos;
		float magnitude = ((Vector3)(ref val)).magnitude;
		val = position2 - lastRightPos;
		float magnitude2 = ((Vector3)(ref val)).magnitude;
		float num = deadzoneThreshold * 0.1f;
		if (!leftIsMoving && magnitude > deadzoneThreshold)
		{
			leftIsMoving = true;
			leftStillTime = 0f;
		}
		else if (leftIsMoving && magnitude < num)
		{
			leftStillTime += deltaTime;
			if (leftStillTime >= 0.5f)
			{
				leftIsMoving = false;
			}
		}
		else if (leftIsMoving)
		{
			leftStillTime = 0f;
		}
		if (!rightIsMoving && magnitude2 > deadzoneThreshold)
		{
			rightIsMoving = true;
			rightStillTime = 0f;
		}
		else if (rightIsMoving && magnitude2 < num)
		{
			rightStillTime += deltaTime;
			if (rightStillTime >= 0.5f)
			{
				rightIsMoving = false;
			}
		}
		else if (rightIsMoving)
		{
			rightStillTime = 0f;
		}
		bufferIndex = (bufferIndex + 1) % 5;
		if (leftIsMoving)
		{
			leftPosBuffer[bufferIndex] = position;
			lastLeftPos = position;
		}
		else
		{
			leftPosBuffer[bufferIndex] = lastLeftPos;
		}
		if (rightIsMoving)
		{
			rightPosBuffer[bufferIndex] = position2;
			lastRightPos = position2;
		}
		else
		{
			rightPosBuffer[bufferIndex] = lastRightPos;
		}
		leftRotBuffer[bufferIndex] = leftController.rotation;
		rightRotBuffer[bufferIndex] = rightController.rotation;
		Vector3 val2 = Vector3.zero;
		Vector3 val3 = Vector3.zero;
		Quaternion val4 = Quaternion.identity;
		Quaternion val5 = Quaternion.identity;
		for (int i = 0; i < 5; i++)
		{
			val2 += leftPosBuffer[i];
			val3 += rightPosBuffer[i];
			if (i == 0)
			{
				val4 = leftRotBuffer[i];
				val5 = rightRotBuffer[i];
			}
			else
			{
				val4 = Quaternion.Slerp(val4, leftRotBuffer[i], 0.5f);
				val5 = Quaternion.Slerp(val5, rightRotBuffer[i], 0.5f);
			}
		}
		val2 /= 5f;
		val3 /= 5f;
		float num2 = (useFullRange ? 1f : 0.75f);
		float num3 = smoothingValue * num2;
		leftController.position = Vector3.Lerp(leftController.position, val2, num3);
		rightController.position = Vector3.Lerp(rightController.position, val3, num3);
		leftController.rotation = Quaternion.Slerp(leftController.rotation, val4, num3);
		rightController.rotation = Quaternion.Slerp(rightController.rotation, val5, num3);
	}

	private float GetActualSmoothingPercent()
	{
		return smoothingValue * (useFullRange ? 100f : 75f);
	}

	public override void OnGUI()
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Expected O, but got Unknown
		//IL_0086: 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)
		if (isVisible)
		{
			GUI.color = new Color(0f, 0f, 0f, 0.5f);
			GUI.Box(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), "");
			GUI.color = Color.white;
			windowRect = GUI.Window(0, windowRect, new WindowFunction(DrawWindow), "", GUI.skin.window);
			((Rect)(ref windowRect)).x = Mathf.Clamp(((Rect)(ref windowRect)).x, 0f, (float)Screen.width - ((Rect)(ref windowRect)).width);
			((Rect)(ref windowRect)).y = Mathf.Clamp(((Rect)(ref windowRect)).y, 0f, (float)Screen.height - ((Rect)(ref windowRect)).height);
		}
	}

	private void DrawWindow(int windowID)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: 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_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_0187: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0251: Unknown result type (might be due to invalid IL or missing references)
		GUI.Label(new Rect(10f, 10f, ((Rect)(ref windowRect)).width - 20f, 30f), "Painting VR Controller Smoother");
		GUI.Box(new Rect(10f, 45f, ((Rect)(ref windowRect)).width - 20f, 2f), "");
		GUI.Label(new Rect(15f, 60f, ((Rect)(ref windowRect)).width - 30f, 25f), $"Smoothing: {smoothingValue * 100f:F0}% (Actual: {GetActualSmoothingPercent():F0}%)");
		float num = GUI.HorizontalSlider(new Rect(15f, 90f, ((Rect)(ref windowRect)).width - 30f, 20f), smoothingValue, 0f, 1f);
		if (num != smoothingValue)
		{
			smoothingValue = num;
			MelonLogger.Msg($"[Painting VR Controller Smoother] Smoothing updated: {GetActualSmoothingPercent():F0}%");
		}
		GUI.Label(new Rect(15f, 120f, ((Rect)(ref windowRect)).width - 30f, 25f), $"Deadzone: {deadzoneThreshold * 1000f:F1} mm");
		float num2 = GUI.HorizontalSlider(new Rect(15f, 150f, ((Rect)(ref windowRect)).width - 30f, 20f), deadzoneThreshold, 0f, 0.02f);
		if (num2 != deadzoneThreshold)
		{
			deadzoneThreshold = num2;
			MelonLogger.Msg($"[Painting VR Controller Smoother] Deadzone updated: {deadzoneThreshold * 1000f:F1} mm");
		}
		if (GUI.Button(new Rect(15f, 180f, ((Rect)(ref windowRect)).width - 30f, 25f), useFullRange ? "Using Full Range (100% Max)" : "Using Limited Range (75% Max)"))
		{
			useFullRange = !useFullRange;
		}
		if (GUI.Button(new Rect(((Rect)(ref windowRect)).width - 30f, 5f, 25f, 25f), "X"))
		{
			isVisible = false;
		}
		HandleWindowDrag();
	}

	private void HandleWindowDrag()
	{
		//IL_0029: 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_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: 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)
		//IL_0047: Expected I4, but got Unknown
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(0f, 0f, ((Rect)(ref windowRect)).width, 20f);
		Event current = Event.current;
		EventType type = current.type;
		EventType val2 = type;
		switch ((int)val2)
		{
		case 0:
			if (((Rect)(ref val)).Contains(current.mousePosition))
			{
				isDragging = true;
			}
			break;
		case 1:
			isDragging = false;
			break;
		case 3:
			if (isDragging)
			{
				ref Rect reference = ref windowRect;
				((Rect)(ref reference)).x = ((Rect)(ref reference)).x + current.delta.x;
				ref Rect reference2 = ref windowRect;
				((Rect)(ref reference2)).y = ((Rect)(ref reference2)).y + current.delta.y;
			}
			break;
		case 2:
			break;
		}
	}
}