Decompiled source of QuickPins v1.0.1

QuickPins.dll

Decompiled 21 hours 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 HarmonyLib;
using Microsoft.CodeAnalysis;
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("QuickPins")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("QuickPins")]
[assembly: AssemblyTitle("QuickPins")]
[assembly: AssemblyVersion("1.0.0.0")]
[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;
		}
	}
}
namespace QuickPins
{
	[BepInPlugin("com.dreich.quickpins", "Quick Pins", "1.0.1")]
	public class QuickPinsPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.dreich.quickpins";

		public const string PluginName = "Quick Pins";

		public const string PluginVersion = "1.0.1";

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<float> MinDistance;

		internal static ConfigEntry<bool> ShowMessage;

		private static readonly List<PinBinding> bindings = new List<PinBinding>();

		private void Awake()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Turn the pin keys on or off.");
			MinDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MinDistance", 10f, new ConfigDescription("A pin is not placed if a pin of the same type is already within this many meters of you. 0 = always place.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			ShowMessage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowMessage", true, "Show a message in the top left corner when a pin is placed.");
			AddBinding((PinType)0, "Fire", (KeyCode)257);
			AddBinding((PinType)1, "Home", (KeyCode)258);
			AddBinding((PinType)2, "Hammer", (KeyCode)259);
			AddBinding((PinType)3, "Dot", (KeyCode)260);
			AddBinding((PinType)6, "Portal", (KeyCode)261);
		}

		private void AddBinding(PinType type, string displayName, KeyCode defaultKey)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			ConfigEntry<KeyboardShortcut> key = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>(displayName + " Pin", "Key", new KeyboardShortcut(defaultKey, Array.Empty<KeyCode>()), "Key that places a " + displayName + " pin at your position. Set to None to turn it off.");
			ConfigEntry<string> label = ((BaseUnityPlugin)this).Config.Bind<string>(displayName + " Pin", "Label", "", "Text shown next to the pin on the map. Leave empty for no text.");
			bindings.Add(new PinBinding(type, displayName, key, label));
		}

		private void Update()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			if (!Enabled.Value || !QuickPin.CanUseKeys())
			{
				return;
			}
			foreach (PinBinding binding in bindings)
			{
				KeyboardShortcut value = binding.Key.Value;
				if ((int)((KeyboardShortcut)(ref value)).MainKey != 0)
				{
					value = binding.Key.Value;
					if (((KeyboardShortcut)(ref value)).IsDown())
					{
						QuickPin.Place(binding);
						break;
					}
				}
			}
		}
	}
	internal sealed class PinBinding
	{
		internal readonly PinType Type;

		internal readonly string DisplayName;

		internal readonly ConfigEntry<KeyboardShortcut> Key;

		internal readonly ConfigEntry<string> Label;

		internal PinBinding(PinType type, string displayName, ConfigEntry<KeyboardShortcut> key, ConfigEntry<string> label)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			Type = type;
			DisplayName = displayName;
			Key = key;
			Label = label;
		}
	}
	internal static class QuickPin
	{
		private static readonly FieldRef<Minimap, List<PinData>> PinsRef = AccessTools.FieldRefAccess<Minimap, List<PinData>>("m_pins");

		private static readonly MethodInfo AddPinMethod = AccessTools.Method(typeof(Minimap), "AddPin", (Type[])null, (Type[])null);

		internal static bool CanUseKeys()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsDead() || (Object)(object)Minimap.instance == (Object)null)
			{
				return false;
			}
			if ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus())
			{
				return false;
			}
			if (Console.IsVisible() || TextInput.IsVisible() || Minimap.InTextInput())
			{
				return false;
			}
			if (Menu.IsVisible() || InventoryGui.IsVisible() || StoreGui.IsVisible())
			{
				return false;
			}
			return true;
		}

		internal static void Place(PinBinding binding)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			Minimap instance = Minimap.instance;
			if ((Object)(object)localPlayer == (Object)null || (Object)(object)instance == (Object)null || AddPinMethod == null)
			{
				return;
			}
			Vector3 position = ((Component)localPlayer).transform.position;
			if (HasNearbyPin(instance, binding.Type, position))
			{
				if (QuickPinsPlugin.ShowMessage.Value)
				{
					((Character)localPlayer).Message((MessageType)1, "There is already a " + binding.DisplayName + " pin here", 0, (Sprite)null, false);
				}
				return;
			}
			string text = (binding.Label.Value ?? "").Trim();
			ParameterInfo[] parameters = AddPinMethod.GetParameters();
			object[] array = new object[parameters.Length];
			array[0] = position;
			array[1] = binding.Type;
			array[2] = text;
			array[3] = true;
			array[4] = false;
			if (parameters.Length > 5)
			{
				array[5] = 0L;
			}
			AddPinMethod.Invoke(instance, array);
			if (QuickPinsPlugin.ShowMessage.Value)
			{
				((Character)localPlayer).Message((MessageType)1, "Placed a " + binding.DisplayName + " pin", 0, (Sprite)null, false);
			}
		}

		private static bool HasNearbyPin(Minimap minimap, PinType type, Vector3 position)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: 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_0047: Unknown result type (might be due to invalid IL or missing references)
			float value = QuickPinsPlugin.MinDistance.Value;
			if (value <= 0f)
			{
				return false;
			}
			List<PinData> list = PinsRef.Invoke(minimap);
			if (list == null)
			{
				return false;
			}
			foreach (PinData item in list)
			{
				if (item.m_type == type && Utils.DistanceXZ(item.m_pos, position) < value)
				{
					return true;
				}
			}
			return false;
		}
	}
}