Decompiled source of CustomCalendarPlugin v1.6.0

CustomCalendarPlugin.dll

Decompiled 2 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 Bounce.Singletons;
using ModdingTales;
using Newtonsoft.Json;
using Talespire;
using Unity.Mathematics;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CustomCalendarPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CustomCalendarPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("CustomCalendarPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.6.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.6.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.customcalendar", "Custom Calendar Plug-In", "1.6.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class CustomCalendarPlugin : BaseUnityPlugin
{
	public static class Utility
	{
		public static void PostOnMainPage(BaseUnityPlugin plugin)
		{
			string text = "Lord Ashes" + ("Lord Ashes".ToUpper().EndsWith("S") ? "'" : "'s");
			ModdingUtils.AddPluginToMenuList(plugin, text);
		}

		public static bool isBoardLoaded()
		{
			return SimpleSingletonBehaviour<CameraController>.HasInstance && SingletonStateMBehaviour<BoardSessionManager, State<BoardSessionManager>>.HasInstance && !BoardSessionManager.IsLoading;
		}
	}

	public const string Name = "Custom Calendar Plug-In";

	public const string Guid = "org.lordashes.plugins.customcalendar";

	public const string Version = "1.6.0.0";

	public const string Author = "Lord Ashes";

	public static bool displayTime = true;

	public static int minutesPerHour = 60;

	public static int hoursPerDay = 24;

	public static ConfigEntry<int> currentMinute;

	public static ConfigEntry<int> currentHour;

	public static ConfigEntry<int> currentDay;

	public static ConfigEntry<int> currentMonth;

	public static ConfigEntry<int> calendarPosX;

	public static ConfigEntry<int> calendarPosY;

	public static ConfigEntry<int> calendarWidth;

	public static ConfigEntry<int> calendarHeight;

	public static Texture2D bannerTexture = Image.LoadTexture("Banner.png", (CacheType)999);

	private GUIStyle guiStyle = new GUIStyle();

	private List<List<string>> calendar = JsonConvert.DeserializeObject<List<List<string>>>(File.ReadAllText("Calendar.json", (CacheType)999));

	private ConfigEntry<KeyboardShortcut> advanceDayTriggerKey { get; set; }

	private ConfigEntry<KeyboardShortcut> receedDayTriggerKey { get; set; }

	private ConfigEntry<KeyboardShortcut> advanceMinutesTriggerKey { get; set; }

	private ConfigEntry<KeyboardShortcut> setDateTimeTriggerKey { get; set; }

	private string ChatHandler(string chatMessage, string sender, SourceRole source)
	{
		Debug.Log((object)("Custom Calendar Plug-In: Received Calendar Update (" + chatMessage + ")"));
		SetCalendar(chatMessage.Replace("/campaigndate", "").Trim());
		return null;
	}

	private void SetCalendar(string Calendar)
	{
		try
		{
			string[] array = Calendar.Split(new char[1] { ':' });
			currentMonth.Value = int.Parse(array[0]);
			currentDay.Value = int.Parse(array[1]);
			currentHour.Value = int.Parse(array[2]);
			currentMinute.Value = int.Parse(array[3]);
			Debug.Log((object)("Custom Calendar Plug-In: Date/Time Now " + currentMonth.Value + ":" + currentDay.Value + ":" + currentHour.Value + ":" + currentMinute.Value));
		}
		catch (Exception)
		{
			Debug.LogWarning((object)("Custom Calendar Plug-In: Error Setting Date/Time '" + Calendar + "'"));
		}
		((BaseUnityPlugin)this).Config.Save();
	}

	private void SendCalendar(int currentMonth, int currentDay, int currentHour, int currentMinute)
	{
		//IL_00b6: 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)
		Debug.Log((object)("Custom Calendar Plug-In: Broadcast Calendar Update (" + currentMonth + ":" + currentDay + ":" + currentHour + ":" + currentMinute + ")"));
		ChatManager.SendChatMessageToBoard("/campaigndate " + currentMonth + ":" + currentDay + ":" + currentHour + ":" + currentMinute, LocalClient.Id.Value, (float3?)null, false);
	}

	private void Awake()
	{
		//IL_0018: 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_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Unknown result type (might be due to invalid IL or missing references)
		LoggingPlugin.SetLogLevel(((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", (DiagnosticLevel)3, (ConfigDescription)null).Value);
		string? assemblyQualifiedName = ((object)this).GetType().AssemblyQualifiedName;
		DiagnosticLevel logLevel = LoggingPlugin.GetLogLevel();
		Debug.Log((object)(assemblyQualifiedName + ": Active. (Diagnostic Mode = " + ((object)(DiagnosticLevel)(ref logLevel)).ToString() + ")"));
		displayTime = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Display Time", true, (ConfigDescription)null).Value;
		minutesPerHour = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Minutes Per Hour", 60, (ConfigDescription)null).Value;
		hoursPerDay = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Hours Per Day", 24, (ConfigDescription)null).Value;
		advanceMinutesTriggerKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Adjust Minutes", new KeyboardShortcut((KeyCode)109, (KeyCode[])(object)new KeyCode[1] { (KeyCode)303 }), (ConfigDescription)null);
		advanceDayTriggerKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Add Day", new KeyboardShortcut((KeyCode)97, (KeyCode[])(object)new KeyCode[1] { (KeyCode)303 }), (ConfigDescription)null);
		receedDayTriggerKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Subtract Day", new KeyboardShortcut((KeyCode)115, (KeyCode[])(object)new KeyCode[1] { (KeyCode)303 }), (ConfigDescription)null);
		setDateTimeTriggerKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Set Date And Time", new KeyboardShortcut((KeyCode)100, (KeyCode[])(object)new KeyCode[1] { (KeyCode)303 }), (ConfigDescription)null);
		guiStyle.alignment = (TextAnchor)4;
		guiStyle.normal.textColor = Color.black;
		guiStyle.fontSize = ((BaseUnityPlugin)this).Config.Bind<int>("Hotkeys", "Display Font Size", 16, (ConfigDescription)null).Value;
		guiStyle.fontStyle = (FontStyle)1;
		currentMonth = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Current Month", 1, (ConfigDescription)null);
		currentDay = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Current Day", 1, (ConfigDescription)null);
		currentHour = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Current Hour", 0, (ConfigDescription)null);
		currentMinute = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Current Minute", 0, (ConfigDescription)null);
		calendarPosX = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Calendar Position Left", 0, (ConfigDescription)null);
		calendarPosY = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Calendar Position Top", 60, (ConfigDescription)null);
		calendarWidth = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Calendar Width", 1920, (ConfigDescription)null);
		calendarHeight = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Calendar Height", 47, (ConfigDescription)null);
		if (calendarWidth.Value < 0)
		{
			calendarWidth.Value = ((Texture)bannerTexture).width;
		}
		if (calendarHeight.Value < 0)
		{
			calendarHeight.Value = ((Texture)bannerTexture).height;
		}
		LoggingPlugin.LogInfo("Custom Calendar Plug-In: Initalized To Month=" + currentMonth.Value + ", Day=" + currentDay.Value + ", Hour=" + currentHour.Value + ", Minute=" + currentMinute.Value);
		ConformAndDistributeCalendar();
		ChatMessageService.AddHandler("/campaigndate", (Func<string, string, SourceRole, string>)ChatHandler);
		Utility.PostOnMainPage((BaseUnityPlugin)(object)this);
	}

	private void Update()
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: 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_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		if (!Utility.isBoardLoaded())
		{
			return;
		}
		KeyboardShortcut value = advanceMinutesTriggerKey.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			SystemMessage.AskForTextInput("Time Adjustment", "Adjust Minutes By (e.g. +05):", "OK", (Action<string>)delegate(string strMinutes)
			{
				int result = 0;
				if (int.TryParse(strMinutes, out result))
				{
					currentMinute.Value += result;
				}
				ConformAndDistributeCalendar();
			}, (Action)null, "Cancel", (Action)null, "+60");
		}
		value = setDateTimeTriggerKey.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			SystemMessage.AskForTextInput("Time Adjustment", "Specify The Month:Date:Hour:Minute (e.g. 05:10:30:00):", "OK", (Action<string>)delegate(string strDateTime)
			{
				SetCalendar(strDateTime);
				ConformAndDistributeCalendar();
			}, (Action)null, "Cancel", (Action)null, "00:00:00:00");
		}
		value = advanceDayTriggerKey.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			ConfigEntry<int> obj = currentDay;
			int value2 = obj.Value;
			obj.Value = value2 + 1;
			currentHour.Value = 0;
			currentMinute.Value = 0;
			ConformAndDistributeCalendar();
		}
		value = receedDayTriggerKey.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			ConfigEntry<int> obj2 = currentDay;
			int value2 = obj2.Value;
			obj2.Value = value2 - 1;
			currentHour.Value = 0;
			currentMinute.Value = 0;
			ConformAndDistributeCalendar();
		}
	}

	private void OnGUI()
	{
		//IL_007c: 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_00c9: Unknown result type (might be due to invalid IL or missing references)
		if (!Utility.isBoardLoaded())
		{
			return;
		}
		try
		{
			ConformAndDistributeCalendar();
			if (calendar.ElementAt(currentMonth.Value - 1).ElementAt(currentDay.Value - 1) != "")
			{
				GUI.DrawTexture(new Rect((float)calendarPosX.Value, (float)calendarPosY.Value, (float)calendarWidth.Value, (float)calendarHeight.Value), (Texture)(object)bannerTexture);
				if (displayTime)
				{
					GUI.Label(new Rect((float)calendarPosX.Value, (float)(calendarPosY.Value + 2), (float)calendarWidth.Value, (float)(calendarHeight.Value - 2)), calendar.ElementAt(currentMonth.Value - 1).ElementAt(currentDay.Value - 1) + " " + currentHour.Value.ToString("d2") + ":" + currentMinute.Value.ToString("d2"), guiStyle);
				}
				else
				{
					GUI.Label(new Rect((float)calendarPosX.Value, (float)(calendarPosY.Value + 2), (float)calendarWidth.Value, (float)(calendarHeight.Value - 2)), calendar.ElementAt(currentMonth.Value - 1).ElementAt(currentDay.Value - 1), guiStyle);
				}
			}
		}
		catch (Exception ex)
		{
			LoggingPlugin.LogWarning("Custom Calendar Plug-In: Trying To Render Date Month=" + currentMonth.Value + " Day=" + currentDay.Value + " Hour=" + currentHour.Value + " Minute=" + currentMinute.Value + " At " + calendarPosX.Value + "," + calendarPosY.Value + " (" + calendarWidth.Value + "," + calendarHeight.Value + "). Exception: " + ex);
		}
	}

	private bool ConformAndDistributeCalendar()
	{
		bool flag = false;
		while (currentMinute.Value < 0)
		{
			flag = true;
			currentMinute.Value += minutesPerHour;
			ConfigEntry<int> obj = currentHour;
			int value = obj.Value;
			obj.Value = value - 1;
		}
		while (currentMinute.Value >= minutesPerHour)
		{
			flag = true;
			currentMinute.Value -= minutesPerHour;
			ConfigEntry<int> obj2 = currentHour;
			int value = obj2.Value;
			obj2.Value = value + 1;
		}
		while (currentHour.Value < 0)
		{
			flag = true;
			ConfigEntry<int> obj3 = currentDay;
			int value = obj3.Value;
			obj3.Value = value - 1;
			currentHour.Value -= hoursPerDay;
		}
		while (currentHour.Value >= hoursPerDay)
		{
			flag = true;
			currentHour.Value -= 60;
			ConfigEntry<int> obj4 = currentDay;
			int value = obj4.Value;
			obj4.Value = value - 1;
		}
		while (currentDay.Value < 1)
		{
			flag = true;
			currentMonth.Value -= 1;
			currentDay.Value += calendar.ElementAt(currentMonth.Value - 1).Count;
		}
		while (currentDay.Value > calendar.ElementAt(currentMonth.Value - 1).Count)
		{
			flag = true;
			currentDay.Value -= calendar.ElementAt(currentMonth.Value - 1).Count;
			ConfigEntry<int> obj5 = currentMonth;
			int value = obj5.Value;
			obj5.Value = value + 1;
		}
		if (flag)
		{
			LoggingPlugin.LogWarning("Custom Calendar Plug-In: Adjusted Calendar To Month=" + currentMonth.Value + " Day=" + currentDay.Value + " Hour=" + currentHour.Value + " Minute=" + currentMinute.Value);
			SendCalendar(currentMonth.Value, currentDay.Value, currentHour.Value, currentMinute.Value);
			return true;
		}
		return false;
	}
}