Decompiled source of LobbyBrowser v1.1.0

BepInEx/plugins/LobbyBrowser.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
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.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.Mono;
using Extensions;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Steamworks;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
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("LobbyBrowser")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("Lobby Browser")]
[assembly: AssemblyTitle("LobbyBrowser")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace LobbyBrowser
{
	[BepInPlugin("LobbyBrowser", "Lobby Browser", "1.0.2")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(SteamMatchmaking), "SetLobbyType")]
		public static class SteamMatchmaking_SetLobbyType_Patch
		{
			private static void Prefix(ref ELobbyType eLobbyType)
			{
				//IL_008c: Unknown result type (might be due to invalid IL or missing references)
				//IL_007f: 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_0087: Unknown result type (might be due to invalid IL or missing references)
				//IL_0094: Unknown result type (might be due to invalid IL or missing references)
				//IL_0096: Expected I4, but got Unknown
				if (Object.op_Implicit((Object)(object)lobbyManager))
				{
					object? value = AccessTools.Field(((object)lobbyManager).GetType(), "lobbyModeSetting").GetValue(lobbyManager);
					int? num = ((DropdownSettingItem)(((value is DropdownSettingItem) ? value : null)?)).index;
					if (1 == 0)
					{
					}
					ELobbyType val = (ELobbyType)(num switch
					{
						0 => 1, 
						1 => 0, 
						2 => 2, 
						_ => eLobbyType, 
					});
					if (1 == 0)
					{
					}
					eLobbyType = (ELobbyType)(int)val;
					Debug.Log((object)$"LobbyType patch -> {eLobbyType}");
				}
			}
		}

		[HarmonyPatch(typeof(LobbyModeDropdownButton), "ApplyVisual")]
		public static class LobbyModeDropdownButton_ApplyVisual_Patch
		{
			private static bool Prefix(ref LobbyModeDropdownButton __instance, bool updateSetting)
			{
				FieldInfo fieldInfo = AccessTools.Field(((object)__instance).GetType(), "lobbyModeSetting");
				FieldInfo fieldInfo2 = AccessTools.Field(((object)__instance).GetType(), "settingsLayout");
				FieldInfo fieldInfo3 = AccessTools.Field(((object)__instance).GetType(), "friendsOnlySprite");
				FieldInfo fieldInfo4 = AccessTools.Field(((object)__instance).GetType(), "privateSprite");
				FieldInfo fieldInfo5 = AccessTools.Field(((object)__instance).GetType(), "targetImage");
				FieldInfo fieldInfo6 = AccessTools.Field(((object)__instance).GetType(), "_modeIndex");
				object? value = fieldInfo.GetValue(__instance);
				DropdownSettingItem val = (DropdownSettingItem)((value is DropdownSettingItem) ? value : null);
				object? value2 = fieldInfo2.GetValue(__instance);
				SettingsLayout val2 = (SettingsLayout)((value2 is SettingsLayout) ? value2 : null);
				object? value3 = fieldInfo3.GetValue(__instance);
				Sprite sprite = (Sprite)((value3 is Sprite) ? value3 : null);
				object? value4 = fieldInfo4.GetValue(__instance);
				Sprite sprite2 = (Sprite)((value4 is Sprite) ? value4 : null);
				object? value5 = fieldInfo5.GetValue(__instance);
				Image val3 = (Image)((value5 is Image) ? value5 : null);
				int num = (int)fieldInfo6.GetValue(__instance);
				if (!Object.op_Implicit((Object)(object)publicSprite))
				{
					publicSprite = val3.sprite;
				}
				string text = "";
				switch (num)
				{
				case 0:
					text = "Open To Friends";
					val3.sprite = sprite;
					break;
				case 1:
					text = "Invite Only";
					val3.sprite = sprite2;
					break;
				case 2:
					text = "Public";
					val3.sprite = publicSprite;
					break;
				}
				fieldInfo5.SetValue(__instance, val3);
				AccessTools.Method(((object)__instance).GetType(), "ShowModePanel", (Type[])null, (Type[])null).Invoke(__instance, new object[1] { text });
				if (!updateSetting || !Object.op_Implicit((Object)(object)val))
				{
					return false;
				}
				if (num < 0 || num >= val.options.Count)
				{
					return false;
				}
				val.index = num;
				fieldInfo.SetValue(__instance, val);
				if (Object.op_Implicit((Object)(object)val2))
				{
					val2.NotifyChanged((SettingItemBase)(object)val);
					fieldInfo2.SetValue(__instance, val2);
				}
				((SettingItemBase)val).NotifyChanged();
				return false;
			}
		}

		[HarmonyPatch(typeof(LobbyModeDropdownButton), "OnClick")]
		public static class LobbyModeDropdownButton_OnClick_Patch
		{
			private static bool Prefix(ref LobbyModeDropdownButton __instance)
			{
				FieldInfo fieldInfo = AccessTools.Field(((object)__instance).GetType(), "_modeIndex");
				int num = (int)fieldInfo.GetValue(__instance);
				fieldInfo.SetValue(__instance, (num + 1) % 3);
				num = (int)fieldInfo.GetValue(__instance);
				AccessTools.Method(((object)__instance).GetType(), "ApplyVisual", (Type[])null, (Type[])null).Invoke(__instance, new object[1] { true });
				Debug.Log((object)$"Patched mode index -> {num}");
				return false;
			}
		}

		[HarmonyPatch(typeof(LobbyModeDropdownButton), "InitFromSetting")]
		public static class LobbyModeDropdownButton_InitFromSetting_Patch
		{
			private static bool Prefix(ref LobbyModeDropdownButton __instance)
			{
				FieldInfo fieldInfo = AccessTools.Field(((object)__instance).GetType(), "lobbyModeSetting");
				FieldInfo fieldInfo2 = AccessTools.Field(((object)__instance).GetType(), "_modeIndex");
				object? value = fieldInfo.GetValue(__instance);
				DropdownSettingItem val = (DropdownSettingItem)((value is DropdownSettingItem) ? value : null);
				if (!Object.op_Implicit((Object)(object)val))
				{
					return true;
				}
				fieldInfo2.SetValue(__instance, Mathf.Clamp(val.index, 0, 2));
				return false;
			}
		}

		private static ManualLogSource Logger;

		private GameObject canvas;

		private Transform hostButton;

		private PublicLobbyListUI lobbyBrowser;

		private static LobbyManager lobbyManager = MonoSingleton<LobbyManager>.Instance;

		private static Sprite publicSprite;

		private Random random = new Random();

		private void Awake()
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected O, but got Unknown
			Harmony.CreateAndPatchAll(((object)this).GetType().Assembly, "Lobby Browser");
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			Logger = ((BaseUnityPlugin)this).Logger;
			ManualLogSource logger = Logger;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(7, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("LobbyBrowser");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" loaded");
			}
			logger.LogInfo(val);
			SceneManager.sceneLoaded += OnScene;
		}

		public void OnScene(Scene scene, LoadSceneMode mode)
		{
			//IL_023f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Expected O, but got Unknown
			//IL_0013: 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_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Expected O, but got Unknown
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Expected O, but got Unknown
			//IL_020d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0221: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Expected O, but got Unknown
			bool flag = default(bool);
			try
			{
				lobbyManager = MonoSingleton<LobbyManager>.Instance;
				AddDropdown();
				Scene activeScene = SceneManager.GetActiveScene();
				if (((Scene)(ref activeScene)).name != "MainMenuScene")
				{
					return;
				}
				activeScene = SceneManager.GetActiveScene();
				canvas = ((IEnumerable<GameObject>)((Scene)(ref activeScene)).GetRootGameObjects()).FirstOrDefault((Func<GameObject, bool>)((GameObject go) => ((Object)go).name.Contains("Canvas")));
				if (!Object.op_Implicit((Object)(object)canvas))
				{
					ManualLogSource logger = Logger;
					BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(19, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Canvas not found - ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<GameObject>(canvas);
					}
					logger.LogError(val);
					return;
				}
				GameObject obj = canvas;
				lobbyBrowser = ((obj != null) ? ((Component)obj.transform.Find("PublicLobbies")).GetComponent<PublicLobbyListUI>() : null);
				GameObject obj2 = canvas;
				hostButton = ((obj2 != null) ? obj2.transform.Find("UI").Find("Panel").Find("Navbar")
					.Find("Host Button") : null);
				if (!Object.op_Implicit((Object)(object)lobbyBrowser) || !Object.op_Implicit((Object)(object)hostButton))
				{
					ManualLogSource logger2 = Logger;
					BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(29, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Browser/Button not found - ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<PublicLobbyListUI>(lobbyBrowser);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Transform>(hostButton);
					}
					logger2.LogError(val);
				}
				else
				{
					Transform obj3 = hostButton;
					CreateButton("Browse Games", (obj3 != null) ? obj3.parent : null, Vector3.zero, (UnityAction[])(object)new UnityAction[1] { lobbyBrowser.Show });
					PublicLobbyListUI obj4 = lobbyBrowser;
					CreateButton("Let it ride.", (obj4 != null) ? ((Component)obj4).gameObject.transform : null, new Vector3(-95f, 310f, 0f), (UnityAction[])(object)new UnityAction[1] { LetItRide });
				}
			}
			catch (Exception ex)
			{
				ManualLogSource logger3 = Logger;
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(64, 0, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Exception - report it to the mod developer @resetdev on discord!");
				}
				logger3.LogError(val);
				Logger.LogError((object)ex);
			}
		}

		public void CreateButton(string buttonName, Transform parent, Vector3 position, UnityAction[] actions)
		{
			//IL_0023: 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_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			Transform val = Object.Instantiate<Transform>(hostButton, parent);
			((TMP_Text)((Component)val).GetComponentInChildren<TextMeshProUGUI>()).text = buttonName;
			((Object)val).name = buttonName;
			if (position != Vector3.zero)
			{
				((Component)val).transform.localPosition = position;
			}
			val.SetSiblingIndex(1);
			SFXGenericUIButtonComponent component = ((Component)val).GetComponent<SFXGenericUIButtonComponent>();
			Button component2 = ((Component)val).GetComponent<Button>();
			ButtonClickedEvent val2 = new ButtonClickedEvent();
			((UnityEventBase)val2).AddListener((object)component, ((object)component).GetType().GetMethod("PlayClick"));
			ThreadingExtensions.ForEachParallel<UnityAction>((IList<UnityAction>)actions, (Action<UnityAction>)((UnityEvent)val2).AddListener, -1);
			component2.onClick = val2;
		}

		public void LetItRide()
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			List<GameObject> list = ((object)lobbyBrowser).GetType().GetField("_entries", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(lobbyBrowser) as List<GameObject>;
			if (list == null || list.Count == 0)
			{
				if (list == null)
				{
					ManualLogSource logger = Logger;
					bool flag = default(bool);
					BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(21, 0, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Failed to get entries");
					}
					logger.LogError(val);
				}
			}
			else
			{
				list[random.Next(list.Count)].GetComponent<PublicLobbyListEntry>().OnClick();
			}
		}

		public void AddDropdown()
		{
			if (Object.op_Implicit((Object)(object)lobbyManager))
			{
				object? value = AccessTools.Field(((object)lobbyManager).GetType(), "lobbyModeSetting").GetValue(lobbyManager);
				DropdownSettingItem val = (DropdownSettingItem)((value is DropdownSettingItem) ? value : null);
				if (Object.op_Implicit((Object)(object)val) && val.options.Count == 2)
				{
					val.options.Add("Public");
				}
				Logger.LogDebug((object)"Added dropdown option");
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "LobbyBrowser";

		public const string PLUGIN_NAME = "Lobby Browser";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}