Decompiled source of SoftCoinCap v0.4.0

SoftCoinCap.dll

Decompiled an hour ago
using System;
using System.Collections;
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 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 SoftCoinCap
{
	internal static class CoinCap
	{
		private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

		private static readonly FieldInfo MaxCoinsField = typeof(PlayerInventory).GetField("maxCoins", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static ManualLogSource log;

		private static int bypassDepth;

		internal static bool Bypassed => bypassDepth > 0;

		internal static void Initialize(ManualLogSource logger)
		{
			log = logger;
			if (MaxCoinsField == null)
			{
				log.LogWarning((object)"Could not find the max coins field. Only the property override will remove the hard cap.");
			}
		}

		internal static void EnterBypass()
		{
			bypassDepth++;
		}

		internal static void ExitBypass()
		{
			bypassDepth = Math.Max(0, bypassDepth - 1);
		}

		internal static void LiftCap(PlayerInventory inventory)
		{
			try
			{
				if (MaxCoinsField != null && (Object)(object)inventory != (Object)null)
				{
					MaxCoinsField.SetValue(inventory, int.MaxValue);
				}
			}
			catch (Exception ex)
			{
				log.LogError((object)ex);
			}
		}

		internal static bool ShouldKeep(int coinsHeld)
		{
			try
			{
				double num = ChanceFor(coinsHeld);
				return num >= 1.0 || (double)Random.value < num;
			}
			catch (Exception ex)
			{
				log.LogError((object)ex);
				return true;
			}
		}

		internal static double ChanceFor(int coinsHeld)
		{
			int num = coinsHeld - Plugin.Threshold.Value;
			if (num <= 0)
			{
				return 1.0;
			}
			double num2 = (double)Plugin.Acceleration.Value * Math.Pow(num, Plugin.Exponent.Value);
			return 1.0 / (1.0 + num2);
		}
	}
	[HarmonyPatch(/*Could not decode attribute arguments.*/)]
	internal static class MaxCoinsGetterPatch
	{
		private static void Postfix(ref int __result)
		{
			__result = int.MaxValue;
		}
	}
	[HarmonyPatch(typeof(PlayerInventory), "Start")]
	internal static class InventoryStartPatch
	{
		private static void Postfix(PlayerInventory __instance)
		{
			CoinCap.LiftCap(__instance);
		}
	}
	[HarmonyPatch(typeof(PlayerInventory), "AddCoin")]
	internal static class AddCoinPatch
	{
		private static bool Prefix(PlayerInventory __instance, Coin coin)
		{
			if (CoinCap.Bypassed || (Object)(object)coin == (Object)null)
			{
				return true;
			}
			if (CoinCap.ShouldKeep(__instance.TotalCoins))
			{
				return true;
			}
			CoinVanish.Begin(coin);
			return false;
		}
	}
	[HarmonyPatch(typeof(PlayerInventory), "ApplyLoadedGameSession")]
	internal static class LoadedSessionPatch
	{
		private static void Prefix()
		{
			CoinCap.EnterBypass();
		}

		private static void Finalizer()
		{
			CoinCap.ExitBypass();
		}
	}
	[HarmonyPatch]
	internal static class CoinsFromAbovePatch
	{
		private static MethodBase TargetMethod()
		{
			return AccessTools.EnumeratorMoveNext((MethodBase)AccessTools.Method(typeof(UIFeedbackManager), "SpawnCoinsFromAboveCorountine", (Type[])null, (Type[])null));
		}

		private static void Prefix()
		{
			CoinCap.EnterBypass();
		}

		private static void Finalizer()
		{
			CoinCap.ExitBypass();
		}
	}
	internal static class CoinVanish
	{
		private const float BaseDuration = 0.5f;

		private const float BaseSpinSpeed = 1440f;

		private const float BaseLift = 46f;

		internal static void Begin(Coin coin)
		{
			((MonoBehaviour)coin).StartCoroutine(Run(coin, Mathf.Max(0.05f, Plugin.VanishSpeed.Value)));
		}

		private static IEnumerator Run(Coin coin, float speed)
		{
			Transform body = ((Component)coin).transform;
			Vector3 startScale = body.localScale;
			Vector3 axis = Random.onUnitSphere;
			Rigidbody physics = coin.Rigidbody;
			float duration = 0.5f / speed;
			float spin = 1440f * speed;
			float lift = 46f * speed * speed;
			float elapsed = 0f;
			if ((Object)(object)physics != (Object)null)
			{
				physics.freezeRotation = true;
				physics.useGravity = false;
			}
			while (elapsed < duration)
			{
				yield return null;
				if ((Object)(object)coin == (Object)null)
				{
					yield break;
				}
				elapsed += Time.deltaTime;
				if ((Object)(object)physics != (Object)null)
				{
					physics.linearVelocity += Vector3.up * (lift * Time.deltaTime);
				}
				float num = Mathf.Clamp01(elapsed / duration);
				body.Rotate(axis, spin * Time.deltaTime, (Space)0);
				body.localScale = startScale * (1f - num * num);
			}
			if ((Object)(object)coin != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)coin).gameObject);
			}
		}
	}
	[BepInPlugin("com.king_tajin.softcoincap", "Soft Coin Cap", "0.4.0")]
	[BepInProcess("Slots and Daggers.exe")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ConfigEntry<int> Threshold;

		internal static ConfigEntry<float> Acceleration;

		internal static ConfigEntry<float> Exponent;

		internal static ConfigEntry<float> VanishSpeed;

		private void Awake()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Expected O, but got Unknown
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Expected O, but got Unknown
			Threshold = ((BaseUnityPlugin)this).Config.Bind<int>("Coins", "Threshold", 90, new ConfigDescription("Coins you can hold before each new coin has a chance to not be kept", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 1000), Array.Empty<object>()));
			Acceleration = ((BaseUnityPlugin)this).Config.Bind<float>("Coins", "Acceleration", 0.00044181f, new ConfigDescription("Scales how quickly the chance of keeping a coin drops. With k coins above the threshold the chance is 1 / (1 + this value x k ^ Exponent). Use 0 for no drop at all", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			Exponent = ((BaseUnityPlugin)this).Config.Bind<float>("Coins", "Exponent", 3.6236f, new ConfigDescription("How sharply the chance falls. Low values decline gradually, high values stay near 100% and then drop off quickly", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 10f), Array.Empty<object>()));
			VanishSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Visuals", "VanishSpeed", 0.16f, new ConfigDescription("How fast a lost coin spins, shrinks and rises away. Lower is slower and floatier, higher is faster", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.05f, 5f), Array.Empty<object>()));
			CoinCap.Initialize(((BaseUnityPlugin)this).Logger);
			Harmony val = new Harmony("com.king_tajin.softcoincap");
			Type[] array = new Type[5]
			{
				typeof(MaxCoinsGetterPatch),
				typeof(InventoryStartPatch),
				typeof(AddCoinPatch),
				typeof(LoadedSessionPatch),
				typeof(CoinsFromAbovePatch)
			};
			foreach (Type type in array)
			{
				try
				{
					val.PatchAll(type);
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)ex);
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Soft Coin Cap loaded");
		}
	}
}