Decompiled source of UltraRankSounds v1.0.4

plugins/UltraRankSounds/UltraRankSounds.dll

Decompiled a month 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using PluginConfig.API;
using PluginConfig.API.Decorators;
using PluginConfig.API.Fields;
using PluginConfig.API.Functionals;
using UltraRankSounds;
using UltraRankSounds.Components;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("UltraRankSounds")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4+7b0fa75ad0a9b37dfd74f4232ac1f9137062de01")]
[assembly: AssemblyProduct("UltraRankSounds")]
[assembly: AssemblyTitle("UltraRankSounds")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
public class SoundsConfig
{
	public class SoundCollection
	{
		private List<string> savedStrings = new List<string>();

		private int cursor;

		public List<string> Sounds
		{
			get
			{
				return savedStrings;
			}
			set
			{
				throw new NotSupportedException();
			}
		}

		public void AddSound(string soundfile)
		{
			savedStrings.Add(soundfile);
		}

		public void Clear()
		{
			savedStrings.Clear();
			cursor = 0;
		}

		public string DecideSound()
		{
			if (savedStrings.Count == 0)
			{
				return "none";
			}
			if (global::UltraRankSounds.UltraRankSounds.PlaySoundsInAlphabeticOrder.value)
			{
				cursor++;
				cursor %= savedStrings.Count;
				return savedStrings[cursor];
			}
			return savedStrings[randomNumGen.Next(0, savedStrings.Count)];
		}
	}

	public class Uprank
	{
		public static SoundCollection DestructiveSound = new SoundCollection();

		public static SoundCollection ChaoticSound = new SoundCollection();

		public static SoundCollection BrutalSound = new SoundCollection();

		public static SoundCollection AnarchicSound = new SoundCollection();

		public static SoundCollection SupremeSound = new SoundCollection();

		public static SoundCollection SSadisticSound = new SoundCollection();

		public static SoundCollection SSShitstormSound = new SoundCollection();

		public static SoundCollection ULTRAKILLSound = new SoundCollection();

		public static void RegisterSounds(string[] files)
		{
			foreach (string text in files)
			{
				string fileName = Path.GetFileName(text);
				if (!fileName.StartsWith("downrank-"))
				{
					global::UltraRankSounds.UltraRankSounds.Log("found sound file '" + fileName + "'");
					if (fileName.StartsWith("ULTR"))
					{
						ULTRAKILLSound.AddSound(text);
					}
					else if (fileName.StartsWith("SSS"))
					{
						SSShitstormSound.AddSound(text);
					}
					else if (fileName.StartsWith("SS"))
					{
						SSadisticSound.AddSound(text);
					}
					else if (fileName.StartsWith("S"))
					{
						SupremeSound.AddSound(text);
					}
					else if (fileName.StartsWith("D"))
					{
						DestructiveSound.AddSound(text);
					}
					else if (fileName.StartsWith("C"))
					{
						ChaoticSound.AddSound(text);
					}
					else if (fileName.StartsWith("B"))
					{
						BrutalSound.AddSound(text);
					}
					else if (fileName.StartsWith("A"))
					{
						AnarchicSound.AddSound(text);
					}
				}
			}
			global::UltraRankSounds.UltraRankSounds.Log("Uprank Sounds");
			global::UltraRankSounds.UltraRankSounds.Log("ULTRAKILL '" + string.Join(",", ULTRAKILLSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("SSShitstorm '" + string.Join(",", SSShitstormSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("SSadistic '" + string.Join(",", SSadisticSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Supreme '" + string.Join(",", SupremeSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Destructive '" + string.Join(",", DestructiveSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Chaotic '" + string.Join(",", ChaoticSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Brutal '" + string.Join(",", BrutalSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Anarchic '" + string.Join(",", AnarchicSound.Sounds.ToArray()) + "'");
		}
	}

	public class Downrank
	{
		public static SoundCollection DestructiveSound = new SoundCollection();

		public static SoundCollection ChaoticSound = new SoundCollection();

		public static SoundCollection BrutalSound = new SoundCollection();

		public static SoundCollection AnarchicSound = new SoundCollection();

		public static SoundCollection SupremeSound = new SoundCollection();

		public static SoundCollection SSadisticSound = new SoundCollection();

		public static SoundCollection SSShitstormSound = new SoundCollection();

		public static void RegisterSounds(string[] files)
		{
			foreach (string text in files)
			{
				string fileName = Path.GetFileName(text);
				if (fileName.StartsWith("downrank-"))
				{
					if (fileName.StartsWith("downrank-SSS"))
					{
						SSShitstormSound.AddSound(text);
					}
					else if (fileName.StartsWith("downrank-SS"))
					{
						SSadisticSound.AddSound(text);
					}
					else if (fileName.StartsWith("downrank-S"))
					{
						SupremeSound.AddSound(text);
					}
					else if (fileName.StartsWith("downrank-D"))
					{
						DestructiveSound.AddSound(text);
					}
					else if (fileName.StartsWith("downrank-C"))
					{
						ChaoticSound.AddSound(text);
					}
					else if (fileName.StartsWith("downrank-B"))
					{
						BrutalSound.AddSound(text);
					}
					else if (fileName.StartsWith("downrank-A"))
					{
						AnarchicSound.AddSound(text);
					}
				}
			}
			global::UltraRankSounds.UltraRankSounds.Log("Downrank Sounds");
			global::UltraRankSounds.UltraRankSounds.Log("SSShitstorm '" + string.Join(",", SSShitstormSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("SSadistic '" + string.Join(",", SSadisticSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Supreme '" + string.Join(",", SupremeSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Destructive '" + string.Join(",", DestructiveSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Chaotic '" + string.Join(",", ChaoticSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Brutal '" + string.Join(",", BrutalSound.Sounds.ToArray()) + "'");
			global::UltraRankSounds.UltraRankSounds.Log("Anarchic '" + string.Join(",", AnarchicSound.Sounds.ToArray()) + "'");
		}
	}

	public static string DefaultSoundParentFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "";

	public static string DefaultSoundFolder = Path.Combine(DefaultSoundParentFolder, "sounds") ?? "";

	private static Random randomNumGen = new Random();

	private static int soundFileCount = 0;

	public static string GetAscensionRankSoundName(int rank)
	{
		return rank switch
		{
			0 => Uprank.DestructiveSound.DecideSound(), 
			1 => Uprank.ChaoticSound.DecideSound(), 
			2 => Uprank.BrutalSound.DecideSound(), 
			3 => Uprank.AnarchicSound.DecideSound(), 
			4 => Uprank.SupremeSound.DecideSound(), 
			5 => Uprank.SSadisticSound.DecideSound(), 
			6 => Uprank.SSShitstormSound.DecideSound(), 
			7 => Uprank.ULTRAKILLSound.DecideSound(), 
			_ => "", 
		};
	}

	public static string GetDescensionRankSoundName(int rank)
	{
		return rank switch
		{
			0 => Downrank.DestructiveSound.DecideSound(), 
			1 => Downrank.ChaoticSound.DecideSound(), 
			2 => Downrank.BrutalSound.DecideSound(), 
			3 => Downrank.AnarchicSound.DecideSound(), 
			4 => Downrank.SupremeSound.DecideSound(), 
			5 => Downrank.SSadisticSound.DecideSound(), 
			6 => Downrank.SSShitstormSound.DecideSound(), 
			_ => "", 
		};
	}

	public static void UpdateSoundEntries()
	{
		string[] files = Directory.GetFiles(DefaultSoundFolder);
		if (files.Length == 0)
		{
			global::UltraRankSounds.UltraRankSounds.Log("Cannot find any sound files", error: true);
		}
		else if (files.Length != soundFileCount)
		{
			Uprank.RegisterSounds(files);
			Downrank.RegisterSounds(files);
			soundFileCount = files.Length;
		}
	}
}
namespace UltraRankSounds
{
	internal static class PluginInfo
	{
		public const string GUID = "gamingnoobdev.ultrakill.UltraRankSounds";

		public const string NAME = "UltraRankSounds";

		public const string VERSION = "1.0.4";
	}
	[BepInProcess("ULTRAKILL.exe")]
	[BepInPlugin("gamingnoobdev.ultrakill.UltraRankSounds", "UltraRankSounds", "1.0.4")]
	public class UltraRankSounds : BaseUnityPlugin
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static BoolValueChangeEventDelegate <0>__ChangedModStatus;

			public static OnValueChangeEventDelegate <1>__VolumeChanged;

			public static OnClick <2>__UpdateSoundEntries;

			public static OnClick <3>__OpenDefaultSoundsFolder;
		}

		private static ManualLogSource logger;

		public static FloatSliderField VolumeSlider;

		public static ButtonField OpenSoundsFolder;

		public static ButtonField UpdateSoundFiles;

		public static BoolField PlaySoundsInAlphabeticOrder;

		public static BoolField PlayIfDescended;

		public static BoolField EnableSounds;

		private static PluginConfigurator config;

		private static void InitConfig()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Expected O, but got Unknown
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Expected O, but got Unknown
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Expected O, but got Unknown
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Expected O, but got Unknown
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Expected O, but got Unknown
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Expected O, but got Unknown
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Expected O, but got Unknown
			config = PluginConfigurator.Create("UltraRankSounds", "gamingnoobdev.ultrakill.UltraRankSounds");
			config.SetIconWithURL("https://raw.githubusercontent.com/GAMINGNOOBdev/UltraRankSounds/refs/heads/master/icon.png");
			new ConfigHeader(config.rootPanel, "Sound settings", 24);
			EnableSounds = new BoolField(config.rootPanel, "Enable rank sounds", "ultraranksounds.enabled", true);
			PlayIfDescended = new BoolField(config.rootPanel, "Play sound when rank descended", "ultraranksounds.playdescended", false);
			PlaySoundsInAlphabeticOrder = new BoolField(config.rootPanel, "Play sounds in alpabetical order", "ultraranksounds.playinalphabeticorder", false);
			VolumeSlider = new FloatSliderField(config.rootPanel, "Volume", "ultraranksounds.volume", new Tuple<float, float>(0f, 1f), 1f, 2);
			new ConfigHeader(config.rootPanel, "Sound files", 24);
			OpenSoundsFolder = new ButtonField(config.rootPanel, "Open sounds folder", "ultraranksounds.opensoundsfolder");
			UpdateSoundFiles = new ButtonField(config.rootPanel, "Update sound files", "ultraranksounds.updatesoundfiles");
			BoolField enableSounds = EnableSounds;
			object obj = <>O.<0>__ChangedModStatus;
			if (obj == null)
			{
				BoolValueChangeEventDelegate val = ChangedModStatus;
				<>O.<0>__ChangedModStatus = val;
				obj = (object)val;
			}
			enableSounds.onValueChange += (BoolValueChangeEventDelegate)obj;
			FloatSliderField volumeSlider = VolumeSlider;
			object obj2 = <>O.<1>__VolumeChanged;
			if (obj2 == null)
			{
				OnValueChangeEventDelegate val2 = VolumeChanged;
				<>O.<1>__VolumeChanged = val2;
				obj2 = (object)val2;
			}
			volumeSlider.onValueChange += (OnValueChangeEventDelegate)obj2;
			ButtonField updateSoundFiles = UpdateSoundFiles;
			object obj3 = <>O.<2>__UpdateSoundEntries;
			if (obj3 == null)
			{
				OnClick val3 = SoundsConfig.UpdateSoundEntries;
				<>O.<2>__UpdateSoundEntries = val3;
				obj3 = (object)val3;
			}
			updateSoundFiles.onClick += (OnClick)obj3;
			ButtonField openSoundsFolder = OpenSoundsFolder;
			object obj4 = <>O.<3>__OpenDefaultSoundsFolder;
			if (obj4 == null)
			{
				OnClick val4 = OpenDefaultSoundsFolder;
				<>O.<3>__OpenDefaultSoundsFolder = val4;
				obj4 = (object)val4;
			}
			openSoundsFolder.onClick += (OnClick)obj4;
			EnableSounds.TriggerValueChangeEvent();
			VolumeSlider.TriggerValueChangeEvent();
		}

		private static void ChangedModStatus(BoolValueChangeEvent e)
		{
			((ConfigField)PlayIfDescended).interactable = e.value;
			((ConfigField)VolumeSlider).interactable = e.value;
		}

		private static void VolumeChanged(FloatSliderValueChangeEvent e)
		{
			CustomSoundPlayer.Instance?.SetSoundVolume(e.newValue);
		}

		private static void OpenDefaultSoundsFolder()
		{
			Application.OpenURL(new Uri(SoundsConfig.DefaultSoundFolder).AbsoluteUri);
		}

		private void Awake()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			logger = ((BaseUnityPlugin)this).Logger;
			SoundsConfig.UpdateSoundEntries();
			InitConfig();
			new Harmony("gamingnoobdev.ultrakill.UltraRankSounds").PatchAll();
			Log("Sound parent folder: '" + SoundsConfig.DefaultSoundParentFolder + "'");
		}

		public static void Log(string message, bool error = false)
		{
			if (logger != null)
			{
				if (error)
				{
					logger.LogError((object)message);
				}
				else
				{
					logger.LogInfo((object)message);
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "UltraRankSounds";

		public const string PLUGIN_NAME = "UltraRankSounds";

		public const string PLUGIN_VERSION = "1.0.4";
	}
}
namespace UltraRankSounds.Patches
{
	[HarmonyPatch(typeof(StyleHUD))]
	public class StyleHUDPatch
	{
		private static CustomSoundPlayer customSoundPlayer;

		private static int lastRankIndex;

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StyleHUD), "Start")]
		public static void StyleHUD_Start_Postfix(GameObject ___styleHud, StyleHUD __instance)
		{
			customSoundPlayer = ___styleHud.AddComponent<CustomSoundPlayer>();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StyleHUD), "ComboStart")]
		public static void StyleHUD_ComboStart_Postfix()
		{
			if (UltraRankSounds.EnableSounds.value && UltraRankSounds.PlayIfDescended.value)
			{
				PlaySoundForRank(0, "RankD");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StyleHUD), "AddPoints")]
		public static void StyleHUD_AddPoints_Postfix(string pointID, StyleHUD __instance)
		{
			string localizedName = __instance.GetLocalizedName(pointID);
			if (!Utility.IsNullOrWhiteSpace(localizedName) && !Utility.IsNullOrWhiteSpace(pointID))
			{
				UltraRankSounds.Log("style bonus '" + localizedName + "' with id '" + pointID + "' added");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StyleHUD), "AscendRank")]
		public static void StyleHUD_AscendRank_Postfix(StyleHUD __instance)
		{
			int rankIndex = __instance.rankIndex;
			string name = ((Object)__instance.currentRank.sprite).name;
			PlaySoundForRank(rankIndex, name);
			lastRankIndex = rankIndex;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StyleHUD), "DescendRank")]
		public static void StyleHUD_DescendRank_Postfix(bool ___comboActive, StyleHUD __instance)
		{
			if (UltraRankSounds.EnableSounds.value && UltraRankSounds.PlayIfDescended.value && ___comboActive)
			{
				int rankIndex = __instance.rankIndex;
				string name = ((Object)__instance.currentRank.sprite).name;
				UltraRankSounds.Log($"style rank [index: {rankIndex} oldindex: {lastRankIndex}] | name '{name}'");
				PlaySoundForRank(rankIndex, name, ascended: false);
				lastRankIndex = rankIndex;
			}
		}

		private static void PlaySoundForRank(int index, string name, bool ascended = true)
		{
			if (UltraRankSounds.EnableSounds.value && !((Object)(object)customSoundPlayer == (Object)null))
			{
				string soundPathForRankIndex = GetSoundPathForRankIndex(index, ascended);
				string text = (ascended ? "ascended" : "descended");
				UltraRankSounds.Log($"{text} style rank to '{name}' [index: {index} oldindex: {lastRankIndex}] | playing sound '{soundPathForRankIndex}'");
				customSoundPlayer.PlaySound(soundPathForRankIndex);
			}
		}

		private static string GetSoundPathForRankIndex(int rank, bool ascended)
		{
			if (ascended)
			{
				return SoundsConfig.GetAscensionRankSoundName(rank);
			}
			return SoundsConfig.GetDescensionRankSoundName(rank);
		}
	}
}
namespace UltraRankSounds.Components
{
	public class CustomSoundPlayer : MonoBehaviour
	{
		private static CustomSoundPlayer _instance;

		public AudioSource source;

		private float soundVolume;

		private string soundPath;

		public static CustomSoundPlayer Instance
		{
			get
			{
				return _instance;
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		private void Start()
		{
			_instance = this;
			source = ((Component)this).gameObject.AddComponent<AudioSource>();
			SetSoundVolume(UltraRankSounds.VolumeSlider.value);
		}

		public void SetSoundVolume(float volume)
		{
			soundVolume = volume;
		}

		public void PlaySound(string file)
		{
			if (!string.IsNullOrEmpty(file))
			{
				if (!File.Exists(file))
				{
					UltraRankSounds.Log("Could not find audio file '" + file + "'", error: true);
					return;
				}
				soundPath = file;
				((Component)this).gameObject.SetActive(true);
				((MonoBehaviour)this).StartCoroutine(PlaySoundRoutine());
				UltraRankSounds.Log("Playing sound file '" + file + "'");
			}
		}

		private IEnumerator PlaySoundRoutine()
		{
			WaitUntil soundFinished = new WaitUntil((Func<bool>)(() => Application.isFocused && !source.isPlaying));
			FileInfo fileInfo = new FileInfo(soundPath);
			AudioType val = CustomMusicFileBrowser.extensionTypeDict[fileInfo.Extension.ToLower()];
			UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(new Uri(soundPath).AbsoluteUri, val);
			try
			{
				DownloadHandler downloadHandler = request.downloadHandler;
				DownloadHandlerAudioClip handler = (DownloadHandlerAudioClip)(object)((downloadHandler is DownloadHandlerAudioClip) ? downloadHandler : null);
				handler.streamAudio = false;
				request.SendWebRequest();
				yield return request;
				source.clip = handler.audioClip;
				source.volume = soundVolume;
				source.Play();
				yield return soundFinished;
				((Component)this).gameObject.SetActive(false);
				Object.Destroy((Object)(object)handler.audioClip);
			}
			finally
			{
				((IDisposable)request)?.Dispose();
			}
		}
	}
}