Decompiled source of Expanded Map Markers v0.1.5

plugins/ExpandedMapMarkers.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("OutwardModTemplate")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OutwardModTemplate")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c5450fe0-edcf-483f-b9ea-4b1ef9d36da7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace NamedMapMarkers;

internal static class ExtendedIcons
{
	private sealed class CustomMarkerIconDef
	{
		public readonly string Key;

		public readonly string FileName;

		public readonly int TargetWidth;

		public readonly int TargetHeight;

		public CustomMarkerIconDef(string key, string fileName, int targetWidth, int targetHeight)
		{
			Key = key;
			FileName = fileName;
			TargetWidth = targetWidth;
			TargetHeight = targetHeight;
		}
	}

	private static readonly HashSet<MapDisplay> ExtendedMaps = new HashSet<MapDisplay>();

	private const float WheelIconScale = 0.7f;

	private const int CustomSpriteCanvasSize = 128;

	private static Sprite[] cachedCustomSprites;

	private static readonly CustomMarkerIconDef[] CustomMarkerIcons = new CustomMarkerIconDef[5]
	{
		new CustomMarkerIconDef("GoldenStar", "weathered_golden_star_emblem.png", 62, 62),
		new CustomMarkerIconDef("TealDroplet", "teal_teardrop_emblem.png", 48, 70),
		new CustomMarkerIconDef("LimeChevron", "neon_lime_weathered_chevron_badge.png", 60, 58),
		new CustomMarkerIconDef("RedCross", "weathered_red_cross_emblem.png", 60, 60),
		new CustomMarkerIconDef("IndigoRing", "weathered_indigo_ring_emblem.png", 62, 62)
	};

