Decompiled source of Teleportation v1.0.1

Mods/Teleportation.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BoneLib;
using BoneLib.BoneMenu;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using MelonLoader;
using MelonLoader.Preferences;
using MelonLoader.Utils;
using Microsoft.CodeAnalysis;
using Teleportation;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Networking;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Teleportation")]
[assembly: AssemblyDescription("Bonelab Teleport Mod")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Teleportation")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("A1B2C3D4-E5F6-4789-ABCD-1234567890AB")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: MelonInfo(typeof(Core), "Teleportation", "1.0.0", "Waspothegreat", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Teleportation
{
	public enum HandMode
	{
		Right,
		Left
	}
	public class Core : MelonMod
	{
		private static MelonPreferences_Category prefsCategory;

		private static MelonPreferences_Entry<bool> enabledEntry;

		private static MelonPreferences_Entry<float> rangeEntry;

		private static MelonPreferences_Entry<HandMode> handModeEntry;

		private static MelonPreferences_Entry<bool> soundEnabledEntry;

		private static MelonPreferences_Entry<float> volumeEntry;

		private static MelonPreferences_Entry<float> rangeIncrementEntry;

		private static bool isEnabled = true;

		private static float maxRange = 100f;

		private static float rangeIncrement = 5f;

		private static HandMode currentHandMode = HandMode.Right;

		private static bool soundEnabled = true;

		private static float soundVolume = 1f;

		private static bool isAiming = false;

		private static GameObject markerObject;

		private static bool wasStickDown = false;

		private static bool wasTriggerDown = false;

		private static AudioClip teleportClip;

		private static UnityWebRequest _soundLoadRequest;

		private static UnityWebRequestAsyncOperation _soundLoadOperation;

		public override void OnInitializeMelon()
		{
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			prefsCategory = MelonPreferences.CreateCategory("Teleportation");
			enabledEntry = prefsCategory.CreateEntry<bool>("Enabled", true, "Enabled", "Enable or disable the teleportation mod", false, false, (ValueValidator)null, (string)null);
			rangeEntry = prefsCategory.CreateEntry<float>("MaxRange", 100f, "Max Range", "Maximum teleportation distance", false, false, (ValueValidator)null, (string)null);
			rangeIncrementEntry = prefsCategory.CreateEntry<float>("RangeIncrement", 5f, "Range Increment", "Increment step for range slider in BoneMenu", false, false, (ValueValidator)null, (string)null);
			handModeEntry = prefsCategory.CreateEntry<HandMode>("HandMode", HandMode.Right, "Hand Mode", "Which hand to use for teleportation", false, false, (ValueValidator)null, (string)null);
			soundEnabledEntry = prefsCategory.CreateEntry<bool>("SoundEnabled", true, "Sound Enabled", "Play sound on teleportation", false, false, (ValueValidator)null, (string)null);
			volumeEntry = prefsCategory.CreateEntry<float>("Volume", 1f, "Volume", "Sound volume (0.0 - 1.0)", false, false, (ValueValidator)null, (string)null);
			isEnabled = enabledEntry.Value;
			maxRange = rangeEntry.Value;
			rangeIncrement = rangeIncrementEntry.Value;
			currentHandMode = handModeEntry.Value;
			soundEnabled = soundEnabledEntry.Value;
			soundVolume = volumeEntry.Value;
			LoadEmbeddedSound();
			Page obj = Page.Root.CreatePage("Teleportation", new Color(0.6f, 0f, 1f), 0, true);
			obj.CreateBool("Enabled", Color.green, isEnabled, (Action<bool>)delegate(bool v)
			{
				isEnabled = v;
				enabledEntry.Value = v;
				prefsCategory.SaveToFile(false);
				if (!isEnabled && (Object)(object)markerObject != (Object)null)
				{
					markerObject.SetActive(false);
				}
			});
			obj.CreateFloat("Range", Color.yellow, maxRange, rangeIncrement, 10f, 500000000f, (Action<float>)delegate(float v)
			{
				maxRange = v;
				rangeEntry.Value = v;
				prefsCategory.SaveToFile(false);
			});
			obj.CreateEnum("Bind", Color.white, (Enum)currentHandMode, (Action<Enum>)delegate(Enum v)
			{
				currentHandMode = (HandMode)(object)v;
				handModeEntry.Value = (HandMode)(object)v;
				prefsCategory.SaveToFile(false);
				isAiming = false;
				if ((Object)(object)markerObject != (Object)null)
				{
					markerObject.SetActive(false);
				}
			});
			obj.CreateBool("Play Sound", Color.white, soundEnabled, (Action<bool>)delegate(bool v)
			{
				soundEnabled = v;
				soundEnabledEntry.Value = v;
				prefsCategory.SaveToFile(false);
			});
			obj.CreateFloat("Volume", Color.white, soundVolume, 0.05f, 0f, 1f, (Action<float>)delegate(float v)
			{
				soundVolume = v;
				volumeEntry.Value = v;
				prefsCategory.SaveToFile(false);
			});
		}

		private void LoadEmbeddedSound()
		{
			try
			{
				Assembly executingAssembly = Assembly.GetExecutingAssembly();
				string text = "Teleportation.Resources.teleport.wav";
				using Stream stream = executingAssembly.GetManifestResourceStream(text);
				if (stream != null)
				{
					using (MemoryStream memoryStream = new MemoryStream())
					{
						stream.CopyTo(memoryStream);
						string text2 = Path.Combine(MelonEnvironment.UserDataDirectory, "Teleportation", "teleport_embedded.wav");
						string directoryName = Path.GetDirectoryName(text2);
						if (!Directory.Exists(directoryName))
						{
							Directory.CreateDirectory(directoryName);
						}
						byte[] array = memoryStream.ToArray();
						try
						{
							teleportClip = CreateAudioClipFromWavData(array);
							if ((Object)(object)teleportClip != (Object)null)
							{
								((Object)teleportClip).name = "TeleportSFX";
								MelonLogger.Msg("Custom sound loaded via manual parsing.");
								return;
							}
						}
						catch (Exception ex)
						{
							MelonLogger.Warning("Manual WAV parsing failed: " + ex.Message + ". Falling back to UnityWebRequest.");
						}
						File.WriteAllBytes(text2, array);
						string text3 = "file://" + text2;
						try
						{
							UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip(text3, (AudioType)20);
							UnityWebRequestAsyncOperation soundLoadOperation = audioClip.SendWebRequest();
							_soundLoadRequest = audioClip;
							_soundLoadOperation = soundLoadOperation;
							return;
						}
						catch (MissingMethodException ex2)
						{
							MelonLogger.Error("Sound not supported on this version (Method Missing): " + ex2.Message);
							soundEnabled = false;
							return;
						}
						catch (Exception ex3)
						{
							MelonLogger.Error("Unexpected error loading sound: " + ex3.Message);
							return;
						}
					}
				}
				MelonLogger.Warning("Embedded resource '" + text + "' not found.");
			}
			catch (Exception ex4)
			{
				MelonLogger.Error("Failed to load embedded sound: " + ex4.Message);
			}
		}

		private static AudioClip CreateAudioClipFromWavData(byte[] wavData)
		{
			if (wavData == null || wavData.Length < 44)
			{
				return null;
			}
			int num = BitConverter.ToInt16(wavData, 22);
			int num2 = BitConverter.ToInt32(wavData, 24);
			if (BitConverter.ToInt16(wavData, 34) != 16)
			{
				return null;
			}
			int num3 = 44;
			int num4 = (wavData.Length - num3) / 2;
			int num5 = num4 / num;
			float[] array = new float[num4];
			for (int i = 0; i < num4; i++)
			{
				short num6 = BitConverter.ToInt16(wavData, num3 + i * 2);
				array[i] = (float)num6 / 32768f;
			}
			AudioClip obj = AudioClip.Create("TeleportSFX", num5, num, num2, false);
			obj.SetData(Il2CppStructArray<float>.op_Implicit(array), 0);
			((Object)obj).hideFlags = (HideFlags)32;
			return obj;
		}

		public override void OnUpdate()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Invalid comparison between Unknown and I4
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_025e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			//IL_023e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			if (_soundLoadOperation != null && ((AsyncOperation)_soundLoadOperation).isDone)
			{
				try
				{
					if ((int)_soundLoadRequest.result == 1)
					{
						teleportClip = DownloadHandlerAudioClip.GetContent(_soundLoadRequest);
						if ((Object)(object)teleportClip != (Object)null)
						{
							((Object)teleportClip).name = "TeleportSFX";
						}
					}
					else
					{
						MelonLogger.Warning("Failed to load sound: " + _soundLoadRequest.error);
					}
				}
				catch (Exception ex)
				{
					MelonLogger.Error("Failed to retrieve sound content: " + ex.Message);
				}
				_soundLoadOperation = null;
				_soundLoadRequest = null;
			}
			if (!isEnabled)
			{
				if ((Object)(object)markerObject != (Object)null)
				{
					markerObject.SetActive(false);
				}
			}
			else
			{
				if ((Object)(object)Player.ControllerRig == (Object)null)
				{
					return;
				}
				BaseController val = ((currentHandMode == HandMode.Right) ? Player.RightController : Player.LeftController);
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				bool thumbStickDown = val.GetThumbStickDown();
				if (thumbStickDown && !wasStickDown)
				{
					MelonLogger.Msg("Stick Clicked! Toggling Aim.");
					isAiming = !isAiming;
					if (!isAiming && (Object)(object)markerObject != (Object)null)
					{
						markerObject.SetActive(false);
					}
				}
				wasStickDown = thumbStickDown;
				if (isAiming)
				{
					Transform transform = ((Component)val).transform;
					int num = -262144257;
					RaycastHit val2 = default(RaycastHit);
					if (Physics.Raycast(transform.position, transform.forward, ref val2, maxRange, num))
					{
						Transform root = ((Component)((RaycastHit)(ref val2)).collider).transform.root;
						if ((Object)(object)root == (Object)(object)((Component)Player.RigManager).transform || ((Object)root).name.Contains("Player"))
						{
							if ((Object)(object)markerObject != (Object)null)
							{
								markerObject.SetActive(false);
							}
							wasTriggerDown = val._primaryAxis > 0.5f;
							return;
						}
						UpdateMarker(((RaycastHit)(ref val2)).point, ((RaycastHit)(ref val2)).normal);
						bool num2 = val._primaryAxis > 0.5f;
						if (num2 && !wasTriggerDown)
						{
							if (Vector3.Dot(((RaycastHit)(ref val2)).normal, Vector3.down) > 0.5f)
							{
								float num3 = 1.75f;
								if ((Object)(object)Player.PhysicsRig != (Object)null && (Object)(object)Player.Avatar != (Object)null)
								{
									num3 = Player.Avatar.height;
								}
								Vector3 position = ((RaycastHit)(ref val2)).point + Vector3.down * num3;
								TeleportPlayer(position);
							}
							else
							{
								TeleportPlayer(((RaycastHit)(ref val2)).point);
							}
							if (soundEnabled && (Object)(object)teleportClip != (Object)null)
							{
								PlayTeleportSound();
							}
						}
						wasTriggerDown = num2;
					}
					else if ((Object)(object)markerObject != (Object)null)
					{
						markerObject.SetActive(false);
					}
				}
				else if ((Object)(object)markerObject != (Object)null)
				{
					markerObject.SetActive(false);
				}
			}
		}

		private void PlayTeleportSound()
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			if ((Object)(object)teleportClip == (Object)null)
			{
				return;
			}
			try
			{
				Audio.Play2DOneShot(teleportClip, (AudioMixerGroup)null, soundVolume, 1f);
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("BoneLib audio failed: " + ex.Message + ". Trying manual fallback.");
				GameObject val = new GameObject("TeleportSound_Manual");
				AudioSource obj = val.AddComponent<AudioSource>();
				obj.clip = teleportClip;
				obj.spatialBlend = 0f;
				obj.volume = soundVolume;
				((Behaviour)obj).enabled = true;
				val.SetActive(true);
				obj.Play();
				Object.Destroy((Object)(object)val, teleportClip.length + 0.5f);
			}
		}

		private void UpdateMarker(Vector3 position, Vector3 normal)
		{
			//IL_0217: Unknown result type (might be due to invalid IL or missing references)
			//IL_0218: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: Unknown result type (might be due to invalid IL or missing references)
			//IL_0237: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Expected O, but got Unknown
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)markerObject == (Object)null)
			{
				markerObject = new GameObject("TeleportMarker");
				LineRenderer val = markerObject.AddComponent<LineRenderer>();
				val.useWorldSpace = false;
				val.loop = true;
				val.startWidth = 0.05f;
				val.endWidth = 0.05f;
				val.alignment = (LineAlignment)1;
				((Renderer)val).shadowCastingMode = (ShadowCastingMode)0;
				((Renderer)val).receiveShadows = false;
				int num2 = (val.positionCount = 40);
				float num3 = 0.5f;
				Vector3[] array = (Vector3[])(object)new Vector3[num2];
				for (int i = 0; i < num2; i++)
				{
					float num4 = (float)Math.PI / 180f * ((float)i * 360f / (float)num2);
					array[i] = new Vector3(Mathf.Cos(num4) * num3, Mathf.Sin(num4) * num3, 0f);
				}
				val.SetPositions(Il2CppStructArray<Vector3>.op_Implicit(array));
				Shader val2 = Shader.Find("Universal Render Pipeline/Unlit");
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = Shader.Find("SLZ/SLZ Unlit");
				}
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = Shader.Find("Unlit/Color");
				}
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = Shader.Find("Hidden/Internal-Colored");
				}
				if ((Object)(object)val2 != (Object)null)
				{
					((Renderer)val).material = new Material(val2);
					((Renderer)val).material.color = new Color(1f, 1f, 1f, 1f);
					if (((Object)val2).name.Contains("Universal Render Pipeline"))
					{
						((Renderer)val).material.SetColor("_BaseColor", new Color(1f, 1f, 1f, 1f));
					}
					else if (((Object)val2).name.Contains("SLZ") && ((Renderer)val).material.HasProperty("_Color"))
					{
						((Renderer)val).material.SetColor("_Color", new Color(1f, 1f, 1f, 1f));
					}
					((Renderer)val).material.renderQueue = 4000;
				}
				else
				{
					MelonLogger.Error("CRITICAL: No valid shader found for LineRenderer!");
				}
			}
			markerObject.SetActive(true);
			markerObject.transform.position = position + normal * 0.02f;
			markerObject.transform.forward = normal;
		}

		private void TeleportPlayer(Vector3 position)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Player.RigManager != (Object)null)
			{
				Player.RigManager.Teleport(position, true);
			}
		}
	}
}