Decompiled source of PositionalAudio v1.0.1

PositionalAudio.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Microsoft.CodeAnalysis;
using UnityEngine;
using WildSkies.Player;
using WildSkies.Service;
using mumblelib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("PositionalAudio")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+6c0c6e6ae28ce460b35e57febc4ef7342f5c4d5e")]
[assembly: AssemblyProduct("Adds Mumble Link Positional Audio support")]
[assembly: AssemblyTitle("PositionalAudio")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace mumblelib
{
	internal class Text
	{
		public static Encoding Encoding = Encoding.Unicode;
	}
	[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
	public struct Frame
	{
		public uint uiVersion;

		public uint uiTick;

		public unsafe fixed float fAvatarPosition[3];

		public unsafe fixed float fAvatarFront[3];

		public unsafe fixed float fAvatarTop[3];

		public unsafe fixed ushort name[256];

		public unsafe fixed float fCameraPosition[3];

		public unsafe fixed float fCameraFront[3];

		public unsafe fixed float fCameraTop[3];

		public unsafe fixed ushort id[256];

		public uint context_len;

		public unsafe fixed byte context[256];

		public unsafe fixed ushort description[2048];

		public unsafe void SetName(string name)
		{
			fixed (Frame* ptr = &this)
			{
				byte[] bytes = Text.Encoding.GetBytes(name + "\0");
				Marshal.Copy(bytes, 0, new IntPtr(ptr->name), bytes.Length);
			}
		}

		public unsafe void SetDescription(string desc)
		{
			fixed (Frame* ptr = &this)
			{
				byte[] bytes = Text.Encoding.GetBytes(desc + "\0");
				Marshal.Copy(bytes, 0, new IntPtr(ptr->description), bytes.Length);
			}
		}

		public unsafe void SetID(string id)
		{
			fixed (Frame* ptr = &this)
			{
				byte[] bytes = Text.Encoding.GetBytes(id + "\0");
				Marshal.Copy(bytes, 0, new IntPtr(ptr->id), bytes.Length);
			}
		}

		public unsafe void SetContext(string context)
		{
			fixed (Frame* ptr = &this)
			{
				byte[] bytes = Encoding.UTF8.GetBytes(context);
				context_len = (uint)Math.Min(256, bytes.Length);
				Marshal.Copy(bytes, 0, new IntPtr(ptr->context), (int)context_len);
			}
		}
	}
	public class MumbleLinkFile : IDisposable
	{
		private readonly MemoryMappedFile memoryMappedFile;

		private unsafe readonly Frame* ptr;

		private bool disposed;

		private static string LinkFileName()
		{
			return "MumbleLink";
		}

		public unsafe MumbleLinkFile(MemoryMappedFile memoryMappedFile)
		{
			this.memoryMappedFile = memoryMappedFile ?? throw new ArgumentNullException("memoryMappedFile");
			byte* pointer = null;
			memoryMappedFile.CreateViewAccessor().SafeMemoryMappedViewHandle.AcquirePointer(ref pointer);
			ptr = (Frame*)pointer;
		}

		public unsafe Frame* FramePtr()
		{
			return ptr;
		}

		public static MumbleLinkFile CreateOrOpen()
		{
			return new MumbleLinkFile(MemoryMappedFile.CreateOrOpen(LinkFileName(), Marshal.SizeOf<Frame>()));
		}

		public void Dispose()
		{
			Dispose(disposing: true);
			GC.SuppressFinalize(this);
		}

		private void assertNotDisposed()
		{
			if (disposed)
			{
				throw new ObjectDisposedException(GetType().FullName);
			}
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!disposed)
			{
				if (disposing)
				{
					memoryMappedFile.Dispose();
				}
				disposed = true;
			}
		}
	}
}
namespace PositionalAudio
{
	internal class DebugInfo : MonoBehaviour
	{
		private void OnGUI()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			GUILayout.BeginArea(new Rect(0f, 0f, 500f, 200f));
			GUILayout.Box("PlayerId: " + PositionalAudioPlugin.playerId, (Il2CppReferenceArray<GUILayoutOption>)null);
			GUILayout.Box("context: " + PositionalAudioPlugin.context, (Il2CppReferenceArray<GUILayoutOption>)null);
			Vector3 val = PositionalAudioPlugin.playerCoords;
			GUILayout.Box("coords: " + ((object)(Vector3)(ref val)).ToString(), (Il2CppReferenceArray<GUILayoutOption>)null);
			val = PositionalAudioPlugin.playerRotation;
			GUILayout.Box("rot: " + ((object)(Vector3)(ref val)).ToString(), (Il2CppReferenceArray<GUILayoutOption>)null);
			GUILayout.EndArea();
		}
	}
	[BepInPlugin("PositionalAudio", "Adds Mumble Link Positional Audio support", "1.0.1")]
	public class PositionalAudioPlugin : BasePlugin
	{
		internal enum PlayerState
		{
			MainMenu,
			InWorld
		}

		internal static ManualLogSource mls;

		internal static Vector3 playerCoords;

		internal static Vector3 playerRotation;

		internal static string playerId = "";

		internal static LocalPlayer player = null;

		internal static MumbleLinkFile mumbleLink;

		internal static string context = "Lobby";

		internal static GameObject UiGameObject;

		public override void Load()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			mls = ((BasePlugin)this).Log;
			Harmony.CreateAndPatchAll(typeof(PositionalAudioPlugin), "PositionalAudio");
			ManualLogSource val = mls;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(18, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Plugin ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("PositionalAudio");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" is loaded!");
			}
			val.LogInfo(val2);
		}

		public override bool Unload()
		{
			mumbleLink.Dispose();
			return ((BasePlugin)this).Unload();
		}

		public unsafe static void startMumbleLink()
		{
			if (mumbleLink != null)
			{
				stopMumbleLink();
			}
			mumbleLink = MumbleLinkFile.CreateOrOpen();
			Frame* ptr = mumbleLink.FramePtr();
			ptr->SetName("LostSkies");
			ptr->uiVersion = 2u;
			ptr->SetID(playerId);
			ptr->SetContext(context);
		}

		public static void stopMumbleLink()
		{
			mumbleLink.Dispose();
			mumbleLink = null;
		}

		public unsafe static void updateMumbleLink()
		{
			if (mumbleLink == null)
			{
				startMumbleLink();
			}
			Frame* ptr = mumbleLink.FramePtr();
			float* fixedElementField = ptr->fAvatarPosition;
			*fixedElementField = playerCoords.x;
			ptr->fAvatarPosition[1] = playerCoords.y;
			ptr->fAvatarPosition[2] = playerCoords.z;
			float* fixedElementField2 = ptr->fAvatarFront;
			*fixedElementField2 = playerRotation.x;
			ptr->fAvatarFront[1] = playerRotation.y;
			ptr->fAvatarFront[2] = playerRotation.z;
			float* fixedElementField3 = ptr->fCameraPosition;
			*fixedElementField3 = playerCoords.x;
			ptr->fCameraPosition[1] = playerCoords.y;
			ptr->fCameraPosition[2] = playerCoords.z;
			float* fixedElementField4 = ptr->fCameraFront;
			*fixedElementField4 = playerRotation.x;
			ptr->fCameraFront[1] = playerRotation.y;
			ptr->fCameraFront[2] = playerRotation.z;
			ptr->SetID(playerId);
			ptr->SetContext(context);
			ptr->uiTick++;
		}

		[HarmonyPatch(typeof(PlayerNetwork), "Update")]
		[HarmonyPrefix]
		private static void Update(PlayerNetwork __instance)
		{
			//IL_0015: 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_0029: 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_0031: 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_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)
			if (!__instance.NetworkInstantiated)
			{
				playerCoords = __instance.PlayerSync.PlayerWorldPosition;
				Ray lookRay = player.CameraManager.LookRay;
				Vector3 direction = ((Ray)(ref lookRay)).direction;
				playerRotation = ((Vector3)(ref direction)).normalized;
				playerId = player.PlayerNetwork.NetworkPlayerId;
				context = player._sessionService._currentWorldId;
				updateMumbleLink();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(LocalPlayerService), "RegisterLocalPlayer")]
		private static void PlayerCreated(LocalPlayerService __instance)
		{
			player = __instance._localPlayer;
			startMumbleLink();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(LocalPlayerService), "UnregisterPlayer")]
		private static void PlayerDestroyed(LocalPlayerService __instance)
		{
			stopMumbleLink();
			player = null;
			context = "Lobby";
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "PositionalAudio";

		public const string PLUGIN_NAME = "Adds Mumble Link Positional Audio support";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}