Decompiled source of TalentPace v1.4.17

Cjayride.TalentPace.dll

Decompiled 10 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("Cjayride.TalentPace")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.4.17.0")]
[assembly: AssemblyInformationalVersion("1.4.17+15344e0ba9bf8820fb4e0ee22cc1b9b0b4d70a87")]
[assembly: AssemblyProduct("Cjayride.TalentPace")]
[assembly: AssemblyTitle("Cjayride.TalentPace")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.4.17.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 Cjayride.TalentPace
{
	internal static class ActiveSkillRespec
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static UnityAction <>9__23_0;

			internal void <EnsureButton>b__23_0()
			{
				TryRefund(Player.m_localPlayer);
			}
		}

		internal static ConfigEntry<bool> EnableRespec;

		internal static ConfigEntry<bool> AutoRefund;

		private static Type _pointService;

		private static Type _store;

		private static Type _reset;

		private static Type _ui;

		private static MethodInfo _getTotal;

		private static MethodInfo _getSpent;

		private static MethodInfo _resetActives;

		private static MethodInfo _resetSpent;

		private static MethodInfo _refreshUi;

		private static FieldInfo _rebirthButton;

		private static bool _patched;

		private static Button _button;

		internal static void Bind(ConfigFile config)
		{
			EnableRespec = config.Bind<bool>("Skills Reworked UI", "Free Active Skill respec when over budget", true, "When spent Active Skill points exceed what the current SkillsReworked config grants, show a free respec on the skills window. Refunds Active Skills only (not Level, Prestige, or Talent Tree). No coin or level cost.");
			AutoRefund = config.Bind<bool>("Skills Reworked UI", "Auto refund over-budget Active Skills", true, "On spawn, if spent Active Skill points exceed the current budget, refund them automatically (same as the free respec button).");
		}

		internal static void Apply(Harmony harmony, Plugin plugin)
		{
			if ((EnableRespec.Value || AutoRefund.Value) && !PatchNow(harmony))
			{
				((MonoBehaviour)plugin).StartCoroutine(RetryPatch(harmony));
			}
		}

		private static IEnumerator RetryPatch(Harmony harmony)
		{
			for (int i = 0; i < 60; i++)
			{
				if (_patched)
				{
					break;
				}
				yield return (object)new WaitForSeconds(1f);
				PatchNow(harmony);
			}
		}

		private static bool PatchNow(Harmony harmony)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Expected O, but got Unknown
			if (_patched)
			{
				return true;
			}
			if (!EnsureReflection())
			{
				return false;
			}
			MethodInfo methodInfo = SR.Method(_ui, "RefreshRebirthResetButton");
			if (methodInfo != null && EnableRespec.Value)
			{
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(ActiveSkillRespec), "AfterRefreshButton", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			MethodInfo methodInfo2 = AccessTools.DeclaredMethod(typeof(Player), "OnSpawned", new Type[1] { typeof(bool) }, (Type[])null);
			if (methodInfo2 != null && AutoRefund.Value)
			{
				harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, new HarmonyMethod(typeof(ActiveSkillRespec), "AfterSpawned", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			_patched = true;
			Plugin.Log("Talent Pace: Active Skill over-budget respec hooked.");
			return true;
		}

		private static bool EnsureReflection()
		{
			if (_getTotal != null)
			{
				return true;
			}
			_pointService = SR.Type("SkillsReworked.Systems.Progression.SkillPointService");
			_store = SR.Type("SkillsReworked.Persistence.PlayerProgressionStore");
			_reset = SR.Type("SkillsReworked.Systems.Reset.ProgressionResetService");
			_ui = SR.Type("SkillsReworked.UI.SkillPanel.SkillUiController");
			if (_pointService == null || _store == null || _reset == null)
			{
				return false;
			}
			_getTotal = SR.Method(_pointService, "GetTotalPoints", typeof(Player));
			_getSpent = SR.Method(_store, "GetSpentSkillPoints", typeof(Player));
			_resetActives = SR.Method(_reset, "ResetActiveSkills", typeof(Skills));
			_resetSpent = SR.Method(_reset, "ResetSpentPoints", typeof(Player));
			_refreshUi = ((_ui == null) ? null : SR.Method(_ui, "RefreshAfterProgressionChange", typeof(Player)));
			_rebirthButton = ((_ui == null) ? null : SR.Field(_ui, "RebirthButton"));
			if (_getTotal != null && _getSpent != null && _resetActives != null)
			{
				return _resetSpent != null;
			}
			return false;
		}

		internal static bool NeedsRespec(Player player)
		{
			if ((Object)(object)player == (Object)null || !EnsureReflection())
			{
				return false;
			}
			try
			{
				int num = Convert.ToInt32(_getTotal.Invoke(null, new object[1] { player }));
				return Convert.ToInt32(_getSpent.Invoke(null, new object[1] { player })) > num;
			}
			catch (Exception)
			{
				return false;
			}
		}

		internal static bool TryRefund(Player player)
		{
			if ((Object)(object)player == (Object)null || !NeedsRespec(player))
			{
				return false;
			}
			Skills skills = ((Character)player).GetSkills();
			if ((Object)(object)skills == (Object)null)
			{
				return false;
			}
			try
			{
				_resetActives.Invoke(null, new object[1] { skills });
				_resetSpent.Invoke(null, new object[1] { player });
			}
			catch (Exception ex)
			{
				Plugin.Log("Talent Pace: Active Skill refund failed: " + ex.Message);
				return false;
			}
			try
			{
				if (_refreshUi != null)
				{
					_refreshUi.Invoke(null, new object[1] { player });
				}
			}
			catch (Exception)
			{
			}
			((Character)player).Message((MessageType)2, "Active Skill points refunded (over new budget). Spend them again.", 0, (Sprite)null, false);
			Plugin.Log("Talent Pace: refunded over-budget Active Skills for " + player.GetPlayerName());
			return true;
		}

		private static void AfterSpawned(Player __instance, bool spawnValkyrie)
		{
			if (AutoRefund.Value && !((Object)(object)__instance == (Object)null))
			{
				if ((Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsServer())
				{
					TryRefund(__instance);
				}
				else if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer)
				{
					TryRefund(__instance);
				}
			}
		}

		private static void AfterRefreshButton()
		{
			if (!EnableRespec.Value || _rebirthButton == null)
			{
				return;
			}
			object? value = _rebirthButton.GetValue(null);
			Button val = (Button)((value is Button) ? value : null);
			if (!((Object)(object)val == (Object)null))
			{
				Player localPlayer = Player.m_localPlayer;
				bool active = (Object)(object)localPlayer != (Object)null && NeedsRespec(localPlayer);
				EnsureButton(((Component)val).transform.parent);
				if ((Object)(object)_button != (Object)null)
				{
					((Component)_button).gameObject.SetActive(active);
				}
			}
		}

		private static void EnsureButton(Transform parent)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_0067: 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_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: 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_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Expected O, but got Unknown
			if ((Object)(object)parent == (Object)null)
			{
				return;
			}
			Transform val = parent.Find("TalentPace_ActiveRespec");
			if ((Object)(object)val != (Object)null)
			{
				_button = ((Component)val).GetComponent<Button>();
				return;
			}
			GameObject val2 = new GameObject("TalentPace_ActiveRespec", new Type[1] { typeof(RectTransform) });
			val2.transform.SetParent(parent, false);
			RectTransform component = val2.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0.5f, 0f);
			component.anchorMax = new Vector2(0.5f, 0f);
			component.pivot = new Vector2(0.5f, 0f);
			component.anchoredPosition = new Vector2(0f, 8f);
			component.sizeDelta = new Vector2(220f, 28f);
			Image val3 = val2.AddComponent<Image>();
			((Graphic)val3).color = new Color(0.15f, 0.35f, 0.2f, 0.92f);
			Button val4 = val2.AddComponent<Button>();
			((Selectable)val4).targetGraphic = (Graphic)(object)val3;
			ButtonClickedEvent onClick = val4.onClick;
			object obj = <>c.<>9__23_0;
			if (obj == null)
			{
				UnityAction val5 = delegate
				{
					TryRefund(Player.m_localPlayer);
				};
				<>c.<>9__23_0 = val5;
				obj = (object)val5;
			}
			((UnityEvent)onClick).AddListener((UnityAction)obj);
			GameObject val6 = new GameObject("Text", new Type[1] { typeof(RectTransform) });
			val6.transform.SetParent(val2.transform, false);
			RectTransform component2 = val6.GetComponent<RectTransform>();
			component2.anchorMin = Vector2.zero;
			component2.anchorMax = Vector2.one;
			component2.offsetMin = Vector2.zero;
			component2.offsetMax = Vector2.zero;
			Text obj2 = val6.AddComponent<Text>();
			obj2.text = "Free respec (over Active points)";
			obj2.alignment = (TextAnchor)4;
			obj2.fontSize = 12;
			((Graphic)obj2).color = Color.white;
			obj2.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
			_button = val4;
		}
	}
	internal static class BlockTooltipPatch
	{
		internal static void Apply(Harmony harmony)
		{
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: 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_00e6: Expected O, but got Unknown
			Type typeFromHandle = typeof(ItemData);
			foreach (MethodInfo declaredMethod in AccessTools.GetDeclaredMethods(typeFromHandle))
			{
				if (!(declaredMethod.Name != "GetTooltip") && declaredMethod.IsStatic && !(declaredMethod.ReturnType != typeof(string)))
				{
					ParameterInfo[] parameters = declaredMethod.GetParameters();
					if ((parameters.Length == 3 || parameters.Length == 6) && !(parameters[0].ParameterType != typeFromHandle) && !(parameters[1].ParameterType != typeof(int)) && !(parameters[2].ParameterType != typeof(bool)))
					{
						harmony.Patch((MethodBase)declaredMethod, (HarmonyMethod)null, new HarmonyMethod(typeof(BlockTooltipPatch), "TooltipPostfix", (Type[])null)
						{
							priority = 0
						}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					}
				}
			}
			Type type = AccessTools.TypeByName("EpicLoot.AugaTooltip");
			MethodInfo methodInfo = ((type == null) ? null : AccessTools.Method(type, "PreprocessTooltipStat", (Type[])null, (Type[])null));
			if (methodInfo != null)
			{
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(BlockTooltipPatch), "AugaPostfix", (Type[])null)
				{
					priority = 0
				}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		private static void TooltipPostfix(object[] __args, ref string __result)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Invalid comparison between Unknown and I4
			if (Plugin.RestoreVanillaBlockTooltip.Value && __args != null && __args.Length >= 3 && Convert.ToBoolean(__args[2]))
			{
				object obj = __args[0];
				ItemData val = (ItemData)((obj is ItemData) ? obj : null);
				if (val?.m_shared != null && (int)val.m_shared.m_itemType == 5)
				{
					Restore(ref __result, val, (int)__args[1]);
				}
			}
		}

		private static void AugaPostfix(ItemData item, string label, ref Tuple<string, string> __result)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Invalid comparison between Unknown and I4
			if (Plugin.RestoreVanillaBlockTooltip.Value && item?.m_shared != null && (int)item.m_shared.m_itemType == 5 && __result != null)
			{
				string text = label + " " + __result.Item1;
				if (text.IndexOf("block", StringComparison.OrdinalIgnoreCase) >= 0 || text.IndexOf("$item_blockarmor", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					string item2 = item.GetBaseBlockPower(item.m_quality).ToString("0");
					__result = Tuple.Create(__result.Item1, item2);
				}
			}
		}

		private static void Restore(ref string tooltip, ItemData item, int quality)
		{
			if (!string.IsNullOrEmpty(tooltip))
			{
				string text = item.GetBaseBlockPower(quality).ToString("0");
				string text2 = item.m_shared.m_deflectionForce.ToString("0");
				tooltip = Regex.Replace(tooltip, "\\$item_blockarmor:.*", "$item_blockarmor: <color=orange>" + text + "</color> <color=yellow>(" + text + ")</color>");
				tooltip = Regex.Replace(tooltip, "\\$item_blockforce:.*", "$item_blockforce: <color=orange>" + text2 + "</color>");
			}
		}
	}
	internal static class CapHint
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static UnityAction <0>__OnTalentEffectsClicked;

			public static UnityAction <1>__OnShardstoneEffectsClicked;

			public static UnityAction <2>__OnFanfareTestClicked;

			public static UnityAction <3>__OnFreeRespecClicked;
		}

		private const string OverlayName = "TalentPaceOverlay";

		private const string ClickLayerName = "TalentPaceClicks";

		private const string LabelName = "TalentPaceCap";

		private const string FreeRespecButtonName = "TalentPaceFreeRespec";

		private const string TalentEffectsButtonName = "TalentPaceTalentEffects";

		private const string ShardstoneEffectsButtonName = "TalentPaceShardstoneEffects";

		private const string FanfareTestButtonName = "TalentPaceFanfareTest";

		private const float RefreshInterval = 0.25f;

		private const float ButtonWidth = 160f;

		private const float ButtonHeight = 28f;

		private const float SideMargin = 10f;

		private const float Gap = 18f;

		private const int OverlayOrder = 20;

		private static FieldInfo _view;

		private static FieldInfo _statsPanel;

		private static FieldInfo _buildsPanel;

		private static FieldInfo _confirmDialog;

		private static FieldInfo _statsOpen;

		private static PropertyInfo _statsRoot;

		private static PropertyInfo _buildsRoot;

		private static PropertyInfo _isBuildsOpen;

		private static PropertyInfo _textDialogIsOpen;

		private static PropertyInfo _textDialogPanel;

		private static PropertyInfo _confirmIsVisible;

		private static PropertyInfo _confirmRoot;

		private static PropertyInfo _titleText;

		private static PropertyInfo _pointsText;

		private static PropertyInfo _panelRect;

		private static MethodInfo _getRuntime;

		private static MethodInfo _totalPoints;

		private static MethodInfo _windowRefresh;

		private static float _nextSample;

		private static bool _logged;

		private static RectTransform _overlay;

		private static RectTransform _clickLayer;

		private static Text _label;

		private static Button _freeRespecButton;

		private static Button _talentEffectsButton;

		private static Button _shardstoneEffectsButton;

		private static Button _fanfareTestButton;

		private static Component _anchorComponent;

		private static Transform _statsHomeParent;

		private static int _statsHomeSibling = -1;

		private static Transform _buildsHomeParent;

		private static int _buildsHomeSibling = -1;

		private static Text _titleFontSource;

		private static string _lastSignature;

		private static int _lastColorMark = -1;

		private static readonly object[] _oneArg = new object[1];

		private static readonly object[] _twoArg = new object[2];

		private static readonly string[] DefaultReadFontNames = new string[4] { "Verdana", "Segoe UI", "Arial", "Tahoma" };

		internal static ConfigEntry<string> FontNames;

		internal static ConfigEntry<FontStyle> FontStyle;

		internal static ConfigEntry<int> FontSize;

		internal static ConfigEntry<bool> ListInstalledFonts;

		private static Font _readFont;

		private static string _readFontKey;

		private static bool _loggedFont;

		private static Font _statsFont;

		private static Font _titleFont;

		public static void Apply(Harmony harmony)
		{
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: Expected O, but got Unknown
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fd: Expected O, but got Unknown
			//IL_0281: Unknown result type (might be due to invalid IL or missing references)
			//IL_028e: Expected O, but got Unknown
			//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d5: Expected O, but got Unknown
			//IL_030f: Unknown result type (might be due to invalid IL or missing references)
			//IL_031c: Expected O, but got Unknown
			//IL_042d: Unknown result type (might be due to invalid IL or missing references)
			//IL_043a: Expected O, but got Unknown
			//IL_0468: Unknown result type (might be due to invalid IL or missing references)
			//IL_0475: Expected O, but got Unknown
			//IL_04a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b0: Expected O, but got Unknown
			if (ListInstalledFonts != null && ListInstalledFonts.Value)
			{
				LogAvailableFonts();
			}
			Type type = TT.Type("UI.TalentWindow");
			MethodInfo methodInfo = ((type == null) ? null : AccessTools.DeclaredMethod(type, "Refresh", (Type[])null, (Type[])null));
			if (methodInfo == null)
			{
				Plugin.Log("Talent window Refresh was not found. Cap text and effect toggles will not appear.");
				return;
			}
			Plugin.Log("Talent window Refresh hooked (" + methodInfo.DeclaringType.FullName + ").");
			_windowRefresh = methodInfo;
			_view = AccessTools.Field(type, "_view");
			_statsPanel = AccessTools.DeclaredField(type, "_statsPanel");
			_buildsPanel = AccessTools.DeclaredField(type, "_buildsPanel");
			_confirmDialog = AccessTools.DeclaredField(type, "_confirmDialog");
			_statsOpen = AccessTools.DeclaredField(type, "_statsOpen");
			_isBuildsOpen = AccessTools.Property(type, "IsBuildsPanelOpen");
			Type type2 = TT.Type("UI.StatsPanelView");
			_statsRoot = ((type2 == null) ? null : AccessTools.Property(type2, "Root"));
			Type type3 = TT.Type("UI.BuildsPanelView");
			_buildsRoot = ((type3 == null) ? null : AccessTools.Property(type3, "Root"));
			Type type4 = TT.Type("UI.ValheimTextDialog");
			_textDialogIsOpen = ((type4 == null) ? null : AccessTools.Property(type4, "IsOpen"));
			_textDialogPanel = ((type4 == null) ? null : AccessTools.Property(type4, "Panel"));
			MethodInfo methodInfo2 = ((type4 == null) ? null : AccessTools.DeclaredMethod(type4, "Request", (Type[])null, (Type[])null));
			if (methodInfo2 != null)
			{
				harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, new HarmonyMethod(typeof(CapHint), "AfterAuxShow", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			MethodInfo methodInfo3 = ((type4 == null) ? null : AccessTools.DeclaredMethod(type4, "Tick", (Type[])null, (Type[])null));
			if (methodInfo3 != null)
			{
				harmony.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, new HarmonyMethod(typeof(CapHint), "AfterAuxShow", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			Type type5 = TT.Type("UI.TalentConfirmDialog");
			_confirmIsVisible = ((type5 == null) ? null : AccessTools.Property(type5, "IsVisible"));
			_confirmRoot = ((type5 == null) ? null : AccessTools.Property(type5, "Root"));
			MethodInfo methodInfo4 = ((type5 == null) ? null : AccessTools.DeclaredMethod(type5, "Show", (Type[])null, (Type[])null));
			if (methodInfo4 != null)
			{
				harmony.Patch((MethodBase)methodInfo4, (HarmonyMethod)null, new HarmonyMethod(typeof(CapHint), "AfterAuxShow", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			MethodInfo methodInfo5 = ((type2 == null) ? null : AccessTools.DeclaredMethod(type2, "SetVisible", (Type[])null, (Type[])null));
			if (methodInfo5 != null)
			{
				harmony.Patch((MethodBase)methodInfo5, (HarmonyMethod)null, new HarmonyMethod(typeof(CapHint), "AfterStatsShow", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			MethodInfo methodInfo6 = ((type3 == null) ? null : AccessTools.DeclaredMethod(type3, "SetVisible", (Type[])null, (Type[])null));
			if (methodInfo6 != null)
			{
				harmony.Patch((MethodBase)methodInfo6, (HarmonyMethod)null, new HarmonyMethod(typeof(CapHint), "AfterStatsShow", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			Type type6 = TT.Type("UI.TalentWindowView");
			_titleText = ((type6 == null) ? null : AccessTools.Property(type6, "TitleText"));
			_pointsText = ((type6 == null) ? null : AccessTools.Property(type6, "PointsText"));
			_panelRect = ((type6 == null) ? null : AccessTools.Property(type6, "PanelRect"));
			Type type7 = TT.Type("Core.TalentRuntimeService");
			Type type8 = TT.Type("Core.TalentRuntime");
			Type type9 = TT.Type("Core.TalentPointBudgetService");
			_getRuntime = ((type7 == null) ? null : AccessTools.DeclaredMethod(type7, "Get", new Type[1] { typeof(Player) }, (Type[])null));
			_totalPoints = ((type9 == null || type8 == null) ? null : AccessTools.DeclaredMethod(type9, "GetTotalPoints", new Type[2]
			{
				typeof(Player),
				type8
			}, (Type[])null));
			harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(CapHint), "AfterRefresh", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			MethodInfo methodInfo7 = AccessTools.DeclaredMethod(type, "Toggle", (Type[])null, (Type[])null);
			if (methodInfo7 != null)
			{
				harmony.Patch((MethodBase)methodInfo7, (HarmonyMethod)null, new HarmonyMethod(typeof(CapHint), "AfterRefresh", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			MethodInfo methodInfo8 = AccessTools.DeclaredMethod(type, "ApplyZOrder", (Type[])null, (Type[])null);
			if (methodInfo8 != null)
			{
				harmony.Patch((MethodBase)methodInfo8, (HarmonyMethod)null, new HarmonyMethod(typeof(CapHint), "AfterApplyZOrder", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		private static void AfterApplyZOrder()
		{
			EnsureOverlayOrder();
			EnsureStatsOnTop();
		}

		private static void AfterRefresh()
		{
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_022e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02da: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0428: Unknown result type (might be due to invalid IL or missing references)
			//IL_043e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0454: Unknown result type (might be due to invalid IL or missing references)
			//IL_0464: Unknown result type (might be due to invalid IL or missing references)
			//IL_0484: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_04ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_04dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_04e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_04e8: Expected O, but got Unknown
			//IL_0526: Unknown result type (might be due to invalid IL or missing references)
			//IL_053a: Unknown result type (might be due to invalid IL or missing references)
			//IL_053f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0545: Expected O, but got Unknown
			//IL_056c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0580: Unknown result type (might be due to invalid IL or missing references)
			//IL_0585: Unknown result type (might be due to invalid IL or missing references)
			//IL_058b: Expected O, but got Unknown
			//IL_05ac: Unknown result type (might be due to invalid IL or missing references)
			RectTransform val = Host();
			if ((Object)(object)val != (Object)null && !((Component)val).gameObject.activeSelf)
			{
				((Component)val).gameObject.SetActive(true);
			}
			if (Time.unscaledTime < _nextSample && (Object)(object)_label != (Object)null)
			{
				EnsureStatsOnTop();
				return;
			}
			_nextSample = Time.unscaledTime + 0.25f;
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			RectTransform val2 = Anchor();
			val = Host();
			if ((Object)(object)val2 == (Object)null || (Object)(object)val == (Object)null || !((Component)val2).gameObject.activeInHierarchy)
			{
				if (!_logged)
				{
					_logged = true;
					Plugin.Log("Talent cap overlay skipped: points label=" + ((Object)(object)val2 != (Object)null) + " overlay=" + ((Object)(object)val != (Object)null) + ".");
				}
				return;
			}
			Sample(localPlayer);
			string text = Pace.Hint(localPlayer);
			if (string.IsNullOrEmpty(text))
			{
				text = "No talent cap.";
			}
			bool flag = Pace.NeedsFreeRespec(localPlayer);
			bool talentEffectsOn = EffectToggles.TalentEffectsOn;
			bool shardstoneEffectsOn = EffectToggles.ShardstoneEffectsOn;
			bool epicLootPresent = EffectToggles.EpicLootPresent;
			bool showTestButtonOn = SkillsLevelUpFanfare.ShowTestButtonOn;
			int num = ((!flag) ? 1 : 2);
			Transform parent = ((Transform)_overlay).parent;
			Vector2 val3 = BottomLeftIn(val2, (RectTransform)(object)((parent is RectTransform) ? parent : null));
			string text2 = text + "|" + flag + "|" + talentEffectsOn + "|" + shardstoneEffectsOn + "|" + epicLootPresent + "|" + showTestButtonOn + "|" + val3.x.ToString("0.0") + "|" + val3.y.ToString("0.0") + "|" + ((FontNames == null) ? "" : FontNames.Value) + "|" + ((FontStyle == null) ? "" : ((object)FontStyle.Value/*cast due to .constrained prefix*/).ToString()) + "|" + ((FontSize == null) ? "0" : FontSize.Value.ToString());
			if (!(text2 == _lastSignature) || !((Object)(object)_label != (Object)null) || !((Component)_label).gameObject.activeInHierarchy)
			{
				_lastSignature = text2;
				Text val4 = EnsureLabel();
				ApplyWindowFont(val4);
				Rect val5 = OverlayLayoutBounds();
				float num2 = LabelWidth(val2, val, val5);
				Vector2 val6 = PanelLocalToOverlay(new Vector2(val3.x, val3.y - 18f));
				float num3 = val6.y;
				float num4 = val6.x;
				if (num4 < ((Rect)(ref val5)).xMin + 1f || num4 > ((Rect)(ref val5)).xMax - 80f || num3 > ((Rect)(ref val5)).yMax - 8f || num3 < ((Rect)(ref val5)).yMin + 40f)
				{
					num4 = ((Rect)(ref val5)).xMin + 10f;
					num3 = ((Rect)(ref val5)).yMax - 72f;
				}
				num4 = Mathf.Max(((Rect)(ref val5)).xMin + 10f, num4);
				val4.text = text;
				if (num != _lastColorMark)
				{
					_lastColorMark = num;
					((Graphic)val4).color = (flag ? new Color(1f, 0.45f, 0.35f, 1f) : new Color(1f, 0.82f, 0.35f, 1f));
				}
				RectTransform component = ((Component)val4).GetComponent<RectTransform>();
				float num5 = LabelHeight(val4);
				float num6 = (float)(1 + (epicLootPresent ? 1 : 0) + (showTestButtonOn ? 1 : 0) + (flag ? 1 : 0)) * 36f;
				float num7 = ((Rect)(ref val5)).yMin + 10f + num5 + num6;
				if (num3 < num7)
				{
					num3 = num7;
				}
				component.anchorMin = new Vector2(0f, 1f);
				component.anchorMax = new Vector2(0f, 1f);
				component.pivot = new Vector2(0f, 1f);
				component.sizeDelta = new Vector2(num2, num5);
				component.anchoredPosition = new Vector2(num4 - ((Rect)(ref val5)).xMin, num3 - ((Rect)(ref val5)).yMax);
				float num8 = num3 - num5 - 8f;
				string caption = "Talent Effects [" + (talentEffectsOn ? "enabled" : "disabled") + "]";
				bool enabledLook = talentEffectsOn;
				float x = num4;
				float y = num8;
				Rect bounds = val5;
				object obj = <>O.<0>__OnTalentEffectsClicked;
				if (obj == null)
				{
					UnityAction val7 = OnTalentEffectsClicked;
					<>O.<0>__OnTalentEffectsClicked = val7;
					obj = (object)val7;
				}
				EnsureToggleButton("TalentPaceTalentEffects", ref _talentEffectsButton, caption, enabledLook, x, y, bounds, visible: true, (UnityAction)obj);
				num8 -= 36f;
				string caption2 = "Shardstone Effects [" + (shardstoneEffectsOn ? "enabled" : "disabled") + "]";
				bool enabledLook2 = shardstoneEffectsOn;
				float x2 = num4;
				float y2 = num8;
				Rect bounds2 = val5;
				bool visible = epicLootPresent;
				object obj2 = <>O.<1>__OnShardstoneEffectsClicked;
				if (obj2 == null)
				{
					UnityAction val8 = OnShardstoneEffectsClicked;
					<>O.<1>__OnShardstoneEffectsClicked = val8;
					obj2 = (object)val8;
				}
				EnsureToggleButton("TalentPaceShardstoneEffects", ref _shardstoneEffectsButton, caption2, enabledLook2, x2, y2, bounds2, visible, (UnityAction)obj2);
				if (epicLootPresent)
				{
					num8 -= 36f;
				}
				float x3 = num4;
				float y3 = num8;
				Rect bounds3 = val5;
				bool visible2 = showTestButtonOn;
				object obj3 = <>O.<2>__OnFanfareTestClicked;
				if (obj3 == null)
				{
					UnityAction val9 = OnFanfareTestClicked;
					<>O.<2>__OnFanfareTestClicked = val9;
					obj3 = (object)val9;
				}
				EnsureToggleButton("TalentPaceFanfareTest", ref _fanfareTestButton, "Test SR level ding / VFX", enabledLook: true, x3, y3, bounds3, visible2, (UnityAction)obj3);
				if (showTestButtonOn)
				{
					SetFanfareTestButtonColor(_fanfareTestButton);
					num8 -= 36f;
				}
				EnsureFreeRespecButton(num4, num8, val5, flag);
				EnsureStatsOnTop();
				if (!_logged)
				{
					_logged = true;
					Plugin.Log("Talent window cap line active (below the point counter).");
				}
			}
		}

		private static float LabelWidth(RectTransform anchor, RectTransform host, Rect bounds)
		{
			//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_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			Rect rect = host.rect;
			float num = ((Rect)(ref rect)).width - 20f;
			rect = anchor.rect;
			return Mathf.Clamp(Mathf.Max(420f, ((Rect)(ref rect)).width * 1.15f), 420f, Mathf.Max(new float[3]
			{
				420f,
				num,
				((Rect)(ref bounds)).width - 20f
			}));
		}

		private static float LabelHeight(Text label)
		{
			float num = (float)label.fontSize * 1.2f;
			float num2 = 0f;
			try
			{
				num2 = label.preferredHeight;
			}
			catch (Exception)
			{
				num2 = 0f;
			}
			if (num2 <= 1f)
			{
				num2 = (float)label.fontSize * 1.4f * 6f;
			}
			return Mathf.Max(num, Mathf.Ceil(num2) + 4f);
		}

		private static Text EnsureLabel()
		{
			//IL_006b: 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_00bf: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_label != (Object)null && Object.op_Implicit((Object)(object)_label) && (Object)(object)((Component)_label).transform.parent == (Object)(object)_overlay)
			{
				return _label;
			}
			Transform val = ((Transform)_overlay).Find("TalentPaceCap");
			Text val2 = (((Object)(object)val != (Object)null) ? ((Component)val).GetComponent<Text>() : null);
			if ((Object)(object)val2 == (Object)null)
			{
				GameObject val3 = new GameObject("TalentPaceCap");
				val3.transform.SetParent((Transform)(object)_overlay, false);
				val2 = val3.AddComponent<Text>();
				val2.alignment = (TextAnchor)0;
				val2.horizontalOverflow = (HorizontalWrapMode)1;
				val2.verticalOverflow = (VerticalWrapMode)1;
				val2.supportRichText = true;
				((Graphic)val2).raycastTarget = false;
				((Graphic)val2).color = new Color(1f, 0.82f, 0.35f, 1f);
			}
			ApplyWindowFont(val2);
			_label = val2;
			_lastColorMark = -1;
			return val2;
		}

		private static void ApplyWindowFont(Text label)
		{
			//IL_0131: 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_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			Text val = TitleText();
			int num = ((FontSize != null) ? FontSize.Value : 0);
			int num2 = ((num > 0) ? num : (((Object)(object)val != (Object)null) ? Mathf.Clamp(Mathf.RoundToInt((float)val.fontSize * 0.58f), 15, 20) : 17));
			Font val2 = null;
			if (StatsFontRequested())
			{
				val2 = StatsFontAsset();
			}
			if ((Object)(object)val2 == (Object)null && WindowFontRequested())
			{
				val2 = TitleFontAsset();
			}
			if ((Object)(object)val2 == (Object)null)
			{
				val2 = ReadFont(num2);
			}
			if ((Object)(object)val2 == (Object)null)
			{
				val2 = TitleFontAsset();
			}
			if ((Object)(object)val2 == (Object)null)
			{
				val2 = Resources.GetBuiltinResource<Font>("Arial.ttf");
			}
			if ((Object)(object)val2 != (Object)null && label.font != val2)
			{
				label.font = val2;
				if (!_loggedFont)
				{
					_loggedFont = true;
					Plugin.Log("Talent Pace: description text font = " + ((Object)val2).name + " (requested: " + ((FontNames == null) ? "" : FontNames.Value) + ").");
				}
			}
			if (label.fontSize != num2)
			{
				label.fontSize = num2;
			}
			FontStyle val3 = (FontStyle)((FontStyle != null) ? ((int)FontStyle.Value) : 0);
			if (label.fontStyle != val3)
			{
				label.fontStyle = val3;
			}
		}

		private static bool WindowFontRequested()
		{
			return NamesRequested("Valheim-");
		}

		private static bool StatsFontRequested()
		{
			return NamesRequested("Stats");
		}

		private static bool NamesRequested(string prefix)
		{
			string[] array = ((FontNames == null) ? "" : FontNames.Value).Split(new char[1] { ',' });
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i].Trim().StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
				{
					return true;
				}
			}
			return false;
		}

		private static Font StatsFontAsset()
		{
			Font val = null;
			object obj = ((_statsPanel == null) ? null : _statsPanel.GetValue(null));
			object obj2 = ((obj == null || _statsRoot == null) ? null : _statsRoot.GetValue(obj, null));
			Transform val2 = (Transform)((obj2 is Transform) ? obj2 : null);
			GameObject val3 = (GameObject)(((Object)(object)val2 != (Object)null) ? ((Component)val2).gameObject : ((obj2 is GameObject) ? obj2 : null));
			if ((Object)(object)val3 != (Object)null)
			{
				Text[] componentsInChildren = val3.GetComponentsInChildren<Text>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					Font font = componentsInChildren[i].font;
					if ((Object)(object)font != (Object)null)
					{
						val = font;
						break;
					}
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				_statsFont = val;
				return val;
			}
			return _statsFont;
		}

		private static Font TitleFontAsset()
		{
			Text val = TitleText();
			Font val2 = (((Object)(object)val != (Object)null) ? val.font : null);
			if ((Object)(object)val2 != (Object)null)
			{
				_titleFont = val2;
			}
			return val2;
		}

		private static Font ReadFont(int size)
		{
			string[] array = FontNameList();
			string text = string.Join("|", array) + "@" + size;
			if ((Object)(object)_readFont != (Object)null && _readFontKey == text)
			{
				return _readFont;
			}
			Font val = null;
			if (array.Length != 0)
			{
				try
				{
					val = Font.CreateDynamicFontFromOSFont(array, size);
				}
				catch (Exception)
				{
					val = null;
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				_readFont = val;
				_readFontKey = text;
			}
			return val;
		}

		private static string[] FontNameList()
		{
			string text = ((FontNames == null) ? "" : FontNames.Value);
			if (string.IsNullOrWhiteSpace(text))
			{
				return DefaultReadFontNames;
			}
			string[] array = text.Split(new char[1] { ',' });
			List<string> list = new List<string>();
			string[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				string text2 = array2[i].Trim();
				if (text2.Length > 0)
				{
					list.Add(text2);
				}
			}
			if (list.Count <= 0)
			{
				return DefaultReadFontNames;
			}
			return list.ToArray();
		}

		private static void LogAvailableFonts()
		{
			try
			{
				string[] pathsToOSFonts = Font.GetPathsToOSFonts();
				if (pathsToOSFonts == null || pathsToOSFonts.Length == 0)
				{
					Plugin.Log("Talent Pace: no OS font files were reported; the description text falls back to the window font.");
					return;
				}
				List<string> list = new List<string>();
				string[] array = pathsToOSFonts;
				for (int i = 0; i < array.Length; i++)
				{
					string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(array[i]);
					if (!list.Contains(fileNameWithoutExtension))
					{
						list.Add(fileNameWithoutExtension);
					}
				}
				Plugin.Log("Talent Pace: available fonts (" + list.Count + "): " + string.Join(", ", list.ToArray()));
			}
			catch (Exception ex)
			{
				Plugin.Log("Talent Pace: the font list could not be read: " + ex.Message);
			}
		}

		private static Text TitleText()
		{
			if ((Object)(object)_titleFontSource != (Object)null && Object.op_Implicit((Object)(object)_titleFontSource))
			{
				return _titleFontSource;
			}
			object view = ViewObject();
			Component obj = FindComponent(_titleText, view);
			_titleFontSource = (Text)(object)((obj is Text) ? obj : null);
			return _titleFontSource;
		}

		private static RectTransform Anchor()
		{
			if ((Object)(object)_anchorComponent != (Object)null && Object.op_Implicit((Object)(object)_anchorComponent))
			{
				Transform transform = _anchorComponent.transform;
				return (RectTransform)(object)((transform is RectTransform) ? transform : null);
			}
			object obj = ViewObject();
			if (obj == null)
			{
				return null;
			}
			_anchorComponent = FindComponent(_pointsText, obj) ?? FindComponent(_titleText, obj);
			if ((Object)(object)_anchorComponent == (Object)null)
			{
				Component obj2 = FindComponent(_panelRect, obj);
				Transform obj3 = ((obj2 != null) ? obj2.transform : null);
				RectTransform val = (RectTransform)(object)((obj3 is RectTransform) ? obj3 : null);
				if ((Object)(object)val != (Object)null)
				{
					Transform val2 = ((Transform)val).Find("Points");
					if ((Object)(object)val2 != (Object)null)
					{
						Text componentInChildren = ((Component)val2).GetComponentInChildren<Text>(true);
						if ((Object)(object)componentInChildren != (Object)null)
						{
							_anchorComponent = (Component)(object)componentInChildren;
						}
					}
				}
			}
			if (!((Object)(object)_anchorComponent == (Object)null))
			{
				Transform transform2 = _anchorComponent.transform;
				return (RectTransform)(object)((transform2 is RectTransform) ? transform2 : null);
			}
			return null;
		}

		private static RectTransform Host()
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Expected O, but got Unknown
			object view = ViewObject();
			Component obj = FindComponent(_panelRect, view);
			Transform obj2 = ((obj != null) ? obj.transform : null);
			RectTransform val = (RectTransform)(object)((obj2 is RectTransform) ? obj2 : null);
			if ((Object)(object)val == (Object)null)
			{
				RectTransform val2 = Anchor();
				val = (RectTransform)(((Object)(object)val2 == (Object)null) ? null : /*isinst with value type is only supported in some contexts*/);
			}
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			if ((Object)(object)_overlay != (Object)null && Object.op_Implicit((Object)(object)_overlay) && (Object)(object)((Transform)_overlay).parent == (Object)(object)val)
			{
				LayoutOverlayRect(_overlay, val);
				EnsureClickLayer();
				return _overlay;
			}
			Transform val3 = ((Transform)val).Find("TalentPaceOverlay");
			GameObject val4 = (((Object)(object)val3 != (Object)null) ? ((Component)val3).gameObject : null);
			if ((Object)(object)val4 == (Object)null)
			{
				val4 = new GameObject("TalentPaceOverlay");
				val4.transform.SetParent((Transform)(object)val, false);
			}
			RectTransform val5 = val4.GetComponent<RectTransform>();
			if ((Object)(object)val5 == (Object)null)
			{
				val5 = val4.AddComponent<RectTransform>();
			}
			Canvas val6 = val4.GetComponent<Canvas>();
			if ((Object)(object)val6 == (Object)null)
			{
				val6 = val4.AddComponent<Canvas>();
			}
			val6.overrideSorting = true;
			GraphicRaycaster component = val4.GetComponent<GraphicRaycaster>();
			if ((Object)(object)component != (Object)null)
			{
				Object.Destroy((Object)(object)component);
			}
			CanvasGroup val7 = val4.GetComponent<CanvasGroup>();
			if ((Object)(object)val7 == (Object)null)
			{
				val7 = val4.AddComponent<CanvasGroup>();
			}
			val7.blocksRaycasts = true;
			val7.interactable = true;
			Canvas rootCanvas = val6.rootCanvas;
			int num = (((Object)(object)rootCanvas != (Object)null) ? (rootCanvas.sortingOrder + 20) : 20);
			if (val6.sortingOrder != num)
			{
				val6.sortingOrder = num;
			}
			LayoutOverlayRect(val5, val);
			_overlay = val5;
			EnsureClickLayer();
			((Component)val5).gameObject.SetActive(true);
			_label = null;
			_freeRespecButton = null;
			_talentEffectsButton = null;
			_shardstoneEffectsButton = null;
			_fanfareTestButton = null;
			_lastSignature = null;
			_lastColorMark = -1;
			_statsHomeParent = null;
			_statsHomeSibling = -1;
			_buildsHomeParent = null;
			_buildsHomeSibling = -1;
			return val5;
		}

		private static object ViewObject()
		{
			if (!(_view == null))
			{
				return _view.GetValue(null);
			}
			return null;
		}

		private static Component FindComponent(PropertyInfo property, object view)
		{
			if (property == null || view == null)
			{
				return null;
			}
			object? value = property.GetValue(view, null);
			return (Component)((value is Component) ? value : null);
		}

		private static void LayoutOverlayRect(RectTransform overlay, RectTransform panel)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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_0041: 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_0057: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)overlay == (Object)null))
			{
				overlay.anchorMin = Vector2.zero;
				overlay.anchorMax = Vector2.one;
				overlay.pivot = new Vector2(0.5f, 0.5f);
				overlay.offsetMin = Vector2.zero;
				overlay.offsetMax = Vector2.zero;
				overlay.anchoredPosition = Vector2.zero;
				overlay.sizeDelta = Vector2.zero;
			}
		}

		private static void EnsureClickLayer()
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			//IL_006e: 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)
			//IL_0084: 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)
			if (!((Object)(object)_overlay == (Object)null))
			{
				Transform val = ((Transform)_overlay).Find("TalentPaceClicks");
				GameObject val2 = (((Object)(object)val != (Object)null) ? ((Component)val).gameObject : null);
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = new GameObject("TalentPaceClicks");
					val2.transform.SetParent((Transform)(object)_overlay, false);
				}
				RectTransform val3 = val2.GetComponent<RectTransform>();
				if ((Object)(object)val3 == (Object)null)
				{
					val3 = val2.AddComponent<RectTransform>();
				}
				val3.anchorMin = Vector2.zero;
				val3.anchorMax = Vector2.one;
				val3.offsetMin = Vector2.zero;
				val3.offsetMax = Vector2.zero;
				Canvas val4 = val2.GetComponent<Canvas>();
				if ((Object)(object)val4 == (Object)null)
				{
					val4 = val2.AddComponent<Canvas>();
				}
				val4.overrideSorting = false;
				EnsureOverlayRaycaster(val2);
				CanvasGroup val5 = val2.GetComponent<CanvasGroup>();
				if ((Object)(object)val5 == (Object)null)
				{
					val5 = val2.AddComponent<CanvasGroup>();
				}
				val5.blocksRaycasts = true;
				val5.interactable = true;
				_clickLayer = val3;
			}
		}

		private static Rect OverlayLayoutBounds()
		{
			//IL_0012: 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_0034: 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)
			if ((Object)(object)_overlay != (Object)null)
			{
				Rect rect = _overlay.rect;
				return new Rect(((Rect)(ref rect)).x, ((Rect)(ref rect)).y, ((Rect)(ref rect)).width, ((Rect)(ref rect)).height);
			}
			return HostBounds((RectTransform)(((Object)(object)_overlay != (Object)null) ? /*isinst with value type is only supported in some contexts*/: null));
		}

		private static Vector2 PanelLocalToOverlay(Vector2 panelLocal)
		{
			//IL_0036: 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_003f: 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_004f: 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_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			RectTransform val = (RectTransform)(((Object)(object)_overlay == (Object)null) ? null : /*isinst with value type is only supported in some contexts*/);
			if ((Object)(object)val == (Object)null || (Object)(object)_overlay == (Object)null)
			{
				return panelLocal;
			}
			Vector3 val2 = ((Transform)val).TransformPoint(new Vector3(panelLocal.x, panelLocal.y, 0f));
			return Vector2.op_Implicit(((Transform)_overlay).InverseTransformPoint(val2));
		}

		private static Rect HostBounds(RectTransform parent)
		{
			//IL_0024: 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_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)parent == (Object)null)
			{
				return new Rect(-500f, -400f, 1000f, 800f);
			}
			Rect rect = parent.rect;
			return new Rect(((Rect)(ref rect)).x, ((Rect)(ref rect)).y, ((Rect)(ref rect)).width, ((Rect)(ref rect)).height);
		}

		private static Vector2 BottomLeftIn(RectTransform rt, RectTransform target)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: 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_0039: 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_003f: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			Rect rect = rt.rect;
			float xMin = ((Rect)(ref rect)).xMin;
			rect = rt.rect;
			Vector3 val = ((Transform)rt).TransformPoint(xMin, ((Rect)(ref rect)).yMin, 0f);
			if ((Object)(object)target == (Object)null)
			{
				return Vector2.op_Implicit(val);
			}
			return Vector2.op_Implicit(((Transform)target).InverseTransformPoint(val));
		}

		private static void AfterStatsShow(bool visible)
		{
			EnsureStatsOnTop();
		}

		private static void AfterAuxShow()
		{
			EnsureStatsOnTop();
		}

		private static bool AuxPanelShown()
		{
			if (_statsOpen != null)
			{
				try
				{
					object value = _statsOpen.GetValue(null);
					bool flag = default(bool);
					int num;
					if (value is bool)
					{
						flag = (bool)value;
						num = 1;
					}
					else
					{
						num = 0;
					}
					if (((uint)num & (flag ? 1u : 0u)) != 0)
					{
						return true;
					}
				}
				catch (Exception)
				{
				}
			}
			if (_isBuildsOpen != null)
			{
				try
				{
					object value2 = _isBuildsOpen.GetValue(null, null);
					bool flag2 = default(bool);
					int num2;
					if (value2 is bool)
					{
						flag2 = (bool)value2;
						num2 = 1;
					}
					else
					{
						num2 = 0;
					}
					if (((uint)num2 & (flag2 ? 1u : 0u)) != 0)
					{
						return true;
					}
				}
				catch (Exception)
				{
				}
			}
			if (StaticBool(_textDialogIsOpen) || InstanceBool(_confirmIsVisible, ConfirmDialogInstance()))
			{
				return true;
			}
			return false;
		}

		private static bool StaticBool(PropertyInfo property)
		{
			if (property == null)
			{
				return false;
			}
			try
			{
				object value = property.GetValue(null, null);
				bool flag = default(bool);
				int num;
				if (value is bool)
				{
					flag = (bool)value;
					num = 1;
				}
				else
				{
					num = 0;
				}
				return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
			}
			catch (Exception)
			{
				return false;
			}
		}

		private static bool InstanceBool(PropertyInfo property, object instance)
		{
			if (property == null || instance == null)
			{
				return false;
			}
			try
			{
				object value = property.GetValue(instance, null);
				bool flag = default(bool);
				int num;
				if (value is bool)
				{
					flag = (bool)value;
					num = 1;
				}
				else
				{
					num = 0;
				}
				return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
			}
			catch (Exception)
			{
				return false;
			}
		}

		private static object ConfirmDialogInstance()
		{
			if (!(_confirmDialog == null))
			{
				return _confirmDialog.GetValue(null);
			}
			return null;
		}

		private static GameObject PanelRoot(FieldInfo panelField, PropertyInfo rootProp)
		{
			if (panelField == null || rootProp == null)
			{
				return null;
			}
			object value = panelField.GetValue(null);
			if (value == null)
			{
				return null;
			}
			object value2 = rootProp.GetValue(value, null);
			Transform val = (Transform)((value2 is Transform) ? value2 : null);
			if ((Object)(object)val != (Object)null)
			{
				return ((Component)val).gameObject;
			}
			return (GameObject)((value2 is GameObject) ? value2 : null);
		}

		private static void EnsureOverlayRaycaster(GameObject go)
		{
			if (!((Object)(object)go == (Object)null) && (Object)(object)go.GetComponent<GraphicRaycaster>() == (Object)null)
			{
				go.AddComponent<GraphicRaycaster>();
			}
		}

		private static int ButtonFontSize()
		{
			Text obj = TitleText();
			return Mathf.Clamp(Mathf.RoundToInt((((float?)((obj != null) ? new int?(obj.fontSize) : ((int?)null))) ?? 28f) * 0.38f), 11, 13);
		}

		private static void EnsureOverlayOrder()
		{
			if ((Object)(object)_overlay == (Object)null)
			{
				return;
			}
			Canvas component = ((Component)_overlay).GetComponent<Canvas>();
			if (!((Object)(object)component == (Object)null))
			{
				Canvas rootCanvas = component.rootCanvas;
				int num = (((Object)(object)rootCanvas != (Object)null) ? (rootCanvas.sortingOrder + 20) : 20);
				if (component.sortingOrder != num)
				{
					component.sortingOrder = num;
				}
			}
		}

		private static void EnsureStatsOnTop()
		{
			//IL_0061: 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_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			EnsureOverlayOrder();
			RestoreHome(_statsPanel, _statsRoot, ref _statsHomeParent, ref _statsHomeSibling);
			RestoreHome(_buildsPanel, _buildsRoot, ref _buildsHomeParent, ref _buildsHomeSibling);
			GameObject val = PanelRoot(_statsPanel, _statsRoot);
			if ((Object)(object)val != (Object)null)
			{
				Image component = val.GetComponent<Image>();
				if ((Object)(object)component != (Object)null)
				{
					Color color = ((Graphic)component).color;
					if (color.a < 0.999f)
					{
						color.a = 1f;
						((Graphic)component).color = color;
					}
				}
			}
			if ((Object)(object)_overlay != (Object)null)
			{
				((Component)_overlay).gameObject.SetActive(!AuxPanelShown());
			}
			RaiseDialog(TextDialogPanel());
			RaiseDialog(ConfirmDialogRoot());
		}

		private static GameObject TextDialogPanel()
		{
			if (_textDialogPanel == null)
			{
				return null;
			}
			try
			{
				object? value = _textDialogPanel.GetValue(null, null);
				object obj = ((value is GameObject) ? value : null);
				if (obj == null)
				{
					object? value2 = _textDialogPanel.GetValue(null, null);
					object? obj2 = ((value2 is Component) ? value2 : null);
					obj = ((obj2 != null) ? ((Component)obj2).gameObject : null);
				}
				return (GameObject)obj;
			}
			catch (Exception)
			{
				return null;
			}
		}

		private static GameObject ConfirmDialogRoot()
		{
			object obj = ConfirmDialogInstance();
			if (obj == null || _confirmRoot == null)
			{
				return null;
			}
			try
			{
				object value = _confirmRoot.GetValue(obj, null);
				object obj2 = ((value is GameObject) ? value : null);
				if (obj2 == null)
				{
					object obj3 = ((value is Component) ? value : null);
					obj2 = ((obj3 != null) ? ((Component)obj3).gameObject : null);
				}
				return (GameObject)obj2;
			}
			catch (Exception)
			{
				return null;
			}
		}

		private static void RaiseDialog(GameObject go)
		{
			if ((Object)(object)go == (Object)null)
			{
				return;
			}
			Canvas val = go.GetComponent<Canvas>() ?? go.GetComponentInChildren<Canvas>(true);
			if ((Object)(object)val != (Object)null)
			{
				val.overrideSorting = true;
				Canvas val2 = (((Object)(object)_overlay == (Object)null) ? null : ((Component)_overlay).GetComponent<Canvas>());
				int num = (((Object)(object)val2 != (Object)null) ? val2.sortingOrder : 20) + 10;
				if (val.sortingOrder != num)
				{
					val.sortingOrder = num;
				}
			}
			go.transform.SetAsLastSibling();
		}

		private static void RestoreHome(FieldInfo panelField, PropertyInfo rootProp, ref Transform home, ref int sibling)
		{
			GameObject val = PanelRoot(panelField, rootProp);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Transform transform = val.transform;
			if ((Object)(object)home == (Object)null && (Object)(object)transform.parent != (Object)null && (Object)(object)transform.parent != (Object)(object)_overlay && ((Object)(object)_clickLayer == (Object)null || (Object)(object)transform.parent != (Object)(object)_clickLayer))
			{
				home = transform.parent;
				sibling = transform.GetSiblingIndex();
			}
			if ((Object)(object)_overlay != (Object)null && (Object)(object)home != (Object)null && ((Object)(object)transform.parent == (Object)(object)_overlay || (Object)(object)transform.parent == (Object)(object)_clickLayer))
			{
				transform.SetParent(home, false);
				if (sibling >= 0 && sibling < home.childCount)
				{
					transform.SetSiblingIndex(sibling);
				}
			}
			Canvas component = val.GetComponent<Canvas>();
			if ((Object)(object)component != (Object)null && ((Object)(object)_overlay == (Object)null || (Object)(object)component != (Object)(object)((Component)_overlay).GetComponent<Canvas>()))
			{
				component.overrideSorting = false;
			}
		}

		private static void EnsureToggleButton(string name, ref Button stored, string caption, bool enabledLook, float x, float y, Rect bounds, bool visible, UnityAction onClick)
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			Transform val = (Transform)(object)(((Object)(object)_clickLayer != (Object)null) ? _clickLayer : _overlay);
			Transform val2 = val.Find(name);
			Button val3 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponent<Button>() : null);
			if ((Object)(object)val3 == (Object)null)
			{
				if (!visible)
				{
					return;
				}
				GameObject val4 = new GameObject(name);
				val4.transform.SetParent(val, false);
				Image val5 = val4.AddComponent<Image>();
				val3 = val4.AddComponent<Button>();
				GameObject val6 = new GameObject("Text");
				val6.transform.SetParent(val4.transform, false);
				Text obj = val6.AddComponent<Text>();
				obj.alignment = (TextAnchor)4;
				((Graphic)obj).color = Color.white;
				obj.supportRichText = true;
				((Graphic)obj).raycastTarget = false;
				ApplyWindowFont(obj);
				obj.fontSize = ButtonFontSize();
				RectTransform component = ((Component)obj).GetComponent<RectTransform>();
				component.anchorMin = Vector2.zero;
				component.anchorMax = Vector2.one;
				component.offsetMin = Vector2.zero;
				component.offsetMax = Vector2.zero;
				((Selectable)val3).targetGraphic = (Graphic)(object)val5;
				((Graphic)val5).raycastTarget = true;
				stored = val3;
			}
			((UnityEventBase)val3.onClick).RemoveAllListeners();
			((UnityEvent)val3.onClick).AddListener(onClick);
			Text componentInChildren = ((Component)val3).GetComponentInChildren<Text>();
			if ((Object)(object)componentInChildren != (Object)null)
			{
				componentInChildren.text = caption;
				componentInChildren.fontSize = ButtonFontSize();
			}
			Image component2 = ((Component)val3).GetComponent<Image>();
			if ((Object)(object)component2 != (Object)null)
			{
				((Graphic)component2).color = (enabledLook ? new Color(0.15f, 0.35f, 0.12f, 0.95f) : new Color(0.38f, 0.16f, 0.12f, 0.95f));
			}
			PlaceButton(val3, x, y, bounds);
			((Component)val3).gameObject.SetActive(visible);
		}

		private static void PlaceButton(Button button, float x, float y, Rect bounds)
		{
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			RectTransform component = ((Component)button).GetComponent<RectTransform>();
			float num = Mathf.Min(160f, Mathf.Max(80f, ((Rect)(ref bounds)).width - 20f));
			float num2 = Mathf.Min(x, ((Rect)(ref bounds)).xMax - num - 10f);
			float num3 = y;
			float num4 = ((Rect)(ref bounds)).yMax - 10f - 28f;
			float num5 = ((Rect)(ref bounds)).yMin + 10f;
			if (num3 > num4)
			{
				num3 = num4;
			}
			if (num3 < num5)
			{
				num3 = num5;
			}
			if (num2 < ((Rect)(ref bounds)).xMin + 10f)
			{
				num2 = ((Rect)(ref bounds)).xMin + 10f;
			}
			component.anchorMin = new Vector2(0f, 1f);
			component.anchorMax = new Vector2(0f, 1f);
			component.pivot = new Vector2(0f, 1f);
			component.sizeDelta = new Vector2(num, 28f);
			component.anchoredPosition = new Vector2(num2 - ((Rect)(ref bounds)).xMin, num3 - ((Rect)(ref bounds)).yMax);
		}

		private static void OnTalentEffectsClicked()
		{
			bool num = !EffectToggles.TalentEffectsOn;
			EffectToggles.SetTalentEffects(num);
			TellPlayer(num ? "Talent effects enabled" : "Talent effects disabled");
			RefreshOverlayNow();
		}

		private static void OnShardstoneEffectsClicked()
		{
			bool num = !EffectToggles.ShardstoneEffectsOn;
			EffectToggles.SetShardstoneEffects(num);
			TellPlayer(num ? "Shardstone effects enabled" : "Shardstone effects disabled");
			RefreshOverlayNow();
		}

		private static void OnFanfareTestClicked()
		{
			SkillsLevelUpFanfare.TryPlayTest();
		}

		private static void SetFanfareTestButtonColor(Button button)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)button == (Object)null))
			{
				Image component = ((Component)button).GetComponent<Image>();
				if ((Object)(object)component != (Object)null)
				{
					((Graphic)component).color = new Color(0.22f, 0.18f, 0.38f, 0.95f);
				}
			}
		}

		private static void TellPlayer(string text)
		{
			Plugin.Log("Talent Pace: " + text);
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer != (Object)null)
			{
				((Character)localPlayer).Message((MessageType)2, text, 0, (Sprite)null, false);
			}
		}

		private static void RefreshOverlayNow()
		{
			_lastSignature = null;
			_nextSample = 0f;
			if (_windowRefresh != null)
			{
				try
				{
					_windowRefresh.Invoke(null, null);
				}
				catch (Exception)
				{
				}
			}
			AfterRefresh();
		}

		private static void EnsureFreeRespecButton(float x, float y, Rect bounds, bool visible)
		{
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: 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_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Expected O, but got Unknown
			Transform val = (Transform)(object)(((Object)(object)_clickLayer != (Object)null) ? _clickLayer : _overlay);
			Transform val2 = val.Find("TalentPaceFreeRespec");
			Button val3 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponent<Button>() : null);
			if ((Object)(object)val3 == (Object)null)
			{
				if (!visible)
				{
					return;
				}
				GameObject val4 = new GameObject("TalentPaceFreeRespec");
				val4.transform.SetParent(val, false);
				((Graphic)val4.AddComponent<Image>()).color = new Color(0.15f, 0.35f, 0.12f, 0.95f);
				val3 = val4.AddComponent<Button>();
				GameObject val5 = new GameObject("Text");
				val5.transform.SetParent(val4.transform, false);
				Text obj = val5.AddComponent<Text>();
				obj.alignment = (TextAnchor)4;
				((Graphic)obj).color = Color.white;
				obj.supportRichText = true;
				((Graphic)obj).raycastTarget = false;
				obj.text = "Free respec (over talent limit)";
				ApplyWindowFont(obj);
				obj.fontSize = ButtonFontSize();
				RectTransform component = ((Component)obj).GetComponent<RectTransform>();
				component.anchorMin = Vector2.zero;
				component.anchorMax = Vector2.one;
				component.offsetMin = Vector2.zero;
				component.offsetMax = Vector2.zero;
				ButtonClickedEvent onClick = val3.onClick;
				object obj2 = <>O.<3>__OnFreeRespecClicked;
				if (obj2 == null)
				{
					UnityAction val6 = OnFreeRespecClicked;
					<>O.<3>__OnFreeRespecClicked = val6;
					obj2 = (object)val6;
				}
				((UnityEvent)onClick).AddListener((UnityAction)obj2);
				_freeRespecButton = val3;
			}
			PlaceButton(val3, x, y, bounds);
			((Component)val3).gameObject.SetActive(visible);
		}

		private static void OnFreeRespecClicked()
		{
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null) && Pace.TryFreeRespec(localPlayer))
			{
				Pace.InvalidateHintCache(localPlayer);
				_lastSignature = null;
				_nextSample = 0f;
				if (_windowRefresh != null)
				{
					_windowRefresh.Invoke(null, null);
				}
			}
		}

		private static void Sample(Player player)
		{
			if (_getRuntime == null || _totalPoints == null)
			{
				return;
			}
			try
			{
				_oneArg[0] = player;
				object obj = _getRuntime.Invoke(null, _oneArg);
				if (obj == null)
				{
					_oneArg[0] = null;
					return;
				}
				_twoArg[0] = player;
				_twoArg[1] = obj;
				_totalPoints.Invoke(null, _twoArg);
				_oneArg[0] = null;
				_twoArg[0] = null;
				_twoArg[1] = null;
			}
			catch (Exception)
			{
			}
		}
	}
	internal static class DamageTooltipPatch
	{
		internal static void Apply(Harmony harmony)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.Method(typeof(DamageTypes), "GetTooltipString", new Type[1] { typeof(SkillType) }, (Type[])null);
			if (methodInfo != null)
			{
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(DamageTooltipPatch), "Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		private static void Postfix(DamageTypes __instance, SkillType skillType, ref string __result)
		{
			//IL_004c: 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_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: 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_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: 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_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				return;
			}
			Plugin.Log($"DamageTooltipFix ran. Result length: {__result?.Length ?? 0}");
			float ttFactor;
			StringBuilder sb;
			if (!string.IsNullOrEmpty(__result) && !((Object)(object)Player.m_localPlayer == (Object)null))
			{
				float skillFactor = ((Character)Player.m_localPlayer).GetSkillFactor(skillType);
				ttFactor = 0.4f + 0.3f * skillFactor;
				__result = Regex.Replace(__result, "<color=orange>(\\d+)</color>", (Match match) => match.Value);
				sb = new StringBuilder();
				Add("$inventory_blunt", __instance.m_blunt);
				Add("$inventory_slash", __instance.m_slash);
				Add("$inventory_pierce", __instance.m_pierce);
				Add("$inventory_chop", __instance.m_chop);
				Add("$inventory_pickaxe", __instance.m_pickaxe);
				Add("$inventory_fire", __instance.m_fire);
				Add("$inventory_frost", __instance.m_frost);
				Add("$inventory_lightning", __instance.m_lightning);
				Add("$inventory_poison", __instance.m_poison);
				Add("$inventory_spirit", __instance.m_spirit);
				Add("$inventory_damage", __instance.m_damage);
				if (sb.Length > 0)
				{
					__result = sb.ToString().TrimStart(new char[1] { '\n' });
				}
			}
			void Add(string token, float listed)
			{
				if (!(listed <= 0f))
				{
					int num = Mathf.RoundToInt(listed);
					int num2 = Mathf.RoundToInt(listed * ttFactor);
					sb.Append($"\n{token}: <color=orange>{num}</color> <color=yellow>({num2})</color>");
				}
			}
		}
	}
	internal static class EffectToggles
	{
		internal static ConfigEntry<bool> TalentEffects;

		internal static ConfigEntry<bool> ShardstoneEffects;

		private static bool _epicLoot;

		private static MethodInfo _syncToPlayer;

		private static MethodInfo _getRuntime;

		private static MethodInfo _resetToDefault;

		internal static bool EpicLootPresent => _epicLoot;

		internal static bool TalentEffectsOn
		{
			get
			{
				if (TalentEffects != null)
				{
					return TalentEffects.Value;
				}
				return true;
			}
		}

		internal static bool ShardstoneEffectsOn
		{
			get
			{
				if (ShardstoneEffects != null)
				{
					return ShardstoneEffects.Value;
				}
				return true;
			}
		}

		public static void Bind(ConfigFile config)
		{
			TalentEffects = config.Bind<bool>("Effect toggles", "Enable talent effects", true, "When false, Talent Tree combat and aura effects do not apply. Use this while taming so talent procs do not kill the creature. Points and the tree itself stay as they are. On by default.");
			ShardstoneEffects = config.Bind<bool>("Effect toggles", "Enable shardstone effects", true, "When false, EpicLoot socketed shardstone bonuses do not apply (item enchants are unchanged). The button in the talent window is shown only if EpicLoot is installed. On by default.");
		}

		public static void Apply(Harmony harmony)
		{
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Expected O, but got Unknown
			//IL_0312: Unknown result type (might be due to invalid IL or missing references)
			//IL_0319: Expected O, but got Unknown
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			//IL_0247: Expected O, but got Unknown
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Expected O, but got Unknown
			_epicLoot = AccessTools.TypeByName("EpicLoot.MagicItem") != null;
			Type type = TT.Type("Core.SE_TalentPassives");
			Type type2 = TT.Type("Core.TalentPassivesService");
			Type type3 = TT.Type("Core.TalentRuntimeService");
			Type type4 = TT.Type("Core.TalentRuntime");
			_resetToDefault = ((type == null) ? null : AccessTools.DeclaredMethod(type, "ResetToDefault", (Type[])null, (Type[])null));
			_syncToPlayer = ((type2 == null || type4 == null) ? null : AccessTools.DeclaredMethod(type2, "SyncToPlayer", new Type[1] { type4 }, (Type[])null));
			_getRuntime = ((type3 == null) ? null : AccessTools.DeclaredMethod(type3, "Get", new Type[1] { typeof(Player) }, (Type[])null));
			if (type != null)
			{
				PatchIfFound(harmony, type, "SyncFromCache", "AfterSyncFromCache", postfix: true);
				PatchIfFound(harmony, type, "ModifyAttack", "KeepIfTalentsOn", postfix: false);
				PatchIfFound(harmony, type, "UpdateStatusEffect", "KeepIfTalentsOn", postfix: false);
				PatchIfFound(harmony, type, "ModifyAttackStaminaUsage", "KeepIfTalentsOn", postfix: false);
			}
			if (_syncToPlayer != null)
			{
				harmony.Patch((MethodBase)_syncToPlayer, (HarmonyMethod)null, new HarmonyMethod(typeof(EffectToggles), "AfterSyncToPlayer", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			GateTalentHub(harmony, "Patches.CharacterDamagePatches");
			GateTalentHub(harmony, "Patches.CharacterApplyDamagePatches", boolPrefixAllowsOriginal: true);
			GateTalentHub(harmony, "Patches.CharacterHealPatches");
			GateTalentHub(harmony, "Patches.PlayerUpdatePatches");
			GateTalentHub(harmony, "Patches.HumanoidBlockAttackPatches");
			GateTalentHub(harmony, "Patches.PlayerGetTotalFoodValuePatches");
			GatePlayerGetTotalFoodValue(harmony);
			Type type5 = TT.Type("Talents.Bulwark.BulwarkImmolationPatch");
			if (type5 != null)
			{
				PatchDeclared(harmony, type5, "OnPlayerUpdatePostfix", "KeepIfTalentsOn");
			}
			else
			{
				Plugin.Log("Talent Pace: Bulwark Immolation patch was not found; Player.Update hub is still gated.");
			}
			MethodInfo[] methods = typeof(SEMan).GetMethods(BindingFlags.Instance | BindingFlags.Public);
			for (int i = 0; i < methods.Length; i++)
			{
				if (!(methods[i].Name != "AddStatusEffect"))
				{
					ParameterInfo[] parameters = methods[i].GetParameters();
					if (parameters.Length != 0 && parameters[0].ParameterType == typeof(StatusEffect))
					{
						harmony.Patch((MethodBase)methods[i], new HarmonyMethod(typeof(EffectToggles), "BeforeAddStatusEffect", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					}
					else if (parameters.Length != 0 && parameters[0].ParameterType == typeof(int))
					{
						harmony.Patch((MethodBase)methods[i], new HarmonyMethod(typeof(EffectToggles), "BeforeAddStatusEffectHash", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					}
				}
			}
			if (_epicLoot)
			{
				Type type6 = AccessTools.TypeByName("EpicLoot.MagicItem");
				if (type6 != null)
				{
					MethodInfo methodInfo = AccessTools.DeclaredMethod(type6, "HasEffect", (Type[])null, (Type[])null);
					MethodInfo methodInfo2 = AccessTools.DeclaredMethod(type6, "SumEffectValue", (Type[])null, (Type[])null);
					MethodInfo methodInfo3 = AccessTools.DeclaredMethod(type6, "GetEffects", (Type[])null, (Type[])null);
					MethodInfo methodInfo4 = AccessTools.DeclaredMethod(type6, "GetTotalEffectValue", (Type[])null, (Type[])null);
					HarmonyMethod val = new HarmonyMethod(typeof(EffectToggles), "StripSocketedFlag", (Type[])null);
					if (methodInfo != null)
					{
						harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					}
					if (methodInfo2 != null)
					{
						harmony.Patch((MethodBase)methodInfo2, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					}
					if (methodInfo3 != null)
					{
						harmony.Patch((MethodBase)methodInfo3, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					}
					if (methodInfo4 != null)
					{
						harmony.Patch((MethodBase)methodInfo4, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					}
					Plugin.Log("Talent Pace: EpicLoot found — shardstone effect toggle is available.");
				}
			}
			else
			{
				Plugin.Log("Talent Pace: EpicLoot not installed — shardstone effect button is hidden.");
			}
		}

		private static void PatchIfFound(Harmony harmony, Type type, string method, string patch, bool postfix)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.DeclaredMethod(type, method, (Type[])null, (Type[])null);
			if (!(methodInfo == null))
			{
				HarmonyMethod val = new HarmonyMethod(typeof(EffectToggles), patch, (Type[])null);
				if (postfix)
				{
					harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				else
				{
					harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
		}

		private static void GateTalentHub(Harmony harmony, string typeName, bool boolPrefixAllowsOriginal = false)
		{
			Type type = TT.Type(typeName);
			if (type == null)
			{
				Plugin.Log("Talent Pace: talent hub " + typeName + " was not found.");
				return;
			}
			string prefix = (boolPrefixAllowsOriginal ? "KeepBoolPrefixAllowOriginalIfTalentsOff" : "KeepIfTalentsOn");
			MethodInfo[] methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			bool flag = false;
			foreach (MethodInfo methodInfo in methods)
			{
				if (methodInfo.Name == "Prefix")
				{
					PatchDeclared(harmony, type, methodInfo.Name, prefix);
					flag = true;
				}
				else if (methodInfo.Name == "Postfix")
				{
					PatchDeclared(harmony, type, methodInfo.Name, "KeepIfTalentsOn");
					flag = true;
				}
			}
			if (!flag)
			{
				Plugin.Log("Talent Pace: talent hub " + type.FullName + " has no Prefix/Postfix to gate.");
			}
		}

		private static void GatePlayerGetTotalFoodValue(Harmony harmony)
		{
			PatchDeclared(harmony, TT.Type("Talents.Shared.MinorStatPatches"), "OnPlayerGetTotalFoodValuePostfix", "KeepIfTalentsOn");
			PatchDeclared(harmony, TT.Type("Talents.Bulwark.BulwarkIroncladPatch"), "OnPlayerGetTotalFoodValuePostfix", "KeepIfTalentsOn");
		}

		private static void PatchDeclared(Harmony harmony, Type type, string method, string prefix)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			if (!(type == null))
			{
				MethodInfo methodInfo = AccessTools.DeclaredMethod(type, method, (Type[])null, (Type[])null);
				if (!(methodInfo == null))
				{
					harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(EffectToggles), prefix, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
		}

		private static bool KeepIfTalentsOn()
		{
			return TalentEffectsOn;
		}

		private static bool KeepBoolPrefixAllowOriginalIfTalentsOff(ref bool __result)
		{
			if (TalentEffectsOn)
			{
				return true;
			}
			__result = true;
			return false;
		}

		private static void AfterSyncFromCache(object __instance)
		{
			if (!TalentEffectsOn)
			{
				ResetPassives(__instance);
			}
		}

		private static void AfterSyncToPlayer()
		{
			if (!TalentEffectsOn)
			{
				ApplyToLocalPlayer();
			}
		}

		private static bool BeforeAddStatusEffect(StatusEffect statusEffect)
		{
			if (TalentEffectsOn || (Object)(object)statusEffect == (Object)null || TT.Assembly == null)
			{
				return true;
			}
			if (((object)statusEffect).GetType().Assembly != TT.Assembly)
			{
				return true;
			}
			return ((Object)statusEffect).name == "TT_TalentPassives";
		}

		private static bool BeforeAddStatusEffectHash(int nameHash)
		{
			if (TalentEffectsOn || (Object)(object)ObjectDB.instance == (Object)null)
			{
				return true;
			}
			StatusEffect statusEffect = ObjectDB.instance.GetStatusEffect(nameHash);
			if ((Object)(object)statusEffect == (Object)null)
			{
				return true;
			}
			if (((Object)statusEffect).name == "TT_TalentPassives")
			{
				return true;
			}
			if (!(TT.Assembly == null))
			{
				return ((object)statusEffect).GetType().Assembly != TT.Assembly;
			}
			return true;
		}

		private static void StripSocketedFlag(ref bool includeSocketed)
		{
			if (!ShardstoneEffectsOn)
			{
				includeSocketed = false;
			}
		}

		internal static void SetTalentEffects(bool on)
		{
			if (TalentEffects == null)
			{
				Plugin.Log("Talent Pace: Enable talent effects config is missing.");
				return;
			}
			TalentEffects.Value = on;
			try
			{
				((BaseUnityPlugin)Plugin.Instance).Config.Save();
			}
			catch (Exception)
			{
			}
			try
			{
				ApplyToLocalPlayer();
			}
			catch (Exception ex2)
			{
				Plugin.Log("Talent Pace: applying talent toggle failed: " + ex2.Message);
			}
		}

		internal static void SetShardstoneEffects(bool on)
		{
			if (ShardstoneEffects == null)
			{
				Plugin.Log("Talent Pace: Enable shardstone effects config is missing.");
				return;
			}
			ShardstoneEffects.Value = on;
			try
			{
				((BaseUnityPlugin)Plugin.Instance).Config.Save();
			}
			catch (Exception)
			{
			}
		}

		internal static void ApplyToLocalPlayer()
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			if (TalentEffectsOn)
			{
				if (_syncToPlayer != null && _getRuntime != null)
				{
					object obj = _getRuntime.Invoke(null, new object[1] { localPlayer });
					if (obj != null)
					{
						_syncToPlayer.Invoke(null, new object[1] { obj });
					}
				}
				return;
			}
			StripTalentStatus((Character)(object)localPlayer);
			List<Character> list = new List<Character>();
			Character.GetCharactersInRange(((Component)localPlayer).transform.position, 80f, list);
			for (int i = 0; i < list.Count; i++)
			{
				Character val = list[i];
				if ((Object)(object)val != (Object)null && (Object)(object)val != (Object)(object)localPlayer)
				{
					StripTalentStatus(val);
				}
			}
		}

		private static void StripTalentStatus(Character character)
		{
			if ((Object)(object)character == (Object)null || TT.Assembly == null)
			{
				return;
			}
			SEMan sEMan = character.GetSEMan();
			if (sEMan == null)
			{
				return;
			}
			List<StatusEffect> statusEffects = sEMan.GetStatusEffects();
			if (statusEffects == null)
			{
				return;
			}
			for (int num = statusEffects.Count - 1; num >= 0; num--)
			{
				StatusEffect val = statusEffects[num];
				if (!((Object)(object)val == (Object)null) && !(((object)val).GetType().Assembly != TT.Assembly))
				{
					if (((Object)val).name == "TT_TalentPassives")
					{
						ResetPassives(val);
					}
					else
					{
						sEMan.RemoveStatusEffect(val, true);
					}
				}
			}
		}

		private static void ResetPassives(object passives)
		{
			if (passives == null || _resetToDefault == null)
			{
				return;
			}
			try
			{
				_resetToDefault.Invoke(passives, null);
			}
			catch (Exception)
			{
			}
		}
	}
	internal static class EvaSurtrRewards
	{
		private enum EvaSurtrKind
		{
			None,
			Mist,
			Ash,
			Deep
		}

		internal const string PrefabMist = "HBC_Shard_MistQueen";

		internal const string PrefabAsh = "HBC_Shard_AshDemon";

		internal const string PrefabDeep = "HBC_Shard_DeepJotunn";

		private const string KeyCapMist = "talentpace_eva_cap_mist";

		private const string KeyCapAsh = "talentpace_eva_cap_ash";

		private const string LegacyJotunnPointsKey = "talentpace_eva_points_deep";

		internal static int EvaCapBonus(Player player)
		{
			if ((Object)(object)player == (Object)null || !Plugin.EnableTalentPointCap.Value)
			{
				return 0;
			}
			int num = 0;
			if (((Humanoid)player).HaveUniqueKey("talentpace_eva_cap_mist"))
			{
				num += Plugin.EvaCapPerBoss.Value;
			}
			if (((Humanoid)player).HaveUniqueKey("talentpace_eva_cap_ash"))
			{
				num += Plugin.EvaCapPerBoss.Value;
			}
			if (((Humanoid)player).HaveUniqueKey("talentpace_eva_cap_deep") || ((Humanoid)player).HaveUniqueKey("talentpace_eva_points_deep"))
			{
				num += Plugin.EvaJotunnCapBonus.Value;
			}
			return num;
		}

		internal static bool TrySell(StoreGui storeGui, ItemData item)
		{
			//IL_0099: 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_00ac: 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)
			if ((Object)(object)storeGui == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null || item == null)
			{
				return false;
			}
			if (!TryMap(HaldorShardSell.PrefabName(item), out var kind))
			{
				return false;
			}
			Player localPlayer = Player.m_localPlayer;
			bool flag = IsFirstReward(localPlayer, kind);
			int num = CoinsFor(kind);
			if (flag)
			{
				GrantFirstReward(localPlayer, kind);
			}
			if (!RemoveOneFromSellSlot(localPlayer, item))
			{
				return false;
			}
			if (num > 0)
			{
				AddCoins(storeGui, localPlayer, item, num);
			}
			string text = (flag ? FirstRewardMessage(kind, num) : $"Sold for {num} coins (talent reward already claimed).");
			((Character)localPlayer).Message((MessageType)1, text, 0, (Sprite)null, false);
			storeGui.m_sellEffects.Create(((Component)storeGui).transform.position, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
			Trader trader = storeGui.m_trader;
			if (trader != null)
			{
				trader.OnSold();
			}
			storeGui.FillList();
			return true;
		}

		private static bool IsFirstReward(Player player, EvaSurtrKind kind)
		{
			switch (kind)
			{
			case EvaSurtrKind.Mist:
				return !((Humanoid)player).HaveUniqueKey("talentpace_eva_cap_mist");
			case EvaSurtrKind.Ash:
				return !((Humanoid)player).HaveUniqueKey("talentpace_eva_cap_ash");
			case EvaSurtrKind.Deep:
				if (!((Humanoid)player).HaveUniqueKey("talentpace_eva_cap_deep"))
				{
					return !((Humanoid)player).HaveUniqueKey("talentpace_eva_points_deep");
				}
				return false;
			default:
				return false;
			}
		}

		private static void GrantFirstReward(Player player, EvaSurtrKind kind)
		{
			switch (kind)
			{
			case EvaSurtrKind.Mist:
				((Humanoid)player).AddUniqueKey("talentpace_eva_cap_mist");
				break;
			case EvaSurtrKind.Ash:
				((Humanoid)player).AddUniqueKey("talentpace_eva_cap_ash");
				break;
			case EvaSurtrKind.Deep:
				((Humanoid)player).AddUniqueKey("talentpace_eva_cap_deep");
				break;
			}
			Pace.InvalidateStage(player);
			Pace.MarkDirty(player);
			Pace.InvalidateHintCache(player);
		}

		private static int CoinsFor(EvaSurtrKind kind)
		{
			return kind switch
			{
				EvaSurtrKind.Mist => Plugin.EvaCoinsMist.Value, 
				EvaSurtrKind.Ash => Plugin.EvaCoinsAsh.Value, 
				EvaSurtrKind.Deep => Plugin.EvaCoinsDeep.Value, 
				_ => 0, 
			};
		}

		private static string FirstRewardMessage(EvaSurtrKind kind, int coins)
		{
			switch (kind)
			{
			case EvaSurtrKind.Mist:
			case EvaSurtrKind.Ash:
				return $"Haldor accepts the shard: +{Plugin.EvaCapPerBoss.Value} talent limit (once per character), {coins} coins.";
			case EvaSurtrKind.Deep:
				return $"Haldor accepts the shard: +{Plugin.EvaJotunnCapBonus.Value} talent limit (once per character), {coins} coins.";
			default:
				return "Shard accepted.";
			}
		}

		private static bool TryMap(string prefab, out EvaSurtrKind kind)
		{
			kind = EvaSurtrKind.None;
			if (string.IsNullOrEmpty(prefab))
			{
				return false;
			}
			switch (prefab)
			{
			case "HBC_Shard_MistQueen":
			case "Cjayride_EvaSurtr_Mist":
				kind = EvaSurtrKind.Mist;
				return true;
			case "HBC_Shard_AshDemon":
			case "Cjayride_EvaSurtr_Ash":
				kind = EvaSurtrKind.Ash;
				return true;
			case "HBC_Shard_DeepJotunn":
			case "Cjayride_EvaSurtr_Deep":
				kind = EvaSurtrKind.Deep;
				return true;
			default:
				return false;
			}
		}

		private static bool RemoveOneFromSellSlot(Player player, ItemData item)
		{
			if (item.m_stack > 1)
			{
				item.m_stack--;
				((Humanoid)player).GetInventory().Changed(false, false);
				return true;
			}
			return ((Humanoid)player).GetInventory().RemoveItem(item);
		}

		private static void AddCoins(StoreGui storeGui, Player player, ItemData sold, int amount)
		{
			ItemDrop coinPrefab = storeGui.m_coinPrefab;
			if (!((Object)(object)coinPrefab == (Object)null) && amount > 0)
			{
				bool flag = ((Humanoid)player).GetInventory().ItemCheated(sold.m_shared.m_name, -1, true);
				((Humanoid)player).GetInventory().AddItem(((Object)((Component)coinPrefab).gameObject).name, amount, coinPrefab.m_itemData.m_quality, coinPrefab.m_itemData.m_variant, 0L, string.Empty, flag, false);
			}
		}
	}
	internal static class HaldorShardSell
	{
		private static FieldInfo _teSelectedItem;

		private static FieldInfo _teItemField;

		internal static bool TradersExtendedPatched;

		private static Harmony _harmony;

		internal static void Apply(Harmony harmony)
		{
			//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)
			//IL_0046: Expected O, but got Unknown
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Expected O, but got Unknown
			_harmony = harmony;
			harmony.Patch((MethodBase)AccessTools.Method(typeof(StoreGui), "SellItem", (Type[])null, (Type[])null), new HarmonyMethod(typeof(HaldorShardSell), "VanillaSellPrefix", (Type[])null)
			{
				priority = 800
			}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(StoreGui), "GetSellableItem", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(HaldorShardSell), "GetSellableItemPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			PatchTradersExtended(harmony);
		}

		internal static void PatchTradersExtended(Harmony harmony)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Expected O, but got Unknown
			if (TradersExtendedPatched)
			{
				return;
			}
			Type type = AccessTools.TypeByName("TradersExtended.StorePanel");
			if (!(type == null))
			{
				MethodInfo methodInfo = AccessTools.Method(type, "SellSelectedItem", new Type[1] { typeof(StoreGui) }, (Type[])null);
				if (methodInfo == null)
				{
					Plugin.Log("TradersExtended StorePanel.SellSelectedItem not found.");
					return;
				}
				_teSelectedItem = AccessTools.Field(type, "selectedItem");
				harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(HaldorShardSell), "TeSellPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				TradersExtendedPatched = true;
				Plugin.Log("Patched TradersExtended Haldor sell for HBC talent shards.");
			}
		}

		internal static void RetryTePatch()
		{
			if (_harmony != null)
			{
				PatchTradersExtended(_harmony);
			}
		}

		private static bool TeSellPrefix(StoreGui storeGui)
		{
			return !TrySellSelected(storeGui, GetTeSelectedItem());
		}

		private static bool VanillaSellPrefix(StoreGui __instance)
		{
			ItemData item = (TradersExtendedPatched ? GetTeSelectedItem() : ResolveVanillaItem(__instance));
			return !TrySellSelected(__instance, item);
		}

		private static void GetSellableItemPostfix(ref ItemData __result)
		{
			if (TradersExtendedPatched)
			{
				ItemData teSelectedItem = GetTeSelectedItem();
				if (teSelectedItem != null)
				{
					__result = teSelectedItem;
				}
			}
		}

		private static bool TrySellSelected(StoreGui storeGui, ItemData item)
		{
			if (item == null)
			{
				return false;
			}
			if (Plugin.EnableEvaSurtrRewards.Value && EvaSurtrRewards.TrySell(storeGui, item))
			{
				return true;
			}
			if (Plugin.EnableMlzHeroicRewards.Value && MlzHeroicRewards.TrySell(storeGui, item))
			{
				return true;
			}
			return false;
		}

		internal static ItemData GetTeSelectedItem()
		{
			if (_teSelectedItem == null)
			{
				return null;
			}
			object value = _teSelectedItem.GetValue(null);
			if (value == null)
			{
				return null;
			}
			if (_teItemField == null)
			{
				_teItemField = AccessTools.Field(value.GetType(), "item");
			}
			object? obj = _teItemField?.GetValue(value);
			return (ItemData)((obj is ItemData) ? obj : null);
		}

		private static ItemData ResolveVanillaItem(StoreGui storeGui)
		{
			object? obj = AccessTools.Method(typeof(StoreGui), "GetSellableItem", (Type[])null, (Type[])null)?.Invoke(storeGui, null);
			return (ItemData)((obj is ItemData) ? obj : null);
		}

		internal static string PrefabName(ItemData item)
		{
			if (item == null)
			{
				return string.Empty;
			}
			if ((Object)(object)item.m_dropPrefab != (Object)null)
			{
				string name = ((Object)item.m_dropPrefab).name;
				if (IsKnownShardId(name))
				{
					return name;
				}
			}
			string text = MatchObjectDbShared(item);
			if (!string.IsNullOrEmpty(text))
			{
				return text;
			}
			if ((Object)(object)item.m_dropPrefab != (Object)null)
			{
				return ((Object)item.m_dropPrefab).name;
			}
			return item.m_shared?.m_name ?? string.Empty;
		}

		internal static bool IsKnownShardId(string prefab)
		{
			switch (prefab)
			{
			default:
				return prefab == "Cjayride_MLZShard_AshQueen";
			case "HBC_Shard_MistQueen":
			case "HBC_Shard_AshDemon":
			case "HBC_Shard_DeepJotunn":
			case "HBC_Shard_Frigga":
			case "HBC_Shard_Balder":
			case "HBC_Shard_Kraken":
			case "HBC_Shard_Surtr":
			case "HBC_Shard_AshQueen":
			case "Cjayride_EvaSurtr_Mist":
			case "Cjayride_EvaSurtr_Ash":
			case "Cjayride_EvaSurtr_Deep":
			case "Cjayride_MLZShard_Frigga":
			case "Cjayride_MLZShard_Balder":
			case "Cjayride_MLZShard_Kraken":
			case "Cjayride_MLZShard_Surtr":
				return true;
			}
		}

		private static string MatchObjectDbShared(ItemData item)
		{
			if (item.m_shared == null || (Object)(object)ObjectDB.instance == (Object)null)
			{
				return null;
			}
			List<GameObject> items = ObjectDB.instance.m_items;
			if (items == null)
			{
				return null;
			}
			for (int i = 0; i < items.Count; i++)
			{
				GameObject val = items[i];
				if (!((Object)(object)val == (Object)null) && IsKnownShardId(((Object)val).name))
				{
					ItemDrop component = val.GetComponent<ItemDrop>();
					if ((Object)(object)component != (Object)null && component.m_itemData != null && component.m_itemData.m_shared == item.m_shared)
					{
						return ((Object)val).name;
					}
				}
			}
			return null;
		}
	}
	internal static class MlzHeroicRewards
	{
		private enum MlzHeroicKind
		{
			None,
			Frigga,
			Balder,
			Kraken,
			Surtr,
			AshQueen
		}

		internal const string PrefabFrigga = "HBC_Shard_Frigga";

		internal const string PrefabBalder = "HBC_Shard_Balder";

		internal const string PrefabKraken = "HBC_Shard_Kraken";

		internal const string PrefabSurtr = "HBC_Shard_Surtr";

		internal const string PrefabAshQueen = "HBC_Shard_AshQueen";

		private const string KeyFrigga = "talentpace_mlz_cap_frigga";

		private const string KeyBalder = "talentpace_mlz_cap_balder";

		private const string KeyKraken = "talentpace_mlz_cap_kraken";

		private const string KeySurtr = "talentpace_mlz_cap_surtr";

		private const string KeyAshQueen = "talentpace_mlz_cap_ashqueen";

		internal static int MlzCapBonus(Player player)
		{
			if ((Object)(object)player == (Object)null || !Plugin.EnableTalentPointCap.Value)
			{
				return 0;
			}
			int num = 0;
			if (((Humanoid)player).HaveUniqueKey("talentpace_mlz_cap_frigga"))
			{
				num += Plugin.MlzFriggaCapBonus.Value;
			}
			if (((Humanoid)player).HaveUniqueKey("talentpace_mlz_cap_balder"))
			{
				num += Plugin.MlzBalderCapBonus.Value;
			}
			if (((Humanoid)player).HaveUniqueKey("talentpace_mlz_cap_kraken"))
			{
				num += Plugin.MlzKrakenCapBonus.Value;
			}
			if (((Humanoid)player).HaveUniqueKey("talentpace_mlz_cap_surtr"))
			{
				num += Plugin.MlzSurtrCapBonus.Value;
			}
			if (((Humanoid)player).HaveUniqueKey("talentpace_mlz_cap_ashqueen"))
			{
				num += Plugin.MlzAshQueenCapBonus.Value;
			}
			return num;
		}

		internal static bool TrySell(StoreGui storeGui, ItemData item)
		{
			//IL_0099: 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_00ac: 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)
			if ((Object)(object)storeGui == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null || item == null)
			{
				return false;
			}
			if (!TryMap(HaldorShardSell.PrefabName(item), out var kind))
			{
				return false;
			}
			Player localPlayer = Player.m_localPlayer;
			bool flag = IsFirstReward(localPlayer, kind);
			int num = CoinsFor(kind);
			if (flag)
			{
				GrantFirstReward(localPlayer, kind);
			}
			if (!RemoveOneFromSellSlot(localPlayer, item))
			{
				return false;
			}
			if (num > 0)
			{
				AddCoins(storeGui, localPlayer, item, num);
			}
			string text = (flag ? FirstRewardMessage(kind, num) : $"Sold for {num} coins (talent reward already claimed).");
			((Character)localPlayer).Message((MessageType)1, text, 0, (Sprite)null, false);
			storeGui.m_sellEffects.Create(((Component)storeGui).transform.position, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
			Trader trader = storeGui.m_trader;
			if (trader != null)
			{
				trader.OnSold();
			}
			storeGui.FillList();
			return true;
		}

		private static bool IsFirstReward(Player player, MlzHeroicKind kind)
		{
			return !((Humanoid)player).HaveUniqueKey(UniqueKeyFor(kind));
		}

		private static string UniqueKeyFor(MlzHeroicKind kind)
		{
			return kind switch
			{
				MlzHeroicKind.Frigga => "talentpace_mlz_cap_frigga", 
				MlzHeroicKind.Balder => "talentpace_mlz_cap_balder", 
				MlzHeroicKind.Kraken => "talentpace_mlz_cap_kraken", 
				MlzHeroicKind.Surtr => "talentpace_mlz_cap_surtr", 
				MlzHeroicKind.AshQueen => "talentpace_mlz_cap_ashqueen", 
				_ => string.Empty, 
			};
		}

		private static void GrantFirstReward(Player player, MlzHeroicKind kind)
		{
			string text = UniqueKeyFor(kind);
			if (!string.IsNullOrEmpty(text))
			{
				((Humanoid)player).AddUniqueKey(text);
			}
			Pace.InvalidateStage(player);
			Pace.MarkDirty(player);
			Pace.InvalidateHintCache(player);
		}

		private static int CapFor(MlzHeroicKind kind)
		{
			return kind switch
			{
				MlzHeroicKind.Frigga => Plugin.MlzFriggaCapBonus.Value, 
				MlzHeroicKind.Balder => Plugin.MlzBalderCapBonus.Value