Decompiled source of JesterJukebox v1.0.0

BepInEx/plugins/JesterJukebox/JesterJukebox.dll

Decompiled 6 hours 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("JesterJukebox")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("JesterJukebox")]
[assembly: AssemblyTitle("JesterJukebox")]
[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 JesterJukebox
{
	public enum JesterSlot
	{
		Windup,
		Popup,
		Chase,
		Kill
	}
	[Serializable]
	internal class PackInfo
	{
		public string name;
	}
	public class JesterPack
	{
		public const string MarkerFile = "jester_jukebox.json";

		private static readonly string[] Extensions = new string[3] { ".ogg", ".wav", ".mp3" };

		private readonly string[] files = new string[4];

		private readonly AudioClip[] clips = (AudioClip[])(object)new AudioClip[4];

		public string Name { get; }

		public string Folder { get; }

		private JesterPack(string name, string folder)
		{
			Name = name;
			Folder = folder;
		}

		internal static JesterPack FromMarker(string markerPath)
		{
			string folder = Path.GetDirectoryName(markerPath);
			string text = null;
			try
			{
				text = JsonUtility.FromJson<PackInfo>(File.ReadAllText(markerPath))?.name;
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Could not read " + markerPath + ": " + ex.Message));
			}
			if (string.IsNullOrWhiteSpace(text))
			{
				text = Path.GetFileName(folder);
			}
			JesterPack jesterPack = new JesterPack(text, folder);
			foreach (JesterSlot value in Enum.GetValues(typeof(JesterSlot)))
			{
				string baseName = value.ToString().ToLowerInvariant();
				jesterPack.files[(int)value] = Extensions.Select((string ext) => Path.Combine(folder, baseName + ext)).FirstOrDefault(File.Exists);
			}
			if (jesterPack.files.All((string f) => f == null))
			{
				Plugin.Log.LogWarning((object)("Pack '" + text + "' (" + folder + ") has no windup/popup/chase/kill sound files; skipping."));
				return null;
			}
			return jesterPack;
		}

		public bool Has(JesterSlot slot)
		{
			return files[(int)slot] != null;
		}

		public AudioClip Get(JesterSlot slot)
		{
			return clips[(int)slot];
		}

		internal void Load()
		{
			for (int i = 0; i < files.Length; i++)
			{
				if (files[i] != null && (Object)(object)clips[i] == (Object)null)
				{
					clips[i] = LoadClip(files[i]);
				}
			}
		}

		internal void Unload()
		{
			for (int i = 0; i < clips.Length; i++)
			{
				if ((Object)(object)clips[i] != (Object)null)
				{
					Object.Destroy((Object)(object)clips[i]);
					clips[i] = null;
				}
			}
		}

		public override string ToString()
		{
			IEnumerable<string> values = from s in Enum.GetValues(typeof(JesterSlot)).Cast<JesterSlot>().Where(Has)
				select s.ToString().ToLowerInvariant();
			return Name + " [" + string.Join(", ", values) + "]";
		}

		private static AudioClip LoadClip(string path)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: 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_005b: 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_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Invalid comparison between Unknown and I4
			string text = Path.GetExtension(path).ToLowerInvariant();
			AudioType val = ((text == ".ogg") ? ((AudioType)14) : ((!(text == ".mp3")) ? ((AudioType)20) : ((AudioType)13)));
			AudioType val2 = val;
			UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip("file:///" + path.Replace('\\', '/'), val2);
			try
			{
				((DownloadHandlerAudioClip)audioClip.downloadHandler).streamAudio = false;
				UnityWebRequestAsyncOperation val3 = audioClip.SendWebRequest();
				while (!((AsyncOperation)val3).isDone)
				{
				}
				if ((int)audioClip.result != 1)
				{
					Plugin.Log.LogError((object)("Failed to load " + path + ": " + audioClip.error));
					return null;
				}
				AudioClip content = DownloadHandlerAudioClip.GetContent(audioClip);
				((Object)content).name = Path.GetFileNameWithoutExtension(path);
				return content;
			}
			finally
			{
				((IDisposable)audioClip)?.Dispose();
			}
		}
	}
	public static class PackRegistry
	{
		private static readonly List<JesterPack> packs = new List<JesterPack>();

		private static int? activeSeed;

		public static IReadOnlyList<JesterPack> Packs => packs;

		public static JesterPack Active { get; private set; }

		internal static void Discover(string pluginsDir)
		{
			IEnumerable<string> source;
			try
			{
				source = Directory.EnumerateFiles(pluginsDir, "jester_jukebox.json", SearchOption.AllDirectories);
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Could not scan " + pluginsDir + " for Jester packs: " + ex.Message));
				return;
			}
			packs.AddRange(from p in source.Select(JesterPack.FromMarker)
				where p != null
				select p);
			packs.Sort(delegate(JesterPack a, JesterPack b)
			{
				int num = string.CompareOrdinal(a.Name, b.Name);
				return (num == 0) ? string.CompareOrdinal(Path.GetFileName(a.Folder), Path.GetFileName(b.Folder)) : num;
			});
			if (packs.Count == 0)
			{
				Plugin.Log.LogInfo((object)"No Jester packs installed; the Jester will use its vanilla sounds.");
				return;
			}
			Plugin.Log.LogInfo((object)$"Found {packs.Count} Jester pack(s):");
			foreach (JesterPack pack in packs)
			{
				Plugin.Log.LogInfo((object)$"  {pack}");
			}
		}

		internal static void SelectForSeed(int seed)
		{
			if (packs.Count != 0 && activeSeed != seed)
			{
				activeSeed = seed;
				JesterPack jesterPack = packs[(int)((uint)seed % (uint)packs.Count)];
				if (jesterPack != Active)
				{
					Active?.Unload();
					Active = jesterPack;
					Active.Load();
				}
				Plugin.Log.LogInfo((object)("Jester pack for this landing: " + Active.Name));
			}
		}
	}
	[HarmonyPatch]
	internal static class Patches
	{
		[HarmonyPatch(typeof(RoundManager), "GenerateNewLevelClientRpc")]
		[HarmonyPrefix]
		private static void PickPack(int randomSeed)
		{
			PackRegistry.SelectForSeed(randomSeed);
		}

		[HarmonyPatch(typeof(JesterAI), "Start")]
		[HarmonyPostfix]
		private static void ApplyPack(JesterAI __instance)
		{
			JesterPack active = PackRegistry.Active;
			if (active != null)
			{
				if ((Object)(object)active.Get(JesterSlot.Windup) != (Object)null)
				{
					__instance.popGoesTheWeaselTheme = active.Get(JesterSlot.Windup);
				}
				if ((Object)(object)active.Get(JesterSlot.Popup) != (Object)null)
				{
					__instance.popUpSFX = active.Get(JesterSlot.Popup);
				}
				if ((Object)(object)active.Get(JesterSlot.Chase) != (Object)null)
				{
					__instance.screamingSFX = active.Get(JesterSlot.Chase);
				}
				if ((Object)(object)active.Get(JesterSlot.Kill) != (Object)null)
				{
					__instance.killPlayerSFX = active.Get(JesterSlot.Kill);
				}
			}
		}
	}
	[BepInPlugin("Xylq.JesterJukebox", "JesterJukebox", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			PackRegistry.Discover(Paths.PluginPath);
			new Harmony("Xylq.JesterJukebox").PatchAll(typeof(Plugin).Assembly);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Xylq.JesterJukebox";

		public const string PLUGIN_NAME = "JesterJukebox";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}