Decompiled source of CompassHUD v1.0.4

plugin/Compass.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Compass")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Compass")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("09289634-0026-448c-80f5-8dfb85aeee99")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
public class CompassHUD : MonoBehaviour
{
	private class FixedMarker
	{
		public string id;

		public Vector3 worldPos;

		public Color color;

		public bool alwaysShow;
	}

	[Header("HUD")]
	public bool enabledHud = true;

	[Header("Coordinates (X/Z)")]
	public bool showCoordinates = false;

	public float coordsYOffset = 44f;

	public int coordsDecimals = 1;

	[Header("Compass Layout")]
	public float topOffset = 18f;

	public float width = 520f;

	public float height = 28f;

	[Header("Look / Scale")]
	public float degreesVisible = 120f;

	public float tickEveryDegrees = 15f;

	public float majorTickEvery = 45f;

	[Header("Center Line")]
	public bool centerLineRed = true;

	public float centerLineThickness = 3f;

	[Header("Other Players (blue)")]
	public bool showOtherPlayers = true;

	public float otherPlayersMaxDistance = 250f;

	[Header("NPCs (optional later)")]
	public bool showNpcMarkers = false;

	public float npcMaxDistance = 250f;

	[Header("Fixed Markers")]
	public bool showFixedMarkers = true;

	public float fixedMarkerMaxDistance = 999999f;

	public string tavernMarkerId = "Tavern";

	private GUIStyle _cardinalStyle;

	private GUIStyle _coordStyle;

	private GUIStyle _markerLabelStyle;

	private readonly Dictionary<string, FixedMarker> _fixedMarkers = new Dictionary<string, FixedMarker>(StringComparer.OrdinalIgnoreCase);

	private static Material _guiMat;

	private void Update()
	{
		if (Input.GetKeyDown((KeyCode)257))
		{
			showCoordinates = !showCoordinates;
		}
	}

	private void OnGUI()
	{
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_010f: Unknown result type (might be due to invalid IL or missing references)
		if (!enabledHud)
		{
			return;
		}
		EnsureStyles();
		Transform localPlayerTransform = GetLocalPlayerTransform();
		if ((Object)(object)localPlayerTransform == (Object)null)
		{
			return;
		}
		Transform headingReference = GetHeadingReference(localPlayerTransform);
		if (!((Object)(object)headingReference == (Object)null))
		{
			float headingDegrees = GetHeadingDegrees(headingReference);
			Rect val = default(Rect);
			((Rect)(ref val))..ctor((float)Screen.width * 0.5f - width * 0.5f, topOffset, width, height);
			GUI.Box(val, GUIContent.none);
			DrawTicks(val, headingDegrees);
			DrawCenterLine(val);
			DrawCardinals(val, headingDegrees);
			if (showFixedMarkers)
			{
				DrawFixedMarkers(val, headingDegrees, localPlayerTransform.position);
			}
			if (showOtherPlayers)
			{
				DrawOtherPlayers(val, headingDegrees, localPlayerTransform.position);
			}
			if (showNpcMarkers)
			{
				DrawNpcMarkersStub(val, headingDegrees, localPlayerTransform.position);
			}
			if (showCoordinates)
			{
				DrawCoords(val, localPlayerTransform.position);
			}
		}
	}

