Decompiled source of LCBetterClock v1.0.3

plugins/BetterClock.dll

Decompiled a year ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BetterClock")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BetterClock")]
[assembly: AssemblyCopyright("Copyright © BlueAmulet 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("de27b4d1-820d-4505-a953-6001420281e4")]
[assembly: AssemblyFileVersion("1.0.3")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.3.0")]
namespace BetterClock
{
	[BepInPlugin("BlueAmulet.BetterClock", "BetterClock", "1.0.3")]
	public class BetterClock : BaseUnityPlugin
	{
		internal const string Name = "BetterClock";

		internal const string Author = "BlueAmulet";

		internal const string ID = "BlueAmulet.BetterClock";

		internal const string Version = "1.0.3";

		public void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			Settings.InitConfig(((BaseUnityPlugin)this).Config);
			Harmony val = new Harmony("BlueAmulet.BetterClock");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Applying Harmony patches");
			val.PatchAll(Assembly.GetExecutingAssembly());
			int num = 0;
			foreach (MethodBase patchedMethod in val.GetPatchedMethods())
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Patched " + patchedMethod.DeclaringType.Name + "." + patchedMethod.Name));
				num++;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)(num + " patches applied"));
		}
	}
	internal static class Settings
	{
		internal static ConfigEntry<bool> compact;

		internal static ConfigEntry<bool> leadingZero;

		internal static ConfigEntry<bool> darkZero;

		internal static ConfigEntry<bool> fasterUpdate;

		internal static ConfigEntry<bool> raiseClock;

		internal static ConfigEntry<bool> hours24;

		internal static ConfigEntry<bool> properTime;

		internal static ConfigEntry<float> visibilityShip;

		internal static ConfigEntry<float> visibilityOutside;

		internal static ConfigEntry<float> visibilityInside;

		internal static ConfigEntry<float> visibilityOverride;

		internal static ConfigEntry<KeyboardShortcut> overrideKeybind;

		internal static ConfigEntry<bool> overrideToggle;

		public static void InitConfig(ConfigFile config)
		{
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			compact = config.Bind<bool>("Clock", "CompactClock", true, "Makes the clock more compact");
			leadingZero = config.Bind<bool>("Clock", "LeadingZero", true, "Adds a leading zero to hours before 10");
			darkZero = config.Bind<bool>("Clock", "DarkZero", true, "Leading zeros are dark");
			fasterUpdate = config.Bind<bool>("Clock", "FasterUpdate", true, "Update the clock more often");
			raiseClock = config.Bind<bool>("Clock", "RaiseClock", true, "Raise the clock near the top of the screen");
			hours24 = config.Bind<bool>("Clock", "24Hours", false, "Use 24 hour time");
			properTime = config.Bind<bool>("Clock", "ProperTime", true, "Fix time formatting caused by the game or other mods");
			visibilityShip = config.Bind<float>("Clock", "VisibilityShip", 1f, "Visibility of clock inside ship");
			visibilityOutside = config.Bind<float>("Clock", "VisibilityOutside", 1f, "Visibility of clock outside");
			visibilityInside = config.Bind<float>("Clock", "VisibilityInside", 0.25f, "Visibility of clock inside factory");
			visibilityOverride = config.Bind<float>("Clock", "VisibilityOverride", 1f, "Visibility when using override keybind");
			overrideKeybind = config.Bind<KeyboardShortcut>("Clock", "OverrideKeybind", KeyboardShortcut.Empty, "Keybind to trigger visibility override");
			overrideToggle = config.Bind<bool>("Clock", "OverrideToggle", false, "Switch the override keybind between toggle or hold");
		}
	}
}
namespace BetterClock.Patches
{
	[HarmonyPatch(typeof(HUDManager))]
	internal static class ClockPatch
	{
		private static int lastTime = -1;

