Decompiled source of RerollConfirmation v0.1.2

plugins/RerollConfirmation.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
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("RerollConfirmation")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.2.0")]
[assembly: AssemblyInformationalVersion("0.1.2+82ebcc19dbec49047e276f70868b2c65319a9b19")]
[assembly: AssemblyProduct("RerollConfirmation")]
[assembly: AssemblyTitle("RerollConfirmation")]
[assembly: AssemblyVersion("0.1.2.0")]
[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 RerollConfirmation
{
	[BepInPlugin("RerollConfirmation", "RerollConfirmation", "0.1.2")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger = new ManualLogSource("RerollConfirmation");

		internal static ConfigEntry<bool> Enabled = null;

		internal static ConfigEntry<int> TrinketShopThreshold = null;

		internal static ConfigEntry<int> OtherShopThreshold = null;

		public void Awake()
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enables the reroll gold warning icon and confirmation dialog.");
			TrinketShopThreshold = ((BaseUnityPlugin)this).Config.Bind<int>("Thresholds", "TrinketShopMinimumGold", 135, "Minimum gold to keep after a reroll at the Trinket (Merchant of Trinkets) shop before warning.");
			OtherShopThreshold = ((BaseUnityPlugin)this).Config.Bind<int>("Thresholds", "OtherShopMinimumGold", 90, "Minimum gold to keep after a reroll at other shops (Card/Steel/Arms/Magic) before warning.");
			Logger.LogInfo((object)"Plugin RerollConfirmation is loaded!");
			new Harmony("RerollConfirmation").PatchAll();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "RerollConfirmation";

		public const string PLUGIN_NAME = "RerollConfirmation";

		public const string PLUGIN_VERSION = "0.1.2";
	}
}
namespace RerollConfirmation.Patches
{
	[HarmonyPatch(typeof(BuyButton), "Refresh")]
	internal static class BuyButton_RerollWarning_Patch
	{
		private const string WarningSuffix = " <color=#FF3B3B><b>!</b></color>";

		private static void Postfix(BuyButton __instance)
		{
			if (!Plugin.Enabled.Value)
			{
				return;
			}
			MerchantGoodUIBase componentInParent = ((Component)__instance).GetComponentInParent<MerchantGoodUIBase>();
			object obj;
			if (componentInParent == null)
			{
				obj = null;
			}
			else
			{
				MerchantGoodState goodState = componentInParent.GoodState;
				obj = ((goodState != null) ? ((RewardState)goodState).RewardData : null);
			}
			if (!(obj is RerollMerchantRewardData) || !componentInParent.Affordable)
			{
				return;
			}
			AllGameManagers instance = AllGameManagers.Instance;
			SaveManager val = ((instance != null) ? instance.GetSaveManager() : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Traverse val2 = Traverse.Create((object)__instance);
			TMP_Text value = val2.Field<TMP_Text>("label").Value;
			if (!((Object)(object)value == (Object)null))
			{
				MerchantData value2 = Traverse.Create((object)componentInParent).Field<MerchantData>("merchantData").Value;
				if (val.GetGold() - val2.Field<int>("cost").Value < RerollGoldThreshold.GetMinimumGoldAfterReroll(value2, val))
				{
					value.text += " <color=#FF3B3B><b>!</b></color>";
				}
			}
		}
	}
	[HarmonyPatch(typeof(MerchantScreen), "TryPurchase")]
	internal static class MerchantScreen_RerollConfirmation_Patch
	{
		private static bool suppressConfirmation;

		private static bool Prefix(MerchantScreen __instance, MerchantGoodUIBase goodUI)
		{
			if (suppressConfirmation || !Plugin.Enabled.Value)
			{
				return true;
			}
			MerchantGoodUIBase obj = goodUI;
			object obj2;
			if (obj == null)
			{
				obj2 = null;
			}
			else
			{
				MerchantGoodState goodState = obj.GoodState;
				obj2 = ((goodState != null) ? ((RewardState)goodState).RewardData : null);
			}
			if (!(obj2 is RerollMerchantRewardData))
			{
				return true;
			}
			SaveManager value = Traverse.Create((object)__instance).Field<SaveManager>("saveManager").Value;
			MerchantData value2 = Traverse.Create((object)__instance).Field<MerchantData>("sourceMerchantData").Value;
			int cost = goodUI.GoodState.GetCost(value2);
			if (!value.CanAfford(cost))
			{
				return true;
			}
			int num = value.GetGold() - cost;
			int minimumGoldAfterReroll = RerollGoldThreshold.GetMinimumGoldAfterReroll(value2, value);
			if (num >= minimumGoldAfterReroll)
			{
				return true;
			}
			Traverse.Create((object)__instance).Field<ScreenManager>("screenManager").Value.ShowConfirmationDialog($"Rerolling will leave you with only {num} gold. Reroll anyway?", (Action)delegate
			{
				suppressConfirmation = true;
				try
				{
					Traverse.Create((object)__instance).Method("TryPurchase", new object[1] { goodUI }).GetValue();
				}
				finally
				{
					suppressConfirmation = false;
				}
			}, (Action)null, (Anchor)0, false);
			return false;
		}
	}
	internal static class RerollGoldThreshold
	{
		private const string TrinketShopAssetNameMarker = "ArtifactMerchant";

		private const string GuildMarkerRelicAssetKey = "CheaperMerchants";

		private const double GuildMarkerDiscountMultiplier = 0.75;

		public static int GetMinimumGoldAfterReroll(MerchantData? merchantData, SaveManager? saveManager)
		{
			int num = (((Object)(object)merchantData != (Object)null && ((Object)merchantData).name.Contains("ArtifactMerchant")) ? Plugin.TrinketShopThreshold.Value : Plugin.OtherShopThreshold.Value);
			if (!((Object)(object)saveManager != (Object)null) || !saveManager.GetAllRelics().Any((RelicState relic) => relic.GetAssetName() == "CheaperMerchants"))
			{
				return num;
			}
			return (int)Math.Round((double)num * 0.75);
		}
	}
}