Decompiled source of Leos Music and Sounds Recode v1.0.0

plugins/MeltdownMusicFix.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("MeltdownMusicFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MeltdownMusicFix")]
[assembly: AssemblyTitle("MeltdownMusicFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 MeltdownMusicFix
{
	public class MusicFader : MonoBehaviour
	{
		private AudioSource source;

		private float startVolume;

		private float elapsed;

		public static void FadeOutAndStop(AudioSource source)
		{
			if (!((Object)(object)source == (Object)null) && !((Object)(object)((Component)source).GetComponent<MusicFader>() != (Object)null))
			{
				MusicFader musicFader = ((Component)source).gameObject.AddComponent<MusicFader>();
				musicFader.source = source;
				musicFader.startVolume = source.volume;
			}
		}

		private void Update()
		{
			if ((Object)(object)source == (Object)null)
			{
				Object.Destroy((Object)(object)this);
				return;
			}
			elapsed += Time.deltaTime;
			float num = Mathf.Clamp01(elapsed / 5f);
			source.volume = Mathf.Lerp(startVolume, 0f, num);
			if (num >= 1f)
			{
				source.Stop();
				source.volume = startVolume;
				Object.Destroy((Object)(object)this);
			}
		}
	}
	[BepInPlugin("me.LeosMusicMod.MeltdownFadeRestorePatch", "Meltdown Music Fade Restore Patch", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "me.LeosMusicMod.MeltdownFadeRestorePatch";

		public const string PluginName = "Meltdown Music Fade Restore Patch";

		public const string PluginVersion = "1.0.0";

		public const float FadeSeconds = 5f;

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Type type = AccessTools.TypeByName("FacilityMeltdown.MeltdownSequence.Behaviours.MeltdownHandler");
			if (type == null)
			{
				Log.LogInfo((object)"FacilityMeltdown not found, skipping patch.");
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "Update", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Log.LogWarning((object)"MeltdownHandler.Update not found (FacilityMeltdown updated?), skipping patch.");
				return;
			}
			new Harmony("me.LeosMusicMod.MeltdownFadeRestorePatch").Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "ReplaceStopWithFade", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)"Patched MeltdownHandler.Update: meltdown music will now fade out.");
		}

		private static IEnumerable<CodeInstruction> ReplaceStopWithFade(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo stop = AccessTools.Method(typeof(AudioSource), "Stop", Type.EmptyTypes, (Type[])null);
			MethodInfo fade = AccessTools.Method(typeof(MusicFader), "FadeOutAndStop", (Type[])null, (Type[])null);
			bool replaced = false;
			foreach (CodeInstruction ins in instructions)
			{
				if (CodeInstructionExtensions.Calls(ins, stop))
				{
					ins.opcode = OpCodes.Call;
					ins.operand = fade;
					replaced = true;
				}
				yield return ins;
			}
			if (!replaced)
			{
				Log.LogWarning((object)"AudioSource.Stop() not found in MeltdownHandler.Update, patch did nothing.");
			}
		}
	}
}

plugins/ShipEarlyLeaveMusic.dll

