Decompiled source of AmayaWithoutCooldown v0.1.3

plugins/AmayaWithoutCooldown.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using HarmonyLib;
using SuperFantasyKingdom;
using SuperFantasyKingdom.Clickable;
using SuperFantasyKingdom.UI;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[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("AmayaWithoutCooldown")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.3.0")]
[assembly: AssemblyInformationalVersion("0.1.3+7da0f4e41dc9e201e7fc4baa1d55c82273bff014")]
[assembly: AssemblyProduct("AmayaWithoutCooldown")]
[assembly: AssemblyTitle("AmayaWithoutCooldown")]
[assembly: AssemblyVersion("0.1.3.0")]
namespace AmayaWithoutCooldown;

internal sealed class AmayaShopView : MonoBehaviour
{
	internal static readonly FieldRef<OverlayManager, UIOverlayBase> CurrentOverlay = AccessTools.FieldRefAccess<OverlayManager, UIOverlayBase>("m_CurrentlyOpen");

	private static readonly FieldRef<OverlayManager, UIRelicDetails> Details = AccessTools.FieldRefAccess<OverlayManager, UIRelicDetails>("relicDetails");

	private UIOverlayMerchant merchant;

	private Transform buys;

	private Button reroll;

	private Button cancel;

	private CanvasGroup rerollGroup;

	private Color originalRerollDisabledColor;

	private const float NativeDimAlpha = 0.6f;

	private const float ReadableDimAlpha = 0.9f;

	private RectTransform header;

	private TMP_Text title;

	private Transform resources;

	private RectTransform strip;

	private CanvasGroup stripGroup;

	private float offset;

	private readonly Vector3[] corners = (Vector3[])(object)new Vector3[4];

	internal static void ShowPurchasedIcon(GameObject card, string identifier)
	{
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: 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_00d2: 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_00d9: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)card == (Object)null || string.IsNullOrEmpty(identifier))
		{
			return;
		}
		Relic relic = AddressablesManager.Instance.GetRelic(identifier);
		if (!((Object)(object)relic == (Object)null) && !(((Item)relic).GetIdentifier() != identifier))
		{
			Sprite icon = ((Item)relic).GetIcon();
			if (!((Object)(object)icon == (Object)null))
			{
				Image component = new GameObject("Purchased relic", new Type[2]
				{
					typeof(RectTransform),
					typeof(Image)
				}).GetComponent<Image>();
				((Component)component).transform.SetParent(card.transform, false);
				component.sprite = icon;
				component.preserveAspect = true;
				((Graphic)component).raycastTarget = false;
				RectTransform rectTransform = ((Graphic)component).rectTransform;
				rectTransform.anchorMin = new Vector2(0.34f, 0.63f);
				rectTransform.anchorMax = new Vector2(0.66f, 0.85f);
				Vector2 offsetMin = (rectTransform.offsetMax = Vector2.zero);
				rectTransform.offsetMin = offsetMin;
			}
		}
	}

	internal void Open()
	{
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		merchant = ((Component)this).GetComponent<UIOverlayMerchant>();
		buys = ((Component)this).transform.Find("Panel/BuyButtons");
		Transform obj = ((Component)this).transform.Find("Panel/Buttons/Reroll/Button");
		reroll = ((obj != null) ? ((Component)obj).GetComponent<Button>() : null);
		Transform obj2 = ((Component)this).transform.Find("Panel/Buttons/Cancel/Button");
		cancel = ((obj2 != null) ? ((Component)obj2).GetComponent<Button>() : null);
		ref RectTransform reference = ref header;
		Transform obj3 = ((Component)this).transform.Find("HeaderContainer");
		reference = (RectTransform)(object)((obj3 is RectTransform) ? obj3 : null);
		RectTransform obj4 = header;
		object obj5;
		if (obj4 == null)
		{
			obj5 = null;
		}
		else
		{
			Transform obj6 = ((Transform)obj4).Find("Header");
			obj5 = ((obj6 != null) ? ((Component)obj6).GetComponent<TMP_Text>() : null);
		}
		title = (TMP_Text)obj5;
		if (Chainloader.PluginInfos.ContainsKey("sfk.daybook"))
		{
			UIHeader obj7 = Object.FindObjectOfType<UIHeader>();
			resources = ((obj7 != null) ? ((Component)obj7).transform.Find("PanelResources") : null);
		}
		Button obj8 = reroll;
		rerollGroup = ((obj8 != null) ? ((Component)obj8).GetComponent<CanvasGroup>() : null);
		if ((Object)(object)reroll != (Object)null)
		{
			ColorBlock colors = ((Selectable)reroll).colors;
			originalRerollDisabledColor = ((ColorBlock)(ref colors)).disabledColor;
		}
		((Behaviour)this).enabled = true;
	}

	internal static void ReadablePrice(Button button, CanvasGroup group)
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)button == (Object)null) && !((Object)(object)group == (Object)null) && Mathf.Approximately(group.alpha, 0.6f) && !((Selectable)button).IsInteractable())
		{
			ColorBlock colors = ((Selectable)button).colors;
			Color disabledColor = ((ColorBlock)(ref colors)).disabledColor;
			disabledColor.a = 1f;
			((ColorBlock)(ref colors)).disabledColor = disabledColor;
			((Selectable)button).colors = colors;
			group.alpha = 0.9f;
		}
	}

	private void LateUpdate()
	{
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Expected O, but got Unknown
		if ((Object)(object)CurrentOverlay.Invoke(OverlayManager.Instance) != (Object)(object)merchant || Details.Invoke(OverlayManager.Instance).IsActive())
		{
			return;
		}
		ClearLedger();
		if ((Object)(object)reroll != (Object)null)
		{
			ReadablePrice(reroll, rerollGroup);
		}
		GamepadManager instance = GamepadManager.Instance;
		EventSystem current = EventSystem.current;
		if ((Object)(object)current == (Object)null || !instance.IsGamepadInUse())
		{
			return;
		}
		PauseMenuManger instance2 = PauseMenuManger.Instance;
		if ((instance2 != null && instance2.IsOpen()) || Usable(current.currentSelectedGameObject))
		{
			return;
		}
		GameObject val = instance.GetGameObjectToSelect();
		if (!Usable(val))
		{
			val = null;
			if ((Object)(object)buys != (Object)null)
			{
				foreach (Transform buy in buys)
				{
					Transform val2 = buy;
					if (Usable(((Component)val2).gameObject))
					{
						val = ((Component)val2).gameObject;
						break;
					}
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				Button obj = reroll;
				if (Usable((obj != null) ? ((Component)obj).gameObject : null))
				{
					val = ((Component)reroll).gameObject;
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				Button obj2 = cancel;
				if (Usable((obj2 != null) ? ((Component)obj2).gameObject : null))
				{
					val = ((Component)cancel).gameObject;
				}
			}
		}
		if ((Object)(object)val != (Object)null)
		{
			instance.SetGameObjectToSelect(val, true);
		}
	}

	private bool Usable(GameObject obj)
	{
		if ((Object)(object)obj != (Object)null && obj.activeInHierarchy && obj.transform.IsChildOf(((Component)this).transform))
		{
			Button component = obj.GetComponent<Button>();
			if (component != null && ((Behaviour)component).isActiveAndEnabled)
			{
				return ((Selectable)component).IsInteractable();
			}
		}
		return false;
	}

	internal bool CanBackOut()
	{
		if (((Behaviour)this).isActiveAndEnabled && !Details.Invoke(OverlayManager.Instance).IsActive())
		{
			Button obj = cancel;
			return Usable((obj != null) ? ((Component)obj).gameObject : null);
		}
		return false;
	}

	private void ClearLedger()
	{
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_0171: Unknown result type (might be due to invalid IL or missing references)
		//IL_017e: Unknown result type (might be due to invalid IL or missing references)
		//IL_018c: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)resources == (Object)null || (Object)(object)title == (Object)null || !((Behaviour)title).isActiveAndEnabled)
		{
			return;
		}
		if ((Object)(object)strip == (Object)null)
		{
			ref RectTransform reference = ref strip;
			Transform obj = resources.Find("Daybook/Daily changes");
			reference = (RectTransform)(object)((obj is RectTransform) ? obj : null);
			RectTransform obj2 = strip;
			stripGroup = ((obj2 != null) ? ((Component)obj2).GetComponentInParent<CanvasGroup>() : null);
		}
		float num = 0f;
		if ((Object)(object)strip != (Object)null && ((Component)strip).gameObject.activeInHierarchy && ((Object)(object)stripGroup == (Object)null || stripGroup.alpha > 0f))
		{
			Transform parent = ((Transform)header).parent;
			strip.GetWorldCorners(corners);
			Vector3 val = parent.InverseTransformPoint(corners[0]);
			Vector3 val2 = parent.InverseTransformPoint(corners[2]);
			Bounds textBounds = title.textBounds;
			Vector3 val3 = parent.InverseTransformPoint(title.transform.TransformPoint(((Bounds)(ref textBounds)).max));
			Vector3 val4 = parent.InverseTransformPoint(title.transform.TransformPoint(((Bounds)(ref textBounds)).min));
			if (val4.x < val2.x && val3.x > val.x && val4.y + offset < val2.y)
			{
				num = Mathf.Max(0f, val3.y + offset - val.y + 4f);
			}
		}
		SetOffset(num);
	}

	private void SetOffset(float value)
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)header != (Object)null && Mathf.Abs(value - offset) > 0.01f)
		{
			RectTransform obj = header;
			obj.anchoredPosition += Vector2.up * (offset - value);
		}
		offset = value;
	}

	private void OnDisable()
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: 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_0039: Unknown result type (might be due to invalid IL or missing references)
		SetOffset(0f);
		if (!((Object)(object)reroll == (Object)null))
		{
			ColorBlock colors = ((Selectable)reroll).colors;
			((ColorBlock)(ref colors)).disabledColor = originalRerollDisabledColor;
			((Selectable)reroll).colors = colors;
			if ((Object)(object)rerollGroup != (Object)null && Mathf.Approximately(rerollGroup.alpha, 0.9f))
			{
				rerollGroup.alpha = 0.6f;
			}
		}
	}
}
internal sealed class OfferStock
{
	private const int DayTag = 1096237825;

