Decompiled source of PeakPhotonTransport v1.0.0

PeakPhotonTransport.dll

Decompiled a month 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 ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
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("Swerrio")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Swerrio")]
[assembly: AssemblyDescription("Forces Photon onto WSS/443 or TCP instead of UDP.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+1129f569e55964d3b29ca5375ab893ea5c99946a")]
[assembly: AssemblyProduct("PEAK Photon Transport")]
[assembly: AssemblyTitle("PeakPhotonTransport")]
[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 PeakPhotonTransport
{
	[HarmonyPatch(typeof(LoadBalancingClient), "ConnectUsingSettings")]
	internal static class ConnectUsingSettingsPatch
	{
		[HarmonyPrefix]
		[HarmonyPriority(0)]
		private static void Prefix(LoadBalancingClient __instance, AppSettings appSettings)
		{
			Transport.ApplyTo(appSettings, "ConnectUsingSettings");
			Transport.ForcePeer(__instance, "ConnectUsingSettings");
		}
	}
	[HarmonyPatch]
	internal static class ConnectPathPatches
	{
		[HarmonyTargetMethods]
		private static IEnumerable<MethodBase> TargetMethods()
		{
			Type t = typeof(LoadBalancingClient);
			yield return AccessTools.Method(t, "ConnectToNameServer", (Type[])null, (Type[])null);
			yield return AccessTools.Method(t, "ConnectToMasterServer", (Type[])null, (Type[])null);
			yield return AccessTools.Method(t, "ConnectToRegionMaster", new Type[1] { typeof(string) }, (Type[])null);
			yield return AccessTools.Method(t, "ReconnectToMaster", (Type[])null, (Type[])null);
			yield return AccessTools.Method(t, "ReconnectAndRejoin", (Type[])null, (Type[])null);
			yield return AccessTools.Method(t, "Connect", Type.EmptyTypes, (Type[])null);
		}

		[HarmonyPrefix]
		[HarmonyPriority(0)]
		private static void Prefix(LoadBalancingClient __instance, MethodBase __originalMethod)
		{
			Transport.ApplyToPunSettings(__originalMethod.Name);
			Transport.ForcePeer(__instance, __originalMethod.Name);
		}
	}
	[HarmonyPatch]
	internal static class ConnectDiagnosticsPatch
	{
		[HarmonyTargetMethod]
		private static MethodBase TargetMethod()
		{
			return AccessTools.Method(typeof(PhotonPeer), "Connect", new Type[5]
			{
				typeof(string),
				typeof(string),
				typeof(string),
				typeof(object),
				typeof(object)
			}, (Type[])null);
		}

		[HarmonyPostfix]
		private static void Postfix(PhotonPeer __instance, string __0, bool __result)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.Verbose != null && Plugin.Verbose.Value)
			{
				string text = ((__instance == null) ? "?" : ((object)__instance.TransportProtocol/*cast due to .constrained prefix*/).ToString());
				string text2 = ((__instance == null) ? null : __instance.SocketImplementation?.Name) ?? "?";
				Plugin.Log.LogInfo((object)$"[Photon] connect -> {__0} | transport={text} socket={text2} attempt-started={__result}");
			}
		}
	}
	public enum TransportMode
	{
		Udp,
		Tcp,
		WebSocketSecure
	}
	[BepInPlugin("io.swerr.peakphotontransport", "PEAK Photon Transport", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "io.swerr.peakphotontransport";

		public const string Name = "PEAK Photon Transport";

		public const string Version = "1.0.0";

		public const string Author = "Swerrio";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<TransportMode> Mode;

		internal static ConfigEntry<bool> AllowFallback;

		internal static ConfigEntry<bool> Verbose;

		private void Awake()
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch. Turn off to leave Photon completely untouched (vanilla UDP behaviour).");
			Mode = ((BaseUnityPlugin)this).Config.Bind<TransportMode>("Transport", "Protocol", TransportMode.WebSocketSecure, "Transport Photon should connect with.\nWebSocketSecure - TLS on port 443.\nTcp - plain TCP on 4530/4531/4533.\nUdp - Photon's default.");
			AllowFallback = ((BaseUnityPlugin)this).Config.Bind<bool>("Transport", "EnableProtocolFallback", true, "Let Photon switch to WebSocketSecure on its own if the chosen protocol fails to connect.\nPhoton's fallback only ever moves towards WebSocketSecure, never back to UDP, so this\ncannot undo the fix. It does nothing when Protocol is already WebSocketSecure.");
			Verbose = ((BaseUnityPlugin)this).Config.Bind<bool>("Diagnostics", "VerboseLogging", true, "Log every Photon server hop with the transport actually used.\nUseful when checking whether the fix took effect.");
			new Harmony("io.swerr.peakphotontransport").PatchAll(typeof(Plugin).Assembly);
			Log.LogInfo((object)(Enabled.Value ? ($"Forcing Photon transport to {Transport.Desired} " + "(protocol fallback " + (AllowFallback.Value ? "enabled" : "disabled") + ").") : "Disabled by config, Photon keeps its default transport."));
		}
	}
	internal static class Transport
	{
		internal static ConnectionProtocol Desired => (ConnectionProtocol)(Plugin.Mode.Value switch
		{
			TransportMode.Tcp => 1, 
			TransportMode.WebSocketSecure => 5, 
			_ => 0, 
		});

		internal static bool Active
		{
			get
			{
				if (Plugin.Enabled != null)
				{
					return Plugin.Enabled.Value;
				}
				return false;
			}
		}

		internal static void ApplyTo(AppSettings settings, string origin)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			if (Active && settings != null)
			{
				ConnectionProtocol desired = Desired;
				bool num = settings.Protocol != desired || settings.Port != 0;
				settings.Protocol = desired;
				settings.Port = 0;
				settings.EnableProtocolFallback = Plugin.AllowFallback.Value;
				if (num && Plugin.Verbose.Value)
				{
					Plugin.Log.LogInfo((object)$"[{origin}] AppSettings.Protocol -> {desired} (port reset to protocol default)");
				}
			}
		}

		internal static void ForcePeer(LoadBalancingClient client, string origin)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			if (!Active || client == null)
			{
				return;
			}
			LoadBalancingPeer loadBalancingPeer = client.LoadBalancingPeer;
			if (loadBalancingPeer == null || (int)((PhotonPeer)loadBalancingPeer).PeerState != 0)
			{
				return;
			}
			ConnectionProtocol desired = Desired;
			if (((PhotonPeer)loadBalancingPeer).TransportProtocol != desired)
			{
				((PhotonPeer)loadBalancingPeer).TransportProtocol = desired;
				if (Plugin.Verbose.Value)
				{
					Plugin.Log.LogInfo((object)$"[{origin}] peer.TransportProtocol -> {desired}");
				}
			}
			client.ExpectedProtocol = null;
			client.EnableProtocolFallback = Plugin.AllowFallback.Value;
		}

		internal static void ApplyToPunSettings(string origin)
		{
			if (!Active)
			{
				return;
			}
			try
			{
				ServerSettings photonServerSettings = PhotonNetwork.PhotonServerSettings;
				if ((Object)(object)photonServerSettings != (Object)null)
				{
					ApplyTo(photonServerSettings.AppSettings, origin);
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("[" + origin + "] could not reach PhotonNetwork.PhotonServerSettings: " + ex.Message));
			}
		}
	}
}