Decompiled source of MeltdownSyncMusic v1.3.2

BepInEx/plugins/MeltdownSyncMusic/FacilityMeltdownMusicSync.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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using PizzaTowerEscapeMusic;
using PizzaTowerEscapeMusic.Scripting;
using PizzaTowerEscapeMusic.Scripting.ScriptEvents;
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("FacilityMeltdownMusicSync")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FacilityMeltdownMusicSync")]
[assembly: AssemblyTitle("FacilityMeltdownMusicSync")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 FacilityMeltdownMusicSync
{
	[BepInPlugin("potatoh.facilitymeltdownmusicsync", "FacilityMeltdownMusicSync", "0.0.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "potatoh.facilitymeltdownmusicsync";

		public const string PluginName = "FacilityMeltdownMusicSync";

		public const string PluginVersion = "0.0.2";

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("potatoh.facilitymeltdownmusicsync").PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"FacilityMeltdownMusicSync loaded.");
		}
	}
}
namespace FacilityMeltdownMusicSync.Patches
{
	[HarmonyPatch(typeof(MusicManager), "PlayMusic")]
	internal static class MusicManagerPatch
	{
		private sealed class OriginalMusicNamesHolder
		{
			public string[] Value;

			public OriginalMusicNamesHolder(string[] value)
			{
				Value = value;
			}
		}

		private static readonly ConditionalWeakTable<ScriptEvent_PlayMusic, OriginalMusicNamesHolder> OriginalMusicNames = new ConditionalWeakTable<ScriptEvent_PlayMusic, OriginalMusicNamesHolder>();

		private static ManualLogSource Log => Plugin.Log;

		private static void Prefix(Script script, ScriptEvent_PlayMusic musicEvent)
		{
			try
			{
				if (ShouldHandle(musicEvent))
				{
					string[] musicNames = musicEvent.musicNames;
					if (musicNames != null && musicNames.Length > 1)
					{
						OriginalMusicNames.Remove(musicEvent);
						OriginalMusicNames.Add(musicEvent, new OriginalMusicNamesHolder(musicNames));
						int syncedIndex = GetSyncedIndex(musicNames.Length);
						string text = musicNames[syncedIndex];
						musicEvent.musicNames = new string[1] { text };
						Log.LogInfo((object)$"[MeltdownSync] Chose synced track index {syncedIndex}: {text}");
					}
				}
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"[MeltdownSync] Prefix failed: {arg}");
			}
		}

		private static void Postfix(Script script, ScriptEvent_PlayMusic musicEvent)
		{
			try
			{
				if (musicEvent != null && OriginalMusicNames.TryGetValue(musicEvent, out OriginalMusicNamesHolder value))
				{
					musicEvent.musicNames = value.Value;
					OriginalMusicNames.Remove(musicEvent);
				}
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"[MeltdownSync] Postfix failed: {arg}");
			}
		}

		private static bool ShouldHandle(ScriptEvent_PlayMusic musicEvent)
		{
			if (musicEvent == null)
			{
				return false;
			}
			if (musicEvent.musicNames == null || musicEvent.musicNames.Length == 0)
			{
				return false;
			}
			if (!string.Equals(musicEvent.tag, "EscapeMusic", StringComparison.Ordinal))
			{
				return false;
			}
			return true;
		}

		private static int GetSyncedIndex(int trackCount)
		{
			if (trackCount <= 0)
			{
				return 0;
			}
			int num = (((Object)(object)StartOfRound.Instance != (Object)null) ? StartOfRound.Instance.randomMapSeed : 0);
			if (num == int.MinValue)
			{
				num = 0;
			}
			num = Math.Abs(num);
			return num % trackCount;
		}
	}
}