Decompiled source of Carturs UI HUD v1.0.4

plugins/CarturUIHud/CarturUIHud.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("CarturUIHud")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+4ec8df5c3c7871becec5eefcfc2a967a9f148d9c")]
[assembly: AssemblyProduct("CarturUIHud")]
[assembly: AssemblyTitle("CarturUIHud")]
[assembly: AssemblyVersion("1.0.2.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 CarturUIHud
{
	internal static class AssetLoader
	{
		internal const float BaseBarHeight = 32f;

		private const float BorderLeftPx = 118f;

		private const float BorderRightPx = 95f;

		private const float BorderRailPx = 16f;

		private static Texture2D s_barFrame;

		private static Texture2D s_barFill;

		private static Texture2D s_foodFrame;

		internal static float NaturalWindowUnits { get; private set; }

		internal static float FrameLeftUnits { get; private set; }

		internal static float FrameRightUnits { get; private set; }

		internal static float FrameRailUnits { get; private set; }

		internal static float MinWindowUnits => 24f;

		public static Sprite BarFrame { get; private set; }

		public static Sprite BarFill { get; private set; }

		public static Sprite FoodFrame { get; private set; }

		public static string AssetsDir { get; private set; }

		public static bool LoadTextures(ManualLogSource log)
		{
			AssetsDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "", "assets");
			s_barFrame = Load("bar_frame.png", log);
			s_barFill = Load("bar_fill.png", log);
			s_foodFrame = Load("food_frame.png", log);
			if ((Object)(object)s_barFrame != (Object)null && (Object)(object)s_barFill != (Object)null)
			{
				return (Object)(object)s_foodFrame != (Object)null;
			}
			return false;
		}

		public static void BuildSprites(float referencePixelsPerUnit, ManualLogSource log)
		{
			//IL_0087: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
			float num = (float)((Texture)s_barFrame).height * referencePixelsPerUnit / 32f;
			float num2 = (float)((Texture)s_barFill).height * referencePixelsPerUnit / 32f;
			float num3 = referencePixelsPerUnit / num;
			FrameLeftUnits = 118f * num3;
			FrameRightUnits = 95f * num3;
			FrameRailUnits = 16f * num3;
			NaturalWindowUnits = ((float)((Texture)s_barFrame).width - 118f - 95f) * num3;
			BarFrame = Make(s_barFrame, new Vector4(118f, 16f, 95f, 16f), num);
			BarFill = Make(s_barFill, Vector4.zero, num2);
			FoodFrame = Make(s_foodFrame, Vector4.zero, referencePixelsPerUnit);
			log.LogInfo((object)$"canvas referencePixelsPerUnit {referencePixelsPerUnit}; frame ppu {num:0.##}, fill ppu {num2:0.##}, ornaments {FrameLeftUnits:0.#} + {FrameRightUnits:0.#}, natural window {NaturalWindowUnits:0.#}, at {32f} tall");
		}

		private static Sprite Make(Texture2D tex, Vector4 border, float pixelsPerUnit)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)tex == (Object)null)
			{
				return null;
			}
			return Sprite.Create(tex, new Rect(0f, 0f, (float)((Texture)tex).width, (float)((Texture)tex).height), new Vector2(0.5f, 0.5f), pixelsPerUnit, 0u, (SpriteMeshType)0, border);
		}

		private static Texture2D Load(string fileName, ManualLogSource log)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			string text = Path.Combine(AssetsDir, fileName);
			if (!File.Exists(text))
			{
				log.LogWarning((object)("asset missing: " + text));
				return null;
			}
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false)
			{
				wrapMode = (TextureWrapMode)1,
				filterMode = (FilterMode)1
			};
			if (LoadImageViaReflection(val, File.ReadAllBytes(text)))
			{
				return val;
			}
			log.LogWarning((object)("ImageConversion.LoadImage failed for " + text));
			return null;
		}

		private static bool LoadImageViaReflection(Texture2D tex, byte[] data)
		{
			Type type = AccessTools.TypeByName("UnityEngine.ImageConversion");
			if (type == null)
			{
				return false;
			}
			MethodInfo methodInfo = null;
			MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
			foreach (MethodInfo methodInfo2 in methods)
			{
				if (!(methodInfo2.Name != "LoadImage"))
				{
					ParameterInfo[] parameters = methodInfo2.GetParameters();
					if (parameters.Length == 3 && parameters[1].ParameterType == typeof(byte[]))
					{
						methodInfo = methodInfo2;
						break;
					}
				}
			}
			if (methodInfo == null)
			{
				return false;
			}
			object obj = methodInfo.Invoke(null, new object[3] { tex, data, false });
			bool flag = default(bool);
			int num;
			if (obj is bool)
			{
				flag = (bool)obj;
				num = 1;
			}
			else
			{
				num = 0;
			}
			return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
		}
	}
	internal sealed class BarSkin
	{
		public RectTransform Panel;

		public RectTransform Inner;

		public GuiBar Fast;

		public GuiBar Slow;

		public Image Frame;

		public TMP_Text Text;

		public float Height = 1f;

		public float LengthScale = 1f;

		public float FullStat = 325f;

		public float FrameScale = 1f;

		public Vector2 FrameOffset = Vector2.zero;

		private float m_window = -1f;

		public bool Valid
		{
			get
			{
				if ((Object)(object)Panel != (Object)null && (Object)(object)Fast != (Object)null)
				{
					return (Object)(object)Slow != (Object)null;
				}
				return false;
			}
		}

		public void ApplyHeight(float multiplier)
		{
			Height = Mathf.Max(0.1f, multiplier);
			m_window = -1f;
			RefreshFrame();
		}

		public void ApplyLength(float multiplier)
		{
			LengthScale = Mathf.Max(0.1f, multiplier);
			m_window = -1f;
		}

		public void ApplyFrame(float scale, Vector2 offset)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			FrameScale = Mathf.Max(0.1f, scale);
			FrameOffset = offset;
			RefreshFrame();
		}

		private Vector2 RestingOffset()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			float num = Height * FrameScale;
			return new Vector2((AssetLoader.FrameRightUnits - AssetLoader.FrameLeftUnits) * num * 0.5f, 0f);
		}

		public void Drive(float current, float max)
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			if (!Valid)
			{
				return;
			}
			float num = Mathf.Max(max / FullStat * 560.9f * LengthScale, AssetLoader.MinWindowUnits);
			if (!Mathf.Approximately(num, m_window))
			{
				m_window = num;
				Panel.sizeDelta = new Vector2(num, 32f * Height);
				if ((Object)(object)Inner != (Object)null && (Object)(object)Inner != (Object)(object)Panel)
				{
					Inner.sizeDelta = Vector2.zero;
				}
				Fast.SetWidth(num);
				Slow.SetWidth(num);
				SizeFill(Fast);
				SizeFill(Slow);
				RefreshFrame();
			}
			Fast.SetMaxValue(max);
			Fast.SetValue(current);
			Slow.SetMaxValue(max);
			Slow.SetValue(current);
			if ((Object)(object)Text != (Object)null)
			{
				string text = Mathf.CeilToInt(current).ToString();
				if (Text.text != text)
				{
					Text.text = text;
				}
			}
		}

		public void Show(bool visible)
		{
			if ((Object)(object)Panel != (Object)null && ((Component)Panel).gameObject.activeSelf != visible)
			{
				((Component)Panel).gameObject.SetActive(visible);
			}
		}

		private void RefreshFrame()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_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_004b: 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_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)Frame == (Object)null))
			{
				float num = Height * FrameScale;
				Frame.pixelsPerUnitMultiplier = 1f / num;
				RectTransform val = (RectTransform)((Component)Frame).transform;
				val.anchorMin = Vector2.zero;
				val.anchorMax = Vector2.one;
				val.sizeDelta = new Vector2((AssetLoader.FrameLeftUnits + AssetLoader.FrameRightUnits) * num, AssetLoader.FrameRailUnits * 2f * num);
				val.anchoredPosition = RestingOffset() + FrameOffset;
			}
		}

		private void SizeFill(GuiBar guiBar)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			RectTransform val = guiBar?.m_bar;
			if (!((Object)(object)val == (Object)null))
			{
				val.sizeDelta = new Vector2(val.sizeDelta.x, 32f * Height);
				Image component = ((Component)val).GetComponent<Image>();
				if ((Object)(object)component != (Object)null)
				{
					component.pixelsPerUnitMultiplier = 1f / Height;
				}
			}
		}
	}
	internal static class HudLayout
	{
		private class Element
		{
			public string Key;

			public string Label;

			public RectTransform Move;

			public RectTransform Hit;

			public ConfigEntry<string> Entry;

			public GameObject Overlay;

			public float Scale = 1f;

			public Action<float> OnScale;

			public Action<float> OnLength;

			public float Length = 1f;

			public Action<float, Vector2> OnFrame;

			public float FrameScale = 1f;

			public Vector2 FrameOffset;
		}

		private class EditHandle : MonoBehaviour, IBeginDragHandler, IEventSystemHandler, IDragHandler, IEndDragHandler, IScrollHandler
		{
			private Element m_element;

			private Vector2 m_grabOffset;

			private bool m_frameDrag;

			public void Init(Element element)
			{
				m_element = element;
			}

			public void OnBeginDrag(PointerEventData eventData)
			{
				//IL_004e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0041: Unknown result type (might be due to invalid IL or missing references)
				//IL_0053: Unknown result type (might be due to invalid IL or missing references)
				//IL_0061: Unknown result type (might be due to invalid IL or missing references)
				//IL_006c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0071: Unknown result type (might be due to invalid IL or missing references)
				//IL_0072: Unknown result type (might be due to invalid IL or missing references)
				//IL_0077: Unknown result type (might be due to invalid IL or missing references)
				if (m_element != null && s_editing)
				{
					m_frameDrag = Ctrl() && m_element.OnFrame != null;
					Vector2 val = (m_frameDrag ? m_element.FrameOffset : m_element.Move.anchoredPosition);
					m_grabOffset = ToLocal(m_element.Move, eventData.pressPosition, eventData.pressEventCamera) - val;
				}
			}

			public void OnDrag(PointerEventData eventData)
			{
				//IL_001c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_002d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0032: Unknown result type (might be due to invalid IL or missing references)
				//IL_0037: Unknown result type (might be due to invalid IL or missing references)
				//IL_0063: Unknown result type (might be due to invalid IL or missing references)
				//IL_0046: Unknown result type (might be due to invalid IL or missing references)
				//IL_0047: Unknown result type (might be due to invalid IL or missing references)
				if (m_element != null && s_editing)
				{
					Vector2 val = ToLocal(m_element.Move, eventData.position, eventData.pressEventCamera) - m_grabOffset;
					if (m_frameDrag)
					{
						m_element.FrameOffset = val;
						Apply(m_element);
					}
					else
					{
						m_element.Move.anchoredPosition = val;
					}
				}
			}

			public void OnEndDrag(PointerEventData eventData)
			{
				if (m_element != null)
				{
					Save(m_element);
					Persist();
				}
			}

			public void OnScroll(PointerEventData eventData)
			{
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				if (m_element == null || !s_editing)
				{
					return;
				}
				float y = eventData.scrollDelta.y;
				if (!(Mathf.Abs(y) < 0.01f))
				{
					float num = Mathf.Sign(y) * 0.05f;
					if (Ctrl() && m_element.OnFrame != null)
					{
						m_element.FrameScale = Mathf.Clamp(m_element.FrameScale + num, 0.3f, 3f);
					}
					else if (Shift() && m_element.OnLength != null)
					{
						m_element.Length = Mathf.Clamp(m_element.Length + num, 0.3f, 3f);
					}
					else
					{
						m_element.Scale = Mathf.Clamp(m_element.Scale + num, 0.3f, 3f);
					}
					Apply(m_element);
					Save(m_element);
					Persist();
				}
			}
		}

		private const float MinScale = 0.3f;

		private const float MaxScale = 3f;

		private const float ScaleStep = 0.05f;

		private static readonly List<Element> s_elements = new List<Element>();

		private static readonly FieldInfo s_mouseCapture = AccessTools.Field(typeof(GameCamera), "m_mouseCapture");

		private static ConfigFile s_config;

		private static ConfigEntry<bool> s_editMode;

		private static TMP_FontAsset s_font;

		private static Sprite s_white;

		private static FileSystemWatcher s_watcher;

		private static volatile bool s_reloadPending;

		private static float s_ignoreWritesUntil;

		private static bool s_editing;

		private static readonly List<GraphicRaycaster> s_addedRaycasters = new List<GraphicRaycaster>();

		internal static ManualLogSource Log;

		public static bool Editing => s_editing;

		public static void Init(ConfigFile config, ConfigEntry<bool> editMode)
		{
			s_config = config;
			s_editMode = editMode;
			s_config.SaveOnConfigSet = false;
			Watch();
		}

		private static void Watch()
		{
			try
			{
				string configFilePath = s_config.ConfigFilePath;
				s_watcher = new FileSystemWatcher(Path.GetDirectoryName(configFilePath), Path.GetFileName(configFilePath))
				{
					NotifyFilter = (NotifyFilters.Size | NotifyFilters.LastWrite)
				};
				s_watcher.Changed += delegate
				{
					s_reloadPending = true;
				};
				s_watcher.EnableRaisingEvents = true;
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("could not watch the config file, changes to it will need a restart: " + ex.Message));
			}
		}

		private static void Persist()
		{
			s_ignoreWritesUntil = Time.realtimeSinceStartup + 1f;
			s_config.Save();
		}

		private static void ReloadFromConfig()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: 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)
			foreach (Element s_element in s_elements)
			{
				if (TryParse(s_element.Entry.Value, out var position, out var scale, out var length, out var frameScale, out var frameOffset))
				{
					s_element.Move.anchoredPosition = position;
					s_element.Scale = Mathf.Clamp(scale, 0.3f, 3f);
					s_element.Length = Mathf.Clamp(length, 0.3f, 3f);
					s_element.FrameScale = Mathf.Clamp(frameScale, 0.3f, 3f);
					s_element.FrameOffset = frameOffset;
					Apply(s_element);
				}
			}
			Log.LogInfo((object)"config reloaded from disk");
		}

		public static void Reset(TMP_FontAsset font)
		{
			//IL_006f: 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_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: 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: Expected O, but got Unknown
			foreach (Element s_element in s_elements)
			{
				if ((Object)(object)s_element.Overlay != (Object)null)
				{
					Object.Destroy((Object)(object)s_element.Overlay);
				}
			}
			s_elements.Clear();
			s_editing = false;
			s_font = font;
			if (!((Object)(object)s_white != (Object)null))
			{
				Texture2D val = new Texture2D(1, 1, (TextureFormat)4, false);
				val.SetPixel(0, 0, Color.white);
				val.Apply();
				s_white = Sprite.Create(val, new Rect(0f, 0f, 1f, 1f), new Vector2(0.5f, 0.5f));
			}
		}

		public static void Register(string key, string label, RectTransform move, RectTransform hit, Vector2 fallbackPosition, float fallbackScale = 1f, Action<float> onScale = null, Action<float> onLength = null, Action<float, Vector2> onFrame = null, float fallbackFrameScale = 1f, float fallbackLength = 1f, Vector2? supersededPosition = null)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: 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_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: 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_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)move == (Object)null) && !((Object)(object)hit == (Object)null))
			{
				Element element = new Element
				{
					Key = key,
					Label = label,
					Move = move,
					Hit = hit,
					OnScale = onScale,
					OnLength = onLength,
					OnFrame = onFrame,
					Entry = s_config.Bind<string>("Layout", key, Format(fallbackPosition, fallbackScale, fallbackLength, fallbackFrameScale, Vector2.zero), "Layout of the " + label + ". Written by edit mode; x,y,size,length,frameSize,frameX,frameY.")
				};
				if (!TryParse(element.Entry.Value, out var position, out var scale, out var length, out var frameScale, out var frameOffset))
				{
					position = fallbackPosition;
					scale = fallbackScale;
					length = fallbackLength;
					frameScale = fallbackFrameScale;
					frameOffset = Vector2.zero;
					Log.LogWarning((object)("could not read layout for " + key + " (\"" + element.Entry.Value + "\") - using the default"));
				}
				if (supersededPosition.HasValue && Mathf.Approximately(position.x, supersededPosition.Value.x) && Mathf.Approximately(position.y, supersededPosition.Value.y))
				{
					position = fallbackPosition;
					element.Entry.Value = Format(position, scale, length, frameScale, frameOffset);
					Log.LogInfo((object)("moved " + key + " off the position an older version defaulted it to"));
				}
				element.Scale = Mathf.Clamp(scale, 0.3f, 3f);
				element.Length = Mathf.Clamp(length, 0.3f, 3f);
				element.FrameScale = Mathf.Clamp(frameScale, 0.3f, 3f);
				element.FrameOffset = frameOffset;
				move.anchoredPosition = position;
				Apply(element);
				s_elements.Add(element);
			}
		}

		public static void Tick()
		{
			if (s_reloadPending)
			{
				s_reloadPending = false;
				if (Time.realtimeSinceStartup > s_ignoreWritesUntil)
				{
					s_config.Reload();
					ReloadFromConfig();
				}
			}
			if (s_editMode.Value != s_editing)
			{
				Toggle();
			}
		}

		private static void Toggle()
		{
			s_editing = s_editMode.Value;
			foreach (Element s_element in s_elements)
			{
				EnsureOverlay(s_element);
				if ((Object)(object)s_element.Overlay != (Object)null)
				{
					s_element.Overlay.SetActive(s_editing);
				}
			}
			if (s_editing)
			{
				AddRaycasters();
			}
			else
			{
				RemoveRaycasters();
			}
			if ((Object)(object)GameCamera.instance != (Object)null)
			{
				s_mouseCapture?.SetValue(GameCamera.instance, !s_editing);
			}
			else
			{
				Log.LogWarning((object)"no GameCamera - the cursor will not be released for editing");
			}
			if (!s_editing)
			{
				Persist();
			}
			Log.LogInfo((object)(s_editing ? "layout edit on - drag: move | wheel: size | shift+wheel: bar length | ctrl+wheel: frame size | ctrl+drag: frame offset" : ("layout edit off - saved to " + s_config.ConfigFilePath)));
		}

		private static void AddRaycasters()
		{
			foreach (Element s_element in s_elements)
			{
				Canvas val = (((Object)(object)s_element.Hit != (Object)null) ? ((Component)s_element.Hit).GetComponentInParent<Canvas>() : null);
				if ((Object)(object)val == (Object)null)
				{
					Log.LogWarning((object)("no canvas above " + s_element.Key + " - it cannot be dragged"));
				}
				else if (!((Object)(object)((Component)val).GetComponent<GraphicRaycaster>() != (Object)null))
				{
					GraphicRaycaster item = ((Component)val).gameObject.AddComponent<GraphicRaycaster>();
					s_addedRaycasters.Add(item);
					Log.LogInfo((object)("added a GraphicRaycaster to " + ((Object)val).name + " for edit mode"));
				}
			}
			if ((Object)(object)EventSystem.current == (Object)null)
			{
				Log.LogWarning((object)"no EventSystem - nothing can be dragged");
			}
		}

		private static void RemoveRaycasters()
		{
			foreach (GraphicRaycaster s_addedRaycaster in s_addedRaycasters)
			{
				if ((Object)(object)s_addedRaycaster != (Object)null)
				{
					Object.Destroy((Object)(object)s_addedRaycaster);
				}
			}
			s_addedRaycasters.Clear();
		}

		private static bool Ctrl()
		{
			if (!Input.GetKey((KeyCode)306))
			{
				return Input.GetKey((KeyCode)305);
			}
			return true;
		}

		private static bool Shift()
		{
			if (!Input.GetKey((KeyCode)304))
			{
				return Input.GetKey((KeyCode)303);
			}
			return true;
		}

		private static Vector2 ToLocal(RectTransform rt, Vector2 screen, Camera cam)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			Transform parent = ((Transform)rt).parent;
			Vector2 result = default(Vector2);
			RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)(object)((parent is RectTransform) ? parent : null), screen, cam, ref result);
			return result;
		}

		private static void EnsureOverlay(Element e)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Expected O, but got Unknown
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)e.Overlay != (Object)null))
			{
				e.Overlay = new GameObject("CarturUIHud_Edit_" + e.Key, new Type[1] { typeof(RectTransform) });
				RectTransform val = (RectTransform)e.Overlay.transform;
				((Transform)val).SetParent((Transform)(object)e.Hit, false);
				val.anchorMin = Vector2.zero;
				val.anchorMax = Vector2.one;
				val.offsetMin = Vector2.zero;
				val.offsetMax = Vector2.zero;
				Image obj = e.Overlay.AddComponent<Image>();
				obj.sprite = s_white;
				((Graphic)obj).color = new Color(0.95f, 0.75f, 0.15f, 0.1f);
				((Graphic)obj).raycastTarget = true;
				((Component)obj).gameObject.AddComponent<EditHandle>().Init(e);
				Outline obj2 = e.Overlay.AddComponent<Outline>();
				((Shadow)obj2).effectColor = new Color(1f, 0.92f, 0.25f, 0.85f);
				((Shadow)obj2).effectDistance = new Vector2(2f, -2f);
				((Shadow)obj2).useGraphicAlpha = false;
				if (!((Object)(object)s_font == (Object)null))
				{
					GameObject val2 = new GameObject("Label", new Type[1] { typeof(RectTransform) });
					RectTransform val3 = (RectTransform)val2.transform;
					((Transform)val3).SetParent((Transform)(object)val, false);
					val3.anchorMin = new Vector2(0.5f, 0.5f);
					val3.anchorMax = new Vector2(0.5f, 0.5f);
					val3.sizeDelta = new Vector2(180f, 24f);
					val3.anchoredPosition = Vector2.zero;
					TextMeshProUGUI obj3 = val2.AddComponent<TextMeshProUGUI>();
					((TMP_Text)obj3).font = s_font;
					((TMP_Text)obj3).text = e.Label;
					((TMP_Text)obj3).fontSize = 13f;
					((Graphic)obj3).color = new Color(1f, 0.95f, 0.75f, 0.85f);
					((TMP_Text)obj3).alignment = (TextAlignmentOptions)514;
					((Graphic)obj3).raycastTarget = false;
				}
			}
		}

		private static void Apply(Element e)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			if (e.OnScale != null)
			{
				e.OnScale(e.Scale);
			}
			else
			{
				((Transform)e.Move).localScale = Vector3.one * e.Scale;
			}
			e.OnLength?.Invoke(e.Length);
			e.OnFrame?.Invoke(e.FrameScale, e.FrameOffset);
		}

		private static void Save(Element e)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			e.Entry.Value = Format(e.Move.anchoredPosition, e.Scale, e.Length, e.FrameScale, e.FrameOffset);
		}

		private static string Format(Vector2 position, float scale, float length, float frameScale, Vector2 frameOffset)
		{
			//IL_0012: 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_0049: 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)
			return string.Format(CultureInfo.InvariantCulture, "{0:0.##},{1:0.##},{2:0.###},{3:0.###},{4:0.###},{5:0.##},{6:0.##}", position.x, position.y, scale, length, frameScale, frameOffset.x, frameOffset.y);
		}

		private static bool TryParse(string value, out Vector2 position, out float scale, out float length, out float frameScale, out Vector2 frameOffset)
		{
			//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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: 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_00f0: Unknown result type (might be due to invalid IL or missing references)
			position = Vector2.zero;
			scale = 1f;
			length = 1f;
			frameScale = 1f;
			frameOffset = Vector2.zero;
			if (string.IsNullOrEmpty(value))
			{
				return false;
			}
			string[] array = value.Split(new char[1] { ',' });
			if (array.Length < 3)
			{
				return false;
			}
			CultureInfo invariantCulture = CultureInfo.InvariantCulture;
			if (!float.TryParse(array[0], NumberStyles.Float, invariantCulture, out var result) || !float.TryParse(array[1], NumberStyles.Float, invariantCulture, out var result2) || !float.TryParse(array[2], NumberStyles.Float, invariantCulture, out scale))
			{
				return false;
			}
			if (array.Length >= 4 && !float.TryParse(array[3], NumberStyles.Float, invariantCulture, out length))
			{
				return false;
			}
			if (array.Length >= 7)
			{
				if (!float.TryParse(array[4], NumberStyles.Float, invariantCulture, out frameScale) || !float.TryParse(array[5], NumberStyles.Float, invariantCulture, out var result3) || !float.TryParse(array[6], NumberStyles.Float, invariantCulture, out var result4))
				{
					return false;
				}
				frameOffset = new Vector2(result3, result4);
			}
			position = new Vector2(result, result2);
			return true;
		}
	}
	[HarmonyPatch(typeof(Player), "TakeInput")]
	internal static class EditInputBlock
	{
		[HarmonyPostfix]
		private static void Postfix(ref bool __result)
		{
			if (HudLayout.Editing)
			{
				__result = false;
			}
		}
	}
	internal static class HudSkin
	{
		private static readonly Vector2 HealthHome = new Vector2(307.5f, 309.25f);

		private static readonly Vector2 StaminaHome = new Vector2(330.75f, 272.75f);

		private static readonly Vector2 EitrHome = new Vector2(330.75f, 236.25f);

		private static readonly Vector2 AdrenalineHome = new Vector2(330.75f, 199.75f);

		private static readonly Vector2 SharedRowLegacy = new Vector2(330.75f, 219.75f);

		private const float HealthFrame = 0.85f;

		private const float HealthLength = 1f;

		private const float StaminaLength = 0.9728f;

		private const float RowThreeLength = 0.9728f;

		private const float FoodSize = 82f;

		private const float PowerSize = 104f;

		private static readonly Vector2 ClusterHome = new Vector2(90.95f, 143.6f);

		private const float ClusterScale = 0.9f;

		private static readonly Vector2 ClusterSize = new Vector2(230.2f, 210.35f);

		private static readonly Vector2 Food0 = new Vector2(127.8f, 161.15f);

		private static readonly Vector2 Food1 = new Vector2(183.05f, 104.15f);

		private static readonly Vector2 Food2 = new Vector2(127.8f, 47.15f);

		private static readonly Vector2 Power = new Vector2(59.8f, 103.9f);

		private static readonly float[] FoodScales = new float[3] { 1.2f, 1.15f, 1.15f };

		private const float PowerScale = 1.15f;

		private const float HealthHeight = 0.9f;

		private const float StatHeight = 0.55f;

		private const float HealthFull = 325f;

		private const float StaminaFull = 285f;

		private const float EitrFull = 315f;

		private const float AdrenalineFull = 100f;

		internal const float MaxWindowUnits = 560.9f;

		private const float IconFraction = 0.55f;

		internal const string EaqsGuid = "randyknapp.mods.equipmentandquickslots";

		internal static readonly BarSkin[] Bars = new BarSkin[4]
		{
			new BarSkin(),
			new BarSkin(),
			new BarSkin(),
			new BarSkin()
		};

		private static Image[] s_foodFrames;

		private static TMP_Text[] s_foodTimes = (TMP_Text[])(object)new TMP_Text[3];

		private static readonly ItemData[] s_slotItems = (ItemData[])(object)new ItemData[3];

		private static bool s_quickSlotMode;

		private static bool s_started;

		private static MethodInfo s_getQuickSlotItems;

		internal static ManualLogSource Log;

		[HarmonyPatch(typeof(Hud), "Awake")]
		[HarmonyPostfix]
		private static void Skin(Hud __instance)
		{
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_019b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			Transform val = ((Component)__instance).transform.Find("hudroot");
			if ((Object)(object)val == (Object)null)
			{
				Log.LogWarning((object)"no hudroot - nothing skinned");
				return;
			}
			Canvas componentInParent = ((Component)__instance).GetComponentInParent<Canvas>();
			AssetLoader.BuildSprites(((Object)(object)componentInParent != (Object)null) ? componentInParent.referencePixelsPerUnit : 100f, Log);
			HudLayout.Reset(((Object)(object)__instance.m_healthText != (Object)null) ? __instance.m_healthText.font : null);
			s_started = false;
			Build(0, 325f, "health", "Health", 0.9f, 0.85f, 1f, HealthHome, val, __instance.m_healthBarRoot, __instance.m_healthBarRoot, __instance.m_healthBarFast, __instance.m_healthBarSlow, __instance.m_healthText, __instance.m_healthAnimator);
			Vector2 staminaHome = StaminaHome;
			RectTransform staminaBar2Root = __instance.m_staminaBar2Root;
			RectTransform staminaBar2Root2 = __instance.m_staminaBar2Root;
			Transform obj = ((staminaBar2Root2 != null) ? ((Transform)staminaBar2Root2).Find("Stamina") : null);
			Build(1, 285f, "stamina", "Stamina", 0.55f, 1f, 0.9728f, staminaHome, val, staminaBar2Root, (RectTransform)(object)((obj is RectTransform) ? obj : null), __instance.m_staminaBar2Fast, __instance.m_staminaBar2Slow, __instance.m_staminaText, __instance.m_staminaAnimator);
			Vector2 eitrHome = EitrHome;
			RectTransform eitrBarRoot = __instance.m_eitrBarRoot;
			RectTransform eitrBarRoot2 = __instance.m_eitrBarRoot;
			Transform obj2 = ((eitrBarRoot2 != null) ? ((Transform)eitrBarRoot2).Find("Stamina") : null);
			Build(2, 315f, "eitr", "Eitr", 0.55f, 1f, 0.9728f, eitrHome, val, eitrBarRoot, (RectTransform)(object)((obj2 is RectTransform) ? obj2 : null), __instance.m_eitrBarFast, __instance.m_eitrBarSlow, __instance.m_eitrText, __instance.m_eitrAnimator, SharedRowLegacy);
			Vector2 adrenalineHome = AdrenalineHome;
			RectTransform adrenalineBarRoot = __instance.m_adrenalineBarRoot;
			RectTransform adrenalineBarRoot2 = __instance.m_adrenalineBarRoot;
			Transform obj3 = ((adrenalineBarRoot2 != null) ? ((Transform)adrenalineBarRoot2).Find("Stamina") : null);
			Build(3, 100f, "adrenaline", "Adrenaline", 0.55f, 1f, 0.9728f, adrenalineHome, val, adrenalineBarRoot, (RectTransform)(object)((obj3 is RectTransform) ? obj3 : null), __instance.m_adrenalineBarFast, __instance.m_adrenalineBarSlow, __instance.m_adrenalineText, __instance.m_adrenalineAnimator, SharedRowLegacy);
			RectTransform healthPanel = __instance.m_healthPanel;
			Hide((healthPanel != null) ? ((Transform)healthPanel).Find("healthicon") : null);
			RectTransform healthPanel2 = __instance.m_healthPanel;
			Hide((healthPanel2 != null) ? ((Transform)healthPanel2).Find("foodicon") : null);
			RectTransform healthPanel3 = __instance.m_healthPanel;
			Hide((healthPanel3 != null) ? ((Transform)healthPanel3).Find("foodicon (1)") : null);
			RectTransform val2 = MakeCluster(val);
			SkinFood(__instance, val2);
			SkinPower(__instance, val2);
			HudLayout.Register("cluster", "Food + power", val2, val2, ClusterHome, 0.9f);
			Log.LogInfo((object)("driving 4 bars, skinned 3 food boxes and the guardian power box" + (s_quickSlotMode ? ", food from Equipment and Quick Slots" : ", food from vanilla")));
		}

		private static void Build(int index, float fullStat, string key, string label, float height, float frameScale, float length, Vector2 position, Transform hudroot, RectTransform panel, RectTransform inner, GuiBar fast, GuiBar slow, TMP_Text text, Animator animator, Vector2? supersededPosition = null)
		{
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: 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_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: 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_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)panel == (Object)null)
			{
				Log.LogWarning((object)("no panel for " + label + " - that bar is left alone"));
				return;
			}
			BarSkin barSkin = Bars[index];
			barSkin.Panel = panel;
			barSkin.Inner = inner ?? panel;
			barSkin.Fast = fast;
			barSkin.Slow = slow;
			barSkin.Text = text;
			barSkin.FullStat = fullStat;
			if ((Object)(object)animator != (Object)null)
			{
				((Behaviour)animator).enabled = false;
			}
			CanvasGroup[] componentsInChildren = ((Component)panel).GetComponentsInChildren<CanvasGroup>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].alpha = 1f;
			}
			Transform[] componentsInChildren2 = ((Component)panel).GetComponentsInChildren<Transform>(true);
			foreach (Transform obj in componentsInChildren2)
			{
				obj.localRotation = Quaternion.identity;
				obj.localScale = Vector3.one;
			}
			Vector2 val2 = default(Vector2);
			if ((Object)(object)text != (Object)null)
			{
				RectTransform val = (RectTransform)text.transform;
				((Transform)val).SetParent((Transform)(object)barSkin.Inner, false);
				((Vector2)(ref val2))..ctor(0.5f, 0.5f);
				val.anchorMax = val2;
				val.anchorMin = val2;
				val.pivot = new Vector2(0.5f, 0.5f);
				val.anchoredPosition = Vector2.zero;
				((Transform)val).localRotation = Quaternion.identity;
				((Transform)val).localScale = Vector3.one;
				((Transform)val).SetAsLastSibling();
			}
			barSkin.Frame = SkinFrame(barSkin.Inner);
			SkinFill(fast);
			SkinFill(slow);
			((Transform)panel).SetParent(hudroot, false);
			val2 = (panel.anchorMin = (panel.anchorMax = Vector2.zero));
			panel.pivot = new Vector2(0f, 0.5f);
			((Transform)panel).SetAsFirstSibling();
			RectTransform hit = (RectTransform)((!((Object)(object)barSkin.Frame != (Object)null)) ? ((object)panel) : ((object)(RectTransform)((Component)barSkin.Frame).transform));
			HudLayout.Register(key, label, panel, hit, position, height, barSkin.ApplyHeight, barSkin.ApplyLength, barSkin.ApplyFrame, frameScale, length, supersededPosition);
		}

		private static Image SkinFrame(RectTransform bar)
		{
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)bar == (Object)null)
			{
				return null;
			}
			Transform val = ((Transform)bar).Find("border");
			if ((Object)(object)val == (Object)null)
			{
				Log.LogWarning((object)("no 'border' child under " + ((Object)bar).name + " - that bar keeps the vanilla frame"));
				return null;
			}
			if (!((Component)val).gameObject.activeSelf)
			{
				((Component)val).gameObject.SetActive(true);
			}
			Image component = ((Component)val).GetComponent<Image>();
			if ((Object)(object)component == (Object)null)
			{
				Log.LogWarning((object)("'border' under " + ((Object)bar).name + " has no Image - that bar keeps the vanilla frame"));
				return null;
			}
			component.sprite = AssetLoader.BarFrame;
			component.type = (Type)1;
			((Graphic)component).color = Color.white;
			component.fillCenter = false;
			val.SetAsLastSibling();
			return component;
		}

		private static void SkinFill(GuiBar guiBar)
		{
			object obj;
			if (guiBar == null)
			{
				obj = null;
			}
			else
			{
				RectTransform bar = guiBar.m_bar;
				obj = ((bar != null) ? ((Component)bar).GetComponent<Image>() : null);
			}
			Image val = (Image)obj;
			if (!((Object)(object)val == (Object)null))
			{
				val.sprite = AssetLoader.BarFill;
				val.type = (Type)2;
			}
		}

		private static void SkinFood(Hud hud, RectTransform cluster)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: 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_00d3: 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_00f1: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			s_quickSlotMode = Chainloader.PluginInfos.ContainsKey("randyknapp.mods.equipmentandquickslots") && ResolveEaqs();
			s_foodFrames = (Image[])(object)new Image[3];
			s_foodTimes = (TMP_Text[])(object)new TMP_Text[3];
			Vector2[] array = (Vector2[])(object)new Vector2[3] { Food0, Food1, Food2 };
			Vector2 val4 = default(Vector2);
			for (int i = 0; i < 3; i++)
			{
				RectTransform healthPanel = hud.m_healthPanel;
				Transform val = ((healthPanel != null) ? ((Transform)healthPanel).Find("food" + i) : null);
				if (!((Object)(object)val == (Object)null))
				{
					Image component = ((Component)val).GetComponent<Image>();
					if ((Object)(object)component != (Object)null)
					{
						component.sprite = AssetLoader.FoodFrame;
						component.type = (Type)0;
						component.preserveAspect = true;
						((Graphic)component).color = Color.white;
					}
					s_foodFrames[i] = component;
					Seat((RectTransform)val, cluster, array[i], 82f, FoodScales[i]);
					Image val2 = ((hud.m_foodIcons.Length > i) ? hud.m_foodIcons[i] : null);
					if ((Object)(object)val2 != (Object)null)
					{
						float num = 36.899998f;
						((RectTransform)((Component)val2).transform).sizeDelta = new Vector2(0f - num, 0f - num);
					}
					s_foodTimes[i] = ((hud.m_foodTime.Length > i) ? hud.m_foodTime[i] : null);
					if ((Object)(object)s_foodTimes[i] != (Object)null)
					{
						RectTransform val3 = (RectTransform)s_foodTimes[i].transform;
						((Vector2)(ref val4))..ctor(0.5f, 0f);
						val3.anchorMax = val4;
						val3.anchorMin = val4;
						val3.pivot = new Vector2(0.5f, 0f);
						val3.anchoredPosition = new Vector2(0f, 16.4f);
					}
				}
			}
			if (s_quickSlotMode)
			{
				hud.m_foodIcons = Array.Empty<Image>();
				hud.m_foodBars = Array.Empty<Image>();
				hud.m_foodTime = Array.Empty<TMP_Text>();
			}
		}

		private static bool ResolveEaqs()
		{
			Type type = AccessTools.TypeByName("EquipmentAndQuickSlots.API");
			s_getQuickSlotItems = type?.GetMethod("GetQuickSlotItems", BindingFlags.Static | BindingFlags.Public);
			if (s_getQuickSlotItems == null)
			{
				Log.LogWarning((object)"Equipment and Quick Slots is loaded but API.GetQuickSlotItems is gone - falling back to vanilla food");
				return false;
			}
			MethodInfo method = type.GetMethod("AddSlotItemChangedListener", BindingFlags.Static | BindingFlags.Public);
			if (method != null)
			{
				Action<string, ItemData, ItemData> action = delegate
				{
					RefreshQuickSlots();
				};
				method.Invoke(null, new object[1] { action });
			}
			else
			{
				Log.LogWarning((object)"Equipment and Quick Slots API.AddSlotItemChangedListener is gone - quick slot icons will not refresh");
			}
			return true;
		}

		private static void RefreshQuickSlots()
		{
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			if (s_getQuickSlotItems == null || s_foodFrames == null)
			{
				return;
			}
			IList<ItemData> list = s_getQuickSlotItems.Invoke(null, null) as IList<ItemData>;
			for (int i = 0; i < s_foodFrames.Length; i++)
			{
				s_slotItems[i] = ((list != null && i < list.Count) ? list[i] : null);
				Image val = s_foodFrames[i];
				if ((Object)(object)val == (Object)null || ((Component)val).transform.childCount == 0)
				{
					continue;
				}
				Image component = ((Component)((Component)val).transform.GetChild(0)).GetComponent<Image>();
				if (!((Object)(object)component == (Object)null))
				{
					ItemData val2 = s_slotItems[i];
					((Component)component).gameObject.SetActive(val2 != null);
					if (val2 != null)
					{
						component.sprite = val2.GetIcon();
						((Graphic)component).color = Color.white;
					}
				}
			}
		}

		private static void ShowMatchingFoodTimes(Player player)
		{
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			List<Food> foods = player.GetFoods();
			for (int i = 0; i < s_foodTimes.Length; i++)
			{
				TMP_Text val = s_foodTimes[i];
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				Food val2 = null;
				ItemData val3 = s_slotItems[i];
				if (val3?.m_shared != null)
				{
					foreach (Food item in foods)
					{
						if (item?.m_item?.m_shared != null && item.m_item.m_shared.m_name == val3.m_shared.m_name)
						{
							val2 = item;
							break;
						}
					}
				}
				if (val2 == null)
				{
					if (((Component)val).gameObject.activeSelf)
					{
						((Component)val).gameObject.SetActive(false);
					}
					continue;
				}
				if (!((Component)val).gameObject.activeSelf)
				{
					((Component)val).gameObject.SetActive(true);
				}
				float num = val2.m_time / Game.m_foodRate;
				string text = ((num >= 60f) ? (Mathf.CeilToInt(num / 60f) + "m") : (Mathf.FloorToInt(num) + "s"));
				if (val.text != text)
				{
					val.text = text;
				}
				((Graphic)val).color = (Color)((num >= 60f) ? Color.white : new Color(1f, 1f, 1f, 0.4f + Mathf.Sin(Time.time * 10f) * 0.6f));
			}
		}

		private static void SkinPower(Hud hud, RectTransform cluster)
		{
			//IL_0013: 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)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			RectTransform gpRoot = hud.m_gpRoot;
			if (!((Object)(object)gpRoot == (Object)null))
			{
				Seat(gpRoot, cluster, Power, 104f, 1.15f);
				Transform obj = ((Transform)gpRoot).Find("Bkg");
				Image val = ((obj != null) ? ((Component)obj).GetComponent<Image>() : null);
				if ((Object)(object)val != (Object)null)
				{
					val.sprite = AssetLoader.FoodFrame;
					val.type = (Type)0;
					val.preserveAspect = true;
					((Graphic)val).color = Color.white;
					((RectTransform)((Component)val).transform).sizeDelta = new Vector2(104f, 104f);
				}
				float num = 57.2f;
				if ((Object)(object)hud.m_gpIcon != (Object)null)
				{
					((RectTransform)((Component)hud.m_gpIcon).transform).sizeDelta = new Vector2(num, num);
				}
				MoveText(hud.m_gpName, new Vector2(0f, (0f - num) * 0.18f));
				MoveText(hud.m_gpCooldown, new Vector2(0f, (0f - num) * 0.42f));
			}
		}

		private static void MoveText(TMP_Text text, Vector2 position)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: 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_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)text == (Object)null))
			{
				RectTransform val = (RectTransform)text.transform;
				Vector2 val2 = default(Vector2);
				((Vector2)(ref val2))..ctor(0.5f, 0.5f);
				val.anchorMax = val2;
				val.anchorMin = val2;
				val.pivot = new Vector2(0.5f, 0.5f);
				val.anchoredPosition = position;
			}
		}

		[HarmonyPatch(typeof(Hud), "Update")]
		[HarmonyPostfix]
		private static void Frame(Hud __instance)
		{
			HudLayout.Tick();
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			bool editing = HudLayout.Editing;
			Bars[0].Drive(((Character)localPlayer).GetHealth(), ((Character)localPlayer).GetMaxHealth());
			Bars[1].Drive(localPlayer.GetStamina(), ((Character)localPlayer).GetMaxStamina());
			float maxEitr = ((Character)localPlayer).GetMaxEitr();
			float maxAdrenaline = ((Character)localPlayer).GetMaxAdrenaline();
			Bars[3].Show(maxAdrenaline > 0f || editing);
			if (maxAdrenaline > 0f)
			{
				Bars[3].Drive(localPlayer.GetAdrenaline(), maxAdrenaline);
			}
			Bars[2].Show(maxEitr > 0f || editing);
			if (maxEitr > 0f)
			{
				Bars[2].Drive(localPlayer.GetEitr(), maxEitr);
			}
			if (editing && (Object)(object)__instance.m_gpRoot != (Object)null && !((Component)__instance.m_gpRoot).gameObject.activeSelf)
			{
				((Component)__instance.m_gpRoot).gameObject.SetActive(true);
			}
			if (s_quickSlotMode)
			{
				ShowMatchingFoodTimes(localPlayer);
			}
			if (s_started)
			{
				return;
			}
			s_started = true;
			if (s_quickSlotMode)
			{
				Transform obj = ((Component)__instance).transform.Find("hudroot");
				Transform val = ((obj != null) ? obj.Find("QuickSlotsHotkeyBar") : null);
				if ((Object)(object)val != (Object)null)
				{
					Hide(val);
				}
				else
				{
					Log.LogWarning((object)"QuickSlotsHotkeyBar not found - quick slot items may show twice");
				}
				RefreshQuickSlots();
			}
		}

		private static RectTransform MakeCluster(Transform hudroot)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//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_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: 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_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			RectTransform val = (RectTransform)new GameObject("CarturUIHud_Cluster", new Type[1] { typeof(RectTransform) }).transform;
			((Transform)val).SetParent(hudroot, false);
			Vector2 anchorMin = (val.anchorMax = Vector2.zero);
			val.anchorMin = anchorMin;
			val.pivot = Vector2.zero;
			val.sizeDelta = ClusterSize;
			((Transform)val).SetAsFirstSibling();
			return val;
		}

		private static void Seat(RectTransform rt, RectTransform cluster, Vector2 offset, float size, float scale)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//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)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			((Transform)rt).SetParent((Transform)(object)cluster, false);
			Vector2 anchorMin = (rt.anchorMax = Vector2.zero);
			rt.anchorMin = anchorMin;
			rt.pivot = new Vector2(0.5f, 0.5f);
			rt.sizeDelta = new Vector2(size, size);
			rt.anchoredPosition = offset;
			((Transform)rt).localScale = Vector3.one * scale;
		}

		private static void Hide(Transform t)
		{
			if ((Object)(object)t != (Object)null)
			{
				((Component)t).gameObject.SetActive(false);
			}
		}
	}
	[HarmonyPatch(typeof(Hud))]
	internal static class VanillaBars
	{
		[HarmonyPatch("UpdateHealth")]
		[HarmonyPrefix]
		private static bool Health()
		{
			return false;
		}

		[HarmonyPatch("UpdateStamina")]
		[HarmonyPrefix]
		private static bool Stamina()
		{
			return false;
		}

		[HarmonyPatch("UpdateEitr")]
		[HarmonyPrefix]
		private static bool Eitr()
		{
			return false;
		}

		[HarmonyPatch("UpdateAdrenaline")]
		[HarmonyPrefix]
		private static bool Adrenaline()
		{
			return false;
		}
	}
	[BepInPlugin("com.jekkle.valheim.carturuihud", "Carturs UI - HUD", "1.0.4")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.jekkle.valheim.carturuihud";

		public const string PluginName = "Carturs UI - HUD";

		public const string PluginVersion = "1.0.4";

		private void Awake()
		{
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				HudSkin.Log = ((BaseUnityPlugin)this).Logger;
				HudLayout.Log = ((BaseUnityPlugin)this).Logger;
				if (!AssetLoader.LoadTextures(((BaseUnityPlugin)this).Logger))
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("assets missing from " + AssetLoader.AssetsDir + " - HUD left vanilla"));
					return;
				}
				ConfigEntry<bool> editMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Layout", "editMode", false, "Turn on to arrange the HUD: drag a piece to move it, wheel to size it, shift+wheel for a bar's length, ctrl+wheel and ctrl+drag for its frame. Turn off when done - the layout is saved either way.");
				HudLayout.Init(((BaseUnityPlugin)this).Config, editMode);
				Harmony val = new Harmony("com.jekkle.valheim.carturuihud");
				val.PatchAll(typeof(HudSkin));
				val.PatchAll(typeof(VanillaBars));
				val.PatchAll(typeof(EditInputBlock));
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("patch failed, HUD left vanilla: " + ex));
			}
		}
	}
}