Decompiled source of FadeEmptyChests v1.2.0

FadeEmptyChests.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EntityStates;
using EntityStates.Barrel;
using Microsoft.CodeAnalysis;
using On.EntityStates.Barrel;
using On.RoR2;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[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("FadeEmptyChests")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+4667f2c578ae53d043ee77af1a78253f0ba0bb22")]
[assembly: AssemblyProduct("FadeEmptyChests")]
[assembly: AssemblyTitle("FadeEmptyChests")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 SSM24.FadeEmptyChests
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.SSM24.FadeEmptyChests", "Fade Empty Chests", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class FadeEmptyChests : BaseUnityPlugin
	{
		public static ConfigEntry<float> FadeMultiplier;

		public static ConfigEntry<float> DrifterFadeMultiplier;

		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_DrifterFadeMultiplier = 0.8f;

		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_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Expected O, but got Unknown
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Expected O, but got Unknown
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Expected O, but got Unknown
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Expected O, but got Unknown
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Expected O, but got Unknown
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Expected O, but got Unknown
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Expected O, but got Unknown
			//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Expected O, but got Unknown
			//IL_01db: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Expected O, but got Unknown
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Expected O, but got Unknown
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Expected O, but got Unknown
			//IL_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Expected O, but got Unknown
			//IL_02b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bb: Expected O, but got Unknown
			//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cc: Expected O, but got Unknown
			//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02dd: Expected O, but got Unknown
			//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ee: Expected O, but got Unknown
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Expected O, but got Unknown
			//IL_027b: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Log.Init(((BaseUnityPlugin)this).Logger);
			FadeMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("FadeEmptyChests", "FadeMultiplier", 0.25f, "How much to fade empty containers. (0 = transparent, 1 = opaque)");
			DrifterFadeMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("FadeEmptyChests", "DrifterFadeMultiplier", 0.8f, "How much to fade empty containers when playing as Drifter. (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.");
			ModSettingsManager.AddOption((BaseOption)new SliderOption(FadeMultiplier, new SliderConfig
			{
				name = "Fade Multiplier",
				description = "How much to fade empty containers.\n\n0% is fully transparent\n100% is fully opaque",
				max = 1f,
				FormatString = "{0:0%}"
			}));
			ModSettingsManager.AddOption((BaseOption)new SliderOption(DrifterFadeMultiplier, new SliderConfig
			{
				name = "Drifter Fade Multiplier",
				description = "How much to fade empty containers, specifically when playing as Drifter.\n\n0% is fully transparent\n100% is fully opaque",
				max = 1f,
				FormatString = "{0:0%}"
			}));
			ModSettingsManager.AddOption((BaseOption)new SliderOption(BrightnessMultiplier, new SliderConfig
			{
				name = "Brightness Multiplier",
				description = "How much to darken empty containers.\n\n0% is fully black\n100% is normal brightness",
				max = 1f,
				FormatString = "{0:0%}"
			}));
			ModSettingsManager.AddOption((BaseOption)new SliderOption(FadeOutTime, new SliderConfig
			{
				name = "Fade Out Time",
				max = 5f,
				FormatString = "{0:0.00}s"
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(ShouldApplyToMultishops, new CheckBoxConfig
			{
				name = "Should Apply to Multishops"
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(ShouldApplyToAdaptiveChests, new CheckBoxConfig
			{
				name = "Should Apply to Adaptive Chests"
			}));
			try
			{
				using Stream stream = File.OpenRead(Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "icon.png"));
				Texture2D val = new Texture2D(0, 0);
				byte[] array = new byte[stream.Length];
				stream.Read(array, 0, (int)stream.Length);
				if (ImageConversion.LoadImage(val, array))
				{
					ModSettingsManager.SetModIcon(Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0f, 0f)));
				}
			}
			catch (FileNotFoundException)
			{
			}
			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);
		}

		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, PayCostContext payCostContext, PayCostResults payCostResult)
		{
			orig.Invoke(self, payCostContext, payCostResult);
			if (self.isTripleDroneVendor || !ShouldApplyToMultishops.Value || !NetworkServer.active)
			{
				return;
			}
			bool flag = false;
			for (int i = 0; i < self.terminalGameObjects.Length; i++)
			{
				GameObject val = self.terminalGameObjects[i];
				PurchaseInteraction component = val.GetComponent<PurchaseInteraction>();
				if ((Object)(object)payCostContext.purchaseInteraction == (Object)(object)component)
				{
					val.AddComponent<FadeObject>();
					flag = self.doCloseOnTerminalPurchase[i];
				}
			}
			if (!flag)
			{
				return;
			}
			GameObject[] terminalGameObjects = self.terminalGameObjects;
			foreach (GameObject val2 in terminalGameObjects)
			{
				if ((Object)(object)val2.GetComponent<FadeObject>() == (Object)null)
				{
					val2.AddComponent<FadeObject>();
				}
			}
		}

		private void On_Opened_OnEnter(orig_OnEnter orig, Opened self)
		{
			orig.Invoke(self);
			if (ShouldApplyToAdaptiveChests.Value && NetworkServer.active)
			{
				Transform modelTransform = ((EntityState)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
	{
		[CompilerGenerated]
		private sealed class <InterpolateBrightnessAndFade>d__18 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public FadeObject <>4__this;

			private float <currentLerp>5__2;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <InterpolateBrightnessAndFade>d__18(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_007d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0087: Expected O, but got Unknown
				int num = <>1__state;
				FadeObject fadeObject = <>4__this;
				switch (num)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<currentLerp>5__2 = 0f;
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if (<currentLerp>5__2 <= 1f)
				{
					fadeObject.currentFade = Mathf.SmoothStep(1f, fadeObject.TargetFade, <currentLerp>5__2);
					fadeObject.currentBrightness = Mathf.SmoothStep(1f, fadeObject.TargetBrightness, <currentLerp>5__2);
					<currentLerp>5__2 += Time.deltaTime / fadeObject.FadeOutTime;
					<>2__current = (object)new WaitForEndOfFrame();
					<>1__state = 1;
					return true;
				}
				fadeObject.lerpFinished = true;
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		[CompilerGenerated]
		private sealed class <WaitUntilVisible>d__19 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public FadeObject <>4__this;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <WaitUntilVisible>d__19(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_007f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0089: Expected O, but got Unknown
				//IL_002e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0038: Expected O, but got Unknown
				//IL_0058: Unknown result type (might be due to invalid IL or missing references)
				//IL_0062: Expected O, but got Unknown
				int num = <>1__state;
				FadeObject fadeObject = <>4__this;
				bool flag;
				switch (num)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					flag = false;
					goto IL_004f;
				case 1:
					<>1__state = -1;
					flag = fadeObject.RenderersAreVisible();
					goto IL_004f;
				case 2:
					{
						<>1__state = -1;
						fadeObject.RefreshRenderers();
						SceneCamera.onSceneCameraPreRender += new SceneCameraDelegate(fadeObject.OnSceneCameraPreRender);
						return false;
					}
					IL_004f:
					if (!flag)
					{
						<>2__current = (object)new WaitForEndOfFrame();
						<>1__state = 1;
						return true;
					}
					<>2__current = (object)new WaitForSecondsRealtime(0.05f);
					<>1__state = 2;
					return true;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private Renderer[] renderers;

		private MaterialPropertyBlock propertyStorage;

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

		private Dictionary<Renderer, DitherModel> ditherModels = new Dictionary<Renderer, DitherModel>();

		private float currentFade = 1f;

		private float currentBrightness = 1f;

		private bool lerpFinished;

		private bool isDrifter;

		private float TargetFade
		{
			get
			{
				if (!isDrifter)
				{
					return FadeEmptyChests.FadeMultiplier.Value;
				}
				return FadeEmptyChests.DrifterFadeMultiplier.Value;
			}
		}

		private float TargetBrightness => FadeEmptyChests.BrightnessMultiplier.Value;

		private float FadeOutTime => FadeEmptyChests.FadeOutTime.Value;

		private void Start()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			LocalUser firstLocalUser = LocalUserManager.GetFirstLocalUser();
			isDrifter = firstLocalUser != null && ((Object)firstLocalUser.cachedBody).name.Contains("DrifterBody");
			propertyStorage = new MaterialPropertyBlock();
			renderers = ((Component)this).gameObject.GetComponentsInChildren<Renderer>();
			((MonoBehaviour)this).StartCoroutine(WaitUntilVisible());
			((MonoBehaviour)this).StartCoroutine(InterpolateBrightnessAndFade());
		}

		private bool HasDitherModel(Renderer renderer)
		{
			if (ditherModels.ContainsKey(renderer))
			{
				return (Object)(object)ditherModels[renderer] != (Object)null;
			}
			bool flag = false;
			Log.Debug($"Caching result of HasDitherModel for {((Object)((Component)this).gameObject).name}: {flag}");
			foreach (DitherModel instances in DitherModel.instancesList)
			{
				if (instances.renderers.Contains(renderer))
				{
					flag = true;
					ditherModels[renderer] = instances;
					break;
				}
			}
			if (!flag)
			{
				ditherModels[renderer] = null;
			}
			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();
					Log.Debug("Renderers became null, refreshing reference to renderers");
					result = false;
					break;
				}
				if (!val.isVisible)
				{
					result = false;
					break;
				}
			}
			return result;
		}

		[IteratorStateMachine(typeof(<InterpolateBrightnessAndFade>d__18))]
		private IEnumerator InterpolateBrightnessAndFade()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <InterpolateBrightnessAndFade>d__18(0)
			{
				<>4__this = this
			};
		}

		[IteratorStateMachine(typeof(<WaitUntilVisible>d__19))]
		private IEnumerator WaitUntilVisible()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <WaitUntilVisible>d__19(0)
			{
				<>4__this = this
			};
		}

		private void OnSceneCameraPreRender(SceneCamera _)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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)
			if (lerpFinished)
			{
				currentFade = TargetFade;
				currentBrightness = TargetBrightness;
			}
			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)
			{
				Log.Debug("Setting color failed, refreshing reference to renderers");
				RefreshRenderers();
			}
		}

		private void ChangeFade(Renderer renderer)
		{
			if (!HasDitherModel(renderer))
			{
				try
				{
					renderer.GetPropertyBlock(propertyStorage);
				}
				catch (NullReferenceException)
				{
					Log.Debug("GetPropertyBlock failed, refreshing reference to renderers");
					RefreshRenderers();
					return;
				}
				propertyStorage.SetFloat("_Fade", currentFade);
				renderer.SetPropertyBlock(propertyStorage);
			}
			else
			{
				DitherModel obj = ditherModels[renderer];
				obj.fade *= currentFade;
				obj.UpdateDither();
			}
		}

		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);
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
}