Decompiled source of RealisticShipHorn v0.2.1

BepInEx/plugins/RealisticShipHorn.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[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 RealisticShipHorn
{
	[BepInPlugin("paul.realisticshiphorn", "Realistic Ship Horn", "0.2.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "paul.realisticshiphorn";

		public const string PluginName = "Realistic Ship Horn";

		public const string PluginVersion = "0.2.0";

		internal static ManualLogSource Log;

		private ConfigEntry<float> _minDistance;

		private ConfigEntry<float> _maxDistance;

		private ConfigEntry<float> _volume;

		private ConfigEntry<float> _farVolume;

		private ConfigEntry<bool> _force3D;

		private readonly HashSet<int> _configuredSources = new HashSet<int>();

		private void Awake()
		{
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_minDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "MinDistance", 4f, "Distance in metres before the horn begins to fade.");
			_maxDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "MaxDistance", 1220f, "Approximate maximum audible distance in metres.");
			_volume = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "Volume", 1f, "Horn volume multiplier (1 = unchanged). Keep this at or below 1 initially.");
			_farVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "FarVolume", 0.6f, new ConfigDescription("Fraction of the horn's volume at 75% of the fade distance.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_force3D = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio", "Force3D", true, "Convert the ship horn AudioSources to positional 3D audio.");
			SceneManager.sceneLoaded += OnSceneLoaded;
			((MonoBehaviour)this).StartCoroutine(FindHornSourcesLoop());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Realistic Ship Horn 0.2.0 loaded. Waiting for ship horn audio sources.");
		}

		private void OnDestroy()
		{
			SceneManager.sceneLoaded -= OnSceneLoaded;
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			_configuredSources.Clear();
		}

		private IEnumerator FindHornSourcesLoop()
		{
			WaitForSeconds wait = new WaitForSeconds(1f);
			while (true)
			{
				ConfigureHornSources();
				yield return wait;
			}
		}

		private void ConfigureHornSources()
		{
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Expected O, but got Unknown
			AudioSource[] array = Object.FindObjectsOfType<AudioSource>(true);
			foreach (AudioSource val in array)
			{
				if ((Object)(object)val == (Object)null || !IsShipHornSource(val))
				{
					continue;
				}
				int instanceID = ((Object)val).GetInstanceID();
				if (!_configuredSources.Contains(instanceID))
				{
					if (_force3D.Value)
					{
						val.spatialBlend = 1f;
					}
					val.minDistance = Mathf.Max(0.1f, _minDistance.Value);
					val.maxDistance = Mathf.Max(val.minDistance + 1f, _maxDistance.Value);
					float num = val.minDistance / val.maxDistance;
					float num2 = Mathf.Lerp(num, 1f, 0.75f);
					val.rolloffMode = (AudioRolloffMode)2;
					val.SetCustomCurve((AudioSourceCurveType)0, new AnimationCurve((Keyframe[])(object)new Keyframe[4]
					{
						new Keyframe(0f, 1f),
						new Keyframe(num, 1f),
						new Keyframe(num2, _farVolume.Value),
						new Keyframe(1f, 0f)
					}));
					val.dopplerLevel = 0f;
					val.volume = Mathf.Clamp01(_volume.Value);
					_configuredSources.Add(instanceID);
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Configured ship horn source: " + GetHierarchyPath(((Component)val).transform)));
				}
			}
		}

		private static bool IsShipHornSource(AudioSource source)
		{
			string name = ((Object)((Component)source).gameObject).name;
			if (!(name == "HornAudio"))
			{
				return name == "HornAudioDistant";
			}
			return true;
		}

		private static string GetHierarchyPath(Transform transform)
		{
			string text = ((Object)transform).name;
			while ((Object)(object)transform.parent != (Object)null)
			{
				transform = transform.parent;
				text = ((Object)transform).name + "/" + text;
			}
			return text;
		}
	}
}