Decompiled source of TooManyPlayers v1.1.1

TooManyPlayers.dll

Decompiled 12 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using Bifrost.ConstEnum;
using FishySteamworks.Server;
using HarmonyLib;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using TooManyPlayers;
using TooManyPlayers.Config;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Core), "TooManyPlayers", "1.1.1", "DooDesch", null)]
[assembly: MelonGame("ReLUGames", "MIMESIS")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DooDesch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright © DooDesch")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1+4cf1043020118b898f6eaf2b442aeb0c5656ed81")]
[assembly: AssemblyProduct("TooManyPlayers")]
[assembly: AssemblyTitle("TooManyPlayers")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: AssemblyMetadata("AI_Assisted_Creation", "This assembly was partially created with the assistance of Generative AI.")]
[assembly: AssemblyMetadata("AI_Model_Vendor", "Anthropic/OpenAI")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.1.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 DooDesch
{
	internal static class ModVersion
	{
		internal const string Current = "1.1.1";
	}
}
namespace TooManyPlayers
{
	public sealed class Core : MelonMod
	{
		public override void OnInitializeMelon()
		{
			TooManyPlayersPreferences.Initialize();
			MelonLogger.Msg($"TooManyPlayers initialized. Max players: {TooManyPlayersPreferences.MaxPlayers}");
		}
	}
}
namespace TooManyPlayers.Patches
{
	internal static class DataConstsPatches
	{
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		internal static class ConstructorPatch
		{
			private static void Postfix(DataConsts __instance)
			{
				try
				{
					if (MaxPlayerCountField == null)
					{
						MelonLogger.Error("[TooManyPlayers] DataConsts.C_MaxPlayerCount field not found - cannot raise player cap.");
						return;
					}
					int maxPlayers = TooManyPlayersPreferences.MaxPlayers;
					int num = (int)MaxPlayerCountField.GetValue(__instance);
					MaxPlayerCountField.SetValue(__instance, maxPlayers);
					MelonLogger.Msg($"[TooManyPlayers] Raised C_MaxPlayerCount from {num} to {maxPlayers}.");
				}
				catch (Exception ex)
				{
					MelonLogger.Error("[TooManyPlayers] Failed to raise C_MaxPlayerCount: " + ex.Message);
				}
			}
		}

		private static readonly FieldInfo MaxPlayerCountField = typeof(DataConsts).GetField("C_MaxPlayerCount", BindingFlags.Instance | BindingFlags.Public);
	}
	internal static class GameSessionInfoPatches
	{
		[HarmonyPatch(typeof(GameSessionInfo), "AddPlayerSteamID")]
		internal static class AddPlayerSteamIDPatch
		{
			private static void Postfix(GameSessionInfo __instance, ulong steamID, bool isHost, bool __result)
			{
				if (!__result)
				{
					MelonLogger.Warning($"[TooManyPlayers] Failed to add player (SteamID: {steamID})");
				}
			}
		}
	}
	internal static class ServerSocketPatches
	{
		[HarmonyPatch(typeof(ServerSocket), "GetMaximumClients")]
		internal static class GetMaximumClientsPatch
		{
			private static bool Prefix(ref int __result)
			{
				__result = Math.Min(TooManyPlayersPreferences.MaxPlayers, 32766);
				return false;
			}
		}

		[HarmonyPatch(typeof(ServerSocket), "SetMaximumClients")]
		internal static class SetMaximumClientsPatch
		{
			private static bool Prefix(ref int value)
			{
				int num = Math.Min(TooManyPlayersPreferences.MaxPlayers, 32766);
				if (value != num)
				{
					value = num;
				}
				return true;
			}
		}

		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		internal static class ServerSocketConstructorPatch
		{
			private static void Postfix(ServerSocket __instance)
			{
				try
				{
					MethodInfo method = typeof(ServerSocket).GetMethod("SetMaximumClients", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (method == null)
					{
						MelonLogger.Error("[TooManyPlayers] ServerSocket.SetMaximumClients not found - the cap stays at the vanilla value.");
						return;
					}
					method.Invoke(__instance, new object[1] { TooManyPlayersPreferences.MaxPlayers });
				}
				catch (Exception ex)
				{
					MelonLogger.Error("[TooManyPlayers] Failed to raise the client cap on the server socket: " + ex.Message);
				}
			}
		}
	}
	internal static class SteamInviteDispatcherPatches
	{
		[HarmonyPatch(typeof(SteamInviteDispatcher), "CreateLobby")]
		internal static class CreateLobbyPatch
		{
			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				List<CodeInstruction> list = new List<CodeInstruction>(instructions);
				MethodInfo getMethod = typeof(TooManyPlayersPreferences).GetProperty("MaxPlayers", BindingFlags.Static | BindingFlags.NonPublic).GetGetMethod(nonPublic: true);
				bool flag = false;
				for (int i = 0; i + 1 < list.Count; i++)
				{
					if (list[i].opcode == OpCodes.Ldc_I4_4 && list[i + 1].opcode == OpCodes.Call && list[i + 1].operand is MethodInfo methodInfo && methodInfo.DeclaringType?.FullName == "Steamworks.SteamMatchmaking" && methodInfo.Name == "CreateLobby")
					{
						list[i].opcode = OpCodes.Call;
						list[i].operand = getMethod;
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					throw new InvalidOperationException("[TooManyPlayers] SteamInviteDispatcher.CreateLobby cap argument was not found.");
				}
				return list;
			}
		}
	}
}
namespace TooManyPlayers.Config
{
	internal static class TooManyPlayersPreferences
	{
		private const string CategoryId = "TooManyPlayers";

		private static MelonPreferences_Category _category;

		private static MelonPreferences_Entry<int> _maxPlayers;

		internal static int MaxPlayers => _maxPlayers.Value;

		internal static void Initialize()
		{
			if (_category == null)
			{
				_category = MelonPreferences.CreateCategory("TooManyPlayers", "TooManyPlayers");
				_maxPlayers = CreateEntry("MaxPlayers", 8, "Maximum Players", "The maximum number of players allowed in a session. Default: 8. Minimum: 4, Maximum: 32766.");
				if (_maxPlayers.Value < 4)
				{
					MelonLogger.Warning($"MaxPlayers value {_maxPlayers.Value} is below minimum of 4. Clamping to 4.");
					_maxPlayers.Value = 4;
				}
				else if (_maxPlayers.Value > 32766)
				{
					MelonLogger.Warning($"MaxPlayers value {_maxPlayers.Value} exceeds maximum of 32766. Clamping to 32766.");
					_maxPlayers.Value = 32766;
				}
			}
		}

		private static MelonPreferences_Entry<T> CreateEntry<T>(string identifier, T defaultValue, string displayName, string description = null)
		{
			if (_category == null)
			{
				throw new InvalidOperationException("Preference category not initialized.");
			}
			return _category.CreateEntry<T>(identifier, defaultValue, displayName, description, false, false, (ValueValidator)null, (string)null);
		}
	}
}