	public void SetTavernXZ(float x, float z)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		Transform localPlayerTransform = GetLocalPlayerTransform();
		float num = (((Object)(object)localPlayerTransform != (Object)null) ? localPlayerTransform.position.y : 0f);
		AddOrUpdateFixedMarker(tavernMarkerId, new Vector3(x, num, z), Color.white, alwaysShow: true);
	}

	public void AddOrUpdateFixedMarker(string id, Vector3 worldPos, Color color, bool alwaysShow)
	{
		//IL_0040: 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)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		if (!string.IsNullOrEmpty(id))
		{
			if (!_fixedMarkers.TryGetValue(id, out var value))
			{
				value = new FixedMarker();
				_fixedMarkers[id] = value;
			}
			value.id = id;
			value.worldPos = worldPos;
			value.color = color;
			value.alwaysShow = alwaysShow;
		}
	}

	public void RemoveFixedMarker(string id)
	{
		if (!string.IsNullOrEmpty(id))
		{
			_fixedMarkers.Remove(id);
		}
	}

	private void DrawTicks(Rect box, float heading)
	{
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
		float num = degreesVisible * 0.5f;
		float num2 = Mathf.Max(1f, tickEveryDegrees);
		float num3 = heading - num;
		float num4 = heading + num;
		float num5 = Mathf.Floor(num3 / num2) * num2;
		for (float num6 = num5; num6 <= num4 + 0.0001f; num6 += num2)
		{
			float num7 = Mathf.DeltaAngle(heading, num6);
			if (!(Mathf.Abs(num7) > num))
			{
				float num8 = Mathf.InverseLerp(0f - num, num, num7);
				float num9 = Mathf.Lerp(((Rect)(ref box)).xMin, ((Rect)(ref box)).xMax, num8);
				float v = Normalize360(num6);
				bool flag = ApproximatelyModulo(v, majorTickEvery);
				float t = (flag ? 2f : 1f);
				float num10 = (flag ? (((Rect)(ref box)).height * 0.8f) : (((Rect)(ref box)).height * 0.55f));
				DrawLine(new Vector2(num9, ((Rect)(ref box)).yMax), new Vector2(num9, ((Rect)(ref box)).yMax - num10), t, Color.white);
			}
		}
	}

	private void DrawCenterLine(Rect box)
	{
		//IL_0003: 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_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: 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)
		float x = ((Rect)(ref box)).center.x;
		DrawLine(new Vector2(x, ((Rect)(ref box)).yMin), new Vector2(x, ((Rect)(ref box)).yMax), Mathf.Max(1f, centerLineThickness), centerLineRed ? Color.red : Color.white);
	}

	private void DrawCardinals(Rect box, float heading)
	{
		//IL_0002: 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_0028: 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)
		DrawCardinal(box, heading, 0f, "N");
		DrawCardinal(box, heading, 90f, "E");
		DrawCardinal(box, heading, 180f, "S");
		DrawCardinal(box, heading, 270f, "W");
	}

	private void DrawCardinal(Rect box, float heading, float deg, string txt)
	{
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		float num = Mathf.DeltaAngle(heading, deg);
		float num2 = degreesVisible * 0.5f;
		if (!(Mathf.Abs(num) > num2))
		{
			float num3 = Mathf.InverseLerp(0f - num2, num2, num);
			float num4 = Mathf.Lerp(((Rect)(ref box)).xMin, ((Rect)(ref box)).xMax, num3);
			Rect val = default(Rect);
			((Rect)(ref val))..ctor(num4 - 20f, ((Rect)(ref box)).yMin - 22f, 40f, 22f);
			GUI.Label(val, txt, _cardinalStyle);
		}
	}

	private void DrawCoords(Rect box, Vector3 pos)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		string text = $"X:{Math.Round(pos.x, coordsDecimals)}  Z:{Math.Round(pos.z, coordsDecimals)}";
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(((Rect)(ref box)).center.x - 90f, ((Rect)(ref box)).yMax + coordsYOffset, 180f, 20f);
		GUI.Label(val, text, _coordStyle);
	}

	private void DrawFixedMarkers(Rect box, float heading, Vector3 myPos)
	{
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: 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_0095: 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_00a0: Unknown result type (might be due to invalid IL or missing references)
		if (_fixedMarkers.Count == 0)
		{
			return;
		}
		foreach (KeyValuePair<string, FixedMarker> fixedMarker in _fixedMarkers)
		{
			FixedMarker value = fixedMarker.Value;
			if (value != null && !string.IsNullOrEmpty(value.id))
			{
				Vector3 deltaXZ = value.worldPos - myPos;
				deltaXZ.y = 0f;
				float magnitude = ((Vector3)(ref deltaXZ)).magnitude;
				if (value.alwaysShow || !(magnitude > fixedMarkerMaxDistance))
				{
					DrawMarkerInViewOnly(box, heading, deltaXZ, value.id, value.color);
				}
			}
		}
	}

	private void DrawOtherPlayers(Rect box, float heading, Vector3 myPos)
	{
		//IL_0036: 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_003c: 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)
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		PlayerNet[] array = Object.FindObjectsOfType<PlayerNet>();
		foreach (PlayerNet val in array)
		{
			if (!((Object)(object)val == (Object)null) && !((NetworkBehaviour)val).IsOwner)
			{
				Vector3 deltaXZ = ((Component)val).transform.position - myPos;
				deltaXZ.y = 0f;
				if (!(((Vector3)(ref deltaXZ)).magnitude > otherPlayersMaxDistance))
				{
					string label = ((!string.IsNullOrEmpty(val.pname)) ? val.pname : "Player");
					DrawMarkerInViewOnly(box, heading, deltaXZ, label, Color.blue);
				}
			}
		}
	}

	private void DrawNpcMarkersStub(Rect box, float heading, Vector3 myPos)
	{
	}

	private void DrawMarkerInViewOnly(Rect box, float heading, Vector3 deltaXZ, string label, Color color)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: 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_009c: Unknown result type (might be due to invalid IL or missing references)
		float num = degreesVisible * 0.5f;
		float num2 = BearingDegrees(deltaXZ);
		float num3 = Mathf.DeltaAngle(heading, num2);
		if (!(Mathf.Abs(num3) > num))
		{
			float num4 = Mathf.InverseLerp(0f - num, num, num3);
			float num5 = Mathf.Lerp(((Rect)(ref box)).xMin, ((Rect)(ref box)).xMax, num4);
			DrawTriangleFilled(new Vector2(num5, ((Rect)(ref box)).yMax - 2f), 6f, 8f, color);
			Rect val = default(Rect);
			((Rect)(ref val))..ctor(num5 - 60f, ((Rect)(ref box)).yMax + 2f, 120f, 18f);
			GUI.Label(val, label, _markerLabelStyle);
		}
	}

	private void DrawLine(Vector2 a, Vector2 b, float t, Color c)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: 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_000c: 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_000e: 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_0014: 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_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: 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_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		Matrix4x4 matrix = GUI.matrix;
		Color color = GUI.color;
		Vector2 val = b - a;
		float num = Mathf.Atan2(val.y, val.x) * 57.29578f;
		GUI.color = c;
		GUIUtility.RotateAroundPivot(num, a);
		GUI.DrawTexture(new Rect(a.x, a.y - t * 0.5f, ((Vector2)(ref val)).magnitude, t), (Texture)(object)Texture2D.whiteTexture);
		GUI.matrix = matrix;
		GUI.color = color;
	}

	private void DrawTriangleFilled(Vector2 tip, float halfW, float h, Color c)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Invalid comparison between Unknown and I4
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: 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_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		if (Event.current != null && (int)Event.current.type == 7 && !float.IsNaN(tip.x) && !float.IsNaN(tip.y) && !float.IsInfinity(tip.x) && !float.IsInfinity(tip.y))
		{
			GL.PushMatrix();
			GL.LoadPixelMatrix(0f, (float)Screen.width, (float)Screen.height, 0f);
			Material guiMaterial = GetGuiMaterial();
			if ((Object)(object)guiMaterial == (Object)null)
			{
				GL.PopMatrix();
				return;
			}
			guiMaterial.SetPass(0);
			GL.Begin(4);
			GL.Color(c);
			GL.Vertex3(tip.x - halfW, tip.y, 0f);
			GL.Vertex3(tip.x + halfW, tip.y, 0f);
			GL.Vertex3(tip.x, tip.y - h, 0f);
			GL.End();
			GL.PopMatrix();
		}
	}

	private static Material GetGuiMaterial()
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Expected O, but got Unknown
		if ((Object)(object)_guiMat != (Object)null)
		{
			return _guiMat;
		}
		Shader val = Shader.Find("Hidden/Internal-Colored");
		_guiMat = new Material(val);
		((Object)_guiMat).hideFlags = (HideFlags)61;
		_guiMat.SetInt("_SrcBlend", 5);
		_guiMat.SetInt("_DstBlend", 10);
		_guiMat.SetInt("_Cull", 0);
		_guiMat.SetInt("_ZWrite", 0);
		return _guiMat;
	}

	private void EnsureStyles()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		//IL_005b: 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)
		//IL_007b: Expected O, but got Unknown
		//IL_00af: 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_00cf: Expected O, but got Unknown
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		if (_cardinalStyle == null)
		{
			_cardinalStyle = new GUIStyle(GUI.skin.label);
			_cardinalStyle.alignment = (TextAnchor)4;
			_cardinalStyle.fontStyle = (FontStyle)1;
			_cardinalStyle.fontSize = 22;
			_cardinalStyle.normal.textColor = Color.white;
			_coordStyle = new GUIStyle(GUI.skin.label);
			_coordStyle.alignment = (TextAnchor)4;
			_coordStyle.fontStyle = (FontStyle)1;
			_coordStyle.normal.textColor = new Color(1f, 0.6f, 0.1f);
			_markerLabelStyle = new GUIStyle(GUI.skin.label);
			_markerLabelStyle.alignment = (TextAnchor)4;
			_markerLabelStyle.fontSize = 11;
			_markerLabelStyle.normal.textColor = Color.white;
		}
	}

	private float GetHeadingDegrees(Transform t)
	{
		//IL_0002: 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_0036: 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)
		Vector3 forward = t.forward;
		forward.y = 0f;
		if (((Vector3)(ref forward)).sqrMagnitude < 0.0001f)
		{
			return 0f;
		}
		((Vector3)(ref forward)).Normalize();
		float d = Mathf.Atan2(forward.x, forward.z) * 57.29578f;
		return Normalize360(d);
	}

	private float BearingDegrees(Vector3 d)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		if (((Vector3)(ref d)).sqrMagnitude < 0.0001f)
		{
			return 0f;
		}
		((Vector3)(ref d)).Normalize();
		float d2 = Mathf.Atan2(d.x, d.z) * 57.29578f;
		return Normalize360(d2);
	}

	private float Normalize360(float d)
	{
		d %= 360f;
		if (d < 0f)
		{
			d += 360f;
		}
		return d;
	}

	private bool ApproximatelyModulo(float v, float m)
	{
		if (m <= 0.001f)
		{
			return false;
		}
		float num = v % m;
		return num < 0.01f || Mathf.Abs(num - m) < 0.01f;
	}

	private Transform GetLocalPlayerTransform()
	{
		return ((Object)(object)PlayerNet.Instance != (Object)null) ? ((Component)PlayerNet.Instance).transform : null;
	}

	private Transform GetHeadingReference(Transform fallbackPlayer)
	{
		try
		{
			if ((Object)(object)PlayerMovement.Instance != (Object)null && (Object)(object)PlayerMovement.Instance.mainCamera != (Object)null)
			{
				return ((Component)PlayerMovement.Instance.mainCamera).transform;
			}
		}
		catch
		{
		}
		Camera main = Camera.main;
		if ((Object)(object)main != (Object)null)
		{
			return ((Component)main).transform;
		}
		return fallbackPlayer;
	}
}
[BepInPlugin("travv.compasshud", "Compass HUD", "1.0.4")]
public class CompassPlugin : BaseUnityPlugin
{
	private GameObject _go;

	private CompassHUD _hud;

	private void Awake()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		_go = new GameObject("CompassHUD_Runtime");
		Object.DontDestroyOnLoad((Object)(object)_go);
		_hud = _go.AddComponent<CompassHUD>();
		_hud.SetTavernXZ(4f, 54.5f);
		_hud.showCoordinates = false;
		_hud.showOtherPlayers = true;
		_hud.otherPlayersMaxDistance = 250f;
		_hud.showNpcMarkers = false;
		_hud.npcMaxDistance = 250f;
	}

	private void OnDestroy()
	{
		if ((Object)(object)_go != (Object)null)
		{
			Object.Destroy((Object)(object)_go);
		}
	}
}