Decompiled source of MapNametags v0.1.0

BepInEx/plugins/Nametags.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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 FlowStudio.Map;
using HarmonyLib;
using MapNametags.Behaviours;
using MapNametags.Patches;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
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("Nametags")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+45b3c0091e7123eb4183950fbd655a211ec53a89")]
[assembly: AssemblyProduct("Map Nametags")]
[assembly: AssemblyTitle("Nametags")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/csh/lens-island-map-nametags.git")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 MapNametags
{
	public static class NametagManager
	{
		private static readonly Dictionary<string, AbsoluteMapLabelBehaviour> Labels = new Dictionary<string, AbsoluteMapLabelBehaviour>();

		private static RectTransform CanvasRT
		{
			get
			{
				MapUIManager instance = MapUIManager.Instance;
				if (instance == null)
				{
					return null;
				}
				Canvas componentInParent = ((Component)instance).GetComponentInParent<Canvas>();
				if (componentInParent == null)
				{
					return null;
				}
				return ((Component)componentInParent).GetComponent<RectTransform>();
			}
		}

		public static void CreateLocalPlayerNametag()
		{
			CreateNametag("LocalPlayerNametag", MapUIManager.Instance.m_playerReference, () => "You", () => Color.red);
		}

		public static void CreateNametag(string key, RectTransform markerRT, Func<string> getText, Func<Color> getColor)
		{
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			if (string.IsNullOrEmpty(key))
			{
				throw new ArgumentNullException("key");
			}
			if ((Object)(object)markerRT == (Object)null)
			{
				throw new ArgumentNullException("markerRT");
			}
			if (!Labels.TryGetValue(key, out var value) || !((Object)(object)value != (Object)null))
			{
				RectTransform canvasRT = CanvasRT;
				if ((Object)(object)canvasRT == (Object)null)
				{
					throw new InvalidOperationException("CanvasRT is null!");
				}
				GameObject val = new GameObject("Nametag_" + key, new Type[2]
				{
					typeof(RectTransform),
					typeof(AbsoluteMapLabelBehaviour)
				});
				val.transform.SetParent((Transform)(object)canvasRT, false);
				value = val.GetComponent<AbsoluteMapLabelBehaviour>();
				value.MarkerKey = key;
				value.Initialize(canvasRT, markerRT, 16f, getText, getColor);
				Labels[key] = value;
			}
		}

		public static void RemoveLabel(string key)
		{
			if (Labels.TryGetValue(key, out var value) && (Object)(object)value != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)value).gameObject);
				Labels.Remove(key);
				return;
			}
			RectTransform canvasRT = CanvasRT;
			if (canvasRT == null)
			{
				return;
			}
			AbsoluteMapLabelBehaviour[] componentsInChildren = ((Component)canvasRT).GetComponentsInChildren<AbsoluteMapLabelBehaviour>(true);
			foreach (AbsoluteMapLabelBehaviour absoluteMapLabelBehaviour in componentsInChildren)
			{
				if (!(absoluteMapLabelBehaviour.MarkerKey != key))
				{
					Object.Destroy((Object)(object)((Component)absoluteMapLabelBehaviour).gameObject);
					break;
				}
			}
		}

		public static void SetAllLabelsVisible(bool visible)
		{
			RectTransform canvasRT = CanvasRT;
			if (canvasRT != null)
			{
				AbsoluteMapLabelBehaviour[] componentsInChildren = ((Component)canvasRT).GetComponentsInChildren<AbsoluteMapLabelBehaviour>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					((Component)componentsInChildren[i]).gameObject.SetActive(visible);
				}
			}
		}

		public static void RemoveAllLabels()
		{
			foreach (AbsoluteMapLabelBehaviour value in Labels.Values)
			{
				if ((Object)(object)value != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)value).gameObject);
				}
			}
			Labels.Clear();
			AbsoluteMapLabelBehaviour[] array = Object.FindObjectsOfType<AbsoluteMapLabelBehaviour>();
			for (int i = 0; i < array.Length; i++)
			{
				Object.Destroy((Object)(object)((Component)array[i]).gameObject);
			}
		}
	}
	[BepInPlugin("com.smrkn.island-named-map-markers", "Map Nametags", "0.1.0")]
	public class MapNametagsPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <CheckMapState>d__13 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public MapNametagsPlugin <>4__this;

			private WaitForSeconds <wait>5__2;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <CheckMapState>d__13(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<wait>5__2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0024: Unknown result type (might be due to invalid IL or missing references)
				//IL_002e: Expected O, but got Unknown
				int num = <>1__state;
				MapNametagsPlugin mapNametagsPlugin = <>4__this;
				switch (num)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<wait>5__2 = new WaitForSeconds(0.1f);
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if (((Behaviour)mapNametagsPlugin).enabled)
				{
					if (mapNametagsPlugin._inGameScene && MapUIManager.Instance != null)
					{
						bool isOpen = MapUIManager.IsOpen;
						if (isOpen != mapNametagsPlugin._wasMapOpen)
						{
							NametagManager.SetAllLabelsVisible(isOpen);
							mapNametagsPlugin._wasMapOpen = isOpen;
						}
					}
					<>2__current = <wait>5__2;
					<>1__state = 1;
					return true;
				}
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		internal static ConfigEntry<bool> DisplayLocalPlayerNametag;

		internal static ManualLogSource Logger;

		private static readonly HashSet<string> NonGameScenes = new HashSet<string> { "Boot", "Intro", "MainMenu", "LoadingScreen" };

		private Coroutine _mapCheckRoutine;

		private bool _inGameScene;

		private bool _wasMapOpen;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			//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)
			Logger = ((BaseUnityPlugin)this).Logger;
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			Logger.LogInfo((object)"Plugin Map Nametags is loaded!");
			DisplayLocalPlayerNametag = ((BaseUnityPlugin)this).Config.Bind<bool>("Nametags", "Display Local Player Nametag", true, (ConfigDescription)null);
			_harmony = new Harmony("com.smrkn.island-named-map-markers");
			_harmony.PatchAll(typeof(NamedPlayerMarkerPatches));
			foreach (MethodBase patchedMethod in _harmony.GetPatchedMethods())
			{
				Logger.LogDebug((object)("Patched " + patchedMethod.DeclaringType?.Name + "." + patchedMethod.Name));
			}
			HashSet<string> nonGameScenes = NonGameScenes;
			Scene activeScene = SceneManager.GetActiveScene();
			_inGameScene = !nonGameScenes.Contains(((Scene)(ref activeScene)).name);
		}

		private void Start()
		{
			_mapCheckRoutine = ((MonoBehaviour)this).StartCoroutine(CheckMapState());
		}

		private void OnEnable()
		{
			SceneManager.sceneLoaded += OnSceneLoaded;
			if (DisplayLocalPlayerNametag != null)
			{
				DisplayLocalPlayerNametag.SettingChanged += ToggleLocalPlayerNametagUsage;
			}
		}

		private void OnDisable()
		{
			SceneManager.sceneLoaded -= OnSceneLoaded;
			if (DisplayLocalPlayerNametag != null)
			{
				DisplayLocalPlayerNametag.SettingChanged -= ToggleLocalPlayerNametagUsage;
			}
		}

		private void ToggleLocalPlayerNametagUsage(object sender, EventArgs e)
		{
			if (_inGameScene)
			{
				if (DisplayLocalPlayerNametag.Value)
				{
					NametagManager.CreateLocalPlayerNametag();
				}
				else
				{
					NametagManager.RemoveLabel("LocalPlayerNametag");
				}
			}
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			if (_inGameScene && ((Scene)(ref scene)).name == "MainMenu")
			{
				NametagManager.RemoveAllLabels();
			}
			_inGameScene = !NonGameScenes.Contains(((Scene)(ref scene)).name);
		}

		[IteratorStateMachine(typeof(<CheckMapState>d__13))]
		private IEnumerator CheckMapState()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <CheckMapState>d__13(0)
			{
				<>4__this = this
			};
		}

		private void OnDestroy()
		{
			if (_mapCheckRoutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(_mapCheckRoutine);
			}
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			NametagManager.RemoveAllLabels();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.smrkn.island-named-map-markers";

		public const string PLUGIN_NAME = "Map Nametags";

		public const string PLUGIN_VERSION = "0.1.0";
	}
}
namespace MapNametags.Patches
{
	public static class NamedPlayerMarkerPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(MapManager), "RemoveMarkerInstance")]
		public static void RemoveMarkerInstancePostfix(string key)
		{
			if (key.StartsWith("Client_"))
			{
				NametagManager.RemoveLabel(key);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MapUIManager), "Update")]
		public static void UpdateClampMarkerTiltPostfix()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			RectTransform playerReference = MapUIManager.Instance.m_playerReference;
			if (!((Object)(object)playerReference == (Object)null))
			{
				Vector3 localEulerAngles = ((Transform)playerReference).localEulerAngles;
				((Transform)playerReference).localEulerAngles = new Vector3(0f, 0f, localEulerAngles.z);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MapUIManager), "Init")]
		public static void InitPostfix()
		{
			if (MapNametagsPlugin.DisplayLocalPlayerNametag.Value)
			{
				NametagManager.CreateLocalPlayerNametag();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MapManager), "SpawnMapMarker")]
		public static void SpawnMapMarkerPostfix(string key, byte type)
		{
			if (type != 45)
			{
				return;
			}
			Marker marker = MapManager.Instance.GetMarker(key);
			if (!((Object)(object)marker == (Object)null))
			{
				RectTransform component = ((Component)marker).GetComponent<RectTransform>();
				if (!((Object)(object)component == (Object)null))
				{
					NametagManager.CreateNametag(key, component, GetPlayerName, GetColor);
				}
			}
			Color GetColor()
			{
				//IL_003c: Unknown result type (might be due to invalid IL or missing references)
				string text;
				if (!key.StartsWith("Client_"))
				{
					text = key;
				}
				else
				{
					string text2 = key;
					text = text2.Substring(7, text2.Length - 7);
				}
				string text3 = text;
				return NetworkBootstrapper.instance.networkPlayers.GetPlayerColor(text3);
			}
			string GetPlayerName()
			{
				//IL_0047: Unknown result type (might be due to invalid IL or missing references)
				string text4;
				if (!key.StartsWith("Client_"))
				{
					text4 = key;
				}
				else
				{
					string text5 = key;
					text4 = text5.Substring(7, text5.Length - 7);
				}
				string text6 = text4;
				NetworkPlayerData val = default(NetworkPlayerData);
				if (!NetworkBootstrapper.instance.networkPlayers.TryGetPlayerData(text6, ref val))
				{
					return text6;
				}
				return val.username;
			}
		}
	}
}
namespace MapNametags.Behaviours
{
	[DisallowMultipleComponent]
	public class AbsoluteMapLabelBehaviour : MonoBehaviour
	{
		private RectTransform _labelRt;

