Decompiled source of Custom Game Music v1.0.0

BepInEx/plugins/CustomGameAudio/StrafTatMusic.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 HarmonyLib;
using Microsoft.CodeAnalysis;
using StrafTatMusic.Monos;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("StrafTatMusic")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("StrafTatMusic")]
[assembly: AssemblyTitle("StrafTatMusic")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace StrafTatMusic
{
	[BepInPlugin("malco.customGameMusic", "CustomGameMusic", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public AudioClip[]? music;

		public GameObject gm;

		private readonly Harmony harmony = new Harmony("malco.customGameMusic");

		public static Plugin instance;

		private void Awake()
		{
			instance = this;
			string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "custommusicmanager");
			AssetBundle val = AssetBundle.LoadFromFile(text);
			gm = val.LoadAsset<GameObject>("Assets/CustomMusicManager.prefab");
			gm.AddComponent<CustomGameMusic>();
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Patched CustomGameMusic");
		}
	}
}
namespace StrafTatMusic.Patches
{
	[HarmonyPatch(typeof(Settings))]
	internal class SettingsPatcher
	{
		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void StartPatcher()
		{
			GameObject val = Object.Instantiate<GameObject>(Plugin.instance.gm);
		}
	}
}
namespace StrafTatMusic.Monos
{
	internal class CustomGameMusic : MonoBehaviour
	{
		public List<AudioClip> music = new List<AudioClip>();

		public List<AudioClip> availableMusic = new List<AudioClip>();

		private TextMeshProUGUI musicText;

		private Animator anim;

		private string directory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "audio");

		private void Awake()
		{
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			musicText = ((Component)((Component)this).transform.GetChild(0)).GetComponent<TextMeshProUGUI>();
			anim = ((Component)this).GetComponent<Animator>();
			Directory.CreateDirectory(directory);
			string[] files = Directory.GetFiles(directory);
			if (files.Length != 0)
			{
				((MonoBehaviour)this).StartCoroutine(LoadSongs(files));
			}
		}

		private void Update()
		{
			if (Input.GetKeyUp((KeyCode)110))
			{
				NextSong();
			}
		}

		private IEnumerator LoadSongs(string[] songPaths)
		{
			foreach (string song in songPaths)
			{
				UnityWebRequest loader = UnityWebRequestMultimedia.GetAudioClip(song, GetAudioType(song));
				DownloadHandler downloadHandler = loader.downloadHandler;
				((DownloadHandlerAudioClip)((downloadHandler is DownloadHandlerAudioClip) ? downloadHandler : null)).streamAudio = true;
				if (loader == null)
				{
					yield break;
				}
				yield return null;
				loader.SendWebRequest();
				yield return null;
				while (!loader.isDone)
				{
					yield return null;
				}
				yield return null;
				if ((int)loader.result == 1)
				{
					AudioClip clip = DownloadHandlerAudioClip.GetContent(loader);
					if (Object.op_Implicit((Object)(object)clip) && (int)clip.loadState == 2)
					{
						((Object)clip).name = Path.GetFileName(song);
						music.Add(clip);
					}
				}
			}
			availableMusic = new List<AudioClip>(music);
			((Behaviour)SoundManager.Instance._musicSource).enabled = true;
		}

		private AudioType GetAudioType(string path)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//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_0067: 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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			if (string.IsNullOrEmpty(path))
			{
				return (AudioType)0;
			}
			string text = Path.GetExtension(path)?.ToLower();
			if (1 == 0)
			{
			}
			AudioType result = (AudioType)(text switch
			{
				".ogg" => 14, 
				".wav" => 20, 
				".mp3" => 13, 
				_ => 0, 
			});
			if (1 == 0)
			{
			}
			return result;
		}

		private void NextSong()
		{
			//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)
			if (availableMusic.Count == 0)
			{
				availableMusic = new List<AudioClip>(music);
			}
			int index = Random.Range(0, availableMusic.Count);
			AudioClip val = availableMusic[index];
			availableMusic.RemoveAt(index);
			SoundManager.Instance._musicSource.clip = val;
			SoundManager.Instance._musicSource.Play();
			((TMP_Text)musicText).text = ((Object)val).name;
			AnimatorStateInfo currentAnimatorStateInfo = anim.GetCurrentAnimatorStateInfo(0);
			if (((AnimatorStateInfo)(ref currentAnimatorStateInfo)).IsName("New State 0"))
			{
				anim.Play("New State 0", 0, 0f);
			}
			else
			{
				anim.SetTrigger("play");
			}
		}

		private void FixedUpdate()
		{
			//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")
			{
				if (!SoundManager.Instance._musicSource.isPlaying)
				{
					NextSong();
				}
			}
			else
			{
				SoundManager.Instance._musicSource.Stop();
			}
		}
	}
}