Decompiled source of Audio Listener v0.0.1

OutwardAudioListener.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using OutwardAudioListener.Helpers;
using OutwardAudioListener.Managers;
using SideLoader;
using SideLoader.Helpers;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("OutwardAudioListener")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OutwardAudioListener")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c5450fe0-edcf-483f-b9ea-4b1ef9d36da7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
public class DragParent : MonoBehaviour, IDragHandler, IEventSystemHandler, IBeginDragHandler
{
	private RectTransform parentRectTransform;

	private Vector3 offset;

	private void Start()
	{
		parentRectTransform = ((Component)((Component)this).transform.parent).GetComponent<RectTransform>();
	}

	public void OnBeginDrag(PointerEventData eventData)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		offset = ((Transform)parentRectTransform).position - Input.mousePosition;
	}

	public void OnDrag(PointerEventData eventData)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		((Transform)parentRectTransform).position = Input.mousePosition + offset;
	}
}
namespace OutwardAudioListener
{
	[BepInPlugin("gymmed.outward_audio_listener", "Audio Listener", "0.0.1")]
	public class AudioListener : BaseUnityPlugin
	{
		public const string GUID = "gymmed.outward_audio_listener";

		public const string NAME = "Audio Listener";

		public const string VERSION = "0.0.1";

		public static string prefix = "[GymMed-Audio-Listener]";

		public const string GUI_SHOW = "Audio Listener GUI Show/Hide";

		internal static ManualLogSource Log;

		internal void Awake()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogMessage((object)"Hello world from Audio Listener 0.0.1!");
			CustomKeybindings.AddAction("Audio Listener GUI Show/Hide", (KeybindingsCategory)0, (ControlType)2, (InputType)1);
			SL.OnPacksLoaded += delegate
			{
				try
				{
					GUIManager.Instance.CreateCanvas();
					FillAudiosToGUI();
				}
				catch (Exception ex)
				{
					LogMessage("We encountered a problem: " + ex.Message);
				}
			};
			new Harmony("gymmed.outward_audio_listener").PatchAll();
		}

		internal void Update()
		{
			if (CustomKeybindings.GetKeyDown("Audio Listener GUI Show/Hide"))
			{
				GUIMainCanvasManager gUIMainCanvasManager = GUIManager.Instance?.MainCanvasManager;
				if ((Object)(object)gUIMainCanvasManager == (Object)null)
				{
					LogMessage("Can't access null! Make sure GUI Canvas exist!");
					return;
				}
				if (((Component)gUIMainCanvasManager).gameObject.activeSelf)
				{
					gUIMainCanvasManager.HideCanvas();
					return;
				}
				gUIMainCanvasManager.ShowCanvas();
				((Component)gUIMainCanvasManager).transform.SetAsLastSibling();
			}
		}

		public static void LogMessage(string message)
		{
			Log.LogMessage((object)(prefix + " " + message));
		}

		public static void FillAudiosToGUI()
		{
			try
			{
				GUIMainCanvasManager gUIMainCanvasManager = GUIManager.Instance?.MainCanvasManager;
				if ((Object)(object)gUIMainCanvasManager == (Object)null)
				{
					LogMessage("Can't access null! Make sure GUI Canvas exist!");
					return;
				}
				HashSet<Sounds> allGameSounds = AudioHelper.GetAllGameSounds();
				gUIMainCanvasManager.AvailableSounds = allGameSounds;
				gUIMainCanvasManager.FillAudioData();
				((Component)gUIMainCanvasManager).transform.SetAsLastSibling();
				LogMessage("Success with GUI Canvas creation!");
			}
			catch (Exception ex)
			{
				LogMessage("OutwardAudioListener@FillAudiosToGUI error: " + ex.Message);
			}
		}
	}
}
namespace OutwardAudioListener.Managers
{
	public class GUIMainCanvasManager : MonoBehaviour
	{
		private HashSet<Sounds> _availableSounds = new HashSet<Sounds>();

		private Dictionary<string, Sounds> _audioDictionary = new Dictionary<string, Sounds>();

		private SplitableSoundSource _playingSound;

		private Text _resultText;

		private Button _playSoundButton;

		private Button _stopSoundButton;

		private Button _stopAllSoundsButton;

