Decompiled source of CustomBoombox v1.0.0

BoomboxSounds.dll

Decompiled 4 hours ago
using System;
using System.Collections;
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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
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("BoomboxSounds")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BoomboxSounds")]
[assembly: AssemblyTitle("BoomboxSounds")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 BoomboxSounds
{
	[BepInPlugin("BoomboxSounds", "BoomboxSounds", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginId = "mattcy.bg.boombox";

		public const string PluginName = "Boombox Sounds";

		public const string Version = "1.0.0";

		internal static ManualLogSource Logger;

		private Harmony harmony;

		public static List<AudioClip> ClipsLoaded = new List<AudioClip>();

		public static string ClipsFolder;

		public static AudioClip currentClip = null;

		public static AudioSource source = null;

		public static int index = 0;

		private GameObject cachedBoombox;

		private float nextCheckTime = 0f;

		private bool hasPlayed = false;

		private string notificationText = "";

		private float textAlpha = 1f;

		public static Plugin Instance { get; private set; }

		private ConfigEntry<KeyboardShortcut> ReloadSounds { get; set; }

		private ConfigEntry<KeyboardShortcut> ChangeMusic { get; set; }

		private ConfigEntry<bool> ChangeJonathanMusic { get; set; }

		private void Awake()
		{
			//IL_003f: 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_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Expected O, but got Unknown
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			ClipsFolder = Path.Combine(Paths.PluginPath, "BoomboxCustomSounds");
			ReloadSounds = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Config", "Reload Songs", new KeyboardShortcut((KeyCode)114, Array.Empty<KeyCode>()), (ConfigDescription)null);
			ChangeMusic = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Config", "Change Music", new KeyboardShortcut((KeyCode)121, Array.Empty<KeyCode>()), (ConfigDescription)null);
			ChangeJonathanMusic = ((BaseUnityPlugin)this).Config.Bind<bool>("Config", "Change Jonathan Music", true, (ConfigDescription)null);
			if (!Directory.Exists(ClipsFolder))
			{
				Directory.CreateDirectory(ClipsFolder);
			}
			LoadAllCustomSounds();
			harmony = new Harmony("mattcy.bg.boombox");
			harmony.PatchAll(typeof(Plugin).Assembly);
		}

		private void Update()
		{
			//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_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			KeyboardShortcut value = ReloadSounds.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				ReloadSongs();
			}
			value = ChangeMusic.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				SwicthCurrentMusic();
			}
			if (!ChangeJonathanMusic.Value)
			{
				return;
			}
			GameObject val = GameObject.Find("Jonathan(Clone)");
			if ((Object)(object)val == (Object)null)
			{
				hasPlayed = false;
				cachedBoombox = null;
			}
			if ((Object)(object)cachedBoombox == (Object)null)
			{
				if (!(Time.time >= nextCheckTime))
				{
					return;
				}
				nextCheckTime = Time.time + 1f;
				if ((Object)(object)val != (Object)null)
				{
					Transform val2 = val.transform.Find("Controller/jonathan_unity/Armature/boombox/Boombox/GameObject");
					if ((Object)(object)val2 != (Object)null)
					{
						cachedBoombox = ((Component)val2).gameObject;
					}
				}
			}
			else if (!hasPlayed && ClipsLoaded.Count > 0)
			{
				AudioSource component = cachedBoombox.GetComponent<AudioSource>();
				if (!((Object)(object)component == (Object)null))
				{
					int num = Random.Range(0, ClipsLoaded.Count);
					component.Stop();
					component.clip = ClipsLoaded[num];
					component.Play();
					hasPlayed = true;
				}
			}
		}

		public void ShowText(string text, float duration)
		{
			((MonoBehaviour)this).StopAllCoroutines();
			((MonoBehaviour)this).StartCoroutine(FadeTextRoutine(text, duration));
		}

		private IEnumerator FadeTextRoutine(string text, float duration)
		{
			notificationText = text;
			textAlpha = 1f;
			yield return (object)new WaitForSeconds(duration);
			while (textAlpha > 0f)
			{
				textAlpha -= Time.deltaTime * 2f;
				yield return null;
			}
			notificationText = "";
		}

		private void OnGUI()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Expected O, but got Unknown
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Expected O, but got Unknown
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: 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 (!string.IsNullOrEmpty(notificationText))
			{
				GUIStyle val = new GUIStyle(GUI.skin.label);
				val.alignment = (TextAnchor)4;
				val.fontSize = 60;
				val.fontStyle = (FontStyle)1;
				val.wordWrap = false;
				val.clipping = (TextClipping)0;
				val.normal.textColor = new Color(1f, 1f, 1f, textAlpha);
				float num = (float)Screen.width * 0.9f;
				Vector2 val2 = val.CalcSize(new GUIContent(notificationText));
				while (val2.x > num && val.fontSize > 12)
				{
					int fontSize = val.fontSize;
					val.fontSize = fontSize - 1;
					val2 = val.CalcSize(new GUIContent(notificationText));
				}
				GUIStyle val3 = new GUIStyle(val);
				val3.normal.textColor = new Color(0f, 0f, 0f, textAlpha);
				float num2 = val2.x + 20f;
				float num3 = val2.y + 10f;
				float num4 = (float)Screen.width / 2f - num2 / 2f;
				float num5 = (float)Screen.height - 300f;
				GUI.Label(new Rect(num4, num5, num2, num3), notificationText, val);
			}
		}

		private void ReloadSongs()
		{
			ClipsLoaded.Clear();
			LoadAllCustomSounds();
		}

		private void PlayNewSong()
		{
			if ((Object)(object)source != (Object)null)
			{
				if ((Object)(object)source.clip != (Object)(object)currentClip)
				{
					source.clip = currentClip;
				}
				source.Stop();
				source.Play();
			}
		}

		private void SwicthCurrentMusic()
		{
			if (ClipsLoaded.Count != 0)
			{
				index = (index + 1) % ClipsLoaded.Count;
				currentClip = ClipsLoaded[index];
				ShowText("Now Playing - " + ((Object)currentClip).name, 2f);
				PlayNewSong();
			}
		}

		private void LoadAllCustomSounds()
		{
			//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_0064: 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_0092: Invalid comparison between Unknown and I4
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			string[] array = (from f in Directory.GetFiles(ClipsFolder, "*.*")
				where f.EndsWith(".wav", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase)
				select f).ToArray();
			Array.Sort(array);
			string[] array2 = array;
			foreach (string text in array2)
			{
				AudioType audioType = GetAudioType(text);
				UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip("file://" + text, audioType);
				try
				{
					audioClip.SendWebRequest();
					while (!audioClip.isDone)
					{
					}
					if ((int)audioClip.result == 1)
					{
						AudioClip content = DownloadHandlerAudioClip.GetContent(audioClip);
						((Object)content).name = Path.GetFileNameWithoutExtension(text);
						ClipsLoaded.Add(content);
						Logger.LogInfo((object)$"Loaded Clip: {((Object)content).name} ({audioType})");
					}
					else
					{
						Logger.LogError((object)("Failed to load clip " + Path.GetFileName(text) + ": " + audioClip.error));
					}
				}
				finally
				{
					((IDisposable)audioClip)?.Dispose();
				}
			}
			Logger.LogInfo((object)$"Total clips loaded: {ClipsLoaded.Count}");
		}

		private AudioType GetAudioType(string filePath)
		{
			//IL_0022: 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)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			string text = Path.GetExtension(filePath).ToLower();
			string text2 = text;
			string text3 = text2;
			if (text3 == ".mp3")
			{
				return (AudioType)13;
			}
			return (AudioType)20;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "BoomboxSounds";

		public const string PLUGIN_NAME = "BoomboxSounds";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace BoomboxSounds.Patches
{
	[HarmonyPatch(typeof(PlayerController), "OnEquipped")]
	internal static class InventoryOnNetworkSpawnPatch
	{
		private static void Postfix(InventoryBase __instance, string name)
		{
			if (!(name == "music box") || Plugin.ClipsLoaded.Count <= 0)
			{
				return;
			}
			AudioSource componentInChildren = ((Component)__instance).gameObject.GetComponentInChildren<AudioSource>();
			if ((Object)(object)componentInChildren != (Object)null)
			{
				Plugin.source = componentInChildren;
				if ((Object)(object)Plugin.currentClip == (Object)null)
				{
					Plugin.currentClip = Plugin.ClipsLoaded[Plugin.index];
				}
				if ((Object)(object)componentInChildren.clip != (Object)(object)Plugin.currentClip)
				{
					componentInChildren.clip = Plugin.currentClip;
				}
				Plugin.Instance.ShowText("Now Playing - " + ((Object)Plugin.currentClip).name, 2f);
				componentInChildren.Play();
			}
		}
	}
}