Decompiled source of PeakVoiceFix v1.0.3

PeakVoiceFix.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using Photon.Voice;
using Photon.Voice.PUN;
using Photon.Voice.Unity;
using Steamworks;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("PhotonRealtime")]
[assembly: IgnoresAccessChecksTo("PhotonUnityNetworking")]
[assembly: IgnoresAccessChecksTo("PhotonVoice.API")]
[assembly: IgnoresAccessChecksTo("PhotonVoice")]
[assembly: IgnoresAccessChecksTo("PhotonVoice.PUN")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("PeakVoiceFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+e5c2fcd3f8f85ec89046949b470a607e7318b186")]
[assembly: AssemblyProduct("PeakVoiceFix")]
[assembly: AssemblyTitle("PeakVoiceFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
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;
		}
	}
}
namespace PeakVoiceFix
{
	[HarmonyPatch(typeof(Character))]
	[HarmonyWrapSafe]
	public class CharacterPatch
	{
		private static float lastAttemptTime;

		private const float RETRY_INTERVAL = 1f;

		private static float lastRetryTime;

		private static PlayerConnectionLog log;

		private static PunVoiceClient punVoice;

		private static ulong hasHostRegistered { get; set; }

		private static ulong hasClientAttempted { get; set; }

		public static string? GameServerAddress { get; set; }

		public static string? MasterServerAddress { get; set; }

		public static string? AppId { get; set; }

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		public static void HandleVoiceConnection()
		{
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Invalid comparison between Unknown and I4
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Invalid comparison between Unknown and I4
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Invalid comparison between Unknown and I4
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e3: Invalid comparison between Unknown and I4
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_027c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			if (Time.time - lastAttemptTime < 1f)
			{
				return;
			}
			lastAttemptTime = Time.time;
			if ((Object)(object)log == (Object)null)
			{
				log = Object.FindFirstObjectByType<PlayerConnectionLog>();
			}
			if ((Object)(object)punVoice == (Object)null)
			{
				GameObject val = GameObject.Find("VoiceClient");
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				punVoice = val.GetComponent<PunVoiceClient>();
				if ((Object)(object)punVoice == (Object)null || ((VoiceConnection)punVoice).Client == null)
				{
					return;
				}
			}
			if ((Object)(object)log != (Object)null && (Object)(object)punVoice != (Object)null)
			{
				if ((int)((LoadBalancingClient)((VoiceConnection)punVoice).Client).State == 14 && Time.time - lastRetryTime > 10f)
				{
					lastRetryTime = Time.time;
					bool flag = ((VoiceFollowClient)punVoice).ConnectVoice();
					VoiceFix.logger.LogInfo((object)$"try to ConnectVoice:{flag}");
				}
				if (log.currentLog.Count < 2)
				{
					Scene activeScene = SceneManager.GetActiveScene();
					if (((Scene)(ref activeScene)).name == "Airport" || (int)((LoadBalancingClient)((VoiceConnection)punVoice).Client).State != 9 || ((int)((LoadBalancingClient)((VoiceConnection)punVoice).Client).State == 9 && ((VoiceConnection)punVoice).linkedSpeakers.Count == 0 && Character.AllCharacters.Count != 1))
					{
						lock (log.currentLog)
						{
							if ((int)((LoadBalancingClient)((VoiceConnection)punVoice).Client).State == 14)
							{
								log.currentLog.Add($"<color=red>Voice State:{((LoadBalancingClient)((VoiceConnection)punVoice).Client).State}</color>");
							}
							else
							{
								log.currentLog.Add($"{log.GetColorTag(log.joinedColor)}Voice State:{((LoadBalancingClient)((VoiceConnection)punVoice).Client).State}</color>");
								log.currentLog.Add($"{log.GetColorTag(log.joinedColor)}People In Voice:{((VoiceConnection)punVoice).linkedSpeakers.Count + 1}</color>");
							}
							log.RebuildString();
							log.currentLog.Clear();
						}
					}
					else if (!log.currentLog.Any() && !string.IsNullOrWhiteSpace(((TMP_Text)log.text).text))
					{
						log.RebuildString();
					}
				}
				else if (log.currentLog.Count > 2)
				{
					return;
				}
			}
			if (PhotonNetwork.IsMasterClient)
			{
				HandleHostRegistration();
			}
			else
			{
				HandleClientConnection();
			}
		}

		private static void HandleHostRegistration()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			if (FixCheck(hasHostRegistered, out var lobbyId))
			{
				SteamMatchmaking.SetLobbyData(lobbyId, "GameServerAddress", ((LoadBalancingClient)((VoiceConnection)punVoice).Client).GameServerAddress);
				SteamMatchmaking.SetLobbyData(lobbyId, "MasterServerAddress", ((LoadBalancingClient)((VoiceConnection)punVoice).Client).MasterServerAddress);
				SteamMatchmaking.SetLobbyData(lobbyId, "CloudRegion", ((LoadBalancingClient)((VoiceConnection)punVoice).Client).CloudRegion);
				VoiceFix.logger.LogInfo((object)("Host set lobby data: " + ((LoadBalancingClient)((VoiceConnection)punVoice).Client).GameServerAddress + ", " + ((LoadBalancingClient)((VoiceConnection)punVoice).Client).MasterServerAddress + ", " + ((LoadBalancingClient)((VoiceConnection)punVoice).Client).CloudRegion));
				hasHostRegistered = lobbyId.m_SteamID;
			}
		}

