Decompiled source of lollipop BackgroundMusicMod v0.1.3

BepInEx/plugins/BackgroundMusicMod.dll

Decompiled 5 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.mthwj.minecraftmusic")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("com.mthwj.minecraftmusic")]
[assembly: AssemblyTitle("MinecraftMusicPlugin")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace MinecraftMusicPlugin
{
	[BepInPlugin("com.mthwj.minecraftmusic", "MinecraftMusicPlugin", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(AmbienceAudio), "FixedUpdate")]
		private static class AmbienceAudio_FixedUpdate_Patch
		{
			[CompilerGenerated]
			private sealed class <LoadAndPlayAudio>d__2 : IEnumerator<object>, IEnumerator, IDisposable
			{
				private int <>1__state;

				private object <>2__current;

				public MonoBehaviour context;

				public string path;

				private UnityWebRequest <uwr>5__1;

				private AudioClip <clip>5__2;

				private AudioSource <source>5__3;

				object IEnumerator<object>.Current
				{
					[DebuggerHidden]
					get
					{
						return <>2__current;
					}
				}

				object IEnumerator.Current
				{
					[DebuggerHidden]
					get
					{
						return <>2__current;
					}
				}

				[DebuggerHidden]
				public <LoadAndPlayAudio>d__2(int <>1__state)
				{
					this.<>1__state = <>1__state;
				}

				[DebuggerHidden]
				void IDisposable.Dispose()
				{
					int num = <>1__state;
					if (num == -3 || num == 1)
					{
						try
						{
						}
						finally
						{
							<>m__Finally1();
						}
					}
					<uwr>5__1 = null;
					<clip>5__2 = null;
					<source>5__3 = null;
					<>1__state = -2;
				}

				private bool MoveNext()
				{
					//IL_009f: Unknown result type (might be due to invalid IL or missing references)
					//IL_00a5: Invalid comparison between Unknown and I4
					try
					{
						switch (<>1__state)
						{
						default:
							return false;
						case 0:
							<>1__state = -1;
							if (Logger != null)
							{
								Logger.LogInfo((object)("Starting to load MP3 from: " + path));
							}
							<uwr>5__1 = UnityWebRequestMultimedia.GetAudioClip("file:///" + path, (AudioType)13);
							<>1__state = -3;
							<>2__current = <uwr>5__1.SendWebRequest();
							<>1__state = 1;
							return true;
						case 1:
							<>1__state = -3;
							if ((int)<uwr>5__1.result == 1)
							{
								<clip>5__2 = DownloadHandlerAudioClip.GetContent(<uwr>5__1);
								if (Logger != null)
								{
									Logger.LogInfo((object)$"MP3 loaded, length: {<clip>5__2.length} seconds");
								}
								<source>5__3 = ((Component)context).GetComponent<AmbienceAudio>().mainMusic;
								<source>5__3.clip = <clip>5__2;
								<source>5__3.volume = 0.5f;
								<source>5__3.Play();
								if (Logger != null)
								{
									Logger.LogInfo((object)("Playing MP3 via mainMusic: " + Path.GetFileName(path)));
								}
								isMusicPlaying = true;
								<clip>5__2 = null;
								<source>5__3 = null;
							}
							else if (Logger != null)
							{
								Logger.LogError((object)("Failed to load MP3: " + <uwr>5__1.error));
							}
							<>m__Finally1();
							<uwr>5__1 = null;
							return false;
						}
					}
					catch
					{
						//try-fault
						((IDisposable)this).Dispose();
						throw;
					}
				}

				bool IEnumerator.MoveNext()
				{
					//ILSpy generated this explicit interface implementation from .override directive in MoveNext
					return this.MoveNext();
				}

				private void <>m__Finally1()
				{
					<>1__state = -1;
					if (<uwr>5__1 != null)
					{
						((IDisposable)<uwr>5__1).Dispose();
					}
				}

				[DebuggerHidden]
				void IEnumerator.Reset()
				{
					throw new NotSupportedException();
				}
			}

			private static bool Prefix(AmbienceAudio __instance)
			{
				//IL_0088: 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)
				Transform transform = ((Component)__instance).transform;
				object obj;
				if (transform == null)
				{
					obj = null;
				}
				else
				{
					Transform root = transform.root;
					obj = ((root != null) ? ((Component)root).GetComponent<CharacterData>() : null);
				}
				CharacterData val = (CharacterData)obj;
				if ((Object)(object)val == (Object)null)
				{
					if (Logger != null)
					{
						Logger.LogWarning((object)"CharacterData component not found on root transform");
					}
					return true;
				}
				if (val.sinceDead > 0.5f && !val.passedOut && !val.dead && !val.fullyPassedOut)
				{
					float z = ((Component)__instance).transform.position.z;
					float y = ((Component)__instance).transform.position.y;
					isMusicPlaying = __instance.mainMusic.isPlaying;
					if (z > __instance.beachStingerZ && !__instance.playedBeach)
					{
						if (Logger != null)
						{
							Logger.LogInfo((object)"Entered beach region, overriding with background music");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Beach");
						__instance.playedBeach = true;
						return false;
					}
					if (z > __instance.tropicsStingerZ && !__instance.playedTropics)
					{
						if (Logger != null)
						{
							Logger.LogInfo((object)"Entered tropics region, overriding with background music");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Tropics");
						__instance.playedTropics = true;
						return false;
					}
					if (z > __instance.alpineStingerZ && !__instance.playedAlpine)
					{
						if (Logger != null)
						{
							Logger.LogInfo((object)"Entered alpine region, overriding with background music");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Alpine");
						__instance.playedAlpine = true;
						return false;
					}
					if (z > __instance.calderaStingerZ && !__instance.playedCaldera)
					{
						if (Logger != null)
						{
							Logger.LogInfo((object)"Entered caldera region, overriding with background music");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Caldera");
						__instance.playedCaldera = true;
						return false;
					}
					if (y > __instance.kilnStingerY && !__instance.playedKiln)
					{
						if (Logger != null)
						{
							Logger.LogInfo((object)"Entered kiln region, overriding with background music");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Kiln");
						__instance.playedKiln = true;
						return false;
					}
					if (z > __instance.peaksTingerZ && !__instance.playedPeak)
					{
						if (Logger != null)
						{
							Logger.LogInfo((object)"Entered peak region, overriding with background music");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Peak");
						__instance.playedPeak = true;
						return false;
					}
					if (!isMusicPlaying && (Object)(object)__instance.mainMusic.clip != (Object)null)
					{
						if (Logger != null)
						{
							Logger.LogInfo((object)"Current track ended, playing new random background music");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Continuous");
						return false;
					}
				}
				else
				{
					if (Logger != null)
					{
						Logger.LogInfo((object)"Character conditions not met, fading out music");
					}
					__instance.mainMusic.volume = Mathf.Lerp(__instance.mainMusic.volume, 0f, 0.05f);
				}
				return true;
			}

			private static void PlayRandomMp3(MonoBehaviour context, string region)
			{
				string[] files = Directory.GetFiles(musicFolder, "*.mp3");
				if (files.Length != 0)
				{
					Random random = new Random();
					string path = files[random.Next(files.Length)];
					if (Logger != null)
					{
						Logger.LogInfo((object)("Loading MP3 for " + region + ": " + Path.GetFileName(path)));
					}
					context.StartCoroutine(LoadAndPlayAudio(context, path));
				}
				else if (Logger != null)
				{
					Logger.LogError((object)("No MP3 files found in " + musicFolder + " for " + region + "!"));
				}
			}

			[IteratorStateMachine(typeof(<LoadAndPlayAudio>d__2))]
			private static IEnumerator LoadAndPlayAudio(MonoBehaviour context, string path)
			{
				//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
				return new <LoadAndPlayAudio>d__2(0)
				{
					context = context,
					path = path
				};
			}
		}

		internal static ManualLogSource? Logger;

		private static string musicFolder = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\PEAK\\BepInEx\\plugins\\backgroundmusic";

		private static bool isMusicPlaying = false;

		public const string Id = "com.mthwj.minecraftmusic";

		public static string Name => "MinecraftMusicPlugin";

		public static string Version => "0.1.0";

		private void Awake()
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			if (Logger != null)
			{
				Logger.LogInfo((object)("Plugin " + Name + " is loaded! Music folder: " + musicFolder));
			}
			Harmony val = new Harmony("com.mthwj.minecraftmusic.patch");
			val.PatchAll();
			if (Logger != null)
			{
				Logger.LogInfo((object)"Harmony patches applied successfully");
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}