Decompiled source of Skywurst v0.1.4

Skywurst.dll

Decompiled 12 hours ago
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using Atlas;
using BepInEx;
using BepInEx.Logging;
using FistVR;
using UnityEngine;
using UnityEngine.Audio;

[assembly: Debuggable(DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
namespace quaternion.Skywurst
{
	[BepInPlugin("quaternion.Skywurst", "Skywurst", "0.1.3")]
	[BepInProcess("h3vr.exe")]
	[Description("Built with MeatKit")]
	[BepInDependency("nrgill28.Atlas", "1.0.1")]
	public class SkywurstPlugin : BaseUnityPlugin
	{
		private static readonly string BasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

		internal static ManualLogSource Logger;

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			LoadAssets();
		}

		private void LoadAssets()
		{
			AtlasPlugin.RegisterScene(Path.Combine(BasePath, "skywurst"));
		}
	}
}
namespace Skywurst
{
	[DisallowMultipleComponent]
	[RequireComponent(typeof(AudioSource))]
	[AddComponentMenu("Skywurst/Fountain ambience")]
	public sealed class SkywurstFountainAudio : MonoBehaviour
	{
		public const string MixerName = "Enviroment";

		public const string GroupName = "Environment Main Ambient";

		private AudioSource source;

		private AudioListener listener;

		private bool authoredMute;

		private bool routed;

		public bool IsRouted => (Object)(object)source != (Object)null && (Object)(object)source.outputAudioMixerGroup != (Object)null;

		private void Awake()
		{
			source = ((Component)this).GetComponent<AudioSource>();
			authoredMute = source.mute;
		}

		private void OnEnable()
		{
			((MonoBehaviour)this).StartCoroutine(KeepPlaying());
		}

		private IEnumerator KeepPlaying()
		{
			int attempts = 0;
			bool reported = false;
			while (true)
			{
				if ((Object)(object)source == (Object)null)
				{
					source = ((Component)this).GetComponent<AudioSource>();
				}
				routed = TryBind();
				if (!routed)
				{
					int num;
					attempts = (num = attempts + 1);
					if (num == 30)
					{
						Debug.LogWarning((object)"Skywurst fountain is waiting for H3VR's ambient mixer.", (Object)(object)this);
					}
				}
				else if (((Behaviour)source).enabled && (Object)(object)source.clip != (Object)null)
				{
					if ((int)source.clip.loadState == 0)
					{
						source.clip.LoadAudioData();
					}
					if ((int)source.clip.loadState == 2)
					{
						if (!source.isPlaying && !AudioListener.pause)
						{
							source.Play();
						}
						if (!reported)
						{
							Debug.Log((object)("SKYWURST_FOUNTAIN_READY mixer=" + ((Object)source.outputAudioMixerGroup).name + " clip=" + ((Object)source.clip).name + " playing=" + source.isPlaying + " volume=" + source.volume + " range=" + source.maxDistance), (Object)(object)this);
							reported = true;
						}
					}
					else if ((int)source.clip.loadState == 3 && !reported)
					{
						Debug.LogError((object)"Skywurst fountain audio clip failed to load.", (Object)(object)this);
						reported = true;
					}
				}
				if ((Object)(object)listener == (Object)null || !((Behaviour)listener).enabled || !((Component)listener).gameObject.activeInHierarchy)
				{
					listener = null;
					AudioListener[] array = Object.FindObjectsOfType<AudioListener>();
					foreach (AudioListener val in array)
					{
						if (((Behaviour)val).enabled && ((Component)val).gameObject.activeInHierarchy)
						{
							listener = val;
							break;
						}
					}
				}
				yield return (object)new WaitForSecondsRealtime(1f);
			}
		}

		private void LateUpdate()
		{
			//IL_0033: 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_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)
			if (!((Object)(object)source == (Object)null))
			{
				int num;
				if ((Object)(object)listener != (Object)null)
				{
					Vector3 val = ((Component)listener).transform.position - ((Component)this).transform.position;
					num = ((((Vector3)(ref val)).sqrMagnitude >= source.maxDistance * source.maxDistance) ? 1 : 0);
				}
				else
				{
					num = 0;
				}
				bool flag = (byte)num != 0;
				source.mute = authoredMute || !routed || flag;
			}
		}

		private void OnDisable()
		{
			((MonoBehaviour)this).StopAllCoroutines();
			if ((Object)(object)source != (Object)null)
			{
				source.Stop();
				source.mute = authoredMute;
			}
			routed = false;
		}

		private bool TryBind()
		{
			if ((Object)(object)source.outputAudioMixerGroup != (Object)null && ((Object)source.outputAudioMixerGroup).name == "Environment Main Ambient" && ((Object)source.outputAudioMixerGroup.audioMixer).name == "Enviroment")
			{
				return true;
			}
			AudioMixer[] array = Resources.FindObjectsOfTypeAll<AudioMixer>();
			foreach (AudioMixer val in array)
			{
				if (((Object)val).name != "Enviroment")
				{
					continue;
				}
				AudioMixerGroup[] array2 = val.FindMatchingGroups("Environment Main Ambient");
				foreach (AudioMixerGroup val2 in array2)
				{
					if (!(((Object)val2).name != "Environment Main Ambient"))
					{
						source.outputAudioMixerGroup = val2;
						return true;
					}
				}
			}
			return false;
		}
	}
	[DisallowMultipleComponent]
	[AddComponentMenu("Skywurst/Night vision sky")]
	public sealed class SkywurstNightVisionSky : MonoBehaviour
	{
		public Material NightVisionSky;

		private FVRSceneSettings target;

		private Material previous;

		private IEnumerator Start()
		{
			while ((Object)(object)GM.CurrentSceneSettings == (Object)null)
			{
				yield return null;
			}
			target = GM.CurrentSceneSettings;
			previous = target.NVSkySwap;
			if ((Object)(object)NightVisionSky != (Object)null)
			{
				target.NVSkySwap = NightVisionSky;
			}
		}

		private void OnDestroy()
		{
			if ((Object)(object)target != (Object)null && (Object)(object)target.NVSkySwap == (Object)(object)NightVisionSky)
			{
				target.NVSkySwap = previous;
			}
		}
	}
	[DisallowMultipleComponent]
	[AddComponentMenu("Skywurst/Void item recovery")]
	public sealed class SkywurstVoidRecovery : MonoBehaviour
	{
		[Tooltip("Matches Atlas's native player catcher; 30 metres below the lawn.")]
		public float CatchHeight = -17f;

		public Transform SpawnPoint;

		[Tooltip("Local offset from the player spawn, leaving room for the player.")]
		public Vector3 ItemReturnOffset = new Vector3(0f, 0.8f, 1.25f);

		private float nextCheck;

		private int returnSlot;

		private readonly HashSet<FVRPhysicalObject> checkedRoots = new HashSet<FVRPhysicalObject>();

		private void FixedUpdate()
		{
			if (!(Time.time < nextCheck) && !((Object)(object)GM.CurrentSceneSettings == (Object)null))
			{
				nextCheck = Time.time + 0.1f;
				RecoverFallenItems();
			}
		}

		public int RecoverFallenItems()
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)SpawnPoint == (Object)null || FVRInteractiveObject.All == null)
			{
				return 0;
			}
			checkedRoots.Clear();
			int num = 0;
			List<FVRInteractiveObject> all = FVRInteractiveObject.All;
			for (int i = 0; i < all.Count; i++)
			{
				FVRInteractiveObject obj = all[i];
				FVRPhysicalObject val = (FVRPhysicalObject)(object)((obj is FVRPhysicalObject) ? obj : null);
				if (!((Object)(object)val == (Object)null) && ((Component)val).gameObject.activeInHierarchy && !(((Component)val).transform.position.y >= CatchHeight))
				{
					FVRPhysicalObject val2 = AssemblyRoot(val);
					if (!((Object)(object)val2 == (Object)null) && checkedRoots.Add(val2) && !(((Component)val2).transform.position.y >= CatchHeight) && TryRecover(val2))
					{
						num++;
					}
				}
			}
			return num;
		}

		private static FVRPhysicalObject AssemblyRoot(FVRPhysicalObject item)
		{
			FVRPhysicalObject val = item;
			Transform parent = ((Component)item).transform.parent;
			while ((Object)(object)parent != (Object)null)
			{
				FVRPhysicalObject component = ((Component)parent).GetComponent<FVRPhysicalObject>();
				if ((Object)(object)component != (Object)null)
				{
					val = component;
				}
				parent = parent.parent;
			}
			if ((Object)(object)val.QuickbeltSlot != (Object)null)
			{
				FVRPhysicalObject affixedToRoot = val.QuickbeltSlot.GetAffixedToRoot();
				if ((Object)(object)affixedToRoot != (Object)null && (Object)(object)affixedToRoot != (Object)(object)val)
				{
					val = affixedToRoot;
				}
			}
			return val;
		}

		public bool TryRecover(FVRPhysicalObject root)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0331: Unknown result type (might be due to invalid IL or missing references)
			//IL_0336: Unknown result type (might be due to invalid IL or missing references)
			//IL_033b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0369: Unknown result type (might be due to invalid IL or missing references)
			//IL_0371: Unknown result type (might be due to invalid IL or missing references)
			//IL_0373: Unknown result type (might be due to invalid IL or missing references)
			//IL_0378: Unknown result type (might be due to invalid IL or missing references)
			//IL_037d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_03eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0395: Unknown result type (might be due to invalid IL or missing references)
			//IL_039a: Unknown result type (might be due to invalid IL or missing references)
			//IL_03af: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)SpawnPoint == (Object)null || (Object)(object)root == (Object)null || ((Component)root).transform.position.y >= CatchHeight || !((Component)root).gameObject.activeInHierarchy || (Object)(object)root.QuickbeltSlot != (Object)null)
			{
				return false;
			}
			List<FVRPhysicalObject> list = new List<FVRPhysicalObject>();
			list.Add(root);
			List<FVRPhysicalObject> list2 = list;
			HashSet<FVRPhysicalObject> hashSet = new HashSet<FVRPhysicalObject>();
			hashSet.Add(root);
			HashSet<FVRPhysicalObject> hashSet2 = hashSet;
			HashSet<Rigidbody> hashSet3 = new HashSet<Rigidbody>();
			HashSet<Transform> hashSet4 = new HashSet<Transform>();
			bool flag = false;
			Bounds bounds = default(Bounds);
			((Bounds)(ref bounds))..ctor(((Component)root).transform.position, Vector3.zero);
			for (int i = 0; i < list2.Count; i++)
			{
				FVRPhysicalObject val = list2[i];
				if (((FVRInteractiveObject)val).IsHeld || val.IsAltHeld)
				{
					return false;
				}
				hashSet4.Add(((Component)val).transform);
				FVRPhysicalObject[] componentsInChildren = ((Component)val).GetComponentsInChildren<FVRPhysicalObject>(true);
				foreach (FVRPhysicalObject item in componentsInChildren)
				{
					if (hashSet2.Add(item))
					{
						list2.Add(item);
					}
				}
				List<FVRPhysicalObject> containedObjects = val.GetContainedObjects();
				if (containedObjects != null)
				{
					foreach (FVRPhysicalObject item3 in containedObjects)
					{
						if ((Object)(object)item3 != (Object)null && hashSet2.Add(item3))
						{
							list2.Add(item3);
						}
					}
				}
				if (val.Slots != null)
				{
					FVRQuickBeltSlot[] slots = val.Slots;
					foreach (FVRQuickBeltSlot val2 in slots)
					{
						if ((Object)(object)val2 != (Object)null && (Object)(object)val2.CurObject != (Object)null && hashSet2.Add(val2.CurObject))
						{
							list2.Add(val2.CurObject);
						}
					}
				}
				Rigidbody[] componentsInChildren2 = ((Component)val).GetComponentsInChildren<Rigidbody>(true);
				foreach (Rigidbody item2 in componentsInChildren2)
				{
					hashSet3.Add(item2);
				}
				if (val.DependantRBs != null)
				{
					Rigidbody[] dependantRBs = val.DependantRBs;
					foreach (Rigidbody val3 in dependantRBs)
					{
						if ((Object)(object)val3 != (Object)null)
						{
							hashSet3.Add(val3);
						}
					}
				}
				Collider[] componentsInChildren3 = ((Component)val).GetComponentsInChildren<Collider>();
				foreach (Collider val4 in componentsInChildren3)
				{
					if (val4.enabled && !val4.isTrigger)
					{
						if (!flag)
						{
							bounds = val4.bounds;
							flag = true;
						}
						else
						{
							((Bounds)(ref bounds)).Encapsulate(val4.bounds);
						}
					}
				}
			}
			if (hashSet3.Count == 0)
			{
				return false;
			}
			Vector3 val5 = SpawnPoint.TransformPoint(ItemReturnOffset);
			Vector3 val6 = default(Vector3);
			((Vector3)(ref val6))..ctor((float)(returnSlot % 3 - 1) * 0.65f, 0f, (float)(returnSlot / 3) * 0.65f);
			val5 += SpawnPoint.TransformDirection(val6);
			if (flag)
			{
				val5.y = Mathf.Max(val5.y, SpawnPoint.position.y + 0.15f + ((Component)root).transform.position.y - ((Bounds)(ref bounds)).min.y);
			}
			Vector3 delta = val5 - ((Component)root).transform.position;
			TranslateAssembly(hashSet4, hashSet3, delta);
			returnSlot = (returnSlot + 1) % 9;
			return true;
		}

		private static void TranslateAssembly(HashSet<Transform> transforms, HashSet<Rigidbody> bodies, Vector3 delta)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: 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_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: 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)
			//IL_011f: 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)
			Dictionary<Rigidbody, Vector3> dictionary = new Dictionary<Rigidbody, Vector3>();
			foreach (Rigidbody body in bodies)
			{
				dictionary.Add(body, body.position);
				transforms.Add(((Component)body).transform);
			}
			foreach (Transform transform in transforms)
			{
				bool flag = false;
				Transform parent = transform.parent;
				while ((Object)(object)parent != (Object)null)
				{
					if (transforms.Contains(parent))
					{
						flag = true;
						break;
					}
					parent = parent.parent;
				}
				if (!flag)
				{
					transform.position += delta;
				}
			}
			foreach (Rigidbody body2 in bodies)
			{
				body2.position = dictionary[body2] + delta;
				if (!body2.isKinematic)
				{
					body2.velocity = Vector3.zero;
					body2.angularVelocity = Vector3.zero;
					body2.WakeUp();
				}
			}
		}
	}
}