Decompiled source of SingerJester v1.0.5

BepInEx/plugins/JesterSymphony.dll

Decompiled 2 months 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 System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using JesterSymphony.Patches;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
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("JesterSymphony")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Makes jester play a random song")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+cdbb978f666d25aeb2b1621aa4e9087cb339030a")]
[assembly: AssemblyProduct("JesterSymphony")]
[assembly: AssemblyTitle("JesterSymphony")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.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;
		}
	}
}
namespace JesterSymphony
{
	[BepInPlugin("JesterSymphony", "JesterSymphony", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal AudioClip screaming;

		internal AudioClip windup;

		internal AudioClip popUp;

		private string[] screamingClips;

		private string[] windupClips;

		private string[] popUpClips;

		internal Random rand;

		internal static ManualLogSource LoggerInstance;

		private Harmony harmony = new Harmony("JesterSymphony");

		public static Config Config { get; internal set; }

		internal static Plugin Instance { get; private set; }

		internal List<LinkedSongs> LinkedSongs { get; set; } = new List<LinkedSongs>();


		internal string ExecutingPath => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

		private void Awake()
		{
			Config = new Config(((BaseUnityPlugin)this).Config);
			rand = new Random(0);
			Instance = this;
			LoggerInstance = ((BaseUnityPlugin)this).Logger;
			((BaseUnityPlugin)this).Logger.LogMessage((object)"Loading Audio Files");
			if (Directory.Exists(ExecutingPath + "/Screaming"))
			{
				IOrderedEnumerable<string> source = from f in Directory.GetFiles(ExecutingPath + "/Screaming")
					orderby f
					select f;
				screamingClips = source.ToArray();
			}
			if (Directory.Exists(ExecutingPath + "/Windup"))
			{
				IOrderedEnumerable<string> source2 = from f in Directory.GetFiles(ExecutingPath + "/Windup")
					orderby f
					select f;
				windupClips = source2.ToArray();
			}
			if (Directory.Exists(ExecutingPath + "/Popup"))
			{
				IOrderedEnumerable<string> source3 = from f in Directory.GetFiles(ExecutingPath + "/Popup")
					orderby f
					select f;
				popUpClips = source3.ToArray();
			}
			GetLinkedSongs();
			LoadNewClips();
			((BaseUnityPlugin)this).Logger.LogMessage((object)"Patching");
			harmony.PatchAll(typeof(JesterAIPatch));
			harmony.PatchAll(typeof(RoundManagerPatch));
			harmony.PatchAll(typeof(Plugin));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin JesterSymphony is loaded!");
		}

		public async void LoadNewClips()
		{
			int selectedScreaming = rand.Next(Config.IncludeDefaultScreaming.Value ? (-1) : 0, screamingClips.Length);
			int num = rand.Next(Config.IncludeDefaultWindup.Value ? (-1) : 0, windupClips.Length);
			int selectedPopup = rand.Next(Config.IncludeDefaultPopUp.Value ? (-1) : 0, popUpClips.Length);
			if (num >= 0 && windupClips.Length != 0)
			{
				int num2 = FindWindupInLinked(Path.GetFileName(windupClips[num]));
				if (num2 >= 0)
				{
					num2 = FindSongInScreaming(LinkedSongs[num2].ScreamingName);
					if (num2 >= 0)
					{
						selectedScreaming = num2;
						((BaseUnityPlugin)this).Logger.LogMessage((object)"Found Link!");
					}
				}
				windup = await LoadClip(windupClips[num]);
			}
			else
			{
				windup = null;
			}
			if (selectedScreaming >= 0 && screamingClips.Length != 0)
			{
				screaming = await LoadClip(screamingClips[selectedScreaming]);
			}
			else
			{
				screaming = null;
			}
			if (selectedPopup >= 0 && popUpClips.Length != 0)
			{
				popUp = await LoadClip(popUpClips[selectedPopup]);
			}
			else
			{
				popUp = null;
			}
		}

		public int FindWindupInLinked(string name)
		{
			for (int i = 0; i < LinkedSongs.Count; i++)
			{
				if (Path.GetFileName(LinkedSongs[i].WindupName) == name)
				{
					return i;
				}
			}
			return -1;
		}

		public int FindSongInScreaming(string name)
		{
			for (int i = 0; i < screamingClips.Length; i++)
			{
				if (Path.GetFileName(screamingClips[i]) == name)
				{
					return i;
				}
			}
			return -1;
		}

		public void GetLinkedSongs()
		{
			try
			{
				if (!File.Exists(ExecutingPath + "/linkedSongs.json"))
				{
					((BaseUnityPlugin)this).Logger.LogMessage((object)"No linkfile found!");
					return;
				}
				string text = File.ReadAllText(ExecutingPath + "/linkedSongs.json");
				LinkedSongs = JsonConvert.DeserializeObject<List<LinkedSongs>>(text);
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)ex.Message);
			}
		}

