Decompiled source of Overstaffed v0.2.0

Overstaffed.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Overstaffed.Patches;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Overstaffed")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyInformationalVersion("0.2.0+af53bf0a480434b4286152982db8af13b820e15a")]
[assembly: AssemblyProduct("Overstaffed")]
[assembly: AssemblyTitle("Overstaffed")]
[assembly: AssemblyVersion("0.2.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 Overstaffed
{
	[BepInPlugin("darkharasho.Overstaffed", "Overstaffed", "0.2.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal const int MaxAllowedPlayers = 20;

		internal const int MinAllowedPlayers = 1;

		internal const int DefaultPlayers = 10;

		internal static ManualLogSource Log;

		internal static ConfigEntry<int> ConfigMaxPlayers;

		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_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Expected O, but got Unknown
			//IL_009f: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			ConfigMaxPlayers = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxPlayers", 10, new ConfigDescription("The maximum number of players allowed in a server.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 20), Array.Empty<object>()));
			Log.LogInfo((object)string.Format("Overstaffed v{0} loading — MaxPlayers={1}", "0.2.0", ConfigMaxPlayers.Value));
			Harmony val = new Harmony("darkharasho.Overstaffed");
			TryPatch(val, typeof(GameManagerAwakePatch));
			TryPatch(val, typeof(GameManagerSetMaxPlayersPatch));
			TryPatch(val, typeof(NetworkConnectOnConnectedToMasterPatch));
			Log.LogInfo((object)"Overstaffed loaded.");
		}

		private static void TryPatch(Harmony harmony, Type patchType)
		{
			try
			{
				harmony.CreateClassProcessor(patchType).Patch();
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("[Overstaffed] Failed to apply " + patchType.Name + ": " + ex.GetType().Name + ": " + ex.Message));
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "darkharasho.Overstaffed";

		public const string PLUGIN_NAME = "Overstaffed";

		public const string PLUGIN_VERSION = "0.2.0";
	}
}
namespace Overstaffed.Patches
{
	[HarmonyPatch(typeof(GameManager), "Awake")]
	internal static class GameManagerAwakePatch
	{
		private static void Postfix(GameManager __instance)
		{
			int value = Plugin.ConfigMaxPlayers.Value;
			int maxPlayersPhoton = __instance.maxPlayersPhoton;
			int maxPlayers = __instance.maxPlayers;
			if (value > __instance.maxPlayersPhoton)
			{
				__instance.maxPlayersPhoton = value;
			}
			__instance.maxPlayers = value;
			Plugin.Log.LogInfo((object)($"[GameManager.Awake] maxPlayers {maxPlayers} -> {__instance.maxPlayers}, " + $"maxPlayersPhoton {maxPlayersPhoton} -> {__instance.maxPlayersPhoton}"));
		}
	}
	[HarmonyPatch(typeof(GameManager), "SetMaxPlayers")]
	internal static class GameManagerSetMaxPlayersPatch
	{
		private static void Prefix(GameManager __instance, ref int _target)
		{
			if (_target > __instance.maxPlayersPhoton)
			{
				__instance.maxPlayersPhoton = _target;
			}
		}
	}
	[HarmonyPatch(typeof(NetworkConnect), "OnConnectedToMaster")]
	internal static class NetworkConnectOnConnectedToMasterPatch
	{
		public static int GetExpectedMaxPlayers()
		{
			int value = Plugin.ConfigMaxPlayers.Value;
			if (value < 1)
			{
				return 1;
			}
			if (value > 255)
			{
				return 255;
			}
			return value;
		}

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo helper = AccessTools.Method(typeof(NetworkConnectOnConnectedToMasterPatch), "GetExpectedMaxPlayers", (Type[])null, (Type[])null);
			int replaced = 0;
			foreach (CodeInstruction instruction in instructions)
			{
				if (instruction.opcode == OpCodes.Ldc_I4_6 && replaced == 0)
				{
					replaced++;
					instruction.opcode = OpCodes.Call;
					instruction.operand = helper;
					yield return instruction;
				}
				else
				{
					yield return instruction;
				}
			}
			if (replaced == 0)
			{
				Plugin.Log.LogWarning((object)"[NetworkConnect.OnConnectedToMaster] transpiler did NOT find ldc.i4.6 — public matchmaking expectedMaxPlayers will stay at vanilla 6. Private/friends lobbies still respect MaxPlayers via the GameManager patches.");
			}
			else
			{
				Plugin.Log.LogInfo((object)"[NetworkConnect.OnConnectedToMaster] transpiler replaced ldc.i4.6 with GetExpectedMaxPlayers()");
			}
		}
	}
}