Decompiled source of MoonOutsideMapTablet v1.0.1

MoonOutsideMapTablet.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
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("MoonOutsideMapTablet")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MoonOutsideMapTablet")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("071e76a6-5a96-42ed-8dae-dd4caa94da1f")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MoonOutsideMapTablet;

[BepInPlugin("hoppinhauler_moonoutsidemaptablet", "Moon Outside Map Tablet", "1.0.0")]
public class MoonOutsideMapTabletPlugin : BaseUnityPlugin
{
	private struct MapPoint
	{
		public EntranceKind Kind;

		public Vector3 Pos;

		public MapPoint(EntranceKind k, Vector3 p)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			Kind = k;
			Pos = p;
		}
	}

	private enum EntranceKind
	{
		Unknown,
		Main,
		FireExit
	}

	public const string ModGuid = "hoppinhauler_moonoutsidemaptablet";

	public const string ModName = "Moon Outside Map Tablet";

	public const string ModVersion = "1.0.0";

	private Harmony _harmony;

	private ConfigEntry<KeyCode> _toggleKey;

	private ConfigEntry<int> _mapSizePx;

	private ConfigEntry<float> _metersPerPixel;

	private ConfigEntry<float> _dotRadiusPx;

	private ConfigEntry<float> _rotationOffsetDeg;

	private GameObject _canvasGo;

	private GameObject _panelGo;

	private RawImage _mapImage;

	private Text _legendText;

	private Text _hintText;

	private Texture2D _tex;

	private Vector3? _shipPos;

	private Transform _shipTr;

	private readonly List<MapPoint> _points = new List<MapPoint>();

	private bool _visible;

	private void Awake()
	{
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Expected O, but got Unknown
		_toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Input", "ToggleKey", (KeyCode)109, "Key to open/close the map tablet");
		_mapSizePx = ((BaseUnityPlugin)this).Config.Bind<int>("UI", "MapSizePx", 512, "Map texture size (256..1024)");
		_metersPerPixel = ((BaseUnityPlugin)this).Config.Bind<float>("Map", "MetersPerPixel", 1.2f, "World meters per pixel");
		_dotRadiusPx = ((BaseUnityPlugin)this).Config.Bind<float>("Map", "DotRadiusPx", 4f, "Entrance dot radius");
		_rotationOffsetDeg = ((BaseUnityPlugin)this).Config.Bind<float>("Map", "RotationOffsetDeg", -90f, "Map rotation offset. 90 = rotate left, -90 = rotate right");
		_harmony = new Harmony("hoppinhauler_moonoutsidemaptablet");
		_harmony.PatchAll();
		SafeEnsureUI();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Moon Outside Map Tablet loaded");
	}

	private void OnDestroy()
	{
		try
		{
			_harmony.UnpatchSelf();
		}
		catch
		{
		}
	}

	private void Update()
	{
		if (!IsUIReady())
		{
			SafeEnsureUI();
		}
		if (IsUIReady() && !ShouldIgnoreHotkey() && IsTogglePressed())
		{
			_visible = !_visible;
			_panelGo.SetActive(_visible);
			if (_visible)
			{
				RefreshAndRedraw();
			}
		}
	}

	private bool IsTogglePressed()
	{
		//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_0044: Unknown result type (might be due to invalid IL or missing references)
		Keyboard current = Keyboard.current;
		if (current == null)
		{
			return false;
		}
		KeyCode value = _toggleKey.Value;
		if (!Enum.TryParse<Key>(((object)(KeyCode)(ref value)).ToString(), out Key result))
		{
			return false;
		}
		KeyControl val = current[result];
		return val != null && ((ButtonControl)val).wasPressedThisFrame;
	}

	private bool ShouldIgnoreHotkey()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Invalid comparison between Unknown and I4
		if ((int)Cursor.lockState != 1)
		{
			return true;
		}
		EventSystem current = EventSystem.current;
		if ((Object)(object)current == (Object)null)
		{
			return false;
		}
		GameObject currentSelectedGameObject = current.currentSelectedGameObject;
		if ((Object)(object)currentSelectedGameObject == (Object)null)
		{
			return false;
		}
		if ((Object)(object)currentSelectedGameObject.GetComponent<InputField>() != (Object)null)
		{
			return true;
		}
		Component[] components = currentSelectedGameObject.GetComponents<Component>();
		foreach (Component val in components)
		{
			if ((Object)(object)val != (Object)null && ((object)val).GetType().Name == "TMP_InputField")
			{
				return true;
			}
		}
		return false;
	}

	private bool IsUIReady()
	{
		return Object.op_Implicit((Object)(object)_canvasGo) && Object.op_Implicit((Object)(object)_panelGo) && Object.op_Implicit((Object)(object)_mapImage) && Object.op_Implicit((Object)(object)_legendText);
	}

	private void SafeEnsureUI()
	{
		try
		{
			CreateUI();
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)ex);
		}
	}

	private void CreateUI()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Expected O, but got Unknown
		//IL_00ae: 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)
		//IL_00dc: 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_00f2: Expected O, but got Unknown
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_canvasGo != (Object)null))
		{
			_canvasGo = new GameObject("MoonOutsideMapTablet_Canvas");
			Object.DontDestroyOnLoad((Object)(object)_canvasGo);
			Canvas val = _canvasGo.AddComponent<Canvas>();
			val.renderMode = (RenderMode)0;
			_canvasGo.AddComponent<CanvasScaler>();
			_canvasGo.AddComponent<GraphicRaycaster>();
			_panelGo = new GameObject("Panel");
			_panelGo.transform.SetParent(_canvasGo.transform, false);
			Image val2 = _panelGo.AddComponent<Image>();
			((Graphic)val2).color = new Color(0f, 0f, 0f, 0.75f);
			RectTransform component = _panelGo.GetComponent<RectTransform>();
			component.sizeDelta = new Vector2(700f, 620f);
			component.anchoredPosition = Vector2.zero;
			GameObject val3 = new GameObject("Map");
			val3.transform.SetParent(_panelGo.transform, false);
			_mapImage = val3.AddComponent<RawImage>();
			val3.GetComponent<RectTransform>().sizeDelta = new Vector2(560f, 560f);
			_legendText = CreateText("Legend", new Vector2(560f, 140f), (TextAnchor)0, -260f);
			_hintText = CreateText("Hint", new Vector2(320f, 30f), (TextAnchor)2, 280f);
			_hintText.fontSize = 16;
			_panelGo.SetActive(false);
		}
	}

	private Text CreateText(string name, Vector2 size, TextAnchor anchor, float yOffset)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Expected O, but got Unknown
		//IL_0042: 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_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject(name);
		val.transform.SetParent(_panelGo.transform, false);
		Text val2 = val.AddComponent<Text>();
		val2.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
		val2.fontSize = 18;
		((Graphic)val2).color = Color.white;
		val2.alignment = anchor;
		RectTransform component = val.GetComponent<RectTransform>();
		component.sizeDelta = size;
		component.anchoredPosition = new Vector2(0f, yOffset);
		return val2;
	}

	private void EnsureTexture()
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Expected O, but got Unknown
		int num = Mathf.Clamp(_mapSizePx.Value, 256, 1024);
		if (!((Object)(object)_tex != (Object)null) || ((Texture)_tex).width != num)
		{
			_tex = new Texture2D(num, num, (TextureFormat)4, false);
			((Texture)_tex).filterMode = (FilterMode)0;
			_mapImage.texture = (Texture)(object)_tex;
		}
	}

	private void RefreshAndRedraw()
	{
		EnsureTexture();
		RefreshPoints();
		DrawMap();
	}

	private void RefreshPoints()
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0122: Unknown result type (might be due to invalid IL or missing references)
		_points.Clear();
		_shipTr = null;
		_shipPos = null;
		GameObject val = FindShipAnchor();
		if ((Object)(object)val != (Object)null)
		{
			_shipTr = val.transform;
			_shipPos = _shipTr.position;
		}
		MonoBehaviour[] array = Object.FindObjectsOfType<MonoBehaviour>();
		foreach (MonoBehaviour val2 in array)
		{
			if ((Object)(object)val2 == (Object)null || ((object)val2).GetType().Name != "EntranceTeleport")
			{
				continue;
			}
			bool? flag = TryGetBool(val2, "isEntranceToBuilding");
			if (!flag.HasValue || flag.Value)
			{
				EntranceKind k = EntranceKind.Unknown;
				int? num = TryGetInt(val2, "entranceId");
				if (num == 0)
				{
					k = EntranceKind.Main;
				}
				else if (num.GetValueOrDefault() == 1)
				{
					k = EntranceKind.FireExit;
				}
				_points.Add(new MapPoint(k, ((Component)val2).transform.position));
			}
		}
		_legendText.text = "Ship: " + (_shipPos.HasValue ? "OK" : "NOT FOUND") + "\n" + $"Entrances: {_points.Count}\n" + $"Scale: 1px = {_metersPerPixel.Value:0.0}m\n" + $"Rotation: {_rotationOffsetDeg.Value:0}°";
		_hintText.text = $"Toggle: {_toggleKey.Value}";
	}

	private GameObject FindShipAnchor()
	{
		GameObject[] array = Object.FindObjectsOfType<GameObject>();
		foreach (GameObject val in array)
		{
			if (Object.op_Implicit((Object)(object)val) && ((Object)val).name.IndexOf("Terminal", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return ((Component)val.transform.root).gameObject;
			}
		}
		return null;
	}

	private void DrawMap()
	{
		//IL_00d6: 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_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: 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_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0177: Unknown result type (might be due to invalid IL or missing references)
		//IL_017c: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Unknown result type (might be due to invalid IL or missing references)
		int width = ((Texture)_tex).width;
		int height = ((Texture)_tex).height;
		int num = width / 2;
		int num2 = height / 2;
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				_tex.SetPixel(j, i, (j % 32 == 0 || i % 32 == 0) ? new Color(0.15f, 0.15f, 0.15f) : new Color(0.09f, 0.09f, 0.1f));
			}
		}
		if (!_shipPos.HasValue)
		{
			DrawDot(num, num2, 6f, Color.yellow);
			_tex.Apply();
			return;
		}
		DrawDot(num, num2, 6f, Color.white);
		foreach (MapPoint point in _points)
		{
			Vector2 val = WorldDeltaToShipLocal(point.Pos - _shipPos.Value);
			int x = num + Mathf.RoundToInt(val.x / _metersPerPixel.Value);
			int y = num2 + Mathf.RoundToInt(val.y / _metersPerPixel.Value);
			Color c = ((point.Kind == EntranceKind.Main) ? Color.green : ((point.Kind == EntranceKind.FireExit) ? Color.red : Color.cyan));
			DrawDot(x, y, _dotRadiusPx.Value, c);
		}
		_tex.Apply();
	}

	private Vector2 WorldDeltaToShipLocal(Vector3 delta)
	{
		//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_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: 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_0062: 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_006b: Unknown result type (might be due to invalid IL or missing references)
		Vector3 right = _shipTr.right;
		Vector3 forward = _shipTr.forward;
		float num = Vector3.Dot(delta, right);
		float num2 = Vector3.Dot(delta, forward);
		float num3 = _rotationOffsetDeg.Value * ((float)Math.PI / 180f);
		return new Vector2(num * Mathf.Cos(num3) - num2 * Mathf.Sin(num3), num * Mathf.Sin(num3) + num2 * Mathf.Cos(num3));
	}

	private void DrawDot(int x, int y, float r, Color c)
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		int num = Mathf.CeilToInt(r);
		for (int i = -num; i <= num; i++)
		{
			for (int j = -num; j <= num; j++)
			{
				if ((float)(j * j + i * i) <= r * r && x + j >= 0 && x + j < ((Texture)_tex).width && y + i >= 0 && y + i < ((Texture)_tex).height)
				{
					_tex.SetPixel(x + j, y + i, c);
				}
			}
		}
	}

	private static bool? TryGetBool(object o, string n)
	{
		FieldInfo field = o.GetType().GetField(n, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (field != null && field.FieldType == typeof(bool))
		{
			return (bool)field.GetValue(o);
		}
		return null;
	}

	private static int? TryGetInt(object o, string n)
	{
		FieldInfo field = o.GetType().GetField(n, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (field != null)
		{
			return Convert.ToInt32(field.GetValue(o));
		}
		return null;
	}
}