Decompiled source of AudioMixerGroupFix v1.0.0

plugins/AudioMixerGroupFix.dll

Decompiled 2 hours ago
using System;
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;
using UnityEngine.Audio;

[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("AudioMixerGroupFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AudioMixerGroupFix")]
[assembly: AssemblyTitle("AudioMixerGroupFix")]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 AudioMixerGroupFix
{
	[BepInPlugin("AudioMixerGroupFix", "AudioMixerGroupFix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "AudioMixerGroupFix";

		public const string PluginName = "AudioMixerGroupFix";

		public const string PluginVersion = "1.0.0";

		private static ManualLogSource? _log;

		private static ConfigEntry<bool>? _enableFix;

		private static ConfigEntry<bool>? _logFixedObjects;

		private Harmony? _harmony;

		private void Awake()
		{
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			_log = ((BaseUnityPlugin)this).Logger;
			_enableFix = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableFix", true, "Automatically assign a vanilla AudioMixerGroup to custom level AudioSources that have none assigned.");
			_logFixedObjects = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LogFixedObjects", false, "Log each GameObject whose AudioSource received a fallback AudioMixerGroup at Debug level.");
			MethodInfo methodInfo = AccessTools.Method("REPOLib.Extensions.AudioSourceExtensions:FixAudioMixerGroup", new Type[2]
			{
				typeof(AudioSource),
				typeof(GameObject)
			}, (Type[])null);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Could not find REPOLib.Extensions.AudioSourceExtensions.FixAudioMixerGroup(AudioSource, GameObject).");
				return;
			}
			MethodInfo methodInfo2 = AccessTools.Method(typeof(Plugin), "FixAudioMixerGroupPrefix", (Type[])null, (Type[])null);
			_harmony = new Harmony("AudioMixerGroupFix");
			_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"AudioMixerGroupFix v1.0.0 loaded and patched REPOLib.Extensions.AudioSourceExtensions.FixAudioMixerGroup.");
		}

		private void OnDestroy()
		{
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private static bool FixAudioMixerGroupPrefix(AudioSource audioSource, GameObject rootObject)
		{
			ConfigEntry<bool> enableFix = _enableFix;
			if ((enableFix != null && !enableFix.Value) || (Object)(object)audioSource == (Object)null || (Object)(object)audioSource.outputAudioMixerGroup != (Object)null)
			{
				return true;
			}
			if ((Object)(object)AudioManager.instance == (Object)null)
			{
				return true;
			}
			AudioMixerGroup val = ResolveFallbackMixerGroup(audioSource);
			if ((Object)(object)val == (Object)null)
			{
				return true;
			}
			audioSource.outputAudioMixerGroup = val;
			enableFix = _logFixedObjects;
			if (enableFix != null && enableFix.Value && _log != null)
			{
				string text = (((Object)(object)rootObject == (Object)null || (Object)(object)rootObject == (Object)(object)((Component)audioSource).gameObject) ? ((Object)((Component)audioSource).gameObject).name : (((Object)rootObject).name + "/" + ((Object)((Component)audioSource).gameObject).name));
				_log.LogDebug((object)("Assigned fallback AudioMixerGroup \"" + ((Object)val).name + "\" to \"" + text + "\"."));
			}
			return true;
		}

		private static AudioMixerGroup? ResolveFallbackMixerGroup(AudioSource audioSource)
		{
			Traverse obj = Traverse.Create((object)AudioManager.instance);
			AudioMixerGroup value = obj.Field("SoundMasterGroup").GetValue<AudioMixerGroup>();
			AudioMixerGroup value2 = obj.Field("MusicMasterGroup").GetValue<AudioMixerGroup>();
			string text = ((Object)((Component)audioSource).gameObject).name ?? string.Empty;
			if (text.IndexOf("loop distance", StringComparison.OrdinalIgnoreCase) >= 0 || text.IndexOf("ambience", StringComparison.OrdinalIgnoreCase) >= 0 || text.IndexOf("ambient", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				AudioMixerGroup val = FindGroup((value != null) ? value.audioMixer : null, "Ambience");
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
			}
			else if (text.IndexOf("music", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				AudioMixerGroup val2 = FindGroup((value2 != null) ? value2.audioMixer : null, "Music") ?? value2;
				if ((Object)(object)val2 != (Object)null)
				{
					return val2;
				}
			}
			else
			{
				AudioMixerGroup val3 = FindGroup((value != null) ? value.audioMixer : null, "Sound Effects");
				if ((Object)(object)val3 != (Object)null)
				{
					return val3;
				}
			}
			return value;
		}

		private static AudioMixerGroup? FindGroup(AudioMixer? mixer, string groupName)
		{
			if ((Object)(object)mixer == (Object)null)
			{
				return null;
			}
			AudioMixerGroup[] array = mixer.FindMatchingGroups(groupName);
			if (array == null || array.Length == 0)
			{
				return null;
			}
			return array[0];
		}
	}
}