Decompiled source of CapsulePath v0.1.0

BepInEx\plugins\CapsulePath.dll

Decompiled a day ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("CapsulePath")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CapsulePath")]
[assembly: AssemblyTitle("CapsulePath")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CapsulePath;

[BepInPlugin("com.local.capsulepath", "Capsule Path", "0.1.0")]
public class CapsulePathPlugin : BaseUnityPlugin
{
	private const KeyCode RecalculateKey = 107;

	private const KeyCode ToggleViewKey = 111;

	private readonly NavMeshPath _navPath = new NavMeshPath();

	private LineRenderer _line;

	private readonly List<GameObject> _cornerMarkers = new List<GameObject>();

	private bool _pathVisible = true;

	private Vector3? _capsulePos;

	private int _prevBotCount;

	private string _sceneName = string.Empty;

	private void Awake()
	{
		//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)
		Scene activeScene = SceneManager.GetActiveScene();
		_sceneName = ((Scene)(ref activeScene)).name;
		EnsureLineRenderer();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Capsule Path loaded. K=calculate path, O=toggle path.");
	}

	private void Update()
	{
		HandleSceneChange();
		TrackCapsulePosition();
		if (Input.GetKeyDown((KeyCode)111))
		{
			_pathVisible = !_pathVisible;
			if ((Object)(object)_line != (Object)null)
			{
				((Renderer)_line).enabled = _pathVisible;
			}
			SetMarkersVisible(_pathVisible);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[CapsulePath] Visibility: " + (_pathVisible ? "ON" : "OFF")));
		}
		if (Input.GetKeyDown((KeyCode)107))
		{
			RecalculatePath();
		}
	}

	private void HandleSceneChange()
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		Scene activeScene = SceneManager.GetActiveScene();
		string name = ((Scene)(ref activeScene)).name;
		if (!(name == _sceneName))
		{
			_sceneName = name;
			_capsulePos = null;
			_prevBotCount = 0;
			ClearPath();
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[CapsulePath] Scene changed -> state reset (" + _sceneName + ")"));
		}
	}

	private void TrackCapsulePosition()
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		int valueOrDefault = (BotHandler.instance?.bots?.Count).GetValueOrDefault();
		if (_prevBotCount == 0 && valueOrDefault > 0)
		{
			Camera main = Camera.main;
			if ((Object)(object)main != (Object)null)
			{
				_capsulePos = ((Component)main).transform.position;
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"[CapsulePath] Capsule anchor recorded: {_capsulePos.Value}");
			}
		}
		else if (valueOrDefault == 0 && _prevBotCount > 0)
		{
			_capsulePos = null;
			ClearPath();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[CapsulePath] Capsule anchor cleared.");
		}
		_prevBotCount = valueOrDefault;
	}

	private void RecalculatePath()
	{
		//IL_005b: 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_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: 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_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: 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_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Invalid comparison between Unknown and I4
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_0173: 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_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
		if (!_capsulePos.HasValue)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"[CapsulePath] Capsule position is unknown yet.");
			return;
		}
		Player localPlayer = Player.localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"[CapsulePath] Local player not found.");
			return;
		}
		if (!TryGetLocalAvatarPosition(localPlayer, out var position, out var source))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"[CapsulePath] Could not resolve dynamic local avatar position.");
			return;
		}
		Vector3 position2 = position;
		Vector3 position3 = _capsulePos.Value;
		if (!TrySampleToNavMesh(ref position2) || !TrySampleToNavMesh(ref position3))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"[CapsulePath] Could not sample start/end to NavMesh.");
			ClearPath();
			RenderDirectFallback(position, _capsulePos.Value);
			return;
		}
		if (!NavMesh.CalculatePath(position2, position3, -1, _navPath) || _navPath.corners == null || _navPath.corners.Length < 2)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"[CapsulePath] Path not found.");
			ClearPath();
			RenderDirectFallback(position, _capsulePos.Value);
			return;
		}
		if ((int)_navPath.status == 2)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"[CapsulePath] Path invalid.");
			ClearPath();
			RenderDirectFallback(position, _capsulePos.Value);
			return;
		}
		RenderPath(_navPath.corners);
		float num = 0f;
		for (int i = 1; i < _navPath.corners.Length; i++)
		{
			num += Vector3.Distance(_navPath.corners[i - 1], _navPath.corners[i]);
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"[CapsulePath] Path calculated. Corners={_navPath.corners.Length}, status={_navPath.status}, visible={_pathVisible}, len={num:F1}m, src={source}, player={position}, capsule={_capsulePos.Value}");
	}

	private static bool TryGetLocalAvatarPosition(Player local, out Vector3 position, out string source)
	{
		//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_0011: 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)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: 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_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: 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_0046: 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_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Vector3 val = local.HeadPosition();
			Vector3 val2 = val + Vector3.down * 1.5f;
			RaycastHit val3 = default(RaycastHit);
			if (Physics.Raycast(val, Vector3.down, ref val3, 3f))
			{
				position = ((RaycastHit)(ref val3)).point + Vector3.up * 0.1f;
				source = "player.HeadPosition()+groundRay";
				return true;
			}
			position = val2;
			source = "player.HeadPosition()-1.5m";
			return true;
		}
		catch
		{
		}
		if ((Object)(object)((Component)local).transform != (Object)null)
		{
			position = ((Component)local).transform.position;
			source = "player.transform.position";
			return true;
		}
		position = default(Vector3);
		source = "none";
		return false;
	}

	private static bool TrySampleToNavMesh(ref Vector3 position)
	{
		//IL_0001: 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_001f: Unknown result type (might be due to invalid IL or missing references)
		NavMeshHit val = default(NavMeshHit);
		if (!NavMesh.SamplePosition(position, ref val, 4f, -1))
		{
			return false;
		}
		position = ((NavMeshHit)(ref val)).position;
		return true;
	}

	private void RenderPath(Vector3[] corners)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		EnsureLineRenderer();
		_line.positionCount = corners.Length;
		for (int i = 0; i < corners.Length; i++)
		{
			_line.SetPosition(i, corners[i] + Vector3.up * 0.12f);
		}
		((Renderer)_line).enabled = _pathVisible;
		RebuildCornerMarkers(corners);
	}

	private void RenderDirectFallback(Vector3 start, Vector3 end)
	{
		//IL_0019: 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_0024: 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)
		//IL_003a: 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_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: 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_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		EnsureLineRenderer();
		_line.positionCount = 2;
		_line.SetPosition(0, start + Vector3.up * 0.2f);
		_line.SetPosition(1, end + Vector3.up * 0.2f);
		((Renderer)_line).enabled = _pathVisible;
		RebuildCornerMarkers((Vector3[])(object)new Vector3[2] { start, end });
	}

	private void ClearPath()
	{
		if (!((Object)(object)_line == (Object)null))
		{
			_line.positionCount = 0;
			ClearCornerMarkers();
		}
	}

	private void EnsureLineRenderer()
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Expected O, but got Unknown
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_line != (Object)null))
		{
			GameObject val = new GameObject("CapsulePathRenderer");
			Object.DontDestroyOnLoad((Object)(object)val);
			_line = val.AddComponent<LineRenderer>();
			_line.useWorldSpace = true;
			_line.loop = false;
			_line.widthMultiplier = 0.175f;
			_line.positionCount = 0;
			_line.numCapVertices = 4;
			_line.numCornerVertices = 2;
			((Renderer)_line).shadowCastingMode = (ShadowCastingMode)0;
			((Renderer)_line).receiveShadows = false;
			((Renderer)_line).material = BuildLineMaterial();
			_line.startColor = new Color(0.15f, 1f, 0.25f, 0.95f);
			_line.endColor = new Color(0.15f, 1f, 0.25f, 0.95f);
			_line.textureMode = (LineTextureMode)0;
			_line.alignment = (LineAlignment)0;
			((Renderer)_line).enabled = _pathVisible;
		}
	}

	private Material BuildLineMaterial()
	{
		//IL_0026: 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_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Expected O, but got Unknown
		return new Material(Shader.Find("Universal Render Pipeline/Unlit") ?? Shader.Find("Unlit/Color") ?? Shader.Find("Sprites/Default"))
		{
			color = new Color(0.15f, 1f, 0.25f, 0.95f)
		};
	}

	private void RebuildCornerMarkers(Vector3[] corners)
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: 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_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: 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_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_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: 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)
		ClearCornerMarkers();
		for (int i = 0; i < corners.Length; i++)
		{
			GameObject val = GameObject.CreatePrimitive((PrimitiveType)0);
			((Object)val).name = $"CapsulePathCorner_{i}";
			val.transform.position = corners[i] + Vector3.up * 0.2f;
			val.transform.localScale = Vector3.one * 0.3f;
			Collider component = val.GetComponent<Collider>();
			if ((Object)(object)component != (Object)null)
			{
				Object.Destroy((Object)(object)component);
			}
			Renderer component2 = val.GetComponent<Renderer>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.sharedMaterial = BuildLineMaterial();
			}
			GameObject val2 = new GameObject("Beacon");
			val2.transform.SetParent(val.transform, false);
			LineRenderer obj = val2.AddComponent<LineRenderer>();
			obj.useWorldSpace = false;
			obj.positionCount = 2;
			obj.SetPosition(0, Vector3.zero);
			obj.SetPosition(1, Vector3.up * 4f);
			obj.widthMultiplier = 0.175f;
			((Renderer)obj).material = BuildLineMaterial();
			obj.startColor = Color.cyan;
			obj.endColor = Color.cyan;
			val.SetActive(_pathVisible);
			_cornerMarkers.Add(val);
		}
	}

	private void SetMarkersVisible(bool visible)
	{
		for (int i = 0; i < _cornerMarkers.Count; i++)
		{
			if ((Object)(object)_cornerMarkers[i] != (Object)null)
			{
				_cornerMarkers[i].SetActive(visible);
			}
		}
	}

	private void ClearCornerMarkers()
	{
		for (int i = 0; i < _cornerMarkers.Count; i++)
		{
			if ((Object)(object)_cornerMarkers[i] != (Object)null)
			{
				Object.Destroy((Object)(object)_cornerMarkers[i]);
			}
		}
		_cornerMarkers.Clear();
	}

	private void OnDestroy()
	{
		ClearCornerMarkers();
		if ((Object)(object)_line != (Object)null)
		{
			Object.Destroy((Object)(object)((Component)_line).gameObject);
		}
	}
}
internal static class PluginInfo
{
	public const string GUID = "com.local.capsulepath";

	public const string Name = "Capsule Path";

	public const string Version = "0.1.0";
}