Decompiled source of Clock v1.0.13

plugins/Clock.dll

Decompiled an hour ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("Clock")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.13.0")]
[assembly: AssemblyInformationalVersion("1.0.13+116e0a986afd643a3298d7a5001c8b7d0cef517f")]
[assembly: AssemblyProduct("Clock")]
[assembly: AssemblyTitle("Clock")]
[assembly: AssemblyVersion("1.0.13.0")]
namespace Clock;

[BepInPlugin("fr.romaindurand.clock", "Clock", "1.0.13")]
public class Plugin : BaseUnityPlugin
{
	private const string Version = "1.0.13";

	private TMP_Text _clockText;

	private Image _clockIcon;

	private Sprite _sunSprite;

	private Sprite _moonSprite;

	private void Awake()
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"Clock {((BaseUnityPlugin)this).Info.Metadata.Version} est chargé.");
	}

	private void Update()
	{
		if (!((Object)(object)EnvMan.instance == (Object)null))
		{
			if ((Object)(object)_clockText == (Object)null)
			{
				CreateClockText();
			}
			if (!((Object)(object)_clockText == (Object)null))
			{
				float dayFraction = EnvMan.instance.GetDayFraction();
				_clockText.text = GameClock.FormatWorldTime(dayFraction);
				UpdateIcon(GameClock.IsWorldNight(dayFraction));
			}
		}
	}

	private void CreateClockText()
	{
		//IL_0068: 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)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Expected O, but got Unknown
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0191: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01df: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e6: Expected O, but got Unknown
		//IL_0218: Unknown result type (might be due to invalid IL or missing references)
		//IL_0224: Unknown result type (might be due to invalid IL or missing references)
		//IL_0239: 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_0262: Unknown result type (might be due to invalid IL or missing references)
		Hud instance = Hud.instance;
		if (!((Object)(object)instance == (Object)null) && !((Object)(object)instance.m_statusEffectTemplate == (Object)null))
		{
			TMP_Text componentInChildren = ((Component)instance.m_statusEffectTemplate).GetComponentInChildren<TMP_Text>(true);
			if (!((Object)(object)componentInChildren == (Object)null) && !((Object)(object)componentInChildren.font == (Object)null))
			{
				GameObject val = new GameObject("ClockCanvas", new Type[2]
				{
					typeof(Canvas),
					typeof(CanvasScaler)
				});
				Canvas component = val.GetComponent<Canvas>();
				component.renderMode = (RenderMode)0;
				component.sortingOrder = 100;
				CanvasScaler component2 = val.GetComponent<CanvasScaler>();
				component2.uiScaleMode = (ScaleMode)1;
				component2.referenceResolution = new Vector2(1920f, 1080f);
				GameObject val2 = new GameObject("ClockText", new Type[1] { typeof(RectTransform) });
				val2.transform.SetParent(((Component)component).transform, false);
				_clockText = (TMP_Text)(object)val2.AddComponent<TextMeshProUGUI>();
				_clockText.font = componentInChildren.font;
				_clockText.fontSharedMaterial = componentInChildren.fontSharedMaterial;
				((Graphic)_clockText).color = ((Graphic)componentInChildren).color;
				_clockText.fontStyle = componentInChildren.fontStyle;
				_clockText.fontWeight = componentInChildren.fontWeight;
				_clockText.fontSize = 28f;
				_clockText.alignment = (TextAlignmentOptions)514;
				RectTransform rectTransform = _clockText.rectTransform;
				rectTransform.anchorMin = new Vector2(0.5f, 1f);
				rectTransform.anchorMax = rectTransform.anchorMin;
				rectTransform.pivot = new Vector2(0.5f, 1f);
				rectTransform.anchoredPosition = new Vector2(0f, -32f);
				rectTransform.sizeDelta = new Vector2(240f, 36f);
				GameObject val3 = new GameObject("ClockIcon", new Type[1] { typeof(RectTransform) });
				val3.transform.SetParent((Transform)(object)rectTransform, false);
				_clockIcon = val3.AddComponent<Image>();
				RectTransform rectTransform2 = ((Graphic)_clockIcon).rectTransform;
				rectTransform2.anchorMin = new Vector2(0.5f, 0.5f);
				rectTransform2.anchorMax = rectTransform2.anchorMin;
				rectTransform2.pivot = new Vector2(0.5f, 0.5f);
				rectTransform2.anchoredPosition = new Vector2(-50f, -1f);
				rectTransform2.sizeDelta = new Vector2(22f, 22f);
			}
		}
	}

	private void UpdateIcon(bool night)
	{
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)_sunSprite == (Object)null)
		{
			_sunSprite = CreateIconSprite(moon: false);
		}
		if ((Object)(object)_moonSprite == (Object)null)
		{
			_moonSprite = CreateIconSprite(moon: true);
		}
		Sprite val = (night ? _moonSprite : _sunSprite);
		if (!((Object)(object)_clockIcon.sprite == (Object)(object)val))
		{
			_clockIcon.sprite = val;
			((Graphic)_clockIcon).color = (night ? new Color(0.75f, 0.85f, 1f) : new Color(1f, 0.85f, 0.25f));
		}
	}

	private static Sprite CreateIconSprite(bool moon)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		Texture2D val = new Texture2D(64, 64, (TextureFormat)4, false);
		((Texture)val).filterMode = (FilterMode)1;
		for (int i = 0; i < 64; i++)
		{
			for (int j = 0; j < 64; j++)
			{
				float num = (float)j - 31.5f;
				float num2 = (float)i - 31.5f;
				float num3 = num * num + num2 * num2;
				bool flag;
				if (moon)
				{
					flag = num3 <= 576f;
					float num4 = (float)j - 41.5f;
					flag = flag && num4 * num4 + num2 * num2 > 400f;
				}
				else
				{
					flag = num3 <= 324f;
					float num5 = Mathf.Abs(num);
					float num6 = Mathf.Abs(num2);
					bool flag2 = (num5 <= 2f || num6 <= 2f) && num3 <= 729f;
					bool flag3 = Mathf.Abs(num5 - num6) <= 2f && num3 <= 576f;
					flag = flag || flag2 || flag3;
				}
				val.SetPixel(j, i, flag ? Color.white : Color.clear);
			}
		}
		val.Apply();
		return Sprite.Create(val, new Rect(0f, 0f, 64f, 64f), new Vector2(0.5f, 0.5f), 64f);
	}
}
internal static class GameClock
{
	private const int CycleSeconds = 1800;

