Decompiled source of Menu Music Control v1.0.0

MenuMusicControl.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.EventSystems;
using UnityEngine.Events;
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: AssemblyTitle("Menu Music Control")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Atlas Playbook v0.4.1")]
[assembly: AssemblyProduct("Menu Music Control")]
[assembly: AssemblyCopyright("Copyright © Atlas Playbook v0.4.1 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1655f3c0-5a59-4c70-b63a-1bf820828d48")]
[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 MainMenuMusicVolumeMod;

[BepInPlugin("seeya.MenuMusicControl", "Menu Music Control", "1.0.0")]
public class MainMenuMusicVolumeMod : BaseUnityPlugin
{
	[HarmonyPatch]
	private static class MenuMusicPatches
	{
		[HarmonyPatch(typeof(MenuManager), "Awake")]
		[HarmonyPrefix]
		public static void PreAwake(MenuManager __instance)
		{
			MainMenuMusicVolumeMod mainMenuMusicVolumeMod = Object.FindObjectOfType<MainMenuMusicVolumeMod>();
			if ((Object)(object)mainMenuMusicVolumeMod == (Object)null)
			{
				return;
			}
			AudioSource[] components = ((Component)__instance).GetComponents<AudioSource>();
			AudioSource[] array = components;
			foreach (AudioSource val in array)
			{
				val.volume = mainMenuMusicVolumeMod.volumeConfig.Value;
				if ((Object)(object)mainMenuMusicVolumeMod.menuMusicSource == (Object)null && val.isPlaying)
				{
					mainMenuMusicVolumeMod.menuMusicSource = val;
				}
			}
			Transform val2 = ((Component)__instance).transform.Find("Menu1");
			if (!((Object)(object)val2 != (Object)null))
			{
				return;
			}
			AudioSource component = ((Component)val2).GetComponent<AudioSource>();
			if ((Object)(object)component != (Object)null)
			{
				component.volume = mainMenuMusicVolumeMod.volumeConfig.Value;
				if ((Object)(object)mainMenuMusicVolumeMod.menuMusicSource == (Object)null && component.isPlaying)
				{
					mainMenuMusicVolumeMod.menuMusicSource = component;
				}
			}
		}

		[HarmonyPatch(typeof(MenuManager), "Start")]
		[HarmonyPrefix]
		public static void PreStart(MenuManager __instance)
		{
			MainMenuMusicVolumeMod mainMenuMusicVolumeMod = Object.FindObjectOfType<MainMenuMusicVolumeMod>();
			if ((Object)(object)mainMenuMusicVolumeMod == (Object)null)
			{
				return;
			}
			AudioSource[] components = ((Component)__instance).GetComponents<AudioSource>();
			AudioSource[] array = components;
			foreach (AudioSource val in array)
			{
				val.volume = mainMenuMusicVolumeMod.volumeConfig.Value;
				if ((Object)(object)mainMenuMusicVolumeMod.menuMusicSource == (Object)null)
				{
					mainMenuMusicVolumeMod.menuMusicSource = val;
				}
			}
			Transform val2 = ((Component)__instance).transform.Find("Menu1");
			if (!((Object)(object)val2 != (Object)null))
			{
				return;
			}
			AudioSource component = ((Component)val2).GetComponent<AudioSource>();
			if ((Object)(object)component != (Object)null)
			{
				component.volume = mainMenuMusicVolumeMod.volumeConfig.Value;
				if ((Object)(object)mainMenuMusicVolumeMod.menuMusicSource == (Object)null && component.isPlaying)
				{
					mainMenuMusicVolumeMod.menuMusicSource = component;
				}
			}
		}

		[HarmonyPatch(typeof(MenuManager), "Awake")]
		[HarmonyPostfix]
		public static void PatchMainMenuAwake(MenuManager __instance)
		{
			MainMenuMusicVolumeMod mainMenuMusicVolumeMod = Object.FindObjectOfType<MainMenuMusicVolumeMod>();
			if ((Object)(object)mainMenuMusicVolumeMod == (Object)null || mainMenuMusicVolumeMod.disableSlider.Value)
			{
				return;
			}
			GameObject val = GameObject.Find("Canvas/MenuContainer");
			if (!((Object)(object)val == (Object)null))
			{
				Transform val2 = val.transform.Find("SettingsPanel");
				if (!((Object)(object)val2 == (Object)null))
				{
					mainMenuMusicVolumeMod.SetupVolumeSlider(val2);
				}
			}
		}

		[HarmonyPatch(typeof(AudioSource), "Play", new Type[] { })]
		[HarmonyPrefix]
		public static void PreAudioSourcePlay(AudioSource __instance)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			MainMenuMusicVolumeMod mainMenuMusicVolumeMod = Object.FindObjectOfType<MainMenuMusicVolumeMod>();
			if ((Object)(object)mainMenuMusicVolumeMod == (Object)null)
			{
				return;
			}
			Scene activeScene = SceneManager.GetActiveScene();
			if (((Scene)(ref activeScene)).name == "MainMenu")
			{
				__instance.volume = mainMenuMusicVolumeMod.volumeConfig.Value;
				if (__instance.isPlaying && (Object)(object)mainMenuMusicVolumeMod.menuMusicSource != (Object)(object)__instance)
				{
					mainMenuMusicVolumeMod.menuMusicSource = __instance;
				}
			}
		}
	}

	public ConfigEntry<float> volumeConfig;

	public ConfigEntry<bool> disableSlider;

	private Slider volumeSlider;

	private Harmony harmony;

	private bool mainMenuSliderSetup = false;

	private AudioSource menuMusicSource;

	private AudioMixer audioMixer;

	private float pendingVolume = -1f;

	private bool isDraggingSlider = false;

	public void Awake()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Expected O, but got Unknown
		volumeConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Main Menu Music", "Volume", 0.5f, new ConfigDescription("Main menu music volume (0.0 to 1.0)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		disableSlider = ((BaseUnityPlugin)this).Config.Bind<bool>("Main Menu Music", "DisableSlider", false, "Set to true to hide the volume slider in the main menu settings. Volume can then only be adjusted via this config file.");
		harmony = new Harmony("seeya.MenuMusicControl");
		harmony.PatchAll(typeof(MenuMusicPatches));
		SceneManager.activeSceneChanged += OnSceneChanged;
		audioMixer = Resources.Load<AudioMixer>("MainMixer");
		ApplyVolumeToMenuMusic(volumeConfig.Value);
	}

	private void Start()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		Scene activeScene = SceneManager.GetActiveScene();
		if (((Scene)(ref activeScene)).name == "MainMenu")
		{
			ApplyVolumeToMenuMusic(volumeConfig.Value);
		}
	}

	private void OnDestroy()
	{
		SceneManager.activeSceneChanged -= OnSceneChanged;
	}

	private void OnSceneChanged(Scene oldScene, Scene newScene)
	{
		if (((Scene)(ref newScene)).name != "MainMenu")
		{
			mainMenuSliderSetup = false;
			volumeSlider = null;
			if ((Object)(object)menuMusicSource != (Object)null && volumeConfig.Value == 0f)
			{
				menuMusicSource.Stop();
			}
			else if ((Object)(object)menuMusicSource != (Object)null)
			{
				menuMusicSource.volume = volumeConfig.Value;
			}
		}
		if (((Scene)(ref newScene)).name == "MainMenu" || ((Scene)(ref newScene)).name == "SampleSceneRelay")
		{
			ApplyVolumeToMenuMusic(volumeConfig.Value);
		}
	}

	private void Update()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		Scene activeScene = SceneManager.GetActiveScene();
		if (!(((Scene)(ref activeScene)).name == "MainMenu"))
		{
			return;
		}
		GameObject val = GameObject.Find("Canvas/MenuContainer");
		if ((Object)(object)val != (Object)null)
		{
			Transform val2 = val.transform.Find("SettingsPanel");
			if ((Object)(object)val2 != (Object)null && ((Component)val2).gameObject.activeInHierarchy && !disableSlider.Value)
			{
				if (!mainMenuSliderSetup)
				{
					SetupVolumeSlider(val2);
					mainMenuSliderSetup = true;
				}
			}
			else
			{
				mainMenuSliderSetup = false;
			}
		}
		if (!isDraggingSlider && pendingVolume >= 0f)
		{
			volumeConfig.Value = pendingVolume;
			ApplyVolumeToMenuMusic(pendingVolume);
			pendingVolume = -1f;
		}
		else if (!isDraggingSlider)
		{
			ApplyVolumeToMenuMusic(volumeConfig.Value);
		}
	}

	private void SetupVolumeSlider(Transform settingsPanel)
	{
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_022b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0230: Unknown result type (might be due to invalid IL or missing references)
		//IL_0233: Unknown result type (might be due to invalid IL or missing references)
		//IL_023a: Expected O, but got Unknown
		//IL_0262: Unknown result type (might be due to invalid IL or missing references)
		//IL_0267: Unknown result type (might be due to invalid IL or missing references)
		//IL_026a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0271: Expected O, but got Unknown
		//IL_02f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0302: Expected O, but got Unknown
		if (disableSlider.Value)
		{
			return;
		}
		Transform val = settingsPanel.Find("VolumeSetting");
		if ((Object)(object)val != (Object)null)
		{
			Object.Destroy((Object)(object)((Component)val).gameObject);
		}
		Transform val2 = settingsPanel.Find("BrightnessSetting");
		if ((Object)(object)val2 == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"BrightnessSetting not found in SettingsPanel!");
			return;
		}
		GameObject val3 = Object.Instantiate<GameObject>(((Component)val2).gameObject, settingsPanel);
		((Object)val3).name = "VolumeSetting";
		RectTransform component = val3.GetComponent<RectTransform>();
		RectTransform component2 = ((Component)val2).GetComponent<RectTransform>();
		Vector2 anchoredPosition = component2.anchoredPosition;
		component.anchoredPosition = new Vector2(anchoredPosition.x, anchoredPosition.y - 50f);
		Transform val4 = val3.transform.Find("Text (1)");
		if ((Object)(object)val4 != (Object)null)
		{
			TMP_Text component3 = ((Component)val4).GetComponent<TMP_Text>();
			if ((Object)(object)component3 != (Object)null)
			{
				component3.text = "Menu Volume:";
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"TMP_Text component not found on Text (1)!");
			}
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Text (1) GameObject not found in VolumeSetting!");
		}
		Transform val5 = val3.transform.Find("Slider");
		if ((Object)(object)val5 != (Object)null)
		{
			volumeSlider = ((Component)val5).GetComponent<Slider>();
			if ((Object)(object)volumeSlider != (Object)null)
			{
				int persistentEventCount = ((UnityEventBase)volumeSlider.onValueChanged).GetPersistentEventCount();
				if (persistentEventCount > 0)
				{
					((UnityEventBase)volumeSlider.onValueChanged).SetPersistentListenerState(0, (UnityEventCallState)0);
				}
				((UnityEventBase)volumeSlider.onValueChanged).RemoveAllListeners();
				volumeSlider.minValue = 0f;
				volumeSlider.maxValue = 100f;
				volumeSlider.wholeNumbers = false;
				volumeSlider.value = volumeConfig.Value * 100f;
				ApplyVolumeToMenuMusic(volumeConfig.Value);
				EventTrigger val6 = ((Component)volumeSlider).gameObject.AddComponent<EventTrigger>();
				Entry val7 = new Entry
				{
					eventID = (EventTriggerType)13
				};
				((UnityEvent<BaseEventData>)(object)val7.callback).AddListener((UnityAction<BaseEventData>)delegate
				{
					isDraggingSlider = true;
				});
				val6.triggers.Add(val7);
				Entry val8 = new Entry
				{
					eventID = (EventTriggerType)14
				};
				((UnityEvent<BaseEventData>)(object)val8.callback).AddListener((UnityAction<BaseEventData>)delegate
				{
					isDraggingSlider = false;
				});
				val6.triggers.Add(val8);
				((UnityEvent<float>)(object)volumeSlider.onValueChanged).AddListener((UnityAction<float>)delegate(float value)
				{
					ApplyVolumeToMenuMusic(pendingVolume = value / 100f);
				});
				Transform val9 = settingsPanel.Find("Confirm");
				if ((Object)(object)val9 != (Object)null)
				{
					Button component4 = ((Component)val9).GetComponent<Button>();
					if ((Object)(object)component4 != (Object)null)
					{
						((UnityEvent)component4.onClick).AddListener((UnityAction)delegate
						{
							if ((Object)(object)volumeSlider != (Object)null)
							{
								volumeSlider.value = volumeConfig.Value * 100f;
								ApplyVolumeToMenuMusic(volumeConfig.Value);
								if ((Object)(object)menuMusicSource != (Object)null && volumeConfig.Value == 0f)
								{
									menuMusicSource.Stop();
								}
							}
						});
					}
					else
					{
						((BaseUnityPlugin)this).Logger.LogError((object)"Confirm GameObject found, but no Button component!");
					}
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Confirm GameObject not found in SettingsPanel!");
				}
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Slider component not found in VolumeSetting!");
			}
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Slider GameObject not found in VolumeSetting!");
		}
	}

	private void ApplyVolumeToMenuMusic(float volume)
	{
		bool flag = false;
		GameObject val = GameObject.Find("MenuManager");
		if ((Object)(object)val != (Object)null)
		{
			Transform val2 = val.transform.Find("Menu1");
			if ((Object)(object)val2 != (Object)null)
			{
				AudioSource component = ((Component)val2).GetComponent<AudioSource>();
				if ((Object)(object)component != (Object)null)
				{
					component.volume = volume;
					flag = true;
					if ((Object)(object)menuMusicSource == (Object)null || (component.isPlaying && (Object)(object)menuMusicSource != (Object)(object)component))
					{
						menuMusicSource = component;
					}
				}
			}
			AudioSource component2 = val.GetComponent<AudioSource>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.volume = volume;
				flag = true;
				if ((Object)(object)menuMusicSource == (Object)null && component2.isPlaying)
				{
					menuMusicSource = component2;
				}
			}
		}
		GameObject[] array = (GameObject[])(object)new GameObject[4]
		{
			GameObject.Find("Music"),
			GameObject.Find("AudioManager"),
			GameObject.Find("SoundManager"),
			GameObject.Find("MenuMusic")
		};
		GameObject[] array2 = array;
		foreach (GameObject val3 in array2)
		{
			if (!((Object)(object)val3 != (Object)null))
			{
				continue;
			}
			AudioSource component3 = val3.GetComponent<AudioSource>();
			if ((Object)(object)component3 != (Object)null)
			{
				component3.volume = volume;
				flag = true;
				if ((Object)(object)menuMusicSource == (Object)null && component3.isPlaying)
				{
					menuMusicSource = component3;
				}
			}
		}
		if ((Object)(object)menuMusicSource != (Object)null)
		{
			menuMusicSource.volume = volume;
			flag = true;
		}
		AudioSource[] array3 = Object.FindObjectsOfType<AudioSource>();
		AudioSource[] array4 = array3;
		foreach (AudioSource val4 in array4)
		{
			if (val4.isPlaying && (Object)(object)val4 != (Object)(object)menuMusicSource)
			{
				val4.volume = volume;
				flag = true;
			}
		}
		if ((Object)(object)audioMixer != (Object)null)
		{
			float num = Mathf.Lerp(-80f, 0f, volume);
			if (audioMixer.SetFloat("MusicVolume", num))
			{
				flag = true;
			}
			else if (audioMixer.SetFloat("MasterVolume", num))
			{
				flag = true;
			}
			else if (audioMixer.SetFloat("MenuMusicVolume", num))
			{
				flag = true;
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Failed to apply volume to any AudioMixer group");
			}
		}
		if (!flag)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"No relevant AudioSource or AudioMixer found to apply volume");
		}
	}
}