	internal static void ExtendMarkerWheel(MapDisplay map)
	{
		if ((Object)(object)map == (Object)null || ExtendedMaps.Contains(map))
		{
			return;
		}
		try
		{
			RadialSelector value = Traverse.Create((object)map).Field("m_markerSelector").GetValue<RadialSelector>();
			if ((Object)(object)value == (Object)null)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogError((object)"Extended Icons: could not find MapDisplay.m_markerSelector.");
				}
				return;
			}
			RadialSelectorItem[] componentsInChildren = ((Component)value).GetComponentsInChildren<RadialSelectorItem>(true);
			if (componentsInChildren == null || componentsInChildren.Length == 0)
			{
				ManualLogSource log2 = Plugin.Log;
				if (log2 != null)
				{
					log2.LogError((object)"Extended Icons: marker selector contains no RadialSelectorItems.");
				}
				return;
			}
			Sprite[] addedMarkerSprites = map.AddedMarkerSprites;
			if (addedMarkerSprites == null || addedMarkerSprites.Length == 0)
			{
				ManualLogSource log3 = Plugin.Log;
				if (log3 != null)
				{
					log3.LogError((object)"Extended Icons: MapDisplay.AddedMarkerSprites was null or empty.");
				}
				return;
			}
			Sprite[] array = EnsureCustomSpritesLoaded(addedMarkerSprites[0]);
			int num = addedMarkerSprites.Length;
			Sprite[] array2 = (Sprite[])(object)new Sprite[addedMarkerSprites.Length + array.Length];
			Array.Copy(addedMarkerSprites, array2, addedMarkerSprites.Length);
			Array.Copy(array, 0, array2, addedMarkerSprites.Length, array.Length);
			map.AddedMarkerSprites = array2;
			RadialSelectorItem val = componentsInChildren[0];
			for (int i = 0; i < array.Length; i++)
			{
				GameObject val2 = Object.Instantiate<GameObject>(((Component)val).gameObject, ((Component)val).transform.parent);
				((Object)val2).name = "NamedMapMarkers_" + CustomMarkerIcons[i].Key;
				val2.SetActive(true);
				RadialSelectorItem component = val2.GetComponent<RadialSelectorItem>();
				Image val3 = null;
				val3 = ((!((Object)(object)component != (Object)null) || !((Object)(object)component.Image != (Object)null)) ? val2.GetComponentInChildren<Image>(true) : component.Image);
				if ((Object)(object)val3 != (Object)null)
				{
					val3.sprite = array[i];
					val3.preserveAspect = true;
					continue;
				}
				ManualLogSource log4 = Plugin.Log;
				if (log4 != null)
				{
					log4.LogError((object)("Extended Icons: could not find Image on cloned wheel item " + ((Object)val2).name + "."));
				}
			}
			value.Refresh();
			ResizeAllWheelIcons(value);
			ExtendedMaps.Add(map);
		}
		catch (Exception ex)
		{
			ManualLogSource log5 = Plugin.Log;
			if (log5 != null)
			{
				log5.LogError((object)"Extended Icons: failed to extend marker wheel.");
			}
			ManualLogSource log6 = Plugin.Log;
			if (log6 != null)
			{
				log6.LogError((object)ex);
			}
		}
	}

	private static void ResizeAllWheelIcons(RadialSelector selector)
	{
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)selector == (Object)null)
		{
			return;
		}
		RadialSelectorItem[] componentsInChildren = ((Component)selector).GetComponentsInChildren<RadialSelectorItem>(true);
		if (componentsInChildren == null)
		{
			return;
		}
		foreach (RadialSelectorItem val in componentsInChildren)
		{
			if (!((Object)(object)val == (Object)null))
			{
				Image val2 = val.Image;
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = ((Component)val).GetComponentInChildren<Image>(true);
				}
				if (!((Object)(object)val2 == (Object)null))
				{
					val2.preserveAspect = true;
					((Transform)((Graphic)val2).rectTransform).localScale = new Vector3(0.7f, 0.7f, 1f);
				}
			}
		}
	}

	private static Sprite[] EnsureCustomSpritesLoaded(Sprite fallbackSprite)
	{
		if (cachedCustomSprites != null && cachedCustomSprites.Length == CustomMarkerIcons.Length)
		{
			return cachedCustomSprites;
		}
		cachedCustomSprites = (Sprite[])(object)new Sprite[CustomMarkerIcons.Length];
		string text = null;
		if ((Object)(object)Plugin.Instance != (Object)null && ((BaseUnityPlugin)Plugin.Instance).Info != null)
		{
			text = Path.GetDirectoryName(((BaseUnityPlugin)Plugin.Instance).Info.Location);
		}
		string text2 = (string.IsNullOrEmpty(text) ? "" : Path.Combine(text, "Icons"));
		if (string.IsNullOrEmpty(text2) || !Directory.Exists(text2))
		{
			ManualLogSource log = Plugin.Log;
			if (log != null)
			{
				log.LogError((object)("Extended Icons: Icons folder not found: " + text2));
			}
			for (int i = 0; i < CustomMarkerIcons.Length; i++)
			{
				cachedCustomSprites[i] = fallbackSprite;
			}
			return cachedCustomSprites;
		}
		for (int j = 0; j < CustomMarkerIcons.Length; j++)
		{
			CustomMarkerIconDef customMarkerIconDef = CustomMarkerIcons[j];
			string filePath = Path.Combine(text2, customMarkerIconDef.FileName);
			Sprite val = LoadSpriteFromPng(filePath, customMarkerIconDef.TargetWidth, customMarkerIconDef.TargetHeight);
			if ((Object)(object)val != (Object)null)
			{
				((Object)val).name = "NamedMapMarkers_" + customMarkerIconDef.Key;
				cachedCustomSprites[j] = val;
				continue;
			}
			cachedCustomSprites[j] = fallbackSprite;
			ManualLogSource log2 = Plugin.Log;
			if (log2 != null)
			{
				log2.LogError((object)("Extended Icons: could not load '" + customMarkerIconDef.FileName + "'. " + $"Custom slot {j + 5} is using the vanilla fallback sprite."));
			}
		}
		return cachedCustomSprites;
	}

	private static Sprite LoadSpriteFromPng(string filePath, int targetWidth, int targetHeight)
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		//IL_00de: 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)
		try
		{
			if (!File.Exists(filePath))
			{
				return null;
			}
			byte[] array = File.ReadAllBytes(filePath);
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
			if (!ImageConversion.LoadImage(val, array))
			{
				Object.Destroy((Object)(object)val);
				return null;
			}
			((Object)val).name = Path.GetFileNameWithoutExtension(filePath);
			((Texture)val).filterMode = (FilterMode)1;
			((Texture)val).wrapMode = (TextureWrapMode)1;
			if (!TextureHasTransparency(val))
			{
				MakeBlackBackgroundTransparent(val);
			}
			Texture2D val2 = CreateVanillaSizedMarkerTexture(val, targetWidth, targetHeight);
			Object.Destroy((Object)(object)val);
			if ((Object)(object)val2 == (Object)null)
			{
				return null;
			}
			((Object)val2).name = Path.GetFileNameWithoutExtension(filePath) + "_128";
			((Texture)val2).filterMode = (FilterMode)1;
			((Texture)val2).wrapMode = (TextureWrapMode)1;
			return Sprite.Create(val2, new Rect(0f, 0f, 128f, 128f), new Vector2(0.5f, 0.5f), 100f);
		}
		catch (Exception ex)
		{
			ManualLogSource log = Plugin.Log;
			if (log != null)
			{
				log.LogError((object)("Extended Icons: error loading marker PNG '" + filePath + "'."));
			}
			ManualLogSource log2 = Plugin.Log;
			if (log2 != null)
			{
				log2.LogError((object)ex);
			}
			return null;
		}
	}

	private static bool TextureHasTransparency(Texture2D texture)
	{
		if ((Object)(object)texture == (Object)null)
		{
			return false;
		}
		Color32[] pixels = texture.GetPixels32();
		for (int i = 0; i < pixels.Length; i++)
		{
			if (pixels[i].a < 250)
			{
				return true;
			}
		}
		return false;
	}

	private static void MakeBlackBackgroundTransparent(Texture2D texture)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: 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_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)texture == (Object)null)
		{
			return;
		}
		Color32[] pixels = texture.GetPixels32();
		for (int i = 0; i < pixels.Length; i++)
		{
			Color32 val = pixels[i];
			if (val.r <= 2 && val.g <= 2 && val.b <= 2)
			{
				val.a = 0;
				pixels[i] = val;
			}
		}
		texture.SetPixels32(pixels);
		texture.Apply(false, false);
	}

	private static Texture2D CreateVanillaSizedMarkerTexture(Texture2D source, int targetWidth, int targetHeight)
	{
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Expected O, but got Unknown
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		//IL_024b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0250: Unknown result type (might be due to invalid IL or missing references)
		//IL_025e: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)source == (Object)null)
		{
			return null;
		}
		targetWidth = Mathf.Clamp(targetWidth, 1, 128);
		targetHeight = Mathf.Clamp(targetHeight, 1, 128);
		Color32[] pixels = source.GetPixels32();
		int num = ((Texture)source).width;
		int num2 = ((Texture)source).height;
		int num3 = -1;
		int num4 = -1;
		for (int i = 0; i < ((Texture)source).height; i++)
		{
			int num5 = i * ((Texture)source).width;
			for (int j = 0; j < ((Texture)source).width; j++)
			{
				if (pixels[num5 + j].a > 8)
				{
					if (j < num)
					{
						num = j;
					}
					if (j > num3)
					{
						num3 = j;
					}
					if (i < num2)
					{
						num2 = i;
					}
					if (i > num4)
					{
						num4 = i;
					}
				}
			}
		}
		if (num3 < num || num4 < num2)
		{
			ManualLogSource log = Plugin.Log;
			if (log != null)
			{
				log.LogError((object)"Extended Icons: marker texture contained no visible pixels.");
			}
			return null;
		}
		int num6 = num3 - num + 1;
		int num7 = num4 - num2 + 1;
		Texture2D val = new Texture2D(128, 128, (TextureFormat)4, false);
		Color[] array = (Color[])(object)new Color[16384];
		for (int k = 0; k < array.Length; k++)
		{
			array[k] = Color.clear;
		}
		val.SetPixels(array);
		int num8 = (128 - targetWidth) / 2;
		int num9 = (128 - targetHeight) / 2;
		for (int l = 0; l < targetHeight; l++)
		{
			float num10 = ((targetHeight <= 1) ? 0.5f : (((float)l + 0.5f) / (float)targetHeight));
			float num11 = (float)num2 + num10 * (float)num7 - 0.5f;
			float num12 = ((((Texture)source).height <= 1) ? 0f : (num11 / ((float)((Texture)source).height - 1f)));
			for (int m = 0; m < targetWidth; m++)
			{
				float num13 = ((targetWidth <= 1) ? 0.5f : (((float)m + 0.5f) / (float)targetWidth));
				float num14 = (float)num + num13 * (float)num6 - 0.5f;
				float num15 = ((((Texture)source).width <= 1) ? 0f : (num14 / ((float)((Texture)source).width - 1f)));
				Color pixelBilinear = source.GetPixelBilinear(Mathf.Clamp01(num15), Mathf.Clamp01(num12));
				val.SetPixel(num8 + m, num9 + l, pixelBilinear);
			}
		}
		val.Apply(false, false);
		return val;
	}
}
[HarmonyPatch(typeof(MapDisplay), "Show", new Type[] { typeof(CharacterUI) })]
internal static class ExtendedIcons_MapDisplay_Show_Patch
{
	private static void Prefix(MapDisplay __instance)
	{
		ExtendedIcons.ExtendMarkerWheel(__instance);
	}
}
internal sealed class NamedIcons : MonoBehaviour
{
	internal static readonly Queue<string> PendingLoadedNames = new Queue<string>();

