Decompiled source of RossPortalsPlus v1.0.0

RossPortalsPlus.dll

Decompiled 17 hours ago
using System;
using System.Collections;
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.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using RossPortals.Core;
using RossPortals.Game.Framework;
using RossPortals.Game.Portals;
using RossPortals.Game.UI;
using Splatform;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RossPortalsPlus")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RossPortalsPlus")]
[assembly: AssemblyTitle("RossPortalsPlus")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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 RossPortals.Game
{
	internal static class ModInfo
	{
		public const string Guid = "com.dreich.rossportalsplus";

		public const string Name = "RossPortalsPlus";

		public const string Version = "1.0.0";

		public const string RossPortalsGuid = "com.rossdwest.rossportals";

		public const string RossPortalsPatchGuid = "com.dreich.rossportalspatch";

		public const string XPortalGuid = "yay.spikehimself.xportal";

		private const string SavePrefix = "RossPortals";

		private const string PatchSavePrefix = "RossPortalsPatch";

		public const string KeyTarget = "RossPortals_TargetId";

		public const string KeyPrevious = "RossPortals_PreviousId";

		public const string LegacyKeyTarget = "XPortal_TargetId";

		public const string LegacyKeyPrevious = "XPortal_PreviousId";

		public const string KeyDefault = "RossPortals_Default";

		public const string KeyShowOnMap = "RossPortals_ShowOnMap";

		public const string KeyUid = "RossPortalsPatch_Uid";

		public const string KeyTargetUid = "RossPortalsPatch_TargetUid";

		public const string RpcResync = "RossPortals_Resync";

		public const string RpcSyncPortal = "RossPortals_SyncPortal";

		public const string RpcSyncRequest = "RossPortals_SyncRequest";

		public const string RpcAddOrUpdate = "RossPortals_AddOrUpdate";

		public const string RpcRemove = "RossPortals_Remove";
	}
	[BepInPlugin("com.dreich.rossportalsplus", "RossPortalsPlus", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInIncompatibility("yay.spikehimself.xportal")]
	[BepInIncompatibility("com.rossdwest.rossportals")]
	[BepInIncompatibility("com.dreich.rossportalspatch")]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class RossPortalsPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//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: Expected O, but got Unknown
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("com.dreich.rossportalsplus");
			ValheimCompat.Verify();
			PatchEachClassIndividually();
			PortalConfig.Bind(((BaseUnityPlugin)this).Config);
			if (!GUIManager.IsHeadless())
			{
				PanelScaler.Init();
			}
			GameObject val = new GameObject("RossPortalsPlusManager");
			Object.DontDestroyOnLoad((Object)val);
			val.transform.SetParent(((Component)this).gameObject.transform);
			val.AddComponent<Scheduler>();
			if (!GUIManager.IsHeadless())
			{
				new PortalConfigPanel();
			}
			MinimapManager.OnVanillaMapDataLoaded += PortalManager.RequestInitialSync;
			Log.LogInfo((object)"RossPortalsPlus 1.0.0 loaded");
		}

		private void Update()
		{
			if (!Env.IsHeadless)
			{
				PortalConfigPanel.Instance?.HandleInput();
			}
		}

		private void LateUpdate()
		{
			if (!Env.IsHeadless)
			{
				PanelScaler.Tick();
				JoinSync.Tick();
			}
		}

		private void OnDestroy()
		{
			MinimapManager.OnVanillaMapDataLoaded -= PortalManager.RequestInitialSync;
			PanelScaler.Release();
			PortalConfigPanel.Instance?.Dispose();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void PatchEachClassIndividually()
		{
			List<string> list = new List<string>();
			try
			{
				Type[] typesFromAssembly = AccessTools.GetTypesFromAssembly(Assembly.GetExecutingAssembly());
				foreach (Type type in typesFromAssembly)
				{
					if (type.GetCustomAttributes(typeof(HarmonyPatch), inherit: false).Length == 0)
					{
						continue;
					}
					try
					{
						List<MethodInfo> list2 = _harmony.CreateClassProcessor(type).Patch();
						if (list2 == null || list2.Count == 0)
						{
							list.Add(type.Name + "=SKIPPED(Prepare() false or no target)");
							continue;
						}
						foreach (MethodInfo item in list2)
						{
							list.Add(item.DeclaringType?.Name + "." + item.Name + "=OK(via " + type.Name + ")");
						}
					}
					catch (Exception ex)
					{
						list.Add(type.Name + "=FAILED(" + ex.GetType().Name + ")");
						Log.LogError((object)$"Harmony patch '{type.FullName}' failed to apply; continuing without it: {ex}");
					}
				}
			}
			catch (Exception ex2)
			{
				list.Add("enumeration=FAILED(" + ex2.GetType().Name + ")");
				Log.LogError((object)$"Harmony patching failed unexpectedly; continuing without the affected patch(es): {ex2}");
			}
			Log.LogInfo((object)("RossPortalsPlus Harmony patches: " + string.Join("; ", list)));
		}
	}
}
namespace RossPortals.Game.UI
{
	internal static class JatifyLook
	{
		private const string JatifyGuid = "jatify.ui";

		private const string ApiType = "Jatify.UI.ModArt";

		private const int LeastApiVersion = 2;

		private const string FrameName = "jatify-paper";

		private const string FillName = "Fill";

		private const float RuleInset = 30f;

		private const float HoverInk = 0.08f;

		private const float PressInk = 0.14f;

		private const float GroupInk = 0.06f;

		private const float PlaceholderAlpha = 0.6f;

		private const float HoverLift = 1.15f;

		private const float PressShade = 0.85f;

		private static readonly Color SelectedWash = new Color(0.85f, 0.62f, 0.2f, 0.28f);

		private static readonly Color SelectedInk = new Color(0.45f, 0.12f, 0.08f);

		private static readonly Color LightCaption = new Color(0.95f, 0.9f, 0.8f);

		private static readonly Color MutedSource = new Color(0.65f, 0.65f, 0.62f);

		private static bool _tried;

		private static bool _bound;

		private static bool _warned;

		private static Func<string, bool> _woodFor;

		private static Action<Image, Image> _paper;

		private static Action<Image> _paperFace;

		private static Action<Image> _smallFace;

		private static Action<Image> _smallPrimary;

		private static Func<RectTransform, float, float, float, Image> _rule;

		private static Func<Image, Image, float, float> _rail;

		private static Func<Color, Color> _ink;

		internal static bool Active
		{
			get
			{
				if (!Bind())
				{
					return false;
				}
				try
				{
					return _woodFor("com.dreich.rossportalsplus");
				}
				catch (Exception e)
				{
					Warn(e);
					return false;
				}
			}
		}

		internal static Color Text => _ink(Color.white);

		internal static Color Muted => _ink(MutedSource);

		internal static void DressPanel(GameObject panel, float panelWidth, float topRule, float bottomRule)
		{
			Guard(delegate
			{
				//IL_0017: 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_00b8: 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_0104: Expected O, but got Unknown
				Image[] components = panel.GetComponents<Image>();
				for (int i = 0; i < components.Length; i++)
				{
					((Graphic)components[i]).color = Color.clear;
				}
				Image component = new GameObject("jatify-paper", new Type[2]
				{
					typeof(RectTransform),
					typeof(Image)
				}).GetComponent<Image>();
				((Component)component).transform.SetParent(panel.transform, false);
				((Component)component).transform.SetAsFirstSibling();
				Stretch(((Graphic)component).rectTransform);
				((Graphic)component).raycastTarget = true;
				Image component2 = new GameObject("Fill", new Type[2]
				{
					typeof(RectTransform),
					typeof(Image)
				}).GetComponent<Image>();
				((Component)component2).transform.SetParent(((Component)component).transform, false);
				Stretch(((Graphic)component2).rectTransform);
				((Graphic)component2).raycastTarget = false;
				_paper(component, component2);
				RectTransform arg = (RectTransform)panel.transform;
				float arg2 = panelWidth - 60f;
				_rule(arg, 30f, topRule, arg2);
				_rule(arg, 30f, bottomRule, arg2);
			});
		}

		internal static void DressText(Text text)
		{
			Guard(delegate
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				((Graphic)text).color = Text;
				NoOutline((Graphic)(object)text);
			});
		}

		internal static void DressField(InputField field)
		{
			Guard(delegate
			{
				//IL_0047: 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)
				//IL_007a: Unknown result type (might be due to invalid IL or missing references)
				//IL_007c: 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_0088: Unknown result type (might be due to invalid IL or missing references)
				//IL_0093: Unknown result type (might be due to invalid IL or missing references)
				Action<Image> paperFace = _paperFace;
				Graphic targetGraphic = ((Selectable)field).targetGraphic;
				paperFace((Image)(((object)((targetGraphic is Image) ? targetGraphic : null)) ?? ((object)((Component)field).GetComponent<Image>())));
				if ((Object)(object)field.textComponent != (Object)null)
				{
					((Graphic)field.textComponent).color = Text;
					NoOutline((Graphic)(object)field.textComponent);
				}
				Graphic placeholder = field.placeholder;
				Text val = (Text)(object)((placeholder is Text) ? placeholder : null);
				if (val != null)
				{
					Color muted = Muted;
					((Graphic)val).color = new Color(muted.r, muted.g, muted.b, 0.6f);
					NoOutline((Graphic)(object)val);
				}
			});
		}

		internal static void DressButton(Button button, bool primary)
		{
			Guard(delegate
			{
				//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_004b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0057: 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)
				//IL_008d: 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)
				//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
				Graphic targetGraphic = ((Selectable)button).targetGraphic;
				Image obj = (Image)(object)((targetGraphic is Image) ? targetGraphic : null);
				if (primary)
				{
					_smallPrimary(obj);
				}
				else
				{
					_smallFace(obj);
				}
				((Selectable)button).transition = (Transition)1;
				ColorBlock colors = ((Selectable)button).colors;
				((ColorBlock)(ref colors)).normalColor = Color.white;
				((ColorBlock)(ref colors)).selectedColor = Color.white;
				((ColorBlock)(ref colors)).highlightedColor = new Color(1.15f, 1.15f, 1.15f);
				((ColorBlock)(ref colors)).pressedColor = new Color(0.85f, 0.85f, 0.85f);
				((Selectable)button).colors = colors;
				Text[] componentsInChildren = ((Component)button).GetComponentsInChildren<Text>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					((Graphic)componentsInChildren[i]).color = LightCaption;
				}
			});
		}

		internal static void DressToggle(Toggle toggle)
		{
			Guard(delegate
			{
				//IL_0038: Unknown result type (might be due to invalid IL or missing references)
				Action<Image> paperFace = _paperFace;
				Graphic targetGraphic = ((Selectable)toggle).targetGraphic;
				paperFace((Image)(object)((targetGraphic is Image) ? targetGraphic : null));
				if ((Object)(object)toggle.graphic != (Object)null)
				{
					toggle.graphic.color = Text;
				}
			});
		}

		internal static void DressScroll(ScrollRect scroll)
		{
			Guard(delegate
			{
				//IL_0051: 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)
				//IL_005b: Unknown result type (might be due to invalid IL or missing references)
				Image component = ((Component)scroll).GetComponent<Image>();
				if ((Object)(object)component != (Object)null)
				{
					Hide(component);
				}
				Scrollbar verticalScrollbar = scroll.verticalScrollbar;
				if (!((Object)(object)verticalScrollbar == (Object)null))
				{
					Image component2 = ((Component)verticalScrollbar).GetComponent<Image>();
					Graphic targetGraphic = ((Selectable)verticalScrollbar).targetGraphic;
					Image arg = (Image)(object)((targetGraphic is Image) ? targetGraphic : null);
					Func<Image, Image, float, float> rail = _rail;
					Rect rect = ((RectTransform)((Component)verticalScrollbar).transform).rect;
					rail(component2, arg, ((Rect)(ref rect)).width);
				}
			});
		}

		internal static ColorBlock RowColors(bool isGroup, bool selected)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: 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_0024: 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)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			Color val = (selected ? SelectedWash : (isGroup ? Wash(0.06f) : Color.clear));
			ColorBlock defaultColorBlock = ColorBlock.defaultColorBlock;
			((ColorBlock)(ref defaultColorBlock)).normalColor = val;
			((ColorBlock)(ref defaultColorBlock)).selectedColor = val;
			((ColorBlock)(ref defaultColorBlock)).highlightedColor = (selected ? SelectedWash : Wash(0.08f));
			((ColorBlock)(ref defaultColorBlock)).pressedColor = Wash(0.14f);
			((ColorBlock)(ref defaultColorBlock)).colorMultiplier = 1f;
			((ColorBlock)(ref defaultColorBlock)).fadeDuration = 0f;
			return defaultColorBlock;
		}

		internal static Color RowTextColor(bool selected)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			if (!selected)
			{
				return Text;
			}
			return SelectedInk;
		}

		internal static void NoOutline(Graphic graphic)
		{
			Outline component = ((Component)graphic).GetComponent<Outline>();
			if ((Object)(object)component != (Object)null)
			{
				((Behaviour)component).enabled = false;
			}
		}

		private static void Hide(Image image)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			Mask component = ((Component)image).GetComponent<Mask>();
			if ((Object)(object)component == (Object)null)
			{
				((Graphic)image).color = Color.clear;
				return;
			}
			component.showMaskGraphic = false;
			((Graphic)image).color = Color.white;
		}

		private static Color Wash(float alpha)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: 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_0012: 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)
			Color text = Text;
			return new Color(text.r, text.g, text.b, alpha);
		}

		private static void Stretch(RectTransform rect)
		{
			//IL_0001: 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_0017: 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)
			rect.anchorMin = Vector2.zero;
			rect.anchorMax = Vector2.one;
			rect.offsetMin = Vector2.zero;
			rect.offsetMax = Vector2.zero;
		}

		private static bool Bind()
		{
			if (_tried)
			{
				return _bound;
			}
			_tried = true;
			if (!Chainloader.PluginInfos.TryGetValue("jatify.ui", out var value) || (Object)(object)value.Instance == (Object)null)
			{
				return false;
			}
			try
			{
				Type type = ((object)value.Instance).GetType().Assembly.GetType("Jatify.UI.ModArt", throwOnError: false);
				FieldInfo fieldInfo = type?.GetField("Version", BindingFlags.Static | BindingFlags.Public);
				if (type == null || fieldInfo == null || (int)fieldInfo.GetValue(null) < 2)
				{
					return false;
				}
				_woodFor = Make<Func<string, bool>>(type, "WoodFor");
				_paper = Make<Action<Image, Image>>(type, "Paper");
				_paperFace = Make<Action<Image>>(type, "PaperFace");
				_smallFace = Make<Action<Image>>(type, "SmallFace");
				_smallPrimary = Make<Action<Image>>(type, "SmallPrimary");
				_rule = Make<Func<RectTransform, float, float, float, Image>>(type, "Rule");
				_rail = Make<Func<Image, Image, float, float>>(type, "Rail");
				_ink = Make<Func<Color, Color>>(type, "Ink");
				_bound = _woodFor != null && _paper != null && _paperFace != null && _smallFace != null && _smallPrimary != null && _rule != null && _rail != null && _ink != null;
				if (!_bound)
				{
					RossPortalsPlugin.Log.LogWarning((object)"Jatify UI's art API has changed; the portal window keeps its own look.");
				}
			}
			catch (Exception e)
			{
				_bound = false;
				Warn(e);
			}
			return _bound;
		}

		private static T Make<T>(Type api, string name) where T : Delegate
		{
			MethodInfo method = api.GetMethod(name, BindingFlags.Static | BindingFlags.Public);
			if (!(method != null))
			{
				return null;
			}
			return (T)Delegate.CreateDelegate(typeof(T), method, throwOnBindFailure: false);
		}

		private static void Guard(Action dress)
		{
			try
			{
				dress();
			}
			catch (Exception e)
			{
				Warn(e);
			}
		}

		private static void Warn(Exception e)
		{
			if (!_warned)
			{
				_warned = true;
				RossPortalsPlugin.Log.LogWarning((object)("Jatify styling of the portal window failed: " + e.Message));
			}
		}
	}
	internal static class PanelScaler
	{
		private const float DefaultPixelsPerUnit = 50f;

		private const float ReferenceWidth = 1920f;

		private const float ReferenceHeight = 1080f;

		private const float SmallestScale = 0.01f;

		private const float ScreenShare = 0.95f;

		private const float NoExtraScale = 1f;

		private const string CanvasName = "RossPortalsPlusGUI";

		private static FieldInfo _largeGuiScale;

		private static FieldInfo _minWidth;

		private static FieldInfo _minHeight;

		private static GameObject _canvas;

		private static CanvasScaler _scaler;

		private static GameObject _guiFront;

		private static GameObject _hosted;

		internal static void Init()
		{
			_largeGuiScale = AccessTools.Field(typeof(GuiScaler), "m_largeGuiScale");
			_minWidth = AccessTools.Field(typeof(GuiScaler), "m_minWidth");
			_minHeight = AccessTools.Field(typeof(GuiScaler), "m_minHeight");
			if (_largeGuiScale == null)
			{
				RossPortalsPlugin.Log.LogWarning((object)"GuiScaler.m_largeGuiScale not found; the portal panel follows the resolution only, not the GUI Scale slider.");
			}
		}

		internal static void Tick()
		{
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Expected O, but got Unknown
			GameObject val = PortalConfigPanel.Instance?.Panel;
			if ((Object)(object)_canvas != (Object)null && (Object)(object)_guiFront == (Object)null)
			{
				Release();
			}
			if ((Object)(object)_hosted != (Object)null && (Object)(object)_hosted != (Object)(object)val)
			{
				Object.Destroy((Object)(object)_hosted);
				_hosted = null;
			}
			if (!((Object)(object)val == (Object)null) && ((!((Object)(object)_canvas == (Object)null) && !((Object)(object)val.transform.parent != (Object)(object)_canvas.transform)) || Adopt(val)) && val.activeSelf)
			{
				float num = (PortalConfig.ScaleWithGameGui.Value ? GameGuiScale() : 1f);
				num *= PortalConfig.ExtraPanelScale.Value;
				num = FitOnScreen((RectTransform)val.transform, num);
				if (!Mathf.Approximately(_scaler.scaleFactor, num))
				{
					_scaler.scaleFactor = num;
				}
			}
		}

		internal static void Release()
		{
			if ((Object)(object)_canvas != (Object)null)
			{
				Object.Destroy((Object)(object)_canvas);
			}
			_canvas = null;
			_scaler = null;
			_guiFront = null;
			_hosted = null;
		}

		private static bool Adopt(GameObject panel)
		{
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Expected O, but got Unknown
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: 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_00e2: 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_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			Transform parent = panel.transform.parent;
			if ((Object)(object)parent == (Object)null)
			{
				return false;
			}
			Canvas component = ((Component)parent).GetComponent<Canvas>();
			if ((Object)(object)component == (Object)null || (Object)(object)parent.parent == (Object)null)
			{
				return false;
			}
			if ((Object)(object)_canvas == (Object)null)
			{
				_canvas = new GameObject("RossPortalsPlusGUI", new Type[5]
				{
					typeof(RectTransform),
					typeof(GuiPixelFix),
					typeof(Canvas),
					typeof(CanvasScaler),
					typeof(GraphicRaycaster)
				});
				_canvas.layer = ((Component)parent).gameObject.layer;
				_canvas.transform.SetParent(parent.parent, false);
				RectTransform val = (RectTransform)_canvas.transform;
				val.anchorMin = Vector2.zero;
				val.anchorMax = Vector2.one;
				val.offsetMin = Vector2.zero;
				val.offsetMax = Vector2.zero;
				val.anchoredPosition = Vector2.zero;
				Canvas component2 = _canvas.GetComponent<Canvas>();
				component2.additionalShaderChannels = component.additionalShaderChannels;
				component2.renderMode = (RenderMode)0;
				component2.overrideSorting = true;
				component2.sortingOrder = component.sortingOrder + 1;
				_scaler = _canvas.GetComponent<CanvasScaler>();
				CanvasScaler component3 = ((Component)parent).GetComponent<CanvasScaler>();
				_scaler.referencePixelsPerUnit = (((Object)(object)component3 != (Object)null) ? component3.referencePixelsPerUnit : 50f);
				_scaler.uiScaleMode = (ScaleMode)0;
				_scaler.scaleFactor = 1f;
				_guiFront = ((Component)parent).gameObject;
			}
			panel.transform.SetParent(_canvas.transform, false);
			_hosted = panel;
			return true;
		}

		private static float GameGuiScale()
		{
			float num = ReadStatic(_minWidth, 1920f);
			float num2 = ReadStatic(_minHeight, 1080f);
			float num3 = ReadStatic(_largeGuiScale, 1f);
			float num4 = Mathf.Min((float)Screen.width / num, (float)Screen.height / num2) * num3;
			if (!(num4 > 0.01f))
			{
				return 1f;
			}
			return num4;
		}

		private static float FitOnScreen(RectTransform rt, float scale)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			Rect rect = rt.rect;
			Vector2 size = ((Rect)(ref rect)).size;
			if (size.x <= 0f || size.y <= 0f)
			{
				return scale;
			}
			float num = Mathf.Min((float)Screen.width * 0.95f / size.x, (float)Screen.height * 0.95f / size.y);
			return Mathf.Min(scale, num);
		}

		private static float ReadStatic(FieldInfo field, float fallback)
		{
			if (field == null)
			{
				return fallback;
			}
			object value = field.GetValue(null);
			if (value is float)
			{
				return (float)value;
			}
			if (value is int num)
			{
				return num;
			}
			return fallback;
		}
	}
	internal sealed class PortalConfigPanel
	{
		private readonly struct SelectableRow
		{
			public readonly Button Button;

			public readonly Text Label;

			public readonly string Key;

			public SelectableRow(Button button, Text label, string key)
			{
				Button = button;
				Label = label;
				Key = key;
			}
		}

		private const float PanelWidth = 640f;

		private const float PanelHeight = 600f;

		private const float RowHeight = 30f;

		private const float TopRuleY = 46f;

		private const float BottomRuleY = 537f;

		private static bool _jatify;

		private readonly Dictionary<SortMode, Button> _sortButtons = new Dictionary<SortMode, Button>();

		private GameObject _panel;

		private InputField _nameField;

		private InputField _searchField;

		private Text _destinationText;

		private Toggle _defaultToggle;

		private Toggle _showMapToggle;

		private RectTransform _content;

		private Font _font;

		private readonly Dictionary<SortMode, Text> _sortLabels = new Dictionary<SortMode, Text>();

		private bool _built;

		private PortalRecord _portal;

		private string _selectedKey;

		private SortMode _sort;

		private readonly HashSet<string> _collapsed = new HashSet<string>();

		private List<DisplayRow> _lastRows;

		private readonly List<SelectableRow> _selectableRows = new List<SelectableRow>();

		public static PortalConfigPanel Instance { get; private set; }

		public bool IsOpen
		{
			get
			{
				if ((Object)(object)_panel != (Object)null)
				{
					return _panel.activeSelf;
				}
				return false;
			}
		}

		internal GameObject Panel => _panel;

		public PortalConfigPanel()
		{
			Instance = this;
			GUIManager.OnCustomGUIAvailable += OnGuiRecreated;
		}

		private void OnGuiRecreated()
		{
			if (_portal != null)
			{
				GUIManager.BlockInput(false);
			}
			_built = false;
			_panel = null;
			_portal = null;
			_sortLabels.Clear();
			_selectableRows.Clear();
			_lastRows = null;
		}

		public void Open(PortalRecord portal)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			if (!Env.IsHeadless && EnsureBuilt())
			{
				PortalManager.RefreshList();
				_portal = portal;
				_selectedKey = (portal.HasTarget ? PortalKey.Of(portal.Target) : null);
				_nameField.text = portal.Name ?? string.Empty;
				_searchField.text = string.Empty;
				_defaultToggle.isOn = portal.IsDefault;
				_showMapToggle.isOn = portal.ShowOnMap;
				_panel.SetActive(true);
				GUIManager.BlockInput(true);
				_lastRows = null;
				Rebuild();
				Scheduler.Instance?.NextFrame(delegate
				{
					_searchField.ActivateInputField();
				});
			}
		}

		public void Close()
		{
			if ((Object)(object)_panel != (Object)null)
			{
				_panel.SetActive(false);
			}
			GUIManager.BlockInput(false);
			_portal = null;
		}

		public void HandleInput()
		{
			if (IsOpen && Input.GetKeyDown((KeyCode)27))
			{
				Close();
			}
		}

		public void OnRegistryChanged()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if (IsOpen && _portal != null)
			{
				_portal = PortalRegistry.Instance.GetById(_portal.Id) ?? _portal;
				Rebuild();
			}
		}

		public void Dispose()
		{
			GUIManager.OnCustomGUIAvailable -= OnGuiRecreated;
			if ((Object)(object)_panel != (Object)null)
			{
				Object.Destroy((Object)(object)_panel);
			}
			_panel = null;
			_built = false;
			if (Instance == this)
			{
				Instance = null;
			}
		}

		private bool EnsureBuilt()
		{
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: 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_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_0207: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: Unknown result type (might be due to invalid IL or missing references)
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_0261: 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_0291: Unknown result type (might be due to invalid IL or missing references)
			//IL_029b: Expected O, but got Unknown
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_034b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0355: Expected O, but got Unknown
			//IL_036a: Unknown result type (might be due to invalid IL or missing references)
			//IL_036b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0376: 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_03a9: Expected O, but got Unknown
			//IL_03be: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f9: Expected O, but got Unknown
			if (_built && (Object)(object)_panel != (Object)null)
			{
				return true;
			}
			if (GUIManager.Instance == null || (Object)(object)GUIManager.CustomGUIFront == (Object)null)
			{
				RossPortalsPlugin.Log.LogWarning((object)"GUI not ready; cannot open the portal panel yet.");
				return false;
			}
			_built = false;
			_sortLabels.Clear();
			_sortButtons.Clear();
			_selectableRows.Clear();
			Font val = (_font = GUIManager.Instance.AveriaSerifBold);
			Transform transform = GUIManager.CustomGUIFront.transform;
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(0.5f, 0.5f);
			Vector2 val3 = default(Vector2);
			((Vector2)(ref val3))..ctor(0.5f, 1f);
			_panel = GUIManager.Instance.CreateWoodpanel(transform, val2, val2, Vector2.zero, 640f, 600f, false);
			_panel.SetActive(false);
			GUIManager.Instance.CreateText("Configure Portal", _panel.transform, val3, val3, new Vector2(0f, -28f), val, 24, Color.white, true, Color.black, 600f, 34f, false);
			_nameField = GUIManager.Instance.CreateInputField(_panel.transform, val3, val3, new Vector2(0f, -74f), (ContentType)0, "Portal name", 18, 580f, 36f).GetComponent<InputField>();
			_searchField = GUIManager.Instance.CreateInputField(_panel.transform, val3, val3, new Vector2(0f, -118f), (ContentType)0, "Search portals...", 18, 580f, 34f).GetComponent<InputField>();
			((UnityEvent<string>)(object)_searchField.onValueChanged).AddListener((UnityAction<string>)delegate
			{
				Rebuild();
			});
			CreateSortButton(SortMode.Name, "Name", -160f, val);
			CreateSortButton(SortMode.Nearest, "Nearest", 0f, val);
			CreateSortButton(SortMode.Recent, "Recent", 160f, val);
			_destinationText = GUIManager.Instance.CreateText(string.Empty, _panel.transform, val3, val3, new Vector2(-110f, -198f), val, 18, Color.white, true, Color.black, 360f, 30f, false).GetComponent<Text>();
			_destinationText.alignment = (TextAnchor)3;
			((UnityEvent)GUIManager.Instance.CreateButton("Clear", _panel.transform, val3, val3, new Vector2(210f, -198f), 110f, 30f).GetComponent<Button>().onClick).AddListener((UnityAction)delegate
			{
				Select(null);
			});
			GameObject obj = GUIManager.Instance.CreateScrollView(_panel.transform, false, true, 12f, 6f, HandleColors(), new Color(0f, 0f, 0f, 0.6f), 590f, 300f);
			((RectTransform)obj.transform).anchoredPosition = new Vector2(0f, -85f);
			ScrollRect componentInChildren = obj.GetComponentInChildren<ScrollRect>();
			_content = componentInChildren.content;
			componentInChildren.scrollSensitivity = 400f;
			VerticalLayoutGroup component = ((Component)_content).GetComponent<VerticalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)component).childControlWidth = true;
			((HorizontalOrVerticalLayoutGroup)component).childForceExpandWidth = true;
			((HorizontalOrVerticalLayoutGroup)component).spacing = 2f;
			((LayoutGroup)component).padding = new RectOffset(6, 6, 6, 6);
			GameObject val4 = GUIManager.Instance.CreateButton("OK", _panel.transform, val3, val3, new Vector2(225f, -566f), 130f, 40f);
			((UnityEvent)val4.GetComponent<Button>().onClick).AddListener(new UnityAction(Submit));
			((UnityEvent)GUIManager.Instance.CreateButton("Cancel", _panel.transform, val3, val3, new Vector2(81f, -566f), 130f, 40f).GetComponent<Button>().onClick).AddListener(new UnityAction(Close));
			_defaultToggle = CreateCheckbox("Default", -278f, -566f, 70f);
			_showMapToggle = CreateCheckbox("Show on map", -150f, -566f, 130f);
			_jatify = JatifyLook.Active;
			if (_jatify)
			{
				StyleWithJatify(componentInChildren, val4.GetComponent<Button>());
			}
			_built = true;
			return true;
		}

		private void StyleWithJatify(ScrollRect scroll, Button ok)
		{
			JatifyLook.DressPanel(_panel, 640f, 46f, 537f);
			Text[] componentsInChildren = _panel.GetComponentsInChildren<Text>(true);
			foreach (Text val in componentsInChildren)
			{
				if ((Object)(object)((Component)val).GetComponentInParent<Button>() == (Object)null && (Object)(object)((Component)val).GetComponentInParent<InputField>() == (Object)null)
				{
					JatifyLook.DressText(val);
				}
			}
			InputField[] componentsInChildren2 = _panel.GetComponentsInChildren<InputField>(true);
			for (int i = 0; i < componentsInChildren2.Length; i++)
			{
				JatifyLook.DressField(componentsInChildren2[i]);
			}
			Toggle[] componentsInChildren3 = _panel.GetComponentsInChildren<Toggle>(true);
			for (int i = 0; i < componentsInChildren3.Length; i++)
			{
				JatifyLook.DressToggle(componentsInChildren3[i]);
			}
			Button[] componentsInChildren4 = _panel.GetComponentsInChildren<Button>(true);
			foreach (Button val2 in componentsInChildren4)
			{
				if (!((Component)val2).transform.IsChildOf((Transform)(object)_content))
				{
					JatifyLook.DressButton(val2, (Object)(object)val2 == (Object)(object)ok);
				}
			}
			JatifyLook.DressScroll(scroll);
		}

		private Toggle CreateCheckbox(string label, float x, float y, float labelWidth)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: 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_0043: 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)
			//IL_004a: 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_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor(0.5f, 1f);
			GameObject val2 = GUIManager.Instance.CreateToggle(_panel.transform, 24f, 24f);
			RectTransform val3 = (RectTransform)val2.transform;
			val3.anchorMin = val;
			val3.anchorMax = val;
			val3.pivot = new Vector2(0.5f, 0.5f);
			val3.anchoredPosition = new Vector2(x, y);
			Text[] componentsInChildren = val2.GetComponentsInChildren<Text>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].text = string.Empty;
			}
			Text component = GUIManager.Instance.CreateText(label, _panel.transform, val, val, new Vector2(x + 18f + labelWidth / 2f, y), _font, 16, Color.white, true, Color.black, labelWidth, 24f, false).GetComponent<Text>();
			component.alignment = (TextAnchor)3;
			((Graphic)component).raycastTarget = false;
			return val2.GetComponent<Toggle>();
		}

		private void CreateSortButton(SortMode mode, string token, float x, Font font)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Expected O, but got Unknown
			GameObject val = GUIManager.Instance.CreateButton(token, _panel.transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(x, -156f), 150f, 30f);
			((UnityEvent)val.GetComponent<Button>().onClick).AddListener((UnityAction)delegate
			{
				_sort = mode;
				Rebuild();
			});
			_sortLabels[mode] = val.GetComponentInChildren<Text>();
			_sortButtons[mode] = val.GetComponent<Button>();
		}

		private static ColorBlock HandleColors()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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)
			ColorBlock defaultColorBlock = ColorBlock.defaultColorBlock;
			((ColorBlock)(ref defaultColorBlock)).normalColor = new Color(0.35f, 0.35f, 0.35f, 1f);
			((ColorBlock)(ref defaultColorBlock)).highlightedColor = new Color(0.5f, 0.5f, 0.5f, 1f);
			((ColorBlock)(ref defaultColorBlock)).pressedColor = new Color(0.6f, 0.6f, 0.6f, 1f);
			((ColorBlock)(ref defaultColorBlock)).colorMultiplier = 1f;
			return defaultColorBlock;
		}

		private void Rebuild()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: 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_0066: 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)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Expected O, but got Unknown
			if (!_built || _portal == null)
			{
				return;
			}
			Vector3 val = (((Object)(object)Player.m_localPlayer != (Object)null) ? ((Component)Player.m_localPlayer).transform.position : Vector3.zero);
			IReadOnlyList<DisplayRow> readOnlyList = PortalListView.Build(PortalRegistry.Instance.BuildEntries(_portal.Id), PortalConfig.Separator, _searchField.text, _sort, new Vec3(val.x, val.y, val.z), PortalManager.RecentKeys, _collapsed);
			if (_lastRows != null && ((Transform)_content).childCount > 0 && RowsEqual(_lastRows, readOnlyList))
			{
				return;
			}
			_lastRows = new List<DisplayRow>(readOnlyList);
			for (int num = ((Transform)_content).childCount - 1; num >= 0; num--)
			{
				Object.DestroyImmediate((Object)(object)((Component)((Transform)_content).GetChild(num)).gameObject);
			}
			_selectableRows.Clear();
			_destinationText.text = "Destination: " + SelectedName();
			if (_jatify)
			{
				foreach (KeyValuePair<SortMode, Button> sortButton in _sortButtons)
				{
					JatifyLook.DressButton(sortButton.Value, sortButton.Key == _sort);
				}
			}
			else
			{
				foreach (KeyValuePair<SortMode, Text> sortLabel in _sortLabels)
				{
					((Graphic)sortLabel.Value).color = ((sortLabel.Key == _sort) ? Color.yellow : Color.white);
				}
			}
			BuildRow("(no destination)", "", 0, isGroup: false, _selectedKey == null, (UnityAction)delegate
			{
				Select(null);
			}, null, selectable: true);
			foreach (DisplayRow item in readOnlyList)
			{
				AddRow(item);
			}
		}

		private static bool RowsEqual(IReadOnlyList<DisplayRow> a, IReadOnlyList<DisplayRow> b)
		{
			if (a.Count != b.Count)
			{
				return false;
			}
			for (int i = 0; i < a.Count; i++)
			{
				DisplayRow displayRow = a[i];
				DisplayRow displayRow2 = b[i];
				if (displayRow.Kind != displayRow2.Kind || displayRow.Depth != displayRow2.Depth || displayRow.Label != displayRow2.Label || displayRow.GroupPath != displayRow2.GroupPath || displayRow.Collapsed != displayRow2.Collapsed || displayRow.PortalCount != displayRow2.PortalCount || displayRow.PortalId != displayRow2.PortalId || FormatDistance(displayRow.Distance) != FormatDistance(displayRow2.Distance))
				{
					return false;
				}
			}
			return true;
		}

		private void AddRow(DisplayRow row)
		{
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Expected O, but got Unknown
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			if (row.Kind == RowKind.Group)
			{
				string arrow = (row.Collapsed ? "▶" : "▼");
				string path = row.GroupPath;
				BuildRow(row.Label, row.PortalCount.ToString(), row.Depth, isGroup: true, selected: false, (UnityAction)delegate
				{
					if (!_collapsed.Remove(path))
					{
						_collapsed.Add(path);
					}
					Rebuild();
				}, arrow);
			}
			else
			{
				string label = (string.IsNullOrEmpty(row.Label) ? "(no name)" : row.Label);
				bool selected = row.PortalId == _selectedKey;
				string key = row.PortalId;
				BuildRow(label, FormatDistance(row.Distance), row.Depth, isGroup: false, selected, (UnityAction)delegate
				{
					Select(key);
				}, null, selectable: true, key);
			}
		}

		private void BuildRow(string label, string trailer, int depth, bool isGroup, bool selected, UnityAction onClick, string arrow = null, bool selectable = false, string key = null)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0052: 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)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: 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_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: 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)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("row", new Type[3]
			{
				typeof(RectTransform),
				typeof(Image),
				typeof(Button)
			});
			val.transform.SetParent((Transform)(object)_content, false);
			Image component = val.GetComponent<Image>();
			((Graphic)component).color = Color.white;
			Button component2 = val.GetComponent<Button>();
			((Selectable)component2).targetGraphic = (Graphic)(object)component;
			((Selectable)component2).transition = (Transition)1;
			ColorBlock val2 = (((Selectable)component2).colors = RowColors(isGroup, selected));
			((Behaviour)component2).enabled = false;
			((Behaviour)component2).enabled = true;
			((Graphic)component).canvasRenderer.SetColor(((ColorBlock)(ref val2)).normalColor);
			((UnityEvent)component2.onClick).AddListener(onClick);
			LayoutElement obj = val.AddComponent<LayoutElement>();
			obj.minHeight = 30f;
			obj.preferredHeight = 30f;
			obj.flexibleWidth = 1f;
			Color color = RowTextColor(isGroup, selected);
			float num = 12f + (float)depth * 22f;
			if (arrow != null)
			{
				AddLabel(val.transform, arrow, (TextAnchor)3, color, num, 60f, 11);
				num += 14f;
			}
			Text label2 = AddLabel(val.transform, label, (TextAnchor)3, color, num, 60f);
			if (!string.IsNullOrEmpty(trailer))
			{
				AddLabel(val.transform, trailer, (TextAnchor)5, (Color)(_jatify ? JatifyLook.Muted : new Color(0.65f, 0.65f, 0.62f)), 8f, 10f);
			}
			if (selectable)
			{
				_selectableRows.Add(new SelectableRow(component2, label2, key));
			}
		}

		private Text AddLabel(Transform parent, string text, TextAnchor anchor, Color color, float leftPad, float rightPad, int fontSize = 17)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_004b: 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_0051: 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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: 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_00a5: 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_00e3: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("label", new Type[3]
			{
				typeof(RectTransform),
				typeof(Text),
				typeof(Outline)
			});
			val.transform.SetParent(parent, false);
			RectTransform val2 = (RectTransform)val.transform;
			val2.anchorMin = Vector2.zero;
			val2.anchorMax = Vector2.one;
			val2.offsetMin = new Vector2(leftPad, 0f);
			val2.offsetMax = new Vector2(0f - rightPad, 0f);
			Text component = val.GetComponent<Text>();
			component.font = _font;
			component.fontSize = fontSize;
			((Graphic)component).color = color;
			component.alignment = anchor;
			component.horizontalOverflow = (HorizontalWrapMode)1;
			component.verticalOverflow = (VerticalWrapMode)0;
			component.text = text;
			Outline component2 = val.GetComponent<Outline>();
			((Shadow)component2).effectColor = new Color(0f, 0f, 0f, 0.6f);
			((Behaviour)component2).enabled = !_jatify;
			return component;
		}

		private static ColorBlock RowColors(bool isGroup, bool selected)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: 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_006a: 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_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: 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_00d3: 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)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (_jatify)
			{
				return JatifyLook.RowColors(isGroup, selected);
			}
			Color val = (selected ? new Color(0.85f, 0.7f, 0.2f, 0.3f) : (isGroup ? new Color(1f, 1f, 1f, 0.1f) : new Color(1f, 1f, 1f, 0.02f)));
			ColorBlock defaultColorBlock = ColorBlock.defaultColorBlock;
			((ColorBlock)(ref defaultColorBlock)).normalColor = val;
			((ColorBlock)(ref defaultColorBlock)).highlightedColor = new Color(1f, 1f, 1f, 0.16f);
			((ColorBlock)(ref defaultColorBlock)).pressedColor = new Color(1f, 1f, 1f, 0.24f);
			((ColorBlock)(ref defaultColorBlock)).selectedColor = val;
			((ColorBlock)(ref defaultColorBlock)).colorMultiplier = 1f;
			((ColorBlock)(ref defaultColorBlock)).fadeDuration = 0.08f;
			return defaultColorBlock;
		}

		private void Select(string key)
		{
			//IL_0051: 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)
			_selectedKey = key;
			_destinationText.text = "Destination: " + SelectedName();
			foreach (SelectableRow selectableRow in _selectableRows)
			{
				bool selected = selectableRow.Key == _selectedKey;
				((Selectable)selectableRow.Button).colors = RowColors(isGroup: false, selected);
				((Graphic)selectableRow.Label).color = RowTextColor(isGroup: false, selected);
			}
		}

		private static Color RowTextColor(bool isGroup, bool selected)
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: 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_001c: Unknown result type (might be due to invalid IL or missing references)
			if (!_jatify)
			{
				if (!selected)
				{
					if (!isGroup)
					{
						return new Color(0.92f, 0.9f, 0.85f);
					}
					return new Color(0.85f, 0.9f, 1f);
				}
				return new Color(1f, 0.86f, 0.4f);
			}
			return JatifyLook.RowTextColor(selected);
		}

		private void Submit()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: 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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			ZDOID newTarget = ZDOID.None;
			if (_selectedKey != null)
			{
				PortalRecord byKey = PortalRegistry.Instance.GetByKey(_selectedKey);
				if (byKey != null)
				{
					newTarget = byKey.Id;
				}
			}
			PortalManager.SubmitPortalConfig(_portal, _nameField.text, newTarget, _defaultToggle.isOn, _showMapToggle.isOn);
			Close();
		}

		private string SelectedName()
		{
			if (_selectedKey == null)
			{
				return "(none)";
			}
			PortalRecord byKey = PortalRegistry.Instance.GetByKey(_selectedKey);
			if (byKey == null)
			{
				return "(none)";
			}
			if (!string.IsNullOrEmpty(byKey.Name))
			{
				return byKey.Name;
			}
			return "(no name)";
		}

		private static string FormatDistance(float metres)
		{
			if (metres >= 1000f)
			{
				return $"{metres / 1000f:0.0} km";
			}
			return $"{(int)metres} m";
		}
	}
}
namespace RossPortals.Game.Portals
{
	internal static class Env
	{
		public static bool IsServer
		{
			get
			{
				if ((Object)(object)ZNet.instance != (Object)null)
				{
					return ZNet.instance.IsServer();
				}
				return false;
			}
		}

