Decompiled source of FehuSoundtrackPlayer v420.101.3

plugins/FehuSoundtrackPlayer/FehuSoundtrackPlayer.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
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 UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("FehuSoundtrackPlayer")]
[assembly: AssemblyProduct("FehuSoundtrackPlayer")]
[assembly: AssemblyCompany("Black Viking")]
[assembly: AssemblyCopyright("Black Viking")]
[assembly: AssemblyFileVersion("420.2.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("420.2.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 Fehu
{
	[BepInPlugin("com.forteca.fehusoundtrackplayer", "FehuSoundtrackPlayer", "420.2.2")]
	[BepInProcess("valheim.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.forteca.fehusoundtrackplayer";

		public const string PluginName = "FehuSoundtrackPlayer";

		public const string PluginVersion = "420.2.2";

		private Harmony _harmony;

		internal static Plugin Instance { get; private set; }

		internal static string DataRoot => Path.Combine(Paths.ConfigPath, "FehuSoundtrackPlayer");

		internal ConfigFile RuntimeConfig { get; private set; }

		internal SoundtrackController Controller { get; private set; }

		private void Awake()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			Instance = this;
			Directory.CreateDirectory(DataRoot);
			RuntimeConfig = new ConfigFile(Path.Combine(DataRoot, "com.forteca.fehusoundtrackplayer.cfg"), true);
			Controller = ((Component)this).GetComponent<SoundtrackController>() ?? ((Component)this).gameObject.AddComponent<SoundtrackController>();
			Controller.Initialize(RuntimeConfig, ((BaseUnityPlugin)this).Logger);
			_harmony = new Harmony("com.forteca.fehusoundtrackplayer");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[FehuSoundtrackPlayer] 420.2.2 ready");
		}

		private void OnDestroy()
		{
			Controller?.Shutdown();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Instance = null;
		}
	}
	[HarmonyPatch]
	internal static class MusicManStartPatch
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			return from method in AccessTools.GetDeclaredMethods(typeof(MusicMan))
				where method.Name == "StartMusic"
				select method;
		}

		private static bool Prefix()
		{
			if (!((Object)(object)Plugin.Instance == (Object)null) && !((Object)(object)Plugin.Instance.Controller == (Object)null))
			{
				return !Plugin.Instance.Controller.OwnsPlayback;
			}
			return true;
		}
	}
	internal sealed class TrackInfo
	{
		internal string Path;

		internal string Group;

		internal string Title;
	}
	internal sealed class SoundtrackController : MonoBehaviour
	{
		private static readonly FieldInfo MusicSourceField = typeof(MusicMan).GetField("m_musicSource", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly FieldInfo CharacterScreenField = FindFejdField("m_characterSelectScreen");

		private static readonly FieldInfo SettingsField = FindFejdField("m_settingsPopup");

		private static readonly FieldInfo[] FejdSubmenuFields = new FieldInfo[11]
		{
			FindFejdField("m_startGamePanel"),
			FindFejdField("m_selectCharacterPanel"),
			FindFejdField("m_newCharacterPanel"),
			FindFejdField("m_characterSelectScreen"),
			FindFejdField("m_serverListPanel"),
			FindFejdField("m_createWorldPanel"),
			FindFejdField("m_worldListPanel"),
			FindFejdField("m_settingsPopup"),
			FindFejdField("m_settingsPanel"),
			FindFejdField("m_creditsPanel"),
			FindFejdField("m_manageSavesMenu")
		};

		private static readonly Color PrimarySilver = new Color(0.94f, 0.95f, 0.97f, 1f);

		private static readonly Color SecondarySilver = new Color(0.82f, 0.84f, 0.88f, 1f);

		private static readonly Color TextShadow = new Color(0.035f, 0.04f, 0.05f, 0.9f);

		private readonly List<TrackInfo> _tracks = new List<TrackInfo>();

		private ConfigEntry<bool> _enabled;

		private ConfigEntry<bool> _showMenu;

		private ConfigEntry<bool> _showGame;

		private ConfigEntry<string> _musicRoot;

		private ConfigEntry<bool> _recursive;

		private ConfigEntry<string> _extensions;

		private ConfigEntry<bool> _showTrackName;

		private ConfigEntry<float> _defaultVolume;

		private ConfigEntry<bool> _autoPlay;

		private ConfigEntry<int> _panelOffsetX;

		private ConfigEntry<int> _panelOffsetY;

		private ManualLogSource _log;

		private AudioSource _source;

		private AudioSource _pausedNativeSource;

		private UnityWebRequest _loadRequest;

		private int _loadGeneration;

		private int _currentIndex = -1;

		private bool _libraryOpen;

		private bool _minimized;

		private bool _manualPause;

		private bool _draggingPanel;

		private Vector2 _dragOffset;

		private Vector2 _scroll;

		private GUIStyle _barStyle;

		private GUIStyle _buttonStyle;

		private GUIStyle _labelStyle;

		private GUIStyle _groupStyle;

		private bool _initialized;

		internal bool OwnsPlayback { get; private set; }

		private static FieldInfo FindFejdField(string fieldName)
		{
			return typeof(FejdStartup).GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		}

		internal void Initialize(ConfigFile config, ManualLogSource log)
		{
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Expected O, but got Unknown
			if (!_initialized)
			{
				_initialized = true;
				_log = log;
				_enabled = config.Bind<bool>("General", "Enabled", true, "Enable the soundtrack player.");
				_showMenu = config.Bind<bool>("General", "ShowInMainMenu", true, "Show underneath the Valheim logo.");
				_showGame = config.Bind<bool>("General", "ShowInGame", true, "Keep the same player available in game.");
				_musicRoot = config.Bind<string>("Library", "MusicRoot", Plugin.DataRoot, "Path relative to the Valheim directory, or an absolute path.");
				_recursive = config.Bind<bool>("Library", "RecursiveSearch", true, "Discover MP3 and OGG files in nested folders.");
				_extensions = config.Bind<string>("Library", "SupportedExtensions", ".mp3,.ogg", "Comma-separated audio extensions.");
				_showTrackName = config.Bind<bool>("Player", "ShowTrackName", true, "Show the selected file name.");
				_defaultVolume = config.Bind<float>("Player", "DefaultVolume", 0.8f, new ConfigDescription("Initial player volume.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
				_autoPlay = config.Bind<bool>("Player", "AutoPlay", false, "Start playback automatically after entering the menu. Disabled by default; playback is user-controlled.");
				_panelOffsetX = config.Bind<int>("Layout", "PanelOffsetX", 0, "Horizontal soundtrack-panel offset. Hold Ctrl and drag with the left mouse button.");
				_panelOffsetY = config.Bind<int>("Layout", "PanelOffsetY", 0, "Vertical soundtrack-panel offset. Hold Ctrl and drag with the left mouse button.");
				_source = ((Component)this).GetComponent<AudioSource>() ?? ((Component)this).gameObject.AddComponent<AudioSource>();
				_source.playOnAwake = false;
				_source.loop = false;
				_source.volume = _defaultVolume.Value;
				_source.spatialBlend = 0f;
				_source.dopplerLevel = 0f;
				_source.bypassEffects = true;
				_source.bypassListenerEffects = true;
				_source.bypassReverbZones = true;
				ScanLibrary();
				if (_autoPlay.Value && _tracks.Count > 0)
				{
					PlayIndex(0);
				}
			}
		}

		private void ScanLibrary()
		{
			_tracks.Clear();
			string text = ResolveMusicRoot(_musicRoot.Value);
			if (!Directory.Exists(text))
			{
				_log.LogWarning((object)("[FehuSoundtrackPlayer] Music root does not exist: " + text));
				return;
			}
			HashSet<string> hashSet = new HashSet<string>(from x in _extensions.Value.Split(new char[1] { ',' })
				select x.Trim().ToLowerInvariant() into x
				where x.Length > 0
				select x);
			try
			{
				foreach (string item in EnumerateAudioFiles(text, _recursive.Value).OrderBy<string, string>((string x) => x, StringComparer.OrdinalIgnoreCase))
				{
					if (hashSet.Contains(Path.GetExtension(item).ToLowerInvariant()))
					{
						string directoryName = Path.GetDirectoryName(item.Substring(text.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
						string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(item);
						string text2 = (string.IsNullOrWhiteSpace(directoryName) ? PrefixGroup(fileNameWithoutExtension) : directoryName.Replace(Path.DirectorySeparatorChar, '/'));
						_tracks.Add(new TrackInfo
						{
							Path = item,
							Group = text2,
							Title = fileNameWithoutExtension
						});
					}
				}
				_log.LogInfo((object)("[FehuSoundtrackPlayer] Discovered " + _tracks.Count + " track(s) under " + text));
			}
			catch (Exception ex)
			{
				_log.LogWarning((object)("[FehuSoundtrackPlayer] Library scan stopped safely: " + ex.Message));
			}
		}

		private IEnumerable<string> EnumerateAudioFiles(string root, bool recursive)
		{
			Stack<string> pending = new Stack<string>();
			pending.Push(root);
			while (pending.Count > 0)
			{
				string directory = pending.Pop();
				string[] files;
				try
				{
					files = Directory.GetFiles(directory);
				}
				catch (Exception ex)
				{
					_log.LogWarning((object)("[FehuSoundtrackPlayer] Skipping inaccessible folder '" + directory + "': " + ex.Message));
					continue;
				}
				for (int i = 0; i < files.Length; i++)
				{
					yield return files[i];
				}
				if (recursive)
				{
					string[] directories;
					try
					{
						directories = Directory.GetDirectories(directory);
					}
					catch (Exception ex2)
					{
						_log.LogWarning((object)("[FehuSoundtrackPlayer] Cannot enumerate subfolders of '" + directory + "': " + ex2.Message));
						continue;
					}
					for (int num = directories.Length - 1; num >= 0; num--)
					{
						pending.Push(directories[num]);
					}
				}
			}
		}

		private static string ResolveMusicRoot(string configured)
		{
			if (string.Equals((configured ?? string.Empty).Replace('\\', '/'), "BepInEx/config/expand_world", StringComparison.OrdinalIgnoreCase))
			{
				return Plugin.DataRoot;
			}
			if (Path.IsPathRooted(configured ?? string.Empty))
			{
				return Path.GetFullPath(configured);
			}
			string text = (configured ?? string.Empty).Replace('/', Path.DirectorySeparatorChar);
			string text2 = text;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			if (text2.StartsWith("BepInEx" + directorySeparatorChar, StringComparison.OrdinalIgnoreCase))
			{
				string text3 = text;
				directorySeparatorChar = Path.DirectorySeparatorChar;
				text = text3.Substring(("BepInEx" + directorySeparatorChar).Length);
				return Path.GetFullPath(Path.Combine(Paths.BepInExRootPath, text));
			}
			return Path.GetFullPath(Path.Combine(Paths.GameRootPath, text));
		}

		private static string PrefixGroup(string title)
		{
			if (!string.IsNullOrEmpty(title) && title[0] == '[')
			{
				int num = title.IndexOf(']');
				if (num > 1)
				{
					return title.Substring(1, num - 1).ToUpperInvariant();
				}
			}
			return "OTHER";
		}

		private void Update()
		{
			if (!_enabled.Value)
			{
				return;
			}
			if (OwnsPlayback)
			{
				AudioSource val = NativeMusicSource();
				if ((Object)(object)val != (Object)null && (Object)(object)val != (Object)(object)_source && val.isPlaying)
				{
					val.Pause();
				}
			}
			if (_autoPlay.Value && OwnsPlayback && !_manualPause && _loadRequest == null && (Object)(object)_source.clip != (Object)null && !_source.isPlaying)
			{
				Next();
			}
			if (_loadRequest != null && _loadRequest.isDone)
			{
				FinishLoad();
			}
		}

		private void OnGUI()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			if (_enabled.Value && ShouldShow())
			{
				EnsureStyles();
				float num = Mathf.Min((float)Screen.width / 1920f, (float)Screen.height / 1080f);
				Matrix4x4 matrix = GUI.matrix;
				GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(num, num, 1f));
				float num2 = (float)Screen.width / num;
				float num3 = (float)Screen.height / num;
				float num4 = (_minimized ? 176f : 420f);
				Rect val = default(Rect);
				((Rect)(ref val))..ctor((num2 - num4) * 0.5f, 6f, num4, 28f);
				Rect bar = val;
				((Rect)(ref bar)).x = Mathf.Clamp(((Rect)(ref bar)).x + (float)_panelOffsetX.Value, 0f, Mathf.Max(0f, num2 - ((Rect)(ref bar)).width));
				((Rect)(ref bar)).y = Mathf.Clamp(((Rect)(ref bar)).y + (float)_panelOffsetY.Value, 0f, Mathf.Max(0f, num3 - ((Rect)(ref bar)).height));
				HandlePanelDrag(ref bar, val, num2, num3);
				DrawBar(bar);
				if (_libraryOpen)
				{
					DrawLibrary(new Rect(((Rect)(ref bar)).x, ((Rect)(ref bar)).yMax + 4f, ((Rect)(ref bar)).width, 220f));
				}
				GUI.matrix = matrix;
			}
		}

		private void HandlePanelDrag(ref Rect bar, Rect canonicalBar, float logicalWidth, float logicalHeight)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Invalid comparison between Unknown and I4
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: 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)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Invalid comparison between Unknown and I4
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Invalid comparison between Unknown and I4
			Event current = Event.current;
			bool flag = current.control || Input.GetKey((KeyCode)306) || Input.GetKey((KeyCode)305);
			if ((int)current.type == 0 && current.button == 0 && flag && ((Rect)(ref bar)).Contains(current.mousePosition))
			{
				_draggingPanel = true;
				_dragOffset = current.mousePosition - new Vector2(((Rect)(ref bar)).x, ((Rect)(ref bar)).y);
				current.Use();
			}
			if (_draggingPanel && (int)current.type == 3 && flag)
			{
				Vector2 val = current.mousePosition - _dragOffset;
				((Rect)(ref bar)).x = Mathf.Clamp(val.x, 0f, Mathf.Max(0f, logicalWidth - ((Rect)(ref bar)).width));
				((Rect)(ref bar)).y = Mathf.Clamp(val.y, 0f, Mathf.Max(0f, logicalHeight - ((Rect)(ref bar)).height));
				_panelOffsetX.Value = Mathf.RoundToInt(((Rect)(ref bar)).x - ((Rect)(ref canonicalBar)).x);
				_panelOffsetY.Value = Mathf.RoundToInt(((Rect)(ref bar)).y - ((Rect)(ref canonicalBar)).y);
				current.Use();
			}
			if (_draggingPanel && ((int)current.type == 1 || !flag))
			{
				Plugin.Instance.RuntimeConfig.Save();
				_draggingPanel = false;
				if ((int)current.type == 1)
				{
					current.Use();
				}
			}
		}

		private bool ShouldShow()
		{
			if (IsMainMenu())
			{
				if (_showMenu.Value)
				{
					return !IsAnyFejdSubmenuOpen();
				}
				return false;
			}
			return _showGame.Value;
		}

		private static bool IsAnyFejdSubmenuOpen()
		{
			for (int i = 0; i < FejdSubmenuFields.Length; i++)
			{
				if (IsFejdScreenOpen(FejdSubmenuFields[i]))
				{
					return true;
				}
			}
			return false;
		}

		private static bool IsMainMenu()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			Scene activeScene = SceneManager.GetActiveScene();
			string name = ((Scene)(ref activeScene)).name;
			if (!string.Equals(name, "start", StringComparison.OrdinalIgnoreCase))
			{
				return string.Equals(name, "main", StringComparison.OrdinalIgnoreCase);
			}
			return true;
		}

		private static bool IsFejdScreenOpen(FieldInfo field)
		{
			if ((Object)(object)FejdStartup.instance == (Object)null || field == null)
			{
				return false;
			}
			object? value = field.GetValue(FejdStartup.instance);
			GameObject val = (GameObject)((value is GameObject) ? value : null);
			if ((Object)(object)val != (Object)null)
			{
				return val.activeInHierarchy;
			}
			return false;
		}

		private void DrawBar(Rect bar)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_022e: 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_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f6: Unknown result type (might be due to invalid IL or missing references)
			DrawPanel(bar, parchmentBody: false);
			float num = ((Rect)(ref bar)).y + 2f;
			string text = (_showTrackName.Value ? ("Fehu Soundtrack — " + ((_currentIndex >= 0 && _currentIndex < _tracks.Count) ? _tracks[_currentIndex].Title : "Stopped")) : "Fehu Soundtrack");
			if (_minimized)
			{
				DrawShadowedLabel(new Rect(((Rect)(ref bar)).x + 9f, num, 132f, 24f), "Fehu Soundtrack", _labelStyle, PrimarySilver);
				if (DrawPlayerButton(new Rect(((Rect)(ref bar)).xMax - 30f, num, 25f, 24f), "+"))
				{
					_minimized = false;
				}
				return;
			}
			DrawShadowedLabel(new Rect(((Rect)(ref bar)).x + 9f, num, 142f, 24f), text, _labelStyle, BarTextColor());
			float num2 = ((Rect)(ref bar)).x + 154f;
			if (DrawPlayerButton(new Rect(num2, num, 25f, 24f), "|<", Color.black))
			{
				Previous();
			}
			num2 += 27f;
			if (DrawPlayerButton(new Rect(num2, num, 27f, 24f), _source.isPlaying ? "II" : ">", Color.black))
			{
				TogglePlay();
			}
			num2 += 29f;
			if (DrawPlayerButton(new Rect(num2, num, 25f, 24f), ">|", Color.black))
			{
				Next();
			}
			num2 += 28f;
			if (DrawPlayerButton(new Rect(num2, num, 25f, 24f), "↻", Color.black))
			{
				_autoPlay.Value = !_autoPlay.Value;
			}
			num2 += 30f;
			_source.volume = GUI.HorizontalSlider(new Rect(num2, num + 7f, 58f, 12f), _source.volume, 0f, 1f);
			num2 += 63f;
			if (DrawPlayerButton(new Rect(num2, num, 36f, 24f), "LIB", Color.black))
			{
				_libraryOpen = !_libraryOpen;
			}
			if (DrawPlayerButton(new Rect(((Rect)(ref bar)).xMax - 58f, num, 25f, 24f), "−", Color.black))
			{
				_minimized = true;
				_libraryOpen = false;
			}
			if (DrawPlayerButton(new Rect(((Rect)(ref bar)).xMax - 30f, num, 25f, 24f), "×", Color.black))
			{
				_enabled.Value = false;
				_libraryOpen = false;
			}
		}

		private void DrawLibrary(Rect panel)
		{
			//IL_0001: 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_00ac: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: 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_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			DrawPanel(panel, parchmentBody: true);
			Rect val = default(Rect);
			((Rect)(ref val))..ctor(((Rect)(ref panel)).x + 14f, ((Rect)(ref panel)).y + 14f, ((Rect)(ref panel)).width - 28f, ((Rect)(ref panel)).height - 28f);
			float num = Mathf.Max(((Rect)(ref val)).height, (float)_tracks.Count * 30f + (float)_tracks.Select((TrackInfo t) => t.Group).Distinct<string>(StringComparer.OrdinalIgnoreCase).Count() * 26f + 8f);
			_scroll = GUI.BeginScrollView(val, _scroll, new Rect(0f, 0f, ((Rect)(ref val)).width - 20f, num), false, true);
			float num2 = 0f;
			string text = null;
			for (int num3 = 0; num3 < _tracks.Count; num3++)
			{
				TrackInfo trackInfo = _tracks[num3];
				if (!string.Equals(text, trackInfo.Group, StringComparison.OrdinalIgnoreCase))
				{
					text = trackInfo.Group;
					DrawShadowedLabel(new Rect(6f, num2, ((Rect)(ref val)).width - 32f, 24f), text, _groupStyle, PrimarySilver);
					num2 += 25f;
				}
				if (DrawPlayerButton(new Rect(12f, num2, ((Rect)(ref val)).width - 40f, 27f), trackInfo.Title, (Color?)new Color(0.1f, 0.055f, 0.02f, 1f), (FontStyle?)(FontStyle)0))
				{
					PlayIndex(num3);
					_libraryOpen = false;
				}
				num2 += 30f;
			}
			GUI.EndScrollView();
		}

		private void Previous()
		{
			if (_tracks.Count > 0)
			{
				PlayIndex((_currentIndex <= 0) ? (_tracks.Count - 1) : (_currentIndex - 1));
			}
		}

		private void Next()
		{
			if (_tracks.Count > 0)
			{
				PlayIndex((_currentIndex + 1) % _tracks.Count);
			}
		}

		private void TogglePlay()
		{
			if ((Object)(object)_source.clip == (Object)null)
			{
				if (_tracks.Count > 0)
				{
					PlayIndex((_currentIndex >= 0) ? _currentIndex : 0);
				}
				return;
			}
			TakeOwnership();
			if (_source.isPlaying)
			{
				_manualPause = true;
				_source.Pause();
			}
			else
			{
				_manualPause = false;
				_source.Play();
			}
		}

		private void PlayIndex(int index)
		{
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			if (index >= 0 && index < _tracks.Count && File.Exists(_tracks[index].Path))
			{
				TakeOwnership();
				_manualPause = false;
				_currentIndex = index;
				_source.Stop();
				if ((Object)(object)_source.clip != (Object)null)
				{
					Object.Destroy((Object)(object)_source.clip);
				}
				_source.clip = null;
				UnityWebRequest loadRequest = _loadRequest;
				if (loadRequest != null)
				{
					loadRequest.Abort();
				}
				UnityWebRequest loadRequest2 = _loadRequest;
				if (loadRequest2 != null)
				{
					loadRequest2.Dispose();
				}
				AudioType val = (AudioType)(string.Equals(Path.GetExtension(_tracks[index].Path), ".ogg", StringComparison.OrdinalIgnoreCase) ? 14 : 13);
				_loadRequest = UnityWebRequestMultimedia.GetAudioClip(new Uri(_tracks[index].Path).AbsoluteUri, val);
				_loadGeneration++;
				_loadRequest.SendWebRequest();
			}
		}

		private void FinishLoad()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Invalid comparison between Unknown and I4
			UnityWebRequest loadRequest = _loadRequest;
			_loadRequest = null;
			try
			{
				if ((int)loadRequest.result != 1)
				{
					_log.LogWarning((object)("[FehuSoundtrackPlayer] Track skipped: " + loadRequest.error));
					return;
				}
				AudioClip content = DownloadHandlerAudioClip.GetContent(loadRequest);
				if ((Object)(object)content == (Object)null)
				{
					_log.LogWarning((object)"[FehuSoundtrackPlayer] Decoder returned no clip");
					return;
				}
				_source.clip = content;
				_manualPause = false;
				_source.Play();
			}
			catch (Exception ex)
			{
				_log.LogWarning((object)("[FehuSoundtrackPlayer] Track skipped safely: " + ex.Message));
			}
			finally
			{
				loadRequest.Dispose();
			}
		}

		private void TakeOwnership()
		{
			if (!OwnsPlayback)
			{
				OwnsPlayback = true;
				_pausedNativeSource = NativeMusicSource();
				if ((Object)(object)_pausedNativeSource != (Object)null && _pausedNativeSource.isPlaying)
				{
					_pausedNativeSource.Pause();
				}
			}
		}

		private static AudioSource NativeMusicSource()
		{
			if ((Object)(object)MusicMan.instance == (Object)null || MusicSourceField == null)
			{
				return null;
			}
			object? value = MusicSourceField.GetValue(MusicMan.instance);
			return (AudioSource)((value is AudioSource) ? value : null);
		}

		private Color BarTextColor()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			return new Color(0.9f, 0.67f, 0.16f, 1f);
		}

		private void EnsureStyles()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: 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)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			//IL_007f: 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_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Expected O, but got Unknown
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			if (_barStyle == null)
			{
				_barStyle = new GUIStyle(GUI.skin.box);
				_buttonStyle = new GUIStyle(GUI.skin.button)
				{
					fontSize = 10,
					fontStyle = (FontStyle)1,
					alignment = (TextAnchor)4
				};
				_labelStyle = new GUIStyle(GUI.skin.label)
				{
					fontSize = 11,
					alignment = (TextAnchor)3,
					clipping = (TextClipping)1
				};
				_groupStyle = new GUIStyle(GUI.skin.label)
				{
					fontSize = 14,
					fontStyle = (FontStyle)1,
					alignment = (TextAnchor)3,
					clipping = (TextClipping)1
				};
				_labelStyle.normal.textColor = PrimarySilver;
				_groupStyle.normal.textColor = PrimarySilver;
				_buttonStyle.normal.background = null;
				_buttonStyle.hover.background = null;
				_buttonStyle.active.background = null;
				_buttonStyle.normal.textColor = PrimarySilver;
				_buttonStyle.hover.textColor = Color.white;
				_buttonStyle.active.textColor = SecondarySilver;
			}
		}

		private bool DrawPlayerButton(Rect rect, string text)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			return DrawPlayerButton(rect, text, null, null);
		}

		private bool DrawPlayerButton(Rect rect, string text, Color? forcedTextColor)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			return DrawPlayerButton(rect, text, forcedTextColor, null);
		}

		private bool DrawPlayerButton(Rect rect, string text, Color? forcedTextColor, FontStyle? forcedFontStyle)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			bool flag = ((Rect)(ref rect)).Contains(Event.current.mousePosition);
			Color color = GUI.color;
			GUI.color = (flag ? new Color(0.2f, 0.22f, 0.26f, 0.98f) : new Color(0.075f, 0.08f, 0.1f, 0.96f));
			GUI.DrawTexture(rect, (Texture)(object)Texture2D.whiteTexture);
			GUI.color = color;
			bool result = GUI.Button(rect, GUIContent.none, GUIStyle.none);
			Color textColor = _buttonStyle.normal.textColor;
			FontStyle fontStyle = _buttonStyle.fontStyle;
			if (forcedTextColor.HasValue)
			{
				_buttonStyle.normal.textColor = forcedTextColor.Value;
			}
			else if (flag)
			{
				_buttonStyle.normal.textColor = Color.white;
			}
			if (forcedFontStyle.HasValue)
			{
				_buttonStyle.fontStyle = forcedFontStyle.Value;
			}
			DrawShadowedLabel(rect, text ?? string.Empty, _buttonStyle, _buttonStyle.normal.textColor);
			_buttonStyle.normal.textColor = textColor;
			_buttonStyle.fontStyle = fontStyle;
			return result;
		}

		private static void DrawShadowedLabel(Rect rect, string text, GUIStyle style, Color color)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			Color textColor = style.normal.textColor;
			style.normal.textColor = TextShadow;
			GUI.Label(new Rect(((Rect)(ref rect)).x + 1.2f, ((Rect)(ref rect)).y + 1.2f, ((Rect)(ref rect)).width, ((Rect)(ref rect)).height), text, style);
			style.normal.textColor = color;
			GUI.Label(rect, text, style);
			style.normal.textColor = textColor;
		}

		private void DrawPanel(Rect rect, bool parchmentBody)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			Color color = GUI.color;
			GUI.color = (parchmentBody ? new Color(0.055f, 0.06f, 0.07f, 0.98f) : new Color(0.035f, 0.04f, 0.05f, 0.98f));
			GUI.DrawTexture(rect, (Texture)(object)Texture2D.whiteTexture);
			GUI.color = color;
		}

		internal void Shutdown()
		{
			_loadGeneration++;
			UnityWebRequest loadRequest = _loadRequest;
			if (loadRequest != null)
			{
				loadRequest.Abort();
			}
			UnityWebRequest loadRequest2 = _loadRequest;
			if (loadRequest2 != null)
			{
				loadRequest2.Dispose();
			}
			_loadRequest = null;
			if ((Object)(object)_source != (Object)null)
			{
				_source.Stop();
			}
			if ((Object)(object)_pausedNativeSource != (Object)null)
			{
				_pausedNativeSource.UnPause();
			}
			OwnsPlayback = false;
		}
	}
}