Decompiled source of BoomboxMod v1.0.0

BepInEx/plugins/Lethalmod2.dll

Decompiled 5 hours 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 BepInEx;
using BepInEx.Logging;
using HarmonyLib;
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: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Lethalmod2")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Lethalmod2")]
[assembly: AssemblyTitle("Lethalmod2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.yourname.boomboxmod", "Boombox Custom Music", "1.0.0")]
public class BoomboxMod : BaseUnityPlugin
{
	[HarmonyPatch]
	public class BoomboxPatch
	{
		[HarmonyPatch(typeof(BoomboxItem), "StartMusic", new Type[]
		{
			typeof(bool),
			typeof(bool)
		})]
		[HarmonyPrefix]
		public static bool StartMusicPatch(BoomboxItem __instance, bool startMusic, bool pitchDown)
		{
			logger.LogInfo((object)"StartMusicPatch triggered!");
			if (!startMusic)
			{
				return true;
			}
			if (musicFiles.Length == 0)
			{
				logger.LogWarning((object)"No MP3 files found in BoomboxMusic folder.");
				return true;
			}
			if ((Object)(object)__instance.boomboxAudio == (Object)null)
			{
				logger.LogError((object)"BoomboxItem has no AudioSource!");
				return true;
			}
			logger.LogInfo((object)"Overriding boombox audio with custom music...");
			__instance.boomboxAudio.Stop();
			((MonoBehaviour)__instance).StartCoroutine(PlayNextTrack(__instance.boomboxAudio));
			return false;
		}
	}

	private static ManualLogSource logger;

	private static string musicDirectory;

	private static AudioSource audioSource;

	private static string[] musicFiles;

	private static int currentTrackIndex;

	private void Awake()
	{
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Expected O, but got Unknown
		logger = ((BaseUnityPlugin)this).Logger;
		logger.LogInfo((object)"Boombox Custom Music Loaded");
		string fullName = Directory.GetParent(Application.dataPath).FullName;
		musicDirectory = Path.Combine(Paths.BepInExRootPath, "plugins", "BoomboxMusic");
		if (!Directory.Exists(musicDirectory))
		{
			Directory.CreateDirectory(musicDirectory);
		}
		musicFiles = Directory.GetFiles(musicDirectory, "*.mp3");
		logger.LogInfo((object)"Found music files:");
		string[] array = musicFiles;
		foreach (string text in array)
		{
			logger.LogInfo((object)text);
		}
		logger.LogInfo((object)"Applying Harmony patches...");
		Harmony val = new Harmony("com.yourname.boomboxmod");
		val.PatchAll(Assembly.GetExecutingAssembly());
		logger.LogInfo((object)"Harmony patches applied!");
		List<MethodInfo> declaredMethods = AccessTools.GetDeclaredMethods(typeof(BoomboxItem));
		foreach (MethodInfo item in declaredMethods)
		{
			logger.LogInfo((object)("Found method in BoomboxItem: " + item.Name));
		}
	}

	private static IEnumerator PlayNextTrack(AudioSource boomboxAudio)
	{
		if (musicFiles.Length == 0)
		{
			yield break;
		}
		currentTrackIndex = (currentTrackIndex + 1) % musicFiles.Length;
		string filePath = "file://" + musicFiles[currentTrackIndex];
		logger.LogInfo((object)("Playing: " + filePath));
		UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(filePath, (AudioType)13);
		try
		{
			yield return www.SendWebRequest();
			if ((int)www.result != 1)
			{
				logger.LogError((object)("Failed to load audio: " + www.error));
				yield break;
			}
			boomboxAudio.clip = DownloadHandlerAudioClip.GetContent(www);
			boomboxAudio.Play();
			logger.LogInfo((object)"Custom music is now playing!");
		}
		finally
		{
			((IDisposable)www)?.Dispose();
		}
	}
}