Decompiled source of FeederName v1.2.4

plugins/FeederName.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("0.0.0.0")]
[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 FeederName
{
	internal sealed class FeederNameConfig
	{
		public ConfigEntry<bool> Enabled;

		public ConfigEntry<int> MaxChars;

		public ConfigEntry<float> OffsetX;

		public ConfigEntry<float> OffsetY;

		public ConfigEntry<float> FontSize;

		public ConfigEntry<string> TextColor;

		public ConfigEntry<float> KeepAfterFeed;

		public ConfigEntry<bool> ShowItemName;

		public ConfigEntry<bool> ShowTargetName;

		public static FeederNameConfig Instance { get; internal set; }

		public void Init(ConfigFile config)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Expected O, but got Unknown
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Expected O, but got Unknown
			Enabled = config.Bind<bool>("显示", "显示喂食者名字", true, "总开关。关闭后不再为喂食图标显示名字。");
			MaxChars = config.Bind<int>("显示", "名字最大字符数", 4, new ConfigDescription("名字最多显示几个字符(1 个中文字算 1 个字符),超出部分直接截断,0 表示不限制。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 50), Array.Empty<object>()));
			OffsetX = config.Bind<float>("显示", "水平偏移", 0f, "名字相对喂食图标顶部的水平偏移(像素),正数向右。");
			OffsetY = config.Bind<float>("显示", "垂直偏移", 20f, "名字相对喂食图标顶部的垂直偏移(像素),正数向上。");
			FontSize = config.Bind<float>("显示", "字号", 24f, new ConfigDescription("名字字号(像素),数值越大字越大。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(6f, 128f), Array.Empty<object>()));
			TextColor = config.Bind<string>("显示", "文字颜色", "#FFFFFF", "名字颜色,格式 #RRGGBB 或 #RRGGBBAA。");
			KeepAfterFeed = config.Bind<float>("显示", "喂食结束后保持显示", 2f, new ConfigDescription("喂食结束后保持显示名字的秒数,0 表示不保持(喂食结束立即消失)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 30f), Array.Empty<object>()));
			ShowItemName = config.Bind<bool>("显示", "显示喂食物品", true, "是否在名字后显示喂食的物品名称,如「张三 (苹果)」。");
			ShowTargetName = config.Bind<bool>("显示", "显示喂食目标", true, "是否在我喂食时显示被喂食者的名字。");
		}
	}
	internal sealed class FeederNameLabel : MonoBehaviour
	{
		private UI_UseItemProgressFriend _target;

		private TextMeshProUGUI _label;

		private int _giverID;

		private ushort _itemID;

		private string _appliedText;

		private float _appliedFontSize = -1f;

		private string _appliedColorHex;

		private Vector2 _appliedPosition = new Vector2(float.NaN, float.NaN);

		private float _nextRetry;

		private string _cachedName;

		private string _cachedItemName;

		private bool _nameResolved;

		public static void Attach(UI_UseItemProgressFriend target, int giverID, ushort itemID)
		{
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)target == (Object)null || (Object)(object)((Component)target).GetComponentInChildren<FeederNameLabel>() != (Object)null)
			{
				return;
			}
			try
			{
				RectTransform val = (((Object)(object)target.icon != (Object)null) ? ((Graphic)target.icon).rectTransform : null);
				if ((Object)(object)val == (Object)null)
				{
					val = target.rect;
				}
				if ((Object)(object)val == (Object)null)
				{
					Transform transform = ((Component)target).transform;
					val = (RectTransform)(object)((transform is RectTransform) ? transform : null);
				}
				if (!((Object)(object)val == (Object)null))
				{
					GameObject val2 = new GameObject("FeederNameLabel", new Type[1] { typeof(RectTransform) })
					{
						hideFlags = (HideFlags)61
					};
					val2.transform.SetParent((Transform)(object)val, false);
					RectTransform component = val2.GetComponent<RectTransform>();
					component.anchorMin = new Vector2(0.5f, 1f);
					component.anchorMax = new Vector2(0.5f, 1f);
					component.pivot = new Vector2(0.5f, 1f);
					component.sizeDelta = new Vector2(200f, 40f);
					TextMeshProUGUI val3 = val2.AddComponent<TextMeshProUGUI>();
					((TMP_Text)val3).text = "";
					((TMP_Text)val3).alignment = (TextAlignmentOptions)514;
					((Graphic)val3).raycastTarget = false;
					((TMP_Text)val3).enableAutoSizing = false;
					TMP_FontAsset font = NameFontProvider.GetFont();
					if ((Object)(object)font != (Object)null)
					{
						((TMP_Text)val3).font = font;
					}
					else
					{
						PluginInfo.Warn("未找到合适的中文字体,喂食者名字可能显示为方块(使用 TMP 默认字体)");
					}
					FeederNameLabel feederNameLabel = val2.AddComponent<FeederNameLabel>();
					feederNameLabel._target = target;
					feederNameLabel._label = val3;
					feederNameLabel._giverID = giverID;
					feederNameLabel._itemID = itemID;
				}
			}
			catch (Exception ex)
			{
				PluginInfo.Warn("附加喂食者名字标签失败:" + ex.Message);
			}
		}

		private void OnDestroy()
		{
			_label = null;
		}

		private void Update()
		{
			FeederNameConfig instance = FeederNameConfig.Instance;
			if (instance == null)
			{
				return;
			}
			if (!instance.Enabled.Value)
			{
				HideLabel();
				return;
			}
			if ((Object)(object)_target == (Object)null || (Object)(object)_label == (Object)null)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			if (!_nameResolved && Time.unscaledTime >= _nextRetry)
			{
				_cachedName = ResolveName();
				_cachedItemName = ResolveItemName(_itemID);
				_nextRetry = Time.unscaledTime + 0.5f;
				if (!string.IsNullOrEmpty(_cachedName))
				{
					_nameResolved = true;
				}
			}
			if (string.IsNullOrEmpty(_cachedName))
			{
				HideLabel();
				return;
			}
			string text = BuildDisplayName(_cachedName, _cachedItemName, instance);
			if (!string.Equals(_appliedText, text, StringComparison.Ordinal))
			{
				((TMP_Text)_label).text = text;
				((Graphic)_label).SetVerticesDirty();
				((Graphic)_label).SetLayoutDirty();
				_appliedText = text;
			}
			ApplyStyle(instance);
		}

		public void CreatePersistentLabel()
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: 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_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			FeederNameConfig instance = FeederNameConfig.Instance;
			if (instance == null || instance.KeepAfterFeed.Value <= 0f || _giverID <= 0)
			{
				return;
			}
			try
			{
				Transform val = (((Object)(object)GUIManager.instance != (Object)null) ? GUIManager.instance.friendProgressTF : null);
				if ((Object)(object)val == (Object)null)
				{
					PluginInfo.Warn("无法找到持久父节点,跳过创建独立标签");
					return;
				}
				GameObject val2 = new GameObject("FeederNamePersistentLabel", new Type[1] { typeof(RectTransform) });
				((Object)val2).hideFlags = (HideFlags)61;
				val2.transform.SetParent(val, false);
				if ((Object)(object)_label != (Object)null && (Object)(object)((TMP_Text)_label).rectTransform != (Object)null)
				{
					val2.transform.position = ((Transform)((TMP_Text)_label).rectTransform).position;
				}
				RectTransform component = val2.GetComponent<RectTransform>();
				component.anchorMin = new Vector2(0.5f, 1f);
				component.anchorMax = new Vector2(0.5f, 1f);
				component.pivot = new Vector2(0.5f, 1f);
				component.sizeDelta = new Vector2(200f, 40f);
				TextMeshProUGUI val3 = val2.AddComponent<TextMeshProUGUI>();
				((TMP_Text)val3).alignment = (TextAlignmentOptions)514;
				((Graphic)val3).raycastTarget = false;
				((TMP_Text)val3).enableAutoSizing = false;
				TMP_FontAsset font = NameFontProvider.GetFont();
				if ((Object)(object)font != (Object)null)
				{
					((TMP_Text)val3).font = font;
				}
				((TMP_Text)val3).fontSize = instance.FontSize.Value;
				if (HexColor.TryParse(instance.TextColor.Value, out var color))
				{
					((Graphic)val3).color = color;
				}
				val2.AddComponent<PersistentLabelController>().Initialize(_giverID, _itemID, instance.KeepAfterFeed.Value);
			}
			catch (Exception ex)
			{
				PluginInfo.Warn("创建独立标签失败:" + ex.Message);
			}
		}

		private string BuildDisplayName(string name, string itemName, FeederNameConfig cfg)
		{
			if (string.IsNullOrEmpty(name))
			{
				return string.Empty;
			}
			name = Truncate(name, cfg.MaxChars.Value);
			if (!cfg.ShowItemName.Value || _itemID == 0)
			{
				return name;
			}
			if (string.IsNullOrEmpty(itemName))
			{
				return name;
			}
			return name + " (" + Truncate(itemName, cfg.MaxChars.Value) + ")";
		}

		private string ResolveName()
		{
			if (_giverID <= 0)
			{
				return null;
			}
			Character val = default(Character);
			if (Character.GetCharacterWithPhotonID(_giverID, ref val) && (Object)(object)val != (Object)null && !string.IsNullOrEmpty(val.characterName))
			{
				return val.characterName;
			}
			try
			{
				PhotonView photonView = PhotonNetwork.GetPhotonView(_giverID);
				if ((Object)(object)photonView != (Object)null && photonView.Owner != null)
				{
					string nickName = photonView.Owner.NickName;
					if (!string.IsNullOrEmpty(nickName))
					{
						return nickName;
					}
				}
			}
			catch
			{
			}
			return null;
		}

		private static string ResolveItemName(ushort itemID)
		{
			if (itemID == 0)
			{
				return null;
			}
			try
			{
				Item val = default(Item);
				if (ItemDatabase.TryGetItem(itemID, ref val) && (Object)(object)val != (Object)null)
				{
					string name = val.GetName();
					if (!string.IsNullOrEmpty(name))
					{
						return name;
					}
				}
			}
			catch
			{
			}
			return null;
		}

		private static string Truncate(string name, int maxChars)
		{
			if (string.IsNullOrEmpty(name))
			{
				return string.Empty;
			}
			if (maxChars <= 0 || name.Length <= maxChars)
			{
				return name;
			}
			return name.Substring(0, maxChars);
		}

		private void HideLabel()
		{
			if (!((Object)(object)_label == (Object)null) && !string.IsNullOrEmpty(_appliedText))
			{
				((TMP_Text)_label).text = string.Empty;
				_appliedText = string.Empty;
			}
		}

		private void ApplyStyle(FeederNameConfig cfg)
		{
			//IL_00d4: 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_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_label == (Object)null))
			{
				if (Mathf.Abs(cfg.FontSize.Value - _appliedFontSize) > 0.001f)
				{
					((TMP_Text)_label).fontSize = cfg.FontSize.Value;
					_appliedFontSize = cfg.FontSize.Value;
				}
				string value = cfg.TextColor.Value;
				if (!string.Equals(value, _appliedColorHex, StringComparison.Ordinal) && HexColor.TryParse(value, out var color))
				{
					((Graphic)_label).color = color;
					_appliedColorHex = value;
				}
				Vector2 val = default(Vector2);
				((Vector2)(ref val))..ctor(cfg.OffsetX.Value, cfg.OffsetY.Value);
				if (float.IsNaN(_appliedPosition.x) || _appliedPosition != val)
				{
					((TMP_Text)_label).rectTransform.anchoredPosition = val;
					_appliedPosition = val;
				}
			}
		}
	}
	internal sealed class PersistentLabelController : MonoBehaviour
	{
		private TextMeshProUGUI _label;

		private int _giverID;

		private ushort _itemID;

		private float _destroyTime;

		private string _appliedText;

		private float _nextRetry;

		private string _cachedName;

		private string _cachedItemName;

		private bool _nameResolved;

		private string _cachedText;

		private float _appliedFontSize = -1f;

		private string _appliedColorHex;

		private Vector2 _appliedPosition = new Vector2(float.NaN, float.NaN);

		public void Initialize(int giverID, ushort itemID, float duration)
		{
			_giverID = giverID;
			_itemID = itemID;
			_destroyTime = Time.unscaledTime + duration;
			_label = ((Component)this).GetComponent<TextMeshProUGUI>();
			if ((Object)(object)_label != (Object)null)
			{
				((TMP_Text)_label).text = "";
			}
		}

		private void Update()
		{
			if (Time.unscaledTime >= _destroyTime)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
				return;
			}
			FeederNameConfig instance = FeederNameConfig.Instance;
			if (instance == null || !instance.Enabled.Value || (Object)(object)_label == (Object)null)
			{
				HideLabel();
				return;
			}
			if (!_nameResolved && Time.unscaledTime >= _nextRetry)
			{
				_cachedName = ResolveName();
				_cachedItemName = ResolveItemName(_itemID);
				_nextRetry = Time.unscaledTime + 0.5f;
				if (!string.IsNullOrEmpty(_cachedName))
				{
					_nameResolved = true;
				}
			}
			if (string.IsNullOrEmpty(_cachedName))
			{
				HideLabel();
				return;
			}
			string text = BuildDisplayName(_cachedName, _cachedItemName, instance);
			if (!string.Equals(_cachedText, text, StringComparison.Ordinal))
			{
				((TMP_Text)_label).text = text;
				((Graphic)_label).SetVerticesDirty();
				((Graphic)_label).SetLayoutDirty();
				_cachedText = text;
				_appliedText = text;
			}
			ApplyStyle(instance);
		}

		private void HideLabel()
		{
			if (!((Object)(object)_label == (Object)null) && !string.IsNullOrEmpty(_appliedText))
			{
				((TMP_Text)_label).text = string.Empty;
				_appliedText = string.Empty;
				_cachedText = string.Empty;
			}
		}

		private void ApplyStyle(FeederNameConfig cfg)
		{
			//IL_00d4: 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_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_label == (Object)null))
			{
				if (Mathf.Abs(cfg.FontSize.Value - _appliedFontSize) > 0.001f)
				{
					((TMP_Text)_label).fontSize = cfg.FontSize.Value;
					_appliedFontSize = cfg.FontSize.Value;
				}
				string value = cfg.TextColor.Value;
				if (!string.Equals(value, _appliedColorHex, StringComparison.Ordinal) && HexColor.TryParse(value, out var color))
				{
					((Graphic)_label).color = color;
					_appliedColorHex = value;
				}
				Vector2 val = default(Vector2);
				((Vector2)(ref val))..ctor(cfg.OffsetX.Value, cfg.OffsetY.Value);
				if (float.IsNaN(_appliedPosition.x) || _appliedPosition != val)
				{
					((TMP_Text)_label).rectTransform.anchoredPosition = val;
					_appliedPosition = val;
				}
			}
		}

		private string ResolveName()
		{
			if (_giverID <= 0)
			{
				return null;
			}
			Character val = default(Character);
			if (Character.GetCharacterWithPhotonID(_giverID, ref val) && (Object)(object)val != (Object)null && !string.IsNullOrEmpty(val.characterName))
			{
				return val.characterName;
			}
			try
			{
				PhotonView photonView = PhotonNetwork.GetPhotonView(_giverID);
				if ((Object)(object)photonView != (Object)null && photonView.Owner != null)
				{
					string nickName = photonView.Owner.NickName;
					if (!string.IsNullOrEmpty(nickName))
					{
						return nickName;
					}
				}
			}
			catch
			{
			}
			return null;
		}

		private string BuildDisplayName(string name, string itemName, FeederNameConfig cfg)
		{
			if (string.IsNullOrEmpty(name))
			{
				return string.Empty;
			}
			name = Truncate(name, cfg.MaxChars.Value);
			if (!cfg.ShowItemName.Value || _itemID == 0)
			{
				return name;
			}
			if (string.IsNullOrEmpty(itemName))
			{
				return name;
			}
			return name + " (" + Truncate(itemName, cfg.MaxChars.Value) + ")";
		}

		private static string ResolveItemName(ushort itemID)
		{
			if (itemID == 0)
			{
				return null;
			}
			try
			{
				Item val = default(Item);
				if (ItemDatabase.TryGetItem(itemID, ref val) && (Object)(object)val != (Object)null)
				{
					string name = val.GetName();
					if (!string.IsNullOrEmpty(name))
					{
						return name;
					}
				}
			}
			catch
			{
			}
			return null;
		}

		private static string Truncate(string name, int maxChars)
		{
			if (string.IsNullOrEmpty(name))
			{
				return string.Empty;
			}
			if (maxChars <= 0 || name.Length <= maxChars)
			{
				return name;
			}
			return name.Substring(0, maxChars);
		}
	}
	internal sealed class FeederTargetLabel : MonoBehaviour
	{
		private static FeederTargetLabel _instance;

		private TextMeshProUGUI _label;

		private UI_UseItemProgress _progress;

		private string _appliedText;

		private float _appliedFontSize = -1f;

		private string _appliedColorHex;

		private Vector2 _appliedPosition = new Vector2(float.NaN, float.NaN);

		private string _cachedName;

		private bool _feeding;

		private static float _nextRetry = -1f;

		public static bool NeedsCreate => (Object)(object)_instance == (Object)null;

		public static void Create()
		{
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_instance != (Object)null || (_nextRetry > 0f && Time.unscaledTime < _nextRetry))
			{
				return;
			}
			_nextRetry = Time.unscaledTime + 0.5f;
			try
			{
				UI_UseItemProgress val = Object.FindFirstObjectByType<UI_UseItemProgress>();
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				RectTransform component = ((Component)val).GetComponent<RectTransform>();
				if ((Object)(object)component == (Object)null)
				{
					PluginInfo.Warn("FeederTargetLabel: UI_UseItemProgress 没有 RectTransform");
					return;
				}
				GameObject val2 = new GameObject("FeederTargetLabel", new Type[1] { typeof(RectTransform) })
				{
					hideFlags = (HideFlags)61
				};
				val2.transform.SetParent((Transform)(object)component, false);
				RectTransform component2 = val2.GetComponent<RectTransform>();
				component2.anchorMin = new Vector2(0.5f, 1f);
				component2.anchorMax = new Vector2(0.5f, 1f);
				component2.pivot = new Vector2(0.5f, 1f);
				component2.sizeDelta = new Vector2(200f, 40f);
				TextMeshProUGUI val3 = val2.AddComponent<TextMeshProUGUI>();
				((TMP_Text)val3).text = "";
				((TMP_Text)val3).alignment = (TextAlignmentOptions)514;
				((Graphic)val3).raycastTarget = false;
				((TMP_Text)val3).enableAutoSizing = false;
				TMP_FontAsset font = NameFontProvider.GetFont();
				if ((Object)(object)font != (Object)null)
				{
					((TMP_Text)val3).font = font;
				}
				_instance = val2.AddComponent<FeederTargetLabel>();
				_instance._label = val3;
				_instance._progress = val;
				_nextRetry = -1f;
			}
			catch (Exception ex)
			{
				PluginInfo.Warn("FeederTargetLabel: 创建失败 - " + ex.Message);
			}
		}

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

		private void Update()
		{
			if ((Object)(object)_instance == (Object)null)
			{
				Create();
				return;
			}
			FeederNameConfig instance = FeederNameConfig.Instance;
			if (instance == null)
			{
				return;
			}
			if (!instance.Enabled.Value || !instance.ShowTargetName.Value || (Object)(object)_label == (Object)null)
			{
				HideLabel();
				_feeding = false;
				return;
			}
			if (!CheckFeeding())
			{
				if (_feeding)
				{
					_feeding = false;
					_cachedName = null;
				}
				HideLabel();
				return;
			}
			if (!_feeding)
			{
				_feeding = true;
				_cachedName = ResolveTargetName();
			}
			else if (string.IsNullOrEmpty(_cachedName))
			{
				_cachedName = ResolveTargetName();
			}
			if (string.IsNullOrEmpty(_cachedName))
			{
				HideLabel();
				return;
			}
			string text = Truncate(_cachedName, instance.MaxChars.Value);
			if (!string.Equals(_appliedText, text, StringComparison.Ordinal))
			{
				((TMP_Text)_label).text = text;
				((Graphic)_label).SetVerticesDirty();
				((Graphic)_label).SetLayoutDirty();
				_appliedText = text;
			}
			ApplyStyle(instance);
		}

		private bool CheckFeeding()
		{
			try
			{
				if ((Object)(object)Character.localCharacter == (Object)null || (Object)(object)Character.localCharacter.data == (Object)null)
				{
					return false;
				}
				if ((Object)(object)Character.localCharacter.data.currentItem == (Object)null)
				{
					return false;
				}
				if (!Character.localCharacter.data.currentItem.shouldShowCastProgress)
				{
					return false;
				}
				if (Character.localCharacter.data.currentItem.progress <= 0f)
				{
					return false;
				}
				if ((Object)(object)Interaction.instance == (Object)null || !Interaction.instance.hasValidTargetCharacter)
				{
					return false;
				}
				return true;
			}
			catch (Exception ex)
			{
				PluginInfo.Log("FeederTargetLabel.CheckFeeding 异常:" + ex.Message);
				return false;
			}
		}

		private string ResolveTargetName()
		{
			try
			{
				if ((Object)(object)Interaction.instance == (Object)null)
				{
					return null;
				}
				if (Interaction.instance.currentHovered == null)
				{
					return null;
				}
				IInteractible currentHovered = Interaction.instance.currentHovered;
				CharacterInteractible val = (CharacterInteractible)(object)((currentHovered is CharacterInteractible) ? currentHovered : null);
				if ((Object)(object)val == (Object)null)
				{
					return null;
				}
				if ((Object)(object)val.character == (Object)null)
				{
					return null;
				}
				string characterName = val.character.characterName;
				if (string.IsNullOrEmpty(characterName))
				{
					return null;
				}
				return characterName;
			}
			catch (Exception ex)
			{
				PluginInfo.Warn("FeederTargetLabel.ResolveTargetName 异常:" + ex.Message);
			}
			return null;
		}

		private static string Truncate(string name, int maxChars)
		{
			if (string.IsNullOrEmpty(name))
			{
				return string.Empty;
			}
			if (maxChars <= 0 || name.Length <= maxChars)
			{
				return name;
			}
			return name.Substring(0, maxChars);
		}

		private void HideLabel()
		{
			if (!((Object)(object)_label == (Object)null) && !string.IsNullOrEmpty(_appliedText))
			{
				((TMP_Text)_label).text = string.Empty;
				_appliedText = string.Empty;
			}
		}

		private void ApplyStyle(FeederNameConfig cfg)
		{
			//IL_00d4: 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_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_label == (Object)null))
			{
				if (Mathf.Abs(cfg.FontSize.Value - _appliedFontSize) > 0.001f)
				{
					((TMP_Text)_label).fontSize = cfg.FontSize.Value;
					_appliedFontSize = cfg.FontSize.Value;
				}
				string value = cfg.TextColor.Value;
				if (!string.Equals(value, _appliedColorHex, StringComparison.Ordinal) && HexColor.TryParse(value, out var color))
				{
					((Graphic)_label).color = color;
					_appliedColorHex = value;
				}
				Vector2 val = default(Vector2);
				((Vector2)(ref val))..ctor(cfg.OffsetX.Value, cfg.OffsetY.Value);
				if (float.IsNaN(_appliedPosition.x) || _appliedPosition != val)
				{
					((TMP_Text)_label).rectTransform.anchoredPosition = val;
					_appliedPosition = val;
				}
			}
		}
	}
	internal static class HexColor
	{
		public static bool TryParse(string hex, out Color color)
		{
			//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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			color = Color.white;
			if (string.IsNullOrEmpty(hex))
			{
				return false;
			}
			string text = hex.Trim();
			if (text.StartsWith("#", StringComparison.Ordinal))
			{
				text = text.Substring(1);
			}
			else if (text.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
			{
				text = text.Substring(2);
			}
			if (text.Length != 6 && text.Length != 8)
			{
				return false;
			}
			int value = 255;
			if (!TryByte(text, 0, out var value2))
			{
				return false;
			}
			if (!TryByte(text, 2, out var value3))
			{
				return false;
			}
			if (!TryByte(text, 4, out var value4))
			{
				return false;
			}
			if (text.Length == 8 && !TryByte(text, 6, out value))
			{
				return false;
			}
			color = new Color((float)value2 / 255f, (float)value3 / 255f, (float)value4 / 255f, (float)value / 255f);
			return true;
		}

		private static bool TryByte(string text, int index, out int value)
		{
			return int.TryParse(text.Substring(index, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value);
		}
	}
	internal static class NameFontProvider
	{
		private static TMP_FontAsset _font;

		private static bool _warnedMissing;

		public static TMP_FontAsset GetFont()
		{
			if ((Object)(object)_font != (Object)null)
			{
				return _font;
			}
			try
			{
				_font = Resolve();
			}
			catch (Exception ex)
			{
				PluginInfo.Warn("解析名字字体失败:" + ex.Message);
			}
			if ((Object)(object)_font == (Object)null && !_warnedMissing)
			{
				_warnedMissing = true;
				PluginInfo.Warn("未能找到支持中文的字体,喂食者名字中的中文可能显示为方块。");
			}
			return _font;
		}

		private static TMP_FontAsset Resolve()
		{
			FontFallbackSwapper val = null;
			try
			{
				val = FontFallbackSwapper.instance;
			}
			catch
			{
			}
			if ((Object)(object)val != (Object)null && (Object)(object)val.mainBaseFont != (Object)null)
			{
				TMP_FontAsset mainBaseFont = val.mainBaseFont;
				TMP_FontAsset val2 = null;
				if (HasCjk(val.simplifiedChineseFont))
				{
					val2 = val.simplifiedChineseFont;
				}
				else if (HasCjk(val.traditionalChineseFont))
				{
					val2 = val.traditionalChineseFont;
				}
				if ((Object)(object)val2 != (Object)null && val2 != mainBaseFont)
				{
					EnsureFallback(mainBaseFont, val2);
				}
				return mainBaseFont;
			}
			if ((Object)(object)val != (Object)null && HasCjk(val.simplifiedChineseFont))
			{
				return val.simplifiedChineseFont;
			}
			return FindFontByNameOrCjk();
		}

		private static TMP_FontAsset FindFontByNameOrCjk()
		{
			TMP_FontAsset[] array = null;
			try
			{
				array = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
			}
			catch
			{
			}
			if (array == null)
			{
				return null;
			}
			TMP_FontAsset val = null;
			TMP_FontAsset val2 = null;
			TMP_FontAsset[] array2 = array;
			foreach (TMP_FontAsset val3 in array2)
			{
				if (!((Object)(object)val3 == (Object)null))
				{
					string text = ((Object)val3).name ?? string.Empty;
					if ((Object)(object)val2 == (Object)null && text == "DarumaDropOne-Regular SDF")
					{
						val2 = val3;
					}
					if ((Object)(object)val == (Object)null && HasCjk(val3))
					{
						val = val3;
					}
				}
			}
			return val ?? val2;
		}

		private static void EnsureFallback(TMP_FontAsset target, TMP_FontAsset fallback)
		{
			if (!((Object)(object)target == (Object)null) && !((Object)(object)fallback == (Object)null) && target != fallback)
			{
				List<TMP_FontAsset> list = target.fallbackFontAssetTable;
				if (list == null)
				{
					list = (target.fallbackFontAssetTable = new List<TMP_FontAsset>());
				}
				if (!list.Contains(fallback))
				{
					list.Add(fallback);
					RefreshFont(target);
					PluginInfo.Log("已为字体 " + ((Object)target).name + " 追加中文回退:" + ((Object)fallback).name);
				}
			}
		}

		private static void RefreshFont(TMP_FontAsset font)
		{
			try
			{
				if (font.characterLookupTable != null)
				{
					font.characterLookupTable.Clear();
				}
				if (font.glyphLookupTable != null)
				{
					font.glyphLookupTable.Clear();
				}
				font.ReadFontAssetDefinition();
				TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, (Object)(object)font);
			}
			catch (Exception ex)
			{
				PluginInfo.Warn("刷新字体失败:" + ex.Message);
			}
		}

		private static bool HasCjk(TMP_FontAsset font)
		{
			if ((Object)(object)font == (Object)null)
			{
				return false;
			}
			if (font.characterLookupTable != null && font.characterLookupTable.Count > 0 && font.characterLookupTable.ContainsKey(19968u))
			{
				return true;
			}
			if (font.characterTable != null)
			{
				try
				{
					if (font.HasCharacter(19968))
					{
						return true;
					}
				}
				catch
				{
				}
			}
			return false;
		}
	}
	internal static class Patches
	{
		private static Harmony _harmony;

		public static void Apply()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Expected O, but got Unknown
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			try
			{
				_harmony = new Harmony("FeederName");
				HarmonyMethod val = new HarmonyMethod(AccessTools.Method(typeof(Patches), "OnInitPostfix", (Type[])null, (Type[])null));
				_harmony.Patch((MethodBase)AccessTools.Method(typeof(UI_UseItemProgressFriend), "Init", (Type[])null, (Type[])null), (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				HarmonyMethod val2 = new HarmonyMethod(AccessTools.Method(typeof(Patches), "OnKillPrefix", (Type[])null, (Type[])null));
				_harmony.Patch((MethodBase)AccessTools.Method(typeof(UI_UseItemProgressFriend), "Kill", (Type[])null, (Type[])null), val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				PluginInfo.Log("Harmony 补丁已应用(UI_UseItemProgressFriend.Init + Kill)。");
			}
			catch (Exception ex)
			{
				PluginInfo.Warn("应用 Harmony 补丁失败:" + ex.Message);
			}
		}

		private static void OnInitPostfix(UI_UseItemProgressFriend __instance, FeedData feedData)
		{
			if (!((Object)(object)__instance == (Object)null))
			{
				FeederNameLabel.Attach(__instance, __instance.giverID, feedData?.itemID ?? 0);
			}
		}

		private static void OnKillPrefix(UI_UseItemProgressFriend __instance)
		{
			if (!((Object)(object)__instance == (Object)null))
			{
				FeederNameLabel componentInChildren = ((Component)__instance).GetComponentInChildren<FeederNameLabel>();
				if (!((Object)(object)componentInChildren == (Object)null))
				{
					componentInChildren.CreatePersistentLabel();
				}
			}
		}
	}
	[BepInPlugin("FeederName", "喂食者名字", "1.2.4")]
	[BepInProcess("PEAK.exe")]
	public class Plugin : BaseUnityPlugin
	{
		internal static Plugin Instance { get; private set; }

		private void Awake()
		{
			Instance = this;
			PluginInfo.Init(((BaseUnityPlugin)this).Logger);
			FeederNameConfig.Instance = new FeederNameConfig();
			FeederNameConfig.Instance.Init(((BaseUnityPlugin)this).Config);
			Patches.Apply();
			FeederTargetLabel.Create();
			PluginInfo.Log("已加载 喂食者名字 v1.2.4,配置文件:BepInEx/config/FeederName.cfg");
		}

		private void Update()
		{
			if (FeederTargetLabel.NeedsCreate)
			{
				FeederTargetLabel.Create();
			}
		}
	}
	public static class PluginInfo
	{
		public const string Guid = "FeederName";

		public const string Name = "喂食者名字";

		public const string Version = "1.2.4";

		private static ManualLogSource _log;

		public static void Init(ManualLogSource log)
		{
			_log = log;
		}

		public static void Log(string message)
		{
			ManualLogSource log = _log;
			if (log != null)
			{
				log.LogMessage((object)message);
			}
		}

		public static void Warn(string message)
		{
			ManualLogSource log = _log;
			if (log != null)
			{
				log.LogWarning((object)message);
			}
		}
	}
}