		public static bool IsHeadless => GUIManager.IsHeadless();

		public static bool GameStarted { get; set; }

		public static bool ShuttingDown
		{
			get
			{
				if ((Object)(object)Game.instance != (Object)null)
				{
					return Game.instance.m_shuttingDown;
				}
				return false;
			}
		}

		public static long ServerPeerId => ZRoutedRpc.instance.GetServerPeerID();
	}
	internal static class JoinSync
	{
		private const float RetryDelaySeconds = 5f;

		private const int MaxRetries = 5;

		private const string RetryReason = " joined (retry)";

		private const string UnknownPlayer = "a player";

		private static bool _listReceived;

		private static bool _waiting;

		private static int _retries;

		private static float _nextRetry;

		internal static void Reset()
		{
			_listReceived = false;
			_waiting = false;
			_retries = 0;
		}

		internal static void ListReceived()
		{
			_listReceived = true;
		}

		internal static void PlayerSpawned(Player player)
		{
			if (!((Object)(object)player != (Object)(object)Player.m_localPlayer) && !_listReceived && !_waiting && !((Object)(object)ZNet.instance == (Object)null) && !Env.IsServer)
			{
				_waiting = true;
				_retries = 0;
				_nextRetry = Time.unscaledTime + 5f;
			}
		}

