Decompiled source of NoBackgroundAudioUnofficialUpdate v1.0.0

plugins/NoBackgroundAudioUnofficialUpdate.dll

Decompiled 10 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using BepInEx;
using NoBackgroundAudio.Extensions;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("NoBackgroundAudioUnofficialUpdate")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("ReviOS 11 23.10")]
[assembly: AssemblyProduct("NoBackgroundAudioUnofficialUpdate")]
[assembly: AssemblyCopyright("Copyright © ReviOS 11 23.10 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e8b64329-b3eb-40c6-b4a8-d6a991eb5e1d")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NoBackgroundAudio
{
	[BepInPlugin("NoBackgroundAudio", "NoBackgroundAudio", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin NoBackgroundAudio loaded");
			Start();
		}

		private async void Start()
		{
			while ((Object)(object)IngamePlayerSettings.Instance == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Waiting for IngamePlayerSettings to be available...");
				await Task.Delay(1000);
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"IngamePlayerSettings is now available. Adding focus events.");
			((Component)IngamePlayerSettings.Instance).gameObject.AddOnApplicationFocusEvents(OnApplicationFocus);
		}

		private void OnApplicationFocus(bool hasFocus)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Application focus changed. Has focus: {hasFocus}");
			if (!hasFocus)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Application lost focus. Muting audio and microphone.");
				MuteAudio(mute: true);
				MuteMicrophone(mute: true);
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Application gained focus. Unmuting audio and microphone.");
				MuteAudio(mute: false);
				MuteMicrophone(mute: false);
			}
		}

		private void MuteAudio(bool mute)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Setting AudioListener.pause to {mute}");
			AudioListener.pause = mute;
		}

		private void MuteMicrophone(bool mute)
		{
			string[] devices = Microphone.devices;
			foreach (string text in devices)
			{
				if (Microphone.IsRecording(text))
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Microphone '" + text + "' is currently recording. Stopping recording."));
					Microphone.End(text);
				}
			}
			if (!mute)
			{
				string text2 = ((Microphone.devices.Length != 0) ? Microphone.devices[0] : null);
				if (!string.IsNullOrEmpty(text2) && !Microphone.IsRecording(text2))
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Microphone '" + text2 + "' is not recording. Starting recording."));
					Microphone.Start(text2, false, 1, 44100);
				}
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "NoBackgroundAudio";

		public const string PLUGIN_NAME = "NoBackgroundAudio";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace NoBackgroundAudio.Extensions
{
	public static class GameObjectExtensions
	{
		public static void AddOnApplicationFocusEvents(this GameObject gameObject, Action<bool> onApplicationFocus)
		{
			ApplicationFocusHandler applicationFocusHandler = gameObject.AddComponent<ApplicationFocusHandler>();
			applicationFocusHandler.OnApplicationFocusEvent += onApplicationFocus;
		}
	}
	public class ApplicationFocusHandler : MonoBehaviour
	{
		public event Action<bool> OnApplicationFocusEvent;

		private void OnApplicationFocus(bool hasFocus)
		{
			this.OnApplicationFocusEvent?.Invoke(hasFocus);
		}
	}
}