Decompiled source of BetterPlayerAudio v1.1.1

korgen.BetterPlayerAudio.dll

Decompiled 2 years ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using BetterPlayerAudio.Hooks;
using LobbyCompatibility.Attributes;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using On;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("korgen.BetterPlayerAudio")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("BetterPlayerAudio")]
[assembly: AssemblyTitle("korgen.BetterPlayerAudio")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 BetterPlayerAudio
{
	[BepInPlugin("korgen.BetterPlayerAudio", "BetterPlayerAudio", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[LobbyCompatibility(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BetterPlayerAudio : BaseUnityPlugin
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_Update <0>__audioPatch;

			public static hook_AddUserToPlayerList <1>__patchJoin;
		}

		public static BetterPlayerAudio Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Hook();
			Logger.LogInfo((object)"korgen.BetterPlayerAudio v1.1.0 has loaded!");
		}

		internal static void Hook()
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_0057: 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_0062: Expected O, but got Unknown
			Logger.LogDebug((object)"Hooking...");
			if (MoreCompanyCompat.enabled)
			{
				MoreCompanyCompat.moreCompanyHook(patch: true);
			}
			else
			{
				object obj = <>O.<0>__audioPatch;
				if (obj == null)
				{
					hook_Update val = korgenAudioPatch.audioPatch;
					<>O.<0>__audioPatch = val;
					obj = (object)val;
				}
				QuickMenuManager.Update += (hook_Update)obj;
				object obj2 = <>O.<1>__patchJoin;
				if (obj2 == null)
				{
					hook_AddUserToPlayerList val2 = korgenAudioPatch.patchJoin;
					<>O.<1>__patchJoin = val2;
					obj2 = (object)val2;
				}
				QuickMenuManager.AddUserToPlayerList += (hook_AddUserToPlayerList)obj2;
			}
			Logger.LogDebug((object)"Finished Hooking!");
		}

		internal static void Unhook()
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_0057: 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_0062: Expected O, but got Unknown
			Logger.LogDebug((object)"Unhooking...");
			if (MoreCompanyCompat.enabled)
			{
				MoreCompanyCompat.moreCompanyHook(patch: false);
			}
			else
			{
				object obj = <>O.<0>__audioPatch;
				if (obj == null)
				{
					hook_Update val = korgenAudioPatch.audioPatch;
					<>O.<0>__audioPatch = val;
					obj = (object)val;
				}
				QuickMenuManager.Update -= (hook_Update)obj;
				object obj2 = <>O.<1>__patchJoin;
				if (obj2 == null)
				{
					hook_AddUserToPlayerList val2 = korgenAudioPatch.patchJoin;
					<>O.<1>__patchJoin = val2;
					obj2 = (object)val2;
				}
				QuickMenuManager.AddUserToPlayerList -= (hook_AddUserToPlayerList)obj2;
			}
			Logger.LogDebug((object)"Finished Unhooking!");
		}
	}
	public static class MoreCompanyCompat
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_Update <0>__audioPatch;

			public static hook_AddUserToPlayerList <1>__patchJoin;

			public static hook_OpenQuickMenu <2>__patchOpen;
		}

		private static bool? _enabled;

		public static bool enabled
		{
			get
			{
				if (!_enabled.HasValue)
				{
					_enabled = Chainloader.PluginInfos.ContainsKey("me.swipez.melonloader.morecompany");
				}
				return _enabled.Value;
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		public static void moreCompanyHook(bool patch)
		{
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Expected O, but got Unknown
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Expected O, but got Unknown
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Expected O, but got Unknown
			if (patch)
			{
				object obj = <>O.<0>__audioPatch;
				if (obj == null)
				{
					hook_Update val = moreCompanyPatch.audioPatch;
					<>O.<0>__audioPatch = val;
					obj = (object)val;
				}
				QuickMenuManager.Update += (hook_Update)obj;
				object obj2 = <>O.<1>__patchJoin;
				if (obj2 == null)
				{
					hook_AddUserToPlayerList val2 = moreCompanyPatch.patchJoin;
					<>O.<1>__patchJoin = val2;
					obj2 = (object)val2;
				}
				QuickMenuManager.AddUserToPlayerList += (hook_AddUserToPlayerList)obj2;
				object obj3 = <>O.<2>__patchOpen;
				if (obj3 == null)
				{
					hook_OpenQuickMenu val3 = moreCompanyPatch.patchOpen;
					<>O.<2>__patchOpen = val3;
					obj3 = (object)val3;
				}
				QuickMenuManager.OpenQuickMenu += (hook_OpenQuickMenu)obj3;
			}
			else
			{
				object obj4 = <>O.<0>__audioPatch;
				if (obj4 == null)
				{
					hook_Update val4 = moreCompanyPatch.audioPatch;
					<>O.<0>__audioPatch = val4;
					obj4 = (object)val4;
				}
				QuickMenuManager.Update -= (hook_Update)obj4;
				object obj5 = <>O.<1>__patchJoin;
				if (obj5 == null)
				{
					hook_AddUserToPlayerList val5 = moreCompanyPatch.patchJoin;
					<>O.<1>__patchJoin = val5;
					obj5 = (object)val5;
				}
				QuickMenuManager.AddUserToPlayerList -= (hook_AddUserToPlayerList)obj5;
				object obj6 = <>O.<2>__patchOpen;
				if (obj6 == null)
				{
					hook_OpenQuickMenu val6 = moreCompanyPatch.patchOpen;
					<>O.<2>__patchOpen = val6;
					obj6 = (object)val6;
				}
				QuickMenuManager.OpenQuickMenu -= (hook_OpenQuickMenu)obj6;
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "korgen.BetterPlayerAudio";

		public const string PLUGIN_NAME = "BetterPlayerAudio";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace BetterPlayerAudio.Hooks
{
	public class korgenAudioPatch
	{
		private static Dictionary<ulong, float> playerVolumes = new Dictionary<ulong, float>();

		public static void writeToConfig()
		{
			string contents = JsonConvert.SerializeObject((object)playerVolumes);
			File.WriteAllText("playerVolumes.json", contents);
		}

		public static void updateDictionary()
		{
			if (File.Exists("playerVolumes.json"))
			{
				playerVolumes = JsonConvert.DeserializeObject<Dictionary<ulong, float>>(File.ReadAllText("playerVolumes.json"));
			}
			else
			{
				writeToConfig();
			}
		}

		public static void audioPatch(orig_Update orig, QuickMenuManager self)
		{
			orig.Invoke(self);
			for (int i = 0; i < self.playerListSlots.Length; i++)
			{
				if (self.playerListSlots[i].isConnected)
				{
					string text = "[ " + (self.playerListSlots[i].volumeSlider.value - self.playerListSlots[i].volumeSlider.minValue) / (self.playerListSlots[i].volumeSlider.maxValue - self.playerListSlots[i].volumeSlider.minValue) * 100f + "% ]";
					((TMP_Text)((Component)self.playerListSlots[i].volumeSliderContainer.transform.GetChild(2)).GetComponent<TextMeshProUGUI>()).text = text;
					saveNewAudioValue(self.playerListSlots[i].playerSteamId, (self.playerListSlots[i].volumeSlider.value - self.playerListSlots[i].volumeSlider.minValue) / (self.playerListSlots[i].volumeSlider.maxValue - self.playerListSlots[i].volumeSlider.minValue));
				}
			}
		}

		public static void patchJoin(orig_AddUserToPlayerList orig, QuickMenuManager self, ulong steamId, string playerName, int playerObjectId)
		{
			orig.Invoke(self, steamId, playerName, playerObjectId);
			updateDictionary();
			if (playerVolumes.ContainsKey(steamId))
			{
				self.playerListSlots[playerObjectId].volumeSlider.value = playerVolumes[steamId] * (self.playerListSlots[playerObjectId].volumeSlider.maxValue - self.playerListSlots[playerObjectId].volumeSlider.minValue) + self.playerListSlots[playerObjectId].volumeSlider.minValue;
			}
		}

		public static void saveNewAudioValue(ulong steamId, float volume)
		{
			if (playerVolumes.ContainsKey(steamId))
			{
				if (playerVolumes[steamId] != volume)
				{
					playerVolumes[steamId] = volume;
					writeToConfig();
				}
			}
			else
			{
				playerVolumes.Add(steamId, volume);
				writeToConfig();
			}
		}
	}
	public class moreCompanyPatch
	{
		private static Dictionary<int, ulong> playerSteamIds = new Dictionary<int, ulong>();

		private static Dictionary<ulong, float> playerVolumes = new Dictionary<ulong, float>();

		public static void writeToConfig()
		{
			string contents = JsonConvert.SerializeObject((object)playerVolumes);
			File.WriteAllText("playerVolumes.json", contents);
		}

		public static void updateDictionary()
		{
			if (File.Exists("playerVolumes.json"))
			{
				playerVolumes = JsonConvert.DeserializeObject<Dictionary<ulong, float>>(File.ReadAllText("playerVolumes.json"));
			}
			else
			{
				writeToConfig();
			}
		}

		public static void audioPatch(orig_Update orig, QuickMenuManager self)
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			orig.Invoke(self);
			GameObject val = GameObject.Find("QuickmenuOverride(Clone)");
			Transform val2 = null;
			if ((Object)(object)val != (Object)null)
			{
				val2 = val.transform.Find("Holder");
			}
			if (!((Object)(object)val2 != (Object)null))
			{
				return;
			}
			List<GameObject> list = new List<GameObject>();
			foreach (Transform item in val2)
			{
				Transform val3 = item;
				if (!((Object)null == (Object)(object)val3))
				{
					list.Add(((Component)val3).gameObject);
				}
			}
			for (int i = 0; i < list.Count; i++)
			{
				Slider component = ((Component)list[i].transform.Find("PlayerVolumeSlider")).GetComponent<Slider>();
				TextMeshProUGUI component2 = ((Component)list[i].transform.Find("Text (1)")).GetComponent<TextMeshProUGUI>();
				string text = "[ " + (component.value - component.minValue) / (component.maxValue - component.minValue) * 100f + "% ]";
				((TMP_Text)component2).text = text;
				if (playerSteamIds.ContainsKey(i))
				{
					ulong steamId = playerSteamIds[i];
					saveNewAudioValue(steamId, (component.value - component.minValue) / (component.maxValue - component.minValue));
				}
			}
		}

		public static void patchJoin(orig_AddUserToPlayerList orig, QuickMenuManager self, ulong steamId, string playerName, int playerObjectId)
		{
			orig.Invoke(self, steamId, playerName, playerObjectId);
			updateDictionary();
			playerSteamIds[playerObjectId] = steamId;
			BetterPlayerAudio.Logger.LogInfo((object)("Fixed More Company Bug" + playerVolumes[0uL]));
			if (playerVolumes.ContainsKey(steamId))
			{
				BetterPlayerAudio.Logger.LogInfo((object)("Player volume updated! Old: " + SoundManager.Instance.playerVoiceVolumes[playerObjectId] + " New: " + playerVolumes[steamId] * 2f));
				SoundManager.Instance.playerVoiceVolumes[playerObjectId] = playerVolumes[steamId] * 2f;
			}
			else
			{
				BetterPlayerAudio.Logger.LogError((object)"SteamID not found");
				playerVolumes.Add(steamId, 10f);
				saveNewAudioValue(steamId, 0f);
			}
		}

		public static void patchOpen(orig_OpenQuickMenu orig, QuickMenuManager self)
		{
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			orig.Invoke(self);
			Thread.Sleep(150);
			GameObject val = GameObject.Find("QuickmenuOverride(Clone)");
			Transform val2 = null;
			if ((Object)(object)val != (Object)null)
			{
				val2 = val.transform.Find("Holder");
			}
			if (!((Object)(object)val2 != (Object)null))
			{
				return;
			}
			List<GameObject> list = new List<GameObject>();
			foreach (Transform item in val2)
			{
				Transform val3 = item;
				if (!((Object)null == (Object)(object)val3))
				{
					list.Add(((Component)val3).gameObject);
				}
			}
			for (int i = 0; i < list.Count; i++)
			{
				Slider component = ((Component)list[i].transform.Find("PlayerVolumeSlider")).GetComponent<Slider>();
				if (playerSteamIds.ContainsKey(i))
				{
					ulong key = playerSteamIds[i];
					if (playerVolumes.ContainsKey(key) && SoundManager.Instance.playerVoiceVolumes[i] != playerVolumes[key] * 2f)
					{
						BetterPlayerAudio.Logger.LogWarning((object)("Volume Mismatch! Old: " + SoundManager.Instance.playerVoiceVolumes[i] + " New: " + playerVolumes[key] * 2f));
						component.value = playerVolumes[key] * (component.maxValue - component.minValue) + component.minValue;
					}
				}
			}
		}

		public static void saveNewAudioValue(ulong steamId, float volume)
		{
			if (playerVolumes.ContainsKey(steamId))
			{
				if (playerVolumes[steamId] != volume)
				{
					playerVolumes[steamId] = volume;
					writeToConfig();
				}
			}
			else
			{
				playerVolumes.Add(steamId, volume);
				writeToConfig();
			}
		}
	}
}