		public static bool FixCheck(ulong checkValue, out CSteamID lobbyId)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			lobbyId = CSteamID.Nil;
			punVoice = null;
			if (!GameHandler.GetService<SteamLobbyHandler>().InSteamLobby(ref lobbyId))
			{
				return false;
			}
			if (lobbyId.m_SteamID == checkValue)
			{
				return false;
			}
			GameObject val = GameObject.Find("VoiceClient");
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			punVoice = val.GetComponent<PunVoiceClient>();
			if ((Object)(object)punVoice == (Object)null || ((VoiceConnection)punVoice).Client == null)
			{
				return false;
			}
			if (string.IsNullOrEmpty(((LoadBalancingClient)((VoiceConnection)punVoice).Client).GameServerAddress) || string.IsNullOrEmpty(((LoadBalancingClient)((VoiceConnection)punVoice).Client).MasterServerAddress))
			{
				return false;
			}
			return true;
		}

		private static void HandleClientConnection()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			if (!FixCheck(hasClientAttempted, out var lobbyId))
			{
				return;
			}
			string lobbyData = SteamMatchmaking.GetLobbyData(lobbyId, "GameServerAddress");
			string lobbyData2 = SteamMatchmaking.GetLobbyData(lobbyId, "MasterServerAddress");
			string lobbyData3 = SteamMatchmaking.GetLobbyData(lobbyId, "CloudRegion");
			if (string.IsNullOrEmpty(lobbyData) || string.IsNullOrEmpty(lobbyData2))
			{
				hasClientAttempted = lobbyId.m_SteamID;
				VoiceFix.logger.LogWarning((object)"host no install VoiceFix");
				return;
			}
			VoiceFix.logger.LogInfo((object)("Client got lobby data: " + lobbyData + ", " + lobbyData2 + ", " + lobbyData3));
			if (((LoadBalancingClient)((VoiceConnection)punVoice).Client).MasterServerAddress != lobbyData2 || ((LoadBalancingClient)((VoiceConnection)punVoice).Client).GameServerAddress != lobbyData)
			{
				MasterServerAddress = lobbyData2;
				GameServerAddress = lobbyData;
				((LoadBalancingClient)((VoiceConnection)punVoice).Client).MasterServerAddress = lobbyData2;
				((LoadBalancingClient)((VoiceConnection)punVoice).Client).GameServerAddress = lobbyData;
				((LoadBalancingClient)((VoiceConnection)punVoice).Client).CloudRegion = lobbyData3;
				AppId = ((LoadBalancingClient)((VoiceConnection)punVoice).Client).AppId;
				VoiceFix.logger.LogInfo((object)"Reconnecting voice client...");
				((LoadBalancingClient)((VoiceConnection)punVoice).Client).Disconnect();
				bool flag = ((LoadBalancingClient)((VoiceConnection)punVoice).Client).ReconnectAndRejoin();
				VoiceFix.logger.LogInfo((object)$"Reconnect result: {flag}");
			}
			else
			{
				VoiceFix.logger.LogInfo((object)"No need to reconnect voice client, addresses match.");
			}
			hasClientAttempted = lobbyId.m_SteamID;
		}
	}
	[HarmonyPatch(typeof(LoadBalancingClient))]
	[HarmonyWrapSafe]
	public class LoadBalancingClientPatch
	{
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPrefix]
		public static void MasterServerAddress(LoadBalancingClient __instance, ref string value)
		{
			VoiceFix.logger.LogInfo((object)("LoadBalancingClient(" + __instance.AppId + "): MasterServerAddress set to " + value));
			if (GameHandler.GetService<SteamLobbyHandler>().InSteamLobby() && !PhotonNetwork.IsMasterClient && !string.IsNullOrWhiteSpace(CharacterPatch.MasterServerAddress) && value != CharacterPatch.MasterServerAddress && __instance.AppId == CharacterPatch.AppId)
			{
				VoiceFix.logger.LogInfo((object)("Overriding MasterServerAddress to " + CharacterPatch.MasterServerAddress));
				value = CharacterPatch.MasterServerAddress;
			}
		}

		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPrefix]
		public static void GameServerAddress(LoadBalancingClient __instance, ref string value)
		{
			VoiceFix.logger.LogInfo((object)("LoadBalancingClient(" + __instance.AppId + "): GameServerAddress set to " + value));
			if (GameHandler.GetService<SteamLobbyHandler>().InSteamLobby() && !PhotonNetwork.IsMasterClient && !string.IsNullOrWhiteSpace(CharacterPatch.GameServerAddress) && value != CharacterPatch.GameServerAddress && __instance.AppId == CharacterPatch.AppId)
			{
				VoiceFix.logger.LogInfo((object)("Overriding GameServerAddress to " + CharacterPatch.GameServerAddress));
				value = CharacterPatch.GameServerAddress;
			}
		}
	}
	[BepInPlugin("chuxiaaaa.PeakVoiceFix", "PeakVoiceFix", "1.0.3")]
	public class VoiceFix : BaseUnityPlugin
	{
		public static ManualLogSource logger;

		private void Awake()
		{
			logger = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(typeof(CharacterPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(LoadBalancingClientPatch), (string)null);
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}