Decompiled source of Rangefinder v1.0.0

BepInEx/plugins/Rangefinder.dll

Decompiled 15 hours 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("Rangefinder")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Rangefinder")]
[assembly: AssemblyTitle("Rangefinder")]
[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;
		}
	}
}
[BepInPlugin("sparroh.rangefinder", "Rangefinder", "1.0.0")]
[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.0";

	internal static ManualLogSource Logger;

	private Harmony harmony;

	private RangefinderMod rangefinder;

	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.rangefinder");
		}
		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.rangefinder.cfg");
			fileSystemWatcher.Changed += delegate
			{
				configFile.Reload();
			};
			fileSystemWatcher.EnableRaisingEvents = true;
		}
		catch (Exception ex2)
		{
			Logger.LogWarning((object)("Failed to set up config watcher: " + ex2.Message));
		}
		try
		{
			rangefinder = new RangefinderMod(configFile, harmony);
		}
		catch (Exception ex3)
		{
			Logger.LogError((object)("Failed to initialize Rangefinder: " + ex3.Message));
		}
		try
		{
			harmony.PatchAll();
		}
		catch (Exception ex4)
		{
			Logger.LogError((object)("Failed to apply Harmony patches: " + ex4.Message));
		}
		Logger.LogInfo((object)"Rangefinder loaded successfully.");
	}

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

	private void OnDestroy()
	{
		try
		{
			if (rangefinder != null)
			{
				rangefinder.OnDestroy();
			}
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Error in Rangefinder.OnDestroy(): " + ex.Message));
		}
		try
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Error unpatching Harmony: " + ex2.Message));
		}
	}
}
public class RangefinderHUD
{
	private HudHandle hud;

	public bool IsAlive
	{
		get
		{
			if (hud != null && (Object)(object)hud.GameObject != (Object)null)
			{
				return hud.Primary != null;
			}
			return false;
		}
	}

	public void Setup()
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		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 = ((RangefinderMod.valueColor != null) ? RangefinderMod.valueColor.Value : UIColors.Sky);
			Color val2 = ((RangefinderMod.noTargetColor != null) ? RangefinderMod.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 ((Object)(object)hud.GameObject != (Object)null)
			{
				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", "EnableRangefinderHUD", true, "Enable the rangefinder display");
		rangefinderMaxRange = configFile.Bind<float>("General", "RangefinderMaxRange", 500f, "Maximum range for rangefinder (meters)");
		valueColor = ConfigColor.Bind(configFile, "Colors", "ValueColor", UIColors.Sky, "Rich-text color for range values (hex RRGGBB or #RRGGBB).");
		noTargetColor = ConfigColor.Bind(configFile, "Colors", "NoTargetColor", 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();
	}
}
internal static class RangefinderSystem
{
	private static RangefinderHUD rangefinderHUD;

	private static bool isInitialized;

	private static LayerMask raycastLayers;

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

	private static float MaxRange
	{
		get
		{
			if (RangefinderMod.rangefinderMaxRange == null)
			{
				return 500f;
			}
			return RangefinderMod.rangefinderMaxRange.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.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}