		internal static void Tick()
		{
			if (!_waiting)
			{
				return;
			}
			if (_listReceived || (Object)(object)ZNet.instance == (Object)null || ZRoutedRpc.instance == null)
			{
				_waiting = false;
			}
			else
			{
				if (Time.unscaledTime < _nextRetry)
				{
					return;
				}
				if (_retries < 5)
				{
					_retries++;
					_nextRetry = Time.unscaledTime + 5f;
					try
					{
						PortalRpc.RequestSync(PlayerName() + " joined (retry)");
						return;
					}
					catch (Exception ex)
					{
						_waiting = false;
						RossPortalsPlugin.Log.LogError((object)("Asking the server for the portal list again failed: " + ex));
						return;
					}
				}
				_waiting = false;
				RossPortalsPlugin.Log.LogWarning((object)"The server never sent the portal list; opening a portal's panel will ask for it again.");
			}
		}

		private static string PlayerName()
		{
			PlayerProfile val = (((Object)(object)Game.instance != (Object)null) ? Game.instance.GetPlayerProfile() : null);
			if (val == null)
			{
				return "a player";
			}
			return val.GetName();
		}
	}
	[HarmonyPatch(typeof(Player), "OnSpawned")]
	internal static class Player_OnSpawned
	{
		private static void Postfix(Player __instance)
		{
			JoinSync.PlayerSpawned(__instance);
		}
	}
	internal static class MapPins
	{
		private const PinType PinType = (PinType)6;

