Decompiled source of MicToggle v1.0.1

BepInEx\plugins\MicToggleMod\MicToggleMod.dll

Decompiled 16 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Voice.Unity;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.UI;
using YAPYAP;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MicToggleMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MicToggleMod")]
[assembly: AssemblyTitle("MicToggleMod")]
[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.yapyap.mod.mictoggle", "YapYap Mic Toggle", "1.0.1")]
public sealed class MicToggleMod : BaseUnityPlugin
{
	[HarmonyPatch(typeof(VoiceManager), "Update")]
	private static class VoiceManager_Update_Patch
	{
		private static void Postfix(VoiceManager __instance)
		{
			if (!MicEnabled && !((Object)(object)__instance == (Object)null) && !((Object)(object)((VoiceConnection)__instance).PrimaryRecorder == (Object)null))
			{
				((VoiceConnection)__instance).PrimaryRecorder.TransmitEnabled = false;
			}
		}
	}

	private static MicToggleMod _instance;

	internal static bool MicEnabled = true;

	private Harmony _harmony;

	private GameObject _uiRoot;

	private Image _micIcon;

	private Text _micText;

	private Texture2D _iconTexture;

	private Sprite _iconSprite;

	private const string LogPrefix = "[柠檬][MicToggle] ";

	private void Awake()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[柠檬][MicToggle] MOD 加载中...");
		_instance = this;
		_harmony = new Harmony("com.yapyap.mod.mictoggle");
		_harmony.PatchAll(typeof(MicToggleMod).Assembly);
		EnsureUi();
		RefreshUi();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[柠檬][MicToggle] MOD 已加载。按 M 键切换开麦/关麦。");
	}

	private void OnDestroy()
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[柠檬][MicToggle] MOD 卸载中...");
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
			_harmony = null;
		}
		if ((Object)(object)_uiRoot != (Object)null)
		{
			Object.Destroy((Object)(object)_uiRoot);
			_uiRoot = null;
		}
		if ((Object)(object)_iconSprite != (Object)null)
		{
			Object.Destroy((Object)(object)_iconSprite);
			_iconSprite = null;
		}
		if ((Object)(object)_iconTexture != (Object)null)
		{
			Object.Destroy((Object)(object)_iconTexture);
			_iconTexture = null;
		}
		if ((Object)(object)_instance == (Object)(object)this)
		{
			_instance = null;
		}
	}

	private void Update()
	{
		if (!WasTogglePressed())
		{
			return;
		}
		if ((Object)(object)InputManager.Instance != (Object)null && InputManager.Instance.IsInputLocked)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[柠檬][MicToggle] 当前输入被锁定,忽略 M 键切换。");
			return;
		}
		if (IsTypingIntoUi())
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[柠檬][MicToggle] 正在输入文本,忽略 M 键切换。");
			return;
		}
		bool micEnabled = MicEnabled;
		MicEnabled = !MicEnabled;
		RefreshUi();
		if (micEnabled != MicEnabled)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[柠檬][MicToggle] " + (MicEnabled ? "已开麦(别人能听到)" : "已关麦(别人听不到)")));
		}
	}

	private bool WasTogglePressed()
	{
		Keyboard current = Keyboard.current;
		if (current == null)
		{
			return false;
		}
		return ((ButtonControl)current.mKey).wasPressedThisFrame;
	}

	private bool IsTypingIntoUi()
	{
		EventSystem current = EventSystem.current;
		if ((Object)(object)current == (Object)null)
		{
			return false;
		}
		GameObject currentSelectedGameObject = current.currentSelectedGameObject;
		if ((Object)(object)currentSelectedGameObject == (Object)null)
		{
			return false;
		}
		TMP_InputField component = currentSelectedGameObject.GetComponent<TMP_InputField>();
		if ((Object)(object)component != (Object)null && component.isFocused)
		{
			return true;
		}
		InputField component2 = currentSelectedGameObject.GetComponent<InputField>();
		if ((Object)(object)component2 != (Object)null)
		{
			return component2.isFocused;
		}
		return false;
	}

	private void EnsureUi()
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Expected O, but got Unknown
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Expected O, but got Unknown
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: 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_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Expected O, but got Unknown
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_uiRoot != (Object)null))
		{
			_uiRoot = new GameObject("MicToggleMod_UI");
			Object.DontDestroyOnLoad((Object)(object)_uiRoot);
			Canvas obj = _uiRoot.AddComponent<Canvas>();
			obj.renderMode = (RenderMode)0;
			obj.sortingOrder = 5000;
			CanvasScaler obj2 = _uiRoot.AddComponent<CanvasScaler>();
			obj2.uiScaleMode = (ScaleMode)1;
			obj2.referenceResolution = new Vector2(1920f, 1080f);
			_uiRoot.AddComponent<GraphicRaycaster>();
			GameObject val = new GameObject("MicIcon");
			val.transform.SetParent(_uiRoot.transform, false);
			_micIcon = val.AddComponent<Image>();
			_micIcon.sprite = GetOrCreateIconSprite();
			((Graphic)_micIcon).raycastTarget = false;
			_micIcon.type = (Type)0;
			RectTransform component = val.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(1f, 0f);
			component.anchorMax = new Vector2(1f, 0f);
			component.pivot = new Vector2(1f, 0f);
			component.anchoredPosition = new Vector2(-32f, 32f);
			component.sizeDelta = new Vector2(52f, 36f);
			GameObject val2 = new GameObject("MicText");
			val2.transform.SetParent(val.transform, false);
			_micText = val2.AddComponent<Text>();
			((Graphic)_micText).raycastTarget = false;
			_micText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
			_micText.fontSize = 18;
			_micText.alignment = (TextAnchor)4;
			_micText.text = "MIC";
			((Graphic)_micText).color = Color.white;
			RectTransform component2 = val2.GetComponent<RectTransform>();
			component2.anchorMin = Vector2.zero;
			component2.anchorMax = Vector2.one;
			component2.offsetMin = Vector2.zero;
			component2.offsetMax = Vector2.zero;
		}
	}

	private Sprite GetOrCreateIconSprite()
	{
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)_iconSprite != (Object)null)
		{
			return _iconSprite;
		}
		if ((Object)(object)_iconTexture == (Object)null)
		{
			_iconTexture = new Texture2D(1, 1, (TextureFormat)4, false);
			_iconTexture.SetPixel(0, 0, Color.white);
			_iconTexture.Apply(false, true);
		}
		_iconSprite = Sprite.Create(_iconTexture, new Rect(0f, 0f, 1f, 1f), new Vector2(0.5f, 0.5f), 100f);
		return _iconSprite;
	}

	private static bool IsChineseLanguage(SystemLanguage systemLanguage)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Invalid comparison between Unknown and I4
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Invalid comparison between Unknown and I4
		if ((int)systemLanguage == 6 || systemLanguage - 40 <= 1)
		{
			return true;
		}
		return false;
	}

	private static string GetMicLabel(bool micEnabled)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		bool flag = IsChineseLanguage(Application.systemLanguage);
		if (micEnabled)
		{
			if (!flag)
			{
				return "MIC ON";
			}
			return "开麦";
		}
		if (!flag)
		{
			return "MIC OFF";
		}
		return "关麦";
	}

	private void RefreshUi()
	{
		//IL_005c: 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)
		EnsureUi();
		if (!((Object)(object)_micIcon == (Object)null))
		{
			if (MicEnabled)
			{
				((Graphic)_micIcon).color = new Color(0.15f, 0.85f, 0.2f, 0.95f);
			}
			else
			{
				((Graphic)_micIcon).color = new Color(0.9f, 0.15f, 0.15f, 0.95f);
			}
			if ((Object)(object)_micText != (Object)null)
			{
				_micText.text = GetMicLabel(MicEnabled);
			}
		}
	}
}