Decompiled source of RetroactiveFade v1.0.1

plugins/RetroactiveFade.dll

Decompiled 11 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using RoR2;
using SSM24.FadeEmptyChests;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace FluxSquido.RetroactiveFade;

[BepInPlugin("FluxSquido.RetroactiveFade", "RetroactiveFade", "1.0.5")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class RetroactiveFadePlugin : BaseUnityPlugin
{
	public const string GUID = "FluxSquido.RetroactiveFade";

	public const string NAME = "RetroactiveFade";

	public const string VERSION = "1.0.5";

	internal static ManualLogSource Log;

	private const float ReconcileInterval = 0.1f;

	internal static float Interval
	{
		[CompilerGenerated]
		get
		{
			return 0.1f;
		}
	}

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		GameObject val = new GameObject("RetroactiveFadeManager");
		Object.DontDestroyOnLoad((Object)(object)val);
		((Object)val).hideFlags = (HideFlags)61;
		val.AddComponent<FadeReconciler>();
		Log.LogInfo((object)"RetroactiveFade 1.0.5 loaded.");
	}
}
public class FadeReconciler : MonoBehaviour
{
	private readonly Dictionary<GameObject, PurchaseInteraction> _starSuppressed = new Dictionary<GameObject, PurchaseInteraction>();

	private static readonly FieldInfo LerpFinishedField = typeof(FadeObject).GetField("lerpFinished", BindingFlags.Instance | BindingFlags.NonPublic);

	private void Start()
	{
		((MonoBehaviour)this).StartCoroutine(Loop());
	}

	private IEnumerator Loop()
	{
		WaitForSeconds wait = new WaitForSeconds(RetroactiveFadePlugin.Interval);
		while (true)
		{
			try
			{
				Reconcile();
			}
			catch (Exception ex)
			{
				RetroactiveFadePlugin.Log.LogError((object)("Reconcile error: " + ex));
			}
			yield return wait;
		}
	}

