Decompiled source of TimerHUD v1.0.0

TimerHUD.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Eremite;
using Eremite.Buildings;
using Eremite.Buildings.UI.Trade;
using Eremite.Model;
using Eremite.Model.State;
using Eremite.Model.Trade;
using Eremite.Services;
using Eremite.View.HUD;
using Eremite.View.Popups;
using Eremite.WorldMap;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("TimerHUD")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Example Mod for Against The Storm")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d6ada77561ccfe85ca4c2c807fd24c8c3fc0d51c")]
[assembly: AssemblyProduct("TimerHUD")]
[assembly: AssemblyTitle("TimerHUD")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 TimerHUD
{
	public class HighlightReference : MonoBehaviour
	{
		public GameObject Highlight;
	}
	public class DesiredGoodsHUDWidget : MonoBehaviour
	{
		private GameObject rowGO;

		private RectTransform rowRT;

		private LayoutElement rowLE;

		private readonly List<GameObject> slots = new List<GameObject>();

		private readonly List<RectTransform> iconRows = new List<RectTransform>();

		private RectTransform firstRowRT;

		private RectTransform iconContainerRT;

		private float nextUpdateTime = 0f;

		private const int IconsPerRow = 10;

		private const float IconSpacing = 0f;

		private const float RowHeight = 24f;

		private const float RowSpacing = 0f;

		private bool toggleEnabled = true;

		public void SetToggleState(bool enabled)
		{
			toggleEnabled = enabled;
			if (!enabled && (Object)(object)rowGO != (Object)null)
			{
				rowGO.SetActive(false);
			}
		}

		public void Initialize()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Expected O, but got Unknown
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Expected O, but got Unknown
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fb: Expected O, but got Unknown
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_027b: Expected O, but got Unknown
			Transform parent = ((Component)this).transform.parent;
			rowGO = new GameObject("DesiredGoodsRow", new Type[1] { typeof(RectTransform) });
			rowGO.transform.SetParent(parent, false);
			rowRT = rowGO.GetComponent<RectTransform>();
			rowLE = rowGO.AddComponent<LayoutElement>();
			rowLE.minHeight = 24f;
			rowLE.preferredHeight = 24f;
			VerticalLayoutGroup val = rowGO.AddComponent<VerticalLayoutGroup>();
			((LayoutGroup)val).childAlignment = (TextAnchor)0;
			((HorizontalOrVerticalLayoutGroup)val).spacing = 0f;
			((HorizontalOrVerticalLayoutGroup)val).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val).childForceExpandHeight = false;
			GameObject val2 = new GameObject("DG_Row_0", new Type[1] { typeof(RectTransform) });
			val2.transform.SetParent((Transform)(object)rowRT, false);
			firstRowRT = val2.GetComponent<RectTransform>();
			LayoutElement val3 = val2.AddComponent<LayoutElement>();
			val3.minHeight = 24f;
			val3.preferredHeight = 24f;
			HorizontalLayoutGroup val4 = val2.AddComponent<HorizontalLayoutGroup>();
			((LayoutGroup)val4).childAlignment = (TextAnchor)0;
			((HorizontalOrVerticalLayoutGroup)val4).spacing = 0f;
			((HorizontalOrVerticalLayoutGroup)val4).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val4).childForceExpandHeight = false;
			GameObject val5 = new GameObject("DesiredGoodsLabel", new Type[1] { typeof(Text) });
			val5.transform.SetParent((Transform)(object)firstRowRT, false);
			Text component = val5.GetComponent<Text>();
			component.font = Plugin.DefaultFont;
			component.fontSize = 20;
			((Graphic)component).color = Color.white;
			component.alignment = (TextAnchor)3;
			component.text = "Desired Goods";
			LayoutElement val6 = val5.AddComponent<LayoutElement>();
			val6.minWidth = 150f;
			val6.preferredWidth = 150f;
			GameObject val7 = new GameObject("IconContainer", new Type[1] { typeof(RectTransform) });
			val7.transform.SetParent((Transform)(object)firstRowRT, false);
			iconContainerRT = val7.GetComponent<RectTransform>();
			VerticalLayoutGroup val8 = val7.AddComponent<VerticalLayoutGroup>();
			((LayoutGroup)val8).childAlignment = (TextAnchor)0;
			((HorizontalOrVerticalLayoutGroup)val8).spacing = 0f;
			((HorizontalOrVerticalLayoutGroup)val8).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val8).childForceExpandHeight = false;
			Button val9 = rowGO.AddComponent<Button>();
			((Selectable)val9).transition = (Transition)0;
			((UnityEvent)val9.onClick).AddListener(new UnityAction(OnRowClicked));
			rowGO.SetActive(false);
		}

		private void Update()
		{
			if (!Plugin.InSettlement() || (Object)(object)rowGO == (Object)null)
			{
				return;
			}
			if (!toggleEnabled)
			{
				rowGO.SetActive(false);
				return;
			}
			if (!IsTradingPostBuilt())
			{
				rowGO.SetActive(false);
				return;
			}
			rowGO.SetActive(true);
			if (!(Time.time < nextUpdateTime))
			{
				nextUpdateTime = Time.time + 1f;
				UpdateDesiredGoods();
			}
		}

		private bool IsTradingPostBuilt()
		{
			IBuildingsService buildingsService = GameMB.BuildingsService;
			Dictionary<int, TradingPost> dictionary = ((buildingsService != null) ? buildingsService.TradingPosts : null);
			if (dictionary == null || dictionary.Values == null)
			{
				return false;
			}
			foreach (TradingPost value in dictionary.Values)
			{
				if (value != null && (((BuildingState)(value.state?)).finished).GetValueOrDefault())
				{
					return true;
				}
			}
			return false;
		}

		private void OnRowClicked()
		{
			TradeRoutesPopupAccess.ToggleFromHUD();
		}

		private void ClearSlots()
		{
			foreach (GameObject slot in slots)
			{
				if ((Object)(object)slot != (Object)null)
				{
					Object.Destroy((Object)(object)slot);
				}
			}
			slots.Clear();
			foreach (RectTransform iconRow in iconRows)
			{
				if ((Object)(object)iconRow != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)iconRow).gameObject);
				}
			}
			iconRows.Clear();
		}

		private RectTransform CreateIconRow()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			GameObject val = new GameObject($"DG_IconRow_{iconRows.Count}", new Type[1] { typeof(RectTransform) });
			val.transform.SetParent((Transform)(object)iconContainerRT, false);
			RectTransform component = val.GetComponent<RectTransform>();
			LayoutElement val2 = val.AddComponent<LayoutElement>();
			val2.minHeight = 24f;
			val2.preferredHeight = 24f;
			HorizontalLayoutGroup val3 = val.AddComponent<HorizontalLayoutGroup>();
			((LayoutGroup)val3).childAlignment = (TextAnchor)3;
			((HorizontalOrVerticalLayoutGroup)val3).spacing = 0f;
			((HorizontalOrVerticalLayoutGroup)val3).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val3).childForceExpandHeight = false;
			iconRows.Add(component);
			return component;
		}

		private void UpdateDesiredGoods()
		{
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_024c: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_0378: Unknown result type (might be due to invalid IL or missing references)
			//IL_037f: Expected O, but got Unknown
			//IL_03b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0402: Unknown result type (might be due to invalid IL or missing references)
			//IL_0411: Unknown result type (might be due to invalid IL or missing references)
			//IL_0434: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0307: Unknown result type (might be due to invalid IL or missing references)
			//IL_0323: Unknown result type (might be due to invalid IL or missing references)
			TradingPost val = GameMB.BuildingsService.TradingPosts?.Values?.FirstOrDefault();
			if ((Object)(object)val == (Object)null || val.state == null || !((BuildingState)val.state).finished)
			{
				ClearSlots();
				return;
			}
			TradeState trade = Serviceable.StateService.Trade;
			if (trade == null)
			{
				ClearSlots();
				return;
			}
			List<TownOfferState> panelLogicOffers = GetPanelLogicOffers();
			if (panelLogicOffers.Count == 0)
			{
				ClearSlots();
				return;
			}
			var list = (from o in panelLogicOffers
				select new
				{
					Offer = o,
					Good = Serviceable.Settings.GetGood(o.good.name)
				} into x
				where (Object)(object)x.Good != (Object)null
				select x).ToList();
			if (list.Count == 0)
			{
				ClearSlots();
				return;
			}
			IStorageService storageService = Serviceable.StorageService;
			Storage val2 = ((storageService != null) ? storageService.GetStorage() : null);
			if ((Object)(object)val2 == (Object)null)
			{
				ClearSlots();
				return;
			}
			ClearSlots();
			RectTransform val3 = CreateIconRow();
			int num = 0;
			foreach (var item in list)
			{
				TownOfferState offer = item.Offer;
				GoodModel good = item.Good;
				int amount = offer.good.amount;
				int amount2 = val2.GetAmount(((Object)good).name);
				bool active = amount2 < amount;
				if (!((Object)(object)Plugin.TemplateGoodSlot == (Object)null))
				{
					if (num >= 10)
					{
						val3 = CreateIconRow();
						num = 0;
					}
					GameObject val4 = Object.Instantiate<GameObject>(((Component)Plugin.TemplateGoodSlot).gameObject, (Transform)(object)val3);
					((Object)val4).name = $"DG_{((Object)good).name}_{amount}_Slot";
					float num2 = 1f / 3f;
					val4.transform.localScale = new Vector3(num2, num2, 1f);
					GoodSlot component = val4.GetComponent<GoodSlot>();
					Good val5 = default(Good);
					val5.name = ((Object)good).name;
					val5.amount = amount;
					Good up = val5;
					component.SetUp(up);
					Transform val6 = val4.transform.Find("Frame");
					if ((Object)(object)val6 != (Object)null)
					{
						Object.Destroy((Object)(object)((Component)val6).gameObject);
					}
					Transform obj = val4.transform.Find("Counter");
					RectTransform val7 = ((obj != null) ? ((Component)obj).GetComponent<RectTransform>() : null);
					if ((Object)(object)val7 != (Object)null)
					{
						val7.anchorMin = new Vector2(0.5f, 0.5f);
						val7.anchorMax = new Vector2(0.5f, 0.5f);
						val7.pivot = new Vector2(0.5f, 0.5f);
						val7.anchoredPosition = new Vector2(-10f, -14f);
						((Transform)val7).localScale = new Vector3(1.8f, 1.8f, 1f);
					}
					RectTransform component2 = ((Component)val4.transform.Find("Icon")).GetComponent<RectTransform>();
					GameObject val8 = new GameObject("Highlight_" + ((Object)good).name, new Type[2]
					{
						typeof(RectTransform),
						typeof(Image)
					});
					val8.transform.SetParent(((Transform)component2).parent, false);
					val8.transform.SetSiblingIndex(0);
					RectTransform component3 = val8.GetComponent<RectTransform>();
					component3.anchorMin = new Vector2(0.5f, 0.5f);
					component3.anchorMax = new Vector2(0.5f, 0.5f);
					component3.pivot = new Vector2(0.5f, 0.5f);
					Vector2 sizeDelta = component2.sizeDelta;
					component3.sizeDelta = sizeDelta * 1.15f;
					component3.anchoredPosition = component2.anchoredPosition;
					Image component4 = val8.GetComponent<Image>();
					component4.sprite = Plugin.HighlightSprite;
					((Graphic)component4).color = Color.white;
					component4.type = (Type)0;
					component4.preserveAspect = false;
					((Graphic)component4).raycastTarget = false;
					val8.SetActive(false);
					val4.AddComponent<HighlightReference>().Highlight = val8;
					LayoutElement val9 = val4.GetComponent<LayoutElement>() ?? val4.AddComponent<LayoutElement>();
					val9.minWidth = 26f;
					val9.preferredWidth = 26f;
					val9.minHeight = 8f;
					val9.preferredHeight = 8f;
					Button component5 = val4.GetComponent<Button>();
					if ((Object)(object)component5 != (Object)null)
					{
						((UnityEventBase)component5.onClick).RemoveAllListeners();
						((Behaviour)component5).enabled = false;
					}
					HighlightReference component6 = val4.GetComponent<HighlightReference>();
					if ((Object)(object)component6 != (Object)null)
					{
						component6.Highlight.SetActive(active);
					}
					slots.Add(val4);
					num++;
				}
			}
			int count = iconRows.Count;
			float num3 = (float)count * 24f + (float)(count - 1) * 0f;
			rowLE.minHeight = num3;
			rowLE.preferredHeight = num3;
		}

		private List<TownOfferState> GetPanelLogicOffers()
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			List<TownOfferState> list = new List<TownOfferState>();
			TradeState trade = Serviceable.StateService.Trade;
			if (trade?.tradeTowns == null)
			{
				return list;
			}
			bool onlyAvailableTradeRoutes = GameMB.StateService.Prefs.onlyAvailableTradeRoutes;
			foreach (TradeTownState tradeTown in trade.tradeTowns)
			{
				List<TownOfferState> offers = tradeTown.offers;
				if (offers == null)
				{
					continue;
				}
				foreach (TownOfferState item in offers)
				{
					if (item != null)
					{
						_ = item.good;
						if (0 == 0 && !string.IsNullOrEmpty(item.good.name) && !item.accpeted && (!onlyAvailableTradeRoutes || GameMB.TradeRoutesService.CanAcceptAnyAmount(item)) && !(item.good.name == "Provisions") && !(item.good.name == "Amber"))
						{
							list.Add(item);
						}
					}
				}
			}
			return list;
		}
	}
	public class NewcomerHUDWidget : MonoBehaviour
	{
		private Image icon;

		private Image vanillaIcon;

		private Text timerText;

		private bool iconBuilt = false;

		private Transform parent;

		private Canvas canvas;

		private GameObject rowGO;

		public void Build(Transform parent, Canvas canvas)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			this.parent = parent;
			this.canvas = canvas;
			rowGO = new GameObject("NewcomerTimerRow", new Type[1] { typeof(RectTransform) });
			rowGO.layer = ((Component)canvas).gameObject.layer;
			rowGO.transform.SetParent(parent, false);
			HorizontalLayoutGroup val = rowGO.AddComponent<HorizontalLayoutGroup>();
			((LayoutGroup)val).childAlignment = (TextAnchor)3;
			((HorizontalOrVerticalLayoutGroup)val).spacing = 4f;
			((HorizontalOrVerticalLayoutGroup)val).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val).childForceExpandHeight = false;
			LayoutElement val2 = rowGO.AddComponent<LayoutElement>();
			val2.preferredWidth = -1f;
			val2.flexibleWidth = 0f;
			GameObject val3 = new GameObject("NewcomerTimerText", new Type[1] { typeof(Text) });
			val3.layer = ((Component)canvas).gameObject.layer;
			val3.transform.SetParent(rowGO.transform, false);
			timerText = val3.GetComponent<Text>();
			timerText.font = Plugin.DefaultFont;
			timerText.fontSize = 20;
			((Graphic)timerText).color = Color.white;
			timerText.alignment = (TextAnchor)3;
		}

		private void Update()
		{
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.InSettlement() || (Object)(object)rowGO == (Object)null)
			{
				return;
			}
			if (!Plugin.ShowNewcomerTimer.Value)
			{
				rowGO.SetActive(false);
				return;
			}
			rowGO.SetActive(true);
			if (!iconBuilt)
			{
				TryBuildIcon();
			}
			if ((Object)(object)icon != (Object)null && (Object)(object)vanillaIcon != (Object)null)
			{
				((Graphic)icon).material = ((Graphic)vanillaIcon).material;
				((Graphic)icon).color = ((Graphic)vanillaIcon).color;
			}
			UpdateTimerText();
		}

		private void TryBuildIcon()
		{
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Expected O, but got Unknown
			Canvas val = Plugin.Instance.FindHUDCanvas();
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Transform val2 = ((Component)val).transform.Find("ControlHUD/NewcomersButton/Button/Icon");
			if (!((Object)(object)val2 == (Object)null))
			{
				vanillaIcon = ((Component)val2).GetComponent<Image>();
				if (!((Object)(object)vanillaIcon == (Object)null))
				{
					GameObject val3 = Object.Instantiate<GameObject>(((Component)vanillaIcon).gameObject, rowGO.transform);
					((Object)val3).name = "NewcomersIconClone";
					val3.transform.SetSiblingIndex(0);
					icon = val3.GetComponent<Image>();
					icon.preserveAspect = true;
					int num = 24;
					RectTransform component = val3.GetComponent<RectTransform>();
					((Transform)component).localScale = Vector3.one;
					component.sizeDelta = new Vector2((float)num, (float)num);
					LayoutElement val4 = val3.GetComponent<LayoutElement>() ?? val3.AddComponent<LayoutElement>();
					val4.preferredWidth = num;
					val4.preferredHeight = num;
					val4.minWidth = num;
					val4.minHeight = num;
					Button val5 = val3.AddComponent<Button>();
					((Selectable)val5).transition = (Transition)0;
					((UnityEvent)val5.onClick).AddListener(new UnityAction(OpenPopup));
					iconBuilt = true;
				}
			}
		}

		private void UpdateTimerText()
		{
			INewcomersService newcomersService = GameMB.NewcomersService;
			BiomeModel biome = Serviceable.Biome;
			if (newcomersService == null || (Object)(object)biome == (Object)null)
			{
				timerText.text = "Newcomers: loading...";
				return;
			}
			if (newcomersService.AreNewcomersWaitning())
			{
				timerText.text = "Newcomers are waiting.";
				return;
			}
			float timeToNextVisit = newcomersService.GetTimeToNextVisit();
			timerText.text = "Newcomers in: " + FormatTime(timeToNextVisit);
		}

		private void OpenPopup()
		{
			INewcomersService newcomersService = GameMB.NewcomersService;
			if (newcomersService == null)
			{
				return;
			}
			List<NewcomersGroup> currentNewcomers = newcomersService.GetCurrentNewcomers();
			if (currentNewcomers == null || currentNewcomers.Count == 0)
			{
				return;
			}
			object gameBlackboardService = GameMB.GameBlackboardService;
			PropertyInfo property = gameBlackboardService.GetType().GetProperty("OnNewcomersPopupRequested");
			if (property == null)
			{
				return;
			}
			object value = property.GetValue(gameBlackboardService);
			if (value != null)
			{
				MethodInfo method = value.GetType().GetMethod("OnNext");
				if (!(method == null))
				{
					method.Invoke(value, new object[1] { currentNewcomers });
				}
			}
		}

		private string FormatTime(float seconds)
		{
			int num = Mathf.FloorToInt(seconds);
			int num2 = num / 60;
			int num3 = num % 60;
			return $"{num2}:{num3:00}";
		}
	}
	[BepInPlugin("TimerHUD", "TimerHUD", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <CaptureGoodSlotTemplate>d__25 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public Plugin <>4__this;

			private GameObject[] <>s__1;

			private int <>s__2;

			private GameObject <go>5__3;

			private GoodSlot <slot>5__4;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <CaptureGoodSlotTemplate>d__25(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>s__1 = null;
				<go>5__3 = null;
				<slot>5__4 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if ((Object)(object)TemplateGoodSlot == (Object)null)
				{
					<>s__1 = Resources.FindObjectsOfTypeAll<GameObject>();
					for (<>s__2 = 0; <>s__2 < <>s__1.Length; <>s__2++)
					{
						<go>5__3 = <>s__1[<>s__2];
						<slot>5__4 = <go>5__3.GetComponent<GoodSlot>();
						if ((Object)(object)<slot>5__4 != (Object)null)
						{
							TemplateGoodSlot = <slot>5__4;
							Log.LogInfo((object)("Captured GoodSlot template: " + ((Object)TemplateGoodSlot).name));
							break;
						}
						<slot>5__4 = null;
						<go>5__3 = null;
					}
					<>s__1 = null;
					<>2__current = null;
					<>1__state = 1;
					return true;
				}
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		public static ConfigEntry<float> HUDScale;

		internal static Plugin Instance;

		internal static ManualLogSource Log;

		internal static Harmony Harmony;

		public static Sprite HighlightSprite;

		public static Font DefaultFont;

		public static ConfigEntry<float> HUDPosX;

		public static ConfigEntry<float> HUDPosY;

		public static ConfigEntry<bool> ShowTradeRoutes;

		public static ConfigEntry<bool> ShowDesiredGoods;

		public static ConfigEntry<bool> ShowTraderTimer;

		public static ConfigEntry<bool> ShowNewcomerTimer;

		public static ConfigEntry<bool> ShowSeasonTimer;

		public static GoodSlot TemplateGoodSlot;

		public const int DefaultFontSize = 20;

		public const int DefaultSubscriptFontSize = 16;

		public const int DefaultPortraitSize = 24;

		public const int DefaultRouteIconSize = 8;

		public const int DefaultNewcomerSize = 24;

		public const float DesiredGoodsTextScaleFactor = 4f;

		public const float DesiredGoodsTextXOffset = -30f;

		public const float DesiredGoodsTextYOffset = 10f;

		private void Awake()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			HighlightSprite = CreateRedHighlightSprite();
			((MonoBehaviour)this).StartCoroutine(CaptureGoodSlotTemplate());
			DefaultFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
			Harmony = new Harmony("TraderTimer.harmony");
			Harmony.PatchAll();
			HUDScale = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "HUDScale", 1f, "Master scale for the entire Timer HUD.");
			HUDPosX = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "HUDPosX", -120f, "Horizontal position. Don't bother trying to edit in API menu.");
			HUDPosY = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "HUDPosY", 60f, "Vertical position. Don't bother trying to edit in API menu.");
			HUDScale.SettingChanged += OnHUDScaleChanged;
			ShowTradeRoutes = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "ShowTradeRoutes", true, (ConfigDescription)null);
			ShowDesiredGoods = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "ShowDesiredGoods", true, (ConfigDescription)null);
			ShowTraderTimer = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "ShowTraderTimer", true, (ConfigDescription)null);
			ShowNewcomerTimer = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "ShowNewcomerTimer", true, (ConfigDescription)null);
			ShowSeasonTimer = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "ShowSeasonTimer", true, (ConfigDescription)null);
			ShowNewcomerTimer.SettingChanged += delegate
			{
				TimerHUDManager traderWidget2 = TimerHUDUtil.TraderWidget;
				if ((Object)(object)traderWidget2 != (Object)null)
				{
					((Component)traderWidget2).gameObject.SetActive(true);
				}
			};
			ShowSeasonTimer.SettingChanged += delegate
			{
				TimerHUDManager traderWidget = TimerHUDUtil.TraderWidget;
				if ((Object)(object)traderWidget != (Object)null)
				{
					((Component)traderWidget).gameObject.SetActive(true);
				}
			};
			((Component)this).gameObject.AddComponent<TimerHUDBootstrap>();
		}

		private void OnHUDScaleChanged(object sender, EventArgs e)
		{
			TimerHUDManager.Instance?.ApplyScale();
		}

		public static Sprite CreateRedHighlightSprite()
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Expected O, but got Unknown
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(1, 1);
			val.SetPixel(0, 0, new Color(1f, 0f, 0f, 0.35f));
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, 1f, 1f), new Vector2(0.5f, 0.5f));
		}

		[IteratorStateMachine(typeof(<CaptureGoodSlotTemplate>d__25))]
		private IEnumerator CaptureGoodSlotTemplate()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <CaptureGoodSlotTemplate>d__25(0)
			{
				<>4__this = this
			};
		}

		internal static bool InSettlement()
		{
			try
			{
				if (GameMB.GameTimeService == null)
				{
					return false;
				}
				if (GameMB.BuildingsService == null)
				{
					return false;
				}
				Canvas[] array = Object.FindObjectsOfType<Canvas>(true);
				foreach (Canvas val in array)
				{
					string text = ((Object)val).name.ToLowerInvariant();
					if (text == "hud" || text.Contains("ingamehud"))
					{
						return true;
					}
				}
				return false;
			}
			catch
			{
				return false;
			}
		}

		public Canvas FindHUDCanvas()
		{
			Canvas[] array = Object.FindObjectsOfType<Canvas>(true);
			foreach (Canvas val in array)
			{
				string text = ((Object)val).name.ToLowerInvariant();
				if (text == "hud" || text.Contains("ingamehud") || text.Contains("hudcanvas"))
				{
					return val;
				}
			}
			Canvas[] array2 = Object.FindObjectsOfType<Canvas>(true);
			foreach (Canvas val2 in array2)
			{
				string text2 = ((Object)val2).name.ToLowerInvariant();
				if (text2.Contains("hud") && !text2.Contains("controlhud"))
				{
					return val2;
				}
			}
			Canvas[] array3 = Object.FindObjectsOfType<Canvas>(true);
			foreach (Canvas val3 in array3)
			{
				string text3 = ((Object)val3).name.ToLowerInvariant();
				if (text3.Contains("controlhud"))
				{
					return val3;
				}
			}
			return null;
		}
	}
	public class TimerHUDBootstrap : MonoBehaviour
	{
		private void Update()
		{
			if ((Object)(object)TimerHUDUtil.TraderWidget == (Object)null || (Object)(object)((Component)TimerHUDUtil.TraderWidget).gameObject == (Object)null)
			{
				TimerHUDUtil.TraderWidget = null;
				TimerHUDUtil.GetOrCreateTraderWidget();
			}
		}
	}
	public static class TimerHUDUtil
	{
		internal static TimerHUDManager TraderWidget;

		public static TimerHUDManager GetOrCreateTraderWidget()
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			if ((Object)(object)TraderWidget != (Object)null && (Object)(object)((Component)TraderWidget).gameObject != (Object)null)
			{
				return TraderWidget;
			}
			TraderWidget = null;
			Canvas val = Plugin.Instance.FindHUDCanvas();
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			GameObject val2 = new GameObject("TimerHUDWidget");
			val2.transform.SetParent(((Component)val).transform, false);
			TraderWidget = val2.AddComponent<TimerHUDManager>();
			TraderWidget.Initialize();
			return TraderWidget;
		}
	}
	public class SeasonTimerHUDWidget : MonoBehaviour
	{
		private Text seasonText;

		private Image icon;

		private Image vanillaIcon;

		private Image background;

		private Image vanillaBackground;

		private bool iconBuilt = false;

		private ICalendarService calendar;

		private GameObject rowGO;

		public void Build(Transform parent, Canvas canvas)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			rowGO = new GameObject("SeasonTimerRow", new Type[1] { typeof(RectTransform) });
			rowGO.layer = ((Component)canvas).gameObject.layer;
			rowGO.transform.SetParent(parent, false);
			HorizontalLayoutGroup val = rowGO.AddComponent<HorizontalLayoutGroup>();
			((LayoutGroup)val).childAlignment = (TextAnchor)3;
			((HorizontalOrVerticalLayoutGroup)val).spacing = 4f;
			((HorizontalOrVerticalLayoutGroup)val).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val).childForceExpandHeight = false;
			LayoutElement val2 = rowGO.AddComponent<LayoutElement>();
			val2.preferredWidth = -1f;
			val2.flexibleWidth = 0f;
		}

		private void Update()
		{
			//IL_00b3: 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)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			if (!Plugin.InSettlement() || (Object)(object)rowGO == (Object)null)
			{
				return;
			}
			if (!Plugin.ShowSeasonTimer.Value)
			{
				rowGO.SetActive(false);
				return;
			}
			rowGO.SetActive(true);
			if (!iconBuilt)
			{
				TryBuildIcon();
			}
			if ((Object)(object)icon != (Object)null && (Object)(object)vanillaIcon != (Object)null)
			{
				((Graphic)icon).material = ((Graphic)vanillaIcon).material;
				((Graphic)icon).color = ((Graphic)vanillaIcon).color;
			}
			if ((Object)(object)background != (Object)null && (Object)(object)vanillaBackground != (Object)null)
			{
				((Graphic)background).material = ((Graphic)vanillaBackground).material;
				((Graphic)background).color = ((Graphic)vanillaBackground).color;
				background.fillAmount = vanillaBackground.fillAmount;
				background.fillMethod = vanillaBackground.fillMethod;
				background.fillOrigin = vanillaBackground.fillOrigin;
				background.fillClockwise = vanillaBackground.fillClockwise;
			}
			if (calendar == null)
			{
				calendar = Serviceable.CalendarService;
			}
			if (calendar == null)
			{
				if ((Object)(object)seasonText != (Object)null)
				{
					seasonText.text = "Season: loading...";
				}
			}
			else
			{
				UpdateSeasonTimer();
			}
		}

		private void TryBuildIcon()
		{
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_0218: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0242: Expected O, but got Unknown
			//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
			Canvas val = Plugin.Instance.FindHUDCanvas();
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Transform val2 = ((Component)val).transform.Find("Calendar/Season");
			if ((Object)(object)val2 == (Object)null)
			{
				return;
			}
			Transform val3 = val2.Find("Icon");
			Transform val4 = val2.Find("Background");
			if (!((Object)(object)val3 == (Object)null) && !((Object)(object)val4 == (Object)null))
			{
				vanillaIcon = ((Component)val3).GetComponent<Image>();
				vanillaBackground = ((Component)val4).GetComponent<Image>();
				if (!((Object)(object)vanillaIcon == (Object)null) && !((Object)(object)vanillaBackground == (Object)null))
				{
					int num = 24;
					GameObject val5 = Object.Instantiate<GameObject>(((Component)vanillaIcon).gameObject, rowGO.transform);
					((Object)val5).name = "SeasonIconClone";
					icon = val5.GetComponent<Image>();
					icon.preserveAspect = true;
					RectTransform component = val5.GetComponent<RectTransform>();
					((Transform)component).localScale = Vector3.one;
					component.sizeDelta = new Vector2((float)num, (float)num);
					LayoutElement val6 = val5.GetComponent<LayoutElement>() ?? val5.AddComponent<LayoutElement>();
					val6.preferredWidth = num;
					val6.preferredHeight = num;
					val6.minWidth = num;
					val6.minHeight = num;
					GameObject val7 = Object.Instantiate<GameObject>(((Component)vanillaBackground).gameObject, val5.transform);
					((Object)val7).name = "SeasonBackgroundClone";
					background = val7.GetComponent<Image>();
					background.preserveAspect = true;
					RectTransform component2 = val7.GetComponent<RectTransform>();
					component2.anchorMin = new Vector2(0.5f, 0.5f);
					component2.anchorMax = new Vector2(0.5f, 0.5f);
					component2.pivot = new Vector2(0.5f, 0.5f);
					component2.anchoredPosition = Vector2.zero;
					((Transform)component2).localScale = Vector3.one;
					component2.sizeDelta = new Vector2((float)num, (float)num);
					GameObject val8 = new GameObject("SeasonTimerText", new Type[1] { typeof(Text) });
					val8.layer = rowGO.layer;
					val8.transform.SetParent(rowGO.transform, false);
					seasonText = val8.GetComponent<Text>();
					seasonText.font = Plugin.DefaultFont;
					seasonText.fontSize = 20;
					((Graphic)seasonText).color = Color.white;
					seasonText.alignment = (TextAnchor)3;
					iconBuilt = true;
				}
			}
		}

		private void UpdateSeasonTimer()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected I4, but got Unknown
			//IL_003c: 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_0044: 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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			float timeTillNextSeasonChange = calendar.GetTimeTillNextSeasonChange();
			string arg = FormatTime(timeTillNextSeasonChange);
			Season season = calendar.Season;
			if (1 == 0)
			{
			}
			Season val = (Season)((int)season switch
			{
				0 => 1, 
				1 => 2, 
				2 => 0, 
				_ => 0, 
			});
			if (1 == 0)
			{
			}
			Season val2 = val;
			seasonText.text = $"{val2} season in: {arg}";
		}

		private string FormatTime(float seconds)
		{
			int num = Mathf.FloorToInt(seconds);
			int num2 = num / 60;
			int num3 = num % 60;
			return $"{num2}:{num3:00}";
		}
	}
	public class TimerHUDManager : MonoBehaviour
	{
		private RectTransform bgRT;

		public static TimerHUDManager Instance;

		private RectTransform routesContainerRT;

		private DesiredGoodsHUDWidget desiredWidget;

		private TradeRoutesHUDWidget routesWidget;

		private NewcomerHUDWidget newcomerWidget;

		private SeasonTimerHUDWidget seasonWidget;

		private bool uiBuilt = false;

		public TraderTimerHUDWidget TraderTimerWidget { get; private set; }

		public void Initialize()
		{
			uiBuilt = false;
		}

		private void Start()
		{
			if (Plugin.InSettlement())
			{
				Canvas val = Plugin.Instance.FindHUDCanvas();
				if ((Object)(object)val != (Object)null && !uiBuilt)
				{
					CreateUI(val);
					uiBuilt = true;
				}
			}
		}

		private void Update()
		{
			if (uiBuilt)
			{
				if ((Object)(object)routesContainerRT != (Object)null)
				{
					((Component)routesContainerRT).gameObject.SetActive(Plugin.ShowTradeRoutes.Value);
				}
				if ((Object)(object)desiredWidget != (Object)null)
				{
					desiredWidget.SetToggleState(Plugin.ShowDesiredGoods.Value);
				}
				if ((Object)(object)TraderTimerWidget != (Object)null)
				{
					TraderTimerWidget.SetToggleState(Plugin.ShowTraderTimer.Value);
				}
			}
		}

		public void ApplyScale()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)bgRT == (Object)null))
			{
				float value = Plugin.HUDScale.Value;
				((Transform)bgRT).localScale = new Vector3(value, value, 1f);
			}
		}

		public void ApplyPosition()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)bgRT == (Object)null))
			{
				bgRT.anchoredPosition = new Vector2(Plugin.HUDPosX.Value, Plugin.HUDPosY.Value);
			}
		}

		private void CreateUI(Canvas parentCanvas)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: 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: Expected O, but got Unknown
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Expected O, but got Unknown
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e5: Expected O, but got Unknown
			//IL_023f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Expected O, but got Unknown
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_0297: Expected O, but got Unknown
			//IL_02e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f0: Expected O, but got Unknown
			//IL_032b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0332: Expected O, but got Unknown
			Instance = this;
			GameObject val = new GameObject("TimerHUD_Background", new Type[1] { typeof(Image) });
			val.layer = ((Component)parentCanvas).gameObject.layer;
			val.transform.SetParent(((Component)parentCanvas).transform, false);
			Image component = val.GetComponent<Image>();
			((Graphic)component).color = new Color(0f, 0f, 0f, 0.55f);
			val.AddComponent<UIDragHandler>();
			bgRT = val.GetComponent<RectTransform>();
			bgRT.anchorMin = new Vector2(0.8f, 0f);
			bgRT.anchorMax = new Vector2(0.8f, 0f);
			bgRT.pivot = new Vector2(0f, 0f);
			ApplyScale();
			ApplyPosition();
			LayoutElement val2 = val.AddComponent<LayoutElement>();
			val2.minWidth = 260f;
			ContentSizeFitter val3 = val.AddComponent<ContentSizeFitter>();
			val3.horizontalFit = (FitMode)2;
			val3.verticalFit = (FitMode)2;
			VerticalLayoutGroup val4 = val.AddComponent<VerticalLayoutGroup>();
			((LayoutGroup)val4).childAlignment = (TextAnchor)6;
			((LayoutGroup)val4).padding = new RectOffset(2, 2, 0, 0);
			((HorizontalOrVerticalLayoutGroup)val4).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val4).childForceExpandHeight = false;
			GameObject val5 = new GameObject("TradeRoutesContainer", new Type[1] { typeof(RectTransform) });
			val5.layer = ((Component)parentCanvas).gameObject.layer;
			val5.transform.SetParent(val.transform, false);
			routesContainerRT = val5.GetComponent<RectTransform>();
			VerticalLayoutGroup val6 = val5.AddComponent<VerticalLayoutGroup>();
			((LayoutGroup)val6).childAlignment = (TextAnchor)0;
			((HorizontalOrVerticalLayoutGroup)val6).spacing = 2f;
			((HorizontalOrVerticalLayoutGroup)val6).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val6).childForceExpandHeight = false;
			GameObject val7 = new GameObject("TradeRoutesHUDWidget", new Type[1] { typeof(TradeRoutesHUDWidget) });
			val7.transform.SetParent((Transform)(object)routesContainerRT, false);
			routesWidget = val7.GetComponent<TradeRoutesHUDWidget>();
			routesWidget.Initialize((Transform)(object)routesContainerRT);
			GameObject val8 = new GameObject("DesiredGoodsWidget", new Type[2]
			{
				typeof(RectTransform),
				typeof(DesiredGoodsHUDWidget)
			});
			val8.transform.SetParent(val.transform, false);
			desiredWidget = val8.GetComponent<DesiredGoodsHUDWidget>();
			desiredWidget.Initialize();
			desiredWidget.SetToggleState(Plugin.ShowDesiredGoods.Value);
			GameObject val9 = new GameObject("TraderTimerHUDWidget");
			val9.transform.SetParent(val.transform, false);
			TraderTimerWidget = val9.AddComponent<TraderTimerHUDWidget>();
			TraderTimerWidget.Build(val.transform, parentCanvas);
			TraderTimerWidget.SetToggleState(Plugin.ShowTraderTimer.Value);
			GameObject val10 = new GameObject("NewcomerHUDWidget");
			val10.transform.SetParent(val.transform, false);
			newcomerWidget = val10.AddComponent<NewcomerHUDWidget>();
			newcomerWidget.Build(val.transform, parentCanvas);
			GameObject val11 = new GameObject("SeasonTimerHUDWidget");
			val11.transform.SetParent(val.transform, false);
			seasonWidget = val11.AddComponent<SeasonTimerHUDWidget>();
			seasonWidget.Build(val.transform, parentCanvas);
		}
	}
	public class TradeRoutesHUDWidget : MonoBehaviour
	{
		private class RouteRow
		{
			public RouteState route;

			public GameObject root;

			public Text townText;

			public Image amberIcon;

			public Text amberAmount;

			public Text timeText;
		}

		private readonly List<RouteRow> rows = new List<RouteRow>();

		private float nextUpdateTime = 0f;

		public Transform routesContainer;

		public void Initialize(Transform routesContainer)
		{
			this.routesContainer = routesContainer;
		}

		private void ClearRows()
		{
			foreach (RouteRow row in rows)
			{
				if ((Object)(object)row.root != (Object)null)
				{
					Object.Destroy((Object)(object)row.root);
				}
			}
			rows.Clear();
		}

		private void AddRow()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Expected O, but got Unknown
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Expected O, but got Unknown
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0242: Expected O, but got Unknown
			//IL_02e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ea: Expected O, but got Unknown
			//IL_0355: Unknown result type (might be due to invalid IL or missing references)
			//IL_039b: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a2: Expected O, but got Unknown
			//IL_040d: Unknown result type (might be due to invalid IL or missing references)
			Canvas val = Plugin.Instance.FindHUDCanvas();
			if (!((Object)(object)val == (Object)null))
			{
				int count = rows.Count;
				GameObject val2 = new GameObject($"TradeRouteRow_{count}", new Type[1] { typeof(RectTransform) });
				val2.layer = ((Component)val).gameObject.layer;
				val2.transform.SetParent(routesContainer, false);
				((Graphic)val2.AddComponent<Image>()).color = new Color(0f, 0f, 0f, 0f);
				((UnityEvent)val2.AddComponent<Button>().onClick).AddListener(new UnityAction(OpenRoutePanel));
				val2.AddComponent<RectMask2D>();
				RectTransform component = val2.GetComponent<RectTransform>();
				component.anchorMin = new Vector2(0f, 1f);
				component.anchorMax = new Vector2(0f, 1f);
				component.pivot = new Vector2(0f, 1f);
				LayoutElement val3 = val2.AddComponent<LayoutElement>();
				val3.minHeight = 24f;
				val3.preferredHeight = 24f;
				HorizontalLayoutGroup val4 = val2.AddComponent<HorizontalLayoutGroup>();
				((LayoutGroup)val4).childAlignment = (TextAnchor)3;
				((HorizontalOrVerticalLayoutGroup)val4).spacing = 0f;
				((HorizontalOrVerticalLayoutGroup)val4).childForceExpandWidth = false;
				((HorizontalOrVerticalLayoutGroup)val4).childControlWidth = true;
				RouteRow routeRow = new RouteRow();
				routeRow.root = val2;
				GameObject val5 = new GameObject("TownName", new Type[1] { typeof(Text) });
				val5.layer = ((Component)val).gameObject.layer;
				val5.transform.SetParent(val2.transform, false);
				routeRow.townText = val5.GetComponent<Text>();
				routeRow.townText.font = Plugin.DefaultFont;
				routeRow.townText.fontSize = 20;
				routeRow.townText.alignment = (TextAnchor)3;
				((Graphic)routeRow.townText).color = Color.white;
				LayoutElement val6 = val5.AddComponent<LayoutElement>();
				val6.minWidth = 150f;
				val6.preferredWidth = 150f;
				GameObject val7 = new GameObject("AmberIcon", new Type[1] { typeof(Image) });
				val7.layer = ((Component)val).gameObject.layer;
				val7.transform.SetParent(val2.transform, false);
				routeRow.amberIcon = val7.GetComponent<Image>();
				routeRow.amberIcon.preserveAspect = true;
				float num = 21.333334f;
				RectTransform component2 = ((Component)routeRow.amberIcon).GetComponent<RectTransform>();
				LayoutElement val8 = val7.AddComponent<LayoutElement>();
				val8.preferredWidth = num;
				val8.minWidth = num;
				val8.preferredHeight = num;
				val8.minHeight = num;
				GameObject val9 = new GameObject("AmberAmount", new Type[1] { typeof(Text) });
				val9.layer = ((Component)val).gameObject.layer;
				val9.transform.SetParent(val2.transform, false);
				routeRow.amberAmount = val9.GetComponent<Text>();
				routeRow.amberAmount.font = Plugin.DefaultFont;
				routeRow.amberAmount.fontSize = 20;
				routeRow.amberAmount.alignment = (TextAnchor)3;
				((Graphic)routeRow.amberAmount).color = Color.white;
				LayoutElement val10 = val9.AddComponent<LayoutElement>();
				val10.preferredWidth = 30f;
				val10.minWidth = 30f;
				GameObject val11 = new GameObject("TimeRemaining", new Type[1] { typeof(Text) });
				val11.layer = ((Component)val).gameObject.layer;
				val11.transform.SetParent(val2.transform, false);
				routeRow.timeText = val11.GetComponent<Text>();
				routeRow.timeText.font = Plugin.DefaultFont;
				routeRow.timeText.fontSize = 20;
				routeRow.timeText.alignment = (TextAnchor)5;
				((Graphic)routeRow.timeText).color = Color.white;
				LayoutElement val12 = val11.AddComponent<LayoutElement>();
				val12.preferredWidth = 40f;
				val12.minWidth = 40f;
				rows.Add(routeRow);
			}
		}

		private void OpenRoutePanel()
		{
			TradeRoutesPopupAccess.ToggleFromHUD();
		}

		private void UpdateRowVisuals()
		{
			TradeState trade = Serviceable.StateService.Trade;
			List<RouteState> routes = trade.routes;
			for (int i = 0; i < rows.Count; i++)
			{
				if (rows[i].root.activeSelf)
				{
					RouteState route = routes[i];
					rows[i].route = route;
					RouteRow routeRow = rows[i];
					TradeTownState val = Serviceable.StateService.Trade.tradeTowns.Find((TradeTownState t) => t.id == route.townId);
					routeRow.townText.text = ((val != null) ? TextsGatekeeper.GetText(val.townName) : route.townName);
					GoodModel good = Serviceable.Settings.GetGood(route.price.name);
					routeRow.amberIcon.sprite = good?.icon;
					routeRow.amberAmount.text = $"{route.price.amount}";
					float seconds = Mathf.Max(0f, (1f - route.progress) * route.travelTime);
					routeRow.timeText.text = FormatTime(seconds);
				}
			}
		}

		private string FormatTime(float seconds)
		{
			int num = Mathf.FloorToInt(seconds);
			int num2 = num / 60;
			int num3 = num % 60;
			return $"{num2}:{num3:00}";
		}

		private void Update()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			if (!Plugin.InSettlement() || Time.time < nextUpdateTime)
			{
				return;
			}
			nextUpdateTime = Time.time + 1f;
			TradeRoutesService val = (TradeRoutesService)Serviceable.TradeRoutesService;
			if (val == null)
			{
				ClearRows();
				return;
			}
			TradeState trade = Serviceable.StateService.Trade;
			if (trade == null)
			{
				ClearRows();
				return;
			}
			List<RouteState> routes = trade.routes;
			int num = Mathf.Min(routes.Count, 9);
			while (rows.Count < num)
			{
				AddRow();
			}
			for (int i = 0; i < rows.Count; i++)
			{
				rows[i].root.SetActive(i < num);
			}
			UpdateRowVisuals();
		}
	}
	[HarmonyPatch(typeof(TradeRoutesPopupButton), "Start")]
	public static class TradeRoutesPopupButton_Start_Patch
	{
		public static TradeRoutesPopupButton LastInstance;

		private static void Postfix(TradeRoutesPopupButton __instance)
		{
			LastInstance = __instance;
		}
	}
	public static class TradeRoutesPopupAccess
	{
		private static void EnsureButtonInstance()
		{
			if ((Object)(object)TradeRoutesPopupButton_Start_Patch.LastInstance != (Object)null)
			{
				return;
			}
			Component[] array = Object.FindObjectsOfType<Component>(true);
			foreach (Component val in array)
			{
				TradeRoutesPopupButton val2 = (TradeRoutesPopupButton)(object)((val is TradeRoutesPopupButton) ? val : null);
				if (val2 != null)
				{
					TradeRoutesPopupButton_Start_Patch.LastInstance = val2;
					break;
				}
			}
		}

		public static void ToggleFromHUD()
		{
			EnsureButtonInstance();
			TradeRoutesPopupButton lastInstance = TradeRoutesPopupButton_Start_Patch.LastInstance;
			if ((Object)(object)lastInstance == (Object)null)
			{
				return;
			}
			Type typeFromHandle = typeof(TradeRoutesPopupButton);
			FieldInfo fieldInfo = AccessTools.Field(typeFromHandle, "isShown");
			if (fieldInfo == null)
			{
				return;
			}
			bool flag;
			try
			{
				flag = (bool)fieldInfo.GetValue(lastInstance);
			}
			catch
			{
				return;
			}
			string text = (flag ? "HidePopup" : "ShowPopup");
			MethodInfo methodInfo = AccessTools.Method(typeFromHandle, text, (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				return;
			}
			try
			{
				methodInfo.Invoke(lastInstance, null);
			}
			catch
			{
			}
		}
	}
	public class TraderTimerHUDWidget : MonoBehaviour
	{
		private Image portrait;

		private Text timerText;

		private TradeService cachedTradeService;

		private ICalendarService cachedCalendarService;

		public TraderVisitState visit;

		public bool traderHere;

		public float departureTime = -1f;

		private GameObject rowGO;

		private static bool portraitToggleOpen;

		private bool toggleEnabled = true;

		public void SetToggleState(bool enabled)
		{
			toggleEnabled = enabled;
			if (!enabled && (Object)(object)rowGO != (Object)null)
			{
				rowGO.SetActive(false);
			}
		}

		public void Build(Transform parent, Canvas canvas)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Expected O, but got Unknown
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Expected O, but got Unknown
			//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
			rowGO = new GameObject("TraderTimerRow", new Type[1] { typeof(RectTransform) });
			rowGO.layer = ((Component)canvas).gameObject.layer;
			rowGO.transform.SetParent(parent, false);
			HorizontalLayoutGroup val = rowGO.AddComponent<HorizontalLayoutGroup>();
			((LayoutGroup)val).childAlignment = (TextAnchor)3;
			((HorizontalOrVerticalLayoutGroup)val).spacing = 4f;
			((HorizontalOrVerticalLayoutGroup)val).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)val).childForceExpandHeight = false;
			GameObject val2 = new GameObject("TimerHUDPortrait", new Type[1] { typeof(Image) });
			val2.layer = ((Component)canvas).gameObject.layer;
			val2.transform.SetParent(rowGO.transform, false);
			portrait = val2.GetComponent<Image>();
			portrait.preserveAspect = true;
			Button val3 = val2.AddComponent<Button>();
			((UnityEvent)val3.onClick).AddListener(new UnityAction(OnPortraitClicked));
			RectTransform component = val2.GetComponent<RectTransform>();
			float num = 1f / canvas.scaleFactor;
			((Transform)component).localScale = new Vector3(num, num, 1f);
			component.sizeDelta = new Vector2(24f, 24f);
			LayoutElement val4 = val2.AddComponent<LayoutElement>();
			val4.preferredWidth = 24f;
			val4.minWidth = 24f;
			val4.preferredHeight = 24f;
			val4.minHeight = 24f;
			GameObject val5 = new GameObject("TimerHUDText", new Type[1] { typeof(Text) });
			val5.layer = ((Component)canvas).gameObject.layer;
			val5.transform.SetParent(rowGO.transform, false);
			timerText = val5.GetComponent<Text>();
			timerText.font = Plugin.DefaultFont;
			timerText.fontSize = 20;
			((Graphic)timerText).color = Color.white;
			timerText.alignment = (TextAnchor)3;
			rowGO.SetActive(false);
		}

		private void Update()
		{
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			if (!Plugin.InSettlement() || (Object)(object)rowGO == (Object)null)
			{
				return;
			}
			if (!toggleEnabled)
			{
				rowGO.SetActive(false);
				return;
			}
			bool flag = IsTradingPostBuilt();
			rowGO.SetActive(flag);
			if (flag)
			{
				if (cachedTradeService == null)
				{
					cachedTradeService = (TradeService)Serviceable.TradeService;
				}
				if (cachedCalendarService == null)
				{
					cachedCalendarService = Serviceable.CalendarService;
				}
				UpdateTimerText();
			}
		}

		private bool IsTradingPostBuilt()
		{
			IBuildingsService buildingsService = GameMB.BuildingsService;
			Dictionary<int, TradingPost> dictionary = ((buildingsService != null) ? buildingsService.TradingPosts : null);
			if (dictionary == null || dictionary.Values == null)
			{
				return false;
			}
			foreach (TradingPost value in dictionary.Values)
			{
				if (value != null && (((BuildingState)(value.state?)).finished).GetValueOrDefault())
				{
					return true;
				}
			}
			return false;
		}

		private void UpdateTimerText()
		{
			TradeService val = cachedTradeService;
			if (val == null)
			{
				timerText.text = "Trader: loading…";
				return;
			}
			TraderVisitState currentMainVisit = val.GetCurrentMainVisit();
			float gameTime = GameMB.GameTime;
			if (currentMainVisit == null)
			{
				traderHere = false;
				visit = null;
				departureTime = -1f;
				ClearPortrait();
				timerText.text = "Trader: not here";
				return;
			}
			SetPortraitFromVisit(currentMainVisit);
			float timeLeftTo = val.GetTimeLeftTo(currentMainVisit);
			if (!traderHere && timeLeftTo <= 0f)
			{
				traderHere = true;
				visit = currentMainVisit;
				TraderModel trader = val.GetTrader(currentMainVisit);
				float stayingTime = trader.stayingTime;
				float arrivalTime = currentMainVisit.arrivalTime;
				departureTime = arrivalTime + stayingTime;
			}
			if (traderHere && gameTime > departureTime)
			{
				traderHere = false;
				visit = null;
				departureTime = -1f;
				timerText.text = "Trader: not here";
				ClearPortrait();
			}
			else if (!traderHere)
			{
				timerText.text = "Trader arrives in: " + FormatTime(timeLeftTo);
			}
			else
			{
				float num = departureTime - gameTime;
				if (num < 0f)
				{
					num = 0f;
				}
				timerText.text = "Trader departs in: " + FormatTime(num);
			}
		}

		private string FormatTime(float seconds)
		{
			int num = Mathf.FloorToInt(seconds);
			int num2 = num / 60;
			int num3 = num % 60;
			return $"{num2}:{num3:00}";
		}

		public void SetPortraitFromVisit(TraderVisitState visit)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)portrait == (Object)null) && visit != null)
			{
				TradeService val = (TradeService)(((object)cachedTradeService) ?? ((object)(TradeService)Serviceable.TradeService));
				TraderModel trader = val.GetTrader(visit);
				if ((Object)(object)trader != (Object)null)
				{
					portrait.sprite = trader.icon;
				}
			}
		}

		public void ClearPortrait()
		{
			if ((Object)(object)portrait != (Object)null)
			{
				portrait.sprite = null;
			}
		}

		private void OnPortraitClicked()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			TradeService val = (TradeService)(((object)cachedTradeService) ?? ((object)(TradeService)Serviceable.TradeService));
			TraderVisitState val2 = (traderHere ? visit : val.GetCurrentMainVisit());
			TraderPanel instance = TraderPanel.Instance;
			if (!((Object)(object)instance == (Object)null) && val2 != null)
			{
				if (portraitToggleOpen)
				{
					((Popup)instance).Hide();
					portraitToggleOpen = false;
				}
				else
				{
					instance.Show(val2, true);
					portraitToggleOpen = true;
				}
			}
		}
	}
	public class UIDragHandler : MonoBehaviour, IBeginDragHandler, IEventSystemHandler, IDragHandler
	{
		private RectTransform rect;

		private Vector2 offset;

		private void Awake()
		{
			rect = ((Component)this).GetComponent<RectTransform>();
		}

		public void OnBeginDrag(PointerEventData eventData)
		{
			//IL_0012: 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_0036: 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)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			Transform parent = ((Transform)rect).parent;
			RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)(object)((parent is RectTransform) ? parent : null), eventData.position, eventData.pressEventCamera, ref offset);
			offset = rect.anchoredPosition - offset;
		}

		public void OnDrag(PointerEventData eventData)
		{
			//IL_0012: 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_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_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			Transform parent = ((Transform)rect).parent;
			Vector2 val = default(Vector2);
			if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)(object)((parent is RectTransform) ? parent : null), eventData.position, eventData.pressEventCamera, ref val))
			{
				rect.anchoredPosition = val + offset;
				Plugin.HUDPosX.Value = Mathf.RoundToInt(rect.anchoredPosition.x);
				Plugin.HUDPosY.Value = Mathf.RoundToInt(rect.anchoredPosition.y);
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "TimerHUD";

		public const string PLUGIN_NAME = "TimerHUD";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}