	private const int SeedTag = 1096237826;

	private const int Tag = 1096237827;

	private const int NativeFields = 5;

	private int key;

	private int rerolls;

	private string[] offers;

	private readonly HashSet<int> sold = new HashSet<int>();

	private readonly HashSet<int> supplied = new HashSet<int>();

	private string supplier = "";

	internal int View { get; private set; }

	internal bool IsCurrent(int stockKey, int currentRerolls)
	{
		if (key == stockKey)
		{
			return rerolls == currentRerolls;
		}
		return false;
	}

	internal void LoadedCounter(int current)
	{
		if (offers != null && current == 0 && rerolls > 0)
		{
			rerolls = 0;
		}
	}

	internal bool Matches(int slot, string id)
	{
		if (offers != null && slot >= 0 && slot < offers.Length && offers[slot] == id)
		{
			return !IsSold(slot);
		}
		return false;
	}

	internal bool SelectStock(int stockKey, int currentRerolls, bool refresh = false)
	{
		View++;
		if (refresh || !IsCurrent(stockKey, currentRerolls))
		{
			key = stockKey;
			rerolls = currentRerolls;
			sold.Clear();
			supplied.Clear();
			supplier = "";
			offers = null;
		}
		return offers != null;
	}

	internal void Remember(List<string> chosen)
	{
		offers = chosen.ToArray();
	}

