Decompiled source of MapCoordinatesDisplay v0.4.0

MapCoordinateDisplay.dll

Decompiled 11 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
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(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MapCoordinateDisplay;

[BepInPlugin("aedenthorn.MapCoordinateDisplay", "Map Coordinate Display", "0.4.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { text }).GetValue();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { ((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded" }).GetValue();
				return false;
			}
			return true;
		}
	}

	private static BepInExPlugin context;

	private Harmony harmony;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<bool> isDebug;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<bool> showPlayerCoordinates;

	public static ConfigEntry<bool> showCursorCoordinates;

	public static ConfigEntry<bool> showCursorCoordinatesFirst;

	public static ConfigEntry<int> coordFontSize;

	public static ConfigEntry<bool> coordsUseShadow;

	public static ConfigEntry<float> clockShadowOffset;

	public static ConfigEntry<Vector2> coordPosition;

	public static ConfigEntry<string> titleString;

	public static ConfigEntry<string> cursorString;

	public static ConfigEntry<string> playerString;

	public static ConfigEntry<string> fontName;

	public static ConfigEntry<Color> playerCoordFontColor;

	public static ConfigEntry<Color> cursorCoordFontColor;

	public static ConfigEntry<Color> windowBackgroundColor;

	public static ConfigEntry<TextAnchor> alignment;

	private Rect windowRect;

	private int windowId = 5318008;

	private Rect coordRect;

	private Rect doubleSize;

	private Rect secondRect;

	private GUIStyle cursorStyle;

	private GUIStyle playerStyle;

	private GUIStyle windowStyle;

	private static string playerPos = "";

	private static string cursorPos = "";

	private string lastFontName;

	private Font currentFont;

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug.Value)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	private void Awake()
	{
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_025a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0269: Unknown result type (might be due to invalid IL or missing references)
		//IL_026e: 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)
		//IL_0289: Unknown result type (might be due to invalid IL or missing references)
		//IL_0293: Expected O, but got Unknown
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 907, "Nexus mod ID for updates");
		isDebug = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "IsDebug", false, "Enable debug log");
		showPlayerCoordinates = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowPlayerCoordinates", true, "Show player coordinates.");
		showCursorCoordinates = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowCursorCoordinates", true, "Show cursor coordinates.");
		showCursorCoordinatesFirst = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowCursorCoordinatesFirst", true, "Show cursor coordinates above player coordinates.");
		titleString = ((BaseUnityPlugin)this).Config.Bind<string>("Display", "TitleString", "Map Coordinates", "Title string");
		cursorString = ((BaseUnityPlugin)this).Config.Bind<string>("Display", "CursorString", "Cursor {0}", "Cursor coordinates text. {0} is replaced by the coordinates.");
		playerString = ((BaseUnityPlugin)this).Config.Bind<string>("Display", "PlayerString", "Player {0}", "Player coordinates text. {0} is replaced by the coordinates.");
		coordPosition = ((BaseUnityPlugin)this).Config.Bind<Vector2>("Display", "CoordPosition", new Vector2((float)Screen.width / 2f, 0f), "Coordinates text current screen position (draggable)");
		coordFontSize = ((BaseUnityPlugin)this).Config.Bind<int>("Display", "CoordFontSize", 20, "Coordinate font size");
		playerCoordFontColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Display", "PlayerCoordFontColor", Color.white, "Player coordinate font color");
		cursorCoordFontColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Display", "CursorCoordFontColor", Color.white, "Cursor coordinate font color");
		windowBackgroundColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Display", "windowBackgroundColor", Color.clear, "Window background color");
		fontName = ((BaseUnityPlugin)this).Config.Bind<string>("Display", "FontName", "AveriaSerifLibre-Bold", "Font name");
		alignment = ((BaseUnityPlugin)this).Config.Bind<TextAnchor>("Display", "TextAlignment", (TextAnchor)1, "Text alignment");
		coordPosition.SettingChanged += CoordPosition_SettingChanged;
		windowRect = new Rect(coordPosition.Value, new Vector2(1000f, 100f));
		harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
		harmony.PatchAll();
	}

	private void CoordPosition_SettingChanged(object sender, EventArgs e)
	{
		//IL_0007: 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_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		windowRect = new Rect(coordPosition.Value, new Vector2(1000f, 100f));
	}

	private void OnDestroy()
	{
		Dbgl("Destroying plugin");
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchAll((string)null);
		}
	}

	private static Font GetFont(string fontName, int fontSize)
	{
		Font[] array = Resources.FindObjectsOfTypeAll<Font>();
		Font[] array2 = array;
		foreach (Font val in array2)
		{
			if (((Object)val).name == fontName)
			{
				return val;
			}
		}
		return Font.CreateDynamicFontFromOSFont(fontName, fontSize);
	}

	private void OnGUI()
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: 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)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Expected O, but got Unknown
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: 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_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Expected O, but got Unknown
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0244: Unknown result type (might be due to invalid IL or missing references)
		//IL_0258: Unknown result type (might be due to invalid IL or missing references)
		//IL_026c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0276: Unknown result type (might be due to invalid IL or missing references)
		//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0334: Unknown result type (might be due to invalid IL or missing references)
		//IL_034c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0357: Unknown result type (might be due to invalid IL or missing references)
		//IL_037c: Unknown result type (might be due to invalid IL or missing references)
		//IL_038e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0393: Unknown result type (might be due to invalid IL or missing references)
		//IL_039f: Unknown result type (might be due to invalid IL or missing references)
		//IL_03be: Expected O, but got Unknown
		//IL_03b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03be: Unknown result type (might be due to invalid IL or missing references)
		//IL_0389: Unknown result type (might be due to invalid IL or missing references)
		//IL_03df: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0432: Unknown result type (might be due to invalid IL or missing references)
		if (!modEnabled.Value || !Object.op_Implicit((Object)(object)Player.m_localPlayer) || ((!Object.op_Implicit((Object)(object)Hud.instance) || !Traverse.Create((object)Hud.instance).Method("IsVisible", Array.Empty<object>()).GetValue<bool>()) && !Minimap.IsOpen()))
		{
			return;
		}
		cursorStyle = new GUIStyle
		{
			richText = true,
			fontSize = coordFontSize.Value,
			alignment = alignment.Value
		};
		cursorStyle.normal.textColor = cursorCoordFontColor.Value;
		playerStyle = new GUIStyle
		{
			richText = true,
			fontSize = coordFontSize.Value,
			alignment = alignment.Value
		};
		playerStyle.normal.textColor = playerCoordFontColor.Value;
		windowStyle = GUI.skin.window;
		if (lastFontName != fontName.Value)
		{
			lastFontName = fontName.Value;
			Dbgl("new font " + fontName.Value);
			Font font = GetFont(fontName.Value, 20);
			if ((Object)(object)font == (Object)null)
			{
				Dbgl("new font not found");
			}
			else
			{
				currentFont = font;
			}
		}
		if ((Object)(object)currentFont != (Object)null)
		{
			GUIStyle obj = cursorStyle;
			object obj2;
			if (obj == null)
			{
				obj2 = null;
			}
			else
			{
				Font font2 = obj.font;
				obj2 = ((font2 != null) ? ((Object)font2).name : null);
			}
			if ((string?)obj2 != ((Object)currentFont).name)
			{
				Dbgl("setting font " + ((Object)currentFont).name);
				cursorStyle.font = currentFont;
				playerStyle.font = currentFont;
				windowStyle.font = currentFont;
			}
		}
		playerPos = (showPlayerCoordinates.Value ? string.Format(playerString.Value, (object)new Vector3(((Component)Player.m_localPlayer).transform.position.x, ((Component)Player.m_localPlayer).transform.position.z, ((Component)Player.m_localPlayer).transform.position.y)) : "");
		if (Minimap.IsOpen() && showCursorCoordinates.Value)
		{
			Vector3 value = Traverse.Create((object)Minimap.instance).Method("ScreenToWorldPoint", new object[1] { Input.mousePosition }).GetValue<Vector3>();
			cursorPos = string.Format(cursorString.Value, (object)new Vector2(value.x, value.z));
		}
		else
		{
			cursorPos = "";
		}
		if (playerPos.Length + cursorPos.Length != 0)
		{
			GUI.backgroundColor = windowBackgroundColor.Value;
			windowRect = GUILayout.Window(windowId, new Rect(((Rect)(ref windowRect)).position, ((Rect)(ref coordRect)).position + ((playerPos.Length > 0 && cursorPos.Length > 0) ? ((Rect)(ref doubleSize)).size : ((Rect)(ref coordRect)).size)), new WindowFunction(WindowBuilder), titleString.Value, windowStyle, Array.Empty<GUILayoutOption>());
			if (!Input.GetKey((KeyCode)323) && (((Rect)(ref windowRect)).x != coordPosition.Value.x || ((Rect)(ref windowRect)).y != coordPosition.Value.y))
			{
				coordPosition.Value = new Vector2(((Rect)(ref windowRect)).x, ((Rect)(ref windowRect)).y);
			}
		}
	}

	private void WindowBuilder(int id)
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Expected O, but got Unknown
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: 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_0031: Expected O, but got Unknown
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: 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)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: 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_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_014f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: 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_01b7: Unknown result type (might be due to invalid IL or missing references)
		if (cursorPos.Length > playerPos.Length)
		{
			coordRect = GUILayoutUtility.GetRect(new GUIContent(cursorPos), cursorStyle);
		}
		else
		{
			coordRect = GUILayoutUtility.GetRect(new GUIContent(playerPos), playerStyle);
		}
		doubleSize = new Rect(((Rect)(ref coordRect)).position, new Vector2(((Rect)(ref coordRect)).width, ((Rect)(ref coordRect)).height * 2f));
		secondRect = new Rect(((Rect)(ref coordRect)).position + new Vector2(0f, ((Rect)(ref coordRect)).height), ((Rect)(ref coordRect)).size);
		if (cursorPos.Length > 0 && playerPos.Length > 0)
		{
			GUI.DragWindow(doubleSize);
			if (showCursorCoordinatesFirst.Value)
			{
				GUI.Label(coordRect, cursorPos, cursorStyle);
				GUI.Label(secondRect, playerPos, playerStyle);
			}
			else
			{
				GUI.Label(coordRect, playerPos, playerStyle);
				GUI.Label(secondRect, cursorPos, cursorStyle);
			}
		}
		else
		{
			GUI.DragWindow(coordRect);
			if (cursorPos.Length > 0)
			{
				GUI.Label(coordRect, cursorPos, cursorStyle);
			}
			else if (playerPos.Length > 0)
			{
				GUI.Label(coordRect, playerPos, playerStyle);
			}
		}
	}
}