Decompiled source of NoClip v1.0.0

NoClip.dll

Decompiled 2 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using Microsoft.CodeAnalysis;
using Pigeon.Movement;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[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 NoClip
{
	[BepInPlugin("com.coloron.noclip", "Noclip", "1.0.0")]
	[MycoMod(/*Could not decode attribute arguments.*/)]
	public class Noclip : BaseUnityPlugin
	{
		private float _flySpeed = 50f;

		private float _fastFlyMult = 5f;

		private bool _noclipActive = false;

		private GameObject _localPlayer;

		private CharacterController _cc;

		private Rigidbody _rb;

		private PlayerLook _playerLook;

		private Player _playerComponent;

		private FieldInfo _fiPositionNow;

		private FieldInfo _fiPositionNext;

		private List<MonoBehaviour> _disabledScripts = new List<MonoBehaviour>();

		private bool _wasCcEnabled;

		private bool _wasRbKinematic;

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[NoclipMod] v1.6.0 Loaded");
		}

		private void Update()
		{
			if (Keyboard.current != null)
			{
				if (((ButtonControl)Keyboard.current.f3Key).wasPressedThisFrame)
				{
					ToggleNoclip();
				}
				if (_noclipActive)
				{
					HandleNoclipMovement();
					HandleNoclipCamera();
				}
			}
		}

		private void FindPlayer()
		{
			if ((Object)(object)_localPlayer != (Object)null)
			{
				return;
			}
			NetworkManager singleton = NetworkManager.Singleton;
			if (((singleton != null) ? singleton.SpawnManager : null) != null)
			{
				NetworkObject localPlayerObject = NetworkManager.Singleton.SpawnManager.GetLocalPlayerObject();
				if ((Object)(object)localPlayerObject != (Object)null)
				{
					SetPlayer(((Component)localPlayerObject).gameObject);
					return;
				}
			}
			NetworkObject[] array = Object.FindObjectsByType<NetworkObject>((FindObjectsSortMode)0);
			foreach (NetworkObject val in array)
			{
				if (val.IsOwner && ((Object)(object)((Component)val).GetComponent<CharacterController>() != (Object)null || (Object)(object)((Component)val).GetComponentInChildren<Camera>() != (Object)null))
				{
					SetPlayer(((Component)val).gameObject);
					break;
				}
			}
		}

		private void SetPlayer(GameObject obj)
		{
			_localPlayer = obj;
			_cc = obj.GetComponent<CharacterController>();
			_rb = obj.GetComponent<Rigidbody>();
			_playerLook = obj.GetComponentInChildren<PlayerLook>();
			_playerComponent = obj.GetComponent<Player>() ?? obj.GetComponentInParent<Player>() ?? obj.GetComponentInChildren<Player>();
			if ((Object)(object)_playerComponent != (Object)null)
			{
				Type type = ((object)_playerComponent).GetType();
				_fiPositionNow = type.GetField("iPositionNow", BindingFlags.Instance | BindingFlags.NonPublic);
				_fiPositionNext = type.GetField("iPositionNext", BindingFlags.Instance | BindingFlags.NonPublic);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[NoclipMod] Player component found on: " + ((Object)((Component)_playerComponent).gameObject).name));
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[NoclipMod] iPositionNow field:  " + ((_fiPositionNow != null) ? "OK" : "NOT FOUND")));
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[NoclipMod] iPositionNext field: " + ((_fiPositionNext != null) ? "OK" : "NOT FOUND")));
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[NoclipMod] Player component not found — snapback patch inactive.");
			}
		}

		private void DisableLogic()
		{
			_disabledScripts.Clear();
			MonoBehaviour[] componentsInChildren = _localPlayer.GetComponentsInChildren<MonoBehaviour>();
			foreach (MonoBehaviour val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val == (Object)(object)this) && ((Behaviour)val).enabled && !(val is PlayerLook))
				{
					string text = ((object)val).GetType().Name.ToLower();
					string text2 = ((object)val).GetType().Namespace?.ToLower() ?? "";
					bool flag = text.Contains("move") || text.Contains("motor") || text.Contains("walker") || text2.Contains("movement");
					bool flag2 = (text.Contains("networktransform") || text.Contains("synctransform")) && !text.Contains("clientnetworktransform");
					bool flag3 = !text.Equals("networkobject") && !text2.Contains(".ui") && !text.Contains("ui") && !text.Contains("hud") && !text.Contains("display") && !text.Contains("matrix");
					if ((flag || flag2) && flag3)
					{
						((Behaviour)val).enabled = false;
						_disabledScripts.Add(val);
					}
				}
			}
			if ((Object)(object)_cc != (Object)null)
			{
				_wasCcEnabled = ((Collider)_cc).enabled;
				((Collider)_cc).enabled = false;
			}
			if ((Object)(object)_rb != (Object)null)
			{
				_wasRbKinematic = _rb.isKinematic;
				_rb.isKinematic = true;
			}
		}

		private void RestoreLogic()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_cc != (Object)null)
			{
				((Collider)_cc).enabled = _wasCcEnabled;
			}
			if ((Object)(object)_rb != (Object)null)
			{
				_rb.isKinematic = _wasRbKinematic;
				if (!_wasRbKinematic)
				{
					_rb.linearVelocity = Vector3.zero;
				}
			}
			foreach (MonoBehaviour disabledScript in _disabledScripts)
			{
				if ((Object)(object)disabledScript != (Object)null)
				{
					((Behaviour)disabledScript).enabled = true;
				}
			}
			_disabledScripts.Clear();
		}

		private void ToggleNoclip()
		{
			FindPlayer();
			if (!((Object)(object)_localPlayer == (Object)null))
			{
				_noclipActive = !_noclipActive;
				if (_noclipActive)
				{
					DisableLogic();
				}
				else
				{
					((MonoBehaviour)this).StartCoroutine(ExitNoclipRoutine());
				}
			}
		}

		private IEnumerator ExitNoclipRoutine()
		{
			Vector3 exitLocalPos = _localPlayer.transform.localPosition;
			if (_fiPositionNow != null && _fiPositionNext != null && (Object)(object)_playerComponent != (Object)null)
			{
				_fiPositionNow.SetValue(_playerComponent, exitLocalPos);
				_fiPositionNext.SetValue(_playerComponent, exitLocalPos);
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"[NoclipMod] Patched interpolation fields to {exitLocalPos}");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[NoclipMod] Could not patch interpolation fields — snapback may occur.");
			}
			Physics.SyncTransforms();
			yield return (object)new WaitForFixedUpdate();
			RestoreLogic();
		}

		private void HandleNoclipCamera()
		{
			//IL_002a: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: 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_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: 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_007e: 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_008b: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_playerLook == (Object)null) && Mouse.current != null)
			{
				Vector2 mouseDelta = _playerLook.GetMouseDelta();
				Transform transform = _localPlayer.transform;
				transform.localRotation *= Quaternion.Euler(0f, mouseDelta.x, 0f);
				Vector2 xLookBounds = _playerLook.XLookBounds;
				_playerLook.XLookRotation = Mathf.Clamp(_playerLook.XLookRotation - mouseDelta.y, xLookBounds.x, xLookBounds.y);
			}
		}

		private void HandleNoclipMovement()
		{
			//IL_0067: 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_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: 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_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: 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_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//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_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: 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)
			if (!((Object)(object)_localPlayer == (Object)null))
			{
				Transform val = (((Object)(object)_playerLook != (Object)null) ? ((Component)_playerLook.Camera).transform : _localPlayer.transform);
				float num = _flySpeed;
				if (((ButtonControl)Keyboard.current.leftShiftKey).isPressed)
				{
					num *= _fastFlyMult;
				}
				Vector3 val2 = Vector3.zero;
				if (((ButtonControl)Keyboard.current.wKey).isPressed)
				{
					val2 += val.forward;
				}
				if (((ButtonControl)Keyboard.current.sKey).isPressed)
				{
					val2 -= val.forward;
				}
				if (((ButtonControl)Keyboard.current.dKey).isPressed)
				{
					val2 += val.right;
				}
				if (((ButtonControl)Keyboard.current.aKey).isPressed)
				{
					val2 -= val.right;
				}
				if (((ButtonControl)Keyboard.current.spaceKey).isPressed)
				{
					val2 += Vector3.up;
				}
				if (((ButtonControl)Keyboard.current.leftCtrlKey).isPressed)
				{
					val2 -= Vector3.up;
				}
				Transform transform = _localPlayer.transform;
				transform.position += val2 * num * Time.deltaTime;
			}
		}
	}
}