Decompiled source of BigWalkVelocity v1.1.0

BepInEx/plugins/BigWalkVelocity/BigWalkVelocity.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using BepInEx.Unity.IL2CPP.Configuration;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("BigWalkVelocity")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+49824035963d2f0d122d6d657da6b5a3ceb7e038")]
[assembly: AssemblyProduct("BigWalkVelocity")]
[assembly: AssemblyTitle("BigWalkVelocity")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.Sch1nni.bigwalkvelocity", "Big Walk Velocity", "1.0.0")]
public class Plugin : BasePlugin
{
	internal static ManualLogSource Logger;

	internal static ConfigEntry<KeyboardShortcut> ToggleKey;

	internal static ConfigEntry<KeyboardShortcut> ResetTopKey;

	public override void Load()
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		Logger = ((BasePlugin)this).Log;
		ToggleKey = ((BasePlugin)this).Config.Bind<KeyboardShortcut>("Display", "Toggle Key", new KeyboardShortcut((KeyCode)289, Array.Empty<KeyCode>()), "Show/hide velocity display.");
		ResetTopKey = ((BasePlugin)this).Config.Bind<KeyboardShortcut>("Display", "Reset Top Key", new KeyboardShortcut((KeyCode)288, Array.Empty<KeyCode>()), "Reset top speed values.");
		Logger.LogInfo((object)"=================================");
		Logger.LogInfo((object)"Big Walk Velocity loaded");
		Logger.LogInfo((object)"F8 = Toggle velocity display");
		Logger.LogInfo((object)"F7 = Reset top speed values");
		Logger.LogInfo((object)"=================================");
		((BasePlugin)this).AddComponent<VelocityDisplay>();
	}
}
public class VelocityDisplay : MonoBehaviour
{
	private bool visible = true;

	private Text text;

	private Rigidbody playerRb;

	private Vector3 lastPosition;

	private bool hasLastPosition;

	private float searchTimer = 0f;

	private float topTotal;

	private float topHorizontal;

	private float topVertical;

	private const int SmoothingSamples = 5;

	private readonly Queue<Vector3> velocitySamples = new Queue<Vector3>();

	private Vector3 smoothedVelocity = Vector3.zero;

	public void Start()
	{
		BuildUI();
		TryFindPlayerRigidbody();
		Plugin.Logger.LogInfo((object)"Velocity display initialized.");
	}

