Decompiled source of BanishCards v1.0.1

BanishCards.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("BanishCards")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c4042332252518a7ffc070ae7680aa47b0a2e04f")]
[assembly: AssemblyProduct("BanishCards")]
[assembly: AssemblyTitle("BanishCards")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BanishCards;

public class BanishButtonControl : MonoBehaviour
{
	private Button button;

	private Image buttonImage;

	private Text buttonText;

	private void Awake()
	{
		button = ((Component)this).GetComponent<Button>();
		buttonImage = ((Component)this).GetComponent<Image>();
		buttonText = ((Component)this).GetComponentInChildren<Text>();
	}

	private void OnEnable()
	{
		UpdateState();
	}

	public void UpdateState()
	{
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		int value = Plugin.Instance.MaxBanishesConfig.Value;
		int banishesThisRun = Plugin.Instance.BanishesThisRun;
		FieldInfo fieldInfo = AccessTools.Field(typeof(CardManager), "cards");
		UpgradeCard[] array = (UpgradeCard[])fieldInfo.GetValue(CardManager.instance);
		int num = 0;
		for (int i = 0; i < array.Length; i++)
		{
			if ((Object)(object)array[i] != (Object)null)
			{
				num++;
			}
		}
		int num2 = num - 1;
		int num3 = value - banishesThisRun;
		int num4 = Mathf.Min(num3, num2);
		bool flag = banishesThisRun >= value;
		bool flag2 = num <= 1 && !flag;
		((Selectable)button).interactable = !(flag || flag2);
		if ((Object)(object)buttonImage != (Object)null)
		{
			Color color = ((Graphic)buttonImage).color;
			color.a = (flag ? 0f : (flag2 ? 0.3f : 1f));
			((Graphic)buttonImage).color = color;
		}
		if ((Object)(object)buttonText != (Object)null)
		{
			Color color2 = ((Graphic)buttonText).color;
			color2.a = (flag ? 0f : (flag2 ? 0.3f : 1f));
			((Graphic)buttonText).color = color2;
		}
		((Component)this).gameObject.SetActive(!flag);
	}
}
[HarmonyPatch(typeof(CardManager), "DisplayCards")]
internal class Patch_DisplayCards
{
	public static class UIRefreshHelper
	{
		public static void ForceRefreshCanvas()
		{
			GameObject val = GameObject.Find("CardUI/Canvas");
			if ((Object)(object)val != (Object)null)
			{
				val.SetActive(false);
				val.SetActive(true);
			}
		}
	}

	private static void Postfix(CardManager __instance, int count)
	{
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_0177: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_029c: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a6: Expected O, but got Unknown
		//IL_023d: Unknown result type (might be due to invalid IL or missing references)
		//IL_024c: Unknown result type (might be due to invalid IL or missing references)
		FieldInfo fieldInfo = AccessTools.Field(typeof(CardManager), "cardHolders");
		GameObject[] array = (GameObject[])fieldInfo.GetValue(__instance);
		for (int i = 0; i < count; i++)
		{
			try
			{
				Transform transform = array[i].transform;
				Transform val = transform.Find($"Highlight{i}") ?? transform.Find("Highlight");
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				Transform val2 = val.Find("SelectButton");
				if ((Object)(object)val2 == (Object)null)
				{
					continue;
				}
				Button component = ((Component)val2).GetComponent<Button>();
				if (!((Object)(object)component == (Object)null))
				{
					GameObject val3 = Object.Instantiate<GameObject>(((Component)component).gameObject, val);
					((Object)val3).name = "BanishButton";
					val3.transform.localScale = Vector3.one;
					val3.transform.localPosition = new Vector3(0f, 135f, 0f);
					val3.transform.SetAsLastSibling();
					Button component2 = val3.GetComponent<Button>();
					if ((Object)(object)component2 != (Object)null)
					{
						Object.DestroyImmediate((Object)(object)component2);
					}
					Button val4 = val3.AddComponent<Button>();
					((Selectable)val4).transition = ((Selectable)component).transition;
					((Selectable)val4).colors = ((Selectable)component).colors;
					Navigation navigation = default(Navigation);
					((Navigation)(ref navigation)).mode = (Mode)0;
					((Selectable)val4).navigation = navigation;
					Image component3 = val3.GetComponent<Image>();
					Image component4 = ((Component)component).GetComponent<Image>();
					component3.sprite = component4.sprite;
					component3.type = component4.type;
					((Graphic)component3).color = ((Graphic)component4).color;
					((Graphic)component3).raycastTarget = true;
					((Selectable)val4).targetGraphic = (Graphic)(object)component3;
					Text componentInChildren = val3.GetComponentInChildren<Text>();
					Text componentInChildren2 = ((Component)component).GetComponentInChildren<Text>();
					if ((Object)(object)componentInChildren != (Object)null && (Object)(object)componentInChildren2 != (Object)null)
					{
						componentInChildren.text = "Banish";
						componentInChildren.font = componentInChildren2.font;
						componentInChildren.fontSize = componentInChildren2.fontSize;
						((Graphic)componentInChildren).color = ((Graphic)componentInChildren2).color;
						componentInChildren.alignment = componentInChildren2.alignment;
						componentInChildren.resizeTextForBestFit = componentInChildren2.resizeTextForBestFit;
						componentInChildren.resizeTextMinSize = componentInChildren2.resizeTextMinSize;
						componentInChildren.resizeTextMaxSize = componentInChildren2.resizeTextMaxSize;
					}
					int capturedIndex = i;
					((UnityEvent)val4.onClick).AddListener((UnityAction)delegate
					{
						BanishCard(__instance, capturedIndex);
					});
					val3.AddComponent<BanishButtonControl>();
					val3.SetActive(true);
				}
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Error creating banish button for card {i}: {arg}");
			}
		}
	}

	private static void BanishCard(CardManager cardManager, int index)
	{
		FieldInfo fieldInfo = AccessTools.Field(typeof(CardManager), "cards");
		UpgradeCard[] array = (UpgradeCard[])fieldInfo.GetValue(cardManager);
		int num = 0;
		for (int i = 0; i < array.Length; i++)
		{
			if ((Object)(object)array[i] != (Object)null)
			{
				num++;
			}
		}
		int value = Plugin.Instance.MaxBanishesConfig.Value;
		int num2 = value - Plugin.Instance.BanishesThisRun;
		int num3 = num - 1;
		int num4 = Mathf.Min(num2, num3);
		if (num4 <= 0)
		{
			return;
		}
		FieldInfo fieldInfo2 = AccessTools.Field(typeof(CardManager), "availableCards");
		List<UpgradeCard> list = (List<UpgradeCard>)fieldInfo2.GetValue(cardManager);
		FieldInfo fieldInfo3 = AccessTools.Field(typeof(CardManager), "cardHolders");
		GameObject[] array2 = (GameObject[])fieldInfo3.GetValue(cardManager);
		if (array == null || index >= array.Length)
		{
			return;
		}
		UpgradeCard val = array[index];
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Plugin.Instance.BanishesThisRun++;
		if (list.Contains(val))
		{
			list.Remove(val);
		}
		int num5 = 0;
		for (int j = 0; j < array.Length; j++)
		{
			if ((Object)(object)array[j] != (Object)null)
			{
				num5++;
			}
		}
		for (int k = index; k < num5 - 1; k++)
		{
			array[k] = array[k + 1];
		}
		array[num5 - 1] = null;
		array2[num5 - 1].SetActive(false);
		FieldInfo fieldInfo4 = AccessTools.Field(typeof(CardManager), "titles");
		FieldInfo fieldInfo5 = AccessTools.Field(typeof(CardManager), "images");
		FieldInfo fieldInfo6 = AccessTools.Field(typeof(CardManager), "descriptions");
		Text[] array3 = (Text[])fieldInfo4.GetValue(cardManager);
		Image[] array4 = (Image[])fieldInfo5.GetValue(cardManager);
		Text[] array5 = (Text[])fieldInfo6.GetValue(cardManager);
		for (int l = index; l < num5 - 1; l++)
		{
			if ((Object)(object)array[l] != (Object)null)
			{
				array3[l].text = array[l].title;
				array4[l].sprite = array[l].image;
				array5[l].text = array[l].description;
			}
		}
		array3[num5 - 1].text = "";
		array4[num5 - 1].sprite = null;
		array5[num5 - 1].text = "";
		UIRefreshHelper.ForceRefreshCanvas();
	}
}
[BepInPlugin("AgusBut.BanishCards", "BanishCards", "1.0.1")]
public class Plugin : BaseUnityPlugin
{
	internal int BanishesThisRun = 0;

	public static Plugin Instance { get; private set; }

	public static ManualLogSource Log { get; private set; }

	public ConfigEntry<int> MaxBanishesConfig { get; private set; }

	private void Awake()
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Loading [BanishCards 1.0.1]");
		MaxBanishesConfig = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxBanishes", 3, "Maximum number of cards you can banish per run.");
		SceneManager.sceneLoaded += OnSceneLoaded;
		Harmony val = new Harmony("AgusBut.BanishCards");
		val.PatchAll();
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		if (((Scene)(ref scene)).name == "GameScene")
		{
			((BaseUnityPlugin)this).Logger.LogDebug((object)"New run detected. Resetting banish counter.");
			((BaseUnityPlugin)this).Logger.LogDebug((object)$"[BanishCards] Loaded MaxBanishes from config: {MaxBanishesConfig.Value}");
			BanishesThisRun = 0;
		}
	}
}