Decompiled source of GoonLagoon Boat Mute v1.0.0

plugins/GoonLagoon.BoatMute.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.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 GoonLagoon.BoatMute
{
	[BepInPlugin("goonlagoon.boatmute", "GoonLagoon Boat Mute", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		private void Awake()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			new Harmony("goonlagoon.boatmute").PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Boat mute active: all seven OdinShip boats. Client audio only.");
		}

		internal static bool BoatName(string name)
		{
			name = name.Replace("(Clone)", "").Trim();
			switch (name)
			{
			case "MercantShip":
			case "RowingCanoe":
			case "CargoShip":
			case "BigCargoShip":
			case "DoubleRowingCanoe":
			case "LittleBoat":
			case "WarShip":
				return true;
			default:
				return false;
			}
		}

		internal static bool Scoped(Transform t)
		{
			while ((Object)(object)t != (Object)null)
			{
				if ((Object)(object)((Component)t).GetComponent<Player>() != (Object)null)
				{
					return false;
				}
				if (BoatName(((Object)t).name) || (Object)(object)((Component)t).GetComponent<Silencer>() != (Object)null)
				{
					return true;
				}
				t = t.parent;
			}
			return false;
		}

		internal static void Quiet(AudioSource source)
		{
			if (!((Object)(object)source == (Object)null))
			{
				source.mute = true;
				source.volume = 0f;
				source.playOnAwake = false;
				if (source.isPlaying)
				{
					source.Stop();
				}
				((Behaviour)source).enabled = false;
			}
		}

		internal static void Attach(GameObject obj)
		{
			if ((Object)(object)obj != (Object)null && (Object)(object)obj.GetComponent<Silencer>() == (Object)null)
			{
				obj.AddComponent<Silencer>();
			}
		}
	}
	public sealed class Silencer : MonoBehaviour
	{
		private AudioSource[] sources = (AudioSource[])(object)new AudioSource[0];

		private float nextScan;

		private void Awake()
		{
			Refresh();
		}

		private void OnEnable()
		{
			Refresh();
		}

		private void Refresh()
		{
			List<AudioSource> list = new List<AudioSource>();
			AudioSource[] componentsInChildren = ((Component)this).GetComponentsInChildren<AudioSource>(true);
			foreach (AudioSource val in componentsInChildren)
			{
				if ((Object)(object)val != (Object)null && Plugin.Scoped(((Component)val).transform))
				{
					list.Add(val);
				}
			}
			sources = list.ToArray();
			componentsInChildren = sources;
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Plugin.Quiet(componentsInChildren[i]);
			}
			nextScan = Time.unscaledTime + 0.5f;
		}

		private void LateUpdate()
		{
			if (Time.unscaledTime >= nextScan)
			{
				Refresh();
			}
			AudioSource[] array = sources;
			for (int i = 0; i < array.Length; i++)
			{
				Plugin.Quiet(array[i]);
			}
		}
	}
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	internal static class Prefabs
	{
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		private static void Postfix(ZNetScene __instance)
		{
			foreach (GameObject prefab in __instance.m_prefabs)
			{
				if ((Object)(object)prefab != (Object)null && Plugin.BoatName(((Object)prefab).name))
				{
					AudioSource[] componentsInChildren = prefab.GetComponentsInChildren<AudioSource>(true);
					for (int i = 0; i < componentsInChildren.Length; i++)
					{
						Plugin.Quiet(componentsInChildren[i]);
					}
				}
			}
		}
	}
	[HarmonyPatch(typeof(Ship), "Awake")]
	internal static class Boats
	{
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		private static void Postfix(Ship __instance)
		{
			if (Plugin.BoatName(((Object)__instance).name))
			{
				Plugin.Attach(((Component)__instance).gameObject);
			}
		}
	}
	[HarmonyPatch(typeof(ShipEffects), "FadeSound")]
	internal static class Fader
	{
		private static bool Prefix(AudioSource source)
		{
			if ((Object)(object)source == (Object)null || !Plugin.Scoped(((Component)source).transform))
			{
				return true;
			}
			Plugin.Quiet(source);
			return false;
		}
	}
	[HarmonyPatch(typeof(ZSFX), "Play", new Type[] { })]
	internal static class SoundEffects
	{
		private static bool Prefix(ZSFX __instance)
		{
			if (!Plugin.Scoped(((Component)__instance).transform))
			{
				return true;
			}
			AudioSource[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<AudioSource>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Plugin.Quiet(componentsInChildren[i]);
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(EffectList), "Create", new Type[]
	{
		typeof(Vector3),
		typeof(Quaternion),
		typeof(Transform),
		typeof(float),
		typeof(int),
		typeof(ZDOID)
	})]
	internal static class Effects
	{
		[HarmonyPostfix]
		private static void Postfix(Transform baseParent, GameObject[] __result)
		{
			if (!((Object)(object)baseParent == (Object)null) && Plugin.Scoped(baseParent) && __result != null)
			{
				for (int i = 0; i < __result.Length; i++)
				{
					Plugin.Attach(__result[i]);
				}
			}
		}
	}
}