Decompiled source of TargetPortal v1.0.2

plugins/TargetPortal.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace TargetPortal;

[BepInPlugin("com.targetportal.valheim", "TargetPortal", "1.0.1")]
public sealed class TargetPortalPlugin : BaseUnityPlugin
{
	private sealed class PortalEntry
	{
		public ZDO Zdo;

		public string Name;

		public Vector3 Position;
	}

	public const string PluginGuid = "com.targetportal.valheim";

	public const string PluginName = "TargetPortal";

	public const string PluginVersion = "1.0.1";

	internal static TargetPortalPlugin Instance;

	internal static ManualLogSource Log;

	private TeleportWorld _originPortal;

	private bool _mapOpen;

	private Vector2 _scroll;

	private readonly List<PortalEntry> _portals = new List<PortalEntry>();

	private void Awake()
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		new Harmony("com.targetportal.valheim").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"TargetPortal 1.0.1 loaded.");
	}

	internal void OpenPortalMap(TeleportWorld origin)
	{
		if (!((Object)(object)origin == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null))
		{
			_originPortal = origin;
			RefreshPortals();
			_mapOpen = true;
			Cursor.visible = true;
			Cursor.lockState = (CursorLockMode)0;
		}
	}

	private void ClosePortalMap()
	{
		_mapOpen = false;
		_originPortal = null;
		Cursor.visible = false;
		Cursor.lockState = (CursorLockMode)1;
	}

	private void RefreshPortals()
	{
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		_portals.Clear();
		if (ZDOMan.instance == null)
		{
			return;
		}
		List<ZDO> portalList = ZDOMan.instance.GetPortalList();
		if (portalList == null)
		{
			return;
		}
		foreach (ZDO item in portalList)
		{
			string text = item.GetString("tag", string.Empty).Trim();
			if (text.Length != 0)
			{
				PortalEntry portalEntry = new PortalEntry();
				portalEntry.Zdo = item;
				portalEntry.Name = text;
				portalEntry.Position = item.GetPosition();
				_portals.Add(portalEntry);
			}
		}
		_portals.Sort((PortalEntry a, PortalEntry b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase));
	}

	private void OnGUI()
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Invalid comparison between Unknown and I4
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Invalid comparison between Unknown and I4
		if (_mapOpen)
		{
			if ((int)Event.current.type == 4 && (int)Event.current.keyCode == 27)
			{
				ClosePortalMap();
			}
			else
			{
				DrawPortalMap();
			}
		}
	}

	private void DrawPortalMap()
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: 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_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
		float num = (float)Mathf.Min(Screen.width, Screen.height) * 0.045f;
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(num, num, (float)Screen.width - num * 2f, (float)Screen.height - num * 2f);
		GUI.Box(val, string.Empty);
		Rect val2 = default(Rect);
		((Rect)(ref val2))..ctor(((Rect)(ref val)).x + 20f, ((Rect)(ref val)).y + 12f, ((Rect)(ref val)).width - 40f, 38f);
		GUI.Label(val2, "TARGET PORTAL  •  vyber cieľ", MakeStyle(21, (TextAnchor)3, Color.white));
		Rect val3 = default(Rect);
		((Rect)(ref val3))..ctor(((Rect)(ref val)).xMax - 120f, ((Rect)(ref val)).y + 12f, 100f, 32f);
		if (GUI.Button(val3, "Zavrieť"))
		{
			ClosePortalMap();
			return;
		}
		float num2 = Mathf.Clamp(((Rect)(ref val)).width * 0.25f, 190f, 310f);
		Rect val4 = default(Rect);
		((Rect)(ref val4))..ctor(((Rect)(ref val)).x + 18f, ((Rect)(ref val)).y + 58f, num2, ((Rect)(ref val)).height - 76f);
		Rect mapRect = default(Rect);
		((Rect)(ref mapRect))..ctor(((Rect)(ref val4)).xMax + 14f, ((Rect)(ref val4)).y, ((Rect)(ref val)).xMax - ((Rect)(ref val4)).xMax - 32f, ((Rect)(ref val4)).height);
		GUI.Box(val4, "Portály");
		DrawPortalList(new Rect(((Rect)(ref val4)).x + 8f, ((Rect)(ref val4)).y + 28f, ((Rect)(ref val4)).width - 16f, ((Rect)(ref val4)).height - 36f));
		DrawMap(mapRect);
	}

	private void DrawPortalList(Rect area)
	{
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: 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_0076: Unknown result type (might be due to invalid IL or missing references)
		float num = 31f;
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(0f, 0f, ((Rect)(ref area)).width - 18f, (float)_portals.Count * num + 4f);
		_scroll = GUI.BeginScrollView(area, _scroll, val);
		for (int i = 0; i < _portals.Count; i++)
		{
			PortalEntry portalEntry = _portals[i];
			if (GUI.Button(new Rect(0f, (float)i * num, ((Rect)(ref val)).width, num - 3f), portalEntry.Name))
			{
				TravelTo(portalEntry);
				GUI.EndScrollView();
				return;
			}
		}
		GUI.EndScrollView();
	}

	private void DrawMap(Rect mapRect)
	{
		//IL_0001: 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_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: 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_00d8: 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_00f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: 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)
		GUI.Box(mapRect, string.Empty);
		Texture mapTexture = GetMapTexture();
		if ((Object)(object)mapTexture != (Object)null)
		{
			GUI.DrawTexture(mapRect, mapTexture, (ScaleMode)0);
		}
		else
		{
			GUI.Label(mapRect, "Mapa sa ešte načítava…", MakeStyle(16, (TextAnchor)4, Color.white));
		}
		Rect val = default(Rect);
		Rect val2 = default(Rect);
		foreach (PortalEntry portal in _portals)
		{
			if (TryGetMapPoint(portal.Position, out var mapPoint))
			{
				float num = ((Rect)(ref mapRect)).x + mapPoint.x * ((Rect)(ref mapRect)).width;
				float num2 = ((Rect)(ref mapRect)).y + (1f - mapPoint.y) * ((Rect)(ref mapRect)).height;
				((Rect)(ref val))..ctor(num - 7f, num2 - 7f, 14f, 14f);
				Color color = GUI.color;
				GUI.color = new Color(0.2f, 1f, 0.72f, 1f);
				GUI.DrawTexture(val, (Texture)(object)Texture2D.whiteTexture);
				GUI.color = color;
				((Rect)(ref val2))..ctor(num + 9f, num2 - 12f, 130f, 26f);
				if (GUI.Button(val2, portal.Name, MakeStyle(13, (TextAnchor)3, Color.white)))
				{
					TravelTo(portal);
					break;
				}
			}
		}
	}

	private static GUIStyle MakeStyle(int fontSize, TextAnchor alignment, Color color)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		//IL_001a: 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)
		GUIStyle val = new GUIStyle(GUI.skin.label);
		val.fontSize = fontSize;
		val.alignment = alignment;
		val.normal.textColor = color;
		return val;
	}

	private static Texture GetMapTexture()
	{
		if ((Object)(object)Minimap.instance == (Object)null)
		{
			return null;
		}
		object? value = AccessTools.Field(typeof(Minimap), "m_mapTexture").GetValue(Minimap.instance);
		return (Texture)((value is Texture) ? value : null);
	}

	private static bool TryGetMapPoint(Vector3 worldPosition, out Vector2 mapPoint)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		mapPoint = Vector2.zero;
		if ((Object)(object)Minimap.instance == (Object)null)
		{
			return false;
		}
		object[] array = new object[3] { worldPosition, 0f, 0f };
		AccessTools.Method(typeof(Minimap), "WorldToMapPoint", new Type[3]
		{
			typeof(Vector3),
			typeof(float).MakeByRefType(),
			typeof(float).MakeByRefType()
		}, (Type[])null).Invoke(Minimap.instance, array);
		mapPoint = new Vector2((float)array[1], (float)array[2]);
		return true;
	}

	private void TravelTo(PortalEntry destination)
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: 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)
		if (destination != null && !((Object)(object)Player.m_localPlayer == (Object)null))
		{
			Vector3 val = destination.Position + destination.Zdo.GetRotation() * Vector3.forward * 2.2f;
			((Character)Player.m_localPlayer).TeleportTo(val, destination.Zdo.GetRotation(), true);
			ClosePortalMap();
		}
	}
}
[HarmonyPatch(typeof(TeleportWorld), "Interact")]
internal static class TeleportWorld_Interact_Patch
{
	private static bool Prefix(TeleportWorld __instance, Humanoid human, bool hold, bool alt, ref bool __result)
	{
		if (hold || alt || (Object)(object)TargetPortalPlugin.Instance == (Object)null || (Object)(object)human != (Object)(object)Player.m_localPlayer)
		{
			return true;
		}
		TargetPortalPlugin.Instance.OpenPortalMap(__instance);
		__result = true;
		return false;
	}
}