Decompiled source of DineSoundRemover v1.3.2

Happyness.DineSoundRemover.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[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("Happyness.DineSoundRemover")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.3.1.0")]
[assembly: AssemblyInformationalVersion("1.3.1")]
[assembly: AssemblyProduct("DineSoundRemover")]
[assembly: AssemblyTitle("Happyness.DineSoundRemover")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.1.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DineSoundRemover
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Happyness.DineSoundRemover";

		public const string PLUGIN_NAME = "DineSoundRemover";

		public const string PLUGIN_VERSION = "1.3.1";
	}
}
namespace Happyness.DineSoundRemover
{
	[BepInPlugin("com.happyness.dinesoundremover", "DineSoundRemover", "1.3.1")]
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.happyness.dinesoundremover";

		public const string PLUGIN_NAME = "DineSoundRemover";

		public const string PLUGIN_VERSION = "1.3.1";
	}
	[BepInPlugin("com.happyness.dinesoundremover", "DineSoundRemover", "1.3.1")]
	public class DineSoundRemoverPlugin : BaseUnityPlugin
	{
		public class MonoBehaviourProxy : MonoBehaviour
		{
		}

		private static MonoBehaviourProxy coroutineProxy;

		public static readonly string[] BlockedNames = new string[6] { "HeartBeat", "HeartBeat2", "HeartBeat2_0", "HeartBeat3", "FliesBuzzingAndMaggots", "FliesBuzzing" };

		internal static ManualLogSource Logger { get; private set; } = null;


		private void Awake()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("com.happyness.dinesoundremover");
			val.PatchAll();
			Logger.LogInfo((object)"[DineSoundRemover] plugin has loaded!");
			SetupCoroutineRunner();
			SceneManager.sceneLoaded += OnSceneLoaded;
			StartSafeCoroutine(CleanupDelayed("Awake"));
		}

		private void SetupCoroutineRunner()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			if (!((Object)(object)coroutineProxy != (Object)null))
			{
				GameObject val = new GameObject("DineSoundRemover_CoroutineRunner");
				coroutineProxy = val.AddComponent<MonoBehaviourProxy>();
				Object.DontDestroyOnLoad((Object)(object)val);
			}
		}

		public static void StartSafeCoroutine(IEnumerator routine)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			if ((Object)(object)coroutineProxy == (Object)null)
			{
				GameObject val = new GameObject("DineSoundRemover_CoroutineRunner");
				coroutineProxy = val.AddComponent<MonoBehaviourProxy>();
				Object.DontDestroyOnLoad((Object)(object)val);
			}
			((MonoBehaviour)coroutineProxy).StartCoroutine(routine);
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			StartSafeCoroutine(CleanupDelayed("OnSceneLoaded"));
		}

		private static IEnumerator CleanupDelayed(string source)
		{
			yield return (object)new WaitForSeconds(5f);
			RemoveBlockedSources();
		}

		private static void RemoveBlockedSources()
		{
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			int num2 = 0;
			AudioSource[] array = Object.FindObjectsOfType<AudioSource>();
			foreach (AudioSource val in array)
			{
				num2++;
				if ((Object)(object)((val != null) ? val.clip : null) == (Object)null)
				{
					continue;
				}
				string name = ((Object)val.clip).name;
				string[] blockedNames = BlockedNames;
				foreach (string value in blockedNames)
				{
					if (name.Contains(value))
					{
						val.Stop();
						val.clip = null;
						((Behaviour)val).enabled = false;
						num++;
						Logger.LogInfo((object)("Silenced AudioSource for " + name));
						break;
					}
				}
			}
			ManualLogSource logger = Logger;
			object arg = num;
			object arg2 = num2;
			Scene activeScene = SceneManager.GetActiveScene();
			logger.LogInfo((object)$"Cleaned {arg}/{arg2} AudioSources in scene '{((Scene)(ref activeScene)).name}'");
		}
	}
	[HarmonyPatch(typeof(RoundManager), "FinishGeneratingNewLevelClientRpc")]
	public static class DineSoundRemoverPatch
	{
		[HarmonyPrefix]
		public static void BeforeFinishGeneratingNewLevelClientRpc(RoundManager __instance)
		{
			DineSoundRemoverPlugin.StartSafeCoroutine(DineSoundRemoverCleanupCoroutine());
		}

		private static IEnumerator DineSoundRemoverCleanupCoroutine()
		{
			yield return (object)new WaitForSeconds(5f);
			AudioSource[] allAudio = Object.FindObjectsOfType<AudioSource>();
			int cleanedCount = 0;
			AudioSource[] array = allAudio;
			foreach (AudioSource audio in array)
			{
				if ((Object)(object)audio != (Object)null && (Object)(object)((Component)audio).gameObject != (Object)null && ((Object)((Component)audio).gameObject).name.IndexOf("BlockedName", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					audio.Stop();
					cleanedCount++;
				}
			}
			DineSoundRemoverPlugin.Logger.LogInfo((object)$"Cleaned {cleanedCount}/{allAudio.Length} AudioSources after FinishGeneratingNewLevelClientRpc");
		}
	}
}