Decompiled source of Meterheim v1.0.0

Meterheim.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[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("Meterheim")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Meterheim")]
[assembly: AssemblyTitle("Meterheim")]
[assembly: AssemblyVersion("1.0.0.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 Meterheim
{
	internal static class InputGuard
	{
		internal static bool AllowMeasurementKeys()
		{
			Player localPlayer = Player.m_localPlayer;
			if (!Object.op_Implicit((Object)(object)localPlayer) || ((Character)localPlayer).IsDead() || ((Character)localPlayer).IsTeleporting() || ((Character)localPlayer).InIntro() || ((Character)localPlayer).InCutscene())
			{
				return false;
			}
			if ((Object)(object)Game.instance == (Object)null || Game.instance.IsShuttingDown() || Game.instance.WaitingForRespawn())
			{
				return false;
			}
			GameObject val = (((Object)(object)EventSystem.current != (Object)null) ? EventSystem.current.currentSelectedGameObject : null);
			if (Object.op_Implicit((Object)(object)val) && (Object.op_Implicit((Object)(object)val.GetComponent<InputField>()) || Object.op_Implicit((Object)(object)val.GetComponent<TMP_InputField>())))
			{
				return false;
			}
			return true;
		}
	}
	internal sealed class MeasurementEngine
	{
		private Player player;

		private long profileId;

		private bool hasBaseline;

		private Vector3 lastPosition;

		private Vector3 startPosition;

		private float lastSampleTime;

		private bool lastTeleporting;

		private Ship ship;

		private bool hasShipBaseline;

		private Vector3 lastShipPosition;

		private double shipDiagnosticMeters;

		private double rejectedIdleRawMeters;

		private double rejectedFurnitureMeters;

		private double rejectedShipRockMeters;

		private bool lastFurnitureIdle;

		private bool lastAboardShip;

		private Vector3 lastRawPosition;

		private bool hasRawPosition;

		private Vector3 shipTravelAnchor;

		private bool hasShipTravelAnchor;

		internal bool Running { get; private set; }

		internal bool Interrupted { get; private set; }

		internal double PathMeters { get; private set; }

		internal double DisplacementMeters { get; private set; }

		internal Vector3 Position { get; private set; }

		internal bool HasPlayer { get; private set; }

		internal string Motion { get; private set; } = "No player";

		internal string ShipStatus { get; private set; } = "None";

		internal double ShipDiagnosticMeters => shipDiagnosticMeters;

		internal double RejectedIdleRawMeters => rejectedIdleRawMeters;

		internal double RejectedFurnitureMeters => rejectedFurnitureMeters;

		internal double RejectedShipRockMeters => rejectedShipRockMeters;

		internal void Toggle()
		{
			if (Running)
			{
				Running = false;
				hasBaseline = false;
				Squirrel.Note("Manual measurement paused");
			}
			else if (HasPlayer)
			{
				Running = true;
				Interrupted = false;
				hasBaseline = false;
				DisplacementMeters = 0.0;
				Squirrel.Note("Manual measurement started/resumed; new displacement segment");
			}
		}

		internal void Reset()
		{
			Running = false;
			Interrupted = false;
			PathMeters = 0.0;
			DisplacementMeters = 0.0;
			hasBaseline = false;
			rejectedIdleRawMeters = 0.0;
			rejectedFurnitureMeters = 0.0;
			rejectedShipRockMeters = 0.0;
			lastFurnitureIdle = false;
			lastAboardShip = false;
			hasRawPosition = false;
			hasShipTravelAnchor = false;
			Squirrel.Note("Manual measurement reset");
		}

		internal void Sample()
		{
			//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_0117: 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_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0281: Unknown result type (might be due to invalid IL or missing references)
			//IL_0286: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_036a: Unknown result type (might be due to invalid IL or missing references)
			//IL_036f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0379: Unknown result type (might be due to invalid IL or missing references)
			//IL_037e: Unknown result type (might be due to invalid IL or missing references)
			//IL_033f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0341: Unknown result type (might be due to invalid IL or missing references)
			//IL_0347: Unknown result type (might be due to invalid IL or missing references)
			//IL_0349: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0314: Unknown result type (might be due to invalid IL or missing references)
			//IL_0316: Unknown result type (might be due to invalid IL or missing references)
			//IL_0685: Unknown result type (might be due to invalid IL or missing references)
			//IL_0673: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_068a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0698: Unknown result type (might be due to invalid IL or missing references)
			//IL_0690: Unknown result type (might be due to invalid IL or missing references)
			//IL_069d: Unknown result type (might be due to invalid IL or missing references)
			//IL_06d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_06d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_06db: Unknown result type (might be due to invalid IL or missing references)
			//IL_0451: Unknown result type (might be due to invalid IL or missing references)
			//IL_0461: Unknown result type (might be due to invalid IL or missing references)
			//IL_0540: Unknown result type (might be due to invalid IL or missing references)
			//IL_0542: Unknown result type (might be due to invalid IL or missing references)
			//IL_0560: Unknown result type (might be due to invalid IL or missing references)
			//IL_0562: Unknown result type (might be due to invalid IL or missing references)
			//IL_0571: Unknown result type (might be due to invalid IL or missing references)
			//IL_0573: Unknown result type (might be due to invalid IL or missing references)
			//IL_0611: Unknown result type (might be due to invalid IL or missing references)
			//IL_0616: Unknown result type (might be due to invalid IL or missing references)
			//IL_0623: Unknown result type (might be due to invalid IL or missing references)
			//IL_0625: Unknown result type (might be due to invalid IL or missing references)
			//IL_0642: Unknown result type (might be due to invalid IL or missing references)
			//IL_0647: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			Game instance = Game.instance;
			if (!Object.op_Implicit((Object)(object)localPlayer) || !((Object)(object)instance != (Object)null) || instance.IsShuttingDown() || instance.WaitingForRespawn())
			{
				Invalidate("Missing player / session transition", pauseManual: true);
				return;
			}
			PlayerProfile playerProfile = instance.GetPlayerProfile();
			long num = ((playerProfile != null) ? playerProfile.GetPlayerID() : 0);
			if (num == 0L || localPlayer.GetPlayerID() != num)
			{
				Invalidate("Player identity not ready", pauseManual: true);
				return;
			}
			if ((Object)(object)player != (Object)(object)localPlayer || profileId != num)
			{
				player = localPlayer;
				profileId = num;
				Invalidate("Bound to player instance / character " + num, pauseManual: true);
			}
			bool flag = ((Character)localPlayer).IsTeleporting();
			if (flag || ((Character)localPlayer).IsDead() || ((Character)localPlayer).InIntro() || ((Character)localPlayer).InCutscene())
			{
				Invalidate(flag ? "Teleporting" : "Player unavailable", pauseManual: true);
				lastTeleporting = flag;
				return;
			}
			if (lastTeleporting)
			{
				Invalidate("Teleport finished; rebaseline", pauseManual: true);
				lastTeleporting = false;
			}
			Vector3 position = ((Component)localPlayer).transform.position;
			if (!Finite(position))
			{
				Invalidate("Nonfinite player position", pauseManual: true);
				return;
			}
			Position = position;
			double num2 = (hasRawPosition ? Horizontal(lastRawPosition, position) : 0.0);
			lastRawPosition = position;
			hasRawPosition = true;
			HasPlayer = true;
			Motion = (((Character)localPlayer).IsRiding() ? "Riding" : (((Character)localPlayer).IsSwimming() ? "Swimming" : (((Character)localPlayer).IsDebugFlying() ? "Debug flying" : (((Character)localPlayer).IsAttached() ? "Attached" : ((!((Character)localPlayer).IsOnGround()) ? "Airborne" : (((Character)localPlayer).IsRunning() ? "Running" : "Foot (non-run)"))))));
			Ship val = ResolveShip(localPlayer);
			if ((Object)(object)val != (Object)(object)ship)
			{
				ship = val;
				hasShipBaseline = false;
				hasShipTravelAnchor = false;
				Squirrel.Note("Ship reference changed: " + (Object.op_Implicit((Object)(object)ship) ? ((Object)ship).name : "none"));
			}
			ShipStatus = ((!Object.op_Implicit((Object)(object)ship)) ? "None" : (((Object)(object)localPlayer.GetControlledShip() == (Object)(object)ship) ? "Helm" : "Passenger/deck candidate"));
			if (Object.op_Implicit((Object)(object)ship))
			{
				Vector3 position2 = ((Component)ship).transform.position;
				if (Finite(position2))
				{
					if (!hasShipTravelAnchor)
					{
						shipTravelAnchor = position2;
						hasShipTravelAnchor = true;
					}
					if (hasShipBaseline && Time.unscaledTime - lastSampleTime < 1f)
					{
						double num3 = Horizontal(lastShipPosition, position2);
						if (num3 <= 50.0)
						{
							shipDiagnosticMeters += num3;
						}
						else
						{
							Squirrel.Note("Ship jump rejected: " + num3.ToString("F2") + "m");
						}
					}
					lastShipPosition = position2;
					hasShipBaseline = true;
				}
				else
				{
					hasShipBaseline = false;
				}
			}
			if (Running)
			{
				if (!hasBaseline)
				{
					startPosition = position;
					lastPosition = position;
					hasBaseline = true;
				}
				else
				{
					double num4 = Time.unscaledTime - lastSampleTime;
					double num5 = Horizontal(lastPosition, position);
					Vector3 moveDir = ((Character)localPlayer).GetMoveDir();
					bool flag2 = !Object.op_Implicit((Object)(object)ship) && !((Character)localPlayer).IsSwimming() && !((Character)localPlayer).IsRiding() && !((Character)localPlayer).IsAttached() && !((Character)localPlayer).IsDebugFlying() && ((Character)localPlayer).IsOnGround();
					bool flag3 = flag2 && ((Vector3)(ref moveDir)).sqrMagnitude < 0.0025f;
					bool flag4 = !Object.op_Implicit((Object)(object)ship) && ((Character)localPlayer).IsAttached() && !((Character)localPlayer).IsRiding() && !((Character)localPlayer).IsSwimming() && ((Vector3)(ref moveDir)).sqrMagnitude < 0.0025f;
					bool flag5 = Object.op_Implicit((Object)(object)ship) && !((Character)localPlayer).IsSwimming();
					bool flag6 = flag5 && !((Character)localPlayer).IsAttached() && ((Vector3)(ref moveDir)).sqrMagnitude >= 0.0025f;
					bool flag7 = flag5 && hasShipTravelAnchor && Horizontal(shipTravelAnchor, ((Component)ship).transform.position) >= 0.35;
					bool flag8 = flag4 != lastFurnitureIdle || flag5 != lastAboardShip;
					lastFurnitureIdle = flag4;
					lastAboardShip = flag5;
					if (num4 > 1.0 || num5 > Math.Max(5.0, 45.0 * num4))
					{
						Squirrel.Note("Position discontinuity rejected: " + num5.ToString("F2") + "m / " + num4.ToString("F3") + "s");
						Invalidate("Position discontinuity", pauseManual: true);
					}
					else if (flag3)
					{
						rejectedIdleRawMeters += num5;
						lastPosition = position;
					}
					else if (flag4)
					{
						rejectedFurnitureMeters += num5;
						lastPosition = position;
					}
					else if (flag8)
					{
						lastPosition = position;
					}
					else if (flag5 && !flag6 && !flag7)
					{
						rejectedShipRockMeters += num2;
					}
					else
					{
						double num6 = ((flag5 && !flag6) ? 0.35 : ((flag2 && (Object)(object)Plugin.Instance != (Object)null) ? Math.Max(0.0, Math.Min(0.25, Plugin.Instance.MinFootStep.Value)) : 0.0));
						if (num5 >= num6)
						{
							PathMeters += num5;
							DisplacementMeters = Horizontal(startPosition, position);
							lastPosition = position;
							if (flag5 && hasShipTravelAnchor)
							{
								shipTravelAnchor = ((Component)ship).transform.position;
							}
						}
					}
				}
			}
			lastSampleTime = Time.unscaledTime;
			bool flag9 = Object.op_Implicit((Object)(object)ship) && hasShipTravelAnchor;
			Vector3 shipPosition = (flag9 ? ((Component)ship).transform.position : Vector3.zero);
			Vector3 shipAnchor = (flag9 ? shipTravelAnchor : Vector3.zero);
			bool gamePaused = Time.timeScale == 0f;
			Squirrel.Snapshot(Motion, ShipStatus, PathMeters, DisplacementMeters, rejectedIdleRawMeters, rejectedFurnitureMeters, rejectedShipRockMeters, position, shipPosition, shipAnchor, flag9, Running, gamePaused);
		}

		private static Ship ResolveShip(Player p)
		{
			Ship controlledShip = p.GetControlledShip();
			if (Object.op_Implicit((Object)(object)controlledShip))
			{
				return controlledShip;
			}
			controlledShip = ((Character)p).GetStandingOnShip();
			if (Object.op_Implicit((Object)(object)controlledShip))
			{
				return controlledShip;
			}
			controlledShip = Ship.GetLocalShip();
			if (!Object.op_Implicit((Object)(object)controlledShip) || !controlledShip.IsPlayerInBoat(p))
			{
				return null;
			}
			return controlledShip;
		}

		private void Invalidate(string reason, bool pauseManual)
		{
			if (pauseManual && Running)
			{
				Running = false;
				Interrupted = true;
			}
			if (hasBaseline || hasShipBaseline || HasPlayer)
			{
				Squirrel.Note(reason);
			}
			hasBaseline = false;
			lastFurnitureIdle = false;
			lastAboardShip = false;
			hasRawPosition = false;
			hasShipTravelAnchor = false;
			hasShipBaseline = false;
			HasPlayer = false;
			Motion = "Unavailable";
			ShipStatus = "None";
			lastSampleTime = Time.unscaledTime;
		}

		private static double Horizontal(Vector3 a, Vector3 b)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			double num = (double)b.x - (double)a.x;
			double num2 = (double)b.z - (double)a.z;
			return Math.Sqrt(num * num + num2 * num2);
		}

		private static bool Finite(Vector3 p)
		{
			//IL_0000: 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)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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_0041: Unknown result type (might be due to invalid IL or missing references)
			if (!float.IsNaN(p.x) && !float.IsInfinity(p.x) && !float.IsNaN(p.y) && !float.IsInfinity(p.y) && !float.IsNaN(p.z))
			{
				return !float.IsInfinity(p.z);
			}
			return false;
		}
	}
	internal sealed class MeasurementHud
	{
		internal bool Enabled = true;

		private Hud owner;

		private GameObject root;

		private TextMeshProUGUI label;

		private Image background;

		private MeterheimDragHandler dragHandler;

		internal void Ensure()
		{
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: 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_0118: 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_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Expected O, but got Unknown
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0210: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			Hud instance = Hud.instance;
			if (!Object.op_Implicit((Object)(object)instance) || !Object.op_Implicit((Object)(object)instance.m_rootObject))
			{
				Dispose();
			}
			else if (!Object.op_Implicit((Object)(object)root) || !((Object)(object)owner == (Object)(object)instance))
			{
				Dispose();
				owner = instance;
				root = new GameObject("Meterheim_010_DiagnosticPanel", new Type[3]
				{
					typeof(RectTransform),
					typeof(CanvasRenderer),
					typeof(Image)
				});
				root.transform.SetParent(owner.m_rootObject.transform, false);
				RectTransform component = root.GetComponent<RectTransform>();
				component.anchorMin = new Vector2(1f, 1f);
				component.anchorMax = new Vector2(1f, 1f);
				component.pivot = new Vector2(1f, 1f);
				component.anchoredPosition = (Vector2)(((Object)(object)Plugin.Instance != (Object)null) ? Plugin.Instance.GetHudPosition() : new Vector2(-18f, -300f));
				component.sizeDelta = new Vector2(265f, 185f);
				background = root.GetComponent<Image>();
				((Graphic)background).color = new Color(0.1f, 0.085f, 0.07f, 0.79f);
				((Graphic)background).raycastTarget = false;
				dragHandler = root.AddComponent<MeterheimDragHandler>();
				dragHandler.Initialize(component);
				dragHandler.ClampCurrentPosition();
				GameObject val = new GameObject("Meterheim_Text", new Type[2]
				{
					typeof(RectTransform),
					typeof(CanvasRenderer)
				});
				val.SetActive(false);
				val.transform.SetParent(root.transform, false);
				RectTransform component2 = val.GetComponent<RectTransform>();
				component2.anchorMin = Vector2.zero;
				component2.anchorMax = Vector2.one;
				component2.offsetMin = new Vector2(12f, 8f);
				component2.offsetMax = new Vector2(-8f, -8f);
				label = val.AddComponent<TextMeshProUGUI>();
				TMP_Text val2 = (TMP_Text)(Object.op_Implicit((Object)(object)owner.m_hoverName) ? ((object)owner.m_hoverName) : ((object)owner.m_gpName));
				if (Object.op_Implicit((Object)(object)val2) && Object.op_Implicit((Object)(object)val2.font))
				{
					((TMP_Text)label).font = val2.font;
					((TMP_Text)label).fontSharedMaterial = val2.fontSharedMaterial;
				}
				((TMP_Text)label).fontSize = 16f;
				((TMP_Text)label).textWrappingMode = (TextWrappingModes)0;
				((TMP_Text)label).alignment = (TextAlignmentOptions)257;
				((Graphic)label).color = new Color(0.95f, 0.83f, 0.59f, 1f);
				((TMP_Text)label).richText = false;
				((Graphic)label).raycastTarget = false;
				val.SetActive(true);
			}
		}

		internal void UpdateDragAvailability()
		{
			if (Object.op_Implicit((Object)(object)background))
			{
				((Graphic)background).raycastTarget = Menu.IsVisible();
			}
		}

		internal void Refresh(MeasurementEngine measure, TargetMeasurementService target, bool coordinates, bool targetEnabled)
		{
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)root))
			{
				return;
			}
			bool flag = Enabled && Object.op_Implicit((Object)(object)owner) && owner.IsVisible() && measure.HasPlayer;
			if (root.activeSelf != flag)
			{
				root.SetActive(flag);
			}
			if (flag && Object.op_Implicit((Object)(object)label))
			{
				string text = (measure.Running ? "MEASURING" : (measure.Interrupted ? "INTERRUPTED" : "PAUSED"));
				string text2;
				string text3;
				if (!targetEnabled)
				{
					text2 = "OFF";
					text3 = "OFF";
				}
				else if (target.HasTarget)
				{
					text2 = Format(target.HorizontalMeters) + " m";
					text3 = target.TargetName;
				}
				else
				{
					text2 = "---";
					text3 = "---";
				}
				((TMP_Text)label).text = "METERHEIM 1.0.0  [" + text + "]\nTraveled: " + Format(measure.PathMeters) + " m\nDirect: " + Format(measure.DisplacementMeters) + " m\nTarget: " + text2 + "\nObject: " + text3 + "\nMotion: " + measure.Motion + "\nShip: " + measure.ShipStatus + " (" + Format(measure.ShipDiagnosticMeters) + " m)" + (coordinates ? ("\nXYZ: " + measure.Position.x.ToString("F1", CultureInfo.InvariantCulture) + ", " + measure.Position.y.ToString("F1", CultureInfo.InvariantCulture) + ", " + measure.Position.z.ToString("F1", CultureInfo.InvariantCulture)) : "");
			}
		}

		private static string Format(double value)
		{
			return value.ToString("F3", CultureInfo.InvariantCulture);
		}

		internal void Dispose()
		{
			if (Object.op_Implicit((Object)(object)root))
			{
				Object.Destroy((Object)(object)root);
			}
			root = null;
			label = null;
			background = null;
			dragHandler = null;
			owner = null;
		}
	}
	internal sealed class MeterheimDragHandler : MonoBehaviour, IBeginDragHandler, IEventSystemHandler, IDragHandler, IEndDragHandler
	{
		private RectTransform panel;

		private RectTransform parent;

		private Vector2 pointerAtStart;

		private Vector2 panelAtStart;

		private bool dragging;

		internal void Initialize(RectTransform target)
		{
			panel = target;
			ref RectTransform reference = ref parent;
			Transform obj = ((Transform)target).parent;
			reference = (RectTransform)(object)((obj is RectTransform) ? obj : null);
		}

		public void OnBeginDrag(PointerEventData eventData)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			dragging = false;
			if (Menu.IsVisible() && (int)eventData.button == 0 && Object.op_Implicit((Object)(object)parent) && RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, eventData.position, eventData.pressEventCamera, ref pointerAtStart))
			{
				panelAtStart = panel.anchoredPosition;
				dragging = true;
			}
		}

		public void OnDrag(PointerEventData eventData)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			Vector2 val = default(Vector2);
			if (dragging && Menu.IsVisible() && Object.op_Implicit((Object)(object)panel) && Object.op_Implicit((Object)(object)parent) && RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, eventData.position, eventData.pressEventCamera, ref val))
			{
				Vector2 position = panelAtStart + (val - pointerAtStart);
				panel.anchoredPosition = Clamp(position);
			}
		}

		public void OnEndDrag(PointerEventData eventData)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			if (dragging)
			{
				dragging = false;
				if (Object.op_Implicit((Object)(object)panel) && (Object)(object)Plugin.Instance != (Object)null)
				{
					Plugin.Instance.SaveHudPosition(panel.anchoredPosition);
				}
			}
		}

		internal void ClampCurrentPosition()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)panel))
			{
				panel.anchoredPosition = Clamp(panel.anchoredPosition);
			}
		}

		private Vector2 Clamp(Vector2 position)
		{
			//IL_004e: 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_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: 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)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)panel) && Object.op_Implicit((Object)(object)parent))
			{
				Rect rect = parent.rect;
				if (!(((Rect)(ref rect)).width <= 0f))
				{
					rect = parent.rect;
					if (!(((Rect)(ref rect)).height <= 0f))
					{
						rect = parent.rect;
						float width = ((Rect)(ref rect)).width;
						rect = panel.rect;
						float num = 0f - Mathf.Max(0f, width - ((Rect)(ref rect)).width);
						rect = parent.rect;
						float height = ((Rect)(ref rect)).height;
						rect = panel.rect;
						float num2 = 0f - Mathf.Max(0f, height - ((Rect)(ref rect)).height);
						return new Vector2(Mathf.Clamp(position.x, num, 0f), Mathf.Clamp(position.y, num2, 0f));
					}
				}
			}
			return position;
		}
	}
	[BepInPlugin("MaddCatter365.Meterheim", "Meterheim", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		internal ConfigEntry<bool> ModEnabled;

		internal ConfigEntry<KeyboardShortcut> ToggleMeasure;

		internal ConfigEntry<KeyboardShortcut> ResetMeasure;

		internal ConfigEntry<KeyboardShortcut> ToggleHud;

		internal ConfigEntry<bool> Diagnostics;

		internal ConfigEntry<bool> VerboseDiagnostics;

		internal ConfigEntry<bool> ShowCoordinates;

		internal ConfigEntry<float> HudPositionX;

		internal ConfigEntry<float> HudPositionY;

		internal ConfigEntry<bool> ResetHudPosition;

		internal ConfigEntry<float> MinFootStep;

		internal ConfigEntry<bool> TargetEnabled;

		internal ConfigEntry<float> TargetMaxRange;

		private readonly MeasurementEngine engine = new MeasurementEngine();

		private readonly MeasurementHud hud = new MeasurementHud();

		private readonly TargetMeasurementService target = new TargetMeasurementService();

		private float nextHudUpdate;

		internal static Plugin Instance { get; private set; }

		private void Awake()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Expected O, but got Unknown
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Expected O, but got Unknown
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Expected O, but got Unknown
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_0210: Expected O, but got Unknown
			Instance = this;
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("00 General", "Enabled", true, "Enable or disable all Meterheim measurement functions and the HUD.");
			ToggleMeasure = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("01 Controls", "StartPause", new KeyboardShortcut((KeyCode)291, Array.Empty<KeyCode>()), "Start, pause or resume a measurement.");
			ResetMeasure = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("01 Controls", "Reset", new KeyboardShortcut((KeyCode)291, (KeyCode[])(object)new KeyCode[1] { (KeyCode)304 }), "Reset the manual measurement.");
			ToggleHud = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("01 Controls", "ShowHide", new KeyboardShortcut((KeyCode)291, (KeyCode[])(object)new KeyCode[1] { (KeyCode)306 }), "Show or hide the measurement HUD.");
			HudPositionX = ((BaseUnityPlugin)this).Config.Bind<float>("02 HUD", "PositionX", -18f, new ConfigDescription("Saved HUD horizontal position.", (AcceptableValueBase)null, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = false
				}
			}));
			HudPositionY = ((BaseUnityPlugin)this).Config.Bind<float>("02 HUD", "PositionY", -300f, new ConfigDescription("Saved HUD vertical position.", (AcceptableValueBase)null, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = false
				}
			}));
			ResetHudPosition = ((BaseUnityPlugin)this).Config.Bind<bool>("02 HUD", "ResetHudPosition", false, new ConfigDescription("Restore the default HUD position on the next launch.", (AcceptableValueBase)null, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = false
				}
			}));
			MinFootStep = ((BaseUnityPlugin)this).Config.Bind<float>("03 Measurement", "FootStepAnchorMeters", 0.02f, "Minimum foot movement accumulated before the manual tape adds distance (meters). Default: 0.02.");
			TargetEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("04 Target Measurement", "Enabled", true, "Enable crosshair-to-surface distance measurement.");
			TargetMaxRange = ((BaseUnityPlugin)this).Config.Bind<float>("04 Target Measurement", "MaximumRangeMeters", 100f, new ConfigDescription("Maximum crosshair raycast distance in meters.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 500f), Array.Empty<object>()));
			if (ResetHudPosition.Value)
			{
				HudPositionX.Value = -18f;
				HudPositionY.Value = -300f;
				ResetHudPosition.Value = false;
			}
			Diagnostics = ((BaseUnityPlugin)this).Config.Bind<bool>("99 Diagnostics", "Enabled", false, "Log transitions, controls and rejected movement events.");
			VerboseDiagnostics = ((BaseUnityPlugin)this).Config.Bind<bool>("99 Diagnostics", "VerboseMovementSnapshots", false, "Log detailed movement snapshots every two seconds (very chatty).");
			ShowCoordinates = ((BaseUnityPlugin)this).Config.Bind<bool>("99 Diagnostics", "ShowCoordinates", true, "Show player coordinates in the diagnostic HUD.");
			Squirrel.Initialize(((BaseUnityPlugin)this).Logger, Diagnostics, VerboseDiagnostics);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Meterheim 1.0.0 loaded.");
		}

		private void Update()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			if (!ModEnabled.Value || !InputGuard.AllowMeasurementKeys())
			{
				return;
			}
			KeyboardShortcut value = ResetMeasure.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				engine.Reset();
				return;
			}
			value = ToggleHud.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				hud.Enabled = !hud.Enabled;
				return;
			}
			value = ToggleMeasure.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				engine.Toggle();
			}
		}

		private void LateUpdate()
		{
			if (!ModEnabled.Value)
			{
				if (engine.Running)
				{
					engine.Toggle();
				}
				target.Clear();
				hud.Dispose();
				return;
			}
			engine.Sample();
			target.Sample(TargetEnabled.Value, TargetMaxRange.Value);
			hud.Ensure();
			hud.UpdateDragAvailability();
			if (Time.unscaledTime >= nextHudUpdate)
			{
				nextHudUpdate = Time.unscaledTime + 0.15f;
				hud.Refresh(engine, target, ShowCoordinates.Value, TargetEnabled.Value);
			}
		}

		private void OnDestroy()
		{
			target.Clear();
			hud.Dispose();
			if ((Object)(object)Instance == (Object)(object)this)
			{
				Instance = null;
			}
		}

		internal Vector2 GetHudPosition()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			return new Vector2(HudPositionX.Value, HudPositionY.Value);
		}

		internal void SaveHudPosition(Vector2 position)
		{
			//IL_0006: 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)
			HudPositionX.Value = position.x;
			HudPositionY.Value = position.y;
		}
	}
	internal sealed class ConfigurationManagerAttributes
	{
		public bool? Browsable;
	}
	internal static class Squirrel
	{
		private static ManualLogSource logger;

		private static ConfigEntry<bool> enabled;

		private static ConfigEntry<bool> verbose;

		private static float nextSnapshot;

		internal static void Initialize(ManualLogSource source, ConfigEntry<bool> diagnosticToggle, ConfigEntry<bool> verboseToggle)
		{
			logger = source;
			enabled = diagnosticToggle;
			verbose = verboseToggle;
		}

		internal static void Note(string message)
		{
			if (enabled != null && enabled.Value)
			{
				ManualLogSource obj = logger;
				if (obj != null)
				{
					obj.LogInfo((object)("[Squirrel] " + message));
				}
			}
		}

		internal static void Snapshot(string motion, string ship, double path, double displacement, double rejectedIdle, double rejectedFurniture, double rejectedShipRock, Vector3 playerPosition, Vector3 shipPosition, Vector3 shipAnchor, bool hasShip, bool meterheimRunning, bool gamePaused)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: 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)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: 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_014b: 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_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			if (verbose != null && verbose.Value && !(Time.unscaledTime < nextSnapshot))
			{
				nextSnapshot = Time.unscaledTime + 2f;
				float num = (hasShip ? Vector2.Distance(new Vector2(shipPosition.x, shipPosition.z), new Vector2(shipAnchor.x, shipAnchor.z)) : 0f);
				ManualLogSource obj = logger;
				if (obj != null)
				{
					obj.LogInfo((object)("[Squirrel] Motion=" + motion + ", Ship=" + ship + ", " + $"MeterheimRunning={meterheimRunning}, GamePaused={gamePaused}, " + $"Path={path:F3}, Direct={displacement:F3}, " + $"IgnoredIdleRaw={rejectedIdle:F3}, " + $"IgnoredFurnitureRaw={rejectedFurniture:F3}, " + $"IgnoredShipRockRaw={rejectedShipRock:F3}, " + $"PlayerXZ=({playerPosition.x:F3}, {playerPosition.z:F3}), " + "ShipXZ=" + (hasShip ? $"({shipPosition.x:F3}, {shipPosition.z:F3})" : "None") + ", ShipAnchorXZ=" + (hasShip ? $"({shipAnchor.x:F3}, {shipAnchor.z:F3})" : "None") + ", " + $"ShipProgress={num:F3}"));
				}
			}
		}
	}
	internal sealed class TargetMeasurementService
	{
		private const float SampleInterval = 0.1f;

		private static readonly int SurfaceMask = LayerMask.GetMask(new string[12]
		{
			"Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "vehicle", "item", "character", "character_net",
			"character_ghost", "character_noenv"
		});

		private float nextSample;

		private Player lastPlayer;

		private Camera lastCamera;

		internal bool HasTarget { get; private set; }

		internal float HorizontalMeters { get; private set; }

		internal float ThreeDimensionalMeters { get; private set; }

		internal Vector3 SurfacePoint { get; private set; }

		internal string TargetName { get; private set; } = "---";

		internal void Sample(bool enabled, float maxRange)
		{
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: Unknown result type (might be due to invalid IL or missing references)
			//IL_0228: Unknown result type (might be due to invalid IL or missing references)
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0231: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0242: Unknown result type (might be due to invalid IL or missing references)
			//IL_0263: Unknown result type (might be due to invalid IL or missing references)
			//IL_0265: Unknown result type (might be due to invalid IL or missing references)
			//IL_0272: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			GameCamera instance = GameCamera.instance;
			Camera val = (Object.op_Implicit((Object)(object)instance) ? ((Component)instance).GetComponent<Camera>() : null);
			if (!enabled || !Object.op_Implicit((Object)(object)localPlayer) || !Object.op_Implicit((Object)(object)val) || !((Behaviour)val).isActiveAndEnabled || !Object.op_Implicit((Object)(object)Game.instance) || !Object.op_Implicit((Object)(object)Hud.instance) || !Hud.instance.IsVisible() || Hud.IsUserHidden() || Game.IsPaused() || ((Character)localPlayer).IsDead() || ((Character)localPlayer).IsTeleporting() || ((Character)localPlayer).InCutscene() || localPlayer.IsSleeping() || GameCamera.InFreeFly() || Menu.IsVisible() || InventoryGui.IsVisible() || Minimap.IsOpen() || Console.IsVisible() || TextInput.IsVisible() || StoreGui.IsVisible() || Hud.IsPieceSelectionVisible() || Hud.InRadial() || UnifiedPopup.IsVisible())
			{
				Clear();
				return;
			}
			if ((Object)(object)localPlayer != (Object)(object)lastPlayer || (Object)(object)val != (Object)(object)lastCamera)
			{
				Clear();
				lastPlayer = localPlayer;
				lastCamera = val;
			}
			if (Time.unscaledTime < nextSample)
			{
				return;
			}
			nextSample = Time.unscaledTime + 0.1f;
			ClearResult();
			RaycastHit[] array = Physics.RaycastAll(val.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)), Mathf.Max(1f, maxRange), SurfaceMask, (QueryTriggerInteraction)1);
			float num = float.PositiveInfinity;
			Vector3 val2 = Vector3.zero;
			Collider val3 = null;
			RaycastHit[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				RaycastHit val4 = array2[i];
				Collider collider = ((RaycastHit)(ref val4)).collider;
				if (Object.op_Implicit((Object)(object)collider) && !((Component)collider).transform.IsChildOf(((Component)localPlayer).transform))
				{
					Rigidbody attachedRigidbody = collider.attachedRigidbody;
					if ((!Object.op_Implicit((Object)(object)attachedRigidbody) || !((Object)(object)((Component)attachedRigidbody).gameObject == (Object)(object)((Component)localPlayer).gameObject)) && !(((RaycastHit)(ref val4)).distance >= num))
					{
						num = ((RaycastHit)(ref val4)).distance;
						val2 = ((RaycastHit)(ref val4)).point;
						val3 = collider;
					}
				}
			}
			if (Object.op_Implicit((Object)(object)val3))
			{
				Vector3 position = ((Component)localPlayer).transform.position;
				float num2 = val2.x - position.x;
				float num3 = val2.z - position.z;
				HorizontalMeters = Mathf.Sqrt(num2 * num2 + num3 * num3);
				ThreeDimensionalMeters = Vector3.Distance(position, val2);
				SurfacePoint = val2;
				TargetName = ResolveTargetName(val3);
				HasTarget = true;
			}
		}

		private static string ResolveTargetName(Collider collider)
		{
			Piece componentInParent = ((Component)collider).GetComponentInParent<Piece>();
			if (Object.op_Implicit((Object)(object)componentInParent) && !string.IsNullOrEmpty(componentInParent.m_name))
			{
				return LocalizeName(componentInParent.m_name);
			}
			Hoverable componentInParent2 = ((Component)collider).GetComponentInParent<Hoverable>();
			if (componentInParent2 != null)
			{
				string hoverName = componentInParent2.GetHoverName();
				if (!string.IsNullOrWhiteSpace(hoverName))
				{
					return LocalizeName(hoverName);
				}
			}
			if (Object.op_Implicit((Object)(object)((Component)collider).GetComponentInParent<Heightmap>()))
			{
				return "Terrain";
			}
			ZNetView componentInParent3 = ((Component)collider).GetComponentInParent<ZNetView>();
			return CleanPrefabName(((Object)(Object.op_Implicit((Object)(object)componentInParent3) ? ((Component)componentInParent3).gameObject : ((Component)collider).gameObject)).name);
		}

		private static string LocalizeName(string name)
		{
			if (Localization.instance == null)
			{
				return name;
			}
			return Localization.instance.Localize(name);
		}

		private static string CleanPrefabName(string name)
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				return "Unknown surface";
			}
			if (name.EndsWith("(Clone)", StringComparison.Ordinal))
			{
				name = name.Substring(0, name.Length - "(Clone)".Length);
			}
			name = name.Trim().Replace('_', ' ');
			if (name.Length != 0)
			{
				return name;
			}
			return "Unknown surface";
		}

		internal void Clear()
		{
			ClearResult();
			lastPlayer = null;
			lastCamera = null;
			nextSample = 0f;
		}

		private void ClearResult()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			HasTarget = false;
			HorizontalMeters = 0f;
			ThreeDimensionalMeters = 0f;
			SurfacePoint = Vector3.zero;
			TargetName = "---";
		}
	}
}