Decompiled source of Vyrheim v2.3.4

files/plugins/Oryxen.Valheim.UI.DisplayDayTime.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Display Day & Time in HUD")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Oryxen")]
[assembly: AssemblyProduct("Valheim")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e12e7c61-76d0-4cf3-8bb3-7e8abb19d120")]
[assembly: AssemblyFileVersion("1.1.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.1.2.0")]
namespace Oryxen.Valheim.UI.DisplayDayTime
{
	[BepInPlugin("oryxen.valheim.ui.displaydaytime", "Display Day & Time in HUD", "1.1.2")]
	[BepInProcess("valheim.exe")]
	public class DisplayDayTimePlugin : BaseUnityPlugin
	{
		public const string ID = "oryxen.valheim.ui.displaydaytime";

		private static ConfigEntry<bool> _displayUnderMiniMap;

		private static ConfigEntry<bool> _displayDay;

		private static ConfigEntry<bool> _displayTime;

		private static ConfigEntry<bool> _displayBackground;

		private static ConfigEntry<bool> _twentyFourHourClock;

		private static ConfigEntry<int> _fontSize;

		private static ConfigEntry<string> _fontName;

		private static ConfigEntry<Color> _fontColor;

		private static ConfigEntry<bool> _textOutlineEnabled;

		private static ConfigEntry<Color> _textOutlineColor;

		private static ConfigEntry<Color> _backgroundColor;

		private static ConfigEntry<float> _marginBetweenMiniMap;

		private static ConfigEntry<float> _padding;

		private static ConfigEntry<bool> _reverseTextPositions;

		private static ConfigEntry<float> _panelWidth;

		private static ConfigEntry<float> _panelHeight;

		private const string DEFAULT_FONT = "AveriaSansLibre-Bold";

		private static GameObject _panel;

		private static Text _dayText;

		private static Text _timeText;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			_displayUnderMiniMap = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Display under minimap", false, "Display under minimap");
			_displayDay = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Display day", true, "Display day");
			_displayTime = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Display time", true, "Display time");
			_displayBackground = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Display background", true, "Display background");
			_twentyFourHourClock = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "24-hour clock", true, "24-hour clock");
			_fontSize = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Font size", 16, "Font size");
			_fontName = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Font name", "AveriaSansLibre-Bold", "Font name");
			_fontColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "Font color", new Color(1f, 1f, 1f, 0.791f), "Font color");
			_textOutlineEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Text outline enabled", true, "Text outline enabled");
			_textOutlineColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "Text outline color", Color.black, "Text outline color");
			_backgroundColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "Background color", new Color(0f, 0f, 0f, 0.3921569f), "Background color");
			_marginBetweenMiniMap = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Margin between minimap and this panel", 0f, "Margin between minimap and this panel");
			_padding = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Padding left and right", 10f, "Padding left and right from text.");
			_reverseTextPositions = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Reverse text positions", false, "If set to 'true', time will display to the left and day will display to the right.");
			_panelWidth = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Panel width", 200f, "Panel width");
			_panelHeight = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Panel height", 30f, "Panel height");
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "oryxen.valheim.ui.displaydaytime");
		}

		private void OnDestroy()
		{
			if (Object.op_Implicit((Object)(object)Hud.instance) && (Object)(object)_panel != (Object)null)
			{
				Log("Destroying panel...");
				Object.Destroy((Object)(object)_panel);
				Log("Panel destroyed!");
			}
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchAll("oryxen.valheim.ui.displaydaytime");
			}
		}

		private void Update()
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)Player.m_localPlayer) && Object.op_Implicit((Object)(object)Hud.instance) && Traverse.Create((object)Hud.instance).Method("IsVisible", Array.Empty<object>()).GetValue<bool>())
			{
				if ((Object)(object)_panel == (Object)null)
				{
					CreatePanel(Hud.instance);
				}
				RectTransform component = _panel.GetComponent<RectTransform>();
				component.anchoredPosition = new Vector2(-140f, _displayUnderMiniMap.Value ? (-255f - _marginBetweenMiniMap.Value) : (-25f + _marginBetweenMiniMap.Value));
				component.sizeDelta = new Vector2(_panelWidth.Value, _panelHeight.Value);
				Image component2 = _panel.GetComponent<Image>();
				((Behaviour)component2).enabled = _displayBackground.Value;
				((Graphic)component2).color = _backgroundColor.Value;
				((Behaviour)_dayText).enabled = _displayDay.Value;
				((Behaviour)_timeText).enabled = _displayTime.Value;
				if (_displayDay.Value)
				{
					UpdateDay();
				}
				if (_displayTime.Value)
				{
					UpdateTime();
				}
			}
		}

		public static void CreatePanel(Hud hudInstance)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			Log("Creating panel...");
			_panel = new GameObject("DayTimePanel")
			{
				layer = 5
			};
			_panel.transform.SetParent(hudInstance.m_rootObject.transform);
			RectTransform obj = _panel.AddComponent<RectTransform>();
			obj.anchorMin = new Vector2(1f, 1f);
			obj.anchorMax = new Vector2(1f, 1f);
			obj.anchoredPosition = new Vector2(-140f, _displayUnderMiniMap.Value ? (-255f - _marginBetweenMiniMap.Value) : (-25f + _marginBetweenMiniMap.Value));
			obj.sizeDelta = new Vector2(_panelWidth.Value, _panelHeight.Value);
			Sprite sprite = ((IEnumerable<Sprite>)Resources.FindObjectsOfTypeAll<Sprite>()).FirstOrDefault((Func<Sprite, bool>)((Sprite s) => ((Object)s).name == "InputFieldBackground"));
			Image obj2 = _panel.AddComponent<Image>();
			((Behaviour)obj2).enabled = _displayBackground.Value;
			((Graphic)obj2).color = _backgroundColor.Value;
			obj2.sprite = sprite;
			obj2.type = (Type)1;
			Log("Panel created!");
			CreateDay();
			CreateTime();
		}

		private static void CreateDay()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: 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_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			Log("Creating day text...");
			GameObject val = new GameObject("Day")
			{
				layer = 5
			};
			val.transform.SetParent(_panel.transform);
			RectTransform obj = val.AddComponent<RectTransform>();
			obj.anchoredPosition = new Vector2(_reverseTextPositions.Value ? (_panelWidth.Value / 4f - _padding.Value) : (0f - _panelWidth.Value / 4f + _padding.Value), 0f);
			obj.sizeDelta = new Vector2(_panelWidth.Value / 2f, _panelHeight.Value);
			_dayText = val.AddComponent<Text>();
			((Graphic)_dayText).color = _fontColor.Value;
			_dayText.font = GetFont();
			_dayText.fontSize = _fontSize.Value;
			((Behaviour)_dayText).enabled = _displayDay.Value;
			_dayText.alignment = (TextAnchor)(_reverseTextPositions.Value ? 5 : 3);
			Outline obj2 = val.AddComponent<Outline>();
			((Shadow)obj2).effectColor = _textOutlineColor.Value;
			((Shadow)obj2).effectDistance = new Vector2(1f, -1f);
			((Shadow)obj2).useGraphicAlpha = true;
			((MonoBehaviour)obj2).useGUILayout = true;
			((Behaviour)obj2).enabled = _textOutlineEnabled.Value;
			Log("Day created!");
		}

		private static void CreateTime()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: 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_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			Log("Creating time text...");
			GameObject val = new GameObject("Time")
			{
				layer = 5
			};
			val.transform.SetParent(_panel.transform);
			RectTransform obj = val.AddComponent<RectTransform>();
			obj.anchoredPosition = new Vector2(_reverseTextPositions.Value ? (0f - _panelWidth.Value / 4f + _padding.Value) : (_panelWidth.Value / 4f - _padding.Value), 0f);
			obj.sizeDelta = new Vector2(_panelWidth.Value / 2f, _panelHeight.Value);
			_timeText = val.AddComponent<Text>();
			((Graphic)_timeText).color = _fontColor.Value;
			_timeText.font = GetFont();
			_timeText.fontSize = _fontSize.Value;
			((Behaviour)_timeText).enabled = _displayTime.Value;
			_timeText.alignment = (TextAnchor)(_reverseTextPositions.Value ? 3 : 5);
			Outline obj2 = val.AddComponent<Outline>();
			((Shadow)obj2).effectColor = _textOutlineColor.Value;
			((Shadow)obj2).effectDistance = new Vector2(1f, -1f);
			((Shadow)obj2).useGraphicAlpha = true;
			((MonoBehaviour)obj2).useGUILayout = true;
			((Behaviour)obj2).enabled = _textOutlineEnabled.Value;
			Log("Time created!");
		}

		private void UpdateDay()
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			if (((Object)_dayText.font).name != _fontName.Value)
			{
				_dayText.font = GetFont();
			}
			RectTransform component = ((Component)_dayText).GetComponent<RectTransform>();
			component.anchoredPosition = new Vector2(_reverseTextPositions.Value ? (_panelWidth.Value / 4f - _padding.Value) : (0f - _panelWidth.Value / 4f + _padding.Value), 0f);
			component.sizeDelta = new Vector2(_panelWidth.Value / 2f, _panelHeight.Value);
			_dayText.alignment = (TextAnchor)(_reverseTextPositions.Value ? 5 : 3);
			((Graphic)_dayText).color = _fontColor.Value;
			_dayText.fontSize = _fontSize.Value;
			_dayText.text = GetCurrentDayText();
			Outline component2 = ((Component)_dayText).GetComponent<Outline>();
			((Behaviour)component2).enabled = _textOutlineEnabled.Value;
			((Shadow)component2).effectColor = _textOutlineColor.Value;
		}

		private void UpdateTime()
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			if (((Object)_timeText.font).name != _fontName.Value)
			{
				_timeText.font = GetFont();
			}
			RectTransform component = ((Component)_timeText).GetComponent<RectTransform>();
			component.anchoredPosition = new Vector2(_reverseTextPositions.Value ? (0f - _panelWidth.Value / 4f + _padding.Value) : (_panelWidth.Value / 4f - _padding.Value), 0f);
			component.sizeDelta = new Vector2(_panelWidth.Value / 2f, _panelHeight.Value);
			_timeText.alignment = (TextAnchor)(_reverseTextPositions.Value ? 3 : 5);
			((Graphic)_timeText).color = _fontColor.Value;
			_timeText.fontSize = _fontSize.Value;
			_timeText.text = GetCurrentTimeText();
			Outline component2 = ((Component)_timeText).GetComponent<Outline>();
			((Behaviour)component2).enabled = _textOutlineEnabled.Value;
			((Shadow)component2).effectColor = _textOutlineColor.Value;
		}

		private string GetCurrentDayText()
		{
			if (!Object.op_Implicit((Object)(object)EnvMan.instance) || Localization.instance == null)
			{
				return null;
			}
			int num = (int)typeof(EnvMan).GetMethod("GetCurrentDay", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(EnvMan.instance, null);
			return Localization.instance.Localize("$msg_newday", new string[1] { num.ToString() });
		}

		private string GetCurrentTimeText()
		{
			if (!Object.op_Implicit((Object)(object)EnvMan.instance))
			{
				return null;
			}
			float num = (float)typeof(EnvMan).GetField("m_smoothDayFraction", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(EnvMan.instance);
			int num2 = (int)(num * 24f);
			int num3 = (int)((num * 24f - (float)num2) * 60f);
			int second = (int)(((num * 24f - (float)num2) * 60f - (float)num3) * 60f);
			DateTime dateTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, num2, num3, second);
			return dateTime.ToString(_twentyFourHourClock.Value ? "HH:mm" : "hh:mm tt");
		}

		public static Font GetFont()
		{
			Font[] source = Resources.FindObjectsOfTypeAll<Font>();
			Font val = ((IEnumerable<Font>)source).FirstOrDefault((Func<Font, bool>)((Font f) => ((Object)f).name == _fontName.Value));
			if ((Object)(object)val == (Object)null)
			{
				return ((IEnumerable<Font>)source).FirstOrDefault((Func<Font, bool>)((Font f) => ((Object)f).name == "AveriaSansLibre-Bold"));
			}
			return val;
		}

		public static void Log(string message)
		{
			Debug.Log((object)("oryxen.valheim.ui.displaydaytime: " + message));
		}
	}
}
namespace Oryxen.Valheim.UI.DisplayDayTime.GamePatches
{
	[HarmonyPatch(typeof(Hud), "Awake")]
	public static class Hud_Awake_Patch
	{
		public static void Postfix(Hud __instance)
		{
			DisplayDayTimePlugin.CreatePanel(__instance);
		}
	}
}