Decompiled source of FogDistance v1.3.2

plugins/PeakFogDistance.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using Zorro.Core;

[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("Codex for BroneKot")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Shows the vertical distance from the local player to rising fog or final-segment lava in PEAK.")]
[assembly: AssemblyFileVersion("1.3.2.0")]
[assembly: AssemblyInformationalVersion("1.3.2")]
[assembly: AssemblyProduct("PeakFogDistance")]
[assembly: AssemblyTitle("PeakFogDistance")]
[assembly: AssemblyVersion("1.3.2.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 PeakFogDistance
{
	[BepInPlugin("com.bronekot.peak.fogdistance", "PEAK Fog Distance", "1.3.2")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.bronekot.peak.fogdistance";

		public const string PluginName = "PEAK Fog Distance";

		public const string PluginVersion = "1.3.2";

		private const float LookupIntervalSeconds = 1f;

		private const float WarningDistance = 10f;

		private const float LeftMargin = 18f;

		private const float TopMargin = 18f;

		private const float LabelWidth = 280f;

		private const float LabelHeight = 58f;

		private static readonly FieldInfo FlatFogWaitingField = typeof(Fog).GetField("waiting", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly FieldInfo FlatFogWaitTimeField = typeof(Fog).GetField("sinceStop", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly FieldInfo LegacyLavaMovingField = typeof(MovingLava).GetField("timeToMove", BindingFlags.Instance | BindingFlags.NonPublic);

		private readonly Color _safeColor = new Color(1f, 1f, 1f, 0.95f);

		private readonly Color _warningColor = new Color(1f, 0.72f, 0.2f, 1f);

		private readonly Color _dangerColor = new Color(1f, 0.28f, 0.22f, 1f);

		private Fog _flatFog;

		private FogSphere _sphereFog;

		private OrbFogHandler _sphereFogHandler;

		private LavaRising _risingLava;

		private MovingLava _legacyRisingLava;

		private GUIStyle _labelStyle;

		private Font _labelFont;

		private float _nextLookupAt;

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PEAK Fog Distance 1.3.2 loaded.");
		}

		private void Update()
		{
			if (!(Time.unscaledTime < _nextLookupAt))
			{
				_nextLookupAt = Time.unscaledTime + 1f;
				if ((Object)(object)_flatFog == (Object)null)
				{
					_flatFog = Object.FindFirstObjectByType<Fog>();
				}
				if ((Object)(object)_sphereFog == (Object)null)
				{
					_sphereFog = Object.FindFirstObjectByType<FogSphere>();
				}
				if ((Object)(object)_sphereFogHandler == (Object)null)
				{
					_sphereFogHandler = Object.FindFirstObjectByType<OrbFogHandler>();
				}
				if ((Object)(object)_risingLava == (Object)null)
				{
					_risingLava = Object.FindFirstObjectByType<LavaRising>();
				}
				if ((Object)(object)_legacyRisingLava == (Object)null)
				{
					_legacyRisingLava = Object.FindFirstObjectByType<MovingLava>();
				}
				TryUsePeakFont();
			}
		}

		private void OnGUI()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: 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_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			Character localCharacter = Character.localCharacter;
			if (!((Object)(object)localCharacter == (Object)null))
			{
				Character val = Character.observedCharacter;
				if ((Object)(object)val == (Object)null)
				{
					val = localCharacter;
				}
				if (TryGetHazardInfo(val.Center, out var distance, out var isInDanger, out var isLava, out var timerText))
				{
					EnsureStyle();
					string text = ((!isInDanger) ? (isLava ? $"LAVA: {distance:0.0} m" : $"FOG: {distance:0.0} m") : (isLava ? "LAVA: DANGER" : "FOG: DANGER")) + "\n" + timerText;
					Color color = (isInDanger ? _dangerColor : ((distance <= 10f) ? _warningColor : _safeColor));
					Rect val2 = default(Rect);
					((Rect)(ref val2))..ctor(18f, 18f, 280f, 58f);
					Color color2 = GUI.color;
					GUI.color = new Color(0f, 0f, 0f, 0.8f);
					GUI.Label(new Rect(((Rect)(ref val2)).x + 2f, ((Rect)(ref val2)).y + 2f, ((Rect)(ref val2)).width, ((Rect)(ref val2)).height), text, _labelStyle);
					GUI.color = color;
					GUI.Label(val2, text, _labelStyle);
					GUI.color = color2;
				}
			}
		}

		private bool TryGetHazardInfo(Vector3 playerPosition, out float distance, out bool isInDanger, out bool isLava, out string timerText)
		{
			//IL_017b: 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_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: 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_0038: 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)
			if (IsFinalLavaSegment())
			{
				if ((Object)(object)_risingLava != (Object)null && ((Behaviour)_risingLava).isActiveAndEnabled && (Object)(object)_risingLava.lava != (Object)null)
				{
					SetLavaDistance(playerPosition.y, _risingLava.lava.position.y, out distance, out isInDanger);
					isLava = true;
					timerText = GetRisingLavaTimer(distance);
					return true;
				}
				if ((Object)(object)_legacyRisingLava != (Object)null && ((Behaviour)_legacyRisingLava).isActiveAndEnabled)
				{
					SetLavaDistance(playerPosition.y, ((Component)_legacyRisingLava).transform.position.y, out distance, out isInDanger);
					isLava = true;
					timerText = GetLegacyLavaTimer(distance);
					return true;
				}
			}
			if ((Object)(object)_sphereFog != (Object)null && ((Behaviour)_sphereFog).isActiveAndEnabled && Ascents.fogEnabled)
			{
				float num = Mathf.Max(0f, _sphereFog.currentSize);
				float num2 = Vector3.Distance(playerPosition, _sphereFog.fogPoint);
				bool flag = Mathf.Approximately(_sphereFog.ENABLE, 1f);
				if (num <= 0f || num2 >= num)
				{
					distance = 0f;
					isInDanger = flag;
					isLava = false;
					timerText = GetSphereFogTimer(num, num2);
					return true;
				}
				distance = num - num2;
				isInDanger = false;
				isLava = false;
				timerText = GetSphereFogTimer(num, num2);
				return true;
			}
			if ((Object)(object)_flatFog != (Object)null && ((Behaviour)_flatFog).isActiveAndEnabled)
			{
				distance = playerPosition.y - ((Component)_flatFog).transform.position.y;
				isInDanger = distance < 0f;
				isLava = false;
				distance = Mathf.Max(0f, distance);
				timerText = GetFlatFogTimer(distance);
				return true;
			}
			distance = 0f;
			isInDanger = false;
			isLava = false;
			timerText = null;
			return false;
		}

		private string GetSphereFogTimer(float radius, float radialDistance)
		{
			if ((Object)(object)_sphereFogHandler == (Object)null || !((Behaviour)_sphereFogHandler).isActiveAndEnabled)
			{
				return "ETA: --:--";
			}
			if (!_sphereFogHandler.isMoving)
			{
				if (_sphereFogHandler.hasArrived)
				{
					return "ETA: --:--";
				}
				if (_sphereFogHandler.currentID == 0)
				{
					return "START: WAITING";
				}
				float seconds = Mathf.Max(0f, _sphereFogHandler.maxWaitTime - _sphereFogHandler.currentWaitTime);
				return "START: " + FormatTime(seconds);
			}
			float num = Mathf.Abs(_sphereFogHandler.speed);
			float num2 = Mathf.Max(0f, radius - radialDistance);
			if (!(num > 0.001f))
			{
				return "ETA: --:--";
			}
			return "ETA: " + FormatTime(num2 / num);
		}

		private string GetFlatFogTimer(float distance)
		{
			if (ReadPrivateBool(FlatFogWaitingField, _flatFog))
			{
				float num = ReadPrivateFloat(FlatFogWaitTimeField, _flatFog);
				return "START: " + FormatTime(Mathf.Max(0f, _flatFog.maxWaitTime - num));
			}
			float num2 = Mathf.Abs(_flatFog.fogSpeed);
			if (!(num2 > 0.001f))
			{
				return "ETA: --:--";
			}
			return "ETA: " + FormatTime(distance / num2);
		}

		private string GetRisingLavaTimer(float distance)
		{
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			if (!_risingLava.started)
			{
				float seconds = Mathf.Max(0f, _risingLava.initialWaitTime - _risingLava.secondsWaitedToStart);
				return "START: " + FormatTime(seconds);
			}
			if (_risingLava.ended || (Object)(object)_risingLava.topTransform == (Object)null)
			{
				return "ETA: --:--";
			}
			float num = Mathf.Max(0f, _risingLava.travelTime - _risingLava.timeTraveled);
			float num2 = _risingLava.topTransform.position.y - _risingLava.lava.position.y;
			float num3 = ((num > 0.001f) ? (num2 / num) : 0f);
			if (!(num3 > 0.001f))
			{
				return "ETA: --:--";
			}
			return "ETA: " + FormatTime(distance / num3);
		}

		private string GetLegacyLavaTimer(float distance)
		{
			if (!ReadPrivateBool(LegacyLavaMovingField, _legacyRisingLava))
			{
				return "START: WAITING";
			}
			float num = Mathf.Abs(_legacyRisingLava.speed);
			if (!(num > 0.001f))
			{
				return "ETA: --:--";
			}
			return "ETA: " + FormatTime(distance / num);
		}

		private static bool ReadPrivateBool(FieldInfo field, object instance)
		{
			bool flag = default(bool);
			int num;
			if (field != null && instance != null)
			{
				object value = field.GetValue(instance);
				if (value is bool)
				{
					flag = (bool)value;
					num = 1;
				}
				else
				{
					num = 0;
				}
			}
			else
			{
				num = 0;
			}
			return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
		}

		private static float ReadPrivateFloat(FieldInfo field, object instance)
		{
			if (field != null && instance != null)
			{
				object value = field.GetValue(instance);
				if (value is float)
				{
					return (float)value;
				}
			}
			return 0f;
		}

		private static string FormatTime(float seconds)
		{
			if (float.IsNaN(seconds) || float.IsInfinity(seconds))
			{
				return "--:--";
			}
			int num = Mathf.Max(0, Mathf.CeilToInt(seconds));
			return $"{num / 60:00}:{num % 60:00}";
		}

		private static bool IsFinalLavaSegment()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Invalid comparison between Unknown and I4
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Invalid comparison between Unknown and I4
			MapHandler instance = Singleton<MapHandler>.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return false;
			}
			Segment currentSegment = instance.GetCurrentSegment();
			if ((int)currentSegment != 4)
			{
				return (int)currentSegment == 5;
			}
			return true;
		}

		private static void SetLavaDistance(float playerCenterHeight, float lavaHeight, out float distance, out bool isInLava)
		{
			distance = playerCenterHeight - 0.5f - lavaHeight;
			isInLava = distance <= 0f;
			distance = Mathf.Max(0f, distance);
		}

		private void EnsureStyle()
		{
			//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_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: 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_0047: Expected O, but got Unknown
			if (_labelStyle == null)
			{
				_labelStyle = new GUIStyle(GUI.skin.label)
				{
					alignment = (TextAnchor)0,
					font = _labelFont,
					fontSize = 20,
					fontStyle = (FontStyle)1,
					clipping = (TextClipping)0
				};
			}
		}

		private void TryUsePeakFont()
		{
			if ((Object)(object)_labelFont != (Object)null)
			{
				return;
			}
			TMP_FontAsset[] array = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
			foreach (TMP_FontAsset val in array)
			{
				if (!((Object)(object)val == (Object)null) && ((Object)val).name.IndexOf("Binggrae", StringComparison.OrdinalIgnoreCase) >= 0 && !((Object)(object)val.sourceFontFile == (Object)null))
				{
					_labelFont = val.sourceFontFile;
					if (_labelStyle != null)
					{
						_labelStyle.font = _labelFont;
					}
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Using PEAK font asset '" + ((Object)val).name + "'."));
					break;
				}
			}
		}
	}
}