Decompiled source of Rangefinder v1.0.2

BepInEx/plugins/Rangefinder.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("Rangefinder")]
[assembly: AssemblyTitle("Rangefinder")]
[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 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 pendingVisibilityRefresh;

	private static volatile bool reloadPending;

	private static float lastReloadTime;

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

	public static ConfigEntry<float> MaxRange { get; private set; }

	public static ConfigColor ValueColor { get; private set; }

	public static ConfigColor NoTargetColor { 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)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		config = configFile;
		logger = log;
		EnableRangefinder = config.Bind<bool>("General", "Enable Rangefinder", true, "Enable the rangefinder display");
		MaxRange = config.Bind<float>("General", "Max Range", 500f, "Maximum range for rangefinder (meters)");
		ValueColor = ConfigColor.Bind(config, "Colors", "Rangefinder Color", UIColors.Sky, "Rich-text color for range values (hex RRGGBB or #RRGGBB).");
		NoTargetColor = ConfigColor.Bind(config, "Colors", "No Target Color", UIColors.TextMuted, "Rich-text color when no target is hit (hex RRGGBB or #RRGGBB).");
		EnableRangefinder.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();
			pendingVisibilityRefresh = true;
			logger.LogInfo((object)"Config reloaded from disk.");
		}
		catch (Exception ex)
		{
			logger.LogError((object)("Error reloading config: " + ex.Message));
		}
	}

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

	public static void Dispose()
	{
		if (EnableRangefinder != null)
		{
			EnableRangefinder.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.rangefinder.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)
	{
		pendingVisibilityRefresh = true;
	}
}
[BepInPlugin("sparroh.rangefinder", "Rangefinder", "1.0.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[MycoMod(/*Could not decode attribute arguments.*/)]
public class SparrohPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "sparroh.rangefinder";

	public const string PluginName = "Rangefinder";

	public const string PluginVersion = "1.0.2";

	internal static ManualLogSource Logger;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Expected O, but got Unknown
		Logger = ((BaseUnityPlugin)this).Logger;
		ConfigManager.Initialize(((BaseUnityPlugin)this).Config, Logger);
		try
		{
			harmony = new Harmony("sparroh.rangefinder");
			harmony.PatchAll(typeof(RangefinderPatches));
			Logger.LogInfo((object)"Harmony patches applied.");
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Failed to apply Harmony patches: " + ex.Message));
		}
		Logger.LogInfo((object)"Rangefinder v1.0.2 loaded successfully.");
	}

	private void Update()
	{
		ConfigManager.Tick();
		if (ConfigManager.ConsumePendingRefresh())
		{
			RangefinderSystem.SetEnabled(ConfigManager.EnableRangefinder.Value);
		}
	}

	private void OnDestroy()
	{
		try
		{
			RangefinderSystem.Cleanup();
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error in RangefinderSystem.Cleanup(): " + ex.Message));
		}
		ConfigManager.Dispose();
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}
}
public class RangefinderHUD
{
	private HudHandle hud;

	public bool IsAlive
	{
		get
		{
			if (HudHandle.IsValid(hud))
			{
				return hud.Primary != null;
			}
			return false;
		}
	}

	public void Setup()
	{
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		if (hud != null && !hud.IsAlive)
		{
			hud = null;
		}
		if (!IsAlive)
		{
			Transform val = null;
			Transform val2 = default(Transform);
			if (UIHelpers.TryGetReticle(ref val2))
			{
				val = val2;
			}
			else if ((Object)(object)PlayerLook.Instance != (Object)null)
			{
				val = (Transform)(object)PlayerLook.Instance.DefaultHUDParent;
			}
			HudBuilder val3 = HudBuilder.Create("RangefinderHUD").Size(200f, 40f, true).Pivot(new Vector2(0.5f, 0.5f))
				.AddText("RangeText", 22f, (TextAlignmentOptions)514);
			if ((Object)(object)val != (Object)null)
			{
				val3.Parent(val);
			}
			else
			{
				val3.ParentToReticle(true);
			}
			val3.Anchor(0.5f, 0.42f);
			hud = val3.Build();
		}
	}

	public void UpdateRange(float distance)
	{
		//IL_0029: 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)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: 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_0047: 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_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		if (IsAlive && hud.Primary != null)
		{
			Color val = ((ConfigManager.ValueColor != null) ? ConfigManager.ValueColor.Value : UIColors.Sky);
			Color val2 = ((ConfigManager.NoTargetColor != null) ? ConfigManager.NoTargetColor.Value : UIColors.TextMuted);
			if (distance >= 1000f)
			{
				hud.Primary.Text = RichText.Colorize("∞ m", val);
			}
			else if (distance < 0f)
			{
				hud.Primary.Text = RichText.Colorize("--- m", val2);
			}
			else
			{
				hud.Primary.Text = RichText.Colorize($"{distance:F1} m", val);
			}
		}
	}

	public void SetEnabled(bool enabled)
	{
		if (IsAlive)
		{
			hud.SetActive(enabled);
		}
	}

