Decompiled source of DeathFade v1.0.1

Mods/DeathFade.dll

Decompiled 38 minutes ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BoneLib;
using BoneLib.BoneMenu;
using DeathFade;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Main), "DeathFade", "1.0.1", "kamonkus", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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 DeathFade
{
	public static class FadeManager
	{
		private class Overlay
		{
			public GameObject go;

			public MeshRenderer mr;
		}

		private class CamState
		{
			public Camera cam;

			public int mask;

			public object flags;

			public Color bg;
		}

		private const float MuffleTime = 0.05f;

		private const float MuffleHz = 700f;

		private const float RecoverTime = 1f;

		private const float StartDelay = 1.5f;

		private const float HoldAfterLoad = 0.4f;

		private const float MaxCutoff = 22000f;

		private const BindingFlags Inst = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

		private static float _readyAt = float.MaxValue;

		private static bool _dead;

		private static float _deathStart;

		private static float _holdUntil;

		private static float _vis;

		private static float _cut;

		private static float _sil;

		private static float _last;

		private static Type _rmType;

		private static PropertyInfo _healthProp;

		private static Type _hType;

		private static PropertyInfo _pAlive;

		private static PropertyInfo _pImminent;

		private static PropertyInfo _pCurr;

		private static Material _mat;

		private static Mesh _mesh;

		private static bool _visualBad;

		private static readonly Dictionary<long, Overlay> _overlays = new Dictionary<long, Overlay>();

		private static readonly Dictionary<long, CamState> _states = new Dictionary<long, CamState>();

		private static readonly List<Camera> _targets = new List<Camera>();

		private static float _nextRefresh;

		private static string _lastTargets = "";

		private static Camera _cam;

		private static float _nextCamSearch;

		private static AudioLowPassFilter _lp;

		private static GameObject _lpOwner;

		private static bool _filterBad;

		private static PropertyInfo _volProp;

		private static bool _volSearched;

		private static float _origVol = 1f;

		private static bool _volCaptured;

		private static PropertyInfo _pMask;

		private static PropertyInfo _pFlags;

		private static PropertyInfo _pBg;

		private static bool _camPropsSearched;

		private static bool _camPropsBad;

		public static void OnLevelLoaded()
		{
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			_readyAt = realtimeSinceStartup + 1.5f;
			_dead = false;
			_holdUntil = realtimeSinceStartup + 0.4f;
			_rmType = null;
			_healthProp = null;
			_hType = null;
			_pAlive = null;
			_pImminent = null;
			_pCurr = null;
		}

		public static void OnLevelUnloaded()
		{
			_readyAt = float.MaxValue;
			_dead = false;
			if (_vis > 0.01f)
			{
				_holdUntil = float.MaxValue;
			}
		}

		public static void Tick()
		{
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			float num = Mathf.Clamp(realtimeSinceStartup - _last, 0f, 0.1f);
			_last = realtimeSinceStartup;
			if (!Settings.Enabled)
			{
				_dead = false;
				if (_holdUntil == float.MaxValue)
				{
					_holdUntil = 0f;
				}
			}
			else if (realtimeSinceStartup >= _readyAt)
			{
				PollDeath(realtimeSinceStartup);
			}
			if (_dead)
			{
				float num2 = realtimeSinceStartup - _deathStart + num;
				float num3 = Mathf.Clamp01(num2 / Mathf.Max(0.001f, 0.003f));
				float num4 = Mathf.Clamp01(num2 / 0.05f);
				float num5 = Mathf.Clamp01((num2 - 0.025f) / Mathf.Max(0.05f, 0.4f));
				if (num3 > _vis)
				{
					_vis = num3;
				}
				if (num4 > _cut)
				{
					_cut = num4;
				}
				if (num5 > _sil)
				{
					_sil = num5;
				}
			}
			else if (realtimeSinceStartup >= _holdUntil)
			{
				float num6 = num / 1f;
				_vis = Mathf.Max(0f, _vis - num6);
				_cut = Mathf.Max(0f, _cut - num6);
				_sil = Mathf.Max(0f, _sil - num6);
			}
			ApplyVisual(realtimeSinceStartup);
			ApplyAudio(realtimeSinceStartup);
		}

		private static void PollDeath(float now)
		{
			string info;
			int num = ReadState(out info);
			if (num >= 0)
			{
				if (num == 1 && !_dead)
				{
					_dead = true;
					_deathStart = now;
					MelonLogger.Msg("Death detected (" + info + "). Blackout.");
					_nextRefresh = 0f;
					ForceRagdoll();
				}
				else if (num == 0 && _dead)
				{
					_dead = false;
					_holdUntil = now + 0.4f;
					MelonLogger.Msg("Player is alive again (" + info + "). Fading back in.");
				}
			}
		}

		private static int ReadState(out string info)
		{
			info = "";
			object rigManager = Player.RigManager;
			if (rigManager == null)
			{
				return -1;
			}
			Type type = rigManager.GetType();
			if (type != _rmType)
			{
				_rmType = type;
				_healthProp = FindProp(type, "health");
				if (_healthProp == null)
				{
					PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					foreach (PropertyInfo propertyInfo in properties)
					{
						if (propertyInfo.GetIndexParameters().Length == 0 && propertyInfo.PropertyType.Name.IndexOf("Health", StringComparison.OrdinalIgnoreCase) >= 0 && propertyInfo.PropertyType.Name.IndexOf("Mode", StringComparison.OrdinalIgnoreCase) < 0)
						{
							_healthProp = propertyInfo;
							break;
						}
					}
				}
				MelonLogger.Msg("Health property on " + type.Name + ": " + ((_healthProp != null) ? _healthProp.Name : "NOT FOUND"));
				_hType = null;
			}
			if (_healthProp == null)
			{
				return -1;
			}
			object value = _healthProp.GetValue(rigManager, null);
			if (value == null)
			{
				return -1;
			}
			Type type2 = value.GetType();
			if (type2 != _hType)
			{
				_hType = type2;
				_pAlive = FindProp(type2, "alive");
				_pImminent = FindProp(type2, "deathIsImminent");
				_pCurr = FindProp(type2, "curr_Health");
				MelonLogger.Msg("Health type " + type2.FullName + ": alive=" + (_pAlive != null) + ", deathIsImminent=" + (_pImminent != null) + ", curr_Health=" + (_pCurr != null));
			}
			if (_pAlive == null && _pImminent == null && _pCurr == null)
			{
				return -1;
			}
			bool flag = false;
			bool flag2 = true;
			bool flag3 = false;
			float num = 1f;
			if (_pAlive != null)
			{
				flag2 = Convert.ToBoolean(_pAlive.GetValue(value, null));
				if (!flag2)
				{
					flag = true;
				}
			}
			if (_pImminent != null)
			{
				flag3 = Convert.ToBoolean(_pImminent.GetValue(value, null));
				if (flag3)
				{
					flag = true;
				}
			}
			if (_pCurr != null)
			{
				num = Convert.ToSingle(_pCurr.GetValue(value, null));
				if (num <= 0f)
				{
					flag = true;
				}
			}
			info = "alive=" + flag2 + ", imminent=" + flag3 + ", hp=" + num.ToString("0.00");
			return flag ? 1 : 0;
		}

		private static PropertyInfo FindProp(Type t, string name)
		{
			try
			{
				PropertyInfo[] properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (PropertyInfo propertyInfo in properties)
				{
					if (propertyInfo.GetIndexParameters().Length == 0 && string.Equals(propertyInfo.Name, name, StringComparison.OrdinalIgnoreCase))
					{
						return propertyInfo;
					}
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("FindProp(" + name + ") failed: " + (ex.InnerException ?? ex).Message);
			}
			return null;
		}

		private static Camera GetCam(float now)
		{
			if ((Object)(object)_cam != (Object)null)
			{
				return _cam;
			}
			if (now < _nextCamSearch)
			{
				return null;
			}
			_nextCamSearch = now + 1f;
			try
			{
				Il2CppReferenceArray<Camera> allCameras = Camera.allCameras;
				for (int i = 0; i < ((Il2CppArrayBase<Camera>)(object)allCameras).Length; i++)
				{
					Camera val = ((Il2CppArrayBase<Camera>)(object)allCameras)[i];
					if ((Object)(object)val != (Object)null && string.Equals(((Object)val).name, "Headset", StringComparison.OrdinalIgnoreCase))
					{
						_cam = val;
						MelonLogger.Msg("Camera: " + ((Object)val).name);
						break;
					}
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("camera search failed: " + ex.Message);
			}
			return _cam;
		}

		private static float Smooth(float x)
		{
			x = Mathf.Clamp01(x);
			return x * x * (3f - 2f * x);
		}

		private static long Key(Camera c)
		{
			return ((Il2CppObjectBase)c).Pointer.ToInt64();
		}

		private static void RefreshTargets(float now, bool force)
		{
			if (!force && now < _nextRefresh)
			{
				return;
			}
			_nextRefresh = now + 0.2f;
			_targets.Clear();
			try
			{
				Il2CppReferenceArray<Camera> allCameras = Camera.allCameras;
				StringBuilder stringBuilder = new StringBuilder();
				for (int i = 0; i < ((Il2CppArrayBase<Camera>)(object)allCameras).Length; i++)
				{
					Camera val = ((Il2CppArrayBase<Camera>)(object)allCameras)[i];
					if (!((Object)(object)val == (Object)null) && !((Object)(object)val.targetTexture != (Object)null))
					{
						_targets.Add(val);
						if (stringBuilder.Length > 0)
						{
							stringBuilder.Append(", ");
						}
						stringBuilder.Append(((Object)val).name);
					}
				}
				string text = stringBuilder.ToString();
				if (text != _lastTargets)
				{
					_lastTargets = text;
					MelonLogger.Msg("Screen cameras: " + ((text.Length > 0) ? text : "none"));
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("camera list failed: " + ex.Message);
			}
		}

		private static void ApplyVisual(float now)
		{
			bool flag = _vis <= 0.0001f;
			if (!flag || _states.Count != 0 || _overlays.Count != 0)
			{
				float a = Smooth(_vis);
				if (!flag)
				{
					RefreshTargets(now, force: false);
				}
				if (_vis >= 0.999f)
				{
					BlackAll();
				}
				else
				{
					RestoreAll();
				}
				UpdateOverlays(a, flag);
			}
		}

		private static void UpdateOverlays(float a, bool idle)
		{
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			if (_overlays.Count > 0)
			{
				foreach (Overlay value2 in _overlays.Values)
				{
					if ((Object)(object)value2.mr != (Object)null && ((Renderer)value2.mr).enabled)
					{
						((Renderer)value2.mr).enabled = false;
					}
				}
			}
			if (idle || a <= 0.002f || _visualBad)
			{
				return;
			}
			try
			{
				if ((Object)(object)_mesh == (Object)null)
				{
					BuildShared();
				}
				_mat.color = new Color(0f, 0f, 0f, a);
				for (int i = 0; i < _targets.Count; i++)
				{
					Camera val = _targets[i];
					if (!((Object)(object)val == (Object)null))
					{
						long key = Key(val);
						if (!_overlays.TryGetValue(key, out var value) || (Object)(object)value.go == (Object)null)
						{
							value = MakeOverlay();
							_overlays[key] = value;
						}
						Transform transform = ((Component)val).transform;
						value.go.transform.position = transform.position + transform.forward * 0.3f;
						value.go.transform.rotation = transform.rotation;
						((Renderer)value.mr).enabled = true;
					}
				}
			}
			catch (Exception ex)
			{
				_visualBad = true;
				MelonLogger.Error("black overlay failed: " + (ex.InnerException ?? ex));
			}
		}

		private static void BuildShared()
		{
			//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_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_0024: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: 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)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//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_013c: 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_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("DF_tmp");
			_mat = new Material(((Renderer)val.AddComponent<SpriteRenderer>()).sharedMaterial);
			Object.Destroy((Object)val);
			((Object)_mat).hideFlags = (HideFlags)32;
			_mat.renderQueue = 4000;
			_mat.color = new Color(0f, 0f, 0f, 0f);
			_mesh = new Mesh();
			((Object)_mesh).hideFlags = (HideFlags)32;
			_mesh.vertices = new Il2CppStructArray<Vector3>((Vector3[])(object)new Vector3[4]
			{
				new Vector3(-1f, -1f, 0f),
				new Vector3(1f, -1f, 0f),
				new Vector3(1f, 1f, 0f),
				new Vector3(-1f, 1f, 0f)
			});
			_mesh.uv = new Il2CppStructArray<Vector2>((Vector2[])(object)new Vector2[4]
			{
				new Vector2(0f, 0f),
				new Vector2(1f, 0f),
				new Vector2(1f, 1f),
				new Vector2(0f, 1f)
			});
			_mesh.triangles = new Il2CppStructArray<int>(new int[6] { 0, 2, 1, 0, 3, 2 });
			_mesh.bounds = new Bounds(Vector3.zero, new Vector3(4f, 4f, 1f));
			MelonLogger.Msg("Black overlay created.");
		}

		private static Overlay MakeOverlay()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			Overlay obj = new Overlay
			{
				go = new GameObject("DF_Overlay")
			};
			obj.go.layer = 2;
			Object.DontDestroyOnLoad((Object)(object)obj.go);
			obj.go.transform.localScale = new Vector3(2.5f, 2.5f, 1f);
			Component obj2 = (Component)(object)obj.go.AddComponent<MeshFilter>();
			PropertyInfo? obj3 = typeof(MeshFilter).GetProperty("sharedMesh") ?? typeof(MeshFilter).GetProperty("mesh");
			if (obj3 == null)
			{
				throw new Exception("MeshFilter mesh property not found");
			}
			obj3.SetValue(obj2, _mesh, null);
			obj.mr = obj.go.AddComponent<MeshRenderer>();
			((Renderer)obj.mr).sharedMaterial = _mat;
			((Renderer)obj.mr).shadowCastingMode = (ShadowCastingMode)0;
			((Renderer)obj.mr).enabled = false;
			return obj;
		}

		private static void ApplyAudio(float now)
		{
			if (_cut <= 0.0005f && _sil <= 0.0005f)
			{
				if ((Object)(object)_lp != (Object)null && ((Behaviour)_lp).enabled)
				{
					((Behaviour)_lp).enabled = false;
				}
				if (_volCaptured)
				{
					SetVol(_origVol);
					_volCaptured = false;
				}
				return;
			}
			if (!_volCaptured)
			{
				_origVol = GetVol();
				_volCaptured = true;
			}
			SetVol(_origVol * (1f - Smooth(_sil)));
			if (_filterBad)
			{
				return;
			}
			try
			{
				Camera cam = GetCam(now);
				if ((Object)(object)cam == (Object)null)
				{
					return;
				}
				if ((Object)(object)_lp == (Object)null || (Object)(object)_lpOwner != (Object)(object)((Component)cam).gameObject)
				{
					_lpOwner = ((Component)cam).gameObject;
					_lp = _lpOwner.AddComponent<AudioLowPassFilter>();
					if ((Object)(object)_lp == (Object)null)
					{
						_lp = _lpOwner.GetComponent<AudioLowPassFilter>();
					}
					if ((Object)(object)_lp == (Object)null)
					{
						throw new Exception("AudioLowPassFilter could not be added");
					}
					MelonLogger.Msg("Low-pass filter attached to " + ((Object)_lpOwner).name);
				}
				float cutoffFrequency = Mathf.Exp(Mathf.Lerp(Mathf.Log(22000f), Mathf.Log(700f), Smooth(_cut)));
				_lp.cutoffFrequency = cutoffFrequency;
				if (!((Behaviour)_lp).enabled)
				{
					((Behaviour)_lp).enabled = true;
				}
			}
			catch (Exception ex)
			{
				_filterBad = true;
				MelonLogger.Error("low-pass filter failed (volume fade still works): " + (ex.InnerException ?? ex));
			}
		}

		private static bool CamProps()
		{
			if (!_camPropsSearched)
			{
				_camPropsSearched = true;
				Type? typeFromHandle = typeof(Camera);
				_pMask = typeFromHandle.GetProperty("cullingMask");
				_pFlags = typeFromHandle.GetProperty("clearFlags");
				_pBg = typeFromHandle.GetProperty("backgroundColor");
				_camPropsBad = _pMask == null || _pFlags == null || _pBg == null;
				if (_camPropsBad)
				{
					MelonLogger.Warning("Camera cullingMask/clearFlags/backgroundColor not found - using only the overlay.");
				}
			}
			return !_camPropsBad;
		}

		private static void BlackAll()
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			if (!CamProps())
			{
				return;
			}
			for (int i = 0; i < _targets.Count; i++)
			{
				Camera val = _targets[i];
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				try
				{
					long key = Key(val);
					if (!_states.TryGetValue(key, out var value) || (Object)(object)value.cam != (Object)(object)val)
					{
						value = new CamState();
						value.cam = val;
						value.mask = Convert.ToInt32(_pMask.GetValue(val, null));
						value.flags = _pFlags.GetValue(val, null);
						value.bg = (Color)_pBg.GetValue(val, null);
						_states[key] = value;
						MelonLogger.Msg("Camera '" + ((Object)val).name + "' set to full black (old mask " + value.mask + ").");
					}
					_pMask.SetValue(val, 0, null);
					_pFlags.SetValue(val, Enum.ToObject(_pFlags.PropertyType, 2), null);
					_pBg.SetValue(val, (object)new Color(0f, 0f, 0f, 1f), null);
				}
				catch (Exception ex)
				{
					_camPropsBad = true;
					MelonLogger.Error("camera black failed: " + (ex.InnerException ?? ex));
					break;
				}
			}
		}

		private static void RestoreAll()
		{
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			if (_states.Count == 0)
			{
				return;
			}
			try
			{
				if (CamProps())
				{
					foreach (CamState value in _states.Values)
					{
						if (!((Object)(object)value.cam == (Object)null))
						{
							_pMask.SetValue(value.cam, value.mask, null);
							_pFlags.SetValue(value.cam, value.flags, null);
							_pBg.SetValue(value.cam, value.bg, null);
						}
					}
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Error("camera restore failed: " + (ex.InnerException ?? ex));
			}
			_states.Clear();
			MelonLogger.Msg("Cameras restored.");
		}

		private static void ForceRagdoll()
		{
			try
			{
				object physicsRig = Player.PhysicsRig;
				if (physicsRig == null)
				{
					MelonLogger.Warning("PhysicsRig is null - cannot ragdoll.");
					return;
				}
				Type type = physicsRig.GetType();
				MethodInfo methodInfo = null;
				MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (MethodInfo methodInfo2 in methods)
				{
					if (string.Equals(methodInfo2.Name, "RagdollRig", StringComparison.OrdinalIgnoreCase) && methodInfo2.GetParameters().Length == 0)
					{
						methodInfo = methodInfo2;
						break;
					}
				}
				if (methodInfo == null)
				{
					MelonLogger.Warning("RagdollRig() not found on " + type.Name);
					return;
				}
				methodInfo.Invoke(physicsRig, null);
				MelonLogger.Msg("Ragdoll forced via " + type.Name + ".RagdollRig()");
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("ragdoll failed: " + (ex.InnerException ?? ex).Message);
			}
		}

		private static PropertyInfo VolProp()
		{
			if (!_volSearched)
			{
				_volSearched = true;
				_volProp = typeof(AudioListener).GetProperty("volume", BindingFlags.Static | BindingFlags.Public);
				if (_volProp == null)
				{
					MelonLogger.Warning("AudioListener.volume not found - only the low-pass filter will be used.");
				}
			}
			return _volProp;
		}

		private static float GetVol()
		{
			try
			{
				PropertyInfo propertyInfo = VolProp();
				if (propertyInfo != null)
				{
					return Convert.ToSingle(propertyInfo.GetValue(null, null));
				}
			}
			catch (Exception)
			{
			}
			return 1f;
		}

		private static void SetVol(float v)
		{
			try
			{
				PropertyInfo propertyInfo = VolProp();
				if (propertyInfo != null)
				{
					propertyInfo.SetValue(null, v, null);
				}
			}
			catch (Exception)
			{
			}
		}
	}
	public class Main : MelonMod
	{
		private float _nextErrorLog;

		public override void OnInitializeMelon()
		{
			Settings.Init();
			Hooking.OnLevelLoaded += delegate
			{
				FadeManager.OnLevelLoaded();
			};
			Hooking.OnLevelUnloaded += FadeManager.OnLevelUnloaded;
			Settings.BuildMenu();
			MelonLogger.Msg("DeathFade loaded..");
		}

		public override void OnLateUpdate()
		{
			try
			{
				FadeManager.Tick();
				Settings.Tick();
			}
			catch (Exception ex)
			{
				if (Time.realtimeSinceStartup > _nextErrorLog)
				{
					_nextErrorLog = Time.realtimeSinceStartup + 5f;
					MelonLogger.Error("tick error: " + ex);
				}
			}
		}
	}
	public static class Settings
	{
		public const float ScreenFadeMs = 3f;

		public const float SoundFadeTime = 0.4f;

		private static MelonPreferences_Category _cat;

		private static MelonPreferences_Entry<bool> _enabled;

		private static bool _dirty;

		private static float _saveAt;

		public static bool Enabled => _enabled.Value;

		public static void Init()
		{
			_cat = MelonPreferences.CreateCategory("DeathFade");
			_enabled = _cat.CreateEntry<bool>("Enabled", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		}

		public static void BuildMenu()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			Page.Root.CreatePage("DeathFade", Color.red, 0, true).CreateBool("Enabled", Color.white, _enabled.Value, (Action<bool>)delegate(bool v)
			{
				_enabled.Value = v;
				_dirty = true;
				_saveAt = Time.realtimeSinceStartup + 1.5f;
			});
		}

		public static void Tick()
		{
			if (_dirty && Time.realtimeSinceStartup >= _saveAt)
			{
				_dirty = false;
				MelonPreferences.Save();
			}
		}
	}
}