	internal void RestoreOffers(List<string> chosen)
	{
		chosen.Clear();
		chosen.AddRange(offers);
	}

	internal void RecordSupplier(int slot, string provider)
	{
		if (offers == null && slot >= 0 && !string.IsNullOrEmpty(provider))
		{
			supplied.Add(slot);
			supplier = provider;
		}
	}

	internal bool SuppliedBy(int slot, string provider)
	{
		if (supplied.Contains(slot))
		{
			return supplier == provider;
		}
		return false;
	}

	internal bool IsSold(int slot)
	{
		return sold.Contains(slot);
	}

	internal bool MarkSold(int slot)
	{
		if (offers != null && slot >= 0 && slot < offers.Length)
		{
			return sold.Add(slot);
		}
		return false;
	}

	internal void Write(List<int> data)
	{
		if (data.Count < 5)
		{
			return;
		}
		for (int i = 0; i < 3; i++)
		{
			data[2 + i] = (IsSold(i) ? 1 : 0);
		}
		if (offers != null)
		{
			data.Add(1096237827);
			data.Add(key);
			data.Add(rerolls);
			data.Add(offers.Length);
			WriteString(data, supplier);
			for (int j = 0; j < offers.Length; j++)
			{
				data.Add((IsSold(j) ? 1 : 0) | (supplied.Contains(j) ? 2 : 0));
				WriteString(data, offers[j]);
			}
		}
	}

