Decompiled source of QuieterPumpAJump v1.0.0

QuieterPumpAJump.dll

Decompiled 38 minutes 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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("QuieterPumpAJump")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("QuieterPumpAJump")]
[assembly: AssemblyTitle("QuieterPumpAJump")]
[assembly: AssemblyVersion("1.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 QuieterPumpAJump
{
	[BepInPlugin("quieterpumpajump", "QuieterPumpAJump", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "quieterpumpajump";

		public const string PluginName = "QuieterPumpAJump";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		private ConfigEntry<float> _volume;

		private void Awake()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Expected O, but got Unknown
			//IL_0084: 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_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_volume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Volume", 0.3f, new ConfigDescription("Volume of the PumpAJump charge sound, as a fraction of its normal volume. 0 = silent, 1 = unchanged.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			SoundVolume.SetMultiplier(_volume.Value);
			_volume.SettingChanged += delegate
			{
				SoundVolume.SetMultiplier(_volume.Value);
			};
			PatchAudioSource(new Harmony("quieterpumpajump"));
			GameObject val = new GameObject("QuieterPumpAJump_Runner")
			{
				hideFlags = (HideFlags)61
			};
			Object.DontDestroyOnLoad((Object)val);
			val.AddComponent<SoundVolumeRunner>();
			Log.LogInfo((object)string.Format("{0} {1} loaded. Charge sound at {2:0.##}x", "QuieterPumpAJump", "1.0.0", _volume.Value));
		}

		private static void PatchAudioSource(Harmony harmony)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			HarmonyMethod prefix = new HarmonyMethod(typeof(AudioSourcePatches), "PlayPrefix", (Type[])null);
			HarmonyMethod prefix2 = new HarmonyMethod(typeof(AudioSourcePatches), "PlayOneShotPrefix", (Type[])null);
			TryPatch(harmony, AccessTools.Method(typeof(AudioSource), "Play", Type.EmptyTypes, (Type[])null), prefix);
			TryPatch(harmony, AccessTools.Method(typeof(AudioSource), "Play", new Type[1] { typeof(ulong) }, (Type[])null), prefix);
			TryPatch(harmony, AccessTools.Method(typeof(AudioSource), "PlayDelayed", new Type[1] { typeof(float) }, (Type[])null), prefix);
			TryPatch(harmony, AccessTools.Method(typeof(AudioSource), "PlayScheduled", new Type[1] { typeof(double) }, (Type[])null), prefix);
			TryPatch(harmony, AccessTools.Method(typeof(AudioSource), "PlayOneShot", new Type[2]
			{
				typeof(AudioClip),
				typeof(float)
			}, (Type[])null), prefix2);
		}

		private static void TryPatch(Harmony harmony, MethodInfo target, HarmonyMethod prefix)
		{
			if (target == null)
			{
				return;
			}
			try
			{
				harmony.Patch((MethodBase)target, prefix, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)$"Could not patch {target}: {ex.Message}");
			}
		}
	}
	internal static class AudioSourcePatches
	{
		public static void PlayPrefix(AudioSource __instance)
		{
			SoundVolume.Track(__instance);
		}

		public static void PlayOneShotPrefix(AudioSource __instance, AudioClip __0, ref float __1)
		{
			if (SoundVolume.IsTarget(__0) && !SoundVolume.IsTracked(__instance))
			{
				__1 *= SoundVolume.Multiplier;
			}
		}
	}
	internal static class SoundVolume
	{
		private sealed class Tracked
		{
			public float BaseVolume;

			public float Applied;
		}

		private const string TargetClip = "item_trinket_jump_charge";

		private static readonly Dictionary<AudioSource, Tracked> TrackedSources = new Dictionary<AudioSource, Tracked>();

		private static readonly List<AudioSource> ToRemove = new List<AudioSource>();

		internal static float Multiplier = 0.3f;

		internal static bool IsTarget(AudioClip clip)
		{
			if ((Object)(object)clip != (Object)null)
			{
				return string.Equals(((Object)clip).name, "item_trinket_jump_charge", StringComparison.OrdinalIgnoreCase);
			}
			return false;
		}

		internal static bool IsTracked(AudioSource src)
		{
			if ((Object)(object)src != (Object)null)
			{
				return TrackedSources.ContainsKey(src);
			}
			return false;
		}

		internal static void SetMultiplier(float value)
		{
			Multiplier = Mathf.Clamp01(value);
			foreach (KeyValuePair<AudioSource, Tracked> trackedSource in TrackedSources)
			{
				AudioSource key = trackedSource.Key;
				Tracked value2 = trackedSource.Value;
				if (!((Object)(object)key == (Object)null) && Mathf.Approximately(key.volume, value2.Applied))
				{
					value2.Applied = value2.BaseVolume * Multiplier;
					key.volume = value2.Applied;
				}
			}
		}

		internal static void Track(AudioSource src)
		{
			if ((Object)(object)src == (Object)null)
			{
				return;
			}
			Tracked value2;
			if (!IsTarget(src.clip))
			{
				if (TrackedSources.TryGetValue(src, out var value))
				{
					if (Mathf.Approximately(src.volume, value.Applied))
					{
						src.volume = value.BaseVolume;
					}
					TrackedSources.Remove(src);
				}
			}
			else if (TrackedSources.TryGetValue(src, out value2))
			{
				Reapply(src, value2);
			}
			else
			{
				value2 = new Tracked
				{
					BaseVolume = src.volume
				};
				value2.Applied = value2.BaseVolume * Multiplier;
				src.volume = value2.Applied;
				TrackedSources[src] = value2;
			}
		}

		private static void Reapply(AudioSource src, Tracked t)
		{
			if (!Mathf.Approximately(src.volume, t.Applied))
			{
				t.BaseVolume = src.volume;
				t.Applied = t.BaseVolume * Multiplier;
				src.volume = t.Applied;
			}
		}

		internal static void LateUpdate()
		{
			if (TrackedSources.Count == 0)
			{
				return;
			}
			ToRemove.Clear();
			foreach (KeyValuePair<AudioSource, Tracked> trackedSource in TrackedSources)
			{
				AudioSource key = trackedSource.Key;
				Tracked value = trackedSource.Value;
				if ((Object)(object)key == (Object)null)
				{
					ToRemove.Add(key);
				}
				else if (!IsTarget(key.clip))
				{
					if (Mathf.Approximately(key.volume, value.Applied))
					{
						key.volume = value.BaseVolume;
					}
					ToRemove.Add(key);
				}
				else
				{
					Reapply(key, value);
				}
			}
			foreach (AudioSource item in ToRemove)
			{
				TrackedSources.Remove(item);
			}
		}
	}
	internal sealed class SoundVolumeRunner : MonoBehaviour
	{
		private void LateUpdate()
		{
			SoundVolume.LateUpdate();
		}
	}
}