Decompiled source of TraderTimer v1.0.1

TraderTimer.dll

Decompiled 2 weeks ago
using System;
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.Model.State;
using Eremite.Model.Trade;
using Eremite.Services;
using Eremite.Trade.View;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.EventSystems;
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.3.0")]
	public class Plugin : BaseUnityPlugin
	{
		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, (ConfigDescription)null);
			HUDPosY = CustomConfig.Bind<float>("HUD", "HUDPosY", 40f, (ConfigDescription)null);
			FontSize = CustomConfig.Bind<int>("HUD", "FontSize", 26, (ConfigDescription)null);
			PortraitSize = CustomConfig.Bind<int>("HUD", "PortraitSize", 32, (ConfigDescription)null);
			Harmony.PatchAll();
		}

		internal static bool InSettlement()
		{
			return GameMB.GameServices != null && GameMB.GameTimeService != null && GameMB.BuildingsService != null && Serviceable.TradeService != null && Serviceable.CalendarService != null;
		}

		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;

		private bool isVisible = true;

		public void Initialize()
		{
			CreateUI();
			SetVisible(v: true);
		}

		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_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Expected O, but got Unknown
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Expected O, but got Unknown
			//IL_020d: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0266: Expected O, but got Unknown
			//IL_02d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_030a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_0338: 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;
				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;
				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 = false;
				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;
				GameObject val7 = new GameObject("TraderHUDText", new Type[1] { typeof(Text) });
				val7.layer = ((Component)val).gameObject.layer;
				val7.transform.SetParent(val2.transform, false);
				text = val7.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);
			}
		}

		private void SetVisible(bool v)
		{
			if (!((Object)(object)bgRT == (Object)null) && isVisible != v)
			{
				isVisible = v;
				((Component)bgRT).gameObject.SetActive(v);
			}
		}

		private void Update()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Invalid comparison between Unknown and I4
			if (!Plugin.InSettlement())
			{
				SetVisible(v: false);
				return;
			}
			SetVisible(v: true);
			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
			{
				float num = departureTime - gameTime;
				if (num < 0f)
				{
					num = 0f;
				}
				text = "Trader leaves in: " + FormatTime(num);
			}
			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(TradingPost), "OnFinished")]
	public static class TraderHUD_TradingPostFinished_Patch
	{
		private static void Postfix(TradingPost __instance)
		{
			TraderHUDUtil.GetOrCreateWidget();
		}
	}
	[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)
		{
		}
	}
}