Decompiled source of FadeEmptyChests v1.1.4

FadeEmptyChests.dll

Decompiled 3 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EntityStates;
using EntityStates.Barrel;
using MonoMod.Utils;
using On.EntityStates.Barrel;
using On.RoR2;
using RoR2;
using UnityEngine;
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: AssemblyCompany("FadeEmptyChests")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+f5568e8177decc6a6fb6fe0eacc1d429d9a81d9b")]
[assembly: AssemblyProduct("FadeEmptyChests")]
[assembly: AssemblyTitle("FadeEmptyChests")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SSM24.FadeEmptyChests;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("com.SSM24.FadeEmptyChests", "Fade Empty Chests", "1.0.0")]
public class FadeEmptyChests : BaseUnityPlugin
{
	public static ConfigEntry<float> FadeMultiplier;

	public static ConfigEntry<float> BrightnessMultiplier;

	public static ConfigEntry<float> FadeOutTime;

	public static ConfigEntry<bool> ShouldApplyToMultishops;

	public static ConfigEntry<bool> ShouldApplyToAdaptiveChests;

	private const float default_FadeMultiplier = 0.25f;

	private const float default_BrightnessMultiplier = 0.5f;

	private const float default_FadeOutTime = 1f;

	private const bool default_ShouldApplyToMultishops = false;

	private const bool default_ShouldApplyToAdaptiveChests = false;

	internal static FadeEmptyChests Instance;

	public void Awake()
	{
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Expected O, but got Unknown
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Expected O, but got Unknown
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Expected O, but got Unknown
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Expected O, but got Unknown
		Instance = this;
		FadeMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("FadeEmptyChests", "FadeMultiplier", 0.25f, "How much to fade empty containers. (0 = transparent, 1 = opaque)");
		BrightnessMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("FadeEmptyChests", "BrightnessMultiplier", 0.5f, "How much to darken empty containers. (0 = completely black, 1 = normal)");
		FadeOutTime = ((BaseUnityPlugin)this).Config.Bind<float>("FadeEmptyChests", "FadeOutTime", 1f, "How long containers should take to fade out, in seconds.");
		ShouldApplyToMultishops = ((BaseUnityPlugin)this).Config.Bind<bool>("FadeEmptyChests", "ShouldApplyToMultishops", false, "Whether multishops should fade out after use.");
		ShouldApplyToAdaptiveChests = ((BaseUnityPlugin)this).Config.Bind<bool>("FadeEmptyChests", "ShouldApplyToAdaptiveChests", false, "Whether adaptive chests should fade out after use.");
		Opened.OnEnter += new hook_OnEnter(On_Opened_OnEnter);
		DelusionChestController.ResetChestForDelusion += new hook_ResetChestForDelusion(On_DelusionChestController_ResetChestForDelusion);
		Opened.OnEnter += new hook_OnEnter(On_Opened_OnEnter);
		MultiShopController.OnPurchase += new hook_OnPurchase(On_MultiShopController_OnPurchase);
	}

	internal static void Log(LogLevel level, object data)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		((BaseUnityPlugin)Instance).Logger.Log(level, data);
	}

	private void On_Opened_OnEnter(orig_OnEnter orig, Opened self)
	{
		orig.Invoke(self);
		((Component)((EntityState)self).outer.commonComponents.modelLocator.modelTransform).gameObject.AddComponent<FadeObject>();
	}

	private void On_MultiShopController_OnPurchase(orig_OnPurchase orig, MultiShopController self, Interactor interactor, PurchaseInteraction purchaseInteraction)
	{
		orig.Invoke(self, interactor, purchaseInteraction);
		if (!ShouldApplyToMultishops.Value || !NetworkServer.active)
		{
			return;
		}
		DynData<MultiShopController> obj = new DynData<MultiShopController>(self);
		GameObject[] array = obj.Get<GameObject[]>("_terminalGameObjects");
		bool[] array2 = obj.Get<bool[]>("doCloseOnTerminalPurchase");
		bool flag = false;
		for (int i = 0; i < array.Length; i++)
		{
			GameObject val = array[i];
			PurchaseInteraction component = val.GetComponent<PurchaseInteraction>();
			if ((Object)(object)purchaseInteraction == (Object)(object)component)
			{
				val.AddComponent<FadeObject>();
				flag = array2[i];
			}
		}
		if (!flag)
		{
			return;
		}
		GameObject[] array3 = array;
		foreach (GameObject val2 in array3)
		{
			if ((Object)(object)val2.GetComponent<FadeObject>() == (Object)null)
			{
				val2.AddComponent<FadeObject>();
			}
		}
	}

	private void On_Opened_OnEnter(orig_OnEnter orig, EntityState self)
	{
		orig.Invoke(self);
		if (ShouldApplyToAdaptiveChests.Value && NetworkServer.active)
		{
			Transform modelTransform = self.outer.commonComponents.modelLocator.modelTransform;
			((Component)modelTransform.GetChild(0)).gameObject.AddComponent<FadeObject>();
			((Component)modelTransform.GetChild(1)).gameObject.AddComponent<FadeObject>();
		}
	}

	private void On_DelusionChestController_ResetChestForDelusion(orig_ResetChestForDelusion orig, DelusionChestController self)
	{
		orig.Invoke(self);
		FadeObject component = ((Component)((Component)self).gameObject.GetComponent<ModelLocator>().modelTransform).GetComponent<FadeObject>();
		if ((Object)(object)component != (Object)null)
		{
			Object.Destroy((Object)(object)component);
		}
	}
}
public class FadeObject : MonoBehaviour
{
	private Renderer[] renderers;

