Decompiled source of Altimeter v1.0.2

BepInEx/plugins/Altimeter.dll

Decompiled a month 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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Sparroh")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("Altimeter")]
[assembly: AssemblyTitle("Altimeter")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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 class AltimeterMod
{
	private const float RaycastMaxDistance = 1000f;

	private HudHandle hud;

	public static AltimeterMod Instance { get; private set; }

	public bool IsActive
	{
		get
		{
			if (HudHandle.IsValid(hud))
			{
				return hud.IsActive;
			}
			return false;
		}
	}

	public Vector2 GetSize
	{
		get
		{
			//IL_0019: 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)
			if (!HudHandle.IsValid(hud))
			{
				return Vector2.zero;
			}
			return hud.Size;
		}
	}

	public AltimeterMod()
	{
		Instance = this;
	}

	public void OnConfigChanged()
	{
		if (!ConfigManager.EnableAltimeterHUD.Value && HudHandle.IsValid(hud))
		{
			DestroyHud();
		}
		UpdateHudVisibility();
	}

	public void UpdateHudVisibility()
	{
		if (HudHandle.IsValid(hud))
		{
			hud.SetActive(ConfigManager.EnableAltimeterHUD.Value);
		}
	}

	private void CreateAltimeterHUD()
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		if (hud != null && !hud.IsAlive)
		{
			hud = null;
		}
		if (!HudHandle.IsValid(hud))
		{
			hud = HudBuilder.Create("AltimeterHUD").ParentToReticle(true).Anchor(ConfigManager.Anchors.XValue, ConfigManager.Anchors.YValue)
				.Pivot(new Vector2(0f, 0.5f))
				.Size(300f, 25f, true)
				.AddText("AltitudeText", -1f, (TextAlignmentOptions)513)
				.Build();
			if (HudHandle.IsValid(hud))
			{
				hud.EnableReposition("sparroh.altimeter", "Altimeter", ConfigManager.Anchors);
				UpdateHudVisibility();
			}
		}
	}

	private void DestroyHud()
	{
		if (hud != null)
		{
			if (hud.IsAlive)
			{
				hud.Destroy();
			}
			hud = null;
		}
	}

	public void Update()
	{
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		if (!ConfigManager.EnableAltimeterHUD.Value)
		{
			return;
		}
		if (!HudHandle.IsValid(hud))
		{
			CreateAltimeterHUD();
		}
		else if (hud.Primary != null && !((Object)(object)Player.LocalPlayer == (Object)null))
		{
			float num = CalculateAltitude();
			if (num < 1.3f)
			{
				hud.Primary.Text = "On Ground";
			}
			else if (num >= 1000f)
			{
				hud.Primary.Text = "Too High";
			}
			else
			{
				hud.Primary.SetRich("Altitude", num, ConfigManager.ValueColor.Value, "m", "F1");
			}
		}
	}

	private float CalculateAltitude()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: 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_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_0054: 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)
		if ((Object)(object)Player.LocalPlayer == (Object)null)
		{
			return 0f;
		}
		Vector3 val = ((Component)Player.LocalPlayer).transform.position + Vector3.up * 0.1f;
		Vector3 down = Vector3.down;
		RaycastHit[] array = Physics.RaycastAll(val, down, 1000f);
		float num = float.MaxValue;
		RaycastHit[] array2 = array;
		for (int i = 0; i < array2.Length; i++)
		{
			RaycastHit val2 = array2[i];
			if ((Object)(object)((Component)((RaycastHit)(ref val2)).collider).gameObject != (Object)(object)((Component)Player.LocalPlayer).gameObject && !((RaycastHit)(ref val2)).collider.isTrigger)
			{
				num = Mathf.Min(num, ((RaycastHit)(ref val2)).distance);
			}
		}
		if (!(num < float.MaxValue))
		{
			return 1000f;
		}
		return num;
	}

	public void OnDestroy()
	{
		DestroyHud();
	}
}
public static class ConfigManager
{
	private const float DebounceSeconds = 0.25f;

	private static ConfigFile config;

	private static ManualLogSource logger;

	private static FileSystemWatcher configWatcher;

	private static volatile bool pendingRefresh;

	private static volatile bool reloadPending;

	private static float lastReloadTime;

	public static ConfigEntry<bool> EnableAltimeterHUD { get; private set; }

	public static HudAnchors Anchors { get; private set; }

	public static ConfigColor ValueColor { get; private set; }

