Decompiled source of Speedometer v1.2.0

BepInEx/plugins/Speedometer.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("Speedometer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Speedometer")]
[assembly: AssemblyTitle("Speedometer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.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;
		}
	}
}
public static class HudRepositionClient
{
	private const string ApiTypeName = "HudRepositionAPI";

	private static Type _apiType;

	private static MethodInfo _register;

	private static MethodInfo _unregister;

	private static bool _available;

	public static bool IsAvailable
	{
		get
		{
			EnsureResolved();
			return _available;
		}
	}

	public static void Register(string id, string displayName, RectTransform rect, ConfigEntry<float> anchorX, ConfigEntry<float> anchorY)
	{
		if (!_available)
		{
			EnsureResolved(force: true);
		}
		if (!_available || (Object)(object)rect == (Object)null || anchorX == null || anchorY == null)
		{
			return;
		}
		try
		{
			_register.Invoke(null, new object[5] { id, displayName, rect, anchorX, anchorY });
		}
		catch (Exception ex)
		{
			Debug.LogWarning((object)("[HudRepositionClient] Register failed: " + (ex.InnerException?.Message ?? ex.Message)));
		}
	}

	public static void Unregister(string id)
	{
		if (!_available)
		{
			EnsureResolved(force: true);
		}
		if (!_available || string.IsNullOrEmpty(id))
		{
			return;
		}
		try
		{
			_unregister.Invoke(null, new object[1] { id });
		}
		catch (Exception)
		{
		}
	}

	private static void EnsureResolved(bool force = false)
	{
		if (_available && !force)
		{
			return;
		}
		_apiType = null;
		_register = null;
		_unregister = null;
		_available = false;
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			try
			{
				Type type = assembly.GetType("HudRepositionAPI", throwOnError: false);
				if (type != null)
				{
					_apiType = type;
					break;
				}
			}
			catch
			{
			}
		}
		if (_apiType == null)
		{
			return;
		}
		MethodInfo[] methods = _apiType.GetMethods(BindingFlags.Static | BindingFlags.Public);
		foreach (MethodInfo methodInfo in methods)
		{
			ParameterInfo[] parameters = methodInfo.GetParameters();
			if (methodInfo.Name == "Register" && parameters.Length == 5 && parameters[0].ParameterType == typeof(string) && parameters[1].ParameterType == typeof(string) && typeof(RectTransform).IsAssignableFrom(parameters[2].ParameterType))
			{
				_register = methodInfo;
			}
			else if (methodInfo.Name == "Unregister" && parameters.Length == 1 && parameters[0].ParameterType == typeof(string))
			{
				_unregister = methodInfo;
			}
		}
		_available = _register != null && _unregister != null;
	}
}
[BepInPlugin("sparroh.speedometer", "Speedometer", "1.2.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[MycoMod(/*Could not decode attribute arguments.*/)]
public class SparrohPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "sparroh.speedometer";

	public const string PluginName = "Speedometer";

	public const string PluginVersion = "1.2.0";

	internal static ManualLogSource Logger;

	private Harmony harmony;

	private SpeedometerMod speedometer;

	private void Awake()
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Expected O, but got Unknown
		Logger = ((BaseUnityPlugin)this).Logger;
		try
		{
			harmony = new Harmony("sparroh.speedometer");
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Failed to create Harmony instance: " + ex.Message));
			return;
		}
		ConfigFile configFile = ((BaseUnityPlugin)this).Config;
		try
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, "sparroh.speedometer.cfg");
			fileSystemWatcher.Changed += delegate
			{
				configFile.Reload();
			};
			fileSystemWatcher.EnableRaisingEvents = true;
		}
		catch (Exception ex2)
		{
			Logger.LogWarning((object)("Failed to set up config watcher: " + ex2.Message));
		}
		try
		{
			speedometer = new SpeedometerMod(configFile, harmony);
		}
		catch (Exception ex3)
		{
			Logger.LogError((object)("Failed to initialize Speedometer: " + ex3.Message));
		}
		try
		{
			harmony.PatchAll();
		}
		catch (Exception ex4)
		{
			Logger.LogError((object)("Failed to apply Harmony patches: " + ex4.Message));
		}
		Logger.LogInfo((object)"Speedometer loaded successfully.");
	}

	private void Update()
	{
		try
		{
			if (speedometer != null)
			{
				speedometer.UpdateHudVisibility();
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error in Speedometer.UpdateHudVisibility(): " + ex.Message));
		}
		try
		{
			if (speedometer != null)
			{
				speedometer.Update();
			}
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Error in Speedometer.Update(): " + ex2.Message));
		}
	}

	private void OnDestroy()
	{
		try
		{
			if (speedometer != null)
			{
				speedometer.OnDestroy();
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error in Speedometer.OnDestroy(): " + ex.Message));
		}
		try
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Error unpatching Harmony: " + ex2.Message));
		}
	}
}
public class SpeedometerMod
{
	private ConfigEntry<bool> enableSpeedometerHUD;