	private static void WriteString(List<int> data, string value)
	{
		data.Add(value.Length);
		foreach (char item in value)
		{
			data.Add(item);
		}
	}

	private static bool ReadString(List<int> data, ref int offset, out string value)
	{
		value = null;
		if (offset >= data.Count)
		{
			return false;
		}
		int num = data[offset++];
		if (num < 0 || num > data.Count - offset)
		{
			return false;
		}
		char[] array = new char[num];
		for (int i = 0; i < num; i++)
		{
			int num2 = data[offset++];
			if (num2 < 0 || num2 > 65535)
			{
				return false;
			}
			array[i] = (char)num2;
		}
		value = new string(array);
		return true;
	}

	internal static OfferStock Read(List<int> data, int stockKey)
	{
		OfferStock offerStock = new OfferStock
		{
			key = stockKey
		};
		if (data.Count < 5)
		{
			return offerStock;
		}
		offerStock.rerolls = data[0];
		for (int i = 0; i < 3; i++)
		{
			if (data[2 + i] > 0)
			{
				offerStock.sold.Add(i);
			}
		}
		int num = data.IndexOf(1096237827, 5);
		if (num < 0)
		{
			num = data.IndexOf(1096237826, 5);
		}
		if (num < 0)
		{
			num = data.IndexOf(1096237825, 5);
		}
		if (num < 0 || data.Count - num < 4)
		{
			return offerStock;
		}
		bool flag = data[num] == 1096237827;
		int num2 = data[num + 3];
		int offset = num + 4;
		OfferStock offerStock2 = new OfferStock
		{
			key = ((data[num] == 1096237825) ? (data[num + 1] * 31) : data[num + 1]),
			rerolls = data[num + 2]
		};
		if (flag && !ReadString(data, ref offset, out offerStock2.supplier))
		{
			return offerStock;
		}
		if (num2 < 0 || num2 > (data.Count - offset) / (flag ? 3 : 2))
		{
			return offerStock;
		}
		offerStock2.offers = new string[num2];
		if (!flag)
		{
			offerStock2.sold.UnionWith(offerStock.sold);
		}
		for (int j = 0; j < num2; j++)
		{
			if (flag)
			{
				if (offset >= data.Count)
				{
					return offerStock;
				}
				int num3 = data[offset++];
				if ((num3 & -4) != 0)
				{
					return offerStock;
				}
				if ((num3 & 1) != 0)
				{
					offerStock2.sold.Add(j);
				}
				if ((num3 & 2) != 0)
				{
					offerStock2.supplied.Add(j);
				}
			}
			if (!ReadString(data, ref offset, out var value) || value.Length == 0)
			{
				return offerStock;
			}
			offerStock2.offers[j] = value;
		}
		return offerStock2;
	}
}
[BepInPlugin("gkj.sfk.amayawithoutcooldown", "Amaya Without Cooldown", "0.1.3")]
public sealed class Plugin : BaseUnityPlugin
{
	private const string Id = "gkj.sfk.amayawithoutcooldown";

