Decompiled source of Bigger Walk v0.4.1

Bigger_Walk.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using EpicTransport;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using Steamworks;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("Bigger_Walk")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+14ff76b95e9f5eed1fcc2be5b47bf7ffff97b1fb")]
[assembly: AssemblyProduct("Bigger Walk")]
[assembly: AssemblyTitle("Bigger_Walk")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 Bigger_Walk
{
	public static class EosLobbyManagerPatch
	{
		public static void FeedUpdateAttributesPrefix(object update)
		{
			MaxConnectionsPatch.ApplyToLobbyModification(update);
		}
	}
	public static class EosLobbyPatch
	{
		public static void CreateLobbyPrefix(ref uint maxConnections)
		{
			maxConnections = MaxConnectionsPatch.ConfiguredMaxConnectionsUInt;
		}
	}
	public static class EpicTransportServerPatch
	{
		public static void CreateServerPrefix(ref int maxConnections)
		{
			maxConnections = MaxConnectionsPatch.ConfiguredMaxConnections;
		}

		public static void CreateServerPostfix(object __result)
		{
			MaxConnectionsPatch.ApplyToEosServer(__result);
		}
	}
	public static class HouseNetworkManagerPatch
	{
		public static void OnStartServerPrefix(HouseNetworkManager __instance)
		{
			MaxConnectionsPatch.ApplyToNetworkManager((NetworkManager)(object)__instance);
		}

		public static void OnStartHostPrefix(HouseNetworkManager __instance)
		{
			MaxConnectionsPatch.ApplyToNetworkManager((NetworkManager)(object)__instance);
		}

		public static void CreateHostEosLobbyAsyncPrefix(HouseNetworkManager __instance)
		{
			MaxConnectionsPatch.ApplyToNetworkManager((NetworkManager)(object)__instance);
		}
	}
	public static class MaxConnectionsPatch
	{
		private const BindingFlags InstanceFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

		private static Type lobbyModificationSetMaxMembersOptionsType;

		private static MethodInfo setMaxMembersMethod;

		public static int ConfiguredMaxConnections => Math.Max(1, Plugin.MaxConnections.Value);

		public static uint ConfiguredMaxConnectionsUInt => (uint)ConfiguredMaxConnections;

		public static void ApplyToNetworkManager(NetworkManager networkManager)
		{
			if ((Object)(object)networkManager != (Object)null)
			{
				networkManager.maxConnections = ConfiguredMaxConnections;
			}
		}

		public static void ApplyToEosServer(object server)
		{
			try
			{
				PropertyInfo propertyInfo = server?.GetType().GetProperty("maxConnections", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if ((object)propertyInfo != null && propertyInfo.CanWrite && propertyInfo.PropertyType == typeof(int))
				{
					propertyInfo.SetValue(server, ConfiguredMaxConnections);
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Failed to set EOS server maxConnections " + ex));
			}
		}

		public static void ApplyToLobbyModification(object lobbyModification)
		{
			if (lobbyModification == null)
			{
				return;
			}
			try
			{
				Type type = lobbyModificationSetMaxMembersOptionsType ?? (lobbyModificationSetMaxMembersOptionsType = Plugin.ResolveKnownType("Epic.OnlineServices.Lobby.LobbyModificationSetMaxMembersOptions"));
				if (type == null)
				{
					Plugin.Log.LogWarning((object)"Failed to set EOS lobby MaxMembers: options type not found");
					return;
				}
				object obj = Activator.CreateInstance(type);
				SetNumericMember(obj, "MaxMembers", ConfiguredMaxConnections);
				MethodInfo methodInfo = setMaxMembersMethod ?? (setMaxMembersMethod = lobbyModification.GetType().GetMethod("SetMaxMembers", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
				if (methodInfo == null)
				{
					Plugin.Log.LogWarning((object)"Failed to set EOS lobby MaxMembers SetMaxMembers method not found");
					return;
				}
				methodInfo.Invoke(lobbyModification, new object[1] { obj });
			}
			catch (TargetInvocationException ex)
			{
				Plugin.Log.LogWarning((object)("Failed to set EOS lobby MaxMembers " + ((ex.InnerException == null) ? ex.ToString() : ex.InnerException.ToString())));
			}
			catch (Exception ex2)
			{
				Plugin.Log.LogWarning((object)("Failed to set EOS lobby MaxMembers " + ex2));
			}
		}

		private static void SetNumericMember(object target, string memberName, int value)
		{
			Type type = target.GetType();
			FieldInfo fieldInfo = type.GetField("_" + memberName + "_k__BackingField", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? type.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (fieldInfo != null)
			{
				fieldInfo.SetValue(target, ConvertNumber(value, fieldInfo.FieldType));
				return;
			}
			PropertyInfo property = type.GetProperty(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if ((object)property != null && property.CanWrite && property.GetIndexParameters().Length == 0)
			{
				property.SetValue(target, ConvertNumber(value, property.PropertyType));
			}
		}

		private static object ConvertNumber(int value, Type targetType)
		{
			if (targetType == typeof(uint))
			{
				return (uint)Math.Max(0, value);
			}
			if (targetType == typeof(ushort))
			{
				return (ushort)Math.Max(0, value);
			}
			if (targetType == typeof(byte))
			{
				return (byte)Math.Max(0, value);
			}
			if (targetType == typeof(long))
			{
				return (long)value;
			}
			if (targetType == typeof(ulong))
			{
				return (ulong)Math.Max(0, value);
			}
			return value;
		}
	}
	public static class NetworkManagerPatch
	{
		public static void StartServerPrefix(NetworkManager __instance)
		{
			MaxConnectionsPatch.ApplyToNetworkManager(__instance);
		}

		public static void StartHostPrefix(NetworkManager __instance)
		{
			MaxConnectionsPatch.ApplyToNetworkManager(__instance);
		}
	}
	public static class NetworkServerPatch
	{
		public static void ListenPrefix(ref int maxConns)
		{
			int maxConnections = (maxConns = MaxConnectionsPatch.ConfiguredMaxConnections);
			try
			{
				NetworkServer.maxConnections = maxConnections;
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Failed to set NetworkServer.maxConnections " + ex));
			}
		}
	}
	public static class SteamMatchmakingPatch
	{
		public static void CreateLobbyPrefix(ref int cMaxMembers)
		{
			cMaxMembers = MaxConnectionsPatch.ConfiguredMaxConnections;
		}

		public static void SetLobbyMemberLimitPrefix(ref int cMaxMembers)
		{
			cMaxMembers = MaxConnectionsPatch.ConfiguredMaxConnections;
		}
	}
	[BepInPlugin("Bigger_Walk", "Bigger Walk", "1.0.0")]
	public class Plugin : BasePlugin
	{
		internal static ManualLogSource Log;

		private Harmony harmony;

		internal static ConfigEntry<int> MaxConnections;

		public override void Load()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected O, but got Unknown
			Log = ((BasePlugin)this).Log;
			MaxConnections = ((BasePlugin)this).Config.Bind<int>("General", "MaxPlayerCount", 40, "Maximum number of players allowed in the lobby.");
			harmony = new Harmony("Bigger_Walk");
			HarmonyPatches();
			Log.LogInfo((object)"Bigger Walk Loaded");
		}

		private void HarmonyPatches()
		{
			TryPatch(AccessTools.Method(typeof(NetworkManager), "StartServer", Type.EmptyTypes, (Type[])null), AccessTools.Method(typeof(NetworkManagerPatch), "StartServerPrefix", (Type[])null, (Type[])null), null, "NetworkManager.StartServer");
			TryPatch(AccessTools.Method(typeof(NetworkManager), "StartHost", Type.EmptyTypes, (Type[])null), AccessTools.Method(typeof(NetworkManagerPatch), "StartHostPrefix", (Type[])null, (Type[])null), null, "NetworkManager.StartHost");
			TryPatch(AccessTools.Method(typeof(NetworkServer), "Listen", new Type[1] { typeof(int) }, (Type[])null), AccessTools.Method(typeof(NetworkServerPatch), "ListenPrefix", (Type[])null, (Type[])null), null, "NetworkServer.Listen");
			TryPatch(AccessTools.Method(typeof(HouseNetworkManager), "OnStartServer", Type.EmptyTypes, (Type[])null), AccessTools.Method(typeof(HouseNetworkManagerPatch), "OnStartServerPrefix", (Type[])null, (Type[])null), null, "HouseNetworkManager.OnStartServer");
			TryPatch(AccessTools.Method(typeof(HouseNetworkManager), "OnStartHost", Type.EmptyTypes, (Type[])null), AccessTools.Method(typeof(HouseNetworkManagerPatch), "OnStartHostPrefix", (Type[])null, (Type[])null), null, "HouseNetworkManager.OnStartHost");
			TryPatch(AccessTools.Method(typeof(HouseNetworkManager), "CreateHostEosLobbyAsync", Type.EmptyTypes, (Type[])null), AccessTools.Method(typeof(HouseNetworkManagerPatch), "CreateHostEosLobbyAsyncPrefix", (Type[])null, (Type[])null), null, "HouseNetworkManager.CreateHostEosLobbyAsync");
			TryPatch(AccessTools.Method(typeof(EOSLobby), "CreateLobby", (Type[])null, (Type[])null), AccessTools.Method(typeof(EosLobbyPatch), "CreateLobbyPrefix", (Type[])null, (Type[])null), null, "EOSLobby.CreateLobby");
			TryPatch(MethodByTypeName("EOSLobbyManager", "FeedUpdateAttributes"), AccessTools.Method(typeof(EosLobbyManagerPatch), "FeedUpdateAttributesPrefix", (Type[])null, (Type[])null), null, "EOSLobbyManager.FeedUpdateAttributes");
			TryPatch(AccessTools.Method(typeof(SteamMatchmaking), "CreateLobby", (Type[])null, (Type[])null), AccessTools.Method(typeof(SteamMatchmakingPatch), "CreateLobbyPrefix", (Type[])null, (Type[])null), null, "SteamMatchmaking.CreateLobby");
			TryPatch(AccessTools.Method(typeof(SteamMatchmaking), "SetLobbyMemberLimit", (Type[])null, (Type[])null), AccessTools.Method(typeof(SteamMatchmakingPatch), "SetLobbyMemberLimitPrefix", (Type[])null, (Type[])null), null, "SteamMatchmaking.SetLobbyMemberLimit");
			TryPatch(AccessTools.Method(typeof(Server), "CreateServer", (Type[])null, (Type[])null), AccessTools.Method(typeof(EpicTransportServerPatch), "CreateServerPrefix", (Type[])null, (Type[])null), AccessTools.Method(typeof(EpicTransportServerPatch), "CreateServerPostfix", (Type[])null, (Type[])null), "EpicTransport.Server.CreateServer");
		}

		private static MethodInfo MethodByTypeName(string typeName, string methodName)
		{
			Type type = ResolveKnownType(typeName);
			if (!(type == null))
			{
				return AccessTools.Method(type, methodName, (Type[])null, (Type[])null);
			}
			return null;
		}

		internal static Type ResolveKnownType(string typeName)
		{
			Type type = typeof(Server).Assembly.GetType(typeName, throwOnError: false, ignoreCase: false) ?? typeof(HouseNetworkManager).Assembly.GetType(typeName, throwOnError: false, ignoreCase: false) ?? typeof(NetworkManager).Assembly.GetType(typeName, throwOnError: false, ignoreCase: false) ?? Type.GetType(typeName, throwOnError: false, ignoreCase: false);
			if (type != null)
			{
				return type;
			}
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			for (int i = 0; i < assemblies.Length; i++)
			{
				type = assemblies[i].GetType(typeName, throwOnError: false, ignoreCase: false);
				if (type != null)
				{
					return type;
				}
			}
			return null;
		}

		private void TryPatch(MethodInfo original, MethodInfo prefix, MethodInfo postfix, string patchName)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			if (original == null)
			{
				Log.LogError((object)("Failed to find patch target: " + patchName));
				return;
			}
			try
			{
				harmony.Patch((MethodBase)original, (prefix == null) ? ((HarmonyMethod)null) : new HarmonyMethod(prefix), (postfix == null) ? ((HarmonyMethod)null) : new HarmonyMethod(postfix), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to apply the patch for " + patchName + ": " + ex));
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Bigger_Walk";

		public const string PLUGIN_NAME = "Bigger Walk";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}