	private ConfigEntry<float> speedometerAnchorX;

	private ConfigEntry<float> speedometerAnchorY;

	private ConfigColor valueColor;

	private HudHandle hud;

	private FieldInfo currentMoveSpeedField;

	private FieldInfo vkField;

	private FieldInfo rbField;

	private FieldInfo moveVelocityField;

	private PropertyInfo vkProp;

	private PropertyInfo rbProp;

	private readonly ConfigFile configFile;

	private readonly Harmony harmony;

	public static SpeedometerMod Instance { get; private set; }

	public bool IsActive
	{
		get
		{
			if (hud != null)
			{
				return hud.IsActive;
			}
			return false;
		}
	}

	public Vector2 GetSize
	{
		get
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			HudHandle obj = hud;
			if (obj == null)
			{
				return Vector2.zero;
			}
			return obj.Size;
		}
	}

	public SpeedometerMod(ConfigFile configFile, Harmony harmony)
	{
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		this.configFile = configFile;
		this.harmony = harmony;
		Instance = this;
		try
		{
			enableSpeedometerHUD = configFile.Bind<bool>("General", "EnableSpeedometerHUD", true, "Enables the speedometer HUD display.");
			enableSpeedometerHUD.SettingChanged += OnEnableSpeedometerHUDChanged;
			speedometerAnchorX = configFile.Bind<float>("HUD Positioning", "SpeedometerAnchorX", 0.06418981f, "X anchor position for Speedometer (0-1).");
			speedometerAnchorY = configFile.Bind<float>("HUD Positioning", "SpeedometerAnchorY", 0.2298982f, "Y anchor position for Speedometer (0-1).");
			speedometerAnchorX.SettingChanged += OnAnchorChanged;
			speedometerAnchorY.SettingChanged += OnAnchorChanged;
			valueColor = ConfigColor.Bind(configFile, "Colors", "ValueColor", UIColors.Sky, "Rich-text value color for speed (hex RRGGBB or #RRGGBB).");
			currentMoveSpeedField = typeof(Player).GetField("currentMoveSpeed", BindingFlags.Instance | BindingFlags.NonPublic);
			vkField = typeof(Player).GetField("velocity", BindingFlags.Instance | BindingFlags.NonPublic) ?? typeof(Player).GetField("velocity", BindingFlags.Instance | BindingFlags.Public);
			if (vkField == null)
			{
				vkProp = typeof(Player).GetProperty("velocity", BindingFlags.Instance | BindingFlags.Public);
			}
			if (vkField == null && vkProp == null)
			{
				rbField = typeof(Player).GetField("rb", BindingFlags.Instance | BindingFlags.NonPublic) ?? typeof(Player).GetField("rb", BindingFlags.Instance | BindingFlags.Public);
			}
			if (rbField == null && vkField == null && vkProp == null)
			{
				rbProp = typeof(Player).GetProperty("rb", BindingFlags.Instance | BindingFlags.Public);
			}
			if (rbField == null && rbProp == null && vkField == null && vkProp == null)
			{
				moveVelocityField = typeof(Player).GetField("moveVelocity", BindingFlags.Instance | BindingFlags.NonPublic) ?? typeof(Player).GetField("moveVelocity", BindingFlags.Instance | BindingFlags.Public);
			}
		}
		catch (Exception ex)
		{
			SparrohPlugin.Logger.LogError((object)("Failed to initialize Speedometer reflection: " + ex.Message));
		}
	}

	public void UpdateHudVisibility()
	{
		if (hud != null)
		{
			hud.SetActive(enableSpeedometerHUD.Value);
		}
	}

	private void OnEnableSpeedometerHUDChanged(object sender, EventArgs e)
	{
		if (!enableSpeedometerHUD.Value && hud != null)
		{
			DestroyHud();
		}
		UpdateHudVisibility();
	}

	private void OnAnchorChanged(object sender, EventArgs e)
	{
		UpdateAnchors();
	}

	private void UpdateAnchors()
	{
		if (hud != null)
		{
			hud.SetAnchor(speedometerAnchorX.Value, speedometerAnchorY.Value);
		}
	}

	private void CreateSpeedometerHUD()
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		if (hud == null)
		{
			hud = HudBuilder.Create("SpeedometerHUD").ParentToReticle(true).Anchor(speedometerAnchorX.Value, speedometerAnchorY.Value)
				.Pivot(new Vector2(0f, 0.5f))
				.Size(300f, 25f, true)
				.AddText("SpeedText", -1f, (TextAlignmentOptions)513)
				.Build();
			if (hud != null)
			{
				HudRepositionClient.Register("sparroh.speedometer", "Speedometer", hud.Rect, speedometerAnchorX, speedometerAnchorY);
				UpdateHudVisibility();
			}
		}
	}

	private void DestroyHud()
	{
		HudRepositionClient.Unregister("sparroh.speedometer");
		if (hud != null)
		{
			hud.Destroy();
			hud = null;
		}
	}

	public void Update()
	{
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!enableSpeedometerHUD.Value)
			{
				return;
			}
			if (hud == null)
			{
				CreateSpeedometerHUD();
				return;
			}
			if (hud.Primary == null || (Object)(object)Player.LocalPlayer == (Object)null)
			{
				if (hud.Primary != null)
				{
					hud.Primary.Text = "No Player";
				}
				return;
			}
			float num = ReadSpeed();
			if (num > 0f)
			{
				hud.Primary.SetRich("Speed", num, valueColor.Value, "m/s", "F1");
			}
			else
			{
				hud.Primary.Text = "No Speed Detected";
			}
		}
		catch (Exception ex)
		{
			SparrohPlugin.Logger.LogError((object)("Error in Speedometer.Update(): " + ex.Message));
		}
	}

	private float ReadSpeed()
	{
		//IL_0050: 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_0091: 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_00ec: 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_0128: 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_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
		float num = 0f;
		if (vkField != null || vkProp != null)
		{
			if (vkField != null)
			{
				if (vkField.GetValue(Player.LocalPlayer) is Vector3 val)
				{
					num = ((Vector3)(ref val)).magnitude;
				}
			}
			else if (vkProp != null && vkProp.GetValue(Player.LocalPlayer) is Vector3 val2)
			{
				num = ((Vector3)(ref val2)).magnitude;
			}
		}
		else if (rbField != null || rbProp != null)
		{
			Vector3 velocity;
			if (rbField != null)
			{
				object? value = rbField.GetValue(Player.LocalPlayer);
				Rigidbody val3 = (Rigidbody)((value is Rigidbody) ? value : null);
				if (val3 != null)
				{
					velocity = val3.velocity;
					num = ((Vector3)(ref velocity)).magnitude;
				}
			}
			else if (rbProp != null)
			{
				object? value2 = rbProp.GetValue(Player.LocalPlayer);
				Rigidbody val4 = (Rigidbody)((value2 is Rigidbody) ? value2 : null);
				if (val4 != null)
				{
					velocity = val4.velocity;
					num = ((Vector3)(ref velocity)).magnitude;
				}
			}
		}
		if (num == 0f && currentMoveSpeedField != null && currentMoveSpeedField.GetValue(Player.LocalPlayer) is float num2)
		{
			num = num2;
		}
		if (num == 0f && moveVelocityField != null && moveVelocityField.GetValue(Player.LocalPlayer) is Vector3 val5)
		{
			num = ((Vector3)(ref val5)).magnitude;
		}
		return num;
	}

	public void OnDestroy()
	{
		try
		{
			DestroyHud();
		}
		catch (Exception ex)
		{
			SparrohPlugin.Logger.LogError((object)("Error in Speedometer.OnDestroy(): " + ex.Message));
		}
	}
}
namespace Speedometer
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Speedometer";

		public const string PLUGIN_NAME = "Speedometer";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}