Decompiled source of TraderTimer v1.0.0

TraderTimer.dll

Decompiled 20 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Eremite;
using Eremite.Buildings;
using Eremite.Buildings.UI;
using Eremite.Buildings.UI.Trade;
using Eremite.Model.State;
using Eremite.Model.Trade;
using Eremite.Services;
using Eremite.Trade.View;
using Eremite.View.Popups;
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("TraderTimer")]
[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("TraderTimer")]
[assembly: AssemblyTitle("TraderTimer")]
[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 TraderTimer
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "TraderTimer";

		public const string PLUGIN_NAME = "TraderTimer";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace TraderHUD
{
	[BepInPlugin("TraderTimer.plugin", "Trader HUD Timer", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <DelayedHUDInit>d__8 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public Plugin <>4__this;

			private Canvas <found>5__1;

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

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

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

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<found>5__1 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<found>5__1 = null;
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if ((Object)(object)<found>5__1 == (Object)null)
				{
					<found>5__1 = <>4__this.FindHUDCanvas();
					<>2__current = null;
					<>1__state = 1;
					return true;
				}
				TraderHUDUtil.GetOrCreateWidget();
				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();
			}
		}

		internal static Harmony Harmony;

		public static Plugin Instance;

		internal ConfigFile CustomConfig;

		public static ConfigEntry<float> HUDPosX;

		public static ConfigEntry<float> HUDPosY;

		public static ConfigEntry<int> FontSize;

		public static ConfigEntry<int> PortraitSize;

		private void Awake()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			Instance = this;
			Harmony = new Harmony("TraderTimer.harmony");
			CustomConfig = new ConfigFile(Paths.ConfigPath + "/TraderTimer.cfg", true);
			HUDPosX = CustomConfig.Bind<float>("HUD", "HUDPosX", 0f, "HUD X position");
			HUDPosY = CustomConfig.Bind<float>("HUD", "HUDPosY", 40f, "HUD Y position");
			FontSize = CustomConfig.Bind<int>("HUD", "FontSize", 26, "Font size");
			PortraitSize = CustomConfig.Bind<int>("HUD", "PortraitSize", 32, "Portrait size");
			((MonoBehaviour)this).StartCoroutine(DelayedHUDInit());
			Harmony.PatchAll();
		}

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

		public Canvas FindHUDCanvas()
		{
			Canvas[] array = Object.FindObjectsOfType<Canvas>(true);
			Canvas[] array2 = array;
			foreach (Canvas val in array2)
			{
				string text = ((Object)val).name.ToLowerInvariant();
				if (text == "hud" || text.Contains("hudcanvas") || text.Contains("ingamehud"))
				{
					return val;
				}
			}
			Canvas[] array3 = array;
			foreach (Canvas val2 in array3)
			{
				string text2 = ((Object)val2).name.ToLowerInvariant();
				if (text2.Contains("hud") && !text2.Contains("controlhud"))
				{
					return val2;
				}
			}
			Canvas[] array4 = array;
			foreach (Canvas val3 in array4)
			{
				if (((Object)val3).name.ToLowerInvariant().Contains("controlhud"))
				{
					return val3;
				}
			}
			return null;
		}
	}
	public static class TraderHUDUtil
	{
		internal static TraderHUDWidget Widget;

		public static TraderHUDWidget GetOrCreateWidget()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			if ((Object)(object)Widget != (Object)null)
			{
				return Widget;
			}
			GameObject val = new GameObject("TraderHUDWidget");
			Object.DontDestroyOnLoad((Object)(object)val);
			Widget = val.AddComponent<TraderHUDWidget>();
			Widget.Initialize();
			return Widget;
		}
	}
	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_0067: 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 = rect.anchoredPosition.x;
				Plugin.HUDPosY.Value = rect.anchoredPosition.y;
				Plugin.Instance.CustomConfig.Save();
			}
		}
	}
	public class TraderHUDWidget : MonoBehaviour
	{
		internal Image portrait;

		private Text text;

		private RectTransform bgRT;

		public TraderVisitState visit;

		public bool traderHere;

		public float departureTime = -1f;

		public void Initialize()
		{
			CreateUI();
		}

		private void CreateUI()
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected O, but got Unknown
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: 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_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Expected O, but got Unknown
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Expected O, but got Unknown
			//IL_0226: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_028e: Expected O, but got Unknown
			//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ae: Expected O, but got Unknown
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_0352: Unknown result type (might be due to invalid IL or missing references)
			//IL_0369: Unknown result type (might be due to invalid IL or missing references)
			//IL_0380: Unknown result type (might be due to invalid IL or missing references)
			//IL_0397: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ae: Unknown result type (might be due to invalid IL or missing references)
			Canvas val = Plugin.Instance.FindHUDCanvas();
			if (!((Object)(object)val == (Object)null))
			{
				GameObject val2 = new GameObject("TraderHUD_Background", new Type[1] { typeof(Image) });
				val2.layer = ((Component)val).gameObject.layer;
				val2.transform.SetParent(((Component)val).transform, false);
				val2.AddComponent<UIDragHandler>();
				Image component = val2.GetComponent<Image>();
				((Graphic)component).color = new Color(0f, 0f, 0f, 0.55f);
				bgRT = val2.GetComponent<RectTransform>();
				bgRT.anchorMin = new Vector2(0.8f, 0f);
				bgRT.anchorMax = new Vector2(0.8f, 0f);
				bgRT.pivot = new Vector2(0f, 0f);
				((Transform)bgRT).localScale = Vector3.one;
				((Transform)bgRT).localRotation = Quaternion.identity;
				bgRT.anchoredPosition = new Vector2(Plugin.HUDPosX.Value, Plugin.HUDPosY.Value);
				bgRT.sizeDelta = new Vector2(260f, 40f);
				ContentSizeFitter val3 = val2.AddComponent<ContentSizeFitter>();
				val3.horizontalFit = (FitMode)2;
				val3.verticalFit = (FitMode)0;
				HorizontalLayoutGroup val4 = val2.AddComponent<HorizontalLayoutGroup>();
				((LayoutGroup)val4).childAlignment = (TextAnchor)3;
				((LayoutGroup)val4).padding = new RectOffset(2, 2, 2, 2);
				((HorizontalOrVerticalLayoutGroup)val4).spacing = 2f;
				GameObject val5 = new GameObject("TraderHUDPortrait", new Type[1] { typeof(Image) });
				val5.layer = ((Component)val).gameObject.layer;
				val5.transform.SetParent(val2.transform, false);
				portrait = val5.GetComponent<Image>();
				((Graphic)portrait).raycastTarget = true;
				portrait.preserveAspect = true;
				RectTransform component2 = ((Component)portrait).GetComponent<RectTransform>();
				component2.sizeDelta = new Vector2((float)Plugin.PortraitSize.Value, (float)Plugin.PortraitSize.Value);
				LayoutElement val6 = val5.AddComponent<LayoutElement>();
				val6.preferredWidth = Plugin.PortraitSize.Value;
				val6.minWidth = Plugin.PortraitSize.Value;
				val6.flexibleWidth = 0f;
				Button val7 = val5.AddComponent<Button>();
				((UnityEvent)val7.onClick).AddListener(new UnityAction(OnPortraitClicked));
				GameObject val8 = new GameObject("TraderHUDText", new Type[1] { typeof(Text) });
				val8.layer = ((Component)val).gameObject.layer;
				val8.transform.SetParent(val2.transform, false);
				text = val8.GetComponent<Text>();
				text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
				text.fontSize = Plugin.FontSize.Value;
				text.alignment = (TextAnchor)3;
				((Graphic)text).color = Color.white;
				((Graphic)text).raycastTarget = false;
				RectTransform component3 = ((Component)text).GetComponent<RectTransform>();
				component3.anchorMin = new Vector2(0f, 0f);
				component3.anchorMax = new Vector2(1f, 1f);
				component3.pivot = new Vector2(0f, 0.5f);
				component3.offsetMin = new Vector2(0f, 0f);
				component3.offsetMax = new Vector2(0f, 0f);
				text.text = "HUD READY";
			}
		}

		private void OnPortraitClicked()
		{
			if ((Object)(object)SimplePanel.Instance != (Object)null && ((PreviewPanel)SimplePanel.Instance).IsActive())
			{
				((PreviewPanel)SimplePanel.Instance).Hide();
				return;
			}
			if ((Object)(object)TraderPanel.Instance != (Object)null && ((Popup)TraderPanel.Instance).IsShown())
			{
				((Popup)TraderPanel.Instance).Hide();
				return;
			}
			IBuildingsService buildingsService = GameMB.BuildingsService;
			Dictionary<int, Building> dictionary = ((buildingsService != null) ? buildingsService.Buildings : null);
			if (dictionary == null)
			{
				return;
			}
			foreach (KeyValuePair<int, Building> item in dictionary)
			{
				Building value = item.Value;
				TradingPost val = (TradingPost)(object)((value is TradingPost) ? value : null);
				if (val != null)
				{
					GameMB.GameInputService.Pick((IMapObject)(object)val);
					break;
				}
			}
		}

		private void Update()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Invalid comparison between Unknown and I4
			if ((Object)(object)this.text == (Object)null)
			{
				return;
			}
			float gameTime = GameMB.GameTime;
			TradeService val = (TradeService)Serviceable.TradeService;
			TraderVisitState currentMainVisit = val.GetCurrentMainVisit();
			if (!traderHere && currentMainVisit != null)
			{
				SetPortraitFromVisit(currentMainVisit);
			}
			string text;
			if (!traderHere)
			{
				if (currentMainVisit != null)
				{
					float timeLeftTo = val.GetTimeLeftTo(currentMainVisit);
					if ((int)Serviceable.CalendarService.Season == 2)
					{
						float timeTillNextSeasonChange = Serviceable.CalendarService.GetTimeTillNextSeasonChange();
						text = "Trader arrives in: " + FormatTime(timeLeftTo) + " + " + FormatTime(timeTillNextSeasonChange);
					}
					else
					{
						text = "Trader arrives in: " + FormatTime(timeLeftTo);
					}
				}
				else
				{
					text = "Trader: not here";
					if ((Object)(object)portrait != (Object)null)
					{
						portrait.sprite = null;
					}
				}
			}
			else if (departureTime > 0f)
			{
				float num = departureTime - gameTime;
				if (num < 0f)
				{
					num = 0f;
				}
				text = "Trader leaves in: " + FormatTime(num);
			}
			else
			{
				text = "Trader: here (no timing data)";
			}
			this.text.text = text;
		}

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

		internal void SetPortraitFromVisit(TraderVisitState visit)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			if (!((Object)(object)portrait == (Object)null) && visit != null)
			{
				TradeService val = (TradeService)Serviceable.TradeService;
				TraderModel trader = val.GetTrader(visit);
				if ((Object)(object)trader != (Object)null)
				{
					portrait.sprite = trader.icon;
				}
			}
		}
	}
	[HarmonyPatch(typeof(TraderIndicator), "Show")]
	public static class TraderHUD_Show_Patch
	{
		private static void Postfix(TraderIndicator __instance)
		{
			TraderHUDWidget orCreateWidget = TraderHUDUtil.GetOrCreateWidget();
			orCreateWidget.traderHere = true;
			orCreateWidget.visit = __instance.visit;
			float stayingTime = __instance.trader.stayingTime;
			float arrivalTime = orCreateWidget.visit.arrivalTime;
			orCreateWidget.departureTime = arrivalTime + stayingTime;
			orCreateWidget.SetPortraitFromVisit(__instance.visit);
		}
	}
	[HarmonyPatch(typeof(TraderIndicator), "Hide")]
	public static class TraderHUD_Hide_Patch
	{
		private static void Postfix(TraderIndicator __instance)
		{
			TraderHUDWidget orCreateWidget = TraderHUDUtil.GetOrCreateWidget();
			orCreateWidget.traderHere = false;
			orCreateWidget.visit = null;
			orCreateWidget.departureTime = -1f;
			if ((Object)(object)orCreateWidget.portrait != (Object)null)
			{
				orCreateWidget.portrait.sprite = null;
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}