Decompiled source of BalancedFasterInteractables v1.3.1

BalancedFasterInteractables.dll

Decompiled 2 months 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 System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using EntityStates;
using EntityStates.Barrel;
using EntityStates.Duplicator;
using EntityStates.Scrapper;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using RoR2;
using RoR2.EntityLogic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 RiskOfResources
{
	[BepInPlugin("com.riskofresources.fast.interactable", "BalancedFasterInteractables", "1.3.1")]
	[BepInIncompatibility("riskofresources.FasterInteractableBalancer")]
	[BepInIncompatibility("FlyingComputer.ExchangeChanges")]
	[BepInIncompatibility("Felda.ActuallyFaster")]
	internal class BalancedFasterInteractables : BaseUnityPlugin
	{
		private class PurchaseDelay : MonoBehaviour
		{
			private ArgumentCache delay;

			private float original;

			internal static void Set(PurchaseInteraction interaction, float multiplier, bool timed)
			{
				IEnumerable<PurchaseDelay> enumerable = ((Component)interaction).GetComponents<PurchaseDelay>();
				if (!enumerable.Any())
				{
					enumerable = Initialize(interaction);
				}
				float num = 0f;
				foreach (PurchaseDelay item in enumerable)
				{
					item.Update(multiplier);
					num = Mathf.Max(item.original, num);
				}
				if (timed)
				{
					UpdateStopwatch(num - num * multiplier);
				}
				((UnityEventBase)interaction.onPurchase).DirtyPersistentCalls();
			}

			internal static void Remove(PurchaseInteraction interaction)
			{
				PurchaseDelay[] components = ((Component)interaction).gameObject.GetComponents<PurchaseDelay>();
				foreach (PurchaseDelay obj in components)
				{
					((UnityEventBase)interaction.onPurchase).DirtyPersistentCalls();
					obj.Reset();
					Object.Destroy((Object)(object)obj);
				}
			}

			private void Update(float multiplier)
			{
				delay.floatArgument = original * multiplier;
			}

			private void Reset()
			{
				delay.floatArgument = original;
			}

			private static IEnumerable<PurchaseDelay> Initialize(PurchaseInteraction interaction)
			{
				foreach (PersistentCall call in ((UnityEventBase)interaction.onPurchase).m_PersistentCalls.m_Calls)
				{
					if (call.target is DelayedEvent && call.methodName == "CallDelayed")
					{
						PurchaseDelay purchaseDelay = ((Component)interaction).gameObject.AddComponent<PurchaseDelay>();
						purchaseDelay.delay = call.arguments;
						purchaseDelay.original = purchaseDelay.delay.floatArgument;
						yield return purchaseDelay;
					}
				}
			}
		}

		public const string version = "1.3.1";

		public const string identifier = "com.riskofresources.fast.interactable";

		private static ConfigEntry<bool> teleporter;

		private static ConfigEntry<bool> penalty;

		private static ConfigEntry<float> speed;

		private static ConfigEntry<bool> printer;

		private static ConfigEntry<bool> scrapper;

		private static ConfigEntry<bool> shrine;

		private static ConfigEntry<bool> chest;

		private static ConfigEntry<bool> cradle;

		private static ConfigEntry<bool> pool;

		private static ConfigEntry<bool> cauldron;

		private static bool Idle
		{
			get
			{
				if (teleporter.Value)
				{
					TeleporterInteraction instance = TeleporterInteraction.instance;
					if (!(((instance != null) ? instance.currentState : null) is ChargedState))
					{
						return CombatDirector.instancesList.Count > 0;
					}
				}
				return false;
			}
		}

		protected void Awake()
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Expected O, but got Unknown
			teleporter = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Only After Teleporter", true, "By default, this plugin will only take effect after the teleporter has been charged, or all combat directors have deactivated.");
			penalty = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Time Penalty", true, "Any time taken off the activation will be directly added to the game stopwatch.");
			speed = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Speed", 75f, new ConfigDescription("Time to complete each interaction is reduced by this percentage.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			printer = interactable("Printer");
			scrapper = interactable("Scrapper");
			shrine = interactable("Shrine of Chance");
			chest = interactable("Chest");
			cradle = interactable("Void Cradle");
			pool = interactable("Cleansing Pool");
			cauldron = interactable("Lunar Cauldron");
			Harmony.CreateAndPatchAll(typeof(BalancedFasterInteractables), (string)null);
			ConfigEntry<bool> interactable(string key)
			{
				return ((BaseUnityPlugin)this).Config.Bind<bool>("Interactables", key, true, "");
			}
		}

		[HarmonyPatch(typeof(Duplicating), "OnEnter")]
		[HarmonyPostfix]
		private static void PrintFaster(Duplicating __instance)
		{
			bool flag = !printer.Value || Idle;
			((Behaviour)((EntityState)__instance).GetComponent<DelayedEvent>()).enabled = flag;
			if (!flag)
			{
				float num = speed.Value / 100f;
				((EntityState)__instance).GetComponent<PurchaseInteraction>().SetUnavailableTemporarily(4f * (1f - num));
				num *= Duplicating.initialDelayDuration + Duplicating.timeBetweenStartAndDropDroplet;
				((EntityState)__instance).fixedAge = ((EntityState)__instance).fixedAge + num;
				UpdateStopwatch(num);
			}
		}

		[HarmonyPatch(typeof(Duplicating), "BeginCooking")]
		[HarmonyPostfix]
		private static void ScaleAnimation(Duplicating __instance)
		{
			if (printer.Value && !Idle)
			{
				((EntityState)__instance).GetModelAnimator().speed = 125f / (125f - speed.Value);
			}
		}

		[HarmonyPatch(typeof(ScrapperBaseState), "OnEnter")]
		[HarmonyPostfix]
		private static void ScrapQuickly(ScrapperBaseState __instance)
		{
			if (!scrapper.Value || Idle)
			{
				return;
			}
			float num = speed.Value / 100f;
			if (!(__instance is WaitToBeginScrapping))
			{
				if (!(__instance is Scrapping))
				{
					if (!(__instance is ScrappingToIdle))
					{
						return;
					}
					num *= ScrappingToIdle.duration * 0.5f;
				}
				else
				{
					num *= Scrapping.duration;
				}
			}
			else
			{
				num *= WaitToBeginScrapping.duration;
			}
			((EntityState)__instance).fixedAge = ((EntityState)__instance).fixedAge + num;
			UpdateStopwatch(num);
		}

		[HarmonyPatch(typeof(ShrineChanceBehavior), "AddShrineStack")]
		[HarmonyPostfix]
		private static void SpeedUpShrine(ShrineChanceBehavior __instance)
		{
			if (shrine.Value && !Idle)
			{
				float num = speed.Value / 100f * __instance.refreshTimer;
				__instance.refreshTimer -= num;
				UpdateStopwatch(num);
			}
		}

		[HarmonyPatch(typeof(Opening), "OnEnter")]
		[HarmonyPostfix]
		private static void OpenChest(EntityState __instance)
		{
			if (chest.Value && !Idle && __instance.GetComponent<ChestBehavior>() != null)
			{
				float num = speed.Value / 100f;
				UpdateStopwatch(0.45f * Opening.duration * num);
				num = ((num != 1f) ? (1f / (1f - num)) : float.MaxValue);
				__instance.GetModelAnimator().speed = num;
			}
		}

		[HarmonyPatch(typeof(OpeningLunar), "OnEnter")]
		[HarmonyPostfix]
		private static void CrackThatSuckerOpen(BaseState __instance)
		{
			if (cradle.Value && !Idle)
			{
				float num = speed.Value / 100f;
				float num2 = 1f - num;
				if (!Object.op_Implicit((Object)(object)((EntityState)__instance).GetComponent<ScriptedCombatEncounter>()))
				{
					num *= 0.125f;
					num2 += num;
				}
				num *= OpeningLunar.duration;
				num2 = ((num2 == 0f) ? float.MaxValue : (1f / num2));
				((EntityState)__instance).fixedAge = ((EntityState)__instance).fixedAge + num;
				UpdateStopwatch(num);
				((EntityState)__instance).GetModelAnimator().speed = num2;
			}
		}

		[HarmonyPatch(typeof(PurchaseInteraction), "OnInteractionBegin")]
		[HarmonyPrefix]
		private static void TryToInfest(PurchaseInteraction __instance)
		{
			if (Object.op_Implicit((Object)(object)((Component)__instance).GetComponent<ScriptedCombatEncounter>()))
			{
				if (!cradle.Value || Idle)
				{
					PurchaseDelay.Remove(__instance);
				}
				else
				{
					PurchaseDelay.Set(__instance, 1f - speed.Value / 95f, timed: false);
				}
			}
		}

		[HarmonyPatch(typeof(PurchaseInteraction), "OnInteractionBegin")]
		[HarmonyPrefix]
		private static void CleanseRapidly(PurchaseInteraction __instance)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Invalid comparison between Unknown and I4
			if (__instance.isShrine && (int)__instance.costType == 9)
			{
				if (!pool.Value || Idle)
				{
					PurchaseDelay.Remove(__instance);
				}
				else
				{
					PurchaseDelay.Set(__instance, 1f - speed.Value / 100f, timed: true);
				}
			}
		}

		[HarmonyPatch(typeof(PurchaseInteraction), "OnInteractionBegin")]
		[HarmonyPrefix]
		private static void CookSoup(PurchaseInteraction __instance)
		{
			if (__instance.contextToken == "BAZAAR_CAULDRON_CONTEXT")
			{
				if (!cauldron.Value || Idle)
				{
					PurchaseDelay.Remove(__instance);
				}
				else
				{
					PurchaseDelay.Set(__instance, 1f - speed.Value / 100f, timed: true);
				}
			}
		}

		private static void UpdateStopwatch(float time)
		{
			Run instance = Run.instance;
			if (penalty.Value)
			{
				bool? flag = ((instance != null) ? new bool?(instance.isRunStopwatchPaused) : null);
				if (flag.HasValue && !flag.GetValueOrDefault() && NetworkServer.active)
				{
					instance.SetRunStopwatch(instance.GetRunStopwatch() + time);
				}
			}
		}
	}
}