	private void Reconcile()
	{
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)Run.instance == (Object)null)
		{
			return;
		}
		bool flag = LocalHasItem(Items.LowerPricedChests);
		bool flag2 = FadeEmptyChests.ShouldApplyToMultishops != null && FadeEmptyChests.ShouldApplyToMultishops.Value;
		if (flag || _starSuppressed.Count != 0 || flag2)
		{
			DedupeFades();
			List<PurchaseInteraction> instancesList = InstanceTracker.GetInstancesList<PurchaseInteraction>();
			ReconcileSaleStar(flag, instancesList);
			if (flag2)
			{
				ReconcileMultishops(LocalHasEquipment(Equipment.MultiShopCard.equipmentIndex), instancesList);
			}
		}
	}

	private void ReconcileSaleStar(bool hasStar, List<PurchaseInteraction> pis)
	{
		if (hasStar)
		{
			foreach (PurchaseInteraction pi in pis)
			{
				if (IsSaleStarSingle(pi) && pi.available)
				{
					foreach (FadeObject item in GetFadeObjectsFor(pi))
					{
						_starSuppressed[((Component)item).gameObject] = pi;
						Unfade(item);
					}
				}
			}
			return;
		}
		if (_starSuppressed.Count <= 0)
		{
			return;
		}
		foreach (KeyValuePair<GameObject, PurchaseInteraction> item2 in _starSuppressed)
		{
			GameObject key = item2.Key;
			PurchaseInteraction value = item2.Value;
			if (!((Object)(object)key == (Object)null) && ((Object)(object)value == (Object)null || !value.available) && (Object)(object)key.GetComponent<FadeObject>() == (Object)null)
			{
				key.AddComponent<FadeObject>();
			}
		}
		_starSuppressed.Clear();
	}

	private static bool IsSaleStarSingle(PurchaseInteraction pi)
	{
		if ((Object)(object)pi == (Object)null || !pi.saleStarCompatible)
		{
			return false;
		}
		if ((Object)(object)((Component)pi).GetComponent<ShopTerminalBehavior>() != (Object)null)
		{
			return false;
		}
		return true;
	}

	private static List<FadeObject> GetFadeObjectsFor(PurchaseInteraction pi)
	{
		List<FadeObject> list = new List<FadeObject>();
		ModelLocator component = ((Component)pi).GetComponent<ModelLocator>();
		if ((Object)(object)component != (Object)null && (Object)(object)component.modelTransform != (Object)null)
		{
			list.AddRange(((Component)component.modelTransform).GetComponentsInChildren<FadeObject>(true));
		}
		FadeObject[] componentsInChildren = ((Component)pi).GetComponentsInChildren<FadeObject>(true);
		foreach (FadeObject item in componentsInChildren)
		{
			if (!list.Contains(item))
			{
				list.Add(item);
			}
		}
		return list;
	}

	private void ReconcileMultishops(bool hasCard, List<PurchaseInteraction> pis)
	{
		Dictionary<MultiShopController, List<ShopTerminalBehavior>> dictionary = new Dictionary<MultiShopController, List<ShopTerminalBehavior>>();
		foreach (PurchaseInteraction pi in pis)
		{
			if ((Object)(object)pi == (Object)null)
			{
				continue;
			}
			ShopTerminalBehavior component = ((Component)pi).GetComponent<ShopTerminalBehavior>();
			if ((Object)(object)component == (Object)null)
			{
				continue;
			}
			MultiShopController serverMultiShopController = component.serverMultiShopController;
			if (!((Object)(object)serverMultiShopController == (Object)null) && !serverMultiShopController.isTripleDroneVendor)
			{
				if (!dictionary.TryGetValue(serverMultiShopController, out var value))
				{
					value = (dictionary[serverMultiShopController] = new List<ShopTerminalBehavior>());
				}
				value.Add(component);
			}
		}
		foreach (KeyValuePair<MultiShopController, List<ShopTerminalBehavior>> item in dictionary)
		{
			bool flag = false;
			bool flag2 = true;
			foreach (ShopTerminalBehavior item2 in item.Value)
			{
				bool networkhasBeenPurchased = item2.NetworkhasBeenPurchased;
				flag = flag || networkhasBeenPurchased;
				flag2 = flag2 && networkhasBeenPurchased;
			}
			bool desired = flag2 || (!hasCard && flag);
			foreach (ShopTerminalBehavior item3 in item.Value)
			{
				SetFade(((Component)item3).gameObject, desired);
			}
		}
	}

	private static void SetFade(GameObject target, bool desired)
	{
		if ((Object)(object)target == (Object)null)
		{
			return;
		}
		FadeObject component = target.GetComponent<FadeObject>();
		if (desired)
		{
			if ((Object)(object)component == (Object)null)
			{
				target.AddComponent<FadeObject>();
			}
		}
		else if ((Object)(object)component != (Object)null)
		{
			FadeObject[] components = target.GetComponents<FadeObject>();
			foreach (FadeObject fo in components)
			{
				Unfade(fo);
			}
		}
	}

	private static void Unfade(FadeObject fo)
	{
		if ((Object)(object)fo == (Object)null)
		{
			return;
		}
		try
		{
			if (LerpFinishedField != null)
			{
				LerpFinishedField.SetValue(fo, false);
			}
		}
		catch
		{
		}
		Object.Destroy((Object)(object)fo);
	}

	private static void DedupeFades()
	{
		HashSet<GameObject> hashSet = new HashSet<GameObject>();
		FadeObject[] array = Object.FindObjectsOfType<FadeObject>();
		foreach (FadeObject val in array)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			GameObject gameObject = ((Component)val).gameObject;
			if (hashSet.Add(gameObject))
			{
				FadeObject[] components = gameObject.GetComponents<FadeObject>();
				for (int j = 1; j < components.Length; j++)
				{
					Object.Destroy((Object)(object)components[j]);
				}
			}
		}
	}

	private static Inventory LocalInv()
	{
		LocalUser firstLocalUser = LocalUserManager.GetFirstLocalUser();
		CharacterMaster val = ((firstLocalUser == null) ? null : firstLocalUser.cachedMaster);
		return (!((Object)(object)val != (Object)null)) ? null : val.inventory;
	}

	private static bool LocalHasItem(ItemDef item)
	{
		Inventory val = LocalInv();
		return (Object)(object)val != (Object)null && val.GetItemCountEffective(item) > 0;
	}

	private static bool LocalHasEquipment(EquipmentIndex equip)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		Inventory val = LocalInv();
		return (Object)(object)val != (Object)null && val.HasEquipment(equip);
	}
}