	internal static readonly Dictionary<MapMarkerSimpleDisplay, string> MarkerNames = new Dictionary<MapMarkerSimpleDisplay, string>();

	internal const string LabelObjectName = "NamedMapMarkers_Label";

	private MapMarkerSimpleDisplay markerBeingNamed;

	private MapDisplay mapBeingEdited;

	private string markerName = "";

	private bool showNameWindow;

	private bool focusNameField;

	private Rect windowRect = new Rect(0f, 0f, 230f, 45f);

	internal static NamedIcons Instance { get; private set; }

	internal static void Attach(GameObject host)
	{
		if (!((Object)(object)host == (Object)null) && !((Object)(object)Instance != (Object)null))
		{
			NamedIcons component = host.GetComponent<NamedIcons>();
			if ((Object)(object)component != (Object)null)
			{
				Instance = component;
			}
			else
			{
				Instance = host.AddComponent<NamedIcons>();
			}
		}
	}

	private void Awake()
	{
		Instance = this;
	}

	private void OnDestroy()
	{
		if ((Object)(object)Instance == (Object)(object)this)
		{
			Instance = null;
		}
	}

	internal void AskForName(MapDisplay map, MapMarkerSimpleDisplay marker)
	{
		if (!((Object)(object)map == (Object)null) && !((Object)(object)marker == (Object)null))
		{
			mapBeingEdited = map;
			markerBeingNamed = marker;
			markerName = "";
			showNameWindow = true;
			focusNameField = true;
			UpdateInputPosition();
		}
	}

