Decompiled source of LoadAllLobby v1.0.5

BepInEx/plugins/LoadAllLobby.dll

Decompiled 4 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Steamworks;
using Steamworks.Data;
using TMPro;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LoadAllLobby")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LoadAllLobby")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a7c74411-41ca-42e6-a8ec-96aaaecf50f4")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LoadAllLobby;

[BepInPlugin("LoadAllLobby", "LoadAllLobby", "1.0.5")]
public class LoadAllLobbyPlugin : BaseUnityPlugin
{
	public const string Version = "1.0.5";

	public static ConfigEntry<string> Versions { get; set; }

	private void Awake()
	{
		Versions = ((BaseUnityPlugin)this).Config.Bind<string>("LoadVersion", "Versions", "9999,[MoreCompany1.8]", "',' split version number and version name ';'split different versions");
		Harmony.CreateAndPatchAll(typeof(SteamLobbyPatch), (string)null);
	}
}
public static class SteamLobbyPatch
{
	private static readonly Dictionary<int, string> vers = new Dictionary<int, string>();

	private static int version = 60;

	private static Dictionary<ulong, int> lobbyVers = new Dictionary<ulong, int>();

	private static int originVersion = 0;

	[HarmonyPatch(typeof(GameNetworkManager), "Awake")]
	[HarmonyPostfix]
	[HarmonyWrapSafe]
	public static void Awake()
	{
		version = GameNetworkManager.Instance.gameVersionNum;
		try
		{
			string[] array = LoadAllLobbyPlugin.Versions.Value.Split(new char[1] { ';' });
			foreach (string text in array)
			{
				string[] array2 = text.Split(new char[1] { ',' });
				vers.Add(int.Parse(array2[0]), array2[1]);
			}
		}
		catch (Exception ex)
		{
			Debug.LogError((object)ex);
		}
		vers.Add(GameNetworkManager.Instance.gameVersionNum, $"[v{GameNetworkManager.Instance.gameVersionNum}]");
		vers.Add(GameNetworkManager.Instance.gameVersionNum + 9950, $"[v{GameNetworkManager.Instance.gameVersionNum}|MoreCompany1.9]");
	}

