Decompiled source of ValheimBarber v1.0.0

ValheimBarber.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[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 ValheimBarber
{
	internal static class HairSession
	{
		internal const float MinBrightness = 0.1f;

		internal const float MaxBrightness = 2f;

		private static PlayerCustomizaton _active;

		private static int _lastFrame = -100;

		private static Color _rgb = Color.white;

		private static float _intensity = 1f;

		private static Vector3 Current => new Vector3(_rgb.r, _rgb.g, _rgb.b) * _intensity;

		internal static bool CustomActive
		{
			get
			{
				if ((Object)(object)_active != (Object)null && Time.frameCount - _lastFrame <= 2 && PanelActive(_active))
				{
					return NativeSliders.IsReplaced(_active);
				}
				return false;
			}
		}

		private static bool PanelActive(PlayerCustomizaton pc)
		{
			if ((Object)(object)pc == (Object)null)
			{
				return false;
			}
			if ((Object)(object)pc.m_rootPanel != (Object)null)
			{
				return pc.m_rootPanel.activeInHierarchy;
			}
			return ((Behaviour)pc).isActiveAndEnabled;
		}

		internal static void PreUpdate(PlayerCustomizaton pc)
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			if (!PanelActive(pc))
			{
				return;
			}
			Player player = HairColor.GetPlayer(pc);
			if (!((Object)(object)player == (Object)null))
			{
				if ((Object)(object)pc != (Object)(object)_active || Time.frameCount - _lastFrame > 2)
				{
					_active = pc;
					NativeSliders.Ensure(pc);
					SetFrom(HairColor.Get(player));
				}
				_lastFrame = Time.frameCount;
			}
		}

		internal static void PostUpdate(PlayerCustomizaton pc)
		{
			//IL_0030: 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)
			if (!((Object)(object)pc != (Object)(object)_active) && PanelActive(pc) && NativeSliders.IsReplaced(pc))
			{
				Player player = HairColor.GetPlayer(pc);
				if (!((Object)(object)player == (Object)null))
				{
					HairColor.Set(player, Current);
					NativeSliders.Sync(pc, _rgb, _intensity);
				}
			}
		}

		internal static void SetFromNative(Color rgb, float intensity)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_active == (Object)null))
			{
				_rgb = rgb;
				_intensity = Mathf.Max(0.1f, intensity);
				Player player = HairColor.GetPlayer(_active);
				if ((Object)(object)player != (Object)null)
				{
					HairColor.Set(player, Current);
				}
			}
		}

		internal static void EndSession()
		{
			_active = null;
			_lastFrame = -100;
		}

		private static void SetFrom(Vector3 v)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			float num = Mathf.Max(v.x, Mathf.Max(v.y, v.z));
			_intensity = ((num > 1f) ? num : 1f);
			_rgb = new Color(Mathf.Clamp01(v.x / _intensity), Mathf.Clamp01(v.y / _intensity), Mathf.Clamp01(v.z / _intensity));
		}
	}
	internal static class NativeSliders
	{
		private class Data
		{
			public readonly Slider[] Sliders = (Slider[])(object)new Slider[4];

			public readonly TMP_Text[] Labels = (TMP_Text[])(object)new TMP_Text[4];

			public RectTransform Wood;

			public bool Built;
		}

		private static readonly Dictionary<PlayerCustomizaton, Data> Cache = new Dictionary<PlayerCustomizaton, Data>();

		private static readonly Vector3[] Corners = (Vector3[])(object)new Vector3[4];

		private static bool _syncing;

		private const float BottomMarginPx = 22f;

		private static readonly Color[] Tints = (Color[])(object)new Color[4]
		{
			new Color(0.85f, 0.2f, 0.18f),
			new Color(0.25f, 0.75f, 0.3f),
			new Color(0.25f, 0.45f, 0.9f),
			new Color(0.95f, 0.85f, 0.6f)
		};

		internal static void Ensure(PlayerCustomizaton pc)
		{
			if ((Object)(object)pc == (Object)null)
			{
				return;
			}
			CleanCache();
			if (Cache.ContainsKey(pc))
			{
				return;
			}
			Data data = new Data();
			Cache[pc] = data;
			try
			{
				Build(pc, data);
				data.Built = true;
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Não deu pra criar os sliders no painel: {arg}");
			}
		}

		internal static bool IsReplaced(PlayerCustomizaton pc)
		{
			if ((Object)(object)pc != (Object)null && Cache.TryGetValue(pc, out var value))
			{
				return value.Built;
			}
			return false;
		}

		internal static void Sync(PlayerCustomizaton pc, Color rgb, float intensity)
		{
			//IL_00ae: 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_00c0: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)pc == (Object)null || !Cache.TryGetValue(pc, out var value) || !value.Built)
			{
				return;
			}
			string[] sliderNames = Loc.SliderNames;
			for (int i = 0; i < 4; i++)
			{
				if ((Object)(object)value.Labels[i] != (Object)null && value.Labels[i].text != sliderNames[i])
				{
					value.Labels[i].text = sliderNames[i];
				}
			}
			_syncing = true;
			try
			{
				Slider val = value.Sliders[3];
				if ((Object)(object)val != (Object)null)
				{
					float num = Mathf.Max(2f, intensity);
					if (!Mathf.Approximately(val.maxValue, num))
					{
						val.maxValue = num;
					}
				}
				float[] array = new float[4] { rgb.r, rgb.g, rgb.b, intensity };
				for (int j = 0; j < 4; j++)
				{
					if ((Object)(object)value.Sliders[j] != (Object)null && Mathf.Abs(value.Sliders[j].value - array[j]) > 0.0005f)
					{
						value.Sliders[j].SetValueWithoutNotify(array[j]);
					}
				}
			}
			finally
			{
				_syncing = false;
			}
		}

		private static void OnChanged(PlayerCustomizaton pc)
		{
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			if (!_syncing && !((Object)(object)pc == (Object)null) && Cache.TryGetValue(pc, out var value))
			{
				Color rgb = new Color(((Object)(object)value.Sliders[0] != (Object)null) ? value.Sliders[0].value : 0f, ((Object)(object)value.Sliders[1] != (Object)null) ? value.Sliders[1].value : 0f, ((Object)(object)value.Sliders[2] != (Object)null) ? value.Sliders[2].value : 0f);
				float intensity = (((Object)(object)value.Sliders[3] != (Object)null) ? value.Sliders[3].value : 1f);
				HairSession.SetFromNative(rgb, intensity);
			}
		}

		private static void CleanCache()
		{
			foreach (PlayerCustomizaton item in Cache.Keys.Where((PlayerCustomizaton k) => (Object)(object)k == (Object)null).ToList())
			{
				Cache.Remove(item);
			}
		}

		internal static bool TryGetScreenRect(RectTransform rt, out Rect rect)
		{
			//IL_0001: 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_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: 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)
			rect = default(Rect);
			if ((Object)(object)rt == (Object)null)
			{
				return false;
			}
			rt.GetWorldCorners(Corners);
			Camera val = CanvasCamera((Transform)(object)rt);
			float num = float.MaxValue;
			float num2 = float.MaxValue;
			float num3 = float.MinValue;
			float num4 = float.MinValue;
			for (int i = 0; i < 4; i++)
			{
				Vector2 val2 = RectTransformUtility.WorldToScreenPoint(val, Corners[i]);
				num = Mathf.Min(num, val2.x);
				num3 = Mathf.Max(num3, val2.x);
				num2 = Mathf.Min(num2, val2.y);
				num4 = Mathf.Max(num4, val2.y);
			}
			rect = Rect.MinMaxRect(num, num2, num3, num4);
			if (((Rect)(ref rect)).width > 1f)
			{
				return ((Rect)(ref rect)).height > 1f;
			}
			return false;
		}

		private static Camera CanvasCamera(Transform t)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			Canvas componentInParent = ((Component)t).GetComponentInParent<Canvas>();
			if ((Object)(object)componentInParent == (Object)null)
			{
				return null;
			}
			componentInParent = componentInParent.rootCanvas;
			if ((int)componentInParent.renderMode != 0)
			{
				return componentInParent.worldCamera;
			}
			return null;
		}

		private static float WorldPerPixel(RectTransform rt)
		{
			if (!TryGetScreenRect(rt, out var rect) || ((Rect)(ref rect)).height < 1f)
			{
				return 1f;
			}
			rt.GetWorldCorners(Corners);
			float num = Mathf.Abs(Corners[1].y - Corners[0].y);
			if (!(num > 0f))
			{
				return 1f;
			}
			return num / ((Rect)(ref rect)).height;
		}

		private static RectTransform FindWood(PlayerCustomizaton pc)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)pc.m_hairTone == (Object)null || (Object)(object)pc.m_hairLevel == (Object)null)
			{
				return null;
			}
			if (!TryGetScreenRect((RectTransform)((Component)pc.m_hairTone).transform, out var rect))
			{
				return null;
			}
			if (!TryGetScreenRect((RectTransform)((Component)pc.m_hairLevel).transform, out var rect2))
			{
				return null;
			}
			Transform val = ((Component)pc.m_hairTone).transform;
			while ((Object)(object)val.parent != (Object)null && (Object)(object)((Component)val.parent).GetComponent<Canvas>() == (Object)null)
			{
				val = val.parent;
			}
			RectTransform result = null;
			float num = 0f;
			Image[] componentsInChildren = ((Component)val).GetComponentsInChildren<Image>(true);
			foreach (Image val2 in componentsInChildren)
			{
				if ((Object)(object)val2 == (Object)null || !((Component)val2).gameObject.activeInHierarchy || ((Component)val2).transform.IsChildOf(((Component)pc.m_hairTone).transform) || ((Component)val2).transform.IsChildOf(((Component)pc.m_hairLevel).transform) || ((Object)val2).name.StartsWith("ValheimBarber_"))
				{
					continue;
				}
				RectTransform rectTransform = ((Graphic)val2).rectTransform;
				if (TryGetScreenRect(rectTransform, out var rect3) && !(((Rect)(ref rect3)).width > (float)Screen.width * 0.4f) && !(((Rect)(ref rect3)).height > (float)Screen.height * 0.95f) && Contains(rect3, rect) && Contains(rect3, rect2))
				{
					float num2 = ((Rect)(ref rect3)).width * ((Rect)(ref rect3)).height;
					if (num2 > num)
					{
						num = num2;
						result = rectTransform;
					}
				}
			}
			return result;
		}

		private static bool Contains(Rect outer, Rect inner)
		{
			if (((Rect)(ref inner)).xMin >= ((Rect)(ref outer)).xMin - 4f && ((Rect)(ref inner)).xMax <= ((Rect)(ref outer)).xMax + 4f && ((Rect)(ref inner)).yMin >= ((Rect)(ref outer)).yMin - 4f)
			{
				return ((Rect)(ref inner)).yMax <= ((Rect)(ref outer)).yMax + 4f;
			}
			return false;
		}

		private static void GrowToFit(RectTransform wood, float lowestScreenY)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: 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_00a3: 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_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)wood == (Object)null || !TryGetScreenRect(wood, out var rect))
			{
				return;
			}
			float num = ((lowestScreenY - 22f < ((Rect)(ref rect)).yMin) ? (((Rect)(ref rect)).yMin - (lowestScreenY - 22f)) : 0f);
			if (!(num <= 0.5f))
			{
				float num2 = num * WorldPerPixel(wood) / Mathf.Max(0.0001f, Mathf.Abs(((Transform)wood).lossyScale.y));
				if (Mathf.Approximately(wood.anchorMin.y, wood.anchorMax.y))
				{
					wood.sizeDelta += new Vector2(0f, num2);
					wood.anchoredPosition -= new Vector2(0f, num2 * (1f - wood.pivot.y));
				}
				else
				{
					wood.offsetMin -= new Vector2(0f, num2);
				}
			}
		}

		private static void Build(PlayerCustomizaton pc, Data data)
		{
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Expected O, but got Unknown
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Expected O, but got Unknown
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Expected O, but got Unknown
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Expected O, but got Unknown
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_043f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0449: Expected O, but got Unknown
			//IL_036d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0374: Unknown result type (might be due to invalid IL or missing references)
			//IL_0379: Unknown result type (might be due to invalid IL or missing references)
			//IL_037b: Unknown result type (might be due to invalid IL or missing references)
			//IL_038e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0398: Expected O, but got Unknown
			//IL_02e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0315: Unknown result type (might be due to invalid IL or missing references)
			//IL_031f: Expected O, but got Unknown
			//IL_04a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_04bc: Unknown result type (might be due to invalid IL or missing references)
			Slider hairTone = pc.m_hairTone;
			Slider hairLevel = pc.m_hairLevel;
			if ((Object)(object)hairTone == (Object)null || (Object)(object)hairLevel == (Object)null)
			{
				throw new Exception("sliders vanilla não encontrados");
			}
			data.Wood = FindWood(pc);
			if ((Object)(object)data.Wood == (Object)null)
			{
				Plugin.Log.LogWarning((object)"Quadro de madeira não encontrado, o painel não vai crescer.");
			}
			Slider val = ((((Component)hairTone).transform.position.y <= ((Component)hairLevel).transform.position.y) ? hairTone : hairLevel);
			Slider val2 = (((Object)(object)val == (Object)(object)hairTone) ? hairLevel : hairTone);
			Transform val3 = RowOf(val);
			Transform val4 = RowOf(val2);
			Transform parent = val3.parent;
			RectTransform val5 = (RectTransform)(object)((parent is RectTransform) ? parent : null);
			if ((Object)(object)val5 == (Object)null)
			{
				throw new Exception("slider sem pai RectTransform");
			}
			TMP_Text val6 = FindLabel(val4, ((Component)val2).transform);
			TMP_Text val7 = FindLabel(val3, ((Component)val).transform);
			bool flag = (Object)(object)val3 == (Object)(object)((Component)val).transform;
			bool flag2 = (Object)(object)((Component)val5).GetComponent<LayoutGroup>() != (Object)null;
			RectTransform val8 = (RectTransform)val3;
			float num = 0f;
			if ((Object)(object)val4.parent == (Object)(object)val5)
			{
				num = Mathf.Abs(((RectTransform)val4).anchoredPosition.y - val8.anchoredPosition.y);
			}
			if (num < 1f)
			{
				num = 54f;
			}
			float num2 = 0f;
			if (TryGetScreenRect((RectTransform)((Component)val2).transform, out var rect) && TryGetScreenRect((RectTransform)((Component)val).transform, out var rect2))
			{
				num2 = Mathf.Abs(((Rect)(ref rect)).center.y - ((Rect)(ref rect2)).center.y);
			}
			if (num2 < 1f)
			{
				num2 = num;
			}
			Rect rect3;
			float lowerBottomPx = (TryGetScreenRect((RectTransform)((Component)val).transform, out rect3) ? ((Rect)(ref rect3)).yMin : 0f);
			List<RectTransform> list = (flag2 ? new List<RectTransform>() : CollectBelow(pc, data.Wood, val, lowerBottomPx));
			SetupSlider(pc, val2, 0);
			SetupSlider(pc, val, 1);
			if ((Object)(object)val6 != (Object)null)
			{
				StripLocalize(((Component)val6).gameObject);
			}
			if ((Object)(object)val7 != (Object)null)
			{
				StripLocalize(((Component)val7).gameObject);
			}
			int num3 = val3.GetSiblingIndex() + 1;
			List<RectTransform> list2 = new List<RectTransform>();
			string[] array = new string[2] { "Blue", "Brightness" };
			Vector2 val9 = default(Vector2);
			for (int i = 0; i < 2; i++)
			{
				int num4 = 2 + i;
				((Vector2)(ref val9))..ctor(0f, (0f - num) * (float)(i + 1));
				TMP_Text val10 = null;
				if (flag && (Object)(object)val7 != (Object)null)
				{
					GameObject val11 = Object.Instantiate<GameObject>(((Component)val7).gameObject, val7.transform.parent, false);
					((Object)val11).name = "ValheimBarber_Label_" + array[i];
					StripLocalize(val11);
					if (flag2)
					{
						val11.transform.SetSiblingIndex(num3++);
					}
					else
					{
						((RectTransform)val11.transform).anchoredPosition = ((RectTransform)val7.transform).anchoredPosition + val9;
					}
					val10 = val11.GetComponent<TMP_Text>();
					list2.Add((RectTransform)val11.transform);
				}
				GameObject val12 = Object.Instantiate<GameObject>(((Component)val3).gameObject, (Transform)(object)val5, false);
				((Object)val12).name = "ValheimBarber_Row_" + array[i];
				StripLocalize(val12);
				if (flag2)
				{
					val12.transform.SetSiblingIndex(num3++);
				}
				else
				{
					((RectTransform)val12.transform).anchoredPosition = val8.anchoredPosition + val9;
				}
				list2.Add((RectTransform)val12.transform);
				if (!flag)
				{
					val10 = val12.GetComponentsInChildren<TMP_Text>(true).FirstOrDefault();
				}
				Slider val13 = val12.GetComponent<Slider>();
				if ((Object)(object)val13 == (Object)null)
				{
					val13 = val12.GetComponentInChildren<Slider>(true);
				}
				if ((Object)(object)val13 == (Object)null)
				{
					throw new Exception("slider clonado sem componente Slider");
				}
				SetupSlider(pc, val13, num4);
				data.Sliders[num4] = val13;
				data.Labels[num4] = val10;
			}
			data.Sliders[0] = val2;
			data.Sliders[1] = val;
			data.Labels[0] = val6;
			data.Labels[1] = val7;
			float num5 = num2 * 2f * WorldPerPixel((RectTransform)((Component)val).transform);
			foreach (RectTransform item in list)
			{
				if ((Object)(object)((Transform)item).parent != (Object)null && (Object)(object)((Component)((Transform)item).parent).GetComponent<LayoutGroup>() != (Object)null)
				{
					Plugin.Log.LogWarning((object)("'" + ((Object)item).name + "' está num LayoutGroup, mover ele pode não funcionar."));
				}
				((Transform)item).position = ((Transform)item).position - new Vector3(0f, num5, 0f);
			}
			if (flag2)
			{
				LayoutRebuilder.ForceRebuildLayoutImmediate(val5);
			}
			float num6 = float.MaxValue;
			foreach (RectTransform item2 in list2.Concat(list))
			{
				if (TryGetScreenRect(item2, out var rect4))
				{
					num6 = Mathf.Min(num6, ((Rect)(ref rect4)).yMin);
				}
			}
			if (num6 < float.MaxValue)
			{
				GrowToFit(data.Wood, num6);
			}
			Plugin.Log.LogInfo((object)($"Sliders RGB + Brilho no painel (layout: {flag2}, passo: {num2:0.#}px, " + string.Format("quadro: {0}, empurrados: {1}).", ((Object)(object)data.Wood != (Object)null) ? ((Object)data.Wood).name : "nenhum", list.Count)));
		}

		private static List<RectTransform> CollectBelow(PlayerCustomizaton pc, RectTransform wood, Slider lower, float lowerBottomPx)
		{
			//IL_00bd: 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_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			List<RectTransform> list = new List<RectTransform>();
			if ((Object)(object)wood == (Object)null || !TryGetScreenRect(wood, out var rect))
			{
				return list;
			}
			Transform val = (Transform)(((Object)(object)((Transform)wood).parent != (Object)null) ? ((object)((Transform)wood).parent) : ((object)wood));
			HashSet<Transform> hashSet = new HashSet<Transform>();
			Transform val2 = ((Component)lower).transform;
			while ((Object)(object)val2 != (Object)null)
			{
				hashSet.Add(val2);
				val2 = val2.parent;
			}
			List<RectTransform> list2 = new List<RectTransform>();
			RectTransform[] componentsInChildren = ((Component)val).GetComponentsInChildren<RectTransform>(true);
			foreach (RectTransform val3 in componentsInChildren)
			{
				if (!((Object)(object)val3 == (Object)(object)wood) && !hashSet.Contains((Transform)(object)val3) && ((Component)val3).gameObject.activeInHierarchy && !((Transform)val3).IsChildOf(((Component)lower).transform) && Mathf.Approximately(val3.anchorMin.y, val3.anchorMax.y) && !((Transform)wood).IsChildOf((Transform)(object)val3) && TryGetScreenRect(val3, out var rect2))
				{
					bool num = ((Rect)(ref rect2)).center.x >= ((Rect)(ref rect)).xMin && ((Rect)(ref rect2)).center.x <= ((Rect)(ref rect)).xMax;
					bool flag = ((Rect)(ref rect2)).yMin >= ((Rect)(ref rect)).yMin - 6f;
					bool flag2 = ((Rect)(ref rect2)).yMax <= lowerBottomPx + 1f;
					if (num && flag && flag2)
					{
						list2.Add(val3);
					}
				}
			}
			HashSet<Transform> hashSet2 = new HashSet<Transform>((IEnumerable<Transform>)list2);
			foreach (RectTransform item in list2)
			{
				bool flag3 = false;
				Transform parent = ((Transform)item).parent;
				while ((Object)(object)parent != (Object)null)
				{
					if (hashSet2.Contains(parent))
					{
						flag3 = true;
						break;
					}
					parent = parent.parent;
				}
				if (!flag3)
				{
					list.Add(item);
				}
			}
			return list;
		}

		private static void SetupSlider(PlayerCustomizaton pc, Slider slider, int idx)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			slider.onValueChanged = new SliderEvent();
			slider.minValue = ((idx == 3) ? 0.1f : 0f);
			slider.maxValue = ((idx == 3) ? 2f : 1f);
			slider.wholeNumbers = false;
			((UnityEvent<float>)(object)slider.onValueChanged).AddListener((UnityAction<float>)delegate
			{
				OnChanged(pc);
			});
			if ((Object)(object)slider.fillRect != (Object)null)
			{
				Image component = ((Component)slider.fillRect).GetComponent<Image>();
				if ((Object)(object)component != (Object)null)
				{
					((Graphic)component).color = Tints[idx];
				}
			}
		}

		private static Transform RowOf(Slider s)
		{
			Transform parent = ((Component)s).transform.parent;
			if ((Object)(object)parent == (Object)null)
			{
				return ((Component)s).transform;
			}
			bool num = ((Component)parent).GetComponentsInChildren<Slider>(true).Length == 1;
			bool flag = ((Component)parent).GetComponentsInChildren<TMP_Text>(true).Any((TMP_Text t) => !t.transform.IsChildOf(((Component)s).transform));
			if (!(num && flag))
			{
				return ((Component)s).transform;
			}
			return parent;
		}

		private static TMP_Text FindLabel(Transform row, Transform slider)
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)row != (Object)(object)slider)
			{
				return ((IEnumerable<TMP_Text>)((Component)row).GetComponentsInChildren<TMP_Text>(true)).FirstOrDefault((Func<TMP_Text, bool>)((TMP_Text t) => !t.transform.IsChildOf(slider)));
			}
			Transform parent = slider.parent;
			if ((Object)(object)parent == (Object)null)
			{
				return null;
			}
			float y = slider.localPosition.y;
			TMP_Text result = null;
			float num = float.MaxValue;
			foreach (Transform item in parent)
			{
				Transform val = item;
				if ((Object)(object)val == (Object)(object)slider)
				{
					continue;
				}
				TMP_Text component = ((Component)val).GetComponent<TMP_Text>();
				if (!((Object)(object)component == (Object)null))
				{
					float num2 = val.localPosition.y - y;
					if (num2 > 0f && num2 < num)
					{
						num = num2;
						result = component;
					}
				}
			}
			return result;
		}

		private static void StripLocalize(GameObject go)
		{
			MonoBehaviour[] componentsInChildren = go.GetComponentsInChildren<MonoBehaviour>(true);
			foreach (MonoBehaviour val in componentsInChildren)
			{
				if ((Object)(object)val != (Object)null && ((object)val).GetType().Name.StartsWith("Localiz"))
				{
					Object.DestroyImmediate((Object)(object)val);
				}
			}
		}
	}
	internal static class Patches
	{
		internal static bool InVanillaUpdate;

		internal static void Apply(Harmony h)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			//IL_0079: Expected O, but got Unknown
			//IL_0079: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.DeclaredMethod(typeof(PlayerCustomizaton), "Update", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Plugin.Log.LogError((object)"PlayerCustomizaton.Update não encontrado, mod desativado.");
				return;
			}
			try
			{
				h.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Patches), "Update_Prefix", (Type[])null), new HarmonyMethod(typeof(Patches), "Update_Postfix", (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Patches), "Update_Finalizer", (Type[])null), (HarmonyMethod)null);
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Falha ao patchear PlayerCustomizaton.Update: {arg}");
				return;
			}
			TryPatch(h, typeof(Player), "SetHairColor", "SetHairColor_Prefix");
			TryPatch(h, typeof(PlayerCustomizaton), "OnCancel", "OnCancel_Prefix");
		}

		private static void TryPatch(Harmony h, Type type, string method, string prefix)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			try
			{
				MethodInfo methodInfo = AccessTools.DeclaredMethod(type, method, (Type[])null, (Type[])null);
				if (methodInfo == null)
				{
					Plugin.Log.LogWarning((object)(type.Name + "." + method + " não encontrado, patch ignorado."));
				}
				else
				{
					h.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Patches), prefix, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Falha ao patchear {type.Name}.{method}: {arg}");
			}
		}

		private static void Update_Prefix(PlayerCustomizaton __instance)
		{
			try
			{
				HairSession.PreUpdate(__instance);
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)ex);
			}
			InVanillaUpdate = true;
		}

		private static void Update_Postfix(PlayerCustomizaton __instance)
		{
			InVanillaUpdate = false;
			try
			{
				HairSession.PostUpdate(__instance);
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)ex);
			}
		}

		private static Exception Update_Finalizer(Exception __exception)
		{
			InVanillaUpdate = false;
			return __exception;
		}

		private static bool SetHairColor_Prefix()
		{
			if (InVanillaUpdate)
			{
				return !HairSession.CustomActive;
			}
			return true;
		}

		private static void OnCancel_Prefix()
		{
			InVanillaUpdate = false;
			HairSession.EndSession();
		}
	}
	[BepInPlugin("morgnar.valheimbarber", "ValheimBarber", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string ModGUID = "morgnar.valheimbarber";

		public const string ModName = "ValheimBarber";

		public const string ModVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal static Harmony Harmony;

		private void Awake()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Invalid comparison between Unknown and I4
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			if ((int)SystemInfo.graphicsDeviceType == 4)
			{
				Log.LogInfo((object)"ValheimBarber: servidor dedicado detectado, mod inativo (é só client-side).");
				return;
			}
			Harmony = new Harmony("morgnar.valheimbarber");
			Patches.Apply(Harmony);
			Log.LogInfo((object)"ValheimBarber 1.0.0 carregado.");
		}

		private void OnDestroy()
		{
			Harmony harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class HairColor
	{
		private static readonly FieldInfo HairField = AccessTools.Field(typeof(Player), "m_hairColor");

		private static readonly MethodInfo GetPlayerMethod = AccessTools.Method(typeof(PlayerCustomizaton), "GetPlayer", (Type[])null, (Type[])null);

		internal static Player GetPlayer(PlayerCustomizaton pc)
		{
			if ((Object)(object)pc == (Object)null || GetPlayerMethod == null)
			{
				return null;
			}
			try
			{
				object? obj = GetPlayerMethod.Invoke(pc, null);
				return (Player)((obj is Player) ? obj : null);
			}
			catch (Exception)
			{
				return null;
			}
		}

		internal static Vector3 Get(Player p)
		{
			//IL_0016: 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)
			if ((Object)(object)p == (Object)null || HairField == null)
			{
				return Vector3.one;
			}
			return (Vector3)HairField.GetValue(p);
		}

		internal static void Set(Player p, Vector3 color)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)p != (Object)null)
			{
				p.SetHairColor(color);
			}
		}
	}
	internal static class Loc
	{
		private static bool PT => ((Localization.instance != null) ? Localization.instance.GetSelectedLanguage() : "")?.StartsWith("Portuguese") ?? false;

		internal static string[] SliderNames
		{
			get
			{
				if (PT)
				{
					return new string[4] { "Vermelho", "Verde", "Azul", "Brilho" };
				}
				return new string[4] { "Red", "Green", "Blue", "Brightness" };
			}
		}
	}
}