		private RectTransform _canvasRt;

		private Canvas _canvas;

		private RectTransform _markerRt;

		private float _pixelOffset;

		private TextMeshProUGUI _tmp;

		public string MarkerKey { get; set; }

		public void Initialize(RectTransform canvasRt, RectTransform markerRt, float pixelOffset, Func<string> getText, Func<Color> getColor)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			_canvasRt = canvasRt;
			_canvas = ((Component)canvasRt).GetComponent<Canvas>();
			_markerRt = markerRt;
			_pixelOffset = pixelOffset;
			if ((Object)(object)_tmp != (Object)null && getText != null)
			{
				((TMP_Text)_tmp).text = getText();
			}
			if ((Object)(object)_tmp != (Object)null && getColor != null)
			{
				((Graphic)_tmp).color = getColor();
			}
			((Component)this).gameObject.SetActive(MapUIManager.IsOpen);
		}

		private void Awake()
		{
			//IL_002e: 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_004a: 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)
			_labelRt = ((Component)this).gameObject.GetComponent<RectTransform>();
			RectTransform labelRt = _labelRt;
			RectTransform labelRt2 = _labelRt;
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor(0.5f, 0.5f);
			labelRt2.anchorMax = val;
			labelRt.anchorMin = val;
			_labelRt.pivot = new Vector2(0.5f, 0f);
			_labelRt.sizeDelta = new Vector2(120f, 30f);
			((Component)this).gameObject.AddComponent<CanvasRenderer>();
			_tmp = ((Component)this).gameObject.AddComponent<TextMeshProUGUI>();
			((TMP_Text)_tmp).fontSize = 13.5f;
			((TMP_Text)_tmp).alignment = (TextAlignmentOptions)514;
			((TMP_Text)_tmp).fontStyle = (FontStyles)1;
			((Graphic)_tmp).raycastTarget = false;
		}

		private void LateUpdate()
		{
			//IL_0032: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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_0081: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_markerRt == (Object)null || (Object)(object)_canvasRt == (Object)null)
			{
				MapNametagsPlugin.Logger.LogWarning((object)"Label LateUpdate: missing marker/canvas");
				return;
			}
			Camera val = (((int)_canvas.renderMode == 0) ? null : _canvas.worldCamera);
			Vector2 val2 = RectTransformUtility.WorldToScreenPoint(val, ((Transform)_markerRt).position);
			Vector2 anchoredPosition = default(Vector2);
			RectTransformUtility.ScreenPointToLocalPointInRectangle(_canvasRt, val2, val, ref anchoredPosition);
			anchoredPosition.y += _pixelOffset;
			_labelRt.anchoredPosition = anchoredPosition;
		}
	}
}