	[HarmonyPatch(typeof(LobbySlot), "JoinLobbyAfterVerifying")]
	[HarmonyPrefix]
	[HarmonyWrapSafe]
	public static bool JoinLobbyAfterVerifying(SteamId lobbyId)
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		if (originVersion == 0)
		{
			originVersion = GameNetworkManager.Instance.gameVersionNum;
		}
		if (originVersion != 0)
		{
			GameNetworkManager.Instance.gameVersionNum = originVersion;
			((Component)GameNetworkManager.Instance).GetComponent<NetworkManager>().NetworkConfig.ProtocolVersion = (ushort)originVersion;
		}
		if (lobbyVers[lobbyId.Value] == version && GameNetworkManager.Instance.gameVersionNum == 9950 + version)
		{
			GameNetworkManager.Instance.gameVersionNum = version;
			((Component)GameNetworkManager.Instance).GetComponent<NetworkManager>().NetworkConfig.ProtocolVersion = (ushort)version;
			Debug.Log((object)("Set Version -> " + version));
		}
		else if (lobbyVers[lobbyId.Value] == 9999)
		{
			GameNetworkManager.Instance.gameVersionNum = 9999;
			((Component)GameNetworkManager.Instance).GetComponent<NetworkManager>().NetworkConfig.ProtocolVersion = 9999;
			Debug.Log((object)("Set Version -> " + 9999));
		}
		return true;
	}

	[HarmonyPatch(typeof(SteamLobbyManager), "LoadServerList")]
	[HarmonyPrefix]
	[HarmonyWrapSafe]
	public static bool LoadServerList(SteamLobbyManager __instance)
	{
		LoadAllLobby(__instance);
		return false;
	}

	public static async void LoadAllLobby(SteamLobbyManager __instance)
	{
		__instance.censorOffensiveLobbyNames = false;
		if (originVersion != 0)
		{
			GameNetworkManager.Instance.gameVersionNum = originVersion;
		}
		Debug.Log((object)GameNetworkManager.Instance.gameVersionNum);
		lobbyVers = new Dictionary<ulong, int>();
		__instance.SetField<SteamLobbyManager>("refreshServerListTimer", 0f);
		((TMP_Text)__instance.serverListBlankText).text = "Loading server list...";
		__instance.SetField<SteamLobbyManager>("currentLobbyList", null);
		LobbySlot[] array = Object.FindObjectsOfType<LobbySlot>();
		for (int i = 0; i < array.Length; i++)
		{
			Object.Destroy((Object)(object)((Component)array[i]).gameObject);
		}
		List<Lobby> lobbies = new List<Lobby>();
		foreach (KeyValuePair<int, string> item in vers.OrderBy((KeyValuePair<int, string> x) => (GameNetworkManager.Instance.gameVersionNum != x.Key) ? x.Key : 0))
		{
			((TMP_Text)__instance.serverListBlankText).text = "Loading " + item.Value + " server list...";
			int sortByDistanceSetting = __instance.sortByDistanceSetting;
			LobbyQuery lobbyQuery = default(LobbyQuery);
			LobbyQuery val;
			switch (sortByDistanceSetting)
			{
			case 0:
				val = SteamMatchmaking.LobbyList;
				((LobbyQuery)(ref val)).FilterDistanceClose();
				val = SteamMatchmaking.LobbyList;
				val = ((LobbyQuery)(ref val)).FilterDistanceClose();
				val = ((LobbyQuery)(ref val)).WithSlotsAvailable(1);
				lobbyQuery = ((LobbyQuery)(ref val)).WithKeyValue("vers", item.Key.ToString());
				break;
			case 1:
				val = SteamMatchmaking.LobbyList;
				((LobbyQuery)(ref val)).FilterDistanceFar();
				val = SteamMatchmaking.LobbyList;
				val = ((LobbyQuery)(ref val)).FilterDistanceFar();
				val = ((LobbyQuery)(ref val)).WithSlotsAvailable(1);
				lobbyQuery = ((LobbyQuery)(ref val)).WithKeyValue("vers", item.Key.ToString());
				break;
			default:
				val = SteamMatchmaking.LobbyList;
				((LobbyQuery)(ref val)).FilterDistanceWorldwide();
				val = SteamMatchmaking.LobbyList;
				val = ((LobbyQuery)(ref val)).FilterDistanceWorldwide();
				val = ((LobbyQuery)(ref val)).WithSlotsAvailable(1);
				lobbyQuery = ((LobbyQuery)(ref val)).WithKeyValue("vers", item.Key.ToString());
				break;
			}
			if (!((Behaviour)__instance.sortWithChallengeMoonsCheckbox).enabled)
			{
				lobbyQuery = ((LobbyQuery)(ref lobbyQuery)).WithKeyValue("chal", "f");
			}
			if (__instance.serverTagInputField.text != string.Empty)
			{
				lobbyQuery = ((LobbyQuery)(ref lobbyQuery)).WithKeyValue("tag", __instance.serverTagInputField.text.Substring(0, Mathf.Min(19, __instance.serverTagInputField.text.Length)).ToLower());
			}
			Lobby[] lobby = await ((LobbyQuery)(ref lobbyQuery)).RequestAsync();
			if (lobby != null)
			{
				Lobby[] array2 = lobby;
				for (int j = 0; j < array2.Length; j++)
				{
					Lobby item2 = array2[j];
					((Lobby)(ref item2)).SetData("name", item.Value + ((Lobby)(ref item2)).GetData("name"));
					if (!lobbyVers.ContainsKey(((Lobby)(ref item2)).Id.Value))
					{
						lobbyVers.Add(((Lobby)(ref item2)).Id.Value, item.Key);
					}
					item2 = default(Lobby);
				}
				lobbies.AddRange(lobby.Where((Lobby x) => lobbyVers.ContainsKey(((Lobby)(ref x)).Id.Value)));
			}
			lobbyQuery = default(LobbyQuery);
		}
		__instance.SetField<SteamLobbyManager>("currentLobbyList", lobbies.ToArray());
		if (lobbies.Count == 0)
		{
			((TMP_Text)__instance.serverListBlankText).text = "No available servers to join.";
		}
		else
		{
			((TMP_Text)__instance.serverListBlankText).text = "";
		}
		__instance.SetField<SteamLobbyManager>("lobbySlotPositionOffset", 0f);
		__instance.SetField<SteamLobbyManager>("loadLobbyListCoroutine", ((MonoBehaviour)__instance).StartCoroutine((IEnumerator)typeof(SteamLobbyManager).GetMethod("loadLobbyListAndFilter", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(__instance, new object[1] { lobbies.ToArray() })));
		((MonoBehaviour)__instance).StartCoroutine(Fix(__instance));
	}

	public static IEnumerator Fix(SteamLobbyManager __instance)
	{
		for (Coroutine coroutine = __instance.GetField<SteamLobbyManager, Coroutine>("loadLobbyListCoroutine"); coroutine != null; coroutine = __instance.GetField<SteamLobbyManager, Coroutine>("loadLobbyListCoroutine"))
		{
			yield return (object)new WaitForSeconds(0.1f);
		}
		LobbySlot[] ls = Object.FindObjectsOfType<LobbySlot>();
		LobbySlot[] array = ls;
		foreach (LobbySlot item in array)
		{
			((TMP_Text)item.LobbyName).autoSizeTextContainer = true;
		}
	}

	public static T2 GetField<T, T2>(this T obj, string name)
	{
		return (T2)obj.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField).GetValue(obj);
	}

	public static void SetField<T>(this T obj, string name, object value)
	{
		obj.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField).SetValue(obj, value);
	}
}