Decompiled source of AutoPinSigns v1.0.7

AutoPinSigns.dll

Decompiled 6 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("AutoPinSigns")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AutoPinSigns")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1f4794c6-9828-458d-bf06-a6acb8e038ac")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace AutoPinSigns;

[BepInPlugin("shudnal.AutoPinSigns", "Auto Pin Signs", "1.0.7")]
public class AutoPinSigns : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Sign), "UpdateText")]
	public static class Sign_UpdateText_patch
	{
		private static void Postfix(Sign __instance)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: 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_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value || !Object.op_Implicit((Object)(object)Minimap.instance))
			{
				return;
			}
			string text = Regex.Replace(__instance.GetText().ToLower(), "<(.|\\n)*?>", "");
			Vector3 position = ((Component)__instance).transform.position;
			if (itemsPins.ContainsKey(position))
			{
				string text2 = itemsPins[position];
				if (!IsPinnableSign(text))
				{
					DeleteClosestPins(position);
					return;
				}
				if (!(text2 != text))
				{
					return;
				}
				DeleteClosestPins(position);
			}
			if (!IsPinnableSign(text))
			{
				return;
			}
			PinType icon = GetIcon(text);
			if (!Minimap.instance.HaveSimilarPin(position, icon, text, true))
			{
				Minimap.instance.AddPin(position, icon, text, true, false, 0L, "");
				itemsPins.Add(position, text);
				Sprite sprite = Minimap.instance.GetSprite(icon);
				Player localPlayer = Player.m_localPlayer;
				if (localPlayer != null)
				{
					((Character)localPlayer).Message((MessageType)1, "$msg_pin_added: " + text, 0, sprite);
				}
			}
		}
	}

	[HarmonyPatch(typeof(WearNTear), "Destroy")]
	public static class WearNTear_Destroy_patch
	{
		private static void Prefix(ref WearNTear __instance, ZNetView ___m_nview)
		{
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			Sign val = default(Sign);
			if (modEnabled.Value && Object.op_Implicit((Object)(object)___m_nview) && ___m_nview.IsValid() && ((Component)__instance).TryGetComponent<Sign>(ref val) && IsPinnableSign(val.GetText().ToLower()) && itemsPins.ContainsKey(((Component)__instance).transform.position))
			{
				DeleteClosestPins(((Component)__instance).transform.position, 1f);
			}
		}
	}

	[Serializable]
	[CompilerGenerated]
	private sealed class <>c
	{
		public static readonly <>c <>9 = new <>c();

		public static ConsoleEvent <>9__21_0;

		public static ConsoleOptionsFetcher <>9__21_1;

		public static Func<KeyValuePair<Vector3, string>, Vector3> <>9__31_1;

		public static Func<KeyValuePair<Vector3, string>, string> <>9__31_2;

		internal void <InitCommands>b__21_0(ConsoleEventArgs args)
		{
			//IL_00a9: 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)
			if (!modEnabled.Value)
			{
				args.Context.AddString("Mod disabled");
			}
			else
			{
				if (!Object.op_Implicit((Object)(object)Player.m_localPlayer))
				{
					return;
				}
				if (args.Args.Length >= 2 && args.Args[1] == "clear")
				{
					if (args.Args.Length > 2 && float.TryParse(args.Args[2], out var result))
					{
						DeleteClosestPins(((Component)Player.m_localPlayer).transform.position, result);
					}
					else
					{
						DeleteClosestPins(((Component)Player.m_localPlayer).transform.position);
					}
				}
				else
				{
					args.Context.AddString("Syntax: " + typeof(AutoPinSigns).Namespace.ToLower() + " [action]");
				}
			}
		}

		internal List<string> <InitCommands>b__21_1()
		{
			return new List<string> { "clear [range] -  Clear closest to current player pins in set range. Default 5" };
		}

		internal Vector3 <DeleteClosestPins>b__31_1(KeyValuePair<Vector3, string> kv)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			return kv.Key;
		}

		internal string <DeleteClosestPins>b__31_2(KeyValuePair<Vector3, string> kv)
		{
			return kv.Value;
		}
	}

	private const string pluginID = "shudnal.AutoPinSigns";

	private const string pluginName = "Auto Pin Signs";

	private const string pluginVersion = "1.0.7";

	private Harmony _harmony;

	private static ConfigEntry<bool> modEnabled;

	private static ConfigEntry<bool> allowSubstrings;

	private static ConfigEntry<string> configFireList;

	private static ConfigEntry<string> configBaseList;

	private static ConfigEntry<string> configHammerList;

	private static ConfigEntry<string> configPinList;

	private static ConfigEntry<string> configPortalList;

	private static Dictionary<Vector3, string> itemsPins = new Dictionary<Vector3, string>();

	private static readonly HashSet<string> fireList = new HashSet<string>();

	private static readonly HashSet<string> baseList = new HashSet<string>();

	private static readonly HashSet<string> hammerList = new HashSet<string>();

	private static readonly HashSet<string> pinList = new HashSet<string>();

	private static readonly HashSet<string> portalList = new HashSet<string>();

	private static AutoPinSigns instance;

	private void Awake()
	{
		ConfigInit();
		_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "shudnal.AutoPinSigns");
		instance = this;
	}

	private void OnDestroy()
	{
		((BaseUnityPlugin)this).Config.Save();
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}

	private void ConfigInit()
	{
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable the mod");
		allowSubstrings = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Less strict string comparison", false, "Less strict comparison of config substrings. Enable to create pins if sign have any substring instead of exact match");
		configFireList = ((BaseUnityPlugin)this).Config.Bind<string>("Signs", "FireList", "fire", "List of the case-insensitive strings to add Fire pin.  Comma-separate each string.  Default: fire");
		configBaseList = ((BaseUnityPlugin)this).Config.Bind<string>("Signs", "BaseList", "base", "List of the case-insensitive strings to add Base pin.  Comma-separate each string.  Default: base");
		configHammerList = ((BaseUnityPlugin)this).Config.Bind<string>("Signs", "HammerList", "hammer", "List of the strings to add Hammer pin.  Comma-separate each string.  Default: hammer");
		configPinList = ((BaseUnityPlugin)this).Config.Bind<string>("Signs", "PinList", "pin,dot", "List of the strings to add Dot pin.  Comma-separate each string.  Default: pin,dot");
		configPortalList = ((BaseUnityPlugin)this).Config.Bind<string>("Signs", "PortalList", "portal", "List of the strings to add Portal pin.  Comma-separate each string.  Default: portal");
		AddToHS(configFireList.Value, fireList);
		AddToHS(configBaseList.Value, baseList);
		AddToHS(configHammerList.Value, hammerList);
		AddToHS(configPinList.Value, pinList);
		AddToHS(configPortalList.Value, portalList);
		InitCommands();
	}

	public static void InitCommands()
	{
		//IL_0037: 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_0042: Expected O, but got Unknown
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: 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_0066: Expected O, but got Unknown
		string obj = typeof(AutoPinSigns).Namespace.ToLower() ?? "";
		object obj2 = <>c.<>9__21_0;
		if (obj2 == null)
		{
			ConsoleEvent val = delegate(ConsoleEventArgs args)
			{
				//IL_00a9: 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)
				if (!modEnabled.Value)
				{
					args.Context.AddString("Mod disabled");
				}
				else if (Object.op_Implicit((Object)(object)Player.m_localPlayer))
				{
					if (args.Args.Length >= 2 && args.Args[1] == "clear")
					{
						if (args.Args.Length > 2 && float.TryParse(args.Args[2], out var result))
						{
							DeleteClosestPins(((Component)Player.m_localPlayer).transform.position, result);
						}
						else
						{
							DeleteClosestPins(((Component)Player.m_localPlayer).transform.position);
						}
					}
					else
					{
						args.Context.AddString("Syntax: " + typeof(AutoPinSigns).Namespace.ToLower() + " [action]");
					}
				}
			};
			<>c.<>9__21_0 = val;
			obj2 = (object)val;
		}
		object obj3 = <>c.<>9__21_1;
		if (obj3 == null)
		{
			ConsoleOptionsFetcher val2 = () => new List<string> { "clear [range] -  Clear closest to current player pins in set range. Default 5" };
			<>c.<>9__21_1 = val2;
			obj3 = (object)val2;
		}
		new ConsoleCommand(obj, "[action]", (ConsoleEvent)obj2, false, false, false, false, false, (ConsoleOptionsFetcher)obj3, true, false, false);
	}

	private static bool IsSign(HashSet<string> list, string text)
	{
		if (allowSubstrings.Value)
		{
			return list.Any((string x) => text.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
		}
		return list.Contains(text);
	}

	private static bool IsFireSign(string text)
	{
		return IsSign(fireList, text);
	}

	private static bool IsBaseSign(string text)
	{
		return IsSign(baseList, text);
	}

	private static bool IsHammerSign(string text)
	{
		return IsSign(hammerList, text);
	}

	private static bool IsPinSign(string text)
	{
		return IsSign(pinList, text);
	}

	private static bool IsPortalSign(string text)
	{
		return IsSign(portalList, text);
	}

	private static bool IsPinnableSign(string text)
	{
		return IsFireSign(text) || IsBaseSign(text) || IsHammerSign(text) || IsPinSign(text) || IsPortalSign(text);
	}

	private static PinType GetIcon(string text)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: 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)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		if (IsFireSign(text))
		{
			return (PinType)0;
		}
		if (IsBaseSign(text))
		{
			return (PinType)1;
		}
		if (IsHammerSign(text))
		{
			return (PinType)2;
		}
		if (IsPinSign(text))
		{
			return (PinType)3;
		}
		if (IsPortalSign(text))
		{
			return (PinType)6;
		}
		return (PinType)3;
	}

	private static void AddToHS(string text, HashSet<string> HS)
	{
		HS.Clear();
		char[] separator = new char[1] { ',' };
		string[] array = text.Replace(" ", "").Split(separator, StringSplitOptions.RemoveEmptyEntries);
		foreach (string item in array)
		{
			HS.Add(item);
		}
	}

	public static void DeleteClosestPins(Vector3 pos, float distance = 5f)
	{
		//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)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		while (FindAndDeleteClosestPin(pos, distance))
		{
		}
		itemsPins = itemsPins.Where((KeyValuePair<Vector3, string> pin_pos) => Utils.DistanceXZ(pos, pin_pos.Key) >= distance).ToDictionary((KeyValuePair<Vector3, string> kv) => kv.Key, (KeyValuePair<Vector3, string> kv) => kv.Value);
	}

	public static bool FindAndDeleteClosestPin(Vector3 pos, float distance = 5f)
	{
		//IL_002c: 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)
		if (Object.op_Implicit((Object)(object)Minimap.instance))
		{
			foreach (PinData pin in Minimap.instance.m_pins)
			{
				if (Utils.DistanceXZ(pos, pin.m_pos) < distance)
				{
					Minimap.instance.RemovePin(pin);
					return true;
				}
			}
		}
		return false;
	}
}