Decompiled source of Potatoes TNH BGM Loader v4.0.3

PTNHBGML.dll

Decompiled 5 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FMOD.Studio;
using FMODUnity;
using FistVR;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using Sodalite;
using Sodalite.Api;
using Sodalite.UiWidgets;
using Sodalite.Utilities;
using Stratum;
using Stratum.Extensions;
using TNHBGLoader;
using TNHBGLoader.Sosig;
using TNHBGLoader.Soundtrack;
using TNH_BGLoader;
using UnityEngine;
using UnityEngine.UI;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("TNH_BGLoader")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TNH_BGLoader")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("C923A9F9-4F2D-4F11-B5F3-8AB581565FB4")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
public class WavUtility
{
	private const int BlockSize_16Bit = 2;

	public static AudioClip ToAudioClip(string filePath)
	{
		byte[] fileBytes = File.ReadAllBytes(filePath);
		return ToAudioClip(fileBytes);
	}

	public static AudioClip ToAudioClip(byte[] fileBytes, int offsetSamples = 0, string name = "wav")
	{
		int num = BitConverter.ToInt32(fileBytes, 16);
		ushort code = BitConverter.ToUInt16(fileBytes, 20);
		string text = FormatCode(code);
		ushort num2 = BitConverter.ToUInt16(fileBytes, 22);
		int num3 = BitConverter.ToInt32(fileBytes, 24);
		ushort num4 = BitConverter.ToUInt16(fileBytes, 34);
		int num5 = 20 + num + 4;
		int dataSize = BitConverter.ToInt32(fileBytes, num5);
		float[] array = num4 switch
		{
			8 => Convert8BitByteArrayToAudioClipData(fileBytes, num5, dataSize), 
			16 => Convert16BitByteArrayToAudioClipData(fileBytes, num5, dataSize), 
			24 => Convert24BitByteArrayToAudioClipData(fileBytes, num5, dataSize), 
			32 => Convert32BitByteArrayToAudioClipData(fileBytes, num5, dataSize), 
			_ => throw new Exception(num4 + " bit depth is not supported."), 
		};
		AudioClip val = AudioClip.Create(name, array.Length, (int)num2, num3, false);
		val.SetData(array, 0);
		return val;
	}

	private static float[] Convert8BitByteArrayToAudioClipData(byte[] source, int headerOffset, int dataSize)
	{
		int num = BitConverter.ToInt32(source, headerOffset);
		headerOffset += 4;
		float[] array = new float[num];
		sbyte b = sbyte.MaxValue;
		for (int i = 0; i < num; i++)
		{
			array[i] = (float)(int)source[i] / (float)b;
		}
		return array;
	}

	private static float[] Convert16BitByteArrayToAudioClipData(byte[] source, int headerOffset, int dataSize)
	{
		int num = BitConverter.ToInt32(source, headerOffset);
		headerOffset += 4;
		int num2 = 2;
		int num3 = num / num2;
		float[] array = new float[num3];
		short num4 = short.MaxValue;
		int num5 = 0;
		for (int i = 0; i < num3; i++)
		{
			num5 = i * num2 + headerOffset;
			array[i] = (float)BitConverter.ToInt16(source, num5) / (float)num4;
		}
		return array;
	}

	private static float[] Convert24BitByteArrayToAudioClipData(byte[] source, int headerOffset, int dataSize)
	{
		int num = BitConverter.ToInt32(source, headerOffset);
		headerOffset += 4;
		int num2 = 3;
		int num3 = num / num2;
		int num4 = int.MaxValue;
		float[] array = new float[num3];
		byte[] array2 = new byte[4];
		int num5 = 0;
		for (int i = 0; i < num3; i++)
		{
			num5 = i * num2 + headerOffset;
			Buffer.BlockCopy(source, num5, array2, 1, num2);
			array[i] = (float)BitConverter.ToInt32(array2, 0) / (float)num4;
		}
		return array;
	}

	private static float[] Convert32BitByteArrayToAudioClipData(byte[] source, int headerOffset, int dataSize)
	{
		int num = BitConverter.ToInt32(source, headerOffset);
		headerOffset += 4;
		int num2 = 4;
		int num3 = num / num2;
		int num4 = int.MaxValue;
		float[] array = new float[num3];
		int num5 = 0;
		for (int i = 0; i < num3; i++)
		{
			num5 = i * num2 + headerOffset;
			array[i] = (float)BitConverter.ToInt32(source, num5) / (float)num4;
		}
		return array;
	}

	public static byte[] FromAudioClip(AudioClip audioClip)
	{
		string filepath;
		return FromAudioClip(audioClip, out filepath, saveAsFile: false);
	}

	public static byte[] FromAudioClip(AudioClip audioClip, out string filepath, bool saveAsFile = true, string dirname = "recordings")
	{
		MemoryStream stream = new MemoryStream();
		ushort bitDepth = 16;
		int fileSize = audioClip.samples * 2 + 44;
		WriteFileHeader(ref stream, fileSize);
		WriteFileFormat(ref stream, audioClip.channels, audioClip.frequency, bitDepth);
		WriteFileData(ref stream, audioClip, bitDepth);
		byte[] array = stream.ToArray();
		if (saveAsFile)
		{
			filepath = string.Format("{0}/{1}/{2}.{3}", Application.persistentDataPath, dirname, DateTime.UtcNow.ToString("yyMMdd-HHmmss-fff"), "wav");
			Directory.CreateDirectory(Path.GetDirectoryName(filepath));
			File.WriteAllBytes(filepath, array);
		}
		else
		{
			filepath = null;
		}
		stream.Dispose();
		return array;
	}

	private static int WriteFileHeader(ref MemoryStream stream, int fileSize)
	{
		int num = 0;
		int num2 = 12;
		byte[] bytes = Encoding.ASCII.GetBytes("RIFF");
		num += WriteBytesToMemoryStream(ref stream, bytes, "ID");
		int value = fileSize - 8;
		num += WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(value), "CHUNK_SIZE");
		byte[] bytes2 = Encoding.ASCII.GetBytes("WAVE");
		return num + WriteBytesToMemoryStream(ref stream, bytes2, "FORMAT");
	}

	private static int WriteFileFormat(ref MemoryStream stream, int channels, int sampleRate, ushort bitDepth)
	{
		int num = 0;
		int num2 = 24;
		byte[] bytes = Encoding.ASCII.GetBytes("fmt ");
		num += WriteBytesToMemoryStream(ref stream, bytes, "FMT_ID");
		int value = 16;
		num += WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(value), "SUBCHUNK_SIZE");
		ushort value2 = 1;
		num += WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(value2), "AUDIO_FORMAT");
		ushort value3 = Convert.ToUInt16(channels);
		num += WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(value3), "CHANNELS");
		num += WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(sampleRate), "SAMPLE_RATE");
		int value4 = sampleRate * channels * BytesPerSample(bitDepth);
		num += WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(value4), "BYTE_RATE");
		ushort value5 = Convert.ToUInt16(channels * BytesPerSample(bitDepth));
		num += WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(value5), "BLOCK_ALIGN");
		return num + WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(bitDepth), "BITS_PER_SAMPLE");
	}

	private static int WriteFileData(ref MemoryStream stream, AudioClip audioClip, ushort bitDepth)
	{
		int num = 0;
		int num2 = 8;
		float[] array = new float[audioClip.samples * audioClip.channels];
		audioClip.GetData(array, 0);
		byte[] bytes = ConvertAudioClipDataToInt16ByteArray(array);
		byte[] bytes2 = Encoding.ASCII.GetBytes("data");
		num += WriteBytesToMemoryStream(ref stream, bytes2, "DATA_ID");
		int value = Convert.ToInt32(audioClip.samples * 2);
		num += WriteBytesToMemoryStream(ref stream, BitConverter.GetBytes(value), "SAMPLES");
		return num + WriteBytesToMemoryStream(ref stream, bytes, "DATA");
	}

	private static byte[] ConvertAudioClipDataToInt16ByteArray(float[] data)
	{
		MemoryStream memoryStream = new MemoryStream();
		int count = 2;
		short num = short.MaxValue;
		for (int i = 0; i < data.Length; i++)
		{
			memoryStream.Write(BitConverter.GetBytes(Convert.ToInt16(data[i] * (float)num)), 0, count);
		}
		byte[] result = memoryStream.ToArray();
		memoryStream.Dispose();
		return result;
	}

	private static int WriteBytesToMemoryStream(ref MemoryStream stream, byte[] bytes, string tag = "")
	{
		int num = bytes.Length;
		stream.Write(bytes, 0, num);
		return num;
	}

	public static ushort BitDepth(AudioClip audioClip)
	{
		return Convert.ToUInt16((float)(audioClip.samples * audioClip.channels) * audioClip.length / (float)audioClip.frequency);
	}

	private static int BytesPerSample(ushort bitDepth)
	{
		return bitDepth / 8;
	}

	private static int BlockSize(ushort bitDepth)
	{
		return bitDepth switch
		{
			32 => 4, 
			16 => 2, 
			8 => 1, 
			_ => throw new Exception(bitDepth + " bit depth is not supported."), 
		};
	}

	private static string FormatCode(ushort code)
	{
		switch (code)
		{
		case 1:
			return "PCM";
		case 2:
			return "ADPCM";
		case 3:
			return "IEEE";
		case 7:
			return "μ-law";
		case 65534:
			return "WaveFormatExtensable";
		default:
			Debug.LogWarning((object)("Unknown wav code format:" + code));
			return "";
		}
	}
}
namespace TNHBGLoader
{
	public class AnnouncerYamlfest
	{
		public string Name { get; set; }

		[YamlMember(Alias = "Guid")]
		public string GUID { get; set; }

		public string VoiceLines { get; set; }

		[CanBeNull]
		public string Location { get; set; }

		[DefaultValue(0.2f)]
		public float FrontPadding { get; set; }

		[DefaultValue(1.2f)]
		public float BackPadding { get; set; }
	}
	public static class Common
	{
		public static int Wrap(this int self, int min, int max)
		{
			if (self < min)
			{
				return max - (self + min + 1);
			}
			if (self > max)
			{
				return min + (self - max - 1);
			}
			return self;
		}

		public static int Wrap(this int self, int[] range)
		{
			return self.Wrap(range[0], range[1]);
		}
	}
	public class GeneralAPI : MonoBehaviour
	{
		public static Texture2D GetIcon(string guid, string[] paths)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			PluginMain.LogSpam("Loading image for " + guid);
			foreach (string text in paths)
			{
				if (File.Exists(text))
				{
					byte[] array = File.ReadAllBytes(text);
					Texture2D val = new Texture2D(1, 1);
					val.LoadImage(array);
					if ((Object)(object)val != (Object)null)
					{
						PluginMain.LogSpam("Loading icon from " + text);
						return val;
					}
				}
			}
			PluginMain.DebugLog.LogError((object)("Cannot find icon for " + guid + "!\nPossible locations:\n" + string.Join("\n", paths)));
			return null;
		}