		private static readonly Dictionary<string, PinData> _pins = new Dictionary<string, PinData>();

		public static void Refresh()
		{
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: 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_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			if (Env.IsHeadless || (Object)(object)Minimap.instance == (Object)null)
			{
				return;
			}
			HashSet<string> seen = new HashSet<string>();
			foreach (PortalRecord item in PortalRegistry.Instance.GetList())
			{
				if (!item.ShowOnMap)
				{
					continue;
				}
				string key = item.Key;
				seen.Add(key);
				string text = (string.IsNullOrEmpty(item.Name) ? "Portal" : item.Name);
				if (_pins.TryGetValue(key, out var value))
				{
					if (value.m_pos == item.Location && value.m_name == text)
					{
						continue;
					}
					Minimap.instance.RemovePin(value);
				}
				_pins[key] = Minimap.instance.AddPin(item.Location, (PinType)6, text, false, false, 0L, default(PlatformUserID));
			}
			foreach (string item2 in _pins.Keys.Where((string k) => !seen.Contains(k)).ToList())
			{
				Minimap.instance.RemovePin(_pins[item2]);
				_pins.Remove(item2);
			}
		}

		public static void Clear()
		{
			if ((Object)(object)Minimap.instance != (Object)null)
			{
				foreach (PinData value in _pins.Values)
				{
					Minimap.instance.RemovePin(value);
				}
			}
			_pins.Clear();
		}
	}
	internal static class PortalConfig
	{
		private static ConfigEntry<string> _separator;

