Decompiled source of MorePlayersSimple v1.2.8

plugins/MorePlayersSimple.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MorePlayersSimple")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MorePlayersSimple")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e414bffa-f261-43ba-b6c5-3c2f5941d3af")]
[assembly: AssemblyFileVersion("1.2.7.0")]
[assembly: AssemblyInformationalVersion("1.2.7")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.2.7.0")]
namespace MorePlayersSimple;

[BepInPlugin("dyxc666.MorePlayersSimple", "More Players Simple", "1.2.8")]
public class MorePlayersSimplePlugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	public static ConfigEntry<int> MaxPlayersPublic;

	public static ConfigEntry<int> MaxPlayersPrivate;

	internal static string SelfCreatedRoomName = string.Empty;

	private readonly Harmony harmony = new Harmony("dyxc666.MorePlayersSimple");

	private void Awake()
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		MaxPlayersPublic = ((BaseUnityPlugin)this).Config.Bind<int>("常规", "公开房人数上限", 20, new ConfigDescription("公开房间最大玩家数(1-20)。修改后需重新开房才生效。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 20), Array.Empty<object>()));
		MaxPlayersPrivate = ((BaseUnityPlugin)this).Config.Bind<int>("常规", "私人房人数上限", 6, new ConfigDescription("私人房间最大玩家数(1-20,默认 6 = 游戏原版)。通过 GameManager.SetMaxPlayers 生效;修改后需重新开房才生效。注意原版流程按 6 人设计,超过 6 人不保证所有流程稳定。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 20), Array.Empty<object>()));
		harmony.PatchAll();
		Log.LogInfo((object)$"More Players Simple v1.2.8 loaded! Public={MaxPlayersPublic.Value}, Private={MaxPlayersPrivate.Value}");
	}
}
[HarmonyPatch(typeof(LoadBalancingClient), "OpCreateRoom")]
public static class OpCreateRoomPatch
{
	[HarmonyPrefix]
	public static bool Prefix(EnterRoomParams enterRoomParams)
	{
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Expected O, but got Unknown
		if (enterRoomParams.RoomOptions == null)
		{
			enterRoomParams.RoomOptions = new RoomOptions();
		}
		bool isVisible = enterRoomParams.RoomOptions.IsVisible;
		byte b = (byte)(isVisible ? MorePlayersSimplePlugin.MaxPlayersPublic.Value : MorePlayersSimplePlugin.MaxPlayersPrivate.Value);
		enterRoomParams.RoomOptions.MaxPlayers = b;
		MaxPlayersApplier.Desired = b;
		if (!string.IsNullOrEmpty(enterRoomParams.RoomName))
		{
			MorePlayersSimplePlugin.SelfCreatedRoomName = enterRoomParams.RoomName;
		}
		MorePlayersSimplePlugin.Log.LogInfo((object)string.Format("OpCreateRoom [{0}]: MaxPlayers = {1}", isVisible ? "PUBLIC" : "PRIVATE", b));
		return true;
	}
}
[HarmonyPatch(typeof(LoadBalancingClient), "OpJoinOrCreateRoom")]
public static class OpJoinOrCreateRoomPatch
{
	[HarmonyPrefix]
	public static bool Prefix(EnterRoomParams enterRoomParams)
	{
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Expected O, but got Unknown
		if (enterRoomParams.RoomOptions == null)
		{
			enterRoomParams.RoomOptions = new RoomOptions();
		}
		bool flag = enterRoomParams.RoomName != null && enterRoomParams.RoomName.EndsWith("_voice_");
		bool isVisible = enterRoomParams.RoomOptions.IsVisible;
		byte b = (byte)((!flag) ? ((byte)(isVisible ? MorePlayersSimplePlugin.MaxPlayersPublic.Value : MorePlayersSimplePlugin.MaxPlayersPrivate.Value)) : 20);
		enterRoomParams.RoomOptions.MaxPlayers = b;
		if (!flag)
		{
			MaxPlayersApplier.Desired = b;
		}
		if (!string.IsNullOrEmpty(enterRoomParams.RoomName))
		{
			MorePlayersSimplePlugin.SelfCreatedRoomName = enterRoomParams.RoomName;
		}
		MorePlayersSimplePlugin.Log.LogInfo((object)string.Format("[JoinOrCreate] [{0}]{1}: MaxPlayers = {2}", isVisible ? "PUBLIC" : "PRIVATE", flag ? " [VOICE]" : "", b));
		return true;
	}
}
[HarmonyPatch(typeof(SteamLobbyManager), "HostLobby")]
public static class HostLobbyPatch
{
	[HarmonyPostfix]
	public static void Postfix(SteamLobbyManager __instance)
	{
		try
		{
			FieldInfo fieldInfo = AccessTools.Field(typeof(SteamLobbyManager), "currentLobby");
			if (fieldInfo == null)
			{
				return;
			}
			object value = fieldInfo.GetValue(__instance);
			if (value == null)
			{
				return;
			}
			Type type = value.GetType();
			int value2 = MorePlayersSimplePlugin.MaxPlayersPublic.Value;
			PropertyInfo property = type.GetProperty("MaxMembers");
			if (property != null && property.CanWrite)
			{
				property.SetValue(value, value2);
				MorePlayersSimplePlugin.Log.LogInfo((object)$"[MorePlayers] Steam 大厅人数上限已设为 {value2}(属性 MaxMembers)");
				return;
			}
			FieldInfo fieldInfo2 = type.GetField("MaxMembers") ?? type.GetField("maxMembers");
			if (fieldInfo2 != null)
			{
				fieldInfo2.SetValue(value, value2);
				MorePlayersSimplePlugin.Log.LogInfo((object)$"[MorePlayers] Steam 大厅人数上限已设为 {value2}(字段 maxMembers)");
				return;
			}
			MethodInfo method = type.GetMethod("SetLobbyMemberLimit");
			if (method != null)
			{
				method.Invoke(value, new object[1] { value2 });
				MorePlayersSimplePlugin.Log.LogInfo((object)$"[MorePlayers] Steam 大厅人数上限已设为 {value2}(SetLobbyMemberLimit)");
			}
			else
			{
				MorePlayersSimplePlugin.Log.LogWarning((object)"[MorePlayers] 未能设置 Steam 大厅人数上限:当前版本的 Lobby 类型里没有 MaxMembers 属性/字段。");
			}
		}
		catch
		{
			MorePlayersSimplePlugin.Log.LogWarning((object)"Failed to set Steam lobby member limit.");
		}
	}
}
internal static class MaxPlayersApplier
{
	internal static int Desired;

	internal static void Apply(string reason)
	{
		try
		{
			GameManager instance = GameManager.instance;
			if (!((Object)(object)instance == (Object)null) && (!PhotonNetwork.InRoom || PhotonNetwork.IsMasterClient))
			{
				int num = Desired;
				if (num <= 0)
				{
					num = ((PhotonNetwork.CurrentRoom != null && PhotonNetwork.CurrentRoom.IsVisible) ? MorePlayersSimplePlugin.MaxPlayersPublic.Value : MorePlayersSimplePlugin.MaxPlayersPrivate.Value);
				}
				instance.SetMaxPlayers(num);
				MorePlayersSimplePlugin.Log.LogInfo((object)string.Format("[MaxPlayers] 已应用 {0}({1})| GameManager.maxPlayers={2} | 房间={3}", num, reason, instance.maxPlayers, (PhotonNetwork.CurrentRoom != null) ? PhotonNetwork.CurrentRoom.MaxPlayers.ToString() : "无"));
			}
		}
		catch (Exception ex)
		{
			MorePlayersSimplePlugin.Log.LogWarning((object)("[MaxPlayers] 应用失败: " + ex.Message));
		}
	}
}
[HarmonyPatch(typeof(NetworkConnect), "OnCreatedRoom")]
public static class OnCreatedRoomMaxPlayersPatch
{
	[HarmonyPostfix]
	public static void Postfix()
	{
		MaxPlayersApplier.Apply("房间已创建");
	}
}
[HarmonyPatch(typeof(NetworkConnect), "OnJoinedRoom")]
public static class OnJoinedRoomMaxPlayersPatch
{
	[HarmonyPostfix]
	public static void Postfix()
	{
		MaxPlayersApplier.Apply("已进入房间");
	}
}
[HarmonyPatch(typeof(GameManager), "SetMaxPlayers")]
public static class SetMaxPlayersGuardPatch
{
	[HarmonyPrefix]
	public static void Prefix(ref int _target)
	{
		if (!PhotonNetwork.InRoom || PhotonNetwork.IsMasterClient)
		{
			int desired = MaxPlayersApplier.Desired;
			if (desired > 0 && _target == 6 && desired != _target)
			{
				_target = desired;
			}
		}
	}
}
[HarmonyPatch(typeof(MenuPageServerList), "CreateServerElement")]
public static class CreateServerElementPatch
{
	[HarmonyPrefix]
	public static bool Prefix(object _room)
	{
		if (_room == null)
		{
			return true;
		}
		FieldInfo field = _room.GetType().GetField("roomName", BindingFlags.Instance | BindingFlags.Public);
		if (field == null)
		{
			return true;
		}
		string text = field.GetValue(_room) as string;
		if (string.IsNullOrEmpty(text))
		{
			return true;
		}
		bool flag = text == MorePlayersSimplePlugin.SelfCreatedRoomName;
		if (!flag && PhotonNetwork.InRoom && PhotonNetwork.CurrentRoom != null)
		{
			flag = text == PhotonNetwork.CurrentRoom.Name;
		}
		if (flag)
		{
			FieldInfo field2 = _room.GetType().GetField("maxPlayers", BindingFlags.Instance | BindingFlags.Public);
			if (field2 != null)
			{
				field2.SetValue(_room, MorePlayersSimplePlugin.MaxPlayersPublic.Value);
			}
		}
		return true;
	}
}