Decompiled source of IconFix v1.0.0

IconFix.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.UI.Youthcat;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: InternalsVisibleTo("IconFix.SelfTest")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TTenYX")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Add a checkbox to the icon picker: when on, picking an icon also inserts its name text | 图标选取新增勾选框:开启时选取图标会附带名称文本")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("IconFix")]
[assembly: AssemblyTitle("IconFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
internal static class PluginInfo
{
	public const string PLUGIN_GUID = "TTenYX.DSP.IconFix";

	public const string PLUGIN_NAME = "IconFix";

	public const string PLUGIN_VERSION = "1.0.0";
}
namespace IconFix
{
	[BepInPlugin("TTenYX.DSP.IconFix", "IconFix", "1.0.0")]
	public class IconFixPlugin : BaseUnityPlugin
	{
		public const string Guid = "TTenYX.DSP.IconFix";

		public const string Name = "IconFix";

		public const string Version = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> IncludeName;

		internal static ConfigEntry<bool> ShiftClickInsertName;

		internal static ConfigEntry<bool> ShiftClickUpgradeIconInText;

		internal static ConfigEntry<bool> DebugLog;

		private Harmony _harmony;

		internal static IconFixPlugin Instance { get; private set; }

		private void Awake()
		{
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			IncludeName = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "IncludeNameWhenPick", false, "Default state of the picker checkbox. When on, picking an icon also inserts its name text, e.g. \\meik-; (icon + name) instead of \\meik; (icon only). Changes in game are saved automatically.\n图标选取勾选框的默认状态。开启时,选取图标会同时插入名称文本,如 \\meik-;(图标+名称);关闭则仅插入图标 \\meik;。游戏内的改动会自动保存。");
			ShiftClickInsertName = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "ShiftClickInsertName", true, "When on, Shift+LeftClick on an icon temporarily inverts the checkbox state: with the checkbox off, Shift+Click inserts the named tag \\meik-;; with the checkbox on, it inserts the icon-only tag \\meik;.\n开启后,Shift+左键点击图标会临时取反勾选框状态:未勾选时按住 Shift 点击插入带名称的 \\meik-;;已勾选时按住 Shift 点击则插入纯图标 \\meik;。");
			ShiftClickUpgradeIconInText = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "ShiftClickUpgradeIconInText", true, "When on, Shift+LeftClick on a plain icon inside a text field upgrades it in place to the named version, e.g. \\meik; becomes \\meik-;. Works in blueprint descriptions, names, planet/star memos and marker comments. Ctrl+Click still opens the item details.\n开启后,在文本框中按住 Shift 左键点击不含名称的图标,会原地替换为带名称图标(如 \\meik; 变为 \\meik-;)。适用于蓝图介绍、名称、星球/恒星备注与行星标记备注。Ctrl+点击仍是查看详情。");
			DebugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "DebugLog", true, "Print one log line per Shift+Click icon-upgrade attempt to BepInEx/LogOutput.log (for troubleshooting).\n每次 Shift+点击图标升级尝试都会在 BepInEx/LogOutput.log 输出一行日志(用于排查)。");
			_harmony = new Harmony("TTenYX.DSP.IconFix");
			try
			{
				IconPickerPatches.Apply(_harmony);
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Patch failed: {arg}");
			}
			Log.LogInfo((object)string.Format("{0} {1} loaded. includeName(default) = {2}", "IconFix", "1.0.0", IncludeName.Value));
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
			IconHoverEnabler.Tick();
		}
	}
	internal static class IconFixState
	{
		public static bool ApplyShift(bool baseValue, bool shiftHeld)
		{
			if (!shiftHeld || IconFixPlugin.ShiftClickInsertName == null || !IconFixPlugin.ShiftClickInsertName.Value)
			{
				return baseValue;
			}
			return !baseValue;
		}

		public static bool EffectiveIncludeName()
		{
			bool shiftHeld = Input.GetKey((KeyCode)304) || Input.GetKey((KeyCode)303);
			return ApplyShift(IconFixPlugin.IncludeName.Value, shiftHeld);
		}
	}
	internal static class IconFixText
	{
		public static string ToggleLabel => L("图标附带名称", "Icon With Name");

		public static string ToggleTipTitle => L("图标附带名称", "Icon With Name");

		public static string ToggleTipText => L("开启时,点选图标会同时插入名称文本(\\meik-;,图标+名称);关闭则仅插入图标(\\meik;)。\\n按住 Shift 点击图标可临时取反本选项。\\n在文本框中按住 Shift 点击已插入的纯图标,可原地补上名称(Ctrl+点击为查看详情)。", "When on, clicking an icon also inserts its name text (\\meik-;, icon + name); when off, only the icon (\\meik;) is inserted.\\nHold Shift while clicking an icon to temporarily invert this option.\\nHold Shift and click a plain icon inside a text field to add its name in place (Ctrl+Click opens the item details).");

		public static string L(string zh, string en)
		{
			try
			{
				return Localization.isZHCN ? zh : en;
			}
			catch
			{
				return en;
			}
		}
	}
	internal static class IconHoverEnabler
	{
		private const float ScanInterval = 1f;

		private static float _nextScan;

		private static readonly ConditionalWeakTable<Text, object> Hooked = new ConditionalWeakTable<Text, object>();

		public static void Tick()
		{
			if (Time.unscaledTime < _nextScan)
			{
				return;
			}
			_nextScan = Time.unscaledTime + 1f;
			try
			{
				TextEventTrigger[] array = Resources.FindObjectsOfTypeAll<TextEventTrigger>();
				foreach (TextEventTrigger val in array)
				{
					if ((Object)(object)val == (Object)null)
					{
						continue;
					}
					InputField field = (((Object)(object)val.inputField != (Object)null) ? val.inputField : ((Component)val).GetComponentInParent<InputField>(true));
					if ((Object)(object)field == (Object)null)
					{
						continue;
					}
					val.enableIconHover = true;
					Text component = ((Component)val).GetComponent<Text>();
					if ((Object)(object)component != (Object)null && !Hooked.TryGetValue(component, out var _))
					{
						Hooked.Add(component, null);
						component.onTriggerClick += delegate(PointerEventData e, TextTriggerBounds bounds)
						{
							//IL_0007: Unknown result type (might be due to invalid IL or missing references)
							RawDispatch(field, e, bounds);
						};
					}
				}
			}
			catch
			{
			}
		}

		private static void RawDispatch(InputField field, PointerEventData e, TextTriggerBounds bounds)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)field == (Object)null))
			{
				UIMarkerDesc componentInParent = ((Component)field).GetComponentInParent<UIMarkerDesc>(true);
				if ((Object)(object)componentInParent != (Object)null && (Object)(object)componentInParent.todoInputField == (Object)(object)field)
				{
					IconTextUpgrader.HandleMarker(componentInParent, e, bounds);
				}
				else if ((Object)(object)((Component)field).GetComponentInParent<UISmartInputField>(true) != (Object)null)
				{
					IconTextUpgrader.HandleSmart(field, e, bounds);
				}
				else
				{
					IconTextUpgrader.Handle(field, e, bounds);
				}
			}
		}
	}
	internal static class IconPickerPatches
	{
		public static void Apply(Harmony harmony)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Expected O, but got Unknown
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Expected O, but got Unknown
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Expected O, but got Unknown
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Expected O, but got Unknown
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Expected O, but got Unknown
			HarmonyMethod val = new HarmonyMethod(typeof(IconPickerPatches), "UIInputField_Return_Prefix", (Type[])null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(UIInputField), "OnIconSelectSignalPickerReturn", (Type[])null, (Type[])null), val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(UISmartInputField), "OnIconSelectSignalPickerReturn", (Type[])null, (Type[])null), new HarmonyMethod(typeof(IconPickerPatches), "UISmartInputField_Return_Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(UIMarkerDesc), "OnTodoIconSelectSignalPickerReturn", (Type[])null, (Type[])null), new HarmonyMethod(typeof(IconPickerPatches), "UIMarkerTodo_Return_Prefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(UISignalTagPicker), "_OnCreate", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(IconPickerPatches), "SignalTagPicker_OnCreate_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(UIInputField), "OnTextTriggerClick", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(IconPickerPatches), "UIInputField_Trigger_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(UISmartInputField), "OnTextTriggerClick", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(IconPickerPatches), "UISmartInputField_Trigger_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(UIMarkerDesc), "OnTextTriggerClick", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(IconPickerPatches), "UIMarkerTodo_Trigger_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static string IconTag(int signalId)
		{
			return LDB.signals.IconTag(signalId, IconFixState.EffectiveIncludeName());
		}

		private static bool UIInputField_Return_Prefix(UIInputField __instance, int signalId)
		{
			if (signalId == 0 || (Object)(object)__instance.inputField == (Object)null || !__instance.inputField.isFocused)
			{
				return false;
			}
			__instance.inputField.InsertGeneralString(IconTag(signalId), true);
			return false;
		}

		private static bool UISmartInputField_Return_Prefix(UISmartInputField __instance, int signalId)
		{
			if (signalId == 0 || (Object)(object)__instance.inputField == (Object)null)
			{
				return false;
			}
			__instance.inputField.InsertGeneralString(IconTag(signalId), true);
			return false;
		}

		private static bool UIMarkerTodo_Return_Prefix(UIMarkerDesc __instance, int signalId)
		{
			if (signalId == 0 || __instance.marker == null || __instance.marker.id == 0)
			{
				return false;
			}
			Traverse val = Traverse.Create((object)__instance);
			if (val.Field("_event_lock").GetValue<bool>())
			{
				return false;
			}
			InputField todoInputField = __instance.todoInputField;
			if ((Object)(object)todoInputField == (Object)null)
			{
				return false;
			}
			todoInputField.InsertGeneralString(IconTag(signalId), true);
			if (__instance.factory != null)
			{
				__instance.factory.WriteMarkerComment(__instance.marker.id, todoInputField.text);
			}
			try
			{
				object[] parameters = new object[4]
				{
					val.Property("advancedSettingsOpened", (object[])null).GetValue<bool>(),
					0,
					0,
					true
				};
				AccessTools.Method(typeof(UIMarkerDesc), "AdaptiveWindowRect", (Type[])null, (Type[])null).Invoke(__instance, parameters);
				val.Method("HandleTodoInputFieldHeight", Array.Empty<object>()).GetValue();
			}
			catch (Exception ex)
			{
				IconFixPlugin.Log.LogWarning((object)("marker layout refresh failed: " + ex.Message));
			}
			return false;
		}

		private static void SignalTagPicker_OnCreate_Postfix(UISignalTagPicker __instance)
		{
			try
			{
				PickerToggleUI.EnsureCreated(__instance);
			}
			catch (Exception arg)
			{
				IconFixPlugin.Log.LogWarning((object)$"toggle UI init failed: {arg}");
			}
		}

		private static void UIInputField_Trigger_Postfix(UIInputField __instance, PointerEventData e, TextTriggerBounds bounds)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				IconTextUpgrader.Handle(__instance.inputField, e, bounds);
			}
			catch (Exception ex)
			{
				IconFixPlugin.Log.LogWarning((object)("icon upgrade failed: " + ex.Message));
			}
		}

		private static void UISmartInputField_Trigger_Postfix(UISmartInputField __instance, PointerEventData e, TextTriggerBounds bounds)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				IconTextUpgrader.HandleSmart(__instance.inputField, e, bounds);
			}
			catch (Exception ex)
			{
				IconFixPlugin.Log.LogWarning((object)("icon upgrade failed: " + ex.Message));
			}
		}

		private static void UIMarkerTodo_Trigger_Postfix(UIMarkerDesc __instance, PointerEventData e, TextTriggerBounds bounds)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				IconTextUpgrader.HandleMarker(__instance, e, bounds);
			}
			catch (Exception ex)
			{
				IconFixPlugin.Log.LogWarning((object)("icon upgrade failed: " + ex.Message));
			}
		}
	}
	internal static class IconTextUpgrader
	{
		public enum UpgradeResult
		{
			NotFound,
			AlreadyNamed,
			Replaced
		}

		private static int _lastFrame = -1;

		public static string UpgradeAtCaret(string text, int caret, out int newCaret, out UpgradeResult result)
		{
			newCaret = -1;
			result = UpgradeResult.NotFound;
			if (string.IsNullOrEmpty(text))
			{
				return null;
			}
			if (caret < 0)
			{
				caret = 0;
			}
			if (caret > text.Length)
			{
				caret = text.Length;
			}
			for (int i = 0; i < 2; i++)
			{
				int num = caret - i;
				if (num < 0 || num >= text.Length)
				{
					continue;
				}
				int num2 = -1;
				int num3 = Math.Min(text.Length, num + 32);
				for (int j = num; j < num3; j++)
				{
					if (text[j] == ';')
					{
						num2 = j;
						break;
					}
				}
				if (num2 < 0)
				{
					continue;
				}
				int num4 = num2 - 1;
				bool flag = false;
				while (num4 >= 0)
				{
					char c = text[num4];
					if (c == '-')
					{
						flag = true;
						num4--;
						continue;
					}
					if (!char.IsLetterOrDigit(c))
					{
						break;
					}
					num4--;
				}
				if (num4 >= 0 && text[num4] == '\\' && num4 <= num && num2 >= num)
				{
					if (flag)
					{
						result = UpgradeResult.AlreadyNamed;
						return null;
					}
					StringBuilder stringBuilder = new StringBuilder(text.Length + 1);
					stringBuilder.Append(text, 0, num2);
					stringBuilder.Append('-');
					stringBuilder.Append(text, num2, text.Length - num2);
					newCaret = num2 + 2;
					result = UpgradeResult.Replaced;
					return stringBuilder.ToString();
				}
			}
			return null;
		}

		private static void DebugLog(string msg)
		{
			if (IconFixPlugin.DebugLog != null && IconFixPlugin.DebugLog.Value)
			{
				IconFixPlugin.Log.LogInfo((object)("[upgrade] " + msg));
			}
		}

		private static string Describe(InputField field)
		{
			if ((Object)(object)field == (Object)null)
			{
				return "<null>";
			}
			string text = ((Object)((Component)field).gameObject).name;
			Transform parent = ((Component)field).transform.parent;
			int num = 0;
			while ((Object)(object)parent != (Object)null && num < 3)
			{
				text = ((Object)parent).name + "/" + text;
				num++;
				parent = parent.parent;
			}
			return ((object)field).GetType().Name + "(" + text + ")";
		}

		private static bool CanProcess(InputField field, PointerEventData e, TextTriggerBounds bounds)
		{
			//IL_0019: 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_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			if (IconFixPlugin.ShiftClickUpgradeIconInText == null || !IconFixPlugin.ShiftClickUpgradeIconInText.Value)
			{
				return false;
			}
			if (e == null || (int)e.button != 0)
			{
				DebugLog($"skip: button={((e != null) ? new InputButton?(e.button) : ((InputButton?)null))} field={Describe(field)}");
				return false;
			}
			if (!Input.GetKey((KeyCode)304) && !Input.GetKey((KeyCode)303))
			{
				DebugLog("skip: no shift field=" + Describe(field));
				return false;
			}
			if (VFInput.control)
			{
				DebugLog("skip: ctrl held field=" + Describe(field));
				return false;
			}
			if (bounds.type != 2)
			{
				DebugLog($"skip: bounds.type={bounds.type} iconId={bounds.iconId} field={Describe(field)}");
				return false;
			}
			return true;
		}

		private static void Apply(InputField field, string newText, int caret)
		{
			field.text = newText;
			field.caretPosition = caret;
			field.selectionAnchorPosition = caret;
			field.selectionFocusPosition = caret;
			field.ForceLabelUpdate();
		}

		private static void Finish(InputField field, TextTriggerBounds bounds, UpgradeResult result, int caret)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			DebugLog($"{result}: caret={caret} bounds(index={bounds.index},iconId={bounds.iconId},type={bounds.type}) field={Describe(field)}");
			_lastFrame = Time.frameCount;
		}

		public static void Handle(InputField field, PointerEventData e, TextTriggerBounds bounds)
		{
			//IL_000b: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)field == (Object)null) && CanProcess(field, e, bounds) && _lastFrame != Time.frameCount)
			{
				int caretPosition = field.caretPosition;
				int newCaret;
				UpgradeResult result;
				string text = UpgradeAtCaret(field.text, caretPosition, out newCaret, out result);
				if (text == null)
				{
					Finish(field, bounds, result, caretPosition);
					return;
				}
				Apply(field, text, newCaret);
				Finish(field, bounds, result, newCaret);
			}
		}

		public static void HandleSmart(InputField field, PointerEventData e, TextTriggerBounds bounds)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)field == (Object)null) && CanProcess(field, e, bounds) && _lastFrame != Time.frameCount)
			{
				int caretPosition = field.caretPosition;
				int newCaret;
				UpgradeResult result;
				string text = UpgradeAtCaret(field.text, caretPosition, out newCaret, out result);
				if (text == null)
				{
					Finish(field, bounds, result, caretPosition);
					return;
				}
				Apply(field, text, newCaret);
				field.CalculateColorText(false);
				Finish(field, bounds, result, newCaret);
			}
		}

		public static void HandleMarker(UIMarkerDesc marker, PointerEventData e, TextTriggerBounds bounds)
		{
			//IL_0134: 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_0062: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)marker == (Object)null || marker.marker == null || marker.marker.id == 0)
			{
				return;
			}
			InputField todoInputField = marker.todoInputField;
			if ((Object)(object)todoInputField == (Object)null || !CanProcess(todoInputField, e, bounds) || _lastFrame == Time.frameCount)
			{
				return;
			}
			int caretPosition = todoInputField.caretPosition;
			int newCaret;
			UpgradeResult result;
			string text = UpgradeAtCaret(todoInputField.text, caretPosition, out newCaret, out result);
			if (text == null)
			{
				Finish(todoInputField, bounds, result, caretPosition);
				return;
			}
			Apply(todoInputField, text, newCaret);
			PlanetFactory factory = marker.factory;
			if (factory != null)
			{
				factory.WriteMarkerComment(marker.marker.id, todoInputField.text);
			}
			try
			{
				Traverse val = Traverse.Create((object)marker);
				object[] parameters = new object[4]
				{
					val.Property("advancedSettingsOpened", (object[])null).GetValue<bool>(),
					0,
					0,
					true
				};
				AccessTools.Method(typeof(UIMarkerDesc), "AdaptiveWindowRect", (Type[])null, (Type[])null).Invoke(marker, parameters);
				val.Method("HandleTodoInputFieldHeight", Array.Empty<object>()).GetValue();
			}
			catch (Exception ex)
			{
				IconFixPlugin.Log.LogWarning((object)("marker layout refresh failed: " + ex.Message));
			}
			Finish(todoInputField, bounds, result, newCaret);
		}
	}
	internal static class PickerToggleUI
	{
		public const string ObjectName = "iconfix-include-name-toggle";

		private const float Gap = 10f;

		private const float LabelStartX = 24f;

		private static UIButton _button;

		private static Image _checkImage;

		private static Text _label;

		public static void EnsureCreated(UISignalTagPicker picker)
		{
			if ((Object)(object)picker == (Object)null || (Object)(object)picker.displayUnlockBtn == (Object)null || (Object)(object)((Component)picker.displayUnlockBtn).transform.parent == (Object)null)
			{
				return;
			}
			Transform parent = ((Component)picker.displayUnlockBtn).transform.parent;
			Transform val = parent.Find("iconfix-include-name-toggle");
			if ((Object)(object)val != (Object)null)
			{
				_button = ((Component)val).GetComponent<UIButton>();
				_checkImage = FindCheckImage(picker, ((Component)val).gameObject);
				_label = ((Component)val).GetComponentInChildren<Text>(true);
				if ((Object)(object)_label != (Object)null)
				{
					_label.text = IconFixText.ToggleLabel;
					FixLabelRect(_label);
				}
				RefreshVisual(picker);
			}
			else
			{
				CreateToggle(picker, parent);
				RefreshVisual(picker);
			}
		}

		private static void CreateToggle(UISignalTagPicker picker, Transform parent)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Expected O, but got Unknown
			//IL_0100: 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_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: 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_00cb: Unknown result type (might be due to invalid IL or missing references)
			UIButton displayUnlockBtn = picker.displayUnlockBtn;
			RectTransform srcRT = (RectTransform)((Component)displayUnlockBtn).transform;
			GameObject val = Object.Instantiate<GameObject>(((Component)displayUnlockBtn).gameObject, parent);
			((Object)val).name = "iconfix-include-name-toggle";
			Localizer[] componentsInChildren = val.GetComponentsInChildren<Localizer>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Object.Destroy((Object)(object)componentsInChildren[i]);
			}
			_button = val.GetComponent<UIButton>();
			if ((Object)(object)_button != (Object)null)
			{
				_button.onClick += OnToggleClick;
				TipSettings tips = _button.tips;
				tips.tipTitle = IconFixText.ToggleTipTitle;
				tips.tipText = IconFixText.ToggleTipText;
				tips.type = (ItemTipType)2;
				tips.delay = 0.2f;
				tips.topLevel = true;
				_button.tips = tips;
			}
			_label = FindSingleLabel(val);
			if ((Object)(object)_label == (Object)null)
			{
				GameObject val2 = new GameObject("label", new Type[1] { typeof(Text) });
				val2.transform.SetParent(val.transform, false);
				_label = val2.GetComponent<Text>();
				_label.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
				_label.fontSize = 14;
				((Graphic)_label).color = Color.white;
			}
			_label.text = IconFixText.ToggleLabel;
			_label.alignment = (TextAnchor)3;
			_label.horizontalOverflow = (HorizontalWrapMode)1;
			_label.verticalOverflow = (VerticalWrapMode)1;
			((Graphic)_label).raycastTarget = false;
			FixLabelRect(_label);
			_checkImage = FindCheckImage(picker, val);
			Layout(picker, srcRT, (RectTransform)val.transform);
		}

		private static void FixLabelRect(Text label)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			RectTransform val = (RectTransform)((Component)label).transform;
			val.anchorMin = new Vector2(0f, 0.5f);
			val.anchorMax = new Vector2(0f, 0.5f);
			val.pivot = new Vector2(0f, 0.5f);
			val.anchoredPosition = new Vector2(24f, 0f);
			val.sizeDelta = new Vector2(EstimateTextWidth(label), 30f);
		}

		private static Text FindSingleLabel(GameObject go)
		{
			Text[] componentsInChildren = go.GetComponentsInChildren<Text>(true);
			if (componentsInChildren == null || componentsInChildren.Length == 0)
			{
				return null;
			}
			Text result = componentsInChildren[0];
			for (int i = 1; i < componentsInChildren.Length; i++)
			{
				Object.Destroy((Object)(object)((Component)componentsInChildren[i]).gameObject);
			}
			return result;
		}

		private static Image FindCheckImage(UISignalTagPicker picker, GameObject clone)
		{
			Image checkbox = picker.checkbox;
			Transform transform = ((Component)picker.displayUnlockBtn).transform;
			if ((Object)(object)checkbox == (Object)null)
			{
				return clone.GetComponent<Image>();
			}
			if ((Object)(object)((Component)checkbox).transform == (Object)(object)transform)
			{
				return clone.GetComponent<Image>();
			}
			List<string> list = new List<string>();
			Transform val = ((Component)checkbox).transform;
			while ((Object)(object)val != (Object)null && (Object)(object)val != (Object)(object)transform)
			{
				list.Add(((Object)val).name);
				val = val.parent;
			}
			if ((Object)(object)val == (Object)(object)transform)
			{
				list.Reverse();
				string text = string.Join("/", list.ToArray());
				Transform val2 = clone.transform.Find(text);
				if ((Object)(object)val2 != (Object)null)
				{
					Image component = ((Component)val2).GetComponent<Image>();
					if ((Object)(object)component != (Object)null)
					{
						return component;
					}
				}
			}
			return clone.GetComponent<Image>();
		}

		private static void Layout(UISignalTagPicker picker, RectTransform srcRT, RectTransform myRT)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: 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_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			myRT.anchorMin = srcRT.anchorMin;
			myRT.anchorMax = srcRT.anchorMax;
			myRT.pivot = srcRT.pivot;
			((Transform)myRT).localScale = ((Transform)srcRT).localScale;
			Rect rect = srcRT.rect;
			float num;
			if (!(((Rect)(ref rect)).height > 0f))
			{
				num = srcRT.sizeDelta.y;
			}
			else
			{
				rect = srcRT.rect;
				num = ((Rect)(ref rect)).height;
			}
			float num2 = num;
			float num3 = 0f;
			if ((Object)(object)_label != (Object)null)
			{
				num3 = EstimateTextWidth(_label);
			}
			float num4 = Mathf.Max(48f, 24f + num3 + 10f);
			float x = srcRT.anchoredPosition.x;
			rect = srcRT.rect;
			float num5 = x - ((Rect)(ref rect)).width * srcRT.pivot.x;
			myRT.sizeDelta = new Vector2(num4, num2);
			myRT.anchoredPosition = new Vector2(num5 - 10f, srcRT.anchoredPosition.y);
		}

		private static float EstimateTextWidth(Text label)
		{
			if ((Object)(object)label == (Object)null || string.IsNullOrEmpty(label.text))
			{
				return 0f;
			}
			float num = 0f;
			string text = label.text;
			foreach (char c in text)
			{
				num += ((c >= '⺀') ? ((float)label.fontSize) : ((float)label.fontSize * 0.62f));
			}
			return num + 4f;
		}

		private static void OnToggleClick(int obj)
		{
			IconFixPlugin.IncludeName.Value = !IconFixPlugin.IncludeName.Value;
			((BaseUnityPlugin)IconFixPlugin.Instance).Config.Save();
			RefreshVisual(CurrentPicker());
		}

		private static UISignalTagPicker CurrentPicker()
		{
			UIRoot instance = UIRoot.instance;
			UIGame val = (((Object)(object)instance != (Object)null) ? instance.uiGame : null);
			if (!((Object)(object)val != (Object)null))
			{
				return null;
			}
			return val.signalTagPicker;
		}

		public static void RefreshVisual(UISignalTagPicker picker)
		{
			bool value = IconFixPlugin.IncludeName.Value;
			if ((Object)(object)_checkImage != (Object)null && (Object)(object)picker != (Object)null)
			{
				_checkImage.sprite = (value ? picker.checkboxOn : picker.checkboxOff);
			}
			if ((Object)(object)_label != (Object)null)
			{
				_label.text = IconFixText.ToggleLabel;
			}
		}
	}
}