		private Button _closeButton;

		private Dropdown _chooseAudioDropdown;

		private InputField _audioFilterInput;

		private int availableAudioCount;

		public Text ResultText
		{
			get
			{
				return _resultText;
			}
			set
			{
				_resultText = value;
			}
		}

		public HashSet<Sounds> AvailableSounds
		{
			get
			{
				return _availableSounds;
			}
			set
			{
				_availableSounds = value;
			}
		}

		public Dictionary<string, Sounds> AudioDictionary
		{
			get
			{
				return _audioDictionary;
			}
			set
			{
				_audioDictionary = value;
			}
		}

		public Button PlaySoundButton
		{
			get
			{
				return _playSoundButton;
			}
			set
			{
				_playSoundButton = value;
			}
		}

		public Button StopSoundButton
		{
			get
			{
				return _stopSoundButton;
			}
			set
			{
				_stopSoundButton = value;
			}
		}

		public Button CloseButton
		{
			get
			{
				return _closeButton;
			}
			set
			{
				_closeButton = value;
			}
		}

		public Dropdown ChooseAudioDropdown
		{
			get
			{
				return _chooseAudioDropdown;
			}
			set
			{
				_chooseAudioDropdown = value;
			}
		}

		public InputField AudioFilterInput
		{
			get
			{
				return _audioFilterInput;
			}
			set
			{
				_audioFilterInput = value;
			}
		}

		public int AvailableAudioCount
		{
			get
			{
				return availableAudioCount;
			}
			set
			{
				availableAudioCount = value;
			}
		}

		public SplitableSoundSource PlayingSound
		{
			get
			{
				return _playingSound;
			}
			set
			{
				_playingSound = value;
			}
		}

		public Button StopAllSoundsButton
		{
			get
			{
				return _stopAllSoundsButton;
			}
			set
			{
				_stopAllSoundsButton = value;
			}
		}

		private void Awake()
		{
			try
			{
				Init();
			}
			catch (Exception ex)
			{
				AudioListener.LogMessage("GUIMainCanvasManager error: " + ex.Message);
			}
		}

		public void Init()
		{
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Expected O, but got Unknown
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Expected O, but got Unknown
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Expected O, but got Unknown
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Expected O, but got Unknown
			string text = "Background-Panel/Main-Panel/";
			Transform obj = ((Component)this).transform.Find(text + "Lower-Panel/Result-Text");
			ResultText = ((obj != null) ? ((Component)obj).GetComponent<Text>() : null);
			if ((Object)(object)ResultText == (Object)null)
			{
				AudioListener.LogMessage("Couldn't find Result Text component");
			}
			Transform val = ((Component)this).transform.Find(text + "Header-Panel/");
			if ((Object)(object)val == (Object)null)
			{
				ResultAndLogMessage("Couldn't find GUI header");
			}
			else
			{
				((Component)val).gameObject.AddComponent<DragParent>();
				Transform obj2 = val.Find("Close-Button");
				CloseButton = ((obj2 != null) ? ((Component)obj2).GetComponent<Button>() : null);
				if ((Object)(object)CloseButton == (Object)null)
				{
					ResultAndLogMessage("Couldn't find Close Button component");
				}
				else
				{
					((UnityEvent)CloseButton.onClick).AddListener((UnityAction)delegate
					{
						HideCanvas();
					});
				}
			}
			Transform obj3 = ((Component)this).transform.Find(text + "Middle-Panel/Buttons-Panel/Play-Button");
			PlaySoundButton = ((obj3 != null) ? ((Component)obj3).GetComponent<Button>() : null);
			if ((Object)(object)PlaySoundButton == (Object)null)
			{
				ResultAndLogMessage("Couldn't find play audio button component");
			}
			else
			{
				((UnityEvent)PlaySoundButton.onClick).AddListener((UnityAction)delegate
				{
					HandleOnPlayButtonClick();
				});
			}
			Transform obj4 = ((Component)this).transform.Find(text + "Middle-Panel/Buttons-Panel/Stop-Button");
			StopSoundButton = ((obj4 != null) ? ((Component)obj4).GetComponent<Button>() : null);
			if ((Object)(object)StopSoundButton == (Object)null)
			{
				ResultAndLogMessage("Couldn't find stop audio button component");
			}
			else
			{
				((UnityEvent)StopSoundButton.onClick).AddListener((UnityAction)delegate
				{
					HandleOnStopButtonClick();
				});
			}
			Transform obj5 = ((Component)this).transform.Find(text + "Middle-Panel/StopAll-Button");
			StopAllSoundsButton = ((obj5 != null) ? ((Component)obj5).GetComponent<Button>() : null);
			if ((Object)(object)StopAllSoundsButton == (Object)null)
			{
				ResultAndLogMessage("Couldn't find stop all audio button component");
			}
			else
			{
				((UnityEvent)StopAllSoundsButton.onClick).AddListener((UnityAction)delegate
				{
					HandleOnStopAllButtonClick();
				});
			}
			Transform obj6 = ((Component)this).transform.Find(text + "Middle-Panel/AudioPicker-Panel/AudioPick-Dropdown");
			ChooseAudioDropdown = ((obj6 != null) ? ((Component)obj6).GetComponent<Dropdown>() : null);
			if ((Object)(object)ChooseAudioDropdown == (Object)null)
			{
				ResultAndLogMessage("Couldn't find Audio Dropdown component");
			}
			Transform obj7 = ((Component)this).transform.Find(text + "Middle-Panel/AudioFilter-Panel/AudioFilter-Input");
			AudioFilterInput = ((obj7 != null) ? ((Component)obj7).GetComponent<InputField>() : null);
			if ((Object)(object)AudioFilterInput == (Object)null)
			{
				ResultAndLogMessage("Couldn't find Item Filter Input component");
			}
			else
			{
				((UnityEvent<string>)(object)AudioFilterInput.onEndEdit).AddListener((UnityAction<string>)HandleOnAudioFilterEnd);
			}
		}

