Decompiled source of BackgroundLofi v0.1.1

plugins/com.Jonesy__07.BackgroundLofi.dll

Decompiled 2 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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.Jonesy__07.BackgroundLofi")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("com.Jonesy__07.BackgroundLofi")]
[assembly: AssemblyTitle("BackgroundLofi")]
[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 BackgroundLofi
{
	[BepInPlugin("com.Jonesy__07.BackgroundLofi", "BackgroundLofi", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(AmbienceAudio), "FixedUpdate")]
		private static class AmbienceAudio_FixedUpdate_Patch
		{
			[CompilerGenerated]
			private sealed class <LoadAndPlayAudio>d__3 : IEnumerator<object>, IEnumerator, IDisposable
			{
				private int <>1__state;

				private object <>2__current;

				public string path;

				public MonoBehaviour context;

				private UnityWebRequest <uwr>5__2;

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

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

				[DebuggerHidden]
				public <LoadAndPlayAudio>d__3(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__2 = null;
					<>1__state = -2;
				}

				private bool MoveNext()
				{
					//IL_008e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0094: Invalid comparison between Unknown and I4
					try
					{
						switch (<>1__state)
						{
						default:
							return false;
						case 0:
						{
							<>1__state = -1;
							ManualLogSource? logger6 = Logger;
							if (logger6 != null)
							{
								logger6.LogInfo((object)("Starting to load MP3 from: " + path));
							}
							<uwr>5__2 = UnityWebRequestMultimedia.GetAudioClip("file:///" + path, (AudioType)13);
							<>1__state = -3;
							<>2__current = <uwr>5__2.SendWebRequest();
							<>1__state = 1;
							return true;
						}
						case 1:
							<>1__state = -3;
							if ((int)<uwr>5__2.result == 1)
							{
								AudioClip content = DownloadHandlerAudioClip.GetContent(<uwr>5__2);
								if ((Object)(object)content != (Object)null)
								{
									ManualLogSource? logger = Logger;
									if (logger != null)
									{
										logger.LogInfo((object)$"MP3 loaded, length: {content.length} seconds");
									}
									AudioSource mainMusic = ((Component)context).GetComponent<AmbienceAudio>().mainMusic;
									if (mainMusic.isPlaying)
									{
										mainMusic.Stop();
										mainMusic.clip = null;
										ManualLogSource? logger2 = Logger;
										if (logger2 != null)
										{
											logger2.LogDebug((object)("Stopped previous playback for " + Path.GetFileName(path)));
										}
									}
									mainMusic.clip = content;
									mainMusic.volume = Mathf.Max(mainMusic.volume, 0.5f);
									mainMusic.Play();
									ManualLogSource? logger3 = Logger;
									if (logger3 != null)
									{
										logger3.LogInfo((object)("Playing MP3 via mainMusic: " + Path.GetFileName(path)));
									}
									isMusicPlaying = true;
								}
								else
								{
									ManualLogSource? logger4 = Logger;
									if (logger4 != null)
									{
										logger4.LogError((object)("Failed to create AudioClip from " + path + ": Null clip"));
									}
								}
							}
							else
							{
								ManualLogSource? logger5 = Logger;
								if (logger5 != null)
								{
									logger5.LogError((object)("Failed to load MP3: " + <uwr>5__2.error));
								}
							}
							<>m__Finally1();
							<uwr>5__2 = 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__2 != null)
					{
						((IDisposable)<uwr>5__2).Dispose();
					}
				}

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

			private static bool Prefix(AmbienceAudio __instance)
			{
				//IL_0076: 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)
				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)
				{
					ManualLogSource? logger = Logger;
					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)
					{
						ManualLogSource? logger2 = Logger;
						if (logger2 != null)
						{
							logger2.LogInfo((object)"Entered beach region, replacing with Lofi");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Beach");
						__instance.playedBeach = true;
						return false;
					}
					if (z > __instance.tropicsStingerZ && !__instance.playedTropics)
					{
						ManualLogSource? logger3 = Logger;
						if (logger3 != null)
						{
							logger3.LogInfo((object)"Entered tropics region, replacing with Lofi");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Tropics");
						__instance.playedTropics = true;
						return false;
					}
					if (z > __instance.alpineStingerZ && !__instance.playedAlpine)
					{
						ManualLogSource? logger4 = Logger;
						if (logger4 != null)
						{
							logger4.LogInfo((object)"Entered alpine region, replacing with Lofi");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Alpine");
						__instance.playedAlpine = true;
						return false;
					}
					if (z > __instance.calderaStingerZ && !__instance.playedCaldera)
					{
						ManualLogSource? logger5 = Logger;
						if (logger5 != null)
						{
							logger5.LogInfo((object)"Entered caldera region, replacing with Lofi");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Caldera");
						__instance.playedCaldera = true;
						return false;
					}
					if (y > __instance.kilnStingerY && !__instance.playedKiln)
					{
						ManualLogSource? logger6 = Logger;
						if (logger6 != null)
						{
							logger6.LogInfo((object)"Entered kiln region, replacing with Lofi");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Kiln");
						__instance.playedKiln = true;
						return false;
					}
					if (z > __instance.peaksTingerZ && !__instance.playedPeak)
					{
						ManualLogSource? logger7 = Logger;
						if (logger7 != null)
						{
							logger7.LogInfo((object)"Entered peak region, replacing with Lofi");
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, "Peak");
						__instance.playedPeak = true;
						return false;
					}
					if (!isMusicPlaying && (Object)(object)__instance.mainMusic.clip != (Object)null)
					{
						string currentRegion = GetCurrentRegion(__instance);
						ManualLogSource? logger8 = Logger;
						if (logger8 != null)
						{
							logger8.LogInfo((object)("Current track ended, playing new random MP3 for " + currentRegion));
						}
						PlayRandomMp3((MonoBehaviour)(object)__instance, currentRegion);
						return false;
					}
				}
				else
				{
					ManualLogSource? logger9 = Logger;
					if (logger9 != null)
					{
						logger9.LogInfo((object)"Character conditions not met, fading out music");
					}
					__instance.mainMusic.volume = Mathf.Lerp(__instance.mainMusic.volume, 0f, 0.05f);
				}
				return true;
			}

			private static string GetCurrentRegion(AmbienceAudio __instance)
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_0017: Unknown result type (might be due to invalid IL or missing references)
				float z = ((Component)__instance).transform.position.z;
				float y = ((Component)__instance).transform.position.y;
				if (y > __instance.kilnStingerY)
				{
					return "Kiln";
				}
				if (z > __instance.peaksTingerZ)
				{
					return "Peak";
				}
				if (z > __instance.calderaStingerZ)
				{
					return "Caldera";
				}
				if (z > __instance.alpineStingerZ)
				{
					return "Alpine";
				}
				if (z > __instance.tropicsStingerZ)
				{
					return "Tropics";
				}
				if (z > __instance.beachStingerZ)
				{
					return "Beach";
				}
				return "Default";
			}

			private static void PlayRandomMp3(MonoBehaviour context, string region)
			{
				string text = region switch
				{
					"Beach" => "Beach", 
					"Tropics" => "Tropics", 
					"Alpine" => "Alpine", 
					"Caldera" => "Caldera", 
					"Kiln" => "Kiln", 
					"Peak" => "Peak", 
					_ => "Default", 
				};
				string text2 = (Path.IsPathRooted(text) ? text : Path.Combine(musicFolder, text));
				if (!Directory.Exists(text2) || Directory.GetFiles(text2, "*.mp3").Length == 0)
				{
					ManualLogSource? logger = Logger;
					if (logger != null)
					{
						logger.LogWarning((object)("Music folder for " + region + " not found or empty at " + text2 + ", falling back to Default"));
					}
					text2 = Path.Combine(musicFolder, defaultFolderConfig.Value.Trim());
				}
				if (!Directory.Exists(text2))
				{
					ManualLogSource? logger2 = Logger;
					if (logger2 != null)
					{
						logger2.LogError((object)("No MP3s available for " + region + " or Default at " + text2 + "!"));
					}
					return;
				}
				string[] files = Directory.GetFiles(text2, "*.mp3");
				if (files.Length == 0)
				{
					ManualLogSource? logger3 = Logger;
					if (logger3 != null)
					{
						logger3.LogError((object)("No MP3 files found in " + text2 + " for " + region + "!"));
					}
				}
				else
				{
					Random random = new Random();
					string path = files[random.Next(files.Length)];
					ManualLogSource? logger4 = Logger;
					if (logger4 != null)
					{
						logger4.LogInfo((object)("Loading MP3 for " + region + ": " + Path.GetFileName(path)));
					}
					context.StartCoroutine(LoadAndPlayAudio(context, path));
				}
			}

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

		internal static ManualLogSource? Logger;

		private static string musicFolder = string.Empty;

		private static bool isMusicPlaying = false;

		private static ConfigEntry<string> beachFolderConfig = null;

		private static ConfigEntry<string> tropicsFolderConfig = null;

		private static ConfigEntry<string> alpineFolderConfig = null;

		private static ConfigEntry<string> calderaFolderConfig = null;

		private static ConfigEntry<string> kilnFolderConfig = null;

		private static ConfigEntry<string> peakFolderConfig = null;

		private static ConfigEntry<string> defaultFolderConfig = null;

		public const string Id = "com.Jonesy__07.BackgroundLofi";

		public static string Name => "BackgroundLofi";

		public static string Version => "0.1.0";

		private void Awake()
		{
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			musicFolder = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location) ?? throw new InvalidOperationException("Plugin location not found"), "LofiMusic");
			ManualLogSource? logger = Logger;
			if (logger != null)
			{
				logger.LogInfo((object)("Plugin com.Jonesy__07.BackgroundLofi is loaded! Music folder: " + musicFolder));
			}
			Harmony val = new Harmony("com.backgroundlofi.patch");
			val.PatchAll();
			ManualLogSource? logger2 = Logger;
			if (logger2 != null)
			{
				logger2.LogInfo((object)"Harmony patches applied successfully");
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}