	private MaterialPropertyBlock propertyStorage;

	private List<Color> originalColors = new List<Color>();

	private float currentFade = 1f;

	private float currentBrightness = 1f;

	private static BindingFlags staticPrivate = BindingFlags.Static | BindingFlags.NonPublic;

	private static List<DitherModel> instancesList = (List<DitherModel>)typeof(DitherModel).GetField("instancesList", staticPrivate).GetValue(null);

	private float TargetFade => FadeEmptyChests.FadeMultiplier.Value;

	private float TargetBrightness => FadeEmptyChests.BrightnessMultiplier.Value;

	private float FadeOutTime => FadeEmptyChests.FadeOutTime.Value;

	private void Start()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		propertyStorage = new MaterialPropertyBlock();
		renderers = ((Component)this).gameObject.GetComponentsInChildren<Renderer>();
		((MonoBehaviour)this).StartCoroutine(WaitUntilVisible());
		((MonoBehaviour)this).StartCoroutine(InterpolateBrightnessAndFade());
	}

	private bool HasDitherModel(Renderer renderer)
	{
		DynData<Renderer> val = new DynData<Renderer>(renderer);
		object obj = val["hasDitherModel"];
		if (obj != null)
		{
			return (bool)obj;
		}
		bool flag = false;
		foreach (DitherModel instances in instancesList)
		{
			if (instances.renderers.Contains(renderer))
			{
				flag = true;
				break;
			}
		}
		FadeEmptyChests.Log((LogLevel)32, $"Caching result of HasDitherModel for {((Object)((Component)this).gameObject).name}: {flag}");
		val["hasDitherModel"] = flag;
		return flag;
	}

	private void RefreshRenderers()
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		renderers = ((Component)this).gameObject.GetComponentsInChildren<Renderer>();
		originalColors.Clear();
		Renderer[] array = renderers;
		foreach (Renderer val in array)
		{
			originalColors.Add(val.material.color);
		}
	}

	private bool RenderersAreVisible()
	{
		bool result = true;
		Renderer[] array = renderers;
		foreach (Renderer val in array)
		{
			if ((Object)(object)val == (Object)null)
			{
				RefreshRenderers();
				FadeEmptyChests.Log((LogLevel)16, "Renderers became null, refreshing reference to renderers");
				result = false;
				break;
			}
			if (!val.isVisible)
			{
				result = false;
				break;
			}
		}
		return result;
	}

	private IEnumerator InterpolateBrightnessAndFade()
	{
		float currentLerp = 0f;
		while (currentLerp <= 1f)
		{
			currentFade = Mathf.SmoothStep(1f, TargetFade, currentLerp);
			currentBrightness = Mathf.SmoothStep(1f, TargetBrightness, currentLerp);
			currentLerp += Time.deltaTime / FadeOutTime;
			yield return (object)new WaitForEndOfFrame();
		}
	}

	private IEnumerator WaitUntilVisible()
	{
		bool flag = false;
		while (!flag)
		{
			yield return (object)new WaitForEndOfFrame();
			flag = RenderersAreVisible();
		}
		yield return (object)new WaitForSecondsRealtime(0.05f);
		RefreshRenderers();
		SceneCamera.onSceneCameraPreRender += new SceneCameraDelegate(OnSceneCameraPreRender);
	}

	private void OnSceneCameraPreRender(SceneCamera _)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		for (int i = 0; i < renderers.Length; i++)
		{
			Renderer renderer = renderers[i];
			Color color = originalColors[i];
			ChangeColor(renderer, color);
			ChangeFade(renderer);
		}
	}

	private void ChangeColor(Renderer renderer, Color color)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			renderer.material.color = color * currentBrightness;
		}
		catch (NullReferenceException)
		{
			FadeEmptyChests.Log((LogLevel)16, "Setting color failed, refreshing reference to renderers");
			RefreshRenderers();
		}
	}

	private void ChangeFade(Renderer renderer)
	{
		try
		{
			renderer.GetPropertyBlock(propertyStorage);
		}
		catch (NullReferenceException)
		{
			FadeEmptyChests.Log((LogLevel)16, "GetPropertyBlock failed, refreshing reference to renderers");
			RefreshRenderers();
			return;
		}
		if (!HasDitherModel(renderer))
		{
			propertyStorage.SetFloat("_Fade", currentFade);
		}
		else
		{
			float @float = propertyStorage.GetFloat("_Fade");
			propertyStorage.SetFloat("_Fade", @float * currentFade);
		}
		renderer.SetPropertyBlock(propertyStorage);
	}

	private void OnDestroy()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		SceneCamera.onSceneCameraPreRender -= new SceneCameraDelegate(OnSceneCameraPreRender);
		currentBrightness = 1f;
		currentFade = 1f;
		OnSceneCameraPreRender(null);
	}
}