Decompiled source of Reshuffle v1.0.1

ReshuffleMod.dll

Decompiled 2 days 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnboundLib;
using UnboundLib.GameModes;
using UnityEngine;

[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("ReshuffleMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ReshuffleMod")]
[assembly: AssemblyTitle("ReshuffleMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ReshuffleMod
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.hugo.rounds.reshuffle", "Reshuffle", "1.0.0")]
	[BepInProcess("Rounds.exe")]
	public class ReshufflePlugin : BaseUnityPlugin
	{
		public const string ModId = "com.hugo.rounds.reshuffle";

		public const string ModName = "Reshuffle";

		public const string Version = "1.0.0";

		internal static ConfigEntry<KeyCode> ReshuffleKey;

		internal static ConfigEntry<int> MaxReshufflesPerPick;

		internal static ConfigEntry<bool> Unlimited;

		private int reshufflesUsedThisPick;

		private bool reshuffling;

		public static ReshufflePlugin Instance { get; private set; }

		private void Awake()
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			ReshuffleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "ReshuffleKey", (KeyCode)114, "Key to refresh the cards offered during your pick.");
			MaxReshufflesPerPick = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxReshufflesPerPick", 3, "How many times you can reshuffle during a single pick. Ignored if Unlimited is on.");
			Unlimited = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Unlimited", false, "Allow unlimited reshuffles each pick.");
			new Harmony("com.hugo.rounds.reshuffle").PatchAll();
		}

		private void Start()
		{
			GameModeManager.AddHook("PlayerPickStart", (Func<IGameModeHandler, IEnumerator>)OnPickStart);
			GameModeManager.AddHook("PlayerPickEnd", (Func<IGameModeHandler, IEnumerator>)OnPickEnd);
			Unbound.RegisterCredits("Reshuffle", new string[1] { "Hugo" }, "Reshuffle pick cards", "");
		}

		private void Update()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			if (Input.GetKeyDown(ReshuffleKey.Value))
			{
				TryReshuffle();
			}
		}

		internal void ResetPickState()
		{
			reshufflesUsedThisPick = 0;
			reshuffling = false;
		}

		private IEnumerator OnPickStart(IGameModeHandler gm)
		{
			ResetPickState();
			yield break;
		}

		private IEnumerator OnPickEnd(IGameModeHandler gm)
		{
			ResetPickState();
			yield break;
		}

		private void TryReshuffle()
		{
			CardChoice instance = CardChoice.instance;
			if ((Object)(object)instance == (Object)null || !instance.IsPicking || reshuffling)
			{
				return;
			}
			Traverse val = Traverse.Create((object)instance);
			if (val.Field("isPlaying").GetValue<bool>())
			{
				return;
			}
			if (!Unlimited.Value && reshufflesUsedThisPick >= MaxReshufflesPerPick.Value)
			{
				Unbound.BuildInfoPopup($"No reshuffles left ({MaxReshufflesPerPick.Value} max)");
				return;
			}
			Player picker = GetPicker(instance, val);
			if (!((Object)(object)picker == (Object)null) && IsLocalPicker(picker))
			{
				if (HasNonRefreshableOffer(val))
				{
					Unbound.BuildInfoPopup("This hand can't be reshuffled");
				}
				else
				{
					((MonoBehaviour)this).StartCoroutine(DoReshuffle(instance));
				}
			}
		}

		private static bool HasNonRefreshableOffer(Traverse choiceTraverse)
		{
			List<GameObject> value = choiceTraverse.Field("spawnedCards").GetValue<List<GameObject>>();
			if (value == null)
			{
				return false;
			}
			foreach (GameObject item in value)
			{
				if (!((Object)(object)item == (Object)null))
				{
					if ((Object)(object)item.GetComponent("NonRefreshableCard") != (Object)null)
					{
						return true;
					}
					CardInfo component = item.GetComponent<CardInfo>();
					if ((Object)(object)component != (Object)null && component.cardName == "Damnation")
					{
						return true;
					}
				}
			}
			return false;
		}

		private IEnumerator DoReshuffle(CardChoice choice)
		{
			reshuffling = true;
			reshufflesUsedThisPick++;
			choice.picks++;
			Traverse val = Traverse.Create((object)choice).Method("ReplaceCards", new object[2] { null, true });
			yield return ((MonoBehaviour)choice).StartCoroutine(val.GetValue<IEnumerator>());
			if (Unlimited.Value)
			{
				Unbound.BuildInfoPopup("Cards reshuffled");
			}
			else
			{
				Unbound.BuildInfoPopup($"Cards reshuffled ({MaxReshufflesPerPick.Value - reshufflesUsedThisPick} left)");
			}
			reshuffling = false;
		}

		private static Player GetPicker(CardChoice choice, Traverse choiceTraverse)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			if ((int)choiceTraverse.Field("pickerType").GetValue<PickerType>() == 0)
			{
				Player[] playersInTeam = PlayerManager.instance.GetPlayersInTeam(choice.pickrID);
				if (playersInTeam == null || playersInTeam.Length == 0)
				{
					return null;
				}
				return playersInTeam[0];
			}
			if (choice.pickrID < 0 || choice.pickrID >= PlayerManager.instance.players.Count)
			{
				return null;
			}
			return PlayerManager.instance.players[choice.pickrID];
		}

		private static bool IsLocalPicker(Player picker)
		{
			if ((Object)(object)picker?.data == (Object)null)
			{
				return false;
			}
			object value = Traverse.Create((object)picker.data).Field("view").GetValue();
			if (value == null)
			{
				return false;
			}
			return Traverse.Create(value).Property("IsMine", (object[])null).GetValue<bool>();
		}
	}
	[HarmonyPatch(typeof(CardChoice), "StartPick")]
	internal static class StartPickPatch
	{
		private static void Postfix()
		{
			ReshufflePlugin.Instance?.ResetPickState();
		}
	}
}