Decompiled source of AnErrorOccuredLobbyCleanup v1.0.10

BepInEx/plugins/ZaboomafooW-AnErrorOccuredLobbyCleanup-v1.0.10.dll

Decompiled 5 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Steamworks;
using Steamworks.Data;
using UnityEngine;
using UnityEngine.SceneManagement;

[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("AnErrorOccuredLobbyCleanup")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.10.0")]
[assembly: AssemblyInformationalVersion("1.0.10")]
[assembly: AssemblyProduct("AnErrorOccuredLobbyCleanup")]
[assembly: AssemblyTitle("AnErrorOccuredLobbyCleanup")]
[assembly: AssemblyVersion("1.0.10.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 AnErrorOccuredLobbyCleanup
{
	[BepInPlugin("ZaboomafooW.AnErrorOccuredLobbyCleanup", "An Error Occured Lobby Cleanup", "1.0.10")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "ZaboomafooW.AnErrorOccuredLobbyCleanup";

		public const string PluginName = "An Error Occured Lobby Cleanup";

		public const string PluginVersion = "1.0.10";

		private static ManualLogSource? LogSource;

		private static ulong CachedLobbyId;

		private static ulong CachedHostSteamId;

		private bool _subscribed;

		private void Awake()
		{
			LogSource = ((BaseUnityPlugin)this).Logger;
			SteamMatchmaking.OnLobbyEntered += OnLobbyEntered;
			SteamMatchmaking.OnLobbyMemberLeave += OnLobbyMemberLeave;
			SteamMatchmaking.OnLobbyMemberDisconnected += OnLobbyMemberDisconnected;
			SteamMatchmaking.OnLobbyMemberKicked += OnLobbyMemberKicked;
			SteamMatchmaking.OnLobbyMemberBanned += OnLobbyMemberBanned;
			SceneManager.sceneLoaded += OnSceneLoaded;
			_subscribed = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[AnErrorOccuredLobbyCleanup] Loaded v1.0.10");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[AnErrorOccuredLobbyCleanup] Steam lobby host-departure hooks succeeded.");
		}

		private void OnApplicationQuit()
		{
			GameNetworkManager instance = GameNetworkManager.Instance;
			if ((Object)(object)instance != (Object)null && !instance.disableSteam && instance.currentLobby.HasValue)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"[AnErrorOccuredLobbyCleanup] Application quitting; exiting current Steam lobby.");
				instance.LeaveCurrentSteamLobby();
			}
			ClearHostCache();
			if (_subscribed)
			{
				SteamMatchmaking.OnLobbyEntered -= OnLobbyEntered;
				SteamMatchmaking.OnLobbyMemberLeave -= OnLobbyMemberLeave;
				SteamMatchmaking.OnLobbyMemberDisconnected -= OnLobbyMemberDisconnected;
				SteamMatchmaking.OnLobbyMemberKicked -= OnLobbyMemberKicked;
				SteamMatchmaking.OnLobbyMemberBanned -= OnLobbyMemberBanned;
				SceneManager.sceneLoaded -= OnSceneLoaded;
				_subscribed = false;
			}
			LogSource = null;
		}

		private static void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
		{
			if (((Scene)(ref scene)).name != "MainMenu")
			{
				return;
			}
			GameNetworkManager instance = GameNetworkManager.Instance;
			if ((Object)(object)instance != (Object)null && !instance.disableSteam && instance.currentLobby.HasValue)
			{
				ManualLogSource? logSource = LogSource;
				if (logSource != null)
				{
					logSource.LogWarning((object)"[AnErrorOccuredLobbyCleanup] MainMenu retained an orphaned Steam lobby; cleaning it up so other players don't get \"An error occured!\"");
				}
				instance.LeaveCurrentSteamLobby();
			}
			ClearHostCache();
		}

		private static void OnLobbyEntered(Lobby lobby)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: 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)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			GameNetworkManager instance = GameNetworkManager.Instance;
			if ((Object)(object)instance == (Object)null || instance.disableSteam || !instance.currentLobby.HasValue)
			{
				ClearHostCache();
				return;
			}
			ulong value = ((Lobby)(ref lobby)).Id.Value;
			Lobby value2 = instance.currentLobby.Value;
			if (((Lobby)(ref value2)).Id.Value != value)
			{
				return;
			}
			ClearHostCache();
			ulong value3 = ((Lobby)(ref lobby)).Owner.Id.Value;
			if (value != 0L && value3 != 0L)
			{
				CachedLobbyId = value;
				CachedHostSteamId = value3;
				ManualLogSource? logSource = LogSource;
				if (logSource != null)
				{
					logSource.LogDebug((object)$"[AnErrorOccuredLobbyCleanup] Cached Steam lobby {CachedLobbyId} entry owner {CachedHostSteamId} as provisional host fallback.");
				}
			}
		}

		private static void OnLobbyMemberLeave(Lobby lobby, Friend friend)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			HandleHostDeparture(lobby, friend, "left");
		}

		private static void OnLobbyMemberDisconnected(Lobby lobby, Friend friend)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			HandleHostDeparture(lobby, friend, "disconnected from");
		}

		private static void OnLobbyMemberKicked(Lobby lobby, Friend friend, Friend actor)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			HandleHostDeparture(lobby, friend, "was kicked from");
		}

		private static void OnLobbyMemberBanned(Lobby lobby, Friend friend, Friend actor)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			HandleHostDeparture(lobby, friend, "was banned from");
		}

		private static void HandleHostDeparture(Lobby lobby, Friend friend, string departureReason)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			ulong value = ((Lobby)(ref lobby)).Id.Value;
			if (friend.Id.Value == SteamClient.SteamId.Value)
			{
				HandleLocalLobbyRemoval(value, departureReason);
				return;
			}
			GameNetworkManager instance = GameNetworkManager.Instance;
			if ((Object)(object)instance == (Object)null || instance.disableSteam || instance.isHostingGame)
			{
				return;
			}
			if (!instance.currentLobby.HasValue)
			{
				ClearHostCache();
				return;
			}
			Lobby value2 = instance.currentLobby.Value;
			if (((Lobby)(ref value2)).Id.Value != value)
			{
				return;
			}
			if (CachedLobbyId != 0L && CachedLobbyId != ((Lobby)(ref value2)).Id.Value)
			{
				ClearHostCache();
			}
			ulong num = ResolveOriginalHostSteamId(value);
			if (num != 0L && friend.Id.Value == num)
			{
				ManualLogSource? logSource = LogSource;
				if (logSource != null)
				{
					logSource.LogWarning((object)$"[AnErrorOccuredLobbyCleanup] Actual Lethal Company host {num} {departureReason} Steam lobby {value}; cleaning up orphaned Steam lobby so other players don't get \"An error occured!\"");
				}
				QuarantineInheritedLobbyIfOwned(lobby, num);
				instance.LeaveCurrentSteamLobby();
				ClearHostCacheForLobby(value);
			}
		}

		private static void QuarantineInheritedLobbyIfOwned(Lobby lobby, ulong hostSteamId)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				ulong value = SteamClient.SteamId.Value;
				if (value == 0L || value == hostSteamId || ((Lobby)(ref lobby)).Owner.Id.Value != value)
				{
					return;
				}
				if (((Lobby)(ref lobby)).SetJoinable(false))
				{
					ManualLogSource? logSource = LogSource;
					if (logSource != null)
					{
						logSource.LogWarning((object)$"[AnErrorOccuredLobbyCleanup] This client inherited Steam lobby {((Lobby)(ref lobby)).Id.Value}; marked it non-joinable before cleanup.");
					}
				}
				else
				{
					ManualLogSource? logSource2 = LogSource;
					if (logSource2 != null)
					{
						logSource2.LogWarning((object)$"[AnErrorOccuredLobbyCleanup] This client inherited Steam lobby {((Lobby)(ref lobby)).Id.Value}, but Steam did not accept the non-joinable quarantine; cleanup will continue.");
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource? logSource3 = LogSource;
				if (logSource3 != null)
				{
					logSource3.LogWarning((object)$"[AnErrorOccuredLobbyCleanup] Failed to quarantine inherited Steam lobby {((Lobby)(ref lobby)).Id.Value}: {ex.Message}. Cleanup will continue.");
				}
			}
		}

		private static void HandleLocalLobbyRemoval(ulong lobbyId, string removalReason)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			GameNetworkManager instance = GameNetworkManager.Instance;
			if ((Object)(object)instance == (Object)null || instance.disableSteam || instance.isHostingGame)
			{
				ClearHostCacheForLobby(lobbyId);
				return;
			}
			if (!instance.currentLobby.HasValue)
			{
				ClearHostCacheForLobby(lobbyId);
				return;
			}
			Lobby value = instance.currentLobby.Value;
			if (((Lobby)(ref value)).Id.Value != lobbyId)
			{
				ClearHostCacheForLobby(lobbyId);
				return;
			}
			ManualLogSource? logSource = LogSource;
			if (logSource != null)
			{
				logSource.LogWarning((object)$"[AnErrorOccuredLobbyCleanup] Steam reports this client {removalReason} lobby {lobbyId}; clearing retained local lobby state.");
			}
			instance.SetCurrentLobbyNull();
			instance.steamIdsInLobby.Clear();
			ClearHostCacheForLobby(lobbyId);
		}

		private static ulong ResolveOriginalHostSteamId(ulong lobbyId)
		{
			ulong liveHostSteamId = GetLiveHostSteamId();
			if (liveHostSteamId != 0L)
			{
				if (CachedLobbyId != lobbyId || CachedHostSteamId != liveHostSteamId)
				{
					CachedLobbyId = lobbyId;
					CachedHostSteamId = liveHostSteamId;
					ManualLogSource? logSource = LogSource;
					if (logSource != null)
					{
						logSource.LogDebug((object)$"[AnErrorOccuredLobbyCleanup] Confirmed Lethal Company host {CachedHostSteamId} for Steam lobby {CachedLobbyId} from player slot 0.");
					}
				}
				return liveHostSteamId;
			}
			if (CachedLobbyId == lobbyId)
			{
				return CachedHostSteamId;
			}
			return 0uL;
		}

		private static ulong GetLiveHostSteamId()
		{
			StartOfRound instance = StartOfRound.Instance;
			if ((Object)(object)instance == (Object)null || instance.allPlayerScripts == null || instance.allPlayerScripts.Length == 0 || (Object)(object)instance.allPlayerScripts[0] == (Object)null)
			{
				return 0uL;
			}
			return instance.allPlayerScripts[0].playerSteamId;
		}

		private static void ClearHostCacheForLobby(ulong lobbyId)
		{
			if (CachedLobbyId == lobbyId)
			{
				ClearHostCache();
			}
		}

		private static void ClearHostCache()
		{
			CachedLobbyId = 0uL;
			CachedHostSteamId = 0uL;
		}
	}
}