Decompiled source of Clock v1.6.0

ClockMod.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		if (!extra)
		{
			int result;
			if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
			{
				Chat instance = Chat.instance;
				result = ((instance != null && instance.HasFocus()) ? 1 : 0);
			}
			else
			{
				result = 1;
			}
			return (byte)result != 0;
		}
		int result2;
		if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
		{
			Chat instance2 = Chat.instance;
			if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
			{
				TextViewer instance3 = TextViewer.instance;
				result2 = ((instance3 != null && instance3.IsVisible()) ? 1 : 0);
				goto IL_00d2;
			}
		}
		result2 = 1;
		goto IL_00d2;
		IL_00d2:
		return (byte)result2 != 0;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			return Input.GetKeyDown(value.ToLower());
		}
		catch
		{
			return false;
		}
	}

	public static bool CheckKeyHeld(string value, bool req = true)
	{
		try
		{
			return Input.GetKey(value.ToLower());
		}
		catch
		{
			return !req;
		}
	}
}
namespace ClockMod;

[BepInPlugin("aedenthorn.ClockMod", "Clock Mod", "1.6.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	private static class ZNetScene_Awake_Patch
	{
		private static void Postfix()
		{
			if (modEnabled.Value)
			{
				ApplyConfig();
			}
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(debugName + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				ApplyConfig();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { text }).GetValue();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { ((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded" }).GetValue();
				return false;
			}
			if (text.ToLower().Equals(debugName + " osfonts"))
			{
				Traverse.Create((object)__instance).Method("AddString", new object[1] { text }).GetValue();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { "OS Fonts dumped to Player.log" }).GetValue();
				string[] oSInstalledFontNames = Font.GetOSInstalledFontNames();
				string[] array = oSInstalledFontNames;
				foreach (string str in array)
				{
					Dbgl(str);
				}
				return false;
			}
			return true;
		}
	}

	private static string debugName = "clockmod";

	private static int windowId = 434343;

	private static readonly bool isDebug = true;

	private static BepInExPlugin context;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<bool> showingClock;

	public static ConfigEntry<bool> showClockOnChange;

	public static ConfigEntry<float> showClockOnChangeFadeTime;

	public static ConfigEntry<float> showClockOnChangeFadeLength;

	public static ConfigEntry<bool> toggleClockKeyOnPress;

	public static ConfigEntry<bool> clockUseOSFont;

	public static ConfigEntry<bool> clockUseShadow;

	public static ConfigEntry<Color> clockFontColor;

	public static ConfigEntry<Color> clockShadowColor;

	public static ConfigEntry<int> clockShadowOffset;

	public static ConfigEntry<Vector2> clockLocation;

	public static ConfigEntry<string> clockLocationString;

	public static ConfigEntry<int> clockFontSize;

	public static ConfigEntry<string> toggleClockKeyMod;

	public static ConfigEntry<string> toggleClockKey;

	public static ConfigEntry<string> clockFontName;

	public static ConfigEntry<string> clockFormat;

	public static ConfigEntry<string> clockString;

	public static ConfigEntry<TextAnchor> clockTextAlignment;

	public static ConfigEntry<string> clockFuzzyStrings;

	public static ConfigEntry<int> nexusID;

	private static Font clockFont;

	private static GUIStyle style;

	private static GUIStyle style2;

	private static bool configApplied = false;

	private static Vector2 clockPosition;

	private static float shownTime = 0f;

	private static string lastTimeString = "";

	private static Rect windowRect;

	private string newTimeString;

	private static Rect timeRect;

	private void Awake()
	{
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 85, "Nexus mod ID for updates");
		toggleClockKeyMod = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ShowClockKeyMod", "", "Extra modifier key used to toggle the clock display. Leave blank to not require one. Use https://docs.unity3d.com/Manual/ConventionalGameInput.html");
		toggleClockKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ShowClockKey", "home", "Key used to toggle the clock display. use https://docs.unity3d.com/Manual/ConventionalGameInput.html");
		clockLocationString = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ClockLocationString", "50%,3%", "Location on the screen to show the clock (x,y) or (x%,y%)");
		clockLocationString.SettingChanged += ClockLocationString_SettingChanged;
		LoadConfig();
		if (modEnabled.Value)
		{
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}

	private void ClockLocationString_SettingChanged(object sender, EventArgs e)
	{
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: 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)
		string[] array = clockLocationString.Value.Split(new char[1] { ',' });
		clockPosition = new Vector2(array[0].Trim().EndsWith("%") ? (float.Parse(array[0].Trim().Substring(0, array[0].Trim().Length - 1)) / 100f * (float)Screen.width) : float.Parse(array[0].Trim()), array[1].Trim().EndsWith("%") ? (float.Parse(array[1].Trim().Substring(0, array[1].Trim().Length - 1)) / 100f * (float)Screen.height) : float.Parse(array[1].Trim()));
		windowRect = new Rect(clockPosition, new Vector2(1000f, 100f));
	}

	private string GetCurrentTimeString()
	{
		if (!Object.op_Implicit((Object)(object)EnvMan.instance))
		{
			return "";
		}
		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 now = DateTime.Now;
		DateTime theTime = new DateTime(now.Year, now.Month, now.Day, num2, num3, second);
		int value = Traverse.Create((object)EnvMan.instance).Method("GetCurrentDay", Array.Empty<object>()).GetValue<int>();
		return GetCurrentTimeString(theTime, num, value);
	}

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	public void LoadConfig()
	{
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_0277: Unknown result type (might be due to invalid IL or missing references)
		//IL_027c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0284: Unknown result type (might be due to invalid IL or missing references)
		//IL_0295: Unknown result type (might be due to invalid IL or missing references)
		//IL_029b: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ab: Expected O, but got Unknown
		//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_02df: Expected O, but got Unknown
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		showingClock = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowClock", true, "Show the clock?");
		showClockOnChange = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowClockOnChange", false, "Only show the clock when the time changes?");
		showClockOnChangeFadeTime = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ShowClockOnChangeFadeTime", 5f, "If only showing on change, length in seconds to show the clock before begining to fade");
		showClockOnChangeFadeLength = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ShowClockOnChangeFadeLength", 1f, "How long fade should take in seconds");
		clockUseOSFont = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ClockUseOSFont", false, "Set to true to specify the name of a font from your OS; otherwise limited to fonts in the game resources");
		clockUseShadow = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ClockUseShadow", false, "Add a shadow behind the text");
		clockShadowOffset = ((BaseUnityPlugin)this).Config.Bind<int>("General", "ClockShadowOffset", 2, "Shadow offset in pixels");
		clockFontName = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ClockFontName", "AveriaSerifLibre-Bold", "Name of the font to use");
		clockFontSize = ((BaseUnityPlugin)this).Config.Bind<int>("General", "ClockFontSize", 24, "Font size of clock text");
		clockFontColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "ClockFontColor", Color.white, "Font color for the clock");
		clockShadowColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "ClockShadowColor", Color.black, "Color for the shadow");
		toggleClockKeyMod = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ShowClockKeyMod", "", "Extra modifier key used to toggle the clock display. Leave blank to not require one. Use https://docs.unity3d.com/Manual/ConventionalGameInput.html");
		toggleClockKeyOnPress = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowClockKeyOnPress", false, "If true, limit clock display to when the hotkey is down");
		clockFormat = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ClockFormat", "HH:mm", "Time format; set to 'fuzzy' for fuzzy time");
		clockString = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ClockString", "<b>{0}</b>", "Formatted clock string - {0} is replaced by the actual time string, {1} is replaced by the fuzzy string, {2} is replaced by the current day");
		clockTextAlignment = ((BaseUnityPlugin)this).Config.Bind<TextAnchor>("General", "ClockTextAlignment", (TextAnchor)4, "Clock text alignment.");
		clockFuzzyStrings = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ClockFuzzyStrings", "Midnight,Early Morning,Early Morning,Before Dawn,Before Dawn,Dawn,Dawn,Morning,Morning,Late Morning,Late Morning,Midday,Midday,Early Afternoon,Early Afternoon,Afternoon,Afternoon,Evening,Evening,Night,Night,Late Night,Late Night,Midnight", "Fuzzy time strings to split up the day into custom periods if ClockFormat is set to 'fuzzy'; comma-separated");
		newTimeString = "";
		style = new GUIStyle
		{
			richText = true,
			fontSize = clockFontSize.Value,
			alignment = clockTextAlignment.Value
		};
		style2 = new GUIStyle
		{
			richText = true,
			fontSize = clockFontSize.Value,
			alignment = clockTextAlignment.Value
		};
	}

	private void Update()
	{
		if (modEnabled.Value && !AedenthornUtils.IgnoreKeyPresses() && !toggleClockKeyOnPress.Value && PressedToggleKey())
		{
			bool value = showingClock.Value;
			showingClock.Value = !value;
			((BaseUnityPlugin)this).Config.Save();
		}
	}

	private void OnGUI()
	{
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_017c: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01de: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0314: Unknown result type (might be due to invalid IL or missing references)
		//IL_0319: Unknown result type (might be due to invalid IL or missing references)
		//IL_026d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0282: Unknown result type (might be due to invalid IL or missing references)
		//IL_028c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0291: Unknown result type (might be due to invalid IL or missing references)
		//IL_029d: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b1: Expected O, but got Unknown
		//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b1: Unknown result type (might be due to invalid IL or missing references)
		if (modEnabled.Value && configApplied && Object.op_Implicit((Object)(object)Player.m_localPlayer) && Object.op_Implicit((Object)(object)Hud.instance))
		{
			float num = 1f;
			newTimeString = GetCurrentTimeString();
			if (showClockOnChange.Value)
			{
				if (newTimeString == lastTimeString)
				{
					shownTime = 0f;
					if (!toggleClockKeyOnPress.Value || !CheckKeyHeld(toggleClockKey.Value))
					{
						return;
					}
				}
				if (shownTime > showClockOnChangeFadeTime.Value)
				{
					if (shownTime > showClockOnChangeFadeTime.Value + showClockOnChangeFadeLength.Value)
					{
						shownTime = 0f;
						lastTimeString = newTimeString;
						if (!toggleClockKeyOnPress.Value || !CheckKeyHeld(toggleClockKey.Value))
						{
							return;
						}
					}
					num = (showClockOnChangeFadeLength.Value + showClockOnChangeFadeTime.Value - shownTime) / showClockOnChangeFadeLength.Value;
				}
				shownTime += Time.deltaTime;
			}
			style.normal.textColor = new Color(clockFontColor.Value.r, clockFontColor.Value.g, clockFontColor.Value.b, clockFontColor.Value.a * num);
			style2.normal.textColor = new Color(clockShadowColor.Value.r, clockShadowColor.Value.g, clockShadowColor.Value.b, clockShadowColor.Value.a * num);
			if (((!toggleClockKeyOnPress.Value && showingClock.Value) || (toggleClockKeyOnPress.Value && (showClockOnChange.Value || CheckKeyHeld(toggleClockKey.Value)))) && Traverse.Create((object)Hud.instance).Method("IsVisible", Array.Empty<object>()).GetValue<bool>())
			{
				GUI.backgroundColor = Color.clear;
				windowRect = GUILayout.Window(windowId, new Rect(((Rect)(ref windowRect)).position, ((Rect)(ref timeRect)).size), new WindowFunction(WindowBuilder), "", Array.Empty<GUILayoutOption>());
			}
		}
		if (!Input.GetKey((KeyCode)323) && (((Rect)(ref windowRect)).x != clockPosition.x || ((Rect)(ref windowRect)).y != clockPosition.y))
		{
			clockPosition = new Vector2(((Rect)(ref windowRect)).x, ((Rect)(ref windowRect)).y);
			clockLocationString.Value = $"{((Rect)(ref windowRect)).x},{((Rect)(ref windowRect)).y}";
			((BaseUnityPlugin)this).Config.Save();
		}
	}

	private void WindowBuilder(int id)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: 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_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		timeRect = GUILayoutUtility.GetRect(new GUIContent(newTimeString), style);
		GUI.DragWindow(timeRect);
		if (clockUseShadow.Value)
		{
			GUI.Label(new Rect(((Rect)(ref timeRect)).position + new Vector2((float)(-clockShadowOffset.Value), (float)clockShadowOffset.Value), ((Rect)(ref timeRect)).size), newTimeString, style2);
		}
		GUI.Label(timeRect, newTimeString, style);
	}

	private static void ApplyConfig()
	{
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: 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)
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: 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_01b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c3: Expected O, but got Unknown
		//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: 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)
		//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0203: Expected O, but got Unknown
		string[] array = clockLocationString.Value.Split(new char[1] { ',' });
		clockPosition = new Vector2(array[0].Trim().EndsWith("%") ? (float.Parse(array[0].Trim().Substring(0, array[0].Trim().Length - 1)) / 100f * (float)Screen.width) : float.Parse(array[0].Trim()), array[1].Trim().EndsWith("%") ? (float.Parse(array[1].Trim().Substring(0, array[1].Trim().Length - 1)) / 100f * (float)Screen.height) : float.Parse(array[1].Trim()));
		windowRect = new Rect(clockPosition, new Vector2(1000f, 100f));
		if (clockUseOSFont.Value)
		{
			clockFont = Font.CreateDynamicFontFromOSFont(clockFontName.Value, clockFontSize.Value);
		}
		else
		{
			Debug.Log((object)"getting fonts");
			Font[] array2 = Resources.FindObjectsOfTypeAll<Font>();
			Font[] array3 = array2;
			foreach (Font val in array3)
			{
				if (((Object)val).name == clockFontName.Value)
				{
					clockFont = val;
					Debug.Log((object)("got font " + ((Object)val).name));
					break;
				}
			}
		}
		style = new GUIStyle
		{
			richText = true,
			fontSize = clockFontSize.Value,
			alignment = clockTextAlignment.Value,
			font = clockFont
		};
		style2 = new GUIStyle
		{
			richText = true,
			fontSize = clockFontSize.Value,
			alignment = clockTextAlignment.Value,
			font = clockFont
		};
		configApplied = true;
	}

	private string GetCurrentTimeString(DateTime theTime, float fraction, int days)
	{
		string[] array = clockFuzzyStrings.Value.Split(new char[1] { ',' });
		int num = Math.Min((int)((float)array.Length * fraction), array.Length - 1);
		try
		{
			if (clockFormat.Value == "fuzzy")
			{
				return string.Format(clockString.Value, array[num]);
			}
			return string.Format(clockString.Value, theTime.ToString(clockFormat.Value), array[num], days.ToString());
		}
		catch
		{
			return clockString.Value.Replace("{0}", theTime.ToString(clockFormat.Value)).Replace("{1}", array[num]).Replace("{2}", days.ToString());
		}
	}

	private static string GetFuzzyFileName(string lang)
	{
		return ((BaseUnityPlugin)context).Info.Location.Replace("ClockMod.dll", "") + $"clockmod.lang.{lang}.cfg";
	}

	private static bool CheckKeyHeld(string value)
	{
		try
		{
			return Input.GetKey(value.ToLower());
		}
		catch
		{
			return true;
		}
	}

	private bool PressedToggleKey()
	{
		try
		{
			return Input.GetKeyDown(toggleClockKey.Value.ToLower()) && CheckKeyHeld(toggleClockKeyMod.Value);
		}
		catch
		{
			return false;
		}
	}
}