		public static bool IfIsInRange(float input, float min, float max)
		{
			return Mathf.Clamp(input, min, max) == input;
		}
	}
	public class Patcher_FistVR
	{
		[HarmonyPatch(typeof(Sosig), "Start")]
		[HarmonyPrefix]
		public static bool Sosig_Start_SetVLS(Sosig __instance)
		{
			string name = ((Object)__instance.Speech).name;
			if (SosigVLSAPI.VLSGuidOrder.Contains(name))
			{
				if (SosigVLSAPI.CurrentSosigVlsOfVlsSet(name).guid == "h3vr.default")
				{
					return true;
				}
				if (SosigVLSAPI.CurrentSosigVlsOfVlsSet(name).guid == "h3vr.zosig")
				{
					return true;
				}
				__instance.Speech = SosigVLSAPI.CurrentSosigVlsOfVlsSet(name).SpeechSet;
			}
			return true;
		}

		[HarmonyPatch(typeof(FVRFMODController), "SetMasterVolume")]
		[HarmonyPrefix]
		public static bool FVRFMODController_SetMasterVolume_IncludeBGMVol(ref float i)
		{
			i *= PluginMain.BackgroundMusicVolume.Value;
			return true;
		}

		[HarmonyPatch(typeof(SM), "PlayCoreSoundDelayed")]
		[HarmonyPrefix]
		public static bool SM_PlayCoreSoundDelayed_IncludeAnnouncerVol(ref AudioEvent ClipSet)
		{
			ClipSet.VolumeRange.x *= PluginMain.AnnouncerMusicVolume.Value;
			ClipSet.VolumeRange.y *= PluginMain.AnnouncerMusicVolume.Value;
			return true;
		}

		[HarmonyPatch(typeof(TNH_Manager), "VoiceUpdate")]
		[HarmonyPrefix]
		public static bool TNH_Manager_VoiceUpdate_AccountForVoiceLinePadding(TNH_Manager __instance)
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: 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_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.timeTilLineClear >= 0f)
			{
				__instance.timeTilLineClear -= Time.deltaTime;
				return true;
			}
			if (__instance.QueuedLines.Count > 0)
			{
				TNH_VoiceLineID key = __instance.QueuedLines.Dequeue();
				if (__instance.voiceDic_Standard.ContainsKey(key))
				{
					int index = Random.Range(0, __instance.voiceDic_Standard[key].Count);
					AudioClip val = __instance.voiceDic_Standard[key][index];
					AudioEvent val2 = new AudioEvent();
					val2.Clips.Add(val);
					val2.PitchRange = new Vector2(1f, 1f);
					val2.VolumeRange = new Vector2(0.6f, 0.6f);
					__instance.timeTilLineClear = val.length + AnnouncerAPI.CurrentAnnouncer.BackPadding;
					SM.PlayCoreSoundDelayed((FVRPooledAudioType)20, val2, ((Component)__instance).transform.position, AnnouncerAPI.CurrentAnnouncer.FrontPadding);
				}
			}
			return true;
		}

		[HarmonyPatch(typeof(TNH_UIManager), "Start")]
		[HarmonyPostfix]
		public static void TNH_UIManager_SpawnPanel()
		{
			//IL_0037: 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_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			TNHPanel tNHPanel = new TNHPanel();
			tNHPanel.Initialize("tnh", hasBgms: true, hasVls: true, hasAnnouncers: true);
			GameObject orCreatePanel = tNHPanel.Panel.GetOrCreatePanel();
			orCreatePanel.transform.position = new Vector3(0.0561f, 1f, 7.1821f);
			orCreatePanel.transform.localEulerAngles = new Vector3(315f, 0f, 0f);
			orCreatePanel.GetComponent<FVRPhysicalObject>().SetIsKinematicLocked(true);
			GameObject val = new GameObject();
			IconDisplayWaitForInit iconDisplayWaitForInit = val.AddComponent<IconDisplayWaitForInit>();
			iconDisplayWaitForInit.panel = orCreatePanel;
			iconDisplayWaitForInit.bgmpanel = tNHPanel;
			if (!PluginMain.IsSoundtrack.Value)
			{
				for (int i = 0; i < BankAPI.LoadedBankLocations.Count; i++)
				{
					if (Path.GetFileNameWithoutExtension(BankAPI.LoadedBankLocations[i]) == PluginMain.LastLoadedBank.Value)
					{
						BankAPI.SwapBank(i);
						break;
					}
				}
			}
			AnnouncerAPI.CurrentAnnouncerIndex = AnnouncerAPI.GetAnnouncerIndexFromGUID(PluginMain.LastLoadedAnnouncer.Value);
		}

		[HarmonyPatch(typeof(TNH_Manager), "Start")]
		[HarmonyPrefix]
		public static bool TNH_Manager_Start_NukeSongSnippets()
		{
			BankAPI.NukeSongSnippets();
			return true;
		}

		[HarmonyPatch(typeof(TNH_Manager), "InitLibraries")]
		[HarmonyPrefix]
		public static bool TNH_Manager_InitLibraries_LoadAnnouncers(TNH_Manager __instance)
		{
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Expected O, but got Unknown
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();
			AnnouncerManifest currentAnnouncer = AnnouncerAPI.CurrentAnnouncer;
			if (currentAnnouncer.GUID == "h3vr.corrupted")
			{
				TNH_VoiceDatabase voiceDB = __instance.VoiceDB;
				foreach (TNH_VoiceLine line in voiceDB.Lines)
				{
					line.Clip_Standard = line.Clip_Corrupted;
				}
				__instance.VoiceDB = voiceDB;
				return true;
			}
			if (currentAnnouncer.GUID == "h3vr.default")
			{
				return true;
			}
			TNH_VoiceDatabase val = ScriptableObject.CreateInstance<TNH_VoiceDatabase>();
			val.Lines = new List<TNH_VoiceLine>();
			foreach (VoiceLine voiceLine in currentAnnouncer.VoiceLines)
			{
				AudioClip val2 = null;
				val2 = AnnouncerAPI.GetAudioFromFile(voiceLine.ClipPath);
				if ((Object)(object)val2 == (Object)null)
				{
					PluginMain.DebugLog.LogWarning((object)("Failed to load from " + voiceLine.ClipPath + "!"));
				}
				TNH_VoiceLine val3 = new TNH_VoiceLine();
				val3.ID = voiceLine.ID;
				val3.Clip_Standard = val2;
				val.Lines.Add(val3);
			}
			foreach (int vlid in Enum.GetValues(typeof(TNH_VoiceLineID)))
			{
				List<TNH_VoiceLine> list = val.Lines.FindAll((TNH_VoiceLine line) => (int)line.ID == vlid);
				if (list.Count == 0)
				{
					string[] unusedVoicelines = AnnouncerAPI.UnusedVoicelines;
					TNH_VoiceLineID val4 = (TNH_VoiceLineID)vlid;
					if (unusedVoicelines.Any(((object)(TNH_VoiceLineID)(ref val4)).ToString().Contains))
					{
						ManualLogSource debugLog = PluginMain.DebugLog;
						string[] obj = new string[5] { "ID ", null, null, null, null };
						val4 = (TNH_VoiceLineID)vlid;
						obj[1] = ((object)(TNH_VoiceLineID)(ref val4)).ToString();
						obj[2] = " is empty for ";
						obj[3] = currentAnnouncer.GUID;
						obj[4] = "! Was this intentional?";
						debugLog.LogWarning((object)string.Concat(obj));
					}
					List<TNH_VoiceLine> second = __instance.VoiceDB.Lines.FindAll((TNH_VoiceLine line) => (int)line.ID == vlid);
					val.Lines = val.Lines.Concat(second).ToList();
				}
			}
			stopwatch.Stop();
			__instance.VoiceDB = val;
			PluginMain.LogSpam(stopwatch.ElapsedMilliseconds + "ms to load all voicelines!");
			PluginMain.DebugLog.LogInfo((object)("TNH run started! PTNHBGML Info:\nLoaded announcer: " + AnnouncerAPI.CurrentAnnouncer.GUID + "\nLoaded song: " + BankAPI.GetNameFromIndex(BankAPI.CurrentBankIndex)));
			return true;
		}
	}
	public class Patcher_FMOD
	{
		[HarmonyPatch(typeof(RuntimeUtils), "GetBankPath")]
		[HarmonyPrefix]
		public static bool FMODRuntimeUtilsPatch_GetBankPath(ref string bankName, ref string __result)
		{
			string streamingAssetsPath = Application.streamingAssetsPath;
			if (Path.GetExtension(bankName) != ".bank")
			{
				if (Path.IsPathRooted(bankName))
				{
					__result = bankName + ".bank";
					return false;
				}
				__result = $"{streamingAssetsPath}/{bankName}.bank";
				return false;
			}
			if (Path.IsPathRooted(bankName))
			{
				__result = bankName;
				return false;
			}
			__result = $"{streamingAssetsPath}/{bankName}";
			return false;
		}

		[HarmonyPatch(typeof(RuntimeManager))]
		[HarmonyPatch("LoadBank", new Type[]
		{
			typeof(string),
			typeof(bool)
		})]
		[HarmonyPrefix]
		public static bool FMODRuntimeManagerPatch_LoadBank(ref string bankName)
		{
			if (bankName == "MX_TAH" && !BankAPI.BanksEmptyOrNull)
			{
				if (BankAPI.CurrentBankLocation == "Select Random" && !PluginMain.IsSoundtrack.Value)
				{
					PluginMain.DebugLog.LogInfo((object)("Activated Random. Current Bank: " + BankAPI.CurrentBankLocation));
					int num = Random.Range(1, BankAPI.LoadedBankLocations.Count + SoundtrackAPI.Soundtracks.Count);
					PluginMain.DebugLog.LogInfo((object)$"Selected: {num}");
					if (num < BankAPI.LoadedBankLocations.Count)
					{
						PluginMain.IsSoundtrack.Value = false;
						BankAPI.CurrentBankIndex = num;
					}
					else
					{
						PluginMain.IsSoundtrack.Value = true;
						SoundtrackAPI.SelectedSoundtrackIndex = num - BankAPI.LoadedBankLocations.Count;
					}
				}
				PluginMain.DebugLog.LogInfo((object)("IsSoundtrack loading bank/soundtrack time: " + PluginMain.IsSoundtrack.Value));
				if (!PluginMain.IsSoundtrack.Value)
				{
					PluginMain.DebugLog.LogInfo((object)("Injecting bank " + Path.GetFileName(BankAPI.CurrentBankLocation) + " into TNH!"));
					bankName = BankAPI.CurrentBankLocation;
				}
				else
				{
					PluginMain.DebugLog.LogInfo((object)("Loading soundtrack " + SoundtrackAPI.Soundtracks[SoundtrackAPI.SelectedSoundtrackIndex].Guid + " into TNH!"));
				}
			}
			return true;
		}
	}
	[BepInPlugin("dll.potatoes.ptnhbgml", "Potatoes' Take And Hold Background Music Loader", "4.0.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency("stratum", "1.1.0")]
	public class PluginMain : StratumPlugin
	{
		public static ConfigEntry<float> BackgroundMusicVolume;

		public static ConfigEntry<float> AnnouncerMusicVolume;

		public static ConfigEntry<string> LastLoadedBank;

		public static ConfigEntry<string> LastLoadedAnnouncer;

		public static ConfigEntry<string> LastLoadedSosigVLS;

		public static ConfigEntry<bool> EnableDebugLogging;

		public static ConfigEntry<bool> IsSoundtrack;

		public static ConfigEntry<bool> LoadSoundtracksOnStartup;

		internal static ManualLogSource DebugLog;

		private IDeserializer _deserializer;

		public static string AssemblyDirectory
		{
			get
			{
				string codeBase = Assembly.GetExecutingAssembly().CodeBase;
				UriBuilder uriBuilder = new UriBuilder(codeBase);
				string path = Uri.UnescapeDataString(uriBuilder.Path);
				return Path.GetDirectoryName(path);
			}
		}

		public static void LogSpam(object data)
		{
			if (EnableDebugLogging.Value)
			{
				DebugLog.LogInfo(data);
			}
		}

		public void Awake()
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			DebugLog = ((BaseUnityPlugin)this).Logger;
			InitConfig();
			BankAPI.LoadedBankLocations = (from x in BankAPI.GetLegacyBanks()
				orderby x
				select x).ToList();
			BankAPI.LoadedBankLocations = BankAPI.LoadedBankLocations.Distinct().ToList();
			BankAPI.LoadedBankLocations.Insert(0, "Select Random");
			_deserializer = ((BuilderSkeleton<DeserializerBuilder>)new DeserializerBuilder()).WithNamingConvention(UnderscoredNamingConvention.Instance).Build();
			AnnouncerAPI.LoadedAnnouncers.Add(AnnouncerManifest.RandomAnnouncer);
			AnnouncerAPI.LoadedAnnouncers.Add(AnnouncerManifest.DefaultAnnouncer);
			AnnouncerAPI.LoadedAnnouncers.Add(AnnouncerManifest.CorruptedAnnouncer);
			SosigVLSDefinitionSetLaod(new VLSGuidToNameDefinitionsYamlfest
			{
				GuidsToNames = new string[2] { "SosigSpeech_Anton:Default", "SosigSpeech_Zosig:Zosig" }
			});
			SosigVLSAPI.LoadedSosigVLSs.Add(SosigManifest.RandomSosigVLS());
			SosigVLSAPI.LoadedSosigVLSs.Add(SosigManifest.DefaultSosigVLS());
			SosigVLSAPI.LoadedSosigVLSs.Add(SosigManifest.DefaultZosigVLS());
			Harmony.CreateAndPatchAll(typeof(Patcher_FMOD), (string)null);
			Harmony.CreateAndPatchAll(typeof(Patcher_FistVR), (string)null);
			Harmony.CreateAndPatchAll(typeof(TnHSoundtrackInterface), (string)null);
		}

		public void InitConfig()
		{
			EnableDebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable Debug Logs", false, "Spams your log with debug info.");
			BackgroundMusicVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "BGM Volume", 1f, "Changes the magnitude of the BGM volume. Must be between 0 and 4.");
			BackgroundMusicVolume.Value = Mathf.Clamp(BackgroundMusicVolume.Value, 0f, 4f);
			AnnouncerMusicVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Announcer Volume", 1f, "Changes the magnitude of the Announcer volume. Must be between 0 and 20. (Please don't set the volume to 2000%.)");
			AnnouncerMusicVolume.Value = Mathf.Clamp(BackgroundMusicVolume.Value, 0f, 20f);
			LastLoadedBank = ((BaseUnityPlugin)this).Config.Bind<string>("no touchy", "Saved Bank", "MX_TAH", "Not meant to be changed manually. This autosaves your last bank used, so you don't have to reset it every time you launch H3.");
			LastLoadedAnnouncer = ((BaseUnityPlugin)this).Config.Bind<string>("no touchy", "Saved Announcer", "h3vr.default", "Not meant to be changed manually. This autosaves your last announcer used, so you don't have to reset it every time you launch H3.");
			LastLoadedSosigVLS = ((BaseUnityPlugin)this).Config.Bind<string>("no touchy", "Saved Sosig VLS", "h3vr.default", "Not meant to be changed manually. This autosaves your last sosig set used, so you don't have to reset it every time you launch H3.");
			IsSoundtrack = ((BaseUnityPlugin)this).Config.Bind<bool>("no touchy", "Saved Is Soundtrack", false, "Not meant to be changed manually. This autosaves your last sosig set used, so you don't have to reset it every time you launch H3.");
			LoadSoundtracksOnStartup = ((BaseUnityPlugin)this).Config.Bind<bool>("no touchy", "Load Soundtracks On Startup", false, "Debug. Loads ALL soundtracks on H3 start, not the current soundtrack when loading TnH. Enable only to catch soundtrack loading bugs on start.");
		}

		public override void OnSetup(IStageContext<Empty> ctx)
		{
			ctx.Loaders.Add("tnhbankfile", LoadTNHBankFile);
			ctx.Loaders.Add("tnhannouncer", LoadAnnouncer);
			ctx.Loaders.Add("tnhbgmlsosigvls", LoadSosigVoiceLineSet);
			ctx.Loaders.Add("tnhbgmlvlsdictionary", LoadSosigVoiceLineDefinitionSet);
			ctx.Loaders.Add("tnhbgml_soundtrack", LoadSoundtrack);
		}

		public Empty LoadTNHBankFile(FileSystemInfo handle)
		{
			//IL_0032: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			FileInfo fileInfo = ExtFileSystemInfo.ConsumeFile(handle);
			if (!BankAPI.LoadedBankLocations.Contains(fileInfo.FullName))
			{
				BankAPI.LoadedBankLocations.Add(fileInfo.FullName);
			}
			return default(Empty);
		}

		public Empty LoadAnnouncer(FileSystemInfo handle)
		{
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			FileInfo fileInfo = ExtFileSystemInfo.ConsumeFile(handle);
			AnnouncerYamlfest announcerYamlfest = new AnnouncerYamlfest();
			announcerYamlfest = _deserializer.Deserialize<AnnouncerYamlfest>(File.ReadAllText(fileInfo.FullName));
			DebugLog.LogInfo((object)("Loaded announcer file " + announcerYamlfest.GUID));
			announcerYamlfest.Location = fileInfo.FullName;
			AnnouncerAPI.LoadedAnnouncers.Add(AnnouncerAPI.GetManifestFromYamlfest(announcerYamlfest));
			return default(Empty);
		}

		public Empty LoadSosigVoiceLineSet(FileSystemInfo handle)
		{
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			FileInfo fileInfo = ExtFileSystemInfo.ConsumeFile(handle);
			SosigYamlfest sosigYamlfest = new SosigYamlfest();
			sosigYamlfest = _deserializer.Deserialize<SosigYamlfest>(File.ReadAllText(fileInfo.FullName));
			DebugLog.LogInfo((object)("Loaded Sosig Voiceline set " + sosigYamlfest.GUID));
			sosigYamlfest.Location = fileInfo.FullName;
			SosigVLSAPI.LoadedSosigVLSs.Add(SosigVLSAPI.GetManifestFromYamlfest(sosigYamlfest));
			return default(Empty);
		}

		public Empty LoadSosigVoiceLineDefinitionSet(FileSystemInfo handle)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			FileInfo fileInfo = ExtFileSystemInfo.ConsumeFile(handle);
			VLSGuidToNameDefinitionsYamlfest yamlfest = _deserializer.Deserialize<VLSGuidToNameDefinitionsYamlfest>(File.ReadAllText(fileInfo.FullName));
			SosigVLSDefinitionSetLaod(yamlfest);
			return default(Empty);
		}

		public Empty LoadSoundtrack(FileSystemInfo handle)
		{
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: 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)
			FileInfo fileInfo = ExtFileSystemInfo.ConsumeFile(handle);
			SoundtrackYamlfest soundtrackYamlfest = _deserializer.Deserialize<SoundtrackYamlfest>(File.ReadAllText(fileInfo.FullName));
			DebugLog.LogInfo((object)("Soundtrack detected: " + soundtrackYamlfest.Name));
			SoundtrackManifest soundtrackManifest = soundtrackYamlfest.ToManifest(fileInfo.FullName);
			if (LoadSoundtracksOnStartup.Value)
			{
				soundtrackManifest.AssembleMusicData();
			}
			SoundtrackAPI.Soundtracks.Add(soundtrackManifest);
			return default(Empty);
		}

		private void SosigVLSDefinitionSetLaod(VLSGuidToNameDefinitionsYamlfest yamlfest)
		{
			for (int i = 0; i < yamlfest.GuidsToNames.Length; i++)
			{
				string[] array = yamlfest.GuidsToNames[i].Split(new char[1] { ':' });
				DebugLog.LogInfo((object)("Loading Sosig Voiceline Dictionary Set defining: " + array[0]));
				SosigVLSAPI.VLSGuidToName[array[0]] = array[1];
				SosigVLSAPI.CurrentSosigVLSIndex[array[0]] = 1;
				if (array[0] == "SosigSpeech_Zosig")
				{
					SosigVLSAPI.CurrentSosigVLSIndex[array[0]] = 2;
				}
				if (!SosigVLSAPI.VLSGuidOrder.Contains(array[0]))
				{
					SosigVLSAPI.VLSGuidOrder.Add(array[0]);
				}
			}
		}

		public override IEnumerator OnRuntime(IStageContext<IEnumerator> ctx)
		{
			yield break;
		}
	}
	internal static class PluginDetails
	{
		public const string GUID = "dll.potatoes.ptnhbgml";

		public const string NAME = "Potatoes' Take And Hold Background Music Loader";

		public const string VERS = "4.0.3";
	}
	public class Save
	{
		public string LastLoadedBank { get; set; }

		public string LastLoadedAnnouncer { get; set; }

		public string[] LastLoadedVls { get; set; }

		public string[] LastLoadedVlsSets { get; set; }

		public void Write()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			string contents = Path.Combine(Directory.GetCurrentDirectory(), "lastloaded.yaml");
			ISerializer val = ((BuilderSkeleton<SerializerBuilder>)new SerializerBuilder()).WithNamingConvention(UnderscoredNamingConvention.Instance).Build();
			string path = val.Serialize((object)this);
			File.WriteAllText(path, contents);
		}

		public static Save Read()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			string path = Path.Combine(Directory.GetCurrentDirectory(), "lastloaded.yaml");
			IDeserializer val = ((BuilderSkeleton<DeserializerBuilder>)new DeserializerBuilder()).WithNamingConvention(UnderscoredNamingConvention.Instance).Build();
			return val.Deserialize<Save>(File.ReadAllText(path));
		}
	}
	public class SosigYamlfest
	{
		public string Name { get; set; }

		[YamlMember(Alias = "Guid")]
		public string GUID { get; set; }

		public string VoiceLines { get; set; }

		public string? Location { get; set; }

		public float BasePitch { get; set; }

		public float BaseVolume { get; set; }

		public bool ForceDeathSpeech { get; set; }

		public bool UseAltDeathOnHeadExplode { get; set; }

		public bool LessTalkativeSkirmish { get; set; }
	}
	public class VLSGuidToNameDefinitionsYamlfest
	{
		public string[] GuidsToNames { get; set; }
	}
	public class TNHPanel : MonoBehaviour
	{
		public enum TNHPstates
		{
			BGM,
			Announcer,
			Sosig_Voicelines
		}

		public struct BankOrSoundtrack
		{
			public bool IsBank;

			public string BankGuid;

			public string SoundtrackGuid;

			public string Name;
		}

		public LockablePanel Panel;

		public TNHPstates TNHPstate = TNHPstates.BGM;

		public string GameMode;

		public List<BankOrSoundtrack> BGMs;

		public bool HasBgms = true;

		public bool HasVls = true;

		public bool HasAnnouncers = true;

		private TextWidget _bankText;

		private TextWidget _volumeText;

		private ButtonWidget[] _musicButtons = (ButtonWidget[])(object)new ButtonWidget[8];

		private ButtonWidget[] _volControls = (ButtonWidget[])(object)new ButtonWidget[2];

		private ButtonWidget[] _cycleControls = (ButtonWidget[])(object)new ButtonWidget[2];

		private ButtonWidget _switchstate;

		private int _firstMusicIndex;

		public RawImage icondisplay;

		private int _selectedVLSSet;

		public void Initialize(string gameMode, bool hasBgms = true, bool hasVls = false, bool hasAnnouncers = false)
		{
			PluginMain.DebugLog.LogInfo((object)$"Initializing. Gamemode: {gameMode}, {hasBgms} {hasVls} {hasAnnouncers}");
			GameMode = gameMode;
			HasBgms = hasBgms;
			HasVls = hasVls;
			HasAnnouncers = hasAnnouncers;
			PluginMain.DebugLog.LogInfo((object)$"Initialized. Gamemode: {GameMode}, {HasBgms} {HasVls} {HasAnnouncers}");
			if (!hasBgms)
			{
				if (!hasAnnouncers)
				{
					TNHPstate = TNHPstates.Sosig_Voicelines;
				}
				if (!hasVls)
				{
					TNHPstate = TNHPstates.Announcer;
				}
			}
			BGMs = new List<BankOrSoundtrack>();
			if (GameMode == "tnh")
			{
				foreach (string loadedBankLocation in BankAPI.LoadedBankLocations)
				{
					BankOrSoundtrack item = default(BankOrSoundtrack);
					item.IsBank = true;
					item.BankGuid = loadedBankLocation;
					item.Name = BankAPI.GetNameFromLocation(loadedBankLocation);
					BGMs.Add(item);
				}
			}
			foreach (SoundtrackManifest soundtrack in SoundtrackAPI.Soundtracks)
			{
				PluginMain.DebugLog.LogInfo((object)("Gamemode: " + GameMode + ", Soundtrack GM: " + soundtrack.GameMode + " of " + soundtrack.Guid));
				if (!(soundtrack.GameMode != GameMode))
				{
					BankOrSoundtrack item2 = default(BankOrSoundtrack);
					item2.IsBank = false;
					item2.SoundtrackGuid = soundtrack.Guid;
					item2.Name = soundtrack.Name;
					BGMs.Add(item2);
				}
			}
			for (int i = 0; i < BGMs.Count(); i++)
			{
				PluginMain.DebugLog.LogInfo((object)$"BGM Loaded: {BGMs[i].Name}, index {i}, IsBank {BGMs[i].IsBank}");
			}
		}

		public TNHPanel()
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			Panel = new LockablePanel();
			Panel.Configure += ConfigurePanel;
			Panel.TextureOverride = SodaliteUtils.LoadTextureFromBytes(SodaliteUtils.GetResource(Assembly.GetExecutingAssembly(), "panel.png"));
		}

		private void ConfigurePanel(GameObject panel)
		{
			GameObject gameObject = ((Component)panel.transform.Find("OptionsCanvas_0_Main/Canvas")).gameObject;
			UiWidget.CreateAndConfigureWidget<GridLayoutWidget>(gameObject, (Action<GridLayoutWidget>)delegate(GridLayoutWidget widget)
			{
				//IL_0016: 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_0038: Unknown result type (might be due to invalid IL or missing references)
				//IL_0053: Unknown result type (might be due to invalid IL or missing references)
				//IL_006e: Unknown result type (might be due to invalid IL or missing references)
				//IL_007f: Unknown result type (might be due to invalid IL or missing references)
				//IL_009a: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
				((Transform)((UiWidget)widget).RectTransform).localScale = new Vector3(0.07f, 0.07f, 0.07f);
				((Transform)((UiWidget)widget).RectTransform).localPosition = Vector3.zero;
				((UiWidget)widget).RectTransform.anchoredPosition = Vector2.zero;
				((UiWidget)widget).RectTransform.sizeDelta = new Vector2(528.5714f, 342.85715f);
				((UiWidget)widget).RectTransform.pivot = new Vector2(0.5f, 1f);
				((Transform)((UiWidget)widget).RectTransform).localRotation = Quaternion.identity;
				((LayoutWidget<GridLayoutGroup>)(object)widget).LayoutGroup.cellSize = new Vector2(171f, 50f);
				((LayoutWidget<GridLayoutGroup>)(object)widget).LayoutGroup.spacing = Vector2.one * 4f;
				((LayoutWidget<GridLayoutGroup>)(object)widget).LayoutGroup.startCorner = (Corner)0;
				((LayoutWidget<GridLayoutGroup>)(object)widget).LayoutGroup.startAxis = (Axis)0;
				((LayoutGroup)((LayoutWidget<GridLayoutGroup>)(object)widget).LayoutGroup).childAlignment = (TextAnchor)1;
				((LayoutWidget<GridLayoutGroup>)(object)widget).LayoutGroup.constraint = (Constraint)1;
				((LayoutWidget<GridLayoutGroup>)(object)widget).LayoutGroup.constraintCount = 3;
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001a: Unknown result type (might be due to invalid IL or missing references)
					//IL_0024: Expected O, but got Unknown
					//IL_0030: Unknown result type (might be due to invalid IL or missing references)
					button.ButtonText.text = "Cycle List Up";
					button.AddButtonListener(new ButtonClickEvent(UpdateMusicList));
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
					_cycleControls[0] = button;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Expected O, but got Unknown
					//IL_003d: Unknown result type (might be due to invalid IL or missing references)
					int num8 = 0;
					button.ButtonText.text = GetNameWithOffset(num8);
					button.AddButtonListener(new ButtonClickEvent(SetCurrentItem));
					_musicButtons[num8] = button;
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Expected O, but got Unknown
					//IL_003d: Unknown result type (might be due to invalid IL or missing references)
					int num7 = 1;
					button.ButtonText.text = GetNameWithOffset(num7);
					button.AddButtonListener(new ButtonClickEvent(SetCurrentItem));
					_musicButtons[num7] = button;
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<TextWidget>((Action<TextWidget>)delegate(TextWidget text)
				{
					//IL_0049: Unknown result type (might be due to invalid IL or missing references)
					text.Text.text = "Selected:\n" + GetCurrentSelectedItemName();
					_bankText = text;
					text.Text.alignment = (TextAnchor)4;
					Text text6 = text.Text;
					text6.fontSize = text6.fontSize;
					((Transform)((UiWidget)text).RectTransform).localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Expected O, but got Unknown
					//IL_003d: Unknown result type (might be due to invalid IL or missing references)
					int num6 = 2;
					button.ButtonText.text = GetNameWithOffset(num6);
					button.AddButtonListener(new ButtonClickEvent(SetCurrentItem));
					_musicButtons[num6] = button;
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Expected O, but got Unknown
					//IL_003d: Unknown result type (might be due to invalid IL or missing references)
					int num5 = 3;
					button.ButtonText.text = GetNameWithOffset(num5);
					button.AddButtonListener(new ButtonClickEvent(SetCurrentItem));
					_musicButtons[num5] = button;
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001a: Unknown result type (might be due to invalid IL or missing references)
					//IL_0024: Expected O, but got Unknown
					//IL_0030: Unknown result type (might be due to invalid IL or missing references)
					button.ButtonText.text = "Cycle List Down";
					button.AddButtonListener(new ButtonClickEvent(UpdateMusicList));
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
					_cycleControls[1] = button;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Expected O, but got Unknown
					//IL_003d: Unknown result type (might be due to invalid IL or missing references)
					int num4 = 4;
					button.ButtonText.text = GetNameWithOffset(num4);
					button.AddButtonListener(new ButtonClickEvent(SetCurrentItem));
					_musicButtons[num4] = button;
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Expected O, but got Unknown
					//IL_003d: Unknown result type (might be due to invalid IL or missing references)
					int num3 = 5;
					button.ButtonText.text = GetNameWithOffset(num3);
					button.AddButtonListener(new ButtonClickEvent(SetCurrentItem));
					_musicButtons[num3] = button;
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<TextWidget>((Action<TextWidget>)delegate(TextWidget text)
				{
					text.Text.text = "";
					text.Text.alignment = (TextAnchor)4;
					Text text5 = text.Text;
					text5.fontSize += 5;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Expected O, but got Unknown
					//IL_003d: Unknown result type (might be due to invalid IL or missing references)
					int num2 = 6;
					button.ButtonText.text = GetNameWithOffset(num2);
					button.AddButtonListener(new ButtonClickEvent(SetCurrentItem));
					_musicButtons[num2] = button;
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Expected O, but got Unknown
					//IL_003d: Unknown result type (might be due to invalid IL or missing references)
					int num = 7;
					button.ButtonText.text = GetNameWithOffset(num);
					button.AddButtonListener(new ButtonClickEvent(SetCurrentItem));
					_musicButtons[num] = button;
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<TextWidget>((Action<TextWidget>)delegate(TextWidget text)
				{
					text.Text.text = "";
					text.Text.alignment = (TextAnchor)4;
					Text text4 = text.Text;
					text4.fontSize += 5;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001a: Unknown result type (might be due to invalid IL or missing references)
					//IL_0024: Expected O, but got Unknown
					//IL_0030: Unknown result type (might be due to invalid IL or missing references)
					button.ButtonText.text = "BGM";
					button.AddButtonListener(new ButtonClickEvent(SwitchState));
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
					_switchstate = button;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<TextWidget>((Action<TextWidget>)delegate(TextWidget text)
				{
					text.Text.text = "";
					text.Text.alignment = (TextAnchor)4;
					Text text3 = text.Text;
					text3.fontSize += 5;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001a: Unknown result type (might be due to invalid IL or missing references)
					//IL_0024: Expected O, but got Unknown
					//IL_0030: Unknown result type (might be due to invalid IL or missing references)
					button.ButtonText.text = "Decrease volume";
					button.AddButtonListener(new ButtonClickEvent(UpdateVolume));
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
					_volControls[0] = button;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<TextWidget>((Action<TextWidget>)delegate(TextWidget text)
				{
					text.Text.text = GetVolumePercent();
					_volumeText = text;
					text.Text.alignment = (TextAnchor)4;
					Text text2 = text.Text;
					text2.fontSize += 5;
				});
				((LayoutWidget<GridLayoutGroup>)(object)widget).AddChild<ButtonWidget>((Action<ButtonWidget>)delegate(ButtonWidget button)
				{
					//IL_001a: Unknown result type (might be due to invalid IL or missing references)
					//IL_0024: Expected O, but got Unknown
					//IL_0030: Unknown result type (might be due to invalid IL or missing references)
					button.ButtonText.text = "Increase volume";
					button.AddButtonListener(new ButtonClickEvent(UpdateVolume));
					((Component)button.ButtonText).transform.localRotation = Quaternion.identity;
					_volControls[1] = button;
				});
			});
		}

		public void SwitchState(object sender, ButtonClickEventArgs args)
		{
			if (TNHPstate == TNHPstates.BGM)
			{
				TNHPstate = TNHPstates.Announcer;
			}
			else if (TNHPstate == TNHPstates.Announcer)
			{
				TNHPstate = TNHPstates.Sosig_Voicelines;
			}
			else if (TNHPstate == TNHPstates.Sosig_Voicelines)
			{
				TNHPstate = TNHPstates.BGM;
			}
			_switchstate.ButtonText.text = TNHPstate.ToString().Replace('_', ' ');
			_bankText.Text.text = "Selected:\n" + GetCurrentSelectedItemName();
			_firstMusicIndex = 0;
			int num = 0;
			UpdateMusicList(null, null);
			UpdateVolume(null, null);
			SetIcon();
		}

		private void UpdateMusicList(object sender, ButtonClickEventArgs args)
		{
			int num = 0;
			if (sender != null)
			{
				if ((Object)((sender is ButtonWidget) ? sender : null) == (Object)(object)_cycleControls[0])
				{
					num = -1;
				}
				else if ((Object)((sender is ButtonWidget) ? sender : null) == (Object)(object)_cycleControls[1])
				{
					num = 1;
				}
			}
			int num2 = 4;
			num = num2 * num;
			int num3 = _firstMusicIndex + num;
			bool flag = num3 < 0;
			switch (TNHPstate)
			{
			case TNHPstates.BGM:
				if (num3 >= BGMs.Count)
				{
					flag = true;
				}
				break;
			case TNHPstates.Announcer:
				if (num3 >= AnnouncerAPI.LoadedAnnouncers.Count)
				{
					flag = true;
				}
				break;
			case TNHPstates.Sosig_Voicelines:
				if (num3 >= SosigVLSAPI.LoadedSosigVLSs.Count)
				{
					flag = true;
				}
				break;
			}
			if (flag)
			{
				WristMenuAPI.Instance.Aud.PlayOneShot(WristMenuAPI.Instance.AudClip_Err);
				return;
			}
			_firstMusicIndex = num3;
			for (int i = 0; i < _musicButtons.Length; i++)
			{
				_musicButtons[i].ButtonText.text = GetNameWithOffset(i);
			}
		}

		private void CycleVLS(object? sender)
		{
			int[] range = new int[2]
			{
				0,
				SosigVLSAPI.VLSGuidOrder.Count - 1
			};
			if (sender != null)
			{
				_selectedVLSSet = (_selectedVLSSet + (((Object)((sender is ButtonWidget) ? sender : null) == (Object)(object)_volControls[1]) ? 1 : (-1))).Wrap(range);
			}
			_volumeText.Text.text = SosigVLSAPI.VLSGuidToName[SosigVLSAPI.VLSGuidOrder[_selectedVLSSet]];
			_volControls[0].ButtonText.text = SosigVLSAPI.VLSGuidToName[SosigVLSAPI.VLSGuidOrder[(_selectedVLSSet - 1).Wrap(range)]];
			_volControls[1].ButtonText.text = SosigVLSAPI.VLSGuidToName[SosigVLSAPI.VLSGuidOrder[(_selectedVLSSet + 1).Wrap(range)]];
			PlaySnippet(SosigVLSAPI.GetRandomPreview(SosigVLSAPI.CurrentSosigVlsOfVlsSet(_selectedVLSSet).guid));
		}

		private void UpdateVolume(object sender, ButtonClickEventArgs args)
		{
			if (TNHPstate == TNHPstates.Sosig_Voicelines)
			{
				CycleVLS(sender);
				return;
			}
			_volControls[0].ButtonText.text = "Decrease volume";
			_volControls[1].ButtonText.text = "Increase volume";
			float num = 0f;
			if (sender != null)
			{
				if ((Object)((sender is ButtonWidget) ? sender : null) == (Object)(object)_volControls[0])
				{
					num = -0.05f;
				}
				else if ((Object)((sender is ButtonWidget) ? sender : null) == (Object)(object)_volControls[1])
				{
					num = 0.05f;
				}
			}
			switch (TNHPstate)
			{
			case TNHPstates.BGM:
			{
				ConfigEntry<float> backgroundMusicVolume = PluginMain.BackgroundMusicVolume;
				backgroundMusicVolume.Value += num;
				if (!GeneralAPI.IfIsInRange(PluginMain.BackgroundMusicVolume.Value, 0f, 4f))
				{
					WristMenuAPI.Instance.Aud.PlayOneShot(WristMenuAPI.Instance.AudClip_Err);
				}
				PluginMain.BackgroundMusicVolume.Value = Mathf.Clamp(PluginMain.BackgroundMusicVolume.Value, 0f, 4f);
				break;
			}
			case TNHPstates.Announcer:
			{
				ConfigEntry<float> announcerMusicVolume = PluginMain.AnnouncerMusicVolume;
				announcerMusicVolume.Value += num;
				if (!GeneralAPI.IfIsInRange(PluginMain.AnnouncerMusicVolume.Value, 0f, 20f))
				{
					WristMenuAPI.Instance.Aud.PlayOneShot(WristMenuAPI.Instance.AudClip_Err);
				}
				PluginMain.AnnouncerMusicVolume.Value = Mathf.Clamp(PluginMain.AnnouncerMusicVolume.Value, 0f, 20f);
				break;
			}
			}
			_volumeText.Text.text = GetVolumePercent();
		}

		private string GetNameWithOffset(int offset)
		{
			int num = _firstMusicIndex + offset;
			switch (TNHPstate)
			{
			case TNHPstates.BGM:
				if (num < BGMs.Count)
				{
					return $"{num + 1}: {BGMs[num].Name}";
				}
				break;
			case TNHPstates.Announcer:
				if (num < AnnouncerAPI.LoadedAnnouncers.Count)
				{
					return num + 1 + ": " + AnnouncerAPI.LoadedAnnouncers[num].Name;
				}
				break;
			case TNHPstates.Sosig_Voicelines:
				if (num < SosigVLSAPI.LoadedSosigVLSs.Count)
				{
					return num + 1 + ": " + SosigVLSAPI.LoadedSosigVLSs[num].name;
				}
				break;
			}
			return "";
		}

		private string GetCurrentSelectedItemName()
		{
			switch (TNHPstate)
			{
			case TNHPstates.BGM:
				if (!PluginMain.IsSoundtrack.Value)
				{
					return BankAPI.GetNameFromIndex(BankAPI.CurrentBankIndex, returnWithIndex: true);
				}
				return $"{BankAPI.LoadedBankLocations.Count + SoundtrackAPI.SelectedSoundtrackIndex + 1}: {SoundtrackAPI.Soundtracks[SoundtrackAPI.SelectedSoundtrackIndex].Name}";
			case TNHPstates.Announcer:
				return AnnouncerAPI.CurrentAnnouncerIndex + 1 + ": " + AnnouncerAPI.CurrentAnnouncer.Name;
			case TNHPstates.Sosig_Voicelines:
				return SosigVLSAPI.CurrentSosigVlsIndexOfVlsSet(_selectedVLSSet) + 1 + ": " + SosigVLSAPI.CurrentSosigVlsOfVlsSet(_selectedVLSSet).name;
			default:
				return "";
			}
		}

		private string GetVolumePercent()
		{
			float num = 0f;
			switch (TNHPstate)
			{
			case TNHPstates.BGM:
				num = PluginMain.BackgroundMusicVolume.Value;
				break;
			case TNHPstates.Announcer:
				num = PluginMain.AnnouncerMusicVolume.Value;
				break;
			case TNHPstates.Sosig_Voicelines:
				num = 1f;
				break;
			}
			return Mathf.Round(num * 100f).ToString(CultureInfo.InvariantCulture) + "%";
		}

		private void SetCurrentItem(object sender, ButtonClickEventArgs args)
		{
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Expected O, but got Unknown
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Expected O, but got Unknown
			int num = _firstMusicIndex + Array.IndexOf(_musicButtons, (ButtonWidget)((sender is ButtonWidget) ? sender : null));
			if ((Object)(object)GM.TNH_Manager != (Object)null)
			{
				WristMenuAPI.Instance.Aud.PlayOneShot(WristMenuAPI.Instance.AudClip_Err);
				return;
			}
			switch (TNHPstate)
			{
			case TNHPstates.BGM:
				if (BGMs[num].IsBank && BGMs[num].BankGuid == "Select Random")
				{
					num = Random.Range(0, BGMs.Count);
				}
				PluginMain.DebugLog.LogInfo((object)$"Setting song to index {num}, {BGMs[num].Name}");
				if (BGMs[num].IsBank)
				{
					BankAPI.SwapBank(num);
					GameObject val = new GameObject();
					if (BankAPI.LoadedBankLocations[num] != "Your Mix" && BankAPI.LoadedBankLocations[num] != "Select Random")
					{
						val.AddComponent(typeof(PlayFModSnippet));
					}
					PluginMain.DebugLog.LogInfo((object)$"index: {num}, index bank: {BankAPI.LoadedBankLocations[num]}");
					if (BankAPI.LoadedBankLocations[num] == "Your Mix")
					{
						SoundtrackAPI.IsMix = true;
					}
					else
					{
						SoundtrackAPI.IsMix = false;
					}
				}
				else
				{
					PluginMain.IsSoundtrack.Value = true;
					SoundtrackAPI.EnableSoundtrackFromGUID(BGMs[num].SoundtrackGuid);
					GameObject val2 = new GameObject();
					val2.AddComponent(typeof(PlaySoundtrackSnippet));
					SoundtrackAPI.IsMix = false;
				}
				break;
			case TNHPstates.Announcer:
			{
				int index = Mathf.Clamp(num, 0, AnnouncerAPI.LoadedAnnouncers.Count);
				AnnouncerAPI.SwapAnnouncer(AnnouncerAPI.LoadedAnnouncers[index].GUID);
				PlaySnippet(AnnouncerAPI.GetRandomPreview(AnnouncerAPI.CurrentAnnouncer.GUID));
				break;
			}
			case TNHPstates.Sosig_Voicelines:
			{
				int index = Mathf.Clamp(num, 0, SosigVLSAPI.LoadedSosigVLSs.Count);
				SosigVLSAPI.SwapSosigVLS(SosigVLSAPI.LoadedSosigVLSs[index].guid, SosigVLSAPI.VLSGuidOrder[_selectedVLSSet]);
				PlaySnippet(SosigVLSAPI.GetRandomPreview(SosigVLSAPI.CurrentSosigVlsOfVlsSet(_selectedVLSSet).guid));
				break;
			}
			}
			SetIcon();
			_bankText.Text.text = "Selected:\n" + GetCurrentSelectedItemName();
		}

		public void PlaySnippet(AudioClip snip)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			float num = 1f;
			float num2 = 0.6f;
			switch (TNHPstate)
			{
			case TNHPstates.Announcer:
				num2 = 0.6f * PluginMain.AnnouncerMusicVolume.Value;
				break;
			case TNHPstates.Sosig_Voicelines:
				num = SosigVLSAPI.CurrentSosigVlsOfVlsSet(_selectedVLSSet).SpeechSet.BasePitch;
				break;
			}
			AudioEvent val = new AudioEvent();
			val.Clips.Add(snip);
			val.PitchRange = new Vector2(num, num);
			val.VolumeRange = new Vector2(num2, num2);
			SM.PlayGenericSound(val, ((Component)GM.CurrentPlayerBody).transform.position);
		}

		private void SetIcon()
		{
			if ((Object)(object)icondisplay == (Object)null)
			{
				return;
			}
			Debug.Log((object)("IsMix: " + SoundtrackAPI.IsMix + ", CurBank: " + GetCurrentSelectedItemName() + ", CurSoundtrack: " + SoundtrackAPI.Soundtracks[SoundtrackAPI.SelectedSoundtrackIndex].Guid));
			switch (TNHPstate)
			{
			case TNHPstates.BGM:
				if (SoundtrackAPI.IsMix)
				{
					icondisplay.texture = (Texture)(object)BankAPI.GetBankIcon("Your Mix");
				}
				else if (!PluginMain.IsSoundtrack.Value)
				{
					icondisplay.texture = (Texture)(object)BankAPI.GetBankIcon(BankAPI.CurrentBankLocation);
				}
				else
				{
					icondisplay.texture = (Texture)(object)SoundtrackAPI.GetIcon(SoundtrackAPI.SelectedSoundtrackIndex);
				}
				break;
			case TNHPstates.Announcer:
				icondisplay.texture = (Texture)(object)AnnouncerAPI.GetAnnouncerIcon(AnnouncerAPI.CurrentAnnouncer);
				break;
			case TNHPstates.Sosig_Voicelines:
				icondisplay.texture = (Texture)(object)SosigVLSAPI.GetSosigVLSIcon(SosigVLSAPI.CurrentSosigVlsOfVlsSet(_selectedVLSSet));
				break;
			}
		}
	}
}
namespace TNHBGLoader.Soundtrack
{
	public static class SoundtrackAPI
	{
		public static List<SoundtrackManifest> Soundtracks = new List<SoundtrackManifest>();

		public static int SelectedSoundtrackIndex;

		public static bool IsMix;

		public static SoundtrackManifest GetCurrentSoundtrack => Soundtracks[SelectedSoundtrackIndex];

		public static void AssembleMusicData(this SoundtrackManifest manifest)
		{
			string path = Path.Combine(Path.GetDirectoryName(manifest.Path), manifest.Location);
			string[] directories = Directory.GetDirectories(path);
			List<TrackSet> list = new List<TrackSet>();
			string[] array = directories;
			foreach (string text in array)
			{
				string[] array2 = Path.GetFileName(text).Split(new char[1] { '_' });
				if (File.Exists(text) && !text.Contains(".ogg"))
				{
					continue;
				}
				TrackSet item = default(TrackSet);
				item.Tracks = new List<Track>();
				item.Type = array2[0];
				item.Situation = array2[1];
				if (array2.Length == 3)
				{
					item.Metadata = new string[1] { "" };
					item.Name = array2[2];
				}
				else
				{
					item.Metadata = array2[2].Split(new char[1] { '-' });
					item.Name = array2[3];
				}
				string[] files = Directory.GetFiles(text, "*.ogg", SearchOption.TopDirectoryOnly);
				string[] array3 = files;
				foreach (string path2 in array3)
				{
					string fileName = Path.GetFileName(path2);
					Track item2 = default(Track);
					if (Path.GetExtension(fileName) != ".ogg")
					{
						PluginMain.DebugLog.LogError((object)(fileName + " has an invalid extension! (Valid extensions: .ogg, file extension: " + Path.GetExtension(fileName) + ")"));
					}
					else
					{
						item2.Clip = LoadOgg(path2);
					}
					string[] array4 = Path.GetFileNameWithoutExtension(path2).Split(new char[1] { '_' });
					item2.Type = array4[0];
					item2.Situation = item.Situation;
					if (array4.Length == 2)
					{
						item2.Metadata = new string[1] { "" };
						item2.Name = array4[1];
					}
					else
					{
						item2.Metadata = array4[1].Split(new char[1] { '-' });
						item2.Name = array4[2];
					}
					item.Tracks.Add(item2);
				}
				list.Add(item);
			}
			foreach (TrackSet item3 in list)
			{
				PluginMain.DebugLog.LogInfo((object)$"Loading set {item3.Name}, {item3.Type}, {item3.Tracks.Count}, {item3.Situation}");
			}
			manifest.Sets = list;
			manifest.Loaded = true;
		}

		public static SoundtrackManifest ToManifest(this SoundtrackYamlfest yamlfest, string path)
		{
			SoundtrackManifest soundtrackManifest = new SoundtrackManifest();
			soundtrackManifest.Name = yamlfest.Name;
			soundtrackManifest.Guid = yamlfest.Guid;
			soundtrackManifest.Path = path;
			soundtrackManifest.Location = yamlfest.Location;
			soundtrackManifest.Loaded = false;
			soundtrackManifest.GameMode = yamlfest.GameMode;
			return soundtrackManifest;
		}

		public static void LoadSoundtrack(int index)
		{
			PluginMain.IsSoundtrack.Value = true;
			SelectedSoundtrackIndex = index;
		}

		public static TrackSet GetSet(string type, int situation)
		{
			string type2 = type;
			PluginMain.DebugLog.LogInfo((object)$"Getting set of type {type2}, {situation}. Cur soundtrack: {Soundtracks[SelectedSoundtrackIndex].Guid}");
			SoundtrackManifest soundtrackManifest = Soundtracks[SelectedSoundtrackIndex];
			TrackSet[] array = (from x in soundtrackManifest.Sets
				where x.Type == type2
				where x.Situation.TimingsMatch(situation)
				select x).ToArray();
			if (!array.Any())
			{
				array = (from x in soundtrackManifest.Sets
					where x.Type == type2
					where x.Situation == "fallback"
					select x).ToArray();
			}
			if (!array.Any())
			{
				PluginMain.DebugLog.LogError((object)"No set!");
			}
			int num = Random.Range(0, array.Length);
			return array[num];
		}

		public static bool TimingsMatch(this string seqTiming, int situation)
		{
			switch (seqTiming)
			{
			case "all":
				return true;
			case "fallback":
				return false;
			case "death":
				if (situation == -1)
				{
					return true;
				}
				return false;
			case "win":
				if (situation == -2)
				{
					return true;
				}
				return false;
			default:
				if (Enumerable.Contains(seqTiming, ','))
				{
					string[] source = seqTiming.Split(new char[1] { ',' });
					if (source.Contains(situation.ToString()))
					{
						return true;
					}
					return false;
				}
				if (Enumerable.Contains(seqTiming, '-'))
				{
					string[] array = seqTiming.Split(new char[1] { '-' });
					if (situation >= int.Parse(array[0]) && situation <= int.Parse(array[1]))
					{
						return true;
					}
					return false;
				}
				if (seqTiming.Contains("ge"))
				{
					int num = int.Parse(seqTiming.Replace("ge", string.Empty));
					if (situation >= num)
					{
						return true;
					}
					return false;
				}
				if (seqTiming.Contains("le"))
				{
					int num2 = int.Parse(seqTiming.Replace("le", string.Empty));
					if (situation <= num2)
					{
						return true;
					}
					return false;
				}
				if (situation == int.Parse(seqTiming))
				{
					return true;
				}
				return false;
			}
		}

		public static void EnableSoundtrackFromGUID(string guid)
		{
			for (int i = 0; i < Soundtracks.Count; i++)
			{
				if (Soundtracks[i].Guid == guid)
				{
					SelectedSoundtrackIndex = i;
				}
			}
		}

		public static Texture2D GetIcon(int soundtrack)
		{
			return GeneralAPI.GetIcon(Soundtracks[soundtrack].Guid, new string[1] { Path.Combine(Path.GetDirectoryName(Soundtracks[soundtrack].Path), "icon.png") });
		}

		public static AudioClip LoadOgg(string path)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			WWW val = new WWW("file://" + path);
			for (int i = 0; i < 500; i++)
			{
				if (val.isDone)
				{
					break;
				}
				Thread.Sleep(10);
			}
			AudioClip audioClip = WWWAudioExtensions.GetAudioClip(val, false);
			((Object)audioClip).name = Path.GetFileName(path);
			return audioClip;
		}

		public static (AudioClip?, bool) GetSnippet(SoundtrackManifest manifest)
		{
			bool item = true;
			string path = Path.Combine(Path.Combine(Path.GetDirectoryName(manifest.Path), manifest.Location), "preview_loop.ogg");
			if (!File.Exists(path))
			{
				path = Path.Combine(Path.Combine(Path.GetDirectoryName(manifest.Path), manifest.Location), "preview.ogg");
				item = false;
			}
			AudioClip item2 = null;
			if (File.Exists(path))
			{
				item2 = LoadOgg(path);
			}
			return (item2, item);
		}
	}
	public struct Track
	{
		public AudioClip Clip;

		public string Situation;

		public string[] Metadata;

		public string Name;

		public string Type;
	}
	public struct TrackSet
	{
		public List<Track> Tracks;

		public string Situation;

		public string[] Metadata;

		public string Name;

		public string Type;
	}
	public class SoundtrackManifest
	{
		public bool Loaded;

		public string Name;

		public string Path;

		public string Guid;

		public string Location;

		public List<TrackSet> Sets;

		public string GameMode;
	}
	public class SoundtrackPlayer : MonoBehaviour
	{
		public static SoundtrackPlayer? Instance;

		public static SoundtrackManifest? CurrentSoundtrack;

		public static AudioSource[] AudioSources;

		public static int CurrentAudioSource;

		public static List<Track> SongQueue = new List<Track>();

		public static Track CurrentTrack;

		public static string GameMode;

		private static bool isSwitching;

		private static float switchStartTime;

		public static float TrackFadeTime = 1.5f;

		public static float Volume = 1f;

		public static double SongLength;

		public static float SongStartTime;

		public AudioSource GetNotCurrentAudioSource => AudioSources[(CurrentAudioSource == 0) ? 1 : 0];

		public AudioSource GetCurrentAudioSource => AudioSources[CurrentAudioSource];

		public virtual void QueueRandomOfType(TrackSet set, string type, bool mandatory = true)
		{
			string type2 = type;
			Track[] array = set.Tracks.Where((Track x) => x.Type == type2).ToArray();
			if (array.Any())
			{
				QueueRandom(array);
			}
			else if (mandatory)
			{
				PluginMain.DebugLog.LogError((object)("Track set " + set.Name + " did not contain mandatory track type " + type2 + "!"));
			}
		}

		public virtual void QueueRandom(Track[] track)
		{
			Queue(track[Random.Range(0, track.Length)]);
		}

		public virtual void QueueMany(Track[] tracks)
		{
			foreach (Track track in tracks)
			{
				Queue(track);
			}
		}

		public virtual void Queue(Track track)
		{
			PluginMain.DebugLog.LogInfo((object)("Queueing " + track.Type + ", " + track.Name + " of situation " + track.Situation));
			SongQueue.Add(track);
			if (SongQueue.Count == 1)
			{
				GetNotCurrentAudioSource.clip = track.Clip;
			}
		}

		public virtual void ClearQueue()
		{
			SongQueue = new List<Track>();
		}

		private static void CreateAudioSources()
		{
			AudioSources = (AudioSource[])(object)new AudioSource[2]
			{
				((Component)ManagerSingleton<GM>.Instance.m_currentPlayerBody.Head).gameObject.AddComponent<AudioSource>(),
				((Component)ManagerSingleton<GM>.Instance.m_currentPlayerBody.Head).gameObject.AddComponent<AudioSource>()
			};
			AudioSource[] audioSources = AudioSources;
			foreach (AudioSource val in audioSources)
			{
				val.playOnAwake = false;
				val.priority = 0;
				val.volume = Volume;
				val.spatialBlend = 0f;
				val.loop = true;
			}
		}

		public virtual void SwitchSong(Track newTrack, float timeOverride = -1f)
		{
			CurrentTrack = newTrack;
			bool flag = newTrack.Metadata.Any((string x) => x == "loop");
			bool flag2 = newTrack.Metadata.All((string x) => x != "dnf");
			bool flag3 = newTrack.Metadata.Any((string x) => x == "st");
			if (!flag3)
			{
				SongStartTime = Time.time;
			}
			float time = GetCurrentAudioSource.time;
			SongLength = newTrack.Clip.length;
			PluginMain.DebugLog.LogInfo((object)$"{Time.time}: Playing track {newTrack.Name} of calculated length {SongLength} (naive time {newTrack.Clip.length}) with override time {timeOverride}.");
			int currentAudioSource = ((CurrentAudioSource == 0) ? 1 : 0);
			if (flag2)
			{
				switchStartTime = Time.time;
				CurrentAudioSource = currentAudioSource;
				isSwitching = true;
				GetCurrentAudioSource.clip = newTrack.Clip;
				GetCurrentAudioSource.volume = 0f;
				if (flag)
				{
					GetCurrentAudioSource.loop = true;
				}
				else
				{
					GetCurrentAudioSource.loop = false;
				}
				GetCurrentAudioSource.Play();
			}
			else
			{
				GetCurrentAudioSource.Stop();
				if (SongQueue.Count > 0)
				{
					GetCurrentAudioSource.clip = SongQueue[0].Clip;
				}
				CurrentAudioSource = currentAudioSource;
				if ((Object)(object)GetCurrentAudioSource.clip != (Object)(object)newTrack.Clip)
				{
					GetCurrentAudioSource.clip = newTrack.Clip;
				}
				GetCurrentAudioSource.volume = Volume;
				if (flag)
				{
					GetCurrentAudioSource.loop = true;
				}
				else
				{
					GetCurrentAudioSource.loop = false;
				}
				GetCurrentAudioSource.Play();
			}
			GetCurrentAudioSource.time = 0f;
			if (flag3)
			{
				GetCurrentAudioSource.time = time;
			}
			if (timeOverride >= 0f)
			{
				GetCurrentAudioSource.time = timeOverride;
			}
		}

		public void Initialize(string gameMode, SoundtrackManifest soundtrack, float trackFadeTime, float volume)
		{
			GameMode = gameMode;
			Instance = this;
			TrackFadeTime = trackFadeTime;
			CurrentSoundtrack = soundtrack;
			Volume = volume;
			CreateAudioSources();
			if (!CurrentSoundtrack.Loaded)
			{
				CurrentSoundtrack.AssembleMusicData();
			}
		}

		public void Update()
		{
			float num = 0f;
			if (isSwitching)
			{
				num = Time.time - switchStartTime;
				float num2 = num / TrackFadeTime;
				float num3 = num2 * Volume;
				GetCurrentAudioSource.volume = num3;
				GetNotCurrentAudioSource.volume = Volume - num3;
				if (num2 >= 1f)
				{
					isSwitching = false;
					GetNotCurrentAudioSource.Stop();
					Debug.Log((object)$"{Time.time}: Phaseout complete at {Time.time}. Current track time: {GetCurrentAudioSource.time}");
				}
			}
			num = (float)SongLength - (Time.time - SongStartTime);
			float num4 = TrackFadeTime;
			if (SongQueue.Count > 0 && SongQueue[0].Metadata.Contains("dnf"))
			{
				num4 = 0.05f;
			}
			if (num <= num4 && !GetCurrentAudioSource.loop && SongQueue.Count > 0)
			{
				Debug.Log((object)"End of song incoming. Playing next song.");
				PlayNextSongInQueue();
			}
		}

		public virtual void PlayNextSongInQueue()
		{
			if (SongQueue.Count == 0)
			{
				PluginMain.DebugLog.LogError((object)"Ran out of songs in the queue!");
				return;
			}
			Track newTrack = SongQueue[0];
			SongQueue.RemoveAt(0);
			SwitchSong(newTrack);
		}

		public virtual void PlayNextTrackInQueueOfType(string type)
		{
			PlayNextTrackInQueueOfType(new string[1] { type });
		}

		public virtual void PlayNextTrackInQueueOfType(string[] types)
		{
			while (!types.Contains(SongQueue[0].Type))
			{
				Debug.Log((object)("Skipping song " + SongQueue[0].Name + " of type " + SongQueue[0].Type));
				SongQueue.RemoveAt(0);
			}
			PlayNextSongInQueue();
		}
	}
	public class SoundtrackYamlfest
	{
		public string Name { get; set; }

		public string Guid { get; set; }

		public string Location { get; set; }

		public string GameMode { get; set; }
	}
	public class TnHSoundtrackInterface : SoundtrackPlayer
	{
		public static TrackSet HoldMusic;

		public static bool failureSyncInfoReady;

		public static float timeIdentified;

		public static float timeFail;

		public static int Level;

		public static int Intensity = 1;

		public void Awake()
		{
			Initialize("tnh", SoundtrackAPI.GetCurrentSoundtrack, 1.5f, PluginMain.AnnouncerMusicVolume.Value / 4f);
			ClearQueue();
			Level = 0;
			HoldMusic = SoundtrackAPI.GetSet("hold", Level);
			PluginMain.DebugLog.LogInfo((object)$"Level: {Level}");
			if (HoldMusic.Tracks.Any((Track x) => x.Type == "take"))
			{
				QueueTake(HoldMusic);
			}
			else
			{
				QueueTake(SoundtrackAPI.GetSet("take", Level));
			}
		}

		public override void SwitchSong(Track newTrack, float timeOverride = -1f)
		{
			bool flag = newTrack.Metadata.Any((string x) => x == "fs");
			float timeOverride2 = timeOverride;
			if (flag)
			{
				if (failureSyncInfoReady)
				{
					float num = timeFail - timeIdentified - (Time.time - timeIdentified);
					if (num > newTrack.Clip.length)
					{
						PluginMain.DebugLog.LogError((object)$"Soundtrack {SoundtrackAPI.Soundtracks[SoundtrackAPI.SelectedSoundtrackIndex]}:{newTrack.Name} is TOO SHORT! Song length: {newTrack.Clip.length}, Time to Fail: {num}! Lengthen your song!");
						return;
					}
					timeOverride2 = newTrack.Clip.length - num;
					failureSyncInfoReady = false;
				}
				else
				{
					PluginMain.DebugLog.LogError((object)$"Soundtrack {SoundtrackAPI.Soundtracks[SoundtrackAPI.SelectedSoundtrackIndex]}:{newTrack.Name} DID NOT have enough time to get info about how long the hold is! (FailureSync). Please lengthen your transition or intro to give more buffer time for the info to load! It should be AT LEAST 5 seconds.");
				}
			}
			base.SwitchSong(newTrack, timeOverride2);
		}

		[HarmonyPatch(typeof(FVRFMODController), "SwitchTo")]
		[HarmonyPrefix]
		public static bool QueueHoldAndTakeTracks(ref int musicIndex, ref float timeDelayStart, ref bool shouldStop, ref bool shouldDeadStop)
		{
			if (!PluginMain.IsSoundtrack.Value)
			{
				return true;
			}
			if (musicIndex != 1)
			{
				return false;
			}
			SoundtrackPlayer.Instance.QueueRandomOfType(HoldMusic, "intro", mandatory: false);
			if (!HoldMusic.Tracks.Any((Track x) => x.Type.Contains("phase")))
			{
				SoundtrackPlayer.Instance.QueueRandomOfType(HoldMusic, "lo");
				SoundtrackPlayer.Instance.QueueRandomOfType(HoldMusic, "transition", mandatory: false);
				SoundtrackPlayer.Instance.QueueRandomOfType(HoldMusic, "medhi", mandatory: false);
			}
			else
			{
				int phaseNo = 0;
				while (HoldMusic.Tracks.Any((Track x) => x.Type == "phase" + phaseNo) && phaseNo < 20)
				{
					SoundtrackPlayer.Instance.QueueRandomOfType(HoldMusic, "phase" + phaseNo);
					SoundtrackPlayer.Instance.QueueRandomOfType(HoldMusic, "phasetr" + phaseNo, mandatory: false);
					phaseNo++;
				}
			}
			SoundtrackPlayer.Instance.QueueRandomOfType(HoldMusic, "end", mandatory: false);
			Level++;
			PluginMain.DebugLog.LogInfo((object)$"Level: {Level}");
			HoldMusic = SoundtrackAPI.GetSet("hold", Level);
			if (HoldMusic.Tracks.Any((Track x) => x.Type == "take"))
			{
				QueueTake(HoldMusic);
			}
			else
			{
				QueueTake(SoundtrackAPI.GetSet("take", Level));
			}
			SoundtrackPlayer.Instance.PlayNextTrackInQueueOfType(new string[3] { "intro", "lo", "phase0" });
			return false;
		}

		[HarmonyPatch(typeof(TNH_HoldPoint), "BeginPhase")]
		[HarmonyPrefix]
		public static bool PlayPhaseTracks()
		{
			if (!PluginMain.IsSoundtrack.Value)
			{
				return true;
			}
			if (SoundtrackPlayer.SongQueue.Count == 0 || SoundtrackPlayer.CurrentTrack.Type == "intro")
			{
				return true;
			}
			if (SoundtrackPlayer.SongQueue[0].Type.Contains("phasetr") || SoundtrackPlayer.SongQueue[0].Type.Contains("phase"))
			{
				SoundtrackPlayer.Instance.PlayNextSongInQueue();
			}
			return true;
		}

		[HarmonyPatch(typeof(TNH_HoldPoint), "BeginAnalyzing")]
		[HarmonyPrefix]
		public static bool PlayMedHiTrack(TNH_HoldPoint __instance)
		{
			if (!PluginMain.IsSoundtrack.Value)
			{
				return true;
			}
			if (SoundtrackPlayer.SongQueue.Any((Track x) => x.Type == "transition") || SoundtrackPlayer.CurrentTrack.Type == "transition" || Intensity != 2)
			{
				return true;
			}
			if (SoundtrackPlayer.SongQueue.Any((Track x) => x.Type == "medhi"))
			{
				SoundtrackPlayer.Instance.PlayNextTrackInQueueOfType(new string[1] { "medhi" });
			}
			return true;
		}

		[HarmonyPatch(typeof(TNH_Manager), "SetHoldWaveIntensity")]
		[HarmonyPrefix]
		public static bool PlayTransitionTrack(ref int intensity)
		{
			Intensity = intensity;
			if (!PluginMain.IsSoundtrack.Value || intensity != 2)
			{
				return true;
			}
			if (SoundtrackPlayer.SongQueue.All((Track x) => x.Type != "medhi"))
			{
				return true;
			}
			if (SoundtrackPlayer.SongQueue.Any((Track x) => x.Type == "transition"))
			{
				SoundtrackPlayer.Instance.PlayNextTrackInQueueOfType(new string[1] { "transition" });
			}
			return true;
		}

		[HarmonyPatch(typeof(TNH_HoldPoint), "FailOut")]
		[HarmonyPrefix]
		public static bool QueueFailTrack()
		{
			if (!PluginMain.IsSoundtrack.Value)
			{
				return true;
			}
			Track[] array = HoldMusic.Tracks.Where((Track x) => x.Type == "endfail").ToArray();
			if (array.Length == 0)
			{
				return true;
			}
			for (int i = 0; i < SoundtrackPlayer.SongQueue.Count; i++)
			{
				if (SoundtrackPlayer.SongQueue[i].Type == "end")
				{
					SoundtrackPlayer.SongQueue[i] = array[Random.Range(0, array.Length)];
				}
			}
			return true;
		}

		[HarmonyPatch(typeof(TNH_Manager), "SetPhase_Take")]
		[HarmonyPrefix]
		public static bool SkipToEndTrack()
		{
			if (!PluginMain.IsSoundtrack.Value || SoundtrackPlayer.SongQueue.Count == 0)
			{
				return true;
			}
			SoundtrackPlayer.Instance.PlayNextTrackInQueueOfType(new string[4] { "end", "take", "takeintro", "endfail" });
			return true;
		}

		public static void QueueTake(int situation)
		{
			TrackSet set = SoundtrackAPI.GetSet("take", situation);
			QueueTake(set);
		}

		public static void QueueTake(TrackSet set)
		{
			SoundtrackPlayer.Instance.QueueRandomOfType(set, "takeintro", mandatory: false);
			SoundtrackPlayer.Instance.QueueRandomOfType(set, "take");
		}

		[HarmonyPatch(typeof(TNH_Manager), "SetPhase_Dead")]
		[HarmonyPrefix]
		public static bool PlayDeadTrack()
		{
			if (!PluginMain.IsSoundtrack.Value)
			{
				return true;
			}
			SoundtrackPlayer.Instance.ClearQueue();
			QueueTake(-1);
			SoundtrackPlayer.Instance.PlayNextSongInQueue();
			return true;
		}

		[HarmonyPatch(typeof(TNH_Manager), "SetPhase_Completed")]
		[HarmonyPrefix]
		public static bool PlayWinTrack()
		{
			if (!PluginMain.IsSoundtrack.Value)
			{
				return true;
			}
			SoundtrackPlayer.Instance.ClearQueue();
			QueueTake(-2);
			SoundtrackPlayer.Instance.PlayNextSongInQueue();
			return true;
		}

		[HarmonyPatch(typeof(TNH_Manager), "Start")]
		[HarmonyPostfix]
		public static void InitializeTnHSoundtrackInterface(ref TNH_Manager __instance)
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.UsesInstitutionMusic)
			{
				PluginMain.DebugLog.LogWarning((object)"Institution selected. Potatoes' Sound Loader has not yet been updated to support Institution!");
				return;
			}
			if (PluginMain.IsSoundtrack.Value || SoundtrackAPI.IsMix)
			{
				((Component)__instance).gameObject.AddComponent<TnHSoundtrackInterface>();
			}
			((Bus)(ref GM.TNH_Manager.FMODController.MasterBus)).setMute(true);
			PluginMain.DebugLog.LogInfo((object)$"IsMix: {SoundtrackAPI.IsMix.ToString()}, CurSoundtrack: {SoundtrackAPI.Soundtracks[SoundtrackAPI.SelectedSoundtrackIndex].Guid}, IsSOundtrack: {PluginMain.IsSoundtrack.Value}");
		}

		[HarmonyPatch(typeof(TNH_HoldPoint), "BeginAnalyzing")]
		[HarmonyPostfix]
		public static void GatherFailureSyncInfo(ref TNH_HoldPoint __instance)
		{
			failureSyncInfoReady = true;
			timeIdentified = Time.time;
			timeFail = Time.time + 120f + __instance.m_tickDownToIdentification + 3f;
		}

		[HarmonyPatch(typeof(TNH_HoldPointSystemNode), "Start")]
		[HarmonyPrefix]
		public static bool SetOrbTouchTracks(ref TNH_HoldPointSystemNode __instance)
		{
			if (!PluginMain.IsSoundtrack.Value)
			{
				return true;
			}
			List<AudioClip> list = new List<AudioClip>();
			Track[] array = HoldMusic.Tracks.Where((Track x) => x.Type == "orbactivate").ToArray();
			if (array.Length != 0)
			{
				Track[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					Track track = array2[i];
					list.Add(track.Clip);
				}
				__instance.AUDEvent_HoldActivate.Clips = list;
			}
			list = new List<AudioClip>();
			array = HoldMusic.Tracks.Where((Track x) => x.Type == "orbwave").ToArray();
			if (array.Length != 0)
			{
				Track[] array3 = array;
				for (int j = 0; j < array3.Length; j++)
				{
					Track track2 = array3[j];
					list.Add(track2.Clip);
				}
				__instance.HoldPoint.AUDEvent_HoldWave.Clips = list;
			}
			list = new List<AudioClip>();
			array = HoldMusic.Tracks.Where((Track x) => x.Type == "orbsuccess").ToArray();
			if (array.Length != 0)
			{
				Track[] array4 = array;
				for (int k = 0; k < array4.Length; k++)
				{
					Track track3 = array4[k];
					list.Add(track3.Clip);
				}
				__instance.HoldPoint.AUDEvent_Success.Clips = list;
			}
			list = new List<AudioClip>();
			array = HoldMusic.Tracks.Where((Track x) => x.Type == "orbfailure").ToArray();
			if (array.Length != 0)
			{
				Track[] array5 = array;
				for (int l = 0; l < array5.Length; l++)
				{
					Track track4 = array5[l];
					list.Add(track4.Clip);
				}
				__instance.HoldPoint.AUDEvent_Failure.Clips = list;
			}
			return true;
		}
	}
}
namespace TNHBGLoader.Sosig
{
	public class SosigManifest
	{
		public SosigSpeechSet SpeechSet;

		public string guid;

		public string name;

		public string location;

		public List<AudioClip> previews = new List<AudioClip>();

		public static SosigSpeechSet[] sets;

		public static SosigManifest DefaultSosigVLS()
		{
			SosigSpeechSet val = ScriptableObject.CreateInstance<SosigSpeechSet>();
			val.BasePitch = 1.15f;
			return new SosigManifest
			{
				name = "Default",
				guid = "h3vr.default",
				SpeechSet = val
			};
		}

		public static SosigManifest DefaultZosigVLS()
		{
			SosigSpeechSet val = ScriptableObject.CreateInstance<SosigSpeechSet>();
			val.BasePitch = 1.15f;
			return new SosigManifest
			{
				name = "Zosig",
				guid = "h3vr.zosig",
				SpeechSet = val
			};
		}

		public static SosigManifest RandomSosigVLS()
		{
			SosigSpeechSet val = ScriptableObject.CreateInstance<SosigSpeechSet>();
			val.BasePitch = 1.15f;
			return new SosigManifest
			{
				name = "Select Random Voiceline",
				guid = "ptnhbgml.random",
				SpeechSet = val
			};
		}
	}
	public class SosigVLSAPI : MonoBehaviour
	{
		public static List<SosigManifest> LoadedSosigVLSs = new List<SosigManifest>();

		public static Dictionary<string, string> VLSGuidToName = new Dictionary<string, string>();

		public static List<string> VLSGuidOrder = new List<string>();

		public static Dictionary<string, int> CurrentSosigVLSIndex = new Dictionary<string, int>();

		public static SosigSpeechSet[] DefaultVLSs;

		public static int GetSosigVLSIndexFromGUID(string GUID)
		{
			return LoadedSosigVLSs.IndexOf(GetManifestFromGUID(GUID));
		}

		public static int GetSosigVLSIndexFromManifest(SosigManifest manifest)
		{
			return LoadedSosigVLSs.IndexOf(manifest);
		}

		public static SosigManifest GetManifestFromGUID(string GUID)
		{
			string GUID2 = GUID;
			return LoadedSosigVLSs.FindAll((SosigManifest a) => a.guid == GUID2).First();
		}

		public static int CurrentSosigVlsIndexOfVlsSet(int index)
		{
			if (!CurrentSosigVLSIndex.ContainsKey(VLSGuidOrder[index]))
			{
				PluginMain.DebugLog.LogFatal((object)(VLSGuidOrder[index] + " was not in the VLS index database!"));
			}
			return CurrentSosigVLSIndex[VLSGuidOrder[index]];
		}

		public static int CurrentSosigVlsIndexOfVlsSet(string guid)
		{
			return CurrentSosigVLSIndex[guid];
		}

		public static SosigManifest CurrentSosigVlsOfVlsSet(int index)
		{
			return LoadedSosigVLSs[CurrentSosigVLSIndex[VLSGuidOrder[index]]];
		}

		public static SosigManifest CurrentSosigVlsOfVlsSet(string guid)
		{
			return LoadedSosigVLSs[CurrentSosigVLSIndex[guid]];
		}

		public static Texture2D GetSosigVLSIcon(SosigManifest sosigVLS)
		{
			string directoryName = Path.GetDirectoryName(sosigVLS.location);
			string[] array = new string[0];
			return GeneralAPI.GetIcon(paths: (sosigVLS.guid == "h3vr.default") ? new string[1] { Path.Combine(PluginMain.AssemblyDirectory, "default/sosigvls_default.png") } : ((!(sosigVLS.guid == "h3vr.zosig")) ? new string[2]
			{
				directoryName + "/icon.png",
				Directory.GetParent(directoryName)?.ToString() + "/icon.png"
			} : new string[1] { Path.Combine(PluginMain.AssemblyDirectory, "default/sosigvls_zosig.png") }), guid: sosigVLS.guid);
		}

		public static void SwapSosigVLS(string guid, string vlsSet)
		{
			if (guid == "ptnhbgml.random")
			{
				guid = LoadedSosigVLSs[Random.Range(1, LoadedSosigVLSs.Count)].guid;
			}
			CurrentSosigVLSIndex[vlsSet] = GetSosigVLSIndexFromGUID(guid);
		}

		public static SosigManifest GetManifestFromYamlfest(SosigYamlfest yamlfest)
		{
			yamlfest.VoiceLines = Path.Combine(Path.GetDirectoryName(yamlfest.Location), yamlfest.VoiceLines);
			SosigManifest manifest = new SosigManifest();
			manifest.SpeechSet = ScriptableObject.CreateInstance<SosigSpeechSet>();
			manifest.SpeechSet.BasePitch = yamlfest.BasePitch;
			manifest.SpeechSet.BaseVolume = yamlfest.BaseVolume;
			manifest.SpeechSet.ForceDeathSpeech = yamlfest.ForceDeathSpeech;
			manifest.SpeechSet.UseAltDeathOnHeadExplode = yamlfest.UseAltDeathOnHeadExplode;
			manifest.SpeechSet.LessTalkativeSkirmish = yamlfest.LessTalkativeSkirmish;
			manifest.name = yamlfest.Name;
			manifest.guid = yamlfest.GUID;
			manifest.location = yamlfest.Location;
			List<string> list = Directory.GetFiles(yamlfest.VoiceLines, "*.wav", SearchOption.AllDirectories).ToList();
			InitializeLists(ref manifest.SpeechSet);
			foreach (string item in list)
			{
				AddNameToSpeechSet(ref manifest, GetAudioFromFile(item), Path.GetFileName(item) + ", " + new FileInfo(item).Directory.Name);
			}
			return manifest;
		}

		public static AudioClip GetRandomPreview(string guid)
		{
			if (guid == "h3vr.default")
			{
				return GetAudioFromFile(Path.Combine(PluginMain.AssemblyDirectory, "default/sosigvls_default.wav"));
			}
			SosigManifest manifestFromGUID = GetManifestFromGUID(guid);
			if (manifestFromGUID.previews.Count == 0)
			{
				return null;
			}
			int index = Random.Range(0, manifestFromGUID.previews.Count);
			return manifestFromGUID.previews[index];
		}

		public static void InitializeLists(ref SosigSpeechSet speechset)
		{
			speechset.OnJointBreak = new List<AudioClip>();
			speechset.OnJointSlice = new List<AudioClip>();
			speechset.OnJointSever = new List<AudioClip>();
			speechset.OnDeath = new List<AudioClip>();
			speechset.OnBackBreak = new List<AudioClip>();
			speechset.OnNeckBreak = new List<AudioClip>();
			speechset.OnPain = new List<AudioClip>();
			speechset.OnConfusion = new List<AudioClip>();
			speechset.OnDeathAlt = new List<AudioClip>();
			speechset.OnWander = new List<AudioClip>();
			speechset.OnSkirmish = new List<AudioClip>();
			speechset.OnInvestigate = new List<AudioClip>();
			speechset.OnSearchingForGuns = new List<AudioClip>();
			speechset.OnTakingCover = new List<AudioClip>();
			speechset.OnBeingAimedAt = new List<AudioClip>();
			speechset.OnAssault = new List<AudioClip>();
			speechset.OnReloading = new List<AudioClip>();
			speechset.OnMedic = new List<AudioClip>();
			speechset.OnCall_Skirmish = new List<AudioClip>();
			speechset.OnRespond_Skirmish = new List<AudioClip>();
			speechset.OnCall_Assistance = new List<AudioClip>();
			speechset.OnRespond_Assistance = new List<AudioClip>();
		}

		public static void AddNameToSpeechSet(ref SosigManifest manifest, AudioClip clip, string name)
		{
			if ((Object)(object)clip == (Object)null)
			{
				PluginMain.DebugLog.LogFatal((object)("Cannot find clip for " + name + "!"));
			}
			if (name.Contains("pain_joint_break"))
			{
				manifest.SpeechSet.OnJointBreak.Add(clip);
			}
			else if (name.Contains("pain_joint_slice"))
			{
				manifest.SpeechSet.OnJointSlice.Add(clip);
			}
			else if (name.Contains("pain_joint_sever"))
			{
				manifest.SpeechSet.OnJointSever.Add(clip);
			}
			else if (name.Contains("pain_death"))
			{
				manifest.SpeechSet.OnDeath.Add(clip);
			}
			else if (name.Contains("pain_break_back"))
			{
				manifest.SpeechSet.OnBackBreak.Add(clip);
			}
			else if (name.Contains("pain_break_neck"))
			{
				manifest.SpeechSet.OnNeckBreak.Add(clip);
			}
			else if (name.Contains("pain_default"))
			{
				manifest.SpeechSet.OnPain.Add(clip);
			}
			else if (name.Contains("pain_confusion"))
			{
				manifest.SpeechSet.OnConfusion.Add(clip);
			}
			else if (name.Contains("pain_alt_death"))
			{
				manifest.SpeechSet.OnDeathAlt.Add(clip);
			}
			else if (name.Contains("state_wander"))
			{
				manifest.SpeechSet.OnWander.Add(clip);
			}
			else if (name.Contains("state_skirmish"))
			{
				manifest.SpeechSet.OnSkirmish.Add(clip);
			}
			else if (name.Contains("state_investigate"))
			{
				manifest.SpeechSet.OnInvestigate.Add(clip);
			}
			else if (name.Contains("state_gunsearch"))
			{
				manifest.SpeechSet.OnSearchingForGuns.Add(clip);
			}
			else if (name.Contains("state_takecover"))
			{
				manifest.SpeechSet.OnTakingCover.Add(clip);
			}
			else if (name.Contains("state_aimedat"))
			{
				manifest.SpeechSet.OnBeingAimedAt.Add(clip);
			}
			else if (name.Contains("state_assault"))
			{
				manifest.SpeechSet.OnAssault.Add(clip);
			}
			else if (name.Contains("state_reload"))
			{
				manifest.SpeechSet.OnReloading.Add(clip);
			}
			else if (name.Contains("state_medic"))
			{
				manifest.SpeechSet.OnMedic.Add(clip);
			}
			else if (name.Contains("call_skirmish"))
			{
				manifest.SpeechSet.OnCall_Skirmish.Add(clip);
			}
			else if (name.Contains("respond_skirmish"))
			{
				manifest.SpeechSet.OnRespond_Skirmish.Add(clip);
			}
			else if (name.Contains("call_assistance"))
			{
				manifest.SpeechSet.OnCall_Assistance.Add(clip);
			}
			else if (name.Contains("respond_assistance"))
			{
				manifest.SpeechSet.OnRespond_Assistance.Add(clip);
			}
			else if (name.Contains("example"))
			{
				manifest.previews.Add(clip);
			}
			else if (!name.Contains("unused"))
			{
				PluginMain.DebugLog.LogError((object)("voiceline " + name + " has no voiceline match!"));
			}
		}

		public static AudioClip GetAudioFromFile(string path)
		{
			try
			{
				return WavUtility.ToAudioClip(path);
			}
			catch (Exception arg)
			{
				PluginMain.DebugLog.LogError((object)$"Failed loading audio file {Path.GetFileName(path)} with reason {arg}");
				return null;
			}
		}
	}
}
namespace TNH_BGLoader
{
	public class AnnouncerAPI
	{
		public static List<AnnouncerManifest> LoadedAnnouncers = new List<AnnouncerManifest>();

		public static int CurrentAnnouncerIndex = 0;

		private static bool validid = true;

		public static readonly string[] UnusedVoicelines = new string[11]
		{
			"base_", "encryption_cascading", "encryption_cascading", "encryption_orthagonal", "encryption_polymorphic", "encryption_refractive", "game_lose_connection", "game_lose_operator", "loot_regen", "loot_resource",
			"loot_tool"
		};

		public static AnnouncerManifest CurrentAnnouncer => LoadedAnnouncers[CurrentAnnouncerIndex];

		public static void SwapAnnouncer(string GUID)
		{
			if (GUID == "ptnhbgml.random")
			{
				GUID = LoadedAnnouncers[Random.Range(1, LoadedAnnouncers.Count)].GUID;
			}
			CurrentAnnouncerIndex = GetAnnouncerIndexFromGUID(GUID);
			PluginMain.LastLoadedAnnouncer.Value = CurrentAnnouncer.GUID;
		}

		public static int GetAnnouncerIndexFromGUID(string GUID)
		{
			return LoadedAnnouncers.IndexOf(GetManifestFromGUID(GUID));
		}

		public static int GetAnnouncerIndexFromManifest(AnnouncerManifest manifest)
		{
			return LoadedAnnouncers.IndexOf(manifest);
		}

		public static AnnouncerManifest GetManifestFromGUID(string GUID)
		{
			string GUID2 = GUID;
			return LoadedAnnouncers.FindAll((AnnouncerManifest a) => a.GUID == GUID2).First();
		}

		public static Texture2D GetAnnouncerIcon(AnnouncerManifest announcer)
		{
			string directoryName = Path.GetDirectoryName(announcer.Location);
			string[] array = new string[0];
			return GeneralAPI.GetIcon(paths: (!(announcer.GUID == "h3vr.default") && !(announcer.GUID == "h3vr.corrupted")) ? new string[2]
			{
				directoryName + "/icon.png",
				Directory.GetParent(directoryName)?.ToString() + "/icon.png"
			} : new string[1] { Path.Combine(PluginMain.AssemblyDirectory, "default/announcer_default.png") }, guid: announcer.GUID);
		}

		public static AudioClip GetRandomPreview(string guid)
		{
			if (guid == "h3vr.default")
			{
				return GetAudioFromFile(Path.Combine(PluginMain.AssemblyDirectory, "default/announcer_default.wav"));
			}
			if (guid == "h3vr.corrupted")
			{
				return GetAudioFromFile(Path.Combine(PluginMain.AssemblyDirectory, "default/announcer_corrupted.wav"));
			}
			AnnouncerManifest manifestFromGUID = GetManifestFromGUID(guid);
			int index = Random.Range(0, manifestFromGUID.Previews.Count);
			return GetAudioFromFile(manifestFromGUID.Previews[index]);
		}

		public static AnnouncerManifest GetManifestFromYamlfest(AnnouncerYamlfest yamlfest)
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			yamlfest.VoiceLines = Path.Combine(Path.GetDirectoryName(yamlfest.Location), yamlfest.VoiceLines);
			AnnouncerManifest announcerManifest = new AnnouncerManifest();
			announcerManifest.VoiceLines = new List<VoiceLine>();
			announcerManifest.Name = yamlfest.Name;
			announcerManifest.GUID = yamlfest.GUID;
			announcerManifest.FrontPadding = yamlfest.FrontPadding;
			announcerManifest.BackPadding = yamlfest.BackPadding;
			announcerManifest.Location = yamlfest.Location;
			announcerManifest.Previews = Directory.GetFiles(yamlfest.VoiceLines, "example*.wav", SearchOption.AllDirectories).ToList();
			List<string> list = Directory.GetFiles(yamlfest.VoiceLines, "*.wav", SearchOption.AllDirectories).ToList();
			foreach (string item in list)
			{
				VoiceLine voiceLine = new VoiceLine();
				string song = item;
				voiceLine.ID = GetNameFromID(song);
				if (validid)
				{
					voiceLine.ClipPath = item;
					announcerManifest.VoiceLines.Add(voiceLine);
				}
			}
			return announcerManifest;
		}

		public static TNH_VoiceLineID GetNameFromID(string song)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0446: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_0259: Unknown result type (might be due to invalid IL or missing references)
			//IL_0272: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0308: Unknown result type (might be due to invalid IL or missing references)
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_033a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0353: Unknown result type (might be due to invalid IL or missing references)
			//IL_036c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0388: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_040e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0443: Unknown result type (might be due to invalid IL or missing references)
			string fileName = Path.GetFileName(song);
			validid = true;
			if (fileName.Contains("game_intro"))
			{
				return (TNH_VoiceLineID)0;
			}
			if (fileName.Contains("hold_intro"))
			{
				return (TNH_VoiceLineID)1;
			}
			if (fileName.Contains("hold_analyze"))
			{
				return (TNH_VoiceLineID)2;
			}
			if (fileName.Contains("encryption_static"))
			{
				return (TNH_VoiceLineID)10;
			}
			if (fileName.Contains("encryption_hardened"))
			{
				return (TNH_VoiceLineID)11;
			}
			if (fileName.Contains("encryption_swarm"))
			{
				return (TNH_VoiceLineID)12;
			}
			if (fileName.Contains("encryption_recursive"))
			{
				return (TNH_VoiceLineID)13;
			}
			if (fileName.Contains("encryption_stealth"))
			{
				return (TNH_VoiceLineID)14;
			}
			if (fileName.Contains("encryption_agile"))
			{
				return (TNH_VoiceLineID)15;
			}
			if (fileName.Contains("encryption_regen"))
			{
				return (TNH_VoiceLineID)16;
			}
			if (fileName.Contains("encryption_polymorphic"))
			{
				return (TNH_VoiceLineID)17;
			}
			if (fileName.Contains("encryption_cascading"))
			{
				return (TNH_VoiceLineID)18;
			}
			if (fileName.Contains("encryption_orthagonal"))
			{
				return (TNH_VoiceLineID)19;
			}
			if (fileName.Contains("encryption_refractive"))
			{
				return (TNH_VoiceLineID)20;
			}
			if (fileName.Contains("encryption_unknown"))
			{
				return (TNH_VoiceLineID)40;
			}
			if (fileName.Contains("hold_encryption_win"))
			{
				return (TNH_VoiceLineID)50;
			}
			if (fileName.Contains("hold_reminder_early"))
			{
				return (TNH_VoiceLineID)60;
			}
			if (fileName.Contains("hold_reminder_late"))
			{
				return (TNH_VoiceLineID)61;
			}
			if (fileName.Contains("hold_next_layer"))
			{
				return (TNH_VoiceLineID)62;
			}
			if (fileName.Contains("hold_win"))
			{
				return (TNH_VoiceLineID)70;
			}
			if (fileName.Contains("hold_failure"))
			{
				return (TNH_VoiceLineID)71;
			}
			if (fileName.Contains("hold_advance"))
			{
				return (TNH_VoiceLineID)90;
			}
			if (fileName.Contains("loot_token1"))
			{
				return (TNH_VoiceLineID)91;
			}
			if (fileName.Contains("loot_token2"))
			{
				return (TNH_VoiceLineID)92;
			}
			if (fileName.Contains("loot_token3"))
			{
				return (TNH_VoiceLineID)93;
			}
			if (fileName.Contains("loot_token4"))
			{
				return (TNH_VoiceLineID)94;
			}
			if (fileName.Contains("loot_token5"))
			{
				return (TNH_VoiceLineID)95;
			}
			if (fileName.Contains("loot_resource"))
			{
				return (TNH_VoiceLineID)100;
			}
			if (fileName.Contains("loot_tool"))
			{
				return (TNH_VoiceLineID)101;
			}
			if (fileName.Contains("loot_regen"))
			{
				return (TNH_VoiceLineID)102;
			}
			if (fileName.Contains("core_exposed"))
			{
				return (TNH_VoiceLineID)110;
			}
			if (fileName.Contains("core_destroyed"))
			{
				return (TNH_VoiceLineID)111;
			}
			if (fileName.Contains("game_lose_connection"))
			{
				return (TNH_VoiceLineID)120;
			}
			if (fileName.Contains("game_end"))
			{
				return (TNH_VoiceLineID)121;
			}
			if (fileName.Contains("game_lose_operator"))
			{
				return (TNH_VoiceLineID)122;
			}
			if (fileName.Contains("base_lockdown"))
			{
				return (TNH_VoiceLineID)500;
			}
			if (fileName.Contains("base_response"))
			{
				return (TNH_VoiceLineID)501;
			}
			if (fileName.Contains("base_dispatch"))
			{
				return (TNH_VoiceLineID)502;
			}
			if (fileName.Contains("base_compromised"))
			{
				return (TNH_VoiceLineID)503;
			}
			if (fileName.Contains("base_fabricating"))
			{
				return (TNH_VoiceLineID)504;
			}
			if (fileName.Contains("base_error"))
			{