	private void BuildUI()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0048: 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_0065: Expected O, but got Unknown
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: 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_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: 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)
		GameObject val = new GameObject("BigWalkVelocityCanvas");
		Object.DontDestroyOnLoad((Object)(object)val);
		Canvas val2 = val.AddComponent<Canvas>();
		val2.renderMode = (RenderMode)0;
		val2.sortingOrder = 1000;
		CanvasScaler val3 = val.AddComponent<CanvasScaler>();
		val3.uiScaleMode = (ScaleMode)1;
		val3.referenceResolution = new Vector2(1920f, 1080f);
		val.AddComponent<GraphicRaycaster>();
		GameObject val4 = new GameObject("VelocityText");
		val4.transform.SetParent(val.transform, false);
		RectTransform val5 = val4.AddComponent<RectTransform>();
		val5.anchorMin = new Vector2(0f, 1f);
		val5.anchorMax = new Vector2(0f, 1f);
		val5.pivot = new Vector2(0f, 1f);
		val5.anchoredPosition = new Vector2(20f, -20f);
		val5.sizeDelta = new Vector2(420f, 220f);
		text = val4.AddComponent<Text>();
		text.font = FindFallbackFont();
		text.fontSize = 24;
		((Graphic)text).color = Color.white;
		text.text = "Initializing...";
		text.horizontalOverflow = (HorizontalWrapMode)1;
		text.verticalOverflow = (VerticalWrapMode)1;
		Shadow val6 = val4.AddComponent<Shadow>();
		val6.effectColor = new Color(0f, 0f, 0f, 0.8f);
		val6.effectDistance = new Vector2(1.5f, -1.5f);
	}

	private Font FindFallbackFont()
	{
		Font builtinResource = Resources.GetBuiltinResource<Font>("Arial.ttf");
		if ((Object)(object)builtinResource != (Object)null)
		{
			return builtinResource;
		}
		Plugin.Logger.LogWarning((object)"Built-in Arial.ttf not found, falling back to OS font.");
		return Font.CreateDynamicFontFromOSFont("Arial", 24);
	}

	private void TryFindPlayerRigidbody()
	{
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Expected O, but got Unknown
		Rigidbody[] array = Il2CppArrayBase<Rigidbody>.op_Implicit(Object.FindObjectsOfType<Rigidbody>());
		Rigidbody[] array2 = array;
		bool flag = default(bool);
		foreach (Rigidbody val in array2)
		{
			if (((Object)((Component)val).gameObject).name.StartsWith("PlayerCharacter "))
			{
				playerRb = val;
				ResetVelocityTracking();
				ManualLogSource logger = Plugin.Logger;
				BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(26, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Player Rigidbody found: '");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(((Object)((Component)val).gameObject).name);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("'");
				}
				logger.LogInfo(val2);
				break;
			}
		}
	}

	private void ResetVelocityTracking()
	{
		//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_0018: 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)
		if ((Object)(object)playerRb != (Object)null)
		{
			lastPosition = playerRb.position;
		}
		hasLastPosition = true;
		velocitySamples.Clear();
		smoothedVelocity = Vector3.zero;
	}

	private void FixedUpdate()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: 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)
		//IL_0036: 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_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: 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_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: 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)
		//IL_0079: 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_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: 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_00cc: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: 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_011d: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: 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_011a: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)playerRb == (Object)null)
		{
			return;
		}
		Vector3 position = playerRb.position;
		if (!hasLastPosition)
		{
			lastPosition = position;
			hasLastPosition = true;
			return;
		}
		float fixedDeltaTime = Time.fixedDeltaTime;
		if (fixedDeltaTime <= 0f)
		{
			return;
		}
		Vector3 val = position - lastPosition;
		Vector3 item = val / fixedDeltaTime;
		lastPosition = position;
		velocitySamples.Enqueue(item);
		while (velocitySamples.Count > 5)
		{
			velocitySamples.Dequeue();
		}
		Vector3 val2 = Vector3.zero;
		foreach (Vector3 velocitySample in velocitySamples)
		{
			val2 += velocitySample;
		}
		if (velocitySamples.Count > 0)
		{
			val2 /= (float)velocitySamples.Count;
		}
		smoothedVelocity = val2;
	}

	public void Update()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: 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_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		KeyboardShortcut value = Plugin.ToggleKey.Value;
		if (((KeyboardShortcut)(ref value)).IsDown())
		{
			visible = !visible;
		}
		((Component)text).gameObject.SetActive(visible);
		if (!visible)
		{
			return;
		}
		if ((Object)(object)playerRb == (Object)null)
		{
			searchTimer -= Time.deltaTime;
			if (searchTimer <= 0f)
			{
				TryFindPlayerRigidbody();
				searchTimer = 1f;
			}
			text.text = "Waiting for player...";
			return;
		}
		Vector3 val = smoothedVelocity;
		Vector2 val2 = new Vector2(val.x, val.z);
		float magnitude = ((Vector2)(ref val2)).magnitude;
		float num = Mathf.Abs(val.y);
		float magnitude2 = ((Vector3)(ref val)).magnitude;
		value = Plugin.ResetTopKey.Value;
		if (((KeyboardShortcut)(ref value)).IsDown())
		{
			topTotal = 0f;
			topHorizontal = 0f;
			topVertical = 0f;
		}
		topTotal = Mathf.Max(topTotal, magnitude2);
		topHorizontal = Mathf.Max(topHorizontal, magnitude);
		topVertical = Mathf.Max(topVertical, num);
		text.text = $"Total:  {magnitude2:F2}\nHoriz:  {magnitude:F2}\nVert:   {num:F2}\n\nTop Total:  {topTotal:F2}\nTop Horiz:  {topHorizontal:F2}\nTop Vert:   {topVertical:F2}";
	}
}