	private Harmony patches;

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		patches = new Harmony("gkj.sfk.amayawithoutcooldown");
		try
		{
			patches.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Amaya Without Cooldown is ready.");
		}
		catch
		{
			patches.UnpatchSelf();
			throw;
		}
	}

	private void OnDestroy()
	{
		Harmony obj = patches;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}
}
[HarmonyPatch]
internal sealed class PurchaseGuard
{
	private static readonly ConditionalWeakTable<UICard, PurchaseGuard> Cards = new ConditionalWeakTable<UICard, PurchaseGuard>();

	private static readonly FieldRef<UICardRelic, string> CardIdentifier = AccessTools.FieldRefAccess<UICardRelic, string>("m_RelicIdentifier");

	private static readonly FieldRef<UICard, bool> Clickable = AccessTools.FieldRefAccess<UICard, bool>("m_CanBeClicked");

	private readonly BuildingAmayaShop shop;

	private readonly UIOverlayMerchant merchant;

	private readonly OfferStock stock;

	private readonly int view;

	private readonly int slot;

	private readonly string identifier;

	private readonly Relic relic;

	private readonly ResourceAmount[] costs;

	private readonly ResourceAmount[] originalQuote;

	private static readonly FieldRef<UIOverlayMerchant, Func<Relic, ResourceAmount[]>> Quote = AccessTools.FieldRefAccess<UIOverlayMerchant, Func<Relic, ResourceAmount[]>>("m_GetRelicPriceCallback");

	private bool consumed;

	private PurchaseGuard(BuildingAmayaShop shop, UIOverlayMerchant merchant, OfferStock stock, int slot, Relic relic, ResourceAmount[] costs, ResourceAmount[] originalQuote)
	{
		this.shop = shop;
		this.merchant = merchant;
		this.stock = stock;
		view = stock.View;
		this.slot = slot;
		this.relic = relic;
		identifier = ((Item)relic).GetIdentifier();
		this.costs = costs;
		this.originalQuote = originalQuote;
	}

	[HarmonyPrefix]
	[HarmonyPriority(800)]
	[HarmonyPatch(typeof(UIOverlayMerchant), "AddRelic")]
	private static void CaptureQuote(UIOverlayMerchant __instance, ResourceAmount[] costs, out ResourceAmount[] __state)
	{
		__state = (ShopPatches.Owners.TryGetValue(__instance, out var _) ? ((ResourceAmount[])costs?.Clone()) : null);
	}

	[HarmonyPostfix]
	[HarmonyPriority(0)]
	[HarmonyPatch(typeof(UIOverlayMerchant), "AddRelic")]
	private static void Attach(UIOverlayMerchant __instance, GameObject __result, int id, Relic relic, ResourceAmount[] costs, ResourceAmount[] __state)
	{
		if (ShopPatches.Owners.TryGetValue(__instance, out var value) && !((Object)(object)__result == (Object)null) && ShopPatches.Stocks.TryGetValue(value, out var value2))
		{
			UICardRelic component = __result.GetComponent<UICardRelic>();
			if (!((Object)(object)component == (Object)null))
			{
				Track(component, value, __instance, value2, id, relic, costs, __state);
			}
		}
	}

	internal static void Track(UICardRelic card, BuildingAmayaShop owner, UIOverlayMerchant merchant, OfferStock stock, int id, Relic relic, ResourceAmount[] costs, ResourceAmount[] originalQuote)
	{
		Cards.Remove((UICard)(object)card);
		Cards.Add((UICard)(object)card, new PurchaseGuard(owner, merchant, stock, id, relic, costs, originalQuote));
	}

	private bool SameView()
	{
		if ((Object)(object)merchant != (Object)null && (Object)(object)shop != (Object)null && stock.View == view && ShopPatches.Owners.TryGetValue(merchant, out var value) && value == shop && ShopPatches.Stocks.TryGetValue(shop, out var value2) && value2 == stock)
		{
			return ((Behaviour)merchant).isActiveAndEnabled;
		}
		return false;
	}

