Decompiled source of Virality Fixed v1.7.0

plugins/MaxWasUnavailable.Virality.dll

Decompiled 14 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using DefaultNamespace;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Steamworks;
using Unity.Mathematics;
using UnityEngine;
using Virality.Core;
using Virality.Helpers;
using Virality.State;
using Zorro.Settings;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.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.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 Virality
{
	public static class PluginInfo
	{
		public const string PluginGuid = "MaxWasUnavailable.Virality";

		public const string PluginVersion = "1.7.0";
	}
	[ContentWarningPlugin("MaxWasUnavailable.Virality", "1.7.0", false)]
	public class Virality
	{
		[ContentWarningSetting]
		public class MaxPlayersSetting : IntSetting, IExposedSetting
		{
			public SettingCategory GetSettingCategory()
			{
				return (SettingCategory)4;
			}

			public string GetDisplayName()
			{
				return "Max Players";
			}

			public override void ApplyValue()
			{
				MaxPlayers = ValidateValue(((IntSetting)this).Value);
			}

			public override int GetDefaultValue()
			{
				return 12;
			}

			private static int ValidateValue(int value)
			{
				return math.clamp(value, 1, 100);
			}

			public string GetDescription()
			{
				return "The maximum number of players allowed in your lobby.";
			}
		}

		[ContentWarningSetting]
		public class LateJoinSetting : BoolSetting, IExposedSetting
		{
			public SettingCategory GetSettingCategory()
			{
				return (SettingCategory)4;
			}

			public string GetDisplayName()
			{
				return "Allow Late Join";
			}

			public override void ApplyValue()
			{
				AllowLateJoin = ((BoolSetting)this).Value;
			}

			protected override bool GetDefaultValue()
			{
				return true;
			}

			public string GetDescription()
			{
				return "Whether or not to allow players to join your lobby after the game has started.";
			}
		}

		[ContentWarningSetting]
		public class VoiceFixSetting : BoolSetting, IExposedSetting
		{
			public SettingCategory GetSettingCategory()
			{
				return (SettingCategory)4;
			}

			public string GetDisplayName()
			{
				return "Enable Voice Fix";
			}

			public override void ApplyValue()
			{
				EnableVoiceFix = ((BoolSetting)this).Value;
			}

			protected override bool GetDefaultValue()
			{
				return true;
			}

			public string GetDescription()
			{
				return "Whether or not to enable the voice fix.";
			}
		}

		private bool _isPatched;

		private Harmony? Harmony { get; set; }

		internal static Logger? Logger { get; }

		internal static int MaxPlayers { get; private set; }

		internal static bool AllowLateJoin { get; private set; }

		internal static bool EnableVoiceFix { get; private set; }

		public static Virality Instance { get; }

		static Virality()
		{
			MaxPlayers = 12;
			AllowLateJoin = true;
			EnableVoiceFix = true;
			Logger = new Logger();
			Instance = new Virality();
		}

		public Virality()
		{
			Logger?.LogInfo("Plugin MaxWasUnavailable.Virality is loaded!");
		}

		internal void Initialize()
		{
			PatchAll();
		}

		private void PatchAll()
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			if (_isPatched)
			{
				Logger?.LogWarning("Already patched!");
				return;
			}
			Logger?.LogDebug("Patching...");
			if (Harmony == null)
			{
				Harmony harmony = new Harmony("MaxWasUnavailable.Virality");
				Harmony = harmony;
			}
			try
			{
				Harmony.PatchAll();
				_isPatched = true;
				Logger?.LogDebug("Patched!");
			}
			catch (Exception arg)
			{
				Logger?.LogError($"Failed to patch: {arg}");
			}
		}

		public void UnpatchAll()
		{
			if (!_isPatched)
			{
				Logger?.LogWarning("Already unpatched!");
				return;
			}
			Logger?.LogDebug("Unpatching...");
			try
			{
				Harmony harmony = Harmony;
				if (harmony != null)
				{
					harmony.UnpatchSelf();
				}
				_isPatched = false;
				Logger?.LogDebug("Unpatched!");
			}
			catch (Exception arg)
			{
				Logger?.LogError($"Failed to unpatch: {arg}");
			}
		}
	}
	[BepInPlugin("MaxWasUnavailable.Virality", "Virality", "1.7.0")]
	public class ViralityBepInEx : BaseUnityPlugin
	{
		private void Awake()
		{
			Virality.Instance.Initialize();
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("Virality {0} patched, max players {1}", "1.7.0", Virality.MaxPlayers));
		}
	}
}
namespace Virality.State
{
	public static class CurrentObjectiveTracker
	{
		public static Objective? CurrentObjective { get; internal set; }
	}
	public static class DoorOpenTracker
	{
		public static bool IsDoorOpen { get; internal set; }
	}
	public static class PhotonLobbyLimitTracker
	{
		public static int? PlayerLimit { get; set; }
	}
}
namespace Virality.Patches
{
	[HarmonyPatch(typeof(BedBoss))]
	[HarmonyPriority(800)]
	internal static class BedBossPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("OnPlayerJoined")]
		private static bool OnPlayerJoinedPrefix()
		{
			return PlayerHandler.instance.players.Count <= 4;
		}
	}
	[HarmonyPatch(typeof(EscapeMenuMainPage))]
	[HarmonyPriority(800)]
	internal static class EscapeMenuMainPagePatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("Update")]
		private static bool UpdatePrefix(EscapeMenuMainPage __instance)
		{
			bool active = LateJoinHelper.IsLateJoinAllowed || VanillaCheck();
			((Component)__instance.inviteButton).gameObject.SetActive(active);
			return false;
		}

		private static bool VanillaCheck()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			if (MainMenuHandler.SteamLobbyHandler != null && (Object)SurfaceNetworkHandler.Instance != (Object)null)
			{
				return !SurfaceNetworkHandler.HasStarted;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(InviteFriendsTerminal))]
	[HarmonyPriority(800)]
	internal static class InviteFriendsTerminalPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static bool IsGameFullPrefix(ref bool __result)
		{
			if (SteamLobbyHelper.LobbyHandler == null)
			{
				return true;
			}
			__result = PlayerHandler.instance.players.Count > SteamLobbyHelper.LobbyHandler.m_MaxPlayers;
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static bool IsGameStartedPrefix(ref bool __result)
		{
			if (!Virality.AllowLateJoin)
			{
				return true;
			}
			__result = false;
			return false;
		}
	}
	[HarmonyPatch(typeof(MainMenuHandler))]
	[HarmonyPriority(800)]
	internal static class MainMenuHandlerPatches
	{
		[HarmonyTranspiler]
		[HarmonyPatch("OnLobbyHosted")]
		private static IEnumerable<CodeInstruction> OnLobbyHostedTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo operand = AccessTools.Method(typeof(LobbyHelper), "GetLobbyMaxConfig", (Type[])null, (Type[])null);
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			bool flag = false;
			for (int i = 0; i + 1 < list.Count; i++)
			{
				if (list[i].opcode == OpCodes.Ldc_I4_4 && IsMaxPlayersStore(list[i + 1]))
				{
					list[i].opcode = OpCodes.Call;
					list[i].operand = operand;
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				Virality.Logger?.LogError("Could not find the room's max players constant");
			}
			return list;
		}

		private static bool IsMaxPlayersStore(CodeInstruction instruction)
		{
			object operand = instruction.operand;
			if (!(operand is MethodInfo methodInfo))
			{
				if (operand is FieldInfo fieldInfo)
				{
					return fieldInfo.Name == "MaxPlayers";
				}
				return false;
			}
			return methodInfo.Name == "set_MaxPlayers";
		}
	}
	[HarmonyPatch(typeof(Modal))]
	[HarmonyPriority(800)]
	internal static class ModalPatches
	{
		private const string PhotonRoomErrorTitle = "failed to create photon room";

		private const string PhotonLobbyLimitBodyPartial = "max players peer room value is too big";

		private static bool IsPhotonLobbyLimitError(string title, string body)
		{
			if (title.ToLower() == "failed to create photon room")
			{
				return body.ToLower().Contains("max players peer room value is too big");
			}
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch("ShowError")]
		private static void ShowErrorPrefix(string title, ref string body)
		{
			if (IsPhotonLobbyLimitError(title, body))
			{
				int num = int.Parse(body.Split(':').Last());
				Virality.Logger?.LogWarning($"Photon lobby limit error detected. Setting temporary max player override to {num}. You may use a mod such as \"Self Sufficient\" to increase this limit.");
				PhotonLobbyLimitTracker.PlayerLimit = num;
				body += $"\n\nThe lobby player limit will temporarily be set to {num} for the duration of this session.";
				body += "\nYou may use a mod such as \"Self Sufficient\" to host your own Photon server with a higher player limit.";
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("Show")]
		private static void ShowPostfix(string title, ref string body, ref ModalOption[] options)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			if (IsPhotonLobbyLimitError(title, body))
			{
				ModalOption element = new ModalOption("Cancel", (Action)CancelPhotonLobbyLimitOverride);
				options = options.Append(element).ToArray();
			}
		}

		private static void CancelPhotonLobbyLimitOverride()
		{
			PhotonLobbyLimitTracker.PlayerLimit = null;
			Virality.Logger?.LogWarning("Photon lobby limit override cancelled.");
		}
	}
	[HarmonyPatch(typeof(PhotonGameLobbyHandler))]
	[HarmonyPriority(800)]
	internal static class PhotonGameLobbyHandlerPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("Start")]
		private static void OnSteamHostedPrefix()
		{
			if (Virality.EnableVoiceFix)
			{
				OverrideVoiceServerAppId();
			}
		}

		private static void OverrideVoiceServerAppId()
		{
			PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime;
			Virality.Logger?.LogDebug("Voice server app id set to realtime server app id (" + PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice + ")");
		}

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void StartPostfixHide()
		{
			if (PhotonLobbyHelper.IsMasterClient() && Virality.AllowLateJoin && !PhotonLobbyHelper.IsOnSurface())
			{
				LateJoinHelper.DisableLateJoin();
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("SetCurrentObjective")]
		private static bool SetCurrentObjectivePostfix(ref Objective objective)
		{
			if (!PhotonLobbyHelper.IsMasterClient())
			{
				return true;
			}
			bool flag = objective is LeaveHouseObjective;
			if (flag)
			{
				Objective currentObjective = CurrentObjectiveTracker.CurrentObjective;
				flag = (!(currentObjective is InviteFriendsObjective) && !(currentObjective is LeaveHouseObjective)) || 1 == 0;
			}
			if (flag)
			{
				return false;
			}
			Virality.Logger?.LogDebug($"Setting current objective to {objective}.");
			CurrentObjectiveTracker.CurrentObjective = objective;
			return true;
		}
	}
	[HarmonyPatch(typeof(PlayerHandler))]
	[HarmonyPriority(800)]
	internal static class PlayerHandlerPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("AddPlayer")]
		private static void AddPlayerPostfixOpenDoor(Player player)
		{
			if (PhotonLobbyHelper.IsMasterClient() && !player.IsLocal && Virality.AllowLateJoin && DoorOpenTracker.IsDoorOpen && PhotonLobbyHelper.IsOnSurface())
			{
				SendDoorOpenRPC(player);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("AddPlayer")]
		private static void AddPlayerPostfixSyncObjective(Player player)
		{
			if (PhotonLobbyHelper.IsMasterClient() && Virality.AllowLateJoin)
			{
				SyncObjectiveRPC(player);
			}
		}

		private static void SendDoorOpenRPC(Player player)
		{
			SurfaceNetworkHandler.Instance.m_View.RPC("RPCA_OpenDoor", player.refs.view.Controller, Array.Empty<object>());
		}

		private static void SyncObjectiveRPC(Player player)
		{
			Objective currentObjective = CurrentObjectiveTracker.CurrentObjective;
			if (currentObjective != null)
			{
				((MonoBehaviourPun)PhotonGameLobbyHandler.Instance).photonView.RPC("RPC_SetCurrentObjective", player.refs.view.Controller, new object[1] { ObjectiveDatabase.GetIndex(currentObjective) });
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("AllPlayersInBed")]
		private static void AllPlayersInBedPostfix(PlayerHandler __instance, ref bool __result)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected O, but got Unknown
			int num = 0;
			for (int i = 0; i < __instance.playersAlive.Count; i++)
			{
				if ((Object)__instance.playersAlive.ToArray()[i].data.currentBed != (Object)null)
				{
					num++;
				}
			}
			__result = num >= Math.Min(__instance.playersAlive.Count, 4);
		}

		[HarmonyPostfix]
		[HarmonyPatch("AllPlayersAsleep")]
		private static void AllPlayersAsleepPostfix(PlayerHandler __instance, ref bool __result)
		{
			int num = 0;
			for (int i = 0; i < __instance.playersAlive.Count; i++)
			{
				if (__instance.playersAlive.ToArray()[i].data.sleepAmount >= 0.9f)
				{
					num++;
				}
			}
			__result = num >= Math.Min(__instance.playersAlive.Count, 4);
		}
	}
	[HarmonyPatch(typeof(SpawnHandler))]
	[HarmonyPriority(800)]
	internal static class SpawnHandlerPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("FindLocalSpawnIndex")]
		private static void FindLocalSpawnIndexPostfix(ref SpawnHandler __instance)
		{
			if (__instance.m_LocalSpawnIndex != 0)
			{
				int num = Math.Min(Math.Min(__instance.m_HospitalSpawns.Length, __instance.m_HouseSpawns.Length), __instance.m_DiveBellSpawns.Length) - 1;
				SpawnHandler obj = __instance;
				obj.m_LocalSpawnIndex %= num;
				SpawnHandler obj2 = __instance;
				obj2.m_LocalSpawnIndex++;
			}
		}
	}
	[HarmonyPatch(typeof(SteamLobbyHandler))]
	[HarmonyPriority(800)]
	internal static class SteamLobbyHandlerPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("HostMatch")]
		private static void HostMatchPrefix()
		{
			SteamLobbyHelper.SetLobbyMaxToConfig();
		}

		[HarmonyTranspiler]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static IEnumerable<CodeInstruction> ConstructorTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo operand = AccessTools.Method(typeof(LobbyHelper), "GetLobbyMaxConfig", (Type[])null, (Type[])null);
			FieldInfo objB = AccessTools.Field(typeof(SteamLobbyHandler), "m_MaxPlayers");
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			bool flag = false;
			for (int i = 0; i + 1 < list.Count; i++)
			{
				if (list[i].opcode == OpCodes.Ldarg_1 && list[i + 1].opcode == OpCodes.Stfld && object.Equals(list[i + 1].operand, objB))
				{
					list[i].opcode = OpCodes.Call;
					list[i].operand = operand;
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				Virality.Logger?.LogError("Could not find the Steam lobby max players assignment");
			}
			return list;
		}
	}
	[HarmonyPatch(typeof(SurfaceNetworkHandler))]
	[HarmonyPriority(800)]
	internal static class SurfaceNetworkHandlerPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("RPCM_StartGame")]
		private static void RPCM_StartGamePostfix()
		{
			if (PhotonLobbyHelper.IsMasterClient() && Virality.AllowLateJoin && PhotonLobbyHelper.IsOnSurface())
			{
				DoorOpenTracker.IsDoorOpen = true;
				LateJoinHelper.EnableLateJoin();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("OnSlept")]
		private static void OnSleptPostfix()
		{
			if (PhotonLobbyHelper.IsMasterClient() && Virality.AllowLateJoin)
			{
				LateJoinHelper.EnableLateJoin();
			}
		}
	}
}
namespace Virality.Helpers
{
	public static class LateJoinHelper
	{
		public static bool IsLateJoinAllowed { get; private set; }

