Decompiled source of VortexCannon v1.0.0

Mods/VortexCannon.dll

Decompiled 6 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BoneLib.BoneMenu;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Interaction;
using Il2CppSystem.Collections.Generic;
using LabFusion.Data;
using LabFusion.Entities;
using LabFusion.Network;
using LabFusion.Player;
using LabFusion.SDK.Metadata;
using LabFusion.Scene;
using LabFusion.UI.Popups;
using LabFusion.Utilities;
using MelonLoader;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.XR;
using VortexCannon;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(VortexMod), "Vortex Cannon", "1.0.0", "nighthippo", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: MelonAdditionalDependencies(new string[] { "LabFusion", "BoneLib" })]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("VortexCannon")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("VortexCannon")]
[assembly: AssemblyTitle("VortexCannon")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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 VortexCannon
{
	internal sealed class PropField
	{
		private sealed record Prop(NetworkEntity Entity, Rigidbody Body, Il2CppArrayBase<Grip> Grips);

		private const float Radius = 5.5f;

		private const int MaximumBodies = 20;

		private const double RefreshSeconds = 0.14;

		private readonly List<Prop> _nearby = new List<Prop>(20);

		private double _nextRefresh;

		public void Pull(Vector3 center, double now)
		{
			//IL_001b: 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_0046: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			if (now >= _nextRefresh)
			{
				_nextRefresh = now + 0.14;
				FindProps(center);
			}
			foreach (Prop item in _nearby)
			{
				if (CanMove(item, center))
				{
					item.Body.AddForce(PullAcceleration(center - item.Body.worldCenterOfMass, item.Body.velocity), (ForceMode)5);
				}
			}
		}

		public void Launch(Vector3 center, Vector3 direction)
		{
			//IL_0017: 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_002c: 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)
			foreach (Prop item in _nearby)
			{
				if (CanMove(item, center))
				{
					item.Body.AddForce(LaunchVelocity(center, item.Body.worldCenterOfMass, direction), (ForceMode)2);
				}
			}
			Reset();
		}

		public void Reset()
		{
			_nearby.Clear();
			_nextRefresh = 0.0;
		}

		private void FindProps(Vector3 center)
		{
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			_nearby.Clear();
			foreach (NetworkEntity value in NetworkEntityManager.IDManager.RegisteredEntities.IDEntityLookup.Values)
			{
				if (!OwnsProp(value))
				{
					continue;
				}
				NetworkProp extender = value.GetExtender<NetworkProp>();
				if (extender == null || extender.IsCulled || (Object)(object)extender.MarrowEntity == (Object)null)
				{
					continue;
				}
				MarrowEntity marrowEntity = extender.MarrowEntity;
				if (marrowEntity.IsDestroyed || marrowEntity.IsDespawned)
				{
					continue;
				}
				Il2CppReferenceArray<MarrowBody> bodies = marrowEntity.Bodies;
				if (bodies == null || ((Il2CppArrayBase<MarrowBody>)(object)bodies).Length != 1 || (Object)(object)((Il2CppArrayBase<MarrowBody>)(object)bodies)[0] == (Object)null)
				{
					continue;
				}
				Rigidbody rigidbody = ((Il2CppArrayBase<MarrowBody>)(object)bodies)[0]._rigidbody;
				if (!BodyInRange(rigidbody, center) || (Object)(object)((Component)marrowEntity).GetComponentInChildren<Seat>(true) != (Object)null || (Object)(object)((Component)rigidbody).GetComponent<Joint>() != (Object)null)
				{
					continue;
				}
				Il2CppArrayBase<Grip> componentsInChildren = ((Component)marrowEntity).GetComponentsInChildren<Grip>(true);
				if (componentsInChildren.Length == 0)
				{
					continue;
				}
				Prop prop = new Prop(value, rigidbody, componentsInChildren);
				if (CanMove(prop, center))
				{
					_nearby.Add(prop);
					if (_nearby.Count == 20)
					{
						break;
					}
				}
			}
		}

		private static bool OwnsProp(NetworkEntity entity)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)entity.Source == 2 && entity.IsRegistered && !entity.IsDestroyed && !entity.IsOwnerLocked)
			{
				return entity.IsOwner;
			}
			return false;
		}

		private static bool BodyInRange(Rigidbody body, Vector3 center)
		{
			//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_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)body != (Object)null && !body.isKinematic && body.mass <= 150f)
			{
				Vector3 val = body.worldCenterOfMass - center;
				return ((Vector3)(ref val)).sqrMagnitude <= 30.25f;
			}
			return false;
		}

		private static bool CanMove(Prop prop, Vector3 center)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			if (!OwnsProp(prop.Entity) || !BodyInRange(prop.Body, center))
			{
				return false;
			}
			foreach (Grip grip in prop.Grips)
			{
				if ((Object)(object)grip != (Object)null)
				{
					List<Hand> attachedHands = grip.attachedHands;
					if (attachedHands != null && attachedHands.Count > 0)
					{
						return false;
					}
				}
			}
			return true;
		}

		internal static Vector3 PullAcceleration(Vector3 inward, Vector3 velocity)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: 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_000b: 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_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: 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_0048: 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_0053: 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_005d: 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_0076: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = Vector3.Cross(Vector3.up, inward);
			Vector3 normalized = ((Vector3)(ref val)).normalized;
			float num = Mathf.Min(((Vector3)(ref inward)).magnitude * 18f, 12f);
			return Vector3.ClampMagnitude(Vector3.ClampMagnitude(inward * 20f, 62f) + normalized * num - velocity * 4.4f + Vector3.up * 9.81f, 68f);
		}

		internal static Vector3 LaunchVelocity(Vector3 center, Vector3 position, Vector3 direction)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: 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_0012: 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_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: 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_0037: 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)
			Vector3 val = Vector3.ClampMagnitude(position - center, 2.5f);
			return direction * 15f + Vector3.up * 3.5f + val * 1.7f;
		}
	}
	internal sealed class VortexAudio : IDisposable
	{
		private enum Phase
		{
			None,
			Charge,
			Release
		}

		private const int SampleRate = 24000;

		private readonly AudioSource _source;

		private readonly AudioClip _hum;

		private readonly AudioClip _burst;

		private Phase _phase;

		public VortexAudio(GameObject root)
		{
			_source = root.AddComponent<AudioSource>();
			_source.playOnAwake = false;
			_source.spatialBlend = 1f;
			_source.dopplerLevel = 0f;
			_source.minDistance = 1.6f;
			_source.maxDistance = 35f;
			_source.volume = 0.68f;
			_hum = MakeClip("Vortex charge", 1f, (float time) => (0.72f + 0.28f * Wave(5f * time)) * (0.24f * Wave(66f * time) + 0.13f * Wave(99f * time) + 0.08f * Wave(165f * time)));
			Random noise = new Random(73461);
			float filtered = 0f;
			_burst = MakeClip("Vortex discharge", 0.64f, delegate(float time)
			{
				filtered = 0.89f * filtered + 0.11f * ((float)noise.NextDouble() * 2f - 1f);
				float cycles = 49f * time + 11f * (1f - Mathf.Exp(-11f * time));
				return Mathf.Min(time * 190f, 1f) * Mathf.Exp(-7f * time) * (0.52f * Wave(cycles) + 0.75f * filtered);
			});
		}

		public void Charge(float age)
		{
			if (!((Object)(object)_source == (Object)null))
			{
				if (_phase != Phase.Charge)
				{
					_phase = Phase.Charge;
					_source.clip = _hum;
					_source.loop = true;
					_source.Play();
				}
				_source.pitch = Mathf.Min(1.52f, 0.82f + age * 0.23f);
			}
		}

		public void Release()
		{
			if (!((Object)(object)_source == (Object)null) && _phase != Phase.Release)
			{
				_phase = Phase.Release;
				_source.Stop();
				_source.loop = false;
				_source.pitch = 1f;
				_source.clip = _burst;
				_source.Play();
			}
		}

		public void Reset()
		{
			_phase = Phase.None;
			if ((Object)(object)_source != (Object)null)
			{
				_source.Stop();
			}
		}

		private static AudioClip MakeClip(string name, float duration, Func<float, float> sample)
		{
			float[] array = new float[(int)(24000f * duration)];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = sample((float)i / 24000f);
			}
			AudioClip obj = AudioClip.Create(name, array.Length, 1, 24000, false);
			obj.SetData(new Il2CppStructArray<float>(array), 0);
			return obj;
		}

		private static float Wave(float cycles)
		{
			return Mathf.Sin(cycles * 2f * (float)Math.PI);
		}

		public void Dispose()
		{
			Reset();
			Object.Destroy((Object)(object)_hum);
			Object.Destroy((Object)(object)_burst);
		}
	}
	internal readonly record struct VortexCast(Guid Id, bool Fired, Vector3 Center, Vector3 Direction, string Level)
	{
		public string Encode()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: 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_006d: 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_0093: Unknown result type (might be due to invalid IL or missing references)
			return FormattableString.Invariant(FormattableStringFactory.Create("{0:N}|{1}|{2:R}|{3:R}|{4:R}|{5:R}|{6:R}|{7:R}|{8}", Id, Fired ? "F" : "P", Center.x, Center.y, Center.z, Direction.x, Direction.y, Direction.z, Level));
		}

		public static bool TryDecode(string? value, out VortexCast cast)
		{
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			cast = default(VortexCast);
			if (string.IsNullOrEmpty(value) || value.Length > 512)
			{
				return false;
			}
			string[] array = value.Split('|', 9);
			Guid result = default(Guid);
			bool flag = array.Length != 9 || !Guid.TryParseExact(array[0], "N", out result) || result == Guid.Empty;
			if (!flag)
			{
				string text = array[1];
				bool flag2 = ((text == "P" || text == "F") ? true : false);
				flag = !flag2;
			}
			Vector3 vector = default(Vector3);
			Vector3 vector2 = default(Vector3);
			bool flag3 = flag || string.IsNullOrWhiteSpace(array[8]) || !ReadVector(array, 2, out vector) || !ReadVector(array, 5, out vector2) || ((Vector3)(ref vector)).sqrMagnitude > 1E+10f;
			if (!flag3)
			{
				float sqrMagnitude = ((Vector3)(ref vector2)).sqrMagnitude;
				bool flag2 = ((sqrMagnitude < 0.9f || sqrMagnitude > 1.1f) ? true : false);
				flag3 = flag2;
			}
			if (flag3)
			{
				return false;
			}
			cast = new VortexCast(result, array[1] == "F", vector, vector2, array[8]);
			return true;
		}

		private static bool ReadVector(string[] parts, int first, out Vector3 vector)
		{
			//IL_0001: 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)
			vector = default(Vector3);
			if (!ReadFloat(parts[first], out var value) || !ReadFloat(parts[first + 1], out var value2) || !ReadFloat(parts[first + 2], out var value3))
			{
				return false;
			}
			vector = new Vector3(value, value2, value3);
			return true;
		}

		private static bool ReadFloat(string text, out float value)
		{
			if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out value))
			{
				return float.IsFinite(value);
			}
			return false;
		}
	}
	internal sealed class VortexEffects : IDisposable
	{
		private static readonly Color Violet = new Color(0.43f, 0.23f, 1f);

		private static readonly Color Cyan = new Color(0.1f, 0.95f, 1f);

		private const int Samples = 34;

		private GameObject? _root;

		private Material? _material;

		private LineRenderer[]? _rings;

		private VortexAudio? _audio;

		private bool _shaderMissing;

		public void DrawCharge(Vector3 center, float age)
		{
			//IL_0014: 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)
			//IL_006d: 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_011c: Unknown result type (might be due to invalid IL or missing references)
			if (!EnsureRings())
			{
				return;
			}
			_root.transform.position = center;
			_root.SetActive(true);
			_audio.Charge(age);
			for (int i = 0; i < _rings.Length; i++)
			{
				LineRenderer val = _rings[i];
				val.startColor = new Color(0.32f, 0.24f, 0.9f, 0f);
				val.endColor = ((i == 1) ? Violet : Cyan);
				val.startWidth = 0.022f;
				val.endWidth = 0.095f;
				for (int j = 0; j < 34; j++)
				{
					float num = (float)j / 33f;
					float num2 = Mathf.Lerp(1.62f, 0.08f, num) * Mathf.Min(1f, 0.35f + age * 2.4f);
					float num3 = num * 7.2f - age * (5.7f + (float)i * 0.35f) + (float)i * 2.0944f;
					float num4 = (num - 0.5f) * ((i == 1) ? (-1.65f) : 1.65f);
					val.SetPosition(j, new Vector3(Mathf.Cos(num3) * num2, num4, Mathf.Sin(num3) * num2));
				}
			}
		}

		public void DrawRelease(Vector3 center, Vector3 direction, float age)
		{
			//IL_0014: 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_004b: 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_0050: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: 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)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: 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_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: 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_0146: 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_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			if (!EnsureRings())
			{
				return;
			}
			_root.transform.position = center;
			_root.SetActive(true);
			_audio.Release();
			Vector3 val = Vector3.Cross(direction, (Mathf.Abs(direction.y) > 0.9f) ? Vector3.right : Vector3.up);
			Vector3 normalized = ((Vector3)(ref val)).normalized;
			Vector3 val2 = Vector3.Cross(direction, normalized);
			float num = Mathf.Clamp01(1f - age / 0.7f);
			for (int i = 0; i < _rings.Length; i++)
			{
				LineRenderer val3 = _rings[i];
				Color val4 = ((i == 1) ? Violet : Cyan);
				val4.a = num * (1f - (float)i * 0.17f);
				val3.startColor = val4;
				val3.endColor = val4;
				val3.startWidth = 0.12f * num;
				val3.endWidth = val3.startWidth;
				float num2 = 0.26f + age * (7.4f - (float)i * 1.4f);
				Vector3 val5 = direction * (age * (9.5f - (float)i * 2.3f));
				for (int j = 0; j < 34; j++)
				{
					float num3 = (float)j / 33f * 2f * (float)Math.PI;
					val3.SetPosition(j, val5 + (normalized * Mathf.Cos(num3) + val2 * Mathf.Sin(num3)) * num2);
				}
			}
		}

		private bool EnsureRings()
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//IL_009f: 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)
			if ((Object)(object)_root != (Object)null)
			{
				return true;
			}
			if (_shaderMissing)
			{
				return false;
			}
			_audio?.Dispose();
			_audio = null;
			if ((Object)(object)_material == (Object)null)
			{
				Shader val = Shader.Find("Sprites/Default");
				if ((Object)(object)val == (Object)null)
				{
					_shaderMissing = true;
					return false;
				}
				_material = new Material(val);
			}
			_root = new GameObject("Vortex Cannon effect");
			_audio = new VortexAudio(_root);
			_rings = (LineRenderer[]?)(object)new LineRenderer[3];
			for (int i = 0; i < _rings.Length; i++)
			{
				GameObject val2 = new GameObject("Vortex ribbon");
				val2.transform.SetParent(_root.transform, false);
				LineRenderer val3 = val2.AddComponent<LineRenderer>();
				val3.useWorldSpace = false;
				((Renderer)val3).sharedMaterial = _material;
				val3.positionCount = 34;
				val3.numCornerVertices = 2;
				_rings[i] = val3;
			}
			return true;
		}

		public void Hide()
		{
			_audio?.Reset();
			if ((Object)(object)_root != (Object)null)
			{
				_root.SetActive(false);
			}
		}

		public void Dispose()
		{
			_audio?.Dispose();
			if ((Object)(object)_root != (Object)null)
			{
				Object.Destroy((Object)(object)_root);
			}
			if ((Object)(object)_material != (Object)null)
			{
				Object.Destroy((Object)(object)_material);
			}
		}
	}
	public sealed class VortexMod : MelonMod
	{
		private const string CastKey = "VortexCannon.v1.Cast";

		private const double ChargeLimit = 3.3;

		private const double MinimumCharge = 0.25;

		private const double ReleaseLifetime = 0.7;

		private static readonly Color Accent = new Color(0.43f, 0.34f, 1f);

		private readonly PropField _props = new PropField();

		private VortexEffects? _effects;

		private VortexCast? _hostCast;

		private VortexCast? _activeCast;

		private Guid _finishedCast;

		private string? _lastPayload;

		private double _castStarted;

		private double _hostDeadline;

		private double _releaseStarted;

		private double _hostReadyAt;

		private bool _armed;

		private bool _stickPressed;

		private bool _stickCasting;

		private bool _launchPending;

		public override void OnInitializeMelon()
		{
			//IL_000a: 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_003a: 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_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			Page obj = Page.Root.CreatePage("Vortex Cannon", Accent, 0, true);
			obj.CreateBool("Right stick hold + release", Color.white, false, (Action<bool>)SetControllerArmed);
			obj.CreateFunction("Create vortex", Accent, (Action)delegate
			{
				StartCast(fromStick: false);
			});
			obj.CreateFunction("Launch props", Color.cyan, (Action)FireCast);
			MultiplayerHooking.OnLoadingBegin += new UpdateEvent(OnLoading);
			MultiplayerHooking.OnDisconnected += new ServerEvent(OnDisconnected);
			LocalPlayer.OnApplyInitialMetadata += ClearLocalMetadata;
		}

		public override void OnDeinitializeMelon()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			MultiplayerHooking.OnLoadingBegin -= new UpdateEvent(OnLoading);
			MultiplayerHooking.OnDisconnected -= new ServerEvent(OnDisconnected);
			LocalPlayer.OnApplyInitialMetadata -= ClearLocalMetadata;
			ResetEffect();
			_effects?.Dispose();
		}

		private void SetControllerArmed(bool armed)
		{
			_armed = armed;
			_stickPressed = armed && IsStickPressed();
			if (!armed && _stickCasting)
			{
				FireCast();
			}
		}

		public override void OnUpdate()
		{
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			if (!NetworkInfo.HasServer || FusionSceneManager.IsLoading() || !RigData.HasPlayer)
			{
				return;
			}
			double realtimeSinceStartupAsDouble = Time.realtimeSinceStartupAsDouble;
			VortexCast? hostCast;
			if (NetworkInfo.IsHost)
			{
				HandleInput();
				hostCast = _hostCast;
				if (hostCast.HasValue && !hostCast.GetValueOrDefault().Fired && realtimeSinceStartupAsDouble >= _hostDeadline)
				{
					FireCast();
				}
				else
				{
					hostCast = _hostCast;
					if (hostCast.HasValue && hostCast.GetValueOrDefault().Fired && realtimeSinceStartupAsDouble >= _hostDeadline)
					{
						ClearPublished();
					}
				}
			}
			object obj;
			if (!NetworkInfo.IsHost)
			{
				PlayerID hostID = PlayerIDManager.GetHostID();
				obj = ((hostID != null) ? hostID.Metadata.Metadata : null);
			}
			else
			{
				obj = LocalPlayer.Metadata.Metadata;
			}
			string text = ((obj != null) ? ((NetworkMetadata)obj).GetMetadata("VortexCannon.v1.Cast") : null);
			if (text != _lastPayload)
			{
				_lastPayload = text;
				Observe(text, FusionSceneManager.Barcode, realtimeSinceStartupAsDouble);
			}
			hostCast = _activeCast;
			if (!hostCast.HasValue)
			{
				return;
			}
			VortexCast valueOrDefault = hostCast.GetValueOrDefault();
			if (valueOrDefault.Fired)
			{
				if (realtimeSinceStartupAsDouble - _releaseStarted >= 0.7)
				{
					ResetEffect();
				}
				else
				{
					GetEffects().DrawRelease(valueOrDefault.Center, valueOrDefault.Direction, (float)(realtimeSinceStartupAsDouble - _releaseStarted));
				}
			}
			else if (realtimeSinceStartupAsDouble - _castStarted > 4.0)
			{
				ResetEffect();
			}
			else
			{
				GetEffects().DrawCharge(valueOrDefault.Center, (float)(realtimeSinceStartupAsDouble - _castStarted));
			}
		}

		public override void OnFixedUpdate()
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			if (!NetworkInfo.HasServer || FusionSceneManager.IsLoading())
			{
				return;
			}
			VortexCast? activeCast = _activeCast;
			if (activeCast.HasValue)
			{
				VortexCast valueOrDefault = activeCast.GetValueOrDefault();
				if (_launchPending)
				{
					_launchPending = false;
					_props.Launch(valueOrDefault.Center, valueOrDefault.Direction);
				}
				else if (!valueOrDefault.Fired)
				{
					_props.Pull(valueOrDefault.Center, Time.realtimeSinceStartupAsDouble);
				}
			}
		}

		private void HandleInput()
		{
			if (!_armed)
			{
				return;
			}
			bool flag = IsStickPressed();
			if (flag != _stickPressed)
			{
				_stickPressed = flag;
				if (flag)
				{
					StartCast(fromStick: true);
				}
				else if (_stickCasting)
				{
					FireCast();
				}
			}
		}

		private static bool IsStickPressed()
		{
			//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)
			InputDevice deviceAtXRNode = InputDevices.GetDeviceAtXRNode((XRNode)5);
			bool flag = default(bool);
			return ((InputDevice)(ref deviceAtXRNode)).isValid && ((InputDevice)(ref deviceAtXRNode)).TryGetFeatureValue(CommonUsages.primary2DAxisClick, ref flag) && flag;
		}

		private void StartCast(bool fromStick)
		{
			//IL_004d: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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_005c: 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_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: 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)
			if (!CanCast(!fromStick) || _hostCast.HasValue)
			{
				return;
			}
			double realtimeSinceStartupAsDouble = Time.realtimeSinceStartupAsDouble;
			if (realtimeSinceStartupAsDouble < _hostReadyAt)
			{
				return;
			}
			Transform headset = RigData.Refs.Headset;
			string barcode = FusionSceneManager.Barcode;
			if (!((Object)(object)headset == (Object)null) && !string.IsNullOrEmpty(barcode))
			{
				Vector3 position = headset.position;
				Vector3 forward = headset.forward;
				Vector3 center = AimCenter(position, forward, ((Component)RigData.Refs.RigManager).transform);
				VortexCast cast = new VortexCast(Guid.NewGuid(), Fired: false, center, ((Vector3)(ref forward)).normalized, barcode);
				if (Publish(cast))
				{
					_hostDeadline = realtimeSinceStartupAsDouble + 3.3;
					_stickCasting = fromStick;
				}
			}
		}

		private static Vector3 AimCenter(Vector3 origin, Vector3 forward, Transform rig)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: 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_0021: 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_0067: 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_0083: Unknown result type (might be due to invalid IL or missing references)
			float num = 13f;
			float num2 = num;
			foreach (RaycastHit item in (Il2CppArrayBase<RaycastHit>)(object)Physics.RaycastAll(origin, forward, num, -5, (QueryTriggerInteraction)1))
			{
				RaycastHit current = item;
				if ((Object)(object)((RaycastHit)(ref current)).transform != (Object)null && !((RaycastHit)(ref current)).transform.IsChildOf(rig) && ((RaycastHit)(ref current)).distance < num2)
				{
					num2 = ((RaycastHit)(ref current)).distance;
				}
			}
			return origin + forward * Mathf.Clamp(num2 - 0.45f, 0.8f, 7f);
		}

		private void FireCast()
		{
			//IL_0080: 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_0089: 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_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			VortexCast? hostCast = _hostCast;
			if (!hostCast.HasValue)
			{
				return;
			}
			VortexCast valueOrDefault = hostCast.GetValueOrDefault();
			if (valueOrDefault.Fired || !CanCast(showMessage: false))
			{
				return;
			}
			double realtimeSinceStartupAsDouble = Time.realtimeSinceStartupAsDouble;
			if (realtimeSinceStartupAsDouble < _castStarted + 0.25)
			{
				_hostDeadline = _castStarted + 0.25;
				_stickCasting = false;
				return;
			}
			Transform headset = RigData.Refs.Headset;
			Vector3 val;
			if (!((Object)(object)headset != (Object)null))
			{
				val = valueOrDefault.Direction;
			}
			else
			{
				Vector3 forward = headset.forward;
				val = ((Vector3)(ref forward)).normalized;
			}
			Vector3 direction = val;
			if (Publish(valueOrDefault with
			{
				Fired = true,
				Direction = direction
			}))
			{
				_hostDeadline = realtimeSinceStartupAsDouble + 0.7;
				_hostReadyAt = realtimeSinceStartupAsDouble + 1.1;
				_stickCasting = false;
			}
		}

		private bool Publish(VortexCast cast)
		{
			string text = cast.Encode();
			if (!VortexCast.TryDecode(text, out var _) || !LocalPlayer.Metadata.Metadata.TrySetMetadata("VortexCannon.v1.Cast", text))
			{
				return false;
			}
			_hostCast = cast;
			_lastPayload = text;
			Observe(text, cast.Level, Time.realtimeSinceStartupAsDouble);
			return true;
		}

		private void Observe(string? payload, string level, double now)
		{
			if (!VortexCast.TryDecode(payload, out var cast) || cast.Level != level)
			{
				VortexCast? activeCast = _activeCast;
				if (activeCast.HasValue && !activeCast.GetValueOrDefault().Fired)
				{
					ResetEffect();
				}
			}
			else
			{
				if (cast.Id == _finishedCast)
				{
					return;
				}
				if (cast.Fired)
				{
					VortexCast? activeCast = _activeCast;
					if (activeCast.HasValue)
					{
						VortexCast valueOrDefault = activeCast.GetValueOrDefault();
						if (!valueOrDefault.Fired && !(valueOrDefault.Id != cast.Id))
						{
							_activeCast = cast;
							_releaseStarted = now;
							_launchPending = true;
							_finishedCast = cast.Id;
						}
					}
				}
				else if (_activeCast?.Id != cast.Id)
				{
					ResetEffect();
					_activeCast = cast;
					_castStarted = now;
				}
			}
		}

		private static bool CanCast(bool showMessage)
		{
			//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_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: 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_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			if (NetworkInfo.IsHost)
			{
				PlayerID localID = PlayerIDManager.LocalID;
				if (localID != null && localID.IsValid && RigData.HasPlayer && !FusionSceneManager.IsLoading())
				{
					return true;
				}
			}
			if (showMessage)
			{
				Notifier.Send(new Notification
				{
					Title = NotificationText.op_Implicit("Vortex Cannon"),
					Message = NotificationText.op_Implicit("Host a Fusion lobby and load into the map to cast."),
					SaveToMenu = false
				});
			}
			return false;
		}

		private VortexEffects GetEffects()
		{
			return _effects ?? (_effects = new VortexEffects());
		}

		private void ResetEffect()
		{
			_activeCast = null;
			_launchPending = false;
			_props.Reset();
			_effects?.Hide();
		}

		private void ClearPublished()
		{
			if (_hostCast.HasValue && NetworkInfo.IsHost)
			{
				LocalPlayer.Metadata.Metadata.TrySetMetadata("VortexCannon.v1.Cast", string.Empty);
			}
			_hostCast = null;
			_stickCasting = false;
		}

		private void OnLoading()
		{
			ClearPublished();
			ResetEffect();
			_effects?.Dispose();
			_effects = null;
		}

		private void OnDisconnected()
		{
			_hostCast = null;
			_hostReadyAt = 0.0;
			_stickPressed = false;
			_stickCasting = false;
			_lastPayload = null;
			_finishedCast = Guid.Empty;
			ClearLocalMetadata();
			ResetEffect();
		}

		private static void ClearLocalMetadata()
		{
			LocalPlayer.Metadata.Metadata.ForceRemoveLocalMetadata("VortexCannon.v1.Cast");
		}
	}
}