Decompiled source of TheSilentZox v1.0.3

BepInEx/plugins/TheSilentZox/TheSilentZox.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
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("TheSilentZox")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3")]
[assembly: AssemblyProduct("TheSilentZox")]
[assembly: AssemblyTitle("TheSilentZox")]
[assembly: AssemblyVersion("1.0.3.0")]
namespace TheSilentZox;

internal sealed class PortalConnections
{
	private readonly Dictionary<ZDOID, ZDOID> lastTargets = new Dictionary<ZDOID, ZDOID>();

	private readonly HashSet<(ZDOID, ZDOID)> announced = new HashSet<(ZDOID, ZDOID)>();

	public void Clear()
	{
		lastTargets.Clear();
		announced.Clear();
	}

	public bool Observe(ZDOID portal, ZDOID target, bool confirmed, bool loading)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		ZDOID value;
		bool flag = lastTargets.TryGetValue(portal, out value);
		if (flag && value == target)
		{
			return false;
		}
		if (flag && target != ZDOID.None && !confirmed)
		{
			return false;
		}
		if (flag && value != ZDOID.None)
		{
			announced.Remove((portal, value));
			announced.Remove((value, portal));
		}
		lastTargets[portal] = target;
		if (target == ZDOID.None || announced.Contains((target, portal)))
		{
			return false;
		}
		if ((!flag || loading) && lastTargets.TryGetValue(target, out var value2) && value2 == ZDOID.None)
		{
			return false;
		}
		if (announced.Add((portal, target)) && flag && confirmed)
		{
			return !loading;
		}
		return false;
	}
}
[BepInPlugin("com.rustyheathens.thesilentzox", "Silent Zox", "1.0.3")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(TeleportWorld), "Awake")]
	private static class PortalAwake
	{
		private static void Postfix(TeleportWorld __instance)
		{
			AudioSource[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<AudioSource>(true);
			foreach (AudioSource source in componentsInChildren)
			{
				instance.Mute(source);
			}
			instance.Observe(__instance, loading: true);
		}
	}

	[HarmonyPatch(typeof(TeleportWorld), "UpdatePortal")]
	private static class PortalUpdate
	{
		private static void Prefix(EffectList ___m_connected, out EffectList __state)
		{
			__state = instance.portalConnectionEffect;
			instance.portalConnectionEffect = ___m_connected;
		}

		private static void Postfix(TeleportWorld __instance)
		{
			instance.Observe(__instance, loading: false);
		}

		private static void Finalizer(EffectList __state)
		{
			instance.portalConnectionEffect = __state;
		}
	}

	[HarmonyPatch(typeof(TeleportWorld), "Teleport")]
	private static class StartTravel
	{
		private static void Prefix(Player player, out bool __state)
		{
			__state = Object.op_Implicit((Object)(object)player) && ((Character)player).IsTeleporting();
			instance.travelEffectDepth++;
		}

		private static void Postfix(Player player, bool __state)
		{
			if (!__state && Object.op_Implicit((Object)(object)player) && ((Character)player).IsTeleporting())
			{
				instance.travellingPlayer = player;
			}
		}

		private static void Finalizer()
		{
			instance.travelEffectDepth--;
		}
	}

	[HarmonyPatch(typeof(Player), "UpdateTeleport")]
	private static class FinishTravel
	{
		private static void Prefix(Player __instance, out bool __state)
		{
			__state = (Object)(object)instance.travellingPlayer == (Object)(object)__instance;
			if (__state)
			{
				instance.travelEffectDepth++;
			}
		}

		private static void Finalizer(Player __instance, bool __state)
		{
			if (__state)
			{
				instance.travelEffectDepth--;
				if (!((Character)__instance).IsTeleporting())
				{
					instance.travellingPlayer = null;
				}
			}
		}
	}

	private static class SpawnEffect
	{
		internal static MethodInfo FindCreate()
		{
			MethodInfo method = typeof(EffectList).GetMethod("Create", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[6]
			{
				typeof(Vector3),
				typeof(Quaternion),
				typeof(Transform),
				typeof(float),
				typeof(int),
				typeof(ZDOID)
			}, null);
			if (method?.ReturnType == typeof(GameObject[]))
			{
				return method;
			}
			method = typeof(EffectList).GetMethod("Create", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[5]
			{
				typeof(Vector3),
				typeof(Quaternion),
				typeof(Transform),
				typeof(float),
				typeof(int)
			}, null);
			if (!(method?.ReturnType == typeof(GameObject[])))
			{
				return null;
			}
			return method;
		}

		internal static void Postfix(EffectList __instance, GameObject[] __result)
		{
			if (__result == null)
			{
				return;
			}
			bool flag = instance.portalConnectionEffect == __instance || instance.travelEffectDepth > 0;
			foreach (GameObject val in __result)
			{
				if (Object.op_Implicit((Object)(object)val) && (flag || IsPortalSound(((Object)val).name)))
				{
					AudioSource[] componentsInChildren = val.GetComponentsInChildren<AudioSource>(true);
					foreach (AudioSource source in componentsInChildren)
					{
						instance.Mute(source);
					}
				}
			}
		}

		private static bool IsPortalSound(string name)
		{
			if (name.StartsWith("sfx_", StringComparison.OrdinalIgnoreCase))
			{
				if (name.IndexOf("portal", StringComparison.OrdinalIgnoreCase) < 0)
				{
					return name.IndexOf("teleport", StringComparison.OrdinalIgnoreCase) >= 0;
				}
				return true;
			}
			return false;
		}
	}

	public const string PluginGuid = "com.rustyheathens.thesilentzox";

	public const string PluginName = "Silent Zox";

	public const string PluginVersion = "1.0.3";

	private static Plugin instance;

	private readonly PortalConnections connections = new PortalConnections();

	private readonly Dictionary<AudioSource, bool> originalMute = new Dictionary<AudioSource, bool>();

	private readonly List<AudioSource> destroyedSources = new List<AudioSource>();

	private ZDOMan world;

	private ConfigEntry<float> chimeVolume;

	private AudioClip chime;

	private Harmony harmony;

	private EffectList portalConnectionEffect;

	private int travelEffectDepth;

	private Player travellingPlayer;

	private float nextCleanup;

	private void Awake()
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Expected O, but got Unknown
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Expected O, but got Unknown
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Expected O, but got Unknown
		if (Application.isBatchMode)
		{
			return;
		}
		chimeVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "ConnectionDingVolume", 1f, new ConfigDescription("Volume of the portal connection chime.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		chime = CreateDing();
		instance = this;
		harmony = new Harmony("com.rustyheathens.thesilentzox");
		try
		{
			harmony.PatchAll(typeof(Plugin).Assembly);
		}
		catch (Exception ex)
		{
			harmony.UnpatchSelf();
			instance = null;
			((BaseUnityPlugin)this).Logger.LogError((object)ex);
			return;
		}
		try
		{
			MethodInfo methodInfo = SpawnEffect.FindCreate();
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"No supported EffectList.Create signature was found. Spawned portal sounds may still play.");
			}
			else
			{
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(SpawnEffect), "Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Portal sound effect patch active (EffectList.Create, {methodInfo.GetParameters().Length} parameters).");
			}
		}
		catch (Exception arg)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)$"Spawned portal sounds could not be patched. Portal ambience muting and link chimes are active. {arg}");
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Silent Zox by RustyLaserGaming loaded.");
	}

	private void Update()
	{
		if (Time.unscaledTime < nextCleanup)
		{
			return;
		}
		nextCleanup = Time.unscaledTime + 15f;
		foreach (AudioSource key in originalMute.Keys)
		{
			if (!Object.op_Implicit((Object)(object)key))
			{
				destroyedSources.Add(key);
			}
		}
		foreach (AudioSource destroyedSource in destroyedSources)
		{
			originalMute.Remove(destroyedSource);
		}
		destroyedSources.Clear();
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
		if ((Object)(object)instance == (Object)(object)this)
		{
			instance = null;
		}
		foreach (KeyValuePair<AudioSource, bool> item in originalMute)
		{
			if (Object.op_Implicit((Object)(object)item.Key))
			{
				item.Key.mute = item.Value;
			}
		}
		if (Object.op_Implicit((Object)(object)chime))
		{
			Object.Destroy((Object)(object)chime);
		}
	}

	private void Mute(AudioSource source)
	{
		if (Object.op_Implicit((Object)(object)source))
		{
			if (!originalMute.ContainsKey(source))
			{
				originalMute.Add(source, source.mute);
			}
			source.mute = true;
		}
	}

	private void Observe(TeleportWorld portal, bool loading)
	{
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		ZDOMan val = ZDOMan.instance;
		if (val == null)
		{
			return;
		}
		if (world != val)
		{
			world = val;
			connections.Clear();
			travellingPlayer = null;
		}
		ZNetView component = ((Component)portal).GetComponent<ZNetView>();
		ZDO val2 = (Object.op_Implicit((Object)(object)component) ? component.GetZDO() : null);
		if (val2 != null && !(val2.m_uid == ZDOID.None))
		{
			ZDOID val3 = val2.GetConnectionZDOID((ConnectionType)1);
			if (val3 == val2.m_uid)
			{
				val3 = ZDOID.None;
			}
			bool confirmed = val3 != ZDOID.None && val.GetZDO(val3) != null;
			if (connections.Observe(val2.m_uid, val3, confirmed, loading))
			{
				PlayConnectionDing(portal);
			}
		}
	}

	private void PlayConnectionDing(TeleportWorld portal)
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Expected O, but got Unknown
		float value = chimeVolume.Value;
		if (Object.op_Implicit((Object)(object)chime) && !(value <= 0f))
		{
			GameObject val = new GameObject("TheSilentZox_ConnectionDing");
			val.transform.position = ((Component)portal).transform.position;
			AudioSource val2 = val.AddComponent<AudioSource>();
			val2.playOnAwake = false;
			val2.clip = chime;
			val2.volume = value;
			val2.spatialBlend = 1f;
			val2.minDistance = 8f;
			val2.maxDistance = 36f;
			val2.rolloffMode = (AudioRolloffMode)1;
			val2.dopplerLevel = 0f;
			AudioSource componentInChildren = ((Component)portal).GetComponentInChildren<AudioSource>(true);
			if (Object.op_Implicit((Object)(object)componentInChildren))
			{
				val2.outputAudioMixerGroup = componentInChildren.outputAudioMixerGroup;
			}
			val2.Play();
			Object.Destroy((Object)val, chime.length + 0.25f);
		}
	}

	private static AudioClip CreateDing()
	{
		int num = 18522;
		float[] array = new float[num];
		for (int i = 0; i < num; i++)
		{
			float num2 = (float)i / 44100f;
			float num3 = Mathf.Exp(-8.5f * num2) * Mathf.Clamp01(num2 * 90f) * Mathf.Clamp01((0.42f - num2) * 90f);
			float num4 = Mathf.Sin(MathF.PI * 1760f * num2);
			float num5 = 0.34f * Mathf.Sin(MathF.PI * 2640f * num2);
			float num6 = 0.12f * Mathf.Sin(MathF.PI * 3520f * num2);
			array[i] = (num4 + num5 + num6) * num3 * 0.64f;
		}
		AudioClip obj = AudioClip.Create("TheSilentZox_Ding", num, 1, 44100, false);
		obj.SetData(array, 0);
		return obj;
	}
}