		private const float DefaultExtraPanelScale = 1f;

		private const float MinExtraPanelScale = 0.5f;

		private const float MaxExtraPanelScale = 2f;

		public static ConfigEntry<bool> ScaleWithGameGui;

		public static ConfigEntry<float> ExtraPanelScale;

		public static char Separator
		{
			get
			{
				string text = _separator?.Value;
				if (!string.IsNullOrEmpty(text))
				{
					return text[0];
				}
				return '/';
			}
		}

		public static void Bind(ConfigFile config)
		{
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			_separator = config.Bind<string>("Grouping", "FolderSeparator", "/", "The character in a portal's name that starts a folder. \"Mines/Copper\" puts portal \"Copper\" in folder \"Mines\". Client-side; affects only how your own list is grouped.");
			ScaleWithGameGui = config.Bind<bool>("UI", "ScaleWithGameGui", true, "Scale the portal panel the same way Valheim scales its own UI (screen resolution x the GUI Scale slider in Settings > Accessibility).");
			ExtraPanelScale = config.Bind<float>("UI", "ExtraPanelScale", 1f, new ConfigDescription("Extra multiplier on top of the game's GUI scale, for the portal panel only.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 2f), Array.Empty<object>()));
		}
	}
	internal static class PortalKey
	{
		public unsafe static string Of(ZDOID id)
		{
			return ((object)(*(ZDOID*)(&id))/*cast due to .constrained prefix*/).ToString();
		}
	}
	internal static class PortalManager
	{
		private const int RecentCap = 30;

		private static readonly List<string> _recent = new List<string>();

		public static IReadOnlyList<string> RecentKeys => _recent;

		public static void OnGameStarted()
		{
			PortalRegistry.Instance.Reset();
			JoinSync.Reset();
			MapPins.Clear();
			PortalRpc.Register();
		}

		public static void RequestInitialSync()
		{
			PlayerProfile val = (((Object)(object)Game.instance != (Object)null) ? Game.instance.GetPlayerProfile() : null);
			PortalRpc.RequestSync(((val != null) ? val.GetName() : "a player") + " joined");
		}

		public static void RefreshList()
		{
			PortalRpc.RequestSync("panel opened");
		}

		public static void ProcessSyncRequest(long requester, string reason)
		{
			List<ZDO> portalList = ZDOMan.instance.GetPortalList();
			PortalRegistry.Instance.ApplyPortalZdos(portalList);
			RossPortalsPlugin.Log.LogDebug((object)$"Portal sync ({reason}): server sees {portalList.Count} portal(s)");
			PortalRpc.SendResync(requester, PortalRegistry.Instance.Pack(), reason);
			NotifyListChanged();
		}

		public static void ServerAddOrUpdate(PortalRecord record)
		{
			//IL_0095: 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_0041: 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)
			PortalRecord portalRecord = PortalRegistry.Instance.AddOrUpdate(record);
			WriteWithRetry(portalRecord, 5);
			PortalRpc.BroadcastPortal(portalRecord);
			NotifyListChanged();
			if (portalRecord.IsDefault)
			{
				foreach (PortalRecord item in PortalRegistry.Instance.GetList())
				{
					if (!(item.Id == portalRecord.Id) && item.IsDefault)
					{
						item.IsDefault = false;
						PortalZdo.Write(item);
						PortalRpc.BroadcastPortal(item);
					}
				}
			}
			if (portalRecord.HasTarget)
			{
				PortalRecord byId = PortalRegistry.Instance.GetById(portalRecord.Target);
				if (byId != null && !byId.HasTarget)
				{
					byId.Target = portalRecord.Id;
					ServerAddOrUpdate(byId);
				}
			}
		}

		public static void ServerRemove(ZDOID id)
		{
			//IL_0005: 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_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)
			if (!PortalRegistry.Instance.Remove(id))
			{
				return;
			}
			foreach (PortalRecord item in PortalRegistry.Instance.GetPortalsWithTarget(id))
			{
				item.Target = ZDOID.None;
				ServerAddOrUpdate(item);
			}
			PortalRpc.BroadcastResync(PortalRegistry.Instance.Pack(), "portal removed");
			NotifyListChanged();
		}

		private static void WriteWithRetry(PortalRecord record, int attempts)
		{
			//IL_001f: 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)
			if (ZDOMan.instance.GetZDO(record.Id) != null)
			{
				PortalZdo.Write(record);
				return;
			}
			if (attempts <= 0 || (Object)(object)Scheduler.Instance == (Object)null)
			{
				RossPortalsPlugin.Log.LogWarning((object)$"Portal ZDO {record.Id} never materialised; name/target not persisted.");
				return;
			}
			Scheduler.Instance.AfterFrames(3, delegate
			{
				WriteWithRetry(record, attempts - 1);
			});
		}

		public static void OnPortalPlaced(ZDOID id, Vector3 location)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: 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_0020: 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)
			ZDOMan.instance.ForceSendZDO(id);
			ZDOID target = ResolveDefaultTarget(id);
			PortalRpc.RequestAddOrUpdate(new PortalRecord(id)
			{
				Location = location,
				Target = target
			});
		}

