Decompiled source of RetroactiveFade v1.2.0

plugins/RetroactiveFade.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using RetroactiveMacro;
using RoR2;
using SSM24.FadeEmptyChests;
using UnityEngine;
using UnityEngine.Events;
using WolfoQoL_Client;
using WolfoQoL_Client.Reminders;

[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("RetroactiveFade")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0")]
[assembly: AssemblyProduct("RetroactiveFade")]
[assembly: AssemblyTitle("RetroactiveFade")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.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 RetroactiveFade
{
	public class Fader : MonoBehaviour
	{
		private static readonly List<Fader> instances = new List<Fader>();

		internal FadeObject fade;

		private PurchaseInteraction purchase;

		private bool isTerminal;

		private bool owned;

		private bool detached;

		private float weight = 1f;

		private float target = 1f;

		public static void RefreshAll()
		{
			foreach (Fader instance in instances)
			{
				instance.Refresh();
			}
		}

		private void Awake()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			((Behaviour)this).enabled = false;
			ShopTerminalBehavior val = default(ShopTerminalBehavior);
			isTerminal = ((Component)this).TryGetComponent<ShopTerminalBehavior>(ref val);
			purchase = (isTerminal ? ((Component)this).GetComponent<PurchaseInteraction>() : FindOwner(((Component)this).transform));
			if (isTerminal)
			{
				((UnityEvent)purchase.onSetAvailable).AddListener(new UnityAction(Refresh));
				((UnityEvent)purchase.onSetUnavailable).AddListener(new UnityAction(Refresh));
			}
			instances.Add(this);
		}

		private void OnDestroy()
		{
			instances.Remove(this);
		}

		public void Bind(FadeObject fadeObject)
		{
			fade = fadeObject;
			owned = false;
			detached = false;
			target = (ShouldUnfade() ? 0f : 1f);
			weight = target;
			((Behaviour)this).enabled = target == 0f;
		}

		public void Refresh()
		{
			if (Object.op_Implicit((Object)(object)fade))
			{
				float num = (ShouldUnfade() ? 0f : 1f);
				if (num != target)
				{
					target = num;
					((Behaviour)this).enabled = true;
				}
			}
		}

		private bool ShouldUnfade()
		{
			//IL_0045: 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)
			if (!Object.op_Implicit((Object)(object)purchase))
			{
				return false;
			}
			if (isTerminal)
			{
				if (RetroactiveFadePlugin.hasCard && FadeEmptyChests.ShouldApplyToMultishops.Value)
				{
					return purchase.available;
				}
				return false;
			}
			if (RetroactiveFadePlugin.hasSaleStar)
			{
				return purchase.costType == SaleStarCost.SaleStar;
			}
			return false;
		}

		private void Update()
		{
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Expected O, but got Unknown
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Expected O, but got Unknown
			if (!Object.op_Implicit((Object)(object)fade))
			{
				((Behaviour)this).enabled = false;
				return;
			}
			if (!owned)
			{
				if (fade.originalColors.Count == 0)
				{
					if (target == 1f)
					{
						weight = 1f;
						((Behaviour)this).enabled = false;
					}
					else
					{
						SetFade(1f, 1f);
					}
					return;
				}
				((MonoBehaviour)fade).StopAllCoroutines();
				if (weight == 1f && !fade.lerpFinished)
				{
					weight = Mathf.InverseLerp(1f, fade.TargetFade, fade.currentFade);
				}
				owned = true;
			}
			weight = Mathf.MoveTowards(weight, target, Time.deltaTime / FadeEmptyChests.FadeOutTime.Value);
			if (detached && weight > 0f)
			{
				SceneCamera.onSceneCameraPreRender += new SceneCameraDelegate(fade.OnSceneCameraPreRender);
				detached = false;
			}
			SetFade(Mathf.SmoothStep(1f, fade.TargetFade, weight), Mathf.SmoothStep(1f, fade.TargetBrightness, weight));
			if (weight == target)
			{
				if (weight == 0f && !detached)
				{
					fade.OnSceneCameraPreRender((SceneCamera)null);
					SceneCamera.onSceneCameraPreRender -= new SceneCameraDelegate(fade.OnSceneCameraPreRender);
					detached = true;
				}
				else if (weight == 1f)
				{
					fade.lerpFinished = true;
				}
				((Behaviour)this).enabled = false;
			}
		}

		private void SetFade(float alpha, float brightness)
		{
			fade.currentFade = alpha;
			fade.currentBrightness = brightness;
			fade.lerpFinished = false;
		}

		private static PurchaseInteraction FindOwner(Transform model)
		{
			ModelLocator val = default(ModelLocator);
			foreach (PurchaseInteraction instances in InstanceTracker.GetInstancesList<PurchaseInteraction>())
			{
				if (((Component)instances).TryGetComponent<ModelLocator>(ref val) && (Object)(object)val.modelTransform == (Object)(object)model)
				{
					return instances;
				}
			}
			return null;
		}
	}
	[BepInPlugin("FluxSquido.RetroactiveFade", "RetroactiveFade", "1.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class RetroactiveFadePlugin : BaseUnityPlugin
	{
		public const string PluginAuthor = "FluxSquido";

		public const string PluginName = "RetroactiveFade";

		public const string PluginGUID = "FluxSquido.RetroactiveFade";

		public const string PluginVersion = "1.2.0";

		internal static ManualLogSource log;

		internal static bool hasSaleStar;

		internal static bool hasCard;

		private void Awake()
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			log = ((BaseUnityPlugin)this).Logger;
			new Hook((MethodBase)AccessTools.Method(typeof(FadeObject), "Start", (Type[])null, (Type[])null), (Delegate)new Action<Action<FadeObject>, FadeObject>(FadeObject_Start));
			Inventory.onInventoryChangedGlobal += OnInventoryChanged;
			Run.onRunStartGlobal += delegate
			{
				hasSaleStar = (hasCard = false);
			};
			FadeEmptyChests.ShouldApplyToMultishops.SettingChanged += delegate
			{
				Fader.RefreshAll();
			};
			if (WolfoCompat.Enabled)
			{
				WolfoCompat.Init();
			}
		}

		private static void FadeObject_Start(Action<FadeObject> orig, FadeObject self)
		{
			Fader fader = ((Component)self).GetComponent<Fader>();
			if (Object.op_Implicit((Object)(object)fader) && Object.op_Implicit((Object)(object)fader.fade))
			{
				self.renderers = Array.Empty<Renderer>();
				Object.Destroy((Object)(object)self);
				fader.Refresh();
				return;
			}
			orig(self);
			if (!Object.op_Implicit((Object)(object)fader))
			{
				fader = ((Component)self).gameObject.AddComponent<Fader>();
			}
			fader.Bind(self);
		}

		private static void OnInventoryChanged(Inventory inventory)
		{
			LocalUser firstLocalUser = LocalUserManager.GetFirstLocalUser();
			CharacterMaster val = ((firstLocalUser != null) ? firstLocalUser.cachedMaster : null);
			if (Object.op_Implicit((Object)(object)val) && !((Object)(object)val.inventory != (Object)(object)inventory))
			{
				bool flag = inventory.GetItemCountEffective(Items.LowerPricedChests) > 0;
				bool flag2 = HasCard(inventory);
				if (flag != hasSaleStar || flag2 != hasCard)
				{
					hasSaleStar = flag;
					hasCard = flag2;
					Fader.RefreshAll();
				}
			}
		}

		private static bool HasCard(Inventory inventory)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: 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_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: 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)
			EquipmentIndex equipmentIndex = Equipment.MultiShopCard.equipmentIndex;
			for (uint num = 0u; num < inventory.GetEquipmentSlotCount(); num++)
			{
				for (uint num2 = 0u; num2 < inventory.GetEquipmentSetCount(); num2++)
				{
					EquipmentLocation val = new EquipmentLocation
					{
						slot = num,
						set = num2
					};
					EquipmentState equipment = inventory.GetEquipment(ref val);
					if ((QualityCompat.enabled ? QualityCompat.GetBaseEquipmentIndex(equipment.equipmentIndex) : equipment.equipmentIndex) == equipmentIndex && (equipment.charges > 0 || ((FixedTimeStamp)(ref equipment.chargeFinishTime)).timeUntil < 0.5f))
					{
						return true;
					}
				}
			}
			return false;
		}
	}
	internal static class WolfoCompat
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static Manipulator <0>__GlowOnlyOnReopenable;

			public static Manipulator <1>__MessageOnlyWhenSpent;
		}

		public const string GUID = "Wolfo.WolfoQoL_Client";

		public static bool Enabled => Chainloader.PluginInfos.ContainsKey("Wolfo.WolfoQoL_Client");

		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		public static void Init()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Expected O, but got Unknown
			MethodInfo target = AccessTools.FirstMethod(AccessTools.Inner(typeof(VisualsMisc), "<>c"), (Func<MethodInfo, bool>)((MethodInfo method) => method.Name.StartsWith("<BiggerSaleStarRange>") && method.GetParameters().Length == 2));
			object obj = <>O.<0>__GlowOnlyOnReopenable;
			if (obj == null)
			{
				Manipulator val = GlowOnlyOnReopenable;
				<>O.<0>__GlowOnlyOnReopenable = val;
				obj = (object)val;
			}
			Patch(target, (Manipulator)obj);
			MethodInfo target2 = AccessTools.Method(typeof(Reminders_Treasure), "KeyReminderUpdater_Host", (Type[])null, (Type[])null);
			object obj2 = <>O.<1>__MessageOnlyWhenSpent;
			if (obj2 == null)
			{
				Manipulator val2 = MessageOnlyWhenSpent;
				<>O.<1>__MessageOnlyWhenSpent = val2;
				obj2 = (object)val2;
			}
			Patch(target2, (Manipulator)obj2);
		}

		private static void Patch(MethodInfo target, Manipulator manipulator)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			if (target != null)
			{
				new ILHook((MethodBase)target, manipulator);
			}
			else
			{
				RetroactiveFadePlugin.log.LogWarning((object)("Wolfo's QOL target for " + ((Delegate)(object)manipulator).Method.Name + " not found, skipping."));
			}
		}

		private static void GlowOnlyOnReopenable(ILContext il)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(il);
			int purchaseLoc = 0;
			if (!val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[2]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref purchaseLoc),
				(Instruction x) => ILPatternMatchingExt.MatchLdfld<PurchaseInteraction>(x, "saleStarCompatible")
			}))
			{
				RetroactiveFadePlugin.log.LogWarning((object)(((MemberReference)il.Method).Name + ": IL match failed, Sale Star glow compat skipped."));
				return;
			}
			val.Emit(OpCodes.Ldloc, purchaseLoc);
			val.EmitDelegate<Func<bool, PurchaseInteraction, bool>>((Func<bool, PurchaseInteraction, bool>)((bool compatible, PurchaseInteraction purchase) => compatible && IsReopenable(purchase)));
		}

		private static void MessageOnlyWhenSpent(ILContext il)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Expected O, but got Unknown
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(il);
			int countLoc = 0;
			MethodReference val2 = default(MethodReference);
			if (!val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[2]
			{
				(Instruction x) => ILPatternMatchingExt.MatchCallOrCallvirt<Inventory>(x, "GetItemCountEffective"),
				(Instruction x) => ILPatternMatchingExt.MatchStloc(x, ref countLoc)
			}) || !val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction x) => ILPatternMatchingExt.MatchCallOrCallvirt(x, ref val2) && ((MemberReference)val2).Name == "Invoke"
			}))
			{
				RetroactiveFadePlugin.log.LogWarning((object)(((MemberReference)il.Method).Name + ": IL match failed, Sale Star message compat skipped."));
				return;
			}
			val.Emit(OpCodes.Ldloc, countLoc);
			val.Emit(OpCodes.Ldarg_2);
			val.EmitDelegate<Func<int, Interactor, int>>((Func<int, Interactor, int>)((int before, Interactor activator) => before - ((Component)activator).GetComponent<CharacterBody>().inventory.GetItemCountEffective(Items.LowerPricedChests)));
			val.Emit(OpCodes.Stloc, countLoc);
		}

		private static bool IsReopenable(PurchaseInteraction purchase)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			if (RetroactiveMacro.ChangeSaleStar.Value && purchase.costType != SaleStarCost.SaleStar)
			{
				return Object.op_Implicit((Object)(object)((Component)purchase).GetComponent<RouletteChestController>());
			}
			return true;
		}
	}
}