Decompiled source of HofmaDresuBoomboxPlaylist v1.0.5

hofmadresu.hofmadresuboomboxplaylist.dll

Decompiled 5 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using HofmaDresuBoomboxPlaylist.Managers;
using HofmaDresuBoomboxPlaylist.Utilities;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("hofmadresu.hofmadresuboomboxplaylist")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.5.0")]
[assembly: AssemblyInformationalVersion("1.0.5+179f2f927f3cb415df18627d8d31d948e359551d")]
[assembly: AssemblyProduct("HofmaDresuBoomboxPlaylist")]
[assembly: AssemblyTitle("hofmadresu.hofmadresuboomboxplaylist")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.5.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 HofmaDresuBoomboxPlaylist
{
	[BepInPlugin("hofmadresu.hofmadresuboomboxplaylist", "HofmaDresuBoomboxPlaylist", "1.0.5")]
	public class HofmaDresuBoomboxPlaylist : BaseUnityPlugin
	{
		public static HofmaDresuBoomboxPlaylist Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Patch();
			Logger.LogInfo((object)"hofmadresu.hofmadresuboomboxplaylist v1.0.5 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("hofmadresu.hofmadresuboomboxplaylist");
			}
			Logger.LogInfo((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogInfo((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogInfo((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogInfo((object)"Finished unpatching!");
		}

		internal static void LogDebug(string message)
		{
			Instance.Log(message, (LogLevel)32);
		}

		internal static void LogInfo(string message)
		{
			Instance.Log(message, (LogLevel)16);
		}

		internal static void LogWarning(string message)
		{
			Instance.Log(message, (LogLevel)4);
		}

		internal static void LogError(string message)
		{
			Instance.Log(message, (LogLevel)2);
		}

		internal static void LogError(Exception ex)
		{
			Instance.Log(ex.Message + "\n" + ex.StackTrace, (LogLevel)2);
		}

		private void Log(string message, LogLevel logLevel)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			Logger.Log(logLevel, (object)message);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "hofmadresu.hofmadresuboomboxplaylist";

		public const string PLUGIN_NAME = "HofmaDresuBoomboxPlaylist";

		public const string PLUGIN_VERSION = "1.0.5";
	}
}
namespace HofmaDresuBoomboxPlaylist.Utilities
{
	public class SharedCoroutineStarter : MonoBehaviour
	{
		private static SharedCoroutineStarter? _instance;

		public static Coroutine StartCoroutine(IEnumerator routine)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_instance == (Object)null)
			{
				HofmaDresuBoomboxPlaylist.LogInfo("Starting new coroutine isntance");
				_instance = new GameObject("Shared Coroutine Starter").AddComponent<SharedCoroutineStarter>();
				Object.DontDestroyOnLoad((Object)(object)_instance);
			}
			return ((MonoBehaviour)_instance).StartCoroutine(routine);
		}
	}
}
namespace HofmaDresuBoomboxPlaylist.Patches
{
	[HarmonyPatch(typeof(BoomboxItem), "PocketItem")]
	internal class BoomboxItem_PocketItem
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = instructions.ToList();
			bool flag = false;
			for (int i = 0; i < list.Count; i++)
			{
				if (!flag)
				{
					if (list[i].opcode == OpCodes.Call)
					{
						flag = true;
					}
					continue;
				}
				if (list[i].opcode == OpCodes.Ret)
				{
					break;
				}
				list[i].opcode = OpCodes.Nop;
			}
			return list;
		}
	}
	[HarmonyPatch(typeof(BoomboxItem), "Start")]
	internal class BoomboxItem_Start
	{
		private const float targetVolume = 0.4f;

		private static void Postfix(BoomboxItem __instance)
		{
			BoomboxItem __instance2 = __instance;
			if (__instance2.boomboxAudio.volume != 0.4f)
			{
				__instance2.boomboxAudio.volume = 0.4f;
				HofmaDresuBoomboxPlaylist.LogInfo($"volume set to {0.4f}");
			}
			((GrabbableObject)__instance2).itemProperties.requiresBattery = false;
			if (AudioManager.FinishedLoading)
			{
				AudioManager.ApplyClips(__instance2);
				return;
			}
			AudioManager.OnAllSongsLoaded += delegate
			{
				AudioManager.ApplyClips(__instance2);
			};
		}
	}
	[HarmonyPatch(typeof(BoomboxItem), "StartMusic")]
	internal class BoomboxItem_StartMusic
	{
		private static void Postfix(BoomboxItem __instance, bool startMusic)
		{
			if (startMusic)
			{
				switch (((Object)__instance.boomboxAudio.clip).name)
				{
				case "Bitter Regret.mp3":
					__instance.boomboxAudio.volume = 0.6f;
					break;
				case "Lethal Company Blues.mp3":
					__instance.boomboxAudio.volume = 0.4f;
					break;
				case "Scrap Hunt Blues.mp3":
					__instance.boomboxAudio.volume = 0.4f;
					break;
				}
				HofmaDresuBoomboxPlaylist.LogInfo($"Playing {((Object)__instance.boomboxAudio.clip).name} at volume {__instance.boomboxAudio.volume}");
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "Awake")]
	internal class StartOfRound_Awake
	{
		private static void Prefix()
		{
			AudioManager.Load();
		}
	}
}
namespace HofmaDresuBoomboxPlaylist.Managers
{
	internal static class AudioManager
	{
		private static readonly List<AudioClip> clips = new List<AudioClip>();

		private static bool firstRun = true;

		private static bool finishedLoading = false;

		private static readonly string directory = Path.Combine(Paths.BepInExRootPath, "plugins", "hofmadresu-HofmaDresuBoomboxPlaylist");

		private static readonly string[] allowedSongs = new string[3] { "Bitter Regret.mp3", "Lethal Company Blues.mp3", "Scrap Hunt Blues.mp3" };

		public static bool FinishedLoading => finishedLoading;

		public static event Action? OnAllSongsLoaded;

		public static void Load()
		{
			if (!firstRun)
			{
				return;
			}
			firstRun = false;
			string[] files = Directory.GetFiles(directory);
			if (files.Length == 0)
			{
				HofmaDresuBoomboxPlaylist.LogInfo("No songs found!");
				return;
			}
			HofmaDresuBoomboxPlaylist.LogInfo("Preparing to load AudioClips...");
			List<Coroutine> list = new List<Coroutine>();
			string[] array = files;
			foreach (string filePath in array)
			{
				Coroutine item = SharedCoroutineStarter.StartCoroutine(LoadAudioClip(filePath));
				list.Add(item);
			}
			SharedCoroutineStarter.StartCoroutine(WaitForAllClips(list));
		}

		private static IEnumerator LoadAudioClip(string filePath)
		{
			HofmaDresuBoomboxPlaylist.LogInfo("Loading " + filePath + "!");
			if (!filePath.EndsWith(".mp3"))
			{
				yield break;
			}
			if (!allowedSongs.Any(filePath.EndsWith))
			{
				HofmaDresuBoomboxPlaylist.LogError("The song " + filePath + " is not a part of this package, skipping!");
				yield break;
			}
			UnityWebRequest loader = UnityWebRequestMultimedia.GetAudioClip(filePath, (AudioType)13);
			DownloadHandler downloadHandler = loader.downloadHandler;
			DownloadHandlerAudioClip val = (DownloadHandlerAudioClip)(object)((downloadHandler is DownloadHandlerAudioClip) ? downloadHandler : null);
			if (val != null)
			{
				val.streamAudio = true;
			}
			loader.SendWebRequest();
			while (!loader.isDone)
			{
				yield return null;
			}
			if (loader.error != null)
			{
				HofmaDresuBoomboxPlaylist.LogInfo("Error loading clip from path: " + filePath + "\n" + loader.error);
				HofmaDresuBoomboxPlaylist.LogInfo(loader.error);
				yield break;
			}
			AudioClip content = DownloadHandlerAudioClip.GetContent(loader);
			if (Object.op_Implicit((Object)(object)content) && (int)content.loadState == 2)
			{
				HofmaDresuBoomboxPlaylist.LogInfo("Loaded " + filePath);
				((Object)content).name = Path.GetFileName(filePath);
				clips.Add(content);
			}
			else
			{
				HofmaDresuBoomboxPlaylist.LogInfo("Failed to load clip at: " + filePath + "\nThis might be due to an mismatch between the audio codec and the file extension!");
			}
		}

		private static IEnumerator WaitForAllClips(List<Coroutine> coroutines)
		{
			foreach (Coroutine coroutine in coroutines)
			{
				yield return coroutine;
			}
			clips.Sort((AudioClip first, AudioClip second) => ((Object)first).name.CompareTo(((Object)second).name));
			finishedLoading = true;
			AudioManager.OnAllSongsLoaded?.Invoke();
			AudioManager.OnAllSongsLoaded = null;
		}

		public static void ApplyClips(BoomboxItem __instance)
		{
			HofmaDresuBoomboxPlaylist.LogInfo("Applying clips!");
			if (clips.Any())
			{
				__instance.musicAudios = clips.ToArray();
			}
			HofmaDresuBoomboxPlaylist.LogInfo($"Total Clip Count: {__instance.musicAudios.Length}");
		}
	}
}