Decompiled source of EnderPearlMod v2.1.1

EnderPearlMod.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using REPOLib.Modules;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp-firstpass")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("Autodesk.Fbx")]
[assembly: IgnoresAccessChecksTo("Facepunch.Steamworks.Win64")]
[assembly: IgnoresAccessChecksTo("FbxBuildTestAssets")]
[assembly: IgnoresAccessChecksTo("Klattersynth")]
[assembly: IgnoresAccessChecksTo("Photon3Unity3D")]
[assembly: IgnoresAccessChecksTo("PhotonChat")]
[assembly: IgnoresAccessChecksTo("PhotonRealtime")]
[assembly: IgnoresAccessChecksTo("PhotonUnityNetworking")]
[assembly: IgnoresAccessChecksTo("PhotonUnityNetworking.Utilities")]
[assembly: IgnoresAccessChecksTo("PhotonVoice.API")]
[assembly: IgnoresAccessChecksTo("PhotonVoice")]
[assembly: IgnoresAccessChecksTo("PhotonVoice.PUN")]
[assembly: IgnoresAccessChecksTo("SingularityGroup.HotReload.Runtime")]
[assembly: IgnoresAccessChecksTo("SingularityGroup.HotReload.Runtime.Public")]
[assembly: IgnoresAccessChecksTo("Sirenix.OdinInspector.Attributes")]
[assembly: IgnoresAccessChecksTo("Sirenix.Serialization.Config")]
[assembly: IgnoresAccessChecksTo("Sirenix.Serialization")]
[assembly: IgnoresAccessChecksTo("Sirenix.Utilities")]
[assembly: IgnoresAccessChecksTo("Unity.AI.Navigation")]
[assembly: IgnoresAccessChecksTo("Unity.Formats.Fbx.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem.ForUI")]
[assembly: IgnoresAccessChecksTo("Unity.Postprocessing.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.ShaderLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.ShaderGraph.ShaderGraphLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.TextMeshPro")]
[assembly: IgnoresAccessChecksTo("Unity.Timeline")]
[assembly: IgnoresAccessChecksTo("Unity.VisualScripting.Antlr3.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.VisualScripting.Core")]
[assembly: IgnoresAccessChecksTo("Unity.VisualScripting.Flow")]
[assembly: IgnoresAccessChecksTo("Unity.VisualScripting.State")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ARModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.NVIDIAModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UI")]
[assembly: IgnoresAccessChecksTo("websocket-sharp")]
[assembly: AssemblyCompany("CarsonJF")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("EnderPearlMod Mod for REPO")]
[assembly: AssemblyFileVersion("2.1.1.0")]
[assembly: AssemblyInformationalVersion("2.1.1+63e5234df5854796374718135f47009a3a072dec")]
[assembly: AssemblyProduct("EnderPearlMod")]
[assembly: AssemblyTitle("EnderPearlMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.1.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 EnderPearlMod
{
	public static class Vector3Extensions
	{
		public static bool IsFinite(this Vector3 v)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: 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)
			return float.IsFinite(v.x) && float.IsFinite(v.y) && float.IsFinite(v.z);
		}
	}
	public class EnderPearl : MonoBehaviour
	{
		public enum States
		{
			Idle,
			Active,
			Used
		}

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

			private object <>2__current;

			public PlayerController controller;

			public Vector3 targetPos;

			public EnderPearl <>4__this;

			private bool <wasKinematic>5__1;

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

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

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

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

			private bool MoveNext()
			{
				//IL_0041: Unknown result type (might be due to invalid IL or missing references)
				//IL_004b: Expected O, but got Unknown
				//IL_0066: 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_01f0: Unknown result type (might be due to invalid IL or missing references)
				//IL_0248: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
				//IL_008d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0138: Unknown result type (might be due to invalid IL or missing references)
				//IL_014e: 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_0227: Expected O, but got Unknown
				//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00df: Expected O, but got Unknown
				//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
				//IL_01cf: Expected O, but got Unknown
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<>2__current = (object)new WaitForFixedUpdate();
					<>1__state = 1;
					return true;
				case 1:
					<>1__state = -1;
					if (Vector3.Distance(((Component)controller).transform.position, targetPos) > 0.1f)
					{
						((Component)controller).transform.position = targetPos;
					}
					if (!<>4__this.IsPositionInBounds(((Component)controller).transform.position))
					{
						<>4__this.TeleportPlayerBack(controller);
						return false;
					}
					<>2__current = (object)new WaitForSeconds(0.2f);
					<>1__state = 2;
					return true;
				case 2:
				{
					<>1__state = -1;
					if ((Object)(object)controller.rb != (Object)null)
					{
						<wasKinematic>5__1 = controller.rb.isKinematic;
						controller.rb.isKinematic = false;
						controller.rb.velocity = Vector3.zero;
						controller.rb.angularVelocity = Vector3.zero;
						controller.rb.isKinematic = <wasKinematic>5__1;
					}
					controller.OverrideSpeed(1f, 0.1f);
					controller.OverrideTimeScale(1f, 0.1f);
					PhotonView? photonView = <>4__this.photonView;
					if (photonView != null)
					{
						photonView.RPC("OnTeleportCompleteRPC", (RpcTarget)0, Array.Empty<object>());
					}
					<>2__current = (object)new WaitForSeconds(0.5f);
					<>1__state = 3;
					return true;
				}
				case 3:
					<>1__state = -1;
					if (!<>4__this.IsPositionInBounds(((Component)controller).transform.position))
					{
						<>4__this.TeleportPlayerBack(controller);
					}
					<>2__current = (object)new WaitForSeconds(5f);
					<>1__state = 4;
					return true;
				case 4:
					<>1__state = -1;
					if (!<>4__this.IsPositionInBounds(((Component)controller).transform.position))
					{
						Debug.LogWarning((object)("[EnderPearl] Player " + controller.playerName + " left room volume after teleport delay. Returning."));
						<>4__this.TeleportPlayerBack(controller);
					}
					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 const float TELEPORT_MEMORY_DURATION = 5f;

		private const float RETURN_TO_IDLE_TIMEOUT = 3f;

		private const float TELEPORT_COOLDOWN = 0.5f;

		private const float PLAYER_HEIGHT = 2f;

		private const float PLAYER_RADIUS = 0.5f;

		private const float CEILING_BUFFER = 0.2f;

		private const float LIGHT_LERP_SPEED = 5f;

		private const float MIN_LIGHT_INTENSITY = 0.01f;

		private static readonly LayerMask TELEPORT_MASK = LayerMask.op_Implicit(LayerMask.GetMask(new string[3] { "Default", "Environment", "Terrain" }));

		private static readonly LayerMask ROOM_VOLUME_MASK = LayerMask.op_Implicit(LayerMask.GetMask(new string[1] { "RoomVolume" }));

		private static readonly LayerMask CART_WHEELS_MASK = LayerMask.op_Implicit(LayerMask.GetMask(new string[1] { "CartWheels" }));

		[Header("Core Components")]
		[SerializeField]
		private PhysGrabObject? physGrabObject;

		[SerializeField]
		private PhotonView? photonView;

		[SerializeField]
		private Rigidbody? rb;

		[SerializeField]
		private RoomVolumeCheck? roomVolumeCheck;

		[Header("Visual Effects")]
		[SerializeField]
		private Transform? particleSystemTransform;

		[SerializeField]
		private ParticleSystem? particleSystemGlitter;

		[SerializeField]
		private ParticleSystem? teleportSuccessParticles;

		[SerializeField]
		private MeshRenderer? pearlRenderer;

		[Header("Light Settings")]
		[SerializeField]
		private Light? lightWhenHeld;

		[SerializeField]
		private float lightIntensityActive = 4f;

		[SerializeField]
		private Color lightColorActive = new Color(0.5f, 0f, 0.5f);

		[SerializeField]
		private Color lightColorIdle = new Color(0f, 1f, 0f);

		[Header("Sound Effects")]
		[SerializeField]
		private Sound? impactSound;

		private PhysGrabber? lastHolder;

		private float lastHoldTime;

		private float lastTeleportTime;

		private float releaseTime = -1f;

		private bool isTeleporting;

		private bool stateStart;

		private Vector3 impactPosition;

		private Vector3 initialTeleportPosition;

		internal States currentState;

		private List<GameObject> debugCapsules = new List<GameObject>();

		private Color[] debugColors = (Color[])(object)new Color[6]
		{
			new Color(1f, 0f, 0f, 0.5f),
			new Color(0f, 1f, 0f, 0.5f),
			new Color(0f, 0f, 1f, 0.5f),
			new Color(1f, 1f, 0f, 0.5f),
			new Color(1f, 0f, 1f, 0.5f),
			new Color(0f, 1f, 1f, 0.5f)
		};

		private int currentColorIndex = 0;

		public static EnderPearl? Instance { get; private set; }

		private void Awake()
		{
			Instance = this;
			InitializeComponents();
			InitializeState();
		}

		private void Update()
		{
			if (ValidateComponents())
			{
				HandleGrabState();
				HandleReleaseTimeout();
				UpdateState();
			}
		}

		private void OnCollisionEnter(Collision collision)
		{
			//IL_0065: 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_006a: Unknown result type (might be due to invalid IL or missing references)
			if (collision.gameObject.layer != LayerMask.NameToLayer("CartWheels") && !((Object)(object)collision.gameObject.GetComponent<PhysGrabCart>() != (Object)null) && ShouldHandleCollision())
			{
				impactPosition = ((collision.contacts.Length != 0) ? ((ContactPoint)(ref collision.contacts[0])).point : collision.transform.position);
				HandleImpact();
			}
		}

		private void OnDestroy()
		{
			CleanupDebugObjects();
		}

		private void InitializeComponents()
		{
			if (physGrabObject == null)
			{
				physGrabObject = ((Component)this).GetComponent<PhysGrabObject>();
			}
			if (photonView == null)
			{
				photonView = ((Component)this).GetComponent<PhotonView>();
			}
			if (rb == null)
			{
				rb = ((Component)this).GetComponent<Rigidbody>();
			}
			if (roomVolumeCheck == null)
			{
				roomVolumeCheck = ((Component)this).GetComponent<RoomVolumeCheck>();
			}
			if (ValidateComponents())
			{
				if (particleSystemTransform == null)
				{
					particleSystemTransform = ((Component)this).transform.Find("ParticleSystem");
				}
				if (particleSystemGlitter == null)
				{
					particleSystemGlitter = ((Component)this).GetComponentInChildren<ParticleSystem>();
				}
				if (teleportSuccessParticles == null)
				{
					Transform obj = ((Component)this).transform.Find("TeleportParticles");
					teleportSuccessParticles = ((obj != null) ? ((Component)obj).GetComponent<ParticleSystem>() : null);
				}
				if (pearlRenderer == null)
				{
					pearlRenderer = ((Component)this).GetComponent<MeshRenderer>();
				}
				if (lightWhenHeld == null)
				{
					lightWhenHeld = ((Component)this).GetComponentInChildren<Light>();
				}
				if (impactSound == null)
				{
					impactSound = ((Component)this).GetComponent<Sound>();
				}
			}
		}

		private bool ValidateComponents()
		{
			if (!Object.op_Implicit((Object)(object)physGrabObject) || !Object.op_Implicit((Object)(object)photonView) || !Object.op_Implicit((Object)(object)rb) || !Object.op_Implicit((Object)(object)roomVolumeCheck))
			{
				Debug.LogError((object)"[EnderPearl] Critical component missing.");
				((Behaviour)this).enabled = false;
				return false;
			}
			return true;
		}

		private void InitializeState()
		{
			currentState = States.Idle;
			stateStart = true;
			lastHoldTime = -5f;
			StopAllParticleSystems();
		}

		private void HandleGrabState()
		{
			PhysGrabObject? obj = physGrabObject;
			if (obj != null && obj.grabbed)
			{
				List<PhysGrabber> playerGrabbing = physGrabObject.playerGrabbing;
				if (playerGrabbing == null || playerGrabbing.Count <= 0)
				{
					return;
				}
				PhysGrabber val = playerGrabbing[0];
				lastHoldTime = Time.time;
				releaseTime = -1f;
				if (!((Object)(object)val != (Object)(object)lastHolder))
				{
					return;
				}
				lastHolder = val;
				UpdateLastHolder(val);
				if ((Object)(object)photonView != (Object)null && !photonView.IsMine)
				{
					PlayerAvatar playerAvatar = val.playerAvatar;
					if (playerAvatar != null)
					{
						PhotonView obj2 = playerAvatar.photonView;
						if (((obj2 != null) ? new bool?(obj2.IsMine) : null).GetValueOrDefault())
						{
							photonView.RequestOwnership();
						}
					}
				}
				PlayerAvatar playerAvatar2 = val.playerAvatar;
				if (playerAvatar2 != null)
				{
					PhotonView obj3 = playerAvatar2.photonView;
					if (((obj3 != null) ? new bool?(obj3.IsMine) : null).GetValueOrDefault())
					{
						SetState(States.Active);
					}
				}
			}
			else if (currentState == States.Active && releaseTime < 0f)
			{
				releaseTime = Time.time;
			}
		}

		private void HandleReleaseTimeout()
		{
			if (releaseTime > 0f && Time.time - releaseTime >= 3f && !isTeleporting)
			{
				SetState(States.Idle);
			}
		}

		private void UpdateState()
		{
			switch (currentState)
			{
			case States.Active:
				StateActive();
				break;
			case States.Idle:
				StateIdle();
				break;
			case States.Used:
				StateUsed();
				break;
			}
		}

		private void SetState(States state)
		{
			PhotonView? obj = photonView;
			if (obj != null && obj.IsMine)
			{
				photonView.RPC("SetStateRPC", (RpcTarget)0, new object[1] { state });
			}
		}

		[PunRPC]
		private void SetStateRPC(States state)
		{
			currentState = state;
			stateStart = true;
		}

		private void StateActive()
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			if (stateStart)
			{
				ParticleSystem? obj = particleSystemGlitter;
				if (obj != null)
				{
					obj.Clear();
				}
				ParticleSystem? obj2 = particleSystemGlitter;
				if (obj2 != null)
				{
					obj2.Play();
				}
				ParticleSystem? obj3 = teleportSuccessParticles;
				if (obj3 != null)
				{
					obj3.Stop();
				}
				Light? obj4 = lightWhenHeld;
				if (obj4 != null)
				{
					((Component)obj4).gameObject.SetActive(true);
				}
				stateStart = false;
			}
			UpdateLightIntensity(lightIntensityActive);
			UpdatePearlColor(lightColorActive);
		}

		private void StateIdle()
		{
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			if (stateStart)
			{
				StopAllParticleSystems();
				stateStart = false;
			}
			PhysGrabObject? obj = physGrabObject;
			if (obj != null && obj.grabbed)
			{
				PhotonView? obj2 = photonView;
				if (obj2 != null && obj2.IsMine)
				{
					SetState(States.Active);
				}
			}
			UpdateLightIntensity(0f);
			UpdatePearlColor(lightColorIdle);
		}

		private void StateUsed()
		{
			StateIdle();
		}

		private void UpdateLightIntensity(float targetIntensity)
		{
			if (!((Object)(object)lightWhenHeld == (Object)null))
			{
				lightWhenHeld.intensity = Mathf.Lerp(lightWhenHeld.intensity, targetIntensity, Time.deltaTime * 5f);
				if (targetIntensity == 0f && lightWhenHeld.intensity < 0.01f)
				{
					((Component)lightWhenHeld).gameObject.SetActive(false);
				}
			}
		}

		private void UpdatePearlColor(Color color)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			MeshRenderer? obj = pearlRenderer;
			if (obj != null)
			{
				Material material = ((Renderer)obj).material;
				if (material != null)
				{
					Light? obj2 = lightWhenHeld;
					material.SetColor("_EmissionColor", color * ((obj2 != null) ? obj2.intensity : 0f));
				}
			}
		}

		private bool ShouldHandleCollision()
		{
			//IL_0007: 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_0018: Unknown result type (might be due to invalid IL or missing references)
			RaycastHit val = default(RaycastHit);
			int result;
			if (!Physics.Raycast(((Component)this).transform.position, Vector3.down, ref val, 0.1f, LayerMask.op_Implicit(CART_WHEELS_MASK)))
			{
				Collider collider = ((RaycastHit)(ref val)).collider;
				if (!((Object)(object)((collider != null) ? ((Component)collider).GetComponent<PhysGrabCart>() : null) != (Object)null))
				{
					PhotonView? obj = photonView;
					if (obj != null && obj.IsMine && !isTeleporting)
					{
						PhysGrabObject? obj2 = physGrabObject;
						if (obj2 != null && !obj2.grabbed && Time.time - lastHoldTime <= 5f)
						{
							result = ((currentState == States.Active) ? 1 : 0);
							goto IL_00a0;
						}
					}
					result = 0;
					goto IL_00a0;
				}
			}
			return false;
			IL_00a0:
			return (byte)result != 0;
		}

		private void HandleImpact()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			ParticleSystem? obj = particleSystemGlitter;
			if (obj != null)
			{
				obj.Stop();
			}
			Sound? obj2 = impactSound;
			if (obj2 != null)
			{
				obj2.Play(((Component)this).transform.position, 1f, 1f, 1f, 1f);
			}
			TryTeleportLastHolder();
			SetTeleportingState(teleporting: true);
			lastTeleportTime = Time.time;
			SetState(States.Idle);
		}

		private void TryTeleportLastHolder()
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: 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_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)lastHolder == (Object)null || Time.time - lastHoldTime > 5f)
			{
				Debug.LogWarning((object)"[EnderPearl] No last holder or memory duration expired");
			}
			else
			{
				if (!FindSafePositionAroundImpact(impactPosition, out var safePosition) || !safePosition.IsFinite())
				{
					return;
				}
				PhotonView val = lastHolder.playerAvatar?.photonView;
				if ((Object)(object)val != (Object)null)
				{
					PhotonView? obj = photonView;
					if (obj != null)
					{
						obj.RPC("ExecuteTeleportRPC", (RpcTarget)0, new object[4] { val.ViewID, safePosition.x, safePosition.y, safePosition.z });
					}
					ParticleSystem? obj2 = teleportSuccessParticles;
					if (obj2 != null)
					{
						obj2.Clear();
					}
					ParticleSystem? obj3 = teleportSuccessParticles;
					if (obj3 != null)
					{
						obj3.Play();
					}
				}
			}
		}

		[PunRPC]
		private void ExecuteTeleportRPC(int playerViewID, float x, float y, float z)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			PhotonView val = PhotonView.Find(playerViewID);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Vector3 val2 = default(Vector3);
			((Vector3)(ref val2))..ctor(x, y, z);
			if (val2.IsFinite())
			{
				SetTeleportingState(teleporting: true);
				if (val.IsMine)
				{
					TeleportPlayer(val, val2);
				}
			}
		}

		private void TeleportPlayer(PhotonView playerView, Vector3 targetPos)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			PlayerAvatar component = ((Component)playerView).GetComponent<PlayerAvatar>();
			PlayerController instance = PlayerController.instance;
			if (!((Object)(object)component == (Object)null) && !((Object)(object)instance?.rb == (Object)null))
			{
				initialTeleportPosition = ((Component)instance).transform.position;
				PreparePlayerForTeleport(instance);
				ExecuteTeleport(instance, targetPos);
				((MonoBehaviour)this).StartCoroutine(VerifyAndResetTeleport(instance, targetPos));
				ParticleSystem? obj = teleportSuccessParticles;
				if (obj != null)
				{
					obj.Clear();
				}
				ParticleSystem? obj2 = teleportSuccessParticles;
				if (obj2 != null)
				{
					obj2.Play();
				}
			}
		}

		private void PreparePlayerForTeleport(PlayerController controller)
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			controller.InputDisable(0.5f);
			controller.OverrideSpeed(0f, 0.5f);
			controller.OverrideTimeScale(0f, 0.5f);
			bool isKinematic = controller.rb.isKinematic;
			controller.rb.isKinematic = false;
			controller.rb.velocity = Vector3.zero;
			controller.rb.angularVelocity = Vector3.zero;
			controller.rb.isKinematic = isKinematic;
		}

		private void ExecuteTeleport(PlayerController controller, Vector3 targetPos)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			((Component)controller).transform.position = targetPos;
		}

		[IteratorStateMachine(typeof(<VerifyAndResetTeleport>d__65))]
		private IEnumerator VerifyAndResetTeleport(PlayerController controller, Vector3 targetPos)
		{
			//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 <VerifyAndResetTeleport>d__65(0)
			{
				<>4__this = this,
				controller = controller,
				targetPos = targetPos
			};
		}

		private void TeleportPlayerBack(PlayerController controller)
		{
			//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_0020: 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_0032: 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_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: 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)
			if (!((Object)(object)controller == (Object)null))
			{
				Vector3 val = Vector3.zero + Vector3.up;
				Vector3 val2 = (initialTeleportPosition.IsFinite() ? initialTeleportPosition : val);
				if (!IsTeleportPositionSafe(val2))
				{
					Debug.LogWarning((object)"[EnderPearl] Initial teleport return location invalid. Using fallback position.");
					val2 = val;
				}
				PreparePlayerForTeleport(controller);
				ExecuteTeleport(controller, val2);
				((MonoBehaviour)this).StartCoroutine(VerifyAndResetTeleport(controller, val2));
				ParticleSystem? obj = teleportSuccessParticles;
				if (obj != null)
				{
					obj.Clear();
				}
				ParticleSystem? obj2 = teleportSuccessParticles;
				if (obj2 != null)
				{
					obj2.Play();
				}
			}
		}

		[PunRPC]
		private void OnTeleportCompleteRPC()
		{
			SetTeleportingState(teleporting: false);
		}

		private void SetTeleportingState(bool teleporting)
		{
			isTeleporting = teleporting;
			PhotonView? obj = photonView;
			if (obj != null)
			{
				obj.RPC("SetTeleportingStateRPC", (RpcTarget)0, new object[1] { teleporting });
			}
		}

		[PunRPC]
		private void SetTeleportingStateRPC(bool state)
		{
			isTeleporting = state;
		}

		private bool IsTeleportPositionSafe(Vector3 basePosition)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: 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_0035: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: 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_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			for (int i = -5; i <= 5; i++)
			{
				Vector3 val = basePosition + Vector3.up * ((float)i * 0.1f);
				Vector3 val2 = val;
				Vector3 val3 = val + Vector3.up * 2f;
				ShowDebugCapsule(val2);
				Collider[] array = Physics.OverlapCapsule(val2, val3, 0.5f, LayerMask.op_Implicit(TELEPORT_MASK), (QueryTriggerInteraction)1);
				if (array.Length == 0 && !Physics.Raycast(val3, Vector3.up, 0.2f, LayerMask.op_Implicit(TELEPORT_MASK), (QueryTriggerInteraction)1))
				{
					Vector3 val4 = val3;
					Collider[] array2 = Physics.OverlapSphere(val4, 0.5f, LayerMask.op_Implicit(ROOM_VOLUME_MASK), (QueryTriggerInteraction)2);
					if (array2.Length != 0)
					{
						return true;
					}
				}
			}
			return false;
		}

		private bool FindSafePositionAroundImpact(Vector3 impactPos, out Vector3 safePosition)
		{
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: 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_0028: 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_002f: 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_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: 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_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: 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_010d: Unknown result type (might be due to invalid IL or missing references)
			for (int i = 0; i <= 6; i++)
			{
				float num = 0.5f * (float)i;
				for (int j = 0; j < 36; j++)
				{
					Vector3 onUnitSphere = Random.onUnitSphere;
					Vector3 val = impactPos + onUnitSphere * num;
					Vector3 val2 = val;
					Vector3 val3 = val + Vector3.up * 2f;
					Collider[] array = Physics.OverlapCapsule(val2, val3, 0.5f);
					bool flag = false;
					Collider[] array2 = array;
					foreach (Collider val4 in array2)
					{
						if (!val4.isTrigger)
						{
							if ((Object)(object)val4.attachedRigidbody != (Object)null)
							{
								flag = true;
								break;
							}
							if (((1 << ((Component)val4).gameObject.layer) & LayerMask.op_Implicit(ROOM_VOLUME_MASK)) == 0)
							{
								flag = !((Component)val4).CompareTag("Wall") || true;
								break;
							}
						}
					}
					if (!flag && IsInRoomVolume(val))
					{
						safePosition = val;
						return true;
					}
				}
			}
			safePosition = default(Vector3);
			return false;
		}

		private bool IsPositionInBounds(Vector3 position)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)roomVolumeCheck == (Object)null)
			{
				return true;
			}
			return IsInRoomVolume(position);
		}

		private bool IsInRoomVolume(Vector3 position)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: 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_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: 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_0092: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)roomVolumeCheck == (Object)null)
			{
				return true;
			}
			Vector3 val = position + Vector3.up * 2f;
			Collider[] array = Physics.OverlapCapsule(position, val, 0.5f, LayerMask.op_Implicit(ROOM_VOLUME_MASK), (QueryTriggerInteraction)2);
			if (array.Length == 0)
			{
				return false;
			}
			Collider[] array2 = array;
			foreach (Collider val2 in array2)
			{
				RoomVolume val3 = ((Component)val2).GetComponent<RoomVolume>() ?? ((Component)val2).GetComponentInParent<RoomVolume>();
				if (!((Object)(object)val3 == (Object)null) && IsCapsuleFullyInsideCollider(val2, position, val, 0.5f))
				{
					return true;
				}
			}
			return false;
		}

		private bool IsCapsuleFullyInsideCollider(Collider collider, Vector3 bottom, Vector3 top, float radius)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: 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_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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)
			for (int i = 0; i <= 5; i++)
			{
				float num = (float)i / 5f;
				Vector3 val = Vector3.Lerp(bottom, top, num);
				Vector3 val2 = ((Component)collider).transform.InverseTransformPoint(val);
				Bounds bounds = collider.bounds;
				if (!((Bounds)(ref bounds)).Contains(val))
				{
					return false;
				}
			}
			return true;
		}

		private void UpdateLastHolder(PhysGrabber newHolder)
		{
			PhotonView? obj = photonView;
			if (obj == null || !obj.IsMine)
			{
				return;
			}
			int? obj2;
			if (newHolder == null)
			{
				obj2 = null;
			}
			else
			{
				PlayerAvatar playerAvatar = newHolder.playerAvatar;
				if (playerAvatar == null)
				{
					obj2 = null;
				}
				else
				{
					PhotonView obj3 = playerAvatar.photonView;
					obj2 = ((obj3 != null) ? new int?(obj3.ViewID) : null);
				}
			}
			int? num = obj2;
			int valueOrDefault = num.GetValueOrDefault();
			photonView.RPC("SetLastHolderRPC", (RpcTarget)0, new object[1] { valueOrDefault });
		}

		[PunRPC]
		private void SetLastHolderRPC(int viewID)
		{
			object obj2;
			if (viewID != 0)
			{
				PhotonView obj = PhotonView.Find(viewID);
				obj2 = ((obj != null) ? ((Component)obj).GetComponent<PhysGrabber>() : null);
			}
			else
			{
				obj2 = null;
			}
			lastHolder = (PhysGrabber?)obj2;
		}

		private void StopAllParticleSystems()
		{
			ParticleSystem? obj = particleSystemGlitter;
			if (obj != null)
			{
				obj.Stop();
			}
			ParticleSystem? obj2 = teleportSuccessParticles;
			if (obj2 != null)
			{
				obj2.Stop();
			}
		}

		private void ShowDebugCapsule(Vector3 position)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			GameObject item = CreateDebugCapsule(position);
			debugCapsules.Add(item);
			currentColorIndex = (currentColorIndex + 1) % debugColors.Length;
		}

		private GameObject CreateDebugCapsule(Vector3 position)
		{
			//IL_003e: 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_0050: 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)
			//IL_005f: 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)
			GameObject val = GameObject.CreatePrimitive((PrimitiveType)1);
			((Object)val).name = $"TeleportDebugCapsule_{debugCapsules.Count}";
			val.transform.localScale = new Vector3(1f, 1f, 1f);
			val.transform.position = position + Vector3.up * 1f;
			Material material = CreateTransparentMaterial(debugColors[currentColorIndex]);
			((Renderer)val.GetComponent<MeshRenderer>()).material = material;
			Object.Destroy((Object)(object)val.GetComponent<Collider>());
			return val;
		}

		private Material CreateTransparentMaterial(Color color)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: 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_0019: Expected O, but got Unknown
			Material val = new Material(Shader.Find("Standard"))
			{
				color = color
			};
			val.SetFloat("_Mode", 3f);
			val.SetInt("_SrcBlend", 5);
			val.SetInt("_DstBlend", 10);
			val.SetInt("_ZWrite", 0);
			val.DisableKeyword("_ALPHATEST_ON");
			val.EnableKeyword("_ALPHABLEND_ON");
			val.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			val.renderQueue = 3000;
			return val;
		}

		private void CleanupDebugObjects()
		{
		}
	}
	[BepInPlugin("CarsonJF.EnderPearlMod", "EnderPearlMod", "2.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class EnderPearlMod : BaseUnityPlugin
	{
		private static readonly string BundleName = GetModName();

		private AssetBundle? _assetBundle;

		private bool _hasFixedAudioMixerGroups = false;

		internal static EnderPearlMod Instance { get; private set; } = null;


		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private static string GetModName()
		{
			object obj = typeof(EnderPearlMod).GetCustomAttributes(typeof(BepInPlugin), inherit: false)[0];
			BepInPlugin val = (BepInPlugin)((obj is BepInPlugin) ? obj : null);
			return ((val != null) ? val.Name : null) ?? "EnderPearlMod";
		}

		private void LoadAssetBundle()
		{
			if (!((Object)(object)_assetBundle != (Object)null))
			{
				string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
				string text = Path.Combine(directoryName, BundleName + ".bundle");
				_assetBundle = AssetBundle.LoadFromFile(text);
				if ((Object)(object)_assetBundle == (Object)null)
				{
					Logger.LogError((object)("Failed to load bundle from " + text));
				}
			}
		}

		private void LoadValuablesFromResources()
		{
			if ((Object)(object)_assetBundle == (Object)null)
			{
				return;
			}
			List<GameObject> list = (from name in _assetBundle.GetAllAssetNames()
				where name.Contains("/valuables/") && name.EndsWith(".prefab")
				select _assetBundle.LoadAsset<GameObject>(name)).ToList();
			foreach (GameObject item in list)
			{
				if (((Object)item).name.Contains("enderpearl", StringComparison.OrdinalIgnoreCase))
				{
					if (!Object.op_Implicit((Object)(object)item.GetComponent<PhysGrabObject>()))
					{
						Logger.LogInfo((object)"Adding PhysGrabObject component to EnderPearl prefab");
						item.AddComponent<PhysGrabObject>();
					}
					if (!Object.op_Implicit((Object)(object)item.GetComponent<PhotonView>()))
					{
						Logger.LogInfo((object)"Adding PhotonView component to EnderPearl prefab");
						item.AddComponent<PhotonView>();
					}
					if (!Object.op_Implicit((Object)(object)item.GetComponent<EnderPearl>()))
					{
						Logger.LogInfo((object)"Adding EnderPearl component to prefab");
						item.AddComponent<EnderPearl>();
					}
				}
				Valuables.RegisterValuable(item);
			}
			if (list.Count > 0)
			{
				Logger.LogInfo((object)$"Successfully registered {list.Count} valuables through REPOLib");
			}
		}

		private void LoadItemsFromResources()
		{
			if ((Object)(object)_assetBundle == (Object)null)
			{
				return;
			}
			List<GameObject> list = (from name in _assetBundle.GetAllAssetNames()
				where name.Contains("/items/") && name.EndsWith(".prefab")
				select _assetBundle.LoadAsset<GameObject>(name)).ToList();
			foreach (GameObject item in list)
			{
				Item component = item.GetComponent<Item>();
				if ((Object)(object)component != (Object)null)
				{
					Items.RegisterItem(component);
				}
			}
			if (list.Count > 0)
			{
				Logger.LogInfo((object)$"Successfully registered {list.Count} items through REPOLib");
			}
		}

		private void LoadEnemiesFromResources()
		{
			if ((Object)(object)_assetBundle == (Object)null)
			{
				return;
			}
			List<GameObject> list = (from name in _assetBundle.GetAllAssetNames()
				where name.Contains("/enemies/") && name.EndsWith(".prefab")
				select _assetBundle.LoadAsset<GameObject>(name)).ToList();
			foreach (GameObject item in list)
			{
				EnemySetup component = item.GetComponent<EnemySetup>();
				if ((Object)(object)component != (Object)null)
				{
					Enemies.RegisterEnemy(component);
				}
				else
				{
					Logger.LogWarning((object)("Prefab " + ((Object)item).name + " does not contain an EnemySetup component"));
				}
			}
			if (list.Count > 0)
			{
				Logger.LogInfo((object)$"Successfully registered {list.Count} enemies through REPOLib");
			}
		}

		private void FixAllPrefabAudioMixerGroups()
		{
			if ((Object)(object)_assetBundle == (Object)null)
			{
				return;
			}
			List<GameObject> list = (from name in _assetBundle.GetAllAssetNames()
				where name.EndsWith(".prefab")
				select _assetBundle.LoadAsset<GameObject>(name)).ToList();
			foreach (GameObject item in list)
			{
				Utilities.FixAudioMixerGroups(item);
			}
		}

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			Patch();
			LoadAssetBundle();
			LoadValuablesFromResources();
			LoadItemsFromResources();
			LoadEnemiesFromResources();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
			Logger.LogInfo((object)"Got feedback? Let me know @CarsonJF on Discord or @SirCarsonJF on Twitter!");
		}

		internal void Patch()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_0026: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
			if (!_hasFixedAudioMixerGroups)
			{
				FixAllPrefabAudioMixerGroups();
				_hasFixedAudioMixerGroups = true;
			}
		}
	}
}