		public static void EnableLateJoin()
		{
			Virality.Logger?.LogDebug("Enabling late join.");
			SteamLobbyHelper.LobbyHandler.OpenLobby();
			PhotonNetwork.CurrentRoom.IsOpen = true;
			PhotonNetwork.CurrentRoom.IsVisible = true;
			IsLateJoinAllowed = true;
		}

		public static void DisableLateJoin()
		{
			Virality.Logger?.LogDebug("Disabling late join.");
			SteamLobbyHelper.LobbyHandler.HideLobby();
			PhotonNetwork.CurrentRoom.IsOpen = false;
			PhotonNetwork.CurrentRoom.IsVisible = false;
			IsLateJoinAllowed = false;
		}
	}
	public static class LobbyHelper
	{
		public static int GetLobbyMaxConfig()
		{
			return PhotonLobbyLimitTracker.PlayerLimit ?? Virality.MaxPlayers;
		}
	}
	public static class PhotonLobbyHelper
	{
		public static bool IsOnSurface()
		{
			return PhotonGameLobbyHandler.IsSurface;
		}

		public static void SetLobbyMaxPlayers(int maxPlayers)
		{
			PhotonNetwork.CurrentRoom.MaxPlayers = maxPlayers;
		}

		public static void SetLobbyMaxToConfig()
		{
			SetLobbyMaxPlayers(LobbyHelper.GetLobbyMaxConfig());
		}