Decompiled 4 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("ShipEarlyLeaveMusic")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ShipEarlyLeaveMusic")]
[assembly: AssemblyTitle("ShipEarlyLeaveMusic")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 ShipEarlyLeaveMusic
{
	internal static class EarlyLeaveMusic
	{
		public const string ClipName = "ShipEarlyLeaveMusic";

		private const int BlankSampleRate = 8000;

		private const int BlankSeconds = 1;

		private static AudioClip blankClip;

		private static GameObject current;

		private static Func<object> getStartOfRound;

		private static FieldInfo inShipPhase;

		private static FieldInfo shipIsLeaving;

		private static bool meltdownResolved;

		private static Func<bool> meltdownStarted;

		private static Func<object> getMeltdownHandler;

		internal static AudioClip BlankClip => blankClip;

		internal static void Init(Type startOfRoundType)
		{
			PropertyInfo prop = AccessTools.Property(startOfRoundType, "Instance");
			FieldInfo field = AccessTools.Field(startOfRoundType, "Instance");
			if (prop != null)
			{
				getStartOfRound = () => prop.GetValue(null, null);
			}
			else if (field != null)
			{
				getStartOfRound = () => field.GetValue(null);
			}
			else
			{
				Plugin.Log.LogWarning((object)"StartOfRound.Instance not found (neither property nor field); can't detect the ship leaving.");
			}
			inShipPhase = AccessTools.Field(startOfRoundType, "inShipPhase");
			shipIsLeaving = AccessTools.Field(startOfRoundType, "shipIsLeaving");
			if (shipIsLeaving == null)
			{
				Plugin.Log.LogWarning((object)"StartOfRound.shipIsLeaving not found; the music won't fade out when the ship leaves.");
			}
			if (inShipPhase == null)
			{
				Plugin.Log.LogWarning((object)"StartOfRound.inShipPhase not found; hard stop in orbit disabled.");
			}
		}

		private static void ResolveMeltdown()
		{
			if (meltdownResolved)
			{
				return;
			}
			Type type = AccessTools.TypeByName("FacilityMeltdown.API.MeltdownAPI");
			Type type2 = AccessTools.TypeByName("FacilityMeltdown.MeltdownSequence.Behaviours.MeltdownHandler");
			if (type == null && type2 == null)
			{
				return;
			}
			if (type != null)
			{
				PropertyInfo prop = AccessTools.Property(type, "MeltdownStarted");
				FieldInfo field = AccessTools.Field(type, "MeltdownStarted");
				if (prop != null)
				{
					meltdownStarted = () => (bool)prop.GetValue(null, null);
				}
				else if (field != null)
				{
					meltdownStarted = () => (bool)field.GetValue(null);
				}
			}
			if (type2 != null)
			{
				PropertyInfo prop2 = AccessTools.Property(type2, "Instance");
				FieldInfo field2 = AccessTools.Field(type2, "Instance");
				if (prop2 != null)
				{
					getMeltdownHandler = () => prop2.GetValue(null, null);
				}
				else if (field2 != null)
				{
					getMeltdownHandler = () => field2.GetValue(null);
				}
			}
			Plugin.Log.LogInfo((object)("Meltdown detection: MeltdownAPI.MeltdownStarted " + ((meltdownStarted != null) ? "found" : "NOT found") + ", MeltdownHandler.Instance " + ((getMeltdownHandler != null) ? "found" : "NOT found") + "."));
			meltdownResolved = true;
		}

		internal static bool MeltdownActive()
		{
			ResolveMeltdown();
			try
			{
				if (meltdownStarted != null && meltdownStarted())
				{
					return true;
				}
				if (getMeltdownHandler != null)
				{
					object obj = getMeltdownHandler();
					Object val = (Object)((obj is Object) ? obj : null);
					if (val != null)
					{
						return val != (Object)null;
					}
					return obj != null;
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Failed to read meltdown state, disabling meltdown detection: " + ex.Message));
				meltdownStarted = null;
				getMeltdownHandler = null;
			}
			return false;
		}

		internal static void Trigger()
		{
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			if ((Object)(object)current != (Object)null)
			{
				return;
			}
			if (MeltdownActive())
			{
				Plugin.Log.LogInfo((object)"Ship is leaving early, but the meltdown is already running: not playing early-leave music.");
				return;
			}
			if ((Object)(object)blankClip == (Object)null)
			{
				blankClip = AudioClip.Create("ShipEarlyLeaveMusic", 8000, 1, 8000, false);
				blankClip.SetData(new float[8000], 0);
			}
			current = new GameObject("ShipEarlyLeaveMusicPlayer");
			current.AddComponent<EarlyLeaveMusicPlayer>();
			Plugin.Log.LogInfo((object)"Ship is leaving early: starting early-leave music.");
		}

		private static bool ReadBool(FieldInfo field)
		{
			if (getStartOfRound == null || field == null)
			{
				return false;
			}
			object obj = getStartOfRound();
			if (obj == null)
			{
				return false;
			}
			return (bool)field.GetValue(obj);
		}

		internal static bool InShipPhase()
		{
			return ReadBool(inShipPhase);
		}

		internal static bool ShipIsLeaving()
		{
			return ReadBool(shipIsLeaving);
		}
	}
	internal class EarlyLeaveMusicPlayer : MonoBehaviour
	{
		private AudioSource source;

		private float age;

		private float fadeElapsed = -1f;

		private float fadeDuration;

		private float startVolume;

		private float fadeVolume;

		private void Start()
		{
			source = ((Component)this).gameObject.AddComponent<AudioSource>();
			source.clip = EarlyLeaveMusic.BlankClip;
			source.spatialBlend = 0f;
			source.loop = false;
			source.playOnAwake = false;
			source.volume = Plugin.Volume.Value;
			source.Play();
		}

		private void BeginFade(float duration, string reason)
		{
			float num = ((fadeElapsed < 0f) ? float.MaxValue : (fadeDuration - fadeElapsed));
			if (!(duration >= num))
			{
				Plugin.Log.LogInfo((object)$"Fading out early-leave music over {duration}s ({reason}).");
				fadeElapsed = 0f;
				fadeDuration = duration;
				startVolume = source.volume;
				fadeVolume = startVolume;
			}
		}

		private void Update()
		{
			age += Time.deltaTime;
			if ((Object)(object)source == (Object)null)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			if (EarlyLeaveMusic.InShipPhase())
			{
				Plugin.Log.LogInfo((object)"Ship is in orbit: stopping early-leave music.");
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			if (EarlyLeaveMusic.MeltdownActive())
			{
				BeginFade(Plugin.ApparatusFadeOutSeconds.Value, "apparatus pulled");
			}
			if (EarlyLeaveMusic.ShipIsLeaving())
			{
				BeginFade(Plugin.FadeOutSeconds.Value, "ship leaving");
			}
			if (fadeElapsed >= 0f)
			{
				fadeElapsed += Time.deltaTime;
				float num = ((fadeDuration <= 0f) ? 1f : Mathf.Clamp01(fadeElapsed / fadeDuration));
				fadeVolume = Mathf.Lerp(startVolume, 0f, num);
				source.volume = fadeVolume;
				if (num >= 1f)
				{
					source.Stop();
					Object.Destroy((Object)(object)((Component)this).gameObject);
				}
			}
			else if (age > 0.5f && !source.isPlaying)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
			}
		}

		private void LateUpdate()
		{
			if ((Object)(object)source != (Object)null && fadeElapsed >= 0f)
			{
				source.volume = fadeVolume;
			}
		}
	}
	[BepInPlugin("me.LeosMusicMod.ShipEarlyLeaveMusic", "Ship Early Leave Music", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "me.LeosMusicMod.ShipEarlyLeaveMusic";

		public const string PluginName = "Ship Early Leave Music";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<float> Volume;

		internal static ConfigEntry<float> FadeOutSeconds;

		internal static ConfigEntry<float> ApparatusFadeOutSeconds;

		private void Awake()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Volume = ((BaseUnityPlugin)this).Config.Bind<float>("ShipLeaveEarlyVolume", "Volume", 1f, new ConfigDescription("Volume of the early-leave music.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			FadeOutSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Debug", "FadeOutSeconds", 4f, new ConfigDescription("How long the music takes to fade out once the ship starts leaving.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 30f), Array.Empty<object>()));
			ApparatusFadeOutSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Debug", "ApparatusFadeOutSeconds", 1f, new ConfigDescription("How long the music takes to fade out when the apparatus is pulled (so it doesn't overlap the meltdown music).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 30f), Array.Empty<object>()));
			Type type = AccessTools.TypeByName("TimeOfDay");
			Type type2 = AccessTools.TypeByName("StartOfRound");
			if (type == null || type2 == null)
			{
				Log.LogError((object)"Couldn't find TimeOfDay / StartOfRound types, patch not applied.");
				return;
			}
			EarlyLeaveMusic.Init(type2);
			MethodInfo methodInfo = AccessTools.Method(type, "SetShipLeaveEarlyClientRpc", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Log.LogError((object)"TimeOfDay.SetShipLeaveEarlyClientRpc not found (game updated?), patch not applied.");
				return;
			}
			new Harmony("me.LeosMusicMod.ShipEarlyLeaveMusic").Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnShipLeaveEarly", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Log.LogInfo((object)"Patched TimeOfDay.SetShipLeaveEarlyClientRpc.");
		}

		private static void OnShipLeaveEarly()
		{
			try
			{
				EarlyLeaveMusic.Trigger();
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Failed to start early-leave music: {arg}");
			}
		}
	}
}