Decompiled source of MoreMapPins v1.0.0

MoreMapPins.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GUIFramework;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;

[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("MoreMapPins")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MoreMapPins")]
[assembly: AssemblyTitle("MoreMapPins")]
[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 MoreMapPins
{
	[BepInPlugin("roznik.moremapPins", "More Map Pins", "1.0.0")]
	public class MoreMapPinsPlugin : BaseUnityPlugin
	{
		public const string Guid = "roznik.moremapPins";

		public const string PluginName = "More Map Pins";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("roznik.moremapPins");
			Patch(typeof(MinimapStartPatch));
			Patch(typeof(PinNameCapturePatch));
			Log.LogInfo((object)"More Map Pins loaded.");
		}

		private void Patch(Type type)
		{
			try
			{
				_harmony.PatchAll(type);
			}
			catch (Exception ex)
			{
				Log.LogError((object)("More Map Pins could not patch " + type.Name + ": " + ex));
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class Pins
	{
		internal sealed class PinDef
		{
			public readonly string Resource;

			public readonly PinType Type;

			public PinDef(string resource, int index)
			{
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				Resource = resource;
				Type = (PinType)(100 + index);
			}
		}

		public const int FirstType = 100;

		public static readonly PinDef[] All = new PinDef[5]
		{
			new PinDef("ore", 0),
			new PinDef("cherries", 1),
			new PinDef("cave", 2),
			new PinDef("ruins", 3),
			new PinDef("portal", 4)
		};
	}
	internal static class PinNames
	{
		public static string Last;
	}
	[HarmonyPatch(typeof(Minimap), "OnPinTextEntered")]
	internal static class PinNameCapturePatch
	{
		private static void Prefix(Minimap __instance)
		{
			GuiInputField nameInput = __instance.m_nameInput;
			if (!((Object)(object)nameInput == (Object)null))
			{
				string text = ((TMP_InputField)nameInput).text;
				if (!string.IsNullOrWhiteSpace(text))
				{
					PinNames.Last = text.Replace('$', ' ').Replace('<', ' ').Replace('>', ' ')
						.Replace('\t', ' ')
						.Replace('\n', ' ')
						.Replace('\r', ' ')
						.Trim();
				}
			}
		}
	}
	public sealed class PinNameTabAutocomplete : MonoBehaviour
	{
		private GuiInputField _input;

		private void Awake()
		{
			_input = ((Component)this).GetComponent<GuiInputField>();
		}

		private void Update()
		{
			if ((Object)(object)_input == (Object)null || !((Component)_input).gameObject.activeInHierarchy || !ZInput.GetKeyDown((KeyCode)9, true))
			{
				return;
			}
			string last = PinNames.Last;
			if (!string.IsNullOrEmpty(last))
			{
				((TMP_InputField)_input).text = last;
				_input.ActivateInputField();
				((TMP_InputField)_input).caretPosition = last.Length;
				((TMP_InputField)_input).selectionAnchorPosition = last.Length;
				((TMP_InputField)_input).selectionFocusPosition = last.Length;
				if ((Object)(object)EventSystem.current != (Object)null)
				{
					EventSystem.current.SetSelectedGameObject(((Component)_input).gameObject);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Minimap), "Start")]
	internal static class MinimapStartPatch
	{
		private static readonly FieldInfo VisibleField = AccessTools.Field(typeof(Minimap), "m_visibleIconTypes");

		private static readonly FieldInfo SelectedIconsField = AccessTools.Field(typeof(Minimap), "m_selectedIcons");

		private static readonly MethodInfo IconPressedMethod = AccessTools.Method(typeof(Minimap), "IconPressed", new Type[1] { typeof(PinType) }, (Type[])null);

		private static void Postfix(Minimap __instance)
		{
			try
			{
				RegisterTypes(__instance);
				BuildPanel(__instance);
				AttachNameHelper(__instance);
			}
			catch (Exception ex)
			{
				MoreMapPinsPlugin.Log.LogError((object)("More Map Pins failed to initialise: " + ex));
			}
		}

		private static void AttachNameHelper(Minimap minimap)
		{
			if (!((Object)(object)minimap.m_nameInput == (Object)null))
			{
				GameObject gameObject = ((Component)minimap.m_nameInput).gameObject;
				if ((Object)(object)gameObject.GetComponent<PinNameTabAutocomplete>() == (Object)null)
				{
					gameObject.AddComponent<PinNameTabAutocomplete>();
				}
			}
		}

		private static void RegisterTypes(Minimap minimap)
		{
			//IL_0082: 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)
			//IL_0096: 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)
			List<SpriteData> list = new List<SpriteData>();
			Pins.PinDef[] all = Pins.All;
			foreach (Pins.PinDef def in all)
			{
				if (!minimap.m_icons.Exists((SpriteData x) => x.m_name == def.Type))
				{
					Sprite val = LoadSprite(def.Resource);
					if ((Object)(object)val == (Object)null)
					{
						MoreMapPinsPlugin.Log.LogWarning((object)("Missing embedded icon: " + def.Resource));
						continue;
					}
					list.Add(new SpriteData
					{
						m_name = def.Type,
						m_icon = val
					});
				}
			}
			minimap.m_icons.AddRange(list);
			bool[] array = (bool[])VisibleField.GetValue(minimap);
			if (array.Length < 128)
			{
				bool[] array2 = new bool[128];
				Array.Copy(array, array2, array.Length);
				for (int num = array.Length; num < 128; num++)
				{
					array2[num] = true;
				}
				VisibleField.SetValue(minimap, array2);
			}
		}

		private static Sprite LoadSprite(string name)
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MoreMapPins.icons." + name + ".png");
			if (stream == null)
			{
				return null;
			}
			byte[] array = new byte[stream.Length];
			for (int i = 0; i < array.Length; i += stream.Read(array, i, array.Length - i))
			{
			}
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false)
			{
				name = "MoreMapPins_" + name,
				wrapMode = (TextureWrapMode)1,
				filterMode = (FilterMode)1
			};
			ImageConversion.LoadImage(val, array);
			return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f, 0u, (SpriteMeshType)0);
		}

		private static void BuildPanel(Minimap minimap)
		{
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Expected O, but got Unknown
			//IL_00e5: 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_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ce: Expected O, but got Unknown
			//IL_02dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e7: Expected O, but got Unknown
			//IL_027d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)minimap.m_selectedIcon0 == (Object)null)
			{
				MoreMapPinsPlugin.Log.LogWarning((object)"Icon panel not found; custom pins are still available via saved data.");
				return;
			}
			GameObject gameObject = ((Component)((Component)minimap.m_selectedIcon0).transform.parent).gameObject;
			Transform parent = gameObject.transform.parent;
			if ((Object)(object)parent.parent.Find("MoreMapPins_IconPanel") != (Object)null)
			{
				return;
			}
			GameObject val = Object.Instantiate<GameObject>(((Component)parent).gameObject, parent.parent);
			((Object)val).name = "MoreMapPins_IconPanel";
			for (int num = val.transform.childCount - 1; num >= 0; num--)
			{
				Transform child = val.transform.GetChild(num);
				if (((Object)child).name != "Bkg")
				{
					Object.Destroy((Object)(object)((Component)child).gameObject);
				}
			}
			RectTransform val2 = (RectTransform)parent;
			RectTransform val3 = (RectTransform)val.transform;
			float num2 = val2.anchoredPosition.x - val2.sizeDelta.x * 0.5f;
			val3.sizeDelta = new Vector2(76f, val2.sizeDelta.y);
			val3.anchoredPosition = new Vector2(num2 - 4f - 38f, val2.anchoredPosition.y + 120f);
			Dictionary<PinType, Image> dictionary = (Dictionary<PinType, Image>)SelectedIconsField.GetValue(minimap);
			for (int i = 0; i < Pins.All.Length; i++)
			{
				Pins.PinDef def = Pins.All[i];
				Sprite icon = minimap.m_icons.Find((SpriteData x) => x.m_name == def.Type).m_icon;
				if ((Object)(object)icon == (Object)null)
				{
					continue;
				}
				GameObject obj = Object.Instantiate<GameObject>(gameObject, val.transform);
				((Object)obj).name = "MoreMapPins_Icon_" + def.Resource;
				((RectTransform)obj.transform).anchoredPosition = new Vector2(0f, -39.8f - (float)i * 64f);
				Image component = obj.GetComponent<Image>();
				if ((Object)(object)component != (Object)null)
				{
					component.sprite = icon;
					((Graphic)component).color = Color.white;
				}
				Transform val4 = obj.transform.Find("Selected");
				if ((Object)(object)val4 != (Object)null)
				{
					Image component2 = ((Component)val4).GetComponent<Image>();
					if ((Object)(object)component2 != (Object)null)
					{
						((Behaviour)component2).enabled = false;
						dictionary[def.Type] = component2;
					}
				}
				Button component3 = obj.GetComponent<Button>();
				if ((Object)(object)component3 != (Object)null)
				{
					PinType pinType = def.Type;
					component3.onClick = new ButtonClickedEvent();
					((UnityEvent)component3.onClick).AddListener((UnityAction)delegate
					{
						//IL_0011: Unknown result type (might be due to invalid IL or missing references)
						PressIcon(minimap, pinType);
					});
				}
			}
			MoreMapPinsPlugin.Log.LogInfo((object)("Added " + Pins.All.Length + " map pin icons."));
		}

		private unsafe static void PressIcon(Minimap minimap, PinType type)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				IconPressedMethod.Invoke(minimap, new object[1] { type });
			}
			catch (Exception ex)
			{
				MoreMapPinsPlugin.Log.LogError((object)("Could not select pin icon " + ((object)(*(PinType*)(&type))/*cast due to .constrained prefix*/).ToString() + ": " + ex));
			}
		}
	}
}