Decompiled source of MicMuteToggle v1.0.2

BepInEx/plugins/MicMuteToggle/MicMuteToggle.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Photon.Voice.PUN;
using Photon.Voice.Unity;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("MicMuteToggle")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MicMuteToggle")]
[assembly: AssemblyTitle("MicMuteToggle")]
[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;
		}
	}
}
[BepInPlugin("com.peak.micmutetoggle", "MicMuteToggle", "1.0.1")]
public class MicMuteToggle : BaseUnityPlugin
{
	[CompilerGenerated]
	private sealed class <WaitForHUDAndCreateIcon>d__10 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public MicMuteToggle <>4__this;

		private GameObject <hud>5__1;

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

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

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

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

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)"[MicMuteToggle] Waiting for Canvas_HUD...");
				<hud>5__1 = null;
				break;
			case 1:
				<>1__state = -1;
				break;
			}
			if ((Object)(object)(<hud>5__1 = GameObject.Find("/GAME/GUIManager/Canvas_HUD/")) == (Object)null)
			{
				<>2__current = null;
				<>1__state = 1;
				return true;
			}
			((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)"[MicMuteToggle] Canvas_HUD found.");
			if ((Object)(object)<>4__this.iconObject != (Object)null)
			{
				Object.Destroy((Object)(object)<>4__this.iconObject);
				<>4__this.iconObject = null;
			}
			<>4__this.CreateMuteIcon(<hud>5__1);
			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 ConfigEntry<KeyCode> toggleKey;

	private Recorder recorder;

	private GameObject iconObject;

	private RawImage iconImage;

	private Texture2D mutedIcon;

	private void Awake()
	{
		//IL_0034: 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)
		toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "ToggleMuteKey", (KeyCode)109, "Key to toggle microphone mute/unmute");
		ManualLogSource logger = ((BaseUnityPlugin)this).Logger;
		KeyCode value = toggleKey.Value;
		logger.LogInfo((object)("[MicMuteToggle] Loaded. Press " + ((object)(KeyCode)(ref value)).ToString() + " to toggle mic mute."));
		mutedIcon = LoadEmbeddedTexture("MicMuteToggle.Images.MicMuted.png");
		ManualLogSource logger2 = ((BaseUnityPlugin)this).Logger;
		Texture2D obj = mutedIcon;
		object arg = ((obj != null) ? new int?(((Texture)obj).width) : null);
		Texture2D obj2 = mutedIcon;
		logger2.LogInfo((object)$"[MicMuteToggle] Icon texture loaded: {arg}x{((obj2 != null) ? new int?(((Texture)obj2).height) : null)}");
		SceneManager.sceneLoaded += OnSceneLoaded;
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		((MonoBehaviour)this).StartCoroutine(WaitForHUDAndCreateIcon());
	}

	private void Update()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		if (!Input.GetKeyDown(toggleKey.Value))
		{
			return;
		}
		if ((Object)(object)recorder == (Object)null)
		{
			TryFindRecorder();
			if ((Object)(object)recorder == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[MicMuteToggle] Could not find Recorder.");
				return;
			}
		}
		recorder.TransmitEnabled = !recorder.TransmitEnabled;
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[MicMuteToggle] Microphone " + (recorder.TransmitEnabled ? "UNMUTED" : "MUTED")));
		UpdateIconVisibility();
	}

	private void TryFindRecorder()
	{
		Character localCharacter = Character.localCharacter;
		if ((Object)(object)localCharacter == (Object)null)
		{
			return;
		}
		GameObject gameObject = ((Component)localCharacter).gameObject;
		if (!((Object)(object)gameObject == (Object)null))
		{
			PhotonVoiceView component = gameObject.GetComponent<PhotonVoiceView>();
			if ((Object)(object)component != (Object)null)
			{
				recorder = component.RecorderInUse;
			}
		}
	}

	[IteratorStateMachine(typeof(<WaitForHUDAndCreateIcon>d__10))]
	private IEnumerator WaitForHUDAndCreateIcon()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <WaitForHUDAndCreateIcon>d__10(0)
		{
			<>4__this = this
		};
	}

	private void CreateMuteIcon(GameObject hud)
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Expected O, but got Unknown
		//IL_006d: 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)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		iconObject = new GameObject("MicMutedIcon", new Type[3]
		{
			typeof(RectTransform),
			typeof(CanvasRenderer),
			typeof(RawImage)
		});
		iconObject.transform.SetParent(hud.transform, false);
		RectTransform component = iconObject.GetComponent<RectTransform>();
		component.anchorMin = new Vector2(0f, 0f);
		component.anchorMax = new Vector2(0f, 0f);
		component.anchoredPosition = new Vector2(100f, 160f);
		component.sizeDelta = new Vector2(64f, 64f);
		iconImage = iconObject.GetComponent<RawImage>();
		iconImage.texture = (Texture)(object)mutedIcon;
		((Graphic)iconImage).color = Color.white;
		((Behaviour)iconImage).enabled = false;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[MicMuteToggle] Mic icon created and hidden.");
	}

	private void UpdateIconVisibility()
	{
		if ((Object)(object)iconImage != (Object)null && (Object)(object)recorder != (Object)null)
		{
			((Behaviour)iconImage).enabled = !recorder.TransmitEnabled;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[MicMuteToggle] Icon visibility set: " + (((Behaviour)iconImage).enabled ? "ON" : "OFF")));
		}
	}

	private Texture2D LoadEmbeddedTexture(string resourceName)
	{
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Expected O, but got Unknown
		using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
		if (stream == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[MicMuteToggle] Failed to load resource: " + resourceName));
			return null;
		}
		byte[] array = new byte[stream.Length];
		stream.Read(array, 0, array.Length);
		Texture2D val = new Texture2D(2, 2);
		ImageConversion.LoadImage(val, array);
		val.Apply();
		return val;
	}
}