	private const int DaySeconds = 1260;

	private const int NightSeconds = 540;

	private const int NightStartsAtSecond = 1261;

	private const float DawnFraction = 0.25f;

	private const float DuskFraction = 0.75f;

	internal static string Format(float dayFraction)
	{
		int cycleSeconds = GetCycleSeconds(dayFraction);
		return $"{cycleSeconds / 60:00}:{cycleSeconds % 60:00}";
	}

	internal static bool IsNight(float dayFraction)
	{
		return GetCycleSeconds(dayFraction) >= 1261;
	}

	internal static string FormatWorldTime(float worldDayFraction)
	{
		return FormatSeconds(GetWorldSeconds(worldDayFraction));
	}

	internal static bool IsWorldNight(float worldDayFraction)
	{
		if (!(worldDayFraction > 0.75f))
		{
			return worldDayFraction < 0.25f;
		}
		return true;
	}

	private static int GetCycleSeconds(float dayFraction)
	{
		return (int)Math.Floor(Math.Max(0f, Math.Min(dayFraction, 0.999999f)) * 1800f);
	}

	private static string FormatSeconds(int seconds)
	{
		return $"{seconds / 60:00}:{seconds % 60:00}";
	}

	private static int GetWorldSeconds(float worldDayFraction)
	{
		float num = Math.Max(0f, Math.Min(worldDayFraction, 0.999999f));
		if (num >= 0.25f && num <= 0.75f)
		{
			return (int)Math.Floor((num - 0.25f) / 0.5f * 1260f);
		}
		float num2 = ((num > 0.75f) ? ((num - 0.75f) / 0.5f) : ((num + 1f - 0.75f) / 0.5f));
		return Math.Min(1799, 1260 + (int)Math.Floor(num2 * 540f));
	}
}