		public static bool IsMasterClient()
		{
			return PhotonNetwork.IsMasterClient;
		}
	}
	public static class SteamLobbyHelper
	{
		public static SteamLobbyHandler? LobbyHandler => MainMenuHandler.SteamLobbyHandler;

		public static AppId_t GetAppId()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			return SteamUtils.GetAppID();
		}

		public static CSteamID GetUserId()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			return SteamUser.GetSteamID();
		}

		public static CSteamID GetLobbyId()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			return LobbyHandler.m_CurrentLobby;
		}

		public static void SetLobbyMaxPlayers(int maxPlayers)
		{
			Virality.Logger?.LogInfo($"Setting lobby max players to {maxPlayers}.");
			LobbyHandler.m_MaxPlayers = maxPlayers;
		}

		public static void SetLobbyMaxToConfig()
		{
			SetLobbyMaxPlayers(LobbyHelper.GetLobbyMaxConfig());
		}
	}
}
namespace Virality.Core
{
	public class Logger
	{
		private string MessageFormat(string message)
		{
			return "[MaxWasUnavailable.Virality:1.7.0] " + message;
		}

		public void LogDebug(string message)
		{
			Debug.Log((object)MessageFormat(message));
		}

		public void LogInfo(string message)
		{
			Debug.Log((object)MessageFormat(message));
		}

		public void LogWarning(string message)
		{
			Debug.LogWarning((object)MessageFormat(message));
		}

		public void LogError(string message)
		{
			Debug.LogError((object)MessageFormat(message));
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}