Decompiled source of ScalingRerolls v0.2.1

ScalingRerolls.dll

Decompiled an hour ago
using System;
using System.Diagnostics;
using System.IO;
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 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: AssemblyVersion("0.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 ScalingRerolls
{
	[BepInPlugin("com.king_tajin.scalingrerolls", "Scaling Rerolls", "0.2.1")]
	[BepInProcess("Slots and Daggers.exe")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ConfigEntry<float> Acceleration;

		private void Awake()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			Acceleration = ((BaseUnityPlugin)this).Config.Bind<float>("Pricing", "Acceleration", 0.5f, new ConfigDescription("How quickly the reroll price climbs. Each reroll in a shop costs the base price plus this value times k x (k + 1), where k is the number of rerolls already. Use 0 to keep the price flat", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 5f), Array.Empty<object>()));
			RerollPricing.Initialize(((BaseUnityPlugin)this).Logger);
			Harmony val = new Harmony("com.king_tajin.scalingrerolls");
			Type[] array = new Type[4]
			{
				typeof(ShopEnterPatch),
				typeof(RerollPatch),
				typeof(LoadSessionPatch),
				typeof(SaveSessionPatch)
			};
			foreach (Type type in array)
			{
				try
				{
					val.PatchAll(type);
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)ex);
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Scaling Rerolls loaded");
		}
	}
	internal static class RerollPricing
	{
		private const int MaxCost = 200;

		private const int MaxStoredRerolls = 10000;

		private const string StateFileName = "saveFile0.scalingrerolls";

		private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

		private static readonly FieldInfo RerollsLeftField = typeof(UpgradeShopManager).GetField("rerollsLeft", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly FieldInfo OptionRerollField = typeof(UpgradeShopManager).GetField("optionReroll", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly FieldInfo SpriteRerollField = typeof(UpgradeShopManager).GetField("spriteReroll", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly FieldInfo LoadedSessionField = typeof(UpgradeShopManager).GetField("wasPopulatedFromLoadedSession", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly PropertyInfo IsStartShopProperty = typeof(UpgradeShopManager).GetProperty("IsStartShop", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static ManualLogSource log;

		private static int rerollsUsed;

		private static int pendingRestore;

		internal static void Initialize(ManualLogSource logger)
		{
			log = logger;
			if (RerollsLeftField == null || OptionRerollField == null || SpriteRerollField == null || IsStartShopProperty == null)
			{
				log.LogError((object)"Could not find the upgrade shop fields. The game may have been updated, so rerolls will behave as in the base game.");
			}
			if (LoadedSessionField == null)
			{
				log.LogWarning((object)"Could not find the loaded session flag. The reroll price may reset when a saved shop is loaded.");
			}
		}

		internal static bool IsLoadedRepeat(UpgradeShopManager manager)
		{
			try
			{
				return LoadedSessionField != null && (bool)LoadedSessionField.GetValue(manager);
			}
			catch (Exception ex)
			{
				log.LogError((object)ex);
				return false;
			}
		}

		internal static void PrepareRestore(GameSession session)
		{
			try
			{
				pendingRestore = ((session != null && session.wasUpgradeShop == 1) ? ReadState() : 0);
			}
			catch (Exception ex)
			{
				pendingRestore = 0;
				log.LogError((object)ex);
			}
		}

		internal static void ResetShop(UpgradeShopManager manager)
		{
			try
			{
				rerollsUsed = pendingRestore;
				pendingRestore = 0;
				if (RerollsLeftField != null)
				{
					RerollsLeftField.SetValue(manager, int.MaxValue);
				}
				if (rerollsUsed > 0 && CanPrice() && !IsStartShop(manager))
				{
					ApplyCost(manager);
				}
			}
			catch (Exception ex)
			{
				log.LogError((object)ex);
			}
		}

		internal static void AdvanceCost(UpgradeShopManager manager)
		{
			try
			{
				if (CanPrice() && !IsStartShop(manager) && !((Object)(object)GetRerollOption(manager) == (Object)null))
				{
					rerollsUsed++;
					ApplyCost(manager);
				}
			}
			catch (Exception ex)
			{
				log.LogError((object)ex);
			}
		}

		internal static void SaveState(GameSession session)
		{
			try
			{
				int num = ((session != null && session.wasUpgradeShop == 1) ? rerollsUsed : 0);
				string path = StatePath();
				string directoryName = Path.GetDirectoryName(path);
				if (!Directory.Exists(directoryName))
				{
					Directory.CreateDirectory(directoryName);
				}
				File.WriteAllText(path, num.ToString());
			}
			catch (Exception ex)
			{
				log.LogError((object)ex);
			}
		}

		private static int ReadState()
		{
			string path = StatePath();
			if (!File.Exists(path))
			{
				return 0;
			}
			if (!int.TryParse(File.ReadAllText(path).Trim(), out var result))
			{
				return 0;
			}
			return Math.Max(0, Math.Min(result, 10000));
		}

		private static string StatePath()
		{
			return Path.Combine(Application.persistentDataPath, "Save", "saveFile0.scalingrerolls");
		}

		private static bool CanPrice()
		{
			if (OptionRerollField != null && SpriteRerollField != null)
			{
				return IsStartShopProperty != null;
			}
			return false;
		}

		private static bool IsStartShop(UpgradeShopManager manager)
		{
			return (bool)IsStartShopProperty.GetValue(manager, null);
		}

		private static UpgradeShopOption GetRerollOption(UpgradeShopManager manager)
		{
			object? value = OptionRerollField.GetValue(manager);
			return (UpgradeShopOption)((value is UpgradeShopOption) ? value : null);
		}

		private static void ApplyCost(UpgradeShopManager manager)
		{
			UpgradeShopOption rerollOption = GetRerollOption(manager);
			if (!((Object)(object)rerollOption == (Object)null))
			{
				? val = rerollOption;
				int num = CostFor(rerollsUsed);
				object? value = SpriteRerollField.GetValue(manager);
				((UpgradeShopOption)val).SetupIconForReroll(num, (Sprite)((value is Sprite) ? value : null));
			}
		}

		private static int CostFor(int rerollsAlreadyUsed)
		{
			int rerollShopCost = Main.BalancingConfig.RerollShopCost;
			return (int)Math.Min((double)rerollShopCost + Math.Ceiling((double)(Plugin.Acceleration.Value * (float)rerollsAlreadyUsed * (float)(rerollsAlreadyUsed + 1)) - 0.0001), Math.Max(200, rerollShopCost));
		}
	}
	[HarmonyPatch(typeof(UpgradeShopManager), "HandleUpgradeShopEnter")]
	internal static class ShopEnterPatch
	{
		private static void Prefix(UpgradeShopManager __instance, out bool __state)
		{
			__state = RerollPricing.IsLoadedRepeat(__instance);
		}

		private static void Postfix(UpgradeShopManager __instance, bool __state)
		{
			if (!__state)
			{
				RerollPricing.ResetShop(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(UpgradeShopManager), "Reroll")]
	internal static class RerollPatch
	{
		private static void Prefix(UpgradeShopManager __instance)
		{
			RerollPricing.AdvanceCost(__instance);
		}
	}
	[HarmonyPatch(typeof(UpgradeShopManager), "ApplyLoadedGameSession")]
	internal static class LoadSessionPatch
	{
		private static void Prefix(GameSession loadedGameSession)
		{
			RerollPricing.PrepareRestore(loadedGameSession);
		}
	}
	[HarmonyPatch(typeof(GameSession), "SaveGameSession")]
	internal static class SaveSessionPatch
	{
		private static void Postfix(GameSession __instance)
		{
			RerollPricing.SaveState(__instance);
		}
	}
}