		public static void OnPortalDestroyed(ZDOID id)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			PortalRpc.RequestRemove(id);
		}

		public static string BuildHoverText(ZDOID id, Vector3 location)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			PortalRecord obj = PortalRegistry.Instance.GetById(id) ?? Adopt(id, location);
			string text = FriendlyName(obj.Name);
			string text2 = FriendlyTarget(obj);
			return Localization.instance.Localize("Name: " + text + "\nDestination: " + text2 + "\n[<color=yellow><b>$KEY_Use</b></color>] Configure");
		}

		public static void OnPortalInteract(ZDOID id)
		{
			//IL_0014: 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)
			//IL_002a: 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_002e: Unknown result type (might be due to invalid IL or missing references)
			if (!Env.IsHeadless && PortalConfigPanel.Instance != null)
			{
				ZDO zDO = ZDOMan.instance.GetZDO(id);
				PortalRecord portal = PortalRegistry.Instance.GetById(id) ?? Adopt(id, (zDO != null) ? zDO.GetPosition() : Vector3.zero);
				PortalConfigPanel.Instance.Open(portal);
			}
		}

		public static void SubmitPortalConfig(PortalRecord portal, string newName, ZDOID newTarget, bool isDefault, bool showOnMap)
		{
			//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_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			if (newName == null)
			{
				newName = string.Empty;
			}
			if (!(portal.Name == newName) || !(portal.Target == newTarget) || portal.IsDefault != isDefault || portal.ShowOnMap != showOnMap)
			{
				portal.Name = newName;
				portal.Target = newTarget;
				portal.IsDefault = isDefault;
				portal.ShowOnMap = showOnMap;
				PortalRpc.RequestAddOrUpdate(portal);
			}
		}

		public static void RecordUsed(ZDOID target)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			if (!(target == ZDOID.None))
			{
				string item = PortalKey.Of(target);
				_recent.Remove(item);
				_recent.Insert(0, item);
				if (_recent.Count > 30)
				{
					_recent.RemoveRange(30, _recent.Count - 30);
				}
			}
		}

		public static void NotifyListChanged()
		{
			if (!Env.IsHeadless)
			{
				PortalConfigPanel.Instance?.OnRegistryChanged();
				MapPins.Refresh();
			}
		}

		public static ZDOID ResolveDefaultTarget(ZDOID excludeId)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: 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_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_0058: Unknown result type (might be due to invalid IL or missing references)
			foreach (PortalRecord item in PortalRegistry.Instance.GetList())
			{
				if (item.IsDefault && item.Id != excludeId)
				{
					return item.Id;
				}
			}
			return ZDOID.None;
		}

		private static PortalRecord Adopt(ZDOID id, Vector3 location)
		{
			//IL_0005: 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_0014: 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)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			ZDO zDO = ZDOMan.instance.GetZDO(id);
			PortalRecord portalRecord = new PortalRecord(id)
			{
				Location = location,
				Name = ((zDO != null) ? (PortalZdo.GetName(zDO) ?? string.Empty) : string.Empty),
				Target = ((zDO != null) ? PortalZdo.GetTarget(zDO) : ZDOID.None)
			};
			PortalRegistry.Instance.AddOrUpdate(portalRecord);
			PortalRpc.RequestSync("adopted an unknown portal");
			return portalRecord;
		}

		private static string FriendlyName(string name)
		{
			if (!string.IsNullOrEmpty(name))
			{
				return name;
			}
			return "(no name)";
		}

		private static string FriendlyTarget(PortalRecord portal)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			if (!portal.HasTarget)
			{
				return "(none)";
			}
			PortalRecord byId = PortalRegistry.Instance.GetById(portal.Target);
			if (byId == null)
			{
				return "(none)";
			}
			return FriendlyName(byId.Name);
		}
	}
	internal sealed class PortalRecord
	{
		public ZDOID Id;

		public string Name;

		public Vector3 Location;

		public ZDOID Target;

		public bool IsDefault;

		public bool ShowOnMap = true;

		private string _key;

		public bool HasTarget
		{
			get
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				if (Target != ZDOID.None)
				{
					return !((ZDOID)(ref Target)).IsNone();
				}
				return false;
			}
		}

		public string Key => _key ?? (_key = PortalKey.Of(Id));

		public PortalRecord(ZDOID id)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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)
			Id = id;
			Name = string.Empty;
			Location = Vector3.zero;
			Target = ZDOID.None;
		}

		public PortalEntry ToCore()
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			return new PortalEntry(Key, Name, new Vec3(Location.x, Location.y, Location.z), HasTarget ? PortalKey.Of(Target) : null);
		}

		public ZPackage Pack()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: 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_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			ZPackage val = new ZPackage();
			val.Write(Id);
			val.Write(Name ?? string.Empty);
			val.Write(Location);
			val.Write(Target);
			val.Write(IsDefault);
			val.Write(ShowOnMap);
			return val;
		}

		public static PortalRecord FromPackage(ZPackage pkg)
		{
			//IL_0001: 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_001e: Unknown result type (might be due to invalid IL or missing references)
			//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)
			return new PortalRecord(pkg.ReadZDOID())
			{
				Name = pkg.ReadString(),
				Location = pkg.ReadVector3(),
				Target = pkg.ReadZDOID(),
				IsDefault = pkg.ReadBool(),
				ShowOnMap = pkg.ReadBool()
			};
		}
	}
	internal sealed class PortalRegistry
	{
		private readonly Dictionary<ZDOID, PortalRecord> _portals = new Dictionary<ZDOID, PortalRecord>();

		private readonly Dictionary<string, PortalRecord> _byKey = new Dictionary<string, PortalRecord>();

		public static PortalRegistry Instance { get; } = new PortalRegistry();

		public int Count => _portals.Count;

		private PortalRegistry()
		{
		}

		public bool Contains(ZDOID id)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			return _portals.ContainsKey(id);
		}

		public PortalRecord GetById(ZDOID id)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			if (!_portals.TryGetValue(id, out var value))
			{
				return null;
			}
			return value;
		}

		public PortalRecord GetByKey(string key)
		{
			if (key == null || !_byKey.TryGetValue(key, out var value))
			{
				return null;
			}
			return value;
		}

		public List<PortalRecord> GetList()
		{
			return _portals.Values.ToList();
		}

		public List<PortalRecord> GetPortalsWithTarget(ZDOID target)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			return _portals.Values.Where((PortalRecord p) => p.Target == target).ToList();
		}

		public PortalRecord AddOrUpdate(PortalRecord portal)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			_portals[portal.Id] = portal;
			_byKey[portal.Key] = portal;
			return portal;
		}

		public bool Remove(ZDOID id)
		{
			//IL_0006: 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)
			if (!_portals.TryGetValue(id, out var value))
			{
				return false;
			}
			_portals.Remove(id);
			_byKey.Remove(value.Key);
			return true;
		}

		public void Reset()
		{
			_portals.Clear();
			_byKey.Clear();
		}

		public IReadOnlyList<PortalEntry> BuildEntries(ZDOID exclude)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			List<PortalEntry> list = new List<PortalEntry>(_portals.Count);
			foreach (PortalRecord value in _portals.Values)
			{
				if (!(value.Id == exclude))
				{
					list.Add(value.ToCore());
				}
			}
			return list;
		}

		public ZPackage Pack()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Expected O, but got Unknown
			List<PortalRecord> list = GetList();
			ZPackage val = new ZPackage();
			val.Write(list.Count);
			foreach (PortalRecord item in list)
			{
				val.Write(item.Pack());
			}
			return val;
		}

		public void ApplyResync(ZPackage pkg)
		{
			int num = pkg.ReadInt();
			List<PortalRecord> list = new List<PortalRecord>(num);
			for (int i = 0; i < num; i++)
			{
				list.Add(PortalRecord.FromPackage(pkg.ReadPackage()));
			}
			ReplaceAll(list);
		}

		public void ApplyPortalZdos(IEnumerable<ZDO> zdos)
		{
			ReplaceAll(zdos.Select(PortalZdo.Read).ToList());
		}

		private void ReplaceAll(List<PortalRecord> incoming)
		{
			//IL_00a8: 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_00b0: Unknown result type (might be due to invalid IL or missing references)
			foreach (PortalRecord item in incoming)
			{
				AddOrUpdate(item);
			}
			HashSet<ZDOID> incomingIds = new HashSet<ZDOID>(incoming.Select((PortalRecord p) => p.Id));
			foreach (ZDOID item2 in _portals.Keys.Where((ZDOID id) => !incomingIds.Contains(id)).ToList())
			{
				Remove(item2);
			}
		}
	}
	internal static class PortalRpc
	{
		public static void Register()
		{
			ZRoutedRpc instance = ZRoutedRpc.instance;
			instance.Register<string>("RossPortals_SyncRequest", (Action<long, string>)OnSyncRequest);
			instance.Register<ZPackage>("RossPortals_AddOrUpdate", (Action<long, ZPackage>)OnAddOrUpdate);
			instance.Register<ZDOID>("RossPortals_Remove", (Action<long, ZDOID>)OnRemove);
			instance.Register<ZPackage>("RossPortals_SyncPortal", (Action<long, ZPackage>)OnPortalSynced);
			instance.Register<ZPackage, string>("RossPortals_Resync", (Action<long, ZPackage, string>)OnResync);
		}

		public static void RequestSync(string reason)
		{
			ZRoutedRpc.instance.InvokeRoutedRPC(Env.ServerPeerId, "RossPortals_SyncRequest", new object[1] { reason });
		}

		public static void RequestAddOrUpdate(PortalRecord record)
		{
			ZRoutedRpc.instance.InvokeRoutedRPC(Env.ServerPeerId, "RossPortals_AddOrUpdate", new object[1] { record.Pack() });
		}

		public static void RequestRemove(ZDOID id)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			ZRoutedRpc.instance.InvokeRoutedRPC(Env.ServerPeerId, "RossPortals_Remove", new object[1] { id });
		}

		public static void BroadcastPortal(PortalRecord record)
		{
			if (!((Object)(object)ZNet.instance == (Object)null) && ZNet.instance.GetConnectedPeers().Count != 0)
			{
				ZRoutedRpc.instance.InvokeRoutedRPC(0L, "RossPortals_SyncPortal", new object[1] { record.Pack() });
			}
		}

		public static void SendResync(long peer, ZPackage pkg, string reason)
		{
			ZRoutedRpc.instance.InvokeRoutedRPC(peer, "RossPortals_Resync", new object[2] { pkg, reason });
		}

		public static void BroadcastResync(ZPackage pkg, string reason)
		{
			if (!((Object)(object)ZNet.instance == (Object)null) && ZNet.instance.GetConnectedPeers().Count != 0)
			{
				ZRoutedRpc.instance.InvokeRoutedRPC(0L, "RossPortals_Resync", new object[2] { pkg, reason });
			}
		}

		private static void OnSyncRequest(long sender, string reason)
		{
			if (Env.IsServer)
			{
				PortalManager.ProcessSyncRequest(sender, reason);
			}
		}

		private static void OnAddOrUpdate(long sender, ZPackage pkg)
		{
			if (Env.IsServer)
			{
				PortalManager.ServerAddOrUpdate(PortalRecord.FromPackage(pkg));
			}
		}

		private static void OnRemove(long sender, ZDOID id)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			if (Env.IsServer)
			{
				PortalManager.ServerRemove(id);
			}
		}

		private static void OnPortalSynced(long sender, ZPackage pkg)
		{
			if (!Env.IsServer)
			{
				PortalRegistry.Instance.AddOrUpdate(PortalRecord.FromPackage(pkg));
				PortalManager.NotifyListChanged();
			}
		}

		private static void OnResync(long sender, ZPackage pkg, string reason)
		{
			if (!Env.IsServer)
			{
				JoinSync.ListReceived();
				PortalRegistry.Instance.ApplyResync(pkg);
				RossPortalsPlugin.Log.LogDebug((object)$"Portal resync ({reason}): now know {PortalRegistry.Instance.Count} portal(s).");
				PortalManager.NotifyListChanged();
			}
		}
	}
	internal static class PortalZdo
	{
		private const string NoTarget = "none";

		public static string GetName(ZDO zdo)
		{
			return zdo.GetString(ZDOVars.s_tag, "");
		}

		public static void SetName(ZDO zdo, string name)
		{
			zdo.Set(ZDOVars.s_tag, name ?? string.Empty);
		}

		public static ZDOID GetTarget(ZDO zdo)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: 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_0024: Unknown result type (might be due to invalid IL or missing references)
			ZDOID zDOID = zdo.GetZDOID("RossPortals_TargetId");
			if (zDOID == ZDOID.None)
			{
				zDOID = zdo.GetZDOID("XPortal_TargetId");
			}
			return zDOID;
		}

		public static ZDOID GetPrevious(ZDO zdo)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: 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_0024: Unknown result type (might be due to invalid IL or missing references)
			ZDOID zDOID = zdo.GetZDOID("RossPortals_PreviousId");
			if (zDOID == ZDOID.None)
			{
				zDOID = zdo.GetZDOID("XPortal_PreviousId");
			}
			return zDOID;
		}

		public static PortalRecord Read(ZDO zdo)
		{
			//IL_0001: 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_0027: 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)
			return new PortalRecord(zdo.m_uid)
			{
				Name = (GetName(zdo) ?? string.Empty),
				Location = zdo.GetPosition(),
				Target = GetTarget(zdo),
				IsDefault = zdo.GetBool("RossPortals_Default", false),
				ShowOnMap = zdo.GetBool("RossPortals_ShowOnMap", true)
			};
		}

		public static void Write(PortalRecord record)
		{
			//IL_0006: 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_0061: 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)
			ZDO zDO = ZDOMan.instance.GetZDO(record.Id);
			if (zDO != null)
			{
				zDO.SetOwner(ZDOMan.GetSessionID());
				SetName(zDO, record.Name);
				zDO.Set("RossPortals_PreviousId", zDO.m_uid);
				zDO.Set("RossPortals_Default", record.IsDefault);
				zDO.Set("RossPortals_ShowOnMap", record.ShowOnMap);
				SetTarget(zDO, record.Target);
				RecordStableTarget(zDO, record.Target);
				RetireLegacyTarget(zDO);
			}
		}

		private static void SetTarget(ZDO zdo, ZDOID target)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			zdo.Set("RossPortals_TargetId", target);
			zdo.SetConnection((ConnectionType)1, target);
		}

		public static void RestoreConnections()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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_004c: 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_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			List<ZDO> portalList = ZDOMan.instance.GetPortalList();
			if (portalList == null || portalList.Count == 0)
			{
				return;
			}
			Dictionary<ZDOID, ZDOID> dictionary = new Dictionary<ZDOID, ZDOID>();
			foreach (ZDO item in portalList)
			{
				ZDOID previous = GetPrevious(item);
				if (previous != ZDOID.None)
				{
					dictionary[previous] = item.m_uid;
				}
			}
			Dictionary<string, ZDO> dictionary2 = new Dictionary<string, ZDO>();
			Dictionary<ZDOID, string> dictionary3 = new Dictionary<ZDOID, string>();
			foreach (ZDO item2 in portalList)
			{
				string text = item2.GetString("RossPortalsPatch_Uid", "");
				if (string.IsNullOrEmpty(text) || dictionary2.ContainsKey(text))
				{
					text = NewUid();
					Claim(item2);
					item2.Set("RossPortalsPatch_Uid", text);
				}
				dictionary2[text] = item2;
				dictionary3[item2.m_uid] = text;
			}
			foreach (ZDO item3 in portalList)
			{
				ZDOID val = ResolveTarget(item3, dictionary2, dictionary3, dictionary);
				if (item3.GetConnectionZDOID((ConnectionType)1) != val || item3.GetZDOID("RossPortals_TargetId") != val)
				{
					Claim(item3);
					SetTarget(item3, val);
				}
				RetireLegacyTarget(item3);
			}
			foreach (ZDO item4 in portalList)
			{
				if (!(item4.GetZDOID("RossPortals_PreviousId") == item4.m_uid))
				{
					Claim(item4);
					item4.Set("RossPortals_PreviousId", item4.m_uid);
				}
			}
		}

		private static ZDOID ResolveTarget(ZDO zdo, Dictionary<string, ZDO> byUid, Dictionary<ZDOID, string> uidOf, Dictionary<ZDOID, ZDOID> byPreviousId)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: 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_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: 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_0075: Unknown result type (might be due to invalid IL or missing references)
			string text = zdo.GetString("RossPortalsPatch_TargetUid", "");
			if (text == "none")
			{
				return ZDOID.None;
			}
			if (!string.IsNullOrEmpty(text))
			{
				if (!byUid.TryGetValue(text, out var value))
				{
					return ZDOID.None;
				}
				return value.m_uid;
			}
			ZDOID val = StoredTarget(zdo);
			if (val != ZDOID.None && byPreviousId.TryGetValue(val, out var value2))
			{
				zdo.Set("RossPortalsPatch_TargetUid", uidOf[value2]);
				return value2;
			}
			zdo.Set("RossPortalsPatch_TargetUid", "none");
			return ZDOID.None;
		}

		private static ZDOID StoredTarget(ZDO zdo)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_000d: 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_0026: 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_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			ZDOID zDOID = zdo.GetZDOID("RossPortals_TargetId");
			if (zDOID != ZDOID.None)
			{
				return zDOID;
			}
			if (zdo.GetZDOID("RossPortals_PreviousId") != ZDOID.None)
			{
				return ZDOID.None;
			}
			return zdo.GetZDOID("XPortal_TargetId");
		}

		private static void RecordStableTarget(ZDO zdo, ZDOID target)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: 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)
			EnsureUid(zdo);
			if (target == ZDOID.None)
			{
				zdo.Set("RossPortalsPatch_TargetUid", "none");
				return;
			}
			ZDO zDO = ZDOMan.instance.GetZDO(target);
			zdo.Set("RossPortalsPatch_TargetUid", (zDO != null) ? EnsureUid(zDO) : string.Empty);
		}

		private static void RetireLegacyTarget(ZDO zdo)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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)
			if (!(zdo.GetZDOID("XPortal_TargetId") == ZDOID.None))
			{
				Claim(zdo);
				zdo.Set("XPortal_TargetId", ZDOID.None);
			}
		}

		private static string EnsureUid(ZDO zdo)
		{
			string text = zdo.GetString("RossPortalsPatch_Uid", "");
			if (!string.IsNullOrEmpty(text))
			{
				return text;
			}
			text = NewUid();
			Claim(zdo);
			zdo.Set("RossPortalsPatch_Uid", text);
			return text;
		}

		private static string NewUid()
		{
			return Guid.NewGuid().ToString("N");
		}

		private static void Claim(ZDO zdo)
		{
			long sessionID = ZDOMan.GetSessionID();
			if (zdo.GetOwner() != sessionID)
			{
				zdo.SetOwner(sessionID);
			}
		}
	}
	internal sealed class Scheduler : MonoBehaviour
	{
		public static Scheduler Instance { get; private set; }

		private void Awake()
		{
			Instance = this;
		}

		private void OnDestroy()
		{
			if ((Object)(object)Instance == (Object)(object)this)
			{
				Instance = null;
			}
		}

		public void NextFrame(Action action)
		{
			AfterFrames(1, action);
		}

		public void AfterFrames(int frames, Action action)
		{
			if (action != null)
			{
				if (!((Behaviour)this).isActiveAndEnabled)
				{
					action();
				}
				else
				{
					((MonoBehaviour)this).StartCoroutine(Run(frames, action));
				}
			}
		}

		private static IEnumerator Run(int frames, Action action)
		{
			for (int i = 0; i < frames; i++)
			{
				yield return null;
			}
			action();
		}
	}
}
namespace RossPortals.Game.Patches
{
	[HarmonyPatch(typeof(Game), "Awake")]
	internal static class Game_Awake
	{
		private static void Prefix(ref bool ___isModded)
		{
			___isModded = true;
		}
	}
	[HarmonyPatch(typeof(Game), "Start")]
	internal static class Game_Start
	{
		private static void Postfix()
		{
			Env.GameStarted = true;
			PortalManager.OnGameStarted();
		}
	}
	[HarmonyPatch(typeof(Game), "ConnectPortals")]
	internal static class Game_ConnectPortals
	{
		private static bool Prepare()
		{
			return ValheimCompat.RequireMethod(typeof(Game), "ConnectPortals", "portal destinations");
		}