	private void UpdateInputPosition()
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Invalid comparison between Unknown and I4
		//IL_006f: 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)
		//IL_0079: 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)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)markerBeingNamed == (Object)null)
		{
			return;
		}
		Transform transform = ((Component)markerBeingNamed).transform;
		RectTransform val = (RectTransform)(object)((transform is RectTransform) ? transform : null);
		if (!((Object)(object)val == (Object)null))
		{
			Canvas componentInParent = ((Component)markerBeingNamed).GetComponentInParent<Canvas>();
			Camera val2 = null;
			if ((Object)(object)componentInParent != (Object)null && (int)componentInParent.renderMode > 0)
			{
				val2 = componentInParent.worldCamera;
			}
			Vector2 val3 = RectTransformUtility.WorldToScreenPoint(val2, ((Transform)val).position);
			float num = (float)Screen.height - val3.y;
			((Rect)(ref windowRect)).width = 230f;
			((Rect)(ref windowRect)).height = 45f;
			((Rect)(ref windowRect)).x = val3.x + 28f;
			((Rect)(ref windowRect)).y = num - ((Rect)(ref windowRect)).height / 2f;
			((Rect)(ref windowRect)).x = Mathf.Clamp(((Rect)(ref windowRect)).x, 5f, (float)Screen.width - ((Rect)(ref windowRect)).width - 5f);
			((Rect)(ref windowRect)).y = Mathf.Clamp(((Rect)(ref windowRect)).y, 5f, (float)Screen.height - ((Rect)(ref windowRect)).height - 5f);
		}
	}

	private void OnGUI()
	{
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected O, but got Unknown
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		if (showNameWindow)
		{
			if ((Object)(object)markerBeingNamed == (Object)null || (Object)(object)mapBeingEdited == (Object)null)
			{
				CloseInputOnly();
				return;
			}
			UpdateInputPosition();
			windowRect = GUI.Window(918273, windowRect, new WindowFunction(DrawNameWindow), "");
		}
	}

	private void DrawNameWindow(int windowID)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Invalid comparison between Unknown and I4
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Invalid comparison between Unknown and I4
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Invalid comparison between Unknown and I4
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Invalid comparison between Unknown and I4
		Event current = Event.current;
		if ((int)current.type == 4)
		{
			if ((int)current.keyCode == 13 || (int)current.keyCode == 271)
			{
				CommitName();
				current.Use();
				return;
			}
			if ((int)current.keyCode == 27)
			{
				CancelMarkerCreation();
				current.Use();
				return;
			}
		}
		GUI.SetNextControlName("NamedMapMarkers_NameField");
		markerName = GUI.TextField(new Rect(5f, 8f, 220f, 25f), markerName ?? "");
		if (focusNameField)
		{
			GUI.FocusControl("NamedMapMarkers_NameField");
			focusNameField = false;
		}
	}

	private void CommitName()
	{
		if ((Object)(object)markerBeingNamed != (Object)null)
		{
			string name = (markerName ?? "").Trim();
			SetMarkerName(markerBeingNamed, name);
		}
		CloseInputOnly();
	}

	private void CancelMarkerCreation()
	{
		MapMarkerSimpleDisplay val = markerBeingNamed;
		MapDisplay val2 = mapBeingEdited;
		CloseInputOnly();
		if (!((Object)(object)val == (Object)null))
		{
			MarkerNames.Remove(val);
			if ((Object)(object)val2 != (Object)null)
			{
				val2.RemoveAddedMarker(val);
			}
		}
	}

	private void CloseInputOnly()
	{
		markerBeingNamed = null;
		mapBeingEdited = null;
		markerName = "";
		showNameWindow = false;
		focusNameField = false;
	}

	internal void CancelPendingMarker(MapDisplay map)
	{
		if (showNameWindow && !((Object)(object)mapBeingEdited != (Object)(object)map))
		{
			CancelMarkerCreation();
		}
	}

	internal static void SetMarkerName(MapMarkerSimpleDisplay marker, string name)
	{
		if (!((Object)(object)marker == (Object)null))
		{
			MarkerNames[marker] = name ?? "";
			EnsureLabel(marker);
			UpdateLabel(marker);
		}
	}

	internal static string GetMarkerName(MapMarkerSimpleDisplay marker)
	{
		if ((Object)(object)marker == (Object)null)
		{
			return "";
		}
		if (MarkerNames.TryGetValue(marker, out var value))
		{
			return value ?? "";
		}
		return "";
	}

	internal static Text EnsureLabel(MapMarkerSimpleDisplay marker)
	{
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Expected O, but got Unknown
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Expected O, but got Unknown
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0273: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)marker == (Object)null)
		{
			return null;
		}
		NamedMarkerHover namedMarkerHover = ((Component)marker).GetComponent<NamedMarkerHover>();
		if ((Object)(object)namedMarkerHover == (Object)null)
		{
			namedMarkerHover = ((Component)marker).gameObject.AddComponent<NamedMarkerHover>();
		}
		namedMarkerHover.Marker = marker;
		Transform val = ((Component)marker).transform.Find("NamedMapMarkers_Label");
		if ((Object)(object)val != (Object)null)
		{
			return ((Component)val).GetComponentInChildren<Text>();
		}
		GameObject val2 = new GameObject("NamedMapMarkers_Label", new Type[3]
		{
			typeof(RectTransform),
			typeof(CanvasRenderer),
			typeof(Image)
		});
		val2.transform.SetParent(((Component)marker).transform, false);
		RectTransform component = val2.GetComponent<RectTransform>();
		component.anchorMin = new Vector2(0.5f, 0.5f);
		component.anchorMax = new Vector2(0.5f, 0.5f);
		component.pivot = new Vector2(0f, 0.5f);
		component.anchoredPosition = new Vector2(25f, 0f);
		component.sizeDelta = new Vector2(100f, 30f);
		Image component2 = val2.GetComponent<Image>();
		((Graphic)component2).color = new Color(0f, 0f, 0f, 0.8f);
		((Graphic)component2).raycastTarget = false;
		GameObject val3 = new GameObject("Text", new Type[3]
		{
			typeof(RectTransform),
			typeof(CanvasRenderer),
			typeof(Text)
		});
		val3.transform.SetParent(val2.transform, false);
		RectTransform component3 = val3.GetComponent<RectTransform>();
		component3.anchorMin = Vector2.zero;
		component3.anchorMax = Vector2.one;
		component3.offsetMin = new Vector2(5f, 1f);
		component3.offsetMax = new Vector2(-5f, -1f);
		Text component4 = val3.GetComponent<Text>();
		Text[] array = Resources.FindObjectsOfTypeAll<Text>();
		Text[] array2 = array;
		foreach (Text val4 in array2)
		{
			if ((Object)(object)val4 != (Object)null && (Object)(object)val4.font != (Object)null)
			{
				component4.font = val4.font;
				break;
			}
		}
		component4.fontSize = 18;
		component4.alignment = (TextAnchor)3;
		((Graphic)component4).color = Color.white;
		component4.horizontalOverflow = (HorizontalWrapMode)1;
		component4.verticalOverflow = (VerticalWrapMode)1;
		((Graphic)component4).raycastTarget = false;
		val2.SetActive(false);
		return component4;
	}

	internal static void UpdateLabel(MapMarkerSimpleDisplay marker)
	{
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)marker == (Object)null)
		{
			return;
		}
		Text val = EnsureLabel(marker);
		if (!((Object)(object)val == (Object)null))
		{
			string value = (val.text = GetMarkerName(marker));
			GameObject gameObject = ((Component)((Component)val).transform.parent).gameObject;
			if (string.IsNullOrEmpty(value))
			{
				gameObject.SetActive(false);
				return;
			}
			RectTransform component = gameObject.GetComponent<RectTransform>();
			float num = Mathf.Clamp(val.preferredWidth + 12f, 40f, 260f);
			component.sizeDelta = new Vector2(num, 30f);
			gameObject.SetActive(false);
		}
	}

	internal static string EncodeName(string value)
	{
		if (string.IsNullOrEmpty(value))
		{
			return "";
		}
		return Convert.ToBase64String(Encoding.UTF8.GetBytes(value));
	}

	internal static string DecodeName(string value)
	{
		if (string.IsNullOrEmpty(value))
		{
			return "";
		}
		try
		{
			return Encoding.UTF8.GetString(Convert.FromBase64String(value));
		}
		catch (Exception ex)
		{
			ManualLogSource log = Plugin.Log;
			if (log != null)
			{
				log.LogError((object)"Named Icons: could not decode a saved marker name.");
			}
			ManualLogSource log2 = Plugin.Log;
			if (log2 != null)
			{
				log2.LogError((object)ex);
			}
			return "";
		}
	}
}
internal sealed class NamedMarkerHover : MonoBehaviour, IPointerEnterHandler, IEventSystemHandler, IPointerExitHandler
{
	public MapMarkerSimpleDisplay Marker;

