Decompiled source of SpeedMonitor v0.1.1

plugins/MycopunkSpeedMonitor/MycopunkSpeedMonitor.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using MycopunkShared;
using Pigeon.Movement;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MycopunkSpeedMonitor")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1+fe8cef1291afd0a7f7ea8a9ddfd28d3607c4cdd5")]
[assembly: AssemblyProduct("MycopunkSpeedMonitor")]
[assembly: AssemblyTitle("MycopunkSpeedMonitor")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MycopunkSpeedMonitor
{
	internal static class MenuText
	{
		private static readonly Dictionary<string, Dictionary<string, string>> Names = new Dictionary<string, Dictionary<string, string>>
		{
			["en"] = new Dictionary<string, string>
			{
				["speed.position"] = "Calculate speed from position",
				["speed.vertical"] = "Include vertical speed"
			},
			["ja"] = new Dictionary<string, string>
			{
				["speed.position"] = "実際の移動距離から速度を計算",
				["speed.vertical"] = "上下方向の速度を含める"
			}
		};

		private static readonly Dictionary<string, Dictionary<string, string>> Descriptions = new Dictionary<string, Dictionary<string, string>> { ["ja"] = new Dictionary<string, string>
		{
			["speed.position"] = "位置の変化から移動速度を計算します。オフの場合はゲームの速度値を表示します。",
			["speed.vertical"] = "オンの場合は上下方向を含む速度、オフの場合は水平方向のみの速度を表示します。"
		} };

		public static object[] Label(string key)
		{
			return ModsMenuTag.Label(key, (IDictionary<string, Dictionary<string, string>>)Names, (IDictionary<string, Dictionary<string, string>>)Descriptions);
		}
	}
	[BepInPlugin("f1w3.mycopunk.speedmonitor", "Mycopunk Speed Monitor", "0.1.1")]
	[BepInDependency("doku_macaron.mycopunk.macaroncore", "1.1.0")]
	[MycoMod(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		private Player _player;

		private Graphic _bar;

		private TextMeshProUGUI _label;

		private double _nextUpdate;

		private ConfigEntry<bool> _positionSpeed;

		private ConfigEntry<bool> _includeVertical;

		private Vector3 _lastPosition;

		private double _lastSampleTime;

		private bool _hasSample;

		private bool _hasSmoothedSpeed;

		private bool _lastPositionSpeed;

		private bool _lastIncludeVertical;

		private float _smoothedSpeed;

		private readonly List<(double time, Vector3 position)> _positions = new List<(double, Vector3)>();

		private void Awake()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			_positionSpeed = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "UsePositionSpeed", true, new ConfigDescription("Calculate speed from world-position changes. Turn off to use the player's velocity instead.", (AcceptableValueBase)null, MenuText.Label("speed.position")));
			_includeVertical = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "IncludeVerticalSpeed", true, new ConfigDescription("Include vertical movement in speed. Turn off to show horizontal speed only.", (AcceptableValueBase)null, MenuText.Label("speed.vertical")));
			_harmony = new Harmony("f1w3.mycopunk.speedmonitor");
			ModsMenu.Install(_harmony, (BaseUnityPlugin)(object)this);
		}

		private void LateUpdate()
		{
			//IL_0259: Unknown result type (might be due to invalid IL or missing references)
			//IL_025e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0262: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_0273: Unknown result type (might be due to invalid IL or missing references)
			//IL_027a: 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_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0298: 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_02a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02da: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_0326: Unknown result type (might be due to invalid IL or missing references)
			//IL_0331: Expected O, but got Unknown
			//IL_033e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0345: Unknown result type (might be due to invalid IL or missing references)
			//IL_0351: 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_01c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0378: Unknown result type (might be due to invalid IL or missing references)
			//IL_037d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0402: Unknown result type (might be due to invalid IL or missing references)
			//IL_0405: Unknown result type (might be due to invalid IL or missing references)
			//IL_043b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0440: Unknown result type (might be due to invalid IL or missing references)
			//IL_041b: Unknown result type (might be due to invalid IL or missing references)
			//IL_04de: Unknown result type (might be due to invalid IL or missing references)
			//IL_04e0: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.LocalPlayer;
			if ((Object)(object)localPlayer != (Object)(object)_player)
			{
				if ((Object)(object)_label != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)_label).gameObject);
				}
				_label = null;
				_bar = null;
				_player = localPlayer;
				_hasSample = false;
				_nextUpdate = 0.0;
			}
			if ((Object)(object)localPlayer == (Object)null)
			{
				_hasSample = false;
				return;
			}
			if ((Object)(object)_bar == (Object)null)
			{
				_bar = localPlayer.healthbar;
			}
			if ((Object)(object)_bar == (Object)null || (Object)(object)((Transform)_bar.rectTransform).parent == (Object)null)
			{
				_hasSample = false;
				return;
			}
			Canvas canvas = _bar.canvas;
			if ((Object)(object)canvas == (Object)null)
			{
				_hasSample = false;
				return;
			}
			Canvas rootCanvas = canvas.rootCanvas;
			if ((Object)(object)_label != (Object)null && (Object)(object)((TMP_Text)_label).transform.parent != (Object)(object)((Component)rootCanvas).transform)
			{
				Object.Destroy((Object)(object)((Component)_label).gameObject);
				_label = null;
			}
			if ((Object)(object)_label == (Object)null)
			{
				PlayerLook instance = PlayerLook.Instance;
				TextMeshProUGUI val = (((Object)(object)instance != (Object)null) ? instance.PlayerTitleText : null);
				if ((Object)(object)val == (Object)null)
				{
					_hasSample = false;
					return;
				}
				_label = GameHudText.Create(val, ((Component)rootCanvas).transform, "Speed Monitor");
				((TMP_Text)_label).alignment = (TextAlignmentOptions)258;
				((TMP_Text)_label).fontSize = 16f;
				((Graphic)_label).color = Color.white;
				RectTransform rectTransform = ((TMP_Text)_label).rectTransform;
				Vector2 val2 = default(Vector2);
				((Vector2)(ref val2))..ctor(0.5f, 0.5f);
				rectTransform.anchorMax = val2;
				rectTransform.anchorMin = val2;
				rectTransform.pivot = new Vector2(0.5f, 1f);
				((Transform)rectTransform).localRotation = Quaternion.identity;
				((Transform)rectTransform).localScale = Vector3.one;
				((TMP_Text)_label).transform.SetAsLastSibling();
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Speed label created on " + ((Object)rootCanvas).name));
			}
			bool flag = HudVisibility.HealthBarVisible(localPlayer, _bar, canvas);
			GameHudText.SetActive(_label, flag);
			if (!flag)
			{
				_hasSample = false;
				return;
			}
			RectTransform rectTransform2 = _bar.rectTransform;
			Vector3 localScale = ((Transform)rectTransform2).localScale;
			Matrix4x4 val3 = Matrix4x4.TRS(((Transform)rectTransform2).localPosition, ((Transform)rectTransform2).localRotation, new Vector3(1f, localScale.y, localScale.z));
			Transform parent = ((Transform)rectTransform2).parent;
			Rect rect = rectTransform2.rect;
			float x = ((Rect)(ref rect)).center.x;
			rect = rectTransform2.rect;
			Vector3 val4 = parent.TransformPoint(((Matrix4x4)(ref val3)).MultiplyPoint3x4(new Vector3(x, ((Rect)(ref rect)).yMin, 0f)));
			Camera val5 = HudCoordinates.CameraFor(rootCanvas);
			if ((int)rootCanvas.renderMode != 0 && (Object)(object)val5 == (Object)null)
			{
				_hasSample = false;
				return;
			}
			Vector2 val6 = RectTransformUtility.WorldToScreenPoint(val5, val4);
			val6.y -= 5f * (float)Screen.height / 1080f;
			Vector2 val7 = default(Vector2);
			if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)((Component)rootCanvas).transform, val6, val5, ref val7))
			{
				((Transform)((TMP_Text)_label).rectTransform).localPosition = new Vector3(val7.x, val7.y, 0f);
			}
			if (!(Time.unscaledTimeAsDouble >= _nextUpdate))
			{
				return;
			}
			double unscaledTimeAsDouble = Time.unscaledTimeAsDouble;
			Vector3 position = ((Component)localPlayer).transform.position;
			float num = (float)(unscaledTimeAsDouble - _lastSampleTime);
			bool value = _positionSpeed.Value;
			bool value2 = _includeVertical.Value;
			if (!value2)
			{
				position.y = 0f;
			}
			if (!_hasSample || value != _lastPositionSpeed || value2 != _lastIncludeVertical)
			{
				_hasSample = false;
				_hasSmoothedSpeed = false;
				_positions.Clear();
			}
			float num2 = ((_hasSample && num > 0f) ? (Vector3.Distance(position, _lastPosition) / num) : 0f);
			float num3;
			if (value)
			{
				num3 = PositionSpeed(unscaledTimeAsDouble, position, _hasSample && num2 < 0.01f);
			}
			else
			{
				Vector3 velocity = localPlayer.Velocity;
				if (!value2)
				{
					velocity.y = 0f;
				}
				num3 = ((Vector3)(ref velocity)).magnitude;
			}
			if (!value || (_hasSample && num > 0f))
			{
				if (!_hasSmoothedSpeed || num3 < 0.01f)
				{
					_smoothedSpeed = num3;
				}
				else
				{
					_smoothedSpeed = Mathf.Lerp(_smoothedSpeed, num3, 1f - Mathf.Exp((0f - num) / 0.15f));
				}
				_hasSmoothedSpeed = true;
				num3 = _smoothedSpeed;
			}
			GameHudText.SetText(_label, $"{num3:F1} m/s");
			_lastPosition = position;
			_lastSampleTime = unscaledTimeAsDouble;
			_hasSample = true;
			_lastPositionSpeed = value;
			_lastIncludeVertical = value2;
			_nextUpdate = unscaledTimeAsDouble + 0.05000000074505806;
		}

		private float PositionSpeed(double now, Vector3 position, bool stopped)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: 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_0101: Unknown result type (might be due to invalid IL or missing references)
			if (stopped)
			{
				_positions.Clear();
			}
			_positions.Add((now, position));
			double num = now - 0.5;
			while (_positions.Count > 1 && _positions[1].time <= num)
			{
				_positions.RemoveAt(0);
			}
			if (_positions.Count < 2)
			{
				return 0f;
			}
			(double, Vector3) tuple = _positions[0];
			(double, Vector3) tuple2 = _positions[1];
			double num2 = Math.Max(num, tuple.Item1);
			float num3 = (float)((num2 - tuple.Item1) / (tuple2.Item1 - tuple.Item1));
			Vector3 val = Vector3.Lerp(tuple.Item2, tuple2.Item2, num3);
			float num4 = 0f;
			for (int i = 1; i < _positions.Count; i++)
			{
				num4 += Vector3.Distance(val, _positions[i].position);
				val = _positions[i].position;
			}
			double num5 = now - num2;
			if (!(num5 > 0.0))
			{
				return 0f;
			}
			return num4 / (float)num5;
		}

		private void OnDestroy()
		{
			if ((Object)(object)_label != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)_label).gameObject);
			}
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}