Decompiled source of MusicMod v1.2.0

MusicMod.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("MusicMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MusicMod")]
[assembly: AssemblyTitle("MusicMod")]
[assembly: AssemblyVersion("1.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;
		}
	}
}
[BepInPlugin("com.liamgaming.musicmod", "MusicMod", "1.2.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class MusicModPlugin : BaseUnityPlugin
{
	private AudioSource audioSource;

	private List<string> playlist = new List<string>();

	private int currentTrackIndex = -1;

	private bool isPlaying = false;

	private ConfigEntry<float> volume;

	private ConfigEntry<KeyCode> hotkeyPlayPause;

	private ConfigEntry<KeyCode> hotkeyNext;

	private ConfigEntry<KeyCode> hotkeyPrev;

	private string musicFolder;

	private void Awake()
	{
		//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)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		ModRegistry.Register(new ModEntry
		{
			ModName = "MusicMod",
			ModType = ((object)this).GetType()
		});
		musicFolder = Path.Combine(Paths.PluginPath, "music");
		if (!Directory.Exists(musicFolder))
		{
			try
			{
				Directory.CreateDirectory(musicFolder);
			}
			catch
			{
			}
		}
		volume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Volume", 0.5f, "Music player volume (0.0 to 1.0).");
		hotkeyPlayPause = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Hotkeys", "Play Pause Hotkey", (KeyCode)278, "Hotkey to toggle play/pause.");
		hotkeyNext = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Hotkeys", "Next Track Hotkey", (KeyCode)281, "Hotkey to skip to the next track.");
		hotkeyPrev = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Hotkeys", "Previous Track Hotkey", (KeyCode)280, "Hotkey to return to the previous track.");
		audioSource = ((Component)this).gameObject.AddComponent<AudioSource>();
		audioSource.volume = volume.Value;
		audioSource.loop = false;
		volume.SettingChanged += delegate
		{
			if ((Object)(object)audioSource != (Object)null)
			{
				audioSource.volume = volume.Value;
			}
		};
		ScanMusicFolder();
		if (playlist.Count > 0)
		{
			currentTrackIndex = 0;
			isPlaying = true;
			((MonoBehaviour)this).StartCoroutine(LoadAndPlayTrack());
		}
	}

	private void Update()
	{
		//IL_001e: 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_005c: Unknown result type (might be due to invalid IL or missing references)
		if (playlist.Count != 0)
		{
			if (Input.GetKeyDown(hotkeyPlayPause.Value))
			{
				TogglePlay();
			}
			else if (Input.GetKeyDown(hotkeyNext.Value))
			{
				PlayNext();
			}
			else if (Input.GetKeyDown(hotkeyPrev.Value))
			{
				PlayPrevious();
			}
			if (isPlaying && !audioSource.isPlaying && audioSource.time == 0f)
			{
				PlayNext();
			}
		}
	}

	private void ScanMusicFolder()
	{
		playlist.Clear();
		if (!Directory.Exists(musicFolder))
		{
			return;
		}
		string[] files = Directory.GetFiles(musicFolder);
		foreach (string text in files)
		{
			string text2 = Path.GetExtension(text).ToLower();
			if (text2 == ".mp3" || text2 == ".wav" || text2 == ".ogg")
			{
				playlist.Add(text);
			}
		}
	}

	private void TogglePlay()
	{
		if (playlist.Count == 0)
		{
			return;
		}
		if (isPlaying)
		{
			audioSource.Pause();
			isPlaying = false;
			return;
		}
		isPlaying = true;
		if ((Object)(object)audioSource.clip == (Object)null)
		{
			((MonoBehaviour)this).StartCoroutine(LoadAndPlayTrack());
		}
		else
		{
			audioSource.UnPause();
		}
	}

	private void PlayNext()
	{
		if (playlist.Count != 0)
		{
			currentTrackIndex = (currentTrackIndex + 1) % playlist.Count;
			isPlaying = true;
			((MonoBehaviour)this).StartCoroutine(LoadAndPlayTrack());
		}
	}

	private void PlayPrevious()
	{
		if (playlist.Count != 0)
		{
			currentTrackIndex = (currentTrackIndex - 1 + playlist.Count) % playlist.Count;
			isPlaying = true;
			((MonoBehaviour)this).StartCoroutine(LoadAndPlayTrack());
		}
	}

	private IEnumerator LoadAndPlayTrack()
	{
		if (currentTrackIndex < 0 || currentTrackIndex >= playlist.Count)
		{
			yield break;
		}
		string path = playlist[currentTrackIndex];
		string url = "file://" + path.Replace("\\", "/");
		AudioType type = GetAudioType(path);
		UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(url, type);
		try
		{
			yield return request.SendWebRequest();
			if ((int)request.result == 1)
			{
				AudioClip clip = DownloadHandlerAudioClip.GetContent(request);
				audioSource.clip = clip;
				audioSource.Play();
				Debug.Log((object)("[MusicMod] Now playing: " + Path.GetFileName(path)));
			}
			else
			{
				Debug.LogError((object)("[MusicMod] Error loading music track: " + request.error));
			}
		}
		finally
		{
			((IDisposable)request)?.Dispose();
		}
	}

	private AudioType GetAudioType(string path)
	{
		//IL_001e: 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_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		string text = Path.GetExtension(path).ToLower();
		if (text == ".wav")
		{
			return (AudioType)20;
		}
		if (text == ".ogg")
		{
			return (AudioType)14;
		}
		return (AudioType)13;
	}
}