	public void OnPointerEnter(PointerEventData eventData)
	{
		SetVisible(visible: true);
	}

	public void OnPointerExit(PointerEventData eventData)
	{
		SetVisible(visible: false);
	}

	private void SetVisible(bool visible)
	{
		if (!((Object)(object)Marker == (Object)null))
		{
			Transform val = ((Component)Marker).transform.Find("NamedMapMarkers_Label");
			if (!((Object)(object)val == (Object)null))
			{
				string markerName = NamedIcons.GetMarkerName(Marker);
				((Component)val).gameObject.SetActive(visible && !string.IsNullOrEmpty(markerName));
			}
		}
	}
}
[HarmonyPatch(typeof(MapDisplay), "AddMarker")]
internal static class NamedIcons_MapDisplay_AddMarker_Patch
{
	private static void Postfix(MapDisplay __instance, bool _fromLoad, MapMarkerSimpleDisplay __result)
	{
		if ((Object)(object)__result == (Object)null)
		{
			return;
		}
		NamedIcons.MarkerNames.Remove(__result);
		if (_fromLoad)
		{
			string name = "";
			if (NamedIcons.PendingLoadedNames.Count > 0)
			{
				name = NamedIcons.PendingLoadedNames.Dequeue();
			}
			NamedIcons.SetMarkerName(__result, name);
		}
		else
		{
			NamedIcons.SetMarkerName(__result, "");
			NamedIcons.Instance?.AskForName(__instance, __result);
		}
	}
}
[HarmonyPatch(typeof(MapMarkerSimpleDisplay), "ToSaveData")]
internal static class NamedIcons_MapMarkerSimpleDisplay_ToSaveData_Patch
{
	private static void Postfix(MapMarkerSimpleDisplay __instance, ref string __result)
	{
		string markerName = NamedIcons.GetMarkerName(__instance);
		string text = NamedIcons.EncodeName(markerName);
		__result = __result + ";N:" + text;
	}
}
[HarmonyPatch(typeof(MapDisplay), "Load")]
internal static class NamedIcons_MapDisplay_Load_Patch
{
	private static void Prefix(ref string _data)
	{
		NamedIcons.PendingLoadedNames.Clear();
		if (string.IsNullOrEmpty(_data))
		{
			return;
		}
		string[] array = _data.Split(new char[1] { '|' }, StringSplitOptions.RemoveEmptyEntries);
		StringBuilder stringBuilder = new StringBuilder();
		string[] array2 = array;
		foreach (string text in array2)
		{
			string[] array3 = text.Split(new char[1] { ';' });
			if (array3.Length >= 2)
			{
				string item = "";
				if (array3.Length >= 3 && array3[2].StartsWith("N:"))
				{
					item = NamedIcons.DecodeName(array3[2].Substring(2));
				}
				NamedIcons.PendingLoadedNames.Enqueue(item);
				stringBuilder.Append(array3[0]);
				stringBuilder.Append(';');
				stringBuilder.Append(array3[1]);
				stringBuilder.Append('|');
			}
		}
		_data = stringBuilder.ToString();
	}
}
[HarmonyPatch(typeof(MapDisplay), "OnHide")]
internal static class NamedIcons_MapDisplay_OnHide_Patch
{
	private static void Prefix(MapDisplay __instance)
	{
		NamedIcons.Instance?.CancelPendingMarker(__instance);
	}
}
[BepInPlugin("outward.namedmapmarkers", "Named Map Markers", "0.6.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "outward.namedmapmarkers";

	public const string NAME = "Named Map Markers";

	public const string VERSION = "0.6.0";

	internal static Plugin Instance;

	internal static ManualLogSource Log;

	internal void Awake()
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		Log.LogMessage((object)"Named Map Markers 0.6.0 loading...");
		try
		{
			NamedIcons.Attach(((Component)this).gameObject);
			new Harmony("outward.namedmapmarkers").PatchAll();
			Log.LogMessage((object)"Named Map Markers 0.6.0 loaded!");
		}
		catch (Exception ex)
		{
			Log.LogError((object)"Named Map Markers failed to load:");
			Log.LogError((object)ex);
			throw;
		}
	}
}