Decompiled source of ShutupPortal v1.0.0

ShutupPortal.dll

Decompiled 15 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace ShutupPortal;

[BepInPlugin("com.parallel53.shutupportal", "ShutupPortal", "1.0.0")]
public class ShutupPortalPlugin : BaseUnityPlugin
{
	internal static ConfigEntry<float> ActivationVolume;

	internal static ConfigEntry<float> HumVolume;

	internal static ManualLogSource Log;

	private static readonly FieldRef<EffectFade, float> BaseVolume = AccessTools.FieldRefAccess<EffectFade, float>("m_baseVolume");

	private static readonly FieldRef<EffectFade, AudioSource> FadeSource = AccessTools.FieldRefAccess<EffectFade, AudioSource>("m_audioSource");

	private static bool updatingPortal;

	private static bool listed;

	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_008a: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		ActivationVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Volume", "ActivationVolume", 0f, new ConfigDescription("Volume of the sound a portal makes when it activates, from 0 (silent) to 1 (normal). 0 by default.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		HumVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Volume", "HumVolume", 0f, new ConfigDescription("Volume of the hum an active portal makes, from 0 (silent) to 1 (normal). 0 by default. Applies to portals that load after you change it.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		new Harmony("com.parallel53.shutupportal").PatchAll(typeof(ShutupPortalPlugin));
	}

	private static void Quieten(AudioSource source, float volume)
	{
		if ((Object)(object)source == (Object)null)
		{
			return;
		}
		if (volume <= 0f)
		{
			source.mute = true;
			return;
		}
		ZSFX component = ((Component)source).GetComponent<ZSFX>();
		if ((Object)(object)component != (Object)null)
		{
			component.SetVolumeModifier(component.GetVolumeModifier() * volume);
		}
		else
		{
			source.volume *= volume;
		}
	}

	[HarmonyPatch(typeof(TeleportWorld), "UpdatePortal")]
	[HarmonyPrefix]
	private static void BeforeUpdatePortal()
	{
		updatingPortal = true;
	}

	[HarmonyPatch(typeof(TeleportWorld), "UpdatePortal")]
	[HarmonyFinalizer]
	private static void AfterUpdatePortal()
	{
		updatingPortal = false;
	}

	[HarmonyPatch(typeof(EffectList), "Create")]
	[HarmonyPostfix]
	private static void AfterCreate(GameObject[] __result)
	{
		if (!updatingPortal || __result == null)
		{
			return;
		}
		float volume = Mathf.Clamp01(ActivationVolume.Value);
		foreach (GameObject val in __result)
		{
			if (!((Object)(object)val == (Object)null))
			{
				AudioSource[] componentsInChildren = val.GetComponentsInChildren<AudioSource>(true);
				for (int j = 0; j < componentsInChildren.Length; j++)
				{
					Quieten(componentsInChildren[j], volume);
				}
			}
		}
	}

	[HarmonyPatch(typeof(EffectFade), "Awake")]
	[HarmonyPostfix]
	private static void AfterFadeAwake(EffectFade __instance)
	{
		if (!((Object)(object)((Component)__instance).GetComponentInParent<TeleportWorld>() == (Object)null))
		{
			float num = Mathf.Clamp01(HumVolume.Value);
			BaseVolume.Invoke(__instance) *= num;
			if (num <= 0f && (Object)(object)FadeSource.Invoke(__instance) != (Object)null)
			{
				FadeSource.Invoke(__instance).mute = true;
			}
		}
	}

	[HarmonyPatch(typeof(TeleportWorld), "Awake")]
	[HarmonyPostfix]
	private static void AfterPortalAwake(TeleportWorld __instance)
	{
		float num = Mathf.Clamp01(HumVolume.Value);
		AudioSource[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<AudioSource>(true);
		AudioSource[] array = componentsInChildren;
		foreach (AudioSource val in array)
		{
			EffectFade componentInParent = ((Component)val).GetComponentInParent<EffectFade>();
			bool flag = (Object)(object)componentInParent != (Object)null && (Object)(object)((Component)componentInParent).GetComponentInChildren<AudioSource>() == (Object)(object)val;
			if (num <= 0f)
			{
				val.mute = true;
			}
			else if (!flag)
			{
				Quieten(val, num);
			}
		}
		if (!listed)
		{
			listed = true;
			array = componentsInChildren;
			foreach (AudioSource val2 in array)
			{
				Log.LogInfo((object)("Portal sound: " + ((Object)val2).name + " (" + (((Object)(object)val2.clip != (Object)null) ? ((Object)val2.clip).name : "no clip") + ")"));
			}
		}
	}
}