		public void HandleOnAudioFilterEnd(string text)
		{
			FilterAudioData(text);
		}

		public void HandleOnStopAllButtonClick()
		{
			try
			{
				AudioHelper.CleanUpAllMusic();
				ResultMessage("Successfully cleaned all music!");
			}
			catch (Exception ex)
			{
				ResultAndLogMessage("Audio listener error: " + ex.Message);
			}
		}

		public void HandleOnStopButtonClick()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Sounds val = (Sounds)0;
				if ((Object)(object)PlayingSound != (Object)null)
				{
					val = PlayingSound.Sound;
					PlayingSound.StopSound();
				}
				ResultMessage("Successfully stopped audio! \nAudio: " + ChooseAudioDropdown.options[ChooseAudioDropdown.value].text + " \n" + $"Sound Enum: GlobalAudioManager.Sounds.{val}");
			}
			catch (Exception ex)
			{
				ResultAndLogMessage("Audio listener error: " + ex.Message);
			}
		}

		public void HandleOnPlayButtonClick()
		{
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)ChooseAudioDropdown == (Object)null)
			{
				ResultAndLogMessage("Couldn't find audio selection dropdown!");
				return;
			}
			if (ChooseAudioDropdown.options == null || ChooseAudioDropdown.options.Count < 1)
			{
				ResultAndLogMessage($"Couldn't generate audio options in dropdown! Available audio count: {AvailableAudioCount}");
				return;
			}
			string text = ChooseAudioDropdown.options[ChooseAudioDropdown.value].text;
			if (!AudioDictionary.TryGetValue(text, out var value))
			{
				ResultAndLogMessage("Tried to play audio without providing sound!");
				return;
			}
			try
			{
				if ((Object)(object)PlayingSound != (Object)null)
				{
					PlayingSound.StopSound();
				}
				PlayingSound = Global.AudioManager.PlaySound(value, 0f, 1f, 1f, 1f, 1f);
				ResultMessage("Successfully started audio! \nAudio: " + ChooseAudioDropdown.options[ChooseAudioDropdown.value].text + " \n" + $"Sound Enum: GlobalAudioManager.Sounds.{value}");
			}
			catch (Exception ex)
			{
				ResultAndLogMessage("Audio listener error: " + ex.Message);
			}
		}

		public void FilterAudioData(string filter)
		{
			string previousValue = "";
			int selectionValue = 0;
			if (ChooseAudioDropdown.options.Count > 0)
			{
				previousValue = ChooseAudioDropdown.options[ChooseAudioDropdown.value].text;
			}
			ResultMessage("Loading Sounds . . .");
			((Selectable)ChooseAudioDropdown).interactable = false;
			((MonoBehaviour)this).StartCoroutine(FilterAudioCoroutine(filter, previousValue, selectionValue));
		}

		private IEnumerator FilterAudioCoroutine(string filter, string previousValue, int selectionValue, int chunkSize = 50)
		{
			List<string> dropdownOptions = new List<string>();
			Dictionary<string, Sounds> localAudioDictionary = new Dictionary<string, Sounds>();
			HashSet<Sounds> hashSet = AvailableSounds;
			if (!string.IsNullOrEmpty(AudioFilterInput.text))
			{
				hashSet = AudioHelper.GetAudioByFilter(AudioFilterInput.text, AvailableSounds);
			}
			int index = 0;
			int availableCount = hashSet.Count;
			foreach (Sounds item in hashSet)
			{
				Sounds current = item;
				string text = ((object)(Sounds)(ref current)).ToString();
				if (AudioHelper.ContainsIgnoreCase(text, filter))
				{
					if (!string.IsNullOrEmpty(previousValue) && string.Equals(text, previousValue, StringComparison.OrdinalIgnoreCase))
					{
						selectionValue = dropdownOptions.Count;
					}
					dropdownOptions.Add(text);
					localAudioDictionary[text] = current;
				}
				index++;
				if (index % chunkSize == 0)
				{
					yield return null;
				}
			}
			AudioDictionary = localAudioDictionary;
			AvailableAudioCount = availableCount;
			yield return ((MonoBehaviour)this).StartCoroutine(FillDropdownCoroutine(ChooseAudioDropdown, dropdownOptions));
			ChooseAudioDropdown.value = selectionValue;
			ChooseAudioDropdown.RefreshShownValue();
			((UnityEvent<int>)(object)ChooseAudioDropdown.onValueChanged).Invoke(ChooseAudioDropdown.value);
			ResultMessage("Finished Loading Sounds!");
			((Selectable)ChooseAudioDropdown).interactable = true;
		}

		private IEnumerator FillDropdownCoroutine(Dropdown dropdown, List<string> options, int chunkSize = 50)
		{
			dropdown.ClearOptions();
			int index = 0;
			while (index < options.Count)
			{
				int num = Mathf.Min(chunkSize, options.Count - index);
				dropdown.AddOptions(options.GetRange(index, num));
				index += num;
				yield return null;
			}
			dropdown.value = 0;
			dropdown.RefreshShownValue();
			((UnityEvent<int>)(object)dropdown.onValueChanged).Invoke(dropdown.value);
			ResultMessage("Finished Loading Audio!");
			((Selectable)dropdown).interactable = true;
		}

		public void FillAudioData()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				ResultMessage("Loading Audio . . .");
				((Selectable)ChooseAudioDropdown).interactable = false;
				AudioListener.LogMessage($"Available Sounds: {AvailableSounds.Count}.");
				List<string> list = new List<string>();
				Dictionary<string, Sounds> dictionary = new Dictionary<string, Sounds>();
				foreach (Sounds availableSound in AvailableSounds)
				{
					Sounds current = availableSound;
					string text = ((object)(Sounds)(ref current)).ToString();
					list.Add(text);
					dictionary[text] = current;
				}
				AudioDictionary = dictionary;
				AvailableAudioCount = dictionary.Count;
				((MonoBehaviour)this).StartCoroutine(FillDropdownCoroutine(ChooseAudioDropdown, list));
			}
			catch (Exception ex)
			{
				AudioListener.LogMessage("GUIMainCanvasManager@FillAudioData error: " + ex.Message);
			}
		}

		public void FillDropdownChoices(Dropdown dropdown, List<string> options)
		{
			dropdown.ClearOptions();
			dropdown.AddOptions(options);
		}

		public void ResultAndLogMessage(string message)
		{
			AudioListener.LogMessage(message);
			ResultMessage(message);
		}

		public void ResultMessage(string message)
		{
			if ((Object)(object)ResultText == (Object)null)
			{
				AudioListener.LogMessage("Tried to display result message: \"" + message + "\" on null UI Result reference.");
			}
			else
			{
				ResultText.text = message;
			}
		}

		public void HideCanvas()
		{
			((Component)this).gameObject.SetActive(false);
			ForceUnlockCursor.RemoveUnlockSource();
		}

		public void ShowCanvas()
		{
			((Component)this).gameObject.SetActive(true);
			ForceUnlockCursor.AddUnlockSource();
		}
	}
	public class GUIManager
	{
		private static GUIManager _instance;

		private Dictionary<int, Enchantment> _enchantmentsDictionary;

		private Canvas _mainCanvas;

		private GUIMainCanvasManager _mainCanvasManager;

		public static GUIManager Instance
		{
			get
			{
				if (_instance == null)
				{
					_instance = new GUIManager();
				}
				return _instance;
			}
		}

		public Dictionary<int, Enchantment> EnchantmentsDictionary
		{
			get
			{
				return _enchantmentsDictionary;
			}
			set
			{
				_enchantmentsDictionary = value;
			}
		}

		public Canvas MainCanvas
		{
			get
			{
				return _mainCanvas;
			}
			set
			{
				_mainCanvas = value;
			}
		}

		public GUIMainCanvasManager MainCanvasManager
		{
			get
			{
				return _mainCanvasManager;
			}
			set
			{
				_mainCanvasManager = value;
			}
		}

		private GUIManager()
		{
		}

		public void CreateCanvas()
		{
			AudioListener.LogMessage("Initalizing Canvas..");
			GameObject fromAssetBundle = AssetsHelper.GetFromAssetBundle<GameObject>("OutwardAudioListener", "outwardaudiolistenerbundle", "OutwardAudioListenerCanvas");
			if ((Object)(object)fromAssetBundle == (Object)null)
			{
				AudioListener.LogMessage("Failed to load outwardaudiolistenerbundle Asset Bundle");
				return;
			}
			MainCanvas = Object.Instantiate<GameObject>(fromAssetBundle).GetComponent<Canvas>();
			MainCanvasManager = ((Component)MainCanvas).gameObject.AddComponent<GUIMainCanvasManager>();
			Object.DontDestroyOnLoad((Object)(object)MainCanvas);
		}
	}
}
namespace OutwardAudioListener.Helpers
{
	public class AssetsHelper
	{
		public static T GetFromAssetBundle<T>(string SLPackName, string AssetBundle, string key) where T : Object
		{
			if (!SL.PacksLoaded)
			{
				return default(T);
			}
			return SL.GetSLPack(SLPackName).AssetBundles[AssetBundle].LoadAsset<T>(key);
		}
	}
	public static class AudioHelper
	{
		public static HashSet<Sounds> GetAllGameSounds()
		{
			//IL_0023: 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_002a: Unknown result type (might be due to invalid IL or missing references)
			HashSet<Sounds> hashSet = new HashSet<Sounds>();
			foreach (Sounds value in Enum.GetValues(typeof(Sounds)))
			{
				hashSet.Add(value);
			}
			return hashSet;
		}

		public static HashSet<Sounds> GetAudioByFilter(string filter, HashSet<Sounds> availableAudio)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			HashSet<Sounds> hashSet = new HashSet<Sounds>();
			foreach (Sounds item in availableAudio)
			{
				Sounds current = item;
				if (ContainsIgnoreCase(((object)(Sounds)(ref current)).ToString(), filter))
				{
					hashSet.Add(current);
				}
			}
			return hashSet;
		}

		public static void CleanUpAllMusic()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			int num;
			for (num = 0; num < GlobalAudioManager.s_musicSources.Values.Count; num++)
			{
				Object.Destroy((Object)(object)((Component)GlobalAudioManager.s_musicSources.Values[num].Source).gameObject);
				GlobalAudioManager.s_musicSources.Remove(GlobalAudioManager.s_musicSources.Keys[num]);
				num--;
			}
		}

		public static bool ContainsIgnoreCase(string source, string toCheck)
		{
			if (source == null)
			{
				return false;
			}
			return source.IndexOf(toCheck, StringComparison.OrdinalIgnoreCase) >= 0;
		}
	}
}