Decompiled source of LoadingMusic v1.0.2

LoadingMusic.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LoadingMusic")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LoadingMusic")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("85a1a463-8a4d-4d58-b958-04333df36a3d")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LoadingMusic;

[BepInPlugin("ZetaArcade.LoadingMusic", "LoadingMusic", "1.0.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class LoadingMusicBase : BaseUnityPlugin
{
	private readonly Harmony harmony = new Harmony("ZetaArcade.LoadingMusic");

	public static ManualLogSource Log;

	public static LoadingMusicBase Instance;

	public bool tooManyEmotesDetected;

	public bool lethalPhonesDetected;

	public bool codeRebirthDetected;

	public bool lethalThingsDetected;

	internal static Type CodeRebirthInstrumentType;

	internal static FieldInfo CodeRebirthInstrumentPlayersField;

	internal static FieldInfo CodeRebirthAudioPlayerField;

	internal static MethodBase CodeRebirthInstrumentActivateMethod;

	internal static Type TooManyEmotesType;

	internal static MethodBase TooManyEmotesUpdateVolumeMethod;

	internal static Type LethalPhonesType;

	internal static MethodBase LethalPhonesStartRingingMethod;

	internal static Type LethalThingsDingusType;

	internal static MethodBase LethalThingsDingusUpdateMethod;

	public ConfigEntry<bool> ToggleLoadingMusic;

	public ConfigEntry<bool> ToggleLoadingMusicLoop;

	public ConfigEntry<bool> ToggleCruiserMute;

	public ConfigEntry<bool> ToggleScrapsMute;

	public ConfigEntry<bool> ToggleBoomboxMute;

	public ConfigEntry<bool> ToggleEmotesMute;

	public ConfigEntry<bool> ToggleRingtoneMute;

	public ConfigEntry<bool> ToggleScrapsOrbitMute;

	public ConfigEntry<bool> ToggleBoomboxOrbitMute;

	public ConfigEntry<bool> ToggleRingtoneOrbitMute;

	public ConfigEntry<float> MusicFadeTime;

	public ConfigEntry<float> MusicVolume;

	public bool IsMusicPlaying { get; set; } = false;

	private void Awake()
	{
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		ToggleLoadingMusic = ((BaseUnityPlugin)this).Config.Bind<bool>("Loading Music Settings", "Should loading music play", true, "Toggles the loading music in the ship");
		ToggleLoadingMusicLoop = ((BaseUnityPlugin)this).Config.Bind<bool>("Loading Music Settings", "Should loading music loop", true, "When enabled, the loading music will loop until it fades out rather than ending early");
		ToggleCruiserMute = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio Mutes", "Mute Cruiser Radio", true, "When enabled, the Cruiser's Radio will be muted anytime the Loading Music is playing. This is useful for preventing the Radio overlapping with the Loading Music.");
		ToggleScrapsMute = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio Mutes", "Mute Scraps", true, "When enabled, autonomous noisy scraps like Maxwell from LethalThings and the Zort Instruments from CodeRebirth will be muted anytime the Loading Music is playing. This is useful for preventing the audio overlapping with the Loading Music.");
		ToggleScrapsOrbitMute = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio Mutes", "Mute Scraps in Orbit", false, "When enabled, autonomous noisy scraps like Maxwell from LethalThings and the Zort Instruments from CodeRebirth will be also be muted in orbit. This is just a personal preference for me, to reduce the amount of noise spam in Orbit.");
		ToggleBoomboxMute = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio Mutes", "Mute Boombox", true, "When enabled, all Boomboxes will be muted anytime the Loading Music is playing. This is useful for preventing the Boombox overlapping with the Loading Music. Might conflict with other mods that mess with the Boombox mute status.");
		ToggleBoomboxOrbitMute = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio Mutes", "Mute Boombox in Orbit", false, "When enabled, all Boomboxes will also be muted in orbit. This is just a personal preference for me, to reduce the amount of noise spam in Orbit. Might conflict with other mods that mess with the Boombox mute status.");
		ToggleEmotesMute = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio Mutes", "Mute Emotes", true, "When enabled, Emote Audio from TooManyEmotes will be muted anytime the Loading Music is playing. This is useful for preventing the songs overlapping with the Loading Music.");
		ToggleRingtoneMute = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio Mutes", "Mute Ringtones", true, "When enabled, Ringtones from LethalPhones will be muted anytime the Loading Music is playing. This is useful for preventing the Ringtones overlapping with the Loading Music.");
		ToggleRingtoneOrbitMute = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio Mutes", "Mute Ringtones in Orbit", false, "When enabled, Ringtones will be also be muted in orbit. This is just a personal preference for me, to reduce the amount of noise spam in Orbit.");
		MusicFadeTime = ((BaseUnityPlugin)this).Config.Bind<float>("Loading Music Settings", "Loading music fade time", 15f, "How long it should take for the music to fade out");
		MusicVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Loading Music Settings", "Loading music volume", 0.75f, "The volume of the loading music");
		Log = ((BaseUnityPlugin)this).Logger;
		tooManyEmotesDetected = Chainloader.PluginInfos.Values.Any((PluginInfo plugin) => plugin.Metadata.GUID == "FlipMods.TooManyEmotes");
		lethalPhonesDetected = Chainloader.PluginInfos.Values.Any((PluginInfo plugin) => plugin.Metadata.GUID == "LethalPhones");
		lethalThingsDetected = Chainloader.PluginInfos.Values.Any((PluginInfo plugin) => plugin.Metadata.GUID == "HQ_Team.LethalThingsReloaded.Internals");
		codeRebirthDetected = Chainloader.PluginInfos.Values.Any((PluginInfo plugin) => plugin.Metadata.GUID == "CodeRebirth");
		harmony.PatchAll(typeof(RoundManagerMusicPatches));
		harmony.PatchAll(typeof(BoomboxPatch));
		harmony.PatchAll(typeof(VehicleControllerPatch));
		harmony.PatchAll(typeof(StartOfRoundPatch));
		if (tooManyEmotesDetected)
		{
			Log.LogDebug((object)"LoadingMusic --> TooManyEmotes FOUND");
			try
			{
				TooManyEmotesType = AccessTools.TypeByName("TooManyEmotes.Audio.EmoteAudioSource");
				if (TooManyEmotesType == null)
				{
					throw new Exception("Could not find TooManyEmotes.Audio.EmoteAudioSource");
				}
				TooManyEmotesUpdateVolumeMethod = AccessTools.Method(TooManyEmotesType, "UpdateVolume", (Type[])null, (Type[])null);
				if (TooManyEmotesUpdateVolumeMethod == null)
				{
					throw new Exception("Could not find EmoteAudioSource.UpdateVolume");
				}
				harmony.PatchAll(typeof(TooManyEmotesPatch));
				Log.LogDebug((object)"LoadingMusic --> TooManyEmotes PATCHED");
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"LoadingMusic --> TooManyEmotes PATCH FAILED:\n{arg}");
			}
		}
		else
		{
			Log.LogDebug((object)"LoadingMusic --> TooManyEmotes MISSING");
		}
		if (codeRebirthDetected)
		{
			Log.LogDebug((object)"LoadingMusic --> CodeRebirth FOUND");
			try
			{
				CodeRebirthInstrumentType = AccessTools.TypeByName("CodeRebirth.src.Content.Items.InstrumentPlayer");
				if (CodeRebirthInstrumentType == null)
				{
					throw new Exception("Could not find CodeRebirth.src.Content.Items.InstrumentPlayer");
				}
				CodeRebirthInstrumentPlayersField = AccessTools.Field(CodeRebirthInstrumentType, "instrumentPlayers");
				if (CodeRebirthInstrumentPlayersField == null)
				{
					throw new Exception("Could not find InstrumentPlayer.instrumentPlayers");
				}
				CodeRebirthAudioPlayerField = AccessTools.Field(CodeRebirthInstrumentType, "audioPlayer");
				if (CodeRebirthAudioPlayerField == null)
				{
					throw new Exception("Could not find InstrumentPlayer.audioPlayer");
				}
				CodeRebirthInstrumentActivateMethod = AccessTools.Method(CodeRebirthInstrumentType, "ItemActivate", (Type[])null, (Type[])null);
				if (CodeRebirthInstrumentActivateMethod == null)
				{
					throw new Exception("Could not find InstrumentPlayer.ItemActivate");
				}
				harmony.PatchAll(typeof(CodeRebirthInstrumentPatch));
				Log.LogDebug((object)"LoadingMusic --> CodeRebirth PATCHED");
			}
			catch (Exception arg2)
			{
				Log.LogError((object)$"LoadingMusic --> CodeRebirth PATCH FAILED:\n{arg2}");
			}
		}
		else
		{
			Log.LogDebug((object)"LoadingMusic --> CodeRebirth MISSING");
		}
		if (lethalPhonesDetected)
		{
			Log.LogDebug((object)"LoadingMusic --> LethalPhones FOUND");
			try
			{
				LethalPhonesType = AccessTools.TypeByName("Scoops.misc.PlayerPhone");
				if (LethalPhonesType == null)
				{
					throw new Exception("Could not find Scoops.misc.PlayerPhone");
				}
				LethalPhonesStartRingingMethod = AccessTools.Method(LethalPhonesType, "StartRinging", (Type[])null, (Type[])null);
				if (LethalPhonesStartRingingMethod == null)
				{
					throw new Exception("Could not find PlayerPhone.StartRinging");
				}
				harmony.PatchAll(typeof(LethalPhonesPatch));
				Log.LogDebug((object)"LoadingMusic --> LethalPhones PATCHED");
			}
			catch (Exception arg3)
			{
				Log.LogError((object)$"LoadingMusic --> LethalPhones PATCH FAILED:\n{arg3}");
			}
		}
		else
		{
			Log.LogDebug((object)"LoadingMusic --> LethalPhones MISSING");
		}
		if (lethalThingsDetected)
		{
			Log.LogDebug((object)"LoadingMusic --> LethalThings FOUND");
			try
			{
				LethalThingsDingusType = AccessTools.TypeByName("LethalThings.MonoBehaviours.Dingus");
				if (LethalThingsDingusType == null)
				{
					throw new Exception("Could not find LethalThings.MonoBehaviours.Dingus");
				}
				LethalThingsDingusUpdateMethod = AccessTools.Method(LethalThingsDingusType, "Update", (Type[])null, (Type[])null);
				if (LethalThingsDingusUpdateMethod == null)
				{
					throw new Exception("Could not find Dingus.Update");
				}
				Log.LogDebug((object)("LoadingMusic --> Found " + LethalThingsDingusType.FullName + " in " + LethalThingsDingusType.Assembly.GetName().Name));
				harmony.PatchAll(typeof(LethalThingsDingusPatch));
				Log.LogInfo((object)"LoadingMusic --> LethalThings PATCHED");
			}
			catch (Exception arg4)
			{
				Log.LogError((object)$"LoadingMusic --> LethalThings PATCH FAILED:\n{arg4}");
			}
		}
		else
		{
			Log.LogDebug((object)"LoadingMusic --> LethalThings MISSING");
		}
		IsMusicPlaying = false;
		Log.LogInfo((object)"Plugin is loaded!");
	}

	public void MuteCodeRebirthInstruments()
	{
		if (!codeRebirthDetected || CodeRebirthInstrumentPlayersField == null || CodeRebirthAudioPlayerField == null || !(CodeRebirthInstrumentPlayersField.GetValue(null) is IEnumerable enumerable))
		{
			return;
		}
		foreach (object item in enumerable)
		{
			if (item != null)
			{
				object? value = CodeRebirthAudioPlayerField.GetValue(item);
				AudioSource val = (AudioSource)((value is AudioSource) ? value : null);
				if (val != null)
				{
					val.mute = true;
				}
			}
		}
	}
}
[HarmonyPatch(typeof(RoundManager))]
public class RoundManagerMusicPatches
{
	internal static AudioSource loadingAudioSource;

	internal static AudioClip loadingAudioClip = null;

	public static VehicleController Cruiser;

	public static ManualLogSource ThisLogger = LoadingMusicBase.Log;

	[HarmonyPatch("Awake")]
	[HarmonyPrefix]
	private static void CreateAudioSource()
	{
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Invalid comparison between Unknown and I4
		ThisLogger.LogDebug((object)"Audio source being created");
		if ((Object)(object)loadingAudioSource != (Object)null)
		{
			Object.Destroy((Object)(object)loadingAudioSource);
		}
		if ((Object)(object)loadingAudioClip == (Object)null)
		{
			loadingAudioClip = Resources.FindObjectsOfTypeAll(typeof(AudioClip)).Cast<AudioClip>().FirstOrDefault((Func<AudioClip, bool>)((AudioClip a) => ((Object)a).name == "ElevatorJingle"));
		}
		if ((Object)(object)loadingAudioClip == (Object)null)
		{
			ThisLogger.LogDebug((object)"Unable to find elevator jingle");
		}
		if ((int)loadingAudioClip.loadState != 2)
		{
			loadingAudioClip.LoadAudioData();
		}
		loadingAudioSource = Object.Instantiate<AudioSource>(StartOfRound.Instance.speakerAudioSource);
		((Object)loadingAudioSource).name = "LoadingMusic";
		loadingAudioSource.clip = Object.Instantiate<AudioClip>(loadingAudioClip);
		((Object)loadingAudioSource.clip).name = ((Object)loadingAudioSource).name;
		((Component)loadingAudioSource).transform.parent = ((Component)StartOfRound.Instance.speakerAudioSource).transform;
	}

	[HarmonyPatch("GenerateNewLevelClientRpc")]
	[HarmonyPrefix]
	private static void PlayWaitingMusicPatch()
	{
		ThisLogger.LogDebug((object)"PlayWaitingMusicPatch is running");
		if (LoadingMusicBase.Instance.ToggleLoadingMusic.Value)
		{
			loadingAudioSource.loop = LoadingMusicBase.Instance.ToggleLoadingMusicLoop.Value;
			loadingAudioSource.volume = LoadingMusicBase.Instance.MusicVolume.Value;
			loadingAudioSource.Play();
			LoadingMusicBase.Instance.IsMusicPlaying = true;
			LoadingMusicBase.Instance.MuteCodeRebirthInstruments();
			if ((Object)(object)Cruiser == (Object)null && LoadingMusicBase.Instance.ToggleCruiserMute.Value)
			{
				Cruiser = Object.FindFirstObjectByType<VehicleController>();
			}
			if ((Object)(object)Cruiser != (Object)null && LoadingMusicBase.Instance.ToggleCruiserMute.Value)
			{
				Cruiser.radioAudio.mute = true;
				Cruiser.radioInterference.mute = true;
			}
			ThisLogger.LogDebug((object)"Music triggered");
		}
	}

	[HarmonyPatch("ResetEnemySpawningVariables")]
	[HarmonyPostfix]
	private static void StopWaitingMusicPatch()
	{
		ThisLogger.LogDebug((object)"Music fadeout triggered");
		((MonoBehaviour)RoundManager.Instance).StartCoroutine(FadeOutMusic(loadingAudioSource));
	}

	private static IEnumerator FadeOutMusic(AudioSource source)
	{
		float originalVolume = source.volume;
		float timeElapsed = 0f;
		while ((double)source.volume > 0.01)
		{
			LoadingMusicBase.Instance.IsMusicPlaying = true;
			source.volume = Mathf.Lerp(originalVolume, 0f, timeElapsed);
			timeElapsed += Time.deltaTime / LoadingMusicBase.Instance.MusicFadeTime.Value;
			yield return null;
		}
		source.Stop();
		source.volume = originalVolume;
		ThisLogger.LogDebug((object)"Music faded and stopped");
		LoadingMusicBase.Instance.IsMusicPlaying = false;
		if ((Object)(object)Cruiser != (Object)null && LoadingMusicBase.Instance.ToggleCruiserMute.Value)
		{
			Cruiser.radioAudio.mute = false;
			Cruiser.radioInterference.mute = false;
		}
	}
}
[HarmonyPatch(typeof(BoomboxItem))]
internal class BoomboxPatch
{
	[HarmonyPostfix]
	[HarmonyPriority(0)]
	[HarmonyPatch("Update")]
	private static void MuteOnUpdate(BoomboxItem __instance)
	{
		if (LoadingMusicBase.Instance.ToggleBoomboxMute.Value && LoadingMusicBase.Instance.IsMusicPlaying)
		{
			__instance.boomboxAudio.mute = true;
		}
		else if (LoadingMusicBase.Instance.ToggleBoomboxOrbitMute.Value && StartOfRound.Instance.inShipPhase)
		{
			__instance.boomboxAudio.mute = true;
		}
		else
		{
			__instance.boomboxAudio.mute = false;
		}
	}
}
[HarmonyPatch(typeof(VehicleController))]
internal class VehicleControllerPatch
{
	[HarmonyPostfix]
	[HarmonyPriority(0)]
	[HarmonyPatch("LateUpdate")]
	private static void MuteOnUpdate(VehicleController __instance)
	{
		if (LoadingMusicBase.Instance.IsMusicPlaying && LoadingMusicBase.Instance.ToggleCruiserMute.Value)
		{
			__instance.radioAudio.mute = true;
			__instance.radioInterference.mute = true;
		}
	}
}
[HarmonyPatch]
internal class TooManyEmotesPatch
{
	private static MethodBase TargetMethod()
	{
		return LoadingMusicBase.TooManyEmotesUpdateVolumeMethod;
	}

	[HarmonyPostfix]
	[HarmonyPriority(0)]
	private static void UpdateVolumePostfix(AudioSource ___audioSource, AudioSource ___audioLoopSource)
	{
		if (LoadingMusicBase.Instance.ToggleEmotesMute.Value && LoadingMusicBase.Instance.IsMusicPlaying)
		{
			___audioSource.mute = true;
			___audioLoopSource.mute = true;
		}
		else
		{
			___audioSource.mute = false;
			___audioLoopSource.mute = false;
		}
	}
}
[HarmonyPatch]
internal class CodeRebirthInstrumentPatch
{
	private static MethodBase TargetMethod()
	{
		return LoadingMusicBase.CodeRebirthInstrumentActivateMethod;
	}

	[HarmonyPostfix]
	[HarmonyPriority(0)]
	private static void MuteOnActivate(AudioSource ___audioPlayer)
	{
		if (LoadingMusicBase.Instance.ToggleScrapsMute.Value && LoadingMusicBase.Instance.IsMusicPlaying)
		{
			___audioPlayer.mute = true;
		}
		else if (LoadingMusicBase.Instance.ToggleScrapsOrbitMute.Value && StartOfRound.Instance.inShipPhase)
		{
			___audioPlayer.mute = true;
		}
		else
		{
			___audioPlayer.mute = false;
		}
	}
}
[HarmonyPatch(typeof(StartOfRound))]
internal class StartOfRoundPatch
{
	[HarmonyPostfix]
	[HarmonyPatch("SetShipReadyToLand")]
	private static void OnShipReadyToLand()
	{
		LoadingMusicBase.Instance.MuteCodeRebirthInstruments();
	}
}
[HarmonyPatch]
internal class LethalThingsDingusPatch
{
	private static MethodBase TargetMethod()
	{
		return LoadingMusicBase.LethalThingsDingusUpdateMethod;
	}

	[HarmonyPostfix]
	[HarmonyPriority(0)]
	private static void MuteOnUpdate(AudioSource ___musicAudio, AudioSource ___musicAudioFar)
	{
		if (LoadingMusicBase.Instance.ToggleScrapsMute.Value && LoadingMusicBase.Instance.IsMusicPlaying)
		{
			___musicAudio.mute = true;
			___musicAudioFar.mute = true;
		}
		else if (LoadingMusicBase.Instance.ToggleScrapsOrbitMute.Value && StartOfRound.Instance.inShipPhase)
		{
			___musicAudio.mute = true;
			___musicAudioFar.mute = true;
		}
		else
		{
			___musicAudio.mute = false;
			___musicAudioFar.mute = false;
		}
	}
}
[HarmonyPatch]
internal class LethalPhonesPatch
{
	private static MethodBase TargetMethod()
	{
		return LoadingMusicBase.LethalPhonesStartRingingMethod;
	}

	[HarmonyPostfix]
	[HarmonyPriority(0)]
	private static void MuteRingtone(AudioSource ___ringAudio, AudioSource ___corpseRingAudio)
	{
		if (LoadingMusicBase.Instance.ToggleRingtoneMute.Value && LoadingMusicBase.Instance.IsMusicPlaying)
		{
			___ringAudio.mute = true;
			if ((Object)(object)___corpseRingAudio != (Object)null)
			{
				___corpseRingAudio.mute = true;
			}
		}
		else if (LoadingMusicBase.Instance.ToggleRingtoneOrbitMute.Value && StartOfRound.Instance.inShipPhase)
		{
			___ringAudio.mute = true;
			if ((Object)(object)___corpseRingAudio != (Object)null)
			{
				___corpseRingAudio.mute = true;
			}
		}
		else
		{
			___ringAudio.mute = false;
			if ((Object)(object)___corpseRingAudio != (Object)null)
			{
				___corpseRingAudio.mute = false;
			}
		}
	}
}
public static class PluginInfo
{
	public const string modGUID = "ZetaArcade.LoadingMusic";

	public const string modName = "LoadingMusic";

	public const string modVersion = "1.0.2";
}