		private static bool overrideDisplay = false;

		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		public static void PostfixAwake(ref HUDManager __instance)
		{
			//IL_00a0: 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_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: 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_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: 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_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_0076: 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_008f: Unknown result type (might be due to invalid IL or missing references)
			if (!Settings.compact.Value)
			{
				return;
			}
			Transform parent = ((TMP_Text)__instance.clockNumber).transform.parent;
			if (Settings.raiseClock.Value)
			{
				Dictionary<string, PluginInfo> pluginInfos = Chainloader.PluginInfos;
				if (pluginInfos.ContainsKey("SolosRingCompass") || pluginInfos.ContainsKey("LineCompassPlugin"))
				{
					parent.localPosition += new Vector3(0f, 20f, 0f);
				}
				else
				{
					parent.localPosition += new Vector3(0f, 40f, 0f);
				}
			}
			RectTransform component = ((Component)parent).GetComponent<RectTransform>();
			component.sizeDelta = new Vector2(component.sizeDelta.x, 50f);
			((TMP_Text)__instance.clockNumber).enableWordWrapping = false;
			RectTransform component2 = ((Component)__instance.clockIcon).GetComponent<RectTransform>();
			component2.sizeDelta *= 0.6f;
			if (!Settings.hours24.Value)
			{
				Transform transform = ((TMP_Text)__instance.clockNumber).transform;
				transform.localPosition += new Vector3(10f, -1f, 0f);
				Transform transform2 = ((Component)__instance.clockIcon).transform;
				transform2.localPosition += new Vector3(-25f, -2f, 0f);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("SetClockVisible")]
		public static bool PrefixVisible(ref HUDManager __instance)
		{
			//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)
			KeyboardShortcut value = Settings.overrideKeybind.Value;
			if (Settings.overrideToggle.Value)
			{
				if (((KeyboardShortcut)(ref value)).IsDown())
				{
					overrideDisplay = !overrideDisplay;
				}
			}
			else
			{
				overrideDisplay = ((KeyboardShortcut)(ref value)).IsPressed();
			}
			if (overrideDisplay)
			{
				__instance.Clock.targetAlpha = Settings.visibilityOverride.Value;
				return false;
			}
			GameNetworkManager instance = GameNetworkManager.Instance;
			PlayerControllerB val = null;
			if ((Object)(object)instance != (Object)null)
			{
				val = instance.localPlayerController;
			}
			if ((Object)(object)val != (Object)null)
			{
				if (val.isInHangarShipRoom)
				{
					__instance.Clock.targetAlpha = Settings.visibilityShip.Value;
				}
				else if (val.isInsideFactory)
				{
					__instance.Clock.targetAlpha = Settings.visibilityInside.Value;
				}
				else
				{
					__instance.Clock.targetAlpha = Settings.visibilityOutside.Value;
				}
				return false;
			}
			return true;
		}

		[HarmonyPostfix]
		[HarmonyPatch("SetClock")]
		public static void PostfixSetClock(ref HUDManager __instance, ref float timeNormalized, ref float numberOfHours)
		{
			int num = (int)(timeNormalized * (60f * numberOfHours)) + 360;
			int num2 = num / 60 % 24;
			int num3 = num % 60;
			string text = ((!Settings.hours24.Value) ? ((TMP_Text)__instance.clockNumber).text : $"{num2}:{num3:00}");
			if (Settings.properTime.Value)
			{
				if (text.Length >= 3 && text[0] == '0' && text[2] == ':')
				{
					text = text.Substring(1);
				}
				else if (text.StartsWith("24:"))
				{
					text = "0:" + text.Substring(3);
				}
				if (text.StartsWith("0:") && text.EndsWith("M"))
				{
					text = "12:" + text.Substring(2);
				}
				if (num2 < 12 && text.EndsWith("PM"))
				{
					text = text.Substring(0, text.Length - 2) + "AM";
				}
				else if (num2 >= 12 && text.EndsWith("AM"))
				{
					text = text.Substring(0, text.Length - 2) + "PM";
				}
			}
			if (Settings.compact.Value)
			{
				text = text.Replace('\n', ' ').Replace("   ", " ");
			}
			if (Settings.leadingZero.Value && (text.Length <= 4 || text.Length == 7))
			{
				text = ((!Settings.darkZero.Value) ? ("0" + text) : ("<color=#602000>0</color>" + text));
			}
			((TMP_Text)__instance.clockNumber).text = text;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(TimeOfDay))]
		[HarmonyPatch("MoveTimeOfDay")]
		public static void PostfixMoveTimeOfDay(ref TimeOfDay __instance, ref float ___changeHUDTimeInterval)
		{
			if (Settings.fasterUpdate.Value)
			{
				int num = (int)(__instance.normalizedTimeOfDay * (60f * (float)__instance.numberOfHours));
				if (num != lastTime)
				{
					lastTime = num;
					HUDManager.Instance.SetClock(__instance.normalizedTimeOfDay, (float)__instance.numberOfHours, true);
					___changeHUDTimeInterval = 0f;
				}
			}
		}
	}
}