Decompiled source of MusicMicFix v0.0.1

BepInEx/plugins/MusicMicFix/MusicMicFix.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Voice.Unity;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 MusicMicFix
{
	[BepInPlugin("com.texmexdev.musicmicfix", "Music Mic Fix", "0.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(PlayerVoiceChat), "PhotonVoiceCreated")]
		private static class VoiceCreatedPatch
		{
			private static void Postfix(PlayerVoiceChat __instance)
			{
				WebRtcAudioDsp component = ((Component)__instance).GetComponent<WebRtcAudioDsp>();
				Recorder component2 = ((Component)__instance).GetComponent<Recorder>();
				string text = (Object.op_Implicit((Object)(object)component) ? $"WebRTC VAD={component.VAD}, AEC={component.AEC}, NoiseSuppression={component.NoiseSuppression}, AGC={component.AGC}" : "no WebRTC DSP");
				log.LogInfo((object)("Mic voice created: " + text + ", Recorder VoiceDetection=" + (Object.op_Implicit((Object)(object)component2) ? component2.VoiceDetection.ToString() : "no Recorder") + ", bitrate=" + (Object.op_Implicit((Object)(object)component2) ? component2.Bitrate.ToString() : "?") + ". Fix " + (fixOn ? "on" : "off") + ", music mode " + (musicMode ? "on" : "off") + "."));
				Apply(__instance);
			}
		}

		private static ManualLogSource log;

		private static bool fixOn = true;

		private static bool hearSelf;

		private static bool musicMode;

		private static bool? gameDspVad;

		private static bool? gameRecorderVad;

		private static bool? gameAec;

		private static bool? gameNs;

		private static bool? gameAgc;

		private ConfigEntry<Key> hearSelfKey;

		private ConfigEntry<Key> toggleFixKey;

		private ConfigEntry<Key> musicModeKey;

		private TextMeshProUGUI indicator;

		private void Awake()
		{
			//IL_0094: 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_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			log = ((BaseUnityPlugin)this).Logger;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			musicModeKey = ((BaseUnityPlugin)this).Config.Bind<Key>("General", "MusicModeKey", (Key)103, "Toggles music mode: turns off noise suppression and auto gain, which are tuned for speech and can cut music.");
			hearSelfKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Testing", "HearYourselfKey", (Key)101, "Toggles hearing your own mic after the game's voice filter, to test alone.");
			toggleFixKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Testing", "ToggleFixKey", (Key)102, "Switches the fix on and off live, to compare.");
			new Harmony("com.texmexdev.musicmicfix").PatchAll(typeof(VoiceCreatedPatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Loaded. Keys: {musicModeKey.Value} music mode, {hearSelfKey.Value} hear yourself, {toggleFixKey.Value} fix on/off.");
		}

		private void Update()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			IndicatorTick();
			Keyboard current = Keyboard.current;
			PlayerVoiceChat instance = PlayerVoiceChat.instance;
			if (current != null && Object.op_Implicit((Object)(object)instance) && SemiFunc.NoTextInputsActive())
			{
				if (Pressed(current, musicModeKey.Value))
				{
					musicMode = !musicMode;
					Apply(instance);
					log.LogInfo((object)("Music mode " + (musicMode ? "on" : "off") + "."));
				}
				if (Pressed(current, hearSelfKey.Value))
				{
					hearSelf = !hearSelf;
					Apply(instance);
					log.LogInfo((object)("Hear yourself " + (hearSelf ? "on" : "off") + "."));
				}
				if (Pressed(current, toggleFixKey.Value))
				{
					fixOn = !fixOn;
					Apply(instance);
					log.LogInfo((object)("Fix " + (fixOn ? "ON: music should play smoothly" : "OFF: game's voice filter is back") + "."));
				}
				if (hearSelf)
				{
					instance.OverrideHearSelf(0f, 0.2f);
				}
			}
		}

		private void IndicatorTick()
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: 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_0076: 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_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Expected O, but got Unknown
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)indicator))
			{
				TextMeshProUGUI val = (Object.op_Implicit((Object)(object)EnergyUI.instance) ? ((Component)EnergyUI.instance).GetComponent<TextMeshProUGUI>() : null);
				if (!Object.op_Implicit((Object)(object)val))
				{
					return;
				}
				GameObject val2 = new GameObject("MusicModeIndicator", new Type[1] { typeof(RectTransform) });
				val2.transform.SetParent(((TMP_Text)val).transform, false);
				RectTransform val3 = (RectTransform)val2.transform;
				Vector2 anchorMin = (val3.anchorMax = Vector2.zero);
				val3.anchorMin = anchorMin;
				val3.pivot = new Vector2(0f, 1f);
				val3.sizeDelta = new Vector2(400f, ((TMP_Text)val).fontSize * 0.5f);
				indicator = val2.AddComponent<TextMeshProUGUI>();
				((TMP_Text)indicator).font = ((TMP_Text)val).font;
				((TMP_Text)indicator).fontSize = ((TMP_Text)val).fontSize * 0.4f;
				((TMP_Text)indicator).alignment = (TextAlignmentOptions)257;
				((TMP_Text)indicator).textWrappingMode = (TextWrappingModes)0;
				((TMP_Text)indicator).text = "MUSIC MODE";
				Color val4 = default(Color);
				((Color)(ref val4))..ctor(0.35f, 0.85f, 1f);
				Material val5 = new Material(((TMP_Text)val).fontSharedMaterial);
				val5.SetColor(ShaderUtilities.ID_FaceColor, val4);
				val5.SetColor(ShaderUtilities.ID_GlowColor, new Color(val4.r, val4.g, val4.b, val5.GetColor(ShaderUtilities.ID_GlowColor).a));
				((TMP_Text)indicator).fontSharedMaterial = val5;
			}
			if (((Component)indicator).gameObject.activeSelf != musicMode)
			{
				((Component)indicator).gameObject.SetActive(musicMode);
			}
		}

		private static bool Pressed(Keyboard keyboard, Key key)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			if ((int)key != 0)
			{
				return ((ButtonControl)keyboard[key]).wasPressedThisFrame;
			}
			return false;
		}

		private static void Apply(PlayerVoiceChat voice)
		{
			WebRtcAudioDsp component = ((Component)voice).GetComponent<WebRtcAudioDsp>();
			Recorder component2 = ((Component)voice).GetComponent<Recorder>();
			if (Object.op_Implicit((Object)(object)component))
			{
				bool valueOrDefault = gameDspVad == true;
				if (!gameDspVad.HasValue)
				{
					valueOrDefault = component.VAD;
					gameDspVad = valueOrDefault;
				}
				valueOrDefault = gameAec == true;
				if (!gameAec.HasValue)
				{
					valueOrDefault = component.AEC;
					gameAec = valueOrDefault;
				}
				valueOrDefault = gameNs == true;
				if (!gameNs.HasValue)
				{
					valueOrDefault = component.NoiseSuppression;
					gameNs = valueOrDefault;
				}
				valueOrDefault = gameAgc == true;
				if (!gameAgc.HasValue)
				{
					valueOrDefault = component.AGC;
					gameAgc = valueOrDefault;
				}
				component.VAD = !fixOn && gameDspVad.Value;
				component.AEC = !hearSelf && gameAec.Value;
				component.NoiseSuppression = !musicMode && gameNs.Value;
				component.AGC = !musicMode && gameAgc.Value;
			}
			if (Object.op_Implicit((Object)(object)component2))
			{
				bool valueOrDefault = gameRecorderVad == true;
				if (!gameRecorderVad.HasValue)
				{
					valueOrDefault = component2.VoiceDetection;
					gameRecorderVad = valueOrDefault;
				}
				component2.VoiceDetection = !fixOn && gameRecorderVad.Value;
			}
		}
	}
}