Decompiled source of Empress MapTracker v1.0.4

EmpressMapTracker.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Omniscye")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EmpressMapTracker")]
[assembly: AssemblyTitle("EmpressMapTracker")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 Empress.DarkFlareMap
{
	[BepInPlugin("Empress.DarkFlareMap", "DarkFlare Map", "1.0.1")]
	public class DarkFlareMap : BaseUnityPlugin
	{
		public const string PluginGuid = "Empress.DarkFlareMap";

		public const string PluginName = "DarkFlare Map";

		public const string PluginVersion = "1.0.1";

		private ConfigEntry<KeyboardShortcut> _toggleKey = null;

		private ConfigEntry<bool> _showEnemies = null;

		private ConfigEntry<bool> _showPlayers = null;

		private ConfigEntry<bool> _showDeadPlayers = null;

		private ConfigEntry<bool> _showValuables = null;

		private ConfigEntry<float> _scanInterval = null;

		private bool _visible;

		private float _scanTimer;

		private readonly Dictionary<Enemy, MapCustom> _enemyMarkers = new Dictionary<Enemy, MapCustom>();

		private readonly Dictionary<PlayerAvatar, MapCustom> _playerMarkers = new Dictionary<PlayerAvatar, MapCustom>();

		private readonly Dictionary<ValuableObject, MapCustom> _valuableMarkers = new Dictionary<ValuableObject, MapCustom>();

		private Sprite _dotEnemy = null;

		private Sprite _dotPlayer = null;

		private Sprite _dotPlayerDead = null;

		private Sprite _dotValuable = null;

		private static readonly Color EnemyPurple = new Color(0.75f, 0.3f, 1f, 1f);

		private static readonly Color PlayerCyan = new Color(0.4f, 0.9f, 1f, 1f);

		private static readonly Color ValuableGreen = new Color(0.3f, 1f, 0.4f, 1f);

		internal static DarkFlareMap Instance { get; private set; } = null;


		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; private set; }

		private void Awake()
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			_toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "ToggleKey", new KeyboardShortcut((KeyCode)109, Array.Empty<KeyCode>()), "Toggle the DarkFlare map overlay.");
			_showEnemies = ((BaseUnityPlugin)this).Config.Bind<bool>("Filters", "ShowEnemies", true, "Show enemy dots on the map.");
			_showPlayers = ((BaseUnityPlugin)this).Config.Bind<bool>("Filters", "ShowPlayers", true, "Show player dots on the map.");
			_showDeadPlayers = ((BaseUnityPlugin)this).Config.Bind<bool>("Filters", "ShowDeadPlayers", true, "Show hollow ring for dead players.");
			_showValuables = ((BaseUnityPlugin)this).Config.Bind<bool>("Filters", "ShowValuables", true, "Show valuables on the map.");
			_scanInterval = ((BaseUnityPlugin)this).Config.Bind<float>("Performance", "ScanInterval", 0.5f, "How often (seconds) to refresh and attach markers.");
			BuildDotSprites();
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} loaded.");
		}

		internal void Patch()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_0026: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			KeyboardShortcut value = _toggleKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				_visible = !_visible;
				TrySetMapActive(_visible);
			}
			_scanTimer -= Time.unscaledDeltaTime;
			if (_scanTimer <= 0f)
			{
				_scanTimer = Mathf.Max(0.1f, _scanInterval.Value);
				SafeScanAndSyncMarkers();
			}
			if (!_showPlayers.Value || !_showDeadPlayers.Value)
			{
				return;
			}
			foreach (KeyValuePair<PlayerAvatar, MapCustom> item in _playerMarkers.ToList())
			{
				PlayerAvatar key = item.Key;
				MapCustom value2 = item.Value;
				if (!Object.op_Implicit((Object)(object)key))
				{
					_playerMarkers.Remove(key);
					continue;
				}
				MapCustomEntity mapCustomEntity = value2.mapCustomEntity;
				if (Object.op_Implicit((Object)(object)mapCustomEntity) && Object.op_Implicit((Object)(object)mapCustomEntity.spriteRenderer))
				{
					bool deadSet = key.deadSet;
					mapCustomEntity.spriteRenderer.sprite = (deadSet ? _dotPlayerDead : _dotPlayer);
					mapCustomEntity.spriteRenderer.color = PlayerCyan;
				}
			}
		}

		private static void TrySetMapActive(bool active)
		{
			if ((Object)(object)Map.Instance != (Object)null)
			{
				Map.Instance.ActiveSet(active);
			}
			else
			{
				Logger.LogWarning((object)"Map.Instance is null – cannot toggle map.");
			}
		}

		private void SafeScanAndSyncMarkers()
		{
			//IL_0086: 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)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_023f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			if (_showEnemies.Value)
			{
				Enemy[] array = Object.FindObjectsOfType<Enemy>(false);
				foreach (Enemy val in array)
				{
					if (!_enemyMarkers.ContainsKey(val))
					{
						MapCustom val2 = AttachCustomMarker(((Component)val).transform, _dotEnemy, EnemyPurple);
						if (Object.op_Implicit((Object)(object)val2))
						{
							_enemyMarkers[val] = val2;
						}
					}
					else
					{
						EnsureVisuals(_enemyMarkers[val], _dotEnemy, EnemyPurple);
					}
				}
				CleanupDead<Enemy>(_enemyMarkers);
			}
			else
			{
				HideAndClear<Enemy>(_enemyMarkers);
			}
			if (_showPlayers.Value)
			{
				List<PlayerAvatar> list = (((Object)(object)GameDirector.instance != (Object)null) ? GameDirector.instance.PlayerList : null);
				if (list != null)
				{
					foreach (PlayerAvatar item in list)
					{
						if (!Object.op_Implicit((Object)(object)item))
						{
							continue;
						}
						if (!_playerMarkers.ContainsKey(item))
						{
							MapCustom val3 = AttachCustomMarker(Object.op_Implicit((Object)(object)item.playerTransform) ? item.playerTransform : ((Component)item).transform, _dotPlayer, PlayerCyan);
							if (Object.op_Implicit((Object)(object)val3))
							{
								_playerMarkers[item] = val3;
							}
						}
						else
						{
							EnsureVisuals(_playerMarkers[item], _dotPlayer, PlayerCyan);
						}
					}
					CleanupDead<PlayerAvatar>(_playerMarkers);
				}
			}
			else
			{
				HideAndClear<PlayerAvatar>(_playerMarkers);
			}
			if (_showValuables.Value)
			{
				ValuableObject[] array2 = Object.FindObjectsOfType<ValuableObject>(false);
				foreach (ValuableObject val4 in array2)
				{
					if (!_valuableMarkers.ContainsKey(val4))
					{
						MapCustom val5 = AttachCustomMarker(((Component)val4).transform, _dotValuable, ValuableGreen);
						if (Object.op_Implicit((Object)(object)val5))
						{
							_valuableMarkers[val4] = val5;
						}
					}
					else
					{
						EnsureVisuals(_valuableMarkers[val4], _dotValuable, ValuableGreen);
					}
				}
				CleanupDead<ValuableObject>(_valuableMarkers);
			}
			else
			{
				HideAndClear<ValuableObject>(_valuableMarkers);
			}
		}

		private static void CleanupDead<T>(Dictionary<T, MapCustom> dict) where T : Object
		{
			List<T> list = (from kv in dict
				where !Object.op_Implicit((Object)(object)kv.Key) || !Object.op_Implicit((Object)(object)kv.Value)
				select kv.Key).ToList();
			foreach (T item in list)
			{
				try
				{
					if (Object.op_Implicit((Object)(object)dict[item]) && Object.op_Implicit((Object)(object)dict[item].mapCustomEntity))
					{
						dict[item].mapCustomEntity.Hide();
					}
				}
				catch
				{
				}
				dict.Remove(item);
			}
		}

		private static void HideAndClear<T>(Dictionary<T, MapCustom> dict) where T : Object
		{
			foreach (KeyValuePair<T, MapCustom> item in dict)
			{
				try
				{
					if (Object.op_Implicit((Object)(object)item.Value) && Object.op_Implicit((Object)(object)item.Value.mapCustomEntity))
					{
						item.Value.mapCustomEntity.Hide();
					}
				}
				catch
				{
				}
			}
			dict.Clear();
		}

		private MapCustom AttachCustomMarker(Transform target, Sprite sprite, Color color)
		{
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Expected O, but got Unknown
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)target))
			{
				return null;
			}
			if ((Object)(object)Map.Instance == (Object)null)
			{
				Logger.LogWarning((object)"Map.Instance is null – cannot attach marker.");
				return null;
			}
			MapCustom val = ((IEnumerable<MapCustom>)((Component)target).GetComponentsInChildren<MapCustom>(true)).FirstOrDefault((Func<MapCustom, bool>)((MapCustom mc) => Object.op_Implicit((Object)(object)mc) && ((Object)((Component)mc).gameObject).name == "EmpressMapMarker"));
			if (Object.op_Implicit((Object)(object)val))
			{
				EnsureVisuals(val, sprite, color);
				return val;
			}
			GameObject val2 = new GameObject("EmpressMapMarker");
			val2.transform.SetParent(target, false);
			val2.transform.localPosition = Vector3.zero;
			val2.transform.localRotation = Quaternion.identity;
			val2.transform.localScale = Vector3.one;
			MapCustom val3 = val2.AddComponent<MapCustom>();
			val3.sprite = sprite;
			EnsureVisuals(val3, sprite, color);
			return val3;
		}

		private static void EnsureVisuals(MapCustom mapCustom, Sprite sprite, Color color)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)mapCustom))
			{
				mapCustom.sprite = sprite;
				MapCustomEntity mapCustomEntity = mapCustom.mapCustomEntity;
				if (Object.op_Implicit((Object)(object)mapCustomEntity) && Object.op_Implicit((Object)(object)mapCustomEntity.spriteRenderer))
				{
					mapCustomEntity.spriteRenderer.sprite = sprite;
					mapCustomEntity.spriteRenderer.color = color;
				}
			}
		}

		private void BuildDotSprites()
		{
			_dotEnemy = MakeDiscSprite(20, 20, 8);
			_dotPlayer = MakeDiscSprite(20, 20, 8);
			_dotPlayerDead = MakeRingSprite(20, 20, 8, 3);
			_dotValuable = MakeDiamondSprite(20, 20, 8);
		}

		private static Sprite MakeDiscSprite(int w, int h, int r)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: 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)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(w, h, (TextureFormat)5, false);
			((Texture)val).wrapMode = (TextureWrapMode)1;
			float num = (float)w / 2f;
			float num2 = (float)h / 2f;
			int num3 = r * r;
			Color[] array = (Color[])(object)new Color[w * h];
			for (int i = 0; i < h; i++)
			{
				for (int j = 0; j < w; j++)
				{
					float num4 = (float)j - num + 0.5f;
					float num5 = (float)i - num2 + 0.5f;
					bool flag = num4 * num4 + num5 * num5 <= (float)num3;
					array[i * w + j] = (flag ? Color.white : Color.clear);
				}
			}
			val.SetPixels(array);
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, (float)w, (float)h), new Vector2(0.5f, 0.5f), 100f);
		}

		private static Sprite MakeRingSprite(int w, int h, int rOuter, int thickness)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: 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_009d: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(w, h, (TextureFormat)5, false);
			((Texture)val).wrapMode = (TextureWrapMode)1;
			float num = (float)w / 2f;
			float num2 = (float)h / 2f;
			int num3 = rOuter * rOuter;
			int num4 = (rOuter - thickness) * (rOuter - thickness);
			Color[] array = (Color[])(object)new Color[w * h];
			for (int i = 0; i < h; i++)
			{
				for (int j = 0; j < w; j++)
				{
					float num5 = (float)j - num + 0.5f;
					float num6 = (float)i - num2 + 0.5f;
					float num7 = num5 * num5 + num6 * num6;
					bool flag = num7 <= (float)num3 && num7 >= (float)num4;
					array[i * w + j] = (flag ? Color.white : Color.clear);
				}
			}
			val.SetPixels(array);
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, (float)w, (float)h), new Vector2(0.5f, 0.5f), 100f);
		}

		private static Sprite MakeDiamondSprite(int w, int h, int r)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(w, h, (TextureFormat)5, false);
			((Texture)val).wrapMode = (TextureWrapMode)1;
			float num = (float)w / 2f;
			float num2 = (float)h / 2f;
			Color[] array = (Color[])(object)new Color[w * h];
			for (int i = 0; i < h; i++)
			{
				for (int j = 0; j < w; j++)
				{
					float num3 = Mathf.Abs((float)j - num + 0.5f);
					float num4 = Mathf.Abs((float)i - num2 + 0.5f);
					bool flag = num3 + num4 <= (float)r;
					array[i * w + j] = (flag ? Color.white : Color.clear);
				}
			}
			val.SetPixels(array);
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, (float)w, (float)h), new Vector2(0.5f, 0.5f), 100f);
		}

		private void OnGUI()
		{
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			if (_visible && !((Object)(object)Map.Instance == (Object)null) && Map.Instance.Active)
			{
				Rect val = default(Rect);
				((Rect)(ref val))..ctor(0f, 0f, (float)Screen.width, (float)Screen.height);
				GUI.color = new Color(0f, 0f, 0f, 0.35f);
				GUI.DrawTexture(val, (Texture)(object)Texture2D.whiteTexture);
				GUI.color = new Color(0f, 1f, 0.05f, 0.08f);
				float num = 4f;
				for (float num2 = 0f; num2 < ((Rect)(ref val)).height; num2 += num)
				{
					GUI.DrawTexture(new Rect(0f, num2, ((Rect)(ref val)).width, 1f), (Texture)(object)Texture2D.whiteTexture);
				}
				GUI.color = new Color(0f, 1f, 0.2f, 0.5f);
				GUI.DrawTexture(new Rect(0f, 0f, ((Rect)(ref val)).width, 2f), (Texture)(object)Texture2D.whiteTexture);
				GUI.DrawTexture(new Rect(0f, ((Rect)(ref val)).height - 2f, ((Rect)(ref val)).width, 2f), (Texture)(object)Texture2D.whiteTexture);
				GUI.DrawTexture(new Rect(0f, 0f, 2f, ((Rect)(ref val)).height), (Texture)(object)Texture2D.whiteTexture);
				GUI.DrawTexture(new Rect(((Rect)(ref val)).width - 2f, 0f, 2f, ((Rect)(ref val)).height), (Texture)(object)Texture2D.whiteTexture);
			}
		}
	}
}