	private bool Available(UICardRelic card)
	{
		if (!consumed && SameView() && Clickable.Invoke((UICard)(object)card) && stock.Matches(slot, identifier) && CardIdentifier.Invoke(card) == identifier && stock.IsCurrent(ShopPatches.StockKey(shop), ShopPatches.RerollCount.Invoke((BuildingObjectShop)(object)shop)) && ShopPatches.Available(shop, merchant, stock, slot, identifier) == relic && QuoteMatches(Quote.Invoke(merchant)(relic)))
		{
			if (costs != null)
			{
				return ResourceManager.Instance.CanBeBuilt(costs);
			}
			return true;
		}
		return false;
	}

	private bool QuoteMatches(ResourceAmount[] current)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		if (current == null || originalQuote == null)
		{
			return current == originalQuote;
		}
		if (current.Length != originalQuote.Length)
		{
			return false;
		}
		for (int i = 0; i < current.Length; i++)
		{
			if (current[i].type != originalQuote[i].type || current[i].amount != originalQuote[i].amount)
			{
				return false;
			}
		}
		return true;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UIOverlayMerchant), "Select", new Type[] { typeof(UICard) })]
	private static bool BeforeAnimation(UICard __0)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		if (Cards.TryGetValue(__0, out var value))
		{
			return value.Available((UICardRelic)__0);
		}
		return true;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UICard), "Callback")]
	private static bool BeforeTransaction(UICard __instance)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Expected O, but got Unknown
		if (!Cards.TryGetValue(__instance, out var value))
		{
			return true;
		}
		if (!value.Available((UICardRelic)__instance))
		{
			if (!value.consumed && value.SameView())
			{
				value.merchant.Cancel();
			}
			return false;
		}
		value.consumed = true;
		return true;
	}
}
[HarmonyPatch]
internal static class ShopPatches
{
	internal static readonly ConditionalWeakTable<BuildingAmayaShop, OfferStock> Stocks = new ConditionalWeakTable<BuildingAmayaShop, OfferStock>();

	internal static readonly FieldRef<BuildingObjectShop, int> RerollCount = AccessTools.FieldRefAccess<BuildingObjectShop, int>("m_Rerolls");

	[ThreadStatic]
	private static BuildingAmayaShop opening;

	internal static readonly ConditionalWeakTable<UIOverlayMerchant, BuildingAmayaShop> Owners = new ConditionalWeakTable<UIOverlayMerchant, BuildingAmayaShop>();

	private static readonly FieldRef<BuildingObjectShop, List<string>> Pool = AccessTools.FieldRefAccess<BuildingObjectShop, List<string>>("m_Options");

	private static readonly MethodInfo Eligibility = AccessTools.Method(typeof(BuildingAmayaShop), "CanAddRelic", (Type[])null, (Type[])null);

	private static readonly MethodInfo Seed = AccessTools.Method(typeof(BuildingAmayaShop), "GetRandomSeedModifier", (Type[])null, (Type[])null);

	private static readonly FieldRef<UIOverlayMerchant, Func<Relic>> Generator = AccessTools.FieldRefAccess<UIOverlayMerchant, Func<Relic>>("m_GetRelicCallback");

	[HarmonyPrefix]
	[HarmonyPatch(typeof(BuildingAmayaShop), "OnSetData")]
	private static void Load(BuildingAmayaShop __instance, ref List<int> data)
	{
		Stocks.Remove(__instance);
		Stocks.Add(__instance, OfferStock.Read(data, StockKey(__instance)));
		if (data.Count > 1)
		{
			data = new List<int>(data);
			for (int i = 1; i < Math.Min(5, data.Count); i++)
			{
				data[i] = 0;
			}
			while (data.Count < 5)
			{
				data.Add(0);
			}
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(BuildingAmayaShop), "OnSetData")]
	private static void AfterLoad(BuildingAmayaShop __instance)
	{
		if (Stocks.TryGetValue(__instance, out var value))
		{
			value.LoadedCounter(RerollCount.Invoke((BuildingObjectShop)(object)__instance));
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(BuildingObjectShop), "SaveData")]
	private static void Save(BuildingObjectShop __instance, List<int> __result)
	{
		BuildingAmayaShop val = (BuildingAmayaShop)(object)((__instance is BuildingAmayaShop) ? __instance : null);
		if (val != null && Stocks.TryGetValue(val, out var value))
		{
			value.Write(__result);
		}
	}

