Decompiled source of CrossplayJoinCodeFix v1.1.0

plugins/CrossplayJoinCodeFix.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.IO;
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 PlayFab.MultiplayerModels;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("CrossplayJoinCodeFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+b96e9d1110221158a3aef4febdbf67521191c3ee")]
[assembly: AssemblyProduct("CrossplayJoinCodeFix")]
[assembly: AssemblyTitle("CrossplayJoinCodeFix")]
[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 CrossplayJoinCodeFix
{
	[BepInPlugin("Tie.CrossplayJoinCodeFix", "CrossplayJoinCodeFix", "1.1.0")]
	public sealed class CrossplayJoinCodeFixPlugin : BaseUnityPlugin
	{
		public const string Guid = "Tie.CrossplayJoinCodeFix";

		private const string OldGuid = "local.crossplayjoincodefix";

		public const string Version = "1.1.0";

		internal static ConfigEntry<float> TimeoutSeconds;

		internal static ConfigEntry<float> RetrySeconds;

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected O, but got Unknown
			string text = Path.Combine(Paths.ConfigPath, "local.crossplayjoincodefix.cfg");
			if (File.Exists(text) && !File.Exists(((BaseUnityPlugin)this).Config.ConfigFilePath))
			{
				try
				{
					File.Move(text, ((BaseUnityPlugin)this).Config.ConfigFilePath);
					((BaseUnityPlugin)this).Config.Reload();
				}
				catch (IOException)
				{
				}
			}
			Log = ((BaseUnityPlugin)this).Logger;
			TimeoutSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("JoinCode", "TimeoutSeconds", 30f, "How long to keep re-checking an incomplete PlayFab answer before registering a new join code.");
			RetrySeconds = ((BaseUnityPlugin)this).Config.Bind<float>("JoinCode", "RetrySeconds", 2f, "Delay between two join-code checks while waiting.");
			_harmony = new Harmony("Tie.CrossplayJoinCodeFix");
			_harmony.PatchAll(typeof(CheckJoinCodePatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("CrossplayJoinCodeFix {0}: join-code check guarded (timeout {1:0} s, retry every {2:0.#} s)", "1.1.0", TimeoutSeconds.Value, RetrySeconds.Value));
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(ZPlayFabMatchmaking), "OnCheckJoinCodeSuccess")]
	internal static class CheckJoinCodePatch
	{
		private static readonly FieldInfo ServerData = AccessTools.Field(typeof(ZPlayFabMatchmaking), "m_serverData");

		private static readonly FieldInfo RetryIn = AccessTools.Field(typeof(ZPlayFabMatchmaking), "m_retryIn");

		private static readonly MethodInfo ActivateSession = AccessTools.Method(typeof(ZPlayFabMatchmaking), "ActivateSession", (Type[])null, (Type[])null);

		private static readonly MethodInfo OnSessionUpdated = AccessTools.Method(typeof(ZPlayFabMatchmaking), "OnSessionUpdated", (Type[])null, (Type[])null);

		private static readonly Type StateType = AccessTools.Inner(typeof(ZPlayFabMatchmaking), "State");

		private static float _waitingSince = -1f;

		private static bool Prefix(ZPlayFabMatchmaking __instance, FindLobbiesResult result)
		{
			try
			{
				PlayFabManager instance = PlayFabManager.instance;
				string text = ((instance == null) ? null : instance.Entity?.Id);
				if (result?.Lobbies != null && text != null && result.Lobbies.TrueForAll((LobbySummary l) => l != null && l.Owner?.Id != null))
				{
					if (_waitingSince >= 0f)
					{
						CrossplayJoinCodeFixPlugin.Log.LogInfo((object)$"PlayFab answered properly after {Time.realtimeSinceStartup - _waitingSince:0} s");
						_waitingSince = -1f;
					}
					return true;
				}
				object? value = ServerData.GetValue(__instance);
				string text2 = ((PlayFabMatchmakingServerData)(((value is PlayFabMatchmakingServerData) ? value : null)?)).lobbyId;
				if (result?.Lobbies != null && result.Lobbies.Count == 1 && text2 != null && result.Lobbies[0]?.LobbyId == text2)
				{
					Warn("PlayFab answer had no owner, but the lobby is this server's own: activating it");
					_waitingSince = -1f;
					ActivateSession.Invoke(__instance, null);
					return false;
				}
				if (_waitingSince < 0f)
				{
					_waitingSince = Time.realtimeSinceStartup;
				}
				float num = Time.realtimeSinceStartup - _waitingSince;
				string arg = ((result?.Lobbies == null) ? "no lobby list" : ((text == null) ? "no server identity yet" : "a lobby without owner"));
				if (num < CrossplayJoinCodeFixPlugin.TimeoutSeconds.Value)
				{
					Warn($"PlayFab join-code check came back incomplete ({arg}); checking again ({num:0}/{CrossplayJoinCodeFixPlugin.TimeoutSeconds.Value:0} s)");
					RetryIn.SetValue(__instance, Mathf.Max(0.5f, CrossplayJoinCodeFixPlugin.RetrySeconds.Value));
					return false;
				}
				CrossplayJoinCodeFixPlugin.Log.LogWarning((object)$"No usable PlayFab answer after {num:0} s; registering a new join code");
				_waitingSince = -1f;
				OnSessionUpdated.Invoke(__instance, new object[1] { Enum.Parse(StateType, "RegenerateJoinCode") });
				return false;
			}
			catch (Exception arg2)
			{
				CrossplayJoinCodeFixPlugin.Log.LogError((object)$"CrossplayJoinCodeFix: {arg2}");
				return true;
			}
		}

		private static void Warn(string message)
		{
			CrossplayJoinCodeFixPlugin.Log.LogWarning((object)message);
		}
	}
}