Decompiled source of AnyPortal v0.1.0

AnyPortal.dll

Decompiled 2 days ago
using System;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using Splatform;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("AnyPortal")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("AnyPortal")]
[assembly: AssemblyTitle("AnyPortal")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 AnyPortal
{
	internal static class Client
	{
		private const float RequestSeconds = 5f;

		private const int MaxRequests = 12;

		private static List<PortalInfo> _portals = new List<PortalInfo>();

		private static readonly Dictionary<ZDOID, PortalInfo> ById = new Dictionary<ZDOID, PortalInfo>();

		private static float _nextRequest;

		private static int _requests;

		private static bool _warnedProtocol;

		internal static bool Active { get; private set; }

		internal static IReadOnlyList<PortalInfo> Portals => _portals;

		internal static void Reset()
		{
			_portals = new List<PortalInfo>();
			ById.Clear();
			Active = false;
			_nextRequest = 0f;
			_requests = 0;
			MapPins.Clear();
		}

		internal static void Tick()
		{
			if (!Active && _requests < 12 && !(Time.time < _nextRequest) && ZRoutedRpc.instance != null && !((Object)(object)ZNet.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null))
			{
				_nextRequest = Time.time + 5f;
				_requests++;
				ZRoutedRpc.instance.InvokeRoutedRPC("AnyPortal_RequestList", Array.Empty<object>());
			}
		}

		internal static void RPC_List(long sender, ZPackage pkg)
		{
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			if (ZRoutedRpc.instance == null || sender != ZRoutedRpc.instance.GetServerPeerID())
			{
				return;
			}
			List<PortalInfo> list = PortalList.Read(pkg);
			if (list == null)
			{
				if (!_warnedProtocol)
				{
					Plugin.Log.LogWarning((object)"The server runs a different version of AnyPortal; ignoring its portal list.");
				}
				_warnedProtocol = true;
				return;
			}
			if (!Active)
			{
				Plugin.Log.LogInfo((object)$"Server runs AnyPortal: {list.Count} portals");
			}
			_portals = list;
			ById.Clear();
			foreach (PortalInfo item in list)
			{
				ById[item.Id] = item;
			}
			Active = true;
			MapPins.Refresh();
			Picker.OnListChanged();
		}

		internal static PortalInfo Find(ZDOID id)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			if (!ById.TryGetValue(id, out var value))
			{
				return null;
			}
			return value;
		}

		internal static PortalInfo FindByName(string name, ZDOID except)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			string text = PortalList.NameKey(name);
			if (text.Length == 0)
			{
				return null;
			}
			foreach (PortalInfo portal in _portals)
			{
				if (portal.Id != except && PortalList.NameKey(portal.Name) == text)
				{
					return portal;
				}
			}
			return null;
		}

		internal static void SetDestination(ZDOID portal, ZDOID target)
		{
			//IL_0017: 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)
			ZRoutedRpc instance = ZRoutedRpc.instance;
			if (instance != null)
			{
				instance.InvokeRoutedRPC("AnyPortal_SetDestination", new object[2] { portal, target });
			}
		}
	}
	internal static class MapPins
	{
		private static readonly List<PinData> Pins = new List<PinData>();

		internal static void Refresh()
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			Clear();
			Minimap instance = Minimap.instance;
			if ((Object)(object)instance == (Object)null || !Client.Active || !Plugin.ShowOnMap.Value)
			{
				return;
			}
			foreach (PortalInfo portal in Client.Portals)
			{
				PinData val = instance.AddPin(portal.Position, (PinType)8, portal.DisplayName, false, false, 0L, default(PlatformUserID));
				val.m_icon = PortalIcon.Sprite;
				Pins.Add(val);
			}
		}

		internal static void Clear()
		{
			Minimap instance = Minimap.instance;
			if ((Object)(object)instance != (Object)null)
			{
				foreach (PinData pin in Pins)
				{
					instance.RemovePin(pin);
				}
			}
			Pins.Clear();
		}
	}
	internal static class NameCheck
	{
		private static TeleportWorld _portal;

		private static string _text;

		internal static bool Allow(TeleportWorld portal, string text)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			ZDO val = (((Object)(object)portal.m_nview != (Object)null) ? portal.m_nview.GetZDO() : null);
			if (val == null)
			{
				return true;
			}
			PortalInfo portalInfo = Client.FindByName(text, val.m_uid);
			if (portalInfo == null)
			{
				return true;
			}
			Player localPlayer = Player.m_localPlayer;
			if (localPlayer != null)
			{
				((Character)localPlayer).Message((MessageType)2, "A portal named \"" + portalInfo.Label + "\" already exists", 0, (Sprite)null, false);
			}
			_portal = portal;
			_text = text;
			return false;
		}

		internal static void Tick()
		{
			if (!((Object)(object)_portal == (Object)null))
			{
				TextInput instance = TextInput.instance;
				if ((Object)(object)instance == (Object)null)
				{
					Cancel();
				}
				else if (!instance.m_panel.activeSelf && !instance.m_bShouldHideNextFrame)
				{
					TeleportWorld portal = _portal;
					string text = _text;
					Cancel();
					instance.m_queuedSign = (TextReceiver)(object)portal;
					instance.Show("That name is taken: choose another", text, Plugin.MaxNameLength.Value);
				}
			}
		}

		internal static void Cancel()
		{
			_portal = null;
			_text = null;
		}
	}
	internal static class Network
	{
		internal const string List = "AnyPortal_List";

		internal const string RequestList = "AnyPortal_RequestList";

		internal const string SetDestination = "AnyPortal_SetDestination";

		private static readonly Dictionary<int, object> Added = new Dictionary<int, object>();

		private static ZRoutedRpc _rpc;

		internal static void Register()
		{
			ZRoutedRpc rpc = ZRoutedRpc.instance;
			if (rpc != null)
			{
				if (rpc != _rpc)
				{
					Added.Clear();
				}
				_rpc = rpc;
				Add(rpc, "AnyPortal_List", delegate
				{
					rpc.Register<ZPackage>("AnyPortal_List", (Action<long, ZPackage>)Client.RPC_List);
				});
				Add(rpc, "AnyPortal_RequestList", delegate
				{
					rpc.Register("AnyPortal_RequestList", (Action<long>)Server.RPC_RequestList);
				});
				Add(rpc, "AnyPortal_SetDestination", delegate
				{
					rpc.Register<ZDOID, ZDOID>("AnyPortal_SetDestination", (Action<long, ZDOID, ZDOID>)Server.RPC_SetDestination);
				});
			}
		}

		private static void Add(ZRoutedRpc rpc, string name, Action register)
		{
			int stableHashCode = StringExtensionMethods.GetStableHashCode(name);
			rpc.m_functions.Remove(stableHashCode);
			register();
			Added[stableHashCode] = rpc.m_functions[stableHashCode];
		}

		internal static void Unregister()
		{
			ZRoutedRpc instance = ZRoutedRpc.instance;
			if (instance != null && instance == _rpc)
			{
				foreach (KeyValuePair<int, object> item in Added)
				{
					if (instance.m_functions.TryGetValue(item.Key, out var value) && value == item.Value)
					{
						instance.m_functions.Remove(item.Key);
					}
				}
			}
			Added.Clear();
			_rpc = null;
		}
	}
	[HarmonyPatch(typeof(Game), "Start")]
	internal static class GameStartPatch
	{
		private static void Postfix()
		{
			Network.Register();
			Server.Reset();
			Client.Reset();
			Picker.End();
			NameCheck.Cancel();
		}
	}
	[HarmonyPatch(typeof(Game), "ConnectPortals")]
	internal static class GameConnectPortalsPatch
	{
		private static bool Prefix()
		{
			Server.Update();
			return false;
		}
	}
	[HarmonyPatch(typeof(TeleportWorld), "Interact")]
	internal static class TeleportWorldInteractPatch
	{
		private static bool Prefix(TeleportWorld __instance, Humanoid human, bool hold, bool alt, ref bool __result)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			if (!Client.Active || hold)
			{
				return true;
			}
			__result = true;
			if (!PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, true, false))
			{
				((Character)human).Message((MessageType)2, "$piece_noaccess", 0, (Sprite)null, false);
				return false;
			}
			if (alt)
			{
				Picker.Begin(__instance);
			}
			else
			{
				TextInput.instance.RequestText((TextReceiver)(object)__instance, "$piece_portal_tag", Plugin.MaxNameLength.Value);
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(TeleportWorld), "SetText")]
	internal static class TeleportWorldSetTextPatch
	{
		private static bool Prefix(TeleportWorld __instance, string text)
		{
			if (Client.Active)
			{
				return NameCheck.Allow(__instance, text);
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(TeleportWorld), "GetHoverText")]
	internal static class TeleportWorldGetHoverTextPatch
	{
		private static void Postfix(TeleportWorld __instance, ref string __result)
		{
			//IL_002a: 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_0030: 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_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Invalid comparison between Unknown and I4
			if (!Client.Active)
			{
				return;
			}
			ZDO val = (((Object)(object)__instance.m_nview != (Object)null) ? __instance.m_nview.GetZDO() : null);
			if (val == null)
			{
				return;
			}
			TagInfo tagInfo = __instance.GetTagInfo();
			string text = (string.IsNullOrEmpty(tagInfo.text) ? "" : StringExtensionMethods.RemoveRichTextTags(tagInfo.text));
			PortalInfo portalInfo = Client.Find(val.m_uid);
			PortalInfo portalInfo2 = ((portalInfo != null) ? Client.Find(portalInfo.Target) : null);
			Route route = portalInfo?.Route ?? Route.None;
			if (PortalList.NameKey(val.GetString(ZDOVars.s_tag, "")).Length == 0)
			{
				route = Route.Unnamed;
			}
			string text2;
			switch (route)
			{
			case Route.Unnamed:
				text2 = "Name this portal before you can travel from it";
				break;
			case Route.Chosen:
				if (portalInfo2 != null)
				{
					text2 = "Destination portal: \"" + portalInfo2.Label + "\"";
					break;
				}
				goto default;
			case Route.Missing:
				text2 = "Its destination was destroyed";
				break;
			default:
				text2 = "No destination";
				break;
			}
			string text3 = text2;
			string text4 = ((tagInfo.censored && (int)ZNet.m_onlineBackend == 1) ? "$piece_portal_censored_warning\n" : "");
			string text5 = ((ZInput.IsNonClassicFunctionality() && ZInput.IsGamepadActive()) ? "$KEY_AltKeys" : "$KEY_AltPlace");
			__result = Localization.instance.Localize("This portal: \"" + text + "\"\n<color=orange>" + text3 + "</color>\n\n" + text4 + "[<color=yellow><b>$KEY_Use</b></color>] Set this portal\n[<color=yellow><b>" + text5 + " + $KEY_Use</b></color>] Set destination");
		}
	}
	[HarmonyPatch(typeof(Minimap), "OnMapLeftClick")]
	internal static class MinimapOnMapLeftClickPatch
	{
		private static bool Prefix(Minimap __instance)
		{
			return Picker.OnMapClick(__instance);
		}
	}
	[HarmonyPatch(typeof(Minimap), "OnMapDblClick")]
	internal static class MinimapOnMapDblClickPatch
	{
		private static bool Prefix()
		{
			return !Picker.BlocksMapClicks;
		}
	}
	internal static class Picker
	{
		private const float BlockSeconds = 0.5f;

		private static ZDOID _source = ZDOID.None;

		private static float _endedAt = -10f;

		private static TMP_Text _hint;

		internal static bool Active => !((ZDOID)(ref _source)).IsNone();

		internal static bool BlocksMapClicks
		{
			get
			{
				if (!Active)
				{
					return Time.unscaledTime - _endedAt < 0.5f;
				}
				return true;
			}
		}

		internal static void Begin(TeleportWorld portal)
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			Minimap instance = Minimap.instance;
			ZDO val = (((Object)(object)portal.m_nview != (Object)null) ? portal.m_nview.GetZDO() : null);
			if ((Object)(object)localPlayer == (Object)null || (Object)(object)instance == (Object)null || val == null)
			{
				return;
			}
			if (PortalList.NameKey(val.GetString(ZDOVars.s_tag, "")).Length == 0)
			{
				((Character)localPlayer).Message((MessageType)2, "Name this portal first (E), so you can find your way back to it.", 0, (Sprite)null, false);
				return;
			}
			PortalInfo portalInfo = Client.Find(val.m_uid);
			if (portalInfo == null)
			{
				((Character)localPlayer).Message((MessageType)2, "This portal isn't on the map yet. Try again in a moment.", 0, (Sprite)null, false);
				return;
			}
			if (Client.Portals.Count < 2)
			{
				((Character)localPlayer).Message((MessageType)2, "There are no other portals to travel to.", 0, (Sprite)null, false);
				return;
			}
			if (!Plugin.ShowOnMap.Value)
			{
				((Character)localPlayer).Message((MessageType)2, "Turn on Map.ShowPortals in the AnyPortal config to choose a destination.", 0, (Sprite)null, false);
				return;
			}
			_source = portalInfo.Id;
			instance.ShowPointOnMap(((Component)portal).transform.position);
			ShowHint(instance);
		}

		internal static void Tick()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Invalid comparison between Unknown and I4
			if (Active)
			{
				Minimap instance = Minimap.instance;
				if ((Object)(object)instance == (Object)null || (int)instance.m_mode != 2 || (Object)(object)Player.m_localPlayer == (Object)null)
				{
					End();
				}
			}
		}

		internal static void End()
		{
			//IL_0011: 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)
			if (Active)
			{
				_endedAt = Time.unscaledTime;
			}
			_source = ZDOID.None;
			if ((Object)(object)_hint != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)_hint).gameObject);
			}
			_hint = null;
		}

		internal static void OnListChanged()
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			if (Active)
			{
				if (Client.Find(_source) == null)
				{
					End();
				}
				else if ((Object)(object)Minimap.instance != (Object)null)
				{
					ShowHint(Minimap.instance);
				}
			}
		}

		internal static bool OnMapClick(Minimap map)
		{
			//IL_0011: 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_002c: 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_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: 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)
			//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)
			if (!Active)
			{
				return !BlocksMapClicks;
			}
			PortalInfo portalInfo = Closest(map.ScreenToWorldPoint(ZInput.pointerPosition), map.PinInteractRadius);
			if (portalInfo == null)
			{
				return false;
			}
			PortalInfo portalInfo2 = Client.Find(_source);
			End();
			map.SetMapMode((MapMode)1);
			if (portalInfo2 == null)
			{
				return false;
			}
			Player localPlayer = Player.m_localPlayer;
			if (portalInfo.Id == portalInfo2.Id)
			{
				Client.SetDestination(portalInfo2.Id, ZDOID.None);
				if (localPlayer != null)
				{
					((Character)localPlayer).Message((MessageType)2, "\"" + portalInfo2.Label + "\" has no destination now", 0, (Sprite)null, false);
				}
			}
			else
			{
				Client.SetDestination(portalInfo2.Id, portalInfo.Id);
				if (localPlayer != null)
				{
					((Character)localPlayer).Message((MessageType)2, "\"" + portalInfo2.Label + "\" destination portal: \"" + portalInfo.Label + "\"", 0, (Sprite)null, false);
				}
			}
			return false;
		}

		private static PortalInfo Closest(Vector3 point, float radius)
		{
			//IL_0018: 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)
			PortalInfo result = null;
			float num = radius;
			foreach (PortalInfo portal in Client.Portals)
			{
				float num2 = Utils.DistanceXZ(point, portal.Position);
				if (num2 < num)
				{
					result = portal;
					num = num2;
				}
			}
			return result;
		}

		private static void ShowHint(Minimap map)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: 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_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			PortalInfo portalInfo = Client.Find(_source);
			if (portalInfo == null || (Object)(object)map.m_biomeNameLarge == (Object)null)
			{
				return;
			}
			if ((Object)(object)_hint == (Object)null)
			{
				TMP_Text biomeNameLarge = map.m_biomeNameLarge;
				_hint = Object.Instantiate<TMP_Text>(biomeNameLarge, biomeNameLarge.transform.parent);
				((Object)_hint).name = "AnyPortalHint";
				_hint.fontSize = biomeNameLarge.fontSize * 0.5f;
				_hint.rectTransform.anchoredPosition = biomeNameLarge.rectTransform.anchoredPosition + new Vector2(0f, (0f - biomeNameLarge.fontSize) * 1.6f);
				_hint.rectTransform.sizeDelta = new Vector2(Mathf.Max(biomeNameLarge.rectTransform.sizeDelta.x, 1200f), biomeNameLarge.rectTransform.sizeDelta.y * 2f);
			}
			PortalInfo portalInfo2 = Client.Find(portalInfo.Target);
			Route route = portalInfo.Route;
			string text;
			if (route != Route.Chosen)
			{
				if (route != Route.Missing)
				{
					goto IL_012e;
				}
				text = "Its destination was destroyed.";
			}
			else
			{
				if (portalInfo2 == null)
				{
					goto IL_012e;
				}
				text = "Destination portal: \"" + portalInfo2.Label + "\".";
			}
			goto IL_0135;
			IL_0135:
			string text2 = text;
			_hint.text = "Click the portal \"" + portalInfo.Label + "\" should lead to. " + text2 + "\nClick this portal itself to clear its destination, or close the map to cancel.";
			return;
			IL_012e:
			text = "No destination yet.";
			goto IL_0135;
		}
	}
	[BepInPlugin("anyportal", "AnyPortal", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "anyportal";

		public const string Name = "AnyPortal";

		public const string Version = "0.1.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<int> MaxNameLength;

		internal static ConfigEntry<bool> ShowOnMap;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			MaxNameLength = ((BaseUnityPlugin)this).Config.Bind<int>("Portals", "MaxNameLength", 20, new ConfigDescription("Longest portal name you can type. Vanilla allows 10.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 50), Array.Empty<object>()));
			ShowOnMap = ((BaseUnityPlugin)this).Config.Bind<bool>("Map", "ShowPortals", true, "Show every portal in the world on the map, with its name.");
			ShowOnMap.SettingChanged += delegate
			{
				MapPins.Refresh();
			};
			string name = typeof(Plugin).Assembly.GetName().Name;
			_harmony = new Harmony((name == "AnyPortal") ? "anyportal" : ("anyportal." + name));
			_harmony.PatchAll(typeof(Plugin).Assembly);
			Log.LogInfo((object)("AnyPortal 0.1.0 loaded (harmony id " + _harmony.Id + ")"));
			if (ZRoutedRpc.instance != null)
			{
				Network.Register();
			}
		}

		private void Update()
		{
			Server.Tick();
			Client.Tick();
			Picker.Tick();
			NameCheck.Tick();
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Network.Unregister();
			Picker.End();
			NameCheck.Cancel();
			MapPins.Clear();
			PortalIcon.Destroy();
		}
	}
	internal static class PortalIcon
	{
		private const int Size = 64;

		private static Texture2D _texture;

		private static Sprite _sprite;

		internal static Sprite Sprite
		{
			get
			{
				if ((Object)(object)_sprite == (Object)null)
				{
					Create();
				}
				return _sprite;
			}
		}

		internal static void Destroy()
		{
			if ((Object)(object)_sprite != (Object)null)
			{
				Object.Destroy((Object)(object)_sprite);
			}
			if ((Object)(object)_texture != (Object)null)
			{
				Object.Destroy((Object)(object)_texture);
			}
			_sprite = null;
			_texture = null;
		}

		private static void Create()
		{
			//IL_0149: 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)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Expected O, but got Unknown
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			Color32[] array = (Color32[])(object)new Color32[4096];
			for (int i = 0; i < 64; i++)
			{
				for (int j = 0; j < 64; j++)
				{
					float px = (float)j + 0.5f;
					float py = (float)(64 - i) - 0.5f;
					float num = Ellipse(px, py, 32f, 28f, 16f, 21f);
					float num2 = Mathf.Abs(num) - 3f;
					float num3 = Box(px, py, 12f, 50f, 52f, 56f);
					float num4 = Mathf.Min(num2, num3);
					float num5 = Mathf.Clamp01(0.5f - num4);
					float num6 = ((num < 0f) ? (0.35f * Mathf.Clamp01(0.5f - num)) : 0f);
					float num7 = Mathf.Clamp01(0.5f - (num4 - 2f)) * 0.6f;
					float num8 = Mathf.Max(num5, num6);
					float num9 = num8 + num7 * (1f - num8);
					byte b = (byte)((num9 > 0f) ? ((uint)Mathf.RoundToInt(255f * num8 / num9)) : 0u);
					array[i * 64 + j] = new Color32(b, b, b, (byte)Mathf.RoundToInt(255f * num9));
				}
			}
			_texture = new Texture2D(64, 64, (TextureFormat)4, false)
			{
				name = "AnyPortalIcon",
				filterMode = (FilterMode)1,
				wrapMode = (TextureWrapMode)1
			};
			_texture.SetPixels32(array);
			_texture.Apply(false, true);
			_sprite = Sprite.Create(_texture, new Rect(0f, 0f, 64f, 64f), new Vector2(0.5f, 0.5f), 100f);
			((Object)_sprite).name = "AnyPortalIcon";
		}

		private static float Ellipse(float px, float py, float cx, float cy, float rx, float ry)
		{
			float num = (px - cx) / rx;
			float num2 = (py - cy) / ry;
			return (Mathf.Sqrt(num * num + num2 * num2) - 1f) * Mathf.Min(rx, ry);
		}

		private static float Box(float px, float py, float x0, float y0, float x1, float y1)
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			float num = Mathf.Abs(px - (x0 + x1) * 0.5f) - (x1 - x0) * 0.5f;
			float num2 = Mathf.Abs(py - (y0 + y1) * 0.5f) - (y1 - y0) * 0.5f;
			Vector2 val = new Vector2(Mathf.Max(num, 0f), Mathf.Max(num2, 0f));
			return ((Vector2)(ref val)).magnitude + Mathf.Min(Mathf.Max(num, num2), 0f);
		}
	}
	internal enum Route : byte
	{
		None,
		Chosen,
		Missing,
		Unnamed
	}
	internal sealed class PortalInfo
	{
		public ZDOID Id;

		public Vector3 Position;

		public int Prefab;

		public string Name = "";

		public string Author = "";

		public Route Route;

		public ZDOID Target = ZDOID.None;

		public bool HasName => PortalList.NameKey(Name).Length > 0;

		public string DisplayName
		{
			get
			{
				//IL_0028: 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_002d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0035: Unknown result type (might be due to invalid IL or missing references)
				if (!HasName)
				{
					return "";
				}
				PlatformUserID val = (PlatformUserID)(string.IsNullOrEmpty(Author) ? PlatformUserID.None : new PlatformUserID(Author));
				return StringExtensionMethods.RemoveRichTextTags(CensorShittyWords.FilterUGC(Name, (UGCType)4, val, 0L, (GetUserProfileCompletedHandler)null)).Trim();
			}
		}

		public string Label
		{
			get
			{
				if (!HasName)
				{
					return "(unnamed)";
				}
				return DisplayName;
			}
		}
	}
	internal static class PortalList
	{
		private const int Protocol = 1;

		internal static ZPackage Write(List<PortalInfo> portals)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: 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)
			ZPackage val = new ZPackage();
			val.Write(1);
			val.Write(portals.Count);
			foreach (PortalInfo portal in portals)
			{
				val.Write(portal.Id);
				val.Write(portal.Position);
				val.Write(portal.Prefab);
				val.Write(portal.Name ?? "");
				val.Write(portal.Author ?? "");
				val.Write((byte)portal.Route);
				val.Write(portal.Target);
			}
			return val;
		}

		internal static List<PortalInfo> Read(ZPackage pkg)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			if (pkg.ReadInt() != 1)
			{
				return null;
			}
			int num = pkg.ReadInt();
			List<PortalInfo> list = new List<PortalInfo>(num);
			for (int i = 0; i < num; i++)
			{
				list.Add(new PortalInfo
				{
					Id = pkg.ReadZDOID(),
					Position = pkg.ReadVector3(),
					Prefab = pkg.ReadInt(),
					Name = pkg.ReadString(),
					Author = pkg.ReadString(),
					Route = (Route)pkg.ReadByte(),
					Target = pkg.ReadZDOID()
				});
			}
			return list;
		}

		internal static string NameKey(string name)
		{
			if (!string.IsNullOrEmpty(name))
			{
				return StringExtensionMethods.RemoveRichTextTags(name).Trim().ToLowerInvariant();
			}
			return "";
		}
	}
	internal static class Server
	{
		private static readonly int DestinationHash = StringExtensionMethods.GetStableHashCode("AnyPortal.Destination");

		private const float MatchDistance = 1f;

		private const float TickSeconds = 1f;

		private static float _nextTick;

		private static byte[] _sent;

		private static bool IsServer
		{
			get
			{
				if ((Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsServer() && ZDOMan.instance != null)
				{
					return (Object)(object)Game.instance != (Object)null;
				}
				return false;
			}
		}

		internal static void Reset()
		{
			_nextTick = 0f;
			_sent = null;
		}

		internal static void Tick()
		{
			if (!(Time.time < _nextTick) && IsServer)
			{
				_nextTick = Time.time + 1f;
				Update();
			}
		}

		internal static void Update()
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: 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_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			if (!IsServer)
			{
				return;
			}
			List<ZDO> portalList = ZDOMan.instance.GetPortalList();
			portalList.Sort((ZDO a, ZDO b) => (((ZDOID)(ref a.m_uid)).UserID == ((ZDOID)(ref b.m_uid)).UserID) ? ((ZDOID)(ref a.m_uid)).ID.CompareTo(((ZDOID)(ref b.m_uid)).ID) : ((ZDOID)(ref a.m_uid)).UserID.CompareTo(((ZDOID)(ref b.m_uid)).UserID));
			List<PortalInfo> list = Resolve(portalList);
			for (int num = 0; num < portalList.Count; num++)
			{
				ZDO val = portalList[num];
				ZDOID target = list[num].Target;
				if (val.GetConnectionZDOID((ConnectionType)1) != target)
				{
					Game.instance.SetConnection(val, target, false);
				}
			}
			ZPackage val2 = PortalList.Write(list);
			byte[] array = val2.GetArray();
			if (_sent == null || !SameBytes(_sent, array))
			{
				_sent = array;
				ZRoutedRpc instance = ZRoutedRpc.instance;
				if (instance != null)
				{
					instance.InvokeRoutedRPC(0L, "AnyPortal_List", new object[1] { val2 });
				}
			}
		}

		private static List<PortalInfo> Resolve(List<ZDO> zdos)
		{
			//IL_0071: 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)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: 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_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			List<PortalInfo> list = new List<PortalInfo>(zdos.Count);
			Vector3 position = default(Vector3);
			for (int i = 0; i < zdos.Count; i++)
			{
				ZDO val = zdos[i];
				string name = val.GetString(ZDOVars.s_tag, "");
				Route route = Route.None;
				int num = -1;
				if (PortalList.NameKey(name).Length == 0)
				{
					route = Route.Unnamed;
				}
				else if (val.GetVec3(DestinationHash, ref position))
				{
					num = IndexAt(zdos, position, i);
					route = ((num >= 0) ? Route.Chosen : Route.Missing);
				}
				list.Add(new PortalInfo
				{
					Id = val.m_uid,
					Position = val.GetPosition(),
					Prefab = val.GetPrefab(),
					Name = name,
					Author = val.GetString(ZDOVars.s_tagauthor, ""),
					Route = route,
					Target = ((num >= 0) ? zdos[num].m_uid : ZDOID.None)
				});
			}
			return list;
		}

		private static int IndexAt(List<ZDO> zdos, Vector3 position, int self)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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)
			int result = -1;
			float num = 1f;
			for (int i = 0; i < zdos.Count; i++)
			{
				if (i != self)
				{
					Vector3 val = zdos[i].GetPosition() - position;
					float sqrMagnitude = ((Vector3)(ref val)).sqrMagnitude;
					if (sqrMagnitude <= num)
					{
						result = i;
						num = sqrMagnitude;
					}
				}
			}
			return result;
		}

		private static bool SameBytes(byte[] a, byte[] b)
		{
			if (a.Length != b.Length)
			{
				return false;
			}
			for (int i = 0; i < a.Length; i++)
			{
				if (a[i] != b[i])
				{
					return false;
				}
			}
			return true;
		}

		private static bool IsPortal(ZDO zdo)
		{
			if (zdo != null)
			{
				return Game.instance.PortalPrefabHash.Contains(zdo.GetPrefab());
			}
			return false;
		}

		internal static void RPC_RequestList(long sender)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			if (IsServer)
			{
				Update();
				if (_sent != null)
				{
					ZRoutedRpc.instance.InvokeRoutedRPC(sender, "AnyPortal_List", new object[1] { (object)new ZPackage(_sent) });
				}
			}
		}

		internal static void RPC_SetDestination(long sender, ZDOID portalId, ZDOID targetId)
		{
			//IL_000d: 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)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: 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)
			if (!IsServer)
			{
				return;
			}
			ZDO zDO = ZDOMan.instance.GetZDO(portalId);
			if (!IsPortal(zDO))
			{
				return;
			}
			ZDO val = null;
			if (!((ZDOID)(ref targetId)).IsNone())
			{
				val = ZDOMan.instance.GetZDO(targetId);
				if (!IsPortal(val) || val == zDO || PortalList.NameKey(zDO.GetString(ZDOVars.s_tag, "")).Length == 0)
				{
					return;
				}
			}
			zDO.SetOwner(ZDOMan.GetSessionID());
			if (val != null)
			{
				zDO.Set(DestinationHash, val.GetPosition());
			}
			else if (zDO.RemoveVec3(DestinationHash))
			{
				zDO.IncreaseDataRevision();
			}
			ZDOMan.instance.ForceSendZDO(portalId);
			Plugin.Log.LogInfo((object)(string.Format("Portal {0} \"{1}\" -> ", portalId, zDO.GetString(ZDOVars.s_tag, "")) + ((val != null) ? string.Format("{0} \"{1}\"", targetId, val.GetString(ZDOVars.s_tag, "")) : "no destination") + $" (set by peer {sender})"));
			Update();
		}
	}
	internal static class VersionInfo
	{
		public const string Version = "0.1.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}