	public void Destroy()
	{
		if (hud != null)
		{
			if (hud.IsAlive)
			{
				hud.Destroy();
			}
			hud = null;
		}
	}
}
public class RangefinderMod
{
	[HarmonyPatch]
	public static class RangefinderPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(MissionHUD), "Start")]
		public static void MissionHUD_Start_Postfix()
		{
			RangefinderSystem.Initialize();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MissionHUD), "Update")]
		public static void MissionHUD_Update_Postfix()
		{
			RangefinderSystem.UpdateRangefinder();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MissionHUD), "OnDestroy")]
		public static void MissionHUD_OnDestroy_Postfix()
		{
			RangefinderSystem.Cleanup();
		}
	}

	public static ConfigEntry<bool> enableRangefinder;

	public static ConfigEntry<float> rangefinderMaxRange;

	public static ConfigColor valueColor;

	public static ConfigColor noTargetColor;

	private readonly ConfigFile configFile;

	private readonly Harmony harmony;

	public RangefinderMod(ConfigFile configFile, Harmony harmony)
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		this.configFile = configFile;
		this.harmony = harmony;
		enableRangefinder = configFile.Bind<bool>("General", "Enable Rangefinder", true, "Enable the rangefinder display");
		rangefinderMaxRange = configFile.Bind<float>("General", "Max Range", 500f, "Maximum range for rangefinder (meters)");
		valueColor = ConfigColor.Bind(configFile, "Colors", "Rangefinder Color", UIColors.Sky, "Rich-text color for range values (hex RRGGBB or #RRGGBB).");
		noTargetColor = ConfigColor.Bind(configFile, "Colors", "No Target Color", UIColors.TextMuted, "Rich-text color when no target is hit (hex RRGGBB or #RRGGBB).");
		enableRangefinder.SettingChanged += OnEnableRangefinderChanged;
		harmony.PatchAll(typeof(RangefinderPatches));
	}

	public void UpdateHudVisibility()
	{
		RangefinderSystem.SetEnabled(enableRangefinder.Value);
	}

	public void Update()
	{
		RangefinderSystem.UpdateRangefinder();
	}

	public void OnDestroy()
	{
		RangefinderSystem.Cleanup();
		harmony.UnpatchSelf();
	}

	private void OnEnableRangefinderChanged(object sender, EventArgs e)
	{
		UpdateHudVisibility();
	}
}
[HarmonyPatch]
public static class RangefinderPatches
{
	[HarmonyPostfix]
	[HarmonyPatch(typeof(MissionHUD), "Start")]
	public static void MissionHUD_Start_Postfix()
	{
		RangefinderSystem.Initialize();
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(MissionHUD), "Update")]
	public static void MissionHUD_Update_Postfix()
	{
		RangefinderSystem.UpdateRangefinder();
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(MissionHUD), "OnDestroy")]
	public static void MissionHUD_OnDestroy_Postfix()
	{
		RangefinderSystem.Cleanup();
	}
}
internal static class RangefinderSystem
{
	private static RangefinderHUD rangefinderHUD;

	private static bool isInitialized;

	private static readonly LayerMask raycastLayers;

	private static bool EnableRangefinder
	{
		get
		{
			if (ConfigManager.EnableRangefinder != null)
			{
				return ConfigManager.EnableRangefinder.Value;
			}
			return false;
		}
	}

	private static float MaxRange
	{
		get
		{
			if (ConfigManager.MaxRange == null)
			{
				return 500f;
			}
			return ConfigManager.MaxRange.Value;
		}
	}

	static RangefinderSystem()
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		raycastLayers = LayerMask.op_Implicit(LayerMask.GetMask(new string[3] { "Default", "Terrain", "Environment" }));
	}

	public static void Initialize()
	{
		if (isInitialized && rangefinderHUD != null && rangefinderHUD.IsAlive)
		{
			return;
		}
		try
		{
			if (rangefinderHUD == null)
			{
				rangefinderHUD = new RangefinderHUD();
			}
			rangefinderHUD.Setup();
			rangefinderHUD.SetEnabled(EnableRangefinder);
			isInitialized = rangefinderHUD.IsAlive;
		}
		catch (Exception ex)
		{
			SparrohPlugin.Logger.LogError((object)("Failed to initialize Rangefinder: " + ex.Message));
			isInitialized = false;
		}
	}

	public static void UpdateRangefinder()
	{
		//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_0083: Unknown result type (might be due to invalid IL or missing references)
		if (!EnableRangefinder)
		{
			return;
		}
		if (rangefinderHUD == null || !rangefinderHUD.IsAlive)
		{
			isInitialized = false;
			Initialize();
			if (!isInitialized)
			{
				return;
			}
		}
		try
		{
			RaycastHit val = default(RaycastHit);
			if ((Object)(object)Camera.main == (Object)null)
			{
				rangefinderHUD.UpdateRange(-1f);
			}
			else if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3((float)Screen.width / 2f, (float)Screen.height / 2f, 0f)), ref val, MaxRange, LayerMask.op_Implicit(raycastLayers)))
			{
				rangefinderHUD.UpdateRange(((RaycastHit)(ref val)).distance);
			}
			else
			{
				rangefinderHUD.UpdateRange(-1f);
			}
		}
		catch (Exception ex)
		{
			SparrohPlugin.Logger.LogError((object)("Error updating rangefinder: " + ex.Message));
			rangefinderHUD?.UpdateRange(-1f);
		}
	}

	public static void SetEnabled(bool enabled)
	{
		if (rangefinderHUD != null)
		{
			rangefinderHUD.SetEnabled(enabled);
		}
	}

	public static void Cleanup()
	{
		if (rangefinderHUD != null)
		{
			rangefinderHUD.Destroy();
			rangefinderHUD = null;
		}
		isInitialized = false;
	}
}
namespace Rangefinder
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Rangefinder";

		public const string PLUGIN_NAME = "Rangefinder";

		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)
		{
		}
	}
}