Decompiled source of StarWarsElevatorMusic v1.0.0

BepInEx/plugins/ElevatorSymphony.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 ElevatorSymphony.Patches;
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: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("ElevatorSymphony")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("ElevatorSymphony")]
[assembly: AssemblyTitle("ElevatorSymphony")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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 ElevatorSymphony
{
	[BepInPlugin("com.sk737.elevatorsymphony", "ElevatorSymphony", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal const string GUID = "com.sk737.elevatorsymphony";

		internal const string NAME = "ElevatorSymphony";

		internal const string VERSION = "1.1.0";

		private string[] mClipFiles;

		private Harmony mHarmony = new Harmony("com.sk737.elevatorsymphony");

		private int mTotalWeight;

		internal static ManualLogSource LoggerInstance { get; private set; }

		internal static Plugin Instance { get; private set; }

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

		internal Random Random { get; set; }

		public static Config Config { get; private set; }

		private void Awake()
		{
			Instance = this;
			LoggerInstance = ((BaseUnityPlugin)this).Logger;
			if (Directory.Exists(ExecutingPath + "/Music"))
			{
				List<string> list = new List<string>();
				list.AddRange(from f in Directory.GetFiles(ExecutingPath + "/Music")
					orderby f
					select f);
				mClipFiles = list.ToArray();
			}
			Config = new Config(((BaseUnityPlugin)this).Config, mClipFiles);
			foreach (ConfigEntry<int> songWeight in Config.SongWeights)
			{
				mTotalWeight += songWeight.Value;
			}
			mHarmony.PatchAll(typeof(RoundManagerPatch));
			mHarmony.PatchAll(typeof(StartStartOfRoundPatch));
			mHarmony.PatchAll(typeof(Plugin));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin ElevatorSymphony is loaded!");
		}

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

		public async Task LoadClip(string path, Action<AudioClip> callback)
		{
			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;
				}
				AudioClip content = DownloadHandlerAudioClip.GetContent(request);
				((BaseUnityPlugin)this).Logger.LogMessage((object)("Loaded file: " + path));
				callback(content);
			}
			finally
			{
				if (request != null)
				{
					request.Dispose();
				}
			}
		}

		public void LoadNewAudioFile()
		{
			int num = Random.Next(0, mTotalWeight);
			int num2 = -1;
			int num3 = 0;
			for (int i = 0; i < Config.SongWeights.Count; i++)
			{
				int num4 = num3;
				num3 += Config.SongWeights[i].Value;
				if (num > num4 && num < num3)
				{
					num2 = i - 1;
					break;
				}
			}
			if (num2 >= 0 && mClipFiles.Length != 0)
			{
				LoadClip(mClipFiles[num2], SetElevator);
			}
		}

		public void SetElevator(AudioClip clip)
		{
			MineshaftElevatorController val = Object.FindObjectOfType<MineshaftElevatorController>();
			if ((Object)(object)val != (Object)null)
			{
				val.elevatorJingleMusic.clip = clip;
			}
		}
	}
	public class Config
	{
		public static List<ConfigEntry<int>> SongWeights = new List<ConfigEntry<int>>();

		public Config(ConfigFile config, string[] clipFiles)
		{
			SongWeights.Add(config.Bind<int>("General", "Default chance", 100, "Chance of the base games song being picked by the randomizer. The higher the value the greater the chance it is picked."));
			for (int i = 0; i < clipFiles.Length; i++)
			{
				string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(clipFiles[i]);
				SongWeights.Add(config.Bind<int>("General", fileNameWithoutExtension + " chance", 100, "Chance of " + fileNameWithoutExtension + " being picked by the randomizer. The higher the value the greater the chance it is picked."));
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "ElevatorSymphony";

		public const string PLUGIN_NAME = "ElevatorSymphony";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace ElevatorSymphony.Patches
{
	[HarmonyPatch(typeof(RoundManager))]
	internal class RoundManagerPatch
	{
		[HarmonyPatch("InitializeRandomNumberGenerators")]
		[HarmonyPrefix]
		public static void SeedPatch(ref RoundManager __instance)
		{
			Plugin.LoggerInstance.LogInfo((object)$"Initializing random with seed {__instance.playersManager.randomMapSeed}");
			Plugin.Instance.Random = new Random(__instance.playersManager.randomMapSeed);
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartStartOfRoundPatch
	{
		[HarmonyPatch("openingDoorsSequence")]
		[HarmonyPostfix]
		public static void openingDoorsSequencePatch()
		{
			Plugin.Instance.LoadNewAudioFile();
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}