		public AudioType GetAudioType(string extension)
		{
			return (AudioType)(extension switch
			{
				".mp3" => 13, 
				".wav" => 20, 
				_ => 14, 
			});
		}

		public async Task<AudioClip> LoadClip(string path)
		{
			AudioType audioType = GetAudioType(Path.GetExtension(path));
			UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(path, audioType);
			try
			{
				request.SendWebRequest();
				while (!request.isDone)
				{
					await Task.Delay(50);
				}
				if ((int)request.result != 1)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Failed to load file:" + path + ", + " + request.error));
					return null;
				}
				AudioClip content = DownloadHandlerAudioClip.GetContent(request);
				((BaseUnityPlugin)this).Logger.LogMessage((object)("Loaded file: " + path));
				return content;
			}
			finally
			{
				if (request != null)
				{
					request.Dispose();
				}
			}
		}
	}
	public class Config
	{
		public static ConfigEntry<bool> IncludeDefaultWindup;

		public static ConfigEntry<bool> IncludeDefaultScreaming;

		public static ConfigEntry<bool> IncludeDefaultPopUp;

		public Config(ConfigFile config)
		{
			IncludeDefaultWindup = config.Bind<bool>("General", "IncludeDefaultWindup", true, "Allows the randomiser to pick the games windup sound");
			IncludeDefaultScreaming = config.Bind<bool>("General", "IncludeDefaultScreaming", true, "Allows the randomiser to pick the games screaming sound");
			IncludeDefaultPopUp = config.Bind<bool>("General", "IncludeDefaultPopUp", true, "Allows the randomiser to pick the games PopUp sound");
		}
	}
	public struct LinkedSongs
	{
		public string WindupName { get; set; }

		public string ScreamingName { get; set; }
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "JesterSymphony";

		public const string PLUGIN_NAME = "JesterSymphony";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace JesterSymphony.Patches
{
	[HarmonyPatch(typeof(RoundManager))]
	internal class RoundManagerPatch
	{
		[HarmonyPatch("InitializeRandomNumberGenerators")]
		[HarmonyPostfix]
		public static void SeedPatch(ref RoundManager __instance)
		{
			Plugin.LoggerInstance.LogInfo((object)"Loading new audio files!");
			Plugin.LoggerInstance.LogInfo((object)$"Initializing random with seed {__instance.playersManager.randomMapSeed}");
			Plugin.Instance.rand = new Random(__instance.playersManager.randomMapSeed);
			Plugin.Instance.LoadNewClips();
		}
	}
	[HarmonyPatch(typeof(JesterAI))]
	internal class JesterAIPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPrefix]
		public static void JesterPatch(ref AudioClip ___screamingSFX, ref AudioClip ___popGoesTheWeaselTheme, ref AudioClip ___popUpSFX)
		{
			if ((Object)(object)Plugin.Instance.screaming != (Object)null)
			{
				___screamingSFX = Plugin.Instance.screaming;
			}
			if ((Object)(object)Plugin.Instance.windup != (Object)null)
			{
				___popGoesTheWeaselTheme = Plugin.Instance.windup;
			}
			if ((Object)(object)Plugin.Instance.popUp != (Object)null)
			{
				___popUpSFX = Plugin.Instance.popUp;
			}
		}
	}
}