	public static void Initialize(ConfigFile configFile, ManualLogSource log)
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		config = configFile;
		logger = log;
		EnableAltimeterHUD = config.Bind<bool>("General", "Enable Altimeter", true, "Enables the Altimeter HUD display showing player altitude above ground.");
		Anchors = HudAnchors.Bind(config, "Altimeter", 0.06355292f, 0.2589327f, "HUD Positioning");
		ValueColor = ConfigColor.Bind(config, "Colors", "Altimeter Color", UIColors.Shamrock, "Rich-text value color for altitude (hex RRGGBB or #RRGGBB).");
		EnableAltimeterHUD.SettingChanged += OnSettingChanged;
		try
		{
			SetupFileWatcher();
		}
		catch (Exception ex)
		{
			logger.LogError((object)("Error setting up config file watcher: " + ex.Message));
		}
	}

	public static void Tick()
	{
		if (!reloadPending || Time.unscaledTime - lastReloadTime < 0.25f)
		{
			return;
		}
		reloadPending = false;
		lastReloadTime = Time.unscaledTime;
		try
		{
			config.Reload();
			pendingRefresh = true;
			logger.LogInfo((object)"Config reloaded from disk.");
		}
		catch (Exception ex)
		{
			logger.LogError((object)("Error reloading config: " + ex.Message));
		}
	}

	public static bool ConsumePendingRefresh()
	{
		if (!pendingRefresh)
		{
			return false;
		}
		pendingRefresh = false;
		return true;
	}

	public static void Dispose()
	{
		if (EnableAltimeterHUD != null)
		{
			EnableAltimeterHUD.SettingChanged -= OnSettingChanged;
		}
		if (configWatcher != null)
		{
			configWatcher.EnableRaisingEvents = false;
			configWatcher.Changed -= OnConfigFileChanged;
			configWatcher.Created -= OnConfigFileChanged;
			configWatcher.Renamed -= OnConfigFileChanged;
			configWatcher.Dispose();
			configWatcher = null;
		}
	}

	private static void SetupFileWatcher()
	{
		configWatcher = new FileSystemWatcher(Paths.ConfigPath, "sparroh.altimeter.cfg");
		configWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
		configWatcher.Changed += OnConfigFileChanged;
		configWatcher.Created += OnConfigFileChanged;
		configWatcher.Renamed += OnConfigFileChanged;
		configWatcher.EnableRaisingEvents = true;
	}

	private static void OnConfigFileChanged(object sender, FileSystemEventArgs e)
	{
		reloadPending = true;
	}

	private static void OnSettingChanged(object sender, EventArgs e)
	{
		pendingRefresh = true;
	}
}
[BepInPlugin("sparroh.altimeter", "Altimeter", "1.0.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[MycoMod(/*Could not decode attribute arguments.*/)]
public class AltimeterPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "sparroh.altimeter";

	public const string PluginName = "Altimeter";

	public const string PluginVersion = "1.0.2";

	internal static ManualLogSource Logger;

	private AltimeterMod altimeter;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Expected O, but got Unknown
		Logger = ((BaseUnityPlugin)this).Logger;
		try
		{
			ConfigManager.Initialize(((BaseUnityPlugin)this).Config, Logger);
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Failed to initialize config: " + ex.Message));
			return;
		}
		try
		{
			harmony = new Harmony("sparroh.altimeter");
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Failed to create Harmony instance: " + ex2.Message));
			return;
		}
		try
		{
			altimeter = new AltimeterMod();
		}
		catch (Exception ex3)
		{
			Logger.LogError((object)("Failed to initialize Altimeter: " + ex3.Message));
		}
		try
		{
			harmony.PatchAll();
		}
		catch (Exception ex4)
		{
			Logger.LogError((object)("Failed to apply Harmony patches: " + ex4.Message));
		}
		Logger.LogInfo((object)"Altimeter loaded successfully.");
	}

	private void Update()
	{
		try
		{
			ConfigManager.Tick();
			if (ConfigManager.ConsumePendingRefresh() && altimeter != null)
			{
				altimeter.OnConfigChanged();
			}
			if (altimeter != null)
			{
				altimeter.Update();
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error in Altimeter.Update(): " + ex.Message));
		}
	}

	private void OnDestroy()
	{
		try
		{
			if (altimeter != null)
			{
				altimeter.OnDestroy();
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error in Altimeter.OnDestroy(): " + ex.Message));
		}
		try
		{
			ConfigManager.Dispose();
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Error disposing config: " + ex2.Message));
		}
		try
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
		catch (Exception ex3)
		{
			Logger.LogError((object)("Error unpatching Harmony: " + ex3.Message));
		}
	}
}
namespace Altimeter
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Altimeter";

		public const string PLUGIN_NAME = "Altimeter";

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