Decompiled source of FATEDayNight v0.2.4

BepInEx/plugins/FateCoop.DayNight/FateCoop.DayNight.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using TMPro;
using Tableflip;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("PartGhost")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.4.0")]
[assembly: AssemblyInformationalVersion("0.2.4")]
[assembly: AssemblyProduct("FateCoop.DayNight")]
[assembly: AssemblyTitle("FateCoop.DayNight")]
[assembly: AssemblyVersion("0.2.4.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[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 FateCoop.DayNight
{
	internal sealed class ClockOverlay : MonoBehaviour
	{
		private Canvas? canvas;

		private TextMeshProUGUI? label;

		private RectTransform? rect;

		private float nextPlace;

		private float nextBuild;

		private bool shown;

		internal static ClockOverlay? Instance { get; private set; }

		internal static void Install(Transform parent)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)Instance != (Object)null))
			{
				GameObject val = new GameObject("FATE Day Night Clock");
				val.transform.SetParent(parent, false);
				Instance = val.AddComponent<ClockOverlay>();
			}
		}

		internal void Set(bool show, string clock)
		{
			if (!show)
			{
				if ((Object)(object)label != (Object)null)
				{
					((Component)label).gameObject.SetActive(false);
				}
				shown = false;
				return;
			}
			if ((Object)(object)label == (Object)null && Time.unscaledTime >= nextBuild)
			{
				nextBuild = Time.unscaledTime + 1f;
				Build();
			}
			if (!((Object)(object)label == (Object)null) && !((Object)(object)rect == (Object)null))
			{
				if (!shown)
				{
					((Component)label).gameObject.SetActive(true);
					shown = true;
				}
				if (((TMP_Text)label).text != clock)
				{
					((TMP_Text)label).text = clock;
				}
				if (Time.unscaledTime >= nextPlace)
				{
					nextPlace = Time.unscaledTime + 2f;
					Place();
				}
			}
		}

		private bool Build()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Expected O, but got Unknown
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)label != (Object)null && (Object)(object)((Component)label).gameObject != (Object)null)
			{
				return true;
			}
			TMP_FontAsset val = FindFont();
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			GameObject val2 = new GameObject("Clock Canvas", new Type[1] { typeof(RectTransform) });
			val2.transform.SetParent(((Component)this).transform, false);
			canvas = val2.AddComponent<Canvas>();
			canvas.renderMode = (RenderMode)0;
			canvas.sortingOrder = 300;
			GameObject val3 = new GameObject("Clock", new Type[1] { typeof(RectTransform) });
			val3.transform.SetParent(val2.transform, false);
			label = val3.AddComponent<TextMeshProUGUI>();
			((TMP_Text)label).font = val;
			((TMP_Text)label).fontSharedMaterial = ((TMP_Asset)val).material;
			((TMP_Text)label).text = string.Empty;
			((TMP_Text)label).richText = false;
			((Graphic)label).raycastTarget = false;
			((TMP_Text)label).textWrappingMode = (TextWrappingModes)0;
			((TMP_Text)label).alignment = (TextAlignmentOptions)260;
			((TMP_Text)label).fontSize = 26f;
			((Graphic)label).color = new Color(1f, 0.9f, 0.7f, 1f);
			rect = (RectTransform)val3.transform;
			rect.anchorMin = new Vector2(1f, 1f);
			rect.anchorMax = new Vector2(1f, 1f);
			rect.pivot = new Vector2(1f, 1f);
			rect.sizeDelta = new Vector2(320f, 40f);
			rect.anchoredPosition = new Vector2(-28f, -96f);
			return true;
		}

		private void Place()
		{
			//IL_00e9: 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_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)rect == (Object)null) && !((Object)(object)label == (Object)null))
			{
				TextMeshProUGUI val = FindAnchorLabel();
				if ((Object)(object)val != (Object)null && ScreenRect((RectTransform)((TMP_Text)val).transform, out var screen))
				{
					float num = Mathf.Max(14f, ((Rect)(ref screen)).height * 0.7f);
					float num2 = Mathf.Min(((Rect)(ref screen)).xMax, (float)Screen.width - 12f);
					rect.anchoredPosition = new Vector2(num2 - (float)Screen.width, 0f - ((float)Screen.height - ((Rect)(ref screen)).yMin) - num);
					((TMP_Text)label).fontSize = Mathf.Clamp(((TMP_Text)val).fontSize * 0.8f, 14f, 34f);
					((Graphic)label).color = ((Graphic)val).color;
				}
				else
				{
					rect.anchoredPosition = new Vector2(-28f, -96f);
					((TMP_Text)label).fontSize = 26f;
					((Graphic)label).color = new Color(1f, 0.9f, 0.7f, 1f);
				}
			}
		}

		private TextMeshProUGUI? FindAnchorLabel()
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Expected O, but got Unknown
			string text = ((object)Unsafe.As<Scene, Scene>(ref SceneLoad.CurrentScene)/*cast due to .constrained prefix*/).ToString();
			string text2 = (text.StartsWith("Town_", StringComparison.Ordinal) ? text.Substring(5) : text);
			TextMeshProUGUI val = null;
			TextMeshProUGUI val2 = null;
			TextMeshProUGUI val3 = null;
			float num = float.MinValue;
			float num2 = float.MinValue;
			float num3 = float.MinValue;
			TextMeshProUGUI[] array = Resources.FindObjectsOfTypeAll<TextMeshProUGUI>();
			foreach (TextMeshProUGUI val4 in array)
			{
				if ((Object)(object)val4 == (Object)null || (Object)(object)((TMP_Text)val4).font == (Object)null || (Object)(object)((TMP_Text)val4).transform.root == (Object)(object)((Component)this).transform.root)
				{
					continue;
				}
				string text3 = (((TMP_Text)val4).text ?? string.Empty).Trim();
				if (text3.Length == 0 || !ScreenRect((RectTransform)((TMP_Text)val4).transform, out var screen) || ((Rect)(ref screen)).yMin < (float)Screen.height * 0.55f || ((Rect)(ref screen)).xMin < (float)Screen.width * 0.55f)
				{
					continue;
				}
				if (text3.Equals(text2, StringComparison.OrdinalIgnoreCase))
				{
					return val4;
				}
				if (text2.Length > 2 && text3.IndexOf(text2, StringComparison.OrdinalIgnoreCase) >= 0)
				{
					if (((Rect)(ref screen)).yMax > num)
					{
						num = ((Rect)(ref screen)).yMax;
						val = val4;
					}
				}
				else if (text3.Equals("LOCATION", StringComparison.OrdinalIgnoreCase))
				{
					if (((Rect)(ref screen)).yMax > num2)
					{
						num2 = ((Rect)(ref screen)).yMax;
						val2 = val4;
					}
				}
				else if (((Rect)(ref screen)).yMax > num3)
				{
					num3 = ((Rect)(ref screen)).yMax;
					val3 = val4;
				}
			}
			return val ?? val2 ?? val3;
		}

		private static bool ScreenRect(RectTransform source, out Rect screen)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: 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_004a: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: 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)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
			screen = default(Rect);
			Canvas componentInParent = ((Component)source).GetComponentInParent<Canvas>();
			Camera obj = (((Object)(object)componentInParent != (Object)null && (int)componentInParent.renderMode != 0) ? componentInParent.worldCamera : null);
			Rect val = source.rect;
			Vector2 val2 = RectTransformUtility.WorldToScreenPoint(obj, ((Transform)source).TransformPoint(Vector2.op_Implicit(((Rect)(ref val)).min)));
			val = source.rect;
			Vector2 val3 = RectTransformUtility.WorldToScreenPoint(obj, ((Transform)source).TransformPoint(Vector2.op_Implicit(((Rect)(ref val)).max)));
			screen = Rect.MinMaxRect(Mathf.Min(val2.x, val3.x), Mathf.Min(val2.y, val3.y), Mathf.Max(val2.x, val3.x), Mathf.Max(val2.y, val3.y));
			if (((Rect)(ref screen)).width > 1f)
			{
				return ((Rect)(ref screen)).height > 1f;
			}
			return false;
		}

		private TMP_FontAsset? FindFont()
		{
			TextMeshProUGUI[] array = Resources.FindObjectsOfTypeAll<TextMeshProUGUI>();
			foreach (TextMeshProUGUI val in array)
			{
				if (!((Object)(object)val == (Object)null) && !((Object)(object)((TMP_Text)val).font == (Object)null) && !((Object)(object)((TMP_Text)val).transform.root == (Object)(object)((Component)this).transform.root))
				{
					return ((TMP_Text)val).font;
				}
			}
			return TMP_Settings.defaultFontAsset;
		}
	}
	[BepInPlugin("local.fatecoop.daynight", "FATE Day and Night", "0.2.4")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		private const float AlwaysDay = -1f;

		private const float NightSun = 0.25f;

		private const float NightLampIntensity = 6f;

		private const float NightLampRange = 4f;

		private const float NightWaterShade = 0.85f;

		private const float NightFoam = 0.6f;

		private const string WaterColor = "_WaterColor";

		private const string WaterFoam = "_FoamIntensity";

		private static readonly Color Dusk = new Color(1f, 0.78f, 0.6f);

		private static readonly Color DuskSky = new Color(1f, 0.88f, 0.8f);

		private static readonly Color Moon = new Color(0.55f, 0.65f, 1f);

		private static readonly Color NightSky = new Color(0.6f, 0.7f, 1f);

		private static readonly (float Phase, float Sun, Color SunTint, float Ambient, Color AmbientTint)[] DayKeys = new(float, float, Color, float, Color)[7]
		{
			(0f, 1f, Color.white, 1f, Color.white),
			(0.6f, 1f, Color.white, 1f, Color.white),
			(0.65f, 0.6f, Dusk, 0.7f, DuskSky),
			(0.7f, 0.25f, Moon, 0.42f, NightSky),
			(0.9f, 0.25f, Moon, 0.42f, NightSky),
			(0.95f, 0.6f, Dusk, 0.7f, DuskSky),
			(1f, 1f, Color.white, 1f, Color.white)
		};

		private readonly List<(Light light, float intensity, float range)> groveLamps = new List<(Light, float, float)>();

		private readonly List<(Material material, Color color, float foam)> groveWater = new List<(Material, Color, float)>();

		private float dayPhase;

		private Light? groveSun;

		private float daySunIntensity;

		private Color daySunColor;

		private Color daySky;

		private Color dayEquator;

		private Color dayGround;

		private float nextCaptureAttempt;

		private float nextReport;

		private Scene lastScene;

		public const string Guid = "local.fatecoop.daynight";

		public const string DisplayName = "FATE Day and Night";

		public const string Version = "0.2.4";

		private const float MaxMinutes = 180f;

		internal static object? Alive;

		private ConfigEntry<float> dayLength;

		private ConfigEntry<bool> showClock;

		private object? menuModule;

		private float nextMenuRegister;

		private void CaptureGroveDay()
		{
			//IL_0042: 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_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			groveSun = RenderSettings.sun;
			if ((Object)(object)groveSun == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Grove lighting not ready: RenderSettings.sun is null.");
				return;
			}
			daySunIntensity = groveSun.intensity;
			daySunColor = groveSun.color;
			daySky = RenderSettings.ambientSkyColor;
			dayEquator = RenderSettings.ambientEquatorColor;
			dayGround = RenderSettings.ambientGroundColor;
			Scene scene = ((Component)groveSun).gameObject.scene;
			GameObject[] rootGameObjects = ((Scene)(ref scene)).GetRootGameObjects();
			groveLamps.Clear();
			groveLamps.AddRange(from light in rootGameObjects.SelectMany((GameObject root) => root.GetComponentsInChildren<Light>())
				where (int)light.type == 2
				select (light: light, intensity: light.intensity, range: light.range));
			groveWater.Clear();
			groveWater.AddRange(from renderer in rootGameObjects.SelectMany((GameObject root) => root.GetComponentsInChildren<Renderer>())
				where (Object)(object)renderer.sharedMaterial != (Object)null && renderer.sharedMaterial.HasProperty("_WaterColor")
				select renderer.material into water
				select (water: water, water.GetColor("_WaterColor"), water.GetFloat("_FoamIntensity")));
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Grove lighting captured: sun=" + ((Object)groveSun).name + ", " + groveLamps.Count + " lamps, " + groveWater.Count + " water materials, ambient mode " + ((object)RenderSettings.ambientMode/*cast due to .constrained prefix*/).ToString() + "."));
		}

		private float LocalDayPhase()
		{
			if (dayLength == null || !(dayLength.Value > 0f))
			{
				return -1f;
			}
			return dayPhase;
		}

		private float DayPhase()
		{
			return LocalDayPhase();
		}

		private unsafe void UpdateDayNight()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Invalid comparison between Unknown and I4
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: 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_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: 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)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: 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_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: 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)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0222: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_0241: Unknown result type (might be due to invalid IL or missing references)
			//IL_0243: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0254: Unknown result type (might be due to invalid IL or missing references)
			//IL_025b: Unknown result type (might be due to invalid IL or missing references)
			Scene currentScene = SceneLoad.CurrentScene;
			if (currentScene != lastScene)
			{
				lastScene = currentScene;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Day and night scene is now " + ((object)(*(Scene*)(&currentScene))/*cast due to .constrained prefix*/).ToString() + "."));
			}
			if ((int)currentScene != 3)
			{
				return;
			}
			if ((Object)(object)groveSun == (Object)null)
			{
				if (Time.unscaledTime < nextCaptureAttempt)
				{
					return;
				}
				nextCaptureAttempt = Time.unscaledTime + 1f;
				CaptureGroveDay();
				if ((Object)(object)groveSun == (Object)null)
				{
					return;
				}
			}
			float intensity = groveSun.intensity;
			float num = DayPhase();
			float num2;
			Color val;
			float num3;
			Color val2;
			if (!(num < 0f))
			{
				(num2, val, num3, val2) = DayAt(num);
			}
			else
			{
				Color white = Color.white;
				float num4 = 1f;
				Color white2 = Color.white;
				val2 = white2;
				num3 = num4;
				val = white;
				num2 = 1f;
			}
			groveSun.intensity = daySunIntensity * num2;
			groveSun.color = daySunColor * val;
			RenderSettings.ambientSkyColor = daySky * val2 * num3;
			RenderSettings.ambientEquatorColor = dayEquator * val2 * num3;
			RenderSettings.ambientGroundColor = dayGround * val2 * num3;
			float num5 = Mathf.InverseLerp(1f, 0.25f, num2);
			foreach (var (val3, num6, num7) in groveLamps)
			{
				if (!((Object)(object)val3 == (Object)null))
				{
					val3.intensity = num6 * Mathf.Lerp(1f, 6f, num5);
					val3.range = num7 * Mathf.Lerp(1f, 4f, num5);
				}
			}
			foreach (var (val4, val5, num8) in groveWater)
			{
				if (!((Object)(object)val4 == (Object)null))
				{
					val4.SetColor("_WaterColor", Color.Lerp(val5, val5 * NightSky * 0.85f, num5));
					val4.SetFloat("_FoamIntensity", num8 * Mathf.Lerp(1f, 0.6f, num5));
				}
			}
			if (Time.unscaledTime >= nextReport)
			{
				nextReport = Time.unscaledTime + 10f;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Day and night phase " + num.ToString("0.###") + " (" + ClockText(num) + "), sun intensity was " + intensity.ToString("0.###") + ", set to " + groveSun.intensity.ToString("0.###") + "."));
			}
		}

		private static (float sun, Color sunTint, float ambient, Color ambientTint) DayAt(float phase)
		{
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			int val = Array.FindIndex(DayKeys, ((float Phase, float Sun, Color SunTint, float Ambient, Color AmbientTint) key) => key.Phase >= phase);
			(float, float, Color, float, Color) tuple = DayKeys[Math.Max(1, val)];
			(float, float, Color, float, Color) tuple2 = DayKeys[Math.Max(1, val) - 1];
			float num = Mathf.SmoothStep(0f, 1f, (phase - tuple2.Item1) / (tuple.Item1 - tuple2.Item1));
			return (sun: Mathf.Lerp(tuple2.Item2, tuple.Item2, num), sunTint: Color.Lerp(tuple2.Item3, tuple.Item3, num), ambient: Mathf.Lerp(tuple2.Item4, tuple.Item4, num), ambientTint: Color.Lerp(tuple2.Item5, tuple.Item5, num));
		}

		private void Awake()
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			Alive = this;
			dayLength = ((BaseUnityPlugin)this).Config.Bind<float>("Grove", "DayLengthMinutes", 20f, new ConfigDescription("Minutes for one day and night in Grove, 0 to 180. 0 keeps it day. 0.05 is a three-second day for testing.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 180f), Array.Empty<object>()));
			showClock = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "ShowClock", true, "Show the time of day under the town name, in the game's own font.");
			SceneLoad.OnSceneLoaded = (Action<Scene>)Delegate.Combine(SceneLoad.OnSceneLoaded, new Action<Scene>(OnGameSceneLoaded));
			RegisterMenu();
			DayNightDriver.Install();
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Day and night ready: " + Describe()));
		}

		internal void Tick()
		{
			try
			{
				AdvanceClock();
				UpdateDayNight();
				UpdateClock();
				RegisterMenu();
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}

		private void AdvanceClock()
		{
			float num = ((dayLength == null) ? 0f : dayLength.Value);
			if (num <= 0f)
			{
				dayPhase = 0f;
			}
			else
			{
				dayPhase = (dayPhase + Time.unscaledDeltaTime / (num * 60f)) % 1f;
			}
		}

		private void UpdateClock()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Invalid comparison between Unknown and I4
			ClockOverlay instance = ClockOverlay.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				instance.Set(showClock != null && showClock.Value && (int)SceneLoad.CurrentScene == 3, ClockText(LocalDayPhase()));
			}
		}

		internal static string ClockText(float phase)
		{
			if (phase < 0f)
			{
				return "Day";
			}
			float num = ((phase < 0.6f) ? (6f + phase / 0.6f * 12f) : ((phase < 0.7f) ? (18f + (phase - 0.6f) / 0.1f * 2f) : ((!(phase < 0.9f)) ? (5f + (phase - 0.9f) / 0.1f) : (20f + (phase - 0.7f) / 0.2f * 9f))));
			num %= 24f;
			int num2 = (int)num;
			int num3 = (int)((num - (float)num2) * 60f);
			return num2.ToString("00") + ":" + num3.ToString("00");
		}

		private string Describe()
		{
			float value = dayLength.Value;
			if (value <= 0f)
			{
				return "always day";
			}
			if (value < 1f)
			{
				return Mathf.RoundToInt(value * 60f) + " seconds per day";
			}
			return value.ToString("0.#") + " minutes per day";
		}

		private void OnGameSceneLoaded(Scene scene)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			if ((int)scene == 3)
			{
				CaptureGroveDay();
			}
		}

		private void RegisterMenu()
		{
			if (MenuAlive())
			{
				return;
			}
			menuModule = null;
			if (Time.unscaledTime < nextMenuRegister)
			{
				return;
			}
			nextMenuRegister = Time.unscaledTime + 2f;
			Type type = FindType("ZombieGhost.ModMenu.ModMenuApi");
			if (type == null)
			{
				return;
			}
			try
			{
				MethodInfo method = type.GetMethod("RegisterModule", new Type[2]
				{
					typeof(string),
					typeof(string)
				});
				if (!(method == null))
				{
					menuModule = method.Invoke(null, new object[2] { "local.fatecoop.daynight", "FATE Day and Night" });
					Type type2 = menuModule.GetType();
					type2.GetMethod("RegisterSlider")?.Invoke(menuModule, new object[9]
					{
						"daynight.length",
						"Day length (minutes)",
						(Func<float>)(() => dayLength.Value),
						(Action<float>)delegate(float value)
						{
							dayLength.Value = value;
						},
						0f,
						180f,
						0.05f,
						"Cycle",
						"0.##"
					});
					type2.GetMethod("RegisterNote")?.Invoke(menuModule, new object[3] { "daynight.note", "0 keeps Grove in daylight. 0.05 is a three-second day for testing; 180 is a three-hour day.", "Cycle" });
					type2.GetMethod("RegisterToggle")?.Invoke(menuModule, new object[5]
					{
						"daynight.clock",
						"Show the time of day",
						(Func<bool>)(() => showClock.Value),
						(Action<bool>)delegate(bool value)
						{
							showClock.Value = value;
						},
						"Display"
					});
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Day and night registered its speed with FATE Mod Menu.");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("FATE Mod Menu registration skipped: " + ex.Message));
				menuModule = null;
				nextMenuRegister = Time.unscaledTime + 30f;
			}
		}

		private bool MenuAlive()
		{
			if (menuModule == null)
			{
				return false;
			}
			try
			{
				PropertyInfo property = menuModule.GetType().GetProperty("Disposed");
				bool flag = default(bool);
				int num;
				if (property != null)
				{
					object value = property.GetValue(menuModule, null);
					if (value is bool)
					{
						flag = (bool)value;
						num = 1;
					}
					else
					{
						num = 0;
					}
				}
				else
				{
					num = 0;
				}
				return ((uint)num & (flag ? 1u : 0u)) == 0;
			}
			catch
			{
				return false;
			}
		}

		private static Type? FindType(string fullName)
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				try
				{
					Type type = assembly.GetType(fullName, throwOnError: false);
					if (type != null)
					{
						return type;
					}
				}
				catch
				{
				}
			}
			return null;
		}

		private void OnDestroy()
		{
			SceneLoad.OnSceneLoaded = (Action<Scene>)Delegate.Remove(SceneLoad.OnSceneLoaded, new Action<Scene>(OnGameSceneLoaded));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Day and night plugin object destroyed; the clock driver keeps running.");
		}
	}
	internal sealed class DayNightDriver : MonoBehaviour
	{
		internal static void Install()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("FATE DayNight Driver");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<DayNightDriver>();
			ClockOverlay.Install(val.transform);
		}

		private void Update()
		{
			if (Plugin.Alive is Plugin plugin)
			{
				plugin.Tick();
			}
		}
	}
}