Decompiled source of MaxPlayerCount v1.2.3

MaxPlayerCount.dll

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Steamworks;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MaxPlayerCount")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("MaxPlayerCount")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("1.2.3")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.3.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace MaxPlayerCount
{
	[BepInPlugin("Azumatt.MaxPlayerCount", "MaxPlayerCount", "1.2.3")]
	[BepInIncompatibility("org.bepinex.plugins.valheim_plus")]
	public class MaxPlayerCountPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ZNet), "RPC_PeerInfo")]
		internal class MaxPlayersCount
		{
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> MaxPlayersPatch(IEnumerable<CodeInstruction> instructions)
			{
				List<CodeInstruction> list = new List<CodeInstruction>(instructions);
				for (int i = 0; i < list.Count; i++)
				{
					if (!(list[i].opcode == OpCodes.Call) || !(list[i].operand is MethodInfo methodInfo) || !(methodInfo.Name == "GetNrOfPlayers"))
					{
						continue;
					}
					for (int j = i; j < list.Count; j++)
					{
						if (list[j].opcode == OpCodes.Ldc_I4_S)
						{
							list[j].operand = ReplacePlayerLimit();
							break;
						}
					}
					break;
				}
				return list.AsEnumerable();
			}

			internal static int ReplacePlayerLimit(bool playfab = false)
			{
				if (!playfab)
				{
					return _maxPlayers.Value;
				}
				return _maxPlayers.Value + 1;
			}
		}

		[HarmonyPatch(typeof(FejdStartup), "Start")]
		public static class FejdStartupPatch
		{
			private static void Postfix(FejdStartup __instance)
			{
				//IL_0024: Unknown result type (might be due to invalid IL or missing references)
				//IL_0029: Unknown result type (might be due to invalid IL or missing references)
				//IL_002a: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
				//IL_0106: Expected O, but got Unknown
				//IL_0030: Unknown result type (might be due to invalid IL or missing references)
				//IL_0032: Invalid comparison between Unknown and I4
				//IL_006f: Unknown result type (might be due to invalid IL or missing references)
				//IL_007b: Expected O, but got Unknown
				//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c0: Expected O, but got Unknown
				MaxPlayerCountLogger.LogInfo((object)("Patching for backend: " + ((object)(OnlineBackendType)(ref ZNet.m_onlineBackend)).ToString()));
				OnlineBackendType onlineBackend = ZNet.m_onlineBackend;
				if ((int)onlineBackend != 0)
				{
					if ((int)onlineBackend == 1)
					{
						instance._harmony.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(ZPlayFabMatchmaking), "CreateLobby", (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(AccessTools.DeclaredMethod(typeof(FejdStartupPatch), "MaxPlayerPlayfabTranspiler", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null);
						instance._harmony.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(ZPlayFabMatchmaking), "CreateAndJoinNetwork", (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(AccessTools.DeclaredMethod(typeof(FejdStartupPatch), "MaxPlayerPlayfabTranspiler2", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null);
					}
				}
				else
				{
					instance._harmony.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(SteamGameServer), "SetMaxPlayerCount", (Type[])null, (Type[])null), new HarmonyMethod(AccessTools.DeclaredMethod(typeof(FejdStartupPatch), "SetMaxPlayerSteamPrefix", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}

			private static void SetMaxPlayerSteamPrefix(ref int cPlayersMax)
			{
				int value = _maxPlayers.Value;
				if (value >= 1)
				{
					cPlayersMax = value;
				}
			}

			public static IEnumerable<CodeInstruction> MaxPlayerPlayfabTranspiler(IEnumerable<CodeInstruction> instructions)
			{
				foreach (CodeInstruction instruction in instructions)
				{
					if (instruction.opcode == OpCodes.Ldc_I4_S && (sbyte)instruction.operand == 10)
					{
						yield return new CodeInstruction(OpCodes.Ldc_I4, (object)MaxPlayersCount.ReplacePlayerLimit());
					}
					else
					{
						yield return instruction;
					}
				}
			}

			public static IEnumerable<CodeInstruction> MaxPlayerPlayfabTranspiler2(IEnumerable<CodeInstruction> instructions)
			{
				foreach (CodeInstruction instruction in instructions)
				{
					if (instruction.opcode == OpCodes.Ldc_I4_S && (sbyte)instruction.operand == 11)
					{
						yield return new CodeInstruction(OpCodes.Ldc_I4, (object)MaxPlayersCount.ReplacePlayerLimit(playfab: true));
					}
					else
					{
						yield return instruction;
					}
				}
			}
		}

		private class ConfigurationManagerAttributes
		{
			public bool? Browsable = false;
		}

		private class AcceptableShortcuts : AcceptableValueBase
		{
			public AcceptableShortcuts()
				: base(typeof(KeyboardShortcut))
			{
			}

			public override object Clamp(object value)
			{
				return value;
			}

			public override bool IsValid(object value)
			{
				return true;
			}

			public override string ToDescriptionString()
			{
				return "# Acceptable values: " + string.Join(", ", UnityInput.Current.SupportedKeyCodes);
			}
		}

		internal const string ModName = "MaxPlayerCount";

		internal const string ModVersion = "1.2.3";

		internal const string Author = "Azumatt";

		private const string ModGUID = "Azumatt.MaxPlayerCount";

		private static string ConfigFileName = "Azumatt.MaxPlayerCount.cfg";

		private static string ConfigFileFullPath;

		private readonly Harmony _harmony = new Harmony("Azumatt.MaxPlayerCount");

		public static MaxPlayerCountPlugin instance;

		private static readonly ManualLogSource MaxPlayerCountLogger;

		private static ConfigEntry<int> _maxPlayers;

		public void Awake()
		{
			instance = this;
			_maxPlayers = config("1 - General", "MaxPlayerCount", 20, "Override the player count that valheim checks for. Default is the vanilla max of 10.");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				MaxPlayerCountLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				MaxPlayerCountLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
				MaxPlayerCountLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description)
		{
			return ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, description);
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, string description, bool synchronizedSetting = true)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			return config(group, name, value, new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>()));
		}

		static MaxPlayerCountPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			instance = null;
			MaxPlayerCountLogger = Logger.CreateLogSource("MaxPlayerCount");
			_maxPlayers = null;
		}
	}
}