	[HarmonyPrefix]
	[HarmonyPriority(800)]
	[HarmonyPatch(typeof(BuildingAmayaShop), "Merchant")]
	private static void Open(BuildingAmayaShop __instance, UIOverlayMerchant __0, out BuildingAmayaShop __state)
	{
		Owners.Remove(__0);
		Owners.Add(__0, __instance);
		__state = opening;
		opening = __instance;
	}

	[HarmonyFinalizer]
	[HarmonyPatch(typeof(BuildingAmayaShop), "Merchant")]
	private static void EndOpen(BuildingAmayaShop __state)
	{
		opening = __state;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(BuildingObjectShop), "GetDescription")]
	private static void ShopDescription(BuildingObjectShop __instance, ref string __result)
	{
		if (__instance is BuildingAmayaShop && __result?.Trim().Replace("\r\n", "\n").Replace(" \n", "\n") == "Make a deal with Amaya to get a powerful relic!\nStock changes daily. You can only get one per week though, and her price changes weekly as well.")
		{
			__result = "Buy a relic, then return for the remaining offers.\nStock changes daily. Prices change weekly.";
		}
	}

	internal static int StockKey(BuildingAmayaShop shop)
	{
		return (int)Seed.Invoke(shop, null);
	}

	private static string Supplier(UIOverlayMerchant merchant)
	{
		MethodInfo methodInfo = Generator.Invoke(merchant)?.Method;
		if (methodInfo?.DeclaringType == null)
		{
			return null;
		}
		string text = methodInfo.DeclaringType.Assembly.GetName().Name + "/" + methodInfo.DeclaringType.FullName + "/" + methodInfo.Name;
		Patches patchInfo = Harmony.GetPatchInfo((MethodBase)methodInfo);
		if (patchInfo != null)
		{
			return text + "/" + string.Join("/", patchInfo.Owners.OrderBy<string, string>((string owner) => owner, StringComparer.Ordinal));
		}
		return text;
	}

	internal static Relic Available(BuildingAmayaShop shop, UIOverlayMerchant merchant, OfferStock stock, int slot, string id)
	{
		if (string.IsNullOrEmpty(id) || (!Pool.Invoke((BuildingObjectShop)(object)shop).Contains(id) && !stock.SuppliedBy(slot, Supplier(merchant))))
		{
			return null;
		}
		Relic relic = AddressablesManager.Instance.GetRelic(id);
		if ((Object)(object)relic == (Object)null || ((Item)relic).GetIdentifier() != id)
		{
			return null;
		}
		if (!(bool)Eligibility.Invoke(shop, new object[1] { relic }) || !RelicManager.Instance.CanAddRelic(id))
		{
			return null;
		}
		return relic;
	}

	[HarmonyPrefix]
	[HarmonyPriority(0)]
	[HarmonyPatch(typeof(BuildingRelicShop), "GetRelic")]
	private static bool HasOptions(BuildingRelicShop __instance, List<string> ___m_Options)
	{
		if (__instance is BuildingAmayaShop)
		{
			return ___m_Options.Count != 0;
		}
		return true;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UIOverlayMerchant), "GenerateRelics")]
	private static bool BeforeCards(UIOverlayMerchant __instance, ref bool reroll, Func<Relic, ResourceAmount[]> ___m_GetRelicPriceCallback, List<string> ___m_ChosenRelics, out OfferStock __state)
	{
		__state = null;
		if (!Owners.TryGetValue(__instance, out var value))
		{
			return true;
		}
		OfferStock offerStock = (__state = Stocks.GetValue(value, (BuildingAmayaShop _) => new OfferStock()));
		bool refresh = reroll && opening != value;
		if (!offerStock.SelectStock(StockKey(value), RerollCount.Invoke((BuildingObjectShop)(object)value), refresh))
		{
			reroll = true;
			return true;
		}
		offerStock.RestoreOffers(___m_ChosenRelics);
		reroll = false;
		for (int num = 0; num < ___m_ChosenRelics.Count; num++)
		{
			string text = ___m_ChosenRelics[num];
			if (offerStock.IsSold(num))
			{
				AmayaShopView.ShowPurchasedIcon(__instance.AddSold(-1, false), text);
				continue;
			}
			Relic val = Available(value, __instance, offerStock, num, text);
			if ((Object)(object)val == (Object)null)
			{
				__instance.AddSold(-1, false);
			}
			else
			{
				__instance.AddRelic(val, ___m_GetRelicPriceCallback(val), num, true, true);
			}
		}
		return false;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIOverlayMerchant), "GenerateRelics")]
	private static void AfterCards(OfferStock __state, List<string> ___m_ChosenRelics)
	{
		__state?.Remember(___m_ChosenRelics);
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIOverlayMerchant), "AddBuyButton")]
	private static void PriceContrast(UIOverlayMerchant __instance, Transform __result)
	{
		if (Owners.TryGetValue(__instance, out var _) && (Object)(object)__result != (Object)null)
		{
			AmayaShopView.ReadablePrice(((Component)__result).GetComponent<Button>(), ((Component)__result).GetComponent<CanvasGroup>());
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UIOverlayMerchant), "AddRelic")]
	private static bool BindSlot(UIOverlayMerchant __instance, Relic relic, ref int id, List<string> ___m_ChosenRelics, ref GameObject __result)
	{
		if (!Owners.TryGetValue(__instance, out var value) || !Stocks.TryGetValue(value, out var value2))
		{
			return true;
		}
		string identifier = ((Item)relic).GetIdentifier();
		if (id == -1)
		{
			id = ___m_ChosenRelics.IndexOf(identifier);
		}
		else if (id < 0 || id >= ___m_ChosenRelics.Count || ___m_ChosenRelics[id] != identifier)
		{
			return true;
		}
		if (id >= 0 && !Pool.Invoke((BuildingObjectShop)(object)value).Contains(identifier))
		{
			value2.RecordSupplier(id, Supplier(__instance));
		}
		if (id >= 0 && !value2.IsSold(id) && Available(value, __instance, value2, id, identifier) == relic)
		{
			return true;
		}
		__result = __instance.AddSold(-1, false);
		return false;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(BuildingObjectShop), "Callback")]
	private static bool Purchased(BuildingObjectShop __instance, int __0)
	{
		BuildingAmayaShop val = (BuildingAmayaShop)(object)((__instance is BuildingAmayaShop) ? __instance : null);
		if (val == null || !Stocks.TryGetValue(val, out var value))
		{
			return true;
		}
		if (value.IsSold(__0))
		{
			return false;
		}
		if (!value.MarkSold(__0))
		{
			return true;
		}
		((ClickableSprite)val).SetInteractable(true, false);
		return false;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(BuildingObjectShop), "AfterDialogue")]
	private static void MerchantFocus(BuildingObjectShop __instance)
	{
		if (__instance is BuildingAmayaShop)
		{
			UIOverlayMerchant merchant = OverlayManager.Instance.GetMerchant();
			(((Component)merchant).GetComponent<AmayaShopView>() ?? ((Component)merchant).gameObject.AddComponent<AmayaShopView>()).Open();
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UIOverlayMerchant), "Init")]
	private static void ResetView(UIOverlayMerchant __instance)
	{
		Owners.Remove(__instance);
		AmayaShopView component = ((Component)__instance).GetComponent<AmayaShopView>();
		if ((Object)(object)component != (Object)null)
		{
			((Behaviour)component).enabled = false;
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(OverlayManager), "Cancel")]
	private static bool Back(OverlayManager __instance, bool force)
	{
		if (!force)
		{
			UIOverlayBase obj = AmayaShopView.CurrentOverlay.Invoke(__instance);
			UIOverlayMerchant val = (UIOverlayMerchant)(object)((obj is UIOverlayMerchant) ? obj : null);
			if (val != null)
			{
				AmayaShopView component = ((Component)val).GetComponent<AmayaShopView>();
				if (component != null && component.CanBackOut())
				{
					val.Cancel();
					return false;
				}
			}
		}
		return true;
	}
}