		private static bool Prefix()
		{
			return false;
		}
	}
	[HarmonyPatch(typeof(Game), "ConnectPortalsCoroutine")]
	internal static class Game_ConnectPortalsCoroutine
	{
		private static bool Prepare()
		{
			return ValheimCompat.RequireMethod(typeof(Game), "ConnectPortalsCoroutine", "portal destinations");
		}

		private static bool Prefix()
		{
			return false;
		}
	}
	[HarmonyPatch(typeof(Piece), "SetCreator")]
	internal static class Piece_SetCreator
	{
		private static void Postfix(Piece __instance)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)__instance == (Object)null) && __instance.m_name != null && __instance.m_name.Contains("$piece_portal"))
			{
				ZNetView nview = __instance.m_nview;
				ZDO val = (((Object)(object)nview != (Object)null) ? nview.GetZDO() : null);
				if (val != null)
				{
					PortalManager.OnPortalPlaced(val.m_uid, val.GetPosition());
				}
			}
		}
	}
	[HarmonyPatch(typeof(TeleportWorld), "GetHoverText")]
	internal static class TeleportWorld_GetHoverText
	{
		private static bool Prefix(TeleportWorld __instance, ref string __result)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			ZNetView nview = __instance.m_nview;
			if (Env.ShuttingDown || (Object)(object)nview == (Object)null || nview.GetZDO() == null)
			{
				__result = string.Empty;
				return false;
			}
			ZDO zDO = nview.GetZDO();
			__result = PortalManager.BuildHoverText(zDO.m_uid, zDO.GetPosition());
			return false;
		}
	}
	[HarmonyPatch(typeof(TeleportWorld), "Teleport")]
	internal static class TeleportWorld_Teleport
	{
		private static void Postfix(TeleportWorld __instance, Player player)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)player == (Object)null) && !((Object)(object)player != (Object)(object)Player.m_localPlayer) && ((Character)player).IsTeleporting())
			{
				ZNetView nview = __instance.m_nview;
				ZDO val = (((Object)(object)nview != (Object)null) ? nview.GetZDO() : null);
				if (val != null)
				{
					PortalManager.RecordUsed(val.GetConnectionZDOID((ConnectionType)1));
				}
			}
		}
	}
	[HarmonyPatch(typeof(TextInput), "RequestText")]
	internal static class TextInput_RequestText
	{
		private static bool Prefix(TextReceiver sign)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			TeleportWorld val = (TeleportWorld)(object)((sign is TeleportWorld) ? sign : null);
			if (val != null)
			{
				ZNetView nview = val.m_nview;
				ZDO val2 = (((Object)(object)nview != (Object)null) ? nview.GetZDO() : null);
				if (val2 != null)
				{
					PortalManager.OnPortalInteract(val2.m_uid);
				}
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(WearNTear), "Destroy")]
	internal static class WearNTear_Destroy
	{
		private static void Prefix(WearNTear __instance)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			Piece val = (((Object)(object)__instance != (Object)null) ? __instance.m_piece : null);
			if (!((Object)(object)val == (Object)null) && val.m_name != null && val.m_name.Contains("$piece_portal") && val.CanBeRemoved())
			{
				ZNetView nview = val.m_nview;
				ZDO val2 = (((Object)(object)nview != (Object)null) ? nview.GetZDO() : null);
				if (val2 != null)
				{
					PortalManager.OnPortalDestroyed(val2.m_uid);
				}
			}
		}
	}
	[HarmonyPatch(typeof(ZDOMan), "ConnectPortals")]
	internal static class ZDOMan_ConnectPortals
	{
		private static bool Prepare()
		{
			return ValheimCompat.RequireMethod(typeof(ZDOMan), "ConnectPortals", "portal destinations");
		}

		private static bool Prefix()
		{
			PortalZdo.RestoreConnections();
			return false;
		}
	}
}
namespace RossPortals.Game.Framework
{
	internal readonly struct CompatMember
	{
		public string Type { get; }

		public string Member { get; }

		public string Why { get; }

		public CompatMember(string type, string member, string why)
		{
			Type = type;
			Member = member;
			Why = why;
		}
	}
	internal static class ValheimCompat
	{
		private static readonly CompatMember[] Members = new CompatMember[23]
		{
			new CompatMember("TeleportWorld", "GetHoverText", "replace portal hover text"),
			new CompatMember("TeleportWorld", "Teleport", "note recently-used destinations"),
			new CompatMember("TeleportWorld", "m_nview", "reach a portal's ZDO"),
			new CompatMember("TextInput", "RequestText", "open our panel instead of the tag box"),
			new CompatMember("ZDOMan", "GetPortalList", "enumerate every portal (server)"),
			new CompatMember("ZDOMan", "ConnectPortals", "rebuild connections from stored destinations"),
			new CompatMember("ZDOMan", "GetZDO", "resolve a portal by id"),
			new CompatMember("ZDOMan", "GetSessionID", "own a portal ZDO before writing it"),
			new CompatMember("ZDOMan", "ForceSendZDO", "push a just-placed portal to the server"),
			new CompatMember("ZDO", "GetConnectionZDOID", "read a portal's live destination"),
			new CompatMember("ZDO", "SetConnection", "set the destination vanilla teleport reads"),
			new CompatMember("ZDO", "GetString", "read the portal tag/name"),
			new CompatMember("ZDO", "GetZDOID", "read stored destination/previous id"),
			new CompatMember("ZDO", "SetOwner", "own a portal ZDO before writing it"),
			new CompatMember("ZDO", "GetPosition", "a portal's world position"),
			new CompatMember("ZDOVars", "s_tag", "the vanilla portal-name ZDO key"),
			new CompatMember("ZRoutedRpc", "InvokeRoutedRPC", "sync the portal list"),
			new CompatMember("ZRoutedRpc", "Everybody", "broadcast to all peers"),
			new CompatMember("Piece", "SetCreator", "detect a placed portal"),
			new CompatMember("Piece", "CanBeRemoved", "confirm a portal is actually being removed"),
			new CompatMember("WearNTear", "Destroy", "detect a removed portal"),
			new CompatMember("Game", "ConnectPortals", "suppress vanilla tag pairing"),
			new CompatMember("Game", "ConnectPortalsCoroutine", "suppress vanilla tag pairing")
		};

		public static void Verify()
		{
			List<string> list = FindMissing(Members);
			if (list.Count == 0)
			{
				RossPortalsPlugin.Log.LogInfo((object)$"Valheim compatibility check: all {Members.Length} referenced members present.");
				return;
			}
			RossPortalsPlugin.Log.LogError((object)($"Valheim compatibility check: {list.Count} of {Members.Length} referenced members are MISSING. " + "The affected features are disabled. This usually means a Valheim update moved something:"));
			foreach (string item in list)
			{
				RossPortalsPlugin.Log.LogError((object)("  - " + item));
			}
		}

		public static List<string> FindMissing(IEnumerable<CompatMember> members)
		{
			List<string> list = new List<string>();
			if (members == null)
			{
				return lis