Decompiled source of RoundsRandom v1.0.0

RoundsRandom.dll

Decompiled 18 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using TMPro;
using UnboundLib;
using UnboundLib.Networking;
using UnboundLib.Utils.UI;
using UnityEngine;
using UnityEngine.Events;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("RoundsRandom")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+2f925e48a8f7156ff26061e25c3d2f59fac49e4f")]
[assembly: AssemblyProduct("RoundsRandom")]
[assembly: AssemblyTitle("RoundsRandom")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 RoundsRandom
{
	public enum SelectionMode
	{
		Random,
		Worst,
		Best
	}
	[BepInPlugin("com.dandol328.rounds.random", "RoundsRandom", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static Action<GameObject> <0>__CreateModMenu;

			public static NetworkEvent <1>__OnJoinedRoom;
		}

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<SelectionMode> Mode;

		internal static ConfigEntry<int> CardsPerRound;

		private Harmony harmony;

		internal static SelectionMode EffectiveMode { get; private set; }

		internal static int EffectiveCardsPerRound { get; private set; }

		internal static event Action SettingsChanged;

		private void Awake()
		{
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Expected O, but got Unknown
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Expected O, but got Unknown
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Automatically choose cards instead of allowing manual picks.");
			Mode = ((BaseUnityPlugin)this).Config.Bind<SelectionMode>("General", "Mode", SelectionMode.Random, "Random, Worst (lowest rarity), or Best (highest rarity).");
			CardsPerRound = ((BaseUnityPlugin)this).Config.Bind<int>("General", "CardsPerRound", 1, "Number of cards to automatically choose each round, from 1 to 5.");
			EffectiveMode = Mode.Value;
			EffectiveCardsPerRound = Mathf.Clamp(CardsPerRound.Value, 1, 5);
			Unbound.RegisterMenu("Rounds Random", (UnityAction)null, (Action<GameObject>)CreateModMenu, (GameObject)null, false);
			NetworkEventCallbacks obj = ((Component)this).gameObject.AddComponent<NetworkEventCallbacks>();
			object obj2 = <>O.<1>__OnJoinedRoom;
			if (obj2 == null)
			{
				NetworkEvent val = OnJoinedRoom;
				<>O.<1>__OnJoinedRoom = val;
				obj2 = (object)val;
			}
			obj.OnJoinedRoomEvent += (NetworkEvent)obj2;
			harmony = new Harmony("com.dandol328.rounds.random");
			harmony.PatchAll();
		}

		private static void OnJoinedRoom()
		{
			if (PhotonNetwork.IsMasterClient)
			{
				EffectiveMode = Mode.Value;
				EffectiveCardsPerRound = Mathf.Clamp(CardsPerRound.Value, 1, 5);
				BroadcastSettings();
			}
			else
			{
				NetworkingManager.RPC(typeof(HostModeSync), "RequestSettings", Array.Empty<object>());
			}
		}

		private static void SetMode(SelectionMode mode)
		{
			if (!PhotonNetwork.InRoom || PhotonNetwork.IsMasterClient)
			{
				Mode.Value = mode;
				EffectiveMode = mode;
				if (PhotonNetwork.InRoom)
				{
					BroadcastSettings();
				}
				Plugin.SettingsChanged?.Invoke();
			}
		}

		private static void SetCardsPerRound(int cardsPerRound)
		{
			if (!PhotonNetwork.InRoom || PhotonNetwork.IsMasterClient)
			{
				CardsPerRound.Value = cardsPerRound;
				EffectiveCardsPerRound = Mathf.Clamp(cardsPerRound, 1, 5);
				if (PhotonNetwork.InRoom)
				{
					BroadcastSettings();
				}
				Plugin.SettingsChanged?.Invoke();
			}
		}

		private static void BroadcastSettings()
		{
			NetworkingManager.RPC_Others(typeof(HostModeSync), "ReceiveSettings", new object[2]
			{
				(int)EffectiveMode,
				EffectiveCardsPerRound
			});
		}

		internal static void ApplyHostSettings(int mode, int cardsPerRound)
		{
			if (!Enum.IsDefined(typeof(SelectionMode), mode))
			{
				Debug.LogError($"RoundsRandom received invalid host mode value: {mode}.");
				return;
			}
			EffectiveMode = (SelectionMode)mode;
			EffectiveCardsPerRound = Mathf.Clamp(cardsPerRound, 1, 5);
			Plugin.SettingsChanged?.Invoke();
		}

		private static void CreateModMenu(GameObject menu)
		{
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Expected O, but got Unknown
			MenuHandler.CreateToggle(Enabled.Value, "Automatically choose cards", menu, (UnityAction<bool>)delegate(bool value)
			{
				Enabled.Value = value;
			}, 60, true, (Color?)null, (TMP_FontAsset)null, (Material)null, (TextAlignmentOptions?)null);
			GameObject modeButton = null;
			modeButton = MenuHandler.CreateButton(GetModeLabel(), menu, (UnityAction)delegate
			{
				SetMode((SelectionMode)((int)(EffectiveMode + 1) % 3));
				((TMP_Text)modeButton.GetComponentInChildren<TextMeshProUGUI>()).text = GetModeLabel().ToUpperInvariant();
			}, 60, true, (Color?)null, (TMP_FontAsset)null, (Material)null, (TextAlignmentOptions?)null);
			GameObject cardsButton = null;
			cardsButton = MenuHandler.CreateButton(GetCardsPerRoundLabel(), menu, (UnityAction)delegate
			{
				SetCardsPerRound(EffectiveCardsPerRound % 5 + 1);
				((TMP_Text)cardsButton.GetComponentInChildren<TextMeshProUGUI>()).text = GetCardsPerRoundLabel().ToUpperInvariant();
			}, 60, true, (Color?)null, (TMP_FontAsset)null, (Material)null, (TextAlignmentOptions?)null);
			TextMeshProUGUI val = default(TextMeshProUGUI);
			MenuHandler.CreateText("Mode cycles through random, worst, and best.", menu, ref val, 35, false, (Color?)null, (TMP_FontAsset)null, (Material)null, (TextAlignmentOptions?)null);
			SettingsChanged += delegate
			{
				((TMP_Text)modeButton.GetComponentInChildren<TextMeshProUGUI>()).text = GetModeLabel().ToUpperInvariant();
				((TMP_Text)cardsButton.GetComponentInChildren<TextMeshProUGUI>()).text = GetCardsPerRoundLabel().ToUpperInvariant();
			};
		}

		private static string GetModeLabel()
		{
			return $"Mode: {EffectiveMode}";
		}

		private static string GetCardsPerRoundLabel()
		{
			return $"Cards per round: {EffectiveCardsPerRound}";
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(CardChoice), "DoPlayerSelect")]
	internal static class AutomaticPick
	{
		private const float DisplaySeconds = 2f;

		private static GameObject firstCard;

		private static int pickerId = -1;

		private static float readyAt;

		private static bool Prefix(CardChoice __instance, List<GameObject> ___spawnedCards, bool ___isPlaying)
		{
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Expected I4, but got Unknown
			if (!Plugin.Enabled.Value)
			{
				firstCard = null;
				return true;
			}
			if (___spawnedCards.Count == 0 || !Object.op_Implicit((Object)(object)___spawnedCards[0]))
			{
				firstCard = null;
				return false;
			}
			GameObject val = ___spawnedCards[0];
			PhotonView component = val.GetComponent<PhotonView>();
			if (!Object.op_Implicit((Object)(object)component))
			{
				firstCard = null;
				return false;
			}
			if (___isPlaying)
			{
				firstCard = null;
				return false;
			}
			if ((Object)(object)firstCard != (Object)(object)val || pickerId != __instance.pickrID)
			{
				firstCard = val;
				pickerId = __instance.pickrID;
				readyAt = Time.realtimeSinceStartup + 2f;
			}
			if (!component.IsMine)
			{
				return false;
			}
			if (Time.realtimeSinceStartup < readyAt)
			{
				return false;
			}
			GameObject val2 = null;
			int num = 0;
			int num2 = 0;
			foreach (GameObject ___spawnedCard in ___spawnedCards)
			{
				if (!Object.op_Implicit((Object)(object)___spawnedCard))
				{
					continue;
				}
				CardInfo component2 = ___spawnedCard.GetComponent<CardInfo>();
				if (Object.op_Implicit((Object)(object)component2))
				{
					int num3 = (int)component2.rarity;
					if ((Object)(object)val2 == (Object)null || (Plugin.EffectiveMode == SelectionMode.Worst && num3 < num) || (Plugin.EffectiveMode == SelectionMode.Best && num3 > num))
					{
						val2 = ___spawnedCard;
						num = num3;
						num2 = 1;
					}
					else if ((Plugin.EffectiveMode == SelectionMode.Random || num3 == num) && Random.Range(0, ++num2) == 0)
					{
						val2 = ___spawnedCard;
					}
				}
			}
			if (Object.op_Implicit((Object)(object)val2))
			{
				__instance.Pick(val2, false);
				firstCard = null;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(CardChoice), "DoPick")]
	internal static class PickCountPatch
	{
		private static void Prefix(ref int picksToSet)
		{
			picksToSet = Plugin.EffectiveCardsPerRound;
		}
	}
	[HarmonyPatch(typeof(CardChoice), "Update")]
	internal static class CardRevealPatch
	{
		private sealed class RevealState
		{
			internal readonly int PickerId;

			internal readonly Queue<GameObject> Cards = new Queue<GameObject>();

			internal readonly HashSet<GameObject> QueuedCards = new HashSet<GameObject>();

			internal bool Running;

			internal bool PickerShown;

			internal RevealState(int pickerId)
			{
				PickerId = pickerId;
			}
		}

		private const float RevealInterval = 0.25f;

		private static readonly Dictionary<CardChoice, RevealState> States = new Dictionary<CardChoice, RevealState>();

		private static void Postfix(CardChoice __instance, List<GameObject> ___spawnedCards, int ___pickrID)
		{
			if (___pickrID < 0)
			{
				return;
			}
			if (!States.TryGetValue(__instance, out var value) || value.PickerId != ___pickrID)
			{
				value = new RevealState(___pickrID);
				States[__instance] = value;
			}
			if (!value.PickerShown && Object.op_Implicit((Object)(object)CardChoiceVisuals.instance))
			{
				CardChoiceVisuals.instance.Show(___pickrID, true);
				value.PickerShown = true;
			}
			foreach (GameObject ___spawnedCard in ___spawnedCards)
			{
				if (Object.op_Implicit((Object)(object)___spawnedCard) && value.QueuedCards.Add(___spawnedCard))
				{
					value.Cards.Enqueue(___spawnedCard);
				}
			}
			if (!value.Running)
			{
				value.Running = true;
				((MonoBehaviour)__instance).StartCoroutine(RevealQueuedCards(value));
			}
		}

		private static IEnumerator RevealQueuedCards(RevealState state)
		{
			yield return (object)new WaitForSecondsRealtime(0.25f);
			while (state.Cards.Count > 0)
			{
				GameObject card = state.Cards.Dequeue();
				if (Object.op_Implicit((Object)(object)card))
				{
					yield return null;
					CardVisuals componentInChildren = card.GetComponentInChildren<CardVisuals>();
					if (Object.op_Implicit((Object)(object)componentInChildren))
					{
						componentInChildren.ChangeSelected(true);
						PhotonView component = card.GetComponent<PhotonView>();
						if (Object.op_Implicit((Object)(object)component) && component.IsMine)
						{
							CardRevealSync.BroadcastCard(component.ViewID);
						}
					}
				}
				yield return (object)new WaitForSecondsRealtime(0.25f);
			}
			state.Running = false;
		}
	}
	internal static class HostModeSync
	{
		[UnboundRPC]
		internal static void RequestSettings()
		{
			if (PhotonNetwork.IsMasterClient)
			{
				NetworkingManager.RPC_Others(typeof(HostModeSync), "ReceiveSettings", new object[2]
				{
					(int)Plugin.EffectiveMode,
					Plugin.EffectiveCardsPerRound
				});
			}
		}

		[UnboundRPC]
		internal static void ReceiveSettings(int mode, int cardsPerRound)
		{
			Plugin.ApplyHostSettings(mode, cardsPerRound);
		}
	}
	internal static class CardRevealSync
	{
		internal static void BroadcastCard(int viewId)
		{
			NetworkingManager.RPC(typeof(CardRevealSync), "RevealCard", new object[1] { viewId });
		}

		[UnboundRPC]
		internal static void RevealCard(int viewId)
		{
			PhotonView photonView = PhotonNetwork.GetPhotonView(viewId);
			if (Object.op_Implicit((Object)(object)photonView))
			{
				((MonoBehaviour)CardChoice.instance).StartCoroutine(RevealWhenReady(((Component)photonView).gameObject));
			}
		}

		private static IEnumerator RevealWhenReady(GameObject card)
		{
			for (int frame = 0; frame < 60; frame++)
			{
				if (!Object.op_Implicit((Object)(object)card))
				{
					break;
				}
				CardVisuals componentInChildren = card.GetComponentInChildren<CardVisuals>();
				if (Object.op_Implicit((Object)(object)componentInChildren))
				{
					componentInChildren.ChangeSelected(true);
					break;
				}
				yield return null;
			}
		}
	}
}